… | |
… | |
9 | use CFClient; |
9 | use CFClient; |
10 | use CFClient::Texture; |
10 | use CFClient::Texture; |
11 | |
11 | |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
12 | our ($FOCUS, $HOVER, $GRAB); # various widgets |
13 | |
13 | |
|
|
14 | our $LAYOUT; |
14 | our $ROOT; |
15 | our $ROOT; |
15 | our $TOOLTIP; |
16 | our $TOOLTIP; |
16 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
17 | |
18 | |
18 | 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 | } |
19 | |
38 | |
20 | sub check_tooltip { |
39 | sub check_tooltip { |
21 | if (!$GRAB) { |
40 | if (!$GRAB) { |
22 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
41 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
23 | if (length $widget->{tooltip}) { |
42 | if (length $widget->{tooltip}) { |
… | |
… | |
187 | can_events => 1, |
206 | can_events => 1, |
188 | @_ |
207 | @_ |
189 | }, $class; |
208 | }, $class; |
190 | |
209 | |
191 | for (keys %$self) { |
210 | for (keys %$self) { |
192 | if (/^connect_(.*)$/) { |
211 | if (/^on_(.*)$/) { |
193 | $self->connect ($1 => delete $self->{$_}); |
212 | $self->connect ($1 => delete $self->{$_}); |
194 | } |
213 | } |
195 | } |
214 | } |
196 | |
215 | |
197 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
216 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
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 | } |
198 | |
224 | |
199 | $self |
225 | $self |
200 | } |
226 | } |
201 | |
227 | |
202 | sub destroy { |
228 | sub destroy { |
… | |
… | |
227 | } |
253 | } |
228 | |
254 | |
229 | sub set_invisible { |
255 | sub set_invisible { |
230 | my ($self) = @_; |
256 | my ($self) = @_; |
231 | |
257 | |
|
|
258 | return unless $self->{visible}; |
|
|
259 | |
232 | # broken show/hide model |
260 | # broken show/hide model |
233 | |
261 | |
|
|
262 | delete $self->{root}; |
234 | delete $self->{visible}; |
263 | delete $self->{visible}; |
235 | |
264 | |
236 | undef $GRAB if $GRAB == $self; |
265 | undef $GRAB if $GRAB == $self; |
237 | undef $HOVER if $HOVER == $self; |
266 | undef $HOVER if $HOVER == $self; |
238 | |
267 | |
239 | CFClient::UI::check_tooltip |
268 | CFClient::UI::check_tooltip |
240 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
269 | if $CFClient::UI::TOOLTIP->{owner} == $self; |
241 | |
270 | |
242 | $self->focus_out; |
271 | $self->focus_out; |
|
|
272 | |
|
|
273 | $self->emit (visibility_change => 0); |
243 | } |
274 | } |
244 | |
275 | |
245 | sub hide { |
276 | sub hide { |
246 | my ($self) = @_; |
277 | my ($self) = @_; |
247 | |
278 | |
… | |
… | |
459 | sub set_parent { |
490 | sub set_parent { |
460 | my ($self, $parent) = @_; |
491 | my ($self, $parent) = @_; |
461 | |
492 | |
462 | Scalar::Util::weaken ($self->{parent} = $parent); |
493 | Scalar::Util::weaken ($self->{parent} = $parent); |
463 | |
494 | |
|
|
495 | if ($parent->{visible} || 1) { |
464 | $self->{root} = $parent->{root}; |
496 | $self->{root} = $parent->{root}; |
465 | $self->{visible} = $parent->{visible}; |
497 | $self->{visible} = $parent->{visible} + 1; |
|
|
498 | |
|
|
499 | $self->emit (visibility_change => 1) |
|
|
500 | unless $self->{parent}{visible}; |
|
|
501 | } |
466 | |
502 | |
467 | # TODO: req_w _does_change after ->reconfigure |
503 | # TODO: req_w _does_change after ->reconfigure |
468 | $self->check_size |
504 | $self->check_size |
469 | unless exists $self->{req_w}; |
505 | unless exists $self->{req_w}; |
470 | |
506 | |
… | |
… | |
500 | sub emit { |
536 | sub emit { |
501 | my ($self, $signal, @args) = @_; |
537 | my ($self, $signal, @args) = @_; |
502 | |
538 | |
503 | $self->_emit ($signal, @args) |
539 | $self->_emit ($signal, @args) |
504 | || $self->$signal (@args); |
540 | || $self->$signal (@args); |
|
|
541 | } |
|
|
542 | |
|
|
543 | sub visibility_change { |
|
|
544 | #my ($self, $visible) = @_; |
505 | } |
545 | } |
506 | |
546 | |
507 | sub DESTROY { |
547 | sub DESTROY { |
508 | my ($self) = @_; |
548 | my ($self) = @_; |
509 | |
549 | |
… | |
… | |
857 | my $class = shift; |
897 | my $class = shift; |
858 | |
898 | |
859 | my $self; |
899 | my $self; |
860 | |
900 | |
861 | my $slider = new CFClient::UI::Slider |
901 | my $slider = new CFClient::UI::Slider |
862 | vertical => 1, |
902 | vertical => 1, |
863 | range => [0, 0, 1, 0.01], # HACK fix |
903 | range => [0, 0, 1, 0.01], # HACK fix |
864 | connect_changed => sub { |
904 | on_changed => sub { |
865 | $self->{vp}->set_offset (0, $_[1]); |
905 | $self->{vp}->set_offset (0, $_[1]); |
866 | }, |
906 | }, |
867 | ; |
907 | ; |
868 | |
908 | |
869 | $self = $class->SUPER::new ( |
909 | $self = $class->SUPER::new ( |
… | |
… | |
1024 | my ($ev, $x, $y) = @_; |
1064 | my ($ev, $x, $y) = @_; |
1025 | |
1065 | |
1026 | my $dx = $ev->{x} - $ox; |
1066 | my $dx = $ev->{x} - $ox; |
1027 | my $dy = $ev->{y} - $oy; |
1067 | my $dy = $ev->{y} - $oy; |
1028 | |
1068 | |
|
|
1069 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1070 | $self->{user_y} = $wy + $dy * $my; |
1029 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1071 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1030 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1072 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1031 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
1073 | $self->move ($self->{user_x}, $self->{user_y}); |
1032 | $self->check_size; |
1074 | $self->check_size; |
1033 | }; |
1075 | }; |
1034 | |
1076 | |
1035 | } elsif ($lr ^ $td) { |
1077 | } elsif ($lr ^ $td) { |
1036 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1078 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
1039 | $self->{motion} = sub { |
1081 | $self->{motion} = sub { |
1040 | my ($ev, $x, $y) = @_; |
1082 | my ($ev, $x, $y) = @_; |
1041 | |
1083 | |
1042 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1084 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1043 | |
1085 | |
1044 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
1086 | $self->{user_x} = $bx + $x - $ox; |
|
|
1087 | $self->{user_y} = $by + $y - $oy; |
|
|
1088 | $self->move ($self->{user_x}, $self->{user_y}); |
1045 | $self->update; |
1089 | $self->update; |
1046 | }; |
1090 | }; |
1047 | } |
1091 | } |
1048 | } |
1092 | } |
1049 | |
1093 | |
… | |
… | |
2543 | |
2587 | |
2544 | sub new { |
2588 | sub new { |
2545 | my $class = shift; |
2589 | my $class = shift; |
2546 | |
2590 | |
2547 | my $self = $class->SUPER::new ( |
2591 | my $self = $class->SUPER::new ( |
2548 | state => 0, |
2592 | state => 0, |
2549 | connect_activate => \&toggle_flopper, |
2593 | on_activate => \&toggle_flopper, |
2550 | @_ |
2594 | @_ |
2551 | ); |
2595 | ); |
2552 | |
2596 | |
2553 | if ($self->{state}) { |
2597 | if ($self->{state}) { |
2554 | $self->{state} = 0; |
2598 | $self->{state} = 0; |
… | |
… | |
2964 | |
3008 | |
2965 | sub new { |
3009 | sub new { |
2966 | my $class = shift; |
3010 | my $class = shift; |
2967 | |
3011 | |
2968 | $class->SUPER::new ( |
3012 | $class->SUPER::new ( |
|
|
3013 | visible => 1, |
2969 | @_, |
3014 | @_, |
2970 | ) |
3015 | ) |
2971 | } |
3016 | } |
2972 | |
3017 | |
2973 | sub configure { |
3018 | sub configure { |
… | |
… | |
3000 | if exists $child->{req_x}; |
3045 | if exists $child->{req_x}; |
3001 | |
3046 | |
3002 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3047 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3003 | if exists $child->{req_y}; |
3048 | if exists $child->{req_y}; |
3004 | |
3049 | |
|
|
3050 | $X = $self->{user_x} if exists $self->{user_x}; |
|
|
3051 | $Y = $self->{user_y} if exists $self->{user_y}; |
|
|
3052 | |
3005 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
3053 | $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; |
3054 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
3007 | |
3055 | |
3008 | $child->configure ($X, $Y, $W, $H); |
3056 | $child->configure ($X, $Y, $W, $H); |
3009 | } |
3057 | } |
… | |
… | |
3031 | sub add { |
3079 | sub add { |
3032 | my ($self, @children) = @_; |
3080 | my ($self, @children) = @_; |
3033 | |
3081 | |
3034 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3082 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3035 | push @widgets, $w->children; |
3083 | push @widgets, $w->children; |
3036 | $w->{root} = $self; |
3084 | $w->{root} = $self; |
3037 | $w->{visible} = 1; |
3085 | $w->{visible} = $self->{visible} + 1; |
3038 | } |
3086 | } |
3039 | |
3087 | |
3040 | for my $child (@children) { |
3088 | for my $child (@children) { |
3041 | $child->{is_toplevel} = 1; |
3089 | $child->{is_toplevel} = 1; |
3042 | |
3090 | |
… | |
… | |
3079 | $_->() |
3127 | $_->() |
3080 | for values %{delete $self->{refresh_hook}}; |
3128 | for values %{delete $self->{refresh_hook}}; |
3081 | } |
3129 | } |
3082 | |
3130 | |
3083 | if ($self->{check_size}) { |
3131 | if ($self->{check_size}) { |
3084 | my @queue = ([], []); |
3132 | my @queue; |
3085 | |
3133 | |
3086 | for (;;) { |
3134 | for (;;) { |
3087 | if ($self->{check_size}) { |
3135 | if ($self->{check_size}) { |
3088 | # heuristic: check containers last |
3136 | #TODO use array-of-depth approach |
3089 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
3137 | |
|
|
3138 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3090 | for values %{delete $self->{check_size}} |
3139 | @queue, values %{delete $self->{check_size}}; |
3091 | } |
3140 | } |
3092 | |
3141 | |
3093 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
3142 | my $widget = pop @queue || last; |
|
|
3143 | |
|
|
3144 | defined $widget->{visible} or last; # do not resize invisible widgets |
3094 | |
3145 | |
3095 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3146 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3096 | ? @$widget{qw(user_w user_h)} |
3147 | ? @$widget{qw(user_w user_h)} |
3097 | : $widget->size_request; |
3148 | : $widget->size_request; |
3098 | |
3149 | |