| 1 | # ---------------------------------------------------------------------- | ||
| 2 | # Curses::UI::Popupmenu | ||
| 3 | # Curses::UI::PopupmenuListbox | ||
| 4 | # | ||
| 5 | # (c) 2001-2002 by Maurice Makaay. All rights reserved. | ||
| 6 | # This file is part of Curses::UI. Curses::UI is free software. | ||
| 7 | # You can redistribute it and/or modify it under the same terms | ||
| 8 | # as perl itself. | ||
| 9 | # | ||
| 10 | # Currently maintained by Marcus Thiesen | ||
| 11 | # e-mail: marcus@cpan.thiesenweb.de | ||
| 12 | # ---------------------------------------------------------------------- | ||
| 13 | |||
| 14 | # TODO: fix dox | ||
| 15 | |||
| 16 | # ---------------------------------------------------------------------- | ||
| 17 | # Windowable listbox | ||
| 18 | # ---------------------------------------------------------------------- | ||
| 19 | |||
| 20 | package Curses::UI::PopupmenuListbox; | ||
| 21 | |||
| 22 | use Curses; | ||
| 23 | use Curses::UI::Listbox; | ||
| 24 | use Curses::UI::Window; | ||
| 25 | use Curses::UI::Common; | ||
| 26 | |||
| 27 | use vars qw( | ||
| 28 | $VERSION | ||
| 29 | @ISA | ||
| 30 | ); | ||
| 31 | |||
| 32 | $VERSION = '1.0011'; | ||
| 33 | |||
| 34 | @ISA = qw( | ||
| 35 | Curses::UI::Listbox | ||
| 36 | Curses::UI::Window | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub new() | ||
| 40 | { | ||
| 41 | my $class = shift; | ||
| 42 | my $this = $class->SUPER::new(@_); | ||
| 43 | |||
| 44 | # Do own option_select() method. | ||
| 45 | $this->set_routine('option-select', \&option_select); | ||
| 46 | |||
| 47 | return $this; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub option_select() | ||
| 51 | { | ||
| 52 | my $this = shift; | ||
| 53 | $this->SUPER::option_select(); | ||
| 54 | $this->loose_focus; | ||
| 55 | return $this; | ||
| 56 | } | ||
| 57 | |||
| 58 | # Let Curses::UI->usemodule() believe that this module | ||
| 59 | # was already loaded (usemodule() would else try to | ||
| 60 | # require the non-existing file). | ||
| 61 | # | ||
| 62 | $INC{'Curses/UI/PopupmenuListbox.pm'} = $INC{'Curses/UI/Popupmenu.pm'}; | ||
| 63 | |||
| 64 | |||
| 65 | # ---------------------------------------------------------------------- | ||
| 66 | # The Popupmenu | ||
| 67 | # ---------------------------------------------------------------------- | ||
| 68 | |||
| 69 | package Curses::UI::Popupmenu; | ||
| 70 | |||
| 71 | use strict; | ||
| 72 | use Curses; | ||
| 73 | use Curses::UI::Common; | ||
| 74 | use Curses::UI::Widget; | ||
| 75 | use Curses::UI::Listbox; # for maxlabelwidth() | ||
| 76 | |||
| 77 | use vars qw( | ||
| 78 | $VERSION | ||
| 79 | @ISA | ||
| 80 | ); | ||
| 81 | |||
| 82 | $VERSION = '1.10'; | ||
| 83 | |||
| 84 | @ISA = qw( | ||
| 85 | Curses::UI::Widget | ||
| 86 | ); | ||
| 87 | |||
| 88 | my %routines = ( | ||
| 89 | 'loose-focus' => \&loose_focus, | ||
| 90 | 'open-popup' => \&open_popup, | ||
| 91 | 'select-next' => \&select_next, | ||
| 92 | 'select-prev' => \&select_prev, | ||
| 93 | 'mouse-button1' => \&mouse_button1, | ||
| 94 | ); | ||
| 95 | |||
| 96 | my %bindings = ( | ||
| 97 | CUI_TAB() => 'loose-focus', | ||
| 98 | KEY_BTAB() => 'loose-focus', | ||
| 99 | KEY_ENTER() => 'open-popup', | ||
| 100 | KEY_RIGHT() => 'open-popup', | ||
| 101 | "l" => 'open-popup', | ||
| 102 | CUI_SPACE() => 'open-popup', | ||
| 103 | KEY_DOWN() => 'select-next', | ||
| 104 | "j" => 'select-next', | ||
| 105 | KEY_UP() => 'select-prev', | ||
| 106 | "k" => 'select-prev', | ||
| 107 | ); | ||
| 108 | |||
| 109 | sub new () | ||
| 110 | { | ||
| 111 | my $class = shift; | ||
| 112 | |||
| 113 | my %userargs = @_; | ||
| 114 | keys_to_lowercase(\%userargs); | ||
| 115 | |||
| 116 | my %args = ( | ||
| 117 | -parent => undef, # the parent window | ||
| 118 | -width => undef, # the width of the checkbox | ||
| 119 | -x => 0, # the horizontal position rel. to parent | ||
| 120 | -y => 0, # the vertical position rel. to parent | ||
| 121 | -values => [], # values | ||
| 122 | -labels => {}, # labels for the values | ||
| 123 | -selected => undef, # the current selected value | ||
| 124 | -wraparound => undef, # wraparound? | ||
| 125 | -sbborder => 1, # square bracket border | ||
| 126 | -onchange => undef, # event handler | ||
| 127 | -fg => -1, | ||
| 128 | -bg => -1, | ||
| 129 | |||
| 130 | %userargs, | ||
| 131 | |||
| 132 | -bindings => {%bindings}, | ||
| 133 | -routines => {%routines}, | ||
| 134 | |||
| 135 | -focus => 0, # value init | ||
| 136 | -nocursor => 1, # this widget does not use a cursor | ||
| 137 | ); | ||
| 138 | |||
| 139 | # The windowscr height should be 1. | ||
| 140 | $args{-height} = height_by_windowscrheight(1,%args); | ||
| 141 | |||
| 142 | # No width given? Then make the width large | ||
| 143 | # enough to contain the longest label. | ||
| 144 | $args{-width} = width_by_windowscrwidth( | ||
| 145 | maxlabelwidth(%args) + 1, | ||
| 146 | -border => 1 | ||
| 147 | ) unless defined $args{-width}; | ||
| 148 | |||
| 149 | my $this = $class->SUPER::new( %args ); | ||
| 150 | |||
| 151 | $this->layout; | ||
| 152 | |||
| 153 | if ($Curses::UI::ncurses_mouse) { | ||
| 154 | $this->set_mouse_binding('mouse-button1', BUTTON1_CLICKED()); | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | return $this; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub onChange(;$) { shift()->set_event('-onchange', shift()) } | ||
| 162 | |||
| 163 | sub layout() | ||
| 164 | { | ||
| 165 | my $this = shift; | ||
| 166 | |||
| 167 | $this->SUPER::layout() or return; | ||
| 168 | |||
| 169 | # Compute the location and length of the listbox. | ||
| 170 | my $ll = height_by_windowscrheight(@{$this->{-values}}, -border=>1); | ||
| 171 | my $lx = $this->{-x} + $this->{-parent}->{-sx}; | ||
| 172 | my $ly = $this->{-y} + $this->{-parent}->{-sy} + 1; | ||
| 173 | |||
| 174 | # Don't let the listbox grow out of the screen. | ||
| 175 | if ($this->{-y}+$ll > $ENV{LINES}) { | ||
| 176 | $ll = $ENV{LINES} - $this->{-y}; | ||
| 177 | } | ||
| 178 | |||
| 179 | # It's a bit small :-( Can we place it up-side-down? | ||
| 180 | my $lim = int($ENV{LINES}/2); | ||
| 181 | if ($ll < $lim and ($this->{-sy}+$this->{-y}) > $lim) { | ||
| 182 | $ll = height_by_windowscrheight( | ||
| 183 | @{$this->{-values}}, | ||
| 184 | -border=>1 | ||
| 185 | ); | ||
| 186 | my $y = $this->{-y}; | ||
| 187 | $y -= $ll - 1; | ||
| 188 | if ($y<0) | ||
| 189 | { | ||
| 190 | $y = 1; | ||
| 191 | $ll = $this->{-y}; | ||
| 192 | } | ||
| 193 | $ly = $y + $this->{-parent}->{-sy} - 1; | ||
| 194 | } | ||
| 195 | |||
| 196 | # Store the listbox layout setup for later use. | ||
| 197 | $this->{-listbox}->{-x} = $lx; | ||
| 198 | $this->{-listbox}->{-y} = $ly; | ||
| 199 | $this->{-listbox}->{-width} = $this->width; | ||
| 200 | $this->{-listbox}->{-height} = $ll; | ||
| 201 | |||
| 202 | return $this; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub draw(;$) | ||
| 206 | { | ||
| 207 | my $this = shift; | ||
| 208 | my $no_doupdate = shift || 0; | ||
| 209 | |||
| 210 | # Draw the widget. | ||
| 211 | $this->SUPER::draw(1) or return $this; | ||
| 212 | |||
| 213 | # Get the selected label. | ||
| 214 | my $sellabel; | ||
| 215 | if (defined $this->{-selected}) | ||
| 216 | { | ||
| 217 | $sellabel = $this->{-values}->[$this->{-selected}]; | ||
| 218 | $sellabel = $this->{-labels}->{$sellabel} | ||
| 219 | if defined $this->{-labels}->{$sellabel}; | ||
| 220 | } | ||
| 221 | |||
| 222 | # Let there be color | ||
| 223 | if ($Curses::UI::color_support) { | ||
| 224 | my $co = $Curses::UI::color_object; | ||
| 225 | my $pair = $co->get_color_pair( | ||
| 226 | $this->{-fg}, | ||
| 227 | $this->{-bg}); | ||
| 228 | |||
| 229 | $this->{-canvasscr}->attron(COLOR_PAIR($pair)); | ||
| 230 | |||
| 231 | } | ||
| 232 | |||
| 233 | $this->{-canvasscr}->attron(A_REVERSE) if $this->{-focus}; | ||
| 234 | my $width = $this->canvaswidth; | ||
| 235 | if (defined $sellabel) | ||
| 236 | { | ||
| 237 | if (length($sellabel) > $width) { | ||
| 238 | $sellabel = substr($sellabel, 0, $width); | ||
| 239 | $sellabel =~ s/.$/\$/; | ||
| 240 | } | ||
| 241 | } | ||
| 242 | else # No selection yet. | ||
| 243 | { | ||
| 244 | $this->{-canvasscr}->attron(A_DIM); | ||
| 245 | $sellabel = "-"x$width; | ||
| 246 | } | ||
| 247 | |||
| 248 | $this->{-canvasscr}->addstr(0,0, " "x$width); | ||
| 249 | $this->{-canvasscr}->addstr(0,0, $sellabel); | ||
| 250 | $this->{-canvasscr}->move(0,$this->canvaswidth-1); | ||
| 251 | $this->{-canvasscr}->attroff(A_DIM); | ||
| 252 | $this->{-canvasscr}->attroff(A_REVERSE); | ||
| 253 | |||
| 254 | $this->{-canvasscr}->noutrefresh; | ||
| 255 | doupdate() unless $no_doupdate;; | ||
| 256 | |||
| 257 | return $this; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub open_popup() | ||
| 261 | { | ||
| 262 | my $this = shift; | ||
| 263 | my $pre_value = $this->get; | ||
| 264 | |||
| 265 | my %listbox_options = %{$this->{-listbox}}; | ||
| 266 | foreach my $option (qw( | ||
| 267 | -values -labels | ||
| 268 | -selected -wraparound | ||
| 269 | )) { | ||
| 270 | $listbox_options{$option} = $this->{$option} | ||
| 271 | if defined $this->{$option}; | ||
| 272 | } | ||
| 273 | |||
| 274 | my $id = '__popupmenu_listbox_$this'; | ||
| 275 | my $listbox = $this->root->add( | ||
| 276 | $id, 'PopupmenuListbox', | ||
| 277 | -border => 1, | ||
| 278 | -vscrollbar => 1, | ||
| 279 | %listbox_options | ||
| 280 | ); | ||
| 281 | |||
| 282 | $listbox->modalfocus; | ||
| 283 | |||
| 284 | my $post_value = $listbox->get; | ||
| 285 | $this->{-selected} = $listbox->{-selected}; | ||
| 286 | |||
| 287 | if ((not defined $pre_value and | ||
| 288 | defined $post_value) or | ||
| 289 | (defined $pre_value and | ||
| 290 | $pre_value ne $post_value)) { | ||
| 291 | $this->run_event('-onchange'); | ||
| 292 | } | ||
| 293 | |||
| 294 | $this->root->delete($id); | ||
| 295 | $this->root->draw; | ||
| 296 | |||
| 297 | return $this; | ||
| 298 | } | ||
| 299 | |||
| 300 | sub get() | ||
| 301 | { | ||
| 302 | my $this = shift; | ||
| 303 | |||
| 304 | my $value; | ||
| 305 | if (defined $this->{-selected}) { | ||
| 306 | $value = $this->{-values}->[$this->{-selected}]; | ||
| 307 | } | ||
| 308 | |||
| 309 | return $value; | ||
| 310 | } | ||
| 311 | |||
| 312 | sub select_next() | ||
| 313 | { | ||
| 314 | my $this = shift; | ||
| 315 | |||
| 316 | my $pre_value = $this->get; | ||
| 317 | |||
| 318 | if (defined $this->{-selected}) { | ||
| 319 | $this->{-selected}++; | ||
| 320 | if ( $this->{-selected} > (@{$this->{-values}}-1) ) { | ||
| 321 | $this->{-selected} = @{$this->{-values}} - 1; | ||
| 322 | } | ||
| 323 | } else { | ||
| 324 | $this->{-selected} = 0; | ||
| 325 | } | ||
| 326 | |||
| 327 | my $post_value = $this->get; | ||
| 328 | |||
| 329 | if ((not defined $pre_value and defined $post_value) or | ||
| 330 | (defined $pre_value and $pre_value ne $post_value)) { | ||
| 331 | $this->run_event('-onchange'); | ||
| 332 | } | ||
| 333 | |||
| 334 | $this->schedule_draw(1); | ||
| 335 | |||
| 336 | return $this; | ||
| 337 | } | ||
| 338 | |||
| 339 | sub select_prev() | ||
| 340 | { | ||
| 341 | my $this = shift; | ||
| 342 | |||
| 343 | my $pre_value = $this->get; | ||
| 344 | |||
| 345 | if (defined $this->{-selected}) { | ||
| 346 | $this->{-selected}--; | ||
| 347 | $this->{-selected} = 0 if $this->{-selected} <= 0; | ||
| 348 | } else { | ||
| 349 | $this->{-selected} = @{$this->{-values}} - 1; | ||
| 350 | } | ||
| 351 | |||
| 352 | my $post_value = $this->get; | ||
| 353 | |||
| 354 | if ((not defined $pre_value and defined $post_value) or | ||
| 355 | (defined $pre_value and $pre_value ne $post_value)) { | ||
| 356 | $this->run_event('-onchange'); | ||
| 357 | } | ||
| 358 | |||
| 359 | $this->schedule_draw(1); | ||
| 360 | |||
| 361 | return $this; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub mouse_button1($$$;) | ||
| 365 | { | ||
| 366 | my $this = shift; | ||
| 367 | my $event = shift; | ||
| 368 | my $x = shift; | ||
| 369 | my $y = shift; | ||
| 370 | |||
| 371 | unless ($this->{-focus}) { | ||
| 372 | $this->focus; | ||
| 373 | } | ||
| 374 | $this->open_popup; | ||
| 375 | } | ||
| 376 | |||
| 377 | |||
| 378 | 1; | ||
| 379 | |||
| 380 | |||
| 381 | =pod | ||
| 382 | |||
| 383 | =head1 NAME | ||
| 384 | |||
| 385 | Curses::UI::Popupmenu - Create and manipulate popupbox widgets | ||
| 386 | |||
| 387 | =head1 CLASS HIERARCHY | ||
| 388 | |||
| 389 | Curses::UI::Widget | ||
| 390 | | | ||
| 391 | +----Curses::UI::Popupmenu | ||
| 392 | |||
| 393 | |||
| 394 | =head1 SYNOPSIS | ||
| 395 | |||
| 396 | use Curses::UI; | ||
| 397 | my $cui = new Curses::UI; | ||
| 398 | my $win = $cui->add('window_id', 'Window'); | ||
| 399 | |||
| 400 | my $popupbox = $win->add( | ||
| 401 | 'mypopupbox', 'Popupmenu', | ||
| 402 | -values => [1, 2, 3], | ||
| 403 | -labels => { 1 => 'One', | ||
| 404 | 2 => 'Two', | ||
| 405 | 3 => 'Three' }, | ||
| 406 | ); | ||
| 407 | |||
| 408 | $popupbox->focus(); | ||
| 409 | my $value = $popupbox->get(); | ||
| 410 | |||
| 411 | |||
| 412 | =head1 DESCRIPTION | ||
| 413 | |||
| 414 | Curses::UI::Popupmenu is a widget that can be used to create | ||
| 415 | something very similar to a basic L<Curses::UI::Listbox|Curses::UI::Listbox>. | ||
| 416 | The difference is that the widget will show only the | ||
| 417 | currently selected value (or "-------" if no value is yet | ||
| 418 | selected). The list of possible values will be shown as a | ||
| 419 | separate popup window if requested. | ||
| 420 | |||
| 421 | Normally the widget will look something like this: | ||
| 422 | |||
| 423 | [Current value ] | ||
| 424 | |||
| 425 | If the popup window is opened, it looks something like this: | ||
| 426 | |||
| 427 | |||
| 428 | [Current value ] | ||
| 429 | +--------------+ | ||
| 430 | |Other value | | ||
| 431 | |Current value | | ||
| 432 | |Third value | | ||
| 433 | +--------------+ | ||
| 434 | |||
| 435 | |||
| 436 | |||
| 437 | |||
| 438 | =head1 STANDARD OPTIONS | ||
| 439 | |||
| 440 | B<-parent>, B<-x>, B<-y>, B<-width>, B<-height>, | ||
| 441 | B<-pad>, B<-padleft>, B<-padright>, B<-padtop>, B<-padbottom>, | ||
| 442 | B<-ipad>, B<-ipadleft>, B<-ipadright>, B<-ipadtop>, B<-ipadbottom>, | ||
| 443 | B<-title>, B<-titlefullwidth>, B<-titlereverse>, B<-onfocus>, | ||
| 444 | B<-onblur> | ||
| 445 | |||
| 446 | For an explanation of these standard options, see | ||
| 447 | L<Curses::UI::Widget|Curses::UI::Widget>. | ||
| 448 | |||
| 449 | |||
| 450 | |||
| 451 | |||
| 452 | =head1 WIDGET-SPECIFIC OPTIONS | ||
| 453 | |||
| 454 | =over 4 | ||
| 455 | |||
| 456 | =item * B<-values> < LIST > | ||
| 457 | |||
| 458 | =item * B<-labels> < HASHREF > | ||
| 459 | |||
| 460 | =item * B<-selected> < INDEX > | ||
| 461 | |||
| 462 | =item * B<-wraparound> < BOOLEAN > | ||
| 463 | |||
| 464 | These options are exactly the same as the options for | ||
| 465 | the Listbox widget. So for an explanation of these, | ||
| 466 | take a look at L<Curses::UI::Listbox|Curses::UI::Listbox>. | ||
| 467 | |||
| 468 | =item * B<-onchange> < CODEREF > | ||
| 469 | |||
| 470 | This sets the onChange event handler for the popupmenu widget. | ||
| 471 | If a new item is selected, the code in CODEREF will be executed. | ||
| 472 | It will get the widget reference as its argument. | ||
| 473 | |||
| 474 | |||
| 475 | =back | ||
| 476 | |||
| 477 | |||
| 478 | |||
| 479 | |||
| 480 | =head1 METHODS | ||
| 481 | |||
| 482 | =over 4 | ||
| 483 | |||
| 484 | =item * B<new> ( OPTIONS ) | ||
| 485 | |||
| 486 | =item * B<layout> ( ) | ||
| 487 | |||
| 488 | =item * B<draw> ( BOOLEAN ) | ||
| 489 | |||
| 490 | =item * B<intellidraw> ( ) | ||
| 491 | |||
| 492 | =item * B<focus> ( ) | ||
| 493 | |||
| 494 | =item * B<onFocus> ( CODEREF ) | ||
| 495 | |||
| 496 | =item * B<onBlur> ( CODEREF ) | ||
| 497 | |||
| 498 | These are standard methods. See L<Curses::UI::Widget|Curses::UI::Widget> | ||
| 499 | for an explanation of these. | ||
| 500 | |||
| 501 | =item * B<get> ( ) | ||
| 502 | |||
| 503 | This method will return the currently selected value. | ||
| 504 | |||
| 505 | =item * B<onChange> ( CODEREF ) | ||
| 506 | |||
| 507 | This method can be used to set the B<-onchange> event handler | ||
| 508 | (see above) after initialization of the popupmenu. | ||
| 509 | |||
| 510 | =back | ||
| 511 | |||
| 512 | |||
| 513 | |||
| 514 | |||
| 515 | =head1 DEFAULT BINDINGS | ||
| 516 | |||
| 517 | There are bindings for the widget itself and bindings | ||
| 518 | for the popup listbox that can be opened by this widget. | ||
| 519 | |||
| 520 | =head2 The widget itself | ||
| 521 | |||
| 522 | =over 4 | ||
| 523 | |||
| 524 | =item * <B<tab>> | ||
| 525 | |||
| 526 | Call the 'loose-focus' routine. This will have the widget | ||
| 527 | loose its focus. | ||
| 528 | |||
| 529 | =item * <B<enter>>, <B<cursor-right>, <B<l>>, <B<space>> | ||
| 530 | |||
| 531 | Call the 'open-popup' routine. This will show the | ||
| 532 | popup listbox and bring the focus to this listbox. See | ||
| 533 | B<The popup listbox> below for a description of the bindings | ||
| 534 | for this listbox. | ||
| 535 | |||
| 536 | =item * <B<cursor-down>>, <B<j>> | ||
| 537 | |||
| 538 | Call the 'select-next' routine. This will select the | ||
| 539 | item in the list that is after the currently selected | ||
| 540 | item (unless the last item is already selected). If | ||
| 541 | no item is selected, the first item in the list will | ||
| 542 | get selected. | ||
| 543 | |||
| 544 | =item * <B<cursor-up>>, <B<k>> | ||
| 545 | |||
| 546 | Call the 'select-prev' routine. This will select the | ||
| 547 | item in the list that is before the currently selected | ||
| 548 | item (unless the first item is already selected). If | ||
| 549 | no item is selected, the first item in the list will | ||
| 550 | get selected. | ||
| 551 | |||
| 552 | =back | ||
| 553 | |||
| 554 | =head2 The popup listbox | ||
| 555 | |||
| 556 | The bindings for the popup listbox are the same as the bindings | ||
| 557 | for the Listbox widget. So take a look at | ||
| 558 | L<Curses::UI::Listbox|Curses::UI::Listbox> for a description | ||
| 559 | of these. The difference is that the 'loose-focus' and 'option-select' | ||
| 560 | routine will have the popup listbox to close. If the routine | ||
| 561 | 'option-select' is called, the active item will get selected. | ||
| 562 | |||
| 563 | |||
| 564 | =head1 SEE ALSO | ||
| 565 | |||
| 566 | L<Curses::UI>, | ||
| 567 | L<Curses::UI::Listbox> | ||
| 568 | L<Curses::UI::Widget>, | ||
| 569 | L<Curses::UI::Common> | ||
| 570 | |||
| 571 | |||
| 572 | |||
| 573 | |||
| 574 | =head1 AUTHOR | ||
| 575 | |||
| 576 | Copyright (c) 2001-2002 Maurice Makaay. All rights reserved. | ||
| 577 | |||
| 578 | Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de) | ||
| 579 | |||
| 580 | |||
| 581 | This package is free software and is provided "as is" without express | ||
| 582 | or implied warranty. It may be used, redistributed and/or modified | ||
| 583 | under the same terms as perl itself. | ||
| 584 |