… | |
… | |
17 | our $BUTTON_STATE; |
17 | our $BUTTON_STATE; |
18 | |
18 | |
19 | our %WIDGET; # all widgets, weak-referenced |
19 | our %WIDGET; # all widgets, weak-referenced |
20 | |
20 | |
21 | sub get_layout { |
21 | sub get_layout { |
|
|
22 | my $layout; |
|
|
23 | |
22 | for (grep { $_->{name} } values %WIDGET) { |
24 | for (grep { $_->{name} } values %WIDGET) { |
23 | $LAYOUT->{$_->{name}} = { |
25 | my $win = $layout->{$_->{name}} = { }; |
24 | x => $_->{x} / $::WIDTH, |
|
|
25 | y => $_->{y} / $::HEIGHT, |
|
|
26 | w => $_->{w} / $::WIDTH, |
|
|
27 | h => $_->{h} / $::HEIGHT |
|
|
28 | }; |
26 | |
29 | } |
27 | $win->{x} = ($_->{x} + $_->{w} * 0.5) / $::WIDTH if $_->{x} =~ /^[0-9.]+$/; |
|
|
28 | $win->{y} = ($_->{y} + $_->{h} * 0.5) / $::HEIGHT if $_->{y} =~ /^[0-9.]+$/; |
|
|
29 | $win->{w} = $_->{w} / $::WIDTH if defined $_->{w}; |
|
|
30 | $win->{h} = $_->{h} / $::HEIGHT if defined $_->{h}; |
30 | |
31 | |
31 | return $LAYOUT; |
32 | $win->{show} = $_->{visible} && $_->{is_toplevel}; |
|
|
33 | } |
|
|
34 | |
|
|
35 | $layout |
32 | } |
36 | } |
33 | |
37 | |
34 | sub set_layout { |
38 | sub set_layout { |
35 | my ($layout) = @_; |
39 | my ($layout) = @_; |
|
|
40 | |
36 | $LAYOUT = $layout; |
41 | $LAYOUT = $layout; |
37 | } |
42 | } |
38 | |
43 | |
39 | sub check_tooltip { |
44 | sub check_tooltip { |
40 | if (!$GRAB) { |
45 | if (!$GRAB) { |
… | |
… | |
165 | sub rescale_widgets { |
170 | sub rescale_widgets { |
166 | my ($sx, $sy) = @_; |
171 | my ($sx, $sy) = @_; |
167 | |
172 | |
168 | for my $widget (values %WIDGET) { |
173 | for my $widget (values %WIDGET) { |
169 | if ($widget->{is_toplevel}) { |
174 | if ($widget->{is_toplevel}) { |
|
|
175 | $widget->{x} += $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
|
|
176 | $widget->{y} += $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
|
|
177 | |
170 | $widget->{x} = int 0.5 + $widget->{x} * $sx if exists $widget->{x}; |
178 | $widget->{x} = int 0.5 + $widget->{x} * $sx if $widget->{x} =~ /^[0-9.]+$/; |
171 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
179 | $widget->{w} = int 0.5 + $widget->{w} * $sx if exists $widget->{w}; |
172 | $widget->{req_w} = int 0.5 + $widget->{req_w} * $sx if exists $widget->{req_w}; |
180 | $widget->{force_w} = int 0.5 + $widget->{force_w} * $sx if exists $widget->{force_w}; |
173 | $widget->{y} = int 0.5 + $widget->{y} * $sy if exists $widget->{y}; |
181 | $widget->{y} = int 0.5 + $widget->{y} * $sy if $widget->{y} =~ /^[0-9.]+$/; |
174 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
182 | $widget->{h} = int 0.5 + $widget->{h} * $sy if exists $widget->{h}; |
175 | $widget->{req_h} = int 0.5 + $widget->{req_h} * $sy if exists $widget->{req_h}; |
183 | $widget->{force_h} = int 0.5 + $widget->{force_h} * $sy if exists $widget->{force_h}; |
|
|
184 | |
|
|
185 | $widget->{x} -= $widget->{w} * 0.5 if $widget->{x} =~ /^[0-9.]+$/; |
|
|
186 | $widget->{y} -= $widget->{h} * 0.5 if $widget->{y} =~ /^[0-9.]+$/; |
|
|
187 | |
176 | } |
188 | } |
177 | } |
189 | } |
178 | |
190 | |
179 | reconfigure_widgets; |
191 | reconfigure_widgets; |
180 | } |
192 | } |
… | |
… | |
189 | |
201 | |
190 | sub new { |
202 | sub new { |
191 | my $class = shift; |
203 | my $class = shift; |
192 | |
204 | |
193 | my $self = bless { |
205 | my $self = bless { |
194 | x => 0, |
206 | x => "center", |
195 | y => 0, |
207 | y => "center", |
196 | z => 0, |
208 | z => 0, |
|
|
209 | w => undef, |
|
|
210 | h => undef, |
197 | can_events => 1, |
211 | can_events => 1, |
198 | @_ |
212 | @_ |
199 | }, $class; |
213 | }, $class; |
|
|
214 | |
|
|
215 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
200 | |
216 | |
201 | for (keys %$self) { |
217 | for (keys %$self) { |
202 | if (/^on_(.*)$/) { |
218 | if (/^on_(.*)$/) { |
203 | $self->connect ($1 => delete $self->{$_}); |
219 | $self->connect ($1 => delete $self->{$_}); |
204 | } |
220 | } |
205 | } |
221 | } |
206 | |
222 | |
207 | Scalar::Util::weaken ($CFClient::UI::WIDGET{$self+0} = $self); |
|
|
208 | |
|
|
209 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
223 | if (my $layout = $CFClient::UI::LAYOUT->{$self->{name}}) { |
210 | $self->{req_x} = $layout->{x} * $::WIDTH; |
224 | $self->{x} = $layout->{x} * $CFClient::UI::ROOT->{w} if exists $layout->{x}; |
211 | $self->{req_y} = $layout->{y} * $::HEIGHT; |
225 | $self->{y} = $layout->{y} * $CFClient::UI::ROOT->{h} if exists $layout->{y}; |
212 | $self->{def_w} = ($layout->{w} != 0 ? $layout->{w} : 1) * $::WIDTH; |
226 | $self->{force_w} = $layout->{w} * $CFClient::UI::ROOT->{w} if exists $layout->{w}; |
213 | $self->{def_h} = ($layout->{h} != 0 ? $layout->{h} : 1) * $::HEIGHT; |
227 | $self->{force_h} = $layout->{h} * $CFClient::UI::ROOT->{h} if exists $layout->{h}; |
|
|
228 | |
|
|
229 | $self->{x} -= $self->{force_w} * 0.5 if exists $layout->{x}; |
|
|
230 | $self->{y} -= $self->{force_h} * 0.5 if exists $layout->{y}; |
|
|
231 | |
|
|
232 | $self->show if $layout->{show}; |
214 | } |
233 | } |
215 | |
234 | |
216 | $self |
235 | $self |
217 | } |
236 | } |
218 | |
237 | |
… | |
… | |
227 | my ($self) = @_; |
246 | my ($self) = @_; |
228 | |
247 | |
229 | return if $self->{parent}; |
248 | return if $self->{parent}; |
230 | |
249 | |
231 | $CFClient::UI::ROOT->add ($self); |
250 | $CFClient::UI::ROOT->add ($self); |
232 | } |
|
|
233 | |
|
|
234 | sub center { |
|
|
235 | my ($self) = @_; |
|
|
236 | |
|
|
237 | $CFClient::UI::ROOT->on_post_alloc ( |
|
|
238 | "center_$self" => sub { |
|
|
239 | $self->move (($self->{parent}{w} - $self->{w}) * 0.5, ($self->{parent}{h} - $self->{h}) * 0.5); |
|
|
240 | }, |
|
|
241 | ); |
|
|
242 | |
|
|
243 | $self->update; |
|
|
244 | } |
251 | } |
245 | |
252 | |
246 | sub set_visible { |
253 | sub set_visible { |
247 | my ($self) = @_; |
254 | my ($self) = @_; |
248 | |
255 | |
… | |
… | |
303 | |
310 | |
304 | $self->{parent}->remove ($self) |
311 | $self->{parent}->remove ($self) |
305 | if $self->{parent}; |
312 | if $self->{parent}; |
306 | } |
313 | } |
307 | |
314 | |
308 | sub move { |
315 | sub move_abs { |
309 | my ($self, $x, $y, $z) = @_; |
316 | my ($self, $x, $y, $z) = @_; |
310 | |
317 | |
311 | $self->{x} = int $x; |
318 | $self->{x} = List::Util::max 0, int $x; |
312 | $self->{y} = int $y; |
319 | $self->{y} = List::Util::max 0, int $y; |
313 | $self->{z} = $z if defined $z; |
320 | $self->{z} = $z if defined $z; |
314 | |
321 | |
315 | $self->update; |
322 | $self->update; |
316 | } |
323 | } |
317 | |
324 | |
318 | sub set_size { |
325 | sub set_size { |
319 | my ($self, $w, $h) = @_; |
326 | my ($self, $w, $h) = @_; |
320 | |
327 | |
321 | $self->{def_w} = $w; |
328 | $self->{force_w} = $w; |
322 | $self->{def_h} = $h; |
329 | $self->{force_h} = $h; |
323 | |
330 | |
324 | $self->realloc; |
331 | $self->realloc; |
325 | } |
332 | } |
326 | |
333 | |
327 | sub size_request { |
334 | sub size_request { |
… | |
… | |
331 | |
338 | |
332 | sub configure { |
339 | sub configure { |
333 | my ($self, $x, $y, $w, $h) = @_; |
340 | my ($self, $x, $y, $w, $h) = @_; |
334 | |
341 | |
335 | if ($self->{aspect}) { |
342 | if ($self->{aspect}) { |
|
|
343 | my ($ow, $oh) = ($w, $h); |
|
|
344 | |
336 | my $w2 = List::Util::min $w, int $h * $self->{aspect}; |
345 | $w = List::Util::min $w, int $h * $self->{aspect}; |
337 | my $h2 = List::Util::min $h, int $w / $self->{aspect}; |
346 | $h = List::Util::min $h, int $w / $self->{aspect}; |
338 | |
347 | |
339 | # use alignment to adjust x, y |
348 | # use alignment to adjust x, y |
340 | |
349 | |
341 | $x += int +($w - $w2) * 0.5; |
350 | $x += int 0.5 * ($ow - $w); |
342 | $y += int +($h - $h2) * 0.5; |
351 | $y += int 0.5 * ($oh - $h); |
343 | |
|
|
344 | ($w, $h) = ($w2, $h2); |
|
|
345 | } |
352 | } |
346 | |
353 | |
347 | if ($self->{x} != $x || $self->{y} != $y) { |
354 | if ($self->{x} ne $x || $self->{y} ne $y) { |
348 | $self->{x} = $x; |
355 | $self->{x} = $x; |
349 | $self->{y} = $y; |
356 | $self->{y} = $y; |
350 | $self->update; |
357 | $self->update; |
351 | } |
358 | } |
352 | |
359 | |
… | |
… | |
461 | |
468 | |
462 | sub set_parent { |
469 | sub set_parent { |
463 | my ($self, $parent) = @_; |
470 | my ($self, $parent) = @_; |
464 | |
471 | |
465 | Scalar::Util::weaken ($self->{parent} = $parent); |
472 | Scalar::Util::weaken ($self->{parent} = $parent); |
466 | |
|
|
467 | $self->set_visible if $parent->{visible}; |
473 | $self->set_visible if $parent->{visible}; |
468 | } |
474 | } |
469 | |
475 | |
470 | sub connect { |
476 | sub connect { |
471 | my ($self, $signal, $cb) = @_; |
477 | my ($self, $signal, $cb) = @_; |
… | |
… | |
506 | sub update { |
512 | sub update { |
507 | my ($self) = @_; |
513 | my ($self) = @_; |
508 | |
514 | |
509 | $self->{parent}->update |
515 | $self->{parent}->update |
510 | if $self->{parent}; |
516 | if $self->{parent}; |
|
|
517 | } |
|
|
518 | |
|
|
519 | sub reconfigure { |
|
|
520 | my ($self) = @_; |
|
|
521 | |
|
|
522 | $self->realloc; |
|
|
523 | $self->update; |
511 | } |
524 | } |
512 | |
525 | |
513 | sub draw { |
526 | sub draw { |
514 | my ($self) = @_; |
527 | my ($self) = @_; |
515 | |
528 | |
… | |
… | |
533 | glVertex $x , $y + $self->{h}; |
546 | glVertex $x , $y + $self->{h}; |
534 | glEnd; |
547 | glEnd; |
535 | glDisable GL_BLEND; |
548 | glDisable GL_BLEND; |
536 | } |
549 | } |
537 | |
550 | |
538 | if ($ENV{PCLIENT_DEBUG}) { |
551 | if ($ENV{CFPLUS_DEBUG}) { |
539 | glPushMatrix; |
552 | glPushMatrix; |
540 | glColor 1, 1, 0, 1; |
553 | glColor 1, 1, 0, 1; |
541 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
554 | glTranslate $self->{x} + 0.375, $self->{y} + 0.375; |
542 | glBegin GL_LINE_LOOP; |
555 | glBegin GL_LINE_LOOP; |
543 | glVertex 0 , 0; |
556 | glVertex 0 , 0; |
… | |
… | |
619 | my ($class, %arg) = @_; |
632 | my ($class, %arg) = @_; |
620 | $class->SUPER::new (can_events => 0, %arg); |
633 | $class->SUPER::new (can_events => 0, %arg); |
621 | } |
634 | } |
622 | |
635 | |
623 | sub size_request { |
636 | sub size_request { |
624 | (0, 0) |
637 | my ($self) = @_; |
|
|
638 | |
|
|
639 | ($self->{w} + 0, $self->{h} + 0) |
625 | } |
640 | } |
626 | |
641 | |
627 | sub draw { } |
642 | sub draw { } |
628 | |
643 | |
629 | ############################################################################# |
644 | ############################################################################# |
… | |
… | |
1015 | |
1030 | |
1016 | my $self = $class->SUPER::new ( |
1031 | my $self = $class->SUPER::new ( |
1017 | bg => [1, 1, 1, 1], |
1032 | bg => [1, 1, 1, 1], |
1018 | border_bg => [1, 1, 1, 1], |
1033 | border_bg => [1, 1, 1, 1], |
1019 | border => 0.6, |
1034 | border => 0.6, |
1020 | is_toplevel => 1, |
|
|
1021 | can_events => 1, |
1035 | can_events => 1, |
|
|
1036 | min_w => 16, |
|
|
1037 | min_h => 16, |
1022 | @_ |
1038 | @_ |
1023 | ); |
1039 | ); |
1024 | |
1040 | |
1025 | $self->{title} &&= new CFClient::UI::Label |
1041 | $self->{title} &&= new CFClient::UI::Label |
1026 | align => 0, |
1042 | align => 0, |
… | |
… | |
1081 | my ($ev, $x, $y) = @_; |
1097 | my ($ev, $x, $y) = @_; |
1082 | |
1098 | |
1083 | my $dx = $ev->{x} - $ox; |
1099 | my $dx = $ev->{x} - $ox; |
1084 | my $dy = $ev->{y} - $oy; |
1100 | my $dy = $ev->{y} - $oy; |
1085 | |
1101 | |
1086 | $self->{user_x} = $wx + $dx * $mx; |
|
|
1087 | $self->{user_y} = $wy + $dy * $my; |
|
|
1088 | $self->{def_w} = $bw + $dx * ($mx ? -1 : 1); |
1102 | $self->{force_w} = $bw + $dx * ($mx ? -1 : 1); |
1089 | $self->{def_h} = $bh + $dy * ($my ? -1 : 1); |
1103 | $self->{force_h} = $bh + $dy * ($my ? -1 : 1); |
1090 | $self->move ($self->{user_x}, $self->{user_y}); |
1104 | |
1091 | $self->realloc; |
1105 | $self->realloc; |
|
|
1106 | $self->move_abs ($wx + $dx * $mx, $wy + $dy * $my); |
1092 | }; |
1107 | }; |
1093 | |
1108 | |
1094 | } elsif ($lr ^ $td) { |
1109 | } elsif ($lr ^ $td) { |
1095 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1110 | my ($ox, $oy) = ($ev->{x}, $ev->{y}); |
1096 | my ($bx, $by) = ($self->{x}, $self->{y}); |
1111 | my ($bx, $by) = ($self->{x}, $self->{y}); |
… | |
… | |
1098 | $self->{motion} = sub { |
1113 | $self->{motion} = sub { |
1099 | my ($ev, $x, $y) = @_; |
1114 | my ($ev, $x, $y) = @_; |
1100 | |
1115 | |
1101 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1116 | ($x, $y) = ($ev->{x}, $ev->{y}); |
1102 | |
1117 | |
1103 | $self->{user_x} = $bx + $x - $ox; |
1118 | $self->move_abs ($bx + $x - $ox, $by + $y - $oy); |
1104 | $self->{user_y} = $by + $y - $oy; |
|
|
1105 | |
|
|
1106 | $self->move ($self->{user_x}, $self->{user_y}); |
|
|
1107 | $self->realloc; |
|
|
1108 | }; |
1119 | }; |
1109 | } |
1120 | } |
1110 | } |
1121 | } |
1111 | |
1122 | |
1112 | sub button_up { |
1123 | sub button_up { |
… | |
… | |
1415 | ellipsise => 3, # end |
1426 | ellipsise => 3, # end |
1416 | layout => (new CFClient::Layout), |
1427 | layout => (new CFClient::Layout), |
1417 | fontsize => 1, |
1428 | fontsize => 1, |
1418 | align => -1, |
1429 | align => -1, |
1419 | valign => -1, |
1430 | valign => -1, |
1420 | padding => 2, |
1431 | padding_x => 2, |
|
|
1432 | padding_y => 2, |
1421 | can_events => 0, |
1433 | can_events => 0, |
1422 | %arg |
1434 | %arg |
1423 | ); |
1435 | ); |
1424 | |
1436 | |
1425 | if (exists $self->{template}) { |
1437 | if (exists $self->{template}) { |
… | |
… | |
1501 | |
1513 | |
1502 | $w = List::Util::max $w, $w2; |
1514 | $w = List::Util::max $w, $w2; |
1503 | $h = List::Util::max $h, $h2; |
1515 | $h = List::Util::max $h, $h2; |
1504 | } |
1516 | } |
1505 | |
1517 | |
1506 | ( |
1518 | ($w, $h) |
1507 | $w + $self->{padding} * 2, |
|
|
1508 | $h + $self->{padding} * 2, |
|
|
1509 | ) |
|
|
1510 | } |
1519 | } |
1511 | |
1520 | |
1512 | sub size_allocate { |
1521 | sub size_allocate { |
1513 | my ($self, $w, $h, $changed) = @_; |
1522 | my ($self, $w, $h, $changed) = @_; |
1514 | |
1523 | |
… | |
… | |
1538 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1547 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1539 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1548 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1540 | |
1549 | |
1541 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1550 | my $tex = new_from_layout CFClient::Texture $self->{layout}; |
1542 | |
1551 | |
1543 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding} |
1552 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1544 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding} |
1553 | : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} |
1545 | : ($self->{w} - $tex->{w}) * 0.5); |
1554 | : ($self->{w} - $tex->{w}) * 0.5); |
1546 | |
1555 | |
1547 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding} |
1556 | $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} |
1548 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding} |
1557 | : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} |
1549 | : ($self->{h} - $tex->{h}) * 0.5); |
1558 | : ($self->{h} - $tex->{h}) * 0.5); |
1550 | |
1559 | |
1551 | $tex |
1560 | $tex |
1552 | }; |
1561 | }; |
1553 | |
1562 | |
… | |
… | |
1792 | |
1801 | |
1793 | sub new { |
1802 | sub new { |
1794 | my $class = shift; |
1803 | my $class = shift; |
1795 | |
1804 | |
1796 | $class->SUPER::new ( |
1805 | $class->SUPER::new ( |
1797 | padding => 4, |
1806 | padding_x => 4, |
|
|
1807 | padding_y => 4, |
1798 | fg => [1, 1, 1], |
1808 | fg => [1, 1, 1], |
1799 | active_fg => [0, 0, 1], |
1809 | active_fg => [0, 0, 1], |
1800 | can_hover => 1, |
1810 | can_hover => 1, |
1801 | align => 0, |
1811 | align => 0, |
1802 | valign => 0, |
1812 | valign => 0, |
… | |
… | |
1849 | |
1859 | |
1850 | sub new { |
1860 | sub new { |
1851 | my $class = shift; |
1861 | my $class = shift; |
1852 | |
1862 | |
1853 | $class->SUPER::new ( |
1863 | $class->SUPER::new ( |
1854 | padding => 2, |
1864 | padding_x => 2, |
|
|
1865 | padding_y => 2, |
1855 | fg => [1, 1, 1], |
1866 | fg => [1, 1, 1], |
1856 | active_fg => [1, 1, 0], |
1867 | active_fg => [1, 1, 0], |
1857 | bg => [0, 0, 0, 0.2], |
1868 | bg => [0, 0, 0, 0.2], |
1858 | active_bg => [1, 1, 1, 0.5], |
1869 | active_bg => [1, 1, 1, 0.5], |
1859 | state => 0, |
1870 | state => 0, |
… | |
… | |
1863 | } |
1874 | } |
1864 | |
1875 | |
1865 | sub size_request { |
1876 | sub size_request { |
1866 | my ($self) = @_; |
1877 | my ($self) = @_; |
1867 | |
1878 | |
1868 | ($self->{padding} * 2 + 6) x 2 |
1879 | (6) x 2 |
1869 | } |
1880 | } |
1870 | |
1881 | |
1871 | sub button_down { |
1882 | sub button_down { |
1872 | my ($self, $ev, $x, $y) = @_; |
1883 | my ($self, $ev, $x, $y) = @_; |
1873 | |
1884 | |
1874 | if ($x >= $self->{padding} && $x < $self->{w} - $self->{padding} |
1885 | if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} |
1875 | && $y >= $self->{padding} && $y < $self->{h} - $self->{padding}) { |
1886 | && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { |
1876 | $self->{state} = !$self->{state}; |
1887 | $self->{state} = !$self->{state}; |
1877 | $self->_emit (changed => $self->{state}); |
1888 | $self->_emit (changed => $self->{state}); |
1878 | } |
1889 | } |
1879 | } |
1890 | } |
1880 | |
1891 | |
1881 | sub _draw { |
1892 | sub _draw { |
1882 | my ($self) = @_; |
1893 | my ($self) = @_; |
1883 | |
1894 | |
1884 | $self->SUPER::_draw; |
1895 | $self->SUPER::_draw; |
1885 | |
1896 | |
1886 | glTranslate $self->{padding} + 0.375, $self->{padding} + 0.375, 0; |
1897 | glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; |
1887 | |
1898 | |
1888 | my $s = (List::Util::min @$self{qw(w h)}) - $self->{padding} * 2; |
1899 | my ($w, $h) = @$self{qw(w h)}; |
|
|
1900 | |
|
|
1901 | my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; |
1889 | |
1902 | |
1890 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1903 | glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} }; |
1891 | |
1904 | |
1892 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1905 | my $tex = $self->{state} ? $tex[1] : $tex[0]; |
1893 | |
1906 | |
… | |
… | |
2158 | fg => [1, 1, 1], |
2171 | fg => [1, 1, 1], |
2159 | active_fg => [0, 0, 0], |
2172 | active_fg => [0, 0, 0], |
2160 | bg => [0, 0, 0, 0.2], |
2173 | bg => [0, 0, 0, 0.2], |
2161 | active_bg => [1, 1, 1, 0.5], |
2174 | active_bg => [1, 1, 1, 0.5], |
2162 | range => [0, 0, 100, 10, 0], |
2175 | range => [0, 0, 100, 10, 0], |
2163 | req_w => $::WIDTH / 80, |
2176 | min_w => $::WIDTH / 80, |
2164 | req_h => $::WIDTH / 80, |
2177 | min_h => $::WIDTH / 80, |
2165 | vertical => 0, |
2178 | vertical => 0, |
2166 | can_hover => 1, |
2179 | can_hover => 1, |
2167 | inner_pad => 0.02, |
2180 | inner_pad => 0.02, |
2168 | @_ |
2181 | @_ |
2169 | ); |
2182 | ); |
… | |
… | |
2209 | } |
2222 | } |
2210 | |
2223 | |
2211 | sub size_request { |
2224 | sub size_request { |
2212 | my ($self) = @_; |
2225 | my ($self) = @_; |
2213 | |
2226 | |
2214 | my $w = $self->{req_w}; |
2227 | ($self->{req_w}, $self->{req_h}) |
2215 | my $h = $self->{req_h}; |
|
|
2216 | |
|
|
2217 | $self->{vertical} ? ($h, $w) : ($w, $h) |
|
|
2218 | } |
2228 | } |
2219 | |
2229 | |
2220 | sub button_down { |
2230 | sub button_down { |
2221 | my ($self, $ev, $x, $y) = @_; |
2231 | my ($self, $ev, $x, $y) = @_; |
2222 | |
2232 | |
… | |
… | |
2649 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2659 | my ($x, $y) = $widget->coord2global ($widget->{w}, 0); |
2650 | |
2660 | |
2651 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2661 | ($x, $y) = $widget->coord2global (-$self->{w}, 0) |
2652 | if $x + $self->{w} > $::WIDTH; |
2662 | if $x + $self->{w} > $::WIDTH; |
2653 | |
2663 | |
2654 | $self->move ($x, $y); |
2664 | $self->move_abs ($x, $y); |
2655 | }); |
2665 | }); |
2656 | } |
2666 | } |
2657 | |
2667 | |
2658 | sub _draw { |
2668 | sub _draw { |
2659 | my ($self) = @_; |
2669 | my ($self) = @_; |
… | |
… | |
2852 | # maybe save $GRAB? must be careful about events... |
2862 | # maybe save $GRAB? must be careful about events... |
2853 | $GRAB = $self; |
2863 | $GRAB = $self; |
2854 | $self->{button} = $ev->{button}; |
2864 | $self->{button} = $ev->{button}; |
2855 | |
2865 | |
2856 | $self->show; |
2866 | $self->show; |
2857 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
2867 | $self->move_abs ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
2858 | } |
2868 | } |
2859 | |
2869 | |
2860 | sub mouse_motion { |
2870 | sub mouse_motion { |
2861 | my ($self, $ev, $x, $y) = @_; |
2871 | my ($self, $ev, $x, $y) = @_; |
2862 | |
2872 | |
… | |
… | |
3028 | my ($self) = @_; |
3038 | my ($self) = @_; |
3029 | |
3039 | |
3030 | ($self->{w}, $self->{h}) |
3040 | ($self->{w}, $self->{h}) |
3031 | } |
3041 | } |
3032 | |
3042 | |
|
|
3043 | sub _to_pixel { |
|
|
3044 | my ($coord, $size, $max) = @_; |
|
|
3045 | |
|
|
3046 | $coord = |
|
|
3047 | $coord eq "center" ? ($max - $size) * 0.5 |
|
|
3048 | : $coord eq "max" ? $max |
|
|
3049 | : $coord; |
|
|
3050 | |
|
|
3051 | $coord = 0 if $coord < 0; |
|
|
3052 | $coord = $max - $size if $coord > $max - $size; |
|
|
3053 | |
|
|
3054 | int $coord + 0.5 |
|
|
3055 | } |
|
|
3056 | |
3033 | sub size_allocate { |
3057 | sub size_allocate { |
3034 | my ($self, $w, $h, $changed) = @_; |
3058 | my ($self, $w, $h, $changed) = @_; |
3035 | |
3059 | |
3036 | for my $child ($self->children) { |
3060 | for my $child ($self->children) { |
3037 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3061 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
3038 | |
3062 | |
3039 | $X = $child->{req_x} > 0 ? $child->{req_x} : $w - $W - $child->{req_x} + 1 |
3063 | $X = $child->{force_x} if exists $child->{force_x}; |
3040 | if exists $child->{req_x}; |
3064 | $Y = $child->{force_y} if exists $child->{force_y}; |
3041 | |
3065 | |
3042 | $Y = $child->{req_y} > 0 ? $child->{req_y} : $h - $H - $child->{req_y} + 1 |
3066 | $X = _to_pixel $X, $W, $self->{w}; |
3043 | if exists $child->{req_y}; |
3067 | $Y = _to_pixel $Y, $H, $self->{h}; |
3044 | |
|
|
3045 | #delete @$child{qw(req_x req_y)};#d# def_x, def_y |
|
|
3046 | |
|
|
3047 | $X = List::Util::max 0, List::Util::min $w - $W, int $X + 0.5; |
|
|
3048 | $Y = List::Util::max 0, List::Util::min $h - $H, int $Y + 0.5; |
|
|
3049 | |
3068 | |
3050 | $child->configure ($X, $Y, $W, $H); |
3069 | $child->configure ($X, $Y, $W, $H); |
3051 | } |
3070 | } |
3052 | } |
3071 | } |
3053 | |
3072 | |
… | |
… | |
3070 | } |
3089 | } |
3071 | |
3090 | |
3072 | sub add { |
3091 | sub add { |
3073 | my ($self, @children) = @_; |
3092 | my ($self, @children) = @_; |
3074 | |
3093 | |
3075 | for my $child (@children) { |
|
|
3076 | $child->{is_toplevel} = 1; |
3094 | $_->{is_toplevel} = 1 |
3077 | |
3095 | for @children; |
3078 | # integerise window positions |
|
|
3079 | $child->{x} = int $child->{x}; |
|
|
3080 | $child->{y} = int $child->{y}; |
|
|
3081 | } |
|
|
3082 | |
3096 | |
3083 | $self->SUPER::add (@children); |
3097 | $self->SUPER::add (@children); |
3084 | } |
3098 | } |
3085 | |
3099 | |
3086 | sub remove { |
3100 | sub remove { |
3087 | my ($self, @children) = @_; |
3101 | my ($self, @children) = @_; |
3088 | |
3102 | |
3089 | $self->SUPER::remove (@children); |
3103 | $self->SUPER::remove (@children); |
|
|
3104 | |
|
|
3105 | delete $self->{is_toplevel} |
|
|
3106 | for @children; |
3090 | |
3107 | |
3091 | while (@children) { |
3108 | while (@children) { |
3092 | my $w = pop @children; |
3109 | my $w = pop @children; |
3093 | push @children, $w->children; |
3110 | push @children, $w->children; |
3094 | $w->set_invisible; |
3111 | $w->set_invisible; |
… | |
… | |
3128 | |
3145 | |
3129 | my $widget = pop @queue || last; |
3146 | my $widget = pop @queue || last; |
3130 | |
3147 | |
3131 | $widget->{visible} or last; # do not resize invisible widgets |
3148 | $widget->{visible} or last; # do not resize invisible widgets |
3132 | |
3149 | |
3133 | my ($w, $h) = $widget->{def_w} && $widget->{def_h} |
|
|
3134 | ? @$widget{qw(def_w def_h)} |
|
|
3135 | : $widget->size_request; |
3150 | my ($w, $h) = $widget->size_request; |
3136 | |
3151 | |
3137 | Carp::confess "$widget: size_request is negative" if $w < 0 || $h < 0;#d# |
3152 | $w = List::Util::max $widget->{min_w}, $w + $widget->{padding_x} * 2; |
3138 | |
3153 | $h = List::Util::max $widget->{min_h}, $h + $widget->{padding_y} * 2; |
|
|
3154 | |
|
|
3155 | $w = $widget->{force_w} if exists $widget->{force_w}; |
|
|
3156 | $h = $widget->{force_h} if exists $widget->{force_h}; |
|
|
3157 | |
3139 | $widget->{req_w} = $w; |
3158 | $widget->{req_w} = $w; |
3140 | $widget->{req_h} = $h; |
3159 | $widget->{req_h} = $h; |
3141 | |
3160 | |
3142 | $self->{size_alloc}{$widget} = [$widget, $widget->{w} || $w, $widget->{h} || $h]; |
3161 | $self->{size_alloc}{$widget} = [$widget, undef, undef]; |
3143 | |
3162 | |
3144 | push @queue, $widget->{parent} |
3163 | push @queue, $widget->{parent} |
3145 | if $widget->{parent}; |
3164 | if ($self->{w} != $w || $self->{h} != $h) && $widget->{parent}; |
3146 | } |
3165 | } |
3147 | } |
3166 | } |
3148 | |
3167 | |
3149 | while (my $size_alloc = delete $self->{size_alloc}) { |
3168 | while (my $size_alloc = delete $self->{size_alloc}) { |
3150 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
3169 | my @queue = sort $b->[0]{visible} <=> $a->[0]{visible}, |
3151 | values %$size_alloc; |
3170 | values %$size_alloc; |
3152 | |
3171 | |
3153 | while () { |
3172 | while () { |
3154 | my ($widget, $w, $h) = @{ pop @queue or last }; |
3173 | my ($widget, $w, $h) = @{ pop @queue or last }; |
3155 | |
3174 | |
|
|
3175 | $w = $widget->{w} || $widget->{req_w} unless defined $w; |
|
|
3176 | $h = $widget->{h} || $widget->{req_h} unless defined $h; |
|
|
3177 | |
3156 | $w = 0 if $w < 0; |
3178 | $w = 0 if $w < 0; |
3157 | $h = 0 if $h < 0; |
3179 | $h = 0 if $h < 0; |
|
|
3180 | |
|
|
3181 | $w = int $w + 0.5; |
|
|
3182 | $h = int $h + 0.5; |
3158 | |
3183 | |
3159 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
3184 | my $changed = $widget->{w} != $w || $widget->{h} != $h; |
3160 | |
3185 | |
3161 | $widget->{w} = $w; |
3186 | $widget->{w} = $w; |
3162 | $widget->{h} = $h; |
3187 | $widget->{h} = $h; |