ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.229 by root, Wed May 24 21:49:58 2006 UTC vs.
Revision 1.257 by root, Tue May 30 02:22:02 2006 UTC

5 5
6use Scalar::Util (); 6use Scalar::Util ();
7use List::Util (); 7use List::Util ();
8 8
9use CFClient; 9use CFClient;
10use CFClient::Texture;
10 11
11our ($FOCUS, $HOVER, $GRAB); # various widgets 12our ($FOCUS, $HOVER, $GRAB); # various widgets
12 13
14our $LAYOUT;
13our $ROOT; 15our $ROOT;
14our $TOOLTIP; 16our $TOOLTIP;
15our $BUTTON_STATE; 17our $BUTTON_STATE;
16 18
17our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20
21sub get_layout {
22 my $layout;
23
24 for (grep { $_->{name} } values %WIDGET) {
25 my $win = $layout->{$_->{name}} = { };
26
27 $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/;
28 $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/;
29 $win->{w} = $_->{w} / $::WIDTH if defined $_->{w};
30 $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h};
31 }
32
33 $layout
34}
35
36sub set_layout {
37 my ($layout) = @_;
38 $LAYOUT = $layout;
39}
18 40
19sub check_tooltip { 41sub check_tooltip {
20 if (!$GRAB) { 42 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 43 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 44 if (length $widget->{tooltip}) {
23 45
24 if ($TOOLTIP->{owner} != $widget) { 46 if ($TOOLTIP->{owner} != $widget) {
47 $TOOLTIP->hide;
48
25 $TOOLTIP->{owner} = $widget; 49 $TOOLTIP->{owner} = $widget;
26 50
27 my $tip = $widget->{tooltip}; 51 my $tip = $widget->{tooltip};
28 52
29 $tip = $tip->($widget) if CODE:: eq ref $tip; 53 $tip = $tip->($widget) if CODE:: eq ref $tip;
30 54
31 $TOOLTIP->set_tooltip_from ($widget); 55 $TOOLTIP->set_tooltip_from ($widget);
32 $TOOLTIP->show; 56 $TOOLTIP->show;
33
34 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
35
36 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
37 if $x + $TOOLTIP->{w} > $::WIDTH;
38
39 $TOOLTIP->move ($x, $y);
40 $TOOLTIP->check_size;
41 $TOOLTIP->update;
42 } 57 }
43 58
44 return; 59 return;
45 } 60 }
46 } 61 }
50 delete $TOOLTIP->{owner}; 65 delete $TOOLTIP->{owner};
51} 66}
52 67
53# class methods for events 68# class methods for events
54sub feed_sdl_key_down_event { 69sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 70 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 71 if $FOCUS;
57} 72}
58 73
59sub feed_sdl_key_up_event { 74sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 75 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 76 if $FOCUS;
62} 77}
63 78
64sub feed_sdl_button_down_event { 79sub feed_sdl_button_down_event {
65 my ($ev) = @_; 80 my ($ev) = @_;
74 check_tooltip; 89 check_tooltip;
75 } 90 }
76 91
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 92 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 93
79 if ($GRAB) { 94 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 95 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 96}
84 97
85sub feed_sdl_button_up_event { 98sub feed_sdl_button_up_event {
86 my ($ev) = @_; 99 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 100 my ($x, $y) = ($ev->{x}, $ev->{y});
88 101
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 102 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 103
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 104 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 105
93 if ($GRAB) { 106 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 107 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 108
98 if (!$BUTTON_STATE) { 109 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 110 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 111 $grab->update if $grab;
101 $GRAB->update if $GRAB; 112 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 128 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 129
119 check_tooltip; 130 check_tooltip;
120 } 131 }
121 132
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 133 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 134 if $HOVER;
126} 135}
127 136
128# convert position array to integers 137# convert position array to integers
129sub harmonize { 138sub harmonize {
130 my ($vals) = @_; 139 my ($vals) = @_;
144 153
145 $_->update 154 $_->update
146 for @widgets; 155 for @widgets;
147} 156}
148 157
158sub reconfigure_widgets {
159 # make a copy, otherwise C<for> complains about freed values.
160 my @widgets = values %WIDGET;
161
162 $_->reconfigure
163 for @widgets;
164}
165
149# call when resolution changes etc. 166# call when resolution changes etc.
150sub rescale_widgets { 167sub rescale_widgets {
151 my ($sx, $sy) = @_; 168 my ($sx, $sy) = @_;
152 169
153 # make a copy, otherwise for complains about freed values.
154 my @widgets = values %WIDGET; 170 for my $widget (values %WIDGET) {
155
156 for my $widget (@widgets) {
157 if ($widget->{toplevel}) { 171 if ($widget->{is_toplevel}) {
172 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
173 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
174
158 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 175 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
159 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 176 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
160 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 177 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
161 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
162 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 178 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
163 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 179 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
164 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 180 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
165 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; 181
182 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
183 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
184
166 } 185 }
167
168 $widget->reconfigure;
169 } 186 }
187
188 reconfigure_widgets;
170} 189}
171 190
172############################################################################# 191#############################################################################
173 192
174package CFClient::UI::Base; 193package CFClient::UI::Base;
179 198
180sub new { 199sub new {
181 my $class = shift; 200 my $class = shift;
182 201
183 my $self = bless { 202 my $self = bless {
184 x => 0, 203 x => "center",
185 y => 0, 204 y => "center",
186 z => 0, 205 z => 0,
206 w => undef,
207 h => undef,
187 can_events => 1, 208 can_events => 1,
188 @_ 209 @_
189 }, $class; 210 }, $class;
190 211
212 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
213 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
214 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
215 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
216 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
217
218 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
219 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
220 }
221
222 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
223
191 for (keys %$self) { 224 for (keys %$self) {
192 if (/^connect_(.*)$/) { 225 if (/^on_(.*)$/) {
193 $self->connect ($1 => delete $self->{$_}); 226 $self->connect ($1 => delete $self->{$_});
194 } 227 }
195 } 228 }
196 229
197 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
198
199 $self 230 $self
200} 231}
201 232
202sub destroy { 233sub destroy {
203 my ($self) = @_; 234 my ($self) = @_;
206 %$self = (); 237 %$self = ();
207} 238}
208 239
209sub show { 240sub show {
210 my ($self) = @_; 241 my ($self) = @_;
242
211 return if $self->{parent}; 243 return if $self->{parent};
212 244
213 $CFClient::UI::ROOT->add ($self); 245 $CFClient::UI::ROOT->add ($self);
214} 246}
215 247
216sub show_centered { 248sub set_visible {
217 my ($self) = @_; 249 my ($self) = @_;
250
218 return if $self->{parent}; 251 return if $self->{visible};
219 252
220 $self->show; 253 $self->{root} = $self->{parent}{root};
254 $self->{visible} = $self->{parent}{visible} + 1;
221 255
222 $CFClient::UI::ROOT->on_post_alloc ( 256 $self->emit (visibility_change => 1);
223 "centered $self" => sub {
224 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
225 },
226 );
227}
228 257
229sub hide { 258 $self->realloc if !exists $self->{req_w};
259
260 $_->set_visible for $self->children;
261}
262
263sub set_invisible {
230 my ($self) = @_; 264 my ($self) = @_;
265
266 return unless $self->{visible};
267
268 $_->set_invisible for $self->children;
269
270 delete $self->{root};
271 delete $self->{visible};
231 272
232 undef $GRAB if $GRAB == $self; 273 undef $GRAB if $GRAB == $self;
233 undef $HOVER if $HOVER == $self; 274 undef $HOVER if $HOVER == $self;
234 275
276 CFClient::UI::check_tooltip
277 if $TOOLTIP->{owner} == $self;
278
279 $self->focus_out;
280
281 $self->emit (visibility_change => 0);
282}
283
284sub set_visibility {
285 my ($self, $visible) = @_;
286
287 return if $self->{visible} == $visible;
288
289 $visible ? $self->hide
290 : $self->show;
291}
292
293sub toggle_visibility {
294 my ($self) = @_;
295
296 $self->{visible}
297 ? $self->hide
298 : $self->show;
299}
300
301sub hide {
302 my ($self) = @_;
303
304 $self->set_invisible;
305
235 $self->{parent}->remove ($self) 306 $self->{parent}->remove ($self)
236 if $self->{parent}; 307 if $self->{parent};
237} 308}
238 309
239sub move { 310sub move_abs {
240 my ($self, $x, $y, $z) = @_; 311 my ($self, $x, $y, $z) = @_;
241 312
242 $self->{x} = int $x; 313 $self->{x} = List::Util::max 0, int $x;
243 $self->{y} = int $y; 314 $self->{y} = List::Util::max 0, int $y;
244 $self->{z} = $z if defined $z; 315 $self->{z} = $z if defined $z;
245 316
246 $self->update; 317 $self->update;
247} 318}
248 319
249sub set_size { 320sub set_size {
250 my ($self, $w, $h) = @_; 321 my ($self, $w, $h) = @_;
251 322
252 $self->{user_w} = $w; 323 $self->{force_w} = $w;
253 $self->{user_h} = $h; 324 $self->{force_h} = $h;
254 325
255 $self->check_size; 326 $self->realloc;
256} 327}
257 328
258sub size_request { 329sub size_request {
259 require Carp; 330 require Carp;
260 Carp::confess "size_request is abstract"; 331 Carp::confess "size_request is abstract";
262 333
263sub configure { 334sub configure {
264 my ($self, $x, $y, $w, $h) = @_; 335 my ($self, $x, $y, $w, $h) = @_;
265 336
266 if ($self->{aspect}) { 337 if ($self->{aspect}) {
338 my ($ow, $oh) = ($w, $h);
339
267 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 340 $w = List::Util::min $w, int $h * $self->{aspect};
268 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 341 $h = List::Util::min $h, int $w / $self->{aspect};
269 342
270 # use alignment to adjust x, y 343 # use alignment to adjust x, y
271 344
272 $x += int +($w - $w2) * 0.5; 345 $x += int 0.5 * ($ow - $w);
273 $y += int +($h - $h2) * 0.5; 346 $y += int 0.5 * ($oh - $h);
274
275 ($w, $h) = ($w2, $h2);
276 } 347 }
277 348
278 if ($self->{x} != $x || $self->{y} != $y) { 349 if ($self->{x} ne $x || $self->{y} ne $y) {
279 $self->{x} = $x; 350 $self->{x} = $x;
280 $self->{y} = $y; 351 $self->{y} = $y;
281 $self->update; 352 $self->update;
282 } 353 }
283 354
284 if ($self->{w} != $w || $self->{h} != $h) { 355 if ($self->{w} != $w || $self->{h} != $h) {
356 return unless $self->{visible};
357
285 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 358 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
286 } 359 }
287} 360}
288 361
289sub size_allocate { 362sub size_allocate {
290 # nothing to be done 363 # nothing to be done
291} 364}
292 365
293sub reconfigure {
294 my ($self) = @_;
295
296 $self->check_size (1);
297 $self->update;
298}
299
300sub children { 366sub children {
301} 367}
302 368
303sub set_max_size { 369sub set_max_size {
304 my ($self, $w, $h) = @_; 370 my ($self, $w, $h) = @_;
307 delete $self->{max_h}; $self->{max_h} = $h if $h; 373 delete $self->{max_h}; $self->{max_h} = $h if $h;
308} 374}
309 375
310sub set_tooltip { 376sub set_tooltip {
311 my ($self, $tooltip) = @_; 377 my ($self, $tooltip) = @_;
378
379 $tooltip =~ s/^\s+//;
380 $tooltip =~ s/\s+$//;
381
382 return if $self->{tooltip} eq $tooltip;
312 383
313 $self->{tooltip} = $tooltip; 384 $self->{tooltip} = $tooltip;
314 385
315 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 386 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
316 delete $CFClient::UI::TOOLTIP->{owner}; 387 delete $CFClient::UI::TOOLTIP->{owner};
338 return if $FOCUS == $self; 409 return if $FOCUS == $self;
339 return unless $self->{can_focus}; 410 return unless $self->{can_focus};
340 411
341 my $focus = $FOCUS; $FOCUS = $self; 412 my $focus = $FOCUS; $FOCUS = $self;
342 413
343 $self->emit (focus_in => $focus); 414 $self->_emit (focus_in => $focus);
344 415
345 $focus->update if $focus; 416 $focus->update if $focus;
346 $FOCUS->update; 417 $FOCUS->update;
347} 418}
348 419
351 422
352 return unless $FOCUS == $self; 423 return unless $FOCUS == $self;
353 424
354 my $focus = $FOCUS; undef $FOCUS; 425 my $focus = $FOCUS; undef $FOCUS;
355 426
356 $self->emit (focus_out => $focus); 427 $self->_emit (focus_out => $focus);
357 428
358 $focus->update if $focus; #? 429 $focus->update if $focus; #?
430
431 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
432 unless $FOCUS;
359} 433}
360 434
361sub mouse_motion { } 435sub mouse_motion { }
362sub button_up { } 436sub button_up { }
363sub key_down { } 437sub key_down { }
364sub key_up { } 438sub key_up { }
365 439
366sub button_down { 440sub button_down {
367 my ($self, $ev, $x, $y) = @_; 441 my ($self, $ev, $x, $y) = @_;
368 442
369 $self->focus_in; 443 $self->focus_in;
372sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 446sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
373sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 447sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
374sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 448sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
375sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 449sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
376sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 450sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
451
452sub find_widget {
453 my ($self, $x, $y) = @_;
454
455 return () unless $self->{can_events};
456
457 return $self
458 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
459 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
460
461 ()
462}
463
464sub set_parent {
465 my ($self, $parent) = @_;
466
467 Scalar::Util::weaken ($self->{parent} = $parent);
468
469 $self->set_visible if $parent->{visible};
470}
471
472sub connect {
473 my ($self, $signal, $cb) = @_;
474
475 push @{ $self->{signal_cb}{$signal} }, $cb;
476}
477
478sub _emit {
479 my ($self, $signal, @args) = @_;
480
481 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
482}
483
484sub emit {
485 my ($self, $signal, @args) = @_;
486
487 $self->_emit ($signal, @args)
488 || $self->$signal (@args);
489}
490
491sub visibility_change {
492 #my ($self, $visible) = @_;
493}
494
495sub realloc {
496 my ($self) = @_;
497
498 if ($self->{visible}) {
499 return if $self->{root}{realloc}{$self};
500
501 $self->{root}{realloc}{$self} = $self;
502 $self->{root}->update;
503 } else {
504 delete $self->{req_w};
505 }
506}
507
508sub update {
509 my ($self) = @_;
510
511 $self->{parent}->update
512 if $self->{parent};
513}
514
515sub reconfigure {
516 my ($self) = @_;
517
518 $self->realloc;
519 $self->update;
520}
377 521
378sub draw { 522sub draw {
379 my ($self) = @_; 523 my ($self) = @_;
380 524
381 return unless $self->{h} && $self->{w}; 525 return unless $self->{h} && $self->{w};
398 glVertex $x , $y + $self->{h}; 542 glVertex $x , $y + $self->{h};
399 glEnd; 543 glEnd;
400 glDisable GL_BLEND; 544 glDisable GL_BLEND;
401 } 545 }
402 546
403 if ($ENV{PCLIENT_DEBUG}) { 547 if ($ENV{CFPLUS_DEBUG}) {
404 glPushMatrix; 548 glPushMatrix;
405 glColor 1, 1, 0, 1; 549 glColor 1, 1, 0, 1;
406 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 550 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
407 glBegin GL_LINE_LOOP; 551 glBegin GL_LINE_LOOP;
408 glVertex 0 , 0; 552 glVertex 0 , 0;
409 glVertex $self->{w}, 0; 553 glVertex $self->{w} - 1, 0;
410 glVertex $self->{w}, $self->{h}; 554 glVertex $self->{w} - 1, $self->{h} - 1;
411 glVertex 0 , $self->{h}; 555 glVertex 0 , $self->{h} - 1;
412 glEnd; 556 glEnd;
413 glPopMatrix; 557 glPopMatrix;
414 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 558 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
415 } 559 }
416} 560}
417 561
418sub _draw { 562sub _draw {
419 my ($self) = @_; 563 my ($self) = @_;
420 564
421 warn "no draw defined for $self\n"; 565 warn "no draw defined for $self\n";
422}
423
424sub find_widget {
425 my ($self, $x, $y) = @_;
426
427 return () unless $self->{can_events};
428
429 return $self
430 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
431 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
432
433 ()
434}
435
436sub set_parent {
437 my ($self, $parent) = @_;
438
439 Scalar::Util::weaken ($self->{parent} = $parent);
440
441 # TODO: req_w _does_change after ->reconfigure
442 $self->check_size
443 unless exists $self->{req_w};
444
445 $self->show;
446}
447
448sub check_size {
449 my ($self, $forced) = @_;
450
451 $self->{force_alloc} = 1 if $forced;
452 $CFClient::UI::ROOT->{check_size}{$self} = $self;
453}
454
455sub update {
456 my ($self) = @_;
457
458 $self->{parent}->update
459 if $self->{parent};
460}
461
462sub connect {
463 my ($self, $signal, $cb) = @_;
464
465 push @{ $self->{signal_cb}{$signal} }, $cb;
466}
467
468sub emit {
469 my ($self, $signal, @args) = @_;
470
471 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
472} 566}
473 567
474sub DESTROY { 568sub DESTROY {
475 my ($self) = @_; 569 my ($self) = @_;
476 570
534 my ($class, %arg) = @_; 628 my ($class, %arg) = @_;
535 $class->SUPER::new (can_events => 0, %arg); 629 $class->SUPER::new (can_events => 0, %arg);
536} 630}
537 631
538sub size_request { 632sub size_request {
539 (0, 0) 633 my ($self) = @_;
634
635 ($self->{w} + 0, $self->{h} + 0)
540} 636}
541 637
542sub draw { } 638sub draw { }
543 639
544############################################################################# 640#############################################################################
573 $self->{children} = [ 669 $self->{children} = [
574 sort { $a->{z} <=> $b->{z} } 670 sort { $a->{z} <=> $b->{z} }
575 @{$self->{children}}, @widgets 671 @{$self->{children}}, @widgets
576 ]; 672 ];
577 673
578 $self->check_size (1); 674 $self->realloc;
579 $self->update;
580} 675}
581 676
582sub children { 677sub children {
583 @{ $_[0]{children} } 678 @{ $_[0]{children} }
584} 679}
589 delete $child->{parent}; 684 delete $child->{parent};
590 $child->hide; 685 $child->hide;
591 686
592 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 687 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
593 688
594 $self->check_size; 689 $self->realloc;
595 $self->update;
596} 690}
597 691
598sub clear { 692sub clear {
599 my ($self) = @_; 693 my ($self) = @_;
600 694
604 for (@$children) { 698 for (@$children) {
605 delete $_->{parent}; 699 delete $_->{parent};
606 $_->hide; 700 $_->hide;
607 } 701 }
608 702
609 $self->check_size; 703 $self->realloc;
610 $self->update;
611} 704}
612 705
613sub find_widget { 706sub find_widget {
614 my ($self, $x, $y) = @_; 707 my ($self, $x, $y) = @_;
615 708
668sub size_request { 761sub size_request {
669 $_[0]{children}[0]->size_request 762 $_[0]{children}[0]->size_request
670} 763}
671 764
672sub size_allocate { 765sub size_allocate {
673 my ($self, $w, $h) = @_; 766 my ($self, $w, $h, $changed) = @_;
674 767
675 $self->{children}[0]->configure (0, 0, $w, $h); 768 $self->{children}[0]->configure (0, 0, $w, $h);
676} 769}
677 770
678############################################################################# 771#############################################################################
695 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 788 $ROOT->on_post_alloc ($self => sub { $self->render_child });
696 $self->SUPER::update; 789 $self->SUPER::update;
697} 790}
698 791
699sub size_allocate { 792sub size_allocate {
700 my ($self, $w, $h) = @_; 793 my ($self, $w, $h, $changed) = @_;
701 794
702 $self->SUPER::size_allocate ($w, $h); 795 $self->SUPER::size_allocate ($w, $h, $changed);
703 $self->update; 796 $self->update
797 if $changed;
704} 798}
705 799
706sub _render { 800sub _render {
707 $_[0]{children}[0]->draw; 801 $_[0]{children}[0]->draw;
708} 802}
739 833
740package CFClient::UI::ViewPort; 834package CFClient::UI::ViewPort;
741 835
742our @ISA = CFClient::UI::Window::; 836our @ISA = CFClient::UI::Window::;
743 837
838sub new {
839 my $class = shift;
840
841 $class->SUPER::new (
842 scroll_x => 0,
843 scroll_y => 1,
844 @_,
845 )
846}
847
744sub size_request { 848sub size_request {
745 my ($self) = @_; 849 my ($self) = @_;
746 850
747 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 851 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
748 852
749 @$self{qw(child_w child_h)} 853 $w = 10 if $self->{scroll_x};
854 $h = 10 if $self->{scroll_y};
855
856 ($w, $h)
750} 857}
751 858
752sub size_allocate { 859sub size_allocate {
753 my ($self, $w, $h) = @_; 860 my ($self, $w, $h, $changed) = @_;
754 861
755 my ($cw, $ch) = @$self{qw(child_w child_h)}; 862 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
756# $w = $self->{w}; 863 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
864
757 $self->child->configure (0, 0, $cw, $ch); 865 $self->child->configure (0, 0, $w, $h);
758 $self->update; 866 $self->update;
759} 867}
760 868
761sub set_offset { 869sub set_offset {
762 my ($self, $x, $y) = @_; 870 my ($self, $x, $y) = @_;
813 my $class = shift; 921 my $class = shift;
814 922
815 my $self; 923 my $self;
816 924
817 my $slider = new CFClient::UI::Slider 925 my $slider = new CFClient::UI::Slider
818 vertical => 1, 926 vertical => 1,
819 range => [0, 0, 1, 0.01], # HACK fix 927 range => [0, 0, 1, 0.01], # HACK fix
820 connect_changed => sub { 928 on_changed => sub {
821 $self->{vp}->set_offset (0, $_[1]); 929 $self->{vp}->set_offset (0, $_[1]);
822 }, 930 },
823 ; 931 ;
824 932
825 $self = $class->SUPER::new ( 933 $self = $class->SUPER::new (
833 $self->add ($self->{slider}); 941 $self->add ($self->{slider});
834 942
835 $self 943 $self
836} 944}
837 945
946sub update {
947 my ($self) = @_;
948
949 $self->SUPER::update;
950
951 # todo: overwrite size_allocate of child
952 my $child = $self->{vp}->child;
953 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
954}
955
838sub size_allocate { 956sub size_allocate {
839 my ($self, $w, $h) = @_; 957 my ($self, $w, $h, $changed) = @_;
840 958
841 $self->SUPER::size_allocate ($w, $h); 959 $self->SUPER::size_allocate ($w, $h, $changed);
842 960
843 my $child = $self->{vp}->child; 961 my $child = $self->{vp}->child;
844 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 962 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
845} 963}
846 964
893 1011
894our @ISA = CFClient::UI::Bin::; 1012our @ISA = CFClient::UI::Bin::;
895 1013
896use CFClient::OpenGL; 1014use CFClient::OpenGL;
897 1015
898my @tex = 1016my $bg =
1017 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1018 mipmap => 1, wrap => 1;
1019
1020my @border =
899 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1021 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
900 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1022 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
901 1023
902sub new { 1024sub new {
903 my $class = shift; 1025 my $class = shift;
904 1026
905 # TODO: user_x, user_y, overwrite moveto?
906
907 my $self = $class->SUPER::new ( 1027 my $self = $class->SUPER::new (
908 bg => [1, 1, 1, 1], 1028 bg => [1, 1, 1, 1],
909 border_bg => [1, 1, 1, 1], 1029 border_bg => [1, 1, 1, 1],
910 border => 0.6, 1030 border => 0.6,
911 toplevel => 1,
912 can_events => 1, 1031 can_events => 1,
913 @_ 1032 @_
914 ); 1033 );
915 1034
916 $self->{title} &&= new CFClient::UI::Label 1035 $self->{title} &&= new CFClient::UI::Label
917 align => 0, 1036 align => 0,
936 $h + $self->border * 2, 1055 $h + $self->border * 2,
937 ) 1056 )
938} 1057}
939 1058
940sub size_allocate { 1059sub size_allocate {
941 my ($self, $w, $h) = @_; 1060 my ($self, $w, $h, $changed) = @_;
1061
1062 return unless $changed;
942 1063
943 $h -= List::Util::max 0, $self->border * 2; 1064 $h -= List::Util::max 0, $self->border * 2;
944 $w -= List::Util::max 0, $self->border * 2; 1065 $w -= List::Util::max 0, $self->border * 2;
945 1066
946 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1067 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
970 my ($ev, $x, $y) = @_; 1091 my ($ev, $x, $y) = @_;
971 1092
972 my $dx = $ev->{x} - $ox; 1093 my $dx = $ev->{x} - $ox;
973 my $dy = $ev->{y} - $oy; 1094 my $dy = $ev->{y} - $oy;
974 1095
975 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1096 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
976 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1097 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1098
1099 $self->realloc;
977 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1100 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
978 $self->check_size;
979 }; 1101 };
980 1102
981 } elsif ($lr ^ $td) { 1103 } elsif ($lr ^ $td) {
982 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1104 my ($ox, $oy) = ($ev->{x}, $ev->{y});
983 my ($bx, $by) = ($self->{x}, $self->{y}); 1105 my ($bx, $by) = ($self->{x}, $self->{y});
985 $self->{motion} = sub { 1107 $self->{motion} = sub {
986 my ($ev, $x, $y) = @_; 1108 my ($ev, $x, $y) = @_;
987 1109
988 ($x, $y) = ($ev->{x}, $ev->{y}); 1110 ($x, $y) = ($ev->{x}, $ev->{y});
989 1111
990 $self->move ($bx + $x - $ox, $by + $y - $oy); 1112 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
991 $self->update;
992 }; 1113 };
993 } 1114 }
994} 1115}
995 1116
996sub button_up { 1117sub button_up {
1015 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1136 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1016 1137
1017 my $border = $self->border; 1138 my $border = $self->border;
1018 1139
1019 glColor @{ $self->{border_bg} }; 1140 glColor @{ $self->{border_bg} };
1020 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1141 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1021 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1142 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1022 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1143 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1023 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1144 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1024 1145
1025 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1146 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1026 my $bg = $tex[0]; 1147 glColor @{ $self->{bg} };
1027 1148
1028 # TODO: repeat texture not scale 1149 # TODO: repeat texture not scale
1150 # solve this better(?)
1029 my $rep_x = $cw / $bg->{w}; 1151 $bg->{s} = $cw / $bg->{w};
1030 my $rep_y = $ch / $bg->{h}; 1152 $bg->{t} = $ch / $bg->{h};
1031
1032 glColor @{ $self->{bg} };
1033
1034 $bg->{s} = $rep_x;
1035 $bg->{t} = $rep_y;
1036 $bg->{wrap_mode} = 1;
1037 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1153 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1038 } 1154 }
1039 1155
1040 glDisable GL_TEXTURE_2D; 1156 glDisable GL_TEXTURE_2D;
1041 1157
1057sub new { 1173sub new {
1058 my $class = shift; 1174 my $class = shift;
1059 1175
1060 $class->SUPER::new ( 1176 $class->SUPER::new (
1061 col_expand => [], 1177 col_expand => [],
1062 @_ 1178 @_,
1063 ) 1179 )
1180}
1181
1182sub children {
1183 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1064} 1184}
1065 1185
1066sub add { 1186sub add {
1067 my ($self, $x, $y, $child) = @_; 1187 my ($self, $x, $y, $child) = @_;
1068 1188
1069 $child->set_parent ($self); 1189 $child->set_parent ($self);
1070 $self->{children}[$y][$x] = $child; 1190 $self->{children}[$y][$x] = $child;
1071 1191
1072 $child->check_size; 1192 $self->realloc;
1073} 1193}
1074 1194
1075sub children {
1076 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1077}
1078
1079# TODO: move to container class maybe? send childs a signal on removal? 1195# TODO: move to container class maybe? send children a signal on removal?
1080sub clear { 1196sub clear {
1081 my ($self) = @_; 1197 my ($self) = @_;
1082 1198
1083 my @children = $self->children; 1199 my @children = $self->children;
1084 delete $self->{children}; 1200 delete $self->{children};
1086 for (@children) { 1202 for (@children) {
1087 delete $_->{parent}; 1203 delete $_->{parent};
1088 $_->hide; 1204 $_->hide;
1089 } 1205 }
1090 1206
1091 $self->update; 1207 $self->realloc;
1092} 1208}
1093 1209
1094sub get_wh { 1210sub get_wh {
1095 my ($self) = @_; 1211 my ($self) = @_;
1096 1212
1123 (sum @$hs), 1239 (sum @$hs),
1124 ) 1240 )
1125} 1241}
1126 1242
1127sub size_allocate { 1243sub size_allocate {
1128 my ($self, $w, $h) = @_; 1244 my ($self, $w, $h, $changed) = @_;
1129 1245
1130 my ($ws, $hs) = $self->get_wh; 1246 my ($ws, $hs) = $self->get_wh;
1131 1247
1132 my $req_w = sum @$ws; 1248 my $req_w = (sum @$ws) || 1;
1133 my $req_h = sum @$hs; 1249 my $req_h = (sum @$hs) || 1;
1134 1250
1135 # TODO: nicer code && do row_expand 1251 # TODO: nicer code && do row_expand
1136 my @col_expand = @{$self->{col_expand}}; 1252 my @col_expand = @{$self->{col_expand}};
1137 @col_expand = (1) x @$ws unless @col_expand; 1253 @col_expand = (1) x @$ws unless @col_expand;
1138 my $col_expand = (sum @col_expand) || 1; 1254 my $col_expand = (sum @col_expand) || 1;
1192 } 1308 }
1193} 1309}
1194 1310
1195############################################################################# 1311#############################################################################
1196 1312
1313package CFClient::UI::Box;
1314
1315our @ISA = CFClient::UI::Container::;
1316
1317sub size_request {
1318 my ($self) = @_;
1319
1320 $self->{vertical}
1321 ? (
1322 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1323 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1324 )
1325 : (
1326 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1327 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1328 )
1329}
1330
1331sub size_allocate {
1332 my ($self, $w, $h, $changed) = @_;
1333
1334 my $space = $self->{vertical} ? $h : $w;
1335 my $children = $self->{children};
1336
1337 my @req;
1338
1339 if ($self->{homogeneous}) {
1340 @req = ($space / (@$children || 1)) x @$children;
1341 } else {
1342 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1343 my $req = List::Util::sum @req;
1344
1345 if ($req > $space) {
1346 # ah well, not enough space
1347 $_ *= $space / $req for @req;
1348 } else {
1349 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1350
1351 $space = ($space - $req) / $expand; # remaining space to give away
1352
1353 $req[$_] += $space * $children->[$_]{expand}
1354 for 0 .. $#$children;
1355 }
1356 }
1357
1358 CFClient::UI::harmonize \@req;
1359
1360 my $pos = 0;
1361 for (0 .. $#$children) {
1362 my $alloc = $req[$_];
1363 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1364
1365 $pos += $alloc;
1366 }
1367
1368 1
1369}
1370
1371#############################################################################
1372
1197package CFClient::UI::HBox; 1373package CFClient::UI::HBox;
1198 1374
1199# TODO: wrap into common Box base class
1200
1201our @ISA = CFClient::UI::Container::; 1375our @ISA = CFClient::UI::Box::;
1202 1376
1203sub size_request { 1377sub new {
1204 my ($self) = @_; 1378 my $class = shift;
1205 1379
1206 my @alloc = map [$_->size_request], @{$self->{children}}; 1380 $class->SUPER::new (
1207 1381 vertical => 0,
1208 ( 1382 @_,
1209 (List::Util::sum map $_->[0], @alloc),
1210 (List::Util::max map $_->[1], @alloc),
1211 ) 1383 )
1212} 1384}
1213 1385
1214sub size_allocate {
1215 my ($self, $w, $h) = @_;
1216
1217 ($h, $w) = ($w, $h);
1218
1219 my $children = $self->{children};
1220
1221 my @h = map $_->{req_w}, @$children;
1222
1223 my $req_h = List::Util::sum @h;
1224
1225 if ($req_h > $h) {
1226 # ah well, not enough space
1227 $_ *= $h / $req_h for @h;
1228 } else {
1229 my $exp = List::Util::sum map $_->{expand}, @$children;
1230 $exp ||= 1;
1231
1232 for (0 .. $#$children) {
1233 my $child = $children->[$_];
1234
1235 my $alloc_h = $h[$_];
1236 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1237 $h[$_] = $alloc_h;
1238 }
1239 }
1240
1241 CFClient::UI::harmonize \@h;
1242
1243 my $y = 0;
1244 for (0 .. $#$children) {
1245 my $child = $children->[$_];
1246 my $h = $h[$_];
1247 $child->configure ($y, 0, $h, $w);
1248
1249 $y += $h;
1250 }
1251
1252 1
1253}
1254
1255############################################################################# 1386#############################################################################
1256 1387
1257package CFClient::UI::VBox; 1388package CFClient::UI::VBox;
1258 1389
1259# TODO: wrap into common Box base class
1260
1261our @ISA = CFClient::UI::Container::; 1390our @ISA = CFClient::UI::Box::;
1262 1391
1263sub size_request { 1392sub new {
1264 my ($self) = @_; 1393 my $class = shift;
1265 1394
1266 my @alloc = map [$_->size_request], @{$self->{children}}; 1395 $class->SUPER::new (
1267 1396 vertical => 1,
1268 ( 1397 @_,
1269 (List::Util::max map $_->[0], @alloc),
1270 (List::Util::sum map $_->[1], @alloc),
1271 ) 1398 )
1272}
1273
1274sub size_allocate {
1275 my ($self, $w, $h) = @_;
1276
1277 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1278
1279 my $children = $self->{children};
1280
1281 my @h = map $_->{req_h}, @$children;
1282
1283 my $req_h = List::Util::sum @h;
1284
1285 if ($req_h > $h) {
1286 # ah well, not enough space
1287 $_ *= $h / $req_h for @h;
1288 } else {
1289 my $exp = List::Util::sum map $_->{expand}, @$children;
1290 $exp ||= 1;
1291
1292 for (0 .. $#$children) {
1293 my $child = $children->[$_];
1294
1295 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1296 }
1297 }
1298
1299 CFClient::UI::harmonize \@h;
1300
1301 my $y = 0;
1302 for (0 .. $#$children) {
1303 my $child = $children->[$_];
1304 my $h = $h[$_];
1305 $child->configure (0, $y, $w, $h);
1306
1307 $y += $h;
1308 }
1309
1310 1
1311} 1399}
1312 1400
1313############################################################################# 1401#############################################################################
1314 1402
1315package CFClient::UI::Label; 1403package CFClient::UI::Label;
1378 $self->{text} = "T$text"; 1466 $self->{text} = "T$text";
1379 1467
1380 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1468 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1381 $self->{layout}->set_text ($text); 1469 $self->{layout}->set_text ($text);
1382 1470
1471 $self->realloc;
1383 $self->update; 1472 $self->update;
1384 $self->check_size;
1385} 1473}
1386 1474
1387sub set_markup { 1475sub set_markup {
1388 my ($self, $markup) = @_; 1476 my ($self, $markup) = @_;
1389 1477
1393 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1481 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1394 1482
1395 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1483 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1396 $self->{layout}->set_markup ($markup); 1484 $self->{layout}->set_markup ($markup);
1397 1485
1486 $self->realloc;
1398 $self->update; 1487 $self->update;
1399 $self->check_size;
1400} 1488}
1401 1489
1402sub size_request { 1490sub size_request {
1403 my ($self) = @_; 1491 my ($self) = @_;
1404 1492
1425 $h + $self->{padding} * 2, 1513 $h + $self->{padding} * 2,
1426 ) 1514 )
1427} 1515}
1428 1516
1429sub size_allocate { 1517sub size_allocate {
1430 my ($self, $w, $h) = @_; 1518 my ($self, $w, $h, $changed) = @_;
1431 1519
1432 delete $self->{texture}; 1520 delete $self->{texture}
1521 if $changed;
1433} 1522}
1434 1523
1435sub set_fontsize { 1524sub set_fontsize {
1436 my ($self, $fontsize) = @_; 1525 my ($self, $fontsize) = @_;
1437 1526
1438 $self->{fontsize} = $fontsize; 1527 $self->{fontsize} = $fontsize;
1439 delete $self->{texture}; 1528 delete $self->{texture};
1440 1529
1441 $self->update; 1530 $self->realloc;
1442 $self->check_size;
1443} 1531}
1444 1532
1445sub _draw { 1533sub _draw {
1446 my ($self) = @_; 1534 my ($self) = @_;
1447 1535
1519 $self->{text} = $text; 1607 $self->{text} = $text;
1520 1608
1521 $text =~ s/./*/g if $self->{hidden}; 1609 $text =~ s/./*/g if $self->{hidden};
1522 $self->{layout}->set_text ("$text "); 1610 $self->{layout}->set_text ("$text ");
1523 1611
1524 $self->emit (changed => $self->{text}); 1612 $self->_emit (changed => $self->{text});
1525} 1613}
1526 1614
1527sub set_text { 1615sub set_text {
1528 my ($self, $text) = @_; 1616 my ($self, $text) = @_;
1529 1617
1530 $self->{cursor} = length $text; 1618 $self->{cursor} = length $text;
1531 $self->_set_text ($text); 1619 $self->_set_text ($text);
1532 $self->update; 1620
1533 $self->check_size; 1621 $self->realloc;
1534} 1622}
1535 1623
1536sub get_text { 1624sub get_text {
1537 $_[0]{text} 1625 $_[0]{text}
1538} 1626}
1541 my ($self) = @_; 1629 my ($self) = @_;
1542 1630
1543 my ($w, $h) = $self->SUPER::size_request; 1631 my ($w, $h) = $self->SUPER::size_request;
1544 1632
1545 ($w + 1, $h) # add 1 for cursor 1633 ($w + 1, $h) # add 1 for cursor
1546}
1547
1548sub size_allocate {
1549 my ($self, $w, $h) = @_;
1550
1551 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1552} 1634}
1553 1635
1554sub key_down { 1636sub key_down {
1555 my ($self, $ev) = @_; 1637 my ($self, $ev) = @_;
1556 1638
1571 } elsif ($sym == CFClient::SDLK_HOME) { 1653 } elsif ($sym == CFClient::SDLK_HOME) {
1572 $self->{cursor} = 0; 1654 $self->{cursor} = 0;
1573 } elsif ($sym == CFClient::SDLK_END) { 1655 } elsif ($sym == CFClient::SDLK_END) {
1574 $self->{cursor} = length $text; 1656 $self->{cursor} = length $text;
1575 } elsif ($uni == 27) { 1657 } elsif ($uni == 27) {
1576 $self->emit ('escape'); 1658 $self->_emit ('escape');
1577 } elsif ($uni) { 1659 } elsif ($uni) {
1578 substr $text, $self->{cursor}++, 0, chr $uni; 1660 substr $text, $self->{cursor}++, 0, chr $uni;
1579 } 1661 }
1580 1662
1581 $self->_set_text ($text); 1663 $self->_set_text ($text);
1582 $self->update; 1664
1583 $self->check_size; 1665 $self->realloc;
1584} 1666}
1585 1667
1586sub focus_in { 1668sub focus_in {
1587 my ($self) = @_; 1669 my ($self) = @_;
1588 1670
1668 if ($sym == 13) { 1750 if ($sym == 13) {
1669 unshift @{$self->{history}}, 1751 unshift @{$self->{history}},
1670 my $txt = $self->get_text; 1752 my $txt = $self->get_text;
1671 $self->{history_pointer} = -1; 1753 $self->{history_pointer} = -1;
1672 $self->{history_saveback} = ''; 1754 $self->{history_saveback} = '';
1673 $self->emit (activate => $txt); 1755 $self->_emit (activate => $txt);
1674 $self->update; 1756 $self->update;
1675 1757
1676 } elsif ($sym == CFClient::SDLK_UP) { 1758 } elsif ($sym == CFClient::SDLK_UP) {
1677 if ($self->{history_pointer} < 0) { 1759 if ($self->{history_pointer} < 0) {
1678 $self->{history_saveback} = $self->get_text; 1760 $self->{history_saveback} = $self->get_text;
1726 can_events => 1, 1808 can_events => 1,
1727 @_ 1809 @_
1728 ) 1810 )
1729} 1811}
1730 1812
1813sub activate { }
1814
1731sub button_up { 1815sub button_up {
1732 my ($self, $ev, $x, $y) = @_; 1816 my ($self, $ev, $x, $y) = @_;
1733 1817
1818 $self->emit ("activate")
1734 if ($x >= 0 && $x < $self->{w} 1819 if $x >= 0 && $x < $self->{w}
1735 && $y >= 0 && $y < $self->{h}) { 1820 && $y >= 0 && $y < $self->{h};
1736 $self->emit ("activate");
1737 }
1738} 1821}
1739 1822
1740sub _draw { 1823sub _draw {
1741 my ($self) = @_; 1824 my ($self) = @_;
1742 1825
1794 my ($self, $ev, $x, $y) = @_; 1877 my ($self, $ev, $x, $y) = @_;
1795 1878
1796 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1879 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1797 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1880 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1798 $self->{state} = !$self->{state}; 1881 $self->{state} = !$self->{state};
1799 $self->emit (changed => $self->{state}); 1882 $self->_emit (changed => $self->{state});
1800 } 1883 }
1801} 1884}
1802 1885
1803sub _draw { 1886sub _draw {
1804 my ($self) = @_; 1887 my ($self) = @_;
2080 fg => [1, 1, 1], 2163 fg => [1, 1, 1],
2081 active_fg => [0, 0, 0], 2164 active_fg => [0, 0, 0],
2082 bg => [0, 0, 0, 0.2], 2165 bg => [0, 0, 0, 0.2],
2083 active_bg => [1, 1, 1, 0.5], 2166 active_bg => [1, 1, 1, 0.5],
2084 range => [0, 0, 100, 10, 0], 2167 range => [0, 0, 100, 10, 0],
2085 req_w => $::WIDTH / 80, 2168 min_w => $::WIDTH / 80,
2086 req_h => $::WIDTH / 80, 2169 min_h => $::WIDTH / 80,
2087 vertical => 0, 2170 vertical => 0,
2088 can_hover => 1, 2171 can_hover => 1,
2089 inner_pad => 0.02, 2172 inner_pad => 0.02,
2090 @_ 2173 @_
2091 ); 2174 );
2094 $self->update; 2177 $self->update;
2095 2178
2096 $self 2179 $self
2097} 2180}
2098 2181
2182sub changed { }
2183
2099sub set_range { 2184sub set_range {
2100 my ($self, $range) = @_; 2185 my ($self, $range) = @_;
2101 2186
2102 $self->{range} = $range; 2187 ($range, $self->{range}) = ($self->{range}, $range);
2103 2188
2104 $self->update; 2189 $self->update
2190 if "@$range" ne "@{$self->{range}}";
2105} 2191}
2106 2192
2107sub set_value { 2193sub set_value {
2108 my ($self, $value) = @_; 2194 my ($self, $value) = @_;
2109 2195
2120 if $unit; 2206 if $unit;
2121 2207
2122 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2208 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2123 2209
2124 if ($value != $old_value) { 2210 if ($value != $old_value) {
2125 $self->emit (changed => $value); 2211 $self->_emit (changed => $value);
2126 $self->update; 2212 $self->update;
2127 } 2213 }
2128} 2214}
2129 2215
2130sub size_request { 2216sub size_request {
2131 my ($self) = @_; 2217 my ($self) = @_;
2132 2218
2133 my $w = $self->{req_w}; 2219 ($self->{req_w}, $self->{req_h})
2134 my $h = $self->{req_h};
2135
2136 $self->{vertical} ? ($h, $w) : ($w, $h)
2137} 2220}
2138 2221
2139sub button_down { 2222sub button_down {
2140 my ($self, $ev, $x, $y) = @_; 2223 my ($self, $ev, $x, $y) = @_;
2141 2224
2291 $self->{fontsize} = $fontsize; 2374 $self->{fontsize} = $fontsize;
2292 $self->reflow; 2375 $self->reflow;
2293} 2376}
2294 2377
2295sub size_allocate { 2378sub size_allocate {
2296 my ($self, $w, $h) = @_; 2379 my ($self, $w, $h, $changed) = @_;
2297 2380
2298 $self->SUPER::size_allocate ($w, $h); 2381 $self->SUPER::size_allocate ($w, $h, $changed);
2382
2383 return unless $changed;
2299 2384
2300 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2385 $self->{layout}->set_font ($self->{font}) if $self->{font};
2301 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2386 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2302 $self->{layout}->set_width ($self->{children}[0]{w}); 2387 $self->{layout}->set_width ($self->{children}[0]{w});
2303 2388
2492 2577
2493sub new { 2578sub new {
2494 my $class = shift; 2579 my $class = shift;
2495 2580
2496 my $self = $class->SUPER::new ( 2581 my $self = $class->SUPER::new (
2497 state => 0, 2582 state => 0,
2498 connect_activate => \&toggle_flopper, 2583 on_activate => \&toggle_flopper,
2499 @_ 2584 @_
2500 ); 2585 );
2501 2586
2502 if ($self->{state}) {
2503 $self->{state} = 0;
2504 $self->toggle_flopper;
2505 }
2506
2507 $self 2587 $self
2508} 2588}
2509 2589
2510sub toggle_flopper { 2590sub toggle_flopper {
2511 my ($self) = @_; 2591 my ($self) = @_;
2512 2592
2513 # TODO: use animation 2593 $self->{other}->toggle_visibility;
2514 if ($self->{state} = !$self->{state}) {
2515 $CFClient::UI::ROOT->add ($self->{other});
2516 $self->{other}->move ($self->coord2global (0, $self->{h}));
2517 $self->emit ("open");
2518 } else {
2519 $CFClient::UI::ROOT->remove ($self->{other});
2520 $self->emit ("close");
2521 }
2522
2523 $self->emit (changed => $self->{state});
2524} 2594}
2525 2595
2526############################################################################# 2596#############################################################################
2527 2597
2528package CFClient::UI::Tooltip; 2598package CFClient::UI::Tooltip;
2560 2630
2561 ($w + 4, $h + 4) 2631 ($w + 4, $h + 4)
2562} 2632}
2563 2633
2564sub size_allocate { 2634sub size_allocate {
2565 my ($self, $w, $h) = @_; 2635 my ($self, $w, $h, $changed) = @_;
2566 2636
2637 return unless $changed;
2638
2567 $self->SUPER::size_allocate ($w - 4, $h - 4); 2639 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2640}
2641
2642sub visibility_change {
2643 my ($self, $visible) = @_;
2644
2645 return unless $visible;
2646
2647 $self->{root}->on_post_alloc ("move_$self" => sub {
2648 my $widget = $self->{owner}
2649 or return;
2650
2651 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2652
2653 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2654 if $x + $self->{w} > $::WIDTH;
2655
2656 $self->move_abs ($x, $y);
2657 });
2568} 2658}
2569 2659
2570sub _draw { 2660sub _draw {
2571 my ($self) = @_; 2661 my ($self) = @_;
2572 2662
2589 glVertex $w, $h; 2679 glVertex $w, $h;
2590 glVertex $w, 0; 2680 glVertex $w, 0;
2591 glEnd; 2681 glEnd;
2592 2682
2593 glTranslate 2 - 0.375, 2 - 0.375; 2683 glTranslate 2 - 0.375, 2 - 0.375;
2684
2594 $self->SUPER::_draw; 2685 $self->SUPER::_draw;
2595} 2686}
2596 2687
2597############################################################################# 2688#############################################################################
2598 2689
2604 2695
2605sub new { 2696sub new {
2606 my $class = shift; 2697 my $class = shift;
2607 2698
2608 my $self = $class->SUPER::new ( 2699 my $self = $class->SUPER::new (
2609 aspect => 1, 2700 aspect => 1,
2701 can_events => 0,
2610 @_, 2702 @_,
2611 ); 2703 );
2612 2704
2613 if ($self->{anim} && $self->{animspeed}) { 2705 if ($self->{anim} && $self->{animspeed}) {
2614 Scalar::Util::weaken (my $widget = $self); 2706 Scalar::Util::weaken (my $widget = $self);
2673 $self->SUPER::DESTROY; 2765 $self->SUPER::DESTROY;
2674} 2766}
2675 2767
2676############################################################################# 2768#############################################################################
2677 2769
2678package CFClient::UI::InventoryItem; 2770package CFClient::UI::Inventory;
2679 2771
2680our @ISA = CFClient::UI::HBox::; 2772our @ISA = CFClient::UI::ScrolledWindow::;
2681
2682sub _item_to_desc {
2683 my ($item) = @_;
2684
2685 my $desc =
2686 $item->{nrof} < 2
2687 ? $item->{name}
2688 : "$item->{nrof} × $item->{name_pl}";
2689
2690 $item->{flags} & Crossfire::Protocol::F_OPEN
2691 and $desc .= " (open)";
2692 $item->{flags} & Crossfire::Protocol::F_APPLIED
2693 and $desc .= " (applied)";
2694 $item->{flags} & Crossfire::Protocol::F_UNPAID
2695 and $desc .= " (unpaid)";
2696 $item->{flags} & Crossfire::Protocol::F_MAGIC
2697 and $desc .= " (magic)";
2698 $item->{flags} & Crossfire::Protocol::F_CURSED
2699 and $desc .= " (cursed)";
2700 $item->{flags} & Crossfire::Protocol::F_DAMNED
2701 and $desc .= " (damned)";
2702 $item->{flags} & Crossfire::Protocol::F_LOCKED
2703 and $desc .= " *";
2704
2705 $desc
2706}
2707 2773
2708sub new { 2774sub new {
2709 my $class = shift; 2775 my $class = shift;
2710 2776
2711 my %args = @_;
2712
2713 my $item = delete $args{item};
2714
2715 my $desc = _item_to_desc ($item);
2716
2717 my $self = $class->SUPER::new ( 2777 my $self = $class->SUPER::new (
2718 can_hover => 1,
2719 can_events => 1,
2720 tooltip => ((CFClient::UI::Label::escape $desc)
2721 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2722 connect_button_down => sub {
2723 my ($self, $ev, $x, $y) = @_;
2724
2725 # todo: maybe put examine on 1? but should just be a tooltip :(
2726 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2727 my $targ = $::CONN->{player}{tag};
2728
2729 if ($item->{container} == $::CONN->{player}{tag}) {
2730 $targ = $main::OPENCONT;
2731 }
2732
2733 $::CONN->send ("move $targ $item->{tag} 0");
2734 } elsif ($ev->{button} == 1) {
2735 $::CONN->send ("examine $item->{tag}");
2736 } elsif ($ev->{button} == 2) {
2737 $::CONN->send ("apply $item->{tag}");
2738 } elsif ($ev->{button} == 3) {
2739 my @menu_items = (
2740 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2741 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2742 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2743 (
2744 $item->{flags} & Crossfire::Protocol::F_LOCKED
2745 ? (
2746 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2747 )
2748 : (
2749 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2750 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2751 )
2752 ),
2753 );
2754
2755 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2756 }
2757
2758 1
2759 },
2760 %args
2761 );
2762
2763
2764 $self->add (new CFClient::UI::Face
2765 can_events => 0,
2766 face => $item->{face},
2767 anim => $item->{anim},
2768 animspeed => $item->{animspeed},
2769 );
2770
2771 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2772
2773 $self->{item} = $item;
2774
2775 $self->update_item;
2776
2777 $self
2778}
2779
2780sub update_item {
2781 my ($self) = @_;
2782
2783 my $desc = _item_to_desc ($self->{item});
2784
2785 $self->{name_lbl}->set_text ($desc);
2786}
2787
2788#############################################################################
2789
2790package CFClient::UI::Inventory;
2791
2792our @ISA = CFClient::UI::ScrolledWindow::;
2793
2794sub new {
2795 my $class = shift;
2796
2797 my $self = $class->SUPER::new (
2798 scrolled => (new CFClient::UI::Table), 2778 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2799 @_, 2779 @_,
2800 ); 2780 );
2801 2781
2802 $self 2782 $self
2803} 2783}
2813 or ($a->{name} cmp $b->{name}) 2793 or ($a->{name} cmp $b->{name})
2814 } @$items; 2794 } @$items;
2815 2795
2816 $self->{real_items} = \@items; 2796 $self->{real_items} = \@items;
2817 2797
2798 my $row = 0;
2818 for my $item (@items) { 2799 for my $item (@items) {
2819 $item->{item} = $item; 2800 CFClient::Item::update_widgets $item;
2820 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2821 $item->update_item ();
2822 }
2823 2801
2824 my $i = 0; 2802 $self->{scrolled}->add (0, $row, $item->{face_widget});
2825 for (@items) { 2803 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2826 $self->{scrolled}->add (0, $i, $_); 2804 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2827 my $nrof = $_->{item}->{nrof} || 1;
2828 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2829 }
2830 2805
2831# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2806 $row++;
2832} 2807 }
2833
2834sub size_request {
2835 my ($self) = @_;
2836 ($self->{req_w}, $self->{req_h});
2837} 2808}
2838 2809
2839############################################################################# 2810#############################################################################
2840 2811
2841package CFClient::UI::Menu; 2812package CFClient::UI::Menu;
2876 2847
2877# popup given the event (must be a mouse button down event currently) 2848# popup given the event (must be a mouse button down event currently)
2878sub popup { 2849sub popup {
2879 my ($self, $ev) = @_; 2850 my ($self, $ev) = @_;
2880 2851
2881 $self->emit ("popdown"); 2852 $self->_emit ("popdown");
2882 2853
2883 # maybe save $GRAB? must be careful about events... 2854 # maybe save $GRAB? must be careful about events...
2884 $GRAB = $self; 2855 $GRAB = $self;
2885 $self->{button} = $ev->{button}; 2856 $self->{button} = $ev->{button};
2886 2857
2901 2872
2902 if ($ev->{button} == $self->{button}) { 2873 if ($ev->{button} == $self->{button}) {
2903 undef $GRAB; 2874 undef $GRAB;
2904 $self->hide; 2875 $self->hide;
2905 2876
2906 $self->emit ("popdown"); 2877 $self->_emit ("popdown");
2907 $self->{hover}[1]->() if $self->{hover}; 2878 $self->{hover}[1]->() if $self->{hover};
2908 } 2879 }
2909} 2880}
2910 2881
2911############################################################################# 2882#############################################################################
2973sub add { 2944sub add {
2974 my ($self, $text, %arg) = @_; 2945 my ($self, $text, %arg) = @_;
2975 2946
2976 $text =~ s/^\s+//; 2947 $text =~ s/^\s+//;
2977 $text =~ s/\s+$//; 2948 $text =~ s/\s+$//;
2949
2950 return unless $text;
2978 2951
2979 my $timeout = time + ((delete $arg{timeout}) || 60); 2952 my $timeout = time + ((delete $arg{timeout}) || 60);
2980 2953
2981 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2954 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2982 2955
3025use CFClient::OpenGL; 2998use CFClient::OpenGL;
3026 2999
3027sub new { 3000sub new {
3028 my $class = shift; 3001 my $class = shift;
3029 3002
3030 $class->SUPER::new ( 3003 my $self = $class->SUPER::new (
3004 visible => 1,
3031 @_, 3005 @_,
3032 ) 3006 );
3007
3008 Scalar::Util::weaken ($self->{root} = $self);
3009
3010 $self
3033} 3011}
3034 3012
3035sub configure { 3013sub configure {
3036 my ($self, $x, $y, $w, $h) = @_; 3014 my ($self, $x, $y, $w, $h) = @_;
3037 3015
3038 $self->{w} = $w; 3016 $self->{w} = $w;
3039 $self->{h} = $h; 3017 $self->{h} = $h;
3040} 3018}
3041 3019
3042sub check_size { 3020sub reconfigure {
3043 my ($self) = @_; 3021 my ($self) = @_;
3044 3022
3023 $self->SUPER::reconfigure;
3024
3045 $self->size_allocate ($self->{w}, $self->{h}) 3025 $self->size_allocate ($self->{w}, $self->{h}, 1)
3046 if $self->{w}; 3026 if $self->{w};
3047} 3027}
3048 3028
3049sub size_request { 3029sub size_request {
3050 my ($self) = @_; 3030 my ($self) = @_;
3051 3031
3052 ($self->{w}, $self->{h}) 3032 ($self->{w}, $self->{h})
3053} 3033}
3054 3034
3035sub _to_pixel {
3036 my ($coord, $size, $max) = @_;
3037
3038 $coord =
3039 $coord eq "center" ? ($max - $size) * 0.5
3040 : $coord eq "max" ? $max
3041 : $coord;
3042
3043 $coord = 0 if $coord < 0;
3044 $coord = $max - $size if $coord > $max - $size;
3045
3046 int $coord + 0.5
3047}
3048
3055sub size_allocate { 3049sub size_allocate {
3056 my ($self, $w, $h) = @_; 3050 my ($self, $w, $h, $changed) = @_;
3057
3058 my $old_w = $self->{old_w}; $self->{old_w} = $w;
3059 my $old_h = $self->{old_h}; $self->{old_h} = $h;
3060
3061 CFClient::UI::rescale_widgets $w / $old_w, $h / $old_h
3062 if $old_w && $old_h && ($old_w != $w || $old_h != $h);
3063 3051
3064 for my $child ($self->children) { 3052 for my $child ($self->children) {
3065 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3053 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3066 3054
3067 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3055 $X = $child->{force_x} if exists $child->{force_x};
3068 if exists $child->{req_x}; 3056 $Y = $child->{force_y} if exists $child->{force_y};
3069 3057
3070 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3058 $X = _to_pixel $X, $W, $self->{w};
3071 if exists $child->{req_y}; 3059 $Y = _to_pixel $Y, $H, $self->{h};
3072
3073 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3074 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3075 3060
3076 $child->configure ($X, $Y, $W, $H); 3061 $child->configure ($X, $Y, $W, $H);
3077 } 3062 }
3078} 3063}
3079 3064
3090} 3075}
3091 3076
3092sub update { 3077sub update {
3093 my ($self) = @_; 3078 my ($self) = @_;
3094 3079
3095 $self->check_size;
3096 $::WANT_REFRESH++; 3080 $::WANT_REFRESH++;
3097} 3081}
3098 3082
3099sub add { 3083sub add {
3100 my ($self, @children) = @_; 3084 my ($self, @children) = @_;
3101 3085
3102 for (my @widgets = @children; my $w = pop @widgets; ) {
3103 push @widgets, $w->children;
3104 $w->{root} = $self;
3105 $w->{visible} = 1;
3106 }
3107
3108 for my $child (@children) {
3109 $child->{toplevel} = 1; 3086 $_->{is_toplevel} = 1
3110 3087 for @children;
3111 # integerise window positions
3112 $child->{x} = int $child->{x};
3113 $child->{y} = int $child->{y};
3114 }
3115 3088
3116 $self->SUPER::add (@children); 3089 $self->SUPER::add (@children);
3117} 3090}
3118 3091
3119sub remove { 3092sub remove {
3120 my ($self, @children) = @_; 3093 my ($self, @children) = @_;
3121 3094
3122 $self->SUPER::remove (@children); 3095 $self->SUPER::remove (@children);
3096
3097 delete $self->{is_toplevel}
3098 for @children;
3123 3099
3124 while (@children) { 3100 while (@children) {
3125 my $w = pop @children; 3101 my $w = pop @children;
3126 push @children, $w->children; 3102 push @children, $w->children;
3127 delete $w->{visible}; 3103 $w->set_invisible;
3128 } 3104 }
3129} 3105}
3130 3106
3131sub on_refresh { 3107sub on_refresh {
3132 my ($self, $id, $cb) = @_; 3108 my ($self, $id, $cb) = @_;
3146 while ($self->{refresh_hook}) { 3122 while ($self->{refresh_hook}) {
3147 $_->() 3123 $_->()
3148 for values %{delete $self->{refresh_hook}}; 3124 for values %{delete $self->{refresh_hook}};
3149 } 3125 }
3150 3126
3151 if ($self->{check_size}) { 3127 if ($self->{realloc}) {
3152 my @queue = ([], []); 3128 my @queue;
3153 3129
3154 for (;;) { 3130 while () {
3155 if ($self->{check_size}) { 3131 if ($self->{realloc}) {
3156 # heuristic: check containers last 3132 #TODO use array-of-depth approach
3157 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3133
3158 for values %{delete $self->{check_size}} 3134 @queue = sort { $a->{visible} <=> $b->{visible} }
3135 @queue, values %{delete $self->{realloc}};
3159 } 3136 }
3160 3137
3161 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3138 my $widget = pop @queue || last;
3162 3139
3163 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3140 $widget->{visible} or last; # do not resize invisible widgets
3164 ? @$widget{qw(user_w user_h)} 3141
3165 : $widget->size_request; 3142 my ($w, $h) = $widget->size_request;
3166 3143
3167 if (delete $widget->{force_alloc} 3144 my $min_size = $widget->{is_toplevel} ? 16 : 0;
3168 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3145
3169 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3146 $w = List::Util::max $widget->{min_w} || $widget->{is_toplevel} * 16, $w;
3170 3147 $h = List::Util::max $widget->{min_h} || $widget->{is_toplevel} * 16, $h;
3148
3149 $w = $widget->{force_w} if exists $widget->{force_w};
3150 $h = $widget->{force_h} if exists $widget->{force_h};
3151
3171 $widget->{req_w} = $w; 3152 $widget->{req_w} = $w;
3172 $widget->{req_h} = $h; 3153 $widget->{req_h} = $h;
3173 3154
3174 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3155 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3175 3156
3176 $widget->{parent}->check_size 3157 push @queue, $widget->{parent}
3177 if $widget->{parent}; 3158 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3178 }
3179 } 3159 }
3180 } 3160 }
3181 3161
3182 while ($self->{size_alloc}) { 3162 while (my $size_alloc = delete $self->{size_alloc}) {
3183 for (values %{delete $self->{size_alloc}}) { 3163 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3184 my ($widget, $w, $h) = @$_; 3164 values %$size_alloc;
3165
3166 while () {
3167 my ($widget, $w, $h) = @{ pop @queue or last };
3168
3169 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3170 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3185 3171
3186 $w = 0 if $w < 0; 3172 $w = 0 if $w < 0;
3187 $h = 0 if $h < 0; 3173 $h = 0 if $h < 0;
3188 3174
3175 $w = int $w + 0.5;
3176 $h = int $h + 0.5;
3177
3178 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3179
3189 $widget->{w} = $w; 3180 $widget->{w} = $w;
3190 $widget->{h} = $h; 3181 $widget->{h} = $h;
3191 $widget->size_allocate ($w, $h); 3182
3192 $widget->emit (size_allocate => $w, $h); 3183 $widget->emit (size_allocate => $w, $h, $changed);
3193 } 3184 }
3194 } 3185 }
3195 3186
3196 while ($self->{post_alloc_hook}) { 3187 while ($self->{post_alloc_hook}) {
3197 $_->() 3188 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines