File: HLine.pm

package info (click to toggle)
libtickit-widgets-perl 0.42-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 568 kB
  • sloc: perl: 5,636; makefile: 2
file content (116 lines) | stat: -rw-r--r-- 2,375 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk

use v5.20;
use warnings;
use Object::Pad 0.807;

package Tickit::Widget::HLine 0.42;
class Tickit::Widget::HLine :strict(params);

inherit Tickit::Widget;

use Tickit::Style;
use Tickit::RenderBuffer qw( LINE_SINGLE CAP_BOTH );

use constant WIDGET_PEN_FROM_STYLE => 1;

=head1 NAME

C<Tickit::Widget::HLine> - a widget displaying a horizontal line

=head1 SYNOPSIS

   use Tickit::Widget::VBox;
   use Tickit::Widget::HLine;

   my $vbox = Tickit::Widget::VBox->new;

   $vbox->add( ... );

   $vbox->add( Tickit::Widget::HLine->new );

   $vbox->add( ... );

   Tickit->new( root => $vbox )->run;

=head1 DESCRIPTION

This class provides a widget which displays a single horizontal line, using
line-drawing characters. It consumes the full width of the window given to it
by its parent. By default it is drawn in the vertical middle of the window but
this can be adjusted by the C<valign> style value.

=head1 STYLE

The default style pen is used as the widget pen.

The following additional style keys are used:

=over 4

=item line_style => INT

The style to draw the line in. Must be one of the C<LINE_*> constants from
L<Tickit::RenderBuffer>.

=item valign => NUM | STR

A fraction giving a position within the window to draw the line. Defaults to
C<0.5>, which puts it in the middle.

Symbolic names of C<top>, C<middle> and C<bottom> are also accepted.

=back

=cut

style_definition base =>
   line_style => LINE_SINGLE,
   valign     => 0.5;

style_redraw_keys qw( line_style valign );

my %symbolics = (
   top    => 0.0,
   middle => 0.5,
   bottom => 1.0,
);

=head1 CONSTRUCTOR

=cut

=head2 new

   $hline = Tickit::Widget::HLine->new( %args );

Constructs a new C<Tickit::Widget::HLine> object.

=cut

method lines { 1 }
method cols { 1 }

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

   $rb->eraserect( $rect );

   my ( $line_style, $valign ) = $self->get_style_values(qw( line_style valign ));
   $valign = $symbolics{$valign} if exists $symbolics{$valign};

   my $line = int( ( $rb->lines - 1 ) * $valign );
   $rb->hline_at( $line, 0, $rb->cols - 1, $line_style, $self->pen, CAP_BOTH );
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;