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

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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines