| 1 | package Tkx::ImageButton; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = '0.14'; | ||
| 7 | |||
| 8 | use Tkx; | ||
| 9 | use base qw(Tkx::widget Tkx::MegaConfig); | ||
| 10 | |||
| 11 | __PACKAGE__->_Mega('tkx_ImageButton'); | ||
| 12 | __PACKAGE__->_Config( | ||
| 13 | -imagedisplay => ['METHOD'], | ||
| 14 | -imageover => ['METHOD'], | ||
| 15 | -imageclick => ['METHOD'], | ||
| 16 | -imagedisabled => ['METHOD'], | ||
| 17 | -blink => ['METHOD'], | ||
| 18 | -blinkdelay => ['METHOD'], | ||
| 19 | -state => ['METHOD'], | ||
| 20 | -command => ['METHOD'], | ||
| 21 | ); | ||
| 22 | |||
| 23 | # Locale variables | ||
| 24 | my $tile; | ||
| 25 | my $initialized; | ||
| 26 | |||
| 27 | #---------------------------------------------------------------------------- | ||
| 28 | # Method : _Populate | ||
| 29 | # Purpose : Create a new ImageButton | ||
| 30 | # Notes : | ||
| 31 | #---------------------------------------------------------------------------- | ||
| 32 | sub _Populate { | ||
| 33 | my ($class, $widget, $path, %opt) = @_; | ||
| 34 | my ($data); | ||
| 35 | |||
| 36 | if (!$initialized) { | ||
| 37 | $tile = eval { Tkx::package_require('tile') }; | ||
| 38 | $initialized = 1; | ||
| 39 | } | ||
| 40 | |||
| 41 | my $self = $tile | ||
| 42 | ? $class->new($path)->_parent->new_ttk__label(-name => $path, -class => 'tkx_ImageButton') | ||
| 43 | : $class->new($path)->_parent->new_label(-name => $path); | ||
| 44 | |||
| 45 | $self->_class($class); | ||
| 46 | |||
| 47 | # Data | ||
| 48 | $data = $self->_data(); | ||
| 49 | $data->{-imagedisplay} = delete $opt{-imagedisplay}; | ||
| 50 | $data->{-imageover} = delete $opt{-imageover}; | ||
| 51 | $data->{-imageclick} = delete $opt{-imageclick}; | ||
| 52 | $data->{-imagedisabled} = delete $opt{-imagedisabled}; | ||
| 53 | $data->{-state} = delete $opt{-state}; | ||
| 54 | |||
| 55 | $data->{-blink} = delete $opt{-blink}; | ||
| 56 | |||
| 57 | $data->{-blinkdelay} = delete $opt{-blinkdelay} || 300; | ||
| 58 | $data->{-command} = delete $opt{-command}; | ||
| 59 | |||
| 60 | $data->{-blinkphase} = 0; | ||
| 61 | $data->{-state} = 0; | ||
| 62 | |||
| 63 | $data->{-over} = 0; | ||
| 64 | $data->{-toogled} = 0; | ||
| 65 | |||
| 66 | # Set image and blink if needed | ||
| 67 | $self->_IBLeave(); | ||
| 68 | $self->_IBBlink(); | ||
| 69 | |||
| 70 | # Bindings | ||
| 71 | Tkx::bind($self => '<Enter>' => [\&_IBEnter, $self]); | ||
| 72 | Tkx::bind($self => '<Leave>' => [\&_IBLeave, $self]); | ||
| 73 | Tkx::bind($self => '<ButtonPress-1>' => [\&_IBPress, $self]); | ||
| 74 | Tkx::bind($self => '<ButtonRelease-1>' => [\&_IBRelease, $self]); | ||
| 75 | |||
| 76 | # Widget | ||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | #---------------------------------------------------------------------------- | ||
| 82 | # Method : invoke | ||
| 83 | # Purpose : | ||
| 84 | # Notes : | ||
| 85 | #---------------------------------------------------------------------------- | ||
| 86 | sub invoke { | ||
| 87 | my ($self) = @_; | ||
| 88 | |||
| 89 | if (defined(my $cb = $self->_data->{-command})) { | ||
| 90 | Tkx::i::call($cb); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | #---------------------------------------------------------------------------- | ||
| 96 | # Method : _IBEnter | ||
| 97 | # Purpose : | ||
| 98 | # Notes : | ||
| 99 | #---------------------------------------------------------------------------- | ||
| 100 | sub _IBEnter { | ||
| 101 | my ($self) = @_; | ||
| 102 | |||
| 103 | if ($self->_data->{-state} ne 'disabled') { | ||
| 104 | if (defined(my $imageover = $self->_data->{-imageover})) { | ||
| 105 | $self->configure( -image => $imageover ); | ||
| 106 | } | ||
| 107 | |||
| 108 | $self->_data->{-over} = 1; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | #---------------------------------------------------------------------------- | ||
| 114 | # Method : _IBLeave | ||
| 115 | # Purpose : | ||
| 116 | # Notes : | ||
| 117 | #---------------------------------------------------------------------------- | ||
| 118 | sub _IBLeave { | ||
| 119 | my ($self) = @_; | ||
| 120 | |||
| 121 | if ($self->_data->{-state} ne 'disabled') { | ||
| 122 | if (defined(my $imagedisplay = $self->_data->{-imagedisplay})) { | ||
| 123 | $self->configure( -image => $imagedisplay ); | ||
| 124 | } | ||
| 125 | |||
| 126 | $self->_data->{-over} = 0; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | #---------------------------------------------------------------------------- | ||
| 132 | # Method : _IBPress | ||
| 133 | # Purpose : | ||
| 134 | # Notes : | ||
| 135 | #---------------------------------------------------------------------------- | ||
| 136 | sub _IBPress { | ||
| 137 | my ($self) = @_; | ||
| 138 | |||
| 139 | if ($self->_data->{-state} ne 'disabled') { | ||
| 140 | if (defined(my $imageclick = $self->_data->{-imageclick})) { | ||
| 141 | $self->configure( -image => $imageclick ); | ||
| 142 | } | ||
| 143 | |||
| 144 | $self->_data->{-toogled} = 1; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | |||
| 149 | #---------------------------------------------------------------------------- | ||
| 150 | # Method : _IBRelease | ||
| 151 | # Purpose : | ||
| 152 | # Notes : | ||
| 153 | #---------------------------------------------------------------------------- | ||
| 154 | sub _IBRelease { | ||
| 155 | my ($self) = @_; | ||
| 156 | |||
| 157 | if ($self->_data->{-state} ne 'disabled') { | ||
| 158 | my $imover = $self->_data->{-imageover}; | ||
| 159 | my $imdisplay = $self->_data->{-imagedisplay}; | ||
| 160 | |||
| 161 | if (defined $imover) { $self->configure(-image => $imover) } | ||
| 162 | elsif (defined $imdisplay) { $self->configure(-image => $imdisplay) } | ||
| 163 | |||
| 164 | if ($self->_data->{-over} && $self->_data->{-toogled}) { | ||
| 165 | $self->invoke(); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | $self->_data->{-toogled} = 0; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | #---------------------------------------------------------------------------- | ||
| 174 | # Method : _IBBlink | ||
| 175 | # Purpose : | ||
| 176 | # Notes : | ||
| 177 | #---------------------------------------------------------------------------- | ||
| 178 | sub _IBBlink { | ||
| 179 | my ($self) = @_; | ||
| 180 | |||
| 181 | if ($self->_data->{-blink}) { | ||
| 182 | if ($self->_data->{-blinkphase}) { | ||
| 183 | $self->_IBLeave(); | ||
| 184 | $self->_data->{-blinkphase} = 0; | ||
| 185 | } | ||
| 186 | else { | ||
| 187 | $self->_IBEnter(); | ||
| 188 | $self->_data->{-blinkphase} = 1; | ||
| 189 | } | ||
| 190 | |||
| 191 | Tkx::after($self->_data->{-blinkdelay}, [\&_IBBlink, $self]); | ||
| 192 | } | ||
| 193 | else { | ||
| 194 | $self->_IBLeave(); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | #---------------------------------------------------------------------------- | ||
| 200 | # Method : _config_command | ||
| 201 | # Purpose : Handler for configure(-command => <value>) | ||
| 202 | # Notes : | ||
| 203 | #---------------------------------------------------------------------------- | ||
| 204 | sub _config_command { | ||
| 205 | my ($self, $command) = @_; | ||
| 206 | |||
| 207 | if ($#_ > 0) { | ||
| 208 | $self->_data->{-command} = $command; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | return $self->_data->{-command}; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | #---------------------------------------------------------------------------- | ||
| 216 | # Method : _config_state | ||
| 217 | # Purpose : Handler for configure(-state => <value>) | ||
| 218 | # Notes : | ||
| 219 | #---------------------------------------------------------------------------- | ||
| 220 | sub _config_state { | ||
| 221 | my ($self, $state) = @_; | ||
| 222 | |||
| 223 | if ($#_ > 0) { | ||
| 224 | $self->_data->{-state} = $state; | ||
| 225 | |||
| 226 | if ($state eq 'disabled') { | ||
| 227 | if (defined(my $imagedisabled = $self->_data->{-imagedisabled})) { | ||
| 228 | $self->configure( -image => $imagedisabled ); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | if (defined(my $imagedisplay = $self->_data->{-imagedisplay})) { | ||
| 233 | $self->configure( -image => $imagedisplay ); | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | return $self->_data->{-state}; | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | |||
| 243 | #---------------------------------------------------------------------------- | ||
| 244 | # Method : _config_imagedisplay | ||
| 245 | # Purpose : Handler for configure(-imagedisplay => <value>) | ||
| 246 | # Notes : | ||
| 247 | #---------------------------------------------------------------------------- | ||
| 248 | sub _config_imagedisplay { | ||
| 249 | my ($self, $imagedisplay) = @_; | ||
| 250 | |||
| 251 | if ($#_ > 0) { | ||
| 252 | $self->_data->{-imagedisplay} = $imagedisplay; | ||
| 253 | |||
| 254 | $self->_IBLeave() unless $self->_data->{-over}; | ||
| 255 | } | ||
| 256 | else { | ||
| 257 | return $self->_data->{-imagedisplay}; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | |||
| 262 | #---------------------------------------------------------------------------- | ||
| 263 | # Method : _config_imageover | ||
| 264 | # Purpose : Handler for configure(-imageover => <value>) | ||
| 265 | # Notes : | ||
| 266 | #---------------------------------------------------------------------------- | ||
| 267 | sub _config_imageover { | ||
| 268 | my ($self, $imageover) = @_; | ||
| 269 | |||
| 270 | if ($#_ > 0) { | ||
| 271 | $self->_data->{-imageover} = $imageover; | ||
| 272 | |||
| 273 | $self->_IBEnter() if $self->_data->{-over}; | ||
| 274 | } | ||
| 275 | else { | ||
| 276 | return $self->_data->{-imageover}; | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | #---------------------------------------------------------------------------- | ||
| 282 | # Method : _config_imageclick | ||
| 283 | # Purpose : Handler for configure(-imageclick => <value>) | ||
| 284 | # Notes : | ||
| 285 | #---------------------------------------------------------------------------- | ||
| 286 | sub _config_imageclick { | ||
| 287 | my ($self, $imageclick) = @_; | ||
| 288 | |||
| 289 | if ($#_ > 0) { | ||
| 290 | $self->_data->{-imageclick} = $imageclick; | ||
| 291 | } | ||
| 292 | else { | ||
| 293 | return $self->_data->{-imageclick}; | ||
| 294 | } | ||
| 295 | } | ||
| 296 | |||
| 297 | |||
| 298 | #---------------------------------------------------------------------------- | ||
| 299 | # Method : _config_imagedisabled | ||
| 300 | # Purpose : Handler for configure(-imagedisabled => <value>) | ||
| 301 | # Notes : | ||
| 302 | #---------------------------------------------------------------------------- | ||
| 303 | sub _config_imagedisabled { | ||
| 304 | my ($self, $imagedisabled) = @_; | ||
| 305 | |||
| 306 | if ($#_ > 0) { | ||
| 307 | $self->_data->{-imagedisabled} = $imagedisabled; | ||
| 308 | } | ||
| 309 | else { | ||
| 310 | return $self->_data->{-imagedisabled}; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | #---------------------------------------------------------------------------- | ||
| 316 | # Method : _config_blink | ||
| 317 | # Purpose : Handler for configure(-blink => <value>) | ||
| 318 | # Notes : | ||
| 319 | #---------------------------------------------------------------------------- | ||
| 320 | sub _config_blink { | ||
| 321 | my ($self, $blink) = @_; | ||
| 322 | |||
| 323 | if ($#_ > 0) { | ||
| 324 | my $blinking = $self->_data->{-blink}; | ||
| 325 | $self->_data->{-blink} = $blink; | ||
| 326 | |||
| 327 | $self->_IBBlink() unless $blinking; | ||
| 328 | } | ||
| 329 | else { | ||
| 330 | return $self->_data->{-blink}; | ||
| 331 | } | ||
| 332 | } | ||
| 333 | |||
| 334 | #---------------------------------------------------------------------------- | ||
| 335 | # Method : _config_blinkdelay | ||
| 336 | # Purpose : Handler for configure(-blinkdelay => <value>) | ||
| 337 | # Notes : | ||
| 338 | #---------------------------------------------------------------------------- | ||
| 339 | sub _config_blinkdelay { | ||
| 340 | my ($self, $delay) = @_; | ||
| 341 | |||
| 342 | if ($#_ > 0) { | ||
| 343 | $self->_data->{-blinkdelay} = $delay; | ||
| 344 | } | ||
| 345 | else { | ||
| 346 | return $self->_data->{-blinkdelay}; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | |||
| 351 | |||
| 352 | |||
| 353 | 1; | ||
| 354 | __END__ |