… | |
… | |
113 | } |
113 | } |
114 | |
114 | |
115 | if ($GRAB) { |
115 | if ($GRAB) { |
116 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
116 | if ($ev->{button} == 4 || $ev->{button} == 5) { |
117 | # mousewheel |
117 | # mousewheel |
118 | $ev->{dx} = 0; |
|
|
119 | $ev->{dy} = $ev->{button} * 2 - 9; |
118 | my $delta = $ev->{button} * 2 - 9; |
|
|
119 | my $shift = $ev->{mod} & CFPlus::KMOD_SHIFT; |
|
|
120 | |
|
|
121 | $ev->{dx} = $shift ? $delta : 0; |
|
|
122 | $ev->{dy} = $shift ? 0 : $delta; |
|
|
123 | |
120 | $GRAB->emit (mouse_wheel => $ev); |
124 | $GRAB->emit (mouse_wheel => $ev); |
121 | } else { |
125 | } else { |
122 | $GRAB->emit (button_down => $ev) |
126 | $GRAB->emit (button_down => $ev) |
123 | } |
127 | } |
124 | } |
128 | } |
… | |
… | |
737 | can_events => 0, |
741 | can_events => 0, |
738 | %arg, |
742 | %arg, |
739 | ); |
743 | ); |
740 | |
744 | |
741 | $self->add (@$children) |
745 | $self->add (@$children) |
742 | if $children; |
746 | if $children && @$children; |
743 | |
747 | |
744 | $self |
748 | $self |
745 | } |
749 | } |
746 | |
750 | |
747 | sub realloc { |
751 | sub realloc { |
… | |
… | |
756 | my ($self, @widgets) = @_; |
760 | my ($self, @widgets) = @_; |
757 | |
761 | |
758 | $_->set_parent ($self) |
762 | $_->set_parent ($self) |
759 | for @widgets; |
763 | for @widgets; |
760 | |
764 | |
|
|
765 | # TODO: only do this in widgets that need it, e.g. root, fixed |
761 | use sort 'stable'; |
766 | use sort 'stable'; |
762 | |
767 | |
763 | $self->{children} = [ |
768 | $self->{children} = [ |
764 | sort { $a->{z} <=> $b->{z} } |
769 | sort { $a->{z} <=> $b->{z} } |
765 | @{$self->{children}}, @widgets |
770 | @{$self->{children}}, @widgets |
… | |
… | |
784 | } |
789 | } |
785 | |
790 | |
786 | sub clear { |
791 | sub clear { |
787 | my ($self) = @_; |
792 | my ($self) = @_; |
788 | |
793 | |
789 | my $children = delete $self->{children}; |
794 | my $children = $self->{children}; |
790 | $self->{children} = []; |
795 | $self->{children} = []; |
791 | |
796 | |
792 | for (@$children) { |
797 | for (@$children) { |
793 | delete $_->{parent}; |
798 | delete $_->{parent}; |
794 | $_->hide; |
799 | $_->hide; |
… | |
… | |
954 | sub size_request { |
959 | sub size_request { |
955 | my ($self) = @_; |
960 | my ($self) = @_; |
956 | |
961 | |
957 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
962 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
958 | |
963 | |
959 | $w = 10 if $self->{scroll_x}; |
964 | $w = 1 if $self->{scroll_x}; |
960 | $h = 10 if $self->{scroll_y}; |
965 | $h = 1 if $self->{scroll_y}; |
961 | |
966 | |
962 | ($w, $h) |
967 | ($w, $h) |
963 | } |
968 | } |
964 | |
969 | |
965 | sub invoke_size_allocate { |
970 | sub invoke_size_allocate { |
… | |
… | |
1042 | my $child = delete $arg{child}; |
1047 | my $child = delete $arg{child}; |
1043 | |
1048 | |
1044 | my $self; |
1049 | my $self; |
1045 | |
1050 | |
1046 | my $hslider = new CFPlus::UI::Slider |
1051 | my $hslider = new CFPlus::UI::Slider |
|
|
1052 | col => 0, |
|
|
1053 | row => 1, |
1047 | vertical => 0, |
1054 | vertical => 0, |
1048 | range => [0, 0, 1, 0.01], # HACK fix |
1055 | range => [0, 0, 1, 0.01], # HACK fix |
1049 | on_changed => sub { |
1056 | on_changed => sub { |
1050 | $self->{hpos} = $_[1]; |
1057 | $self->{hpos} = $_[1]; |
1051 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1058 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1052 | }, |
1059 | }, |
1053 | ; |
1060 | ; |
1054 | |
1061 | |
1055 | my $vslider = new CFPlus::UI::Slider |
1062 | my $vslider = new CFPlus::UI::Slider |
|
|
1063 | col => 1, |
|
|
1064 | row => 0, |
1056 | vertical => 1, |
1065 | vertical => 1, |
1057 | range => [0, 0, 1, 0.01], # HACK fix |
1066 | range => [0, 0, 1, 0.01], # HACK fix |
1058 | on_changed => sub { |
1067 | on_changed => sub { |
1059 | $self->{vpos} = $_[1]; |
1068 | $self->{vpos} = $_[1]; |
1060 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1069 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
… | |
… | |
1071 | row_expand => [1, 0], |
1080 | row_expand => [1, 0], |
1072 | %arg, |
1081 | %arg, |
1073 | ); |
1082 | ); |
1074 | |
1083 | |
1075 | $self->{vp} = new CFPlus::UI::ViewPort |
1084 | $self->{vp} = new CFPlus::UI::ViewPort |
|
|
1085 | col => 0, |
|
|
1086 | row => 0, |
1076 | expand => 1, |
1087 | expand => 1, |
1077 | scroll_x => $self->{scroll_x}, |
1088 | scroll_x => $self->{scroll_x}, |
1078 | scroll_y => $self->{scroll_y}, |
1089 | scroll_y => $self->{scroll_y}, |
1079 | on_changed => sub { |
1090 | on_changed => sub { |
1080 | my ($vp, $x, $y) = @_; |
1091 | my ($vp, $x, $y) = @_; |
… | |
… | |
1084 | |
1095 | |
1085 | 0 |
1096 | 0 |
1086 | }, |
1097 | }, |
1087 | ; |
1098 | ; |
1088 | |
1099 | |
1089 | $self->SUPER::add_at (0, 0, $self->{vp}); |
1100 | $self->SUPER::add ($self->{vp}); |
1090 | |
1101 | |
1091 | $self->add ($child) if $child; |
1102 | $self->add ($child) if $child; |
1092 | |
1103 | |
1093 | $self |
1104 | $self |
1094 | } |
1105 | } |
1095 | |
|
|
1096 | #TODO# update range on size_allocate depending on child |
|
|
1097 | |
1106 | |
1098 | sub add { |
1107 | sub add { |
1099 | my ($self, $widget) = @_; |
1108 | my ($self, $widget) = @_; |
1100 | |
1109 | |
1101 | $self->{vp}->add ($self->{child} = $widget); |
1110 | $self->{vp}->add ($self->{child} = $widget); |
… | |
… | |
1108 | |
1117 | |
1109 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1118 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1110 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1119 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1111 | |
1120 | |
1112 | my $visible = $w1 > $w2; |
1121 | my $visible = $w1 > $w2; |
1113 | if ($visible != $self->{hslider}{visible}) { |
1122 | if ($visible != $self->{hslider_visible}) { |
|
|
1123 | $self->{hslider_visible} = $visible; |
1114 | $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) |
1124 | $visible ? $self->SUPER::add ($self->{hslider}) |
1115 | : $self->{hslider}->hide; |
1125 | : $self->SUPER::remove ($self->{hslider}); |
1116 | } |
1126 | } |
1117 | |
1127 | |
1118 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1128 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1119 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1129 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1120 | |
1130 | |
1121 | my $visible = $h1 > $h2; |
1131 | my $visible = $h1 > $h2; |
1122 | if ($visible != $self->{vslider}{visible}) { |
1132 | if ($visible != $self->{vslider_visible}) { |
|
|
1133 | $self->{vslider_visible} = $visible; |
1123 | $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) |
1134 | $visible ? $self->SUPER::add ($self->{vslider}) |
1124 | : $self->{vslider}->hide; |
1135 | : $self->SUPER::remove ($self->{vslider}); |
|
|
1136 | } |
|
|
1137 | } |
|
|
1138 | |
|
|
1139 | sub start_dragging { |
|
|
1140 | my ($self, $ev) = @_; |
|
|
1141 | |
|
|
1142 | $self->grab_focus; |
|
|
1143 | |
|
|
1144 | my $ox = $self->{vp}{view_x}; |
|
|
1145 | my $oy = $self->{vp}{view_y}; |
|
|
1146 | |
|
|
1147 | $self->{motion} = sub { |
|
|
1148 | my ($ev, $x, $y) = @_; |
|
|
1149 | |
|
|
1150 | $ox -= $ev->{xrel}; |
|
|
1151 | $oy -= $ev->{yrel}; |
|
|
1152 | |
|
|
1153 | $self->{vp}->set_offset ($ox, $oy); |
1125 | } |
1154 | }; |
1126 | } |
|
|
1127 | |
|
|
1128 | sub update { |
|
|
1129 | my ($self) = @_; |
|
|
1130 | |
|
|
1131 | $self->update_slider; |
|
|
1132 | $self->SUPER::update; |
|
|
1133 | } |
1155 | } |
1134 | |
1156 | |
1135 | sub invoke_mouse_wheel { |
1157 | sub invoke_mouse_wheel { |
1136 | my ($self, $ev) = @_; |
1158 | my ($self, $ev) = @_; |
1137 | |
1159 | |
1138 | return 0 unless $ev->{dy}; # only vertical movements for now |
|
|
1139 | |
|
|
1140 | $self->{vslider}->emit (mouse_wheel => $ev); |
1160 | $self->{vslider}->emit (mouse_wheel => $ev) if $self->{vslider_visible}; |
|
|
1161 | $self->{hslider}->emit (mouse_wheel => $ev) if $self->{hslider_visible}; |
1141 | |
1162 | |
1142 | 1 |
1163 | 1 |
1143 | } |
1164 | } |
1144 | |
1165 | |
1145 | sub invoke_button_down { |
1166 | sub invoke_button_down { |
1146 | my ($self, $ev, $x, $y) = @_; |
1167 | my ($self, $ev, $x, $y) = @_; |
1147 | |
1168 | |
1148 | if ($ev->{button} == 2) { |
1169 | if ($ev->{button} == 2) { |
1149 | $self->grab_focus; |
1170 | $self->start_dragging ($ev); |
1150 | |
|
|
1151 | my $ox = $self->{vp}{view_x} + $ev->{x}; |
|
|
1152 | my $oy = $self->{vp}{view_y} + $ev->{y}; |
|
|
1153 | |
|
|
1154 | $self->{motion} = sub { |
|
|
1155 | my ($ev, $x, $y) = @_; |
|
|
1156 | |
|
|
1157 | $self->{vp}->set_offset ($ox - $ev->{x}, $oy - $ev->{y}); |
|
|
1158 | $self->update; |
|
|
1159 | }; |
|
|
1160 | |
|
|
1161 | return 1; |
1171 | return 1; |
1162 | } |
1172 | } |
1163 | |
1173 | |
1164 | 0 |
1174 | 0 |
1165 | } |
1175 | } |
… | |
… | |
1575 | |
1585 | |
1576 | ############################################################################# |
1586 | ############################################################################# |
1577 | |
1587 | |
1578 | package CFPlus::UI::Table; |
1588 | package CFPlus::UI::Table; |
1579 | |
1589 | |
1580 | our @ISA = CFPlus::UI::Base::; |
1590 | our @ISA = CFPlus::UI::Container::; |
1581 | |
1591 | |
1582 | use List::Util qw(max sum); |
1592 | use List::Util qw(max sum); |
1583 | |
1593 | |
1584 | use CFPlus::OpenGL; |
1594 | use CFPlus::OpenGL; |
1585 | |
1595 | |
1586 | sub new { |
1596 | sub new { |
1587 | my $class = shift; |
1597 | my $class = shift; |
1588 | |
1598 | |
1589 | $class->SUPER::new ( |
1599 | $class->SUPER::new ( |
1590 | children => [], |
|
|
1591 | col_expand => [], |
1600 | col_expand => [], |
1592 | row_expand => [], |
1601 | row_expand => [], |
1593 | @_, |
1602 | @_, |
1594 | ) |
1603 | ) |
1595 | } |
1604 | } |
1596 | |
1605 | |
1597 | sub children { |
|
|
1598 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1599 | } |
|
|
1600 | |
|
|
1601 | # TODO: store row/col info in child widget and use standard add/del |
|
|
1602 | sub add { |
1606 | sub add { |
1603 | my $self = shift; |
1607 | my ($self, @widgets) = @_; |
1604 | |
1608 | |
1605 | Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# |
1609 | for my $child (@widgets) { |
1606 | $self->add_at (@_); |
1610 | $child->{rowspan} ||= 1; |
|
|
1611 | $child->{colspan} ||= 1; |
|
|
1612 | } |
|
|
1613 | |
|
|
1614 | $self->SUPER::add (@widgets); |
1607 | } |
1615 | } |
1608 | |
1616 | |
1609 | sub add_at { |
1617 | sub add_at { |
1610 | my $self = shift; |
1618 | my $self = shift; |
1611 | |
1619 | |
|
|
1620 | my @widgets; |
|
|
1621 | |
1612 | while (@_) { |
1622 | while (@_) { |
1613 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1623 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1614 | |
1624 | |
1615 | $child->set_parent ($self); |
1625 | $child->{row} = $row; |
1616 | $self->{children}[$row][$col] = $child; |
1626 | $child->{col} = $col; |
1617 | } |
|
|
1618 | |
1627 | |
1619 | $self->{force_realloc} = 1; |
1628 | push @widgets, $child; |
1620 | $self->{force_size_alloc} = 1; |
1629 | } |
1621 | $self->realloc; |
|
|
1622 | } |
|
|
1623 | |
1630 | |
1624 | sub remove { |
1631 | $self->add (@widgets); |
|
|
1632 | } |
|
|
1633 | |
|
|
1634 | sub get_wh { |
1625 | my ($self, $child) = @_; |
1635 | my ($self) = @_; |
1626 | |
1636 | |
1627 | for (@{ $self->{children} }) { |
1637 | my (@w, @h); |
1628 | for (@{ $_ || [] }) { |
1638 | |
1629 | $_ = undef if $_ == $child; |
1639 | my @children = $self->children; |
|
|
1640 | |
|
|
1641 | # first pass, columns |
|
|
1642 | for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { |
|
|
1643 | my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; |
|
|
1644 | |
|
|
1645 | my $sw = sum @w[$c .. $c + $cs - 1]; |
|
|
1646 | |
|
|
1647 | if ($w > $sw) { |
|
|
1648 | $_ += ($w - $sw) / ($sw ? $sw / $_ : $cs) for @w[$c .. $c + $cs - 1]; |
1630 | } |
1649 | } |
1631 | } |
1650 | } |
1632 | } |
|
|
1633 | |
1651 | |
1634 | # TODO: move to container class maybe? send children a signal on removal? |
1652 | # second pass, rows |
1635 | sub clear { |
1653 | for my $widget (sort { $a->{rowspan} <=> $b->{rowspan} } @children) { |
1636 | my ($self) = @_; |
|
|
1637 | |
|
|
1638 | my @children = $self->children; |
|
|
1639 | delete $self->{children}; |
|
|
1640 | |
|
|
1641 | for (@children) { |
|
|
1642 | delete $_->{parent}; |
|
|
1643 | $_->hide; |
|
|
1644 | } |
|
|
1645 | |
|
|
1646 | $self->realloc; |
|
|
1647 | } |
|
|
1648 | |
|
|
1649 | sub get_wh { |
|
|
1650 | my ($self) = @_; |
|
|
1651 | |
|
|
1652 | my (@w, @h); |
|
|
1653 | |
|
|
1654 | for my $y (0 .. $#{$self->{children}}) { |
|
|
1655 | my $row = $self->{children}[$y] |
|
|
1656 | or next; |
|
|
1657 | |
|
|
1658 | for my $x (0 .. $#$row) { |
|
|
1659 | my $widget = $row->[$x] |
|
|
1660 | or next; |
|
|
1661 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
1654 | my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; |
1662 | |
1655 | |
1663 | $w[$x] = max $w[$x], $w; |
1656 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1664 | $h[$y] = max $h[$y], $h; |
1657 | |
|
|
1658 | if ($h > $sh) { |
|
|
1659 | $_ += ($h - $sh) / ($sh ? $sh / $_ : $rs) for @h[$r .. $r + $rs - 1]; |
1665 | } |
1660 | } |
1666 | } |
1661 | } |
1667 | |
1662 | |
1668 | (\@w, \@h) |
1663 | (\@w, \@h) |
1669 | } |
1664 | } |
… | |
… | |
1685 | my ($ws, $hs) = $self->get_wh; |
1680 | my ($ws, $hs) = $self->get_wh; |
1686 | |
1681 | |
1687 | my $req_w = (sum @$ws) || 1; |
1682 | my $req_w = (sum @$ws) || 1; |
1688 | my $req_h = (sum @$hs) || 1; |
1683 | my $req_h = (sum @$hs) || 1; |
1689 | |
1684 | |
1690 | # TODO: nicer code |
1685 | # now linearly scale the rows/columns to the allocated size |
1691 | my @col_expand = @{$self->{col_expand}}; |
1686 | my @col_expand = @{$self->{col_expand}}; |
1692 | @col_expand = (1) x @$ws unless @col_expand; |
1687 | @col_expand = (1) x @$ws unless @col_expand; |
1693 | my $col_expand = (sum @col_expand) || 1; |
1688 | my $col_expand = (sum @col_expand) || 1; |
1694 | |
1689 | |
1695 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1690 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
… | |
… | |
1702 | |
1697 | |
1703 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1698 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1704 | |
1699 | |
1705 | CFPlus::UI::harmonize $hs; |
1700 | CFPlus::UI::harmonize $hs; |
1706 | |
1701 | |
1707 | my $y; |
1702 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
|
|
1703 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1708 | |
1704 | |
1709 | for my $r (0 .. $#{$self->{children}}) { |
1705 | for my $widget ($self->children) { |
1710 | my $row = $self->{children}[$r] |
1706 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; |
1711 | or next; |
|
|
1712 | |
1707 | |
1713 | my $x = 0; |
1708 | $widget->configure ( |
1714 | my $row_h = $hs->[$r]; |
1709 | $x[$c], $y[$r], |
|
|
1710 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1715 | |
1711 | ); |
1716 | for my $c (0 .. $#$row) { |
|
|
1717 | my $col_w = $ws->[$c]; |
|
|
1718 | |
|
|
1719 | if (my $widget = $row->[$c]) { |
|
|
1720 | $widget->configure ($x, $y, $col_w, $row_h); |
|
|
1721 | } |
|
|
1722 | |
|
|
1723 | $x += $col_w; |
|
|
1724 | } |
|
|
1725 | |
|
|
1726 | $y += $row_h; |
|
|
1727 | } |
1712 | } |
1728 | |
1713 | |
1729 | 1 |
1714 | 1 |
1730 | } |
|
|
1731 | |
|
|
1732 | sub find_widget { |
|
|
1733 | my ($self, $x, $y) = @_; |
|
|
1734 | |
|
|
1735 | $x -= $self->{x}; |
|
|
1736 | $y -= $self->{y}; |
|
|
1737 | |
|
|
1738 | my $res; |
|
|
1739 | |
|
|
1740 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
1741 | $res = $_->find_widget ($x, $y) |
|
|
1742 | and return $res; |
|
|
1743 | } |
|
|
1744 | |
|
|
1745 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
1746 | } |
|
|
1747 | |
|
|
1748 | sub _draw { |
|
|
1749 | my ($self) = @_; |
|
|
1750 | |
|
|
1751 | for (grep $_, @{$self->{children}}) { |
|
|
1752 | $_->draw for grep $_, @$_; |
|
|
1753 | } |
|
|
1754 | } |
1715 | } |
1755 | |
1716 | |
1756 | ############################################################################# |
1717 | ############################################################################# |
1757 | |
1718 | |
1758 | package CFPlus::UI::Fixed; |
1719 | package CFPlus::UI::Fixed; |