1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
# You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2012-2023 -- leonerd@leonerd.org.uk
use v5.20;
use warnings;
use Object::Pad 0.807;
package Tickit::Widget::Box 0.60;
class Tickit::Widget::Box :strict(params);
inherit Tickit::ContainerWidget;
apply Tickit::WidgetRole::SingleChildContainer;
use Carp;
use Tickit::Style;
use Tickit::RenderBuffer;
use Tickit::Utils qw( bound );
use constant WIDGET_PEN_FROM_STYLE => 1;
=head1 NAME
C<Tickit::Widget::Box> - apply spacing and positioning to a widget
=head1 SYNOPSIS
use Tickit;
use Tickit::Widget::Box;
use Tickit::Widget::Static;
my $box = Tickit::Widget::Box->new(
bg => "green",
child_lines => "80%",
child_cols => "80%",
)
->set_child(
Tickit::Widget::Static->new(
text => "Hello, world!",
bg => "black",
align => "centre",
valign => "middle",
)
);
Tickit->new( root => $box )->run;
=head1 DESCRIPTION
This subclass of L<Tickit::SingleChildWidget> can apply spacing around the
outside of a given child widget. The size of the Box is controlled by the size
of the child widget bounded by the given limits, allowing it to enforce a
given minimum or maximum size in each of the horizontal and vertical
directions. By setting both the minimum and maximum size to the same value,
the exact size of the child widget can be controlled.
Limits can be specified either as absolute values, or as a percentage of the
maxmium available space.
If the Box is given more space to use than the child widget will consume, the
child will be placed somewhere within the space, at a position that is
controllable using the C<align> and C<valign> properties, as defined by
L<Tickit::WidgetRole::Alignable>.
=head1 STYLE
The default style pen is used as the widget pen.
Note that while the widget pen is mutable and changes to it will result in
immediate redrawing, any changes made will be lost if the widget style is
changed.
=cut
=head1 CONSTRUCTOR
=cut
=head2 new
$box = Tickit::Widget::Box->new( %args );
In addition to the constructor arguments allowed by C<Tickit::Widget> and
C<Tickit::SingleChildWidget>, this constructor also recognises the following
named arguments:
=over 8
=item child_{lines,cols}_{min,max} => NUM or STRING
Initial values for size limit options.
=item child_{lines,cols} => NUM or STRING
Initial values for size forcing options.
=item align => NUM or STRING
=item valign => NUM or STRING
Initial values for alignment options.
=back
=cut
ADJUST :params (
:$align = 0.5,
:$valign = 0.5,
%params, # easier than declaring 6 named ones
) {
foreach (qw( child_lines_min child_lines_max child_cols_min child_cols_max
child_lines child_cols )) {
my $val = delete $params{$_};
$self->${\"set_$_"}( $val ) if defined $val;
}
$self->set_align( $align );
$self->set_valign( $valign );
}
method lines
{
my $child = $self->child;
return $child ? bound( $self->child_lines_min, $child->requested_lines, $self->child_lines_max ) : 1;
}
method cols
{
my $child = $self->child;
return $child ? bound( $self->child_cols_min, $child->requested_cols, $self->child_cols_max ) : 1;
}
method render_to_rb
{
my ( $rb, $rect ) = @_;
$rb->eraserect( $rect );
}
=head1 METHODS
The following methods all accept either absolute sizes, specified in integers,
or percentages, specified in strings of the form C<10%>. If a percentage is
given it specifies a size that is a fraction of the total amount that is
available to the Box.
=head2 child_lines_min
=head2 set_child_lines_min
=head2 child_cols_min
=head2 set_child_cols_min
$min = $box->child_lines_min;
$box->set_child_lines_min( $min );
$min = $box->child_cols_min;
$box->set_child_cols_min( $min );
Accessors for the child size minimum limits. If the child widget requests a
size smaller than these limits, the allocated Window will be resized up to at
least these sizes.
=head2 child_lines_max
=head2 set_child_lines_max
=head2 child_cols_max
=head2 set_child_cols_max
$max = $box->child_lines_max;
$box->set_child_lines_max( $max );
$max = $box->child_cols_max;
$box->set_child_cols_max( $max );
Accessors for the child size maximum limits. If the child widget requests a
size larger than these limits, the allocated Window will be resized down to at
most these sizes.
=head2 set_child_lines
=head2 set_child_cols
$box->set_child_lines( $size );
$box->set_child_cols( $size );
Convenient shortcut mutators that set both the minimum and maximum limit to
the same value. This has the effect of forcing the size of the child widget.
=cut
field $_child_lines_max; method _child_lines_max :lvalue { $_child_lines_max }
field $_child_lines_min; method _child_lines_min :lvalue { $_child_lines_min }
field $_child_cols_max; method _child_cols_max :lvalue { $_child_cols_max }
field $_child_cols_min; method _child_cols_min :lvalue { $_child_cols_min }
# Because I hate copying code 4 times
BEGIN {
use Object::Pad 0.800 ':experimental(mop)';
my $meta = Object::Pad::MOP::Class->for_caller;
foreach my $dir (qw( lines cols )) {
foreach my $lim (qw( max min )) {
my $fieldmeth = "_child_${dir}_${lim}";
my $name = "child_${dir}_${lim}";
$meta->add_method( $name => method {
my $value = $self->$fieldmeth;
if( !defined $value ) {
return undef;
}
elsif( $value =~ m/^(.+)%$/ ) {
my $win = $self->window;
return $win ? int( $1 * $win->$dir / 100 ) : undef;
}
else {
return $value;
}
} );
$meta->add_method( "set_$name" => method {
( $self->$fieldmeth ) = @_;
$self->resized;
} );
}
my $set_min = "set_child_${dir}_min";
my $set_max = "set_child_${dir}_max";
$meta->add_method( "set_child_$dir" => method {
my ( $value ) = @_;
$self->$set_min( $value );
$self->$set_max( $value );
} );
}
}
use Tickit::WidgetRole::Alignable name => "align", dir => "h", reshape => 1;
use Tickit::WidgetRole::Alignable name => "valign", dir => "v", reshape => 1;
method reshape
{
my $window = $self->window or return;
my $child = $self->child or return;
my ( $top, $lines ) = $self->_valign_allocation( $self->lines, $window->lines );
my ( $left, $cols ) = $self->_align_allocation ( $self->cols, $window->cols );
my @geom = ( $top, $left, $lines, $cols );
if( my $childwin = $child->window ) {
$childwin->change_geometry( @geom );
}
else {
$child->set_window( $window->make_sub( @geom ) );
}
$self->redraw;
}
method window_lost
{
my $child = $self->child or return;
$child->set_window( undef );
}
=head1 AUTHOR
Paul Evans <leonerd@leonerd.org.uk>
=cut
0x55AA;
|