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.250 by root, Sun May 28 02:31:03 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 for (grep { $_->{name} } values %WIDGET) {
23 $LAYOUT->{$_->{name}} = {
24 x => $_->{x} / $::WIDTH,
25 y => $_->{y} / $::HEIGHT,
26 w => $_->{w} / $::WIDTH,
27 h => $_->{h} / $::HEIGHT
28 };
29 }
30
31 return $LAYOUT;
32}
33
34sub set_layout {
35 my ($layout) = @_;
36 $LAYOUT = $layout;
37}
18 38
19sub check_tooltip { 39sub check_tooltip {
20 if (!$GRAB) { 40 if (!$GRAB) {
21 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 41 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
22 if (length $widget->{tooltip}) { 42 if (length $widget->{tooltip}) {
50 delete $TOOLTIP->{owner}; 70 delete $TOOLTIP->{owner};
51} 71}
52 72
53# class methods for events 73# class methods for events
54sub feed_sdl_key_down_event { 74sub feed_sdl_key_down_event {
55 $FOCUS->emit (key_down => $_[0]) || $FOCUS->key_down ($_[0]) 75 $FOCUS->emit (key_down => $_[0])
56 if $FOCUS; 76 if $FOCUS;
57} 77}
58 78
59sub feed_sdl_key_up_event { 79sub feed_sdl_key_up_event {
60 $FOCUS->emit (key_up => $_[0]) || $FOCUS->key_up ($_[0]) 80 $FOCUS->emit (key_up => $_[0])
61 if $FOCUS; 81 if $FOCUS;
62} 82}
63 83
64sub feed_sdl_button_down_event { 84sub feed_sdl_button_down_event {
65 my ($ev) = @_; 85 my ($ev) = @_;
74 check_tooltip; 94 check_tooltip;
75 } 95 }
76 96
77 $BUTTON_STATE |= 1 << ($ev->{button} - 1); 97 $BUTTON_STATE |= 1 << ($ev->{button} - 1);
78 98
79 if ($GRAB) { 99 $GRAB->emit (button_down => $ev, $GRAB->coord2local ($x, $y))
80 ($x, $y) = $GRAB->coord2local ($x, $y); 100 if $GRAB;
81 $GRAB->emit (button_down => $ev, $x, $y) || $GRAB->button_down ($ev, $x, $y);
82 }
83} 101}
84 102
85sub feed_sdl_button_up_event { 103sub feed_sdl_button_up_event {
86 my ($ev) = @_; 104 my ($ev) = @_;
87 my ($x, $y) = ($ev->{x}, $ev->{y}); 105 my ($x, $y) = ($ev->{x}, $ev->{y});
88 106
89 my $widget = $GRAB || $ROOT->find_widget ($x, $y); 107 my $widget = $GRAB || $ROOT->find_widget ($x, $y);
90 108
91 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1)); 109 $BUTTON_STATE &= ~(1 << ($ev->{button} - 1));
92 110
93 if ($GRAB) { 111 $GRAB->emit (button_up => $ev, $GRAB->coord2local ($x, $y))
94 ($x, $y) = $GRAB->coord2local ($x, $y); 112 if $GRAB;
95 $GRAB->emit (button_up => $ev, $x, $y) || $GRAB->button_up ($ev, $x, $y);
96 }
97 113
98 if (!$BUTTON_STATE) { 114 if (!$BUTTON_STATE) {
99 my $grab = $GRAB; undef $GRAB; 115 my $grab = $GRAB; undef $GRAB;
100 $grab->update if $grab; 116 $grab->update if $grab;
101 $GRAB->update if $GRAB; 117 $GRAB->update if $GRAB;
117 $HOVER->update if $HOVER && $HOVER->{can_hover}; 133 $HOVER->update if $HOVER && $HOVER->{can_hover};
118 134
119 check_tooltip; 135 check_tooltip;
120 } 136 }
121 137
122 if ($HOVER) {
123 ($x, $y) = $HOVER->coord2local ($x, $y);
124 $HOVER->emit (mouse_motion => $ev, $x, $y) || $HOVER->mouse_motion ($ev, $x, $y); 138 $HOVER->emit (mouse_motion => $ev, $HOVER->coord2local ($x, $y))
125 } 139 if $HOVER;
126} 140}
127 141
128# convert position array to integers 142# convert position array to integers
129sub harmonize { 143sub harmonize {
130 my ($vals) = @_; 144 my ($vals) = @_;
161 for my $widget (values %WIDGET) { 175 for my $widget (values %WIDGET) {
162 if ($widget->{is_toplevel}) { 176 if ($widget->{is_toplevel}) {
163 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; 177 $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x};
164 $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; 178 $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}; 179 $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_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}; 180 $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y};
168 $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; 181 $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}; 182 $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h};
170 $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h};
171 } 183 }
172 } 184 }
173 185
174 reconfigure_widgets; 186 reconfigure_widgets;
175} 187}
192 can_events => 1, 204 can_events => 1,
193 @_ 205 @_
194 }, $class; 206 }, $class;
195 207
196 for (keys %$self) { 208 for (keys %$self) {
197 if (/^connect_(.*)$/) { 209 if (/^on_(.*)$/) {
198 $self->connect ($1 => delete $self->{$_}); 210 $self->connect ($1 => delete $self->{$_});
199 } 211 }
200 } 212 }
201 213
202 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); 214 Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self);
203 215
216 if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) {
217 $self->{req_x} = $layout->{x} * $::WIDTH;
218 $self->{req_y} = $layout->{y} * $::HEIGHT;
219 $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH;
220 $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT;
221 }
222
204 $self 223 $self
205} 224}
206 225
207sub destroy { 226sub destroy {
208 my ($self) = @_; 227 my ($self) = @_;
211 %$self = (); 230 %$self = ();
212} 231}
213 232
214sub show { 233sub show {
215 my ($self) = @_; 234 my ($self) = @_;
235
216 return if $self->{parent}; 236 return if $self->{parent};
217 237
218 $CFClient::UI::ROOT->add ($self); 238 $CFClient::UI::ROOT->add ($self);
219} 239}
220 240
221sub show_centered { 241sub center {
222 my ($self) = @_; 242 my ($self) = @_;
223 return if $self->{parent};
224
225 $self->show;
226 243
227 $CFClient::UI::ROOT->on_post_alloc ( 244 $CFClient::UI::ROOT->on_post_alloc (
228 "centered $self" => sub { 245 "center_$self" => sub {
229 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); 246 $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5);
230 }, 247 },
231 ); 248 );
232}
233 249
234sub hide { 250 $self->update;
251}
252
253sub set_visible {
235 my ($self) = @_; 254 my ($self) = @_;
255
256 return if $self->{visible};
257
258 $self->{root} = $self->{parent}{root};
259 $self->{visible} = $self->{parent}{visible} + 1;
260
261 $self->emit (visibility_change => 1);
262}
263
264sub set_invisible {
265 my ($self) = @_;
266
267 return unless $self->{visible};
268
269 # broken show/hide model
270
271 delete $self->{root};
272 delete $self->{visible};
236 273
237 undef $GRAB if $GRAB == $self; 274 undef $GRAB if $GRAB == $self;
238 undef $HOVER if $HOVER == $self; 275 undef $HOVER if $HOVER == $self;
276
277 CFClient::UI::check_tooltip
278 if $CFClient::UI::TOOLTIP->{owner} == $self;
279
280 $self->focus_out;
281
282 $self->emit (visibility_change => 0);
283}
284
285sub set_visibility {
286 my ($self, $visible) = @_;
287
288 return if $self->{visible} == $visible;
289
290 $visible ? $self->hide
291 : $self->show;
292}
293
294sub toggle_visibility {
295 my ($self) = @_;
296
297 $self->{visible}
298 ? $self->hide
299 : $self->show;
300}
301
302sub hide {
303 my ($self) = @_;
304
305 $self->set_invisible;
239 306
240 $self->{parent}->remove ($self) 307 $self->{parent}->remove ($self)
241 if $self->{parent}; 308 if $self->{parent};
242} 309}
243 310
252} 319}
253 320
254sub set_size { 321sub set_size {
255 my ($self, $w, $h) = @_; 322 my ($self, $w, $h) = @_;
256 323
257 $self->{user_w} = $w; 324 $self->{def_w} = $w;
258 $self->{user_h} = $h; 325 $self->{def_h} = $h;
259 326
260 $self->check_size; 327 $self->check_size;
261} 328}
262 329
263sub size_request { 330sub size_request {
285 $self->{y} = $y; 352 $self->{y} = $y;
286 $self->update; 353 $self->update;
287 } 354 }
288 355
289 if ($self->{w} != $w || $self->{h} != $h) { 356 if ($self->{w} != $w || $self->{h} != $h) {
290 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; 357 $CFClient::UI::ROOT->{size_alloc}{$self+0} = [$self, $w, $h];
291 } 358 }
292} 359}
293 360
294sub size_allocate { 361sub size_allocate {
295 # nothing to be done 362 # nothing to be done
312 delete $self->{max_h}; $self->{max_h} = $h if $h; 379 delete $self->{max_h}; $self->{max_h} = $h if $h;
313} 380}
314 381
315sub set_tooltip { 382sub set_tooltip {
316 my ($self, $tooltip) = @_; 383 my ($self, $tooltip) = @_;
384
385 $tooltip =~ s/^\s+//;
386 $tooltip =~ s/\s+$//;
387
388 return if $self->{tooltip} eq $tooltip;
317 389
318 $self->{tooltip} = $tooltip; 390 $self->{tooltip} = $tooltip;
319 391
320 if ($CFClient::UI::TOOLTIP->{owner} == $self) { 392 if ($CFClient::UI::TOOLTIP->{owner} == $self) {
321 delete $CFClient::UI::TOOLTIP->{owner}; 393 delete $CFClient::UI::TOOLTIP->{owner};
343 return if $FOCUS == $self; 415 return if $FOCUS == $self;
344 return unless $self->{can_focus}; 416 return unless $self->{can_focus};
345 417
346 my $focus = $FOCUS; $FOCUS = $self; 418 my $focus = $FOCUS; $FOCUS = $self;
347 419
348 $self->emit (focus_in => $focus); 420 $self->_emit (focus_in => $focus);
349 421
350 $focus->update if $focus; 422 $focus->update if $focus;
351 $FOCUS->update; 423 $FOCUS->update;
352} 424}
353 425
356 428
357 return unless $FOCUS == $self; 429 return unless $FOCUS == $self;
358 430
359 my $focus = $FOCUS; undef $FOCUS; 431 my $focus = $FOCUS; undef $FOCUS;
360 432
361 $self->emit (focus_out => $focus); 433 $self->_emit (focus_out => $focus);
362 434
363 $focus->update if $focus; #? 435 $focus->update if $focus; #?
436
437 $::MAPWIDGET->focus_in #d# focus mapwidget if no other widget has focus
438 unless $FOCUS;
364} 439}
365 440
366sub mouse_motion { } 441sub mouse_motion { }
367sub button_up { } 442sub button_up { }
368sub key_down { } 443sub key_down { }
369sub key_up { } 444sub key_up { }
370 445
371sub button_down { 446sub button_down {
372 my ($self, $ev, $x, $y) = @_; 447 my ($self, $ev, $x, $y) = @_;
373 448
374 $self->focus_in; 449 $self->focus_in;
408 if ($ENV{PCLIENT_DEBUG}) { 483 if ($ENV{PCLIENT_DEBUG}) {
409 glPushMatrix; 484 glPushMatrix;
410 glColor 1, 1, 0, 1; 485 glColor 1, 1, 0, 1;
411 glTranslate $self->{x} + 0.375, $self->{y} + 0.375; 486 glTranslate $self->{x} + 0.375, $self->{y} + 0.375;
412 glBegin GL_LINE_LOOP; 487 glBegin GL_LINE_LOOP;
413 glVertex 0 , 0; 488 glVertex 0 , 0;
414 glVertex $self->{w}, 0; 489 glVertex $self->{w} - 1, 0;
415 glVertex $self->{w}, $self->{h}; 490 glVertex $self->{w} - 1, $self->{h} - 1;
416 glVertex 0 , $self->{h}; 491 glVertex 0 , $self->{h} - 1;
417 glEnd; 492 glEnd;
418 glPopMatrix; 493 glPopMatrix;
419 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw; 494 #CFClient::UI::Label->new (w => $self->{w}, h => $self->{h}, text => $self, fontsize => 0)->_draw;
420 } 495 }
421} 496}
441sub set_parent { 516sub set_parent {
442 my ($self, $parent) = @_; 517 my ($self, $parent) = @_;
443 518
444 Scalar::Util::weaken ($self->{parent} = $parent); 519 Scalar::Util::weaken ($self->{parent} = $parent);
445 520
446 # TODO: req_w _does_change after ->reconfigure 521 $self->set_visible; #TODO why breakssssss borked damn if $parent->{visible};
522
447 $self->check_size 523 $self->check_size;
448 unless exists $self->{req_w};
449
450 $self->show;
451} 524}
452 525
453sub check_size { 526sub check_size {
454 my ($self, $forced) = @_; 527 my ($self, $forced) = @_;
455 528
468 my ($self, $signal, $cb) = @_; 541 my ($self, $signal, $cb) = @_;
469 542
470 push @{ $self->{signal_cb}{$signal} }, $cb; 543 push @{ $self->{signal_cb}{$signal} }, $cb;
471} 544}
472 545
546sub _emit {
547 my ($self, $signal, @args) = @_;
548
549 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
550}
551
473sub emit { 552sub emit {
474 my ($self, $signal, @args) = @_; 553 my ($self, $signal, @args) = @_;
475 554
476 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} 555 $self->_emit ($signal, @args)
556 || $self->$signal (@args);
557}
558
559sub visibility_change {
560 #my ($self, $visible) = @_;
477} 561}
478 562
479sub DESTROY { 563sub DESTROY {
480 my ($self) = @_; 564 my ($self) = @_;
481 565
594 delete $child->{parent}; 678 delete $child->{parent};
595 $child->hide; 679 $child->hide;
596 680
597 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 681 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
598 682
599 $self->check_size; 683 $self->check_size (1);
600 $self->update; 684 $self->update;
601} 685}
602 686
603sub clear { 687sub clear {
604 my ($self) = @_; 688 my ($self) = @_;
744 828
745package CFClient::UI::ViewPort; 829package CFClient::UI::ViewPort;
746 830
747our @ISA = CFClient::UI::Window::; 831our @ISA = CFClient::UI::Window::;
748 832
833sub new {
834 my $class = shift;
835
836 $class->SUPER::new (
837 scroll_x => 0,
838 scroll_y => 1,
839 @_,
840 )
841}
842
749sub size_request { 843sub size_request {
750 my ($self) = @_; 844 my ($self) = @_;
751 845
752 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 846 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
753 847
754 @$self{qw(child_w child_h)} 848 $w = 10 if $self->{scroll_x};
849 $h = 10 if $self->{scroll_y};
850
851 ($w, $h)
755} 852}
756 853
757sub size_allocate { 854sub size_allocate {
758 my ($self, $w, $h) = @_; 855 my ($self, $w, $h) = @_;
759 856
760 my ($cw, $ch) = @$self{qw(child_w child_h)}; 857 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
761# $w = $self->{w}; 858 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
859
762 $self->child->configure (0, 0, $cw, $ch); 860 $self->child->configure (0, 0, $w, $h);
763 $self->update; 861 $self->update;
764} 862}
765 863
766sub set_offset { 864sub set_offset {
767 my ($self, $x, $y) = @_; 865 my ($self, $x, $y) = @_;
818 my $class = shift; 916 my $class = shift;
819 917
820 my $self; 918 my $self;
821 919
822 my $slider = new CFClient::UI::Slider 920 my $slider = new CFClient::UI::Slider
823 vertical => 1, 921 vertical => 1,
824 range => [0, 0, 1, 0.01], # HACK fix 922 range => [0, 0, 1, 0.01], # HACK fix
825 connect_changed => sub { 923 on_changed => sub {
826 $self->{vp}->set_offset (0, $_[1]); 924 $self->{vp}->set_offset (0, $_[1]);
827 }, 925 },
828 ; 926 ;
829 927
830 $self = $class->SUPER::new ( 928 $self = $class->SUPER::new (
836 $self->{vp}->add ($self->{scrolled}); 934 $self->{vp}->add ($self->{scrolled});
837 $self->add ($self->{vp}); 935 $self->add ($self->{vp});
838 $self->add ($self->{slider}); 936 $self->add ($self->{slider});
839 937
840 $self 938 $self
939}
940
941sub update {
942 my ($self) = @_;
943
944 $self->SUPER::update;
945
946 # todo: overwrite size_allocate of child
947 my $child = $self->{vp}->child;
948 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
841} 949}
842 950
843sub size_allocate { 951sub size_allocate {
844 my ($self, $w, $h) = @_; 952 my ($self, $w, $h) = @_;
845 953
904 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1012 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); 1013 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
906 1014
907sub new { 1015sub new {
908 my $class = shift; 1016 my $class = shift;
909
910 # TODO: user_x, user_y, overwrite moveto?
911 1017
912 my $self = $class->SUPER::new ( 1018 my $self = $class->SUPER::new (
913 bg => [1, 1, 1, 1], 1019 bg => [1, 1, 1, 1],
914 border_bg => [1, 1, 1, 1], 1020 border_bg => [1, 1, 1, 1],
915 border => 0.6, 1021 border => 0.6,
975 my ($ev, $x, $y) = @_; 1081 my ($ev, $x, $y) = @_;
976 1082
977 my $dx = $ev->{x} - $ox; 1083 my $dx = $ev->{x} - $ox;
978 my $dy = $ev->{y} - $oy; 1084 my $dy = $ev->{y} - $oy;
979 1085
1086 $self->{user_x} = $wx + $dx * $mx;
1087 $self->{user_y} = $wy + $dy * $my;
980 $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); 1088 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1);
981 $self->{user_h} = $bh + $dy * ($my ? -1 : 1); 1089 $self->{def_h} = $bh + $dy * ($my ? -1 : 1);
982 $self->move ($wx + $dx * $mx, $wy + $dy * $my); 1090 $self->move ($self->{user_x}, $self->{user_y});
983 $self->check_size; 1091 $self->check_size;
984 }; 1092 };
985 1093
986 } elsif ($lr ^ $td) { 1094 } elsif ($lr ^ $td) {
987 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1095 my ($ox, $oy) = ($ev->{x}, $ev->{y});
990 $self->{motion} = sub { 1098 $self->{motion} = sub {
991 my ($ev, $x, $y) = @_; 1099 my ($ev, $x, $y) = @_;
992 1100
993 ($x, $y) = ($ev->{x}, $ev->{y}); 1101 ($x, $y) = ($ev->{x}, $ev->{y});
994 1102
995 $self->move ($bx + $x - $ox, $by + $y - $oy); 1103 $self->{user_x} = $bx + $x - $ox;
1104 $self->{user_y} = $by + $y - $oy;
1105
1106 $self->move ($self->{user_x}, $self->{user_y});
996 $self->update; 1107 $self->update;
997 }; 1108 };
998 } 1109 }
999} 1110}
1000 1111
1062sub new { 1173sub new {
1063 my $class = shift; 1174 my $class = shift;
1064 1175
1065 $class->SUPER::new ( 1176 $class->SUPER::new (
1066 col_expand => [], 1177 col_expand => [],
1067 @_ 1178 @_,
1068 ) 1179 )
1180}
1181
1182sub children {
1183 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1069} 1184}
1070 1185
1071sub add { 1186sub add {
1072 my ($self, $x, $y, $child) = @_; 1187 my ($self, $x, $y, $child) = @_;
1073 1188
1074 $child->set_parent ($self); 1189 $child->set_parent ($self);
1075 $self->{children}[$y][$x] = $child; 1190 $self->{children}[$y][$x] = $child;
1076 1191
1077 $child->check_size; 1192 $self->check_size (1);
1078} 1193}
1079 1194
1080sub children {
1081 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1082}
1083
1084# TODO: move to container class maybe? send childs a signal on removal? 1195# TODO: move to container class maybe? send children a signal on removal?
1085sub clear { 1196sub clear {
1086 my ($self) = @_; 1197 my ($self) = @_;
1087 1198
1088 my @children = $self->children; 1199 my @children = $self->children;
1089 delete $self->{children}; 1200 delete $self->{children};
1091 for (@children) { 1202 for (@children) {
1092 delete $_->{parent}; 1203 delete $_->{parent};
1093 $_->hide; 1204 $_->hide;
1094 } 1205 }
1095 1206
1207 $self->check_size (1);
1096 $self->update; 1208 $self->update;
1097} 1209}
1098 1210
1099sub get_wh { 1211sub get_wh {
1100 my ($self) = @_; 1212 my ($self) = @_;
1132sub size_allocate { 1244sub size_allocate {
1133 my ($self, $w, $h) = @_; 1245 my ($self, $w, $h) = @_;
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
1202package CFClient::UI::HBox; 1314package CFClient::UI::Box;
1203
1204# TODO: wrap into common Box base class
1205 1315
1206our @ISA = CFClient::UI::Container::; 1316our @ISA = CFClient::UI::Container::;
1207 1317
1208sub size_request { 1318sub size_request {
1209 my ($self) = @_; 1319 my ($self) = @_;
1210 1320
1211 my @alloc = map [$_->size_request], @{$self->{children}}; 1321 $self->{vertical}
1212 1322 ? (
1213 ( 1323 (List::Util::max map $_->{req_w}, @{$self->{children}}),
1214 (List::Util::sum map $_->[0], @alloc), 1324 (List::Util::sum map $_->{req_h}, @{$self->{children}}),
1215 (List::Util::max map $_->[1], @alloc), 1325 )
1216 ) 1326 : (
1327 (List::Util::sum map $_->{req_w}, @{$self->{children}}),
1328 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1329 )
1217} 1330}
1218 1331
1219sub size_allocate { 1332sub size_allocate {
1220 my ($self, $w, $h) = @_; 1333 my ($self, $w, $h) = @_;
1221 1334
1222 ($h, $w) = ($w, $h); 1335 my $space = $self->{vertical} ? $h : $w;
1223
1224 my $children = $self->{children}; 1336 my $children = $self->{children};
1225 1337
1226 my @h = map $_->{req_w}, @$children; 1338 my @req;
1227 1339
1228 my $req_h = List::Util::sum @h; 1340 if ($self->{homogeneous}) {
1229 1341 @req = ($space / (@$children || 1)) x @$children;
1230 if ($req_h > $h) {
1231 # ah well, not enough space
1232 $_ *= $h / $req_h for @h;
1233 } else { 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 {
1234 my $exp = List::Util::sum map $_->{expand}, @$children; 1350 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1235 $exp ||= 1;
1236 1351
1352 $space = ($space - $req) / $expand; # remaining space to give away
1353
1354 $req[$_] += $space * $children->[$_]{expand}
1237 for (0 .. $#$children) { 1355 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 } 1356 }
1244 } 1357 }
1245 1358
1246 CFClient::UI::harmonize \@h; 1359 CFClient::UI::harmonize \@req;
1247 1360
1248 my $y = 0; 1361 my $pos = 0;
1249 for (0 .. $#$children) { 1362 for (0 .. $#$children) {
1250 my $child = $children->[$_];
1251 my $h = $h[$_]; 1363 my $alloc = $req[$_];
1252 $child->configure ($y, 0, $h, $w); 1364 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1253 1365
1254 $y += $h; 1366 $pos += $alloc;
1255 } 1367 }
1256 1368
1257 1 1369 1
1258} 1370}
1259 1371
1260############################################################################# 1372#############################################################################
1261 1373
1374package CFClient::UI::HBox;
1375
1376our @ISA = CFClient::UI::Box::;
1377
1378sub new {
1379 my $class = shift;
1380
1381 $class->SUPER::new (
1382 vertical => 0,
1383 @_,
1384 )
1385}
1386
1387#############################################################################
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;
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
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);
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) = @_;
2102} 2181}
2103 2182
2104sub set_range { 2183sub set_range {
2105 my ($self, $range) = @_; 2184 my ($self, $range) = @_;
2106 2185
2107 $self->{range} = $range; 2186 ($range, $self->{range}) = ($self->{range}, $range);
2108 2187
2109 $self->update; 2188 $self->update
2189 if "@$range" ne "@{$self->{range}}";
2110} 2190}
2111 2191
2112sub set_value { 2192sub set_value {
2113 my ($self, $value) = @_; 2193 my ($self, $value) = @_;
2114 2194
2125 if $unit; 2205 if $unit;
2126 2206
2127 @{$self->{range}} = ($value, $lo, $hi, $page, $unit); 2207 @{$self->{range}} = ($value, $lo, $hi, $page, $unit);
2128 2208
2129 if ($value != $old_value) { 2209 if ($value != $old_value) {
2130 $self->emit (changed => $value); 2210 $self->_emit (changed => $value);
2131 $self->update; 2211 $self->update;
2132 } 2212 }
2133} 2213}
2134 2214
2135sub size_request { 2215sub size_request {
2497 2577
2498sub new { 2578sub new {
2499 my $class = shift; 2579 my $class = shift;
2500 2580
2501 my $self = $class->SUPER::new ( 2581 my $self = $class->SUPER::new (
2502 state => 0, 2582 state => 0,
2503 connect_activate => \&toggle_flopper, 2583 on_activate => \&toggle_flopper,
2504 @_ 2584 @_
2505 ); 2585 );
2506 2586
2507 if ($self->{state}) {
2508 $self->{state} = 0;
2509 $self->toggle_flopper;
2510 }
2511
2512 $self 2587 $self
2513} 2588}
2514 2589
2515sub toggle_flopper { 2590sub toggle_flopper {
2516 my ($self) = @_; 2591 my ($self) = @_;
2517 2592
2518 # TODO: use animation 2593 $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} 2594}
2530 2595
2531############################################################################# 2596#############################################################################
2532 2597
2533package CFClient::UI::Tooltip; 2598package CFClient::UI::Tooltip;
2609 2674
2610sub new { 2675sub new {
2611 my $class = shift; 2676 my $class = shift;
2612 2677
2613 my $self = $class->SUPER::new ( 2678 my $self = $class->SUPER::new (
2614 aspect => 1, 2679 aspect => 1,
2680 can_events => 0,
2615 @_, 2681 @_,
2616 ); 2682 );
2617 2683
2618 if ($self->{anim} && $self->{animspeed}) { 2684 if ($self->{anim} && $self->{animspeed}) {
2619 Scalar::Util::weaken (my $widget = $self); 2685 Scalar::Util::weaken (my $widget = $self);
2678 $self->SUPER::DESTROY; 2744 $self->SUPER::DESTROY;
2679} 2745}
2680 2746
2681############################################################################# 2747#############################################################################
2682 2748
2683package CFClient::UI::InventoryItem; 2749package CFClient::UI::Inventory;
2684 2750
2685our @ISA = CFClient::UI::HBox::; 2751our @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 2752
2713sub new { 2753sub new {
2714 my $class = shift; 2754 my $class = shift;
2715 2755
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 ( 2756 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), 2757 scrolled => (new CFClient::UI::Table col_expand => [0, 1, 0]),
2804 @_, 2758 @_,
2805 ); 2759 );
2806 2760
2807 $self 2761 $self
2808} 2762}
2818 or ($a->{name} cmp $b->{name}) 2772 or ($a->{name} cmp $b->{name})
2819 } @$items; 2773 } @$items;
2820 2774
2821 $self->{real_items} = \@items; 2775 $self->{real_items} = \@items;
2822 2776
2777 my $row = 0;
2823 for my $item (@items) { 2778 for my $item (@items) {
2824 $item->{item} = $item; 2779 CFClient::Item::update_widgets $item;
2825 $item = $item->{widget} ||= new CFClient::UI::InventoryItem item => $item;
2826 $item->update_item ();
2827 }
2828 2780
2829 my $i = 0; 2781 $self->{scrolled}->add (0, $row, $item->{face_widget});
2830 for (@items) { 2782 $self->{scrolled}->add (1, $row, $item->{desc_widget});
2831 $self->{scrolled}->add (0, $i, $_); 2783 $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 2784
2836# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2785 $row++;
2837} 2786 }
2838
2839sub size_request {
2840 my ($self) = @_;
2841 ($self->{req_w}, $self->{req_h});
2842} 2787}
2843 2788
2844############################################################################# 2789#############################################################################
2845 2790
2846package CFClient::UI::Menu; 2791package CFClient::UI::Menu;
2881 2826
2882# popup given the event (must be a mouse button down event currently) 2827# popup given the event (must be a mouse button down event currently)
2883sub popup { 2828sub popup {
2884 my ($self, $ev) = @_; 2829 my ($self, $ev) = @_;
2885 2830
2886 $self->emit ("popdown"); 2831 $self->_emit ("popdown");
2887 2832
2888 # maybe save $GRAB? must be careful about events... 2833 # maybe save $GRAB? must be careful about events...
2889 $GRAB = $self; 2834 $GRAB = $self;
2890 $self->{button} = $ev->{button}; 2835 $self->{button} = $ev->{button};
2891 2836
2906 2851
2907 if ($ev->{button} == $self->{button}) { 2852 if ($ev->{button} == $self->{button}) {
2908 undef $GRAB; 2853 undef $GRAB;
2909 $self->hide; 2854 $self->hide;
2910 2855
2911 $self->emit ("popdown"); 2856 $self->_emit ("popdown");
2912 $self->{hover}[1]->() if $self->{hover}; 2857 $self->{hover}[1]->() if $self->{hover};
2913 } 2858 }
2914} 2859}
2915 2860
2916############################################################################# 2861#############################################################################
2978sub add { 2923sub add {
2979 my ($self, $text, %arg) = @_; 2924 my ($self, $text, %arg) = @_;
2980 2925
2981 $text =~ s/^\s+//; 2926 $text =~ s/^\s+//;
2982 $text =~ s/\s+$//; 2927 $text =~ s/\s+$//;
2928
2929 return unless $text;
2983 2930
2984 my $timeout = time + ((delete $arg{timeout}) || 60); 2931 my $timeout = time + ((delete $arg{timeout}) || 60);
2985 2932
2986 my $group = exists $arg{group} ? $arg{group} : ++$self->{id}; 2933 my $group = exists $arg{group} ? $arg{group} : ++$self->{id};
2987 2934
3031 2978
3032sub new { 2979sub new {
3033 my $class = shift; 2980 my $class = shift;
3034 2981
3035 $class->SUPER::new ( 2982 $class->SUPER::new (
2983 visible => 1,
3036 @_, 2984 @_,
3037 ) 2985 )
3038} 2986}
3039 2987
3040sub configure { 2988sub configure {
3067 if exists $child->{req_x}; 3015 if exists $child->{req_x};
3068 3016
3069 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3017 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1
3070 if exists $child->{req_y}; 3018 if exists $child->{req_y};
3071 3019
3020 #delete @$child{qw(req_x req_y)};#d# def_x, def_y
3021
3072 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; 3022 $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; 3023 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3074 3024
3075 $child->configure ($X, $Y, $W, $H); 3025 $child->configure ($X, $Y, $W, $H);
3076 } 3026 }
3095 $::WANT_REFRESH++; 3045 $::WANT_REFRESH++;
3096} 3046}
3097 3047
3098sub add { 3048sub add {
3099 my ($self, @children) = @_; 3049 my ($self, @children) = @_;
3100
3101 for (my @widgets = @children; my $w = pop @widgets; ) {
3102 push @widgets, $w->children;
3103 $w->{root} = $self;
3104 $w->{visible} = 1;
3105 }
3106 3050
3107 for my $child (@children) { 3051 for my $child (@children) {
3108 $child->{is_toplevel} = 1; 3052 $child->{is_toplevel} = 1;
3109 3053
3110 # integerise window positions 3054 # integerise window positions
3111 $child->{x} = int $child->{x}; 3055 $child->{x} = int $child->{x};
3112 $child->{y} = int $child->{y}; 3056 $child->{y} = int $child->{y};
3113 } 3057 }
3114 3058
3115 $self->SUPER::add (@children); 3059 $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
3116} 3066}
3117 3067
3118sub remove { 3068sub remove {
3119 my ($self, @children) = @_; 3069 my ($self, @children) = @_;
3120 3070
3121 $self->SUPER::remove (@children); 3071 $self->SUPER::remove (@children);
3122 3072
3123 while (@children) { 3073 while (@children) {
3124 my $w = pop @children; 3074 my $w = pop @children;
3125 push @children, $w->children; 3075 push @children, $w->children;
3126 delete $w->{visible}; 3076 $w->set_invisible;
3127 } 3077 }
3128} 3078}
3129 3079
3130sub on_refresh { 3080sub on_refresh {
3131 my ($self, $id, $cb) = @_; 3081 my ($self, $id, $cb) = @_;
3146 $_->() 3096 $_->()
3147 for values %{delete $self->{refresh_hook}}; 3097 for values %{delete $self->{refresh_hook}};
3148 } 3098 }
3149 3099
3150 if ($self->{check_size}) { 3100 if ($self->{check_size}) {
3151 my @queue = ([], []); 3101 my @queue;
3152 3102
3153 for (;;) { 3103 while () {
3154 if ($self->{check_size}) { 3104 if ($self->{check_size}) {
3155 # heuristic: check containers last 3105 #TODO use array-of-depth approach
3156 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ 3106
3107 @queue = sort { $a->{visible} <=> $b->{visible} }
3157 for values %{delete $self->{check_size}} 3108 @queue, values %{delete $self->{check_size}};
3158 } 3109 }
3159 3110
3160 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; 3111 my $widget = pop @queue || last;
3161 3112
3113 defined $widget->{visible} or last; # do not resize invisible widgets
3114
3162 my ($w, $h) = $widget->{user_w} && $widget->{user_h} 3115 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3163 ? @$widget{qw(user_w user_h)} 3116 ? @$widget{qw(def_w def_h)}
3164 : $widget->size_request; 3117 : $widget->size_request;
3165 3118
3166 if (delete $widget->{force_alloc} 3119 if (delete $widget->{force_alloc}
3167 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3120 or $w != $widget->{req_w} or $h != $widget->{req_h}) {
3168 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3121 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d#
3176 if $widget->{parent}; 3129 if $widget->{parent};
3177 } 3130 }
3178 } 3131 }
3179 } 3132 }
3180 3133
3181 while ($self->{size_alloc}) { 3134 while (my $size_alloc = delete $self->{size_alloc}) {
3182 for (values %{delete $self->{size_alloc}}) { 3135 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3183 my ($widget, $w, $h) = @$_; 3136 values %$size_alloc;
3137
3138 while () {
3139 my ($widget, $w, $h) = @{ pop @queue or last };
3184 3140
3185 $w = 0 if $w < 0; 3141 $w = 0 if $w < 0;
3186 $h = 0 if $h < 0; 3142 $h = 0 if $h < 0;
3187 3143
3188 $widget->{w} = $w; 3144 $widget->{w} = $w;
3189 $widget->{h} = $h; 3145 $widget->{h} = $h;
3190 $widget->size_allocate ($w, $h);
3191 $widget->emit (size_allocate => $w, $h); 3146 $widget->emit (size_allocate => $w, $h);
3192 } 3147 }
3193 } 3148 }
3194 3149
3195 while ($self->{post_alloc_hook}) { 3150 while ($self->{post_alloc_hook}) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines