| 1 | # -*- mode: perl; fill-column: 80; comment-column: 80; -*- | ||
| 2 | |||
| 3 | # Tkboundtags -- | ||
| 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: Tkboundtags.pm 56 2008-01-10 23:23:59Z meccanomania $ | ||
| 13 | #------------------------------------------------------------------------------- | ||
| 14 | |||
| 15 | package Tkboundtags; | ||
| 16 | |||
| 17 | $VERSION = '$Revision: 56 $' =~ /\D(\d+)\s*\$$/; | ||
| 18 | |||
| 19 | use Tk::ErrorDialog; | ||
| 20 | |||
| 21 | #------------------------------------------------------------------------------- | ||
| 22 | |||
| 23 | # Wrap bound and bountags into Tk package. | ||
| 24 | |||
| 25 | *Tk::boundtags = \&boundtags; | ||
| 26 | |||
| 27 | #------------------------------------------------------------------------------- | ||
| 28 | # _get_boundtags -- | ||
| 29 | |||
| 30 | sub _get_boundtags { | ||
| 31 | my( $widget ) = @_; | ||
| 32 | |||
| 33 | my @bindtags = $widget -> bindtags; | ||
| 34 | |||
| 35 | my @taglist; | ||
| 36 | my @levellist; | ||
| 37 | |||
| 38 | for( my $bound_index = 0; $bound_index < @bindtags; $bound_index++ ) { | ||
| 39 | next unless( my($level, $tag) = | ||
| 40 | $bindtags[ $bound_index ] =~ /^enter(\d+)#(.*)$/ ); | ||
| 41 | push @taglist, $tag; | ||
| 42 | push @levellist, $level; | ||
| 43 | } | ||
| 44 | |||
| 45 | return( [ @taglist ], [ @levellist ] ); | ||
| 46 | } | ||
| 47 | |||
| 48 | #------------------------------------------------------------------------------- | ||
| 49 | # _set_boundtags -- | ||
| 50 | |||
| 51 | sub _set_boundtags { | ||
| 52 | my( $widget, $taglist, $levellist ) = @_; | ||
| 53 | my @boundtags; | ||
| 54 | |||
| 55 | # Reset bindtags if not found for a class tag. We get called from | ||
| 56 | # SetBindtags for force overwrite of tcl tk defaults bindings into perl tk | ||
| 57 | # default bindings in class derivation in mega or derived widget, in which | ||
| 58 | # case level is 0. | ||
| 59 | |||
| 60 | my $bindtag_index = 0; | ||
| 61 | for( my $tag_index = 0; $tag_index < @$taglist; $tag_index++ ) { | ||
| 62 | my $tag = $taglist -> [$tag_index]; | ||
| 63 | $tag = $tag -> PathName if( ref $tag && $tag -> IsWidget ); | ||
| 64 | |||
| 65 | # Find where to insert/delete, and level. If not found, this is insertion at | ||
| 66 | # level 0. | ||
| 67 | my $bound_index; | ||
| 68 | my $d = 0; | ||
| 69 | for( $bound_index = 0; $bound_index < @boundtags; $bound_index++ ) { | ||
| 70 | last if(($d) = $boundtags[ $bound_index ] =~ | ||
| 71 | /^(?:enter(\d+)#)?${tag}$/ ); | ||
| 72 | } | ||
| 73 | |||
| 74 | # Bind the tag before bounding it. | ||
| 75 | if( $bound_index == @boundtags ) { | ||
| 76 | $bound_index = $bindtag_index; | ||
| 77 | $boundtags[ $bound_index ] = $tag; | ||
| 78 | $bindtag_index++; | ||
| 79 | } | ||
| 80 | |||
| 81 | next unless( my $level = $levellist -> [$tag_index] ); | ||
| 82 | |||
| 83 | # Increase/decrease level | ||
| 84 | if( $d <= $level ) { | ||
| 85 | for( ++$d; $d <= $level; $d++ ) { | ||
| 86 | my $enter = sprintf( "enter%d#%s", $d, $tag ); | ||
| 87 | my $before = sprintf( "before%d#%s", $d, $tag ); | ||
| 88 | my $after = sprintf( "after%d#%s", $d, $tag ); | ||
| 89 | my $leave = sprintf( "leave%d#%s", $d, $tag ); | ||
| 90 | splice @boundtags, $bound_index, 0, $enter, $before; | ||
| 91 | splice @boundtags, $bound_index + 4 * $d - 1, 0, $after, $leave; | ||
| 92 | } | ||
| 93 | $bindtag_index += 4 * $level; | ||
| 94 | |||
| 95 | } else { | ||
| 96 | for( ; $level < $d; --$d ) { | ||
| 97 | splice @boundtags, $bound_index + 4 * $d - 1, 2; | ||
| 98 | splice @boundtags, $bound_index, 2; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | # Update binding table. | ||
| 104 | $widget -> bindtags( \@boundtags ); | ||
| 105 | } | ||
| 106 | |||
| 107 | #------------------------------------------------------------------------------- | ||
| 108 | # boundtags -- | ||
| 109 | # | ||
| 110 | # This procedure is invoked to process the "bindtags" Tcl command. | ||
| 111 | # See the user documentation for details on what it does. | ||
| 112 | |||
| 113 | sub boundtags { | ||
| 114 | my $widget = $_[0]; | ||
| 115 | $widget -> Error( "$widget is not a widget" ) | ||
| 116 | unless( $widget -> IsWidget ); | ||
| 117 | |||
| 118 | # Read bounding table. | ||
| 119 | return _get_boundtags( $widget ) if( @_ == 1 ); | ||
| 120 | |||
| 121 | # Set bounding table. | ||
| 122 | $widget -> Error( "wrong number of arguments" ) if( @_ != 3 ); | ||
| 123 | |||
| 124 | my( $taglist, $levellist ) = @_[ 1, 2 ]; | ||
| 125 | $widget -> Error( "invalid argument type" ) | ||
| 126 | unless( ref $taglist eq 'ARRAY' && | ||
| 127 | ref $levellist eq 'ARRAY' ); | ||
| 128 | |||
| 129 | _set_boundtags( $widget, $taglist, $levellist ); | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | #------------------------------------------------------------------------------- | ||
| 134 | |||
| 135 | 1; | ||
| 136 | |||
| 137 | __END__ |