| 1 | # ---------------------------------------------------------------------- | ||
| 2 | # Curses::UI::Dialog::Question | ||
| 3 | # | ||
| 4 | # (c) 2001-2002 by Luke Closs. 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 | # This was mostly copied from Curses::UI::Dialog::Basic | ||
| 10 | # | ||
| 11 | # Currently maintained by Marcus Thiesen | ||
| 12 | # e-mail: marcus@cpan.thiesenweb.de | ||
| 13 | # ---------------------------------------------------------------------- | ||
| 14 | |||
| 15 | package Curses::UI::Dialog::Question; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use Curses qw(KEY_ENTER); | ||
| 19 | use Curses::UI::Common; | ||
| 20 | use Curses::UI::Window; | ||
| 21 | |||
| 22 | use vars qw( | ||
| 23 | $VERSION | ||
| 24 | @ISA | ||
| 25 | ); | ||
| 26 | |||
| 27 | @ISA = qw( | ||
| 28 | Curses::UI::Window | ||
| 29 | Curses::UI::Common | ||
| 30 | ); | ||
| 31 | |||
| 32 | $VERSION = '1.00'; | ||
| 33 | |||
| 34 | sub new () | ||
| 35 | { | ||
| 36 | my $class = shift; | ||
| 37 | |||
| 38 | my %userargs = @_; | ||
| 39 | keys_to_lowercase(\%userargs); | ||
| 40 | |||
| 41 | my %args = ( | ||
| 42 | -border => 1, | ||
| 43 | -question => '', # The question to show | ||
| 44 | -answer => '', # a default answer | ||
| 45 | -ipad => 1, | ||
| 46 | -fg => -1, | ||
| 47 | -bg => -1, | ||
| 48 | |||
| 49 | %userargs, | ||
| 50 | |||
| 51 | -titleinverse => 1, | ||
| 52 | -centered => 1, | ||
| 53 | ); | ||
| 54 | |||
| 55 | # Create a new object, but remember the current | ||
| 56 | # screen_too_small setting. The width needed for the | ||
| 57 | # buttons can only be computed in the second run | ||
| 58 | # of focus() and we do not want the first run to | ||
| 59 | # set screen_too_small to a true value because | ||
| 60 | # of this. | ||
| 61 | # | ||
| 62 | my $remember = $Curses::UI::screen_too_small; | ||
| 63 | my $this = $class->SUPER::new(%args); | ||
| 64 | |||
| 65 | my $q = $this->add('question', 'TextViewer', | ||
| 66 | -x => 1, -y => 0, | ||
| 67 | -wrapping => 1, | ||
| 68 | -padbottom => 0, | ||
| 69 | -height => 3, | ||
| 70 | -text => $this->{-question}, | ||
| 71 | -bg => $this->{-bg}, | ||
| 72 | -fg => $this->{-fg}, | ||
| 73 | -bbg => $this->{-bg}, | ||
| 74 | -bfg => $this->{-fg}, | ||
| 75 | -focusable => 0, | ||
| 76 | ); | ||
| 77 | |||
| 78 | my $a = $this->add('answer', 'TextEntry', | ||
| 79 | -x => 1, -y => 3, | ||
| 80 | -border => 1, | ||
| 81 | -bg => $this->{-bg}, | ||
| 82 | -fg => $this->{-fg}, | ||
| 83 | -bbg => $this->{-bg}, | ||
| 84 | -bfg => $this->{-fg}, | ||
| 85 | -text => $this->{-answer}); | ||
| 86 | # Push the cursor to the end of the line. | ||
| 87 | $a->{-pos} = 999; | ||
| 88 | |||
| 89 | # Create a hash with arguments that may be passed to | ||
| 90 | # the Buttonbox class. | ||
| 91 | my %buttonargs = ( | ||
| 92 | -buttonalignment => 'right', | ||
| 93 | ); | ||
| 94 | foreach my $arg (qw(-buttons -selected -buttonalignment)) { | ||
| 95 | $buttonargs{$arg} = $this->{$arg} if exists $this->{$arg}; | ||
| 96 | } | ||
| 97 | my $b = $this->add( | ||
| 98 | 'buttons', 'Buttonbox', | ||
| 99 | -y => -1, | ||
| 100 | -bg => $this->{-bg}, | ||
| 101 | -fg => $this->{-fg}, | ||
| 102 | -buttons => [ 'ok', 'cancel' ], | ||
| 103 | |||
| 104 | %buttonargs | ||
| 105 | ); | ||
| 106 | |||
| 107 | # Let the window in which the buttons are loose focus | ||
| 108 | # if a button is pressed, or if enter is hit in the answer box. | ||
| 109 | my $pressed = sub { | ||
| 110 | my $this = shift; | ||
| 111 | my $parent = $this->parent; | ||
| 112 | $parent->{-cancelled} = !$this->get; | ||
| 113 | $parent->loose_focus(); | ||
| 114 | }; | ||
| 115 | $b->set_routine( 'press-button', $pressed ); | ||
| 116 | $a->set_binding( $pressed, KEY_ENTER()); | ||
| 117 | |||
| 118 | # Restore screen_too_small (see above) and | ||
| 119 | # start the second layout pass. | ||
| 120 | $Curses::UI::screen_too_small = $remember; | ||
| 121 | $this->layout; | ||
| 122 | |||
| 123 | # Set the initial focus to the answer box. | ||
| 124 | $a->focus; | ||
| 125 | |||
| 126 | return bless $this, $class; | ||
| 127 | } | ||
| 128 | |||
| 129 | # TODO delete_curses_windows | ||
| 130 | sub layout() | ||
| 131 | { | ||
| 132 | my $this = shift; | ||
| 133 | return $this if $Curses::UI::screen_too_small; | ||
| 134 | |||
| 135 | # The maximum available space on the screen. | ||
| 136 | my $avail_width = $ENV{COLS}; | ||
| 137 | my $avail_height = $ENV{LINES}; | ||
| 138 | |||
| 139 | # Compute the maximum available space for the message. | ||
| 140 | |||
| 141 | $this->process_padding; | ||
| 142 | |||
| 143 | my $avail_textwidth = $avail_width; | ||
| 144 | $avail_textwidth -= 2; # border for the textviewer | ||
| 145 | $avail_textwidth -= 2 if $this->{-border}; | ||
| 146 | $avail_textwidth -= $this->{-ipadleft} - $this->{-ipadright}; | ||
| 147 | |||
| 148 | my $avail_textheight = $avail_height; | ||
| 149 | $avail_textheight -= 2; # border for the textviewer | ||
| 150 | $avail_textheight -= 3; # for answer box | ||
| 151 | $avail_textheight -= 2; # empty line and line of buttons | ||
| 152 | $avail_textheight -= 2 if $this->{-border}; | ||
| 153 | $avail_textheight -= $this->{-ipadtop} - $this->{-ipadbottom}; | ||
| 154 | |||
| 155 | # Break up the message in separate lines if neccessary. | ||
| 156 | my @lines = (); | ||
| 157 | foreach (split (/\n/, $this->{-question})) { | ||
| 158 | push @lines, @{text_wrap($_, $avail_textwidth)}; | ||
| 159 | } | ||
| 160 | |||
| 161 | # Compute the longest line in the message. | ||
| 162 | my $longest_line = 0; | ||
| 163 | foreach (@lines) { | ||
| 164 | $longest_line = length($_) | ||
| 165 | if (length($_) > $longest_line); | ||
| 166 | } | ||
| 167 | |||
| 168 | # Compute the width of the buttons (if the buttons | ||
| 169 | # object is available. This is not the case just after | ||
| 170 | # new() calls SUPER::new()). | ||
| 171 | my $buttons = $this->getobj('buttons'); | ||
| 172 | my $button_width = 0; | ||
| 173 | if (defined $buttons) { | ||
| 174 | $button_width = $buttons->compute_buttonwidth; | ||
| 175 | } | ||
| 176 | |||
| 177 | # Decide what is the longest line. | ||
| 178 | $longest_line = $button_width if $longest_line < $button_width; | ||
| 179 | |||
| 180 | # Check if there is enough space to show the widget. | ||
| 181 | if ($avail_textheight < 1 or $avail_textwidth < $longest_line) { | ||
| 182 | $Curses::UI::screen_too_small = 1; | ||
| 183 | return $this; | ||
| 184 | } | ||
| 185 | |||
| 186 | # Compute the size of the widget. | ||
| 187 | |||
| 188 | my $w = $longest_line; | ||
| 189 | $w += 2; # border of textviewer | ||
| 190 | $w += 2; # extra width for preventing wrapping of text | ||
| 191 | $w += 2 if $this->{-border}; | ||
| 192 | $w += $this->{-ipadleft} + $this->{-ipadright}; | ||
| 193 | |||
| 194 | my $h = @lines; | ||
| 195 | $h += 2; # empty line + line of buttons | ||
| 196 | $h += 3; # for textentry widget | ||
| 197 | $h += 2; # border of textviewer | ||
| 198 | $h += 2 if $this->{-border}; | ||
| 199 | $h += $this->{-ipadtop} + $this->{-ipadbottom}; | ||
| 200 | |||
| 201 | $this->{-width} = $w; | ||
| 202 | $this->{-height} = $h; | ||
| 203 | |||
| 204 | $this->SUPER::layout; | ||
| 205 | |||
| 206 | return $this; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub get() | ||
| 210 | { | ||
| 211 | my $this = shift; | ||
| 212 | return undef if $this->{-cancelled}; | ||
| 213 | $this->getobj('answer')->get; | ||
| 214 | } | ||
| 215 | |||
| 216 | 1; | ||
| 217 | |||
| 218 | |||
| 219 | |||
| 220 | =head1 NAME | ||
| 221 | |||
| 222 | Curses::UI::Dialog::Question - Pose a simple question to the user | ||
| 223 | |||
| 224 | |||
| 225 | =head1 CLASS HIERARCHY | ||
| 226 | |||
| 227 | Curses::UI::Widget | ||
| 228 | | | ||
| 229 | +----Curses::UI::Container | ||
| 230 | | | ||
| 231 | +----Curses::UI::Window | ||
| 232 | | | ||
| 233 | +----Curses::UI::Dialog::Question | ||
| 234 | |||
| 235 | |||
| 236 | =head1 SYNOPSIS | ||
| 237 | |||
| 238 | use Curses::UI; | ||
| 239 | my $cui = new Curses::UI; | ||
| 240 | my $win = $cui->add('window_id', 'Window'); | ||
| 241 | |||
| 242 | # The hard way. | ||
| 243 | # ------------- | ||
| 244 | my $dialog = $win->add( | ||
| 245 | 'mydialog', 'Dialog::Question', | ||
| 246 | -question => 'How super awesome are you?' | ||
| 247 | ); | ||
| 248 | $dialog->modalfocus; | ||
| 249 | $win->delete('mydialog'); | ||
| 250 | |||
| 251 | # The easy way (see Curses::UI documentation). | ||
| 252 | # -------------------------------------------- | ||
| 253 | my $value = $cui->question(-question => 'How super awesome are you?'); | ||
| 254 | |||
| 255 | # or even | ||
| 256 | my $awesomeness = $cui->question('How super awesome are you?'); | ||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | |||
| 261 | =head1 DESCRIPTION | ||
| 262 | |||
| 263 | Curses::UI::Dialog::Question is a basic question dialog. This type of | ||
| 264 | dialog has a message on it, a TextEntry answer box, and one or more | ||
| 265 | buttons. It can be used to have a user enter some answer in response | ||
| 266 | to a question. | ||
| 267 | |||
| 268 | See exampes/demo-widgets in the distribution for a short demo. | ||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | =head1 OPTIONS | ||
| 273 | |||
| 274 | =over 4 | ||
| 275 | |||
| 276 | =item * B<-title> < TEXT > | ||
| 277 | |||
| 278 | Set the title of the dialog window to TEXT. | ||
| 279 | |||
| 280 | =item * B<-question> < TEXT > | ||
| 281 | |||
| 282 | This option sets the question to show to TEXT. The text may | ||
| 283 | contain newline (\n) characters. | ||
| 284 | |||
| 285 | =item * B<-buttons> < ARRAYREF > | ||
| 286 | |||
| 287 | =item * B<-selected> < INDEX > | ||
| 288 | |||
| 289 | =item * B<-buttonalignment> < VALUE > | ||
| 290 | |||
| 291 | These options sets the buttons that have to be used. For an | ||
| 292 | explanation of these options, see the | ||
| 293 | L<Curses::UI::Buttonbox|Curses::UI::Buttonbox> documentation. | ||
| 294 | |||
| 295 | =back | ||
| 296 | |||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | =head1 METHODS | ||
| 301 | |||
| 302 | =over 4 | ||
| 303 | |||
| 304 | =item * B<new> ( HASH ) | ||
| 305 | |||
| 306 | =item * B<layout> ( ) | ||
| 307 | |||
| 308 | =item * B<draw> ( BOOLEAN ) | ||
| 309 | |||
| 310 | =item * B<focus> ( ) | ||
| 311 | |||
| 312 | These are standard methods. See L<Curses::UI::Container|Curses::UI::Container> | ||
| 313 | for an explanation of these. | ||
| 314 | |||
| 315 | =item * B<get> ( ) | ||
| 316 | |||
| 317 | This method will call B<get> on the TextEntry object of the dialog | ||
| 318 | and return its returnvalue. See L<Curses::UI::TextEntry> for more | ||
| 319 | information on this. If the cancel button was pressed, the return | ||
| 320 | value will be undef. | ||
| 321 | |||
| 322 | =back | ||
| 323 | |||
| 324 | |||
| 325 | |||
| 326 | |||
| 327 | =head1 SEE ALSO | ||
| 328 | |||
| 329 | L<Curses::UI|Curses::UI>, | ||
| 330 | L<Curses::UI::Container|Curses::UI::Container>, | ||
| 331 | L<Curses::UI::Buttonbox|Curses::UI::Buttonbox> | ||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | =head1 AUTHOR | ||
| 337 | |||
| 338 | Copyright (c) 2004 Luke Closs <lukec@activestate.com>. All rights reserved. | ||
| 339 | |||
| 340 | Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de) | ||
| 341 | |||
| 342 | |||
| 343 | This package is free software and is provided "as is" without express | ||
| 344 | or implied warranty. It may be used, redistributed and/or modified | ||
| 345 | under the same terms as perl itself. | ||
| 346 |