Debian Codegraph @

Tk/Widget.pm

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # Copyright (c) 1995-2004 Nick Ing-Simmons. All rights reserved.
2 # This program is free software; you can redistribute it and/or
3 # modify it under the same terms as Perl itself.
4 package Tk::Widget;
5 use vars qw($VERSION @DefaultMenuLabels);
6 $VERSION = '4.037'; # was: sprintf '4.%03d', q$Revision: #30 $ =~ /\D(\d+)\s*$/;
7
8 require Tk;
9 use AutoLoader;
10 use strict;
11 use Carp;
12 use base qw(DynaLoader Tk);
13
14 # stubs for 'autoloaded' widget classes
15 sub Button;
16 sub Canvas;
17 sub Checkbutton;
18 sub Entry;
19 sub Frame;
20 sub Label;
21 sub Labelframe;
22 sub Listbox;
23 sub Menu;
24 sub Menubutton;
25 sub Message;
26 sub Panedwindow;
27 sub Radiobutton;
28 sub Scale;
29 sub Scrollbar;
30 sub Spinbox;
31 sub Text;
32 sub Toplevel;
33
34 sub Pixmap;
35 sub Bitmap;
36 sub Photo;
37
38 sub ScrlListbox;
39 sub Optionmenu;
40
41 sub import
42 {
43 my $package = shift;
44 carp 'use Tk::Widget () to pre-load widgets is deprecated' if (@_);
45 my $need;
46 foreach $need (@_)
47 {
48 unless (defined &{$need})
49 {
50 require "Tk/${need}.pm";
51 }
52 croak "Cannot locate $need" unless (defined &{$need});
53 }
54 }
55
56 @DefaultMenuLabels = qw[~File ~Help];
57
58 # Some tidy-ness functions for winfo stuff
59
60 sub True { 1 }
61 sub False { 0 }
62
63 use Tk::Submethods( 'grab' => [qw(current status release -global)],
64 'focus' => [qw(-force -lastfor)],
65 'pack' => [qw(configure forget info propagate slaves)],
66 'grid' => [qw(bbox columnconfigure configure forget info location propagate remove rowconfigure size slaves)],
67 'form' => [qw(check configure forget grid info slaves)],
68 'event' => [qw(add delete generate info)],
69 'place' => [qw(configure forget info slaves)],
70 'wm' => [qw(capture release)],
71 'font' => [qw(actual configure create delete families measure metrics names subfonts)]
72 );
73
74 BEGIN {
75 # FIXME - these don't work in the compiler
76 *IsMenu = \&False;
77 *IsMenubutton = \&False;
78 *configure_self = \&Tk::configure;
79 *cget_self = \&Tk::cget;
80 }
81
82
83
84 Direct Tk::Submethods (
85 'winfo' => [qw(cells class colormapfull depth exists
86 geometry height id ismapped manager name parent reqheight
87 reqwidth rootx rooty screen screencells screendepth screenheight
88 screenmmheight screenmmwidth screenvisual screenwidth visual
89 visualsavailable vrootheight viewable vrootwidth vrootx vrooty
90 width x y toplevel children pixels pointerx pointery pointerxy
91 server fpixels rgb )],
92 'tk' => [qw(appname caret scaling useinputmethods windowingsystem)]);
93
94
95 sub DESTROY
96 {
97 my $w = shift;
98 $w->destroy if ($w->IsWidget);
99 }
100
101 sub Install
102 {
103 # Dynamically loaded widgets add their core commands
104 # to the Tk base class here
105 my ($package,$mw) = @_;
106 }
107
108 sub ClassInit
109 {
110 # Carry out class bindings (or whatever)
111 my ($package,$mw) = @_;
112 return $package;
113 }
114
115 sub CreateOptions
116 {
117 return ();
118 }
119
120 sub CreateArgs
121 {
122 my ($package,$parent,$args) = @_;
123 # Remove from hash %$args any configure-like
124 # options which only apply at create time (e.g. -colormap for Frame),
125 # or which may as well be applied right away
126 # return these as a list of -key => value pairs
127 # Augment same hash with default values for missing mandatory options,
128 # allthough this can be done later in InitObject.
129
130 # Honour -class => if present, we have hacked Tk_ConfigureWidget to
131 # allow -class to be passed to any widget.
132 my @result = ();
133 my $class = delete $args->{'-class'};
134 ($class) = $package =~ /([A-Z][A-Z0-9_]*)$/i unless (defined $class);
135 @result = (-class => "\u$class") if (defined $class);
136 foreach my $opt ($package->CreateOptions)
137 {
138 push(@result, $opt => delete $args->{$opt}) if exists $args->{$opt};
139 }
140 return @result;
141 }
142
143 sub InitObject
144 {
145 my ($obj,$args) = @_;
146 # per object initialization, for example populating
147 # with sub-widgets, adding a few object bindings to augment
148 # inherited class bindings, changing binding tags.
149 # Also another chance to mess with %$args before configure...
150 }
151
152 sub SetBindtags
153 {
154 my ($obj) = @_;
155 $obj->bindtags([ref($obj),$obj,$obj->toplevel,'all']);
156 }
157
158 sub new
159 {
160 local $SIG{'__DIE__'} = \&Carp::croak;
161 my $package = shift;
162 my $parent = shift;
163 $package->InitClass($parent);
164 $parent->BackTrace("Odd number of args to $package->new(...)") unless ((@_ % 2) == 0);
165 my %args = @_;
166 my @args = $package->CreateArgs($parent,\%args);
167 my $cmd = $package->Tk_cmd;
168 my $pname = $parent->PathName;
169 $pname = '' if ($pname eq '.');
170 my $leaf = delete $args{'Name'};
171 if (defined $leaf)
172 {
173 $leaf =~ s/[^a-z0-9_#]+/_/ig;
174 $leaf = lcfirst($leaf);
175 }
176 else
177 {
178 ($leaf) = "\L$package" =~ /([a-z][a-z0-9_]*)$/;
179 }
180 my $lname = $pname . '.' . $leaf;
181 # create a hash indexed by leaf name to speed up
182 # creation of a lot of sub-widgets of the same type
183 # e.g. entries in Table
184 my $nhash = $parent->TkHash('_names_');
185 $nhash->{$leaf} = 0 unless (exists $nhash->{$leaf});
186 while (defined ($parent->Widget($lname)))
187 {
188 $lname = $pname . '.' . $leaf . ++$nhash->{$leaf};
189 }
190 my $obj = eval { &$cmd($parent, $lname, @args) };
191 confess $@ if $@;
192 unless (ref $obj)
193 {
194 die "No value from $cmd $lname" unless defined $obj;
195 warn "$cmd '$lname' returned '$obj'" unless $obj eq $lname;
196 $obj = $parent->Widget($lname = $obj);
197 die "$obj from $lname" unless ref $obj;
198 }
199 bless $obj,$package;
200 $obj->SetBindtags;
201 my $notice = $parent->can('NoticeChild');
202 $parent->$notice($obj,\%args) if $notice;
203 $obj->InitObject(\%args);
204 # ASkludge(\%args,1);
205 $obj->configure(%args) if (%args);
206 # ASkludge(\%args,0);
207 return $obj;
208 }
209
210 sub DelegateFor
211 {
212 my ($w,$method) = @_;
213 while(exists $w->{'Delegates'})
214 {
215 my $delegate = $w->{'Delegates'};
216 my $widget = $delegate->{$method};
217 $widget = $delegate->{DEFAULT} unless (defined $widget);
218 $widget = $w->Subwidget($widget) if (defined $widget && !ref $widget);
219 last unless (defined $widget);
220 last if $widget == $w;
221 $w = $widget;
222 }
223 return $w;
224 }
225
226 sub Delegates
227 {
228 my $cw = shift;
229 my $specs = $cw->TkHash('Delegates');
230 while (@_)
231 {
232 my $key = shift;
233 my $val = shift;
234 $specs->{$key} = $val;
235 }
236 return $specs;
237 }
238
239 sub Construct
240 {
241 my ($base,$name) = @_;
242 my $class = (caller(0))[0];
243 no strict 'refs';
244
245 # Hack for broken ->isa in perl5.6.0
246 delete ${"$class\::"}{'::ISA::CACHE::'} if $] == 5.006;
247
248 # Pre ->isa scheme
249 *{$base.'::Is'.$name} = \&False;
250 *{$class.'::Is'.$name} = \&True;
251
252 # DelegateFor trickyness is to allow Frames and other derived things
253 # to force creation in a delegate e.g. a ScrlText with embeded windows
254 # need those windows to be children of the Text to get clipping right
255 # and not of the Frame which contains the Text and the scrollbars.
256 *{$base.'::'."$name"} = sub { $class->new(shift->DelegateFor('Construct'),@_) };
257 }
258
259 sub IS
260 {
261 return (defined $_[1]) && $_[0] == $_[1];
262 }
263
264 sub _AutoloadTkWidget
265 {
266 my ($self,$method) = @_;
267 my $what = "Tk::Widget::$method";
268 unless (defined &$what)
269 {
270 require "Tk/$method.pm";
271 }
272 return $what;
273 }
274
275 # require UNIVERSAL; don't load .pm use XS code from perl core though
276
277 sub AUTOLOAD
278 {
279 # Take a copy into a 'my' variable so we can recurse
280 my $what = $Tk::Widget::AUTOLOAD;
281 my $save = $@;
282 my $name;
283 # warn "AUTOLOAD $what ".(ref($_[0]) || $_[0])."\n";
284 # Braces used to preserve $1 et al.
285 {
286 my ($pkg,$func) = $what =~ /(.*)::([^:]+)$/;
287 confess("Attempt to load '$what'") unless defined($pkg) && $func =~ /^[\w:]+$/;
288 $pkg =~ s#::#/#g;
289 if (defined($name=$INC{"$pkg.pm"}))
290 {
291 $name =~ s#^(.*)$pkg\.pm$#$1auto/$pkg/$func.al#;
292 }
293 else
294 {
295 $name = "auto/$what.al";
296 $name =~ s#::#/#g;
297 }
298 }
299 # This may fail, catch error and prevent user's __DIE__ handler
300 # from triggering as well...
301 eval {local $SIG{'__DIE__'}; require $name};
302 if ($@)
303 {
304 croak $@ unless ($@ =~ /Can't locate\s+(?:file\s+)?'?\Q$name\E'?/);
305 my($package,$method) = ($what =~ /^(.*)::([^:]*)$/);
306 if (ref $_[0] && !$_[0]->can($method)
307 && $_[0]->can('Delegate')
308 && $method !~ /^(ConfigSpecs|Delegates)/ )
309 {
310 my $delegate = $_[0]->Delegates;
311 if (%$delegate || tied %$delegate)
312 {
313 my $widget = $delegate->{$method};
314 $widget = $delegate->{DEFAULT} unless (defined $widget);
315 if (defined $widget)
316 {
317 my $subwidget = (ref $widget) ? $widget : $_[0]->Subwidget($widget);
318 if (defined $subwidget)
319 {
320 no strict 'refs';
321 # print "AUTOLOAD: $what\n";
322 *{$what} = sub { shift->Delegate($method,@_) };
323 }
324 else
325 {
326 croak "No delegate subwidget '$widget' for $what";
327 }
328 }
329 }
330 }
331 if (!defined(&$what) && ref($_[0]) && $method =~ /^[A-Z]\w+$/)
332 {
333 # Use ->can as ->isa is broken in perl5.6.0
334 my $sub = UNIVERSAL::can($_[0],'_AutoloadTkWidget');
335 if ($sub)
336 {
337 carp "Assuming 'require Tk::$method;'" unless $_[0]->can($method);
338 $what = $_[0]->$sub($method)
339 }
340 }
341 }
342 $@ = $save;
343 $DB::sub = $what; # Tell debugger what is going on...
344 unless (defined &$what)
345 {
346 no strict 'refs';
347 *{$what} = sub { croak("Failed to AUTOLOAD '$what'") };
348 }
349 goto &$what;
350 }
351
352 sub _Destroyed
353 {
354 my $w = shift;
355 my $a = delete $w->{'_Destroy_'};
356 if (ref($a))
357 {
358 while (@$a)
359 {
360 my $ent = pop(@$a);
361 if (ref $ent)
362 {
363 eval {local $SIG{'__DIE__'}; $ent->Call };
364 }
365 else
366 {
367 delete $w->{$ent};
368 }
369 }
370 }
371 }
372
373 sub _OnDestroy
374 {
375 my $w = shift;
376 $w->{'_Destroy_'} = [] unless (exists $w->{'_Destroy_'});
377 push(@{$w->{'_Destroy_'}},@_);
378 }
379
380 sub OnDestroy
381 {
382 my $w = shift;
383 $w->_OnDestroy(Tk::Callback->new(@_));
384 }
385
386 sub TkHash
387 {
388 my ($w,$key) = @_;
389 return $w->{$key} if exists $w->{$key};
390 my $hash = $w->{$key} = {};
391 $w->_OnDestroy($key);
392 return $hash;
393 }
394
395 sub privateData
396 {
397 my $w = shift;
398 my $p = shift || caller;
399 $w->{$p} ||= {};
400 }
401
402 my @image_types;
403 my %image_method;
404
405 sub ImageMethod
406 {
407 shift if (@_ & 1);
408 while (@_)
409 {
410 my ($name,$method) = splice(@_,0,2);
411 push(@image_types,$name);
412 $image_method{$name} = $method;
413 }
414 }
415
416 sub Getimage
417 {
418 my ($w, $name) = @_;
419 my $mw = $w->MainWindow;
420 croak "Usage \$widget->Getimage('name')" unless defined($name);
421 my $images = ($mw->{'__Images__'} ||= {});
422
423 return $images->{$name} if $images->{$name};
424
425 ImageMethod(xpm => 'Pixmap',
426 gif => 'Photo',
427 ppm => 'Photo',
428 xbm => 'Bitmap' ) unless @image_types;
429
430 foreach my $type (@image_types)
431 {
432 my $method = $image_method{$type};
433 my $file = Tk->findINC( "$name.$type" );
434 next unless( $file && $method );
435 my $sub = $w->can($method);
436 unless (defined &$sub)
437 {
438 require Tk::widgets;
439 Tk::widgets->import($method);
440 }
441 $images->{$name} = $w->$method( -file => $file );
442 return $images->{$name};
443 }
444
445 # Try built-in bitmaps
446 $images->{$name} = $w->Pixmap( -id => $name );
447 return $images->{$name};
448 }
449
450 sub SaveGrabInfo
451 {
452 my $w = shift;
453 $Tk::oldGrab = $w->grabCurrent;
454 if (defined $Tk::oldGrab)
455 {
456 $Tk::grabStatus = $Tk::oldGrab->grabStatus;
457 }
458 }
459
460 sub grabSave
461 {
462 my ($w) = @_;
463 my $grab = $w->grabCurrent;
464 return sub {} if (!defined $grab);
465 my $method = ($grab->grabStatus eq 'global') ? 'grabGlobal' : 'grab';
466 return sub { eval {local $SIG{'__DIE__'}; $grab->$method() } };
467 }
468
469 sub focusCurrent
470 {
471 my ($w) = @_;
472 $w->Tk::focus('-displayof');
473 }
474
475 sub focusSave
476 {
477 my ($w) = @_;
478 my $focus = $w->focusCurrent;
479 return sub {} if (!defined $focus);
480 return sub { eval {local $SIG{'__DIE__'}; $focus->focus } };
481 }
482
483 # This is supposed to replicate Tk::after behaviour,
484 # but does auto-cancel when widget is deleted.
485 require Tk::After;
486
487 sub afterCancel
488 {
489 my ($w,$what) = @_;
490 if (defined $what)
491 {
492 return $what->cancel if ref($what);
493 carp "dubious cancel of $what" if 0 && $^W;
494 $w->Tk::after('cancel' => $what);
495 }
496 }
497
498 sub afterIdle
499 {
500 my $w = shift;
501 return Tk::After->new($w,'idle','once',@_);
502 }
503
504 sub afterInfo {
505 my ($w, $id) = @_;
506 if (defined $id) {
507 return ($id->[4], $id->[2], $id->[3]);
508 } else {
509 return sort( keys %{$w->{_After_}} );
510 }
511 }
512
513 sub after
514 {
515 my $w = shift;
516 my $t = shift;
517 if (@_)
518 {
519 if ($t ne 'cancel')
520 {
521 require Tk::After;
522 return Tk::After->new($w,$t,'once',@_)
523 }
524 while (@_)
525 {
526 my $what = shift;
527 $w->afterCancel($what);
528 }
529 }
530 else
531 {
532 $w->Tk::after($t);
533 }
534 }
535
536 sub repeat
537 {
538 require Tk::After;
539 my $w = shift;
540 my $t = shift;
541 return Tk::After->new($w,$t,'repeat',@_);
542 }
543
544 sub FindMenu
545 {
546 # default FindMenu is that there is no menu.
547 return undef;
548 }
549
550 sub XEvent { shift->{'_XEvent_'} }
551
552 sub propertyRoot
553 {
554 my $w = shift;
555 return $w->property(@_,'root');
556 }
557
558 # atom, atomname, containing, interps, pathname
559 # don't work this way - there is no window arg
560 # So we pretend there was an call the C versions from Tk.xs
561
562 sub atom { shift->InternAtom(@_) }
563 sub atomname { shift->GetAtomName(@_) }
564 sub containing { shift->Containing(@_) }
565
566 # interps not done yet
567 # pathname not done yet
568
569 # walk and descendants adapted from Stephen's composite
570 # versions as they only use core features they can go here.
571 # hierachy is reversed in that descendants calls walk rather
572 # than vice versa as this avoids building a list.
573 # Walk should possibly be enhanced so allow early termination
574 # like '-prune' of find.
575
576 sub Walk
577 {
578 # Traverse a widget hierarchy while executing a subroutine.
579 my($cw, $proc, @args) = @_;
580 my $subwidget;
581 foreach $subwidget ($cw->children)
582 {
583 $subwidget->Walk($proc,@args);
584 &$proc($subwidget, @args);
585 }
586 } # end walk
587
588 sub Descendants
589 {
590 # Return a list of widgets derived from a parent widget and all its
591 # descendants of a particular class.
592 # If class is not passed returns the entire widget hierarchy.
593
594 my($widget, $class) = @_;
595 my(@widget_tree) = ();
596
597 $widget->Walk(
598 sub { my ($widget,$list,$class) = @_;
599 push(@$list, $widget) if (!defined($class) or $class eq $widget->class);
600 },
601 \@widget_tree, $class
602 );
603 return @widget_tree;
604 }
605
606 sub Palette
607 {
608 my $w = shift->MainWindow;
609 unless (exists $w->{_Palette_})
610 {
611 my %Palette = ();
612 my $c = $w->Checkbutton();
613 my $e = $w->Entry();
614 my $s = $w->Scrollbar();
615 $Palette{'activeBackground'} = ($c->configure('-activebackground'))[3] ;
616 $Palette{'activeForeground'} = ($c->configure('-activeforeground'))[3];
617 $Palette{'background'} = ($c->configure('-background'))[3];
618 $Palette{'disabledForeground'} = ($c->configure('-disabledforeground'))[3];
619
620 $Palette{'foreground'} = ($c->configure('-foreground'))[3];
621 $Palette{'highlightBackground'} = ($c->configure('-highlightbackground'))[3];
622 $Palette{'highlightColor'} = ($c->configure('-highlightcolor'))[3];
623 $Palette{'insertBackground'} = ($e->configure('-insertbackground'))[3];
624 $Palette{'selectColor'} = ($c->configure('-selectcolor'))[3];
625 $Palette{'selectBackground'} = ($e->configure('-selectbackground'))[3];
626 $Palette{'selectForeground'} = ($e->configure('-selectforeground'))[3];
627 $Palette{'troughColor'} = ($s->configure('-troughcolor'))[3];
628 $c->destroy;
629 $e->destroy;
630 $s->destroy;
631 $w->{_Palette_} = \%Palette;
632 }
633 return $w->{_Palette_};
634 }
635
636 # tk_setPalette --
637 # Changes the default color scheme for a Tk application by setting
638 # default colors in the option database and by modifying all of the
639 # color options for existing widgets that have the default value.
640 #
641 # Arguments:
642 # The arguments consist of either a single color name, which
643 # will be used as the new background color (all other colors will
644 # be computed from this) or an even number of values consisting of
645 # option names and values. The name for an option is the one used
646 # for the option database, such as activeForeground, not -activeforeground.
647 # Additional special option names are:
648 # priority: set the priority for the option database entries, see Tk::option
649 sub setPalette
650 {
651 my $w = shift->MainWindow;
652 # Just return on monochrome displays, otherwise errors will occur
653 return if $w->depth == 1;
654 my %new = (@_ == 1) ? (background => $_[0]) : @_;
655 my $priority = delete($new{'priority'}) || 'widgetDefault';
656
657 # Create an array that has the complete new palette. If some colors
658 # aren't specified, compute them from other colors that are specified.
659
660 die 'must specify a background color' if (!exists $new{background});
661 my @bg = $w->rgb($new{'background'});
662
663 if (!exists $new{foreground})
664 {
665 # Note that the range of each value in the triple returned by
666 # [winfo rgb] is 0-65535, and your eyes are more sensitive to
667 # green than to red, and more to red than to blue.
668 my($r,$g,$b) = @bg;
669 if ($r+1.5*$g+0.5*$b > 100000)
670 {
671 $new{'foreground'} = 'black';
672 }
673 else
674 {
675 $new{'foreground'} = 'white';
676 }
677 }
678 my @fg = $w->rgb($new{'foreground'});
679 my $darkerBg = sprintf('#%02x%02x%02x',9*$bg[0]/2560,9*$bg[1]/2560,9*$bg[2]/2560);
680 foreach my $i ('activeForeground','insertBackground','selectForeground','highlightColor')
681 {
682 $new{$i} = $new{'foreground'} unless (exists $new{$i});
683 }
684 unless (exists $new{'disabledForeground'})
685 {
686 $new{'disabledForeground'} = sprintf('#%02x%02x%02x',(3*$bg[0]+$fg[0])/1024,(3*$bg[1]+$fg[1])/1024,(3*$bg[2]+$fg[2])/1024);
687 }
688 $new{'highlightBackground'} = $new{'background'} unless (exists $new{'highlightBackground'});
689
690 unless (exists $new{'activeBackground'})
691 {
692 my @light;
693 # Pick a default active background that is lighter than the
694 # normal background. To do this, round each color component
695 # up by 15% or 1/3 of the way to full white, whichever is
696 # greater.
697 foreach my $i (0, 1, 2)
698 {
699 $light[$i] = $bg[$i]/256;
700 my $inc1 = $light[$i]*15/100;
701 my $inc2 = (255-$light[$i])/3;
702 if ($inc1 > $inc2)
703 {
704 $light[$i] += $inc1
705 }
706 else
707 {
708 $light[$i] += $inc2
709 }
710 $light[$i] = 255 if ($light[$i] > 255);
711 }
712 $new{'activeBackground'} = sprintf('#%02x%02x%02x',@light);
713 }
714 $new{'selectBackground'} = $darkerBg unless (exists $new{'selectBackground'});
715 $new{'troughColor'} = $darkerBg unless (exists $new{'troughColor'});
716 $new{'selectColor'} = '#b03060' unless (exists $new{'selectColor'});
717
718 # Before doing this, make sure that the Tk::Palette variable holds
719 # the default values of all options, so that tkRecolorTree can
720 # be sure to only change options that have their default values.
721 # If the variable exists, then it is already correct (it was created
722 # the last time this procedure was invoked). If the variable
723 # doesn't exist, fill it in using the defaults from a few widgets.
724 my $Palette = $w->Palette;
725
726 # let's make one of each of the widgets so we know what the
727 # defaults are currently for this platform.
728 $Tk::___tk_set_palette = $w->Toplevel(Name => "___tk_set_palette");
729 $Tk::___tk_set_palette->withdraw;
730 foreach my $q (qw(Button Canvas Checkbutton Entry Frame Label Labelframe
731 Listbox Menubutton Menu Message Radiobutton Scale Scrollbar
732 Spinbox Text
733 ))
734 {
735 $Tk::___tk_set_palette->Component($q, $q);
736 }
737
738 # Walk the widget hierarchy, recoloring all existing windows.
739 my $res = $w->RecolorTree(\%new);
740 if ($res->{addOptionDB})
741 {
742 for (@{ $res->{addOptionDB} })
743 {
744 $w->optionAdd(@$_);
745 }
746 }
747
748 $Tk::___tk_set_palette->destroy;
749
750 # Change the option database so that future windows will get the
751 # same colors.
752 foreach my $option (keys %new)
753 {
754 $w->option('add',"*$option",$new{$option},$priority);
755 # Save the options in the global variable Tk::Palette, for use the
756 # next time we change the options.
757 $Palette->{$option} = $new{$option};
758 }
759 }
760
761 # tkRecolorTree --
762 # This procedure changes the colors in a window and all of its
763 # descendants, according to information provided by the colors
764 # argument. It only modifies colors that have their default values
765 # as specified by the Tk::Palette variable.
766 #
767 # Arguments:
768 # w - The name of a window. This window and all its
769 # descendants are recolored.
770 # colors - The name of an array variable in the caller,
771 # which contains color information. Each element
772 # is named after a widget configuration option, and
773 # each value is the value for that option.
774 sub RecolorTree
775 {
776 my ($w,$colors) = @_;
777 local ($@);
778 my @addOptionDB;
779 my $prototype = (defined $Tk::___tk_set_palette ? $Tk::___tk_set_palette->Subwidget($w->Class) || undef : undef);
780 foreach my $dbOption (keys %$colors)
781 {
782 my $option = "-\L$dbOption";
783 my $class = ucfirst($dbOption);
784 my @value;
785 eval {local $SIG{'__DIE__'}; @value = $w->configure($option) };
786 if (@value)
787 {
788 # if the option database has a preference for this
789 # dbOption, then use it, otherwise use the defaults
790 # for the widget.
791 my $defaultcolor = $w->optionGet($dbOption, $class);
792 no warnings 'uninitialized';
793 if ($defaultcolor eq '' ||
794 ($prototype && $prototype->cget($option) ne $defaultcolor))
795 {
796 $defaultcolor = $value[3];
797 }
798 if ($defaultcolor ne '')
799 {
800 $defaultcolor = join ',', $w->rgb($defaultcolor);
801 }
802 my $chosencolor = $value[4];
803 if ($chosencolor ne '')
804 {
805 $chosencolor = join ',', $w->rgb($chosencolor);
806 }
807 if ($defaultcolor eq $chosencolor)
808 {
809 # Change the option database so that future windows will get
810 # the same colors.
811 push @addOptionDB, ['*'.$w->Class.".$dbOption", $colors->{$dbOption}, 60];
812 $w->configure($option,$colors->{$dbOption});
813 }
814 }
815 }
816 foreach my $child ($w->children)
817 {
818 my $res = $child->RecolorTree($colors);
819 if ($res->{addOptionDB})
820 {
821 push @addOptionDB, @{ $res->{addOptionDB} };
822 }
823 }
824 return { addOptionDB => \@addOptionDB };
825 }
826 # tkDarken --
827 # Given a color name, computes a new color value that darkens (or
828 # brightens) the given color by a given percent.
829 #
830 # Arguments:
831 # color - Name of starting color.
832 # percent - Integer telling how much to brighten or darken as a
833 # percent: 50 means darken by 50%, 110 means brighten
834 # by 10%.
835 sub Darken
836 {
837 my ($w,$color,$percent) = @_;
838 my @l = $w->rgb($color);
839 my $red = $l[0]/256;
840 my $green = $l[1]/256;
841 my $blue = $l[2]/256;
842 $red = int($red*$percent/100);
843 $red = 255 if ($red > 255);
844 $green = int($green*$percent/100);
845 $green = 255 if ($green > 255);
846 $blue = int($blue*$percent/100);
847 $blue = 255 if ($blue > 255);
848 sprintf('#%02x%02x%02x',$red,$green,$blue)
849 }
850 # tk_bisque --
851 # Reset the Tk color palette to the old "bisque" colors.
852 #
853 # Arguments:
854 # None.
855 sub bisque
856 {
857 shift->setPalette('activeBackground' => '#e6ceb1',
858 'activeForeground' => 'black',
859 'background' => '#ffe4c4',
860 'disabledForeground' => '#b0b0b0',
861 'foreground' => 'black',
862 'highlightBackground' => '#ffe4c4',
863 'highlightColor' => 'black',
864 'insertBackground' => 'black',
865 'selectColor' => '#b03060',
866 'selectBackground' => '#e6ceb1',
867 'selectForeground' => 'black',
868 'troughColor' => '#cdb79e'
869 );
870 }
871
872 sub PrintConfig
873 {
874 require Tk::Pretty;
875 my ($w) = (@_);
876 my $c;
877 foreach $c ($w->configure)
878 {
879 print Tk::Pretty::Pretty(@$c),"\n";
880 }
881 }
882
883 sub BusyRecurse
884 {
885 my ($restore,$w,$cursor,$recurse,$top) = @_;
886 my $c = $w->cget('-cursor');
887 my @tags = $w->bindtags;
888 if ($top || defined($c) || $w->isa('Tk::Toplevel'))
889 {
890 push(@$restore, sub { return unless Tk::Exists($w); $w->configure(-cursor => $c); $w->bindtags(\@tags) });
891 $w->configure(-cursor => $cursor);
892 }
893 else
894 {
895 push(@$restore, sub { return unless Tk::Exists($w); $w->bindtags(\@tags) });
896 }
897 $w->bindtags(['Busy',@tags]);
898 if ($recurse)
899 {
900 foreach my $child ($w->children)
901 {
902 BusyRecurse($restore,$child,$cursor,1,0);
903 }
904 }
905 return $restore;
906 }
907
908 sub Busy
909 {
910 my ($w,@args) = @_;
911 return unless $w->viewable;
912 my($sub, %args);
913 for(my $i=0; $i<=$#args; $i++)
914 {
915 if (ref $args[$i] eq 'CODE')
916 {
917 if (defined $sub)
918 {
919 croak "Multiple code definitions not allowed in Tk::Widget::Busy";
920 }
921 $sub = $args[$i];
922 }
923 else
924 {
925 $args{$args[$i]} = $args[$i+1]; $i++;
926 }
927 }
928 my $cursor = delete $args{'-cursor'};
929 my $recurse = delete $args{'-recurse'};
930 $cursor = 'watch' unless defined $cursor;
931 unless (exists $w->{'Busy'})
932 {
933 my @old = ($w->grabSave);
934 my $key;
935 my @config;
936 foreach $key (keys %args)
937 {
938 push(@config,$key => $w->Tk::cget($key));
939 }
940 if (@config)
941 {
942 push(@old, sub { $w->Tk::configure(@config) });
943 $w->Tk::configure(%args);
944 }
945 unless ($w->Tk::bind('Busy'))
946 {
947 $w->Tk::bind('Busy','<Any-KeyPress>',[_busy => 1]);
948 $w->Tk::bind('Busy','<Any-KeyRelease>',[_busy => 0]);
949 $w->Tk::bind('Busy','<Any-ButtonPress>',[_busy => 1]);
950 $w->Tk::bind('Busy','<Any-ButtonRelease>',[_busy => 0]);
951 $w->Tk::bind('Busy','<Any-Motion>',[_busy => 0]);
952 }
953 $w->{'Busy'} = BusyRecurse(\@old,$w,$cursor,$recurse,1);
954 }
955 my $g = $w->grabCurrent;
956 if (defined $g)
957 {
958 # warn "$g has the grab";
959 $g->grabRelease;
960 }
961 $w->update;
962 if (Tk::Exists($w))
963 {
964 eval {local $SIG{'__DIE__'}; $w->grab };
965 $w->update;
966 }
967 if ($sub && Tk::Exists($w))
968 {
969 eval { $sub->() };
970 my $err = $@;
971 $w->Unbusy(-recurse => $recurse);
972 die $err if $err;
973 }
974 }
975
976 sub _busy
977 {
978 my ($w,$f) = @_;
979 $w->bell if $f;
980 $w->break;
981 }
982
983 sub Unbusy
984 {
985 my ($w) = @_;
986 $w->update;
987 $w->grabRelease if Tk::Exists($w);
988 my $old = delete $w->{'Busy'};
989 if (defined $old)
990 {
991 local $SIG{'__DIE__'};
992 eval { &{pop(@$old)} } while (@$old);
993 }
994 $w->update if Tk::Exists($w);
995 }
996
997 sub waitVisibility
998 {
999 my ($w) = shift;
1000 $w->tkwait('visibility',$w);
1001 }
1002
1003 sub waitVariable
1004 {
1005 my ($w) = shift;
1006 $w->tkwait('variable',@_);
1007 }
1008
1009 sub waitWindow
1010 {
1011 my ($w) = shift;
1012 $w->tkwait('window',$w);
1013 }
1014
1015 sub EventWidget
1016 {
1017 my ($w) = @_;
1018 return $w->{'_EventWidget_'};
1019 }
1020
1021 sub Popwidget
1022 {
1023 my ($ew,$method,$w,@args) = @_;
1024 $w->{'_EventWidget_'} = $ew;
1025 $w->$method(@args);
1026 }
1027
1028 sub ColorOptions
1029 {
1030 my ($w,$args) = @_;
1031 my $opt;
1032 $args = {} unless (defined $args);
1033 foreach $opt (qw(-foreground -background -disabledforeground
1034 -activebackground -activeforeground
1035 ))
1036 {
1037 $args->{$opt} = $w->cget($opt) unless (exists $args->{$opt})
1038 }
1039 return (wantarray) ? %$args : $args;
1040 }
1041
1042 sub XscrollBind
1043 {
1044 my ($mw,$class) = @_;
1045 $mw->bind($class,'<Left>', ['xview','scroll',-1,'units']);
1046 $mw->bind($class,'<Control-Left>', ['xview','scroll',-1,'pages']);
1047 $mw->bind($class,'<Control-Prior>',['xview','scroll',-1,'pages']);
1048 $mw->bind($class,'<Right>', ['xview','scroll',1,'units']);
1049 $mw->bind($class,'<Control-Right>',['xview','scroll',1,'pages']);
1050 $mw->bind($class,'<Control-Next>', ['xview','scroll',1,'pages']);
1051
1052 $mw->bind($class,'<Home>', ['xview','moveto',0]);
1053 $mw->bind($class,'<End>', ['xview','moveto',1]);
1054 $mw->XMouseWheelBind($class);
1055 }
1056
1057 sub PriorNextBind
1058 {
1059 my ($mw,$class) = @_;
1060 $mw->bind($class,'<Next>', ['yview','scroll',1,'pages']);
1061 $mw->bind($class,'<Prior>', ['yview','scroll',-1,'pages']);
1062 }
1063
1064 sub XMouseWheelBind
1065 {
1066 my ($mw,$class) = @_;
1067 # <4> and <5> are how mousewheel looks on X
1068 $mw->bind($class,'<Shift-4>', ['xview','scroll',-1,'units']);
1069 $mw->bind($class,'<Shift-5>', ['xview','scroll',1,'units']);
1070 $mw->bind($class,'<Button-6>', ['xview','scroll',-1,'units']);
1071 $mw->bind($class,'<Button-7>', ['xview','scroll',1,'units']);
1072 }
1073
1074 sub YMouseWheelBind
1075 {
1076 my ($mw,$class) = @_;
1077 # <4> and <5> are how mousewheel looks on X
1078 $mw->bind($class,'<4>', ['yview','scroll',-1,'units']);
1079 $mw->bind($class,'<5>', ['yview','scroll',1,'units']);
1080 }
1081
1082 sub YscrollBind
1083 {
1084 my ($mw,$class) = @_;
1085 $mw->PriorNextBind($class);
1086 $mw->bind($class,'<Up>', ['yview','scroll',-1,'units']);
1087 $mw->bind($class,'<Down>', ['yview','scroll',1,'units']);
1088 $mw->YMouseWheelBind($class);
1089 }
1090
1091 sub XYscrollBind
1092 {
1093 my ($mw,$class) = @_;
1094 $mw->YscrollBind($class);
1095 $mw->XscrollBind($class);
1096 # <4> and <5> are how mousewheel looks on X
1097 }
1098
1099 sub MouseWheelBind
1100 {
1101 my($mw,$class) = @_;
1102
1103 # The MouseWheel will typically only fire on Windows. However, one
1104 # could use the "event generate" command to produce MouseWheel
1105 # events on other platforms.
1106
1107 $mw->Tk::bind($class, '<MouseWheel>',
1108 [ sub { $_[0]->yview('scroll',-($_[1]/120)*3,'units') }, Tk::Ev("D")]);
1109
1110 if ($mw->windowingsystem eq 'x11')
1111 {
1112 # Support for mousewheels on Linux/Unix commonly comes through mapping
1113 # the wheel to the extended buttons. If you have a mousewheel, find
1114 # Linux configuration info at:
1115 # http://linuxreviews.org/howtos/xfree/mouse/
1116 $mw->Tk::bind($class, '<4>',
1117 sub { $_[0]->yview('scroll', -3, 'units')
1118 unless $Tk::strictMotif;
1119 });
1120 $mw->Tk::bind($class, '<5>',
1121 sub { $_[0]->yview('scroll', 3, 'units')
1122 unless $Tk::strictMotif;
1123 });
1124 }
1125 }
1126
1127 sub ScrlListbox
1128 {
1129 my $parent = shift;
1130 return $parent->Scrolled('Listbox',-scrollbars => 'w', @_);
1131 }
1132
1133 sub AddBindTag
1134 {
1135 my ($w,$tag) = @_;
1136 my $t;
1137 my @tags = $w->bindtags;
1138 foreach $t (@tags)
1139 {
1140 return if $t eq $tag;
1141 }
1142 $w->bindtags([@tags,$tag]);
1143 }
1144
1145 sub Callback
1146 {
1147 my $w = shift;
1148 my $name = shift;
1149 my $cb = $w->cget($name);
1150 if (defined $cb)
1151 {
1152 return $cb->Call(@_) if (ref $cb);
1153 return $w->$cb(@_);
1154 }
1155 return (wantarray) ? () : undef;
1156 }
1157
1158 sub packAdjust
1159 {
1160 # print 'packAdjust(',join(',',@_),")\n";
1161 require Tk::Adjuster;
1162 my ($w,%args) = @_;
1163 my $delay = delete($args{'-delay'});
1164 $delay = 1 unless (defined $delay);
1165 $w->pack(%args);
1166 %args = $w->packInfo;
1167 my $adj = Tk::Adjuster->new($args{'-in'},
1168 -widget => $w, -delay => $delay, -side => $args{'-side'});
1169 $adj->packed($w,%args);
1170 return $w;
1171 }
1172
1173 sub gridAdjust
1174 {
1175 require Tk::Adjuster;
1176 my ($w,%args) = @_;
1177 my $delay = delete($args{'-delay'});
1178 $delay = 1 unless (defined $delay);
1179 $w->grid(%args);
1180 %args = $w->gridInfo;
1181 my $adj = Tk::Adjuster->new($args{'-in'},-widget => $w, -delay => $delay);
1182 $adj->gridded($w,%args);
1183 return $w;
1184 }
1185
1186 sub place
1187 {
1188 local $SIG{'__DIE__'} = \&Carp::croak;
1189 my $w = shift;
1190 if (@_ && $_[0] =~ /^(?:configure|forget|info|slaves)$/x)
1191 {
1192 $w->Tk::place(@_);
1193 }
1194 else
1195 {
1196 # Two things going on here:
1197 # 1. Add configure on the front so that we can drop leading '-'
1198 $w->Tk::place('configure',@_);
1199 # 2. Return the widget rather than nothing
1200 return $w;
1201 }
1202 }
1203
1204 sub pack
1205 {
1206 local $SIG{'__DIE__'} = \&Carp::croak;
1207 my $w = shift;
1208 if (@_ && $_[0] =~ /^(?:configure|forget|info|propagate|slaves)$/x)
1209 {
1210 # maybe array/scalar context issue with slaves
1211 $w->Tk::pack(@_);
1212 }
1213 else
1214 {
1215 # Two things going on here:
1216 # 1. Add configure on the front so that we can drop leading '-'
1217 $w->Tk::pack('configure',@_);
1218 # 2. Return the widget rather than nothing
1219 return $w;
1220 }
1221 }
1222
1223 sub grid
1224 {
1225 local $SIG{'__DIE__'} = \&Carp::croak;
1226 my $w = shift;
1227 if (@_ && $_[0] =~ /^(?:bbox|columnconfigure|configure|forget|info|location|propagate|remove|rowconfigure|size|slaves)$/x)
1228 {
1229 my $opt = shift;
1230 Tk::grid($opt,$w,@_);
1231 }
1232 else
1233 {
1234 # Two things going on here:
1235 # 1. Add configure on the front so that we can drop leading '-'
1236 Tk::grid('configure',$w,@_);
1237 # 2. Return the widget rather than nothing
1238 return $w;
1239 }
1240 }
1241
1242 sub form
1243 {
1244 local $SIG{'__DIE__'} = \&Carp::croak;
1245 my $w = shift;
1246 if (@_ && $_[0] =~ /^(?:configure|check|forget|grid|info|slaves)$/x)
1247 {
1248 $w->Tk::form(@_);
1249 }
1250 else
1251 {
1252 # Two things going on here:
1253 # 1. Add configure on the front so that we can drop leading '-'
1254 $w->Tk::form('configure',@_);
1255 # 2. Return the widget rather than nothing
1256 return $w;
1257 }
1258 }
1259
1260 sub Scrolled
1261 {
1262 my ($parent,$kind,%args) = @_;
1263 $kind = 'Pane' if $kind eq 'Frame';
1264 # Find args that are Frame create time args
1265 my @args = Tk::Frame->CreateArgs($parent,\%args);
1266 my $name = delete $args{'Name'};
1267 push(@args,'Name' => $name) if (defined $name);
1268 my $cw = $parent->Frame(@args);
1269 @args = ();
1270 # Now remove any args that Frame can handle
1271 foreach my $k ('-scrollbars',map($_->[0],$cw->configure))
1272 {
1273 push(@args,$k,delete($args{$k})) if (exists $args{$k})
1274 }
1275 # Anything else must be for target widget - pass at widget create time
1276 my $w = $cw->$kind(%args);
1277 # Now re-set %args to be ones Frame can handle
1278 %args = @args;
1279 $cw->ConfigSpecs('-scrollbars' => ['METHOD','scrollbars','Scrollbars','se'],
1280 '-background' => [$w,'background','Background'],
1281 '-foreground' => [$w,'foreground','Foreground'],
1282 );
1283 $cw->AddScrollbars($w);
1284 $cw->Default("\L$kind" => $w);
1285 $cw->Delegates('bind' => $w, 'bindtags' => $w, 'menu' => $w);
1286 $cw->ConfigDefault(\%args);
1287 $cw->configure(%args);
1288 return $cw;
1289 }
1290
1291 sub Populate
1292 {
1293 my ($cw,$args) = @_;
1294 }
1295
1296 sub ForwardEvent
1297 {
1298 my $self = shift;
1299 my $to = shift;
1300 $to->PassEvent($self->XEvent);
1301 }
1302
1303 # Save / Return abstract event type as in Tix.
1304 sub EventType
1305 {
1306 my $w = shift;
1307 $w->{'_EventType_'} = $_[0] if @_;
1308 return $w->{'_EventType_'};
1309 }
1310
1311 sub PostPopupMenu
1312 {
1313 my ($w, $X, $Y) = @_;
1314 if (@_ < 3)
1315 {
1316 my $e = $w->XEvent;
1317 $X = $e->X;
1318 $Y = $e->Y;
1319 }
1320 my $menu = $w->menu;
1321 $menu->Post($X,$Y) if defined $menu;
1322 }
1323
1324 sub FillMenu
1325 {
1326 my ($w,$menu,@labels) = @_;
1327 foreach my $lab (@labels)
1328 {
1329 my $method = $lab.'MenuItems';
1330 $method =~ s/~//g;
1331 $method =~ s/[\s-]+/_/g;
1332 if ($w->can($method))
1333 {
1334 $menu->Menubutton(-label => $lab, -tearoff => 0, -menuitems => $w->$method());
1335 }
1336 }
1337 return $menu;
1338 }
1339
1340 sub menu
1341 {
1342 my ($w,$menu) = @_;
1343 if (@_ > 1)
1344 {
1345 $w->_OnDestroy('_MENU_') unless exists $w->{'_MENU_'};
1346 $w->{'_MENU_'} = $menu;
1347 }
1348 return unless defined wantarray;
1349 unless (exists $w->{'_MENU_'})
1350 {
1351 $w->_OnDestroy('_MENU_');
1352 $w->{'_MENU_'} = $menu = $w->Menu(-tearoff => 0);
1353 $w->FillMenu($menu,$w->MenuLabels);
1354 }
1355 return $w->{'_MENU_'};
1356 }
1357
1358 sub MenuLabels
1359 {
1360 return @DefaultMenuLabels;
1361 }
1362
1363 sub FileMenuItems
1364 {
1365 my ($w) = @_;
1366 return [ ["command"=>'E~xit', -command => [ $w, 'WmDeleteWindow']]];
1367 }
1368
1369 sub WmDeleteWindow
1370 {
1371 shift->toplevel->WmDeleteWindow
1372 }
1373
1374 sub BalloonInfo
1375 {
1376 my ($widget,$balloon,$X,$Y,@opt) = @_;
1377 foreach my $opt (@opt)
1378 {
1379 my $info = $balloon->GetOption($opt,$widget);
1380 return $info if defined $info;
1381 }
1382 }
1383
1384 sub ConfigSpecs {
1385
1386 my $w = shift;
1387
1388 return map { ( $_->[0], [ $w, @$_[ 1 .. 4 ] ] ) } $w->configure;
1389
1390 }
1391
1392 *GetSelection =
1393 ($Tk::platform eq 'unix'
1394 ? sub
1395 {
1396 my $w = shift;
1397 my $sel = @_ ? shift : "PRIMARY";
1398 my $txt = eval { local $SIG{__DIE__};
1399 $w->SelectionGet(-selection => $sel, -type => "UTF8_STRING")
1400 };
1401 if ($@)
1402 {
1403 $txt = eval { local $SIG{__DIE__};
1404 $w->SelectionGet(-selection => $sel)
1405 };
1406 if ($@)
1407 {
1408 die "could not find default selection";
1409 }
1410 }
1411 $txt;
1412 }
1413 : sub
1414 {
1415 my $w = shift;
1416 my $sel = @_ ? shift : "PRIMARY";
1417 my $txt = eval { local $SIG{__DIE__};
1418 $w->SelectionGet(-selection => $sel)
1419 };
1420 if ($@)
1421 {
1422 die "could not find default selection";
1423 }
1424 $txt;
1425 }
1426 );
1427
1428 1;
1429 __END__
1430
1431 sub bindDump {
1432
1433 # Dump lots of good binding information. This pretty-print subroutine
1434 # is, essentially, the following code in disguise:
1435 #
1436 # print "Binding information for $w\n";
1437 # foreach my $tag ($w->bindtags) {
1438 # printf "\n Binding tag '$tag' has these bindings:\n";
1439 # foreach my $binding ($w->Tk::bind($tag)) {
1440 # printf " $binding\n";
1441 # }
1442 # }
1443
1444 my ($w) = @_;
1445
1446 my (@bindtags) = $w->bindtags;
1447 my $digits = length( scalar @bindtags );
1448 my ($spc1, $spc2) = ($digits + 33, $digits + 35);
1449 my $format1 = "%${digits}d.";
1450 my $format2 = ' ' x ($digits + 2);
1451 my $n = 0;
1452
1453 my @out;
1454 push @out, sprintf( "\n## Binding information for '%s', %s ##", $w->PathName, $w );
1455
1456 foreach my $tag (@bindtags) {
1457 my (@bindings) = $w->Tk::bind($tag);
1458 $n++; # count this bindtag
1459
1460 if ($#bindings == -1) {
1461 push @out, sprintf( "\n$format1 Binding tag '$tag' has no bindings.\n", $n );
1462 } else {
1463 push @out, sprintf( "\n$format1 Binding tag '$tag' has these bindings:\n", $n );
1464
1465 foreach my $binding ( @bindings ) {
1466 my $callback = $w->Tk::bind($tag, $binding);
1467 push @out, sprintf( "$format2%27s : %-40s\n", $binding, $callback );
1468
1469 if ($callback =~ /SCALAR/) {
1470 if (ref $$callback) {
1471 push @out, sprintf( "%s %s\n", ' ' x $spc1, $$callback );
1472 } else {
1473 push @out, sprintf( "%s '%s'\n", ' ' x $spc1, $$callback );
1474 }
1475 } elsif ($callback =~ /ARRAY/) {
1476 if (ref $callback->[0]) {
1477 push @out, sprintf( "%s %s\n", ' ' x $spc1, $callback->[0] );
1478 } else {
1479 push @out, sprintf( "%s '%s'\n", ' ' x $spc1, $callback->[0] );
1480 }
1481 foreach my $arg (@$callback[1 .. $#$callback]) {
1482 if (ref $arg) {
1483 push @out, sprintf( "%s %-40s", ' ' x $spc2, $arg );
1484 } else {
1485 push @out, sprintf( "%s '%s'", ' ' x $spc2, $arg );
1486 }
1487
1488 if (ref $arg eq 'Tk::Ev') {
1489 if ($arg =~ /SCALAR/) {
1490 push @out, sprintf( ": '$$arg'" );
1491 } else {
1492 push @out, sprintf( ": '%s'", join("' '", @$arg) );
1493 }
1494 }
1495
1496 push @out, sprintf( "\n" );
1497 } # forend callback arguments
1498 } # ifend callback
1499
1500 } # forend all bindings for one tag
1501
1502 } # ifend have bindings
1503
1504 } # forend all tags
1505 push @out, sprintf( "\n" );
1506 return @out;
1507
1508 } # end bindDump
1509
1510
1511 sub ASkludge
1512 {
1513 my ($hash,$sense) = @_;
1514 foreach my $key (%$hash)
1515 {
1516 if ($key =~ /-.*variable/ && ref($hash->{$key}) eq 'SCALAR')
1517 {
1518 if ($sense)
1519 {
1520 my $val = ${$hash->{$key}};
1521 require Tie::Scalar;
1522 tie ${$hash->{$key}},'Tie::StdScalar';
1523 ${$hash->{$key}} = $val;
1524 }
1525 else
1526 {
1527 untie ${$hash->{$key}};
1528 }
1529 }
1530 }
1531 }
1532
1533
1534
1535 # clipboardKeysyms --
1536 # This procedure is invoked to identify the keys that correspond to
1537 # the "copy", "cut", and "paste" functions for the clipboard.
1538 #
1539 # Arguments:
1540 # copy - Name of the key (keysym name plus modifiers, if any,
1541 # such as "Meta-y") used for the copy operation.
1542 # cut - Name of the key used for the cut operation.
1543 # paste - Name of the key used for the paste operation.
1544 #
1545 # This method is obsolete use clipboardOperations and abstract
1546 # event types instead. See Clipboard.pm and Mainwindow.pm
1547
1548 sub clipboardKeysyms
1549 {
1550 my @class = ();
1551 my $mw = shift;
1552 if (ref $mw)
1553 {
1554 $mw = $mw->DelegateFor('bind');
1555 }
1556 else
1557 {
1558 push(@class,$mw);
1559 $mw = shift;
1560 }
1561 if (@_)
1562 {
1563 my $copy = shift;
1564 $mw->Tk::bind(@class,"<$copy>",'clipboardCopy') if (defined $copy);
1565 }
1566 if (@_)
1567 {
1568 my $cut = shift;
1569 $mw->Tk::bind(@class,"<$cut>",'clipboardCut') if (defined $cut);
1570 }
1571 if (@_)
1572 {
1573 my $paste = shift;
1574 $mw->Tk::bind(@class,"<$paste>",'clipboardPaste') if (defined $paste);
1575 }
1576 }
1577
1578 sub pathname
1579 {
1580 my ($w,$id) = @_;
1581 my $x = $w->winfo('pathname',-displayof => oct($id));
1582 return $x->PathName;
1583 }
1584
1585 # ::tk::UnderlineAmpersand --
1586 # This procedure takes some text with ampersand and returns
1587 # text w/o ampersand and position of the ampersand.
1588 # Double ampersands are converted to single ones.
1589 # Position returned is -1 when there is no ampersand.
1590 #
1591 sub UnderlineAmpersand
1592 {
1593 my (undef,$text) = @_;
1594 if ($text =~ m{(?<!&)&(?!&)}g)
1595 {
1596 my $idx = pos $text;
1597 $text =~ s{(?<!&)&(?!&)}{};
1598 ($text, $idx);
1599 }
1600 else
1601 {
1602 ($text, -1);
1603 }
1604 }
1605
1606 # ::tk::SetAmpText --
1607 # Given widget path and text with "magic ampersands",
1608 # sets -text and -underline options for the widget
1609 #
1610 sub SetAmpText
1611 {
1612 my ($w,$text) = @_;
1613 my ($newtext,$under) = $w->UnderlineAmpersand($text);
1614 $w->configure(-text => $newtext, -underline => $under);
1615 }
1616
1617 # ::tk::AmpWidget --
1618 # Creates new widget, turning -text option into -text and
1619 # -underline options, returned by ::tk::UnderlineAmpersand.
1620 #
1621 sub AmpWidget
1622 {
1623 my ($w,$class,%args) = @_;
1624 my @options;
1625 while(my($opt,$val) = each %args)
1626 {
1627 if ($opt eq "-text")
1628 {
1629 my ($newtext,$under) = $w->UnderlineAmpersand($val);
1630 push @options, -text => $newtext, -underline => $under;
1631 }
1632 else
1633 {
1634 push @options, $opt, $val;
1635 }
1636 }
1637 my $result = $w->$class(@options);
1638 if ($result->can('AmpWidgetPostHook'))
1639 {
1640 $result->AmpWidgetPostHook;
1641 }
1642 return $result;
1643 }
1644
1645 # ::tk::FindAltKeyTarget --
1646 # search recursively through the hierarchy of visible widgets
1647 # to find button or label which has $char as underlined character
1648 #
1649 sub FindAltKeyTarget
1650 {
1651 my ($w,$char) = @_;
1652 $char = lc $char;
1653 if ($w->isa('Tk::Button') || $w->isa('Tk::Label'))
1654 {
1655 if ($char eq lc substr($w->cget(-text), $w->cget(-underline), 1))
1656 {
1657 return $w;
1658 }
1659 else
1660 {
1661 return undef;
1662 }
1663 }
1664 else
1665 {
1666 for my $cw ($w->gridSlaves, $w->packSlaves, $w->placeSlaves) # Cannot handle $w->formSlaves here?
1667 {
1668 my $target = $cw->FindAltKeyTarget($char);
1669 return $target if ($target);
1670 }
1671 }
1672 undef;
1673 }
1674
1675 # ::tk::AltKeyInDialog --
1676 # <Alt-Key> event handler for standard dialogs. Sends <<AltUnderlined>>
1677 # to button or label which has appropriate underlined character
1678 #
1679 sub AltKeyInDialog
1680 {
1681 my ($w, $key) = @_;
1682 my $target = $w->FindAltKeyTarget($key);
1683 return if !$target;
1684 $target->eventGenerate('<<AltUnderlined>>');
1685 }
1686
1687 # ::tk::SetFocusGrab --
1688 # swap out current focus and grab temporarily (for dialogs)
1689 # Arguments:
1690 # grab new window to grab
1691 # focus window to give focus to
1692 # Results:
1693 # Returns nothing
1694 #
1695 sub SetFocusGrab
1696 {
1697 my ($grab,$focus) = @_;
1698 my $index = "$grab,$focus";
1699 $Tk::FocusGrab{$index} ||= [];
1700 my $data = $Tk::FocusGrab{$index};
1701 push @$data, $grab->focusCurrent;
1702 my $oldGrab = $grab->grabCurrent;
1703 push @$data, $oldGrab;
1704 if (Tk::Exists($oldGrab))
1705 {
1706 push @$data, $oldGrab->grabStatus;
1707 }
1708 # The "grab" command will fail if another application
1709 # already holds the grab. So catch it.
1710 Tk::catch { $grab->grab };
1711 if (Tk::Exists($focus))
1712 {
1713 $focus->focus;
1714 }
1715 }
1716
1717 # ::tk::RestoreFocusGrab --
1718 # restore old focus and grab (for dialogs)
1719 # Arguments:
1720 # grab window that had taken grab
1721 # focus window that had taken focus
1722 # destroy destroy|withdraw - how to handle the old grabbed window
1723 # Results:
1724 # Returns nothing
1725 #
1726 sub RestoreFocusGrab
1727 {
1728 my ($grab, $focus, $destroy) = @_;
1729 $destroy = 'destroy' if !$destroy;
1730 my $index = "$grab,$focus";
1731 my ($oldFocus, $oldGrab, $oldStatus);
1732 if (exists $Tk::FocusGrab{$index})
1733 {
1734 ($oldFocus, $oldGrab, $oldStatus) = $Tk::FocusGrab{$index};
1735 delete $Tk::FocusGrab{$index};
1736 }
1737 else
1738 {
1739 $oldGrab = "";
1740 }
1741
1742 Tk::catch { $oldFocus->focus };
1743 if (Tk::Exists($grab))
1744 {
1745 $grab->grabRelease;
1746 if ($destroy eq "withdraw")
1747 {
1748 $grab->withdraw;
1749 }
1750 else
1751 {
1752 $grab->destroy;
1753 }
1754 }
1755 if (Tk::Exists($oldGrab) && $oldGrab->ismapped)
1756 {
1757 if ($oldStatus eq "global")
1758 {
1759 $oldGrab->grabGlobal;
1760 }
1761 else
1762 {
1763 $oldGrab->grab;
1764 }
1765 }
1766 }
1767
1768 # See http://rt.cpan.org/Ticket/Display.html?id=30929 and
1769 # http://rt.cpan.org/Ticket/Display.html?id=31016
1770 sub MasterMenu
1771 {
1772 }
1773