| 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 | # Original render code by Tom Molesworth | ||
| 6 | |||
| 7 | use v5.20; | ||
| 8 | use warnings; | ||
| 9 | use Object::Pad 0.807; | ||
| 10 | |||
| 11 | package Tickit::Widget::Placegrid 0.42; | ||
| 12 | class Tickit::Widget::Placegrid :strict(params); | ||
| 13 | |||
| 14 | inherit Tickit::Widget; | ||
| 15 | |||
| 16 | use Tickit::Style; | ||
| 17 | use Tickit::RenderBuffer qw( LINE_SINGLE LINE_THICK ); | ||
| 18 | |||
| 19 | use Tickit::Utils qw( textwidth ); | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | C<Tickit::Widget::Placegrid> - a placeholder grid display | ||
| 24 | |||
| 25 | =head1 DESCRIPTION | ||
| 26 | |||
| 27 | This class provides a widget which displays a simple grid pattern in its | ||
| 28 | display area, and prints the size of the area in its centre. | ||
| 29 | |||
| 30 | It is intended as a placeholder for other widget code while applications are | ||
| 31 | under development. It easily allows a container to have a child that can take | ||
| 32 | any size, and will keep its window area painted (so avoiding bugs caused by | ||
| 33 | "empty" containers that do not clear unused child areas). | ||
| 34 | |||
| 35 | =head1 STYLE | ||
| 36 | |||
| 37 | The default style pen is used as the widget pen. The following style pen | ||
| 38 | prefixes are also used: | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =item grid => PEN | ||
| 43 | |||
| 44 | The pen used to render the grid lines | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | style_definition base => | ||
| 51 | fg => "white", | ||
| 52 | grid_fg => "blue"; | ||
| 53 | |||
| 54 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 55 | |||
| 56 | =head1 CONSTRUCTOR | ||
| 57 | |||
| 58 | =head2 $placegrid = Tickit::Widget::Placegrid->new( %args ) | ||
| 59 | |||
| 60 | Constructs a new C<Tickit::Widget::Placegrid> object. | ||
| 61 | |||
| 62 | Takes the following named arguments. | ||
| 63 | |||
| 64 | =over 8 | ||
| 65 | |||
| 66 | =item title => STR | ||
| 67 | |||
| 68 | Optional. If provided, prints an identifying title just above the box | ||
| 69 | dimensions in the centre of the grid. | ||
| 70 | |||
| 71 | =back | ||
| 72 | |||
| 73 | =cut | ||
| 74 | |||
| 75 | field $_title :param = undef; | ||
| 76 | |||
| 77 | method lines { 1 } | ||
| 78 | method cols { 1 } | ||
| 79 | |||
| 80 | method render_to_rb | ||
| 81 | { | ||
| 82 | my ( $rb, $rect ) = @_; | ||
| 83 | |||
| 84 | $rb->clear($self->pen); | ||
| 85 | |||
| 86 | my ($w, $h) = map $self->window->$_ - 1, qw(cols lines); | ||
| 87 | |||
| 88 | $rb->setpen($self->get_style_pen("grid")); | ||
| 89 | $rb->hline_at(0, 0, $w, LINE_THICK); | ||
| 90 | $rb->hline_at($h, 0, $w, LINE_THICK); | ||
| 91 | $rb->hline_at($h / 2, 0, $w, LINE_SINGLE); | ||
| 92 | $rb->vline_at(0, $h, 0, LINE_THICK); | ||
| 93 | $rb->vline_at(0, $h, $w, LINE_THICK); | ||
| 94 | $rb->vline_at(0, $h, $w / 2, LINE_SINGLE); | ||
| 95 | |||
| 96 | $rb->setpen($self->pen); | ||
| 97 | |||
| 98 | if(defined(my $title = $_title)) { | ||
| 99 | $rb->text_at(($h / 2) - 1, (1 + $w - textwidth($title)) / 2, $title); | ||
| 100 | } | ||
| 101 | |||
| 102 | my $txt = '(' . $self->window->cols . ',' . $self->window->lines . ')'; | ||
| 103 | $rb->text_at($h / 2, (1 + $w - textwidth($txt)) / 2, $txt); | ||
| 104 | } | ||
| 105 | |||
| 106 | =head1 AUTHOR | ||
| 107 | |||
| 108 | Original rendering code by Tom Molesworth <cpan@entitymodel.com> | ||
| 109 | |||
| 110 | Widget wrapping by Paul Evans <leonerd@leonerd.org.uk> | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | 0x55AA; |