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.250 by root, Sun May 28 02:31:03 2006 UTC vs.
Revision 1.258 by root, Tue May 30 02:55:45 2006 UTC

17our $BUTTON_STATE; 17our $BUTTON_STATE;
18 18
19our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
20 20
21sub get_layout { 21sub get_layout {
22 my $layout;
23
22 for (grep { $_->{name} } values %WIDGET) { 24 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = { 25 my $win = $layout->{$_->{name}} = { };
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 }; 26
29 } 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};
30 31
31 return $LAYOUT; 32 $win->{show} = $_->{visible} && $_->{is_toplevel};
33 }
34
35 $layout
32} 36}
33 37
34sub set_layout { 38sub set_layout {
35 my ($layout) = @_; 39 my ($layout) = @_;
40
36 $LAYOUT = $layout; 41 $LAYOUT = $layout;
37} 42}
38 43
39sub check_tooltip { 44sub check_tooltip {
40 if (!$GRAB) { 45 if (!$GRAB) {
41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 46 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
42 if (length $widget->{tooltip}) { 47 if (length $widget->{tooltip}) {
43 48
44 if ($TOOLTIP->{owner} != $widget) { 49 if ($TOOLTIP->{owner} != $widget) {
50 $TOOLTIP->hide;
51
45 $TOOLTIP->{owner} = $widget; 52 $TOOLTIP->{owner} = $widget;
46 53
47 my $tip = $widget->{tooltip}; 54 my $tip = $widget->{tooltip};
48 55
49 $tip = $tip->($widget) if CODE:: eq ref $tip; 56 $tip = $tip->($widget) if CODE:: eq ref $tip;
50 57
51 $TOOLTIP->set_tooltip_from ($widget); 58 $TOOLTIP->set_tooltip_from ($widget);
52 $TOOLTIP->show; 59 $TOOLTIP->show;
53
54 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
55
56 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
57 if $x + $TOOLTIP->{w} > $::WIDTH;
58
59 $TOOLTIP->move ($x, $y);
60 $TOOLTIP->check_size;
61 $TOOLTIP->update;
62 } 60 }
63 61
64 return; 62 return;
65 } 63 }
66 } 64 }
172sub rescale_widgets { 170sub rescale_widgets {
173 my ($sx, $sy) = @_; 171 my ($sx, $sy) = @_;
174 172
175 for my $widget (values %WIDGET) { 173 for my $widget (values %WIDGET) {
176 if ($widget->{is_toplevel}) { 174 if ($widget->{is_toplevel}) {
175 $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
176 $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
177
177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 178 $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/;
178 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 179 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w};
179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; 180 $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w};
180 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; 181 $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/;
181 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 182 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h};
182 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; 183 $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h};
184
185 $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/;
186 $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/;
187
183 } 188 }
184 } 189 }
185 190
186 reconfigure_widgets; 191 reconfigure_widgets;
187} 192}
196 201
197sub new { 202sub new {
198 my $class = shift; 203 my $class = shift;
199 204
200 my $self = bless { 205 my $self = bless {
201 x => 0, 206 x => "center",
202 y => 0, 207 y => "center",
203 z => 0, 208 z => 0,
209 w => undef,
210 h => undef,
204 can_events => 1, 211 can_events => 1,
205 @_ 212 @_
206 }, $class; 213 }, $class;
214
215 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
207 216
208 for (keys %$self) { 217 for (keys %$self) {
209 if (/^on_(.*)$/) { 218 if (/^on_(.*)$/) {
210 $self->connect ($1 => delete $self->{$_}); 219 $self->connect ($1 => delete $self->{$_});
211 } 220 }
212 } 221 }
213 222
214 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
215
216 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { 223 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
217 $self->{req_x} = $layout->{x} * $::WIDTH; 224 $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x};
218 $self->{req_y} = $layout->{y} * $::HEIGHT; 225 $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y};
219 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; 226 $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w};
220 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; 227 $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h};
228
229 $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x};
230 $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y};
231
232 $self->show if $layout->{show};
221 } 233 }
222 234
223 $self 235 $self
224} 236}
225 237
236 return if $self->{parent}; 248 return if $self->{parent};
237 249
238 $CFClient::UI::ROOT->add ($self); 250 $CFClient::UI::ROOT->add ($self);
239} 251}
240 252
241sub center {
242 my ($self) = @_;
243
244 $CFClient::UI::ROOT->on_post_alloc (
245 "center_$self" => sub {
246 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
247 },
248 );
249
250 $self->update;
251}
252
253sub set_visible { 253sub set_visible {
254 my ($self) = @_; 254 my ($self) = @_;
255 255
256 return if $self->{visible}; 256 return if $self->{visible};
257 257
258 $self->{root} = $self->{parent}{root}; 258 $self->{root} = $self->{parent}{root};
259 $self->{visible} = $self->{parent}{visible} + 1; 259 $self->{visible} = $self->{parent}{visible} + 1;
260 260
261 $self->emit (visibility_change => 1); 261 $self->emit (visibility_change => 1);
262
263 $self->realloc if !exists $self->{req_w};
264
265 $_->set_visible for $self->children;
262} 266}
263 267
264sub set_invisible { 268sub set_invisible {
265 my ($self) = @_; 269 my ($self) = @_;
266 270
267 return unless $self->{visible}; 271 return unless $self->{visible};
268 272
269 # broken show/hide model 273 $_->set_invisible for $self->children;
270 274
271 delete $self->{root}; 275 delete $self->{root};
272 delete $self->{visible}; 276 delete $self->{visible};
273 277
274 undef $GRAB if $GRAB == $self; 278 undef $GRAB if $GRAB == $self;
275 undef $HOVER if $HOVER == $self; 279 undef $HOVER if $HOVER == $self;
276 280
277 CFClient::UI::check_tooltip 281 CFClient::UI::check_tooltip
278 if $CFClient::UI::TOOLTIP->{owner} == $self; 282 if $TOOLTIP->{owner} == $self;
279 283
280 $self->focus_out; 284 $self->focus_out;
281 285
282 $self->emit (visibility_change => 0); 286 $self->emit (visibility_change => 0);
283} 287}
306 310
307 $self->{parent}->remove ($self) 311 $self->{parent}->remove ($self)
308 if $self->{parent}; 312 if $self->{parent};
309} 313}
310 314
311sub move { 315sub move_abs {
312 my ($self, $x, $y, $z) = @_; 316 my ($self, $x, $y, $z) = @_;
313 317
314 $self->{x} = int $x; 318 $self->{x} = List::Util::max 0, int $x;
315 $self->{y} = int $y; 319 $self->{y} = List::Util::max 0, int $y;
316 $self->{z} = $z if defined $z; 320 $self->{z} = $z if defined $z;
317 321
318 $self->update; 322 $self->update;
319} 323}
320 324
321sub set_size { 325sub set_size {
322 my ($self, $w, $h) = @_; 326 my ($self, $w, $h) = @_;
323 327
324 $self->{def_w} = $w; 328 $self->{force_w} = $w;
325 $self->{def_h} = $h; 329 $self->{force_h} = $h;
326 330
327 $self->check_size; 331 $self->realloc;
328} 332}
329 333
330sub size_request { 334sub size_request {
331 require Carp; 335 require Carp;
332 Carp::confess "size_request is abstract"; 336 Carp::confess "size_request is abstract";
334 338
335sub configure { 339sub configure {
336 my ($self, $x, $y, $w, $h) = @_; 340 my ($self, $x, $y, $w, $h) = @_;
337 341
338 if ($self->{aspect}) { 342 if ($self->{aspect}) {
343 my ($ow, $oh) = ($w, $h);
344
339 my $w2 = List::Util::min $w, int $h * $self->{aspect}; 345 $w = List::Util::min $w, int $h * $self->{aspect};
340 my $h2 = List::Util::min $h, int $w / $self->{aspect}; 346 $h = List::Util::min $h, int $w / $self->{aspect};
341 347
342 # use alignment to adjust x, y 348 # use alignment to adjust x, y
343 349
344 $x += int +($w - $w2) * 0.5; 350 $x += int 0.5 * ($ow - $w);
345 $y += int +($h - $h2) * 0.5; 351 $y += int 0.5 * ($oh - $h);
346
347 ($w, $h) = ($w2, $h2);
348 } 352 }
349 353
350 if ($self->{x} != $x || $self->{y} != $y) { 354 if ($self->{x} ne $x || $self->{y} ne $y) {
351 $self->{x} = $x; 355 $self->{x} = $x;
352 $self->{y} = $y; 356 $self->{y} = $y;
353 $self->update; 357 $self->update;
354 } 358 }
355 359
356 if ($self->{w} != $w || $self->{h} != $h) { 360 if ($self->{w} != $w || $self->{h} != $h) {
361 return unless $self->{visible};
362
357 $CFClient::UI::ROOT->{size_alloc}{$self+0} = [$self, $w, $h]; 363 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
358 } 364 }
359} 365}
360 366
361sub size_allocate { 367sub size_allocate {
362 # nothing to be done 368 # nothing to be done
363}
364
365sub reconfigure {
366 my ($self) = @_;
367
368 $self->check_size (1);
369 $self->update;
370} 369}
371 370
372sub children { 371sub children {
373} 372}
374 373
452sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 451sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
453sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 452sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
454sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 453sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
455sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 454sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
456sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 455sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
456
457sub find_widget {
458 my ($self, $x, $y) = @_;
459
460 return () unless $self->{can_events};
461
462 return $self
463 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
464 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
465
466 ()
467}
468
469sub set_parent {
470 my ($self, $parent) = @_;
471
472 Scalar::Util::weaken ($self->{parent} = $parent);
473 $self->set_visible if $parent->{visible};
474}
475
476sub connect {
477 my ($self, $signal, $cb) = @_;
478
479 push @{ $self->{signal_cb}{$signal} }, $cb;
480}
481
482sub _emit {
483 my ($self, $signal, @args) = @_;
484
485 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
486}
487
488sub emit {
489 my ($self, $signal, @args) = @_;
490
491 $self->_emit ($signal, @args)
492 || $self->$signal (@args);
493}
494
495sub visibility_change {
496 #my ($self, $visible) = @_;
497}
498
499sub realloc {
500 my ($self) = @_;
501
502 if ($self->{visible}) {
503 return if $self->{root}{realloc}{$self};
504
505 $self->{root}{realloc}{$self} = $self;
506 $self->{root}->update;
507 } else {
508 delete $self->{req_w};
509 }
510}
511
512sub update {
513 my ($self) = @_;
514
515 $self->{parent}->update
516 if $self->{parent};
517}
518
519sub reconfigure {
520 my ($self) = @_;
521
522 $self->realloc;
523 $self->update;
524}
457 525
458sub draw { 526sub draw {
459 my ($self) = @_; 527 my ($self) = @_;
460 528
461 return unless $self->{h} && $self->{w}; 529 return unless $self->{h} && $self->{w};
478 glVertex $x , $y + $self->{h}; 546 glVertex $x , $y + $self->{h};
479 glEnd; 547 glEnd;
480 glDisable GL_BLEND; 548 glDisable GL_BLEND;
481 } 549 }
482 550
483 if ($ENV{PCLIENT_DEBUG}) { 551 if ($ENV{CFPLUS_DEBUG}) {
484 glPushMatrix; 552 glPushMatrix;
485 glColor 1, 1, 0, 1; 553 glColor 1, 1, 0, 1;
486 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 554 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
487 glBegin GL_LINE_LOOP; 555 glBegin GL_LINE_LOOP;
488 glVertex 0 , 0; 556 glVertex 0 , 0;
499 my ($self) = @_; 567 my ($self) = @_;
500 568
501 warn "no draw defined for $self\n"; 569 warn "no draw defined for $self\n";
502} 570}
503 571
504sub find_widget {
505 my ($self, $x, $y) = @_;
506
507 return () unless $self->{can_events};
508
509 return $self
510 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
511 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
512
513 ()
514}
515
516sub set_parent {
517 my ($self, $parent) = @_;
518
519 Scalar::Util::weaken ($self->{parent} = $parent);
520
521 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
522
523 $self->check_size;
524}
525
526sub check_size {
527 my ($self, $forced) = @_;
528
529 $self->{force_alloc} = 1 if $forced;
530 $CFClient::UI::ROOT->{check_size}{$self} = $self;
531}
532
533sub update {
534 my ($self) = @_;
535
536 $self->{parent}->update
537 if $self->{parent};
538}
539
540sub connect {
541 my ($self, $signal, $cb) = @_;
542
543 push @{ $self->{signal_cb}{$signal} }, $cb;
544}
545
546sub _emit {
547 my ($self, $signal, @args) = @_;
548
549 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
550}
551
552sub emit {
553 my ($self, $signal, @args) = @_;
554
555 $self->_emit ($signal, @args)
556 || $self->$signal (@args);
557}
558
559sub visibility_change {
560 #my ($self, $visible) = @_;
561}
562
563sub DESTROY { 572sub DESTROY {
564 my ($self) = @_; 573 my ($self) = @_;
565 574
566 delete $WIDGET{$self+0}; 575 delete $WIDGET{$self+0};
567 #$self->deactivate; 576 #$self->deactivate;
623 my ($class, %arg) = @_; 632 my ($class, %arg) = @_;
624 $class->SUPER::new (can_events => 0, %arg); 633 $class->SUPER::new (can_events => 0, %arg);
625} 634}
626 635
627sub size_request { 636sub size_request {
628 (0, 0) 637 my ($self) = @_;
638
639 ($self->{w} + 0, $self->{h} + 0)
629} 640}
630 641
631sub draw { } 642sub draw { }
632 643
633############################################################################# 644#############################################################################
662 $self->{children} = [ 673 $self->{children} = [
663 sort { $a->{z} <=> $b->{z} } 674 sort { $a->{z} <=> $b->{z} }
664 @{$self->{children}}, @widgets 675 @{$self->{children}}, @widgets
665 ]; 676 ];
666 677
667 $self->check_size (1); 678 $self->realloc;
668 $self->update;
669} 679}
670 680
671sub children { 681sub children {
672 @{ $_[0]{children} } 682 @{ $_[0]{children} }
673} 683}
678 delete $child->{parent}; 688 delete $child->{parent};
679 $child->hide; 689 $child->hide;
680 690
681 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 691 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
682 692
683 $self->check_size (1); 693 $self->realloc;
684 $self->update;
685} 694}
686 695
687sub clear { 696sub clear {
688 my ($self) = @_; 697 my ($self) = @_;
689 698
693 for (@$children) { 702 for (@$children) {
694 delete $_->{parent}; 703 delete $_->{parent};
695 $_->hide; 704 $_->hide;
696 } 705 }
697 706
698 $self->check_size; 707 $self->realloc;
699 $self->update;
700} 708}
701 709
702sub find_widget { 710sub find_widget {
703 my ($self, $x, $y) = @_; 711 my ($self, $x, $y) = @_;
704 712
757sub size_request { 765sub size_request {
758 $_[0]{children}[0]->size_request 766 $_[0]{children}[0]->size_request
759} 767}
760 768
761sub size_allocate { 769sub size_allocate {
762 my ($self, $w, $h) = @_; 770 my ($self, $w, $h, $changed) = @_;
763 771
764 $self->{children}[0]->configure (0, 0, $w, $h); 772 $self->{children}[0]->configure (0, 0, $w, $h);
765} 773}
766 774
767############################################################################# 775#############################################################################
784 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 792 $ROOT->on_post_alloc ($self => sub { $self->render_child });
785 $self->SUPER::update; 793 $self->SUPER::update;
786} 794}
787 795
788sub size_allocate { 796sub size_allocate {
789 my ($self, $w, $h) = @_; 797 my ($self, $w, $h, $changed) = @_;
790 798
791 $self->SUPER::size_allocate ($w, $h); 799 $self->SUPER::size_allocate ($w, $h, $changed);
792 $self->update; 800 $self->update
801 if $changed;
793} 802}
794 803
795sub _render { 804sub _render {
796 $_[0]{children}[0]->draw; 805 $_[0]{children}[0]->draw;
797} 806}
850 859
851 ($w, $h) 860 ($w, $h)
852} 861}
853 862
854sub size_allocate { 863sub size_allocate {
855 my ($self, $w, $h) = @_; 864 my ($self, $w, $h, $changed) = @_;
856 865
857 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 866 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
858 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 867 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
859 868
860 $self->child->configure (0, 0, $w, $h); 869 $self->child->configure (0, 0, $w, $h);
947 my $child = $self->{vp}->child; 956 my $child = $self->{vp}->child;
948 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 957 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
949} 958}
950 959
951sub size_allocate { 960sub size_allocate {
952 my ($self, $w, $h) = @_; 961 my ($self, $w, $h, $changed) = @_;
953 962
954 $self->SUPER::size_allocate ($w, $h); 963 $self->SUPER::size_allocate ($w, $h, $changed);
955 964
956 my $child = $self->{vp}->child; 965 my $child = $self->{vp}->child;
957 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 966 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
958} 967}
959 968
1006 1015
1007our @ISA = CFClient::UI::Bin::; 1016our @ISA = CFClient::UI::Bin::;
1008 1017
1009use CFClient::OpenGL; 1018use CFClient::OpenGL;
1010 1019
1011my @tex = 1020my $bg =
1021 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1022 mipmap => 1, wrap => 1;
1023
1024my @border =
1012 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1025 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1013 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); 1026 qw(d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
1014 1027
1015sub new { 1028sub new {
1016 my $class = shift; 1029 my $class = shift;
1017 1030
1018 my $self = $class->SUPER::new ( 1031 my $self = $class->SUPER::new (
1019 bg => [1, 1, 1, 1], 1032 bg => [1, 1, 1, 1],
1020 border_bg => [1, 1, 1, 1], 1033 border_bg => [1, 1, 1, 1],
1021 border => 0.6, 1034 border => 0.6,
1022 is_toplevel => 1,
1023 can_events => 1, 1035 can_events => 1,
1036 min_w => 16,
1037 min_h => 16,
1024 @_ 1038 @_
1025 ); 1039 );
1026 1040
1027 $self->{title} &&= new CFClient::UI::Label 1041 $self->{title} &&= new CFClient::UI::Label
1028 align => 0, 1042 align => 0,
1047 $h + $self->border * 2, 1061 $h + $self->border * 2,
1048 ) 1062 )
1049} 1063}
1050 1064
1051sub size_allocate { 1065sub size_allocate {
1052 my ($self, $w, $h) = @_; 1066 my ($self, $w, $h, $changed) = @_;
1067
1068 return unless $changed;
1053 1069
1054 $h -= List::Util::max 0, $self->border * 2; 1070 $h -= List::Util::max 0, $self->border * 2;
1055 $w -= List::Util::max 0, $self->border * 2; 1071 $w -= List::Util::max 0, $self->border * 2;
1056 1072
1057 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1073 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1081 my ($ev, $x, $y) = @_; 1097 my ($ev, $x, $y) = @_;
1082 1098
1083 my $dx = $ev->{x} - $ox; 1099 my $dx = $ev->{x} - $ox;
1084 my $dy = $ev->{y} - $oy; 1100 my $dy = $ev->{y} - $oy;
1085 1101
1086 $self->{user_x} = $wx + $dx * $mx;
1087 $self->{user_y} = $wy + $dy * $my;
1088 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1102 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1089 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1103 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1090 $self->move ($self->{user_x}, $self->{user_y}); 1104
1091 $self->check_size; 1105 $self->realloc;
1106 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1092 }; 1107 };
1093 1108
1094 } elsif ($lr ^ $td) { 1109 } elsif ($lr ^ $td) {
1095 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1110 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1096 my ($bx, $by) = ($self->{x}, $self->{y}); 1111 my ($bx, $by) = ($self->{x}, $self->{y});
1098 $self->{motion} = sub { 1113 $self->{motion} = sub {
1099 my ($ev, $x, $y) = @_; 1114 my ($ev, $x, $y) = @_;
1100 1115
1101 ($x, $y) = ($ev->{x}, $ev->{y}); 1116 ($x, $y) = ($ev->{x}, $ev->{y});
1102 1117
1103 $self->{user_x} = $bx + $x - $ox; 1118 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1104 $self->{user_y} = $by + $y - $oy;
1105
1106 $self->move ($self->{user_x}, $self->{user_y});
1107 $self->update;
1108 }; 1119 };
1109 } 1120 }
1110} 1121}
1111 1122
1112sub button_up { 1123sub button_up {
1131 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1142 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1132 1143
1133 my $border = $self->border; 1144 my $border = $self->border;
1134 1145
1135 glColor @{ $self->{border_bg} }; 1146 glColor @{ $self->{border_bg} };
1136 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1147 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1137 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1148 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1138 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1149 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1139 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1150 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1140 1151
1141 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1152 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1142 my $bg = $tex[0]; 1153 glColor @{ $self->{bg} };
1143 1154
1144 # TODO: repeat texture not scale 1155 # TODO: repeat texture not scale
1156 # solve this better(?)
1145 my $rep_x = $cw / $bg->{w}; 1157 $bg->{s} = $cw / $bg->{w};
1146 my $rep_y = $ch / $bg->{h}; 1158 $bg->{t} = $ch / $bg->{h};
1147
1148 glColor @{ $self->{bg} };
1149
1150 $bg->{s} = $rep_x;
1151 $bg->{t} = $rep_y;
1152 $bg->{wrap_mode} = 1;
1153 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1159 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1154 } 1160 }
1155 1161
1156 glDisable GL_TEXTURE_2D; 1162 glDisable GL_TEXTURE_2D;
1157 1163
1187 my ($self, $x, $y, $child) = @_; 1193 my ($self, $x, $y, $child) = @_;
1188 1194
1189 $child->set_parent ($self); 1195 $child->set_parent ($self);
1190 $self->{children}[$y][$x] = $child; 1196 $self->{children}[$y][$x] = $child;
1191 1197
1192 $self->check_size (1); 1198 $self->realloc;
1193} 1199}
1194 1200
1195# TODO: move to container class maybe? send children a signal on removal? 1201# TODO: move to container class maybe? send children a signal on removal?
1196sub clear { 1202sub clear {
1197 my ($self) = @_; 1203 my ($self) = @_;
1202 for (@children) { 1208 for (@children) {
1203 delete $_->{parent}; 1209 delete $_->{parent};
1204 $_->hide; 1210 $_->hide;
1205 } 1211 }
1206 1212
1207 $self->check_size (1); 1213 $self->realloc;
1208 $self->update;
1209} 1214}
1210 1215
1211sub get_wh { 1216sub get_wh {
1212 my ($self) = @_; 1217 my ($self) = @_;
1213 1218
1240 (sum @$hs), 1245 (sum @$hs),
1241 ) 1246 )
1242} 1247}
1243 1248
1244sub size_allocate { 1249sub size_allocate {
1245 my ($self, $w, $h) = @_; 1250 my ($self, $w, $h, $changed) = @_;
1246 1251
1247 my ($ws, $hs) = $self->get_wh; 1252 my ($ws, $hs) = $self->get_wh;
1248 1253
1249 my $req_w = (sum @$ws) || 1; 1254 my $req_w = (sum @$ws) || 1;
1250 my $req_h = (sum @$hs) || 1; 1255 my $req_h = (sum @$hs) || 1;
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1333 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 ) 1334 )
1330} 1335}
1331 1336
1332sub size_allocate { 1337sub size_allocate {
1333 my ($self, $w, $h) = @_; 1338 my ($self, $w, $h, $changed) = @_;
1334 1339
1335 my $space = $self->{vertical} ? $h : $w; 1340 my $space = $self->{vertical} ? $h : $w;
1336 my $children = $self->{children}; 1341 my $children = $self->{children};
1337 1342
1338 my @req; 1343 my @req;
1421 ellipsise => 3, # end 1426 ellipsise => 3, # end
1422 layout => (new CFClient::Layout), 1427 layout => (new CFClient::Layout),
1423 fontsize => 1, 1428 fontsize => 1,
1424 align => -1, 1429 align => -1,
1425 valign => -1, 1430 valign => -1,
1426 padding => 2, 1431 padding_x => 2,
1432 padding_y => 2,
1427 can_events => 0, 1433 can_events => 0,
1428 %arg 1434 %arg
1429 ); 1435 );
1430 1436
1431 if (exists $self->{template}) { 1437 if (exists $self->{template}) {
1467 $self->{text} = "T$text"; 1473 $self->{text} = "T$text";
1468 1474
1469 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1475 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1470 $self->{layout}->set_text ($text); 1476 $self->{layout}->set_text ($text);
1471 1477
1478 $self->realloc;
1472 $self->update; 1479 $self->update;
1473 $self->check_size;
1474} 1480}
1475 1481
1476sub set_markup { 1482sub set_markup {
1477 my ($self, $markup) = @_; 1483 my ($self, $markup) = @_;
1478 1484
1482 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1488 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1483 1489
1484 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; 1490 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1485 $self->{layout}->set_markup ($markup); 1491 $self->{layout}->set_markup ($markup);
1486 1492
1493 $self->realloc;
1487 $self->update; 1494 $self->update;
1488 $self->check_size;
1489} 1495}
1490 1496
1491sub size_request { 1497sub size_request {
1492 my ($self) = @_; 1498 my ($self) = @_;
1493 1499
1507 1513
1508 $w = List::Util::max $w, $w2; 1514 $w = List::Util::max $w, $w2;
1509 $h = List::Util::max $h, $h2; 1515 $h = List::Util::max $h, $h2;
1510 } 1516 }
1511 1517
1512 ( 1518 ($w, $h)
1513 $w + $self->{padding} * 2,
1514 $h + $self->{padding} * 2,
1515 )
1516} 1519}
1517 1520
1518sub size_allocate { 1521sub size_allocate {
1519 my ($self, $w, $h) = @_; 1522 my ($self, $w, $h, $changed) = @_;
1520 1523
1521 delete $self->{texture}; 1524 delete $self->{texture}
1525 if $changed;
1522} 1526}
1523 1527
1524sub set_fontsize { 1528sub set_fontsize {
1525 my ($self, $fontsize) = @_; 1529 my ($self, $fontsize) = @_;
1526 1530
1527 $self->{fontsize} = $fontsize; 1531 $self->{fontsize} = $fontsize;
1528 delete $self->{texture}; 1532 delete $self->{texture};
1529 1533
1530 $self->update; 1534 $self->realloc;
1531 $self->check_size;
1532} 1535}
1533 1536
1534sub _draw { 1537sub _draw {
1535 my ($self) = @_; 1538 my ($self) = @_;
1536 1539
1544 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1547 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1545 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1548 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1546 1549
1547 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1550 my $tex = new_from_layout CFClient::Texture $self->{layout};
1548 1551
1549 $self->{ox} = int ($self->{align} < 0 ? $self->{padding} 1552 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1550 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1553 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1551 : ($self->{w} - $tex->{w}) * 0.5); 1554 : ($self->{w} - $tex->{w}) * 0.5);
1552 1555
1553 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} 1556 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1554 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1557 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1555 : ($self->{h} - $tex->{h}) * 0.5); 1558 : ($self->{h} - $tex->{h}) * 0.5);
1556 1559
1557 $tex 1560 $tex
1558 }; 1561 };
1559 1562
1616sub set_text { 1619sub set_text {
1617 my ($self, $text) = @_; 1620 my ($self, $text) = @_;
1618 1621
1619 $self->{cursor} = length $text; 1622 $self->{cursor} = length $text;
1620 $self->_set_text ($text); 1623 $self->_set_text ($text);
1621 $self->update; 1624
1622 $self->check_size; 1625 $self->realloc;
1623} 1626}
1624 1627
1625sub get_text { 1628sub get_text {
1626 $_[0]{text} 1629 $_[0]{text}
1627} 1630}
1660 } elsif ($uni) { 1663 } elsif ($uni) {
1661 substr $text, $self->{cursor}++, 0, chr $uni; 1664 substr $text, $self->{cursor}++, 0, chr $uni;
1662 } 1665 }
1663 1666
1664 $self->_set_text ($text); 1667 $self->_set_text ($text);
1665 $self->update; 1668
1666 $self->check_size; 1669 $self->realloc;
1667} 1670}
1668 1671
1669sub focus_in { 1672sub focus_in {
1670 my ($self) = @_; 1673 my ($self) = @_;
1671 1674
1798 1801
1799sub new { 1802sub new {
1800 my $class = shift; 1803 my $class = shift;
1801 1804
1802 $class->SUPER::new ( 1805 $class->SUPER::new (
1803 padding => 4, 1806 padding_x => 4,
1807 padding_y => 4,
1804 fg => [1, 1, 1], 1808 fg => [1, 1, 1],
1805 active_fg => [0, 0, 1], 1809 active_fg => [0, 0, 1],
1806 can_hover => 1, 1810 can_hover => 1,
1807 align => 0, 1811 align => 0,
1808 valign => 0, 1812 valign => 0,
1855 1859
1856sub new { 1860sub new {
1857 my $class = shift; 1861 my $class = shift;
1858 1862
1859 $class->SUPER::new ( 1863 $class->SUPER::new (
1860 padding => 2, 1864 padding_x => 2,
1865 padding_y => 2,
1861 fg => [1, 1, 1], 1866 fg => [1, 1, 1],
1862 active_fg => [1, 1, 0], 1867 active_fg => [1, 1, 0],
1863 bg => [0, 0, 0, 0.2], 1868 bg => [0, 0, 0, 0.2],
1864 active_bg => [1, 1, 1, 0.5], 1869 active_bg => [1, 1, 1, 0.5],
1865 state => 0, 1870 state => 0,
1869} 1874}
1870 1875
1871sub size_request { 1876sub size_request {
1872 my ($self) = @_; 1877 my ($self) = @_;
1873 1878
1874 ($self->{padding} * 2 + 6) x 2 1879 (6) x 2
1875} 1880}
1876 1881
1877sub button_down { 1882sub button_down {
1878 my ($self, $ev, $x, $y) = @_; 1883 my ($self, $ev, $x, $y) = @_;
1879 1884
1880 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1885 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1881 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1886 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1882 $self->{state} = !$self->{state}; 1887 $self->{state} = !$self->{state};
1883 $self->_emit (changed => $self->{state}); 1888 $self->_emit (changed => $self->{state});
1884 } 1889 }
1885} 1890}
1886 1891
1887sub _draw { 1892sub _draw {
1888 my ($self) = @_; 1893 my ($self) = @_;
1889 1894
1890 $self->SUPER::_draw; 1895 $self->SUPER::_draw;
1891 1896
1892 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1897 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
1893 1898
1894 my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; 1899 my ($w, $h) = @$self{qw(w h)};
1900
1901 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
1895 1902
1896 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1903 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1897 1904
1898 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1905 my $tex = $self->{state} ? $tex[1] : $tex[0];
1899 1906
2164 fg => [1, 1, 1], 2171 fg => [1, 1, 1],
2165 active_fg => [0, 0, 0], 2172 active_fg => [0, 0, 0],
2166 bg => [0, 0, 0, 0.2], 2173 bg => [0, 0, 0, 0.2],
2167 active_bg => [1, 1, 1, 0.5], 2174 active_bg => [1, 1, 1, 0.5],
2168 range => [0, 0, 100, 10, 0], 2175 range => [0, 0, 100, 10, 0],
2169 req_w => $::WIDTH / 80, 2176 min_w => $::WIDTH / 80,
2170 req_h => $::WIDTH / 80, 2177 min_h => $::WIDTH / 80,
2171 vertical => 0, 2178 vertical => 0,
2172 can_hover => 1, 2179 can_hover => 1,
2173 inner_pad => 0.02, 2180 inner_pad => 0.02,
2174 @_ 2181 @_
2175 ); 2182 );
2178 $self->update; 2185 $self->update;
2179 2186
2180 $self 2187 $self
2181} 2188}
2182 2189
2190sub changed { }
2191
2183sub set_range { 2192sub set_range {
2184 my ($self, $range) = @_; 2193 my ($self, $range) = @_;
2185 2194
2186 ($range, $self->{range}) = ($self->{range}, $range); 2195 ($range, $self->{range}) = ($self->{range}, $range);
2187 2196
2213} 2222}
2214 2223
2215sub size_request { 2224sub size_request {
2216 my ($self) = @_; 2225 my ($self) = @_;
2217 2226
2218 my $w = $self->{req_w}; 2227 ($self->{req_w}, $self->{req_h})
2219 my $h = $self->{req_h};
2220
2221 $self->{vertical} ? ($h, $w) : ($w, $h)
2222} 2228}
2223 2229
2224sub button_down { 2230sub button_down {
2225 my ($self, $ev, $x, $y) = @_; 2231 my ($self, $ev, $x, $y) = @_;
2226 2232
2376 $self->{fontsize} = $fontsize; 2382 $self->{fontsize} = $fontsize;
2377 $self->reflow; 2383 $self->reflow;
2378} 2384}
2379 2385
2380sub size_allocate { 2386sub size_allocate {
2381 my ($self, $w, $h) = @_; 2387 my ($self, $w, $h, $changed) = @_;
2382 2388
2383 $self->SUPER::size_allocate ($w, $h); 2389 $self->SUPER::size_allocate ($w, $h, $changed);
2390
2391 return unless $changed;
2384 2392
2385 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2393 $self->{layout}->set_font ($self->{font}) if $self->{font};
2386 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2394 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2387 $self->{layout}->set_width ($self->{children}[0]{w}); 2395 $self->{layout}->set_width ($self->{children}[0]{w});
2388 2396
2630 2638
2631 ($w + 4, $h + 4) 2639 ($w + 4, $h + 4)
2632} 2640}
2633 2641
2634sub size_allocate { 2642sub size_allocate {
2635 my ($self, $w, $h) = @_; 2643 my ($self, $w, $h, $changed) = @_;
2636 2644
2645 return unless $changed;
2646
2637 $self->SUPER::size_allocate ($w - 4, $h - 4); 2647 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2648}
2649
2650sub visibility_change {
2651 my ($self, $visible) = @_;
2652
2653 return unless $visible;
2654
2655 $self->{root}->on_post_alloc ("move_$self" => sub {
2656 my $widget = $self->{owner}
2657 or return;
2658
2659 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2660
2661 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2662 if $x + $self->{w} > $::WIDTH;
2663
2664 $self->move_abs ($x, $y);
2665 });
2638} 2666}
2639 2667
2640sub _draw { 2668sub _draw {
2641 my ($self) = @_; 2669 my ($self) = @_;
2642 2670
2659 glVertex $w, $h; 2687 glVertex $w, $h;
2660 glVertex $w, 0; 2688 glVertex $w, 0;
2661 glEnd; 2689 glEnd;
2662 2690
2663 glTranslate 2 - 0.375, 2 - 0.375; 2691 glTranslate 2 - 0.375, 2 - 0.375;
2692
2664 $self->SUPER::_draw; 2693 $self->SUPER::_draw;
2665} 2694}
2666 2695
2667############################################################################# 2696#############################################################################
2668 2697
2833 # maybe save $GRAB? must be careful about events... 2862 # maybe save $GRAB? must be careful about events...
2834 $GRAB = $self; 2863 $GRAB = $self;
2835 $self->{button} = $ev->{button}; 2864 $self->{button} = $ev->{button};
2836 2865
2837 $self->show; 2866 $self->show;
2838 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); 2867 $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2839} 2868}
2840 2869
2841sub mouse_motion { 2870sub mouse_motion {
2842 my ($self, $ev, $x, $y) = @_; 2871 my ($self, $ev, $x, $y) = @_;
2843 2872
2977use CFClient::OpenGL; 3006use CFClient::OpenGL;
2978 3007
2979sub new { 3008sub new {
2980 my $class = shift; 3009 my $class = shift;
2981 3010
2982 $class->SUPER::new ( 3011 my $self = $class->SUPER::new (
2983 visible => 1, 3012 visible => 1,
2984 @_, 3013 @_,
2985 ) 3014 );
3015
3016 Scalar::Util::weaken ($self->{root} = $self);
3017
3018 $self
2986} 3019}
2987 3020
2988sub configure { 3021sub configure {
2989 my ($self, $x, $y, $w, $h) = @_; 3022 my ($self, $x, $y, $w, $h) = @_;
2990 3023
2991 $self->{w} = $w; 3024 $self->{w} = $w;
2992 $self->{h} = $h; 3025 $self->{h} = $h;
2993} 3026}
2994 3027
2995sub check_size { 3028sub reconfigure {
2996 my ($self) = @_; 3029 my ($self) = @_;
2997 3030
3031 $self->SUPER::reconfigure;
3032
2998 $self->size_allocate ($self->{w}, $self->{h}) 3033 $self->size_allocate ($self->{w}, $self->{h}, 1)
2999 if $self->{w}; 3034 if $self->{w};
3000} 3035}
3001 3036
3002sub size_request { 3037sub size_request {
3003 my ($self) = @_; 3038 my ($self) = @_;
3004 3039
3005 ($self->{w}, $self->{h}) 3040 ($self->{w}, $self->{h})
3006} 3041}
3007 3042
3043sub _to_pixel {
3044 my ($coord, $size, $max) = @_;
3045
3046 $coord =
3047 $coord eq "center" ? ($max - $size) * 0.5
3048 : $coord eq "max" ? $max
3049 : $coord;
3050
3051 $coord = 0 if $coord < 0;
3052 $coord = $max - $size if $coord > $max - $size;
3053
3054 int $coord + 0.5
3055}
3056
3008sub size_allocate { 3057sub size_allocate {
3009 my ($self, $w, $h) = @_; 3058 my ($self, $w, $h, $changed) = @_;
3010 3059
3011 for my $child ($self->children) { 3060 for my $child ($self->children) {
3012 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3061 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3013 3062
3014 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3063 $X = $child->{force_x} if exists $child->{force_x};
3015 if exists $child->{req_x}; 3064 $Y = $child->{force_y} if exists $child->{force_y};
3016 3065
3017 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3066 $X = _to_pixel $X, $W, $self->{w};
3018 if exists $child->{req_y}; 3067 $Y = _to_pixel $Y, $H, $self->{h};
3019
3020 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3021
3022 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3023 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3024 3068
3025 $child->configure ($X, $Y, $W, $H); 3069 $child->configure ($X, $Y, $W, $H);
3026 } 3070 }
3027} 3071}
3028 3072
3039} 3083}
3040 3084
3041sub update { 3085sub update {
3042 my ($self) = @_; 3086 my ($self) = @_;
3043 3087
3044 $self->check_size;
3045 $::WANT_REFRESH++; 3088 $::WANT_REFRESH++;
3046} 3089}
3047 3090
3048sub add { 3091sub add {
3049 my ($self, @children) = @_; 3092 my ($self, @children) = @_;
3050 3093
3051 for my $child (@children) {
3052 $child->{is_toplevel} = 1; 3094 $_->{is_toplevel} = 1
3053 3095 for @children;
3054 # integerise window positions
3055 $child->{x} = int $child->{x};
3056 $child->{y} = int $child->{y};
3057 }
3058 3096
3059 $self->SUPER::add (@children); 3097 $self->SUPER::add (@children);
3060
3061 for (my @widgets = @children; my $w = pop @widgets; ) {
3062 push @widgets, $w->children;
3063 $w->set_visible;
3064 }
3065
3066} 3098}
3067 3099
3068sub remove { 3100sub remove {
3069 my ($self, @children) = @_; 3101 my ($self, @children) = @_;
3070 3102
3071 $self->SUPER::remove (@children); 3103 $self->SUPER::remove (@children);
3104
3105 delete $self->{is_toplevel}
3106 for @children;
3072 3107
3073 while (@children) { 3108 while (@children) {
3074 my $w = pop @children; 3109 my $w = pop @children;
3075 push @children, $w->children; 3110 push @children, $w->children;
3076 $w->set_invisible; 3111 $w->set_invisible;
3095 while ($self->{refresh_hook}) { 3130 while ($self->{refresh_hook}) {
3096 $_->() 3131 $_->()
3097 for values %{delete $self->{refresh_hook}}; 3132 for values %{delete $self->{refresh_hook}};
3098 } 3133 }
3099 3134
3100 if ($self->{check_size}) { 3135 if ($self->{realloc}) {
3101 my @queue; 3136 my @queue;
3102 3137
3103 while () { 3138 while () {
3104 if ($self->{check_size}) { 3139 if ($self->{realloc}) {
3105 #TODO use array-of-depth approach 3140 #TODO use array-of-depth approach
3106 3141
3107 @queue = sort { $a->{visible} <=> $b->{visible} } 3142 @queue = sort { $a->{visible} <=> $b->{visible} }
3108 @queue, values %{delete $self->{check_size}}; 3143 @queue, values %{delete $self->{realloc}};
3109 } 3144 }
3110 3145
3111 my $widget = pop @queue || last; 3146 my $widget = pop @queue || last;
3112 3147
3113 defined $widget->{visible} or last; # do not resize invisible widgets 3148 $widget->{visible} or last; # do not resize invisible widgets
3114 3149
3115 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3116 ? @$widget{qw(def_w def_h)}
3117 : $widget->size_request; 3150 my ($w, $h) = $widget->size_request;
3118 3151
3119 if (delete $widget->{force_alloc} 3152 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3120 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3153 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3121 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3154
3122 3155 $w = $widget->{force_w} if exists $widget->{force_w};
3156 $h = $widget->{force_h} if exists $widget->{force_h};
3157
3123 $widget->{req_w} = $w; 3158 $widget->{req_w} = $w;
3124 $widget->{req_h} = $h; 3159 $widget->{req_h} = $h;
3125 3160
3126 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3161 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3127 3162
3128 $widget->{parent}->check_size 3163 push @queue, $widget->{parent}
3129 if $widget->{parent}; 3164 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3130 }
3131 } 3165 }
3132 } 3166 }
3133 3167
3134 while (my $size_alloc = delete $self->{size_alloc}) { 3168 while (my $size_alloc = delete $self->{size_alloc}) {
3135 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, 3169 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3136 values %$size_alloc; 3170 values %$size_alloc;
3137 3171
3138 while () { 3172 while () {
3139 my ($widget, $w, $h) = @{ pop @queue or last }; 3173 my ($widget, $w, $h) = @{ pop @queue or last };
3140 3174
3175 $w = $widget->{w} || $widget->{req_w} unless defined $w;
3176 $h = $widget->{h} || $widget->{req_h} unless defined $h;
3177
3141 $w = 0 if $w < 0; 3178 $w = 0 if $w < 0;
3142 $h = 0 if $h < 0; 3179 $h = 0 if $h < 0;
3143 3180
3181 $w = int $w + 0.5;
3182 $h = int $h + 0.5;
3183
3184 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3185
3144 $widget->{w} = $w; 3186 $widget->{w} = $w;
3145 $widget->{h} = $h; 3187 $widget->{h} = $h;
3188
3146 $widget->emit (size_allocate => $w, $h); 3189 $widget->emit (size_allocate => $w, $h, $changed);
3147 } 3190 }
3148 } 3191 }
3149 3192
3150 while ($self->{post_alloc_hook}) { 3193 while ($self->{post_alloc_hook}) {
3151 $_->() 3194 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines