| 1 | # -*- mode: perl; fill-column: 80; comment-column: 80; -*- | ||
| 2 | |||
| 3 | # Tkbound -- | ||
| 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: Tkbound.pm 56 2008-01-10 23:23:59Z meccanomania $ | ||
| 13 | #------------------------------------------------------------------------------- | ||
| 14 | |||
| 15 | package Tkbound; | ||
| 16 | |||
| 17 | $VERSION = '$Revision: 56 $' =~ /\D(\d+)\s*\$$/; | ||
| 18 | |||
| 19 | use Tk qw/Ev/; | ||
| 20 | use Tk::ErrorDialog; | ||
| 21 | use attributes qw/reftype/; | ||
| 22 | |||
| 23 | use base Exporter; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | |||
| 27 | #------------------------------------------------------------------------------- | ||
| 28 | |||
| 29 | # Wrap bound into Tk package. | ||
| 30 | |||
| 31 | *Tk::bound = \&bound; | ||
| 32 | *Tk::normalize_event = \&normalize_event; | ||
| 33 | |||
| 34 | # Create a bound event structure. Bless single argument (references and | ||
| 35 | # scalars), and leave to Ev other structures. | ||
| 36 | |||
| 37 | push @Tk::EXPORT, 'bEv'; # Force bEv export by Tk | ||
| 38 | *Tk::bEv = sub { | ||
| 39 | if (@_ == 1) { | ||
| 40 | my $arg = $_[0]; | ||
| 41 | return bless (((ref $arg) ? $arg : \$arg), 'Tk::bEv'); | ||
| 42 | } | ||
| 43 | |||
| 44 | return bless [@_],'Tk::Ev'; | ||
| 45 | }; | ||
| 46 | |||
| 47 | # Create localized variables into Tk package. | ||
| 48 | our $bevent; | ||
| 49 | our $bwidget; | ||
| 50 | |||
| 51 | *Tk::bwidget = *bwidget; | ||
| 52 | *Tk::bevent = *bevent; | ||
| 53 | |||
| 54 | # Create a bound event function into the Tk::Widget package. | ||
| 55 | *Tk::Widget::bXEvent = sub { bless \shift, 'Tkbound::bXEvent' }; | ||
| 56 | |||
| 57 | #------------------------------------------------------------------------------- | ||
| 58 | |||
| 59 | # Masks for description of out of bounds bloc. | ||
| 60 | |||
| 61 | my $enum_MASK = 0; | ||
| 62 | use constant { | ||
| 63 | enter_MASK => 1 << $enum_MASK++, | ||
| 64 | before_MASK => 1 << $enum_MASK++, | ||
| 65 | previous_MASK => 1 << $enum_MASK++, | ||
| 66 | after_MASK => 1 << $enum_MASK++, | ||
| 67 | leave_MASK => 1 << $enum_MASK++, | ||
| 68 | }; | ||
| 69 | |||
| 70 | # Mask combinations for bloc description. | ||
| 71 | |||
| 72 | use constant { | ||
| 73 | # next before previous | ||
| 74 | np => enter_MASK | before_MASK | previous_MASK | leave_MASK, | ||
| 75 | nP => enter_MASK | before_MASK | leave_MASK, # next before without previous | ||
| 76 | Np => enter_MASK | previous_MASK | leave_MASK,# previous only | ||
| 77 | NP => enter_MASK | leave_MASK, # void | ||
| 78 | |||
| 79 | # previous before next | ||
| 80 | pn => enter_MASK | previous_MASK | after_MASK | leave_MASK, | ||
| 81 | pN => enter_MASK | previous_MASK | leave_MASK,# previous before without next | ||
| 82 | Pn => enter_MASK | after_MASK | leave_MASK, # next only | ||
| 83 | PN => enter_MASK | leave_MASK, # void | ||
| 84 | }; | ||
| 85 | |||
| 86 | use constant MASKS => qw/np nP Np NP pn pN Pn PN/; | ||
| 87 | use constant MASKS_VALUES => ( np, nP, Np, NP, pn, pN, Pn, PN ); | ||
| 88 | |||
| 89 | use constant DEFAULT_BLOC => Np; # Set default bloc to previous. | ||
| 90 | use constant NEXT_MASK => before_MASK | after_MASK; # next mask | ||
| 91 | use constant BOUND_MASK => enter_MASK | leave_MASK; # bound mask | ||
| 92 | |||
| 93 | { | ||
| 94 | no strict; | ||
| 95 | @EXPORT = MASKS; | ||
| 96 | %EXPORT_TAGS = ( bound_mask => [ MASKS ] ); | ||
| 97 | } | ||
| 98 | |||
| 99 | #------------------------------------------------------------------------------- | ||
| 100 | |||
| 101 | # _boundarray, _boundarray_all -- | ||
| 102 | |||
| 103 | # For each (tag, sequence), associate its outs out bounds level and current | ||
| 104 | # mask. Level stores the out of bounds current deep. Mask stores the current out | ||
| 105 | # of bounds configuration. | ||
| 106 | |||
| 107 | sub _boundarray { | ||
| 108 | my( $toplevel, $tag, $sequence ) = @_; | ||
| 109 | my $h = $toplevel -> TkHash( __PACKAGE__ ); | ||
| 110 | my $key = "$tag,$sequence"; | ||
| 111 | $h -> {$key} = [] unless exists $h -> {$key}; | ||
| 112 | return $h -> {$key}; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _boundarray_all { | ||
| 116 | my( $toplevel ) = @_; | ||
| 117 | |||
| 118 | return map { split(/,/,$_) } (keys %{ $toplevel -> TkHash( __PACKAGE__ ) }); | ||
| 119 | } | ||
| 120 | |||
| 121 | #------------------------------------------------------------------------------- | ||
| 122 | |||
| 123 | # _expand_bEv -- | ||
| 124 | |||
| 125 | # Given a command and an event, produce a new command by replacing % constructs | ||
| 126 | # in the original command with information from the X event. | ||
| 127 | |||
| 128 | sub _expand_bEv { | ||
| 129 | my( $bound_event, $script ) = @_; | ||
| 130 | |||
| 131 | return $script unless( ref $script ); | ||
| 132 | |||
| 133 | return $script unless( reftype $script eq 'ARRAY' ); | ||
| 134 | |||
| 135 | return [ map { ref $_ eq 'Tk::bEv' && exists $bound_event -> { $$_ } ? | ||
| 136 | $bound_event -> { $$_ } : $_ | ||
| 137 | } (@$script) ]; | ||
| 138 | } | ||
| 139 | |||
| 140 | #------------------------------------------------------------------------------- | ||
| 141 | # _enter_wrapper | ||
| 142 | |||
| 143 | # This function is called to open the bloc profile. | ||
| 144 | |||
| 145 | sub _enter_wrapper { | ||
| 146 | my( $widget, $tag, $string, $sequence, $level, $oob, $script, @values ) = @_; | ||
| 147 | |||
| 148 | $widget -> Error( "$oob: unexpected bound mask" ) | ||
| 149 | unless( grep /$oob/, MASKS_VALUES ); | ||
| 150 | |||
| 151 | # Can't set before and after together. | ||
| 152 | $widget -> Error( "panic: unexpected before and after change together" ) | ||
| 153 | if( ($oob & NEXT_MASK) == NEXT_MASK ); | ||
| 154 | |||
| 155 | # Get info. | ||
| 156 | my $info = _boundarray( $widget -> toplevel, $tag, $sequence ); | ||
| 157 | my $sz = @$info; | ||
| 158 | |||
| 159 | # Check enter level. | ||
| 160 | $widget -> Error( "panic: unexpected enter level $level" ) | ||
| 161 | if( $sz == 0 || ! (0 < $level && $level < $sz) ); | ||
| 162 | |||
| 163 | # Compute event changes. | ||
| 164 | my $changes = $oob ^ $info -> [ $level ] -> { mask }; | ||
| 165 | |||
| 166 | # Enter and leave mask change is not expected ! | ||
| 167 | $widget -> Error( 'panic: unexpected mask change' ) | ||
| 168 | if( ($changes & BOUND_MASK) == BOUND_MASK ); | ||
| 169 | |||
| 170 | # Set event values if out of bounds. | ||
| 171 | my $bound_event; | ||
| 172 | if( $oob & NEXT_MASK ) { | ||
| 173 | $bound_event = { t => $tag, | ||
| 174 | s => $string, | ||
| 175 | S => $sequence, | ||
| 176 | m => $oob, | ||
| 177 | c => bless [ @values ], 'Tk::Callback' }; | ||
| 178 | $widget -> { _bXEvent_ } = $bound_event; | ||
| 179 | *bwidget = \$widget; | ||
| 180 | $bevent = bless \$widget, 'Tkbound::bXEvent'; | ||
| 181 | } | ||
| 182 | |||
| 183 | # No event if no change. | ||
| 184 | return unless( $changes ); | ||
| 185 | |||
| 186 | # Update mask and bound events. | ||
| 187 | $info -> [ $level ] -> { mask } = $oob; | ||
| 188 | |||
| 189 | # Expand bound events : | ||
| 190 | # * If script given, expand bEv events; | ||
| 191 | # * If no script given, reuse previous callback. | ||
| 192 | # * Otherwise, set to no operation. | ||
| 193 | $script = ($oob & NEXT_MASK) ? | ||
| 194 | ($script ? _expand_bEv( $bound_event, $script ) : \@values) : | ||
| 195 | 'NoOp'; | ||
| 196 | |||
| 197 | # Update bloc bindings. For the before and after tags, set to new callback. | ||
| 198 | # For the previous tag, reset by binding to NoOp all levels from 1 to current | ||
| 199 | # level minus 1, so that nothing can happen; reset also mask; set by | ||
| 200 | # positionning again the enter at current level minus 1, so that other levels | ||
| 201 | # are restored if necessary. | ||
| 202 | |||
| 203 | my $before = sprintf( "before%d#%s", $level, $tag ); | ||
| 204 | my $after = sprintf( "after%d#%s", $level, $tag ); | ||
| 205 | $widget -> bind( $before, $sequence, $script ) if( $changes & before_MASK ); | ||
| 206 | $widget -> bind( $after, $sequence, $script ) if( $changes & after_MASK ); | ||
| 207 | |||
| 208 | if( $changes & previous_MASK ) { | ||
| 209 | if( ($oob & previous_MASK) != previous_MASK ) { | ||
| 210 | for( my $l = $level - 1; $l > 0; --$l ) { | ||
| 211 | my $E = sprintf( "enter%d#%s", $l, $tag ); | ||
| 212 | my $b = sprintf( "before%d#%s", $l, $tag ); | ||
| 213 | my $a = sprintf( "after%d#%s", $l, $tag ); | ||
| 214 | my $L = sprintf( "leave%d#%s", $l, $tag ); | ||
| 215 | $info -> [ $l ] -> { mask } = DEFAULT_BLOC; | ||
| 216 | $widget -> bind( $E, $sequence, 'NoOp' ); | ||
| 217 | $widget -> bind( $b, $sequence, 'NoOp' ); | ||
| 218 | $widget -> bind( $a, $sequence, 'NoOp' ); | ||
| 219 | $widget -> bind( $L, $sequence, 'NoOp' ); | ||
| 220 | } | ||
| 221 | $widget -> bind( $tag, $sequence, 'NoOp' ); | ||
| 222 | } else { | ||
| 223 | if( $level > 1 ) { | ||
| 224 | my $pv = $info -> [ $level - 1 ] -> { cb }; | ||
| 225 | my $A = sprintf( "enter%d#%s", $level - 1, $tag ); | ||
| 226 | $widget -> bind( $A, $sequence, $pv ); | ||
| 227 | } else { | ||
| 228 | my $pv = $info -> [ 1 ] -> { cb }; | ||
| 229 | $widget -> bind( $tag, $sequence, $pv ); | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | } | ||
| 234 | |||
| 235 | #------------------------------------------------------------------------------- | ||
| 236 | # _leave_wrapper | ||
| 237 | |||
| 238 | # This function is called to close the bloc profile. | ||
| 239 | |||
| 240 | sub _leave_wrapper { | ||
| 241 | my( $widget ) = @_; | ||
| 242 | |||
| 243 | $widget -> { _bXEvent_ } = undef; | ||
| 244 | *bwidget = \undef; | ||
| 245 | $bevent = undef; | ||
| 246 | } | ||
| 247 | |||
| 248 | #------------------------------------------------------------------------------- | ||
| 249 | # normalize_event -- | ||
| 250 | |||
| 251 | # Use bind to ckeck if there is a binding, and the event string syntax; use | ||
| 252 | # virtual event to force event string normalisation. | ||
| 253 | |||
| 254 | sub normalize_event { | ||
| 255 | my( $widget, $string ) = @_; | ||
| 256 | |||
| 257 | # Assume no '<<bound>>' event. | ||
| 258 | $widget -> eventAdd( '<<bound>>', $string ); | ||
| 259 | my $sequence = $widget -> eventInfo( '<<bound>>' ) -> [ 0 ]; | ||
| 260 | $widget -> eventDelete( '<<bound>>', $string ); | ||
| 261 | |||
| 262 | return $sequence; | ||
| 263 | } | ||
| 264 | |||
| 265 | #------------------------------------------------------------------------------- | ||
| 266 | # _create_bounding -- | ||
| 267 | |||
| 268 | # In order to make Ev sequences evaluate correcty, the binding table | ||
| 269 | # properties are used to set up the out of bounds mechanism. | ||
| 270 | |||
| 271 | # A tag in the binding table is set up for out of bounds into a bloc of tags as | ||
| 272 | # follows : | ||
| 273 | # | ||
| 274 | # enter before previous after leave | ||
| 275 | # | ||
| 276 | # * enter is first placed into the binding table to receive control in | ||
| 277 | # a callback that takes the sequence, the out of bounds flags, the values | ||
| 278 | # evaluated for tag, for future use. The whole bloc is controlled | ||
| 279 | # by the out of bounds flag. | ||
| 280 | # | ||
| 281 | # * before and after tags are placed around the previous tag. One of each | ||
| 282 | # is activated following the bounds flag, the other left to NoOp. | ||
| 283 | # | ||
| 284 | # * previous tag is the original tag. It can be also left to NoOp if not | ||
| 285 | # called. | ||
| 286 | # | ||
| 287 | # * leave tag terminates the bloc | ||
| 288 | |||
| 289 | # Blocs can also be surrounded around other such blocs, in which case the | ||
| 290 | # original tag plays the role of the whole bloc enter. Blocs are then indexed | ||
| 291 | # by their level. Level 0 is original callback; level is 1 for thirst bound | ||
| 292 | # binding, and so on. | ||
| 293 | |||
| 294 | sub _create_bounding { | ||
| 295 | my( $widget, $tag, $string, $oob, $script ) = @_; | ||
| 296 | |||
| 297 | # Warning : widget does not refer to the widget that will receive the actual | ||
| 298 | # callback; it could be any widget. The actual widget is linked to tag, for | ||
| 299 | # which legal values beginning by a dot (such as .entry or .), in addition to | ||
| 300 | # special tags such as Entry for class and all for any widget are known to tcl | ||
| 301 | # version of tk. perl itself handles special tags (such as Tk::Entry) for | ||
| 302 | # class. | ||
| 303 | |||
| 304 | # Look for previous callback. After initialisation, the bindings yields to no | ||
| 305 | # boundings : level to 0, enter to tag. There is not way out of bind to | ||
| 306 | # check wether the event string is handled. On first level, catch any error | ||
| 307 | # thrown by bind, and assign a new boundtag element. For other levels, there | ||
| 308 | # is a binding for enter that was previously set up; catching is thus | ||
| 309 | # unecessary. | ||
| 310 | |||
| 311 | # Get the actual event string. | ||
| 312 | my $sequence = normalize_event( $widget, $string ); | ||
| 313 | |||
| 314 | my $original_cb = $widget -> bind( $tag, $sequence ); | ||
| 315 | my $previous_cb; | ||
| 316 | |||
| 317 | my $info = _boundarray( $widget -> toplevel, $tag, $sequence ); | ||
| 318 | my $level = scalar @$info; | ||
| 319 | |||
| 320 | if( $level == 0 ) { | ||
| 321 | # At level 0, if no callback, no bounding is possible. | ||
| 322 | # [ bounding is binding !] | ||
| 323 | $widget -> Error( "no previous callback" ) | ||
| 324 | unless $previous_cb = $original_cb; | ||
| 325 | |||
| 326 | $info -> [ 0 ] = { mask => undef, cb => undef }; | ||
| 327 | $level++; | ||
| 328 | |||
| 329 | } else { | ||
| 330 | # If original callback has been deleted, all boundings should be deleted | ||
| 331 | # too, from level 1 to current level. | ||
| 332 | $widget -> Error( "panic: unexpected original callback deletion" ) | ||
| 333 | unless( $original_cb ); | ||
| 334 | |||
| 335 | my $enter = sprintf( "enter%d#%s", $level - 1, $tag ); | ||
| 336 | $previous_cb = $widget -> bind( $enter, $sequence ); | ||
| 337 | |||
| 338 | # Previous callback cannot be deleted directly. | ||
| 339 | $widget -> Error( "panic: unexpected previous callback deletion" ) | ||
| 340 | unless( $previous_cb ); | ||
| 341 | } | ||
| 342 | |||
| 343 | # Set up block enter so that regular callbacks can be evaluated for out of | ||
| 344 | # bounds. | ||
| 345 | my @ev_oob; | ||
| 346 | if( ref $oob ) { | ||
| 347 | if( ref $oob eq 'CODE' ) { | ||
| 348 | @ev_oob = ([ $oob ]); | ||
| 349 | } elsif( ref $oob eq 'ARRAY' ) { | ||
| 350 | @ev_oob = @$oob; | ||
| 351 | } else { | ||
| 352 | $widget -> Error( "Invalid out of bounds\n" ); | ||
| 353 | } | ||
| 354 | } else { | ||
| 355 | $widget -> Error( "$oob: unexpected bound mask" ) | ||
| 356 | unless( grep /$oob/, MASKS_VALUES ); | ||
| 357 | |||
| 358 | @ev_oob = ([ sub { $oob } ]); | ||
| 359 | } | ||
| 360 | |||
| 361 | # Make new bound tags from bound level, tag and sequence. | ||
| 362 | $info -> [ $level ] = { mask => DEFAULT_BLOC, cb => $previous_cb }; | ||
| 363 | |||
| 364 | my $enter = sprintf( "enter%d#%s", $level, $tag ); | ||
| 365 | my $before = sprintf( "before%d#%s", $level, $tag ); | ||
| 366 | my $after = sprintf( "after%d#%s", $level, $tag ); | ||
| 367 | my $leave = sprintf( "leave%d#%s", $level, $tag ); | ||
| 368 | |||
| 369 | # Make wrapper evaluate arguments of previous callback in its context, for | ||
| 370 | # future use. | ||
| 371 | my @values = ref $previous_cb && reftype $previous_cb eq 'ARRAY' ? | ||
| 372 | @$previous_cb : ($$previous_cb); | ||
| 373 | |||
| 374 | # Set up bloc bindings. | ||
| 375 | my $ew = [ \&_enter_wrapper, | ||
| 376 | $tag, | ||
| 377 | $string, | ||
| 378 | $sequence, | ||
| 379 | $level, | ||
| 380 | Ev(@ev_oob), | ||
| 381 | $script, | ||
| 382 | @values ]; | ||
| 383 | my $lw = \&_leave_wrapper; | ||
| 384 | $widget -> bind( $enter, $sequence, $ew ); | ||
| 385 | $widget -> bind( $before, $sequence, 'NoOp' ); | ||
| 386 | $widget -> bind( $after, $sequence, 'NoOp' ); | ||
| 387 | $widget -> bind( $leave, $sequence, $lw ); | ||
| 388 | } | ||
| 389 | |||
| 390 | #------------------------------------------------------------------------------- | ||
| 391 | # _delete_bounding -- | ||
| 392 | # | ||
| 393 | # Remove an event binding from a binding table. | ||
| 394 | |||
| 395 | sub _delete_bounding { | ||
| 396 | my( $widget, $tag, $string ) = @_; | ||
| 397 | |||
| 398 | my $sequence = normalize_event( $widget, $string ); | ||
| 399 | |||
| 400 | my $info = _boundarray( $widget -> toplevel, $tag, $sequence ); | ||
| 401 | my $level = scalar @$info; | ||
| 402 | |||
| 403 | return unless( $level ); | ||
| 404 | |||
| 405 | # Delete data relative to level. | ||
| 406 | delete $info -> [ --$level ]; | ||
| 407 | |||
| 408 | # Delete tags and bindings accordingly. | ||
| 409 | my $enter = sprintf( "enter%d#%s", $level, $tag ); | ||
| 410 | my $before = sprintf( "before%d#%s", $level, $tag ); | ||
| 411 | my $after = sprintf( "after%d#%s", $level, $tag ); | ||
| 412 | my $leave = sprintf( "leave%d#%s", $level, $tag ); | ||
| 413 | |||
| 414 | $widget -> bind( $enter, $sequence, undef ); | ||
| 415 | $widget -> bind( $before, $sequence, undef ); | ||
| 416 | $widget -> bind( $after, $sequence, undef ); | ||
| 417 | $widget -> bind( $leave, $sequence, undef ); | ||
| 418 | |||
| 419 | # Delete all. | ||
| 420 | delete $info -> [ 0 ] if( $level == 1 ); | ||
| 421 | } | ||
| 422 | |||
| 423 | #------------------------------------------------------------------------------- | ||
| 424 | # _get_bounding -- | ||
| 425 | # | ||
| 426 | # Return the command associated with a given event string. | ||
| 427 | |||
| 428 | sub _get_bounding { | ||
| 429 | my( $widget, $tag, $string ) = @_; | ||
| 430 | |||
| 431 | # Get the actual event string. | ||
| 432 | my $sequence = normalize_event( $widget, $string ); | ||
| 433 | |||
| 434 | my $info = _boundarray( $widget -> toplevel, $tag, $sequence ); | ||
| 435 | my $level = scalar @$info; | ||
| 436 | |||
| 437 | return unless( $level ); | ||
| 438 | |||
| 439 | # return $widget -> bind( $tag, $string ); | ||
| 440 | return $info -> [ 1 ] -> { cb }; | ||
| 441 | } | ||
| 442 | |||
| 443 | #------------------------------------------------------------------------------- | ||
| 444 | # _get_all_boundings -- | ||
| 445 | # | ||
| 446 | # Return a list of event strings for all the boundings associated with a given | ||
| 447 | # object. | ||
| 448 | |||
| 449 | sub _get_all_boundings { | ||
| 450 | my( $widget, $tag ) = @_; | ||
| 451 | |||
| 452 | my $i = 0; | ||
| 453 | my $t; | ||
| 454 | return grep { | ||
| 455 | my $r; | ||
| 456 | SWITCH : { | ||
| 457 | ( $i % 2 == 0 ) && do { | ||
| 458 | # Save tag | ||
| 459 | $t = $_; | ||
| 460 | $r = undef; | ||
| 461 | last; | ||
| 462 | }; | ||
| 463 | ( $t eq $tag ) && do { | ||
| 464 | # Keep $_ for tag | ||
| 465 | $t = undef; | ||
| 466 | $r = 1; | ||
| 467 | last; | ||
| 468 | }; | ||
| 469 | { | ||
| 470 | $r = undef; | ||
| 471 | last; | ||
| 472 | }; | ||
| 473 | } | ||
| 474 | |||
| 475 | ++$i; | ||
| 476 | $r; | ||
| 477 | } _boundarray_all( $widget -> toplevel ); | ||
| 478 | } | ||
| 479 | |||
| 480 | #------------------------------------------------------------------------------- | ||
| 481 | # bound -- | ||
| 482 | |||
| 483 | # The bound command is handled specially, it must *always* be called | ||
| 484 | # with a widget object. And only the <> form of sequence is allowed | ||
| 485 | # so that the following forms of call can be spotted: | ||
| 486 | |||
| 487 | # $widget->bound(); | ||
| 488 | # $widget->bound('tag'); | ||
| 489 | # $widget->bound('<...>'); | ||
| 490 | # $widget->bound('tag','<...>'); | ||
| 491 | # $widget->bound('<...>',oob); | ||
| 492 | # $widget->bound('<...>',oob,script); | ||
| 493 | # $widget->bound('tag','<...>',oob); | ||
| 494 | # $widget->bound('tag','<...>',oob,script); | ||
| 495 | |||
| 496 | sub bound { | ||
| 497 | # bound is *always* be called with a widget object. | ||
| 498 | my $widget = $_[0]; | ||
| 499 | $widget -> Error( "$widget is not a widget" ) | ||
| 500 | unless( ref $widget && $widget -> IsWidget ); | ||
| 501 | |||
| 502 | if( @_ < 2 || @_ > 5 ) { | ||
| 503 | $widget -> Error( "wrong number of arguments bound ?pattern? ?oob ?script??"); | ||
| 504 | return; | ||
| 505 | } | ||
| 506 | |||
| 507 | |||
| 508 | # Normalise arguments to | ||
| 509 | # $widget -> bound( $tag, $event, $oob, $script ); | ||
| 510 | my $string = $_[1]; | ||
| 511 | |||
| 512 | # Evaluate path name if event given. | ||
| 513 | if( substr( $string, 0, 1 ) eq '<' ) { | ||
| 514 | $string = $widget -> PathName; | ||
| 515 | splice @_, 1, 0, $string; | ||
| 516 | } | ||
| 517 | |||
| 518 | # If there are four or five arguments, the command is modifying a | ||
| 519 | # bounding. If there are three arguments, the command is querying a bounding. | ||
| 520 | # If there are only two arguments, the command is querying all the boundings | ||
| 521 | # for the given tag/window. | ||
| 522 | |||
| 523 | if( @_ > 3 ) { | ||
| 524 | my( $sequence, $oob, $script ) = @_[2 .. 4]; | ||
| 525 | |||
| 526 | # If the oob is null, just delete the bounding. | ||
| 527 | return _delete_bounding( $widget, $string, $sequence ) | ||
| 528 | unless( $oob ); | ||
| 529 | |||
| 530 | # Otherwise, create the bounding. | ||
| 531 | return _create_bounding( $widget, $string, $sequence, $oob, $script ); | ||
| 532 | } | ||
| 533 | |||
| 534 | # Get the bounding. | ||
| 535 | return _get_bounding( $widget, $string, $_[2] ) if( @_ == 3 ); | ||
| 536 | |||
| 537 | # Get all boundings. | ||
| 538 | return _get_all_boundings( $widget, $string ); | ||
| 539 | } | ||
| 540 | |||
| 541 | #------------------------------------------------------------------------------- | ||
| 542 | |||
| 543 | package Tkbound::bXEvent; | ||
| 544 | |||
| 545 | use vars qw/$AUTOLOAD/; | ||
| 546 | |||
| 547 | sub DESTROY { } | ||
| 548 | |||
| 549 | sub AUTOLOAD { | ||
| 550 | my $meth = ( $AUTOLOAD =~ /(\w)$/ )[ 0 ]; | ||
| 551 | |||
| 552 | no strict 'refs'; | ||
| 553 | *{ $AUTOLOAD } = sub { | ||
| 554 | my $bevent = shift; | ||
| 555 | $$bevent -> { _bXEvent_ } -> { $meth }; | ||
| 556 | }; | ||
| 557 | |||
| 558 | goto &$AUTOLOAD; | ||
| 559 | } | ||
| 560 | |||
| 561 | |||
| 562 | |||
| 563 | 1; | ||
| 564 | |||
| 565 | __END__ |