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.230 by root, Wed May 24 21:59:54 2006 UTC vs.
Revision 1.256 by root, Tue May 30 01:42:16 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) = @_;
158sub rescale_widgets { 167sub rescale_widgets {
159 my ($sx, $sy) = @_; 168 my ($sx, $sy) = @_;
160 169
161 for my $widget (values %WIDGET) { 170 for my $widget (values %WIDGET) {
162 if ($widget->{is_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
163 $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.]+$/;
164 $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};
165 $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};
166 $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w};
167 $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.]+$/;
168 $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};
169 $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};
170 $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
171 } 185 }
172 } 186 }
173 187
174 reconfigure_widgets; 188 reconfigure_widgets;
175} 189}
184 198
185sub new { 199sub new {
186 my $class = shift; 200 my $class = shift;
187 201
188 my $self = bless { 202 my $self = bless {
189 x => 0, 203 x => "center",
190 y => 0, 204 y => "center",
191 z => 0, 205 z => 0,
206 w => undef,
207 h => undef,
192 can_events => 1, 208 can_events => 1,
193 @_ 209 @_
194 }, $class; 210 }, $class;
195 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
196 for (keys %$self) { 224 for (keys %$self) {
197 if (/^connect_(.*)$/) { 225 if (/^on_(.*)$/) {
198 $self->connect ($1 => delete $self->{$_}); 226 $self->connect ($1 => delete $self->{$_});
199 } 227 }
200 } 228 }
201 229
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203
204 $self 230 $self
205} 231}
206 232
207sub destroy { 233sub destroy {
208 my ($self) = @_; 234 my ($self) = @_;
211 %$self = (); 237 %$self = ();
212} 238}
213 239
214sub show { 240sub show {
215 my ($self) = @_; 241 my ($self) = @_;
242
216 return if $self->{parent}; 243 return if $self->{parent};
217 244
218 $CFClient::UI::ROOT->add ($self); 245 $CFClient::UI::ROOT->add ($self);
219} 246}
220 247
221sub show_centered { 248sub set_visible {
222 my ($self) = @_; 249 my ($self) = @_;
250
223 return if $self->{parent}; 251 return if $self->{visible};
224 252
225 $self->show; 253 $self->{root} = $self->{parent}{root};
254 $self->{visible} = $self->{parent}{visible} + 1;
226 255
227 $CFClient::UI::ROOT->on_post_alloc ( 256 $self->emit (visibility_change => 1);
228 "centered $self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
230 },
231 );
232}
233 257
234sub hide { 258 $self->realloc if !exists $self->{req_w};
259
260 $_->set_visible for $self->children;
261}
262
263sub set_invisible {
235 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};
236 272
237 undef $GRAB if $GRAB == $self; 273 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self; 274 undef $HOVER if $HOVER == $self;
239 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
240 $self->{parent}->remove ($self) 306 $self->{parent}->remove ($self)
241 if $self->{parent}; 307 if $self->{parent};
242} 308}
243 309
244sub move { 310sub move_abs {
245 my ($self, $x, $y, $z) = @_; 311 my ($self, $x, $y, $z) = @_;
246 312
247 $self->{x} = int $x; 313 $self->{x} = List::Util::max 0, int $x;
248 $self->{y} = int $y; 314 $self->{y} = List::Util::max 0, int $y;
249 $self->{z} = $z if defined $z; 315 $self->{z} = $z if defined $z;
250 316
251 $self->update; 317 $self->update;
252} 318}
253 319
254sub set_size { 320sub set_size {
255 my ($self, $w, $h) = @_; 321 my ($self, $w, $h) = @_;
256 322
257 $self->{user_w} = $w; 323 $self->{force_w} = $w;
258 $self->{user_h} = $h; 324 $self->{force_h} = $h;
259 325
260 $self->check_size; 326 $self->realloc;
261} 327}
262 328
263sub size_request { 329sub size_request {
264 require Carp; 330 require Carp;
265 Carp::confess "size_request is abstract"; 331 Carp::confess "size_request is abstract";
267 333
268sub configure { 334sub configure {
269 my ($self, $x, $y, $w, $h) = @_; 335 my ($self, $x, $y, $w, $h) = @_;
270 336
271 if ($self->{aspect}) { 337 if ($self->{aspect}) {
338 my ($ow, $oh) = ($w, $h);
339
272 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 340 $w = List::Util::min $w, int $h * $self->{aspect};
273 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 341 $h = List::Util::min $h, int $w / $self->{aspect};
274 342
275 # use alignment to adjust x, y 343 # use alignment to adjust x, y
276 344
277 $x += int +($w - $w2) * 0.5; 345 $x += int 0.5 * ($ow - $w);
278 $y += int +($h - $h2) * 0.5; 346 $y += int 0.5 * ($oh - $h);
279
280 ($w, $h) = ($w2, $h2);
281 } 347 }
282 348
283 if ($self->{x} != $x || $self->{y} != $y) { 349 if ($self->{x} ne $x || $self->{y} ne $y) {
284 $self->{x} = $x; 350 $self->{x} = $x;
285 $self->{y} = $y; 351 $self->{y} = $y;
286 $self->update; 352 $self->update;
287 } 353 }
288 354
289 if ($self->{w} != $w || $self->{h} != $h) { 355 if ($self->{w} != $w || $self->{h} != $h) {
356 return unless $self->{visible};
357
290 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 358 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
291 } 359 }
292} 360}
293 361
294sub size_allocate { 362sub size_allocate {
295 # nothing to be done 363 # nothing to be done
296} 364}
297 365
298sub reconfigure {
299 my ($self) = @_;
300
301 $self->check_size (1);
302 $self->update;
303}
304
305sub children { 366sub children {
306} 367}
307 368
308sub set_max_size { 369sub set_max_size {
309 my ($self, $w, $h) = @_; 370 my ($self, $w, $h) = @_;
312 delete $self->{max_h}; $self->{max_h} = $h if $h; 373 delete $self->{max_h}; $self->{max_h} = $h if $h;
313} 374}
314 375
315sub set_tooltip { 376sub set_tooltip {
316 my ($self, $tooltip) = @_; 377 my ($self, $tooltip) = @_;
378
379 $tooltip =~ s/^\s+//;
380 $tooltip =~ s/\s+$//;
381
382 return if $self->{tooltip} eq $tooltip;
317 383
318 $self->{tooltip} = $tooltip; 384 $self->{tooltip} = $tooltip;
319 385
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 386 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner}; 387 delete $CFClient::UI::TOOLTIP->{owner};
343 return if $FOCUS == $self; 409 return if $FOCUS == $self;
344 return unless $self->{can_focus}; 410 return unless $self->{can_focus};
345 411
346 my $focus = $FOCUS; $FOCUS = $self; 412 my $focus = $FOCUS; $FOCUS = $self;
347 413
348 $self->emit (focus_in => $focus); 414 $self->_emit (focus_in => $focus);
349 415
350 $focus->update if $focus; 416 $focus->update if $focus;
351 $FOCUS->update; 417 $FOCUS->update;
352} 418}
353 419
356 422
357 return unless $FOCUS == $self; 423 return unless $FOCUS == $self;
358 424
359 my $focus = $FOCUS; undef $FOCUS; 425 my $focus = $FOCUS; undef $FOCUS;
360 426
361 $self->emit (focus_out => $focus); 427 $self->_emit (focus_out => $focus);
362 428
363 $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;
364} 433}
365 434
366sub mouse_motion { } 435sub mouse_motion { }
367sub button_up { } 436sub button_up { }
368sub key_down { } 437sub key_down { }
369sub key_up { } 438sub key_up { }
370 439
371sub button_down { 440sub button_down {
372 my ($self, $ev, $x, $y) = @_; 441 my ($self, $ev, $x, $y) = @_;
373 442
374 $self->focus_in; 443 $self->focus_in;
377sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 446sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
378sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 447sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
379sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 448sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
380sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 449sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
381sub 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}
382 521
383sub draw { 522sub draw {
384 my ($self) = @_; 523 my ($self) = @_;
385 524
386 return unless $self->{h} && $self->{w}; 525 return unless $self->{h} && $self->{w};
403 glVertex $x , $y + $self->{h}; 542 glVertex $x , $y + $self->{h};
404 glEnd; 543 glEnd;
405 glDisable GL_BLEND; 544 glDisable GL_BLEND;
406 } 545 }
407 546
408 if ($ENV{PCLIENT_DEBUG}) { 547 if ($ENV{CFPLUS_DEBUG}) {
409 glPushMatrix; 548 glPushMatrix;
410 glColor 1, 1, 0, 1; 549 glColor 1, 1, 0, 1;
411 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 550 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
412 glBegin GL_LINE_LOOP; 551 glBegin GL_LINE_LOOP;
413 glVertex 0 , 0; 552 glVertex 0 , 0;
414 glVertex $self->{w}, 0; 553 glVertex $self->{w} - 1, 0;
415 glVertex $self->{w}, $self->{h}; 554 glVertex $self->{w} - 1, $self->{h} - 1;
416 glVertex 0 , $self->{h}; 555 glVertex 0 , $self->{h} - 1;
417 glEnd; 556 glEnd;
418 glPopMatrix; 557 glPopMatrix;
419 #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;
420 } 559 }
421} 560}
422 561
423sub _draw { 562sub _draw {
424 my ($self) = @_; 563 my ($self) = @_;
425 564
426 warn "no draw defined for $self\n"; 565 warn "no draw defined for $self\n";
427}
428
429sub find_widget {
430 my ($self, $x, $y) = @_;
431
432 return () unless $self->{can_events};
433
434 return $self
435 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
436 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
437
438 ()
439}
440
441sub set_parent {
442 my ($self, $parent) = @_;
443
444 Scalar::Util::weaken ($self->{parent} = $parent);
445
446 # TODO: req_w _does_change after ->reconfigure
447 $self->check_size
448 unless exists $self->{req_w};
449
450 $self->show;
451}
452
453sub check_size {
454 my ($self, $forced) = @_;
455
456 $self->{force_alloc} = 1 if $forced;
457 $CFClient::UI::ROOT->{check_size}{$self} = $self;
458}
459
460sub update {
461 my ($self) = @_;
462
463 $self->{parent}->update
464 if $self->{parent};
465}
466
467sub connect {
468 my ($self, $signal, $cb) = @_;
469
470 push @{ $self->{signal_cb}{$signal} }, $cb;
471}
472
473sub emit {
474 my ($self, $signal, @args) = @_;
475
476 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
477} 566}
478 567
479sub DESTROY { 568sub DESTROY {
480 my ($self) = @_; 569 my ($self) = @_;
481 570
539 my ($class, %arg) = @_; 628 my ($class, %arg) = @_;
540 $class->SUPER::new (can_events => 0, %arg); 629 $class->SUPER::new (can_events => 0, %arg);
541} 630}
542 631
543sub size_request { 632sub size_request {
544 (0, 0) 633 my ($self) = @_;
634
635 ($self->{w} + 0, $self->{h} + 0)
545} 636}
546 637
547sub draw { } 638sub draw { }
548 639
549############################################################################# 640#############################################################################
578 $self->{children} = [ 669 $self->{children} = [
579 sort { $a->{z} <=> $b->{z} } 670 sort { $a->{z} <=> $b->{z} }
580 @{$self->{children}}, @widgets 671 @{$self->{children}}, @widgets
581 ]; 672 ];
582 673
583 $self->check_size (1); 674 $self->realloc;
584 $self->update;
585} 675}
586 676
587sub children { 677sub children {
588 @{ $_[0]{children} } 678 @{ $_[0]{children} }
589} 679}
594 delete $child->{parent}; 684 delete $child->{parent};
595 $child->hide; 685 $child->hide;
596 686
597 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 687 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
598 688
599 $self->check_size; 689 $self->realloc;
600 $self->update;
601} 690}
602 691
603sub clear { 692sub clear {
604 my ($self) = @_; 693 my ($self) = @_;
605 694
609 for (@$children) { 698 for (@$children) {
610 delete $_->{parent}; 699 delete $_->{parent};
611 $_->hide; 700 $_->hide;
612 } 701 }
613 702
614 $self->check_size; 703 $self->realloc;
615 $self->update;
616} 704}
617 705
618sub find_widget { 706sub find_widget {
619 my ($self, $x, $y) = @_; 707 my ($self, $x, $y) = @_;
620 708
673sub size_request { 761sub size_request {
674 $_[0]{children}[0]->size_request 762 $_[0]{children}[0]->size_request
675} 763}
676 764
677sub size_allocate { 765sub size_allocate {
678 my ($self, $w, $h) = @_; 766 my ($self, $w, $h, $changed) = @_;
679 767
680 $self->{children}[0]->configure (0, 0, $w, $h); 768 $self->{children}[0]->configure (0, 0, $w, $h);
681} 769}
682 770
683############################################################################# 771#############################################################################
700 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 788 $ROOT->on_post_alloc ($self => sub { $self->render_child });
701 $self->SUPER::update; 789 $self->SUPER::update;
702} 790}
703 791
704sub size_allocate { 792sub size_allocate {
705 my ($self, $w, $h) = @_; 793 my ($self, $w, $h, $changed) = @_;
706 794
707 $self->SUPER::size_allocate ($w, $h); 795 $self->SUPER::size_allocate ($w, $h, $changed);
708 $self->update; 796 $self->update
797 if $changed;
709} 798}
710 799
711sub _render { 800sub _render {
712 $_[0]{children}[0]->draw; 801 $_[0]{children}[0]->draw;
713} 802}
744 833
745package CFClient::UI::ViewPort; 834package CFClient::UI::ViewPort;
746 835
747our @ISA = CFClient::UI::Window::; 836our @ISA = CFClient::UI::Window::;
748 837
838sub new {
839 my $class = shift;
840
841 $class->SUPER::new (
842 scroll_x => 0,
843 scroll_y => 1,
844 @_,
845 )
846}
847
749sub size_request { 848sub size_request {
750 my ($self) = @_; 849 my ($self) = @_;
751 850
752 @$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)};
753 852
754 @$self{qw(child_w child_h)} 853 $w = 10 if $self->{scroll_x};
854 $h = 10 if $self->{scroll_y};
855
856 ($w, $h)
755} 857}
756 858
757sub size_allocate { 859sub size_allocate {
758 my ($self, $w, $h) = @_; 860 my ($self, $w, $h, $changed) = @_;
759 861
760 my ($cw, $ch) = @$self{qw(child_w child_h)}; 862 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
761# $w = $self->{w}; 863 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
864
762 $self->child->configure (0, 0, $cw, $ch); 865 $self->child->configure (0, 0, $w, $h);
763 $self->update; 866 $self->update;
764} 867}
765 868
766sub set_offset { 869sub set_offset {
767 my ($self, $x, $y) = @_; 870 my ($self, $x, $y) = @_;
818 my $class = shift; 921 my $class = shift;
819 922
820 my $self; 923 my $self;
821 924
822 my $slider = new CFClient::UI::Slider 925 my $slider = new CFClient::UI::Slider
823 vertical => 1, 926 vertical => 1,
824 range => [0, 0, 1, 0.01], # HACK fix 927 range => [0, 0, 1, 0.01], # HACK fix
825 connect_changed => sub { 928 on_changed => sub {
826 $self->{vp}->set_offset (0, $_[1]); 929 $self->{vp}->set_offset (0, $_[1]);
827 }, 930 },
828 ; 931 ;
829 932
830 $self = $class->SUPER::new ( 933 $self = $class->SUPER::new (
838 $self->add ($self->{slider}); 941 $self->add ($self->{slider});
839 942
840 $self 943 $self
841} 944}
842 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
843sub size_allocate { 956sub size_allocate {
844 my ($self, $w, $h) = @_; 957 my ($self, $w, $h, $changed) = @_;
845 958
846 $self->SUPER::size_allocate ($w, $h); 959 $self->SUPER::size_allocate ($w, $h, $changed);
847 960
848 my $child = $self->{vp}->child; 961 my $child = $self->{vp}->child;
849 $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]);
850} 963}
851 964
898 1011
899our @ISA = CFClient::UI::Bin::; 1012our @ISA = CFClient::UI::Bin::;
900 1013
901use CFClient::OpenGL; 1014use CFClient::OpenGL;
902 1015
903my @tex = 1016my $bg =
1017 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1018 mipmap => 1, wrap => 1;
1019
1020my @border =
904 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1021 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
905 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);
906 1023
907sub new { 1024sub new {
908 my $class = shift; 1025 my $class = shift;
909
910 # TODO: user_x, user_y, overwrite moveto?
911 1026
912 my $self = $class->SUPER::new ( 1027 my $self = $class->SUPER::new (
913 bg => [1, 1, 1, 1], 1028 bg => [1, 1, 1, 1],
914 border_bg => [1, 1, 1, 1], 1029 border_bg => [1, 1, 1, 1],
915 border => 0.6, 1030 border => 0.6,
941 $h + $self->border * 2, 1056 $h + $self->border * 2,
942 ) 1057 )
943} 1058}
944 1059
945sub size_allocate { 1060sub size_allocate {
946 my ($self, $w, $h) = @_; 1061 my ($self, $w, $h, $changed) = @_;
1062
1063 return unless $changed;
947 1064
948 $h -= List::Util::max 0, $self->border * 2; 1065 $h -= List::Util::max 0, $self->border * 2;
949 $w -= List::Util::max 0, $self->border * 2; 1066 $w -= List::Util::max 0, $self->border * 2;
950 1067
951 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1068 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
975 my ($ev, $x, $y) = @_; 1092 my ($ev, $x, $y) = @_;
976 1093
977 my $dx = $ev->{x} - $ox; 1094 my $dx = $ev->{x} - $ox;
978 my $dy = $ev->{y} - $oy; 1095 my $dy = $ev->{y} - $oy;
979 1096
980 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1097 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
981 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1098 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1099
1100 $self->realloc;
982 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1101 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
983 $self->check_size;
984 }; 1102 };
985 1103
986 } elsif ($lr ^ $td) { 1104 } elsif ($lr ^ $td) {
987 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1105 my ($ox, $oy) = ($ev->{x}, $ev->{y});
988 my ($bx, $by) = ($self->{x}, $self->{y}); 1106 my ($bx, $by) = ($self->{x}, $self->{y});
990 $self->{motion} = sub { 1108 $self->{motion} = sub {
991 my ($ev, $x, $y) = @_; 1109 my ($ev, $x, $y) = @_;
992 1110
993 ($x, $y) = ($ev->{x}, $ev->{y}); 1111 ($x, $y) = ($ev->{x}, $ev->{y});
994 1112
995 $self->move ($bx + $x - $ox, $by + $y - $oy); 1113 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
996 $self->update;
997 }; 1114 };
998 } 1115 }
999} 1116}
1000 1117
1001sub button_up { 1118sub button_up {
1020 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1137 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1021 1138
1022 my $border = $self->border; 1139 my $border = $self->border;
1023 1140
1024 glColor @{ $self->{border_bg} }; 1141 glColor @{ $self->{border_bg} };
1025 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1142 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1026 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1143 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1027 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1144 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1028 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1145 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1029 1146
1030 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1147 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1031 my $bg = $tex[0]; 1148 glColor @{ $self->{bg} };
1032 1149
1033 # TODO: repeat texture not scale 1150 # TODO: repeat texture not scale
1151 # solve this better(?)
1034 my $rep_x = $cw / $bg->{w}; 1152 $bg->{s} = $cw / $bg->{w};
1035 my $rep_y = $ch / $bg->{h}; 1153 $bg->{t} = $ch / $bg->{h};
1036
1037 glColor @{ $self->{bg} };
1038
1039 $bg->{s} = $rep_x;
1040 $bg->{t} = $rep_y;
1041 $bg->{wrap_mode} = 1;
1042 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1154 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1043 } 1155 }
1044 1156
1045 glDisable GL_TEXTURE_2D; 1157 glDisable GL_TEXTURE_2D;
1046 1158
1062sub new { 1174sub new {
1063 my $class = shift; 1175 my $class = shift;
1064 1176
1065 $class->SUPER::new ( 1177 $class->SUPER::new (
1066 col_expand => [], 1178 col_expand => [],
1067 @_ 1179 @_,
1068 ) 1180 )
1181}
1182
1183sub children {
1184 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1069} 1185}
1070 1186
1071sub add { 1187sub add {
1072 my ($self, $x, $y, $child) = @_; 1188 my ($self, $x, $y, $child) = @_;
1073 1189
1074 $child->set_parent ($self); 1190 $child->set_parent ($self);
1075 $self->{children}[$y][$x] = $child; 1191 $self->{children}[$y][$x] = $child;
1076 1192
1077 $child->check_size; 1193 $self->realloc;
1078} 1194}
1079 1195
1080sub children {
1081 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1082}
1083
1084# TODO: move to container class maybe? send childs a signal on removal? 1196# TODO: move to container class maybe? send children a signal on removal?
1085sub clear { 1197sub clear {
1086 my ($self) = @_; 1198 my ($self) = @_;
1087 1199
1088 my @children = $self->children; 1200 my @children = $self->children;
1089 delete $self->{children}; 1201 delete $self->{children};
1091 for (@children) { 1203 for (@children) {
1092 delete $_->{parent}; 1204 delete $_->{parent};
1093 $_->hide; 1205 $_->hide;
1094 } 1206 }
1095 1207
1096 $self->update; 1208 $self->realloc;
1097} 1209}
1098 1210
1099sub get_wh { 1211sub get_wh {
1100 my ($self) = @_; 1212 my ($self) = @_;
1101 1213
1128 (sum @$hs), 1240 (sum @$hs),
1129 ) 1241 )
1130} 1242}
1131 1243
1132sub size_allocate { 1244sub size_allocate {
1133 my ($self, $w, $h) = @_; 1245 my ($self, $w, $h, $changed) = @_;
1134 1246
1135 my ($ws, $hs) = $self->get_wh; 1247 my ($ws, $hs) = $self->get_wh;
1136 1248
1137 my $req_w = sum @$ws; 1249 my $req_w = (sum @$ws) || 1;
1138 my $req_h = sum @$hs; 1250 my $req_h = (sum @$hs) || 1;
1139 1251
1140 # TODO: nicer code && do row_expand 1252 # TODO: nicer code && do row_expand
1141 my @col_expand = @{$self->{col_expand}}; 1253 my @col_expand = @{$self->{col_expand}};
1142 @col_expand = (1) x @$ws unless @col_expand; 1254 @col_expand = (1) x @$ws unless @col_expand;
1143 my $col_expand = (sum @col_expand) || 1; 1255 my $col_expand = (sum @col_expand) || 1;
1197 } 1309 }
1198} 1310}
1199 1311
1200############################################################################# 1312#############################################################################
1201 1313
1314package CFClient::UI::Box;
1315
1316our @ISA = CFClient::UI::Container::;
1317
1318sub size_request {
1319 my ($self) = @_;
1320
1321 $self->{vertical}
1322 ? (
1323 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1324 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1325 )
1326 : (
1327 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 )
1330}
1331
1332sub size_allocate {
1333 my ($self, $w, $h, $changed) = @_;
1334
1335 my $space = $self->{vertical} ? $h : $w;
1336 my $children = $self->{children};
1337
1338 my @req;
1339
1340 if ($self->{homogeneous}) {
1341 @req = ($space / (@$children || 1)) x @$children;
1342 } else {
1343 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1344 my $req = List::Util::sum @req;
1345
1346 if ($req > $space) {
1347 # ah well, not enough space
1348 $_ *= $space / $req for @req;
1349 } else {
1350 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1351
1352 $space = ($space - $req) / $expand; # remaining space to give away
1353
1354 $req[$_] += $space * $children->[$_]{expand}
1355 for 0 .. $#$children;
1356 }
1357 }
1358
1359 CFClient::UI::harmonize \@req;
1360
1361 my $pos = 0;
1362 for (0 .. $#$children) {
1363 my $alloc = $req[$_];
1364 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1365
1366 $pos += $alloc;
1367 }
1368
1369 1
1370}
1371
1372#############################################################################
1373
1202package CFClient::UI::HBox; 1374package CFClient::UI::HBox;
1203 1375
1204# TODO: wrap into common Box base class
1205
1206our @ISA = CFClient::UI::Container::; 1376our @ISA = CFClient::UI::Box::;
1207 1377
1208sub size_request { 1378sub new {
1209 my ($self) = @_; 1379 my $class = shift;
1210 1380
1211 my @alloc = map [$_->size_request], @{$self->{children}}; 1381 $class->SUPER::new (
1212 1382 vertical => 0,
1213 ( 1383 @_,
1214 (List::Util::sum map $_->[0], @alloc),
1215 (List::Util::max map $_->[1], @alloc),
1216 ) 1384 )
1217} 1385}
1218 1386
1219sub size_allocate {
1220 my ($self, $w, $h) = @_;
1221
1222 ($h, $w) = ($w, $h);
1223
1224 my $children = $self->{children};
1225
1226 my @h = map $_->{req_w}, @$children;
1227
1228 my $req_h = List::Util::sum @h;
1229
1230 if ($req_h > $h) {
1231 # ah well, not enough space
1232 $_ *= $h / $req_h for @h;
1233 } else {
1234 my $exp = List::Util::sum map $_->{expand}, @$children;
1235 $exp ||= 1;
1236
1237 for (0 .. $#$children) {
1238 my $child = $children->[$_];
1239
1240 my $alloc_h = $h[$_];
1241 $alloc_h += ($h - $req_h) * $child->{expand} / $exp;
1242 $h[$_] = $alloc_h;
1243 }
1244 }
1245
1246 CFClient::UI::harmonize \@h;
1247
1248 my $y = 0;
1249 for (0 .. $#$children) {
1250 my $child = $children->[$_];
1251 my $h = $h[$_];
1252 $child->configure ($y, 0, $h, $w);
1253
1254 $y += $h;
1255 }
1256
1257 1
1258}
1259
1260############################################################################# 1387#############################################################################
1261 1388
1262package CFClient::UI::VBox; 1389package CFClient::UI::VBox;
1263 1390
1264# TODO: wrap into common Box base class
1265
1266our @ISA = CFClient::UI::Container::; 1391our @ISA = CFClient::UI::Box::;
1267 1392
1268sub size_request { 1393sub new {
1269 my ($self) = @_; 1394 my $class = shift;
1270 1395
1271 my @alloc = map [$_->size_request], @{$self->{children}}; 1396 $class->SUPER::new (
1272 1397 vertical => 1,
1273 ( 1398 @_,
1274 (List::Util::max map $_->[0], @alloc),
1275 (List::Util::sum map $_->[1], @alloc),
1276 ) 1399 )
1277}
1278
1279sub size_allocate {
1280 my ($self, $w, $h) = @_;
1281
1282 Carp::confess "negative size" if $w < 0 || $h < 0;#d#
1283
1284 my $children = $self->{children};
1285
1286 my @h = map $_->{req_h}, @$children;
1287
1288 my $req_h = List::Util::sum @h;
1289
1290 if ($req_h > $h) {
1291 # ah well, not enough space
1292 $_ *= $h / $req_h for @h;
1293 } else {
1294 my $exp = List::Util::sum map $_->{expand}, @$children;
1295 $exp ||= 1;
1296
1297 for (0 .. $#$children) {
1298 my $child = $children->[$_];
1299
1300 $h[$_] += ($h - $req_h) * $child->{expand} / $exp;
1301 }
1302 }
1303
1304 CFClient::UI::harmonize \@h;
1305
1306 my $y = 0;
1307 for (0 .. $#$children) {
1308 my $child = $children->[$_];
1309 my $h = $h[$_];
1310 $child->configure (0, $y, $w, $h);
1311
1312 $y += $h;
1313 }
1314
1315 1
1316} 1400}
1317 1401
1318############################################################################# 1402#############################################################################
1319 1403
1320package CFClient::UI::Label; 1404package CFClient::UI::Label;
1383 $self->{text} = "T$text"; 1467 $self->{text} = "T$text";
1384 1468
1385 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1469 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1386 $self->{layout}->set_text ($text); 1470 $self->{layout}->set_text ($text);
1387 1471
1472 $self->realloc;
1388 $self->update; 1473 $self->update;
1389 $self->check_size;
1390} 1474}
1391 1475
1392sub set_markup { 1476sub set_markup {
1393 my ($self, $markup) = @_; 1477 my ($self, $markup) = @_;
1394 1478
1398 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1482 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1399 1483
1400 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1484 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1401 $self->{layout}->set_markup ($markup); 1485 $self->{layout}->set_markup ($markup);
1402 1486
1487 $self->realloc;
1403 $self->update; 1488 $self->update;
1404 $self->check_size;
1405} 1489}
1406 1490
1407sub size_request { 1491sub size_request {
1408 my ($self) = @_; 1492 my ($self) = @_;
1409 1493
1430 $h + $self->{padding} * 2, 1514 $h + $self->{padding} * 2,
1431 ) 1515 )
1432} 1516}
1433 1517
1434sub size_allocate { 1518sub size_allocate {
1435 my ($self, $w, $h) = @_; 1519 my ($self, $w, $h, $changed) = @_;
1436 1520
1437 delete $self->{texture}; 1521 delete $self->{texture}
1522 if $changed;
1438} 1523}
1439 1524
1440sub set_fontsize { 1525sub set_fontsize {
1441 my ($self, $fontsize) = @_; 1526 my ($self, $fontsize) = @_;
1442 1527
1443 $self->{fontsize} = $fontsize; 1528 $self->{fontsize} = $fontsize;
1444 delete $self->{texture}; 1529 delete $self->{texture};
1445 1530
1446 $self->update; 1531 $self->realloc;
1447 $self->check_size;
1448} 1532}
1449 1533
1450sub _draw { 1534sub _draw {
1451 my ($self) = @_; 1535 my ($self) = @_;
1452 1536
1524 $self->{text} = $text; 1608 $self->{text} = $text;
1525 1609
1526 $text =~ s/./*/g if $self->{hidden}; 1610 $text =~ s/./*/g if $self->{hidden};
1527 $self->{layout}->set_text ("$text "); 1611 $self->{layout}->set_text ("$text ");
1528 1612
1529 $self->emit (changed => $self->{text}); 1613 $self->_emit (changed => $self->{text});
1530} 1614}
1531 1615
1532sub set_text { 1616sub set_text {
1533 my ($self, $text) = @_; 1617 my ($self, $text) = @_;
1534 1618
1535 $self->{cursor} = length $text; 1619 $self->{cursor} = length $text;
1536 $self->_set_text ($text); 1620 $self->_set_text ($text);
1537 $self->update; 1621
1538 $self->check_size; 1622 $self->realloc;
1539} 1623}
1540 1624
1541sub get_text { 1625sub get_text {
1542 $_[0]{text} 1626 $_[0]{text}
1543} 1627}
1546 my ($self) = @_; 1630 my ($self) = @_;
1547 1631
1548 my ($w, $h) = $self->SUPER::size_request; 1632 my ($w, $h) = $self->SUPER::size_request;
1549 1633
1550 ($w + 1, $h) # add 1 for cursor 1634 ($w + 1, $h) # add 1 for cursor
1551}
1552
1553sub size_allocate {
1554 my ($self, $w, $h) = @_;
1555
1556 $self->_set_text (delete $self->{text});#d# don't check for == inside _set_text
1557} 1635}
1558 1636
1559sub key_down { 1637sub key_down {
1560 my ($self, $ev) = @_; 1638 my ($self, $ev) = @_;
1561 1639
1576 } elsif ($sym == CFClient::SDLK_HOME) { 1654 } elsif ($sym == CFClient::SDLK_HOME) {
1577 $self->{cursor} = 0; 1655 $self->{cursor} = 0;
1578 } elsif ($sym == CFClient::SDLK_END) { 1656 } elsif ($sym == CFClient::SDLK_END) {
1579 $self->{cursor} = length $text; 1657 $self->{cursor} = length $text;
1580 } elsif ($uni == 27) { 1658 } elsif ($uni == 27) {
1581 $self->emit ('escape'); 1659 $self->_emit ('escape');
1582 } elsif ($uni) { 1660 } elsif ($uni) {
1583 substr $text, $self->{cursor}++, 0, chr $uni; 1661 substr $text, $self->{cursor}++, 0, chr $uni;
1584 } 1662 }
1585 1663
1586 $self->_set_text ($text); 1664 $self->_set_text ($text);
1587 $self->update; 1665
1588 $self->check_size; 1666 $self->realloc;
1589} 1667}
1590 1668
1591sub focus_in { 1669sub focus_in {
1592 my ($self) = @_; 1670 my ($self) = @_;
1593 1671
1673 if ($sym == 13) { 1751 if ($sym == 13) {
1674 unshift @{$self->{history}}, 1752 unshift @{$self->{history}},
1675 my $txt = $self->get_text; 1753 my $txt = $self->get_text;
1676 $self->{history_pointer} = -1; 1754 $self->{history_pointer} = -1;
1677 $self->{history_saveback} = ''; 1755 $self->{history_saveback} = '';
1678 $self->emit (activate => $txt); 1756 $self->_emit (activate => $txt);
1679 $self->update; 1757 $self->update;
1680 1758
1681 } elsif ($sym == CFClient::SDLK_UP) { 1759 } elsif ($sym == CFClient::SDLK_UP) {
1682 if ($self->{history_pointer} < 0) { 1760 if ($self->{history_pointer} < 0) {
1683 $self->{history_saveback} = $self->get_text; 1761 $self->{history_saveback} = $self->get_text;
1731 can_events => 1, 1809 can_events => 1,
1732 @_ 1810 @_
1733 ) 1811 )
1734} 1812}
1735 1813
1814sub activate { }
1815
1736sub button_up { 1816sub button_up {
1737 my ($self, $ev, $x, $y) = @_; 1817 my ($self, $ev, $x, $y) = @_;
1738 1818
1819 $self->emit ("activate")
1739 if ($x >= 0 && $x < $self->{w} 1820 if $x >= 0 && $x < $self->{w}
1740 && $y >= 0 && $y < $self->{h}) { 1821 && $y >= 0 && $y < $self->{h};
1741 $self->emit ("activate");
1742 }
1743} 1822}
1744 1823
1745sub _draw { 1824sub _draw {
1746 my ($self) = @_; 1825 my ($self) = @_;
1747 1826
1799 my ($self, $ev, $x, $y) = @_; 1878 my ($self, $ev, $x, $y) = @_;
1800 1879
1801 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1880 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding}
1802 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1881 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) {
1803 $self->{state} = !$self->{state}; 1882 $self->{state} = !$self->{state};
1804 $self->emit (changed => $self->{state}); 1883 $self->_emit (changed => $self->{state});
1805 } 1884 }
1806} 1885}
1807 1886
1808sub _draw { 1887sub _draw {
1809 my ($self) = @_; 1888 my ($self) = @_;
2099 $self->update; 2178 $self->update;
2100 2179
2101 $self 2180 $self
2102} 2181}
2103 2182
2183sub changed { }
2184
2104sub set_range { 2185sub set_range {
2105 my ($self, $range) = @_; 2186 my ($self, $range) = @_;
2106 2187
2107 $self->{range} = $range; 2188 ($range, $self->{range}) = ($self->{range}, $range);
2108 2189
2109 $self->update; 2190 $self->update
2191 if "@$range" ne "@{$self->{range}}";
2110} 2192}
2111 2193
2112sub set_value { 2194sub set_value {
2113 my ($self, $value) = @_; 2195 my ($self, $value) = @_;
2114 2196
2125 if $unit; 2207 if $unit;
2126 2208
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2209 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128 2210
2129 if ($value != $old_value) { 2211 if ($value != $old_value) {
2130 $self->emit (changed => $value); 2212 $self->_emit (changed => $value);
2131 $self->update; 2213 $self->update;
2132 } 2214 }
2133} 2215}
2134 2216
2135sub size_request { 2217sub size_request {
2296 $self->{fontsize} = $fontsize; 2378 $self->{fontsize} = $fontsize;
2297 $self->reflow; 2379 $self->reflow;
2298} 2380}
2299 2381
2300sub size_allocate { 2382sub size_allocate {
2301 my ($self, $w, $h) = @_; 2383 my ($self, $w, $h, $changed) = @_;
2302 2384
2303 $self->SUPER::size_allocate ($w, $h); 2385 $self->SUPER::size_allocate ($w, $h, $changed);
2386
2387 return unless $changed;
2304 2388
2305 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2389 $self->{layout}->set_font ($self->{font}) if $self->{font};
2306 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2390 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2307 $self->{layout}->set_width ($self->{children}[0]{w}); 2391 $self->{layout}->set_width ($self->{children}[0]{w});
2308 2392
2497 2581
2498sub new { 2582sub new {
2499 my $class = shift; 2583 my $class = shift;
2500 2584
2501 my $self = $class->SUPER::new ( 2585 my $self = $class->SUPER::new (
2502 state => 0, 2586 state => 0,
2503 connect_activate => \&toggle_flopper, 2587 on_activate => \&toggle_flopper,
2504 @_ 2588 @_
2505 ); 2589 );
2506 2590
2507 if ($self->{state}) {
2508 $self->{state} = 0;
2509 $self->toggle_flopper;
2510 }
2511
2512 $self 2591 $self
2513} 2592}
2514 2593
2515sub toggle_flopper { 2594sub toggle_flopper {
2516 my ($self) = @_; 2595 my ($self) = @_;
2517 2596
2518 # TODO: use animation 2597 $self->{other}->toggle_visibility;
2519 if ($self->{state} = !$self->{state}) {
2520 $CFClient::UI::ROOT->add ($self->{other});
2521 $self->{other}->move ($self->coord2global (0, $self->{h}));
2522 $self->emit ("open");
2523 } else {
2524 $CFClient::UI::ROOT->remove ($self->{other});
2525 $self->emit ("close");
2526 }
2527
2528 $self->emit (changed => $self->{state});
2529} 2598}
2530 2599
2531############################################################################# 2600#############################################################################
2532 2601
2533package CFClient::UI::Tooltip; 2602package CFClient::UI::Tooltip;
2565 2634
2566 ($w + 4, $h + 4) 2635 ($w + 4, $h + 4)
2567} 2636}
2568 2637
2569sub size_allocate { 2638sub size_allocate {
2570 my ($self, $w, $h) = @_; 2639 my ($self, $w, $h, $changed) = @_;
2571 2640
2641 return unless $changed;
2642
2572 $self->SUPER::size_allocate ($w - 4, $h - 4); 2643 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2644}
2645
2646sub visibility_change {
2647 my ($self, $visible) = @_;
2648
2649 return unless $visible;
2650
2651 $self->{root}->on_post_alloc ("move_$self" => sub {
2652 my $widget = $self->{owner}
2653 or return;
2654
2655 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2656
2657 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2658 if $x + $self->{w} > $::WIDTH;
2659
2660 $self->move_abs ($x, $y);
2661 });
2573} 2662}
2574 2663
2575sub _draw { 2664sub _draw {
2576 my ($self) = @_; 2665 my ($self) = @_;
2577 2666
2594 glVertex $w, $h; 2683 glVertex $w, $h;
2595 glVertex $w, 0; 2684 glVertex $w, 0;
2596 glEnd; 2685 glEnd;
2597 2686
2598 glTranslate 2 - 0.375, 2 - 0.375; 2687 glTranslate 2 - 0.375, 2 - 0.375;
2688
2599 $self->SUPER::_draw; 2689 $self->SUPER::_draw;
2600} 2690}
2601 2691
2602############################################################################# 2692#############################################################################
2603 2693
2609 2699
2610sub new { 2700sub new {
2611 my $class = shift; 2701 my $class = shift;
2612 2702
2613 my $self = $class->SUPER::new ( 2703 my $self = $class->SUPER::new (
2614 aspect => 1, 2704 aspect => 1,
2705 can_events => 0,
2615 @_, 2706 @_,
2616 ); 2707 );
2617 2708
2618 if ($self->{anim} && $self->{animspeed}) { 2709 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self); 2710 Scalar::Util::weaken (my $widget = $self);
2678 $self->SUPER::DESTROY; 2769 $self->SUPER::DESTROY;
2679} 2770}
2680 2771
2681############################################################################# 2772#############################################################################
2682 2773
2683package CFClient::UI::InventoryItem; 2774package CFClient::UI::Inventory;
2684 2775
2685our @ISA = CFClient::UI::HBox::; 2776our @ISA = CFClient::UI::ScrolledWindow::;
2686
2687sub _item_to_desc {
2688 my ($item) = @_;
2689
2690 my $desc =
2691 $item->{nrof} < 2
2692 ? $item->{name}
2693 : "$item->{nrof} × $item->{name_pl}";
2694
2695 $item->{flags} & Crossfire::Protocol::F_OPEN
2696 and $desc .= " (open)";
2697 $item->{flags} & Crossfire::Protocol::F_APPLIED
2698 and $desc .= " (applied)";
2699 $item->{flags} & Crossfire::Protocol::F_UNPAID
2700 and $desc .= " (unpaid)";
2701 $item->{flags} & Crossfire::Protocol::F_MAGIC
2702 and $desc .= " (magic)";
2703 $item->{flags} & Crossfire::Protocol::F_CURSED
2704 and $desc .= " (cursed)";
2705 $item->{flags} & Crossfire::Protocol::F_DAMNED
2706 and $desc .= " (damned)";
2707 $item->{flags} & Crossfire::Protocol::F_LOCKED
2708 and $desc .= " *";
2709
2710 $desc
2711}
2712 2777
2713sub new { 2778sub new {
2714 my $class = shift; 2779 my $class = shift;
2715 2780
2716 my %args = @_;
2717
2718 my $item = delete $args{item};
2719
2720 my $desc = _item_to_desc ($item);
2721
2722 my $self = $class->SUPER::new ( 2781 my $self = $class->SUPER::new (
2723 can_hover => 1,
2724 can_events => 1,
2725 tooltip => ((CFClient::UI::Label::escape $desc)
2726 . "\n<small>leftclick - examine\nshift+leftclick - move/pickup/drop\nmiddle click - apply\nrightclick - menu</small>"),
2727 connect_button_down => sub {
2728 my ($self, $ev, $x, $y) = @_;
2729
2730 # todo: maybe put examine on 1? but should just be a tooltip :(
2731 if (($ev->{mod} & CFClient::KMOD_SHIFT) && $ev->{button} == 1) {
2732 my $targ = $::CONN->{player}{tag};
2733
2734 if ($item->{container} == $::CONN->{player}{tag}) {
2735 $targ = $main::OPENCONT;
2736 }
2737
2738 $::CONN->send ("move $targ $item->{tag} 0");
2739 } elsif ($ev->{button} == 1) {
2740 $::CONN->send ("examine $item->{tag}");
2741 } elsif ($ev->{button} == 2) {
2742 $::CONN->send ("apply $item->{tag}");
2743 } elsif ($ev->{button} == 3) {
2744 my @menu_items = (
2745 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2746 ["mark", sub { $::CONN->send ("mark ". pack "N", $item->{tag}) }],
2747 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2748 (
2749 $item->{flags} & Crossfire::Protocol::F_LOCKED
2750 ? (
2751 ["unlock", sub { $::CONN->send ("lock " . pack "CN", 0, $item->{tag}) }],
2752 )
2753 : (
2754 ["lock", sub { $::CONN->send ("lock " . pack "CN", 1, $item->{tag}) }],
2755 ["drop", sub { $::CONN->send ("move $main::OPENCONT $item->{tag} 0") }],
2756 )
2757 ),
2758 );
2759
2760 CFClient::UI::Menu->new (items => \@menu_items)->popup ($ev);
2761 }
2762
2763 1
2764 },
2765 %args
2766 );
2767
2768
2769 $self->add (new CFClient::UI::Face
2770 can_events => 0,
2771 face => $item->{face},
2772 anim => $item->{anim},
2773 animspeed => $item->{animspeed},
2774 );
2775
2776 $self->add ($self->{name_lbl} = new CFClient::UI::Label can_events => 0);
2777
2778 $self->{item} = $item;
2779
2780 $self->update_item;
2781
2782 $self
2783}
2784
2785sub update_item {
2786 my ($self) = @_;
2787
2788 my $desc = _item_to_desc ($self->{item});
2789
2790 $self->{name_lbl}->set_text ($desc);
2791}
2792
2793#############################################################################
2794
2795package CFClient::UI::Inventory;
2796
2797our @ISA = CFClient::UI::ScrolledWindow::;
2798
2799sub new {
2800 my $class = shift;
2801
2802 my $self = $class->SUPER::new (
2803 scrolled => (new CFClient::UI::Table), 2782 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2804 @_, 2783 @_,
2805 ); 2784 );
2806 2785
2807 $self 2786 $self
2808} 2787}
2818 or ($a->{name} cmp $b->{name}) 2797 or ($a->{name} cmp $b->{name})
2819 } @$items; 2798 } @$items;
2820 2799
2821 $self->{real_items} = \@items; 2800 $self->{real_items} = \@items;
2822 2801
2802 my $row = 0;
2823 for my $item (@items) { 2803 for my $item (@items) {
2824 $item->{item} = $item; 2804 CFClient::Item::update_widgets $item;
2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828 2805
2829 my $i = 0; 2806 $self->{scrolled}->add (0, $row, $item->{face_widget});
2830 for (@items) { 2807 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2831 $self->{scrolled}->add (0, $i, $_); 2808 $self->{scrolled}->add (2, $row, $item->{weight_widget});
2832 my $nrof = $_->{item}->{nrof} || 1;
2833 $self->{scrolled}->add (1, $i++, new CFClient::UI::Label text => ($_->{item}->{weight} * $nrof) / 1000);
2834 }
2835 2809
2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2810 $row++;
2837} 2811 }
2838
2839sub size_request {
2840 my ($self) = @_;
2841 ($self->{req_w}, $self->{req_h});
2842} 2812}
2843 2813
2844############################################################################# 2814#############################################################################
2845 2815
2846package CFClient::UI::Menu; 2816package CFClient::UI::Menu;
2881 2851
2882# popup given the event (must be a mouse button down event currently) 2852# popup given the event (must be a mouse button down event currently)
2883sub popup { 2853sub popup {
2884 my ($self, $ev) = @_; 2854 my ($self, $ev) = @_;
2885 2855
2886 $self->emit ("popdown"); 2856 $self->_emit ("popdown");
2887 2857
2888 # maybe save $GRAB? must be careful about events... 2858 # maybe save $GRAB? must be careful about events...
2889 $GRAB = $self; 2859 $GRAB = $self;
2890 $self->{button} = $ev->{button}; 2860 $self->{button} = $ev->{button};
2891 2861
2906 2876
2907 if ($ev->{button} == $self->{button}) { 2877 if ($ev->{button} == $self->{button}) {
2908 undef $GRAB; 2878 undef $GRAB;
2909 $self->hide; 2879 $self->hide;
2910 2880
2911 $self->emit ("popdown"); 2881 $self->_emit ("popdown");
2912 $self->{hover}[1]->() if $self->{hover}; 2882 $self->{hover}[1]->() if $self->{hover};
2913 } 2883 }
2914} 2884}
2915 2885
2916############################################################################# 2886#############################################################################
2978sub add { 2948sub add {
2979 my ($self, $text, %arg) = @_; 2949 my ($self, $text, %arg) = @_;
2980 2950
2981 $text =~ s/^\s+//; 2951 $text =~ s/^\s+//;
2982 $text =~ s/\s+$//; 2952 $text =~ s/\s+$//;
2953
2954 return unless $text;
2983 2955
2984 my $timeout = time + ((delete $arg{timeout}) || 60); 2956 my $timeout = time + ((delete $arg{timeout}) || 60);
2985 2957
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2958 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987 2959
3030use CFClient::OpenGL; 3002use CFClient::OpenGL;
3031 3003
3032sub new { 3004sub new {
3033 my $class = shift; 3005 my $class = shift;
3034 3006
3035 $class->SUPER::new ( 3007 my $self = $class->SUPER::new (
3008 visible => 1,
3036 @_, 3009 @_,
3037 ) 3010 );
3011
3012 Scalar::Util::weaken ($self->{root} = $self);
3013
3014 $self
3038} 3015}
3039 3016
3040sub configure { 3017sub configure {
3041 my ($self, $x, $y, $w, $h) = @_; 3018 my ($self, $x, $y, $w, $h) = @_;
3042 3019
3043 $self->{w} = $w; 3020 $self->{w} = $w;
3044 $self->{h} = $h; 3021 $self->{h} = $h;
3045} 3022}
3046 3023
3047sub check_size { 3024sub reconfigure {
3048 my ($self) = @_; 3025 my ($self) = @_;
3049 3026
3027 $self->SUPER::reconfigure;
3028
3050 $self->size_allocate ($self->{w}, $self->{h}) 3029 $self->size_allocate ($self->{w}, $self->{h}, 1)
3051 if $self->{w}; 3030 if $self->{w};
3052} 3031}
3053 3032
3054sub size_request { 3033sub size_request {
3055 my ($self) = @_; 3034 my ($self) = @_;
3056 3035
3057 ($self->{w}, $self->{h}) 3036 ($self->{w}, $self->{h})
3058} 3037}
3059 3038
3039sub _to_pixel {
3040 my ($coord, $size, $max) = @_;
3041
3042 $coord =
3043 $coord eq "center" ? ($max - $size) * 0.5
3044 : $coord eq "max" ? $max
3045 : $coord;
3046
3047 $coord = 0 if $coord < 0;
3048 $coord = $max - $size if $coord > $max - $size;
3049
3050 int $coord + 0.5
3051}
3052
3060sub size_allocate { 3053sub size_allocate {
3061 my ($self, $w, $h) = @_; 3054 my ($self, $w, $h, $changed) = @_;
3062 3055
3063 for my $child ($self->children) { 3056 for my $child ($self->children) {
3064 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3057 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3065 3058
3066 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3059 $X = $child->{force_x} if exists $child->{force_x};
3067 if exists $child->{req_x}; 3060 $Y = $child->{force_y} if exists $child->{force_y};
3068 3061
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3062 $X = _to_pixel $X, $W, $self->{w};
3070 if exists $child->{req_y}; 3063 $Y = _to_pixel $Y, $H, $self->{h};
3071
3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3073 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074 3064
3075 $child->configure ($X, $Y, $W, $H); 3065 $child->configure ($X, $Y, $W, $H);
3076 } 3066 }
3077} 3067}
3078 3068
3089} 3079}
3090 3080
3091sub update { 3081sub update {
3092 my ($self) = @_; 3082 my ($self) = @_;
3093 3083
3094 $self->check_size;
3095 $::WANT_REFRESH++; 3084 $::WANT_REFRESH++;
3096} 3085}
3097 3086
3098sub add { 3087sub add {
3099 my ($self, @children) = @_; 3088 my ($self, @children) = @_;
3100 3089
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106
3107 for my $child (@children) {
3108 $child->{is_toplevel} = 1; 3090 $_->{is_toplevel} = 1
3109 3091 for @children;
3110 # integerise window positions
3111 $child->{x} = int $child->{x};
3112 $child->{y} = int $child->{y};
3113 }
3114 3092
3115 $self->SUPER::add (@children); 3093 $self->SUPER::add (@children);
3116} 3094}
3117 3095
3118sub remove { 3096sub remove {
3119 my ($self, @children) = @_; 3097 my ($self, @children) = @_;
3120 3098
3121 $self->SUPER::remove (@children); 3099 $self->SUPER::remove (@children);
3100
3101 delete $self->{is_toplevel}
3102 for @children;
3122 3103
3123 while (@children) { 3104 while (@children) {
3124 my $w = pop @children; 3105 my $w = pop @children;
3125 push @children, $w->children; 3106 push @children, $w->children;
3126 delete $w->{visible}; 3107 $w->set_invisible;
3127 } 3108 }
3128} 3109}
3129 3110
3130sub on_refresh { 3111sub on_refresh {
3131 my ($self, $id, $cb) = @_; 3112 my ($self, $id, $cb) = @_;
3145 while ($self->{refresh_hook}) { 3126 while ($self->{refresh_hook}) {
3146 $_->() 3127 $_->()
3147 for values %{delete $self->{refresh_hook}}; 3128 for values %{delete $self->{refresh_hook}};
3148 } 3129 }
3149 3130
3150 if ($self->{check_size}) { 3131 if ($self->{realloc}) {
3151 my @queue = ([], []); 3132 my @queue;
3152 3133
3153 for (;;) { 3134 while () {
3154 if ($self->{check_size}) { 3135 if ($self->{realloc}) {
3155 # heuristic: check containers last 3136 #TODO use array-of-depth approach
3156 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3137
3157 for values %{delete $self->{check_size}} 3138 @queue = sort { $a->{visible} <=> $b->{visible} }
3139 @queue, values %{delete $self->{realloc}};
3158 } 3140 }
3159 3141
3160 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3142 my $widget = pop @queue || last;
3161 3143
3144 $widget->{visible} or last; # do not resize invisible widgets
3145
3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3146 my ($w, $h) = exists $widget->{force_w} && exists $widget->{force_h}
3163 ? @$widget{qw(user_w user_h)} 3147 ? @$widget{qw(force_w force_h)}
3164 : $widget->size_request; 3148 : $widget->size_request;
3165 3149
3166 if (delete $widget->{force_alloc} 3150 my $min_size = $widget->{is_toplevel} ? 16 : 0;
3167 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3151
3168 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3152 $w = $min_size if $w < $min_size;
3169 3153 $h = $min_size if $h < $min_size;
3154
3170 $widget->{req_w} = $w; 3155 $widget->{req_w} = $w;
3171 $widget->{req_h} = $h; 3156 $widget->{req_h} = $h;
3172 3157
3173 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3158 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3174 3159
3175 $widget->{parent}->check_size 3160 push @queue, $widget->{parent}
3176 if $widget->{parent}; 3161 if $widget->{parent};
3177 }
3178 } 3162 }
3179 } 3163 }
3180 3164
3181 while ($self->{size_alloc}) { 3165 while (my $size_alloc = delete $self->{size_alloc}) {
3182 for (values %{delete $self->{size_alloc}}) { 3166 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3183 my ($widget, $w, $h) = @$_; 3167 values %$size_alloc;
3168
3169 while () {
3170 my ($widget, $w, $h) = @{ pop @queue or last };
3171
3172 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3173 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3184 3174
3185 $w = 0 if $w < 0; 3175 $w = 0 if $w < 0;
3186 $h = 0 if $h < 0; 3176 $h = 0 if $h < 0;
3187 3177
3178 $w = int $w + 0.5;
3179 $h = int $h + 0.5;
3180
3181 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3182
3188 $widget->{w} = $w; 3183 $widget->{w} = $w;
3189 $widget->{h} = $h; 3184 $widget->{h} = $h;
3190 $widget->size_allocate ($w, $h); 3185
3191 $widget->emit (size_allocate => $w, $h); 3186 $widget->emit (size_allocate => $w, $h, $changed);
3192 } 3187 }
3193 } 3188 }
3194 3189
3195 while ($self->{post_alloc_hook}) { 3190 while ($self->{post_alloc_hook}) {
3196 $_->() 3191 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines