| 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, 2013-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807 ':experimental(inherit_field)'; | ||
| 9 | |||
| 10 | package Tickit::Widget::VSplit 0.42; | ||
| 11 | class Tickit::Widget::VSplit :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget::LinearSplit qw( $_split_at $_split_len ); | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | use Tickit::RenderBuffer qw( LINE_SINGLE CAP_BOTH ); | ||
| 17 | |||
| 18 | use Carp; | ||
| 19 | |||
| 20 | use List::Util qw( sum max ); | ||
| 21 | |||
| 22 | =head1 NAME | ||
| 23 | |||
| 24 | C<Tickit::Widget::VSplit> - an adjustable vertical split between two widgets | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Tickit; | ||
| 29 | use Tickit::Widget::VSplit; | ||
| 30 | use Tickit::Widget::Static; | ||
| 31 | |||
| 32 | my $vsplit = Tickit::Widget::VSplit->new | ||
| 33 | ->set_left_child ( Tickit::Widget::Static->new( text => "Text above" ) ), | ||
| 34 | ->set_right_child( Tickit::Widget::Static->new( text => "Text below" ) ); | ||
| 35 | |||
| 36 | Tickit->new( root => $vsplit )->run; | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This container widget holds two child widgets, displayed side by side. The two | ||
| 41 | widgets are displayed with a vertical split bar between them, which reacts to | ||
| 42 | mouse click-drag events, allowing the user to adjust the proportion of space | ||
| 43 | given to the two widgets. | ||
| 44 | |||
| 45 | =head1 STYLE | ||
| 46 | |||
| 47 | The default style pen is used as the widget pen. The following style pen | ||
| 48 | prefixes are also used: | ||
| 49 | |||
| 50 | =over 4 | ||
| 51 | |||
| 52 | =item split => PEN | ||
| 53 | |||
| 54 | The pen used to render the vertical split area | ||
| 55 | |||
| 56 | =back | ||
| 57 | |||
| 58 | The following style keys are used: | ||
| 59 | |||
| 60 | =over 4 | ||
| 61 | |||
| 62 | =item spacing => INT | ||
| 63 | |||
| 64 | The number of columns of spacing between the left and right child widgets | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | The following style tags are used: | ||
| 69 | |||
| 70 | =over 4 | ||
| 71 | |||
| 72 | =item :active | ||
| 73 | |||
| 74 | Set when a mouse drag resize operation is occurring | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | style_definition base => | ||
| 81 | split_fg => "white", | ||
| 82 | split_bg => "blue", | ||
| 83 | spacing => 1; | ||
| 84 | |||
| 85 | style_definition ':active' => | ||
| 86 | split_fg => "hi-white", | ||
| 87 | split_b => 1; | ||
| 88 | |||
| 89 | style_reshape_keys qw( spacing ); | ||
| 90 | |||
| 91 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 92 | |||
| 93 | use constant VALUE_METHOD => "cols"; | ||
| 94 | |||
| 95 | =head1 CONSTRUCTOR | ||
| 96 | |||
| 97 | =head2 new | ||
| 98 | |||
| 99 | $vsplit = Tickit::Widget::VSplit->new( %args ); | ||
| 100 | |||
| 101 | Constructs a new C<Tickit::Widget::VSplit> object. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | method lines | ||
| 106 | { | ||
| 107 | return max( | ||
| 108 | $self->left_child ? $self->left_child->requested_lines : 1, | ||
| 109 | $self->right_child ? $self->right_child->requested_lines : 1, | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | method cols | ||
| 114 | { | ||
| 115 | my $spacing = $self->get_style_values( "spacing" ); | ||
| 116 | return sum( | ||
| 117 | $self->left_child ? $self->left_child->requested_cols : 1, | ||
| 118 | $spacing, | ||
| 119 | $self->right_child ? $self->right_child->requested_cols : 1, | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | =head1 ACCESSORS | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | =head2 left_child | ||
| 128 | |||
| 129 | =head2 set_left_child | ||
| 130 | |||
| 131 | $child = $hsplit->left_child; | ||
| 132 | |||
| 133 | $vsplit->set_left_child( $child ); | ||
| 134 | |||
| 135 | Accessor for the child widget used in the left half of the display. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | *left_child = __PACKAGE__->can( "A_child" ); | ||
| 140 | *set_left_child = __PACKAGE__->can( "set_A_child" ); | ||
| 141 | |||
| 142 | =head2 right_child | ||
| 143 | |||
| 144 | =head2 set_right_child | ||
| 145 | |||
| 146 | $child = $hsplit->right_child; | ||
| 147 | |||
| 148 | $vsplit->set_right_child( $child ); | ||
| 149 | |||
| 150 | Accessor for the child widget used in the right half of the display. | ||
| 151 | |||
| 152 | These mutators returning the container widget itself making them suitable to | ||
| 153 | use as chaining mutators; e.g. | ||
| 154 | |||
| 155 | my $container = Tickit::Widget::VSplit->new( ... ) | ||
| 156 | ->set_left_child ( Tickit::Widget::Box->new ... ) | ||
| 157 | ->set_right_child( Tickit::Widget::Box->new ... ); | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | *right_child = __PACKAGE__->can( "B_child" ); | ||
| 162 | *set_right_child = __PACKAGE__->can( "set_B_child" ); | ||
| 163 | |||
| 164 | method _make_child_geom | ||
| 165 | { | ||
| 166 | my ( $start, $len ) = @_; | ||
| 167 | return ( 0, $start, $self->window->lines, $len ); | ||
| 168 | } | ||
| 169 | |||
| 170 | method render_to_rb | ||
| 171 | { | ||
| 172 | my ( $rb, $rect ) = @_; | ||
| 173 | |||
| 174 | my $lines = $self->window->lines; | ||
| 175 | |||
| 176 | $rb->setpen( $self->get_style_pen( "split" ) ); | ||
| 177 | |||
| 178 | $rb->vline_at( 0, $lines-1, $_split_at, LINE_SINGLE, undef, CAP_BOTH ); | ||
| 179 | |||
| 180 | if( $_split_len > 2 ) { | ||
| 181 | foreach my $line ( $rect->linerange ) { | ||
| 182 | $rb->erase_at( $line, $_split_at + 1, $_split_len - 2 ); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | if( $_split_len > 1 ) { | ||
| 186 | $rb->vline_at( 0, $lines-1, $_split_at + $_split_len - 1, LINE_SINGLE, undef, CAP_BOTH ); | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | method on_mouse | ||
| 191 | { | ||
| 192 | my ( $args ) = @_; | ||
| 193 | |||
| 194 | if( $args->type ne "wheel" and $args->button == 1 ) { | ||
| 195 | return $self->_on_mouse( $args->type, $args->col ); | ||
| 196 | } | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | |||
| 200 | =head1 AUTHOR | ||
| 201 | |||
| 202 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | 0x55AA; |