… | |
… | |
741 | can_events => 0, |
741 | can_events => 0, |
742 | %arg, |
742 | %arg, |
743 | ); |
743 | ); |
744 | |
744 | |
745 | $self->add (@$children) |
745 | $self->add (@$children) |
746 | if $children; |
746 | if $children && @$children; |
747 | |
747 | |
748 | $self |
748 | $self |
749 | } |
749 | } |
750 | |
750 | |
751 | sub realloc { |
751 | sub realloc { |
… | |
… | |
760 | my ($self, @widgets) = @_; |
760 | my ($self, @widgets) = @_; |
761 | |
761 | |
762 | $_->set_parent ($self) |
762 | $_->set_parent ($self) |
763 | for @widgets; |
763 | for @widgets; |
764 | |
764 | |
|
|
765 | # TODO: only do this in widgets that need it, e.g. root, fixed |
765 | use sort 'stable'; |
766 | use sort 'stable'; |
766 | |
767 | |
767 | $self->{children} = [ |
768 | $self->{children} = [ |
768 | sort { $a->{z} <=> $b->{z} } |
769 | sort { $a->{z} <=> $b->{z} } |
769 | @{$self->{children}}, @widgets |
770 | @{$self->{children}}, @widgets |
… | |
… | |
958 | sub size_request { |
959 | sub size_request { |
959 | my ($self) = @_; |
960 | my ($self) = @_; |
960 | |
961 | |
961 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
962 | my ($w, $h) = @{$self->child}{qw(req_w req_h)}; |
962 | |
963 | |
963 | $w = 10 if $self->{scroll_x}; |
964 | $w = 1 if $self->{scroll_x}; |
964 | $h = 10 if $self->{scroll_y}; |
965 | $h = 1 if $self->{scroll_y}; |
965 | |
966 | |
966 | ($w, $h) |
967 | ($w, $h) |
967 | } |
968 | } |
968 | |
969 | |
969 | sub invoke_size_allocate { |
970 | sub invoke_size_allocate { |
… | |
… | |
1046 | my $child = delete $arg{child}; |
1047 | my $child = delete $arg{child}; |
1047 | |
1048 | |
1048 | my $self; |
1049 | my $self; |
1049 | |
1050 | |
1050 | my $hslider = new CFPlus::UI::Slider |
1051 | my $hslider = new CFPlus::UI::Slider |
|
|
1052 | col => 0, |
|
|
1053 | row => 1, |
1051 | vertical => 0, |
1054 | vertical => 0, |
1052 | range => [0, 0, 1, 0.01], # HACK fix |
1055 | range => [0, 0, 1, 0.01], # HACK fix |
1053 | on_changed => sub { |
1056 | on_changed => sub { |
1054 | $self->{hpos} = $_[1]; |
1057 | $self->{hpos} = $_[1]; |
1055 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1058 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1056 | }, |
1059 | }, |
1057 | ; |
1060 | ; |
1058 | |
1061 | |
1059 | my $vslider = new CFPlus::UI::Slider |
1062 | my $vslider = new CFPlus::UI::Slider |
|
|
1063 | col => 1, |
|
|
1064 | row => 0, |
1060 | vertical => 1, |
1065 | vertical => 1, |
1061 | range => [0, 0, 1, 0.01], # HACK fix |
1066 | range => [0, 0, 1, 0.01], # HACK fix |
1062 | on_changed => sub { |
1067 | on_changed => sub { |
1063 | $self->{vpos} = $_[1]; |
1068 | $self->{vpos} = $_[1]; |
1064 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1069 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
… | |
… | |
1075 | row_expand => [1, 0], |
1080 | row_expand => [1, 0], |
1076 | %arg, |
1081 | %arg, |
1077 | ); |
1082 | ); |
1078 | |
1083 | |
1079 | $self->{vp} = new CFPlus::UI::ViewPort |
1084 | $self->{vp} = new CFPlus::UI::ViewPort |
|
|
1085 | col => 0, |
|
|
1086 | row => 0, |
1080 | expand => 1, |
1087 | expand => 1, |
1081 | scroll_x => $self->{scroll_x}, |
1088 | scroll_x => $self->{scroll_x}, |
1082 | scroll_y => $self->{scroll_y}, |
1089 | scroll_y => $self->{scroll_y}, |
1083 | on_changed => sub { |
1090 | on_changed => sub { |
1084 | my ($vp, $x, $y) = @_; |
1091 | my ($vp, $x, $y) = @_; |
… | |
… | |
1088 | |
1095 | |
1089 | 0 |
1096 | 0 |
1090 | }, |
1097 | }, |
1091 | ; |
1098 | ; |
1092 | |
1099 | |
1093 | $self->SUPER::add_at (0, 0, $self->{vp}); |
1100 | $self->SUPER::add ($self->{vp}); |
1094 | |
1101 | |
1095 | $self->add ($child) if $child; |
1102 | $self->add ($child) if $child; |
1096 | |
1103 | |
1097 | $self |
1104 | $self |
1098 | } |
1105 | } |
1099 | |
1106 | |
1100 | #TODO# update range on size_allocate depending on child |
1107 | #TODO# update range on size_allocate depending on child? |
1101 | |
1108 | |
1102 | sub add { |
1109 | sub add { |
1103 | my ($self, $widget) = @_; |
1110 | my ($self, $widget) = @_; |
1104 | |
1111 | |
1105 | $self->{vp}->add ($self->{child} = $widget); |
1112 | $self->{vp}->add ($self->{child} = $widget); |
… | |
… | |
1113 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1120 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1114 | $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]); |
1115 | |
1122 | |
1116 | my $visible = $w1 > $w2; |
1123 | my $visible = $w1 > $w2; |
1117 | if ($visible != $self->{hslider}{visible}) { |
1124 | if ($visible != $self->{hslider}{visible}) { |
1118 | $visible ? $self->SUPER::add_at (0, 1, $self->{hslider}) |
1125 | $visible ? $self->SUPER::add ($self->{hslider}) |
1119 | : $self->{hslider}->hide; |
1126 | : $self->SUPER::remove ($self->{hslider}); |
1120 | } |
1127 | } |
1121 | |
1128 | |
1122 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1129 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
1123 | $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]); |
1124 | |
1131 | |
1125 | my $visible = $h1 > $h2; |
1132 | my $visible = $h1 > $h2; |
1126 | if ($visible != $self->{vslider}{visible}) { |
1133 | if ($visible != $self->{vslider}{visible}) { |
1127 | $visible ? $self->SUPER::add_at (1, 0, $self->{vslider}) |
1134 | $visible ? $self->SUPER::add ($self->{vslider}) |
1128 | : $self->{vslider}->hide; |
1135 | : $self->SUPER::remove ($self->{vslider}); |
1129 | } |
1136 | } |
1130 | } |
1137 | } |
1131 | |
1138 | |
1132 | sub update { |
1139 | sub update { |
1133 | my ($self) = @_; |
1140 | my ($self) = @_; |
… | |
… | |
1581 | |
1588 | |
1582 | ############################################################################# |
1589 | ############################################################################# |
1583 | |
1590 | |
1584 | package CFPlus::UI::Table; |
1591 | package CFPlus::UI::Table; |
1585 | |
1592 | |
1586 | our @ISA = CFPlus::UI::Base::; |
1593 | our @ISA = CFPlus::UI::Container::; |
1587 | |
1594 | |
1588 | use List::Util qw(max sum); |
1595 | use List::Util qw(max sum); |
1589 | |
1596 | |
1590 | use CFPlus::OpenGL; |
1597 | use CFPlus::OpenGL; |
1591 | |
1598 | |
1592 | sub new { |
1599 | sub new { |
1593 | my $class = shift; |
1600 | my $class = shift; |
1594 | |
1601 | |
1595 | $class->SUPER::new ( |
1602 | $class->SUPER::new ( |
1596 | children => [], |
|
|
1597 | col_expand => [], |
1603 | col_expand => [], |
1598 | row_expand => [], |
1604 | row_expand => [], |
1599 | @_, |
1605 | @_, |
1600 | ) |
1606 | ) |
1601 | } |
1607 | } |
1602 | |
1608 | |
1603 | sub children { |
|
|
1604 | grep $_, map @$_, grep $_, @{ $_[0]{children} } |
|
|
1605 | } |
|
|
1606 | |
|
|
1607 | # TODO: store row/col info in child widget and use standard add/del |
|
|
1608 | sub add { |
1609 | sub add { |
1609 | my $self = shift; |
1610 | my ($self, @widgets) = @_; |
1610 | |
1611 | |
1611 | Carp::cluck "please use the add_at method instead of calling add, thank you.\n";#d# |
1612 | for my $child (@widgets) { |
1612 | $self->add_at (@_); |
1613 | $child->{rowspan} ||= 1; |
|
|
1614 | $child->{colspan} ||= 1; |
|
|
1615 | } |
|
|
1616 | |
|
|
1617 | $self->SUPER::add (@widgets); |
1613 | } |
1618 | } |
1614 | |
1619 | |
1615 | sub add_at { |
1620 | sub add_at { |
1616 | my $self = shift; |
1621 | my $self = shift; |
1617 | |
1622 | |
|
|
1623 | my @widgets; |
|
|
1624 | |
1618 | while (@_) { |
1625 | while (@_) { |
1619 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1626 | my ($col, $row, $child) = splice @_, 0, 3, (); |
1620 | |
1627 | |
1621 | $child->set_parent ($self); |
1628 | $child->{row} = $row; |
1622 | $self->{children}[$row][$col] = $child; |
1629 | $child->{col} = $col; |
1623 | } |
|
|
1624 | |
1630 | |
1625 | $self->{force_realloc} = 1; |
1631 | push @widgets, $child; |
1626 | $self->{force_size_alloc} = 1; |
1632 | } |
1627 | $self->realloc; |
|
|
1628 | } |
|
|
1629 | |
1633 | |
1630 | sub remove { |
1634 | $self->add (@widgets); |
|
|
1635 | } |
|
|
1636 | |
|
|
1637 | sub get_wh { |
1631 | my ($self, $child) = @_; |
1638 | my ($self) = @_; |
1632 | |
1639 | |
1633 | for (@{ $self->{children} }) { |
1640 | my (@w, @h); |
1634 | for (@{ $_ || [] }) { |
1641 | |
1635 | $_ = undef if $_ == $child; |
1642 | my @children = $self->children; |
|
|
1643 | |
|
|
1644 | # first pass, columns |
|
|
1645 | for my $widget (sort { $b->{colspan} <=> $a->{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]; |
1636 | } |
1652 | } |
1637 | } |
1653 | } |
1638 | } |
|
|
1639 | |
1654 | |
1640 | # TODO: move to container class maybe? send children a signal on removal? |
1655 | # second pass, rows |
1641 | sub clear { |
1656 | for my $widget (sort { $b->{rowspan} <=> $a->{rowspan} } @children) { |
1642 | my ($self) = @_; |
|
|
1643 | |
|
|
1644 | my @children = $self->children; |
|
|
1645 | delete $self->{children}; |
|
|
1646 | |
|
|
1647 | for (@children) { |
|
|
1648 | delete $_->{parent}; |
|
|
1649 | $_->hide; |
|
|
1650 | } |
|
|
1651 | |
|
|
1652 | $self->realloc; |
|
|
1653 | } |
|
|
1654 | |
|
|
1655 | sub get_wh { |
|
|
1656 | my ($self) = @_; |
|
|
1657 | |
|
|
1658 | my (@w, @h); |
|
|
1659 | |
|
|
1660 | for my $y (0 .. $#{$self->{children}}) { |
|
|
1661 | my $row = $self->{children}[$y] |
|
|
1662 | or next; |
|
|
1663 | |
|
|
1664 | for my $x (0 .. $#$row) { |
|
|
1665 | my $widget = $row->[$x] |
|
|
1666 | or next; |
|
|
1667 | my ($w, $h) = @$widget{qw(req_w req_h)}; |
1657 | my ($r, $h, $rs) = @$widget{qw(row req_h rowspan)}; |
1668 | |
1658 | |
1669 | $w[$x] = max $w[$x], $w; |
1659 | my $sh = sum @h[$r .. $r + $rs - 1]; |
1670 | $h[$y] = max $h[$y], $h; |
1660 | |
|
|
1661 | if ($h > $sh) { |
|
|
1662 | $_ += ($h - $sh) / $rs for @h[$r .. $r + $rs - 1]; |
1671 | } |
1663 | } |
1672 | } |
1664 | } |
1673 | |
1665 | |
1674 | (\@w, \@h) |
1666 | (\@w, \@h) |
1675 | } |
1667 | } |
… | |
… | |
1691 | my ($ws, $hs) = $self->get_wh; |
1683 | my ($ws, $hs) = $self->get_wh; |
1692 | |
1684 | |
1693 | my $req_w = (sum @$ws) || 1; |
1685 | my $req_w = (sum @$ws) || 1; |
1694 | my $req_h = (sum @$hs) || 1; |
1686 | my $req_h = (sum @$hs) || 1; |
1695 | |
1687 | |
1696 | # TODO: nicer code |
1688 | # now linearly scale the rows/columns to the allocated size |
1697 | my @col_expand = @{$self->{col_expand}}; |
1689 | my @col_expand = @{$self->{col_expand}}; |
1698 | @col_expand = (1) x @$ws unless @col_expand; |
1690 | @col_expand = (1) x @$ws unless @col_expand; |
1699 | my $col_expand = (sum @col_expand) || 1; |
1691 | my $col_expand = (sum @col_expand) || 1; |
1700 | |
1692 | |
1701 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1693 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
… | |
… | |
1708 | |
1700 | |
1709 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1701 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
1710 | |
1702 | |
1711 | CFPlus::UI::harmonize $hs; |
1703 | CFPlus::UI::harmonize $hs; |
1712 | |
1704 | |
1713 | my $y; |
1705 | my @x; for (0 .. $#$ws) { $x[$_ + 1] = $x[$_] + $ws->[$_] } |
|
|
1706 | my @y; for (0 .. $#$hs) { $y[$_ + 1] = $y[$_] + $hs->[$_] } |
1714 | |
1707 | |
1715 | for my $r (0 .. $#{$self->{children}}) { |
1708 | for my $widget ($self->children) { |
1716 | my $row = $self->{children}[$r] |
1709 | my ($r, $c, $w, $h, $rs, $cs) = @$widget{qw(row col req_w req_h rowspan colspan)}; |
1717 | or next; |
|
|
1718 | |
1710 | |
1719 | my $x = 0; |
1711 | $widget->configure ( |
1720 | my $row_h = $hs->[$r]; |
1712 | $x[$c], $y[$r], |
|
|
1713 | $x[$c + $cs] - $x[$c], $y[$r + $rs] - $y[$r], |
1721 | |
1714 | ); |
1722 | for my $c (0 .. $#$row) { |
|
|
1723 | my $col_w = $ws->[$c]; |
|
|
1724 | |
|
|
1725 | if (my $widget = $row->[$c]) { |
|
|
1726 | $widget->configure ($x, $y, $col_w, $row_h); |
|
|
1727 | } |
|
|
1728 | |
|
|
1729 | $x += $col_w; |
|
|
1730 | } |
|
|
1731 | |
|
|
1732 | $y += $row_h; |
|
|
1733 | } |
1715 | } |
1734 | |
1716 | |
1735 | 1 |
1717 | 1 |
1736 | } |
|
|
1737 | |
|
|
1738 | sub find_widget { |
|
|
1739 | my ($self, $x, $y) = @_; |
|
|
1740 | |
|
|
1741 | $x -= $self->{x}; |
|
|
1742 | $y -= $self->{y}; |
|
|
1743 | |
|
|
1744 | my $res; |
|
|
1745 | |
|
|
1746 | for (grep $_, map @$_, grep $_, @{ $self->{children} }) { |
|
|
1747 | $res = $_->find_widget ($x, $y) |
|
|
1748 | and return $res; |
|
|
1749 | } |
|
|
1750 | |
|
|
1751 | $self->SUPER::find_widget ($x + $self->{x}, $y + $self->{y}) |
|
|
1752 | } |
|
|
1753 | |
|
|
1754 | sub _draw { |
|
|
1755 | my ($self) = @_; |
|
|
1756 | |
|
|
1757 | for (grep $_, @{$self->{children}}) { |
|
|
1758 | $_->draw for grep $_, @$_; |
|
|
1759 | } |
|
|
1760 | } |
1718 | } |
1761 | |
1719 | |
1762 | ############################################################################# |
1720 | ############################################################################# |
1763 | |
1721 | |
1764 | package CFPlus::UI::Fixed; |
1722 | package CFPlus::UI::Fixed; |