| 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, 2011-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::Frame 0.42; | ||
| 11 | class Tickit::Widget::Frame :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::ContainerWidget; | ||
| 14 | |||
| 15 | apply Tickit::WidgetRole::SingleChildContainer; | ||
| 16 | |||
| 17 | use Tickit::Style; | ||
| 18 | |||
| 19 | use Tickit::WidgetRole::Alignable name => "title_align"; | ||
| 20 | |||
| 21 | use Carp; | ||
| 22 | |||
| 23 | use Tickit::Pen; | ||
| 24 | use Tickit::Utils qw( textwidth substrwidth ); | ||
| 25 | use Tickit::RenderBuffer qw( LINE_SINGLE LINE_DOUBLE LINE_THICK CAP_START CAP_END ); | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | C<Tickit::Widget::Frame> - draw a frame around another widget | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use Tickit; | ||
| 34 | use Tickit::Widget::Frame; | ||
| 35 | use Tickit::Widget::Static; | ||
| 36 | |||
| 37 | my $frame = Tickit::Widget::Frame->new( | ||
| 38 | style => { linetype => "single" }, | ||
| 39 | ) | ||
| 40 | ->set_child( | ||
| 41 | Tickit::Widget::Static->new( | ||
| 42 | text => "Hello, world", | ||
| 43 | align => "centre", | ||
| 44 | valign => "middle", | ||
| 45 | ) | ||
| 46 | ); | ||
| 47 | |||
| 48 | Tickit->new( root => $frame )->run; | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This container widget draws a frame around a single child widget. | ||
| 53 | |||
| 54 | =head1 STYLE | ||
| 55 | |||
| 56 | The default style pen is used as the widget pen. The following style pen | ||
| 57 | prefixes are also used: | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =item frame => PEN | ||
| 62 | |||
| 63 | The pen used to render the frame lines | ||
| 64 | |||
| 65 | =back | ||
| 66 | |||
| 67 | The following style keys are used: | ||
| 68 | |||
| 69 | =over 4 | ||
| 70 | |||
| 71 | =item linetype => STRING | ||
| 72 | |||
| 73 | Controls the type of line characters used to draw the frame. Must be one of | ||
| 74 | the following names: | ||
| 75 | |||
| 76 | ascii single double thick solid_inside solid_outside | ||
| 77 | |||
| 78 | The C<ascii> linetype is default, and uses only the C<-|+> ASCII characters. | ||
| 79 | Other linetypes use Unicode box-drawing characters. These may not be supported | ||
| 80 | by all terminals or fonts. | ||
| 81 | |||
| 82 | =item linetype_top => STRING | ||
| 83 | |||
| 84 | =item linetype_bottom => STRING | ||
| 85 | |||
| 86 | =item linetype_left => STRING | ||
| 87 | |||
| 88 | =item linetype_right => STRING | ||
| 89 | |||
| 90 | Overrides the C<linetype> attribute for each side of the frame specifically. | ||
| 91 | If two line-drawing styles meet at corners they should be drawn correctly if | ||
| 92 | C<Tickit::RenderBuffer> can combine the line segments, but in other | ||
| 93 | circumstances the corners are drawn as extensions of the top or bottom line, | ||
| 94 | and the left and right lines do not meet it. | ||
| 95 | |||
| 96 | Any edge's linetype may be set to C<none> to cause that edge not to have a | ||
| 97 | line at all; no extra space will be consumed on that side. | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | style_definition base => | ||
| 104 | linetype => "ascii"; | ||
| 105 | |||
| 106 | style_redraw_keys qw( linetype linetype_top linetype_bottom linetype_left linetype_right ); | ||
| 107 | |||
| 108 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 109 | |||
| 110 | =head1 CONSTRUCTOR | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | =head2 new | ||
| 115 | |||
| 116 | $frame = Tickit::Widget::Frame->new( %args ); | ||
| 117 | |||
| 118 | Constructs a new C<Tickit::Widget::Static> object. | ||
| 119 | |||
| 120 | Takes the following named arguments in addition to those taken by the base | ||
| 121 | L<Tickit::SingleChildWidget> constructor: | ||
| 122 | |||
| 123 | =over 8 | ||
| 124 | |||
| 125 | =item title => STRING | ||
| 126 | |||
| 127 | Optional. | ||
| 128 | |||
| 129 | =item title_align => FLOAT|STRING | ||
| 130 | |||
| 131 | Optional. Defaults to C<0.0> if unspecified. | ||
| 132 | |||
| 133 | =back | ||
| 134 | |||
| 135 | For more details see the accessors below. | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | field $_title :reader :param = undef; | ||
| 140 | field %_has_edge; | ||
| 141 | |||
| 142 | ADJUST :params ( | ||
| 143 | :$title_align = 0, | ||
| 144 | ) { | ||
| 145 | $self->set_title_align( $title_align ); | ||
| 146 | |||
| 147 | # Prepopulate has_* caches | ||
| 148 | $self->on_style_changed_values; | ||
| 149 | } | ||
| 150 | |||
| 151 | =head1 ACCESSORS | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | method on_style_changed_values | ||
| 156 | { | ||
| 157 | my %values = @_; | ||
| 158 | |||
| 159 | my $reshape = 0; | ||
| 160 | |||
| 161 | my $linetype = $values{linetype}[1] // $self->get_style_values( "linetype" ); | ||
| 162 | |||
| 163 | # Cache these | ||
| 164 | foreach (qw( top bottom left right )) { | ||
| 165 | no warnings 'uninitialized'; # treat undef as false | ||
| 166 | |||
| 167 | my $new = ( $values{"linetype_$_"}[1] // $self->get_style_values( "linetype_$_") // $linetype ) | ||
| 168 | ne "none"; | ||
| 169 | |||
| 170 | $reshape = 1 if $_has_edge{$_} != $new; | ||
| 171 | $_has_edge{$_} = $new; | ||
| 172 | } | ||
| 173 | |||
| 174 | $self->reshape if $reshape; | ||
| 175 | } | ||
| 176 | |||
| 177 | method lines | ||
| 178 | { | ||
| 179 | my $child = $self->child; | ||
| 180 | return ( $child ? $child->requested_lines : 0 ) + $_has_edge{top} + $_has_edge{bottom}; | ||
| 181 | } | ||
| 182 | |||
| 183 | method cols | ||
| 184 | { | ||
| 185 | my $child = $self->child; | ||
| 186 | return ( $child ? $child->requested_cols : 0 ) + $_has_edge{left} + $_has_edge{right}; | ||
| 187 | } | ||
| 188 | |||
| 189 | use constant { | ||
| 190 | TOP => 0, | ||
| 191 | BOTTOM => 1, | ||
| 192 | LEFT => 2, | ||
| 193 | RIGHT => 3, | ||
| 194 | CORNER_TL => 4, | ||
| 195 | CORNER_TR => 5, | ||
| 196 | CORNER_BL => 6, | ||
| 197 | CORNER_BR => 7, | ||
| 198 | }; | ||
| 199 | |||
| 200 | # Character numbers from | ||
| 201 | # http://en.wikipedia.org/wiki/Box-drawing_characters | ||
| 202 | |||
| 203 | my %LINECHARS = ( # TOP BOTTOM LEFT RIGHT TL TR BL BR | ||
| 204 | ascii => [ '-', '-', '|', '|', '+', '+', '+', '+' ], | ||
| 205 | solid_inside => [ map chr, 0x2584, 0x2580, 0x2590, 0x258C, 0x2597, 0x2596, 0x259D, 0x2598 ], | ||
| 206 | solid_outside => [ map chr, 0x2580, 0x2584, 0x258C, 0x2590, 0x259B, 0x259C, 0x2599, 0x259F ], | ||
| 207 | ); | ||
| 208 | my %LINESTYLES = ( | ||
| 209 | single => LINE_SINGLE, | ||
| 210 | double => LINE_DOUBLE, | ||
| 211 | thick => LINE_THICK, | ||
| 212 | ); | ||
| 213 | |||
| 214 | =head2 title | ||
| 215 | |||
| 216 | $title = $frame->title; | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | # generated accessor | ||
| 221 | |||
| 222 | =head2 set_title | ||
| 223 | |||
| 224 | $frame->set_title( $title ); | ||
| 225 | |||
| 226 | Accessor for the C<title> property, a string written in the top of the | ||
| 227 | frame. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | method set_title | ||
| 232 | { | ||
| 233 | ( $_title ) = @_; | ||
| 234 | $self->redraw; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 title_align | ||
| 238 | |||
| 239 | =head2 set_title_align | ||
| 240 | |||
| 241 | $title_align = $frame->title_align; | ||
| 242 | |||
| 243 | $frame->set_title_align( $title_align ); | ||
| 244 | |||
| 245 | Accessor for the C<title_align> property. Gives a vlaue in the range C<0.0> to | ||
| 246 | C<1.0> to align the title in the top of the frame. | ||
| 247 | |||
| 248 | See also L<Tickit::WidgetRole::Alignable>. | ||
| 249 | |||
| 250 | =cut | ||
| 251 | |||
| 252 | ## This should come from Tickit::ContainerWidget | ||
| 253 | method children_changed { $self->reshape } | ||
| 254 | |||
| 255 | method reshape | ||
| 256 | { | ||
| 257 | my $window = $self->window or return; | ||
| 258 | my $child = $self->child or return; | ||
| 259 | |||
| 260 | my $lines = $window->lines; | ||
| 261 | my $cols = $window->cols; | ||
| 262 | |||
| 263 | my $extra_lines = $_has_edge{top} + $_has_edge{bottom}; | ||
| 264 | my $extra_cols = $_has_edge{left} + $_has_edge{right}; | ||
| 265 | if( $lines > $extra_lines and $cols > $extra_cols ) { | ||
| 266 | my @geom = ( $_has_edge{top}, $_has_edge{left}, $lines - $extra_lines, $cols - $extra_cols ); | ||
| 267 | |||
| 268 | if( my $childwin = $child->window ) { | ||
| 269 | $childwin->change_geometry( @geom ); | ||
| 270 | } | ||
| 271 | else { | ||
| 272 | my $childwin = $window->make_sub( @geom ); | ||
| 273 | $child->set_window( $childwin ); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | else { | ||
| 277 | if( $child->window ) { | ||
| 278 | $child->set_window( undef ); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | method render_to_rb | ||
| 284 | { | ||
| 285 | my ( $rb, $rect ) = @_; | ||
| 286 | |||
| 287 | $rb->setpen( $self->get_style_pen( "frame" ) ); | ||
| 288 | |||
| 289 | my $cols = $self->window->cols; | ||
| 290 | my $lines = $self->window->lines; | ||
| 291 | |||
| 292 | my $right = $cols - 1; | ||
| 293 | my $bottom = $lines - 1; | ||
| 294 | |||
| 295 | my $linetype = $self->get_style_values( "linetype" ); | ||
| 296 | |||
| 297 | my $linetype_top = $self->get_style_values( "linetype_top" ) // $linetype; | ||
| 298 | my $linetype_bottom = $self->get_style_values( "linetype_bottom" ) // $linetype; | ||
| 299 | my $linetype_left = $self->get_style_values( "linetype_left" ) // $linetype; | ||
| 300 | my $linetype_right = $self->get_style_values( "linetype_right" ) // $linetype; | ||
| 301 | |||
| 302 | my $top_is_line = defined $LINESTYLES{$linetype_top}; | ||
| 303 | my $bottom_is_line = defined $LINESTYLES{$linetype_bottom}; | ||
| 304 | my $left_is_line = defined $LINESTYLES{$linetype_left}; | ||
| 305 | my $right_is_line = defined $LINESTYLES{$linetype_right}; | ||
| 306 | |||
| 307 | my $h_caps = ( $left_is_line ? 0 : CAP_START ) | ( $right_is_line ? 0 : CAP_END ); | ||
| 308 | my $v_caps = ( $top_is_line ? 0 : CAP_START ) | ( $bottom_is_line ? 0 : CAP_END ); | ||
| 309 | my $v_start = $top_is_line ? 0 : $_has_edge{top}; | ||
| 310 | my $v_end = $bottom_is_line ? $bottom : $bottom - $_has_edge{bottom}; | ||
| 311 | |||
| 312 | my $linechars; | ||
| 313 | my $style; | ||
| 314 | |||
| 315 | # Top | ||
| 316 | if( $rect->top == 0 ) { | ||
| 317 | if( $linechars = $LINECHARS{$linetype_top} ) { | ||
| 318 | $rb->goto( 0, 0 ); | ||
| 319 | |||
| 320 | $rb->text( $linechars->[$linetype_top eq $linetype_left ? CORNER_TL : TOP] ); | ||
| 321 | $rb->text( $linechars->[TOP] x ($cols - 2) ) if $cols > 2; | ||
| 322 | $rb->text( $linechars->[$linetype_top eq $linetype_right ? CORNER_TR : TOP] ) if $cols > 1; | ||
| 323 | } | ||
| 324 | elsif( $style = $LINESTYLES{$linetype_top} ) { | ||
| 325 | $rb->hline_at( 0, 0, $right, $style, undef, $h_caps ); | ||
| 326 | } | ||
| 327 | |||
| 328 | if( defined( my $title = $self->title ) ) { | ||
| 329 | my $cols = $self->window->cols; | ||
| 330 | |||
| 331 | # At most we can fit $cols-4 columns of title | ||
| 332 | my ( $left, $titlewidth, $right ) = $self->_title_align_allocation( textwidth( $title ), $cols - 4 ); | ||
| 333 | |||
| 334 | $rb->goto( 0, 1 + $left ); | ||
| 335 | |||
| 336 | $rb->text( " " ); | ||
| 337 | $rb->text( $title ); | ||
| 338 | $rb->text( " " ); | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | # Left | ||
| 343 | if( $rect->left == 0 ) { | ||
| 344 | if( $linechars = $LINECHARS{$linetype_left} ) { | ||
| 345 | $rb->text_at( $_, 0, $linechars->[LEFT] ) for 1 .. $bottom-1; | ||
| 346 | } | ||
| 347 | elsif( $style = $LINESTYLES{$linetype_left} ) { | ||
| 348 | $rb->vline_at( $v_start, $v_end, 0, $style, undef, $v_caps ); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | # Right | ||
| 352 | if( $rect->right == $cols and $cols > 1 ) { | ||
| 353 | if( $linechars = $LINECHARS{$linetype_right} ) { | ||
| 354 | $rb->text_at( $_, $right, $linechars->[RIGHT] ) for 1 .. $bottom-1; | ||
| 355 | } | ||
| 356 | elsif( $style = $LINESTYLES{$linetype_right} ) { | ||
| 357 | $rb->vline_at( $v_start, $v_end, $right, $style, undef, $v_caps ); | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | # Bottom | ||
| 362 | if( $rect->bottom == $lines and $lines > 1 ) { | ||
| 363 | if( $linechars = $LINECHARS{$linetype_bottom} ) { | ||
| 364 | $rb->goto( $bottom, 0 ); | ||
| 365 | |||
| 366 | $rb->text( $linechars->[$linetype_bottom eq $linetype_left ? CORNER_BL : BOTTOM] ); | ||
| 367 | $rb->text( $linechars->[BOTTOM] x ($cols - 2) ) if $cols > 2; | ||
| 368 | $rb->text( $linechars->[$linetype_bottom eq $linetype_right ? CORNER_BR : BOTTOM] ) if $cols > 1; | ||
| 369 | } | ||
| 370 | elsif( $style = $LINESTYLES{$linetype_bottom} ) { | ||
| 371 | $rb->hline_at( $bottom, 0, $right, $style, undef, $h_caps ); | ||
| 372 | } | ||
| 373 | } | ||
| 374 | } | ||
| 375 | |||
| 376 | =head1 TODO | ||
| 377 | |||
| 378 | =over 4 | ||
| 379 | |||
| 380 | =item * | ||
| 381 | |||
| 382 | Specific pen for title. Layered on top of frame pen. | ||
| 383 | |||
| 384 | =item * | ||
| 385 | |||
| 386 | Caption at the bottom of the frame as well. Identical to title. | ||
| 387 | |||
| 388 | =item * | ||
| 389 | |||
| 390 | Consider if it's useful to provide accessors to apply extra padding inside the | ||
| 391 | frame, surrounding the child window. | ||
| 392 | |||
| 393 | =back | ||
| 394 | |||
| 395 | =head1 AUTHOR | ||
| 396 | |||
| 397 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 398 | |||
| 399 | =cut | ||
| 400 | |||
| 401 | 0x55AA; |