… | |
… | |
944 | |
944 | |
945 | ############################################################################# |
945 | ############################################################################# |
946 | |
946 | |
947 | package CFPlus::UI::ViewPort; |
947 | package CFPlus::UI::ViewPort; |
948 | |
948 | |
|
|
949 | use List::Util qw(min max); |
|
|
950 | |
949 | our @ISA = CFPlus::UI::Window::; |
951 | our @ISA = CFPlus::UI::Window::; |
950 | |
952 | |
951 | sub new { |
953 | sub new { |
952 | my $class = shift; |
954 | my $class = shift; |
953 | |
955 | |
… | |
… | |
984 | } |
986 | } |
985 | |
987 | |
986 | sub set_offset { |
988 | sub set_offset { |
987 | my ($self, $x, $y) = @_; |
989 | my ($self, $x, $y) = @_; |
988 | |
990 | |
989 | $self->{view_x} = int $x; |
991 | $self->{view_x} = max 0, min $self->child->{w} - $self->{w}, int $x; |
990 | $self->{view_y} = int $y; |
992 | $self->{view_y} = max 0, min $self->child->{h} - $self->{h}, int $y; |
991 | |
993 | |
|
|
994 | $self->emit (changed => $self->{view_x}, $self->{view_y}); |
992 | $self->update; |
995 | $self->update; |
993 | } |
996 | } |
994 | |
997 | |
995 | # hmm, this does not work for topleft of $self... but we should not ask for that |
998 | # hmm, this does not work for topleft of $self... but we should not ask for that |
996 | sub coord2local { |
999 | sub coord2local { |
… | |
… | |
1072 | row_expand => [1, 0], |
1075 | row_expand => [1, 0], |
1073 | %arg, |
1076 | %arg, |
1074 | ); |
1077 | ); |
1075 | |
1078 | |
1076 | $self->{vp} = new CFPlus::UI::ViewPort |
1079 | $self->{vp} = new CFPlus::UI::ViewPort |
1077 | expand => 1, |
1080 | expand => 1, |
1078 | scroll_x => $self->{scroll_x}, |
1081 | scroll_x => $self->{scroll_x}, |
1079 | scroll_y => $self->{scroll_y}, |
1082 | scroll_y => $self->{scroll_y}, |
|
|
1083 | on_changed => sub { |
|
|
1084 | my ($vp, $x, $y) = @_; |
|
|
1085 | $vp->{parent}{hslider}->set_value ($x); |
|
|
1086 | $vp->{parent}{vslider}->set_value ($y); |
|
|
1087 | |
|
|
1088 | 0 |
|
|
1089 | }, |
1080 | ; |
1090 | ; |
1081 | |
1091 | |
1082 | $self->SUPER::add (0, 0, $self->{vp}); |
1092 | $self->SUPER::add (0, 0, $self->{vp}); |
1083 | $self->SUPER::add (1, 0, $self->{vslider}) if $self->{scroll_y}; |
|
|
1084 | $self->SUPER::add (0, 1, $self->{hslider}) if $self->{scroll_x}; |
|
|
1085 | |
1093 | |
1086 | $self->add ($child) if $child; |
1094 | $self->add ($child) if $child; |
1087 | |
1095 | |
1088 | $self |
1096 | $self |
1089 | } |
1097 | } |
… | |
… | |
1094 | my ($self, $widget) = @_; |
1102 | my ($self, $widget) = @_; |
1095 | |
1103 | |
1096 | $self->{vp}->add ($self->{child} = $widget); |
1104 | $self->{vp}->add ($self->{child} = $widget); |
1097 | } |
1105 | } |
1098 | |
1106 | |
|
|
1107 | sub update_slider { |
|
|
1108 | my ($self) = @_; |
|
|
1109 | |
|
|
1110 | my $child = ($self->{vp} or return)->child; |
|
|
1111 | |
|
|
1112 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
|
|
1113 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
|
|
1114 | |
|
|
1115 | my $visible = $w1 > $w2; |
|
|
1116 | if ($visible != $self->{hslider}{visible}) { |
|
|
1117 | $visible ? $self->SUPER::add (0, 1, $self->{hslider}) |
|
|
1118 | : $self->{hslider}->hide; |
|
|
1119 | } |
|
|
1120 | |
|
|
1121 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
|
|
1122 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
|
|
1123 | |
|
|
1124 | my $visible = $h1 > $h2; |
|
|
1125 | if ($visible != $self->{vslider}{visible}) { |
|
|
1126 | $visible ? $self->SUPER::add (1, 0, $self->{vslider}) |
|
|
1127 | : $self->{vslider}->hide; |
|
|
1128 | } |
|
|
1129 | } |
|
|
1130 | |
|
|
1131 | sub update { |
|
|
1132 | my ($self) = @_; |
|
|
1133 | |
|
|
1134 | $self->SUPER::update; |
|
|
1135 | $self->update_slider; |
|
|
1136 | } |
|
|
1137 | |
1099 | sub invoke_mouse_wheel { |
1138 | sub invoke_mouse_wheel { |
1100 | my ($self, $ev) = @_; |
1139 | my ($self, $ev) = @_; |
1101 | |
1140 | |
1102 | return 0 unless $ev->{dy}; # only vertical movements |
1141 | return 0 unless $ev->{dy}; # only vertical movements for now |
1103 | |
1142 | |
1104 | $self->{vslider}->emit (mouse_wheel => $ev); |
1143 | $self->{vslider}->emit (mouse_wheel => $ev); |
1105 | |
1144 | |
1106 | 1 |
1145 | 1 |
1107 | } |
1146 | } |
1108 | |
1147 | |
1109 | sub update_slider { |
1148 | sub invoke_button_down { |
1110 | my ($self) = @_; |
1149 | my ($self, $ev, $x, $y) = @_; |
1111 | |
1150 | |
1112 | my $child = ($self->{vp} or return)->child; |
1151 | if ($ev->{button} == 2) { |
|
|
1152 | $self->grab_focus; |
1113 | |
1153 | |
1114 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1154 | my $ox = $self->{vp}{view_x} + $ev->{x}; |
1115 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1155 | my $oy = $self->{vp}{view_y} + $ev->{y}; |
1116 | #$self->{hslider}->set_visibility ($w1 != $w2); |
1156 | |
|
|
1157 | $self->{motion} = sub { |
|
|
1158 | my ($ev, $x, $y) = @_; |
1117 | |
1159 | |
1118 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1160 | $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y}); |
1119 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1161 | $self->update; |
1120 | #$self->{vslider}->set_visibility ($h1 != $h2); |
1162 | }; |
1121 | } |
|
|
1122 | |
1163 | |
1123 | sub update { |
1164 | return 1; |
1124 | my ($self) = @_; |
1165 | } |
1125 | |
1166 | |
1126 | $self->SUPER::update; |
1167 | 0 |
1127 | $self->update_slider; |
1168 | } |
|
|
1169 | |
|
|
1170 | sub invoke_button_up { |
|
|
1171 | my ($self, $ev, $x, $y) = @_; |
|
|
1172 | |
|
|
1173 | if (delete $self->{motion}) { |
|
|
1174 | return 1; |
|
|
1175 | } |
|
|
1176 | |
|
|
1177 | 0 |
|
|
1178 | } |
|
|
1179 | |
|
|
1180 | sub invoke_mouse_motion { |
|
|
1181 | my ($self, $ev, $x, $y) = @_; |
|
|
1182 | |
|
|
1183 | if ($self->{motion}) { |
|
|
1184 | $self->{motion}->($ev, $x, $y); |
|
|
1185 | return 1; |
|
|
1186 | } |
|
|
1187 | |
|
|
1188 | 0 |
1128 | } |
1189 | } |
1129 | |
1190 | |
1130 | sub invoke_size_allocate { |
1191 | sub invoke_size_allocate { |
1131 | my ($self, $w, $h) = @_; |
1192 | my ($self, $w, $h) = @_; |
1132 | |
1193 | |
… | |
… | |
1509 | |
1570 | |
1510 | sub new { |
1571 | sub new { |
1511 | my $class = shift; |
1572 | my $class = shift; |
1512 | |
1573 | |
1513 | $class->SUPER::new ( |
1574 | $class->SUPER::new ( |
|
|
1575 | children => [], |
1514 | col_expand => [], |
1576 | col_expand => [], |
1515 | row_expand => [], |
1577 | row_expand => [], |
1516 | @_, |
1578 | @_, |
1517 | ) |
1579 | ) |
1518 | } |
1580 | } |
1519 | |
1581 | |
1520 | sub children { |
1582 | sub children { |
1521 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1583 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
1522 | } |
1584 | } |
1523 | |
1585 | |
|
|
1586 | # TODO: store row/col info in child widget and use standard add/del |
1524 | sub add { |
1587 | sub add { |
1525 | my ($self) = shift; |
1588 | my ($self) = shift; |
1526 | |
1589 | |
1527 | while (@_) { |
1590 | while (@_) { |
1528 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1591 | my ($x, $y, $child) = splice @_, 0, 3, (); |
1529 | $child->set_parent ($self); |
1592 | $child->set_parent ($self); |
1530 | $self->{children}[$y][$x] = $child; |
1593 | $self->{children}[$y][$x] = $child; |
1531 | } |
1594 | } |
1532 | |
1595 | |
1533 | $self->{force_realloc} = 1; |
1596 | $self->{force_realloc} = 1; |
1534 | $self->{force_size_alloc} = 1; |
1597 | $self->{force_size_alloc} = 1; |
1535 | $self->realloc; |
1598 | $self->realloc; |
1536 | } |
1599 | } |
1537 | |
1600 | |
1538 | sub remove { |
1601 | sub remove { |
1539 | my ($self, $child) = @_; |
1602 | my ($self, $child) = @_; |
1540 | |
1603 | |
1541 | # TODO: not yet implemented |
1604 | for (@{ $self->{children} }) { |
|
|
1605 | for (@{ $_ || [] }) { |
|
|
1606 | $_ = undef if $_ == $child; |
|
|
1607 | } |
|
|
1608 | } |
1542 | } |
1609 | } |
1543 | |
1610 | |
1544 | # TODO: move to container class maybe? send children a signal on removal? |
1611 | # TODO: move to container class maybe? send children a signal on removal? |
1545 | sub clear { |
1612 | sub clear { |
1546 | my ($self) = @_; |
1613 | my ($self) = @_; |
… | |
… | |
1659 | my ($self) = @_; |
1726 | my ($self) = @_; |
1660 | |
1727 | |
1661 | for (grep $_, @{$self->{children}}) { |
1728 | for (grep $_, @{$self->{children}}) { |
1662 | $_->draw for grep $_, @$_; |
1729 | $_->draw for grep $_, @$_; |
1663 | } |
1730 | } |
|
|
1731 | } |
|
|
1732 | |
|
|
1733 | ############################################################################# |
|
|
1734 | |
|
|
1735 | package CFPlus::UI::Fixed; |
|
|
1736 | |
|
|
1737 | use List::Util qw(min max); |
|
|
1738 | |
|
|
1739 | our @ISA = CFPlus::UI::Container::; |
|
|
1740 | |
|
|
1741 | sub add { |
|
|
1742 | my ($self, $child, $posmode, $x, $y, $sizemode, $w, $h) = @_; |
|
|
1743 | |
|
|
1744 | $child->{_fixed} = [$posmode, $x, $y, $sizemode, $w, $h]; |
|
|
1745 | $self->SUPER::add ($child); |
|
|
1746 | } |
|
|
1747 | |
|
|
1748 | sub _scale($$$) { |
|
|
1749 | my ($mode, $val, $max) = @_; |
|
|
1750 | |
|
|
1751 | $mode eq "abs" ? $val |
|
|
1752 | : $mode eq "rel" ? $val * $max |
|
|
1753 | : 0 |
|
|
1754 | } |
|
|
1755 | |
|
|
1756 | sub size_request { |
|
|
1757 | my ($self) = @_; |
|
|
1758 | |
|
|
1759 | my ($x1, $y1, $x2, $y2) = (0, 0, 0, 0); |
|
|
1760 | |
|
|
1761 | # determine overall size by querying abs widgets |
|
|
1762 | for my $child ($self->visible_children) { |
|
|
1763 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
|
|
1764 | |
|
|
1765 | if ($pos eq "abs") { |
|
|
1766 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1767 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1768 | |
|
|
1769 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
|
|
1770 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
|
|
1771 | } |
|
|
1772 | } |
|
|
1773 | |
|
|
1774 | my $W = $x2 - $x1; |
|
|
1775 | my $H = $y2 - $y1; |
|
|
1776 | |
|
|
1777 | # now layout remaining widgets |
|
|
1778 | for my $child ($self->visible_children) { |
|
|
1779 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
|
|
1780 | |
|
|
1781 | if ($pos ne "abs") { |
|
|
1782 | $x = _scale $pos, $x, $W; |
|
|
1783 | $y = _scale $pos, $x, $H; |
|
|
1784 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1785 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1786 | |
|
|
1787 | $x1 = min $x1, $x; $x2 = max $x2, $x + $w; |
|
|
1788 | $y1 = min $y1, $y; $y2 = max $y2, $y + $h; |
|
|
1789 | } |
|
|
1790 | } |
|
|
1791 | |
|
|
1792 | my $W = $x2 - $x1; |
|
|
1793 | my $H = $y2 - $y1; |
|
|
1794 | |
|
|
1795 | ($W, $H) |
|
|
1796 | } |
|
|
1797 | |
|
|
1798 | sub invoke_size_allocate { |
|
|
1799 | my ($self, $W, $H) = @_; |
|
|
1800 | |
|
|
1801 | for my $child ($self->visible_children) { |
|
|
1802 | my ($pos, $x, $y, $size, $w, $h) = @{ $child->{_fixed} }; |
|
|
1803 | |
|
|
1804 | $x = _scale $pos, $x, $W; |
|
|
1805 | $y = _scale $pos, $x, $H; |
|
|
1806 | $w = _scale $size, $w, $child->{req_w}; |
|
|
1807 | $h = _scale $size, $h, $child->{req_h}; |
|
|
1808 | |
|
|
1809 | $child->configure ($x, $y, $w, $h); |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | 1 |
1664 | } |
1813 | } |
1665 | |
1814 | |
1666 | ############################################################################# |
1815 | ############################################################################# |
1667 | |
1816 | |
1668 | package CFPlus::UI::Box; |
1817 | package CFPlus::UI::Box; |