| 1 | # -*-mode: perl; fill-column: 80; comment-column: 80; -*- | ||
| 2 | |||
| 3 | # Tk::Bounded -- | ||
| 4 | # | ||
| 5 | # This file provides out of bounds mechanics. | ||
| 6 | # | ||
| 7 | # Copyright (c) 2000-2007 Meccanomania | ||
| 8 | # | ||
| 9 | # See the file "license.terms" for information on usage and redistribution | ||
| 10 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. | ||
| 11 | # | ||
| 12 | # svn: @(#) $Id: Bounded.pm 58 2008-01-10 23:29:20Z meccanomania $ | ||
| 13 | #------------------------------------------------------------------------------- | ||
| 14 | |||
| 15 | package Tk::Bounded; | ||
| 16 | |||
| 17 | # Import bound and boundtags for caller. | ||
| 18 | use Tkbound; | ||
| 19 | use Tkboundtags; | ||
| 20 | |||
| 21 | $VERSION = '$Revision: 58 $' =~ /\D(\d+)\s*\$$/; | ||
| 22 | |||
| 23 | use attributes; | ||
| 24 | use Carp; | ||
| 25 | use base qw/Tk::Derived/; | ||
| 26 | use Tk qw/Ev lsearch/; | ||
| 27 | |||
| 28 | # Deriving from Tk::Derived class is not well documented. | ||
| 29 | |||
| 30 | # We need a Populate function in order to add some config specs, but derived | ||
| 31 | # base class (as not derived from the widget class) cannot handle it. Workaround | ||
| 32 | # it by calling the Populate function in the package that use it. To do so, add | ||
| 33 | # a _super wrapper function into user's class namespace, that, in turn calls the | ||
| 34 | # bounded class Populate function. | ||
| 35 | # [Delegate ?] | ||
| 36 | # | ||
| 37 | # On the other hand, ConfigSpec function is defined in derived class, and we can | ||
| 38 | # call it a as regular Tk derived class does. | ||
| 39 | |||
| 40 | sub Populate { | ||
| 41 | my( $self, $args ) = @_; | ||
| 42 | |||
| 43 | $self -> _callbase( 'Populate', $args ); | ||
| 44 | } | ||
| 45 | |||
| 46 | # [ import function is called on module use. Here, its role is to assign | ||
| 47 | # _boundbase hidden function to the namespace of it package caller to a given | ||
| 48 | # source text take as is. So, _boundbase is defined a as regular function of | ||
| 49 | # the given package. | ||
| 50 | # | ||
| 51 | # Remember that a a clause such as 'use Tk::Bounded( qw/Tk::Entry/ ) yields the | ||
| 52 | # parameter referred to by $module to Tk::Bounded and next parameter referred to | ||
| 53 | # by $base to Tk::Entry. | ||
| 54 | # ] | ||
| 55 | |||
| 56 | sub import { | ||
| 57 | no strict 'refs'; | ||
| 58 | my( $module, $base ) = @_; | ||
| 59 | |||
| 60 | # Add _super hidden function into caller's namespace. | ||
| 61 | my $pkg = caller; | ||
| 62 | |||
| 63 | *{"${pkg}::_super"} = sub { $base }; | ||
| 64 | } | ||
| 65 | |||
| 66 | # _callbase hidden function role is to wrap a given routine into the _super | ||
| 67 | # hidden function defined on module loading. | ||
| 68 | |||
| 69 | sub _callbase { | ||
| 70 | my( $w, $sub ) = ( shift, shift ); | ||
| 71 | |||
| 72 | my $supersub = $w ->_super."::$sub"; | ||
| 73 | |||
| 74 | $w -> $supersub( @_ ); | ||
| 75 | } | ||
| 76 | |||
| 77 | #------------------------- | ||
| 78 | |||
| 79 | # SetBindtags is oververwritten in order to specify bounding for level 1. | ||
| 80 | |||
| 81 | sub SetBindtags { | ||
| 82 | my ($obj) = @_; | ||
| 83 | |||
| 84 | $obj -> boundtags( [ ref $obj, $obj, $obj -> toplevel, 'all' ], | ||
| 85 | [ 1 ] ); | ||
| 86 | } | ||
| 87 | |||
| 88 | #------------------------------------------------------------------------------- | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |