| 1 | package Tk::PopList; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::PopList - Popping a selection list relative to a widget | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use vars qw($VERSION); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | $VERSION = '0.14'; | ||
| 13 | |||
| 14 | 1 | ▶ | use base qw(Tk::Derived Tk::Poplevel); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | |||
| 16 | use Tk; | ||
| 17 | require Tk::HList; | ||
| 18 | |||
| 19 | Construct Tk::Widget 'PopList'; | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | require Tk::PopList; | ||
| 24 | my $list = $window->PopList(@options, | ||
| 25 | -values => [qw/value1 value2 value3 value4/], | ||
| 26 | -widget => $somewidget, | ||
| 27 | ); | ||
| 28 | $list->popUp; | ||
| 29 | |||
| 30 | =head1 DESCRIPTION | ||
| 31 | |||
| 32 | Inherits L<Tk::Poplevel> | ||
| 33 | |||
| 34 | This widget pops a listbox relative to the widget specified in the B<-widget> option. | ||
| 35 | It aligns its size and position to the widget. | ||
| 36 | |||
| 37 | You can specify B<-selectcall> to do something when you select an item. It gets the selected | ||
| 38 | item as parameter. | ||
| 39 | |||
| 40 | You can use the escape key to hide the list. | ||
| 41 | You can use the return key to select an item. | ||
| 42 | |||
| 43 | =head1 OPTIONS | ||
| 44 | |||
| 45 | =over 4 | ||
| 46 | |||
| 47 | =item B<-filter> | ||
| 48 | |||
| 49 | Default value 0 | ||
| 50 | |||
| 51 | Specifies if a filter entry is added. Practical for a long list of values. | ||
| 52 | |||
| 53 | =item B<-motionselect> | ||
| 54 | |||
| 55 | Default value 1 | ||
| 56 | |||
| 57 | When set hoovering over a list item selects it. | ||
| 58 | |||
| 59 | =item B<-nofocus> | ||
| 60 | |||
| 61 | Default value false. If set the list widget will not take focus when popping up. | ||
| 62 | |||
| 63 | =item B<-selectcall> | ||
| 64 | |||
| 65 | Callback, called when a list item is selected. | ||
| 66 | |||
| 67 | =item B<-values> | ||
| 68 | |||
| 69 | List of possible values. | ||
| 70 | |||
| 71 | =back | ||
| 72 | |||
| 73 | =head1 KEYBINDINGS | ||
| 74 | |||
| 75 | =over 4 | ||
| 76 | |||
| 77 | <Down> Moves selection to the next item in the list. | ||
| 78 | <End> Moves selection to the last item in the list. | ||
| 79 | <Escape> Hides the poplist. | ||
| 80 | <Home> Moves selection to the first item in the list. | ||
| 81 | <Return> Selects the current selection and hides the poplist. | ||
| 82 | <Up> Moves selection to the previous item in the list. | ||
| 83 | |||
| 84 | =back | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | =head1 METHODS | ||
| 89 | |||
| 90 | =over 4 | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub Populate { | ||
| 95 | my ($self,$args) = @_; | ||
| 96 | |||
| 97 | $self->SUPER::Populate($args); | ||
| 98 | |||
| 99 | $self->{FE} = undef; | ||
| 100 | |||
| 101 | my $list = $self->Scrolled('HList', | ||
| 102 | -browsecmd => ['Select', $self], | ||
| 103 | -command => ['Select', $self], | ||
| 104 | -highlightthickness => 0, | ||
| 105 | -scrollbars => 'oe', | ||
| 106 | -selectmode => 'single', | ||
| 107 | )->pack(-expand => 1, -fill => 'both'); | ||
| 108 | $list->bind('<Down>', [$self, 'NavDown']); | ||
| 109 | $list->bind('<End>', [$self, 'NavLast']); | ||
| 110 | $list->bind('<Home>', [$self, 'NavFirst']); | ||
| 111 | $list->bind('<Up>', [$self, 'NavUp']); | ||
| 112 | $list->bind('<Escape>', [$self, 'popDown']); | ||
| 113 | $list->bind('<Motion>', [$self, 'MotionSelect', Ev('x'), Ev('y')]); | ||
| 114 | |||
| 115 | $self->Advertise(List => $list); | ||
| 116 | |||
| 117 | $self->ConfigSpecs( | ||
| 118 | -background => ['SELF', 'DESCENDATNS'], | ||
| 119 | -filter => ['PASSIVE', undef, undef, 0], | ||
| 120 | -maxheight => ['PASSIVE', undef, undef, 10], | ||
| 121 | -motionselect => ['PASSIVE', undef, undef, 1], | ||
| 122 | -nofocus => ['PASSIVE', undef, undef, 0], | ||
| 123 | -selectcall => ['CALLBACK', undef, undef, sub {}], | ||
| 124 | -values => ['METHOD', undef, undef, []], | ||
| 125 | DEFAULT => [ $list ], | ||
| 126 | ); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub calculateHeight { | ||
| 130 | my $self = shift; | ||
| 131 | my $list = $self->cget('-values'); | ||
| 132 | my $lb = $self->Subwidget('List'); | ||
| 133 | my $lheight = $self->cget('-maxheight'); | ||
| 134 | if (@$list < $lheight) { $lheight = @$list } | ||
| 135 | my $font = $lb->cget('-font'); | ||
| 136 | my $lineheight = $self->fontMetrics($font, '-linespace') * 1.50; | ||
| 137 | |||
| 138 | my $height = int($lheight * $lineheight) + 2; | ||
| 139 | $height = $height + $self->{FE}->reqheight + 2 if defined $self->{FE}; | ||
| 140 | return $height | ||
| 141 | } | ||
| 142 | |||
| 143 | =item B<filter>I<($filter)> | ||
| 144 | |||
| 145 | Filters the list of values on $filter. | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub filter { | ||
| 150 | my ($self, $filter) = @_; | ||
| 151 | $filter = quotemeta($filter); | ||
| 152 | my $l = $self->Subwidget('List'); | ||
| 153 | my $values = $self->cget('-values'); | ||
| 154 | for (0 .. @$values - 1) { | ||
| 155 | my $val = $l->entrycget($_, '-text'); | ||
| 156 | if ($val =~ /$filter/i) { | ||
| 157 | $l->show(-entry => $_) | ||
| 158 | } else { | ||
| 159 | $l->hide(-entry => $_) | ||
| 160 | } | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | sub MotionSelect { | ||
| 165 | my ($self, $x, $y) = @_; | ||
| 166 | return unless $self->cget('-motionselect'); | ||
| 167 | my $list = $self->Subwidget('List'); | ||
| 168 | $list->selectionClear; | ||
| 169 | my $i = $list->nearest($y); | ||
| 170 | $list->selectionSet($i); | ||
| 171 | $list->anchorSet($i); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub NavDown { | ||
| 175 | my $self = shift; | ||
| 176 | my $l = $self->Subwidget('List'); | ||
| 177 | my ($sel) = $l->infoSelection; | ||
| 178 | my $val = $self->cget('-values'); | ||
| 179 | my $last = @$val - 1; | ||
| 180 | $sel ++; | ||
| 181 | return if $sel > $last; | ||
| 182 | $sel ++ while ($l->infoHidden($sel)) and ($sel < $last); | ||
| 183 | unless ($sel > $last) { | ||
| 184 | $l->selectionClear; | ||
| 185 | $l->selectionSet($sel); | ||
| 186 | $l->anchorSet($sel); | ||
| 187 | $l->see($sel); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub NavFirst { | ||
| 192 | my $self = shift; | ||
| 193 | my $l = $self->Subwidget('List'); | ||
| 194 | $l->selectionClear; | ||
| 195 | $l->selectionSet(0); | ||
| 196 | $l->anchorSet(0); | ||
| 197 | $l->see(0); | ||
| 198 | } | ||
| 199 | |||
| 200 | sub NavLast { | ||
| 201 | my $self = shift; | ||
| 202 | my $l = $self->Subwidget('List'); | ||
| 203 | my $val = $self->cget('-values'); | ||
| 204 | my $last = @$val - 1; | ||
| 205 | $l->selectionClear; | ||
| 206 | $l->selectionSet($last); | ||
| 207 | $l->anchorSet($last); | ||
| 208 | $l->see($last); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub NavUp { | ||
| 212 | my $self = shift; | ||
| 213 | my $l = $self->Subwidget('List'); | ||
| 214 | my ($sel) = $l->infoSelection; | ||
| 215 | $sel--; | ||
| 216 | return if $sel < 0; | ||
| 217 | $sel -- while ($l->infoHidden($sel)) and ($sel >= 0); | ||
| 218 | unless ($self < 0) { | ||
| 219 | $l->selectionClear; | ||
| 220 | $l->selectionSet($sel); | ||
| 221 | $l->anchorSet($sel); | ||
| 222 | $l->see($sel); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | sub popDown { | ||
| 227 | my $self = shift; | ||
| 228 | return unless $self->ismapped; | ||
| 229 | my $e = $self->{FE}; | ||
| 230 | if (defined $e) { | ||
| 231 | $e->packForget; | ||
| 232 | $e->destroy; | ||
| 233 | $self->{FE} = undef; | ||
| 234 | } | ||
| 235 | $self->SUPER::popDown; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub popUp { | ||
| 239 | my $self = shift; | ||
| 240 | |||
| 241 | return if $self->ismapped; | ||
| 242 | |||
| 243 | my $e; | ||
| 244 | if ($self->cget('-filter')) { | ||
| 245 | my $var = 'Filter'; | ||
| 246 | $e = $self->Entry( | ||
| 247 | -textvariable => \$var, | ||
| 248 | ); | ||
| 249 | $e->bind('<FocusIn>', sub { $var = '' }); | ||
| 250 | $e->bind('<Escape>', [$self, 'popDown']); | ||
| 251 | $e->bind('<Key>', sub { $self->filter($e->get) }); | ||
| 252 | $self->{FE} = $e; | ||
| 253 | } | ||
| 254 | |||
| 255 | $self->calculateHeight; | ||
| 256 | $self->SUPER::popUp; | ||
| 257 | |||
| 258 | my $lb = $self->Subwidget('List'); | ||
| 259 | $lb->selectionClear; | ||
| 260 | $lb->anchorClear; | ||
| 261 | # $lb->selectionSet(0) if $lb->infoExists(0); | ||
| 262 | $lb->focus unless $self->cget('-nofocus'); | ||
| 263 | |||
| 264 | my @filterpack = (); | ||
| 265 | my $direction = $self->popDirection; | ||
| 266 | if ($direction eq 'up') { | ||
| 267 | @filterpack = (-after => $lb); | ||
| 268 | } elsif ($direction eq 'down') { | ||
| 269 | @filterpack = (-before => $lb); | ||
| 270 | } | ||
| 271 | $e->pack(@filterpack, | ||
| 272 | -fill => 'x' | ||
| 273 | ) if defined $e; | ||
| 274 | $self->raise; | ||
| 275 | $self->update; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub Select { | ||
| 279 | my $self = shift; | ||
| 280 | |||
| 281 | my $list = $self->Subwidget('List'); | ||
| 282 | |||
| 283 | my ($item) = $list->infoSelection; | ||
| 284 | if ((defined $item) and ($item ne '')) { | ||
| 285 | $item = $list->entrycget($item, '-text'); | ||
| 286 | $self->Callback('-selectcall', $item); | ||
| 287 | } | ||
| 288 | $self->popDown; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub values { | ||
| 292 | my $self = shift; | ||
| 293 | my $l = $self->Subwidget('List'); | ||
| 294 | if (@_) { | ||
| 295 | my $new = shift; | ||
| 296 | $l->deleteAll; | ||
| 297 | my $count = 0; | ||
| 298 | for (@$new) { | ||
| 299 | $l->add($count, -text => $_); | ||
| 300 | $count ++; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | my @v = $l->infoChildren(''); | ||
| 304 | my @values = (); | ||
| 305 | for (@v) { | ||
| 306 | push @values, $l->entrycget($_, '-text'); | ||
| 307 | } | ||
| 308 | return \@values | ||
| 309 | } | ||
| 310 | |||
| 311 | =back | ||
| 312 | |||
| 313 | =head1 LICENSE | ||
| 314 | |||
| 315 | Same as Perl. | ||
| 316 | |||
| 317 | =head1 AUTHOR | ||
| 318 | |||
| 319 | Hans Jeuken (hanje at cpan dot org) | ||
| 320 | |||
| 321 | =head1 BUGS | ||
| 322 | |||
| 323 | Unknown. If you find any, please contact the author. | ||
| 324 | |||
| 325 | =head1 SEE ALSO | ||
| 326 | |||
| 327 | =over 4 | ||
| 328 | |||
| 329 | =item L<Tk> | ||
| 330 | |||
| 331 | =item L<Tk::Poplevel> | ||
| 332 | |||
| 333 | =item L<Tk::Toplevel> | ||
| 334 | |||
| 335 | =item L<Tk::HList> | ||
| 336 | |||
| 337 | =back | ||
| 338 | |||
| 339 | =cut | ||
| 340 | |||
| 341 | 1; | ||
| 342 | __END__ | ||