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