… | |
… | |
5 | |
5 | |
6 | use Scalar::Util (); |
6 | use Scalar::Util (); |
7 | use List::Util (); |
7 | use List::Util (); |
8 | |
8 | |
9 | use CFClient; |
9 | use CFClient; |
|
|
10 | use CFClient::Texture; |
10 | |
11 | |
11 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | |
13 | |
|
|
14 | our $LAYOUT; |
13 | our $ROOT; |
15 | our $ROOT; |
14 | our $TOOLTIP; |
16 | our $TOOLTIP; |
15 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
16 | |
18 | |
17 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
|
|
20 | |
|
|
21 | sub get_layout { |
|
|
22 | for (grep { $_->{name} } values %WIDGET) { |
|
|
23 | $LAYOUT->{$_->{name}} = { |
|
|
24 | x => $_->{x} / $::WIDTH, |
|
|
25 | y => $_->{y} / $::HEIGHT, |
|
|
26 | w => $_->{w} / $::WIDTH, |
|
|
27 | h => $_->{h} / $::HEIGHT |
|
|
28 | }; |
|
|
29 | } |
|
|
30 | |
|
|
31 | return $LAYOUT; |
|
|
32 | } |
|
|
33 | |
|
|
34 | sub set_layout { |
|
|
35 | my ($layout) = @_; |
|
|
36 | $LAYOUT = $layout; |
|
|
37 | } |
18 | |
38 | |
19 | sub check_tooltip { |
39 | sub check_tooltip { |
20 | if (!$GRAB) { |
40 | if (!$GRAB) { |
21 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
22 | if (length $widget->{tooltip}) { |
42 | if (length $widget->{tooltip}) { |
… | |
… | |
186 | can_events => 1, |
206 | can_events => 1, |
187 | @_ |
207 | @_ |
188 | }, $class; |
208 | }, $class; |
189 | |
209 | |
190 | for (keys %$self) { |
210 | for (keys %$self) { |
191 | if (/^connect_(.*)$/) { |
211 | if (/^on_(.*)$/) { |
192 | $self->connect ($1 => delete $self->{$_}); |
212 | $self->connect ($1 => delete $self->{$_}); |
193 | } |
213 | } |
194 | } |
214 | } |
195 | |
215 | |
196 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
216 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
197 | |
217 | |
|
|
218 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
|
|
219 | $self->{user_x} = $layout->{x} * $::WIDTH; |
|
|
220 | $self->{user_y} = $layout->{y} * $::HEIGHT; |
|
|
221 | $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
|
|
222 | $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
|
|
223 | } |
|
|
224 | |
198 | $self |
225 | $self |
|
|
226 | } |
|
|
227 | |
|
|
228 | sub toggle_visibility { |
|
|
229 | my ($self) = @_; |
|
|
230 | |
|
|
231 | if ($self->{visible}) { |
|
|
232 | $self->hide; |
|
|
233 | } else { |
|
|
234 | $self->show; |
|
|
235 | } |
199 | } |
236 | } |
200 | |
237 | |
201 | sub destroy { |
238 | sub destroy { |
202 | my ($self) = @_; |
239 | my ($self) = @_; |
203 | |
240 | |
… | |
… | |
226 | } |
263 | } |
227 | |
264 | |
228 | sub set_invisible { |
265 | sub set_invisible { |
229 | my ($self) = @_; |
266 | my ($self) = @_; |
230 | |
267 | |
|
|
268 | return unless $self->{visible}; |
|
|
269 | |
231 | # broken show/hide model |
270 | # broken show/hide model |
232 | |
271 | |
|
|
272 | delete $self->{root}; |
233 | delete $self->{visible}; |
273 | delete $self->{visible}; |
234 | |
274 | |
235 | undef $GRAB if $GRAB == $self; |
275 | undef $GRAB if $GRAB == $self; |
236 | undef $HOVER if $HOVER == $self; |
276 | undef $HOVER if $HOVER == $self; |
237 | |
277 | |
238 | CFClient::UI::check_tooltip |
278 | CFClient::UI::check_tooltip |
239 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
279 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
240 | |
280 | |
241 | $self->focus_out; |
281 | $self->focus_out; |
|
|
282 | |
|
|
283 | $self->emit (visibility_change => 0); |
242 | } |
284 | } |
243 | |
285 | |
244 | sub hide { |
286 | sub hide { |
245 | my ($self) = @_; |
287 | my ($self) = @_; |
246 | |
288 | |
… | |
… | |
458 | sub set_parent { |
500 | sub set_parent { |
459 | my ($self, $parent) = @_; |
501 | my ($self, $parent) = @_; |
460 | |
502 | |
461 | Scalar::Util::weaken ($self->{parent} = $parent); |
503 | Scalar::Util::weaken ($self->{parent} = $parent); |
462 | |
504 | |
|
|
505 | if ($parent->{visible} || 1) { |
463 | $self->{root} = $parent->{root}; |
506 | $self->{root} = $parent->{root}; |
464 | $self->{visible} = $parent->{visible}; |
507 | $self->{visible} = $parent->{visible} + 1; |
|
|
508 | |
|
|
509 | $self->emit (visibility_change => 1) |
|
|
510 | unless $self->{parent}{visible}; |
|
|
511 | } |
465 | |
512 | |
466 | # TODO: req_w _does_change after ->reconfigure |
513 | # TODO: req_w _does_change after ->reconfigure |
467 | $self->check_size |
514 | $self->check_size |
468 | unless exists $self->{req_w}; |
515 | unless exists $self->{req_w}; |
469 | |
516 | |
… | |
… | |
499 | sub emit { |
546 | sub emit { |
500 | my ($self, $signal, @args) = @_; |
547 | my ($self, $signal, @args) = @_; |
501 | |
548 | |
502 | $self->_emit ($signal, @args) |
549 | $self->_emit ($signal, @args) |
503 | || $self->$signal (@args); |
550 | || $self->$signal (@args); |
|
|
551 | } |
|
|
552 | |
|
|
553 | sub visibility_change { |
|
|
554 | #my ($self, $visible) = @_; |
504 | } |
555 | } |
505 | |
556 | |
506 | sub DESTROY { |
557 | sub DESTROY { |
507 | my ($self) = @_; |
558 | my ($self) = @_; |
508 | |
559 | |
… | |
… | |
856 | my $class = shift; |
907 | my $class = shift; |
857 | |
908 | |
858 | my $self; |
909 | my $self; |
859 | |
910 | |
860 | my $slider = new CFClient::UI::Slider |
911 | my $slider = new CFClient::UI::Slider |
861 | vertical => 1, |
912 | vertical => 1, |
862 | range => [0, 0, 1, 0.01], # HACK fix |
913 | range => [0, 0, 1, 0.01], # HACK fix |
863 | connect_changed => sub { |
914 | on_changed => sub { |
864 | $self->{vp}->set_offset (0, $_[1]); |
915 | $self->{vp}->set_offset (0, $_[1]); |
865 | }, |
916 | }, |
866 | ; |
917 | ; |
867 | |
918 | |
868 | $self = $class->SUPER::new ( |
919 | $self = $class->SUPER::new ( |
… | |
… | |
874 | $self->{vp}->add ($self->{scrolled}); |
925 | $self->{vp}->add ($self->{scrolled}); |
875 | $self->add ($self->{vp}); |
926 | $self->add ($self->{vp}); |
876 | $self->add ($self->{slider}); |
927 | $self->add ($self->{slider}); |
877 | |
928 | |
878 | $self |
929 | $self |
|
|
930 | } |
|
|
931 | |
|
|
932 | sub update { |
|
|
933 | my ($self) = @_; |
|
|
934 | |
|
|
935 | $self->SUPER::update; |
|
|
936 | |
|
|
937 | # todo: overwrite size_allocate of child |
|
|
938 | my $child = $self->{vp}->child; |
|
|
939 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
879 | } |
940 | } |
880 | |
941 | |
881 | sub size_allocate { |
942 | sub size_allocate { |
882 | my ($self, $w, $h) = @_; |
943 | my ($self, $w, $h) = @_; |
883 | |
944 | |
… | |
… | |
1013 | my ($ev, $x, $y) = @_; |
1074 | my ($ev, $x, $y) = @_; |
1014 | |
1075 | |
1015 | my $dx = $ev->{x} - $ox; |
1076 | my $dx = $ev->{x} - $ox; |
1016 | my $dy = $ev->{y} - $oy; |
1077 | my $dy = $ev->{y} - $oy; |
1017 | |
1078 | |
|
|
1079 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1080 | $self->{user_y} = $wy + $dy * $my; |
1018 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1081 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1019 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1082 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1020 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
1083 | $self->move ($self->{user_x}, $self->{user_y}); |
1021 | $self->check_size; |
1084 | $self->check_size; |
1022 | }; |
1085 | }; |
1023 | |
1086 | |
1024 | } elsif ($lr ^ $td) { |
1087 | } elsif ($lr ^ $td) { |
1025 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1088 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
1028 | $self->{motion} = sub { |
1091 | $self->{motion} = sub { |
1029 | my ($ev, $x, $y) = @_; |
1092 | my ($ev, $x, $y) = @_; |
1030 | |
1093 | |
1031 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1094 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1032 | |
1095 | |
1033 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
1096 | $self->{user_x} = $bx + $x - $ox; |
|
|
1097 | $self->{user_y} = $by + $y - $oy; |
|
|
1098 | $self->move ($self->{user_x}, $self->{user_y}); |
1034 | $self->update; |
1099 | $self->update; |
1035 | }; |
1100 | }; |
1036 | } |
1101 | } |
1037 | } |
1102 | } |
1038 | |
1103 | |
… | |
… | |
2136 | } |
2201 | } |
2137 | |
2202 | |
2138 | sub set_range { |
2203 | sub set_range { |
2139 | my ($self, $range) = @_; |
2204 | my ($self, $range) = @_; |
2140 | |
2205 | |
2141 | $self->{range} = $range; |
2206 | ($range, $self->{range}) = ($self->{range}, $range); |
2142 | |
2207 | |
2143 | $self->update; |
2208 | $self->update |
|
|
2209 | if "@$range" ne "@{$self->{range}}"; |
2144 | } |
2210 | } |
2145 | |
2211 | |
2146 | sub set_value { |
2212 | sub set_value { |
2147 | my ($self, $value) = @_; |
2213 | my ($self, $value) = @_; |
2148 | |
2214 | |
… | |
… | |
2531 | |
2597 | |
2532 | sub new { |
2598 | sub new { |
2533 | my $class = shift; |
2599 | my $class = shift; |
2534 | |
2600 | |
2535 | my $self = $class->SUPER::new ( |
2601 | my $self = $class->SUPER::new ( |
2536 | state => 0, |
2602 | state => 0, |
2537 | connect_activate => \&toggle_flopper, |
2603 | on_activate => \&toggle_flopper, |
2538 | @_ |
2604 | @_ |
2539 | ); |
2605 | ); |
2540 | |
2606 | |
2541 | if ($self->{state}) { |
|
|
2542 | $self->{state} = 0; |
|
|
2543 | $self->toggle_flopper; |
|
|
2544 | } |
|
|
2545 | |
|
|
2546 | $self |
2607 | $self |
2547 | } |
2608 | } |
2548 | |
2609 | |
2549 | sub toggle_flopper { |
2610 | sub toggle_flopper { |
2550 | my ($self) = @_; |
2611 | my ($self) = @_; |
2551 | |
2612 | |
2552 | # TODO: use animation |
2613 | $self->{other}->toggle_visibility; |
2553 | if ($self->{state} = !$self->{state}) { |
|
|
2554 | $CFClient::UI::ROOT->add ($self->{other}); |
|
|
2555 | $self->{other}->move ($self->coord2global (0, $self->{h})); |
|
|
2556 | $self->_emit ("open"); |
|
|
2557 | } else { |
|
|
2558 | $CFClient::UI::ROOT->remove ($self->{other}); |
|
|
2559 | $self->_emit ("close"); |
|
|
2560 | } |
|
|
2561 | |
|
|
2562 | $self->_emit (changed => $self->{state}); |
|
|
2563 | } |
2614 | } |
2564 | |
2615 | |
2565 | ############################################################################# |
2616 | ############################################################################# |
2566 | |
2617 | |
2567 | package CFClient::UI::Tooltip; |
2618 | package CFClient::UI::Tooltip; |
… | |
… | |
2751 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2802 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2752 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2803 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2753 | |
2804 | |
2754 | $row++; |
2805 | $row++; |
2755 | } |
2806 | } |
2756 | |
|
|
2757 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2758 | } |
2807 | } |
2759 | |
2808 | |
2760 | sub size_request { |
2809 | sub size_request { |
2761 | my ($self) = @_; |
2810 | my ($self) = @_; |
2762 | ($self->{req_w}, $self->{req_h}); |
2811 | ($self->{req_w}, $self->{req_h}); |
… | |
… | |
2954 | |
3003 | |
2955 | sub new { |
3004 | sub new { |
2956 | my $class = shift; |
3005 | my $class = shift; |
2957 | |
3006 | |
2958 | $class->SUPER::new ( |
3007 | $class->SUPER::new ( |
|
|
3008 | visible => 1, |
2959 | @_, |
3009 | @_, |
2960 | ) |
3010 | ) |
2961 | } |
3011 | } |
2962 | |
3012 | |
2963 | sub configure { |
3013 | sub configure { |
… | |
… | |
2990 | if exists $child->{req_x}; |
3040 | if exists $child->{req_x}; |
2991 | |
3041 | |
2992 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3042 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
2993 | if exists $child->{req_y}; |
3043 | if exists $child->{req_y}; |
2994 | |
3044 | |
|
|
3045 | $X = $self->{user_x} if exists $self->{user_x}; |
|
|
3046 | $Y = $self->{user_y} if exists $self->{user_y}; |
|
|
3047 | |
2995 | $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; |
2996 | $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; |
2997 | |
3050 | |
2998 | $child->configure ($X, $Y, $W, $H); |
3051 | $child->configure ($X, $Y, $W, $H); |
2999 | } |
3052 | } |
… | |
… | |
3021 | sub add { |
3074 | sub add { |
3022 | my ($self, @children) = @_; |
3075 | my ($self, @children) = @_; |
3023 | |
3076 | |
3024 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3077 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3025 | push @widgets, $w->children; |
3078 | push @widgets, $w->children; |
3026 | $w->{root} = $self; |
3079 | $w->{root} = $self; |
3027 | $w->{visible} = 1; |
3080 | $w->{visible} = $self->{visible} + 1; |
3028 | } |
3081 | } |
3029 | |
3082 | |
3030 | for my $child (@children) { |
3083 | for my $child (@children) { |
3031 | $child->{is_toplevel} = 1; |
3084 | $child->{is_toplevel} = 1; |
3032 | |
3085 | |
… | |
… | |
3069 | $_->() |
3122 | $_->() |
3070 | for values %{delete $self->{refresh_hook}}; |
3123 | for values %{delete $self->{refresh_hook}}; |
3071 | } |
3124 | } |
3072 | |
3125 | |
3073 | if ($self->{check_size}) { |
3126 | if ($self->{check_size}) { |
3074 | my @queue = ([], []); |
3127 | my @queue; |
3075 | |
3128 | |
3076 | for (;;) { |
3129 | for (;;) { |
3077 | if ($self->{check_size}) { |
3130 | if ($self->{check_size}) { |
3078 | # heuristic: check containers last |
3131 | #TODO use array-of-depth approach |
3079 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
3132 | |
|
|
3133 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3080 | for values %{delete $self->{check_size}} |
3134 | @queue, values %{delete $self->{check_size}}; |
3081 | } |
3135 | } |
3082 | |
3136 | |
3083 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
3137 | my $widget = pop @queue || last; |
|
|
3138 | |
|
|
3139 | defined $widget->{visible} or last; # do not resize invisible widgets |
3084 | |
3140 | |
3085 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3141 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3086 | ? @$widget{qw(user_w user_h)} |
3142 | ? @$widget{qw(user_w user_h)} |
3087 | : $widget->size_request; |
3143 | : $widget->size_request; |
3088 | |
3144 | |