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