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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines