blib/lib/Curses/UI/Dialog/Calendar.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 90 CC 4 SCAR 13.5
CC
4
Cov
0%
new
6.0
layout
6.0
press_button_callback
6.0
CRAP 3.8
1 # ----------------------------------------------------------------------
2 # Curses::UI::Dialog::Calendar
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::Dialog::Calendar;
14  
15 use strict;
16 use Curses;
17 use Curses::UI::Window;
18 use Curses::UI::Common;
19 use Curses::UI::Widget;
20  
21 use vars qw(
22 $VERSION
23 @ISA
24 );
25  
26 @ISA = qw(
27 Curses::UI::Window
28 Curses::UI::Common
29 );
30  
31 $VERSION = '1.10';
32  
33 sub new ()
34 {
35 my $class = shift;
36  
37 my %userargs = @_;
38 keys_to_lowercase(\%userargs);
39  
40 my %args = (
41 -title => undef,
42 -date => undef,
43 -fg => -1,
44 -bg => -1,
45  
46 %userargs,
47  
48 -selected_date => undef,
49 -border => 1,
50 -centered => 1,
51 -titleinverse => 0,
52 -ipad => 1,
53 );
54  
55 my $this = $class->SUPER::new(%args);
56
57 my $l = $this->root->lang;
58  
59 my $buttons = $this->add(
60 'buttons', 'Buttonbox',
61 -y => -1,
62 -x => 0,
63 -width => undef,
64 -buttonalignment => 'right',
65 -buttons => [ 'ok', 'cancel' ],
66 -bg => $this->{-bg},
67 -fg => $this->{-fg},
68 );
69  
70 # Let the window in which the buttons are loose focus
71 # if a button is pressed.
72 $buttons->set_routine( 'press-button', \&press_button_callback );
73  
74 my $calendar = $this->add(
75 'calendar', 'Calendar',
76 -border => 0,
77 -padbottom => 1,
78 -date => $this->{-date},
79 -bg => $this->{-bg},
80 -fg => $this->{-fg},
81 )->focus;
82  
83 # Selecting a date may bring the focus to the OK button.
84 $calendar->set_routine('date-select', sub{
85 my $cal = shift;
86 $cal->date_select;
87 $cal->parent->getobj('buttons')->{-selected} = 0;
88 $cal->loose_focus;
89 });
90  
91 # Doubleclick on calendar may close the dialog.
92 if ( $Curses::UI::ncurses_mouse ) {
93 $calendar->set_mouse_binding(sub {
94 my $buttons = shift();
95 my @extra = @_;
96 my $this = $buttons->parent;
97 $buttons->do_routine('mouse-button', @extra);
98 $this->{-selected_date} = $this->getobj('calendar')->get;
99 $this->loose_focus;
100 }, BUTTON1_DOUBLE_CLICKED());
101 }
102  
103 # Escape should close the dialog, without setting a date.
104 $this->set_binding(sub {
105 my $this = shift;
106 $this->{-selected_date} = undef;
107 $this->loose_focus;
108 }, CUI_ESCAPE());
109  
110 $this->layout();
111  
112 return bless $this, $class;
113 }
114  
115 sub layout()
116 {
117 my $this = shift;
118  
119 my $cal = $this->getobj('calendar');
120 if ($cal) {
121 $this->{-width} = width_by_windowscrwidth(
122 $this->getobj('calendar')->{-width}, %$this);
123 $this->{-height} = height_by_windowscrheight(
124 $this->getobj('calendar')->{-height}, %$this);
125 }
126  
127 $this->SUPER::layout() or return;
128  
129 return $this;
130 }
131  
132 sub get()
133 {
134 my $this = shift;
135 return $this->{-selected_date};
136 }
137  
138 sub press_button_callback()
139 {
140 my $buttons = shift;
141 my $this = $buttons->parent;
142  
143 my $ok_pressed = $buttons->get;
144 if ($ok_pressed)
145 {
146 $this->{-selected_date} = $this->getobj('calendar')->get;
147 } else {
148 $this->{-selected_date} = undef;
149 }
150  
151 $this->loose_focus;
152 }
153  
154 1;
155  
156  
157 =pod
158  
159 =head1 NAME
160  
161 Curses::UI::Dialog::Calendar - Create and manipulate calendar dialogs
162  
163 =head1 CLASS HIERARCHY
164  
165 Curses::UI::Widget
166 |
167 +----Curses::UI::Container
168 |
169 +----Curses::UI::Window
170 |
171 +----Curses::UI::Dialog::Calendar
172  
173  
174  
175 =head1 SYNOPSIS
176  
177 use Curses::UI;
178 my $cui = new Curses::UI;
179 my $win = $cui->add('window_id', 'Window');
180  
181 # The hard way.
182 # -------------
183 my $dialog = $win->add(
184 'mydialog', 'Dialog::Calendar'
185 );
186 $dialog->modalfocus;
187 $win->delete('mydialog');
188 my $date = $dialog->get();
189  
190 # The easy way (see Curses::UI documentation).
191 # --------------------------------------------
192 $date = $cui->calendardialog();
193  
194  
195  
196  
197 =head1 DESCRIPTION
198  
199 Curses::UI::Dialog::Calendar is a calendar dialog.
200 This type of dialog can be used to select a date.
201  
202 See exampes/demo-widgets in the distribution for a short demo.
203  
204  
205  
206 =head1 OPTIONS
207  
208 =over 4
209  
210 =item * B<-title> < TEXT >
211  
212 Set the title of the dialog window to TEXT.
213  
214 =item * B<-date> < DATE >
215  
216 Set the date to start with to DATE. If -date is
217 not defined, today will be used as the startdate.
218  
219 =back
220  
221  
222  
223  
224 =head1 METHODS
225  
226 =over 4
227  
228 =item * B<new> ( OPTIONS )
229  
230 =item * B<layout> ( )
231  
232 =item * B<draw> ( BOOLEAN )
233  
234 =item * B<focus> ( )
235  
236 =item * B<modalfocus> ( )
237  
238 These are standard methods. See L<Curses::UI::Container|Curses::UI::Container>
239 for an explanation of these.
240  
241 =item * B<get> ( )
242  
243 This method will return the date that was selected or
244 undef if no date was selected.
245  
246 =back
247  
248  
249  
250 =head1 SPECIAL BINDINGS
251  
252 =over 4
253  
254 =item * B<escape>
255  
256 This will invoke the cancel button, so the calendar dialog
257 returns without selecting any date.
258  
259 =back
260  
261  
262  
263 =head1 SEE ALSO
264  
265 L<Curses::UI|Curses::UI>,
266 L<Curses::UI::Container|Curses::UI::Container>,
267 L<Curses::UI::Buttonbox|Curses::UI::Buttonbox>
268  
269  
270  
271  
272 =head1 AUTHOR
273  
274 Copyright (c) 2001-2002 Maurice Makaay. All rights reserved.
275  
276 Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de)
277  
278  
279 This package is free software and is provided "as is" without express
280 or implied warranty. It may be used, redistributed and/or modified
281 under the same terms as perl itself.
282