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.246 by root, Sun May 28 00:40:51 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
240 %$self = (); 242 %$self = ();
241} 243}
242 244
243sub show { 245sub show {
244 my ($self) = @_; 246 my ($self) = @_;
247
245 return if $self->{parent}; 248 return if $self->{parent};
246 249
247 $CFClient::UI::ROOT->add ($self); 250 $CFClient::UI::ROOT->add ($self);
248}
249
250sub show_centered {
251 my ($self) = @_;
252 return if $self->{parent};
253
254 $self->show;
255
256 $CFClient::UI::ROOT->on_post_alloc (
257 "centered $self" => sub {
258 $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5);
259 },
260 );
261} 251}
262 252
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;
515
516 $self->check_size;
517 $self->show;
518}
519
520sub check_size {
521 my ($self, $forced) = @_;
522
523 $self->{force_alloc} = 1 if $forced;
524 $CFClient::UI::ROOT->{check_size}{$self} = $self;
525}
526
527sub update {
528 my ($self) = @_;
529
530 $self->{parent}->update
531 if $self->{parent};
532}
533
534sub connect {
535 my ($self, $signal, $cb) = @_;
536
537 push @{ $self->{signal_cb}{$signal} }, $cb;
538}
539
540sub _emit {
541 my ($self, $signal, @args) = @_;
542
543 List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}
544}
545
546sub emit {
547 my ($self, $signal, @args) = @_;
548
549 $self->_emit ($signal, @args)
550 || $self->$signal (@args);
551}
552
553sub visibility_change {
554 #my ($self, $visible) = @_;
555}
556
557sub DESTROY { 572sub DESTROY {
558 my ($self) = @_; 573 my ($self) = @_;
559 574
560 delete $WIDGET{$self+0}; 575 delete $WIDGET{$self+0};
561 #$self->deactivate; 576 #$self->deactivate;
617 my ($class, %arg) = @_; 632 my ($class, %arg) = @_;
618 $class->SUPER::new (can_events => 0, %arg); 633 $class->SUPER::new (can_events => 0, %arg);
619} 634}
620 635
621sub size_request { 636sub size_request {
622 (0, 0) 637 my ($self) = @_;
638
639 ($self->{w} + 0, $self->{h} + 0)
623} 640}
624 641
625sub draw { } 642sub draw { }
626 643
627############################################################################# 644#############################################################################
656 $self->{children} = [ 673 $self->{children} = [
657 sort { $a->{z} <=> $b->{z} } 674 sort { $a->{z} <=> $b->{z} }
658 @{$self->{children}}, @widgets 675 @{$self->{children}}, @widgets
659 ]; 676 ];
660 677
661 $self->check_size (1); 678 $self->realloc;
662 $self->update;
663} 679}
664 680
665sub children { 681sub children {
666 @{ $_[0]{children} } 682 @{ $_[0]{children} }
667} 683}
672 delete $child->{parent}; 688 delete $child->{parent};
673 $child->hide; 689 $child->hide;
674 690
675 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; 691 $self->{children} = [ grep $_ != $child, @{ $self->{children} } ];
676 692
677 $self->check_size (1); 693 $self->realloc;
678 $self->update;
679} 694}
680 695
681sub clear { 696sub clear {
682 my ($self) = @_; 697 my ($self) = @_;
683 698
687 for (@$children) { 702 for (@$children) {
688 delete $_->{parent}; 703 delete $_->{parent};
689 $_->hide; 704 $_->hide;
690 } 705 }
691 706
692 $self->check_size; 707 $self->realloc;
693 $self->update;
694} 708}
695 709
696sub find_widget { 710sub find_widget {
697 my ($self, $x, $y) = @_; 711 my ($self, $x, $y) = @_;
698 712
751sub size_request { 765sub size_request {
752 $_[0]{children}[0]->size_request 766 $_[0]{children}[0]->size_request
753} 767}
754 768
755sub size_allocate { 769sub size_allocate {
756 my ($self, $w, $h) = @_; 770 my ($self, $w, $h, $changed) = @_;
757 771
758 $self->{children}[0]->configure (0, 0, $w, $h); 772 $self->{children}[0]->configure (0, 0, $w, $h);
759} 773}
760 774
761############################################################################# 775#############################################################################
778 $ROOT->on_post_alloc ($self => sub { $self->render_child }); 792 $ROOT->on_post_alloc ($self => sub { $self->render_child });
779 $self->SUPER::update; 793 $self->SUPER::update;
780} 794}
781 795
782sub size_allocate { 796sub size_allocate {
783 my ($self, $w, $h) = @_; 797 my ($self, $w, $h, $changed) = @_;
784 798
785 $self->SUPER::size_allocate ($w, $h); 799 $self->SUPER::size_allocate ($w, $h, $changed);
786 $self->update; 800 $self->update
801 if $changed;
787} 802}
788 803
789sub _render { 804sub _render {
790 $_[0]{children}[0]->draw; 805 $_[0]{children}[0]->draw;
791} 806}
835} 850}
836 851
837sub size_request { 852sub size_request {
838 my ($self) = @_; 853 my ($self) = @_;
839 854
840 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 855 my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
841 856
842 @$self{qw(child_w child_h)} 857 $w = 10 if $self->{scroll_x};
858 $h = 10 if $self->{scroll_y};
859
860 ($w, $h)
843} 861}
844 862
845sub size_allocate { 863sub size_allocate {
846 my ($self, $w, $h) = @_; 864 my ($self, $w, $h, $changed) = @_;
847 865
848 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; 866 $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w};
849 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; 867 $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h};
850 868
851 $self->child->configure (0, 0, $w, $h); 869 $self->child->configure (0, 0, $w, $h);
938 my $child = $self->{vp}->child; 956 my $child = $self->{vp}->child;
939 $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]);
940} 958}
941 959
942sub size_allocate { 960sub size_allocate {
943 my ($self, $w, $h) = @_; 961 my ($self, $w, $h, $changed) = @_;
944 962
945 $self->SUPER::size_allocate ($w, $h); 963 $self->SUPER::size_allocate ($w, $h, $changed);
946 964
947 my $child = $self->{vp}->child; 965 my $child = $self->{vp}->child;
948 $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]);
949} 967}
950 968
997 1015
998our @ISA = CFClient::UI::Bin::; 1016our @ISA = CFClient::UI::Bin::;
999 1017
1000use CFClient::OpenGL; 1018use CFClient::OpenGL;
1001 1019
1002my @tex = 1020my $bg =
1021 new_from_file CFClient::Texture CFClient::find_rcfile "d1_bg.png",
1022 mipmap => 1, wrap => 1;
1023
1024my @border =
1003 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } 1025 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
1004 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);
1005 1027
1006sub new { 1028sub new {
1007 my $class = shift; 1029 my $class = shift;
1008 1030
1009 my $self = $class->SUPER::new ( 1031 my $self = $class->SUPER::new (
1010 bg => [1, 1, 1, 1], 1032 bg => [1, 1, 1, 1],
1011 border_bg => [1, 1, 1, 1], 1033 border_bg => [1, 1, 1, 1],
1012 border => 0.6, 1034 border => 0.6,
1013 is_toplevel => 1,
1014 can_events => 1, 1035 can_events => 1,
1036 min_w => 16,
1037 min_h => 16,
1015 @_ 1038 @_
1016 ); 1039 );
1017 1040
1018 $self->{title} &&= new CFClient::UI::Label 1041 $self->{title} &&= new CFClient::UI::Label
1019 align => 0, 1042 align => 0,
1038 $h + $self->border * 2, 1061 $h + $self->border * 2,
1039 ) 1062 )
1040} 1063}
1041 1064
1042sub size_allocate { 1065sub size_allocate {
1043 my ($self, $w, $h) = @_; 1066 my ($self, $w, $h, $changed) = @_;
1067
1068 return unless $changed;
1044 1069
1045 $h -= List::Util::max 0, $self->border * 2; 1070 $h -= List::Util::max 0, $self->border * 2;
1046 $w -= List::Util::max 0, $self->border * 2; 1071 $w -= List::Util::max 0, $self->border * 2;
1047 1072
1048 $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)
1072 my ($ev, $x, $y) = @_; 1097 my ($ev, $x, $y) = @_;
1073 1098
1074 my $dx = $ev->{x} - $ox; 1099 my $dx = $ev->{x} - $ox;
1075 my $dy = $ev->{y} - $oy; 1100 my $dy = $ev->{y} - $oy;
1076 1101
1077 $self->{user_x} = $wx + $dx * $mx;
1078 $self->{user_y} = $wy + $dy * $my;
1079 $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); 1102 $self->{force_w} = $bw + $dx * ($mx ? -1 : 1);
1080 $self->{def_h} = $bh + $dy * ($my ? -1 : 1); 1103 $self->{force_h} = $bh + $dy * ($my ? -1 : 1);
1081 $self->move ($self->{user_x}, $self->{user_y}); 1104
1082 $self->check_size; 1105 $self->realloc;
1106 $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my);
1083 }; 1107 };
1084 1108
1085 } elsif ($lr ^ $td) { 1109 } elsif ($lr ^ $td) {
1086 my ($ox, $oy) = ($ev->{x}, $ev->{y}); 1110 my ($ox, $oy) = ($ev->{x}, $ev->{y});
1087 my ($bx, $by) = ($self->{x}, $self->{y}); 1111 my ($bx, $by) = ($self->{x}, $self->{y});
1089 $self->{motion} = sub { 1113 $self->{motion} = sub {
1090 my ($ev, $x, $y) = @_; 1114 my ($ev, $x, $y) = @_;
1091 1115
1092 ($x, $y) = ($ev->{x}, $ev->{y}); 1116 ($x, $y) = ($ev->{x}, $ev->{y});
1093 1117
1094 $self->{user_x} = $bx + $x - $ox; 1118 $self->move_abs ($bx + $x - $ox, $by + $y - $oy);
1095 $self->{user_y} = $by + $y - $oy;
1096
1097 $self->move ($self->{user_x}, $self->{user_y});
1098 $self->update;
1099 }; 1119 };
1100 } 1120 }
1101} 1121}
1102 1122
1103sub button_up { 1123sub button_up {
1122 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1142 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1123 1143
1124 my $border = $self->border; 1144 my $border = $self->border;
1125 1145
1126 glColor @{ $self->{border_bg} }; 1146 glColor @{ $self->{border_bg} };
1127 $tex[1]->draw_quad_alpha (0, 0, $w, $border); 1147 $border[0]->draw_quad_alpha (0, 0, $w, $border);
1128 $tex[3]->draw_quad_alpha (0, $border, $border, $ch); 1148 $border[1]->draw_quad_alpha (0, $border, $border, $ch);
1129 $tex[2]->draw_quad_alpha ($w - $border, $border, $border, $ch); 1149 $border[2]->draw_quad_alpha ($w - $border, $border, $border, $ch);
1130 $tex[4]->draw_quad_alpha (0, $h - $border, $w, $border); 1150 $border[3]->draw_quad_alpha (0, $h - $border, $w, $border);
1131 1151
1132 if (@{$self->{bg}} < 4 || $self->{bg}[3]) { 1152 if (@{$self->{bg}} < 4 || $self->{bg}[3]) {
1133 my $bg = $tex[0]; 1153 glColor @{ $self->{bg} };
1134 1154
1135 # TODO: repeat texture not scale 1155 # TODO: repeat texture not scale
1156 # solve this better(?)
1136 my $rep_x = $cw / $bg->{w}; 1157 $bg->{s} = $cw / $bg->{w};
1137 my $rep_y = $ch / $bg->{h}; 1158 $bg->{t} = $ch / $bg->{h};
1138
1139 glColor @{ $self->{bg} };
1140
1141 $bg->{s} = $rep_x;
1142 $bg->{t} = $rep_y;
1143 $bg->{wrap_mode} = 1;
1144 $bg->draw_quad_alpha ($border, $border, $cw, $ch); 1159 $bg->draw_quad_alpha ($border, $border, $cw, $ch);
1145 } 1160 }
1146 1161
1147 glDisable GL_TEXTURE_2D; 1162 glDisable GL_TEXTURE_2D;
1148 1163
1178 my ($self, $x, $y, $child) = @_; 1193 my ($self, $x, $y, $child) = @_;
1179 1194
1180 $child->set_parent ($self); 1195 $child->set_parent ($self);
1181 $self->{children}[$y][$x] = $child; 1196 $self->{children}[$y][$x] = $child;
1182 1197
1183 $self->check_size (1); 1198 $self->realloc;
1184} 1199}
1185 1200
1186# 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?
1187sub clear { 1202sub clear {
1188 my ($self) = @_; 1203 my ($self) = @_;
1193 for (@children) { 1208 for (@children) {
1194 delete $_->{parent}; 1209 delete $_->{parent};
1195 $_->hide; 1210 $_->hide;
1196 } 1211 }
1197 1212
1198 $self->check_size (1); 1213 $self->realloc;
1199 $self->update;
1200} 1214}
1201 1215
1202sub get_wh { 1216sub get_wh {
1203 my ($self) = @_; 1217 my ($self) = @_;
1204 1218
1231 (sum @$hs), 1245 (sum @$hs),
1232 ) 1246 )
1233} 1247}
1234 1248
1235sub size_allocate { 1249sub size_allocate {
1236 my ($self, $w, $h) = @_; 1250 my ($self, $w, $h, $changed) = @_;
1237 1251
1238 my ($ws, $hs) = $self->get_wh; 1252 my ($ws, $hs) = $self->get_wh;
1239 1253
1240 my $req_w = (sum @$ws) || 1; 1254 my $req_w = (sum @$ws) || 1;
1241 my $req_h = (sum @$hs) || 1; 1255 my $req_h = (sum @$hs) || 1;
1319 (List::Util::max map $_->{req_h}, @{$self->{children}}), 1333 (List::Util::max map $_->{req_h}, @{$self->{children}}),
1320 ) 1334 )
1321} 1335}
1322 1336
1323sub size_allocate { 1337sub size_allocate {
1324 my ($self, $w, $h) = @_; 1338 my ($self, $w, $h, $changed) = @_;
1325 1339
1326 my $space = $self->{vertical} ? $h : $w; 1340 my $space = $self->{vertical} ? $h : $w;
1327 my $children = $self->{children}; 1341 my $children = $self->{children};
1328 1342
1329 my @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; 1343 my @req;
1330 my $req = List::Util::sum @req;
1331 1344
1332 if ($req > $space) { 1345 if ($self->{homogeneous}) {
1333 # ah well, not enough space 1346 @req = ($space / (@$children || 1)) x @$children;
1334 $_ *= $space / $req for @req;
1335 } else { 1347 } else {
1348 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children;
1349 my $req = List::Util::sum @req;
1350
1351 if ($req > $space) {
1352 # ah well, not enough space
1353 $_ *= $space / $req for @req;
1354 } else {
1336 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; 1355 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1;
1337 1356
1357 $space = ($space - $req) / $expand; # remaining space to give away
1358
1338 $req[$_] += ($space - $req) * $children->[$_]{expand} / $expand 1359 $req[$_] += $space * $children->[$_]{expand}
1339 for 0 .. $#$children; 1360 for 0 .. $#$children;
1361 }
1340 } 1362 }
1341 1363
1342 CFClient::UI::harmonize \@req; 1364 CFClient::UI::harmonize \@req;
1343 1365
1344 my $pos = 0; 1366 my $pos = 0;
1404 ellipsise => 3, # end 1426 ellipsise => 3, # end
1405 layout => (new CFClient::Layout), 1427 layout => (new CFClient::Layout),
1406 fontsize => 1, 1428 fontsize => 1,
1407 align => -1, 1429 align => -1,
1408 valign => -1, 1430 valign => -1,
1409 padding => 2, 1431 padding_x => 2,
1432 padding_y => 2,
1410 can_events => 0, 1433 can_events => 0,
1411 %arg 1434 %arg
1412 ); 1435 );
1413 1436
1414 if (exists $self->{template}) { 1437 if (exists $self->{template}) {
1450 $self->{text} = "T$text"; 1473 $self->{text} = "T$text";
1451 1474
1452 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; 1475 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1453 $self->{layout}->set_text ($text); 1476 $self->{layout}->set_text ($text);
1454 1477
1478 $self->realloc;
1455 $self->update; 1479 $self->update;
1456 $self->check_size;
1457} 1480}
1458 1481
1459sub set_markup { 1482sub set_markup {
1460 my ($self, $markup) = @_; 1483 my ($self, $markup) = @_;
1461 1484
1465 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1488 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1466 1489
1467 $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;
1468 $self->{layout}->set_markup ($markup); 1491 $self->{layout}->set_markup ($markup);
1469 1492
1493 $self->realloc;
1470 $self->update; 1494 $self->update;
1471 $self->check_size;
1472} 1495}
1473 1496
1474sub size_request { 1497sub size_request {
1475 my ($self) = @_; 1498 my ($self) = @_;
1476 1499
1490 1513
1491 $w = List::Util::max $w, $w2; 1514 $w = List::Util::max $w, $w2;
1492 $h = List::Util::max $h, $h2; 1515 $h = List::Util::max $h, $h2;
1493 } 1516 }
1494 1517
1495 ( 1518 ($w, $h)
1496 $w + $self->{padding} * 2,
1497 $h + $self->{padding} * 2,
1498 )
1499} 1519}
1500 1520
1501sub size_allocate { 1521sub size_allocate {
1502 my ($self, $w, $h) = @_; 1522 my ($self, $w, $h, $changed) = @_;
1503 1523
1504 delete $self->{texture}; 1524 delete $self->{texture}
1525 if $changed;
1505} 1526}
1506 1527
1507sub set_fontsize { 1528sub set_fontsize {
1508 my ($self, $fontsize) = @_; 1529 my ($self, $fontsize) = @_;
1509 1530
1510 $self->{fontsize} = $fontsize; 1531 $self->{fontsize} = $fontsize;
1511 delete $self->{texture}; 1532 delete $self->{texture};
1512 1533
1513 $self->update; 1534 $self->realloc;
1514 $self->check_size;
1515} 1535}
1516 1536
1517sub _draw { 1537sub _draw {
1518 my ($self) = @_; 1538 my ($self) = @_;
1519 1539
1527 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1547 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1528 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1548 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1529 1549
1530 my $tex = new_from_layout CFClient::Texture $self->{layout}; 1550 my $tex = new_from_layout CFClient::Texture $self->{layout};
1531 1551
1532 $self->{ox} = int ($self->{align} < 0 ? $self->{padding} 1552 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1533 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} 1553 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x}
1534 : ($self->{w} - $tex->{w}) * 0.5); 1554 : ($self->{w} - $tex->{w}) * 0.5);
1535 1555
1536 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} 1556 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1537 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} 1557 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y}
1538 : ($self->{h} - $tex->{h}) * 0.5); 1558 : ($self->{h} - $tex->{h}) * 0.5);
1539 1559
1540 $tex 1560 $tex
1541 }; 1561 };
1542 1562
1599sub set_text { 1619sub set_text {
1600 my ($self, $text) = @_; 1620 my ($self, $text) = @_;
1601 1621
1602 $self->{cursor} = length $text; 1622 $self->{cursor} = length $text;
1603 $self->_set_text ($text); 1623 $self->_set_text ($text);
1604 $self->update; 1624
1605 $self->check_size; 1625 $self->realloc;
1606} 1626}
1607 1627
1608sub get_text { 1628sub get_text {
1609 $_[0]{text} 1629 $_[0]{text}
1610} 1630}
1643 } elsif ($uni) { 1663 } elsif ($uni) {
1644 substr $text, $self->{cursor}++, 0, chr $uni; 1664 substr $text, $self->{cursor}++, 0, chr $uni;
1645 } 1665 }
1646 1666
1647 $self->_set_text ($text); 1667 $self->_set_text ($text);
1648 $self->update; 1668
1649 $self->check_size; 1669 $self->realloc;
1650} 1670}
1651 1671
1652sub focus_in { 1672sub focus_in {
1653 my ($self) = @_; 1673 my ($self) = @_;
1654 1674
1781 1801
1782sub new { 1802sub new {
1783 my $class = shift; 1803 my $class = shift;
1784 1804
1785 $class->SUPER::new ( 1805 $class->SUPER::new (
1786 padding => 4, 1806 padding_x => 4,
1807 padding_y => 4,
1787 fg => [1, 1, 1], 1808 fg => [1, 1, 1],
1788 active_fg => [0, 0, 1], 1809 active_fg => [0, 0, 1],
1789 can_hover => 1, 1810 can_hover => 1,
1790 align => 0, 1811 align => 0,
1791 valign => 0, 1812 valign => 0,
1838 1859
1839sub new { 1860sub new {
1840 my $class = shift; 1861 my $class = shift;
1841 1862
1842 $class->SUPER::new ( 1863 $class->SUPER::new (
1843 padding => 2, 1864 padding_x => 2,
1865 padding_y => 2,
1844 fg => [1, 1, 1], 1866 fg => [1, 1, 1],
1845 active_fg => [1, 1, 0], 1867 active_fg => [1, 1, 0],
1846 bg => [0, 0, 0, 0.2], 1868 bg => [0, 0, 0, 0.2],
1847 active_bg => [1, 1, 1, 0.5], 1869 active_bg => [1, 1, 1, 0.5],
1848 state => 0, 1870 state => 0,
1852} 1874}
1853 1875
1854sub size_request { 1876sub size_request {
1855 my ($self) = @_; 1877 my ($self) = @_;
1856 1878
1857 ($self->{padding} * 2 + 6) x 2 1879 (6) x 2
1858} 1880}
1859 1881
1860sub button_down { 1882sub button_down {
1861 my ($self, $ev, $x, $y) = @_; 1883 my ($self, $ev, $x, $y) = @_;
1862 1884
1863 if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} 1885 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
1864 && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { 1886 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
1865 $self->{state} = !$self->{state}; 1887 $self->{state} = !$self->{state};
1866 $self->_emit (changed => $self->{state}); 1888 $self->_emit (changed => $self->{state});
1867 } 1889 }
1868} 1890}
1869 1891
1870sub _draw { 1892sub _draw {
1871 my ($self) = @_; 1893 my ($self) = @_;
1872 1894
1873 $self->SUPER::_draw; 1895 $self->SUPER::_draw;
1874 1896
1875 glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; 1897 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
1876 1898
1877 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;
1878 1902
1879 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; 1903 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
1880 1904
1881 my $tex = $self->{state} ? $tex[1] : $tex[0]; 1905 my $tex = $self->{state} ? $tex[1] : $tex[0];
1882 1906
2147 fg => [1, 1, 1], 2171 fg => [1, 1, 1],
2148 active_fg => [0, 0, 0], 2172 active_fg => [0, 0, 0],
2149 bg => [0, 0, 0, 0.2], 2173 bg => [0, 0, 0, 0.2],
2150 active_bg => [1, 1, 1, 0.5], 2174 active_bg => [1, 1, 1, 0.5],
2151 range => [0, 0, 100, 10, 0], 2175 range => [0, 0, 100, 10, 0],
2152 req_w => $::WIDTH / 80, 2176 min_w => $::WIDTH / 80,
2153 req_h => $::WIDTH / 80, 2177 min_h => $::WIDTH / 80,
2154 vertical => 0, 2178 vertical => 0,
2155 can_hover => 1, 2179 can_hover => 1,
2156 inner_pad => 0.02, 2180 inner_pad => 0.02,
2157 @_ 2181 @_
2158 ); 2182 );
2161 $self->update; 2185 $self->update;
2162 2186
2163 $self 2187 $self
2164} 2188}
2165 2189
2190sub changed { }
2191
2166sub set_range { 2192sub set_range {
2167 my ($self, $range) = @_; 2193 my ($self, $range) = @_;
2168 2194
2169 ($range, $self->{range}) = ($self->{range}, $range); 2195 ($range, $self->{range}) = ($self->{range}, $range);
2170 2196
2196} 2222}
2197 2223
2198sub size_request { 2224sub size_request {
2199 my ($self) = @_; 2225 my ($self) = @_;
2200 2226
2201 my $w = $self->{req_w}; 2227 ($self->{req_w}, $self->{req_h})
2202 my $h = $self->{req_h};
2203
2204 $self->{vertical} ? ($h, $w) : ($w, $h)
2205} 2228}
2206 2229
2207sub button_down { 2230sub button_down {
2208 my ($self, $ev, $x, $y) = @_; 2231 my ($self, $ev, $x, $y) = @_;
2209 2232
2359 $self->{fontsize} = $fontsize; 2382 $self->{fontsize} = $fontsize;
2360 $self->reflow; 2383 $self->reflow;
2361} 2384}
2362 2385
2363sub size_allocate { 2386sub size_allocate {
2364 my ($self, $w, $h) = @_; 2387 my ($self, $w, $h, $changed) = @_;
2365 2388
2366 $self->SUPER::size_allocate ($w, $h); 2389 $self->SUPER::size_allocate ($w, $h, $changed);
2390
2391 return unless $changed;
2367 2392
2368 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2393 $self->{layout}->set_font ($self->{font}) if $self->{font};
2369 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2394 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2370 $self->{layout}->set_width ($self->{children}[0]{w}); 2395 $self->{layout}->set_width ($self->{children}[0]{w});
2371 2396
2613 2638
2614 ($w + 4, $h + 4) 2639 ($w + 4, $h + 4)
2615} 2640}
2616 2641
2617sub size_allocate { 2642sub size_allocate {
2618 my ($self, $w, $h) = @_; 2643 my ($self, $w, $h, $changed) = @_;
2619 2644
2645 return unless $changed;
2646
2620 $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 });
2621} 2666}
2622 2667
2623sub _draw { 2668sub _draw {
2624 my ($self) = @_; 2669 my ($self) = @_;
2625 2670
2642 glVertex $w, $h; 2687 glVertex $w, $h;
2643 glVertex $w, 0; 2688 glVertex $w, 0;
2644 glEnd; 2689 glEnd;
2645 2690
2646 glTranslate 2 - 0.375, 2 - 0.375; 2691 glTranslate 2 - 0.375, 2 - 0.375;
2692
2647 $self->SUPER::_draw; 2693 $self->SUPER::_draw;
2648} 2694}
2649 2695
2650############################################################################# 2696#############################################################################
2651 2697
2816 # maybe save $GRAB? must be careful about events... 2862 # maybe save $GRAB? must be careful about events...
2817 $GRAB = $self; 2863 $GRAB = $self;
2818 $self->{button} = $ev->{button}; 2864 $self->{button} = $ev->{button};
2819 2865
2820 $self->show; 2866 $self->show;
2821 $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);
2822} 2868}
2823 2869
2824sub mouse_motion { 2870sub mouse_motion {
2825 my ($self, $ev, $x, $y) = @_; 2871 my ($self, $ev, $x, $y) = @_;
2826 2872
2960use CFClient::OpenGL; 3006use CFClient::OpenGL;
2961 3007
2962sub new { 3008sub new {
2963 my $class = shift; 3009 my $class = shift;
2964 3010
2965 $class->SUPER::new ( 3011 my $self = $class->SUPER::new (
2966 visible => 1, 3012 visible => 1,
2967 @_, 3013 @_,
2968 ) 3014 );
3015
3016 Scalar::Util::weaken ($self->{root} = $self);
3017
3018 $self
2969} 3019}
2970 3020
2971sub configure { 3021sub configure {
2972 my ($self, $x, $y, $w, $h) = @_; 3022 my ($self, $x, $y, $w, $h) = @_;
2973 3023
2974 $self->{w} = $w; 3024 $self->{w} = $w;
2975 $self->{h} = $h; 3025 $self->{h} = $h;
2976} 3026}
2977 3027
2978sub check_size { 3028sub reconfigure {
2979 my ($self) = @_; 3029 my ($self) = @_;
2980 3030
3031 $self->SUPER::reconfigure;
3032
2981 $self->size_allocate ($self->{w}, $self->{h}) 3033 $self->size_allocate ($self->{w}, $self->{h}, 1)
2982 if $self->{w}; 3034 if $self->{w};
2983} 3035}
2984 3036
2985sub size_request { 3037sub size_request {
2986 my ($self) = @_; 3038 my ($self) = @_;
2987 3039
2988 ($self->{w}, $self->{h}) 3040 ($self->{w}, $self->{h})
2989} 3041}
2990 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
2991sub size_allocate { 3057sub size_allocate {
2992 my ($self, $w, $h) = @_; 3058 my ($self, $w, $h, $changed) = @_;
2993 3059
2994 for my $child ($self->children) { 3060 for my $child ($self->children) {
2995 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)};
2996 3062
2997 $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 3063 $X = $child->{force_x} if exists $child->{force_x};
2998 if exists $child->{req_x}; 3064 $Y = $child->{force_y} if exists $child->{force_y};
2999 3065
3000 $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 3066 $X = _to_pixel $X, $W, $self->{w};
3001 if exists $child->{req_y}; 3067 $Y = _to_pixel $Y, $H, $self->{h};
3002
3003 delete @$child{qw(req_x req_y)};
3004
3005 $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5;
3006 $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5;
3007 3068
3008 $child->configure ($X, $Y, $W, $H); 3069 $child->configure ($X, $Y, $W, $H);
3009 } 3070 }
3010} 3071}
3011 3072
3022} 3083}
3023 3084
3024sub update { 3085sub update {
3025 my ($self) = @_; 3086 my ($self) = @_;
3026 3087
3027 $self->check_size;
3028 $::WANT_REFRESH++; 3088 $::WANT_REFRESH++;
3029} 3089}
3030 3090
3031sub add { 3091sub add {
3032 my ($self, @children) = @_; 3092 my ($self, @children) = @_;
3033 3093
3034 for my $child (@children) {
3035 $child->{is_toplevel} = 1; 3094 $_->{is_toplevel} = 1
3036 3095 for @children;
3037 # integerise window positions
3038 $child->{x} = int $child->{x};
3039 $child->{y} = int $child->{y};
3040 }
3041 3096
3042 $self->SUPER::add (@children); 3097 $self->SUPER::add (@children);
3043
3044 for (my @widgets = @children; my $w = pop @widgets; ) {
3045 push @widgets, $w->children;
3046 $w->set_visible;
3047 }
3048
3049} 3098}
3050 3099
3051sub remove { 3100sub remove {
3052 my ($self, @children) = @_; 3101 my ($self, @children) = @_;
3053 3102
3054 $self->SUPER::remove (@children); 3103 $self->SUPER::remove (@children);
3104
3105 delete $self->{is_toplevel}
3106 for @children;
3055 3107
3056 while (@children) { 3108 while (@children) {
3057 my $w = pop @children; 3109 my $w = pop @children;
3058 push @children, $w->children; 3110 push @children, $w->children;
3059 $w->set_invisible; 3111 $w->set_invisible;
3078 while ($self->{refresh_hook}) { 3130 while ($self->{refresh_hook}) {
3079 $_->() 3131 $_->()
3080 for values %{delete $self->{refresh_hook}}; 3132 for values %{delete $self->{refresh_hook}};
3081 } 3133 }
3082 3134
3083 if ($self->{check_size}) { 3135 if ($self->{realloc}) {
3084 my @queue; 3136 my @queue;
3085 3137
3086 for (;;) { 3138 while () {
3087 if ($self->{check_size}) { 3139 if ($self->{realloc}) {
3088 #TODO use array-of-depth approach 3140 #TODO use array-of-depth approach
3089 3141
3090 @queue = sort { $a->{visible} <=> $b->{visible} } 3142 @queue = sort { $a->{visible} <=> $b->{visible} }
3091 @queue, values %{delete $self->{check_size}}; 3143 @queue, values %{delete $self->{realloc}};
3092 } 3144 }
3093 3145
3094 my $widget = pop @queue || last; 3146 my $widget = pop @queue || last;
3095 3147
3096 defined $widget->{visible} or last; # do not resize invisible widgets 3148 $widget->{visible} or last; # do not resize invisible widgets
3097 3149
3098 my ($w, $h) = $widget->{def_w} && $widget->{def_h}
3099 ? @$widget{qw(def_w def_h)}
3100 : $widget->size_request; 3150 my ($w, $h) = $widget->size_request;
3101 3151
3102 if (delete $widget->{force_alloc} 3152 $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2;
3103 or $w != $widget->{req_w} or $h != $widget->{req_h}) { 3153 $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2;
3104 Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# 3154
3105 3155 $w = $widget->{force_w} if exists $widget->{force_w};
3156 $h = $widget->{force_h} if exists $widget->{force_h};
3157
3106 $widget->{req_w} = $w; 3158 $widget->{req_w} = $w;
3107 $widget->{req_h} = $h; 3159 $widget->{req_h} = $h;
3108 3160
3109 $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; 3161 $self->{size_alloc}{$widget} = [$widget, undef, undef];
3110 3162
3111 $widget->{parent}->check_size 3163 push @queue, $widget->{parent}
3112 if $widget->{parent}; 3164 if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent};
3113 }
3114 } 3165 }
3115 } 3166 }
3116 3167
3117 while ($self->{size_alloc}) { 3168 while (my $size_alloc = delete $self->{size_alloc}) {
3118 for (values %{delete $self->{size_alloc}}) { 3169 my @queue = sort $b->[0]{visible} <=> $a->[0]{visible},
3119 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;
3120 3177
3121 $w = 0 if $w < 0; 3178 $w = 0 if $w < 0;
3122 $h = 0 if $h < 0; 3179 $h = 0 if $h < 0;
3123 3180
3181 $w = int $w + 0.5;
3182 $h = int $h + 0.5;
3183
3184 my $changed = $widget->{w} != $w || $widget->{h} != $h;
3185
3124 $widget->{w} = $w; 3186 $widget->{w} = $w;
3125 $widget->{h} = $h; 3187 $widget->{h} = $h;
3188
3126 $widget->emit (size_allocate => $w, $h); 3189 $widget->emit (size_allocate => $w, $h, $changed);
3127 } 3190 }
3128 } 3191 }
3129 3192
3130 while ($self->{post_alloc_hook}) { 3193 while ($self->{post_alloc_hook}) {
3131 $_->() 3194 $_->()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines