blib/lib/Curses/UI/Label.pm

filter: Statementstmt 77.0% 77 / 100 Branchbran 45.4% 20 / 44 Conditioncond 30.0% 3 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 61.1% 11 / 18 Podpod 84.6% 11 / 13 totaltotal 63.8% 122 / 191 CC 28 SCAR 22.2
CC
28
Cov
65%
draw
17.9
new
11.1
compute_xpos
8.7
CRAP 9.2
« blib/lib/Curses/UI/Language.pm ↑ Summary blib/lib/Curses/UI/Dialog/Status.pm »
1 # ----------------------------------------------------------------------
2 # Curses::UI::Label
3 #
4 # (c) 2001-2002 by Maurice Makaay. All rights reserved.
5 # This file is part of Curses::UI. Curses::UI is free software.
6 # You can redistribute it and/or modify it under the same terms
7 # as perl itself.
8 #
9 # Currently maintained by Marcus Thiesen
10 # e-mail: marcus@cpan.thiesenweb.de
11 # ----------------------------------------------------------------------
12  
13 # TODO: fix dox
14  
15 package Curses::UI::Label;
16  
17 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Curses;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Curses::UI::Widget;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Curses::UI::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use vars qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
23 $VERSION
24 @ISA
25 1 );
26  
27 1 $VERSION = '1.11';
28  
29 1 @ISA = qw(
30 Curses::UI::Widget
31 );
32  
33 sub new ()
34 1 ▶{
Subroutine new: called CC=9 SCAR=24.0
Pod documented
35 1 my $class = shift;
36  
37 1 my %userargs = @_;
38 1 keys_to_lowercase(\%userargs);
39  
40 1 my %args = (
41 -parent => undef, # the parent window
42 -width => undef, # the width of the label
43 -height => undef, # the height of the label
44 -x => 0, # the hor. pos. rel. to the parent
45 -y => 0, # the vert. pos. rel. to the parent
46 -text => undef, # the text to show
47 -textalignment => undef, # left / middle / right
48 -bold => 0, # Special attributes
49 -reverse => 0,
50 -underline => 0,
51 -dim => 0,
52 -blink => 0,
53 -paddingspaces => 0, # Pad text with spaces?
54 -bg => -1,
55 -fg => -1,
56  
57 %userargs,
58  
59 -nocursor => 1, # This widget uses no cursor
60 -focusable => 0, # This widget can't be focused
61 );
62  
63 # Get the text dimension if -width or -height is undefined.
64 1 my @text_dimension = (undef,1);
65 1 ▶ unless (defined $args{-width} and defined $args{-height}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"-width"} and defined $args{"-height"}) 1 0 1
Condition33%1/3
defined $args{"-width"} and defined $args{"-height"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $args{"-width"} and defined $args{"-height"}
defined $args{"-width"} defined $args{"-height"}
Truth table33%1/3
A and B
A= defined $args{"-width"}B= defined $args{"-height"}
ABresult
0X0
100
111
66 @text_dimension = text_dimension($args{-text})
67 1 ▶ if defined $args{-text};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"-text"} 0 1 1
68 }
69  
70 # If the -height is not set, determine the height
71 # using the initial contents of the -text.
72 1 ▶ if (not defined $args{-height})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"-height"}) 1 0 1
73 {
74 1 my $l = $text_dimension[1];
75 1 ▶ $l = 1 if $l <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $l <= 0 0 1 1
76 1 $args{-height} = height_by_windowscrheight($l, %args);
77 }
78  
79 # No width given? Then make the width the same size
80 # as the text. No initial text? Then let
81 # Curses::UI::Widget figure it out.
82 $args{-width} = width_by_windowscrwidth($text_dimension[0], %args)
83 1 ▶ unless defined $args{-width} or not defined $args{-text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"-width"} or not defined $args{"-text"} 0 1 1
Condition33%1/3
defined $args{"-width"} or not defined $args{"-text"}
l!l&&r!l&&!r
010
MC/DC0%0/2
defined $args{"-width"} or not defined $args{"-text"}
defined $args{"-width"} not defined $args{"-text"}
Truth table33%1/3
A or not(B)
A= defined $args{"-width"}B= not defined $args{"-text"}
ABresult
1X1
011
000
84  
85 # If no text was defined (how silly...) we define an empty string.
86 1 ▶ $args{-text} = '' unless defined $args{-text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"-text"} 1 0 1
87  
88 # Create the widget.
89 1 my $this = $class->SUPER::new( %args );
90  
91 1 $this->layout();
92  
93 1 return $this;
94 }
95  
96 sub layout()
97 2 ▶{
Subroutine layout: called CC=2 SCAR=7.1
Pod documented
98 2 my $this = shift;
99 2 ▶ $this->SUPER::layout or return;
Branch: 1 branch
BranchTrueFalseTotal
$this->SUPER::layout or return 2 0 2
100 2 return $this;
101 }
102  
103  
104 0 ▶sub bold ($;$) { shift()->set_attribute('-bold', shift()) }
Subroutine bold: not called CC=1 SCAR=6.9
Pod documented
105 0 ▶sub reverse ($;$) { shift()->set_attribute('-reverse', shift()) }
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
106 0 ▶sub underline ($;$) { shift()->set_attribute('-underline', shift()) }
Subroutine underline: not called CC=1 SCAR=6.9
Pod documented
107 0 ▶sub dim ($;$) { shift()->set_attribute('-dim', shift()) }
Subroutine dim: not called CC=1 SCAR=6.9
Pod documented
108 0 ▶sub blink ($;$) { shift()->set_attribute('-blink', shift()) }
Subroutine blink: not called CC=1 SCAR=6.9
Pod documented
109  
110 sub set_attribute($$;)
111 0 ▶{
Subroutine set_attribute: not called CC=2 SCAR=17.9
Pod undocumented
112 0 my $this = shift;
113 0 my $attribute = shift;
114 0 ▶ my $value = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
115  
116 0 $this->{$attribute} = $value;
117 0 $this->intellidraw;
118  
119 0 return $this;
120 }
121  
122  
123  
124 sub text($;$)
125 2 ▶{
Subroutine text: called CC=2 SCAR=6.9
Pod documented
126 2 my $this = shift;
127 2 my $text = shift;
128  
129 2 ▶ if (defined $text)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 1 1 2
130 {
131 1 $this->{-text} = $text;
132 1 $this->intellidraw;
133 1 return $this;
134 } else {
135 1 return $this->{-text};
136 }
137 }
138  
139 1 ▶sub get() { shift()->text }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
140  
141 sub textalignment($;)
142 0 ▶{
Subroutine textalignment: not called CC=1 SCAR=6.9
Pod documented
143 0 my $this = shift;
144 0 my $value = shift;
145 0 $this->{-textalignment} = $value;
146 0 $this->intellidraw;
147 0 return $this;
148 }
149  
150 sub compute_xpos()
151 1 ▶{
Subroutine compute_xpos: called CC=5 SCAR=21.6
Pod undocumented
152 1 my $this = shift;
153 1 my $line = shift;
154  
155 # Compute the x location of the text.
156 1 my $xpos = 0;
157 1 ▶ if (defined $this->{-textalignment})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"-textalignment"}) 0 1 1
158 {
159 0 ▶ if ($this->{-textalignment} eq 'right') {
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'-textalignment'} eq 'right') { } 0 0 0
elsif ($this->{'-textalignment'} eq 'middle') { } 0 0 0
160 0 $xpos = $this->canvaswidth - length($line);
161 } elsif ($this->{-textalignment} eq 'middle') {
162 0 $xpos = int (($this->canvaswidth-length($line))/2);
163 }
164 }
165 1 ▶ $xpos = 0 if $xpos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $xpos < 0 0 1 1
166 1 return $xpos;
167 }
168  
169 sub draw(;$)
170 1 ▶{
Subroutine draw: called CC=13 SCAR=28.9
Pod documented
171 1 my $this = shift;
172 1 ▶ my $no_doupdate = shift || 0;
Condition50%1/2
shift() || 0
l!l
10
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
173  
174 # Draw the widget.
175 1 ▶ $this->SUPER::draw(1) or return $this;
Branch: 1 branch
BranchTrueFalseTotal
$this->SUPER::draw(1) or return $this 1 0 1
176  
177 # Clear all attributes.
178 1 $this->{-canvasscr}->attroff(A_REVERSE);
179 1 $this->{-canvasscr}->attroff(A_BOLD);
180 1 $this->{-canvasscr}->attroff(A_UNDERLINE);
181 1 $this->{-canvasscr}->attroff(A_BLINK);
182 1 $this->{-canvasscr}->attroff(A_DIM);
183  
184 # Set wanted attributes.
185 1 ▶ $this->{-canvasscr}->attron(A_REVERSE) if $this->{-reverse};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"-reverse"} 0 1 1
186 1 ▶ $this->{-canvasscr}->attron(A_BOLD) if $this->{-bold};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"-bold"} 0 1 1
187 1 ▶ $this->{-canvasscr}->attron(A_UNDERLINE) if $this->{-underline};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"-underline"} 0 1 1
188 1 ▶ $this->{-canvasscr}->attron(A_BLINK) if $this->{-blink};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"-blink"} 0 1 1
189 1 ▶ $this->{-canvasscr}->attron(A_DIM) if $this->{-dim};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"-dim"} 0 1 1
190  
191 # Let there be color
192 1 ▶ if ($Curses::UI::color_support) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Curses::UI::color_support) 0 1 1
193 0 my $co = $Curses::UI::color_object;
194 my $pair = $co->get_color_pair(
195 $this->{-fg},
196 0 $this->{-bg});
197  
198 0 $this->{-canvasscr}->attron(COLOR_PAIR($pair));
199  
200 }
201  
202 # Draw the text. Clip it if it is too long.
203 1 my $ypos = 0;
204 1 my $split = split_to_lines($this->{-text});
205 1 foreach my $line (@$split)
206 {
207 1 ▶ if (length($line) > $this->canvaswidth) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $line > $this->canvaswidth) { } 1 0 1
elsif ($this->{'-paddingspaces'}) { } 0 0 0
208 # Break text
209 1 $line = substr($line, 0, $this->canvaswidth);
210 1 $line =~ s/.$/\$/;
211 } elsif ($this->{-paddingspaces}) {
212 0 $this->{-canvasscr}->addstr($ypos, 0, " "x$this->canvaswidth);
213 }
214  
215 1 my $xpos = $this->compute_xpos($line);
216 1 $this->{-canvasscr}->addstr($ypos, $xpos, $line);
217  
218 1 $ypos++;
219 }
220  
221 1 $this->{-canvasscr}->noutrefresh;
222 1 ▶ doupdate() unless $no_doupdate;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_doupdate 0 1 1
223  
224 1 return $this;
225 }
226  
227  
228  
229  
230 1 1;
231  
232  
233 =pod
234  
235 =head1 NAME
236  
237 Curses::UI::Label - Create and manipulate label widgets
238  
239 =head1 CLASS HIERARCHY
240  
241 Curses::UI::Widget
242 |
243 +----Curses::UI::Label
244  
245  
246  
247 =head1 SYNOPSIS
248  
249 use Curses::UI;
250 my $cui = new Curses::UI;
251 my $win = $cui->add('window_id', 'Window');
252  
253 my $label = $win->add(
254 'mylabel', 'Label',
255 -text => 'Hello, world!',
256 -bold => 1,
257 );
258  
259 $label->draw;
260  
261  
262  
263 =head1 DESCRIPTION
264  
265 Curses::UI::Label is a widget that shows a textstring.
266 This textstring can be drawn using these special
267 features: bold, dimmed, reverse, underlined, and blinking.
268  
269 See exampes/demo-Curses::UI::Label in the distribution
270 for a short demo.
271  
272  
273  
274 =head1 STANDARD OPTIONS
275  
276 B<-parent>, B<-x>, B<-y>, B<-width>, B<-height>,
277 B<-pad>, B<-padleft>, B<-padright>, B<-padtop>, B<-padbottom>,
278 B<-ipad>, B<-ipadleft>, B<-ipadright>, B<-ipadtop>, B<-ipadbottom>,
279 B<-title>, B<-titlefullwidth>, B<-titlereverse>, B<-onfocus>,
280 B<-onblur>
281  
282 For an explanation of these standard options, see
283 L<Curses::UI::Widget|Curses::UI::Widget>.
284  
285  
286  
287  
288 =head1 WIDGET-SPECIFIC OPTIONS
289  
290 =over 4
291  
292 =item * B<-height> < VALUE >
293  
294 If you do not define B<-height>, the label will compute
295 its needed height using the initial B<-text>.
296  
297 =item * B<-text> < TEXT >
298  
299 This will set the text on the label to TEXT.
300  
301 =item * B<-textalignment> < VALUE >
302  
303 This option controls how the text should be aligned inside
304 the label. VALUE can be 'left', 'middle' and 'right'. The
305 default value for this option is 'left'.
306  
307 =item * B<-paddingspaces> < BOOLEAN >
308  
309 This option controls if padding spaces should be added
310 to the text if the text does not fill the complete width
311 of the widget. The default value for BOOLEAN is false.
312 An example use of this option is:
313  
314 $win->add(
315 'label', 'Label',
316 -width => -1,
317 -paddingspaces => 1,
318 -text => 'A bit of text',
319 );
320  
321 This will create a label that fills the complete width of
322 your screen and which will be completely in reverse font
323 (also the part that has no text on it). See the demo
324 in the distribution (examples/demo-Curses::UI::Label)
325 for a clear example of this)
326  
327 =item * B<-bold> < BOOLEAN >
328  
329 If BOOLEAN is true, text on the label will be drawn in
330 a bold font.
331  
332 =item * B<-dim> < BOOLEAN >
333  
334 If BOOLEAN is true, text on the label will be drawn in
335 a dim font.
336  
337 =item * B<-reverse> < BOOLEAN >
338  
339 If BOOLEAN is true, text on the label will be drawn in
340 a reverse font.
341  
342 =item * B<-underline> < BOOLEAN >
343  
344 If BOOLEAN is true, text on the label will be drawn in
345 an underlined font.
346  
347 =item * B<-blink> < BOOLEAN >
348  
349 If BOOLEAN is option is true, text on the label will be
350 drawn in a blinking font.
351  
352 =back
353  
354  
355  
356  
357 =head1 METHODS
358  
359 =over 4
360  
361 =item * B<new> ( OPTIONS )
362  
363 =item * B<layout> ( )
364  
365 =item * B<draw> ( BOOLEAN )
366  
367 =item * B<intellidraw> ( )
368  
369 =item * B<focus> ( )
370  
371 =item * B<onFocus> ( CODEREF )
372  
373 =item * B<onBlur> ( CODEREF )
374  
375 These are standard methods. See L<Curses::UI::Widget|Curses::UI::Widget>
376 for an explanation of these.
377  
378 =item * B<bold> ( BOOLEAN )
379  
380 =item * B<dim> ( BOOLEAN )
381  
382 =item * B<reverse> ( BOOLEAN )
383  
384 =item * B<underline> ( BOOLEAN )
385  
386 =item * B<blink> ( BOOLEAN )
387  
388 These methods can be used to control the font in which the text on
389 the label is drawn, after creating the widget. The font option
390 will be turned on for a true value of BOOLEAN.
391  
392 =item * B<textalignment> ( VALUE )
393  
394 Set the textalignment. VALUE can be 'left',
395 'middle' or 'right'.
396  
397 =item * B<text> ( [TEXT] )
398  
399 Without the TEXT argument, this method will return the current
400 text of the widget. With a TEXT argument, the text on the widget
401 will be set to TEXT.
402  
403 =item * B<get> ( )
404  
405 This will call the B<text> method without any argument and thus
406 it will return the current text of the label.
407  
408 =back
409  
410  
411  
412  
413 =head1 DEFAULT BINDINGS
414  
415 Since a Label is a non-interacting widget, it does not have
416 any bindings.
417  
418  
419  
420  
421 =head1 SEE ALSO
422  
423 L<Curses::UI|Curses::UI>,
424 L<Curses::UI::Widget|Curses::UI::Widget>,
425  
426  
427  
428  
429 =head1 AUTHOR
430  
431 Copyright (c) 2001-2002 Maurice Makaay. All rights reserved.
432  
433 Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)
434  
435  
436 This package is free software and is provided "as is" without express
437 or implied warranty. It may be used, redistributed and/or modified
438 under the same terms as perl itself.
439  
« blib/lib/Curses/UI/Language.pm ↑ Summary blib/lib/Curses/UI/Dialog/Status.pm »