| 1 |
package Tk::Button; |
| 2 |
# Conversion from Tk4.0 button.tcl competed. |
| 3 |
# |
| 4 |
# Copyright (c) 1992-1994 The Regents of the University of California. |
| 5 |
# Copyright (c) 1994 Sun Microsystems, Inc. |
| 6 |
# Copyright (c) 1995-2003 Nick Ing-Simmons. All rights reserved. |
| 7 |
# This program is free software; you can redistribute it and/or |
| 8 |
|
| 9 |
use vars qw($VERSION); |
| 10 |
$VERSION = '4.010'; # $Id: //depot/Tkutf8/Tk/Button.pm#8 $ |
| 11 |
|
| 12 |
# modify it under the same terms as Perl itself, subject |
| 13 |
# to additional disclaimer in license.terms due to partial |
| 14 |
# derivation from Tk4.0 sources. |
| 15 |
|
| 16 |
use strict; |
| 17 |
|
| 18 |
require Tk::Widget; |
| 19 |
use base qw(Tk::Widget); |
| 20 |
|
| 21 |
use vars qw($buttonWindow $afterId $repeated); |
| 22 |
|
| 23 |
Tk::Methods('deselect','flash','invoke','select','toggle'); |
| 24 |
|
| 25 |
sub Tk_cmd { \&Tk::button } |
| 26 |
|
| 27 |
Construct Tk::Widget 'Button'; |
| 28 |
|
| 29 |
sub ClassInit |
| 30 |
{ |
| 31 |
my ($class,$mw) = @_; |
| 32 |
$mw->bind($class,'<Enter>', 'Enter'); |
| 33 |
$mw->bind($class,'<Leave>', 'Leave'); |
| 34 |
$mw->bind($class,'<1>', 'butDown'); |
| 35 |
$mw->bind($class,'<ButtonRelease-1>', 'butUp'); |
| 36 |
$mw->bind($class,'<space>', 'Invoke'); |
| 37 |
$mw->bind($class,'<Return>', 'Invoke'); |
| 38 |
return $class; |
| 39 |
} |
| 40 |
|
| 41 |
# tkButtonEnter -- |
| 42 |
# The procedure below is invoked when the mouse pointer enters a |
| 43 |
# button widget. It records the button we're in and changes the |
| 44 |
# state of the button to active unless the button is disabled. |
| 45 |
# |
| 46 |
# Arguments: |
| 47 |
# w - The name of the widget. |
| 48 |
|
| 49 |
sub Enter |
| 50 |
{ |
| 51 |
my $w = shift; |
| 52 |
my $E = shift; |
| 53 |
if ($w->cget('-state') ne 'disabled') |
| 54 |
{ |
| 55 |
# On unix the state is active just with mouse-over |
| 56 |
$w->configure(-state => 'active'); |
| 57 |
|
| 58 |
# If the mouse button is down, set the relief to sunken on entry. |
| 59 |
# Overwise, if there's an -overrelief value, set the relief to that. |
| 60 |
$w->{__relief__} = $w->cget('-relief'); |
| 61 |
if (defined $buttonWindow && $w == $buttonWindow) |
| 62 |
{ |
| 63 |
$w->configure(-relief => 'sunken'); |
| 64 |
$w->{__prelief__} = 'sunken'; |
| 65 |
} |
| 66 |
elsif ((my $over = $w->cget('-overrelief')) ne '') |
| 67 |
{ |
| 68 |
$w->configure(-relief => $over); |
| 69 |
$w->{__prelief__} = $over; |
| 70 |
} |
| 71 |
} |
| 72 |
$Tk::window = $w; |
| 73 |
} |
| 74 |
|
| 75 |
# tkButtonLeave -- |
| 76 |
# The procedure below is invoked when the mouse pointer leaves a |
| 77 |
# button widget. It changes the state of the button back to |
| 78 |
# inactive. If we're leaving the button window with a mouse button |
| 79 |
# pressed (tkPriv(buttonWindow) == $w), restore the relief of the |
| 80 |
# button too. |
| 81 |
# |
| 82 |
# Arguments: |
| 83 |
# w - The name of the widget. |
| 84 |
sub Leave |
| 85 |
{ |
| 86 |
my $w = shift; |
| 87 |
$w->configure('-state'=>'normal') if ($w->cget('-state') ne 'disabled'); |
| 88 |
# Restore the original button relief if it was changed by Tk. |
| 89 |
# That is signaled by the existence of Priv($w,prelief). |
| 90 |
if (exists $w->{__relief__}) |
| 91 |
{ |
| 92 |
if (exists $w->{__prelief__} && |
| 93 |
$w->{__prelief__} eq $w->cget('-relief')) |
| 94 |
{ |
| 95 |
$w->configure(-relief => $w->{__relief__}); |
| 96 |
} |
| 97 |
delete $w->{__relief__}; |
| 98 |
delete $w->{__prelief__}; |
| 99 |
} |
| 100 |
undef $Tk::window; |
| 101 |
} |
| 102 |
|
| 103 |
# tkButtonDown -- |
| 104 |
# The procedure below is invoked when the mouse button is pressed in |
| 105 |
# a button widget. It records the fact that the mouse is in the button, |
| 106 |
# saves the button's relief so it can be restored later, and changes |
| 107 |
# the relief to sunken. |
| 108 |
# |
| 109 |
# Arguments: |
| 110 |
# w - The name of the widget. |
| 111 |
sub butDown |
| 112 |
{ |
| 113 |
my $w = shift; |
| 114 |
|
| 115 |
# Only save the button's relief if it does not yet exist. If there |
| 116 |
# is an overrelief setting, Priv($w,relief) will already have been set, |
| 117 |
# and the current value of the -relief option will be incorrect. |
| 118 |
|
| 119 |
if (!exists $w->{__relief__}) |
| 120 |
{ |
| 121 |
$w->{__relief__} = $w->cget('-relief'); |
| 122 |
} |
| 123 |
|
| 124 |
if ($w->cget('-state') ne 'disabled') |
| 125 |
{ |
| 126 |
$buttonWindow = $w; |
| 127 |
$w->configure('-relief' => 'sunken', '-state' => 'active'); |
| 128 |
$w->{__prelief__} = 'sunken'; |
| 129 |
|
| 130 |
# If this button has a repeatdelay set up, get it going with an after |
| 131 |
$w->afterCancel($afterId); |
| 132 |
my $delay = $w->cget('-repeatdelay'); |
| 133 |
$repeated = 0; |
| 134 |
if ($delay > 0) |
| 135 |
{ |
| 136 |
$afterId = $w->after($delay, [$w, 'AutoInvoke']); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
# tkButtonUp -- |
| 142 |
# The procedure below is invoked when the mouse button is released |
| 143 |
# in a button widget. It restores the button's relief and invokes |
| 144 |
# the command as long as the mouse hasn't left the button. |
| 145 |
# |
| 146 |
# Arguments: |
| 147 |
# w - The name of the widget. |
| 148 |
sub butUp |
| 149 |
{ |
| 150 |
my $w = shift; |
| 151 |
if (defined($buttonWindow) && $buttonWindow == $w) |
| 152 |
{ |
| 153 |
undef $buttonWindow; |
| 154 |
|
| 155 |
# Restore the button's relief if it was cached. |
| 156 |
if (exists $w->{__relief__}) |
| 157 |
{ |
| 158 |
if (exists $w->{__prelief__} && |
| 159 |
$w->{__prelief__} eq $w->cget('-relief')) |
| 160 |
{ |
| 161 |
$w->configure(-relief => $w->{__relief__}); |
| 162 |
} |
| 163 |
delete $w->{__relief__}; |
| 164 |
delete $w->{__prelief__}; |
| 165 |
} |
| 166 |
|
| 167 |
# Clean up the after event from the auto-repeater |
| 168 |
$w->afterCancel($afterId); |
| 169 |
|
| 170 |
if ($w->IS($Tk::window) && $w->cget('-state') ne 'disabled') |
| 171 |
{ |
| 172 |
$w->configure(-state => 'normal'); |
| 173 |
# Only invoke the command if it wasn't already invoked by the |
| 174 |
# auto-repeater functionality |
| 175 |
if ($repeated == 0) |
| 176 |
{ |
| 177 |
$w->invoke; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
# tkButtonInvoke -- |
| 184 |
# The procedure below is called when a button is invoked through |
| 185 |
# the keyboard. It simulate a press of the button via the mouse. |
| 186 |
# |
| 187 |
# Arguments: |
| 188 |
# w - The name of the widget. |
| 189 |
sub Invoke |
| 190 |
{ |
| 191 |
my $w = shift; |
| 192 |
if ($w->cget('-state') ne 'disabled') |
| 193 |
{ |
| 194 |
my $oldRelief = $w->cget('-relief'); |
| 195 |
my $oldState = $w->cget('-state'); |
| 196 |
$w->configure('-state' => 'active', '-relief' => 'sunken'); |
| 197 |
$w->idletasks; |
| 198 |
$w->after(100); |
| 199 |
$w->configure('-state' => $oldState, '-relief' => $oldRelief); |
| 200 |
$w->invoke; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
# ::tk::ButtonAutoInvoke -- |
| 205 |
# |
| 206 |
# Invoke an auto-repeating button, and set it up to continue to repeat. |
| 207 |
# |
| 208 |
# Arguments: |
| 209 |
# w button to invoke. |
| 210 |
# |
| 211 |
# Results: |
| 212 |
# None. |
| 213 |
# |
| 214 |
# Side effects: |
| 215 |
# May create an after event to call ::tk::ButtonAutoInvoke. |
| 216 |
sub AutoInvoke |
| 217 |
{ |
| 218 |
my $w = shift; |
| 219 |
$w->afterCancel($afterId); |
| 220 |
my $delay = $w->cget('-repeatinterval'); |
| 221 |
if ($w->IS($Tk::window)) |
| 222 |
{ |
| 223 |
$repeated++; |
| 224 |
$w->invoke; |
| 225 |
} |
| 226 |
if ($delay > 0) |
| 227 |
{ |
| 228 |
$afterId = $w->after($delay, [$w, 'AutoInvoke']); |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
# Used for Tk::Widget::AmpWidget |
| 233 |
sub AmpWidgetPostHook |
| 234 |
{ |
| 235 |
my $w = shift; |
| 236 |
$w->bind('<<AltUnderlined>>' => ['invoke']); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
1; |
| 241 |
|
| 242 |
__END__ |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|