Debian Codegraph @

Tixish/BrowseEntry.pm

raw
1 #
2 # BrowseEntry is a stripped down version of ComboBox.tcl from Tix4.0
3 #
4 # Some additions by Slaven Rezic <[email protected]> to make the widget
5 # look like the Windows' Combobox. There are also additional options.
6 #
7
8 package Tk::BrowseEntry;
9
10 use vars qw($VERSION);
11 $VERSION = '4.015'; # was: sprintf '4.%03d', q$Revision: #13 $ =~ /\D(\d+)\s*$/;
12
13 use Tk qw(Ev);
14 use Carp;
15 use strict;
16
17 use base qw(Tk::Frame);
18 Construct Tk::Widget 'BrowseEntry';
19
20 require Tk::LabEntry;
21
22 sub LabEntryWidget { "LabEntry" }
23 sub ButtonWidget { "Button" }
24 sub ListboxWidget { "Listbox" }
25
26 sub Populate {
27 my ($w, $args) = @_;
28
29 my %labelArgs;
30 while(my($k,$v) = each %$args) {
31 $labelArgs{$k} = $v;
32 delete $args->{$k};
33 }
34
35 $w->Tk::Frame::Populate($args);
36
37 while(my($k,$v) = each %labelArgs) {
38 $args->{$k} = $v;
39 }
40
41 # entry widget and arrow button
42 my $lpack = delete $args->{-labelPack};
43 if (not defined $lpack) {
44 $lpack = [-side => 'left', -anchor => 'e'];
45 }
46 $w->{_BE_Style} = delete $args->{-style} || $Tk::platform;
47 my $LabEntry = $w->LabEntryWidget;
48 my $Listbox = $w->ListboxWidget;
49 my $Button = $w->ButtonWidget;
50 # XXX should this be retained?
51 # if (defined $args->{-state} and $args->{-state} eq 'readonly') { # XXX works only at construction time
52 # $LabEntry = "NoSelLabEntry";
53 # require Tk::NoSelLabEntry;
54 # }
55 my $e;
56 my $var = "";
57 my @LabEntry_args = (-textvariable => \$var);
58 if (exists $args->{-label}) {
59 $e = $w->$LabEntry(-labelPack => $lpack,
60 -label => delete $args->{-label},
61 @LabEntry_args,
62 );
63 } else {
64 $e = $w->$LabEntry(@LabEntry_args);
65 }
66 my $b = $w->$Button(-bitmap => '@' . Tk->findINC($w->{_BE_Style} eq 'MSWin32' ? 'arrowdownwin.xbm' : 'cbxarrow.xbm'));
67 $w->Advertise('entry' => $e);
68 $w->Advertise('arrow' => $b);
69
70 # Pack the button to align vertically with the entry widget
71 my @anch;
72 my $edge = {@$lpack}->{-side};
73 push(@anch,-anchor => 's') if ($edge && $edge eq 'top');
74 push(@anch,-anchor => 'n') if ($edge && $edge eq 'bottom');
75 $b->pack(-side => 'right', -padx => 1, @anch);
76
77 $e->pack(-side => 'right', -fill => 'x', -expand => 1); #XXX, -padx => 1);
78
79 # popup shell for listbox with values.
80 my $c = $w->Toplevel(-bd => 2,
81 -relief => ($w->{_BE_Style} eq 'MSWin32'
82 ? "solid" : "raised"));
83 $c->overrideredirect(1);
84 $c->withdraw;
85 my $sl = $c->Scrolled( $Listbox, qw/-selectmode browse -scrollbars oe/ );
86 if ($w->{_BE_Style} eq 'MSWin32' and $Tk::platform eq 'MSWin32') {
87 $sl->configure(-bg => 'SystemWindow', -relief => "flat");
88 }
89 $w->Advertise('choices' => $c);
90 $w->Advertise('slistbox' => $sl);
91 $sl->pack(-expand => 1, -fill => 'both');
92
93 $sl->Subwidget("scrolled")->bind("<Motion>",sub {
94 return unless ($w->{_BE_Style} eq 'MSWin32');
95 my $e = $_[0]->XEvent;
96 my $y = $e->y;
97 my $inx = $sl->nearest($y);
98 if (defined $inx) {
99 $sl->selectionClear(0, "end");
100 $sl->selectionSet($inx);
101 }
102 });
103
104 # other initializations
105 $w->SetBindings;
106 $w->{'_BE_popped'} = 0;
107 $w->Delegates(get => $sl, DEFAULT => $e);
108 $w->ConfigSpecs(
109 -font => [qw/DESCENDANTS font Font/],
110 -listwidth => [qw/PASSIVE listWidth ListWidth/, undef],
111 -listheight => [{-height => $sl}, qw/listHeight ListHeight/, undef],
112 -listcmd => [qw/CALLBACK listCmd ListCmd/, undef],
113 -autolistwidth => [qw/PASSIVE autoListWidth AutoListWidth/, undef],
114 -autolimitheight => [qw/PASSIVE autoLimitHeight AutoLimitHeight 0/],
115 -browsecmd => [qw/CALLBACK browseCmd BrowseCmd/, undef],
116 -browse2cmd => [qw/CALLBACK browse2Cmd Browse2Cmd/, undef],
117 -choices => [qw/METHOD choices Choices/, undef],
118 -state => [qw/METHOD state State normal/],
119 -arrowimage => [ {-image => $b}, qw/arrowImage ArrowImage/, undef],
120 -variable => [ {'-textvariable' => $e} ],
121 -colorstate => [qw/PASSIVE colorState ColorState/, undef],
122 -command => '-browsecmd',
123 -options => '-choices',
124 -label => [qw/PASSIVE label Label/, undef],
125 -labelPack => [qw/PASSIVE labelPack LabelPack/, undef],
126 #-background => [$e, qw/background Background/, undef],
127 #-foreground => [$e, qw/foreground Foreground/, undef],
128 -buttontakefocus => [{-takefocus => $b}, 'buttonTakefocus',
129 'ButtonTakefocus', 1],
130 DEFAULT => [$e] );
131 }
132
133 sub SetBindings {
134 my ($w) = @_;
135
136 my $e = $w->Subwidget('entry');
137 my $b = $w->Subwidget('arrow');
138
139 # set bind tags
140 $w->bindtags([$w, 'Tk::BrowseEntry', $w->toplevel, 'all']);
141 # as we don't bind $e here leave its tags alone ...
142 # $e->bindtags([$e, ref($e), $e->toplevel, 'all']);
143
144 # bindings for the button and entry
145 $b->bind('<1>',[$w,'BtnDown']);
146 $b->toplevel->bind('<ButtonRelease-1>',[$w,'ButtonHack']);
147 $b->bind('<space>',[$w,'space']);
148
149 # bindings for listbox
150 my $sl = $w->Subwidget('slistbox');
151 my $l = $sl->Subwidget('listbox');
152 $l->bind('<ButtonRelease-1>',[$w,'ListboxRelease',Ev('x'),Ev('y')]);
153 $l->bind('<Escape>' => [$w,'LbClose']);
154 $l->bind('<Return>' => [$w,'Return',$l]);
155
156 # allow click outside the popped up listbox to pop it down.
157 $w->bind('<1>','BtnDown');
158 }
159
160 sub space
161 {
162 my $w = shift;
163 $w->BtnDown;
164 $w->{'_BE_savefocus'} = $w->focusCurrent;
165 $w->Subwidget('slistbox')->focus;
166 }
167
168
169 sub ListboxRelease
170 {
171 my ($w,$x,$y) = @_;
172 $w->ButtonHack;
173 $w->LbChoose($x, $y);
174 }
175
176 sub Return
177 {
178 my ($w,$l) = @_;
179 my($x, $y) = $l->bbox($l->curselection);
180 $w->LbChoose($x, $y)
181 }
182
183
184 sub BtnDown {
185 my ($w) = @_;
186 return if $w->cget( '-state' ) eq 'disabled';
187
188 if ($w->{'_BE_popped'}) {
189 $w->Popdown;
190 $w->{'_BE_buttonHack'} = 0;
191 } else {
192 $w->PopupChoices;
193 $w->{'_BE_buttonHack'} = 1;
194 }
195 }
196
197 sub PopupChoices {
198 my ($w) = @_;
199
200 if (!$w->{'_BE_popped'}) {
201 $w->Callback(-listcmd => $w);
202 my $e = $w->Subwidget('entry');
203 my $c = $w->Subwidget('choices');
204 my $s = $w->Subwidget('slistbox');
205 my $a = $w->Subwidget('arrow');
206 my $y1 = ($w->{_BE_Style} eq 'MSWin32'
207 ? $a->rooty + $a->height
208 : $e->rooty + $e->height + 3
209 );
210 my $bd = $c->cget(-bd) + $c->cget(-highlightthickness);
211 # using the real listbox reqheight rather than the
212 # container frame one, which does not change after resizing the
213 # listbox
214 my $ht = $s->Subwidget("scrolled")->reqheight + 2 * $bd;
215 my $x1 = ($w->{_BE_Style} eq 'MSWin32'
216 ? $e->Subwidget("entry")->rootx
217 : $e->rootx
218 );
219 my ($width, $x2);
220 if (defined $w->cget(-listwidth)) {
221 $width = $w->cget(-listwidth);
222 $x2 = $x1 + $width;
223 } else {
224 $x2 = $a->rootx + $a->width;
225 $width = $x2 - $x1;
226 }
227 my $rw = $c->reqwidth;
228 if ($rw < $width) {
229 $rw = $width
230 } else {
231 if ($rw > $width * 3) {
232 $rw = $width * 3;
233 }
234 if ($rw > $w->vrootwidth) {
235 $rw = $w->vrootwidth;
236 }
237 }
238 $width = $rw;
239
240 # if listbox is too far right, pull it back to the left
241 #
242 if ($x2 > $w->vrootwidth) {
243 $x1 = $w->vrootwidth - $width;
244 }
245
246 # if listbox is too far left, pull it back to the right
247 #
248 if ($x1 < 0) {
249 $x1 = 0;
250 }
251
252 # if listbox is below bottom of screen, pull it up.
253 # check the Win32 taskbar, if possible
254 my $rootheight;
255 if ($Tk::platform eq 'MSWin32' and $^O eq 'MSWin32') {
256 eval {
257 require Win32Util; # XXX should not use a non-CPAN widget
258 $rootheight = (Win32Util::screen_region($w))[3];
259 };
260 }
261 if (!defined $rootheight) {
262 $rootheight = $w->vrootheight;
263 }
264
265 my $y2 = $y1 + $ht;
266 if ($y2 > $rootheight) {
267 $y1 = $y1 - $ht - ($e->height - 5);
268 }
269 $c->geometry(sprintf('%dx%d+%d+%d', $rw, $ht, $x1, $y1));
270 $c->deiconify;
271 $c->raise;
272 $e->focus;
273 $w->{'_BE_popped'} = 1;
274
275 # highlight current selection
276 my $current_sel = $e->get;
277 if (defined $current_sel) {
278 my $i = 0;
279 foreach my $str ($s->get(0, "end")) {
280 local $^W = 0; # in case of undefined strings
281 if ($str eq $current_sel) {
282 $s->selectionClear(0, "end");
283 $s->selectionSet($i);
284 last;
285 }
286 $i++;
287 }
288 }
289
290 $c->configure(-cursor => 'arrow');
291 $w->{'_BE_grabinfo'} = $w->grabSave;
292 $w->grabGlobal;
293 }
294 }
295
296 # choose value from listbox if appropriate
297 sub LbChoose {
298 my ($w, $x, $y) = @_;
299 my $l = $w->Subwidget('slistbox')->Subwidget('listbox');
300 if ((($x < 0) || ($x > $l->Width)) ||
301 (($y < 0) || ($y > $l->Height))) {
302 # mouse was clicked outside the listbox... close the listbox
303 $w->LbClose;
304 } else {
305 # select appropriate entry and close the listbox
306 $w->LbCopySelection;
307 $w->Callback(-browsecmd, $w, $w->Subwidget('entry')->get());
308 $w->Callback(-browse2cmd => $w, $w->LbIndex);
309 }
310 }
311
312 # close the listbox after clearing selection
313 sub LbClose {
314 my ($w) = @_;
315 my $l = $w->Subwidget('slistbox')->Subwidget('listbox');
316 $l->selection('clear', 0, 'end');
317 $w->Popdown;
318 }
319
320 # copy the selection to the entry and close listbox
321 sub LbCopySelection {
322 my ($w) = @_;
323 my $index = $w->LbIndex;
324 if (defined $index) {
325 $w->{'_BE_curIndex'} = $index;
326 my $l = $w->Subwidget('slistbox')->Subwidget('listbox');
327 my $var_ref = $w->cget( '-textvariable' );
328 $$var_ref = $l->get($index);
329 if ($w->{'_BE_popped'}) {
330 $w->Popdown;
331 }
332 }
333 $w->Popdown;
334 }
335
336 sub LbIndex {
337 my ($w, $flag) = @_;
338 my ($sel) = $w->Subwidget('slistbox')->Subwidget('listbox')->curselection;
339 if (defined $sel) {
340 return int($sel);
341 } else {
342 if (defined $flag && ($flag eq 'emptyOK')) {
343 return undef;
344 } else {
345 return 0;
346 }
347 }
348 }
349
350 # pop down the listbox
351 sub Popdown {
352 my ($w) = @_;
353 if ($w->{'_BE_savefocus'} && Tk::Exists($w->{'_BE_savefocus'})) {
354 $w->{'_BE_savefocus'}->focus;
355 delete $w->{'_BE_savefocus'};
356 }
357 if ($w->{'_BE_popped'}) {
358 my $c = $w->Subwidget('choices');
359 $c->withdraw;
360 $w->grabRelease;
361 if (ref $w->{'_BE_grabinfo'} eq 'CODE') {
362 $w->{'_BE_grabinfo'}->();
363 delete $w->{'_BE_grabinfo'};
364 }
365 $w->{'_BE_popped'} = 0;
366 }
367 }
368
369 # This hack is to prevent the ugliness of the arrow being depressed.
370 #
371 sub ButtonHack {
372 my ($w) = @_;
373 if ($w->{'_BE_buttonHack'}) {
374 my $b = $w->Subwidget('arrow');
375 if (Tk::Exists($b)) {
376 $b->butUp;
377 }
378 }
379 }
380
381 sub choices
382 {
383 my ($w,$choices) = @_;
384 if (@_ > 1)
385 {
386 $w->delete( qw/0 end/ );
387 my %hash;
388 my $var = $w->cget('-textvariable');
389 my $old = $$var;
390 foreach my $val (@$choices)
391 {
392 local $^W = 0; # in case of undefined values
393 $w->insert( 'end', $val);
394 $hash{$val} = 1;
395 }
396 $old = $choices->[0]
397 if defined $old && !exists $hash{$old} && defined $choices->[0];
398 $$var = $old;
399 }
400 else
401 {
402 return( $w->get( qw/0 end/ ) );
403 }
404 }
405
406 sub _set_edit_state {
407 my( $w, $state ) = @_;
408
409 my $entry = $w->Subwidget( 'entry' );
410 my $button = $w->Subwidget( 'arrow' );
411
412 if ($w->cget( '-colorstate' )) {
413 my $color;
414 if( $state eq 'normal' ) { # Editable
415 $color = 'gray95';
416 } else { # Not Editable
417 $color = $w->cget( -background ) || 'lightgray';
418 }
419 $entry->Subwidget( 'entry' )->configure( -background => $color );
420 }
421
422 if( $state eq 'readonly' ) {
423 $entry->configure( -state => 'disabled' );
424 $button->configure( -state => 'normal' );
425 if ($w->{_BE_Style} eq 'MSWin32') {
426 $entry->bind('<1>',[$w,'BtnDown']);
427 $w->{_BE_OriginalCursor} = $entry->cget( -cursor );
428 $entry->configure( -cursor => 'left_ptr' );
429 }
430 } else {
431 $entry->configure( -state => $state );
432 if (exists $w->{_BE_OriginalCursor}) {
433 $entry->configure(-cursor => delete $w->{_BE_OriginalCursor});
434 }
435 $button->configure( -state => $state );
436 if ($w->{_BE_Style} eq 'MSWin32') {
437 $entry->bind('<1>',['Button1',Tk::Ev('x')]);
438 }
439 }
440 }
441
442 sub state {
443 my $w = shift;
444 unless( @_ ) {
445 return( $w->{Configure}{-state} );
446 } else {
447 my $state = shift;
448 $w->{Configure}{-state} = $state;
449 $w->_set_edit_state( $state );
450 }
451 }
452
453 sub _max {
454 my $max = shift;
455 foreach my $val (@_) {
456 $max = $val if $max < $val;
457 }
458 return( $max );
459 }
460
461 sub shrinkwrap {
462 my( $w, $size ) = @_;
463
464 unless( defined $size ) {
465 $size = _max( map( length, $w->get( qw/0 end/ ) ) ) || 0;;
466 }
467
468 my $lb = $w->Subwidget( 'slistbox' )->Subwidget( 'listbox' );
469 $w->configure( -width => $size );
470 $lb->configure( -width => $size );
471 }
472
473 sub limitheight {
474 my $w = shift;
475 my $choices_number = shift || $w->Subwidget('slistbox')->index("end");
476 $choices_number = 10 if $choices_number > 10;
477 $w->configure(-listheight => $choices_number) if ($choices_number > 0);
478 }
479
480 sub insert {
481 my $w = shift;
482 $w->Subwidget("slistbox")->insert(@_);
483 if ($w->cget(-autolimitheight)) {
484 $w->limitheight;
485 }
486 if ($w->cget(-autolistwidth)) {
487 $w->updateListWidth(@_[1..$#_]);
488 }
489 }
490
491 sub delete {
492 my $w = shift;
493 $w->Subwidget("slistbox")->delete(@_);
494 if ($w->cget(-autolimitheight)) {
495 $w->limitheight;
496 }
497 if ($w->cget(-autolistwidth)) {
498 $w->updateListWidth();
499 }
500 }
501
502 sub updateListWidth {
503 my $w = shift;
504 my @ins = @_;
505 if (!@ins) {
506 @ins = $w->get(0, "end");
507 }
508
509 my $max_width = 0;
510 foreach my $ins (@ins) {
511 my $new_width = $w->fontMeasure($w->cget(-font), $ins);
512 if ($new_width > $max_width) {
513 $max_width = $new_width;
514 }
515 }
516 if ($max_width > 20) { # be sane
517 $w->configure(-listwidth => $max_width + 32); # XXX for scrollbar
518 }
519 }
520
521 1;
522
523 __END__
524