| 1 | #CursorControl, Manipulate the cursor programmatically. | ||
| 2 | |||
| 3 | use Tk; | ||
| 4 | use Tk::CursorControl; | ||
| 5 | |||
| 6 | use vars qw/$TOP/; | ||
| 7 | use subs | ||
| 8 | qw/cursor_toggleit cursor_warpit cursor_items_start_drag cursor_items_drag/; | ||
| 9 | use vars qw/$temptime $movetime $timescale $timewarp $dir $cursor $direction | ||
| 10 | $trans $place %info $jail %wid $event $r $c @coords/; | ||
| 11 | |||
| 12 | sub cursor { | ||
| 13 | my ($demo) = @_; | ||
| 14 | my $text = qq/ | ||
| 15 | Warp, move, confine and hide your mouse pointer! | ||
| 16 | Use the controls below to warp or move | ||
| 17 | the cursor to any part of the screen | ||
| 18 | or any part of a widget. The nine Radiobuttons and | ||
| 19 | the Optionmenu are used to control your target. | ||
| 20 | You can also make the cursor | ||
| 21 | disappear when it is over the canvas. The "Hide Cursor" button | ||
| 22 | controls whether or not you can see the cursor when it is over | ||
| 23 | the canvas. The "Confine Cursor" button will determine whether | ||
| 24 | or not to confine your mouse cursor to the canvas on a buttonpress. | ||
| 25 | Try moving the rectangle via B1 Click and drag, with this feature | ||
| 26 | on or off. See the difference? | ||
| 27 | See perldoc Tk::CursorControl for more information./; | ||
| 28 | $text =~ s/\n/ /g; | ||
| 29 | $TOP = $MW->WidgetDemo( | ||
| 30 | -name => $demo, | ||
| 31 | -text => $text, | ||
| 32 | -title => 'CursorControl Demonstration', | ||
| 33 | -iconname => 'Cursor', | ||
| 34 | -geometry_manager => 'grid', | ||
| 35 | ); | ||
| 36 | |||
| 37 | $direction = 'c'; | ||
| 38 | $trans = 'show'; | ||
| 39 | $place = 'Screen'; | ||
| 40 | $jail = 1; | ||
| 41 | |||
| 42 | $cursor = $TOP->CursorControl; | ||
| 43 | $wid{mainframe} = $TOP->Frame()->grid( -sticky => 'nsew' ); | ||
| 44 | $wid{frame} = $wid{mainframe}->Frame( -relief => 'sunken', -bd => 3 )->grid( | ||
| 45 | -row => 0, | ||
| 46 | -column => 0, | ||
| 47 | -columnspan => 4, | ||
| 48 | -sticky => 'nsew' | ||
| 49 | ); | ||
| 50 | $wid{canvas} = $wid{frame}->Canvas( | ||
| 51 | -width => 240, | ||
| 52 | -height => 240, | ||
| 53 | -bg => 'papayawhip', | ||
| 54 | -bd => 0, | ||
| 55 | -highlightthickness => 0 | ||
| 56 | )->grid( -sticky => 'nsew' ); | ||
| 57 | |||
| 58 | $wid{canvas}->create( | ||
| 59 | 'rectangle', 10, 10, 60, 60, | ||
| 60 | -fill => 'sienna', | ||
| 61 | -tags => ['RECT'] | ||
| 62 | ); | ||
| 63 | |||
| 64 | $wid{canvas}->Tk::bind( | ||
| 65 | '<1>' => sub { | ||
| 66 | $event = $wid{canvas}->XEvent; | ||
| 67 | $cursor->confine( $wid{canvas} ) if ($jail); | ||
| 68 | &items_start_drag( $event->x, $event->y ); | ||
| 69 | } | ||
| 70 | ); | ||
| 71 | $wid{canvas}->Tk::bind( | ||
| 72 | '<B1-Motion>' => sub { &cursor_items_drag( $Tk::event->x, $Tk::event->y ); } | ||
| 73 | ); | ||
| 74 | $wid{canvas} | ||
| 75 | ->Tk::bind( '<ButtonRelease-1>' => sub { $cursor->release if ($jail); } ); | ||
| 76 | |||
| 77 | $wid{mainframe}->Button( | ||
| 78 | -text => 'DO IT', | ||
| 79 | -background => 'lightgreen', | ||
| 80 | -command => \&cursor_doit | ||
| 81 | )->grid( | ||
| 82 | -row => 4, | ||
| 83 | -column => 3, | ||
| 84 | -rowspan => 1, | ||
| 85 | -sticky => 'nsew' | ||
| 86 | ); | ||
| 87 | |||
| 88 | $dir = { | ||
| 89 | 'nw' => [ 0, 0 ], | ||
| 90 | 'n' => [ 0.5, 0 ], | ||
| 91 | 'ne' => [ 1.0, 0 ], | ||
| 92 | 'w' => [ 0, 0.5 ], | ||
| 93 | 'c' => [ 0.5, 0.5 ], | ||
| 94 | 'e' => [ 1.0, 0.5 ], | ||
| 95 | 'sw' => [ 0, 1.0 ], | ||
| 96 | 's' => [ 0.5, 1.0 ], | ||
| 97 | 'se' => [ 1.0, 1.0 ], | ||
| 98 | }; | ||
| 99 | |||
| 100 | $r = 0; | ||
| 101 | $c = 0; | ||
| 102 | foreach (qw/nw n ne w c e sw s se/) { | ||
| 103 | $r++ unless ( $c % 3 ); | ||
| 104 | $c = 0 unless ( $c % 3 ); | ||
| 105 | $wid{mainframe}->Radiobutton( | ||
| 106 | -value => $_, | ||
| 107 | -variable => \$direction | ||
| 108 | )->grid( | ||
| 109 | -row => $r, | ||
| 110 | -column => $c, | ||
| 111 | -sticky => 'w' | ||
| 112 | ); | ||
| 113 | $c++; | ||
| 114 | } | ||
| 115 | $wid{mainframe}->Optionmenu( | ||
| 116 | -textvariable => \$place, | ||
| 117 | -options => [ 'Screen', 'Canvas', 'Rectangle Item' ] | ||
| 118 | )->grid( | ||
| 119 | -row => 4, | ||
| 120 | -column => 0, | ||
| 121 | -sticky => 'nsew', | ||
| 122 | -columnspan => 3 | ||
| 123 | ); | ||
| 124 | |||
| 125 | $wid{mainframe}->Checkbutton( | ||
| 126 | -text => "Hide Cursor", | ||
| 127 | -onvalue => 'hide', | ||
| 128 | -offvalue => 'show', | ||
| 129 | -variable => \$trans, | ||
| 130 | -command => \&cursor_toggleit | ||
| 131 | )->grid( -row => 1, -column => 3, -sticky => 'w' ); | ||
| 132 | $wid{mainframe}->Checkbutton( | ||
| 133 | -text => "Confine Cursor", | ||
| 134 | -variable => \$jail | ||
| 135 | )->grid( -row => 2, -column => 3, -sticky => 'w' ); | ||
| 136 | $timescale = $wid{mainframe}->Scale( | ||
| 137 | -variable => \$movetime, | ||
| 138 | -resolution => 0.5, | ||
| 139 | -label => "Time (s)", | ||
| 140 | -orient => 'vertical', | ||
| 141 | -from => 5, | ||
| 142 | -to => 0, | ||
| 143 | -width => 10 | ||
| 144 | )->grid( -row => 1, -column => 4, -rowspan => 4 ); | ||
| 145 | $wid{mainframe}->Checkbutton( | ||
| 146 | -text => "Time Warp", | ||
| 147 | -variable => \$timewarp, | ||
| 148 | -command => sub { | ||
| 149 | ($timewarp) | ||
| 150 | ? ( $timescale->configure( -state => 'normal' ) ) | ||
| 151 | : ( $timescale->configure( -state => 'disabled' ) ); | ||
| 152 | } | ||
| 153 | )->grid( -row => 3, -column => 3, -sticky => 'w' ); | ||
| 154 | $timewarp = 1; | ||
| 155 | $movetime = 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub cursor_toggleit { | ||
| 159 | $cursor ->${trans}( $wid{canvas} ); | ||
| 160 | } | ||
| 161 | |||
| 162 | sub cursor_doit { | ||
| 163 | if ( $place eq 'Screen' ) { | ||
| 164 | if ($timewarp) { | ||
| 165 | $temptime = $movetime * 1000; | ||
| 166 | $cursor->moveto( | ||
| 167 | $TOP->screenwidth * $dir->{$direction}[0], | ||
| 168 | $TOP->screenheight * $dir->{$direction}[1], | ||
| 169 | -time => $temptime | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | else { | ||
| 173 | $cursor->warpto( | ||
| 174 | $TOP->screenwidth * $dir->{$direction}[0], | ||
| 175 | $TOP->screenheight * $dir->{$direction}[1] | ||
| 176 | ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | elsif ( $place eq 'Canvas' ) { | ||
| 180 | if ($timewarp) { | ||
| 181 | $temptime = $movetime * 1000; | ||
| 182 | $cursor->moveto( | ||
| 183 | $wid{canvas}, | ||
| 184 | $wid{canvas}->width * $dir->{$direction}[0], | ||
| 185 | $wid{canvas}->height * $dir->{$direction}[1], | ||
| 186 | -time => $temptime | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | $cursor->warpto( | ||
| 191 | $wid{canvas}, | ||
| 192 | $wid{canvas}->width * $dir->{$direction}[0], | ||
| 193 | $wid{canvas}->height * $dir->{$direction}[1] | ||
| 194 | ); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | elsif ( $place eq 'Rectangle Item' ) { | ||
| 198 | @coords = $wid{canvas}->coords('RECT'); | ||
| 199 | if ($timewarp) { | ||
| 200 | $temptime = $movetime * 1000; | ||
| 201 | $cursor->moveto( | ||
| 202 | $wid{canvas}, | ||
| 203 | ( $coords[2] - $coords[0] ) * $dir->{$direction}[0] + $coords[0], | ||
| 204 | ( $coords[3] - $coords[1] ) * $dir->{$direction}[1] + $coords[1], | ||
| 205 | -time => $temptime | ||
| 206 | ); | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | $cursor->warpto( | ||
| 210 | $wid{canvas}, | ||
| 211 | ( $coords[2] - $coords[0] ) * $dir->{$direction}[0] + $coords[0], | ||
| 212 | ( $coords[3] - $coords[1] ) * $dir->{$direction}[1] + $coords[1], | ||
| 213 | ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | sub cursor_items_start_drag { | ||
| 219 | |||
| 220 | ( $x, $y ) = @_; | ||
| 221 | |||
| 222 | $iinfo->{lastX} = $wid{canvas}->canvasx($x); | ||
| 223 | $iinfo->{lastY} = $wid{canvas}->canvasy($y); | ||
| 224 | |||
| 225 | } | ||
| 226 | |||
| 227 | sub cursor_items_drag { | ||
| 228 | |||
| 229 | ( $x, $y ) = @_; | ||
| 230 | |||
| 231 | $x = $wid{canvas}->canvasx($x); | ||
| 232 | $y = $wid{canvas}->canvasy($y); | ||
| 233 | $wid{canvas}->move( 'current', $x - $iinfo->{lastX}, $y - $iinfo->{lastY} ); | ||
| 234 | $iinfo->{lastX} = $x; | ||
| 235 | $iinfo->{lastY} = $y; | ||
| 236 | |||
| 237 | } | ||
| 238 | |||
| 239 | return 1 if caller(); | ||
| 240 | |||
| 241 | require WidgetDemo; | ||
| 242 | |||
| 243 | $MW = new MainWindow; | ||
| 244 | $MW->geometry("+0+0"); | ||
| 245 | $MW->Button( | ||
| 246 | -text => 'Close', | ||
| 247 | -command => sub { $MW->destroy } | ||
| 248 | )->pack; | ||
| 249 | cursor('cursor'); | ||
| 250 | MainLoop; |