| 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, 2009-2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::Static 0.58; | ||
| 11 | class Tickit::Widget::Static :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget; | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | |||
| 17 | use Tickit::WidgetRole::Alignable name => 'align', dir => 'h'; | ||
| 18 | use Tickit::WidgetRole::Alignable name => 'valign', dir => 'v'; | ||
| 19 | |||
| 20 | use List::Util qw( max ); | ||
| 21 | use Tickit::Utils qw( textwidth substrwidth ); | ||
| 22 | |||
| 23 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 24 | |||
| 25 | =head1 NAME | ||
| 26 | |||
| 27 | C<Tickit::Widget::Static> - a widget displaying static text | ||
| 28 | |||
| 29 | =head1 SYNOPSIS | ||
| 30 | |||
| 31 | use Tickit; | ||
| 32 | use Tickit::Widget::Static; | ||
| 33 | |||
| 34 | my $hello = Tickit::Widget::Static->new( | ||
| 35 | text => "Hello, world", | ||
| 36 | align => "centre", | ||
| 37 | valign => "middle", | ||
| 38 | ); | ||
| 39 | |||
| 40 | Tickit->new( root => $hello )->run; | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This class provides a widget which displays a single piece of static text. The | ||
| 45 | text may contain more than one line, separated by linefeed (C<\n>) characters. | ||
| 46 | No other control sequences are allowed in the string. | ||
| 47 | |||
| 48 | =head1 STYLE | ||
| 49 | |||
| 50 | The default style pen is used as the widget pen. | ||
| 51 | |||
| 52 | Note that while the widget pen is mutable and changes to it will result in | ||
| 53 | immediate redrawing, any changes made will be lost if the widget style is | ||
| 54 | changed. | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | =head1 CONSTRUCTOR | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | =head2 new | ||
| 63 | |||
| 64 | $static = Tickit::Widget::Static->new( %args ); | ||
| 65 | |||
| 66 | Constructs a new C<Tickit::Widget::Static> object. | ||
| 67 | |||
| 68 | Takes the following named arguments in addition to those taken by the base | ||
| 69 | L<Tickit::Widget> constructor: | ||
| 70 | |||
| 71 | =over 8 | ||
| 72 | |||
| 73 | =item text => STRING | ||
| 74 | |||
| 75 | The text to display | ||
| 76 | |||
| 77 | =item align => FLOAT|STRING | ||
| 78 | |||
| 79 | Optional. Defaults to C<0.0> if unspecified. | ||
| 80 | |||
| 81 | =item valign => FLOAT|STRING | ||
| 82 | |||
| 83 | Optional. Defaults to C<0.0> if unspecified. | ||
| 84 | |||
| 85 | =item on_click => CODE | ||
| 86 | |||
| 87 | Optional. Defaults to C<undef> if unspecified. | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | For more details see the accessors below. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | field @_lines; | ||
| 96 | field $_on_click :reader :writer :param = undef; | ||
| 97 | |||
| 98 | ADJUST :params ( | ||
| 99 | :$text = undef, | ||
| 100 | :$align = 0, | ||
| 101 | :$valign = 0, | ||
| 102 | ) { | ||
| 103 | $self->set_text( $text ); | ||
| 104 | $self->set_align( $align ); | ||
| 105 | $self->set_valign( $valign ); | ||
| 106 | } | ||
| 107 | |||
| 108 | =head1 ACCESSORS | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | method lines | ||
| 113 | { | ||
| 114 | return scalar @_lines; | ||
| 115 | } | ||
| 116 | |||
| 117 | method cols | ||
| 118 | { | ||
| 119 | return max map { textwidth $_ } @_lines; | ||
| 120 | } | ||
| 121 | |||
| 122 | =head2 text | ||
| 123 | |||
| 124 | $text = $static->text; | ||
| 125 | |||
| 126 | =cut | ||
| 127 | |||
| 128 | method text | ||
| 129 | { | ||
| 130 | return join "\n", @_lines; | ||
| 131 | } | ||
| 132 | |||
| 133 | =head2 set_text | ||
| 134 | |||
| 135 | $static->set_text( $text ); | ||
| 136 | |||
| 137 | Accessor for C<text> property; the actual text on display in the widget | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | method set_text | ||
| 142 | { | ||
| 143 | my ( $text ) = @_; | ||
| 144 | |||
| 145 | my $waslines = $self->lines; | ||
| 146 | my $wascols = $self->cols; | ||
| 147 | |||
| 148 | @_lines = split m/\n/, $text; | ||
| 149 | # split on empty string returns empty list | ||
| 150 | @_lines = ( "" ) if !@_lines; | ||
| 151 | |||
| 152 | $self->resized if $self->lines != $waslines or $self->cols != $wascols; | ||
| 153 | |||
| 154 | $self->redraw; | ||
| 155 | } | ||
| 156 | |||
| 157 | =head2 align | ||
| 158 | |||
| 159 | =head2 set_align | ||
| 160 | |||
| 161 | $align = $static->align; | ||
| 162 | |||
| 163 | $static->set_align( $align ); | ||
| 164 | |||
| 165 | Accessor for horizontal alignment value. | ||
| 166 | |||
| 167 | Gives a value in the range from C<0.0> to C<1.0> to align the text display | ||
| 168 | within the window. If the window is larger than the width of the text, it will | ||
| 169 | be aligned according to this value; with C<0.0> on the left, C<1.0> on the | ||
| 170 | right, and other values inbetween. | ||
| 171 | |||
| 172 | See also L<Tickit::WidgetRole::Alignable>. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | # generated accessor | ||
| 177 | |||
| 178 | method render_to_rb | ||
| 179 | { | ||
| 180 | my ( $rb, $rect ) = @_; | ||
| 181 | |||
| 182 | my $win = $self->window; | ||
| 183 | |||
| 184 | $rb->erase_at( $_, $rect->left, $rect->cols ) for $rect->linerange; | ||
| 185 | |||
| 186 | my $cols = $win->cols; | ||
| 187 | my ( $above, $lines ) = $self->_valign_allocation( $self->lines, $win->lines ); | ||
| 188 | |||
| 189 | foreach my $line ( 0 .. $lines - 1 ) { | ||
| 190 | my $text = $_lines[$line]; | ||
| 191 | |||
| 192 | my ( $left, $textwidth ) = $self->_align_allocation( textwidth( $text ), $cols ); | ||
| 193 | |||
| 194 | $rb->text_at( $above + $line, $left, substrwidth( $text, 0, $textwidth ) ); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | method on_mouse | ||
| 199 | { | ||
| 200 | my ( $args ) = @_; | ||
| 201 | |||
| 202 | return unless $args->type eq "press" and $args->button == 1; | ||
| 203 | return unless $_on_click; | ||
| 204 | |||
| 205 | $_on_click->( $self, $args->line, $args->col ); | ||
| 206 | } | ||
| 207 | |||
| 208 | =head1 AUTHOR | ||
| 209 | |||
| 210 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | 0x55AA; |