| 1 | package Tk::CursorControl; | ||
| 2 | |||
| 3 | require 5.005_62; | ||
| 4 | use Tk 800.015; | ||
| 5 | use Carp; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | $Tk::CursorControl::VERSION = '0.4'; | ||
| 9 | |||
| 10 | my $AlreadyInit = 0; | ||
| 11 | my $CurrentObject = 0; | ||
| 12 | my $Main; | ||
| 13 | |||
| 14 | #Create Aliases to some public methods. | ||
| 15 | *jail = \&confine; | ||
| 16 | *free = \&release; | ||
| 17 | *Show = \&show; | ||
| 18 | |||
| 19 | Construct Tk::Widget 'CursorControl'; | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my ( $me, $parent ) = @_; | ||
| 23 | my $class = ref($me) || $me; | ||
| 24 | my $self = {}; | ||
| 25 | bless $self => $class; | ||
| 26 | |||
| 27 | # provide access to class data | ||
| 28 | $self->{_Init} = \$AlreadyInit; | ||
| 29 | $self->{_CurrentObj} = \$CurrentObject; | ||
| 30 | |||
| 31 | # set MainWindow reference in 'accessible' class data | ||
| 32 | $Main = $parent->MainWindow; | ||
| 33 | $parent->OnDestroy( sub { $self->DESTROY } ); | ||
| 34 | $self->{MAIN} = \$Main if ( defined $Main ); | ||
| 35 | |||
| 36 | if ( ${ $self->{_Init} } == 0 ) { | ||
| 37 | ++${ $self->{_Init} }; | ||
| 38 | $self->_init; | ||
| 39 | ${ $self->{_CurrentObj} } = | ||
| 40 | $self; #store object in case user tries to create two! | ||
| 41 | return $self; | ||
| 42 | } | ||
| 43 | else { | ||
| 44 | ++${ $self->{_Init} }; # DESTROY will be called, so increment anyway | ||
| 45 | # These error messages are now suppressed - JD October 13, 2003 | ||
| 46 | # Thanks for the suggestion Ala. | ||
| 47 | ### carp "A $class object has ALREADY been created !"; | ||
| 48 | ### carp "The object returned is the original object for $class"; | ||
| 49 | # This means that either a module already called Tk::CursorControl on behalf | ||
| 50 | # of the user (i.e. via a 'use SomeModule' where the code within SomeModule | ||
| 51 | # creates a Tk::CursorControl object ---OR--- the programmer didn't read the | ||
| 52 | # documentation and tried to create two or more CursorControl objects for one | ||
| 53 | # MainWindow. | ||
| 54 | return ${ $self->{_CurrentObj} }; #return ORIGINALLY created object | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | # For erroneous understanding of this Class! | ||
| 59 | sub _errmsg { croak "You cannot $_[1] a ", ref( $_[0] ); } | ||
| 60 | |||
| 61 | ########## Public NON-methods ########## | ||
| 62 | # Just in case someone treats this like a Tk widget | ||
| 63 | # Override geometry managers | ||
| 64 | |||
| 65 | sub pack { $_[0]->_errmsg('pack') } | ||
| 66 | sub grid { $_[0]->_errmsg('grid') } | ||
| 67 | sub form { $_[0]->_errmsg('form') } | ||
| 68 | sub place { $_[0]->_errmsg('place') } | ||
| 69 | sub configure { $_[0]->_errmsg('configure') } | ||
| 70 | sub cget { $_[0]->_errmsg('cget') } | ||
| 71 | |||
| 72 | ########## Public Methods ########## | ||
| 73 | sub confine { | ||
| 74 | my ( $self, $widget ) = @_; | ||
| 75 | unless ( defined $widget ) { | ||
| 76 | carp "\$cursor->confine(\$widget)"; | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | |||
| 80 | #free the cursor if already confined elsewhere | ||
| 81 | $self->release if ( $self->{Confined} ); | ||
| 82 | |||
| 83 | #does the widget exist? is it mapped? | ||
| 84 | return unless ( $self->_check($widget) ); | ||
| 85 | |||
| 86 | if ( $self->{Type} eq 'win32' ) { | ||
| 87 | $self->_Win32confine($widget); | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | |||
| 91 | #Then $self->{Type} is the default 'unix' | ||
| 92 | $self->_Unixconfine($widget); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | sub release { | ||
| 97 | my $self = shift; | ||
| 98 | if ( $self->{Type} eq 'win32' ) { | ||
| 99 | $self->_Win32release; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | |||
| 103 | #Then $self->{Type} is the default 'unix' | ||
| 104 | $self->_Unixrelease; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | sub hide { | ||
| 109 | my $self = shift; | ||
| 110 | my $w; | ||
| 111 | |||
| 112 | foreach $w (@_) { | ||
| 113 | |||
| 114 | #bind to Enter and Leave Events | ||
| 115 | if ( $self->{Type} eq 'win32' ) { | ||
| 116 | |||
| 117 | #Showcursor is a system-wide API - so we want to ensure that the cursor doesn't | ||
| 118 | #disappear forever! | ||
| 119 | $self->_Win32saveBindings($w); | ||
| 120 | $self->_Win32setBindings($w); | ||
| 121 | $self->_Win32hidecursor; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | $self->_setOldCursor($w); | ||
| 125 | $w->configure( -cursor => | ||
| 126 | [ '@' . $self->{bitmapfile}, $self->{maskfile}, 'black', 'white' ] ); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub show { | ||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | foreach my $w (@_) { | ||
| 135 | |||
| 136 | #delete bindings for hide Events for specified widget | ||
| 137 | if ( $self->{Type} eq 'win32' ) { | ||
| 138 | $self->_Win32restoreBindings($w); | ||
| 139 | $self->_Win32showcursor; | ||
| 140 | } | ||
| 141 | else { | ||
| 142 | my $cursor = $self->_getOldCursor($w); | ||
| 143 | $w->configure( -cursor => $cursor ) if ($cursor); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | sub moveto { | ||
| 149 | |||
| 150 | # Similar to the warpto sub. Instead - we always use the root window coordinates | ||
| 151 | # So, all we are interested in is the starting x,y coordinates and the ending | ||
| 152 | # x,y coordinates. Most of the code is a copy and paste from the warpto sub. | ||
| 153 | |||
| 154 | my $self = shift; | ||
| 155 | my $w; | ||
| 156 | |||
| 157 | #parse the time off the arguments...there has to be a better way! | ||
| 158 | my $movetime = 1000; #default to 1 second (1000ms) | ||
| 159 | if ( grep /time/, @_ ) { | ||
| 160 | my $i = 0; | ||
| 161 | my $timefound; | ||
| 162 | map { | ||
| 163 | if (/time/) { | ||
| 164 | splice @_, $i, 1; | ||
| 165 | $timefound = pop(@_); | ||
| 166 | $movetime = $timefound if ( $timefound =~ /\d+/ ); | ||
| 167 | } | ||
| 168 | $i++; | ||
| 169 | } @_; | ||
| 170 | } | ||
| 171 | |||
| 172 | #minimum time allowed | ||
| 173 | $movetime = 25 if ( $movetime < 25 ); | ||
| 174 | |||
| 175 | # Three ways of warping: | ||
| 176 | # 1. Pass a widget reference - default warp the cursor to the center. | ||
| 177 | # 2. Pass a widget reference and x,y value - warp the cursor to x,y of that widget. | ||
| 178 | # 3. Pass only an x,y coordinate (with no widget reference) then it is treated like | ||
| 179 | # a screen coordinate. | ||
| 180 | |||
| 181 | my $finalx; | ||
| 182 | my $finaly; | ||
| 183 | my $startx = ${ $self->{MAIN} }->pointerx; | ||
| 184 | my $starty = ${ $self->{MAIN} }->pointery; | ||
| 185 | |||
| 186 | my $argnum = scalar(@_); | ||
| 187 | my $ref = ref( $_[0] ); | ||
| 188 | if ( $ref and $ref =~ /^Tk/ ) { | ||
| 189 | $w = shift; | ||
| 190 | |||
| 191 | # Does the widget exist and is it mapped? | ||
| 192 | return unless ( $self->_check($w) ); | ||
| 193 | |||
| 194 | if ( $argnum == 1 ) { | ||
| 195 | |||
| 196 | #assume only a widget reference passed | ||
| 197 | $self->release if ( $self->{Confined} ); | ||
| 198 | |||
| 199 | #Get ROOT coordinates of the final position | ||
| 200 | $finalx = $w->rootx + ( $w->width / 2 ); | ||
| 201 | $finaly = $w->rooty + ( $w->height / 2 ); | ||
| 202 | |||
| 203 | } | ||
| 204 | elsif ( $argnum == 3 ) { | ||
| 205 | |||
| 206 | #assume a widget reference AND x,y value passed | ||
| 207 | my $x = shift; | ||
| 208 | my $y = shift; | ||
| 209 | |||
| 210 | $self->release if ( $self->{Confined} ); | ||
| 211 | |||
| 212 | #warp pointer to x,y coordinate relative to widgets NW corner | ||
| 213 | my $width = $w->width; | ||
| 214 | my $height = $w->height; | ||
| 215 | $x = 0 if ( $x < 0 ); | ||
| 216 | $x = $width - 1 if ( $x > $width ); | ||
| 217 | $y = 0 if ( $y < 0 ); | ||
| 218 | $y = $height - 1 if ( $y > $height ); | ||
| 219 | |||
| 220 | $finalx = $w->rootx + $x; | ||
| 221 | $finaly = $w->rooty + $y; | ||
| 222 | } | ||
| 223 | } #end if $widget is passed | ||
| 224 | elsif ( $argnum == 2 ) { | ||
| 225 | |||
| 226 | # Assume only an x,y value passed.. | ||
| 227 | |||
| 228 | my $X = shift; | ||
| 229 | my $Y = shift; | ||
| 230 | |||
| 231 | $self->release if ( $self->{Confined} ); | ||
| 232 | |||
| 233 | # Sanity check - don't warp beyond the screen. The window managers won't let you | ||
| 234 | # anyways - but we might as well not try! | ||
| 235 | my $sw = ${ $self->{MAIN} }->screenwidth; | ||
| 236 | my $sh = ${ $self->{MAIN} }->screenheight; | ||
| 237 | |||
| 238 | $X = 0 if ( $X < 0 ); | ||
| 239 | $Y = 0 if ( $Y < 0 ); | ||
| 240 | $X = $sw if ( $X > $sw ); | ||
| 241 | $Y = $sh if ( $Y > $sh ); | ||
| 242 | |||
| 243 | $finalx = $X; | ||
| 244 | $finaly = $Y; | ||
| 245 | |||
| 246 | } | ||
| 247 | |||
| 248 | return unless ( defined $finalx and defined $finaly ); | ||
| 249 | |||
| 250 | #finally "move" the cursor (based on time passed) | ||
| 251 | my $denom = $movetime / 25; | ||
| 252 | my $deltax = ( $finalx - $startx ) / $denom; | ||
| 253 | my $deltay = ( $finaly - $starty ) / $denom; | ||
| 254 | my $interx = $startx; | ||
| 255 | my $intery = $starty; | ||
| 256 | for ( my $i = 1 ; $i <= $denom ; $i++ ) { | ||
| 257 | $interx = $interx + $deltax; | ||
| 258 | $intery = $intery + $deltay; | ||
| 259 | $self->warpto( $interx, $intery ); | ||
| 260 | ${ $self->{MAIN} }->update; | ||
| 261 | |||
| 262 | # Blocking is actually a good thing here. | ||
| 263 | ${ $self->{MAIN} }->after(25); | ||
| 264 | } | ||
| 265 | |||
| 266 | # Now make sure we end up where we originally wanted ! | ||
| 267 | # Round off errors may have crept in. | ||
| 268 | $self->warpto( $finalx, $finaly ); | ||
| 269 | |||
| 270 | } | ||
| 271 | |||
| 272 | sub warpto { | ||
| 273 | |||
| 274 | my $self = shift; | ||
| 275 | my $w; | ||
| 276 | |||
| 277 | # Three ways of warping: | ||
| 278 | # 1. Pass a widget reference - default warp the cursor to the center. | ||
| 279 | # 2. Pass a widget reference and x,y value - warp the cursor to x,y of that widget. | ||
| 280 | # 3. Pass only an x,y coordinate (with no widget reference) then it is treated like | ||
| 281 | # a screen coordinate. | ||
| 282 | |||
| 283 | my $argnum = scalar(@_); | ||
| 284 | my $ref = ref( $_[0] ); | ||
| 285 | if ( $ref and $ref =~ /^Tk/ ) { | ||
| 286 | $w = shift; | ||
| 287 | |||
| 288 | # Does the widget exist and is it mapped? | ||
| 289 | return unless ( $self->_check($w) ); | ||
| 290 | |||
| 291 | if ( $argnum == 1 ) { | ||
| 292 | |||
| 293 | #assume only a widget reference passed | ||
| 294 | $self->release if ( $self->{Confined} ); | ||
| 295 | |||
| 296 | #warp pointer to the center of the widget | ||
| 297 | my $x = ( $w->width / 2 ); | ||
| 298 | my $y = ( $w->height / 2 ); | ||
| 299 | |||
| 300 | $w->eventGenerate( | ||
| 301 | "<Motion>", | ||
| 302 | -when => 'head', | ||
| 303 | -x => $x, | ||
| 304 | -y => $y, | ||
| 305 | -warp => 1 | ||
| 306 | ); | ||
| 307 | } | ||
| 308 | elsif ( $argnum == 3 ) { | ||
| 309 | |||
| 310 | #assume a widget reference AND x,y value passed | ||
| 311 | my $x = shift; | ||
| 312 | my $y = shift; | ||
| 313 | |||
| 314 | $self->release if ( $self->{Confined} ); | ||
| 315 | |||
| 316 | #warp pointer to x,y coordinate relative to widgets NW corner | ||
| 317 | my $width = $w->width; | ||
| 318 | my $height = $w->height; | ||
| 319 | $x = 0 if ( $x < 0 ); | ||
| 320 | $x = $width - 1 if ( $x > $width ); | ||
| 321 | $y = 0 if ( $y < 0 ); | ||
| 322 | $y = $height - 1 if ( $y > $height ); | ||
| 323 | |||
| 324 | $w->eventGenerate( | ||
| 325 | "<Motion>", | ||
| 326 | -when => 'head', | ||
| 327 | -x => $x, | ||
| 328 | -y => $y, | ||
| 329 | -warp => 1 | ||
| 330 | ); | ||
| 331 | } | ||
| 332 | } #end if $widget is passed | ||
| 333 | elsif ( $argnum == 2 ) { | ||
| 334 | |||
| 335 | # Assume only an x,y value passed.. | ||
| 336 | # Warp to specific screen coordinates. There is no way to do this outright using | ||
| 337 | # eventGenerate...at least "I" couldn't find one. | ||
| 338 | # So we use the rootx and rooty of the MainWindow as our anchor position. | ||
| 339 | # and we might end up actually warping to negative values of x and y. | ||
| 340 | |||
| 341 | my $X = shift; | ||
| 342 | my $Y = shift; | ||
| 343 | |||
| 344 | $self->release if ( $self->{Confined} ); | ||
| 345 | |||
| 346 | # works on windows even if iconified - check on unix. Maybe not needed!! | ||
| 347 | # return unless ( $self->_check(${$self->{MAIN}}) ); | ||
| 348 | |||
| 349 | # Sanity check - don't warp beyond the screen. The window managers won't let you | ||
| 350 | # anyways - but we might as well not try! | ||
| 351 | my $sw = ${ $self->{MAIN} }->screenwidth; | ||
| 352 | my $sh = ${ $self->{MAIN} }->screenheight; | ||
| 353 | |||
| 354 | $X = 0 if ( $X < 0 ); | ||
| 355 | $Y = 0 if ( $Y < 0 ); | ||
| 356 | $X = $sw if ( $X > $sw ); | ||
| 357 | $Y = $sh if ( $Y > $sh ); | ||
| 358 | |||
| 359 | my $x = $X - ${ $self->{MAIN} }->rootx; | ||
| 360 | my $y = $Y - ${ $self->{MAIN} }->rooty; | ||
| 361 | |||
| 362 | ${ $self->{MAIN} }->eventGenerate( | ||
| 363 | "<Motion>", | ||
| 364 | -when => 'head', | ||
| 365 | -x => $x, | ||
| 366 | -y => $y, | ||
| 367 | -warp => 1 | ||
| 368 | ); | ||
| 369 | } | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | sub destroy { shift->DESTROY } | ||
| 374 | |||
| 375 | ############# Private methods ################## | ||
| 376 | sub _check { | ||
| 377 | my ( $self, $w ) = @_; | ||
| 378 | my $ok = 1; | ||
| 379 | unless ( $ok = Exists($w) ) { | ||
| 380 | carp "Widget $w: Does not exist!"; | ||
| 381 | } | ||
| 382 | unless ( $ok = $w->viewable ) { | ||
| 383 | carp "Widget $w: is not mapped"; | ||
| 384 | } | ||
| 385 | return $ok; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub _getposition { | ||
| 389 | |||
| 390 | #return the top left corner and width/height of widget passed. | ||
| 391 | my ( $self, $w ) = @_; | ||
| 392 | my $x0 = $w->rootx; | ||
| 393 | my $y0 = $w->rooty; | ||
| 394 | my $width = $w->width; | ||
| 395 | my $height = $w->height; | ||
| 396 | |||
| 397 | return ( $x0, $y0, $width, $height ); | ||
| 398 | } | ||
| 399 | |||
| 400 | sub _getbbox { | ||
| 401 | |||
| 402 | #return the absolute screen coordinates (i.e. bbox) | ||
| 403 | my ( $self, $w ) = @_; | ||
| 404 | my @c = $self->_getposition($w); | ||
| 405 | return ( $c[0], $c[1], $c[0] + $c[2], $c[1] + $c[3] ); | ||
| 406 | } | ||
| 407 | |||
| 408 | sub _init { | ||
| 409 | my $self = shift; | ||
| 410 | |||
| 411 | $self->{Type} = 'unix'; #default to 'perl based' cursor confine | ||
| 412 | |||
| 413 | if ( $Tk::platform eq 'MSWin32' ) { | ||
| 414 | if ( eval "require Win32::API" ) { | ||
| 415 | |||
| 416 | #Create API's | ||
| 417 | $self->{ClipCursor} = | ||
| 418 | Win32::API->new( 'user32', 'ClipCursor', ['P'], 'N' ); | ||
| 419 | $self->{ShowCursor} = | ||
| 420 | Win32::API->new( 'user32', 'ShowCursor', ['N'], 'N' ); | ||
| 421 | $self->{Type} = 'win32' if ( $self->{ClipCursor} && $self->{ShowCursor} ); | ||
| 422 | $self->{DisplayCount} = 0; | ||
| 423 | return if $self->{Type} eq 'win32'; | ||
| 424 | croak "Creating API objects failed for unknown reason"; | ||
| 425 | } | ||
| 426 | else { | ||
| 427 | croak "Please install Win32::API !!"; | ||
| 428 | } | ||
| 429 | } | ||
| 430 | else { | ||
| 431 | |||
| 432 | #retrieve proper filenames for transparent cursor for *NIX.. | ||
| 433 | $self->{bitmapfile} = Tk->findINC('trans_cur.xbm'); | ||
| 434 | $self->{maskfile} = Tk->findINC('trans_cur.mask'); | ||
| 435 | croak "Files for tranparent cursor not found" | ||
| 436 | unless ( $self->{bitmapfile} && $self->{maskfile} ); | ||
| 437 | } | ||
| 438 | } | ||
| 439 | |||
| 440 | ####### On Unix Only ############# | ||
| 441 | sub _setOldCursor { | ||
| 442 | |||
| 443 | #save the current cursor on a ->hide command | ||
| 444 | my ( $self, $w ) = @_; | ||
| 445 | my $oldcursor = $w->cget('-cursor') || 'left_ptr'; | ||
| 446 | $self->{OldCursor}{$w} = $oldcursor if ($oldcursor); | ||
| 447 | } | ||
| 448 | |||
| 449 | sub _getOldCursor { | ||
| 450 | |||
| 451 | #get saved cursor on a ->show command | ||
| 452 | my ( $self, $w ) = @_; | ||
| 453 | my $cursor = $self->{OldCursor}{$w}; | ||
| 454 | delete $self->{OldCursor}{$w}; | ||
| 455 | return ($cursor); | ||
| 456 | } | ||
| 457 | |||
| 458 | sub _Unixconfine { | ||
| 459 | my ( $self, $w ) = @_; | ||
| 460 | my @coords = $self->_getposition($w); | ||
| 461 | |||
| 462 | # Stop Class <Leave> bindings from being triggered - as a Leave should NEVER occur | ||
| 463 | # Why? Because the cursor is 'supposed' to be confined to the widget! Since this | ||
| 464 | # event 'will' still occur we Tk->break it before the Class binding occurs. | ||
| 465 | # An example if we did not do this (and I know from testing) is: a Button relief rapidly | ||
| 466 | # changing between sunken and normal to causing massive flickering | ||
| 467 | |||
| 468 | # Also - We cannot guarantee a Motion binding will get triggered for the passed widget. | ||
| 469 | # Instead we overwrite the Motion binding for the toplevel containg $widget. This virtually | ||
| 470 | # guarantees (fingers crossed) that a proper Motion binding exists. Of course the Leave | ||
| 471 | # event must stay with the passed $widget. | ||
| 472 | |||
| 473 | my @bindtags = $w->bindtags; | ||
| 474 | $w->bindtags( [ @bindtags[ 1, 0, 2, 3 ] ] ); | ||
| 475 | |||
| 476 | #Save current leave binding..if there is one | ||
| 477 | my $old_leave; | ||
| 478 | my $old_motion; | ||
| 479 | eval { $old_leave = $w->Tk::bind('<Any-Leave>') }; | ||
| 480 | eval { $old_motion = $w->toplevel->bind('<Motion>') }; | ||
| 481 | ( defined $old_leave ) | ||
| 482 | ? ( $self->{OldLeave} = $old_leave ) | ||
| 483 | : ( $self->{OldLeave} = 0 ); | ||
| 484 | ( defined $old_motion ) | ||
| 485 | ? ( $self->{OldMotion} = $old_motion ) | ||
| 486 | : ( $self->{OldMotion} = 0 ); | ||
| 487 | |||
| 488 | $w->Tk::bind( '<Any-Leave>', sub { $_[0]->break } ); | ||
| 489 | |||
| 490 | $w->toplevel->bind( | ||
| 491 | '<Motion>', | ||
| 492 | sub { | ||
| 493 | $self->{OldMotion}->Call if ( $self->{OldMotion} ); | ||
| 494 | $self->_warpToConfine( $w, @coords ); | ||
| 495 | } | ||
| 496 | ); | ||
| 497 | $self->{Confined} = $w; | ||
| 498 | } | ||
| 499 | |||
| 500 | sub _Unixrelease { | ||
| 501 | my $self = shift; | ||
| 502 | |||
| 503 | #Restore proper bindtag order for widget.. | ||
| 504 | return unless $self->{Confined}; | ||
| 505 | my @bindtags = $self->{Confined}->bindtags; | ||
| 506 | $self->{Confined}->bindtags( [ @bindtags[ 1, 0, 2, 3 ] ] ); | ||
| 507 | |||
| 508 | ( $self->{OldLeave} ) | ||
| 509 | ? ( $self->{Confined}->Tk::bind( '<Any-Leave>', $self->{OldLeave} ) ) | ||
| 510 | : ( $self->{Confined}->Tk::bind( '<Any-Leave>', '' ) ); | ||
| 511 | ( $self->{OldMotion} ) | ||
| 512 | ? ( $self->{Confined}->toplevel->bind( '<Motion>', $self->{OldMotion} ) ) | ||
| 513 | : ( $self->{Confined}->toplevel->bind( '<Motion>', '' ) ); | ||
| 514 | |||
| 515 | $self->{Confined} = 0; | ||
| 516 | } | ||
| 517 | |||
| 518 | sub _warpToConfine { | ||
| 519 | my ( $self, $w, $x0, $y0, $wi, $he ) = @_; | ||
| 520 | |||
| 521 | my $e = $w->XEvent; | ||
| 522 | my ( $x, $y ) = ( $e->x, $e->y ); | ||
| 523 | |||
| 524 | my $warpneeded = 0; | ||
| 525 | |||
| 526 | if ( $x <= 0 ) { | ||
| 527 | $x = 1; | ||
| 528 | $warpneeded = 1; | ||
| 529 | } | ||
| 530 | elsif ( $x >= $wi ) { | ||
| 531 | $x = $wi - 1; | ||
| 532 | $warpneeded = 1; | ||
| 533 | } | ||
| 534 | |||
| 535 | if ( $y <= 0 ) { | ||
| 536 | $y = 1; | ||
| 537 | $warpneeded = 1; | ||
| 538 | } | ||
| 539 | elsif ( $y >= $he ) { | ||
| 540 | $y = $he - 1; | ||
| 541 | $warpneeded = 1; | ||
| 542 | } | ||
| 543 | |||
| 544 | if ($warpneeded) { | ||
| 545 | $w->eventGenerate( | ||
| 546 | "<Motion>", | ||
| 547 | -when => 'head', | ||
| 548 | -x => $x, | ||
| 549 | -y => $y, | ||
| 550 | -warp => 1 | ||
| 551 | ); | ||
| 552 | } | ||
| 553 | } | ||
| 554 | |||
| 555 | ########### On Win32 Only ############# | ||
| 556 | sub _Win32confine { | ||
| 557 | my ( $self, $w ) = @_; | ||
| 558 | my @coords = $self->_getbbox($w); | ||
| 559 | |||
| 560 | my $rect = CORE::pack "L4", @coords; | ||
| 561 | |||
| 562 | if ( defined $self->{ClipCursor} ) { | ||
| 563 | $self->{ClipCursor}->Call($rect); | ||
| 564 | $self->{Confined} = 1; | ||
| 565 | } | ||
| 566 | } | ||
| 567 | |||
| 568 | sub _Win32release { | ||
| 569 | my $self = shift; | ||
| 570 | my $null = 0; | ||
| 571 | if ( defined $self->{ClipCursor} ) { | ||
| 572 | $self->{ClipCursor}->Call($null); | ||
| 573 | $self->{Confined} = 0; | ||
| 574 | } | ||
| 575 | } | ||
| 576 | |||
| 577 | sub _Win32saveBindings { | ||
| 578 | my ( $self, $w ) = @_; | ||
| 579 | |||
| 580 | # Save current Enter, Leave and Unmap bindings..if there are any | ||
| 581 | # Fully specify Tk::bind in case of a canvas widget. | ||
| 582 | my $old_leave; | ||
| 583 | my $old_enter; | ||
| 584 | my $old_unmap; | ||
| 585 | eval { $old_leave = $w->Tk::bind('<Leave>') }; | ||
| 586 | eval { $old_enter = $w->Tk::bind('<Enter>') }; | ||
| 587 | eval { $old_unmap = $w->Tk::bind('<Unmap>') }; | ||
| 588 | ( defined $old_leave ) | ||
| 589 | ? ( $self->{Win32Leave}{$w} = $old_leave ) | ||
| 590 | : ( $self->{Win32Leave}{$w} = 0 ); | ||
| 591 | ( defined $old_enter ) | ||
| 592 | ? ( $self->{Win32Enter}{$w} = $old_enter ) | ||
| 593 | : ( $self->{Win32Enter}{$w} = 0 ); | ||
| 594 | ( defined $old_unmap ) | ||
| 595 | ? ( $self->{Win32Unmap}{$w} = $old_unmap ) | ||
| 596 | : ( $self->{Win32Unmap}{$w} = 0 ); | ||
| 597 | } | ||
| 598 | |||
| 599 | sub _Win32setBindings { | ||
| 600 | my ( $self, $w ) = @_; | ||
| 601 | $w->Tk::bind( | ||
| 602 | '<Enter>', | ||
| 603 | sub { | ||
| 604 | $self->{Win32Enter}{$w}->Call if ( $self->{Win32Enter}{$w} ); | ||
| 605 | $self->_Win32hidecursor; | ||
| 606 | } | ||
| 607 | ); | ||
| 608 | $w->Tk::bind( | ||
| 609 | '<Leave>', | ||
| 610 | sub { | ||
| 611 | $self->{Win32Leave}{$w}->Call if ( $self->{Win32Leave}{$w} ); | ||
| 612 | $self->_Win32showcursor; | ||
| 613 | } | ||
| 614 | ); | ||
| 615 | |||
| 616 | #ensure cursor gets shown again if widget disappears.. | ||
| 617 | $w->Tk::bind( | ||
| 618 | '<Unmap>', | ||
| 619 | sub { | ||
| 620 | $self->{Win32Unmap}{$w}->Call if ( $self->{Win32Unmap}{$w} ); | ||
| 621 | $self->_Win32showcursor; | ||
| 622 | } | ||
| 623 | ); | ||
| 624 | } | ||
| 625 | |||
| 626 | sub _Win32restoreBindings { | ||
| 627 | my ( $self, $w ) = @_; | ||
| 628 | ( $self->{Win32Enter}{$w} ) | ||
| 629 | ? ( $w->Tk::bind( '<Enter>', $self->{Win32Enter}{$w} ) ) | ||
| 630 | : ( $w->Tk::bind( '<Enter>', '' ) ); | ||
| 631 | ( $self->{Win32Leave}{$w} ) | ||
| 632 | ? ( $w->Tk::bind( '<Leave>', $self->{Win32Leave}{$w} ) ) | ||
| 633 | : ( $w->Tk::bind( '<Leave>', '' ) ); | ||
| 634 | ( $self->{Win32Unmap}{$w} ) | ||
| 635 | ? ( $w->Tk::bind( '<Unmap>', $self->{Win32Unmap}{$w} ) ) | ||
| 636 | : ( $w->Tk::bind( '<Unmap>', '' ) ); | ||
| 637 | } | ||
| 638 | |||
| 639 | sub _Win32hidecursor { | ||
| 640 | my $self = shift; | ||
| 641 | $self->_Win32resetDisplayCount; | ||
| 642 | $self->{DisplayCount} = $self->{ShowCursor}->Call(0); | ||
| 643 | } | ||
| 644 | |||
| 645 | sub _Win32showcursor { | ||
| 646 | my $self = shift; | ||
| 647 | $self->_Win32resetDisplayCount; | ||
| 648 | $self->{DisplayCount} = $self->{ShowCursor}->Call(1); | ||
| 649 | } | ||
| 650 | |||
| 651 | sub _Win32resetDisplayCount { | ||
| 652 | my $self = shift; | ||
| 653 | |||
| 654 | #Decrement display count to get ready for a hide. i.e. set count to 0. | ||
| 655 | if ( $self->{DisplayCount} > 0 ) { | ||
| 656 | my $count = $self->{DisplayCount}; | ||
| 657 | for ( my $i = $count ; $i > 0 ; $i-- ) { | ||
| 658 | $self->{DisplayCount} = $self->{ShowCursor}->Call(0); | ||
| 659 | } | ||
| 660 | } | ||
| 661 | |||
| 662 | #Increment display count to get ready for a show. i.e. set count to -1. | ||
| 663 | elsif ( $self->{DisplayCount} < 0 ) { | ||
| 664 | my $count = $self->{DisplayCount}; | ||
| 665 | for ( my $i = $count ; $i < -1 ; $i++ ) { | ||
| 666 | $self->{DisplayCount} = $self->{ShowCursor}->Call(1); | ||
| 667 | } | ||
| 668 | } | ||
| 669 | } | ||
| 670 | |||
| 671 | ######################################## | ||
| 672 | sub DESTROY { | ||
| 673 | my $self = shift; | ||
| 674 | $self->release; | ||
| 675 | ##carp "DESTROY CALLED ON $self"; | ||
| 676 | --${ $self->{_Init} }; #decrement initialize value | ||
| 677 | } | ||
| 678 | |||
| 679 | 1; | ||
| 680 | |||
| 681 | __END__ |