Alt-Tickit-Widgets-ObjectPad

 view release on metacpan or  search on metacpan

lib/Tickit/Widget/GridBox.pm  view on Meta::CPAN

method children
{
   map {
      my $r = $_;
      map {
         $_grid[$r][$_] ? ( $_grid[$r][$_] ) : ()
      } 0 .. $_max_col
   } 0.. $#_grid;
}

=head1 METHODS

=cut

=head2 $count = $gridbox->rowcount

=head2 $count = $gridbox->colcount

Returns the number of rows or columns in the grid.

=cut

method rowcount
{
   return scalar @_grid;
}

method colcount
{
   return $_max_col + 1;
}

=head2 $gridbox->add( $row, $col, $child, %opts )

Sets the child widget to display in the given grid cell. Cells do not need to
be explicitly constructed; the grid will automatically expand to the size
required. This method can also be used to replace an existing child at the
given cell location. To remove a cell entirely, use the C<remove> method.

The following options are recognised:

=over 8

=item col_expand => INT

=item row_expand => INT

Values for the C<expand> setting for this column or row of the table. The
largest C<expand> setting for any cell in a given column or row sets the value
used to distribute space to that column or row.

=back

=cut

method add
{
   my ( $row, $col, $child, %opts ) = @_;

   if( my $old_child = $_grid[$row][$col] ) {
      $self->SUPER::remove( $old_child );
   }

   $_max_col = $col if $col > $_max_col;

   $_grid[$row][$col] = $child;
   $self->SUPER::add( $child,
      col_expand => $opts{col_expand} || 0,
      row_expand => $opts{row_expand} || 0,
   );
}

=head2 $gridbox->remove( $row, $col )

Removes the child widget on display in the given cell. May shrink the grid if
this was the last child widget in the given row or column.

=cut

method remove
{
   my ( $row, $col ) = @_;

   my $child = $_grid[$row][$col];
   undef $_grid[$row][$col];

   # Tidy up the row
   my $max_col = 0;
   foreach my $col ( reverse 0 .. $#{ $_grid[$row] } ) {
      next if !defined $_grid[$row][$col];

      $max_col = $col+1;
      last;
   }

   splice @{ $_grid[$row] }, $max_col;

   # Tidy up the grid
   my $max_row = 0;
   foreach my $row ( reverse 0 .. $#_grid ) {
      next if !defined $_grid[$row] or !@{ $_grid[$row] };

      $max_row = $row+1;
      last;
   }

   splice @_grid, $max_row;

   $_max_col = max map { $_ ? $#$_ : 0 } @_grid;

   my $childrect = $child->window ? $child->window->rect : undef;

   $self->SUPER::remove( $child );

   $self->window->expose( $childrect ) if $childrect;
}

=head2 $child = $gridbox->get( $row, $col )

Returns the child widget at the given cell in the grid. If the row or column
index are beyond the bounds of the grid, or if there is no widget in the given
cell, returns C<undef>.

=cut

method get
{
   my ( $row, $col ) = @_;

   return undef if $row >= @_grid;
   return $_grid[$row][$col];
}

=head2 @children = $gridbox->get_row( $row )

=head2 @children = $gridbox->get_col( $col )

Convenient shortcut to call C<get> on an entire row or column of the grid.

=cut

method get_row
{
   my ( $row ) = @_;
   return map { $self->get( $row, $_ ) } 0 .. $self->colcount - 1;
}

method get_col
{
   my ( $col ) = @_;
   return map { $self->get( $_, $col ) } 0 .. $self->rowcount - 1;
}

=head2 $gridbox->insert_row( $before_row, [ @children ] )

Inserts a new row into the grid by moving the existing rows after it lower
down. Any child widgets in the referenced array will be set on the cells of
the new row, at an column corresponding to its index in the array. A child of
C<undef> will be skipped over.

=cut

method insert_row
{
   my ( $row, $children ) = @_;

   splice @_grid, $row, 0, [];

   foreach my $col ( 0 .. $#$children ) {
      next unless my $child = $children->[$col];

      $self->add( $row, $col, $child ); # No options
   }



( run in 0.539 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )