blib/lib/Curses/UI/Buttonbox.pm untested

filter: Statementstmt 0.0% 0 / 211 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 369 CC 37 SCAR 37.5
CC
37
Cov
0%
draw
132.0
layout
42.0
mouse_button1
42.0
CRAP 42.7
1 # ----------------------------------------------------------------------
2 # Curses::UI::Buttonbox
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 package Curses::UI::Buttonbox;
14  
15 use strict;
16 use Curses;
17 use Curses::UI::Widget;
18 use Curses::UI::Common;
19  
20 use vars qw(
21 $VERSION
22 @ISA
23 );
24  
25 $VERSION = '1.10';
26  
27 @ISA = qw(
28 Curses::UI::Widget
29 Curses::UI::Common
30 );
31  
32 # Definition of the most common buttons.
33 my %buttondef = (
34 'ok' => {
35 -label => '< OK >',
36 -value => 1,
37 -onpress => undef,
38 -shortcut => 'o',
39 },
40 'cancel'=> {
41 -label => '< Cancel >',
42 -value => 0,
43 -onpress => undef,
44 -shortcut => 'c',
45 },
46 'yes' => {
47 -label => '< yes >',
48 -value => 1,
49 -onpress => undef,
50 -shortcut => 'y',
51 },
52 'no' => {
53 -label => '< No >',
54 -value => 0,
55 -onpress => undef,
56 -shortcut => 'n',
57 },
58
59 );
60  
61 # The default button to use if no buttons were defined.
62 my $default_btn = [ 'ok' ];
63  
64 my %routines = (
65 'press-button' => \&press_button,
66 'loose-focus' => \&loose_focus,
67 'next' => \&next_button,
68 'previous' => \&previous_button,
69 'shortcut' => \&shortcut,
70 'focus-shift' => \&focus_shift,
71 'mouse-button1'=> \&mouse_button1,
72 );
73  
74 my %bindings = (
75 CUI_TAB() => 'focus-shift',
76 KEY_BTAB() => 'focus-shift',
77 KEY_ENTER() => 'press-button',
78 CUI_SPACE() => 'press-button',
79 KEY_UP() => 'previous',
80 "k" => 'previous',
81 KEY_DOWN() => 'next',
82 "j" => 'next',
83 KEY_LEFT() => 'previous',
84 'h' => 'previous',
85 KEY_RIGHT() => 'next',
86 'l' => 'next',
87 '' => 'shortcut',
88 );
89  
90 sub new ()
91 {
92 my $class = shift;
93  
94 my %userargs = @_;
95 keys_to_lowercase(\%userargs);
96
97 my %args = (
98 -parent => undef, # the parent window
99 -buttons => $default_btn, # buttons (arrayref)
100 -buttonalignment => undef, # left / middle / right
101 -selected => 0, # which selected
102 -width => undef, # the width of the buttons widget
103 -x => 0, # the horizontal pos rel. to parent
104 -y => 0, # the vertical pos rel. to parent
105 -bg => -1,
106 -fg => -1,
107  
108 %userargs,
109  
110 -routines => {%routines},
111 -bindings => {%bindings},
112  
113 -focus => 0, # init value
114 -nocursor => 1, # this widget does not use a cursor
115 );
116  
117 # The windowscr height should be 1.
118 my $height = $args{-vertical} ? scalar @{$args{-buttons}} : 1;
119 $args{-height} = height_by_windowscrheight($height ,%args);
120
121 # Create the widget.
122 my $this = $class->SUPER::new( %args );
123
124 # Process button definitions.
125 $this->process_buttondefs;
126  
127 $this->layout();
128  
129 if ($Curses::UI::ncurses_mouse) {
130 $this->set_mouse_binding('mouse-button1', BUTTON1_CLICKED());
131 }
132  
133 return $this;
134 }
135  
136  
137 sub process_buttondefs()
138 {
139 my $this = shift;
140  
141 my $buttons = $this->{-buttons};
142  
143 # Process button types.
144 my @buttons = ();
145 foreach my $button (@$buttons)
146 {
147 if (ref $button eq 'HASH') {
148 # noop, this is a completed button definition
149 } elsif (not ref $button) {
150 my $realbutton = $buttondef{$button};
151 unless (defined $realbutton) {
152 $this->root->fatalerror(
153 "process_buttondefs(): Invalid button type.\n"
154 . "No definition found for '$button'"
155 );
156 }
157  
158 # Check for language support.
159 my $lang_spec = $this->root->lang->get("button_$button");
160 if ($lang_spec) {
161 my ($shortcut, $label) = split /\:/, $lang_spec, 2;
162 $realbutton->{-label} = "< $label >";
163 $realbutton->{-shortcut} = $shortcut;
164 }
165  
166 $button = $realbutton;
167  
168 } else {
169 $this->root->fatalerror(
170 "Invalid button definition.\n"
171 . "It should be a HASH reference,\n"
172 . "but is a " . (ref $button) . " reference."
173 );
174 }
175  
176 keys_to_lowercase($button);
177 push @buttons, $button;
178 }
179  
180 $this->{-buttons} = \@buttons;
181 return $this;
182 }
183  
184 sub layout()
185 {
186 my $this = shift;
187  
188 $this->SUPER::layout() or return;
189  
190 # Compute the space that is needed for the buttons.
191 my $xneed = $this->compute_buttonwidth;
192 my $yneed = $this->compute_buttonheight;
193
194 if ( ($xneed > $this->canvaswidth) || ($yneed > $this->canvasheight) )
195 {
196 $Curses::UI::screen_too_small++;
197 return $this;
198 }
199  
200 # Compute the x location of the buttons.
201 my $xpos = 0;
202 if (defined $this->{-buttonalignment})
203 {
204 if ($this->{-buttonalignment} eq 'right') {
205 $xpos = $this->canvaswidth - $xneed;
206 } elsif ($this->{-buttonalignment} eq 'middle') {
207 $xpos = int (($this->canvaswidth-$xneed)/2);
208 }
209 }
210 $this->{-xpos} = $xpos;
211  
212 $this->{-max_selected} = @{$this->{-buttons}} - 1;
213  
214 # Make shortcuts all upper-case.
215 foreach my $button (@{$this->{-buttons}}) {
216 if (defined $button->{-shortcut}) {
217 $button->{-shortcut} = uc $button->{-shortcut};
218 }
219 }
220  
221 return $this;
222 }
223  
224 sub get_selected_button()
225 {
226 my $this = shift;
227 my $selected = $this->{-selected};
228 my $button = $this->{-buttons}->[$selected];
229 return $button;
230 }
231  
232 sub get()
233 {
234 my $this = shift;
235 my $button = $this->get_selected_button;
236 if (defined $button->{-value}) {
237 return $button->{-value};
238 } else {
239 return $this->{-selected};
240 }
241 }
242  
243 sub next_button()
244 {
245 my $this = shift;
246 $this->{-selected}++;
247 $this->schedule_draw(1);
248 return $this;
249 }
250  
251 sub previous_button()
252 {
253 my $this = shift;
254 $this->{-selected}--;
255 $this->schedule_draw(1);
256 return $this;
257 }
258  
259 # Focus the next button. If the last button was
260 # selected, let the buttonbox loose focus.
261 sub focus_shift()
262 {
263 my $this = shift;
264 my $key = shift;
265  
266 if ( $key eq KEY_BTAB() )
267 {
268 $this->previous_button();
269 if ($this->{-selected} < 0)
270 {
271 # $this->schedule_draw(0);
272 $this->{-selected} = $this->{-max_selected};
273 $this->do_routine('loose-focus', $key);
274 }
275 } else {
276 $this->next_button();
277 if ($this->{-selected} > $this->{-max_selected})
278 {
279 # $this->schedule_draw(0);
280 $this->{-selected} = 0;
281 $this->do_routine('loose-focus', $key);
282 }
283 }
284  
285 return $this;
286 }
287  
288 sub press_button()
289 {
290 my $this = shift;
291 my $button = $this->get_selected_button;
292 my $command = $button->{-onpress};
293 $this->schedule_draw(1);
294 if (defined $command and ref $command eq 'CODE') {
295 $command->($this);
296 }
297 return $this;
298 }
299  
300 sub draw(;$)
301 {
302 my $this = shift;
303 my $no_doupdate = shift || 0;
304  
305 # Draw the widget.
306 $this->SUPER::draw(1) or return $this;
307
308 # Check if active element isn't out of bounds.
309 $this->{-selected} = 0 unless defined $this->{-selected};
310 $this->{-selected} = 0 if $this->{-selected} < 0;
311 $this->{-selected} = $this->{-max_selected}
312 if $this->{-selected} > $this->{-max_selected};
313  
314 # Draw the buttons.
315 my $id = 0;
316 my $x = 0;
317 my $y = 0;
318 my $cursor_x = 0;
319  
320 foreach my $button (@{$this->{-buttons}})
321 {
322 # Let there be color
323 if ($Curses::UI::color_support) {
324 my $co = $Curses::UI::color_object;
325 my $pair = $co->get_color_pair(
326 $this->{-fg},
327 $this->{-bg});
328  
329 $this->{-canvasscr}->attron(COLOR_PAIR($pair));
330  
331 }
332  
333 # Make the focused button reverse.
334 if ($this->{-focus} and defined $this->{-selected}
335 and $id == $this->{-selected}) {
336 $this->{-canvasscr}->attron(A_REVERSE);
337 }
338
339 # Draw the button.
340 $this->{-canvasscr}->addstr(
341 $y, $this->{-xpos} + $x,
342 $button->{-label}
343 );
344  
345 # Draw shortcut if available.
346 my $sc = $button->{-shortcut};
347 if (defined $sc)
348 {
349 my $pos = index(uc $button->{-label}, $sc);
350 if ($pos >= 0)
351 {
352 my $letter = substr($button->{-label}, $pos, 1);
353 $this->{-canvasscr}->attron(A_UNDERLINE);
354 $this->{-canvasscr}->addch(
355 $y, $this->{-xpos} + $x + $pos,
356 $letter
357 );
358 $this->{-canvasscr}->attroff(A_UNDERLINE);
359 }
360 }
361  
362 # Change the $y value if the buttons are to be drawn vertically and leave $x alone
363 if ( (defined $this->{-vertical}) && ($this->{-vertical}) ) {
364 $y++;
365 } else {
366 $x += 1 + length($button->{-label});
367 }
368  
369 $this->{-canvasscr}->attroff(A_REVERSE) if $this->{-focus};
370
371 $id++;
372 }
373 $this->{-canvasscr}->move(0,0);
374 $this->{-canvasscr}->noutrefresh;
375 doupdate() unless $no_doupdate;
376  
377 return $this;
378 }
379  
380 sub mouse_button1($$$$;)
381 {
382 my $this = shift;
383 my $event = shift;
384 my $x = shift;
385 my $y = shift;
386  
387 my $idx = 0;
388 my $bx = $this->{-xpos};
389  
390 # Clicked left of the buttons?
391 return $this if $x < $bx;
392  
393 # Find the button on which was clicked.
394 foreach my $button (@{$this->{-buttons}}) {
395 $bx += length($button->{-label});
396 if ($bx > $x) { last }
397 if ($bx == $x) { $idx = undef; last }
398 $bx += 1;
399 $idx++;
400 }
401 undef $idx if defined $idx and
402 $idx > (@{$this->{-buttons}} - 1);
403  
404 if (defined $idx) {
405 $this->{-selected} = $idx;
406 $this->focus();
407 $this->do_routine('press-button', $event);
408 }
409 }
410  
411 sub compute_buttonheight($;)
412 {
413 my $this = shift;
414 my $height = 1;
415  
416 if ( (defined $this->{-vertical}) && ($this->{-vertical}) ) {
417 $height = scalar @{$this->{-buttons}};
418 }
419 return $height;
420 }
421  
422 sub compute_buttonwidth($;)
423 {
424 my $this = shift;
425  
426 $this->process_buttondefs;
427  
428 my $width=0;
429  
430 if ( (defined $this->{-vertical}) && ($this->{-vertical}) ) {
431 foreach my $button (@{$this->{-buttons}}) {
432 if ($width < length($button->{-label})) {
433 $width = length($button->{-label});
434 }
435 }
436 } else {
437 # Spaces
438 $width = @{$this->{-buttons}} - 1;
439
440 # Buttons
441 foreach my $button (@{$this->{-buttons}}) {
442 $width += length($button->{-label});
443 }
444 }
445 return $width;
446 }
447  
448 sub shortcut()
449 {
450 my $this = shift;
451 my $key = uc shift;
452
453 # Walk through shortcuts to see if the pressed key
454 # is in the list of -shortcuts.
455 my $idx = 0;
456 my $found_idx;
457 SHORTCUT: foreach my $button (@{$this->{-buttons}})
458 {
459 my $sc = $button->{-shortcut};
460 if (defined $sc and $sc eq $key)
461 {
462 $found_idx = $idx;
463 last SHORTCUT;
464 }
465 $idx++;
466 }
467  
468 # Shortcut found?
469 if (defined $found_idx)
470 {
471 $this->{-selected} = $found_idx;
472 return $this->process_bindings(KEY_ENTER());
473 }
474  
475 return $this;
476 }
477  
478 1;
479  
480  
481 =pod
482  
483 =head1 NAME
484  
485 Curses::UI::Buttonbox - Create and manipulate button widgets
486  
487 =head1 CLASS HIERARCHY
488  
489 Curses::UI::Widget
490 |
491 +----Curses::UI::Buttonbox
492  
493  
494 =head1 SYNOPSIS
495  
496 use Curses::UI;
497 my $cui = new Curses::UI;
498 my $win = $cui->add('window_id', 'Window');
499  
500 my $buttons = $win->add(
501 'mybuttons', 'Buttonbox',
502 -buttons => [
503 {
504 -label => '< Button 1 >',
505 -value => 1,
506 -shortcut => 1
507 },{
508 -label => '< Button 2 >',
509 -value => 2,
510 -shortcut => 2
511 }
512 ]
513 );
514  
515 $buttons->focus();
516 my $value = $buttons->get();
517  
518  
519 =head1 DESCRIPTION
520  
521 Curses::UI::Buttonbox is a widget that can be used to create an
522 array of buttons (or, of course, only one button).
523  
524  
525  
526  
527 =head1 STANDARD OPTIONS
528  
529 B<-parent>, B<-x>, B<-y>, B<-width>, B<-height>,
530 B<-pad>, B<-padleft>, B<-padright>, B<-padtop>, B<-padbottom>,
531 B<-ipad>, B<-ipadleft>, B<-ipadright>, B<-ipadtop>, B<-ipadbottom>,
532 B<-title>, B<-titlefullwidth>, B<-titlereverse>, B<-onfocus>,
533 B<-onblur>
534  
535 For an explanation of these standard options, see
536 L<Curses::UI::Widget|Curses::UI::Widget>.
537  
538  
539  
540  
541 =head1 WIDGET-SPECIFIC OPTIONS
542  
543 =over 4
544  
545 =item * B<-buttons> < ARRAYREF >
546  
547 This option takes a reference to a list of buttons.
548 The list may contain both predefined button types and
549 complete button definitions of your own.
550  
551 * B<Your own button definition>
552  
553 A button definition is a reference to a hash. This
554 hash can have the following key-value pairs:
555  
556 obligatory:
557 -----------
558  
559 -label This determines what text should be drawn
560 on the button.
561  
562 optional:
563 ---------
564  
565 -value This determines the returnvalue for the
566 get() method. If the value is not defined,
567 the get() method will return the index
568 of the button.
569  
570 -shortcut The button will act as if it was pressed
571 if the key defined by -shortcut is pressed
572  
573 -onpress If the value for -onpress is a CODE reference,
574 this code will be executes if the button
575 is pressed, before the buttons widget loses
576 focus and returns.
577  
578 * B<Predefined button type>
579  
580 This module has a predefined list of frequently used button
581 types. Using these in B<-buttons> makes things a lot
582 easier. The predefined button types are:
583  
584 ok -label => '< OK >'
585 -shortcut => 'o'
586 -value => 1
587 -onpress => undef
588  
589 cancel -label => '< Cancel >'
590 -shortcut => 'c'
591 -value => 0
592 -onpress => undef
593  
594 yes -label => '< Yes >'
595 -shortcut => 'y'
596 -value => 1
597 -onpress => undef
598  
599 no -label => '< No >'
600 -shortcut => 'n'
601 -value => 0
602 -onpress => undef
603  
604 Example:
605  
606 ....
607 -buttons => [
608 { -label => '< My own button >',
609 -value => 'mine!',
610 -shortcut => 'm' },
611  
612 'ok',
613  
614 'cancel',
615  
616 { -label => '< My second button >',
617 -value => 'another one',
618 -shortcut => 's',
619 -onpress => sub { die "Do not press this button!\n" } }
620 ]
621 ....
622  
623  
624 =item * B<-selected> < INDEX >
625  
626 By default the first button (index = 0) is active. If you
627 want another button to be active at creation time,
628 add this option. The INDEX is the index of the button you
629 want to make active.
630  
631 =item * B<-buttonalignment> < VALUE >
632  
633 You can specify how the buttons should be aligned in the
634 widget. Available values for VALUE are 'left', 'middle'
635 and 'right'.
636  
637 =item * B<-vertical> < BOOLEAN >
638  
639 When set to a true value, it will cause the buttons to be
640 rendered with vertical instead of horizontal alignment.
641  
642  
643 =back
644  
645  
646  
647  
648 =head1 METHODS
649  
650 =over 4
651  
652 =item * B<new> ( OPTIONS )
653  
654 =item * B<layout> ( )
655  
656 =item * B<draw> ( BOOLEAN )
657  
658 =item * B<focus> ( )
659  
660 =item * B<onFocus> ( CODEREF )
661  
662 =item * B<onBlur> ( CODEREF )
663  
664 =item * B<draw_if_visible> ( )
665  
666 These are standard methods. See L<Curses::UI::Widget|Curses::UI::Widget>
667 for an explanation of these.
668  
669 =item * B<get> ( )
670  
671 This method will return the index of the currently active
672 button. If a value is given for that index (using the
673 B<-value> option, see B<-buttons> above), that value will be
674 returned.
675  
676 =back
677  
678  
679  
680  
681 =head1 DEFAULT BINDINGS
682  
683 =over 4
684  
685 =item * <B<enter>>, <B<space>>
686  
687 TODO: Fix dox
688 Call the 'loose-focus' routine. By default this routine will have the
689 container in which the widget is loose its focus. If you do
690 not like this behaviour, then you can have it loose focus itself
691 by calling:
692  
693 $buttonswidget->set_routine('loose-focus', 'RETURN');
694  
695 For an explanation of B<set_routine>, see
696 L<Curses::UI::Widget|Curses::UI::Widget>.
697  
698  
699 =item * <B<cursor left>>, <B<h>>
700  
701 Call the 'previous' routine. This will make the previous
702 button the active button. If the active button already is
703 the first button, nothing will be done.
704  
705 =item * <B<cursor right>>, <B<l>
706  
707 Call the 'next' routine. This will make the next button the
708 active button. If the next button already is the last button,
709 nothing will be done.
710  
711 =item * <B<any other key>>
712  
713 This will call the 'shortcut' routine. This routine will
714 handle the shortcuts that are set by the B<-shortcuts> option.
715  
716 =back
717  
718  
719  
720  
721  
722 =head1 SEE ALSO
723  
724 L<Curses::UI|Curses::UI>,
725 L<Curses::UI::Widget|Curses::UI::Widget>,
726 L<Curses::UI::Common|Curses::UI::Common>
727  
728  
729  
730  
731 =head1 AUTHOR
732  
733 Copyright (c) 2001-2002 Maurice Makaay. All rights reserved.
734  
735 Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)
736  
737  
738 This package is free software and is provided "as is" without express
739 or implied warranty. It may be used, redistributed and/or modified
740 under the same terms as perl itself.
741