… | |
… | |
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); |
|
|
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 | } |
197 | |
224 | |
198 | $self |
225 | $self |
199 | } |
226 | } |
200 | |
227 | |
201 | sub destroy { |
228 | sub destroy { |
… | |
… | |
228 | sub set_invisible { |
255 | sub set_invisible { |
229 | my ($self) = @_; |
256 | my ($self) = @_; |
230 | |
257 | |
231 | # broken show/hide model |
258 | # broken show/hide model |
232 | |
259 | |
|
|
260 | delete $self->{root}; |
233 | delete $self->{visible}; |
261 | delete $self->{visible}; |
234 | |
262 | |
235 | undef $GRAB if $GRAB == $self; |
263 | undef $GRAB if $GRAB == $self; |
236 | undef $HOVER if $HOVER == $self; |
264 | undef $HOVER if $HOVER == $self; |
237 | |
265 | |
… | |
… | |
458 | sub set_parent { |
486 | sub set_parent { |
459 | my ($self, $parent) = @_; |
487 | my ($self, $parent) = @_; |
460 | |
488 | |
461 | Scalar::Util::weaken ($self->{parent} = $parent); |
489 | Scalar::Util::weaken ($self->{parent} = $parent); |
462 | |
490 | |
|
|
491 | $self->{root} = $parent->{root}; |
|
|
492 | $self->{visible} = $parent->{visible} + 1; |
|
|
493 | |
463 | # TODO: req_w _does_change after ->reconfigure |
494 | # TODO: req_w _does_change after ->reconfigure |
464 | $self->check_size |
495 | $self->check_size |
465 | unless exists $self->{req_w}; |
496 | unless exists $self->{req_w}; |
466 | |
497 | |
467 | $self->show; |
498 | $self->show; |
… | |
… | |
618 | delete $child->{parent}; |
649 | delete $child->{parent}; |
619 | $child->hide; |
650 | $child->hide; |
620 | |
651 | |
621 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
652 | $self->{children} = [ grep $_ != $child, @{ $self->{children} } ]; |
622 | |
653 | |
623 | $self->check_size; |
654 | $self->check_size (1); |
624 | $self->update; |
655 | $self->update; |
625 | } |
656 | } |
626 | |
657 | |
627 | sub clear { |
658 | sub clear { |
628 | my ($self) = @_; |
659 | my ($self) = @_; |
… | |
… | |
853 | my $class = shift; |
884 | my $class = shift; |
854 | |
885 | |
855 | my $self; |
886 | my $self; |
856 | |
887 | |
857 | my $slider = new CFClient::UI::Slider |
888 | my $slider = new CFClient::UI::Slider |
858 | vertical => 1, |
889 | vertical => 1, |
859 | range => [0, 0, 1, 0.01], # HACK fix |
890 | range => [0, 0, 1, 0.01], # HACK fix |
860 | connect_changed => sub { |
891 | on_changed => sub { |
861 | $self->{vp}->set_offset (0, $_[1]); |
892 | $self->{vp}->set_offset (0, $_[1]); |
862 | }, |
893 | }, |
863 | ; |
894 | ; |
864 | |
895 | |
865 | $self = $class->SUPER::new ( |
896 | $self = $class->SUPER::new ( |
… | |
… | |
871 | $self->{vp}->add ($self->{scrolled}); |
902 | $self->{vp}->add ($self->{scrolled}); |
872 | $self->add ($self->{vp}); |
903 | $self->add ($self->{vp}); |
873 | $self->add ($self->{slider}); |
904 | $self->add ($self->{slider}); |
874 | |
905 | |
875 | $self |
906 | $self |
|
|
907 | } |
|
|
908 | |
|
|
909 | sub update { |
|
|
910 | my ($self) = @_; |
|
|
911 | |
|
|
912 | $self->SUPER::update; |
|
|
913 | |
|
|
914 | # todo: overwrite size_allocate of child |
|
|
915 | my $child = $self->{vp}->child; |
|
|
916 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]); |
876 | } |
917 | } |
877 | |
918 | |
878 | sub size_allocate { |
919 | sub size_allocate { |
879 | my ($self, $w, $h) = @_; |
920 | my ($self, $w, $h) = @_; |
880 | |
921 | |
… | |
… | |
1010 | my ($ev, $x, $y) = @_; |
1051 | my ($ev, $x, $y) = @_; |
1011 | |
1052 | |
1012 | my $dx = $ev->{x} - $ox; |
1053 | my $dx = $ev->{x} - $ox; |
1013 | my $dy = $ev->{y} - $oy; |
1054 | my $dy = $ev->{y} - $oy; |
1014 | |
1055 | |
|
|
1056 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1057 | $self->{user_y} = $wy + $dy * $my; |
1015 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1058 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1016 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1059 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1017 | $self->move ($wx + $dx * $mx, $wy + $dy * $my); |
1060 | $self->move ($self->{user_x}, $self->{user_y}); |
1018 | $self->check_size; |
1061 | $self->check_size; |
1019 | }; |
1062 | }; |
1020 | |
1063 | |
1021 | } elsif ($lr ^ $td) { |
1064 | } elsif ($lr ^ $td) { |
1022 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1065 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
… | |
… | |
1025 | $self->{motion} = sub { |
1068 | $self->{motion} = sub { |
1026 | my ($ev, $x, $y) = @_; |
1069 | my ($ev, $x, $y) = @_; |
1027 | |
1070 | |
1028 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1071 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1029 | |
1072 | |
1030 | $self->move ($bx + $x - $ox, $by + $y - $oy); |
1073 | $self->{user_x} = $bx + $x - $ox; |
|
|
1074 | $self->{user_y} = $by + $y - $oy; |
|
|
1075 | $self->move ($self->{user_x}, $self->{user_y}); |
1031 | $self->update; |
1076 | $self->update; |
1032 | }; |
1077 | }; |
1033 | } |
1078 | } |
1034 | } |
1079 | } |
1035 | |
1080 | |
… | |
… | |
1101 | col_expand => [], |
1146 | col_expand => [], |
1102 | @_, |
1147 | @_, |
1103 | ) |
1148 | ) |
1104 | } |
1149 | } |
1105 | |
1150 | |
|
|
1151 | sub children { |
|
|
1152 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1153 | } |
|
|
1154 | |
1106 | sub add { |
1155 | sub add { |
1107 | my ($self, $x, $y, $child) = @_; |
1156 | my ($self, $x, $y, $child) = @_; |
1108 | |
1157 | |
1109 | $child->set_parent ($self); |
1158 | $child->set_parent ($self); |
1110 | $self->{children}[$y][$x] = $child; |
1159 | $self->{children}[$y][$x] = $child; |
1111 | |
1160 | |
1112 | $child->check_size; |
1161 | $self->check_size (1); |
1113 | } |
1162 | } |
1114 | |
1163 | |
1115 | sub children { |
|
|
1116 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1117 | } |
|
|
1118 | |
|
|
1119 | # TODO: move to container class maybe? send childs a signal on removal? |
1164 | # TODO: move to container class maybe? send children a signal on removal? |
1120 | sub clear { |
1165 | sub clear { |
1121 | my ($self) = @_; |
1166 | my ($self) = @_; |
1122 | |
1167 | |
1123 | my @children = $self->children; |
1168 | my @children = $self->children; |
1124 | delete $self->{children}; |
1169 | delete $self->{children}; |
… | |
… | |
1126 | for (@children) { |
1171 | for (@children) { |
1127 | delete $_->{parent}; |
1172 | delete $_->{parent}; |
1128 | $_->hide; |
1173 | $_->hide; |
1129 | } |
1174 | } |
1130 | |
1175 | |
|
|
1176 | $self->check_size (1); |
1131 | $self->update; |
1177 | $self->update; |
1132 | } |
1178 | } |
1133 | |
1179 | |
1134 | sub get_wh { |
1180 | sub get_wh { |
1135 | my ($self) = @_; |
1181 | my ($self) = @_; |
… | |
… | |
1167 | sub size_allocate { |
1213 | sub size_allocate { |
1168 | my ($self, $w, $h) = @_; |
1214 | my ($self, $w, $h) = @_; |
1169 | |
1215 | |
1170 | my ($ws, $hs) = $self->get_wh; |
1216 | my ($ws, $hs) = $self->get_wh; |
1171 | |
1217 | |
1172 | my $req_w = sum @$ws; |
1218 | my $req_w = (sum @$ws) || 1; |
1173 | my $req_h = sum @$hs; |
1219 | my $req_h = (sum @$hs) || 1; |
1174 | |
1220 | |
1175 | # TODO: nicer code && do row_expand |
1221 | # TODO: nicer code && do row_expand |
1176 | my @col_expand = @{$self->{col_expand}}; |
1222 | my @col_expand = @{$self->{col_expand}}; |
1177 | @col_expand = (1) x @$ws unless @col_expand; |
1223 | @col_expand = (1) x @$ws unless @col_expand; |
1178 | my $col_expand = (sum @col_expand) || 1; |
1224 | my $col_expand = (sum @col_expand) || 1; |
… | |
… | |
2132 | } |
2178 | } |
2133 | |
2179 | |
2134 | sub set_range { |
2180 | sub set_range { |
2135 | my ($self, $range) = @_; |
2181 | my ($self, $range) = @_; |
2136 | |
2182 | |
2137 | $self->{range} = $range; |
2183 | ($range, $self->{range}) = ($self->{range}, $range); |
2138 | |
2184 | |
2139 | $self->update; |
2185 | $self->update |
|
|
2186 | if "@$range" ne "@{$self->{range}}"; |
2140 | } |
2187 | } |
2141 | |
2188 | |
2142 | sub set_value { |
2189 | sub set_value { |
2143 | my ($self, $value) = @_; |
2190 | my ($self, $value) = @_; |
2144 | |
2191 | |
… | |
… | |
2527 | |
2574 | |
2528 | sub new { |
2575 | sub new { |
2529 | my $class = shift; |
2576 | my $class = shift; |
2530 | |
2577 | |
2531 | my $self = $class->SUPER::new ( |
2578 | my $self = $class->SUPER::new ( |
2532 | state => 0, |
2579 | state => 0, |
2533 | connect_activate => \&toggle_flopper, |
2580 | on_activate => \&toggle_flopper, |
2534 | @_ |
2581 | @_ |
2535 | ); |
2582 | ); |
2536 | |
2583 | |
2537 | if ($self->{state}) { |
2584 | if ($self->{state}) { |
2538 | $self->{state} = 0; |
2585 | $self->{state} = 0; |
… | |
… | |
2747 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2794 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2748 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2795 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2749 | |
2796 | |
2750 | $row++; |
2797 | $row++; |
2751 | } |
2798 | } |
2752 | |
|
|
2753 | # $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; |
|
|
2754 | } |
2799 | } |
2755 | |
2800 | |
2756 | sub size_request { |
2801 | sub size_request { |
2757 | my ($self) = @_; |
2802 | my ($self) = @_; |
2758 | ($self->{req_w}, $self->{req_h}); |
2803 | ($self->{req_w}, $self->{req_h}); |
… | |
… | |
2950 | |
2995 | |
2951 | sub new { |
2996 | sub new { |
2952 | my $class = shift; |
2997 | my $class = shift; |
2953 | |
2998 | |
2954 | $class->SUPER::new ( |
2999 | $class->SUPER::new ( |
|
|
3000 | visible => 1, |
2955 | @_, |
3001 | @_, |
2956 | ) |
3002 | ) |
2957 | } |
3003 | } |
2958 | |
3004 | |
2959 | sub configure { |
3005 | sub configure { |
… | |
… | |
3017 | sub add { |
3063 | sub add { |
3018 | my ($self, @children) = @_; |
3064 | my ($self, @children) = @_; |
3019 | |
3065 | |
3020 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3066 | for (my @widgets = @children; my $w = pop @widgets; ) { |
3021 | push @widgets, $w->children; |
3067 | push @widgets, $w->children; |
3022 | $w->{root} = $self; |
3068 | $w->{root} = $self; |
3023 | $w->{visible} = 1; |
3069 | $w->{visible} = $self->{visible} + 1; |
3024 | } |
3070 | } |
3025 | |
3071 | |
3026 | for my $child (@children) { |
3072 | for my $child (@children) { |
3027 | $child->{is_toplevel} = 1; |
3073 | $child->{is_toplevel} = 1; |
3028 | |
3074 | |
… | |
… | |
3065 | $_->() |
3111 | $_->() |
3066 | for values %{delete $self->{refresh_hook}}; |
3112 | for values %{delete $self->{refresh_hook}}; |
3067 | } |
3113 | } |
3068 | |
3114 | |
3069 | if ($self->{check_size}) { |
3115 | if ($self->{check_size}) { |
3070 | my @queue = ([], []); |
3116 | my @queue; |
3071 | |
3117 | |
3072 | for (;;) { |
3118 | for (;;) { |
3073 | if ($self->{check_size}) { |
3119 | if ($self->{check_size}) { |
3074 | # heuristic: check containers last |
3120 | #TODO use array-of-depth approach |
3075 | push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_ |
3121 | |
|
|
3122 | @queue = sort { $a->{visible} <=> $b->{visible} } |
3076 | for values %{delete $self->{check_size}} |
3123 | @queue, values %{delete $self->{check_size}}; |
3077 | } |
3124 | } |
3078 | |
3125 | |
3079 | my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last; |
3126 | my $widget = pop @queue || last; |
|
|
3127 | |
|
|
3128 | defined $widget->{visible} or last; # do not resize invisible widgets |
3080 | |
3129 | |
3081 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3130 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3082 | ? @$widget{qw(user_w user_h)} |
3131 | ? @$widget{qw(user_w user_h)} |
3083 | : $widget->size_request; |
3132 | : $widget->size_request; |
3084 | |
3133 | |