| 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, 2023 -- leonerd@leonerd.org.uk | ||
| 5 | |||
| 6 | use v5.20; | ||
| 7 | use warnings; | ||
| 8 | use Object::Pad 0.807; | ||
| 9 | |||
| 10 | package Tickit::Widget::HLine 0.42; | ||
| 11 | class Tickit::Widget::HLine :strict(params); | ||
| 12 | |||
| 13 | inherit Tickit::Widget; | ||
| 14 | |||
| 15 | use Tickit::Style; | ||
| 16 | use Tickit::RenderBuffer qw( LINE_SINGLE CAP_BOTH ); | ||
| 17 | |||
| 18 | use constant WIDGET_PEN_FROM_STYLE => 1; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | C<Tickit::Widget::HLine> - a widget displaying a horizontal line | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | use Tickit::Widget::VBox; | ||
| 27 | use Tickit::Widget::HLine; | ||
| 28 | |||
| 29 | my $vbox = Tickit::Widget::VBox->new; | ||
| 30 | |||
| 31 | $vbox->add( ... ); | ||
| 32 | |||
| 33 | $vbox->add( Tickit::Widget::HLine->new ); | ||
| 34 | |||
| 35 | $vbox->add( ... ); | ||
| 36 | |||
| 37 | Tickit->new( root => $vbox )->run; | ||
| 38 | |||
| 39 | =head1 DESCRIPTION | ||
| 40 | |||
| 41 | This class provides a widget which displays a single horizontal line, using | ||
| 42 | line-drawing characters. It consumes the full width of the window given to it | ||
| 43 | by its parent. By default it is drawn in the vertical middle of the window but | ||
| 44 | this can be adjusted by the C<valign> style value. | ||
| 45 | |||
| 46 | =head1 STYLE | ||
| 47 | |||
| 48 | The default style pen is used as the widget pen. | ||
| 49 | |||
| 50 | The following additional style keys are used: | ||
| 51 | |||
| 52 | =over 4 | ||
| 53 | |||
| 54 | =item line_style => INT | ||
| 55 | |||
| 56 | The style to draw the line in. Must be one of the C<LINE_*> constants from | ||
| 57 | L<Tickit::RenderBuffer>. | ||
| 58 | |||
| 59 | =item valign => NUM | STR | ||
| 60 | |||
| 61 | A fraction giving a position within the window to draw the line. Defaults to | ||
| 62 | C<0.5>, which puts it in the middle. | ||
| 63 | |||
| 64 | Symbolic names of C<top>, C<middle> and C<bottom> are also accepted. | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | style_definition base => | ||
| 71 | line_style => LINE_SINGLE, | ||
| 72 | valign => 0.5; | ||
| 73 | |||
| 74 | style_redraw_keys qw( line_style valign ); | ||
| 75 | |||
| 76 | my %symbolics = ( | ||
| 77 | top => 0.0, | ||
| 78 | middle => 0.5, | ||
| 79 | bottom => 1.0, | ||
| 80 | ); | ||
| 81 | |||
| 82 | =head1 CONSTRUCTOR | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | =head2 new | ||
| 87 | |||
| 88 | $hline = Tickit::Widget::HLine->new( %args ); | ||
| 89 | |||
| 90 | Constructs a new C<Tickit::Widget::HLine> object. | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | method lines { 1 } | ||
| 95 | method cols { 1 } | ||
| 96 | |||
| 97 | method render_to_rb | ||
| 98 | { | ||
| 99 | my ( $rb, $rect ) = @_; | ||
| 100 | |||
| 101 | $rb->eraserect( $rect ); | ||
| 102 | |||
| 103 | my ( $line_style, $valign ) = $self->get_style_values(qw( line_style valign )); | ||
| 104 | $valign = $symbolics{$valign} if exists $symbolics{$valign}; | ||
| 105 | |||
| 106 | my $line = int( ( $rb->lines - 1 ) * $valign ); | ||
| 107 | $rb->hline_at( $line, 0, $rb->cols - 1, $line_style, $self->pen, CAP_BOTH ); | ||
| 108 | } | ||
| 109 | |||
| 110 | =head1 AUTHOR | ||
| 111 | |||
| 112 | Paul Evans <leonerd@leonerd.org.uk> | ||
| 113 | |||
| 114 | =cut | ||
| 115 | |||
| 116 | 0x55AA; |