… | |
… | |
40 | if (!$GRAB) { |
40 | if (!$GRAB) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
42 | if (length $widget->{tooltip}) { |
42 | if (length $widget->{tooltip}) { |
43 | |
43 | |
44 | if ($TOOLTIP->{owner} != $widget) { |
44 | if ($TOOLTIP->{owner} != $widget) { |
|
|
45 | $TOOLTIP->hide; |
|
|
46 | |
45 | $TOOLTIP->{owner} = $widget; |
47 | $TOOLTIP->{owner} = $widget; |
46 | |
48 | |
47 | my $tip = $widget->{tooltip}; |
49 | my $tip = $widget->{tooltip}; |
48 | |
50 | |
49 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
51 | $tip = $tip->($widget) if CODE:: eq ref $tip; |
50 | |
52 | |
51 | $TOOLTIP->set_tooltip_from ($widget); |
53 | $TOOLTIP->set_tooltip_from ($widget); |
52 | $TOOLTIP->show; |
54 | $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 | } |
55 | } |
63 | |
56 | |
64 | return; |
57 | return; |
65 | } |
58 | } |
66 | } |
59 | } |
… | |
… | |
221 | } |
214 | } |
222 | |
215 | |
223 | $self |
216 | $self |
224 | } |
217 | } |
225 | |
218 | |
226 | sub toggle_visibility { |
|
|
227 | my ($self) = @_; |
|
|
228 | |
|
|
229 | if ($self->{visible}) { |
|
|
230 | $self->hide; |
|
|
231 | } else { |
|
|
232 | $self->show; |
|
|
233 | } |
|
|
234 | } |
|
|
235 | |
|
|
236 | sub destroy { |
219 | sub destroy { |
237 | my ($self) = @_; |
220 | my ($self) = @_; |
238 | |
221 | |
239 | $self->hide; |
222 | $self->hide; |
240 | %$self = (); |
223 | %$self = (); |
241 | } |
224 | } |
242 | |
225 | |
243 | sub show { |
226 | sub show { |
244 | my ($self) = @_; |
227 | my ($self) = @_; |
|
|
228 | |
245 | return if $self->{parent}; |
229 | return if $self->{parent}; |
246 | |
230 | |
247 | $CFClient::UI::ROOT->add ($self); |
231 | $CFClient::UI::ROOT->add ($self); |
248 | } |
232 | } |
249 | |
233 | |
250 | sub show_centered { |
234 | sub center { |
251 | my ($self) = @_; |
235 | my ($self) = @_; |
252 | return if $self->{parent}; |
|
|
253 | |
|
|
254 | $self->show; |
|
|
255 | |
236 | |
256 | $CFClient::UI::ROOT->on_post_alloc ( |
237 | $CFClient::UI::ROOT->on_post_alloc ( |
257 | "centered $self" => sub { |
238 | "center_$self" => sub { |
258 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
239 | $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5); |
259 | }, |
240 | }, |
260 | ); |
241 | ); |
|
|
242 | |
|
|
243 | $self->update; |
261 | } |
244 | } |
262 | |
245 | |
263 | sub set_visible { |
246 | sub set_visible { |
264 | my ($self) = @_; |
247 | my ($self) = @_; |
265 | |
248 | |
266 | return if $self->{visible}; |
249 | return if $self->{visible}; |
267 | |
250 | |
268 | $self->{root} = $self->{parent}{root}; |
251 | $self->{root} = $self->{parent}{root}; |
269 | $self->{visible} = $self->{parent}{visible} + 1; |
252 | $self->{visible} = $self->{parent}{visible} + 1; |
270 | |
253 | |
271 | $self->emit (visibility_change => 1) |
254 | $self->emit (visibility_change => 1); |
|
|
255 | |
|
|
256 | $self->realloc if !exists $self->{req_w}; |
|
|
257 | |
|
|
258 | $_->set_visible for $self->children; |
272 | } |
259 | } |
273 | |
260 | |
274 | sub set_invisible { |
261 | sub set_invisible { |
275 | my ($self) = @_; |
262 | my ($self) = @_; |
276 | |
263 | |
277 | return unless $self->{visible}; |
264 | return unless $self->{visible}; |
278 | |
265 | |
279 | # broken show/hide model |
266 | $_->set_invisible for $self->children; |
280 | |
267 | |
281 | delete $self->{root}; |
268 | delete $self->{root}; |
282 | delete $self->{visible}; |
269 | delete $self->{visible}; |
283 | |
270 | |
284 | undef $GRAB if $GRAB == $self; |
271 | undef $GRAB if $GRAB == $self; |
285 | undef $HOVER if $HOVER == $self; |
272 | undef $HOVER if $HOVER == $self; |
286 | |
273 | |
287 | CFClient::UI::check_tooltip |
274 | CFClient::UI::check_tooltip |
288 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
275 | if $TOOLTIP->{owner} == $self; |
289 | |
276 | |
290 | $self->focus_out; |
277 | $self->focus_out; |
291 | |
278 | |
292 | $self->emit (visibility_change => 0); |
279 | $self->emit (visibility_change => 0); |
|
|
280 | } |
|
|
281 | |
|
|
282 | sub set_visibility { |
|
|
283 | my ($self, $visible) = @_; |
|
|
284 | |
|
|
285 | return if $self->{visible} == $visible; |
|
|
286 | |
|
|
287 | $visible ? $self->hide |
|
|
288 | : $self->show; |
|
|
289 | } |
|
|
290 | |
|
|
291 | sub toggle_visibility { |
|
|
292 | my ($self) = @_; |
|
|
293 | |
|
|
294 | $self->{visible} |
|
|
295 | ? $self->hide |
|
|
296 | : $self->show; |
293 | } |
297 | } |
294 | |
298 | |
295 | sub hide { |
299 | sub hide { |
296 | my ($self) = @_; |
300 | my ($self) = @_; |
297 | |
301 | |
… | |
… | |
315 | my ($self, $w, $h) = @_; |
319 | my ($self, $w, $h) = @_; |
316 | |
320 | |
317 | $self->{def_w} = $w; |
321 | $self->{def_w} = $w; |
318 | $self->{def_h} = $h; |
322 | $self->{def_h} = $h; |
319 | |
323 | |
320 | $self->check_size; |
324 | $self->realloc; |
321 | } |
325 | } |
322 | |
326 | |
323 | sub size_request { |
327 | sub size_request { |
324 | require Carp; |
328 | require Carp; |
325 | Carp::confess "size_request is abstract"; |
329 | Carp::confess "size_request is abstract"; |
… | |
… | |
345 | $self->{y} = $y; |
349 | $self->{y} = $y; |
346 | $self->update; |
350 | $self->update; |
347 | } |
351 | } |
348 | |
352 | |
349 | if ($self->{w} != $w || $self->{h} != $h) { |
353 | if ($self->{w} != $w || $self->{h} != $h) { |
|
|
354 | return unless $self->{visible}; |
|
|
355 | |
350 | $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h]; |
356 | $self->{root}->{size_alloc}{$self+0} = [$self, $w, $h]; |
351 | } |
357 | } |
352 | } |
358 | } |
353 | |
359 | |
354 | sub size_allocate { |
360 | sub size_allocate { |
355 | # nothing to be done |
361 | # nothing to be done |
356 | } |
|
|
357 | |
|
|
358 | sub reconfigure { |
|
|
359 | my ($self) = @_; |
|
|
360 | |
|
|
361 | $self->check_size (1); |
|
|
362 | $self->update; |
|
|
363 | } |
362 | } |
364 | |
363 | |
365 | sub children { |
364 | sub children { |
366 | } |
365 | } |
367 | |
366 | |
… | |
… | |
445 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
444 | sub w { $_[0]{w} = $_[1] if @_ > 1; $_[0]{w} } |
446 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
445 | sub h { $_[0]{h} = $_[1] if @_ > 1; $_[0]{h} } |
447 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
446 | sub x { $_[0]{x} = $_[1] if @_ > 1; $_[0]{x} } |
448 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
447 | sub y { $_[0]{y} = $_[1] if @_ > 1; $_[0]{y} } |
449 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
448 | sub z { $_[0]{z} = $_[1] if @_ > 1; $_[0]{z} } |
|
|
449 | |
|
|
450 | sub find_widget { |
|
|
451 | my ($self, $x, $y) = @_; |
|
|
452 | |
|
|
453 | return () unless $self->{can_events}; |
|
|
454 | |
|
|
455 | return $self |
|
|
456 | if $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
457 | && $y >= $self->{y} && $y < $self->{y} + $self->{h}; |
|
|
458 | |
|
|
459 | () |
|
|
460 | } |
|
|
461 | |
|
|
462 | sub set_parent { |
|
|
463 | my ($self, $parent) = @_; |
|
|
464 | |
|
|
465 | Scalar::Util::weaken ($self->{parent} = $parent); |
|
|
466 | |
|
|
467 | $self->set_visible if $parent->{visible}; |
|
|
468 | } |
|
|
469 | |
|
|
470 | sub connect { |
|
|
471 | my ($self, $signal, $cb) = @_; |
|
|
472 | |
|
|
473 | push @{ $self->{signal_cb}{$signal} }, $cb; |
|
|
474 | } |
|
|
475 | |
|
|
476 | sub _emit { |
|
|
477 | my ($self, $signal, @args) = @_; |
|
|
478 | |
|
|
479 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
480 | } |
|
|
481 | |
|
|
482 | sub emit { |
|
|
483 | my ($self, $signal, @args) = @_; |
|
|
484 | |
|
|
485 | $self->_emit ($signal, @args) |
|
|
486 | || $self->$signal (@args); |
|
|
487 | } |
|
|
488 | |
|
|
489 | sub visibility_change { |
|
|
490 | #my ($self, $visible) = @_; |
|
|
491 | } |
|
|
492 | |
|
|
493 | sub realloc { |
|
|
494 | my ($self) = @_; |
|
|
495 | |
|
|
496 | if ($self->{visible}) { |
|
|
497 | return if $self->{root}{realloc}{$self}; |
|
|
498 | |
|
|
499 | $self->{root}{realloc}{$self} = $self; |
|
|
500 | $self->{root}->update; |
|
|
501 | } else { |
|
|
502 | delete $self->{req_w}; |
|
|
503 | } |
|
|
504 | } |
|
|
505 | |
|
|
506 | sub update { |
|
|
507 | my ($self) = @_; |
|
|
508 | |
|
|
509 | $self->{parent}->update |
|
|
510 | if $self->{parent}; |
|
|
511 | } |
450 | |
512 | |
451 | sub draw { |
513 | sub draw { |
452 | my ($self) = @_; |
514 | my ($self) = @_; |
453 | |
515 | |
454 | return unless $self->{h} && $self->{w}; |
516 | return unless $self->{h} && $self->{w}; |
… | |
… | |
492 | my ($self) = @_; |
554 | my ($self) = @_; |
493 | |
555 | |
494 | warn "no draw defined for $self\n"; |
556 | warn "no draw defined for $self\n"; |
495 | } |
557 | } |
496 | |
558 | |
497 | sub 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 | |
|
|
509 | sub 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 | |
|
|
520 | sub check_size { |
|
|
521 | my ($self, $forced) = @_; |
|
|
522 | |
|
|
523 | $self->{force_alloc} = 1 if $forced; |
|
|
524 | $CFClient::UI::ROOT->{check_size}{$self} = $self; |
|
|
525 | } |
|
|
526 | |
|
|
527 | sub update { |
|
|
528 | my ($self) = @_; |
|
|
529 | |
|
|
530 | $self->{parent}->update |
|
|
531 | if $self->{parent}; |
|
|
532 | } |
|
|
533 | |
|
|
534 | sub connect { |
|
|
535 | my ($self, $signal, $cb) = @_; |
|
|
536 | |
|
|
537 | push @{ $self->{signal_cb}{$signal} }, $cb; |
|
|
538 | } |
|
|
539 | |
|
|
540 | sub _emit { |
|
|
541 | my ($self, $signal, @args) = @_; |
|
|
542 | |
|
|
543 | List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []} |
|
|
544 | } |
|
|
545 | |
|
|
546 | sub emit { |
|
|
547 | my ($self, $signal, @args) = @_; |
|
|
548 | |
|
|
549 | $self->_emit ($signal, @args) |
|
|
550 | || $self->$signal (@args); |
|
|
551 | } |
|
|
552 | |
|
|
553 | sub visibility_change { |
|
|
554 | #my ($self, $visible) = @_; |
|
|
555 | } |
|
|
556 | |
|
|
557 | sub DESTROY { |
559 | sub DESTROY { |
558 | my ($self) = @_; |
560 | my ($self) = @_; |
559 | |
561 | |
560 | delete $WIDGET{$self+0}; |
562 | delete $WIDGET{$self+0}; |
561 | #$self->deactivate; |
563 | #$self->deactivate; |
… | |
… | |
656 | $self->{children} = [ |
658 | $self->{children} = [ |
657 | sort { $a->{z} <=> $b->{z} } |
659 | sort { $a->{z} <=> $b->{z} } |
658 | @{$self->{children}}, @widgets |
660 | @{$self->{children}}, @widgets |
659 | ]; |
661 | ]; |
660 | |
662 | |
661 | $self->check_size (1); |
663 | $self->realloc; |
662 | $self->update; |
|
|
663 | } |
664 | } |
664 | |
665 | |
665 | sub children { |
666 | sub children { |
666 | @{ $_[0]{children} } |
667 | @{ $_[0]{children} } |
667 | } |
668 | } |
… | |
… | |
672 | delete $child->{parent}; |
673 | delete $child->{parent}; |
673 | $child->hide; |
674 | $child->hide; |
674 | |
675 | |
675 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
676 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
676 | |
677 | |
677 | $self->check_size (1); |
678 | $self->realloc; |
678 | $self->update; |
|
|
679 | } |
679 | } |
680 | |
680 | |
681 | sub clear { |
681 | sub clear { |
682 | my ($self) = @_; |
682 | my ($self) = @_; |
683 | |
683 | |
… | |
… | |
687 | for (@$children) { |
687 | for (@$children) { |
688 | delete $_->{parent}; |
688 | delete $_->{parent}; |
689 | $_->hide; |
689 | $_->hide; |
690 | } |
690 | } |
691 | |
691 | |
692 | $self->check_size; |
692 | $self->realloc; |
693 | $self->update; |
|
|
694 | } |
693 | } |
695 | |
694 | |
696 | sub find_widget { |
695 | sub find_widget { |
697 | my ($self, $x, $y) = @_; |
696 | my ($self, $x, $y) = @_; |
698 | |
697 | |
… | |
… | |
751 | sub size_request { |
750 | sub size_request { |
752 | $_[0]{children}[0]->size_request |
751 | $_[0]{children}[0]->size_request |
753 | } |
752 | } |
754 | |
753 | |
755 | sub size_allocate { |
754 | sub size_allocate { |
756 | my ($self, $w, $h) = @_; |
755 | my ($self, $w, $h, $changed) = @_; |
757 | |
756 | |
758 | $self->{children}[0]->configure (0, 0, $w, $h); |
757 | $self->{children}[0]->configure (0, 0, $w, $h); |
759 | } |
758 | } |
760 | |
759 | |
761 | ############################################################################# |
760 | ############################################################################# |
… | |
… | |
778 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
777 | $ROOT->on_post_alloc ($self => sub { $self->render_child }); |
779 | $self->SUPER::update; |
778 | $self->SUPER::update; |
780 | } |
779 | } |
781 | |
780 | |
782 | sub size_allocate { |
781 | sub size_allocate { |
783 | my ($self, $w, $h) = @_; |
782 | my ($self, $w, $h, $changed) = @_; |
784 | |
783 | |
785 | $self->SUPER::size_allocate ($w, $h); |
784 | $self->SUPER::size_allocate ($w, $h, $changed); |
786 | $self->update; |
785 | $self->update |
|
|
786 | if $changed; |
787 | } |
787 | } |
788 | |
788 | |
789 | sub _render { |
789 | sub _render { |
790 | $_[0]{children}[0]->draw; |
790 | $_[0]{children}[0]->draw; |
791 | } |
791 | } |
… | |
… | |
835 | } |
835 | } |
836 | |
836 | |
837 | sub size_request { |
837 | sub size_request { |
838 | my ($self) = @_; |
838 | my ($self) = @_; |
839 | |
839 | |
840 | @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
840 | my ($w, $h) = @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; |
841 | |
841 | |
842 | @$self{qw(child_w child_h)} |
842 | $w = 10 if $self->{scroll_x}; |
|
|
843 | $h = 10 if $self->{scroll_y}; |
|
|
844 | |
|
|
845 | ($w, $h) |
843 | } |
846 | } |
844 | |
847 | |
845 | sub size_allocate { |
848 | sub size_allocate { |
846 | my ($self, $w, $h) = @_; |
849 | my ($self, $w, $h, $changed) = @_; |
847 | |
850 | |
848 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
851 | $w = $self->{child_w} if $self->{scroll_x} && $self->{child_w}; |
849 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
852 | $h = $self->{child_h} if $self->{scroll_y} && $self->{child_h}; |
850 | |
853 | |
851 | $self->child->configure (0, 0, $w, $h); |
854 | $self->child->configure (0, 0, $w, $h); |
… | |
… | |
938 | my $child = $self->{vp}->child; |
941 | my $child = $self->{vp}->child; |
939 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
942 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
940 | } |
943 | } |
941 | |
944 | |
942 | sub size_allocate { |
945 | sub size_allocate { |
943 | my ($self, $w, $h) = @_; |
946 | my ($self, $w, $h, $changed) = @_; |
944 | |
947 | |
945 | $self->SUPER::size_allocate ($w, $h); |
948 | $self->SUPER::size_allocate ($w, $h, $changed); |
946 | |
949 | |
947 | my $child = $self->{vp}->child; |
950 | my $child = $self->{vp}->child; |
948 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
951 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
949 | } |
952 | } |
950 | |
953 | |
… | |
… | |
1038 | $h + $self->border * 2, |
1041 | $h + $self->border * 2, |
1039 | ) |
1042 | ) |
1040 | } |
1043 | } |
1041 | |
1044 | |
1042 | sub size_allocate { |
1045 | sub size_allocate { |
1043 | my ($self, $w, $h) = @_; |
1046 | my ($self, $w, $h, $changed) = @_; |
|
|
1047 | |
|
|
1048 | return unless $changed; |
1044 | |
1049 | |
1045 | $h -= List::Util::max 0, $self->border * 2; |
1050 | $h -= List::Util::max 0, $self->border * 2; |
1046 | $w -= List::Util::max 0, $self->border * 2; |
1051 | $w -= List::Util::max 0, $self->border * 2; |
1047 | |
1052 | |
1048 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
1053 | $self->{title}->configure ($self->border, int $self->border - $::FONTSIZE * 2, $w, int $::FONTSIZE * 2) |
… | |
… | |
1077 | $self->{user_x} = $wx + $dx * $mx; |
1082 | $self->{user_x} = $wx + $dx * $mx; |
1078 | $self->{user_y} = $wy + $dy * $my; |
1083 | $self->{user_y} = $wy + $dy * $my; |
1079 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1084 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1080 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1085 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1081 | $self->move ($self->{user_x}, $self->{user_y}); |
1086 | $self->move ($self->{user_x}, $self->{user_y}); |
1082 | $self->check_size; |
1087 | $self->realloc; |
1083 | }; |
1088 | }; |
1084 | |
1089 | |
1085 | } elsif ($lr ^ $td) { |
1090 | } elsif ($lr ^ $td) { |
1086 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1091 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1087 | my ($bx, $by) = ($self->{x}, $self->{y}); |
1092 | my ($bx, $by) = ($self->{x}, $self->{y}); |
… | |
… | |
1093 | |
1098 | |
1094 | $self->{user_x} = $bx + $x - $ox; |
1099 | $self->{user_x} = $bx + $x - $ox; |
1095 | $self->{user_y} = $by + $y - $oy; |
1100 | $self->{user_y} = $by + $y - $oy; |
1096 | |
1101 | |
1097 | $self->move ($self->{user_x}, $self->{user_y}); |
1102 | $self->move ($self->{user_x}, $self->{user_y}); |
1098 | $self->update; |
1103 | $self->realloc; |
1099 | }; |
1104 | }; |
1100 | } |
1105 | } |
1101 | } |
1106 | } |
1102 | |
1107 | |
1103 | sub button_up { |
1108 | sub button_up { |
… | |
… | |
1178 | my ($self, $x, $y, $child) = @_; |
1183 | my ($self, $x, $y, $child) = @_; |
1179 | |
1184 | |
1180 | $child->set_parent ($self); |
1185 | $child->set_parent ($self); |
1181 | $self->{children}[$y][$x] = $child; |
1186 | $self->{children}[$y][$x] = $child; |
1182 | |
1187 | |
1183 | $self->check_size (1); |
1188 | $self->realloc; |
1184 | } |
1189 | } |
1185 | |
1190 | |
1186 | # TODO: move to container class maybe? send children a signal on removal? |
1191 | # TODO: move to container class maybe? send children a signal on removal? |
1187 | sub clear { |
1192 | sub clear { |
1188 | my ($self) = @_; |
1193 | my ($self) = @_; |
… | |
… | |
1193 | for (@children) { |
1198 | for (@children) { |
1194 | delete $_->{parent}; |
1199 | delete $_->{parent}; |
1195 | $_->hide; |
1200 | $_->hide; |
1196 | } |
1201 | } |
1197 | |
1202 | |
1198 | $self->check_size (1); |
1203 | $self->realloc; |
1199 | $self->update; |
|
|
1200 | } |
1204 | } |
1201 | |
1205 | |
1202 | sub get_wh { |
1206 | sub get_wh { |
1203 | my ($self) = @_; |
1207 | my ($self) = @_; |
1204 | |
1208 | |
… | |
… | |
1231 | (sum @$hs), |
1235 | (sum @$hs), |
1232 | ) |
1236 | ) |
1233 | } |
1237 | } |
1234 | |
1238 | |
1235 | sub size_allocate { |
1239 | sub size_allocate { |
1236 | my ($self, $w, $h) = @_; |
1240 | my ($self, $w, $h, $changed) = @_; |
1237 | |
1241 | |
1238 | my ($ws, $hs) = $self->get_wh; |
1242 | my ($ws, $hs) = $self->get_wh; |
1239 | |
1243 | |
1240 | my $req_w = (sum @$ws) || 1; |
1244 | my $req_w = (sum @$ws) || 1; |
1241 | my $req_h = (sum @$hs) || 1; |
1245 | my $req_h = (sum @$hs) || 1; |
… | |
… | |
1319 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1323 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
1320 | ) |
1324 | ) |
1321 | } |
1325 | } |
1322 | |
1326 | |
1323 | sub size_allocate { |
1327 | sub size_allocate { |
1324 | my ($self, $w, $h) = @_; |
1328 | my ($self, $w, $h, $changed) = @_; |
1325 | |
1329 | |
1326 | my $space = $self->{vertical} ? $h : $w; |
1330 | my $space = $self->{vertical} ? $h : $w; |
1327 | my $children = $self->{children}; |
1331 | my $children = $self->{children}; |
1328 | |
1332 | |
1329 | my @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; |
1333 | my @req; |
1330 | my $req = List::Util::sum @req; |
|
|
1331 | |
1334 | |
1332 | if ($req > $space) { |
1335 | if ($self->{homogeneous}) { |
1333 | # ah well, not enough space |
1336 | @req = ($space / (@$children || 1)) x @$children; |
1334 | $_ *= $space / $req for @req; |
|
|
1335 | } else { |
1337 | } else { |
|
|
1338 | @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; |
|
|
1339 | my $req = List::Util::sum @req; |
|
|
1340 | |
|
|
1341 | if ($req > $space) { |
|
|
1342 | # ah well, not enough space |
|
|
1343 | $_ *= $space / $req for @req; |
|
|
1344 | } else { |
1336 | my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; |
1345 | my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; |
1337 | |
1346 | |
|
|
1347 | $space = ($space - $req) / $expand; # remaining space to give away |
|
|
1348 | |
1338 | $req[$_] += ($space - $req) * $children->[$_]{expand} / $expand |
1349 | $req[$_] += $space * $children->[$_]{expand} |
1339 | for 0 .. $#$children; |
1350 | for 0 .. $#$children; |
|
|
1351 | } |
1340 | } |
1352 | } |
1341 | |
1353 | |
1342 | CFClient::UI::harmonize \@req; |
1354 | CFClient::UI::harmonize \@req; |
1343 | |
1355 | |
1344 | my $pos = 0; |
1356 | my $pos = 0; |
… | |
… | |
1450 | $self->{text} = "T$text"; |
1462 | $self->{text} = "T$text"; |
1451 | |
1463 | |
1452 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1464 | $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba; |
1453 | $self->{layout}->set_text ($text); |
1465 | $self->{layout}->set_text ($text); |
1454 | |
1466 | |
|
|
1467 | $self->realloc; |
1455 | $self->update; |
1468 | $self->update; |
1456 | $self->check_size; |
|
|
1457 | } |
1469 | } |
1458 | |
1470 | |
1459 | sub set_markup { |
1471 | sub set_markup { |
1460 | my ($self, $markup) = @_; |
1472 | my ($self, $markup) = @_; |
1461 | |
1473 | |
… | |
… | |
1465 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1477 | my $rgba = $markup =~ /span.*(?:foreground|background)/; |
1466 | |
1478 | |
1467 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1479 | $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba; |
1468 | $self->{layout}->set_markup ($markup); |
1480 | $self->{layout}->set_markup ($markup); |
1469 | |
1481 | |
|
|
1482 | $self->realloc; |
1470 | $self->update; |
1483 | $self->update; |
1471 | $self->check_size; |
|
|
1472 | } |
1484 | } |
1473 | |
1485 | |
1474 | sub size_request { |
1486 | sub size_request { |
1475 | my ($self) = @_; |
1487 | my ($self) = @_; |
1476 | |
1488 | |
… | |
… | |
1497 | $h + $self->{padding} * 2, |
1509 | $h + $self->{padding} * 2, |
1498 | ) |
1510 | ) |
1499 | } |
1511 | } |
1500 | |
1512 | |
1501 | sub size_allocate { |
1513 | sub size_allocate { |
1502 | my ($self, $w, $h) = @_; |
1514 | my ($self, $w, $h, $changed) = @_; |
1503 | |
1515 | |
1504 | delete $self->{texture}; |
1516 | delete $self->{texture} |
|
|
1517 | if $changed; |
1505 | } |
1518 | } |
1506 | |
1519 | |
1507 | sub set_fontsize { |
1520 | sub set_fontsize { |
1508 | my ($self, $fontsize) = @_; |
1521 | my ($self, $fontsize) = @_; |
1509 | |
1522 | |
1510 | $self->{fontsize} = $fontsize; |
1523 | $self->{fontsize} = $fontsize; |
1511 | delete $self->{texture}; |
1524 | delete $self->{texture}; |
1512 | |
1525 | |
1513 | $self->update; |
1526 | $self->realloc; |
1514 | $self->check_size; |
|
|
1515 | } |
1527 | } |
1516 | |
1528 | |
1517 | sub _draw { |
1529 | sub _draw { |
1518 | my ($self) = @_; |
1530 | my ($self) = @_; |
1519 | |
1531 | |
… | |
… | |
1599 | sub set_text { |
1611 | sub set_text { |
1600 | my ($self, $text) = @_; |
1612 | my ($self, $text) = @_; |
1601 | |
1613 | |
1602 | $self->{cursor} = length $text; |
1614 | $self->{cursor} = length $text; |
1603 | $self->_set_text ($text); |
1615 | $self->_set_text ($text); |
1604 | $self->update; |
1616 | |
1605 | $self->check_size; |
1617 | $self->realloc; |
1606 | } |
1618 | } |
1607 | |
1619 | |
1608 | sub get_text { |
1620 | sub get_text { |
1609 | $_[0]{text} |
1621 | $_[0]{text} |
1610 | } |
1622 | } |
… | |
… | |
1643 | } elsif ($uni) { |
1655 | } elsif ($uni) { |
1644 | substr $text, $self->{cursor}++, 0, chr $uni; |
1656 | substr $text, $self->{cursor}++, 0, chr $uni; |
1645 | } |
1657 | } |
1646 | |
1658 | |
1647 | $self->_set_text ($text); |
1659 | $self->_set_text ($text); |
1648 | $self->update; |
1660 | |
1649 | $self->check_size; |
1661 | $self->realloc; |
1650 | } |
1662 | } |
1651 | |
1663 | |
1652 | sub focus_in { |
1664 | sub focus_in { |
1653 | my ($self) = @_; |
1665 | my ($self) = @_; |
1654 | |
1666 | |
… | |
… | |
2161 | $self->update; |
2173 | $self->update; |
2162 | |
2174 | |
2163 | $self |
2175 | $self |
2164 | } |
2176 | } |
2165 | |
2177 | |
|
|
2178 | sub changed { } |
|
|
2179 | |
2166 | sub set_range { |
2180 | sub set_range { |
2167 | my ($self, $range) = @_; |
2181 | my ($self, $range) = @_; |
2168 | |
2182 | |
2169 | ($range, $self->{range}) = ($self->{range}, $range); |
2183 | ($range, $self->{range}) = ($self->{range}, $range); |
2170 | |
2184 | |
… | |
… | |
2359 | $self->{fontsize} = $fontsize; |
2373 | $self->{fontsize} = $fontsize; |
2360 | $self->reflow; |
2374 | $self->reflow; |
2361 | } |
2375 | } |
2362 | |
2376 | |
2363 | sub size_allocate { |
2377 | sub size_allocate { |
2364 | my ($self, $w, $h) = @_; |
2378 | my ($self, $w, $h, $changed) = @_; |
2365 | |
2379 | |
2366 | $self->SUPER::size_allocate ($w, $h); |
2380 | $self->SUPER::size_allocate ($w, $h, $changed); |
|
|
2381 | |
|
|
2382 | return unless $changed; |
2367 | |
2383 | |
2368 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2384 | $self->{layout}->set_font ($self->{font}) if $self->{font}; |
2369 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2385 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
2370 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2386 | $self->{layout}->set_width ($self->{children}[0]{w}); |
2371 | |
2387 | |
… | |
… | |
2613 | |
2629 | |
2614 | ($w + 4, $h + 4) |
2630 | ($w + 4, $h + 4) |
2615 | } |
2631 | } |
2616 | |
2632 | |
2617 | sub size_allocate { |
2633 | sub size_allocate { |
2618 | my ($self, $w, $h) = @_; |
2634 | my ($self, $w, $h, $changed) = @_; |
2619 | |
2635 | |
|
|
2636 | return unless $changed; |
|
|
2637 | |
2620 | $self->SUPER::size_allocate ($w - 4, $h - 4); |
2638 | $self->SUPER::size_allocate ($w - 4, $h - 4, $changed); |
|
|
2639 | } |
|
|
2640 | |
|
|
2641 | sub visibility_change { |
|
|
2642 | my ($self, $visible) = @_; |
|
|
2643 | |
|
|
2644 | return unless $visible; |
|
|
2645 | |
|
|
2646 | $self->{root}->on_post_alloc ("move_$self" => sub { |
|
|
2647 | my $widget = $self->{owner} |
|
|
2648 | or return; |
|
|
2649 | |
|
|
2650 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
|
|
2651 | |
|
|
2652 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
|
|
2653 | if $x + $self->{w} > $::WIDTH; |
|
|
2654 | |
|
|
2655 | $self->move ($x, $y); |
|
|
2656 | }); |
2621 | } |
2657 | } |
2622 | |
2658 | |
2623 | sub _draw { |
2659 | sub _draw { |
2624 | my ($self) = @_; |
2660 | my ($self) = @_; |
2625 | |
2661 | |
… | |
… | |
2642 | glVertex $w, $h; |
2678 | glVertex $w, $h; |
2643 | glVertex $w, 0; |
2679 | glVertex $w, 0; |
2644 | glEnd; |
2680 | glEnd; |
2645 | |
2681 | |
2646 | glTranslate 2 - 0.375, 2 - 0.375; |
2682 | glTranslate 2 - 0.375, 2 - 0.375; |
|
|
2683 | |
2647 | $self->SUPER::_draw; |
2684 | $self->SUPER::_draw; |
2648 | } |
2685 | } |
2649 | |
2686 | |
2650 | ############################################################################# |
2687 | ############################################################################# |
2651 | |
2688 | |
… | |
… | |
2960 | use CFClient::OpenGL; |
2997 | use CFClient::OpenGL; |
2961 | |
2998 | |
2962 | sub new { |
2999 | sub new { |
2963 | my $class = shift; |
3000 | my $class = shift; |
2964 | |
3001 | |
2965 | $class->SUPER::new ( |
3002 | my $self = $class->SUPER::new ( |
2966 | visible => 1, |
3003 | visible => 1, |
2967 | @_, |
3004 | @_, |
2968 | ) |
3005 | ); |
|
|
3006 | |
|
|
3007 | Scalar::Util::weaken ($self->{root} = $self); |
|
|
3008 | |
|
|
3009 | $self |
2969 | } |
3010 | } |
2970 | |
3011 | |
2971 | sub configure { |
3012 | sub configure { |
2972 | my ($self, $x, $y, $w, $h) = @_; |
3013 | my ($self, $x, $y, $w, $h) = @_; |
2973 | |
3014 | |
2974 | $self->{w} = $w; |
3015 | $self->{w} = $w; |
2975 | $self->{h} = $h; |
3016 | $self->{h} = $h; |
2976 | } |
3017 | } |
2977 | |
3018 | |
2978 | sub check_size { |
3019 | sub reconfigure { |
2979 | my ($self) = @_; |
3020 | my ($self) = @_; |
2980 | |
3021 | |
|
|
3022 | $self->SUPER::reconfigure; |
|
|
3023 | |
2981 | $self->size_allocate ($self->{w}, $self->{h}) |
3024 | $self->size_allocate ($self->{w}, $self->{h}, 1) |
2982 | if $self->{w}; |
3025 | if $self->{w}; |
2983 | } |
3026 | } |
2984 | |
3027 | |
2985 | sub size_request { |
3028 | sub size_request { |
2986 | my ($self) = @_; |
3029 | my ($self) = @_; |
2987 | |
3030 | |
2988 | ($self->{w}, $self->{h}) |
3031 | ($self->{w}, $self->{h}) |
2989 | } |
3032 | } |
2990 | |
3033 | |
2991 | sub size_allocate { |
3034 | sub size_allocate { |
2992 | my ($self, $w, $h) = @_; |
3035 | my ($self, $w, $h, $changed) = @_; |
2993 | |
3036 | |
2994 | for my $child ($self->children) { |
3037 | for my $child ($self->children) { |
2995 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3038 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
2996 | |
3039 | |
2997 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3040 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
2998 | if exists $child->{req_x}; |
3041 | if exists $child->{req_x}; |
2999 | |
3042 | |
3000 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3043 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3001 | if exists $child->{req_y}; |
3044 | if exists $child->{req_y}; |
3002 | |
3045 | |
3003 | delete @$child{qw(req_x req_y)}; |
3046 | #delete @$child{qw(req_x req_y)};#d# def_x, def_y |
3004 | |
3047 | |
3005 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
3048 | $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; |
3049 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
3007 | |
3050 | |
3008 | $child->configure ($X, $Y, $W, $H); |
3051 | $child->configure ($X, $Y, $W, $H); |
… | |
… | |
3022 | } |
3065 | } |
3023 | |
3066 | |
3024 | sub update { |
3067 | sub update { |
3025 | my ($self) = @_; |
3068 | my ($self) = @_; |
3026 | |
3069 | |
3027 | $self->check_size; |
|
|
3028 | $::WANT_REFRESH++; |
3070 | $::WANT_REFRESH++; |
3029 | } |
3071 | } |
3030 | |
3072 | |
3031 | sub add { |
3073 | sub add { |
3032 | my ($self, @children) = @_; |
3074 | my ($self, @children) = @_; |
… | |
… | |
3038 | $child->{x} = int $child->{x}; |
3080 | $child->{x} = int $child->{x}; |
3039 | $child->{y} = int $child->{y}; |
3081 | $child->{y} = int $child->{y}; |
3040 | } |
3082 | } |
3041 | |
3083 | |
3042 | $self->SUPER::add (@children); |
3084 | $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 | } |
3085 | } |
3050 | |
3086 | |
3051 | sub remove { |
3087 | sub remove { |
3052 | my ($self, @children) = @_; |
3088 | my ($self, @children) = @_; |
3053 | |
3089 | |
… | |
… | |
3078 | while ($self->{refresh_hook}) { |
3114 | while ($self->{refresh_hook}) { |
3079 | $_->() |
3115 | $_->() |
3080 | for values %{delete $self->{refresh_hook}}; |
3116 | for values %{delete $self->{refresh_hook}}; |
3081 | } |
3117 | } |
3082 | |
3118 | |
3083 | if ($self->{check_size}) { |
3119 | if ($self->{realloc}) { |
3084 | my @queue; |
3120 | my @queue; |
3085 | |
3121 | |
3086 | for (;;) { |
3122 | while () { |
3087 | if ($self->{check_size}) { |
3123 | if ($self->{realloc}) { |
3088 | #TODO use array-of-depth approach |
3124 | #TODO use array-of-depth approach |
3089 | |
3125 | |
3090 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3126 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3091 | @queue, values %{delete $self->{check_size}}; |
3127 | @queue, values %{delete $self->{realloc}}; |
3092 | } |
3128 | } |
3093 | |
3129 | |
3094 | my $widget = pop @queue || last; |
3130 | my $widget = pop @queue || last; |
3095 | |
3131 | |
3096 | defined $widget->{visible} or last; # do not resize invisible widgets |
3132 | $widget->{visible} or last; # do not resize invisible widgets |
3097 | |
3133 | |
3098 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
3134 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
3099 | ? @$widget{qw(def_w def_h)} |
3135 | ? @$widget{qw(def_w def_h)} |
3100 | : $widget->size_request; |
3136 | : $widget->size_request; |
3101 | |
3137 | |
3102 | if (delete $widget->{force_alloc} |
|
|
3103 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
|
|
3104 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
3138 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
3105 | |
3139 | |
3106 | $widget->{req_w} = $w; |
3140 | $widget->{req_w} = $w; |
3107 | $widget->{req_h} = $h; |
3141 | $widget->{req_h} = $h; |
3108 | |
3142 | |
3109 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
3143 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
3110 | |
3144 | |
3111 | $widget->{parent}->check_size |
3145 | push @queue, $widget->{parent} |
3112 | if $widget->{parent}; |
3146 | if $widget->{parent}; |
3113 | } |
|
|
3114 | } |
3147 | } |
3115 | } |
3148 | } |
3116 | |
3149 | |
3117 | while ($self->{size_alloc}) { |
3150 | while (my $size_alloc = delete $self->{size_alloc}) { |
3118 | for (values %{delete $self->{size_alloc}}) { |
3151 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
3119 | my ($widget, $w, $h) = @$_; |
3152 | values %$size_alloc; |
|
|
3153 | |
|
|
3154 | while () { |
|
|
3155 | my ($widget, $w, $h) = @{ pop @queue or last }; |
3120 | |
3156 | |
3121 | $w = 0 if $w < 0; |
3157 | $w = 0 if $w < 0; |
3122 | $h = 0 if $h < 0; |
3158 | $h = 0 if $h < 0; |
3123 | |
3159 | |
|
|
3160 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
|
|
3161 | |
3124 | $widget->{w} = $w; |
3162 | $widget->{w} = $w; |
3125 | $widget->{h} = $h; |
3163 | $widget->{h} = $h; |
|
|
3164 | |
3126 | $widget->emit (size_allocate => $w, $h); |
3165 | $widget->emit (size_allocate => $w, $h, $changed); |
3127 | } |
3166 | } |
3128 | } |
3167 | } |
3129 | |
3168 | |
3130 | while ($self->{post_alloc_hook}) { |
3169 | while ($self->{post_alloc_hook}) { |
3131 | $_->() |
3170 | $_->() |