… | |
… | |
175 | for my $widget (values %WIDGET) { |
175 | for my $widget (values %WIDGET) { |
176 | if ($widget->{is_toplevel}) { |
176 | if ($widget->{is_toplevel}) { |
177 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
177 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
178 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
178 | $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}; |
179 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
180 | $widget->{user_w} = int 0.5 + $widget->{user_w} * $sx if exists $widget->{user_w}; |
|
|
181 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
180 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
182 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
181 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
183 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
182 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
184 | $widget->{user_h} = int 0.5 + $widget->{user_h} * $sy if exists $widget->{user_h}; |
|
|
185 | } |
183 | } |
186 | } |
184 | } |
187 | |
185 | |
188 | reconfigure_widgets; |
186 | reconfigure_widgets; |
189 | } |
187 | } |
… | |
… | |
214 | } |
212 | } |
215 | |
213 | |
216 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
214 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
217 | |
215 | |
218 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
216 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
219 | $self->{user_x} = $layout->{x} * $::WIDTH; |
217 | $self->{req_x} = $layout->{x} * $::WIDTH; |
220 | $self->{user_y} = $layout->{y} * $::HEIGHT; |
218 | $self->{req_y} = $layout->{y} * $::HEIGHT; |
221 | $self->{user_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
219 | $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
222 | $self->{user_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
220 | $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
223 | } |
221 | } |
224 | |
222 | |
225 | $self |
223 | $self |
226 | } |
224 | } |
227 | |
225 | |
… | |
… | |
260 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
258 | $self->move (($::WIDTH - $self->{w}) * 0.5, ($::HEIGHT - $self->{h}) * 0.5); |
261 | }, |
259 | }, |
262 | ); |
260 | ); |
263 | } |
261 | } |
264 | |
262 | |
|
|
263 | sub set_visible { |
|
|
264 | my ($self) = @_; |
|
|
265 | |
|
|
266 | return if $self->{visible}; |
|
|
267 | |
|
|
268 | $self->{root} = $self->{parent}{root}; |
|
|
269 | $self->{visible} = $self->{parent}{visible} + 1; |
|
|
270 | |
|
|
271 | $self->emit (visibility_change => 1) |
|
|
272 | } |
|
|
273 | |
265 | sub set_invisible { |
274 | sub set_invisible { |
266 | my ($self) = @_; |
275 | my ($self) = @_; |
267 | |
276 | |
268 | return unless $self->{visible}; |
277 | return unless $self->{visible}; |
269 | |
278 | |
… | |
… | |
303 | } |
312 | } |
304 | |
313 | |
305 | sub set_size { |
314 | sub set_size { |
306 | my ($self, $w, $h) = @_; |
315 | my ($self, $w, $h) = @_; |
307 | |
316 | |
308 | $self->{user_w} = $w; |
317 | $self->{def_w} = $w; |
309 | $self->{user_h} = $h; |
318 | $self->{def_h} = $h; |
310 | |
319 | |
311 | $self->check_size; |
320 | $self->check_size; |
312 | } |
321 | } |
313 | |
322 | |
314 | sub size_request { |
323 | sub size_request { |
… | |
… | |
500 | sub set_parent { |
509 | sub set_parent { |
501 | my ($self, $parent) = @_; |
510 | my ($self, $parent) = @_; |
502 | |
511 | |
503 | Scalar::Util::weaken ($self->{parent} = $parent); |
512 | Scalar::Util::weaken ($self->{parent} = $parent); |
504 | |
513 | |
505 | if ($parent->{visible} || 1) { |
514 | $self->set_visible; |
506 | $self->{root} = $parent->{root}; |
|
|
507 | $self->{visible} = $parent->{visible} + 1; |
|
|
508 | |
515 | |
509 | $self->emit (visibility_change => 1) |
|
|
510 | unless $self->{parent}{visible}; |
|
|
511 | } |
|
|
512 | |
|
|
513 | # TODO: req_w _does_change after ->reconfigure |
|
|
514 | $self->check_size |
516 | $self->check_size; |
515 | unless exists $self->{req_w}; |
|
|
516 | |
|
|
517 | $self->show; |
517 | $self->show; |
518 | } |
518 | } |
519 | |
519 | |
520 | sub check_size { |
520 | sub check_size { |
521 | my ($self, $forced) = @_; |
521 | my ($self, $forced) = @_; |
… | |
… | |
1003 | map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 } |
1003 | 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); |
1004 | qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png); |
1005 | |
1005 | |
1006 | sub new { |
1006 | sub new { |
1007 | my $class = shift; |
1007 | my $class = shift; |
1008 | |
|
|
1009 | # TODO: user_x, user_y, overwrite moveto? |
|
|
1010 | |
1008 | |
1011 | my $self = $class->SUPER::new ( |
1009 | my $self = $class->SUPER::new ( |
1012 | bg => [1, 1, 1, 1], |
1010 | bg => [1, 1, 1, 1], |
1013 | border_bg => [1, 1, 1, 1], |
1011 | border_bg => [1, 1, 1, 1], |
1014 | border => 0.6, |
1012 | border => 0.6, |
… | |
… | |
1076 | my $dx = $ev->{x} - $ox; |
1074 | my $dx = $ev->{x} - $ox; |
1077 | my $dy = $ev->{y} - $oy; |
1075 | my $dy = $ev->{y} - $oy; |
1078 | |
1076 | |
1079 | $self->{user_x} = $wx + $dx * $mx; |
1077 | $self->{user_x} = $wx + $dx * $mx; |
1080 | $self->{user_y} = $wy + $dy * $my; |
1078 | $self->{user_y} = $wy + $dy * $my; |
1081 | $self->{user_w} = $bw + $dx * ($mx ? -1 : 1); |
1079 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1082 | $self->{user_h} = $bh + $dy * ($my ? -1 : 1); |
1080 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1083 | $self->move ($self->{user_x}, $self->{user_y}); |
1081 | $self->move ($self->{user_x}, $self->{user_y}); |
1084 | $self->check_size; |
1082 | $self->check_size; |
1085 | }; |
1083 | }; |
1086 | |
1084 | |
1087 | } elsif ($lr ^ $td) { |
1085 | } elsif ($lr ^ $td) { |
… | |
… | |
1093 | |
1091 | |
1094 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1092 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1095 | |
1093 | |
1096 | $self->{user_x} = $bx + $x - $ox; |
1094 | $self->{user_x} = $bx + $x - $ox; |
1097 | $self->{user_y} = $by + $y - $oy; |
1095 | $self->{user_y} = $by + $y - $oy; |
|
|
1096 | |
1098 | $self->move ($self->{user_x}, $self->{user_y}); |
1097 | $self->move ($self->{user_x}, $self->{user_y}); |
1099 | $self->update; |
1098 | $self->update; |
1100 | }; |
1099 | }; |
1101 | } |
1100 | } |
1102 | } |
1101 | } |
… | |
… | |
1301 | } |
1300 | } |
1302 | } |
1301 | } |
1303 | |
1302 | |
1304 | ############################################################################# |
1303 | ############################################################################# |
1305 | |
1304 | |
1306 | package CFClient::UI::HBox; |
1305 | package CFClient::UI::Box; |
1307 | |
|
|
1308 | # TODO: wrap into common Box base class |
|
|
1309 | |
1306 | |
1310 | our @ISA = CFClient::UI::Container::; |
1307 | our @ISA = CFClient::UI::Container::; |
1311 | |
1308 | |
1312 | sub size_request { |
1309 | sub size_request { |
1313 | my ($self) = @_; |
1310 | my ($self) = @_; |
1314 | |
1311 | |
1315 | my @alloc = map [$_->size_request], @{$self->{children}}; |
1312 | $self->{vertical} |
1316 | |
1313 | ? ( |
1317 | ( |
1314 | (List::Util::max map $_->{req_w}, @{$self->{children}}), |
1318 | (List::Util::sum map $_->[0], @alloc), |
1315 | (List::Util::sum map $_->{req_h}, @{$self->{children}}), |
1319 | (List::Util::max map $_->[1], @alloc), |
1316 | ) |
1320 | ) |
1317 | : ( |
|
|
1318 | (List::Util::sum map $_->{req_w}, @{$self->{children}}), |
|
|
1319 | (List::Util::max map $_->{req_h}, @{$self->{children}}), |
|
|
1320 | ) |
1321 | } |
1321 | } |
1322 | |
1322 | |
1323 | sub size_allocate { |
1323 | sub size_allocate { |
1324 | my ($self, $w, $h) = @_; |
1324 | my ($self, $w, $h) = @_; |
1325 | |
1325 | |
1326 | ($h, $w) = ($w, $h); |
1326 | my $space = $self->{vertical} ? $h : $w; |
1327 | |
|
|
1328 | my $children = $self->{children}; |
1327 | my $children = $self->{children}; |
1329 | |
1328 | |
1330 | my @h = map $_->{req_w}, @$children; |
1329 | my @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; |
1331 | |
|
|
1332 | my $req_h = List::Util::sum @h; |
1330 | my $req = List::Util::sum @req; |
1333 | |
1331 | |
1334 | if ($req_h > $h) { |
1332 | if ($req > $space) { |
1335 | # ah well, not enough space |
1333 | # ah well, not enough space |
1336 | $_ *= $h / $req_h for @h; |
1334 | $_ *= $space / $req for @req; |
1337 | } else { |
1335 | } else { |
1338 | my $exp = List::Util::sum map $_->{expand}, @$children; |
1336 | my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; |
1339 | $exp ||= 1; |
|
|
1340 | |
1337 | |
|
|
1338 | $req[$_] += ($space - $req) * $children->[$_]{expand} / $expand |
1341 | for (0 .. $#$children) { |
1339 | for 0 .. $#$children; |
1342 | my $child = $children->[$_]; |
|
|
1343 | |
|
|
1344 | my $alloc_h = $h[$_]; |
|
|
1345 | $alloc_h += ($h - $req_h) * $child->{expand} / $exp; |
|
|
1346 | $h[$_] = $alloc_h; |
|
|
1347 | } |
|
|
1348 | } |
1340 | } |
1349 | |
1341 | |
1350 | CFClient::UI::harmonize \@h; |
1342 | CFClient::UI::harmonize \@req; |
1351 | |
1343 | |
1352 | my $y = 0; |
1344 | my $pos = 0; |
1353 | for (0 .. $#$children) { |
1345 | for (0 .. $#$children) { |
1354 | my $child = $children->[$_]; |
|
|
1355 | my $h = $h[$_]; |
1346 | my $alloc = $req[$_]; |
1356 | $child->configure ($y, 0, $h, $w); |
1347 | $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); |
1357 | |
1348 | |
1358 | $y += $h; |
1349 | $pos += $alloc; |
1359 | } |
1350 | } |
1360 | |
1351 | |
1361 | 1 |
1352 | 1 |
1362 | } |
1353 | } |
1363 | |
1354 | |
1364 | ############################################################################# |
1355 | ############################################################################# |
1365 | |
1356 | |
|
|
1357 | package CFClient::UI::HBox; |
|
|
1358 | |
|
|
1359 | our @ISA = CFClient::UI::Box::; |
|
|
1360 | |
|
|
1361 | sub new { |
|
|
1362 | my $class = shift; |
|
|
1363 | |
|
|
1364 | $class->SUPER::new ( |
|
|
1365 | vertical => 0, |
|
|
1366 | @_, |
|
|
1367 | ) |
|
|
1368 | } |
|
|
1369 | |
|
|
1370 | ############################################################################# |
|
|
1371 | |
1366 | package CFClient::UI::VBox; |
1372 | package CFClient::UI::VBox; |
1367 | |
1373 | |
1368 | # TODO: wrap into common Box base class |
|
|
1369 | |
|
|
1370 | our @ISA = CFClient::UI::Container::; |
1374 | our @ISA = CFClient::UI::Box::; |
1371 | |
1375 | |
1372 | sub size_request { |
1376 | sub new { |
1373 | my ($self) = @_; |
1377 | my $class = shift; |
1374 | |
1378 | |
1375 | my @alloc = map [$_->size_request], @{$self->{children}}; |
1379 | $class->SUPER::new ( |
1376 | |
1380 | vertical => 1, |
1377 | ( |
1381 | @_, |
1378 | (List::Util::max map $_->[0], @alloc), |
|
|
1379 | (List::Util::sum map $_->[1], @alloc), |
|
|
1380 | ) |
1382 | ) |
1381 | } |
|
|
1382 | |
|
|
1383 | sub size_allocate { |
|
|
1384 | my ($self, $w, $h) = @_; |
|
|
1385 | |
|
|
1386 | Carp::confess "negative size" if $w < 0 || $h < 0;#d# |
|
|
1387 | |
|
|
1388 | my $children = $self->{children}; |
|
|
1389 | |
|
|
1390 | my @h = map $_->{req_h}, @$children; |
|
|
1391 | |
|
|
1392 | my $req_h = List::Util::sum @h; |
|
|
1393 | |
|
|
1394 | if ($req_h > $h) { |
|
|
1395 | # ah well, not enough space |
|
|
1396 | $_ *= $h / $req_h for @h; |
|
|
1397 | } else { |
|
|
1398 | my $exp = List::Util::sum map $_->{expand}, @$children; |
|
|
1399 | $exp ||= 1; |
|
|
1400 | |
|
|
1401 | for (0 .. $#$children) { |
|
|
1402 | my $child = $children->[$_]; |
|
|
1403 | |
|
|
1404 | $h[$_] += ($h - $req_h) * $child->{expand} / $exp; |
|
|
1405 | } |
|
|
1406 | } |
|
|
1407 | |
|
|
1408 | CFClient::UI::harmonize \@h; |
|
|
1409 | |
|
|
1410 | my $y = 0; |
|
|
1411 | for (0 .. $#$children) { |
|
|
1412 | my $child = $children->[$_]; |
|
|
1413 | my $h = $h[$_]; |
|
|
1414 | $child->configure (0, $y, $w, $h); |
|
|
1415 | |
|
|
1416 | $y += $h; |
|
|
1417 | } |
|
|
1418 | |
|
|
1419 | 1 |
|
|
1420 | } |
1383 | } |
1421 | |
1384 | |
1422 | ############################################################################# |
1385 | ############################################################################# |
1423 | |
1386 | |
1424 | package CFClient::UI::Label; |
1387 | package CFClient::UI::Label; |
… | |
… | |
2802 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2765 | $self->{scrolled}->add (1, $row, $item->{desc_widget}); |
2803 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2766 | $self->{scrolled}->add (2, $row, $item->{weight_widget}); |
2804 | |
2767 | |
2805 | $row++; |
2768 | $row++; |
2806 | } |
2769 | } |
2807 | } |
|
|
2808 | |
|
|
2809 | sub size_request { |
|
|
2810 | my ($self) = @_; |
|
|
2811 | ($self->{req_w}, $self->{req_h}); |
|
|
2812 | } |
2770 | } |
2813 | |
2771 | |
2814 | ############################################################################# |
2772 | ############################################################################# |
2815 | |
2773 | |
2816 | package CFClient::UI::Menu; |
2774 | package CFClient::UI::Menu; |
… | |
… | |
3040 | if exists $child->{req_x}; |
2998 | if exists $child->{req_x}; |
3041 | |
2999 | |
3042 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3000 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3043 | if exists $child->{req_y}; |
3001 | if exists $child->{req_y}; |
3044 | |
3002 | |
3045 | $X = $self->{user_x} if exists $self->{user_x}; |
3003 | delete @$child{qw(req_x req_y)}; |
3046 | $Y = $self->{user_y} if exists $self->{user_y}; |
|
|
3047 | |
3004 | |
3048 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
3005 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
3049 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
3006 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
3050 | |
3007 | |
3051 | $child->configure ($X, $Y, $W, $H); |
3008 | $child->configure ($X, $Y, $W, $H); |
… | |
… | |
3071 | $::WANT_REFRESH++; |
3028 | $::WANT_REFRESH++; |
3072 | } |
3029 | } |
3073 | |
3030 | |
3074 | sub add { |
3031 | sub add { |
3075 | my ($self, @children) = @_; |
3032 | my ($self, @children) = @_; |
3076 | |
|
|
3077 | for (my @widgets = @children; my $w = pop @widgets; ) { |
|
|
3078 | push @widgets, $w->children; |
|
|
3079 | $w->{root} = $self; |
|
|
3080 | $w->{visible} = $self->{visible} + 1; |
|
|
3081 | } |
|
|
3082 | |
3033 | |
3083 | for my $child (@children) { |
3034 | for my $child (@children) { |
3084 | $child->{is_toplevel} = 1; |
3035 | $child->{is_toplevel} = 1; |
3085 | |
3036 | |
3086 | # integerise window positions |
3037 | # integerise window positions |
3087 | $child->{x} = int $child->{x}; |
3038 | $child->{x} = int $child->{x}; |
3088 | $child->{y} = int $child->{y}; |
3039 | $child->{y} = int $child->{y}; |
3089 | } |
3040 | } |
3090 | |
3041 | |
3091 | $self->SUPER::add (@children); |
3042 | $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 | |
3092 | } |
3049 | } |
3093 | |
3050 | |
3094 | sub remove { |
3051 | sub remove { |
3095 | my ($self, @children) = @_; |
3052 | my ($self, @children) = @_; |
3096 | |
3053 | |
… | |
… | |
3136 | |
3093 | |
3137 | my $widget = pop @queue || last; |
3094 | my $widget = pop @queue || last; |
3138 | |
3095 | |
3139 | defined $widget->{visible} or last; # do not resize invisible widgets |
3096 | defined $widget->{visible} or last; # do not resize invisible widgets |
3140 | |
3097 | |
3141 | my ($w, $h) = $widget->{user_w} && $widget->{user_h} |
3098 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
3142 | ? @$widget{qw(user_w user_h)} |
3099 | ? @$widget{qw(def_w def_h)} |
3143 | : $widget->size_request; |
3100 | : $widget->size_request; |
3144 | |
3101 | |
3145 | if (delete $widget->{force_alloc} |
3102 | if (delete $widget->{force_alloc} |
3146 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
3103 | or $w != $widget->{req_w} or $h != $widget->{req_h}) { |
3147 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
3104 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |