| 1 | # You may distribute under the terms of either the GNU General Public License | ||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||
| 3 | # | ||
| 4 | # (C) Paul Evans, 2009-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::VBox 0.53; | ||
| 11 | class Tickit::Widget::VBox :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget::LinearBox; | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | use Tickit::RenderBuffer qw( CAP_BOTH ); | ||
| 17 | |||
| 18 | use List::Util qw( sum max ); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | C<Tickit::Widget::VBox> - distribute child widgets in a vertical column | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use Tickit; | ||
| 27 | use Tickit::Widget::VBox; | ||
| 28 | use Tickit::Widget::Static; | ||
| 29 | |||
| 30 | my $vbox = Tickit::Widget::VBox->new; | ||
| 31 | |||
| 32 | foreach my $position (qw( top middle bottom )) { | ||
| 33 | $vbox->add( | ||
| 34 | Tickit::Widget::Static->new( | ||
| 35 | text => $position, | ||
| 36 | align => "centre", | ||
| 37 | valign => $position, | ||
| 38 | ), | ||
| 39 | expand => 1 | ||
| 40 | ); | ||
| 41 | } | ||
| 42 | |||
| 43 | Tickit->new( root => $vbox )->run; | ||
| 44 | |||
| 45 | =head1 DESCRIPTION | ||
| 46 | |||
| 47 | This subclass of L<Tickit::Widget::LinearBox> distributes its children in a | ||
| 48 | vertical column. Its width will be the width of the widest child, and its | ||
| 49 | height will be the sum of the heights of all the children, plus the | ||
| 50 | inter-child spacing. | ||
| 51 | |||
| 52 | Optionally, if given a non-zero spacing between child widgets and a style to | ||
| 53 | draw in, a horizontal dividing line will be drawn between each child. | ||
| 54 | |||
| 55 | =head1 STYLE | ||
| 56 | |||
| 57 | The default style pen is used as the widget pen. The following style pen | ||
| 58 | prefixes are also used: | ||
| 59 | |||
| 60 | =over 4 | ||
| 61 | |||
| 62 | =item line => PEN | ||
| 63 | |||
| 64 | I<Since version 0.52.> | ||
| 65 | |||
| 66 | The pen used to render a dividing line between child widgets. | ||
| 67 | |||
| 68 | =back | ||
| 69 | |||
| 70 | Note that while the widget pen is mutable and changes to it will result in | ||
| 71 | immediate redrawing, any changes made will be lost if the widget style is | ||
| 72 | changed. | ||
| 73 | |||
| 74 | The following style keys are used: | ||
| 75 | |||
| 76 | =over 4 | ||
| 77 | |||
| 78 | =item spacing => INT | ||
| 79 | |||
| 80 | The number of lines of spacing between children | ||
| 81 | |||
| 82 | =item line_style => INT | ||
| 83 | |||
| 84 | I<Since version 0.52.> | ||
| 85 | |||
| 86 | If set, the style to draw a dividing line between each child widget. Must be | ||
| 87 | one of the C<LINE_*> constants from L<Tickit::RenderBuffer>. | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | style_definition base => | ||
| 94 | spacing => 0, | ||
| 95 | line_style => 0; | ||
| 96 | |||
| 97 | style_reshape_keys qw( spacing ); | ||
| 98 | |||
| 99 | style_redraw_keys qw( line_style ); | ||
| 100 | |||
| 101 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 102 | |||
| 103 | method lines | ||
| 104 | { | ||
| 105 | my $spacing = $self->get_style_values( "spacing" ); | ||
| 106 | return ( sum( map { $_->requested_lines } $self->children ) || 1 ) + | ||
| 107 | $spacing * ( $self->children - 1 ); | ||
| 108 | } | ||
| 109 | |||
| 110 | method cols | ||
| 111 | { | ||
| 112 | return max( 1, map { $_->requested_cols } $self->children ); | ||
| 113 | } | ||
| 114 | |||
| 115 | method get_total_quota | ||
| 116 | { | ||
| 117 | my ( $window ) = @_; | ||
| 118 | return $window->lines; | ||
| 119 | } | ||
| 120 | |||
| 121 | method get_child_base | ||
| 122 | { | ||
| 123 | my ( $child ) = @_; | ||
| 124 | return $child->requested_lines; | ||
| 125 | } | ||
| 126 | |||
| 127 | method set_child_window | ||
| 128 | { | ||
| 129 | my ( $child, $top, $lines, $window ) = @_; | ||
| 130 | |||
| 131 | if( $window and $lines ) { | ||
| 132 | if( my $childwin = $child->window ) { | ||
| 133 | $childwin->change_geometry( $top, 0, $lines, $window->cols ); | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | my $childwin = $window->make_sub( $top, 0, $lines, $window->cols ); | ||
| 137 | $child->set_window( $childwin ); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | if( my $childwin = $child->window ) { | ||
| 142 | $child->set_window( undef ); | ||
| 143 | $childwin->close; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | method render_dividing_line | ||
| 149 | { | ||
| 150 | my ( $rb, $prev_win, $next_win, $style, $pen ) = @_; | ||
| 151 | |||
| 152 | my $left = 0; | ||
| 153 | my $gap_top = $prev_win->bottom; | ||
| 154 | my $gap_bottom = $next_win->top - 1; | ||
| 155 | my $right = $next_win->right; | ||
| 156 | |||
| 157 | my $line_row = int( ( $gap_top + $gap_bottom ) / 2 ); | ||
| 158 | |||
| 159 | $rb->hline_at( $line_row, $left, $right, $style, $pen, CAP_BOTH ); | ||
| 160 | } | ||
| 161 | |||
| 162 | =head1 AUTHOR | ||
| 163 | |||
| 164 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | 0x55AA; |