| 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; | ||
| 9 | |||
| 10 | package Tickit::Widget::GridBox 0.42; | ||
| 11 | class Tickit::Widget::GridBox :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::ContainerWidget; | ||
| 14 | |||
| 15 | use experimental 'postderef'; | ||
| 16 | |||
| 17 | use Tickit::Style; | ||
| 18 | |||
| 19 | use Carp; | ||
| 20 | |||
| 21 | use Tickit::Utils 0.29 qw( distribute ); | ||
| 22 | |||
| 23 | use List::Util qw( sum max ); | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | C<Tickit::Widget::GridBox> - lay out a set of child widgets in a grid | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Tickit; | ||
| 32 | use Tickit::Widget::GridBox; | ||
| 33 | use Tickit::Widget::Static; | ||
| 34 | |||
| 35 | my $gridbox = Tickit::Widget::GridBox->new( | ||
| 36 | style => { | ||
| 37 | col_spacing => 2, | ||
| 38 | row_spacing => 1, | ||
| 39 | } | ||
| 40 | ) | ||
| 41 | ->append_row( | ||
| 42 | [ Tickit::Widget::Static->new( text => "top left" ), | ||
| 43 | Tickit::Widget::Static->new( text => "top right" ) ], | ||
| 44 | ) | ||
| 45 | ->append_row( | ||
| 46 | [ Tickit::Widget::Static->new( text => "bottom left" ), | ||
| 47 | Tickit::Widget::Static->new( text => "bottom right" ) ], | ||
| 48 | ); | ||
| 49 | |||
| 50 | Tickit->new( root => $gridbox )->run; | ||
| 51 | |||
| 52 | =head1 DESCRIPTION | ||
| 53 | |||
| 54 | This container widget holds a set of child widgets distributed in a regular | ||
| 55 | grid shape across rows and columns. | ||
| 56 | |||
| 57 | =head1 STYLE | ||
| 58 | |||
| 59 | The default style pen is used as the widget pen. | ||
| 60 | |||
| 61 | The following style keys are used: | ||
| 62 | |||
| 63 | =over 4 | ||
| 64 | |||
| 65 | =item col_spacing => INT | ||
| 66 | |||
| 67 | The number of columns of spacing between columns | ||
| 68 | |||
| 69 | =item row_spacing => INT | ||
| 70 | |||
| 71 | The number of rows of spacing between rows | ||
| 72 | |||
| 73 | =back | ||
| 74 | |||
| 75 | =cut | ||
| 76 | |||
| 77 | style_definition base => | ||
| 78 | row_spacing => 0, | ||
| 79 | col_spacing => 0; | ||
| 80 | |||
| 81 | style_reshape_keys qw( row_spacing col_spacing ); | ||
| 82 | |||
| 83 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 84 | |||
| 85 | =head1 CONSTRUCTOR | ||
| 86 | |||
| 87 | =head2 new | ||
| 88 | |||
| 89 | $gridbox = Tickit::Widget::GridBox->new( %args ); | ||
| 90 | |||
| 91 | Constructs a new C<Tickit::Widget::GridBox> object. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub BUILDARGS { | ||
| 96 | my $class = shift; | ||
| 97 | my %args = @_; | ||
| 98 | |||
| 99 | exists $args{$_} and $args{style}{$_} = delete $args{$_} for qw( row_spacing col_spacing ); | ||
| 100 | |||
| 101 | return $class->SUPER::BUILDARGS( %args ); | ||
| 102 | } | ||
| 103 | |||
| 104 | field @_grid; | ||
| 105 | field $_max_col = -1; | ||
| 106 | |||
| 107 | method lines | ||
| 108 | { | ||
| 109 | my $row_spacing = $self->get_style_values( "row_spacing" ); | ||
| 110 | my $max_row = $#_grid; | ||
| 111 | return ( sum( map { | ||
| 112 | my $r = $_; | ||
| 113 | max map { | ||
| 114 | my $c = $_; | ||
| 115 | my $child = $_grid[$r][$c]; | ||
| 116 | $child ? $child->requested_lines : 0; | ||
| 117 | } 0 .. $_max_col | ||
| 118 | } 0 .. $max_row ) ) + | ||
| 119 | $row_spacing * $max_row; | ||
| 120 | } | ||
| 121 | |||
| 122 | method cols | ||
| 123 | { | ||
| 124 | my $col_spacing = $self->get_style_values( "col_spacing" ); | ||
| 125 | my $max_row = $#_grid; | ||
| 126 | return ( sum( map { | ||
| 127 | my $c = $_; | ||
| 128 | max map { | ||
| 129 | my $r = $_; | ||
| 130 | my $child = $_grid[$r][$c]; | ||
| 131 | $child ? $child->requested_cols : 0; | ||
| 132 | } 0 .. $max_row | ||
| 133 | } 0 .. $_max_col ) ) + | ||
| 134 | $col_spacing * $_max_col; | ||
| 135 | } | ||
| 136 | |||
| 137 | method children | ||
| 138 | { | ||
| 139 | map { | ||
| 140 | my $r = $_; | ||
| 141 | map { | ||
| 142 | $_grid[$r][$_] ? ( $_grid[$r][$_] ) : () | ||
| 143 | } 0 .. $_max_col | ||
| 144 | } 0.. $#_grid; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head1 METHODS | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | =head2 rowcount | ||
| 152 | |||
| 153 | =head2 colcount | ||
| 154 | |||
| 155 | $count = $gridbox->rowcount; | ||
| 156 | |||
| 157 | $count = $gridbox->colcount; | ||
| 158 | |||
| 159 | Returns the number of rows or columns in the grid. | ||
| 160 | |||
| 161 | =cut | ||
| 162 | |||
| 163 | method rowcount | ||
| 164 | { | ||
| 165 | return scalar @_grid; | ||
| 166 | } | ||
| 167 | |||
| 168 | method colcount | ||
| 169 | { | ||
| 170 | return $_max_col + 1; | ||
| 171 | } | ||
| 172 | |||
| 173 | =head2 add | ||
| 174 | |||
| 175 | $gridbox->add( $row, $col, $child, %opts ); | ||
| 176 | |||
| 177 | Sets the child widget to display in the given grid cell. Cells do not need to | ||
| 178 | be explicitly constructed; the grid will automatically expand to the size | ||
| 179 | required. This method can also be used to replace an existing child at the | ||
| 180 | given cell location. To remove a cell entirely, use the C<remove> method. | ||
| 181 | |||
| 182 | The following options are recognised: | ||
| 183 | |||
| 184 | =over 8 | ||
| 185 | |||
| 186 | =item col_expand => INT | ||
| 187 | |||
| 188 | =item row_expand => INT | ||
| 189 | |||
| 190 | Values for the C<expand> setting for this column or row of the table. The | ||
| 191 | largest C<expand> setting for any cell in a given column or row sets the value | ||
| 192 | used to distribute space to that column or row. | ||
| 193 | |||
| 194 | =back | ||
| 195 | |||
| 196 | =cut | ||
| 197 | |||
| 198 | method add | ||
| 199 | { | ||
| 200 | my ( $row, $col, $child, %opts ) = @_; | ||
| 201 | |||
| 202 | if( my $old_child = $_grid[$row][$col] ) { | ||
| 203 | $self->SUPER::remove( $old_child ); | ||
| 204 | } | ||
| 205 | |||
| 206 | $_max_col = $col if $col > $_max_col; | ||
| 207 | |||
| 208 | $_grid[$row][$col] = $child; | ||
| 209 | $self->SUPER::add( $child, | ||
| 210 | col_expand => $opts{col_expand} || 0, | ||
| 211 | row_expand => $opts{row_expand} || 0, | ||
| 212 | ); | ||
| 213 | } | ||
| 214 | |||
| 215 | =head2 remove | ||
| 216 | |||
| 217 | $gridbox->remove( $row, $col ); | ||
| 218 | |||
| 219 | Removes the child widget on display in the given cell. May shrink the grid if | ||
| 220 | this was the last child widget in the given row or column. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | method remove | ||
| 225 | { | ||
| 226 | my ( $row, $col ) = @_; | ||
| 227 | |||
| 228 | my $child = $_grid[$row][$col]; | ||
| 229 | undef $_grid[$row][$col]; | ||
| 230 | |||
| 231 | # Tidy up the row | ||
| 232 | my $max_col = 0; | ||
| 233 | foreach my $col ( reverse 0 .. $#{ $_grid[$row] } ) { | ||
| 234 | next if !defined $_grid[$row][$col]; | ||
| 235 | |||
| 236 | $max_col = $col+1; | ||
| 237 | last; | ||
| 238 | } | ||
| 239 | |||
| 240 | splice $_grid[$row]->@*, $max_col; | ||
| 241 | |||
| 242 | # Tidy up the grid | ||
| 243 | my $max_row = 0; | ||
| 244 | foreach my $row ( reverse 0 .. $#_grid ) { | ||
| 245 | next if !defined $_grid[$row] or !$_grid[$row]->@*; | ||
| 246 | |||
| 247 | $max_row = $row+1; | ||
| 248 | last; | ||
| 249 | } | ||
| 250 | |||
| 251 | splice @_grid, $max_row; | ||
| 252 | |||
| 253 | $_max_col = max map { $_ ? $#$_ : 0 } @_grid; | ||
| 254 | |||
| 255 | my $childrect = $child->window ? $child->window->rect : undef; | ||
| 256 | |||
| 257 | $self->SUPER::remove( $child ); | ||
| 258 | |||
| 259 | $self->window->expose( $childrect ) if $childrect; | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 get | ||
| 263 | |||
| 264 | $child = $gridbox->get( $row, $col ); | ||
| 265 | |||
| 266 | Returns the child widget at the given cell in the grid. If the row or column | ||
| 267 | index are beyond the bounds of the grid, or if there is no widget in the given | ||
| 268 | cell, returns C<undef>. | ||
| 269 | |||
| 270 | =cut | ||
| 271 | |||
| 272 | method get | ||
| 273 | { | ||
| 274 | my ( $row, $col ) = @_; | ||
| 275 | |||
| 276 | return undef if $row >= @_grid; | ||
| 277 | return $_grid[$row][$col]; | ||
| 278 | } | ||
| 279 | |||
| 280 | =head2 get_row | ||
| 281 | |||
| 282 | =head2 get_col | ||
| 283 | |||
| 284 | @children = $gridbox->get_row( $row ); | ||
| 285 | |||
| 286 | @children = $gridbox->get_col( $col ); | ||
| 287 | |||
| 288 | Convenient shortcut to call C<get> on an entire row or column of the grid. | ||
| 289 | |||
| 290 | =cut | ||
| 291 | |||
| 292 | method get_row | ||
| 293 | { | ||
| 294 | my ( $row ) = @_; | ||
| 295 | return map { $self->get( $row, $_ ) } 0 .. $self->colcount - 1; | ||
| 296 | } | ||
| 297 | |||
| 298 | method get_col | ||
| 299 | { | ||
| 300 | my ( $col ) = @_; | ||
| 301 | return map { $self->get( $_, $col ) } 0 .. $self->rowcount - 1; | ||
| 302 | } | ||
| 303 | |||
| 304 | =head2 insert_row | ||
| 305 | |||
| 306 | $gridbox->insert_row( $before_row, [ @children ] ); | ||
| 307 | |||
| 308 | Inserts a new row into the grid by moving the existing rows after it lower | ||
| 309 | down. Any child widgets in the referenced array will be set on the cells of | ||
| 310 | the new row, at an column corresponding to its index in the array. A child of | ||
| 311 | C<undef> will be skipped over. | ||
| 312 | |||
| 313 | Each element of the list should either be a widget object reference directly, | ||
| 314 | or an unblessed hash reference containing additional options. (See | ||
| 315 | L<Tickit::Widget/split_widget_opts>). | ||
| 316 | |||
| 317 | =cut | ||
| 318 | |||
| 319 | method insert_row | ||
| 320 | { | ||
| 321 | my ( $row, $children ) = @_; | ||
| 322 | |||
| 323 | splice @_grid, $row, 0, []; | ||
| 324 | |||
| 325 | foreach my $col ( 0 .. $#$children ) { | ||
| 326 | next unless my $arg = $children->[$col]; | ||
| 327 | |||
| 328 | $self->add( $row, $col, Tickit::Widget::split_widget_opts $arg ); | ||
| 329 | } | ||
| 330 | |||
| 331 | return $self; | ||
| 332 | } | ||
| 333 | |||
| 334 | =head2 insert_col | ||
| 335 | |||
| 336 | $gridbox->insert_col( $before_col, [ @children ] ); | ||
| 337 | |||
| 338 | Inserts a new column into the grid by moving the existing columns after it to | ||
| 339 | the right. Any child widgets in the referenced array will be set on the cells | ||
| 340 | of the new column, at a row corresponding to its index in the array. A child | ||
| 341 | of C<undef> will be skipped over. | ||
| 342 | |||
| 343 | Each child is specified as for C<insert_row>. | ||
| 344 | |||
| 345 | =cut | ||
| 346 | |||
| 347 | method insert_col | ||
| 348 | { | ||
| 349 | my ( $col, $children ) = @_; | ||
| 350 | |||
| 351 | $_max_col++; | ||
| 352 | |||
| 353 | foreach my $row ( 0 .. max( $self->rowcount, scalar @$children ) - 1 ) { | ||
| 354 | splice @{ $_grid[$row] //= [ ( undef ) x $col ] }, $col, 0, ( undef ); | ||
| 355 | |||
| 356 | next unless my $arg = $children->[$row]; | ||
| 357 | |||
| 358 | $self->add( $row, $col, Tickit::Widget::split_widget_opts $arg ); | ||
| 359 | } | ||
| 360 | |||
| 361 | return $self; | ||
| 362 | } | ||
| 363 | |||
| 364 | =head2 append_row | ||
| 365 | |||
| 366 | $gridbox->append_row( [ @children ] ); | ||
| 367 | |||
| 368 | Shortcut to inserting a new row after the end of the current grid. | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | method append_row | ||
| 373 | { | ||
| 374 | return $self->insert_row( $self->rowcount, @_ ); | ||
| 375 | } | ||
| 376 | |||
| 377 | =head2 append_col | ||
| 378 | |||
| 379 | $gridbox->append_col( [ @children ] ); | ||
| 380 | |||
| 381 | Shortcut to inserting a new column after the end of the current grid. | ||
| 382 | |||
| 383 | These four methods return the container widget instance itself making them | ||
| 384 | suitable to use as a chaining mutator; e.g. | ||
| 385 | |||
| 386 | my $container = Tickit::Widget::GridBox->new( ... ) | ||
| 387 | ->append_row( [ Tickit::Widget::Static->new( ... ), | ||
| 388 | Tickit::Widget::Static->new( ... ) ] ) | ||
| 389 | ->append_row( ... ); | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | method append_col | ||
| 394 | { | ||
| 395 | return $self->insert_col( $self->colcount, @_ ); | ||
| 396 | } | ||
| 397 | |||
| 398 | =head2 delete_row | ||
| 399 | |||
| 400 | $gridbox->delete_row( $row ); | ||
| 401 | |||
| 402 | Deletes a row of the grid by moving the existing rows after it higher up. | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | method delete_row | ||
| 407 | { | ||
| 408 | my ( $row ) = @_; | ||
| 409 | |||
| 410 | $self->remove( $row, $_ ) for 0 .. $self->colcount - 1; | ||
| 411 | |||
| 412 | splice @_grid, $row, 1, (); | ||
| 413 | $self->children_changed; | ||
| 414 | } | ||
| 415 | |||
| 416 | =head2 delete_col | ||
| 417 | |||
| 418 | $gridbox->delete_col( $col ); | ||
| 419 | |||
| 420 | Deletes a column of the grid by moving the existing columns after it to the | ||
| 421 | left. | ||
| 422 | |||
| 423 | =cut | ||
| 424 | |||
| 425 | method delete_col | ||
| 426 | { | ||
| 427 | my ( $col ) = @_; | ||
| 428 | |||
| 429 | $self->remove( $_, $col ) for 0 .. $self->rowcount - 1; | ||
| 430 | |||
| 431 | splice $_grid[$_]->@*, $col, 1, () for 0 .. $self->rowcount - 1; | ||
| 432 | $_max_col--; | ||
| 433 | $self->children_changed; | ||
| 434 | } | ||
| 435 | |||
| 436 | method reshape | ||
| 437 | { | ||
| 438 | my $win = $self->window or return; | ||
| 439 | |||
| 440 | my @row_buckets; | ||
| 441 | my @col_buckets; | ||
| 442 | |||
| 443 | my $max_row = $self->rowcount - 1; | ||
| 444 | my $max_col = $self->colcount - 1; | ||
| 445 | |||
| 446 | my ( $row_spacing, $col_spacing ) = $self->get_style_values(qw( row_spacing col_spacing )); | ||
| 447 | |||
| 448 | foreach my $row ( 0 .. $max_row ) { | ||
| 449 | push @row_buckets, { fixed => $row_spacing } if @row_buckets; | ||
| 450 | |||
| 451 | my $base = 0; | ||
| 452 | my $expand = 0; | ||
| 453 | |||
| 454 | foreach my $col ( 0 .. $max_col ) { | ||
| 455 | my $child = $_grid[$row][$col] or next; | ||
| 456 | |||
| 457 | $base = max $base, $child->requested_lines; | ||
| 458 | $expand = max $expand, $self->child_opts( $child )->{row_expand}; | ||
| 459 | } | ||
| 460 | |||
| 461 | push @row_buckets, { | ||
| 462 | row => $row, | ||
| 463 | base => $base, | ||
| 464 | expand => $expand, | ||
| 465 | }; | ||
| 466 | } | ||
| 467 | |||
| 468 | foreach my $col ( 0 .. $max_col ) { | ||
| 469 | push @col_buckets, { fixed => $col_spacing } if @col_buckets; | ||
| 470 | |||
| 471 | my $base = 0; | ||
| 472 | my $expand = 0; | ||
| 473 | |||
| 474 | foreach my $row ( 0 .. $max_row ) { | ||
| 475 | my $child = $_grid[$row][$col] or next; | ||
| 476 | |||
| 477 | $base = max $base, $child->requested_cols; | ||
| 478 | $expand = max $expand, $self->child_opts( $child )->{col_expand}; | ||
| 479 | } | ||
| 480 | |||
| 481 | push @col_buckets, { | ||
| 482 | col => $col, | ||
| 483 | base => $base, | ||
| 484 | expand => $expand, | ||
| 485 | }; | ||
| 486 | } | ||
| 487 | |||
| 488 | distribute( $win->lines, @row_buckets ); | ||
| 489 | distribute( $win->cols, @col_buckets ); | ||
| 490 | |||
| 491 | my @rows; | ||
| 492 | foreach ( @row_buckets ) { | ||
| 493 | $rows[$_->{row}] = [ $_->{start}, $_->{value} ] if defined $_->{row}; | ||
| 494 | } | ||
| 495 | |||
| 496 | my @cols; | ||
| 497 | foreach ( @col_buckets ) { | ||
| 498 | $cols[$_->{col}] = [ $_->{start}, $_->{value} ] if defined $_->{col}; | ||
| 499 | } | ||
| 500 | |||
| 501 | foreach my $row ( 0 .. $max_row ) { | ||
| 502 | foreach my $col ( 0 .. $max_col ) { | ||
| 503 | my $child = $_grid[$row][$col] or next; | ||
| 504 | |||
| 505 | # Don't try to use zero-sized rows or cols | ||
| 506 | next unless $rows[$row][1] and $cols[$col][1]; | ||
| 507 | |||
| 508 | my @geom = ( $rows[$row][0], $cols[$col][0], $rows[$row][1], $cols[$col][1] ); | ||
| 509 | |||
| 510 | if( my $childwin = $child->window ) { | ||
| 511 | $childwin->change_geometry( @geom ); | ||
| 512 | } | ||
| 513 | else { | ||
| 514 | $childwin = $win->make_sub( @geom ); | ||
| 515 | $child->set_window( $childwin ); | ||
| 516 | } | ||
| 517 | } | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | method render_to_rb | ||
| 522 | { | ||
| 523 | my ( $rb, $rect ) = @_; | ||
| 524 | |||
| 525 | $rb->eraserect( $rect ); | ||
| 526 | } | ||
| 527 | |||
| 528 | =head1 TODO | ||
| 529 | |||
| 530 | =over 4 | ||
| 531 | |||
| 532 | =item * | ||
| 533 | |||
| 534 | Add C<move_{row,col}> methods for re-ordering existing rows or columns | ||
| 535 | |||
| 536 | =item * | ||
| 537 | |||
| 538 | Make C<{insert,append,delete,move}> operations more efficient by deferring the | ||
| 539 | C<children_changed> call until they are done. | ||
| 540 | |||
| 541 | =back | ||
| 542 | |||
| 543 | =head1 AUTHOR | ||
| 544 | |||
| 545 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 546 | |||
| 547 | =cut | ||
| 548 | |||
| 549 | 0x55AA; |