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.247 by root, Sun May 28 01:16:04 2006 UTC vs.
Revision 1.252 by root, Sun May 28 23:21:28 2006 UTC

47 my $tip = $widget->{tooltip}; 47 my $tip = $widget->{tooltip};
48 48
49 $tip = $tip->($widget) if CODE:: eq ref $tip; 49 $tip = $tip->($widget) if CODE:: eq ref $tip;
50 50
51 $TOOLTIP->set_tooltip_from ($widget); 51 $TOOLTIP->set_tooltip_from ($widget);
52 $TOOLTIP->show;
53 52
54 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 53 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
55 54
56 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0) 55 ($x, $y) = $widget->coord2global (-$TOOLTIP->{w}, 0)
57 if $x + $TOOLTIP->{w} > $::WIDTH; 56 if $x + $TOOLTIP->{w} > $::WIDTH;
58 57
59 $TOOLTIP->move ($x, $y); 58 $TOOLTIP->move ($x, $y);
60 $TOOLTIP->check_size;
61 $TOOLTIP->update; 59 $TOOLTIP->show;
62 } 60 }
63 61
64 return; 62 return;
65 } 63 }
66 } 64 }
221 } 219 }
222 220
223 $self 221 $self
224} 222}
225 223
226sub toggle_visibility {
227 my ($self) = @_;
228
229 if ($self->{visible}) {
230 $self->hide;
231 } else {
232 $self->show;
233 }
234}
235
236sub destroy { 224sub destroy {
237 my ($self) = @_; 225 my ($self) = @_;
238 226
239 $self->hide; 227 $self->hide;
240 %$self = (); 228 %$self = ();
241} 229}
242 230
243sub show { 231sub show {
244 my ($self) = @_; 232 my ($self) = @_;
233
245 return if $self->{parent}; 234 return if $self->{parent};
246 235
247 $CFClient::UI::ROOT->add ($self); 236 $CFClient::UI::ROOT->add ($self);
248} 237}
249 238
250sub center { 239sub center {
251 my ($self) = @_; 240 my ($self) = @_;
252 241
253 $CFClient::UI::ROOT->on_post_alloc ( 242 $CFClient::UI::ROOT->on_post_alloc (
254 "center_$self" => sub { 243 "center_$self" => sub {
255 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 244 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
256 }, 245 },
257 ); 246 );
258 247
259 $self->update; 248 $self->update;
260} 249}
265 return if $self->{visible}; 254 return if $self->{visible};
266 255
267 $self->{root} = $self->{parent}{root}; 256 $self->{root} = $self->{parent}{root};
268 $self->{visible} = $self->{parent}{visible} + 1; 257 $self->{visible} = $self->{parent}{visible} + 1;
269 258
270 $self->emit (visibility_change => 1) 259 $self->emit (visibility_change => 1);
260
261 $self->realloc if !exists $self->{req_w};
262
263 $_->set_visible for $self->children;
271} 264}
272 265
273sub set_invisible { 266sub set_invisible {
274 my ($self) = @_; 267 my ($self) = @_;
275 268
276 return unless $self->{visible}; 269 return unless $self->{visible};
277 270
278 # broken show/hide model 271 $_->set_invisible for $self->children;
279 272
280 delete $self->{root}; 273 delete $self->{root};
281 delete $self->{visible}; 274 delete $self->{visible};
282 275
283 undef $GRAB if $GRAB == $self; 276 undef $GRAB if $GRAB == $self;
284 undef $HOVER if $HOVER == $self; 277 undef $HOVER if $HOVER == $self;
285 278
286 CFClient::UI::check_tooltip 279 CFClient::UI::check_tooltip
287 if $CFClient::UI::TOOLTIP->{owner} == $self; 280 if $TOOLTIP->{owner} == $self;
288 281
289 $self->focus_out; 282 $self->focus_out;
290 283
291 $self->emit (visibility_change => 0); 284 $self->emit (visibility_change => 0);
285}
286
287sub set_visibility {
288 my ($self, $visible) = @_;
289
290 return if $self->{visible} == $visible;
291
292 $visible ? $self->hide
293 : $self->show;
294}
295
296sub toggle_visibility {
297 my ($self) = @_;
298
299 $self->{visible}
300 ? $self->hide
301 : $self->show;
292} 302}
293 303
294sub hide { 304sub hide {
295 my ($self) = @_; 305 my ($self) = @_;
296 306
314 my ($self, $w, $h) = @_; 324 my ($self, $w, $h) = @_;
315 325
316 $self->{def_w} = $w; 326 $self->{def_w} = $w;
317 $self->{def_h} = $h; 327 $self->{def_h} = $h;
318 328
319 $self->check_size; 329 $self->realloc;
320} 330}
321 331
322sub size_request { 332sub size_request {
323 require Carp; 333 require Carp;
324 Carp::confess "size_request is abstract"; 334 Carp::confess "size_request is abstract";
344 $self->{y} = $y; 354 $self->{y} = $y;
345 $self->update; 355 $self->update;
346 } 356 }
347 357
348 if ($self->{w} != $w || $self->{h} != $h) { 358 if ($self->{w} != $w || $self->{h} != $h) {
359 return unless $self->{visible};
360
349 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 361 $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h];
350 } 362 }
351} 363}
352 364
353sub size_allocate { 365sub size_allocate {
354 # nothing to be done 366 # nothing to be done
355}
356
357sub reconfigure {
358 my ($self) = @_;
359
360 $self->check_size (1);
361 $self->update;
362} 367}
363 368
364sub children { 369sub children {
365} 370}
366 371
444sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } 449sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} }
445sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } 450sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} }
446sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } 451sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} }
447sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } 452sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} }
448sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } 453sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} }
454
455sub find_widget {
456 my ($self, $x, $y) = @_;
457
458 return () unless $self->{can_events};
459
460 return $self
461 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
462 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
463
464 ()
465}
466
467sub set_parent {
468 my ($self, $parent) = @_;
469
470 Scalar::Util::weaken ($self->{parent} = $parent);
471
472 $self->set_visible if $parent->{visible};
473}
474
475sub connect {
476 my ($self, $signal, $cb) = @_;
477
478 push @{ $self->{signal_cb}{$signal} }, $cb;
479}
480
481sub _emit {
482 my ($self, $signal, @args) = @_;
483
484 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
485}
486
487sub emit {
488 my ($self, $signal, @args) = @_;
489
490 $self->_emit ($signal, @args)
491 || $self->$signal (@args);
492}
493
494sub visibility_change {
495 #my ($self, $visible) = @_;
496}
497
498sub realloc {
499 my ($self) = @_;
500
501 if ($self->{visible}) {
502 return if $self->{root}{realloc}{$self};
503
504 $self->{root}{realloc}{$self} = $self;
505 $self->{root}->update;
506 } else {
507 delete $self->{req_w};
508 }
509}
510
511sub update {
512 my ($self) = @_;
513
514 $self->{parent}->update
515 if $self->{parent};
516}
449 517
450sub draw { 518sub draw {
451 my ($self) = @_; 519 my ($self) = @_;
452 520
453 return unless $self->{h} && $self->{w}; 521 return unless $self->{h} && $self->{w};
491 my ($self) = @_; 559 my ($self) = @_;
492 560
493 warn "no draw defined for $self\n"; 561 warn "no draw defined for $self\n";
494} 562}
495 563
496sub find_widget {
497 my ($self, $x, $y) = @_;
498
499 return () unless $self->{can_events};
500
501 return $self
502 if $x >= $self->{x} && $x < $self->{x} + $self->{w}
503 && $y >= $self->{y} && $y < $self->{y} + $self->{h};
504
505 ()
506}
507
508sub set_parent {
509 my ($self, $parent) = @_;
510
511 Scalar::Util::weaken ($self->{parent} = $parent);
512
513 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
514
515 $self->check_size;
516}
517
518sub check_size {
519 my ($self, $forced) = @_;
520
521 $self->{force_alloc} = 1 if $forced;
522 $CFClient::UI::ROOT->{check_size}{$self} = $self;
523}
524
525sub update {
526 my ($self) = @_;
527
528 $self->{parent}->update
529 if $self->{parent};
530}
531
532sub connect {
533 my ($self, $signal, $cb) = @_;
534
535 push @{ $self->{signal_cb}{$signal} }, $cb;
536}
537
538sub _emit {
539 my ($self, $signal, @args) = @_;
540
541 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
542}
543
544sub emit {
545 my ($self, $signal, @args) = @_;
546
547 $self->_emit ($signal, @args)
548 || $self->$signal (@args);
549}
550
551sub visibility_change {
552 #my ($self, $visible) = @_;
553}
554
555sub DESTROY { 564sub DESTROY {
556 my ($self) = @_; 565 my ($self) = @_;
557 566
558 delete $WIDGET{$self+0}; 567 delete $WIDGET{$self+0};
559 #$self->deactivate; 568 #$self->deactivate;
654 $self->{children} = [ 663 $self->{children} = [
655 sort { $a->{z} <=> $b->{z} } 664 sort { $a->{z} <=> $b->{z} }
656 @{$self->{children}}, @widgets 665 @{$self->{children}}, @widgets
657 ]; 666 ];
658 667
659 $self->check_size (1); 668 $self->realloc;
660 $self->update;
661} 669}
662 670
663sub children { 671sub children {
664 @{ $_[0]{children} } 672 @{ $_[0]{children} }
665} 673}
670 delete $child->{parent}; 678 delete $child->{parent};
671 $child->hide; 679 $child->hide;
672 680
673 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 681 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
674 682
675 $self->check_size (1); 683 $self->realloc;
676 $self->update;
677} 684}
678 685
679sub clear { 686sub clear {
680 my ($self) = @_; 687 my ($self) = @_;
681 688
685 for (@$children) { 692 for (@$children) {
686 delete $_->{parent}; 693 delete $_->{parent};
687 $_->hide; 694 $_->hide;
688 } 695 }
689 696
690 $self->check_size; 697 $self->realloc;
691 $self->update;
692} 698}
693 699
694sub find_widget { 700sub find_widget {
695 my ($self, $x, $y) = @_; 701 my ($self, $x, $y) = @_;
696 702
749sub size_request { 755sub size_request {
750 $_[0]{children}[0]->size_request 756 $_[0]{children}[0]->size_request
751} 757}
752 758
753sub size_allocate { 759sub size_allocate {
754 my ($self, $w, $h) = @_; 760 my ($self, $w, $h, $changed) = @_;
755 761
756 $self->{children}[0]->configure (0, 0, $w, $h); 762 $self->{children}[0]->configure (0, 0, $w, $h);
757} 763}
758 764
759############################################################################# 765#############################################################################
776 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 782 $ROOT->on_post_alloc ($self => sub { $self->render_child });
777 $self->SUPER::update; 783 $self->SUPER::update;
778} 784}
779 785
780sub size_allocate { 786sub size_allocate {
781 my ($self, $w, $h) = @_; 787 my ($self, $w, $h, $changed) = @_;
782 788
783 $self->SUPER::size_allocate ($w, $h); 789 $self->SUPER::size_allocate ($w, $h, $changed);
784 $self->update; 790 $self->update
791 if $changed;
785} 792}
786 793
787sub _render { 794sub _render {
788 $_[0]{children}[0]->draw; 795 $_[0]{children}[0]->draw;
789} 796}
842 849
843 ($w, $h) 850 ($w, $h)
844} 851}
845 852
846sub size_allocate { 853sub size_allocate {
847 my ($self, $w, $h) = @_; 854 my ($self, $w, $h, $changed) = @_;
848 855
849 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 856 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
850 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 857 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
851 858
852 $self->child->configure (0, 0, $w, $h); 859 $self->child->configure (0, 0, $w, $h);
939 my $child = $self->{vp}->child; 946 my $child = $self->{vp}->child;
940 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 947 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
941} 948}
942 949
943sub size_allocate { 950sub size_allocate {
944 my ($self, $w, $h) = @_; 951 my ($self, $w, $h, $changed) = @_;
945 952
946 $self->SUPER::size_allocate ($w, $h); 953 $self->SUPER::size_allocate ($w, $h, $changed);
947 954
948 my $child = $self->{vp}->child; 955 my $child = $self->{vp}->child;
949 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); 956 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
950} 957}
951 958
1039 $h + $self->border * 2, 1046 $h + $self->border * 2,
1040 ) 1047 )
1041} 1048}
1042 1049
1043sub size_allocate { 1050sub size_allocate {
1044 my ($self, $w, $h) = @_; 1051 my ($self, $w, $h, $changed) = @_;
1052
1053 return unless $changed;
1045 1054
1046 $h -= List::Util::max 0, $self->border * 2; 1055 $h -= List::Util::max 0, $self->border * 2;
1047 $w -= List::Util::max 0, $self->border * 2; 1056 $w -= List::Util::max 0, $self->border * 2;
1048 1057
1049 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) 1058 $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2)
1078 $self->{user_x} = $wx + $dx * $mx; 1087 $self->{user_x} = $wx + $dx * $mx;
1079 $self->{user_y} = $wy + $dy * $my; 1088 $self->{user_y} = $wy + $dy * $my;
1080 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1089 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
1081 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1090 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
1082 $self->move ($self->{user_x}, $self->{user_y}); 1091 $self->move ($self->{user_x}, $self->{user_y});
1083 $self->check_size; 1092 $self->realloc;
1084 }; 1093 };
1085 1094
1086 } elsif ($lr ^ $td) { 1095 } elsif ($lr ^ $td) {
1087 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1096 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1088 my ($bx, $by) = ($self->{x}, $self->{y}); 1097 my ($bx, $by) = ($self->{x}, $self->{y});
1094 1103
1095 $self->{user_x} = $bx + $x - $ox; 1104 $self->{user_x} = $bx + $x - $ox;
1096 $self->{user_y} = $by + $y - $oy; 1105 $self->{user_y} = $by + $y - $oy;
1097 1106
1098 $self->move ($self->{user_x}, $self->{user_y}); 1107 $self->move ($self->{user_x}, $self->{user_y});
1099 $self->update; 1108 $self->realloc;
1100 }; 1109 };
1101 } 1110 }
1102} 1111}
1103 1112
1104sub button_up { 1113sub button_up {
1179 my ($self, $x, $y, $child) = @_; 1188 my ($self, $x, $y, $child) = @_;
1180 1189
1181 $child->set_parent ($self); 1190 $child->set_parent ($self);
1182 $self->{children}[$y][$x] = $child; 1191 $self->{children}[$y][$x] = $child;
1183 1192
1184 $self->check_size (1); 1193 $self->realloc;
1185} 1194}
1186 1195
1187# TODO: move to container class maybe? send children a signal on removal? 1196# TODO: move to container class maybe? send children a signal on removal?
1188sub clear { 1197sub clear {
1189 my ($self) = @_; 1198 my ($self) = @_;
1194 for (@children) { 1203 for (@children) {
1195 delete $_->{parent}; 1204 delete $_->{parent};
1196 $_->hide; 1205 $_->hide;
1197 } 1206 }
1198 1207
1199 $self->check_size (1); 1208 $self->realloc;
1200 $self->update;
1201} 1209}
1202 1210
1203sub get_wh { 1211sub get_wh {
1204 my ($self) = @_; 1212 my ($self) = @_;
1205 1213
1232 (sum @$hs), 1240 (sum @$hs),
1233 ) 1241 )
1234} 1242}
1235 1243
1236sub size_allocate { 1244sub size_allocate {
1237 my ($self, $w, $h) = @_; 1245 my ($self, $w, $h, $changed) = @_;
1238 1246
1239 my ($ws, $hs) = $self->get_wh; 1247 my ($ws, $hs) = $self->get_wh;
1240 1248
1241 my $req_w = (sum @$ws) || 1; 1249 my $req_w = (sum @$ws) || 1;
1242 my $req_h = (sum @$hs) || 1; 1250 my $req_h = (sum @$hs) || 1;
1320 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1321 ) 1329 )
1322} 1330}
1323 1331
1324sub size_allocate { 1332sub size_allocate {
1325 my ($self, $w, $h) = @_; 1333 my ($self, $w, $h, $changed) = @_;
1326 1334
1327 my $space = $self->{vertical} ? $h : $w; 1335 my $space = $self->{vertical} ? $h : $w;
1328 my $children = $self->{children}; 1336 my $children = $self->{children};
1329 1337
1330 my @req; 1338 my @req;
1459 $self->{text} = "T$text"; 1467 $self->{text} = "T$text";
1460 1468
1461 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1469 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1462 $self->{layout}->set_text ($text); 1470 $self->{layout}->set_text ($text);
1463 1471
1472 $self->realloc;
1464 $self->update; 1473 $self->update;
1465 $self->check_size;
1466} 1474}
1467 1475
1468sub set_markup { 1476sub set_markup {
1469 my ($self, $markup) = @_; 1477 my ($self, $markup) = @_;
1470 1478
1474 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1482 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1475 1483
1476 $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;
1477 $self->{layout}->set_markup ($markup); 1485 $self->{layout}->set_markup ($markup);
1478 1486
1487 $self->realloc;
1479 $self->update; 1488 $self->update;
1480 $self->check_size;
1481} 1489}
1482 1490
1483sub size_request { 1491sub size_request {
1484 my ($self) = @_; 1492 my ($self) = @_;
1485 1493
1506 $h + $self->{padding} * 2, 1514 $h + $self->{padding} * 2,
1507 ) 1515 )
1508} 1516}
1509 1517
1510sub size_allocate { 1518sub size_allocate {
1511 my ($self, $w, $h) = @_; 1519 my ($self, $w, $h, $changed) = @_;
1512 1520
1513 delete $self->{texture}; 1521 delete $self->{texture}
1522 if $changed;
1514} 1523}
1515 1524
1516sub set_fontsize { 1525sub set_fontsize {
1517 my ($self, $fontsize) = @_; 1526 my ($self, $fontsize) = @_;
1518 1527
1519 $self->{fontsize} = $fontsize; 1528 $self->{fontsize} = $fontsize;
1520 delete $self->{texture}; 1529 delete $self->{texture};
1521 1530
1522 $self->update; 1531 $self->realloc;
1523 $self->check_size;
1524} 1532}
1525 1533
1526sub _draw { 1534sub _draw {
1527 my ($self) = @_; 1535 my ($self) = @_;
1528 1536
1608sub set_text { 1616sub set_text {
1609 my ($self, $text) = @_; 1617 my ($self, $text) = @_;
1610 1618
1611 $self->{cursor} = length $text; 1619 $self->{cursor} = length $text;
1612 $self->_set_text ($text); 1620 $self->_set_text ($text);
1613 $self->update; 1621
1614 $self->check_size; 1622 $self->realloc;
1615} 1623}
1616 1624
1617sub get_text { 1625sub get_text {
1618 $_[0]{text} 1626 $_[0]{text}
1619} 1627}
1652 } elsif ($uni) { 1660 } elsif ($uni) {
1653 substr $text, $self->{cursor}++, 0, chr $uni; 1661 substr $text, $self->{cursor}++, 0, chr $uni;
1654 } 1662 }
1655 1663
1656 $self->_set_text ($text); 1664 $self->_set_text ($text);
1657 $self->update; 1665
1658 $self->check_size; 1666 $self->realloc;
1659} 1667}
1660 1668
1661sub focus_in { 1669sub focus_in {
1662 my ($self) = @_; 1670 my ($self) = @_;
1663 1671
2170 $self->update; 2178 $self->update;
2171 2179
2172 $self 2180 $self
2173} 2181}
2174 2182
2183sub changed { }
2184
2175sub set_range { 2185sub set_range {
2176 my ($self, $range) = @_; 2186 my ($self, $range) = @_;
2177 2187
2178 ($range, $self->{range}) = ($self->{range}, $range); 2188 ($range, $self->{range}) = ($self->{range}, $range);
2179 2189
2368 $self->{fontsize} = $fontsize; 2378 $self->{fontsize} = $fontsize;
2369 $self->reflow; 2379 $self->reflow;
2370} 2380}
2371 2381
2372sub size_allocate { 2382sub size_allocate {
2373 my ($self, $w, $h) = @_; 2383 my ($self, $w, $h, $changed) = @_;
2374 2384
2375 $self->SUPER::size_allocate ($w, $h); 2385 $self->SUPER::size_allocate ($w, $h, $changed);
2386
2387 return unless $changed;
2376 2388
2377 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2389 $self->{layout}->set_font ($self->{font}) if $self->{font};
2378 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2390 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2379 $self->{layout}->set_width ($self->{children}[0]{w}); 2391 $self->{layout}->set_width ($self->{children}[0]{w});
2380 2392
2622 2634
2623 ($w + 4, $h + 4) 2635 ($w + 4, $h + 4)
2624} 2636}
2625 2637
2626sub size_allocate { 2638sub size_allocate {
2627 my ($self, $w, $h) = @_; 2639 my ($self, $w, $h, $changed) = @_;
2628 2640
2641 return unless $changed;
2642
2629 $self->SUPER::size_allocate ($w - 4, $h - 4); 2643 $self->SUPER::size_allocate ($w - 4, $h - 4, $changed);
2630} 2644}
2631 2645
2632sub _draw { 2646sub _draw {
2633 my ($self) = @_; 2647 my ($self) = @_;
2634 2648
2651 glVertex $w, $h; 2665 glVertex $w, $h;
2652 glVertex $w, 0; 2666 glVertex $w, 0;
2653 glEnd; 2667 glEnd;
2654 2668
2655 glTranslate 2 - 0.375, 2 - 0.375; 2669 glTranslate 2 - 0.375, 2 - 0.375;
2670
2656 $self->SUPER::_draw; 2671 $self->SUPER::_draw;
2657} 2672}
2658 2673
2659############################################################################# 2674#############################################################################
2660 2675
2969use CFClient::OpenGL; 2984use CFClient::OpenGL;
2970 2985
2971sub new { 2986sub new {
2972 my $class = shift; 2987 my $class = shift;
2973 2988
2974 $class->SUPER::new ( 2989 my $self = $class->SUPER::new (
2975 visible => 1, 2990 visible => 1,
2976 @_, 2991 @_,
2977 ) 2992 );
2993
2994 Scalar::Util::weaken ($self->{root} = $self);
2995
2996 $self
2978} 2997}
2979 2998
2980sub configure { 2999sub configure {
2981 my ($self, $x, $y, $w, $h) = @_; 3000 my ($self, $x, $y, $w, $h) = @_;
2982 3001
2983 $self->{w} = $w; 3002 $self->{w} = $w;
2984 $self->{h} = $h; 3003 $self->{h} = $h;
2985} 3004}
2986 3005
2987sub check_size { 3006sub reconfigure {
2988 my ($self) = @_; 3007 my ($self) = @_;
2989 3008
3009 $self->SUPER::reconfigure;
3010
2990 $self->size_allocate ($self->{w}, $self->{h}) 3011 $self->size_allocate ($self->{w}, $self->{h}, 1)
2991 if $self->{w}; 3012 if $self->{w};
2992} 3013}
2993 3014
2994sub size_request { 3015sub size_request {
2995 my ($self) = @_; 3016 my ($self) = @_;
2996 3017
2997 ($self->{w}, $self->{h}) 3018 ($self->{w}, $self->{h})
2998} 3019}
2999 3020
3000sub size_allocate { 3021sub size_allocate {
3001 my ($self, $w, $h) = @_; 3022 my ($self, $w, $h, $changed) = @_;
3002 3023
3003 for my $child ($self->children) { 3024 for my $child ($self->children) {
3004 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 3025 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
3005 3026
3006 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3027 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1
3007 if exists $child->{req_x}; 3028 if exists $child->{req_x};
3008 3029
3009 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3030 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3010 if exists $child->{req_y}; 3031 if exists $child->{req_y};
3011 3032
3012 delete @$child{qw(req_x req_y)}; 3033 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3013 3034
3014 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3035 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3015 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; 3036 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3016 3037
3017 $child->configure ($X, $Y, $W, $H); 3038 $child->configure ($X, $Y, $W, $H);
3031} 3052}
3032 3053
3033sub update { 3054sub update {
3034 my ($self) = @_; 3055 my ($self) = @_;
3035 3056
3036 $self->check_size;
3037 $::WANT_REFRESH++; 3057 $::WANT_REFRESH++;
3038} 3058}
3039 3059
3040sub add { 3060sub add {
3041 my ($self, @children) = @_; 3061 my ($self, @children) = @_;
3047 $child->{x} = int $child->{x}; 3067 $child->{x} = int $child->{x};
3048 $child->{y} = int $child->{y}; 3068 $child->{y} = int $child->{y};
3049 } 3069 }
3050 3070
3051 $self->SUPER::add (@children); 3071 $self->SUPER::add (@children);
3052
3053 for (my @widgets = @children; my $w = pop @widgets; ) {
3054 push @widgets, $w->children;
3055 $w->set_visible;
3056 }
3057
3058} 3072}
3059 3073
3060sub remove { 3074sub remove {
3061 my ($self, @children) = @_; 3075 my ($self, @children) = @_;
3062 3076
3087 while ($self->{refresh_hook}) { 3101 while ($self->{refresh_hook}) {
3088 $_->() 3102 $_->()
3089 for values %{delete $self->{refresh_hook}}; 3103 for values %{delete $self->{refresh_hook}};
3090 } 3104 }
3091 3105
3092 if ($self->{check_size}) { 3106 if ($self->{realloc}) {
3093 my @queue; 3107 my @queue;
3094 3108
3095 for (;;) { 3109 while () {
3096 if ($self->{check_size}) { 3110 if ($self->{realloc}) {
3097 #TODO use array-of-depth approach 3111 #TODO use array-of-depth approach
3098 3112
3099 @queue = sort { $a->{visible} <=> $b->{visible} } 3113 @queue = sort { $a->{visible} <=> $b->{visible} }
3100 @queue, values %{delete $self->{check_size}}; 3114 @queue, values %{delete $self->{realloc}};
3101 } 3115 }
3102 3116
3103 my $widget = pop @queue || last; 3117 my $widget = pop @queue || last;
3104 3118
3105 defined $widget->{visible} or last; # do not resize invisible widgets 3119 $widget->{visible} or last; # do not resize invisible widgets
3106 3120
3107 my ($w, $h) = $widget->{def_w} && $widget->{def_h} 3121 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3108 ? @$widget{qw(def_w def_h)} 3122 ? @$widget{qw(def_w def_h)}
3109 : $widget->size_request; 3123 : $widget->size_request;
3110 3124
3111 if (delete $widget->{force_alloc}
3112 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3113 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3125 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3114 3126
3115 $widget->{req_w} = $w; 3127 $widget->{req_w} = $w;
3116 $widget->{req_h} = $h; 3128 $widget->{req_h} = $h;
3117 3129
3118 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3130 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h];
3119 3131
3120 $widget->{parent}->check_size 3132 push @queue, $widget->{parent}
3121 if $widget->{parent}; 3133 if $widget->{parent};
3122 }
3123 } 3134 }
3124 } 3135 }
3125 3136
3126 while ($self->{size_alloc}) { 3137 while (my $size_alloc = delete $self->{size_alloc}) {
3127 for (values %{delete $self->{size_alloc}}) { 3138 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3128 my ($widget, $w, $h) = @$_; 3139 values %$size_alloc;
3140
3141 while () {
3142 my ($widget, $w, $h) = @{ pop @queue or last };
3129 3143
3130 $w = 0 if $w < 0; 3144 $w = 0 if $w < 0;
3131 $h = 0 if $h < 0; 3145 $h = 0 if $h < 0;
3132 3146
3147 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3148
3133 $widget->{w} = $w; 3149 $widget->{w} = $w;
3134 $widget->{h} = $h; 3150 $widget->{h} = $h;
3151
3135 $widget->emit (size_allocate => $w, $h); 3152 $widget->emit (size_allocate => $w, $h, $changed);
3136 } 3153 }
3137 } 3154 }
3138 3155
3139 while ($self->{post_alloc_hook}) { 3156 while ($self->{post_alloc_hook}) {
3140 $_->() 3157 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines