… | |
… | |
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 |
… | |
… | |
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 | |
1106 | |
1096 | #TODO# update range on size_allocate depending on child |
1107 | #TODO# update range on size_allocate depending on child? |
1097 | |
1108 | |
1098 | sub add { |
1109 | sub add { |
1099 | my ($self, $widget) = @_; |
1110 | my ($self, $widget) = @_; |
1100 | |
1111 | |
1101 | $self->{vp}->add ($self->{child} = $widget); |
1112 | $self->{vp}->add ($self->{child} = $widget); |
… | |
… | |
1109 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1120 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1110 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1121 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
1111 | |
1122 | |
1112 | my $visible = $w1 > $w2; |
1123 | my $visible = $w1 > $w2; |
1113 | if ($visible != $self->{hslider}{visible}) { |
1124 | if ($visible != $self->{hslider}{visible}) { |
1114 | $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) |
1125 | $visible ? $self->SUPER::add ($self->{hslider}) |
1115 | : $self->{hslider}->hide; |
1126 | : $self->SUPER::remove ($self->{hslider}); |
1116 | } |
1127 | } |
1117 | |
1128 | |
1118 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1129 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1119 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1130 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
1120 | |
1131 | |
1121 | my $visible = $h1 > $h2; |
1132 | my $visible = $h1 > $h2; |
1122 | if ($visible != $self->{vslider}{visible}) { |
1133 | if ($visible != $self->{vslider}{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}); |
1125 | } |
1136 | } |
1126 | } |
1137 | } |
1127 | |
1138 | |
1128 | sub update { |
1139 | sub update { |
1129 | my ($self) = @_; |
1140 | my ($self) = @_; |
… | |
… | |
1133 | } |
1144 | } |
1134 | |
1145 | |
1135 | sub invoke_mouse_wheel { |
1146 | sub invoke_mouse_wheel { |
1136 | my ($self, $ev) = @_; |
1147 | my ($self, $ev) = @_; |
1137 | |
1148 | |
1138 | return 0 unless $ev->{dy}; # only vertical movements for now |
|
|
1139 | |
|
|
1140 | $self->{vslider}->emit (mouse_wheel => $ev); |
1149 | $self->{vslider}->emit (mouse_wheel => $ev) |
|
|
1150 | if $ev->{dy}; |
|
|
1151 | |
|
|
1152 | $self->{hslider}->emit (mouse_wheel => $ev) |
|
|
1153 | if $ev->{dx}; |
1141 | |
1154 | |
1142 | 1 |
1155 | 1 |
1143 | } |
1156 | } |
1144 | |
1157 | |
1145 | sub invoke_button_down { |
1158 | sub invoke_button_down { |
… | |
… | |
1575 | |
1588 | |
1576 | ############################################################################# |
1589 | ############################################################################# |
1577 | |
1590 | |
1578 | package CFPlus::UI::Table; |
1591 | package CFPlus::UI::Table; |
1579 | |
1592 | |
1580 | our @ISA = CFPlus::UI::Base::; |
1593 | our @ISA = CFPlus::UI::Container::; |
1581 | |
1594 | |
1582 | use List::Util qw(max sum); |
1595 | use List::Util qw(max sum); |
1583 | |
1596 | |
1584 | use CFPlus::OpenGL; |
1597 | use CFPlus::OpenGL; |
1585 | |
1598 | |
1586 | sub new { |
1599 | sub new { |
1587 | my $class = shift; |
1600 | my $class = shift; |
1588 | |
1601 | |
1589 | $class->SUPER::new ( |
1602 | $class->SUPER::new ( |
1590 | children => [], |
|
|
1591 | col_expand => [], |
1603 | col_expand => [], |
1592 | row_expand => [], |
1604 | row_expand => [], |
1593 | @_, |
1605 | @_, |
1594 | ) |
1606 | ) |
1595 | } |
1607 | } |
1596 | |
1608 | |
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 { |
1609 | sub add { |
1603 | my $self = shift; |
1610 | my ($self, @widgets) = @_; |
1604 | |
1611 | |
1605 | Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# |
1612 | for my $child (@widgets) { |
1606 | $self->add_at (@_); |
1613 | $child->{rowspan} ||= 1; |
|
|
1614 | $child->{colspan} ||= 1; |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | $self->SUPER::add (@widgets); |
1607 | } |
1618 | } |
1608 | |
1619 | |
1609 | sub add_at { |
1620 | sub add_at { |
1610 | my $self = shift; |
1621 | my $self = shift; |
1611 | |
1622 | |
|
|
1623 | my @widgets; |
|
|
1624 | |
1612 | while (@_) { |
1625 | while (@_) { |
1613 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1626 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1614 | |
1627 | |
1615 | $child->set_parent ($self); |
1628 | $child->{row} = $row; |
1616 | $self->{children}[$row][$col] = $child; |
1629 | $child->{col} = $col; |
1617 | } |
|
|
1618 | |
1630 | |
1619 | $self->{force_realloc} = 1; |
1631 | push @widgets, $child; |
1620 | $self->{force_size_alloc} = 1; |
1632 | } |
1621 | $self->realloc; |
|
|
1622 | } |
|
|
1623 | |
1633 | |
1624 | sub remove { |
1634 | $self->add (@widgets); |
|
|
1635 | } |
|
|
1636 | |
|
|
1637 | sub get_wh { |
1625 | my ($self, $child) = @_; |
1638 | my ($self) = @_; |
1626 | |
1639 | |
1627 | for (@{ $self->{children} }) { |
1640 | my (@w, @h); |
1628 | for (@{ $_ || [] }) { |
1641 | |
1629 | $_ = undef if $_ == $child; |
1642 | my @children = $self->children; |
|
|
1643 | |
|
|
1644 | # first pass, columns |
|
|
1645 | for my $widget (sort { $a->{colspan} <=> $b->{colspan} } @children) { |
|
|
1646 | my ($c, $w, $cs) = @$widget{qw(col req_w colspan)}; |
|
|
1647 | |
|
|
1648 | my $sw = sum @w[$c .. $c + $cs - 1]; |
|
|
1649 | |
|
|
1650 | if ($w > $sw) { |
|
|
1651 | $_ += ($w - $sw) / $cs for @w[$c .. $c + $cs - 1]; |
1630 | } |
1652 | } |
1631 | } |
1653 | } |
1632 | } |
|
|
1633 | |
1654 | |
1634 | # TODO: move to container class maybe? send children a signal on removal? |
1655 | # second pass, rows |
1635 | sub clear { |
1656 | 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)}; |
1657 | my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; |
1662 | |
1658 | |
1663 | $w[$x] = max $w[$x], $w; |
1659 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1664 | $h[$y] = max $h[$y], $h; |
1660 | |
|
|
1661 | if ($h > $sh) { |
|
|
1662 | $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1]; |
1665 | } |
1663 | } |
1666 | } |
1664 | } |
1667 | |
1665 | |
1668 | (\@w, \@h) |
1666 | (\@w, \@h) |
1669 | } |
1667 | } |
… | |
… | |
1685 | my ($ws, $hs) = $self->get_wh; |
1683 | my ($ws, $hs) = $self->get_wh; |
1686 | |
1684 | |
1687 | my $req_w = (sum @$ws) || 1; |
1685 | my $req_w = (sum @$ws) || 1; |
1688 | my $req_h = (sum @$hs) || 1; |
1686 | my $req_h = (sum @$hs) || 1; |
1689 | |
1687 | |
1690 | # TODO: nicer code |
1688 | # now linearly scale the rows/columns to the allocated size |
1691 | my @col_expand = @{$self->{col_expand}}; |
1689 | my @col_expand = @{$self->{col_expand}}; |
1692 | @col_expand = (1) x @$ws unless @col_expand; |
1690 | @col_expand = (1) x @$ws unless @col_expand; |
1693 | my $col_expand = (sum @col_expand) || 1; |
1691 | my $col_expand = (sum @col_expand) || 1; |
1694 | |
1692 | |
1695 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1693 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
… | |
… | |
1702 | |
1700 | |
1703 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1701 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1704 | |
1702 | |
1705 | CFPlus::UI::harmonize $hs; |
1703 | CFPlus::UI::harmonize $hs; |
1706 | |
1704 | |
1707 | my $y; |
1705 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
|
|
1706 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1708 | |
1707 | |
1709 | for my $r (0 .. $#{$self->{children}}) { |
1708 | for my $widget ($self->children) { |
1710 | my $row = $self->{children}[$r] |
1709 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; |
1711 | or next; |
|
|
1712 | |
1710 | |
1713 | my $x = 0; |
1711 | $widget->configure ( |
1714 | my $row_h = $hs->[$r]; |
1712 | $x[$c], $y[$r], |
|
|
1713 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1715 | |
1714 | ); |
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 | } |
1715 | } |
1728 | |
1716 | |
1729 | 1 |
1717 | 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 | } |
1718 | } |
1755 | |
1719 | |
1756 | ############################################################################# |
1720 | ############################################################################# |
1757 | |
1721 | |
1758 | package CFPlus::UI::Fixed; |
1722 | package CFPlus::UI::Fixed; |