| 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::HSplit 0.42; | ||
| 11 | class Tickit::Widget::HSplit :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::HSplit> - an adjustable horizontal split between two widgets | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | use Tickit; | ||
| 29 | use Tickit::Widget::HSplit; | ||
| 30 | use Tickit::Widget::Static; | ||
| 31 | |||
| 32 | my $hsplit = Tickit::Widget::HSplit->new | ||
| 33 | ->set_top_child ( Tickit::Widget::Static->new( text => "Text above" ) ) | ||
| 34 | ->set_bottom_child( Tickit::Widget::Static->new( text => "Text below" ) ); | ||
| 35 | |||
| 36 | Tickit->new( root => $hsplit )->run; | ||
| 37 | |||
| 38 | =head1 DESCRIPTION | ||
| 39 | |||
| 40 | This container widget holds two child widgets, displayed one above the other. | ||
| 41 | The two widgets are displayed with a horizontal split bar between them, which | ||
| 42 | reacts to mouse click-drag events, allowing the user to adjust the proportion | ||
| 43 | of space 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 horizontal 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 lines of spacing between the top and bottom 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 => "lines"; | ||
| 94 | |||
| 95 | =head1 CONSTRUCTOR | ||
| 96 | |||
| 97 | =head2 new | ||
| 98 | |||
| 99 | $hsplit = Tickit::Widget::HSplit->new( %args ); | ||
| 100 | |||
| 101 | Constructs a new C<Tickit::Widget::HSplit> object. | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | method lines | ||
| 106 | { | ||
| 107 | my $spacing = $self->get_style_values( "spacing" ); | ||
| 108 | return sum( | ||
| 109 | $self->top_child ? $self->top_child->requested_lines : 1, | ||
| 110 | $spacing, | ||
| 111 | $self->bottom_child ? $self->bottom_child->requested_lines : 1, | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | method cols | ||
| 116 | { | ||
| 117 | return max( | ||
| 118 | $self->top_child ? $self->top_child->requested_cols : 1, | ||
| 119 | $self->bottom_child ? $self->bottom_child->requested_cols : 1, | ||
| 120 | ); | ||
| 121 | } | ||
| 122 | |||
| 123 | =head1 ACCESSORS | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | =head2 top_child | ||
| 128 | |||
| 129 | =head2 set_top_child | ||
| 130 | |||
| 131 | $child = $hsplit->top_child; | ||
| 132 | |||
| 133 | $hsplit->set_top_child( $child ); | ||
| 134 | |||
| 135 | Accessor for the child widget used in the top half of the display. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | *top_child = __PACKAGE__->can( "A_child" ); | ||
| 140 | *set_top_child = __PACKAGE__->can( "set_A_child" ); | ||
| 141 | |||
| 142 | =head2 bottom_child | ||
| 143 | |||
| 144 | =head2 set_bottom_child | ||
| 145 | |||
| 146 | $child = $hsplit->bottom_child; | ||
| 147 | |||
| 148 | $hsplit->set_bottom_child( $child ); | ||
| 149 | |||
| 150 | Accessor for the child widget used in the bottom 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::HSplit->new( ... ) | ||
| 156 | ->set_top_child ( Tickit::Widget::Box->new ... ) | ||
| 157 | ->set_bottom_child( Tickit::Widget::Box->new ... ); | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | *bottom_child = __PACKAGE__->can( "B_child" ); | ||
| 162 | *set_bottom_child = __PACKAGE__->can( "set_B_child" ); | ||
| 163 | |||
| 164 | method _make_child_geom | ||
| 165 | { | ||
| 166 | my ( $start, $len ) = @_; | ||
| 167 | return ( $start, 0, $len, $self->window->cols ); | ||
| 168 | } | ||
| 169 | |||
| 170 | method render_to_rb | ||
| 171 | { | ||
| 172 | my ( $rb, $rect ) = @_; | ||
| 173 | |||
| 174 | my $cols = $self->window->cols; | ||
| 175 | |||
| 176 | $rb->setpen( $self->get_style_pen( "split" ) ); | ||
| 177 | |||
| 178 | $rb->hline_at( $_split_at, 0, $cols-1, LINE_SINGLE, undef, CAP_BOTH ); | ||
| 179 | |||
| 180 | foreach my $line ( $rect->linerange( 1, $_split_len-2 ) ) { | ||
| 181 | $rb->erase_at( $_split_at + $line, 0, $cols ); | ||
| 182 | } | ||
| 183 | |||
| 184 | if( $_split_len > 1 ) { | ||
| 185 | $rb->hline_at( $_split_at + $_split_len - 1, 0, $cols-1, LINE_SINGLE, undef, CAP_BOTH ); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | method on_mouse | ||
| 190 | { | ||
| 191 | my ( $args ) = @_; | ||
| 192 | |||
| 193 | if( $args->type ne "wheel" and $args->button == 1 ) { | ||
| 194 | return $self->_on_mouse( $args->type, $args->line ); | ||
| 195 | } | ||
| 196 | return; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head1 AUTHOR | ||
| 200 | |||
| 201 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | 0x55AA; |