Tickit-Widgets

 view release on metacpan or  search on metacpan

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


=back

For more details see the accessors below.

=cut

field @_lines;
field $_on_click :reader :writer :param = undef;

ADJUST :params (
   :$text   = undef,
   :$align  = 0,
   :$valign = 0,
) {
   $self->set_text( $text );
   $self->set_align( $align );
   $self->set_valign( $valign );
}

=head1 ACCESSORS

=cut

method lines
{
   return scalar @_lines;
}

method cols
{
   return max map { textwidth $_ } @_lines;
}

=head2 text

   $text = $static->text;

=cut

method text
{
   return join "\n", @_lines;
}

=head2 set_text

   $static->set_text( $text );

Accessor for C<text> property; the actual text on display in the widget

=cut

method set_text
{
   my ( $text ) = @_;

   my $waslines = $self->lines;
   my $wascols  = $self->cols;

   @_lines = split m/\n/, $text;
   # split on empty string returns empty list
   @_lines = ( "" ) if !@_lines;

   $self->resized if $self->lines != $waslines or $self->cols != $wascols;

   $self->redraw;
}

=head2 align

=head2 set_align

   $align = $static->align;

   $static->set_align( $align );

Accessor for horizontal alignment value.

Gives a value in the range from C<0.0> to C<1.0> to align the text display
within the window. If the window is larger than the width of the text, it will
be aligned according to this value; with C<0.0> on the left, C<1.0> on the
right, and other values inbetween.

See also L<Tickit::WidgetRole::Alignable>.

=cut

# generated accessor

method render_to_rb
{
   my ( $rb, $rect ) = @_;

   my $win = $self->window;

   $rb->erase_at( $_, $rect->left, $rect->cols ) for $rect->linerange;

   my $cols = $win->cols;
   my ( $above, $lines ) = $self->_valign_allocation( $self->lines, $win->lines );

   foreach my $line ( 0 .. $lines - 1 ) {
      my $text = $_lines[$line];

      my ( $left, $textwidth ) = $self->_align_allocation( textwidth( $text ), $cols );

      $rb->text_at( $above + $line, $left, substrwidth( $text, 0, $textwidth ) );
   }
}

method on_mouse
{
   my ( $args ) = @_;

   return unless $args->type eq "press" and $args->button == 1;
   return unless $_on_click;

   $_on_click->( $self, $args->line, $args->col );
}

=head1 AUTHOR



( run in 1.886 second using v1.01-cache-2.11-cpan-71847e10f99 )