Widget/HBox.pm untested

filter: Statementstmt 0.0% 0 / 55 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 76 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
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::HBox 0.53;
11 class Tickit::Widget::HBox :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::HBox> - distribute child widgets in a horizontal row
23  
24 =head1 SYNOPSIS
25  
26 use Tickit;
27 use Tickit::Widget::HBox;
28 use Tickit::Widget::Static;
29  
30 my $hbox = Tickit::Widget::HBox->new;
31  
32 foreach my $position (qw( left centre right )) {
33 $hbox->add(
34 Tickit::Widget::Static->new(
35 text => $position,
36 align => $position,
37 valign => "middle",
38 ),
39 expand => 1
40 );
41 }
42  
43 Tickit->new( root => $hbox )->run;
44  
45 =head1 DESCRIPTION
46  
47 This subclass of L<Tickit::Widget::LinearBox> distributes its children in a
48 horizontal row. Its height will be the height of the tallest child, and its
49 width will be the sum of the widths of all the children, plus the inter-child
50 spacing.
51  
52 Optionally, if given a non-zero spacing between child widgets and a style to
53 draw in, a vertical 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 columns 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 return max( 1, map { $_->requested_lines } $self->children );
106 }
107  
108 method cols
109 {
110 my $spacing = $self->get_style_values( "spacing" );
111 return ( sum( map { $_->requested_cols } $self->children ) || 1 ) +
112 $spacing * ( $self->children - 1 );
113 }
114  
115 method get_total_quota
116 {
117 my ( $window ) = @_;
118 return $window->cols;
119 }
120  
121 method get_child_base
122 {
123 my ( $child ) = @_;
124 return $child->requested_cols;
125 }
126  
127 method set_child_window
128 {
129 my ( $child, $left, $cols, $window ) = @_;
130  
131 if( $window and $cols ) {
132 if( my $childwin = $child->window ) {
133 $childwin->change_geometry( 0, $left, $window->lines, $cols );
134 }
135 else {
136 my $childwin = $window->make_sub( 0, $left, $window->lines, $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 $top = 0;
153 my $gap_left = $prev_win->right;
154 my $gap_right = $next_win->left - 1;
155 my $bottom = $next_win->bottom;
156  
157 my $line_col = int( ( $gap_left + $gap_right ) / 2 );
158  
159 $rb->vline_at( $top, $bottom, $line_col, $style, $pen, CAP_BOTH );
160 }
161  
162 =head1 AUTHOR
163  
164 Paul Evans <leonerd@leonerd.org.uk>
165  
166 =cut
167  
168 0x55AA;