| 1 | package Tk::StyledButton; | ||
| 2 | |||
| 3 | require 5.008; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use Tk; | ||
| 8 | use Tk::Balloon; | ||
| 9 | use Tk::Canvas; | ||
| 10 | use Tk::Font; | ||
| 11 | use Tk::Trace; | ||
| 12 | use Tk::PNG; | ||
| 13 | use Tk::JPEG; | ||
| 14 | use Tk::Photo; | ||
| 15 | use MIME::Base64; | ||
| 16 | use Carp; | ||
| 17 | |||
| 18 | use base qw(Tk::Derived Tk::Canvas); | ||
| 19 | |||
| 20 | use constant PI => 3.1415926; | ||
| 21 | use constant PI_OVER_2 => 1.5707963; | ||
| 22 | use constant SQUARE_EDGE_FACTOR => 1.5; | ||
| 23 | |||
| 24 | our $VERSION = '0.10'; | ||
| 25 | |||
| 26 | our $hasgd; # has GD & GD::Text | ||
| 27 | our $hasw32n2f; # has Win32::Font::NameToFile | ||
| 28 | |||
| 29 | our $SIN_PI_OVER_4 = sin(0.78539815); | ||
| 30 | |||
| 31 | our %valid_compound = qw( | ||
| 32 | center 1 | ||
| 33 | left 1 | ||
| 34 | right 1 | ||
| 35 | top 1 | ||
| 36 | bottom 1 | ||
| 37 | none 1 | ||
| 38 | ); | ||
| 39 | |||
| 40 | our %valid_anchor = qw( | ||
| 41 | n 1 | ||
| 42 | s 1 | ||
| 43 | e 1 | ||
| 44 | w 1 | ||
| 45 | center 1 | ||
| 46 | ne 1 | ||
| 47 | nw 1 | ||
| 48 | se 1 | ||
| 49 | sw 1 | ||
| 50 | ); | ||
| 51 | |||
| 52 | our %valid_orient = qw( | ||
| 53 | n ne | ||
| 54 | s se | ||
| 55 | e en | ||
| 56 | w wn | ||
| 57 | ne ne | ||
| 58 | nw nw | ||
| 59 | se se | ||
| 60 | sw sw | ||
| 61 | en en | ||
| 62 | es es | ||
| 63 | wn wn | ||
| 64 | ws ws | ||
| 65 | ); | ||
| 66 | |||
| 67 | our %valid_shapes = qw( | ||
| 68 | rectangle 1 | ||
| 69 | oval 1 | ||
| 70 | round 1 | ||
| 71 | bevel 1 | ||
| 72 | folio 1 | ||
| 73 | ); | ||
| 74 | |||
| 75 | our %valid_styles = qw( | ||
| 76 | flat 1 | ||
| 77 | round 1 | ||
| 78 | shiny 1 | ||
| 79 | gel 1 | ||
| 80 | image 1 | ||
| 81 | ); | ||
| 82 | |||
| 83 | our %notkeymouse_event = qw( | ||
| 84 | Activate 1 | ||
| 85 | Circulate 1 | ||
| 86 | CirculateRequest 1 | ||
| 87 | Colormap 1 | ||
| 88 | Configure 1 | ||
| 89 | ConfigureRequest 1 | ||
| 90 | Create 1 | ||
| 91 | Deactivate 1 | ||
| 92 | Destroy 1 | ||
| 93 | Expose 1 | ||
| 94 | FocusIn 1 | ||
| 95 | FocusOut 1 | ||
| 96 | Gravity 1 | ||
| 97 | Map 1 | ||
| 98 | MapRequest 1 | ||
| 99 | Property 1 | ||
| 100 | Reparent 1 | ||
| 101 | ResizeRequest 1 | ||
| 102 | Unmap 1 | ||
| 103 | Visibility 1 | ||
| 104 | ); | ||
| 105 | |||
| 106 | our $balloon; # set only if we get a -tooltip | ||
| 107 | |||
| 108 | # | ||
| 109 | # needed to compute a polygon to fit a circle | ||
| 110 | # for image-rendered buttons, since createOval | ||
| 111 | # doesn't work with transparent stipple on | ||
| 112 | # Win32 | ||
| 113 | # | ||
| 114 | our @polyfactors = ( | ||
| 115 | 1, 0, | ||
| 116 | 0.866025408250255, 0.5, | ||
| 117 | 0.5, 0.866025394852806, | ||
| 118 | 0, 1, | ||
| 119 | -0.5, 0.866025421647702, | ||
| 120 | -0.866025381455357, 0.5, | ||
| 121 | -1, 0, | ||
| 122 | -0.86602543504515, -0.5, | ||
| 123 | -0.5, -0.866025368057908, | ||
| 124 | 0, -1, | ||
| 125 | 0.5, -0.866025448442596, | ||
| 126 | 0.866025354660458, -0.5 | ||
| 127 | ); | ||
| 128 | |||
| 129 | BEGIN { | ||
| 130 | eval { | ||
| 131 | require GD; | ||
| 132 | require GD::Text::Wrap; | ||
| 133 | }; | ||
| 134 | |||
| 135 | $hasgd = 1 unless $@; | ||
| 136 | |||
| 137 | if ($hasgd && ($^O eq 'MSWin32')) { | ||
| 138 | eval { | ||
| 139 | require Win32::Font::NameToFile; | ||
| 140 | }; | ||
| 141 | $hasw32n2f = 1 unless $@; | ||
| 142 | } | ||
| 143 | if ($hasw32n2f) { | ||
| 144 | use Win32::Font::NameToFile qw(get_ttf_abs_path); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | Construct Tk::Widget 'StyledButton'; | ||
| 149 | |||
| 150 | sub ClassInit { | ||
| 151 | my ($class,$mw) = @_; | ||
| 152 | |||
| 153 | $class->SUPER::ClassInit($mw); | ||
| 154 | # | ||
| 155 | # in order to defer redraws, we queue up requests | ||
| 156 | # and wait until we're idle | ||
| 157 | # | ||
| 158 | $mw->bind($class,'<Configure>', ['_layoutRequest',1]); | ||
| 159 | } | ||
| 160 | |||
| 161 | sub Populate | ||
| 162 | { | ||
| 163 | my ($self, $args) = @_; | ||
| 164 | |||
| 165 | $self->SUPER::Populate($args); | ||
| 166 | # | ||
| 167 | # we use methods for everything, so we immediately redraw | ||
| 168 | # when an option changes | ||
| 169 | # | ||
| 170 | $self->ConfigSpecs( | ||
| 171 | -activeimage => [ 'METHOD' ], | ||
| 172 | -anchor => [qw/METHOD anchor Anchor/, 'center'], | ||
| 173 | -angle => [qw/METHOD angle Angle/, 0.1], | ||
| 174 | -background => [qw/METHOD background Background/], | ||
| 175 | -bitmap => [qw/METHOD bitmap Bitmap/], | ||
| 176 | -command => [qw/CALLBACK command Command/, sub { return 1; }], | ||
| 177 | -compound => [qw/METHOD compound Compound/, 'center'], | ||
| 178 | -dispersion => [qw/METHOD dispersion Dispersion/, 0.8], | ||
| 179 | -font => [qw/METHOD font Font/], | ||
| 180 | -foreground => [qw/METHOD foreground Foreground/, 'black'], | ||
| 181 | -height => [qw/METHOD height Height/], | ||
| 182 | -idleimage => [ 'METHOD' ], | ||
| 183 | -image => [qw/METHOD image Image/], | ||
| 184 | -justify => [qw/METHOD justify Justify/, 'center'], | ||
| 185 | -orient => [qw/METHOD orient Orient/, 'ne'], | ||
| 186 | -padx => [qw/METHOD padx Padx/, 4], | ||
| 187 | -pady => [qw/METHOD pady Pady/, 4], | ||
| 188 | -shape => [qw/METHOD shape Shape/, 'rectangle'], | ||
| 189 | -style => [qw/METHOD style Style/, 'shiny'], | ||
| 190 | -state => [qw/METHOD state State/, 'normal'], | ||
| 191 | -text => [qw/METHOD text Text/], | ||
| 192 | -textvariable => [qw/METHOD textvariable Textvariable/], | ||
| 193 | -tooltip => [qw/METHOD tooltip ToolTip/], | ||
| 194 | -underline => [qw/METHOD underline Underline/], | ||
| 195 | -usegd => [qw/METHOD usegd UseGD/], | ||
| 196 | -verticaltext => [qw/METHOD verticaltext VerticalText/], | ||
| 197 | -width => [qw/METHOD width Width/], | ||
| 198 | -wraplength => [qw/METHOD wraplength Wraplength/, 0], | ||
| 199 | ); | ||
| 200 | # | ||
| 201 | # force a default font | ||
| 202 | # | ||
| 203 | my $font_name = $self->optionGet('font', '*'); | ||
| 204 | my $font; | ||
| 205 | if (!defined $font_name) { | ||
| 206 | my $l = $self->Label; | ||
| 207 | $font = $self->fontCreate($self->fontActual($l->cget('-font'))); | ||
| 208 | $l->destroy; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $font = $self->fontCreate($self->fontActual($font_name)); | ||
| 212 | } | ||
| 213 | $self->{_font} = $font; | ||
| 214 | # | ||
| 215 | # force our background to be transparent | ||
| 216 | # | ||
| 217 | $self->SUPER::configure(-background => ''); | ||
| 218 | $self->_layoutRequest(1); | ||
| 219 | } | ||
| 220 | |||
| 221 | sub textvariable { | ||
| 222 | my ($self, $vref) = @_; | ||
| 223 | |||
| 224 | return $self->{_textvariable} | ||
| 225 | unless defined($vref); | ||
| 226 | |||
| 227 | use Tie::Watch; | ||
| 228 | |||
| 229 | my $st = [ sub { | ||
| 230 | my ($watch, $new_val) = @_; | ||
| 231 | my $argv = $watch->Args(-store); | ||
| 232 | $argv->[0]->{_text} = $new_val; | ||
| 233 | $watch->Store($new_val); | ||
| 234 | $argv->[0]->_layoutRequest(2); | ||
| 235 | }, $self ]; | ||
| 236 | |||
| 237 | $self->{_watch} = Tie::Watch->new(-variable => $vref, -store => $st); | ||
| 238 | $self->OnDestroy( [sub { $_[0]->{_watch}->Unwatch; }, $self] ); | ||
| 239 | $self->{_textvariable} = $vref; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub activeimage { | ||
| 243 | my ($self, $arg) = @_; | ||
| 244 | return $self->{_activeimage} unless defined($arg); | ||
| 245 | $self->{_activeimage} = $arg; | ||
| 246 | $self->_layoutRequest(1) | ||
| 247 | if ($self->{_style} eq 'image'); | ||
| 248 | return $arg; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub anchor { | ||
| 252 | my ($self, $arg) = @_; | ||
| 253 | return $self->{_anchor} unless defined($arg); | ||
| 254 | $self->{_anchor} = $arg; | ||
| 255 | $self->_layoutRequest(1); | ||
| 256 | return $arg; | ||
| 257 | } | ||
| 258 | |||
| 259 | sub angle { | ||
| 260 | my ($self, $arg) = @_; | ||
| 261 | return $self->{_angle} unless defined($arg); | ||
| 262 | $self->{_angle} = $arg; | ||
| 263 | $self->_layoutRequest(1); | ||
| 264 | return $arg; | ||
| 265 | } | ||
| 266 | |||
| 267 | sub background { | ||
| 268 | my ($self, $arg) = @_; | ||
| 269 | return $self->{_background} unless defined($arg); | ||
| 270 | $self->{_background} = $arg; | ||
| 271 | $self->_layoutRequest(1); | ||
| 272 | return $arg; | ||
| 273 | } | ||
| 274 | |||
| 275 | sub bitmap { | ||
| 276 | my ($self, $arg) = @_; | ||
| 277 | return $self->{_bitmap} unless defined($arg); | ||
| 278 | $self->{_bitmap} = $arg; | ||
| 279 | $self->_layoutRequest(1); | ||
| 280 | return $arg; | ||
| 281 | } | ||
| 282 | |||
| 283 | sub compound { | ||
| 284 | my ($self, $arg) = @_; | ||
| 285 | return $self->{_compound} unless defined($arg); | ||
| 286 | $self->{_compound} = $arg; | ||
| 287 | $self->_layoutRequest(1); | ||
| 288 | return $arg; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub dispersion { | ||
| 292 | my ($self, $arg) = @_; | ||
| 293 | return $self->{_dispersion} unless defined($arg); | ||
| 294 | $self->{_dispersion} = $arg; | ||
| 295 | $self->_layoutRequest(1); | ||
| 296 | return $arg; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub font { | ||
| 300 | my ($self, $arg) = @_; | ||
| 301 | return $self->{_font} unless defined($arg); | ||
| 302 | $self->{_font} = $arg; | ||
| 303 | $self->_layoutRequest(1); | ||
| 304 | return $arg; | ||
| 305 | } | ||
| 306 | |||
| 307 | sub foreground { | ||
| 308 | my ($self, $arg) = @_; | ||
| 309 | return $self->{_foreground} unless defined($arg); | ||
| 310 | $self->{_foreground} = $arg; | ||
| 311 | $self->_layoutRequest(1); | ||
| 312 | return $arg; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub height { | ||
| 316 | my ($self, $arg) = @_; | ||
| 317 | return $self->SUPER::cget('-height') unless defined($arg); | ||
| 318 | $self->{_height} = $arg; | ||
| 319 | $self->_layoutRequest(1); | ||
| 320 | return $arg; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub idleimage { | ||
| 324 | my ($self, $arg) = @_; | ||
| 325 | return $self->{_idleimage} unless defined($arg); | ||
| 326 | $self->{_idleimage} = $arg; | ||
| 327 | $self->_layoutRequest(1) | ||
| 328 | if ($self->{_style} eq 'image'); | ||
| 329 | return $arg; | ||
| 330 | } | ||
| 331 | |||
| 332 | sub image { | ||
| 333 | my ($self, $arg) = @_; | ||
| 334 | return $self->{_image} unless defined($arg); | ||
| 335 | $self->{_image} = $arg; | ||
| 336 | $self->_layoutRequest(1); | ||
| 337 | return $arg; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub justify { | ||
| 341 | my ($self, $arg) = @_; | ||
| 342 | return $self->{_justify} unless defined($arg); | ||
| 343 | $self->{_justify} = $arg; | ||
| 344 | $self->_layoutRequest(1); | ||
| 345 | return $arg; | ||
| 346 | } | ||
| 347 | |||
| 348 | sub orient { | ||
| 349 | my ($self, $arg) = @_; | ||
| 350 | return $self->{_orient} unless defined($arg); | ||
| 351 | $self->{_orient} = $arg; | ||
| 352 | $self->_layoutRequest(1); | ||
| 353 | return $arg; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub padx { | ||
| 357 | my ($self, $arg) = @_; | ||
| 358 | return $self->{_padx} unless defined($arg); | ||
| 359 | $self->{_padx} = $arg; | ||
| 360 | $self->_layoutRequest(1); | ||
| 361 | return $arg; | ||
| 362 | } | ||
| 363 | |||
| 364 | sub pady { | ||
| 365 | my ($self, $arg) = @_; | ||
| 366 | return $self->{_pady} unless defined($arg); | ||
| 367 | $self->{_pady} = $arg; | ||
| 368 | $self->_layoutRequest(1); | ||
| 369 | return $arg; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub shape { | ||
| 373 | my ($self, $arg) = @_; | ||
| 374 | return $self->{_shape} unless defined($arg); | ||
| 375 | $self->{_shape} = $arg; | ||
| 376 | $self->_layoutRequest(1); | ||
| 377 | return $arg; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub style { | ||
| 381 | my ($self, $arg) = @_; | ||
| 382 | return $self->{_style} unless defined($arg); | ||
| 383 | $self->{_style} = $arg; | ||
| 384 | $self->_layoutRequest(1); | ||
| 385 | return $arg; | ||
| 386 | } | ||
| 387 | |||
| 388 | sub state { | ||
| 389 | my ($self, $arg) = @_; | ||
| 390 | return $self->{_state} unless defined($arg); | ||
| 391 | $self->{_state} = $arg; | ||
| 392 | $self->_layoutRequest(1); | ||
| 393 | return $arg; | ||
| 394 | } | ||
| 395 | |||
| 396 | sub text { | ||
| 397 | my ($self, $arg) = @_; | ||
| 398 | return $self->{_text} unless defined($arg); | ||
| 399 | $self->{_text} = $arg; | ||
| 400 | $self->_layoutRequest(1); | ||
| 401 | return $arg; | ||
| 402 | } | ||
| 403 | |||
| 404 | sub tooltip { | ||
| 405 | my ($self, $arg) = @_; | ||
| 406 | return $self->{_tooltip} unless defined($arg); | ||
| 407 | $self->{_tooltip} = | ||
| 408 | (ref $arg && (ref $arg eq 'ARRAY')) ? $arg : | ||
| 409 | [ $arg, 300 ]; # use 300 msec default delay | ||
| 410 | # | ||
| 411 | # see if we've got a ballon yet | ||
| 412 | # NOTE: we use a package variable for this, in order | ||
| 413 | # to minimize the number of balloons created | ||
| 414 | # | ||
| 415 | $balloon = $self->Balloon(-background => 'white') | ||
| 416 | unless $balloon; | ||
| 417 | |||
| 418 | $self->_layoutRequest(1); | ||
| 419 | return $arg; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub underline { | ||
| 423 | my ($self, $arg) = @_; | ||
| 424 | return $self->{_underline} unless defined($arg); | ||
| 425 | $self->{_underline} = $arg; | ||
| 426 | $self->_layoutRequest(1); | ||
| 427 | return $arg; | ||
| 428 | } | ||
| 429 | |||
| 430 | sub usegd { | ||
| 431 | my ($self, $arg) = @_; | ||
| 432 | return $self->{_usegd} unless defined($arg); | ||
| 433 | return undef if ($arg && (!$hasgd)); | ||
| 434 | $self->{_usegd} = $arg; | ||
| 435 | # $self->_layoutRequest(1); | ||
| 436 | return $arg; | ||
| 437 | } | ||
| 438 | # | ||
| 439 | # indicates whether vertical text in sideways | ||
| 440 | # buttons should be rendered in GD or not; | ||
| 441 | # valid values are 'GD', 'Tk', or undef, default 'Tk' | ||
| 442 | # Note that 'Tk' causes text to be rendered | ||
| 443 | # vertically top to bottom, whereas GD causes | ||
| 444 | # text to be rendered as an image and then rotate 90 degs. | ||
| 445 | # undef causes the text to be laid out in Tk, but in | ||
| 446 | # the usual horizontal rendering. | ||
| 447 | # | ||
| 448 | sub verticaltext { | ||
| 449 | my ($self, $arg) = @_; | ||
| 450 | return $self->{_verticaltext} unless defined($arg); | ||
| 451 | return undef if ($arg && ($arg eq 'GD') && (!$hasgd)); | ||
| 452 | $self->{_verticaltext} = $arg; | ||
| 453 | $self->_layoutRequest(1); | ||
| 454 | return $arg; | ||
| 455 | } | ||
| 456 | |||
| 457 | sub width { | ||
| 458 | my ($self, $arg) = @_; | ||
| 459 | return $self->SUPER::cget('-width') unless defined($arg); | ||
| 460 | $self->{_width} = $arg; | ||
| 461 | $self->_layoutRequest(1); | ||
| 462 | return $arg; | ||
| 463 | } | ||
| 464 | |||
| 465 | sub wraplength { | ||
| 466 | my ($self, $arg) = @_; | ||
| 467 | return $self->{_wraplength} unless defined($arg); | ||
| 468 | $self->{_wraplength} = $arg; | ||
| 469 | $self->_layoutRequest(1); | ||
| 470 | return $arg; | ||
| 471 | } | ||
| 472 | |||
| 473 | ####################################################### | ||
| 474 | # | ||
| 475 | # widget methods | ||
| 476 | # | ||
| 477 | ####################################################### | ||
| 478 | # | ||
| 479 | # return button image as scalar data rendered via GD | ||
| 480 | # (if GD available) | ||
| 481 | # | ||
| 482 | sub capture { | ||
| 483 | $@ = 'GD or GD::Text not available.', | ||
| 484 | return undef | ||
| 485 | unless $hasgd; | ||
| 486 | |||
| 487 | my $self = shift; | ||
| 488 | my %args = @_; | ||
| 489 | $args{-format} = defined($args{-format}) ? 'png' : lc $args{-format}; | ||
| 490 | |||
| 491 | croak 'Invalid -format ' . $args{-format} | ||
| 492 | unless ($args{-format} eq 'png') || | ||
| 493 | ($args{-format} eq 'gif') || | ||
| 494 | ($args{-format} eq 'jpeg'); | ||
| 495 | |||
| 496 | my ($activeimg, $activecoords, $idleimg, $idlecoords) = | ||
| 497 | $self->_renderButton(1, $args{-gdfont}, $args{-omittext}, $args{-omitimage}); | ||
| 498 | |||
| 499 | return undef unless $activeimg && $idleimg; | ||
| 500 | my $method = $args{-format}; | ||
| 501 | return ($activeimg->$method(), $activecoords, $idleimg->$method(), $idlecoords); | ||
| 502 | } | ||
| 503 | # | ||
| 504 | # alternate bright/dark versions at a specified interval; | ||
| 505 | # if interval is not defined, use Button behavior | ||
| 506 | # (3 quick flashes), else flash at interval until | ||
| 507 | # flash(0) | ||
| 508 | # | ||
| 509 | sub flash { | ||
| 510 | my ($self, $intvl) = @_; | ||
| 511 | |||
| 512 | return 1 | ||
| 513 | if ($self->cget('-state') eq 'disabled'); | ||
| 514 | # | ||
| 515 | # reset any existing flash; should make sure | ||
| 516 | # the raised image is the right one!!! | ||
| 517 | # | ||
| 518 | $self->{_flash}[0]->cancel, | ||
| 519 | delete $self->{_flash} | ||
| 520 | if exists($self->{_flash}); | ||
| 521 | # | ||
| 522 | # if no defined interval, then do Button version | ||
| 523 | # | ||
| 524 | $self->{_flash} = [ $self->repeat(100, [ '_flash', $self ]), 3, 0 ] | ||
| 525 | unless defined($intvl); | ||
| 526 | |||
| 527 | return $self | ||
| 528 | unless $intvl; | ||
| 529 | |||
| 530 | $self->{_flash} = [ $self->repeat($intvl, [ '_flash', $self ]), -1, 0 ]; | ||
| 531 | return $self; | ||
| 532 | } | ||
| 533 | # | ||
| 534 | # emulate button press | ||
| 535 | # | ||
| 536 | sub invoke { | ||
| 537 | my $self = shift; | ||
| 538 | |||
| 539 | return 1 | ||
| 540 | if ($self->cget('-state') eq 'disabled'); | ||
| 541 | |||
| 542 | $self->_OnEnter; | ||
| 543 | $self->idletasks; | ||
| 544 | $self->after(100); | ||
| 545 | $self->_OnLeave; | ||
| 546 | $self->Callback(-command => $self); | ||
| 547 | } | ||
| 548 | # | ||
| 549 | # some widget method overrides to make sure things | ||
| 550 | # get associated with the image, not the canvas | ||
| 551 | # | ||
| 552 | sub focus { | ||
| 553 | # $_[0]->focus($_[0]->{_bind_group}); | ||
| 554 | # $_[0]->{_bind_group}->focus(); | ||
| 555 | } | ||
| 556 | |||
| 557 | sub bind { | ||
| 558 | my $self = shift; | ||
| 559 | my $event = shift; | ||
| 560 | return $notkeymouse_event{$event} ? | ||
| 561 | $self->CanvasBind($event, @_) : | ||
| 562 | $self->SUPER::bind($self->{_bind_group}, $event, @_); | ||
| 563 | } | ||
| 564 | # | ||
| 565 | # accessor for the bind_group, so e.g., | ||
| 566 | # balloons can be attached | ||
| 567 | # | ||
| 568 | sub get_bindtag { | ||
| 569 | return $_[0]->{_bind_group}; | ||
| 570 | } | ||
| 571 | |||
| 572 | ####################################################### | ||
| 573 | # | ||
| 574 | # private methods | ||
| 575 | # | ||
| 576 | ####################################################### | ||
| 577 | sub _flash { | ||
| 578 | my $self = shift; | ||
| 579 | |||
| 580 | #print "flash Exists!!!\n" if exists $self->{_flash}; | ||
| 581 | #print "Not Exists!!!\n" unless exists $self->{_flash}; | ||
| 582 | |||
| 583 | return 1 unless exists($self->{_flash}); | ||
| 584 | |||
| 585 | $self->{_flash}->[0]->cancel, | ||
| 586 | $self->lower($self->{_active_group}, $self->{_idle_group}), | ||
| 587 | delete $self->{_flash}, | ||
| 588 | return 1 | ||
| 589 | unless $self->{_flash}[1]; | ||
| 590 | |||
| 591 | $self->{_flash}[1]-- if ($self->{_flash}[1] > 0); | ||
| 592 | |||
| 593 | if ($self->{_flash}[2]) { | ||
| 594 | $self->lower($self->{_active_group}, $self->{_idle_group}); | ||
| 595 | $self->{_flash}[2] = 0; | ||
| 596 | } | ||
| 597 | else { | ||
| 598 | $self->lower($self->{_idle_group}, $self->{_active_group}); | ||
| 599 | $self->{_flash}[2] = 1; | ||
| 600 | } | ||
| 601 | |||
| 602 | return 1; | ||
| 603 | } | ||
| 604 | |||
| 605 | sub _OnEnter | ||
| 606 | { | ||
| 607 | my $self = shift; | ||
| 608 | |||
| 609 | print "Entered\n" | ||
| 610 | if $self->{_debug}; | ||
| 611 | |||
| 612 | # | ||
| 613 | # ButtonPress is invoking Enter as well, so flag and ignore it | ||
| 614 | # | ||
| 615 | delete $self->{_pressed}, | ||
| 616 | return 1 | ||
| 617 | if $self->{_pressed}; | ||
| 618 | |||
| 619 | return 1 | ||
| 620 | if ($self->cget('-state') eq 'disabled'); | ||
| 621 | # | ||
| 622 | # cancel any flash | ||
| 623 | # | ||
| 624 | $self->flash(0); | ||
| 625 | |||
| 626 | $self->lower($self->{_idle_group}, $self->{_active_group}); | ||
| 627 | return 1; | ||
| 628 | } | ||
| 629 | |||
| 630 | sub _OnLeave | ||
| 631 | { | ||
| 632 | my $self = shift; | ||
| 633 | |||
| 634 | print "Left\n" | ||
| 635 | if $self->{_debug}; | ||
| 636 | # | ||
| 637 | # ButtonRelease is invoking Leave as well, so flag and ignore it | ||
| 638 | # | ||
| 639 | delete $self->{_released}, | ||
| 640 | return 1 | ||
| 641 | if $self->{_released}; | ||
| 642 | # | ||
| 643 | # cancel any flash | ||
| 644 | # | ||
| 645 | $self->flash(0); | ||
| 646 | |||
| 647 | $self->lower($self->{_active_group}, $self->{_idle_group}); | ||
| 648 | return 1; | ||
| 649 | } | ||
| 650 | |||
| 651 | sub _OnPress | ||
| 652 | { | ||
| 653 | my $self = shift; | ||
| 654 | |||
| 655 | print "Pressed\n" | ||
| 656 | if $self->{_debug}; | ||
| 657 | |||
| 658 | return 1 | ||
| 659 | if ($self->cget('-state') eq 'disabled'); | ||
| 660 | # | ||
| 661 | # cancel any flash | ||
| 662 | # | ||
| 663 | $self->flash(0); | ||
| 664 | |||
| 665 | $self->lower($self->{_active_group}, $self->{_idle_group}); | ||
| 666 | # $self->{_pressed} = 1; | ||
| 667 | return 1; | ||
| 668 | } | ||
| 669 | |||
| 670 | sub _OnRelease | ||
| 671 | { | ||
| 672 | my $self = shift; | ||
| 673 | |||
| 674 | print "Released\n" | ||
| 675 | if $self->{_debug}; | ||
| 676 | |||
| 677 | return 1 | ||
| 678 | if ($self->cget('-state') eq 'disabled'); | ||
| 679 | # | ||
| 680 | # cancel any flash | ||
| 681 | # | ||
| 682 | $self->flash(0); | ||
| 683 | |||
| 684 | $self->lower($self->{_idle_group}, $self->{_active_group}); | ||
| 685 | $self->Callback(-command => $self->cget('-command')); | ||
| 686 | # $self->{_released} = 1; | ||
| 687 | # delete $self->{_pressed}; | ||
| 688 | } | ||
| 689 | # | ||
| 690 | # queue up a redraw | ||
| 691 | # | ||
| 692 | sub _layoutRequest { | ||
| 693 | my $self = shift; | ||
| 694 | $self->afterIdle(['_renderButton', $self]) | ||
| 695 | unless $self->{_pending}; | ||
| 696 | $self->{_pending} |= $_[0] if $_[0]; | ||
| 697 | } | ||
| 698 | # | ||
| 699 | # (re)draw the button | ||
| 700 | # | ||
| 701 | sub _renderButton { | ||
| 702 | my ($self, $usegd, $gdfont, $notext, $noimage) = @_; | ||
| 703 | |||
| 704 | $self->{_pending} = 0; | ||
| 705 | my ($angle, $disperse, $compound, $shape, $style, $orient) = ( | ||
| 706 | $self->cget('-angle'), | ||
| 707 | $self->cget('-dispersion'), | ||
| 708 | $self->cget('-compound'), | ||
| 709 | $self->cget('-shape'), | ||
| 710 | $self->cget('-style'), | ||
| 711 | $self->cget('-orient'), | ||
| 712 | ); | ||
| 713 | |||
| 714 | print join("\n", $angle, $disperse, $shape, $style), "\n" | ||
| 715 | if $self->{_debug}; | ||
| 716 | # | ||
| 717 | # validate our widget specific options | ||
| 718 | # | ||
| 719 | croak "Invalid -angle option $angle; must be between 0 and 1" | ||
| 720 | if ($angle < 0) || ($angle > 1); | ||
| 721 | |||
| 722 | croak "Invalid -dispersion option $disperse; must be between 0 and 1" | ||
| 723 | if ($disperse < 0) || ($disperse > 1); | ||
| 724 | |||
| 725 | croak "Invalid -compound option $compound" | ||
| 726 | unless $valid_compound{$compound}; | ||
| 727 | |||
| 728 | croak "Invalid -shape option $shape" | ||
| 729 | unless $valid_shapes{$shape}; | ||
| 730 | |||
| 731 | croak "Invalid -style option $style" | ||
| 732 | unless $valid_styles{$style}; | ||
| 733 | |||
| 734 | croak "Invalid -orient option $orient" | ||
| 735 | if (($shape eq 'bevel') || ($shape eq 'folio')) && | ||
| 736 | (! $valid_orient{$orient}); | ||
| 737 | # | ||
| 738 | # cancel any flash | ||
| 739 | # | ||
| 740 | $self->flash(0); | ||
| 741 | # | ||
| 742 | # create 3 versions: active(bright), idle(dark), and binding(transparent) | ||
| 743 | # | ||
| 744 | $self->delete('all'), | ||
| 745 | delete $self->{_active_group}, | ||
| 746 | delete $self->{_idle_group}, | ||
| 747 | delete $self->{_bind_group} | ||
| 748 | if $self->{_active_group}; | ||
| 749 | # | ||
| 750 | # force our background to be transparent | ||
| 751 | # | ||
| 752 | # $self->SUPER::configure(-background => ''); | ||
| 753 | my $bg = $self->cget('-background') || $self->Parent->cget('-background'); | ||
| 754 | |||
| 755 | my $rgb = $self->rgb($bg) | ||
| 756 | || croak "Invalid background color value"; | ||
| 757 | my @active = @$rgb; | ||
| 758 | |||
| 759 | foreach (0..2) { | ||
| 760 | $active[$_] = $active[$_] + int((65535 - $active[$_]) * 0.4); | ||
| 761 | $active[$_] = 0xFFFF if ($active[$_] > 0xFFFF); | ||
| 762 | } | ||
| 763 | # | ||
| 764 | # if we have some text, compute bbox for it | ||
| 765 | # | ||
| 766 | $self->{_text} = ${$self->{_textvariable}} | ||
| 767 | if $self->{_textvariable}; | ||
| 768 | |||
| 769 | my ($w, $h, $xl, $yl, $xh, $yh, $textw, $texth) = (0,0,0, 0, 100, 100, 0, 0); | ||
| 770 | |||
| 771 | my ($bitmap, $image, $text) = | ||
| 772 | ($self->cget('-bitmap'), $self->cget('-image'), $self->cget('-text')); | ||
| 773 | ($w, $h) = $self->_computeBBox(), | ||
| 774 | $xh = $w + ($self->cget('-padx') << 1), | ||
| 775 | $yh = $h + ($self->cget('-pady') << 1) | ||
| 776 | if ($bitmap || $image || defined($text)); | ||
| 777 | |||
| 778 | # $usegd = 0 unless $usegd; | ||
| 779 | # print "usegd $usegd $w $h\n"; | ||
| 780 | |||
| 781 | # | ||
| 782 | # for round buttons, compute diameter based on hypoteneuse of bbox | ||
| 783 | # | ||
| 784 | $xh = $yh = _round(sqrt(($yh ** 2) + ($xh ** 2))) | ||
| 785 | if ($shape eq 'round'); | ||
| 786 | # | ||
| 787 | # if rendering an image button, compute any scaling | ||
| 788 | # | ||
| 789 | my ($xscale, $yscale) = (1,1); | ||
| 790 | ($xscale, $yscale, $xh, $yh) = $self->_getImageScales($xh, $yh) | ||
| 791 | if ($style eq 'image'); | ||
| 792 | # | ||
| 793 | # if rendering for capture just get the images | ||
| 794 | # NOTE: in future we may want to render the images *and* | ||
| 795 | # display in Tk | ||
| 796 | # NOTE2: we don't create a binding group for this | ||
| 797 | # but we do return the binding coordinates | ||
| 798 | # | ||
| 799 | return ($style eq 'image') ? | ||
| 800 | ($self->_drawGdButton($xh, $yh, $xscale, $yscale, | ||
| 801 | $self->cget('-activeimage'), $gdfont, $notext, $noimage), | ||
| 802 | $self->_drawGdButton($xh, $yh, $xscale, $yscale, | ||
| 803 | $self->cget('-idleimage'), $gdfont, $notext, $noimage)) : | ||
| 804 | |||
| 805 | ($self->_drawGdButton($xh, $yh, @active, $gdfont, $notext, $noimage), | ||
| 806 | $self->_drawGdButton($xh, $yh, @$rgb, $gdfont, $notext, $noimage)) | ||
| 807 | if $usegd; | ||
| 808 | # | ||
| 809 | # update geometry: force to any -width or -height | ||
| 810 | # | ||
| 811 | my $ew = $self->{_width} || $xh + 4; | ||
| 812 | my $eh = $self->{_height} || $yh + 4; | ||
| 813 | $orient = $self->cget('-orient'); | ||
| 814 | if ((($shape eq 'bevel') || ($shape eq 'folio') || ($shape eq 'rectangle')) && | ||
| 815 | ((index($orient, 'w') == 0) || (index($orient, 'e') == 0))) { | ||
| 816 | # print "geometry is $eh, $ew\n"; | ||
| 817 | $self->GeometryRequest($eh,$ew); | ||
| 818 | } | ||
| 819 | else { | ||
| 820 | $self->GeometryRequest($ew,$eh); | ||
| 821 | } | ||
| 822 | |||
| 823 | ($self->{_active_group}, $self->{_idle_group}) = ($style eq 'image') ? | ||
| 824 | ($self->_drawTkButton($xh, $yh, $xscale, $yscale, $self->cget('-activeimage'), $notext, $noimage), | ||
| 825 | $self->_drawTkButton($xh, $yh, $xscale, $yscale, $self->cget('-idleimage'), $notext, $noimage)) : | ||
| 826 | ($self->_drawTkButton($xh, $yh, @active, $notext, $noimage), | ||
| 827 | $self->_drawTkButton($xh, $yh, @$rgb, $notext, $noimage)); | ||
| 828 | # | ||
| 829 | # create binding area for the button | ||
| 830 | # | ||
| 831 | $self->_bindFromImage($xh, $yh, $xscale, $yscale); | ||
| 832 | |||
| 833 | $self->_scaleButtons($xh, $yh) | ||
| 834 | if ($self->{_width} || $self->{_height}); | ||
| 835 | |||
| 836 | $balloon->attach($self, | ||
| 837 | -initwait => $self->{_tooltip}[1], | ||
| 838 | -balloonposition => 'mouse', | ||
| 839 | -msg => { $self->{_bind_group} => $self->{_tooltip}[0] } | ||
| 840 | ) | ||
| 841 | if $self->{_tooltip}; | ||
| 842 | } | ||
| 843 | |||
| 844 | sub _scaleButtons { | ||
| 845 | my ($self, $xh, $yh) = @_; | ||
| 846 | # | ||
| 847 | # scale the image if we have explicit dimensions | ||
| 848 | # | ||
| 849 | my $ew = $self->{_width} || $xh; | ||
| 850 | my $eh = $self->{_height} || $yh; | ||
| 851 | my ($scalex, $scaley) = ($ew/$xh, $eh/$yh); | ||
| 852 | # | ||
| 853 | # do we need to impose the final dimensions ? | ||
| 854 | # | ||
| 855 | # $self->SUPER::configure(-width => $ew, -height => $eh); | ||
| 856 | |||
| 857 | $self->scale($_, 0, 0, $scalex, $scaley) | ||
| 858 | foreach ($self->{_active_group}, $self->{_idle_group}, $self->{_bind_group}); | ||
| 859 | return $self; | ||
| 860 | } | ||
| 861 | # | ||
| 862 | # compute image scales | ||
| 863 | # | ||
| 864 | sub _getImageScales { | ||
| 865 | my ($self, $xh, $yh) = @_; | ||
| 866 | |||
| 867 | my ($activeimg, $idleimg) = | ||
| 868 | ($self->cget('-activeimage'), $self->cget('-idleimage')); | ||
| 869 | |||
| 870 | croak "Missing -activeimage for -style => 'image' option" | ||
| 871 | unless $activeimg; | ||
| 872 | |||
| 873 | croak "Missing -idleimage for -style => 'image' option" | ||
| 874 | unless $idleimg; | ||
| 875 | |||
| 876 | my ($aw, $ah, $iw, $ih) = | ||
| 877 | ($activeimg->width(), $activeimg->height(), | ||
| 878 | $idleimg->width(), $idleimg->height()); | ||
| 879 | |||
| 880 | my ($minw, $minh, $maxw, $maxh) = | ||
| 881 | ((($aw > $iw) ? $iw : $aw), (($ah > $ih) ? $ih : $ah), | ||
| 882 | (($aw > $iw) ? $aw : $iw), (($ah > $ih) ? $ah : $ih)); | ||
| 883 | # | ||
| 884 | # if images smaller than the text/image bbox, | ||
| 885 | # scale them up | ||
| 886 | # | ||
| 887 | my ($xscale, $yscale) = ( | ||
| 888 | (($minw < $xh) ? $xh/$minw : 1), | ||
| 889 | (($minh < $yh) ? $yh/$minh : 1)); | ||
| 890 | |||
| 891 | return ($xscale, $yscale, $maxw * $xscale, $maxh * $yscale); | ||
| 892 | } | ||
| 893 | |||
| 894 | sub _drawRectangle { | ||
| 895 | my ($xh, $yh, $curve) = @_; | ||
| 896 | return (0, 0, $xh, 0, $xh, $yh, 0, $yh); | ||
| 897 | } | ||
| 898 | |||
| 899 | sub _drawBevel { | ||
| 900 | my ($xh, $yh, $curve, $orient) = @_; | ||
| 901 | |||
| 902 | my $top = ((index($orient, 'n') == 0) || (index($orient, 'w') == 0)); | ||
| 903 | my $d = _round(0.2 * $xh); | ||
| 904 | # | ||
| 905 | # returns array of endpoints | ||
| 906 | # | ||
| 907 | my $side = ((index($orient, 's') == 1) || (index($orient, 'w') == 1)); | ||
| 908 | return $top ? | ||
| 909 | ($side ? | ||
| 910 | (0, 0, $xh - $d, 0, $xh, $yh, 0, $yh) : | ||
| 911 | ($d, 0, $xh, 0, $xh, $yh, 0, $yh)) : | ||
| 912 | ($side ? | ||
| 913 | (0, 0, $xh, 0, $xh - $d, $yh, 0, $yh) : | ||
| 914 | (0, 0, $xh, 0, $xh, $yh, $d, $yh)); | ||
| 915 | } | ||
| 916 | |||
| 917 | sub _drawFolio { | ||
| 918 | my ($xh, $yh, $curve, $orient) = @_; | ||
| 919 | |||
| 920 | my $top = ((index($orient, 'n') == 0) || (index($orient, 'w') == 0)); | ||
| 921 | my $d = _round(0.1 * $xh); | ||
| 922 | # | ||
| 923 | # returns array of endpoints | ||
| 924 | # | ||
| 925 | return $top ? | ||
| 926 | ($d, 0, $xh - $d, 0, $xh, $yh, 0, $yh) : | ||
| 927 | (0, 0, $xh, 0, $xh - $d, $yh, $d, $yh); | ||
| 928 | } | ||
| 929 | |||
| 930 | sub _getGDImage { | ||
| 931 | my $image = shift; | ||
| 932 | |||
| 933 | my $imgdata = $image->data(-format => 'png'); | ||
| 934 | $imgdata = decode_base64($imgdata); | ||
| 935 | my $gdimg = GD::Image->new($imgdata); | ||
| 936 | $@ = "Unable to convert image", | ||
| 937 | return undef | ||
| 938 | unless $gdimg; | ||
| 939 | |||
| 940 | return $gdimg; | ||
| 941 | } | ||
| 942 | |||
| 943 | sub _setGDImage { | ||
| 944 | my ($self, $image) = @_; | ||
| 945 | |||
| 946 | return $self->Photo(-data => encode_base64($image->png()), -format => 'png'); | ||
| 947 | } | ||
| 948 | |||
| 949 | sub _getTkCoords { | ||
| 950 | my ($self, $xh, $yh, $bitmap, $image, $text, $compound) = @_; | ||
| 951 | |||
| 952 | my ($imgw, $imgh) = | ||
| 953 | $image ? ($image->width, $image->height) : | ||
| 954 | $bitmap ? $self->_getBitmapSize($bitmap) : | ||
| 955 | (0,0); | ||
| 956 | |||
| 957 | my ($padx, $pady, $imgx, $imgy, $textx, $texty, $textw, $texth) = ( | ||
| 958 | $self->{_padx}, $self->{_pady}, | ||
| 959 | ($xh >> 1), ($yh >> 1), | ||
| 960 | ($xh >> 1), ($yh >> 1), | ||
| 961 | 0, 0); | ||
| 962 | |||
| 963 | unless (($compound eq 'none') || ($compound eq 'center')) { | ||
| 964 | $imgy = ($compound eq 'top') ? $pady + ($imgh >> 1): | ||
| 965 | ($compound eq 'bottom') ? $yh - 4 - $pady - ($imgh >> 1) : $imgy, | ||
| 966 | $imgx = ($compound eq 'left') ? $padx + ($imgw >> 1) : | ||
| 967 | ($compound eq 'right') ? $xh - 4 - $padx - ($imgw >> 1) : $imgx; | ||
| 968 | |||
| 969 | if (defined($text) && | ||
| 970 | (($self->{_shape} ne 'round') || | ||
| 971 | ($compound eq 'left') || ($compound eq 'right'))) { | ||
| 972 | ($textw, $texth) = $self->_computeTextBBox(); | ||
| 973 | $texty = ($compound eq 'top') ? $imgy + 4 + ($texth >> 1) : | ||
| 974 | ($compound eq 'bottom') ? ($texth >> 1) + $pady : $texty; | ||
| 975 | $textx = ($compound eq 'left') ? $imgx +($imgw >> 1) + 4 + ($textw >> 1) : | ||
| 976 | ($compound eq 'right') ? $padx + ($textw >> 1) : $textx; | ||
| 977 | } | ||
| 978 | # | ||
| 979 | # realign the image to cuddle the text | ||
| 980 | # | ||
| 981 | $imgy = ($compound eq 'top') ? $yh >> 2 : ($yh >> 2) + ($yh >> 1) | ||
| 982 | if (($self->{_shape} eq 'round') && | ||
| 983 | (($compound eq 'top') || ($compound eq 'bottom'))); | ||
| 984 | } | ||
| 985 | return ($imgx, $imgy, $textx, $texty, $textw, $texth); | ||
| 986 | } | ||
| 987 | # | ||
| 988 | # NOTE: we don't support bitmaps w/ GD... | ||
| 989 | # | ||
| 990 | sub _getGdCoords { | ||
| 991 | my ($self, $xh, $yh, $image, $text, $compound) = @_; | ||
| 992 | my ($imgw, $imgh) = $image ? ($image->width, $image->height) : (0,0); | ||
| 993 | my ($padx, $pady, $imgx, $imgy, $textx, $texty, $textw, $texth) = ( | ||
| 994 | $self->{_padx}, $self->{_pady}, | ||
| 995 | ($xh - $imgw) >> 1, ($yh - $imgh) >> 1, | ||
| 996 | $xh >> 1, $yh >> 1, | ||
| 997 | 0,0); | ||
| 998 | |||
| 999 | unless (($compound eq 'none') || ($compound eq 'center')) { | ||
| 1000 | $imgy = ($compound eq 'top') ? $pady : | ||
| 1001 | ($compound eq 'bottom') ? $yh + 4 - $pady - $imgh : $imgy, | ||
| 1002 | $imgx = ($compound eq 'left') ? $padx : | ||
| 1003 | ($compound eq 'right') ? $xh + 4 - $padx - $imgw : $imgx; | ||
| 1004 | |||
| 1005 | if (defined($text) && | ||
| 1006 | (($self->{_shape} ne 'round') || | ||
| 1007 | ($compound eq 'left') || ($compound eq 'right'))) { | ||
| 1008 | ($textw, $texth) = $self->_computeTextBBox(); | ||
| 1009 | $texty = ($compound eq 'top') ? $imgy + 4 + ($texth >> 1) : | ||
| 1010 | ($compound eq 'bottom') ? ($texth >> 1) + $pady : $texty; | ||
| 1011 | $textx = ($compound eq 'left') ? $imgx +($imgw >> 1) + 4 + ($textw >> 1) : | ||
| 1012 | ($compound eq 'right') ? $padx + ($textw >> 1) : $textx; | ||
| 1013 | } | ||
| 1014 | # | ||
| 1015 | # realign the image to cuddle the text | ||
| 1016 | # | ||
| 1017 | $imgy = ($compound eq 'top') ? ($yh >> 2 - ($imgh >> 1)) : | ||
| 1018 | ($yh >> 2) + ($yh >> 1) - ($imgh >> 1) | ||
| 1019 | if (($self->{_shape} eq 'round') && | ||
| 1020 | (($compound eq 'top') || ($compound eq 'bottom'))); | ||
| 1021 | } | ||
| 1022 | return ($imgx, $imgy, $textx, $texty, $textw, $texth); | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | sub _drawTkButton { | ||
| 1026 | my ($self, $xh, $yh, $r, $g, $b, $notext, $noimage) = @_; | ||
| 1027 | |||
| 1028 | my ($xl, $yl, $shape, $style, $angle, $disperse, $slots, $text, $bitmap, $image, $orient) = ( | ||
| 1029 | 0, 0, $self->cget('-shape'), $self->cget('-style'), $self->cget('-angle'), | ||
| 1030 | $self->cget('-dispersion'), 15, $self->cget('-text'), $self->cget('-bitmap'), | ||
| 1031 | $self->cget('-image'), $self->cget('-orient')); | ||
| 1032 | |||
| 1033 | my $compound = ($image || $bitmap) ? $self->cget('-compound') : 'center'; | ||
| 1034 | # | ||
| 1035 | # compute locations of image and/or text | ||
| 1036 | # we're in luck, GD and Tk use same methodnames for image bounds | ||
| 1037 | # | ||
| 1038 | my ($imgw, $imgh) = $image ? ($image->width, $image->height) : | ||
| 1039 | $bitmap ? $self->_getBitmapSize($bitmap) : | ||
| 1040 | (0,0); | ||
| 1041 | |||
| 1042 | my ($imgx, $imgy, $textx, $texty, $textw, $texth) = | ||
| 1043 | $self->_getTkCoords($xh, $yh, $bitmap, $image, $text, $compound); | ||
| 1044 | |||
| 1045 | my $group; | ||
| 1046 | my @clines = (); | ||
| 1047 | my ($colors, $lcolors, $offsets, $top, $bottom, $white, $black, $basecolor, $vert, $textfactor); | ||
| 1048 | my @endpts = (); | ||
| 1049 | unless ($style eq 'image') { | ||
| 1050 | # | ||
| 1051 | # for GD compatibility, use middle color as base | ||
| 1052 | # | ||
| 1053 | ($colors, $lcolors, $offsets, $top, $bottom, $white, $black) = | ||
| 1054 | $self->_getColorMap($xh, $yh, $r, $g, $b); | ||
| 1055 | |||
| 1056 | my $midpt = (scalar @$colors) >> 1; | ||
| 1057 | $basecolor = sprintf("#%04X%04X%04X", @{$colors->[$midpt]}); | ||
| 1058 | |||
| 1059 | my $curve = 6; | ||
| 1060 | $vert = (($shape eq 'bevel') || ($shape eq 'folio') || ($shape eq 'rectangle')) && | ||
| 1061 | ((substr($orient, 0, 1) eq 'w') || (substr($orient, 0, 1) eq 'e')); | ||
| 1062 | $textfactor = ((substr($orient, 1, 1) eq 'e') || (substr($orient, 1, 1) eq 's')) ? 1.2 : 0.8 | ||
| 1063 | if ($shape eq 'bevel'); | ||
| 1064 | @endpts = | ||
| 1065 | ($shape eq 'rectangle') ? _drawRectangle($xh, $yh, $curve) : | ||
| 1066 | ($shape eq 'bevel') ? _drawBevel($xh, $yh, $curve, $orient) : | ||
| 1067 | ($shape eq 'folio') ? _drawFolio($xh, $yh, $curve, $orient) : | ||
| 1068 | (); | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | if ($style eq 'image') { | ||
| 1072 | # | ||
| 1073 | # create from an image; r, g, b are xscale, yscale, and the image, | ||
| 1074 | # respectively | ||
| 1075 | # | ||
| 1076 | my $bgimg = $self->createImage($xh>>1, $yh>>1, -image => $b); | ||
| 1077 | $self->scale($bgimg, 0, 0, $r, $g) | ||
| 1078 | unless ($r == 1) && ($g == 1); | ||
| 1079 | push @clines, $bgimg; | ||
| 1080 | } | ||
| 1081 | elsif ($shape eq 'round') { | ||
| 1082 | my $extent = 180; | ||
| 1083 | my $start = 0; | ||
| 1084 | |||
| 1085 | push @clines, $self->createOval( | ||
| 1086 | 0, 0, $xh, $yh, | ||
| 1087 | -outline => $basecolor, | ||
| 1088 | -fill => $basecolor); | ||
| 1089 | |||
| 1090 | unless ($self->{_style} eq 'flat') { | ||
| 1091 | push @clines, $self->createOval( | ||
| 1092 | 1, 1, $xh - 1, $yh - 1, | ||
| 1093 | -outline => 'grey'); | ||
| 1094 | |||
| 1095 | push @clines, $self->createOval( | ||
| 1096 | 2, 2, $xh - 2, $yh - 2, | ||
| 1097 | -outline => 'black'); | ||
| 1098 | |||
| 1099 | push @clines, $self->createOval( | ||
| 1100 | 3, 3, $xh - 3, $yh - 3, | ||
| 1101 | -outline => 'grey'); | ||
| 1102 | |||
| 1103 | push @clines, $self->createOval( | ||
| 1104 | 4, 4, $xh - 4, $yh - 4, | ||
| 1105 | -outline => 'grey'); | ||
| 1106 | |||
| 1107 | $yl = 3; | ||
| 1108 | $yh -= 3; | ||
| 1109 | $xl = 3; | ||
| 1110 | $xh -= 3; | ||
| 1111 | my ($byl, $byh) = ($yl, $yh); | ||
| 1112 | my $i = 0; | ||
| 1113 | while (($i < scalar @$lcolors) && ($yh - $yl > 0)) { | ||
| 1114 | push @clines, $self->createArc( | ||
| 1115 | $xl, $yl, $xh, $yh, | ||
| 1116 | -start => int($start), | ||
| 1117 | -extent => $extent, | ||
| 1118 | -outline => $$lcolors[4+$i++], | ||
| 1119 | -style => 'arc'); | ||
| 1120 | $yl++; $yh--; | ||
| 1121 | |||
| 1122 | $i++, next | ||
| 1123 | unless ($i%6 == 1); | ||
| 1124 | |||
| 1125 | push @clines, $self->createArc( | ||
| 1126 | $xl, $byl, $xh, $byh, | ||
| 1127 | -start => 200+int($start), | ||
| 1128 | -extent => $extent-40, | ||
| 1129 | -outline => $$lcolors[$i++], | ||
| 1130 | -style => 'arc'); | ||
| 1131 | $byl++; $byh--; | ||
| 1132 | } | ||
| 1133 | } | ||
| 1134 | } | ||
| 1135 | elsif ($shape eq 'oval') { | ||
| 1136 | push @clines, | ||
| 1137 | $self->createLine( | ||
| 1138 | $offsets->[$_ - $bottom], | ||
| 1139 | $_, | ||
| 1140 | $xh - $offsets->[$_ - $bottom], | ||
| 1141 | $_, | ||
| 1142 | -fill => shift @$lcolors), | ||
| 1143 | $self->createLine( | ||
| 1144 | $offsets->[$_ - $bottom], | ||
| 1145 | $_, | ||
| 1146 | $offsets->[$_ - $bottom] + 2, | ||
| 1147 | $_, | ||
| 1148 | -fill => $basecolor), | ||
| 1149 | $self->createLine( | ||
| 1150 | $xh - $offsets->[$_ - $bottom] - 2, | ||
| 1151 | $_, | ||
| 1152 | $xh - $offsets->[$_ - $bottom], | ||
| 1153 | $_, | ||
| 1154 | -fill => $basecolor) | ||
| 1155 | foreach ($bottom..$top); | ||
| 1156 | |||
| 1157 | push @clines, | ||
| 1158 | $self->createLine( | ||
| 1159 | $offsets->[$_ - $bottom], | ||
| 1160 | $_, | ||
| 1161 | $xh - $offsets->[$_ - $bottom], | ||
| 1162 | $_, | ||
| 1163 | -fill => $basecolor) | ||
| 1164 | foreach ($top+1..$yh); | ||
| 1165 | } | ||
| 1166 | else { | ||
| 1167 | if ($vert) { | ||
| 1168 | # | ||
| 1169 | # rotate endpts | ||
| 1170 | # | ||
| 1171 | my @rotpts = $self->_rotateShape($xh, @endpts); | ||
| 1172 | |||
| 1173 | push @clines, $self->createPolygon(@rotpts, -fill => $basecolor); | ||
| 1174 | |||
| 1175 | my $lfactor = ($endpts[7] - $endpts[1])/$xh; | ||
| 1176 | my $rfactor = ($endpts[5] - $endpts[3])/$xh; | ||
| 1177 | my ($low, $hi) = ($rotpts[1], $rotpts[7]); | ||
| 1178 | |||
| 1179 | # print "bottom: $bottom top: $top lines: ", scalar @$offsets, "\n"; | ||
| 1180 | |||
| 1181 | $low -= $lfactor, | ||
| 1182 | $hi += $rfactor, | ||
| 1183 | push @clines, | ||
| 1184 | $self->createLine( | ||
| 1185 | $_, _round($low + $offsets->[$_ - $bottom]), | ||
| 1186 | $_, _round($hi - $offsets->[$_ - $bottom]), | ||
| 1187 | -fill => shift @$lcolors) | ||
| 1188 | foreach ($bottom..$top); | ||
| 1189 | } | ||
| 1190 | else { | ||
| 1191 | push @clines, $self->createPolygon(@endpts, -fill => $basecolor); | ||
| 1192 | |||
| 1193 | my $lfactor = ($endpts[0] - $endpts[6])/$yh; | ||
| 1194 | my $rfactor = ($endpts[4] - $endpts[2])/$yh; | ||
| 1195 | my ($low, $hi) = ($endpts[0], $endpts[2]); | ||
| 1196 | |||
| 1197 | $low -= $lfactor, | ||
| 1198 | $hi += $rfactor, | ||
| 1199 | push @clines, | ||
| 1200 | $self->createLine( | ||
| 1201 | _round($low + $offsets->[$_ - $bottom]), $_, | ||
| 1202 | _round($hi - $offsets->[$_ - $bottom]), $_, | ||
| 1203 | -fill => shift @$lcolors) | ||
| 1204 | foreach ($bottom..$top); | ||
| 1205 | } | ||
| 1206 | } | ||
| 1207 | # | ||
| 1208 | # add image and/or text: compute the locations based on -compound | ||
| 1209 | # | ||
| 1210 | my ($imgid, $textid, $ulid); | ||
| 1211 | my @addons = (); | ||
| 1212 | unless ($notext) { | ||
| 1213 | if (defined($text) && ($text ne '') && | ||
| 1214 | ((!$bitmap && !$image) || | ||
| 1215 | ($self->{_compound} ne 'none'))) { | ||
| 1216 | # | ||
| 1217 | # for bevel, move text away from bevel edge | ||
| 1218 | # | ||
| 1219 | if ($shape eq 'bevel') { | ||
| 1220 | $texty = int($texty * $textfactor) | ||
| 1221 | if $vert; | ||
| 1222 | $textx = int($textx * $textfactor) | ||
| 1223 | unless $vert; | ||
| 1224 | } | ||
| 1225 | if ($self->cget('-underline')) { | ||
| 1226 | $ulid = $self->_underlineText($textx, $texty, $text); | ||
| 1227 | unshift @addons, $ulid | ||
| 1228 | if $ulid; | ||
| 1229 | } | ||
| 1230 | if ($vert) { | ||
| 1231 | my $type = $self->cget('-verticaltext'); | ||
| 1232 | if ($type && (uc $type eq 'GD') && $hasgd) { | ||
| 1233 | $textid = $self->createImage( | ||
| 1234 | # $texty, $xh - $textx, | ||
| 1235 | 4, $xh - $textx, | ||
| 1236 | -image => $self->_setGDImage($self->_renderVerticalGdText($text))); | ||
| 1237 | } | ||
| 1238 | else { | ||
| 1239 | $textid = $type ? | ||
| 1240 | $self->createText($textx, $texty, | ||
| 1241 | -anchor => 'center', | ||
| 1242 | -text => $text, | ||
| 1243 | -fill => $self->cget('-foreground'), | ||
| 1244 | # -width => $self->cget('-wraplength'), | ||
| 1245 | # -justify => $self->cget('-justify'), | ||
| 1246 | -font => $self->cget('-font')) : | ||
| 1247 | |||
| 1248 | $self->createText($textx, $texty, | ||
| 1249 | -anchor => 'center', | ||
| 1250 | -text => $text, | ||
| 1251 | -fill => $self->cget('-foreground'), | ||
| 1252 | -width => $self->cget('-wraplength'), | ||
| 1253 | -justify => $self->cget('-justify'), | ||
| 1254 | -font => $self->cget('-font')); | ||
| 1255 | } | ||
| 1256 | } | ||
| 1257 | else { | ||
| 1258 | $textid = $self->createText($textx, $texty, | ||
| 1259 | -anchor => 'center', | ||
| 1260 | -text => $text, | ||
| 1261 | -fill => $self->cget('-foreground'), | ||
| 1262 | -width => $self->cget('-wraplength'), | ||
| 1263 | -justify => $self->cget('-justify'), | ||
| 1264 | -font => $self->cget('-font')); | ||
| 1265 | } | ||
| 1266 | unshift @addons, $textid; | ||
| 1267 | } | ||
| 1268 | } | ||
| 1269 | |||
| 1270 | unless ($noimage) { | ||
| 1271 | if ($shape eq 'bevel') { | ||
| 1272 | $imgy = int($imgy * $textfactor) | ||
| 1273 | if $vert; | ||
| 1274 | $imgx = int($imgx * $textfactor) | ||
| 1275 | unless $vert; | ||
| 1276 | } | ||
| 1277 | $imgid = $image ? $self->createImage($imgx, $imgy, -image => $image) : | ||
| 1278 | $self->createBitmap($imgx, $imgy, -bitmap => $bitmap), | ||
| 1279 | $self->lower($textid, $imgid), | ||
| 1280 | unshift @addons, $imgid | ||
| 1281 | if ($image || $bitmap); | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | return $self->createGroup(0,0, -members => [ @clines, @addons ]); | ||
| 1285 | } | ||
| 1286 | # | ||
| 1287 | # renders the button via GD; returns bot the button image and | ||
| 1288 | # the image binding coordinates | ||
| 1289 | # | ||
| 1290 | sub _drawGdButton { | ||
| 1291 | my ($self, $xh, $yh, $r, $g, $b, $gdfont, $notext, $noimage) = @_; | ||
| 1292 | |||
| 1293 | my ($xl, $yl, $shape, $style, $angle, $disperse, $slots, $text, $image, $orient) = ( | ||
| 1294 | 0, 0, $self->cget('-shape'), $self->cget('-style'), $self->cget('-angle'), | ||
| 1295 | $self->cget('-dispersion'), 15, $self->cget('-text'), $self->cget('-image'), | ||
| 1296 | $self->cget('-orient')); | ||
| 1297 | |||
| 1298 | my $compound = $image ? $self->cget('-compound') : 'center'; | ||
| 1299 | # | ||
| 1300 | # compute locations of image and/or text | ||
| 1301 | # | ||
| 1302 | my ($padx, $pady) = ($self->cget('-padx'), $self->cget('-pady')); | ||
| 1303 | # | ||
| 1304 | # if there's an image, and we're using GD, get its data into a GD image | ||
| 1305 | # | ||
| 1306 | if ($image) { | ||
| 1307 | $image = _getGDImage($image); | ||
| 1308 | return undef unless $image; | ||
| 1309 | } | ||
| 1310 | # | ||
| 1311 | # we're in luck, GD and Tk use same methodnames for image bounds | ||
| 1312 | # | ||
| 1313 | my ($imgw, $imgh) = $image ? ($image->width, $image->height) : (0,0); | ||
| 1314 | |||
| 1315 | my ($imgx, $imgy, $textx, $texty, $textw, $texth) = | ||
| 1316 | $self->_getGdCoords($xh, $yh, $image, $text, $compound); | ||
| 1317 | |||
| 1318 | my $vert = (($shape eq 'bevel') || ($shape eq 'folio') || ($shape eq 'rectangle')) && | ||
| 1319 | ((index($orient, 'w') == 0) || (index($orient, 'e') == 0)); | ||
| 1320 | |||
| 1321 | my $btnimg = $vert ? GD::Image->new($yh, $xh) : GD::Image->new($xh, $yh); | ||
| 1322 | |||
| 1323 | my $curve = 6; | ||
| 1324 | my @endpts = (); | ||
| 1325 | |||
| 1326 | my ($colors, $lcolors, $offsets, $top, $bottom, $white, $black, $midpt, $basecolor, $textfactor); | ||
| 1327 | my %gdcolors = (); | ||
| 1328 | # | ||
| 1329 | # alloc transparent color | ||
| 1330 | # | ||
| 1331 | my $transparent = $btnimg->colorAllocate(1, 1, 1); | ||
| 1332 | $btnimg->transparent($transparent); | ||
| 1333 | |||
| 1334 | $vert ? | ||
| 1335 | $btnimg->filledRectangle(0,0,$yh - 1,$xh - 1, $transparent) : | ||
| 1336 | $btnimg->filledRectangle(0,0,$xh - 1,$yh - 1, $transparent); | ||
| 1337 | |||
| 1338 | unless ($style eq 'image') { | ||
| 1339 | # | ||
| 1340 | # must explicitly allocate colors for GD, | ||
| 1341 | # so we'll create a map of the slotted color strings | ||
| 1342 | # to their GD index...and add the transparent color | ||
| 1343 | # must have a GD image object to allocate | ||
| 1344 | # NOTE: may need to alloc for text ?? | ||
| 1345 | # | ||
| 1346 | ($colors, $lcolors, $offsets, $top, $bottom, $white, $black) = | ||
| 1347 | $self->_getColorMap($xh - 2, $yh - 2, $r, $g, $b); | ||
| 1348 | |||
| 1349 | foreach (@$colors) { | ||
| 1350 | # | ||
| 1351 | # make sure we don't collide transparent with existing | ||
| 1352 | # | ||
| 1353 | $gdcolors{sprintf('#%04X%04X%04X', @$_)} = | ||
| 1354 | $btnimg->colorAllocate( | ||
| 1355 | ($_->[0] >> 8) & 0xFF, | ||
| 1356 | ($_->[1] >> 8) & 0xFF, | ||
| 1357 | ($_->[2] >> 8) & 0xFF), | ||
| 1358 | next | ||
| 1359 | unless (($_->[0] == 256) && ($_->[1] == 256) && ($_->[2] == 256)); | ||
| 1360 | # | ||
| 1361 | # preserve original key since we've alreayd computed | ||
| 1362 | # the line colors based on the original | ||
| 1363 | # | ||
| 1364 | $gdcolors{'#010001000100'} = $btnimg->colorAllocate(1, 1, 2), | ||
| 1365 | } | ||
| 1366 | # | ||
| 1367 | # use midpt as base color | ||
| 1368 | # | ||
| 1369 | $midpt = (scalar @$colors) >> 1; | ||
| 1370 | $basecolor = $gdcolors{sprintf('#%04X%04X%04X', @{$colors->[$midpt]})}; | ||
| 1371 | # | ||
| 1372 | # now xlate any line colors to the indexes | ||
| 1373 | # | ||
| 1374 | $lcolors->[$_] = $gdcolors{$lcolors->[$_]} | ||
| 1375 | foreach (0..$#$lcolors); | ||
| 1376 | |||
| 1377 | $textfactor = ((substr($orient, 1, 1) eq 'e') || (substr($orient, 1, 1) eq 's')) ? 1.2 : 0.8 | ||
| 1378 | if ($shape eq 'bevel'); | ||
| 1379 | @endpts = | ||
| 1380 | ($shape eq 'rectangle') ? _drawRectangle($xh - 2, $yh - 2, $curve) : | ||
| 1381 | ($shape eq 'bevel') ? _drawBevel($xh, $yh, $curve, $orient) : | ||
| 1382 | ($shape eq 'folio') ? _drawFolio($xh, $yh, $curve, $orient) : | ||
| 1383 | (); | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | if ($style eq 'image') { | ||
| 1387 | # | ||
| 1388 | # create from an image; r, g, b are xscale, yscale, and the image, | ||
| 1389 | # respectively | ||
| 1390 | # | ||
| 1391 | my $format = $b->cget('-format'); | ||
| 1392 | my $data = $b->data(-format => $format); | ||
| 1393 | my $bgimg = ($format eq 'GIF') ? GD::Image->newFromGif($data) : | ||
| 1394 | ($format eq 'PNG') ? GD::Image->newFromPng($data) : | ||
| 1395 | GD::Image->newFromJpeg($data); # ($format eq 'JPEG') | ||
| 1396 | |||
| 1397 | if (($r == 1) && ($g == 1)) { | ||
| 1398 | $btnimg->copy($bgimg, 2, 2, 0, 0, $bgimg->width, $bgimg->height); | ||
| 1399 | } | ||
| 1400 | else { | ||
| 1401 | $btnimg->copyResampled($bgimg, 2, 2, 0, 0, | ||
| 1402 | $btnimg->width - 2, $btnimg->height - 2, | ||
| 1403 | $bgimg->width, $bgimg->height); | ||
| 1404 | } | ||
| 1405 | } | ||
| 1406 | elsif ($shape eq 'round') { | ||
| 1407 | my $extent = 180; | ||
| 1408 | my $start = 270; | ||
| 1409 | # | ||
| 1410 | # get the closest to black and grey | ||
| 1411 | # | ||
| 1412 | $black = $btnimg->colorClosest(0,0,0); | ||
| 1413 | my $grey = $btnimg->colorClosest(64, 64, 64); | ||
| 1414 | |||
| 1415 | $btnimg->filledEllipse($xh>>1, $yh>>1, $xh, $yh, $basecolor); | ||
| 1416 | |||
| 1417 | unless ($style eq 'flat') { | ||
| 1418 | $btnimg->ellipse($xh>>1, $yh>>1, $xh - 1, $yh - 2, $grey); | ||
| 1419 | $btnimg->ellipse($xh>>1, $yh>>1, $xh - 3, $yh - 4, $black); | ||
| 1420 | # $btnimg->ellipse($xh>>1, $yh>>1, $xh - 5, $yh - 6, $grey); | ||
| 1421 | # $btnimg->ellipse($xh>>1, $yh>>1, $xh - 7, $yh - 8, $grey); | ||
| 1422 | |||
| 1423 | my ($cx, $cy) = (($xh - $xl)>>1, ($yh - $yl)>>1); | ||
| 1424 | $yl += 3; | ||
| 1425 | $yh -= 3; | ||
| 1426 | $xl += 3; | ||
| 1427 | $xh -= 3; | ||
| 1428 | my ($byl, $byh) = ($yl, $yh); | ||
| 1429 | my $i = 0; | ||
| 1430 | while (($i < scalar @$lcolors) && ($yh - $yl > 0)) { | ||
| 1431 | $btnimg->arc($cx, $cy, $xh, $yh, 180, 360, $$lcolors[4+$i++]); | ||
| 1432 | $yl++; $yh--; | ||
| 1433 | |||
| 1434 | $i++, next | ||
| 1435 | unless ($i%6 == 1); | ||
| 1436 | |||
| 1437 | $btnimg->arc($cx, $cy, $xh, $byh, 20, 160, $$lcolors[$i++]); | ||
| 1438 | $byl++; $byh--; | ||
| 1439 | } | ||
| 1440 | } | ||
| 1441 | } | ||
| 1442 | elsif ($shape eq 'oval') { | ||
| 1443 | # | ||
| 1444 | # this should be optimized to use a brush... | ||
| 1445 | # | ||
| 1446 | $btnimg->line( | ||
| 1447 | $offsets->[$_ - $bottom], $_, | ||
| 1448 | $xh - $offsets->[$_ - $bottom], $_, | ||
| 1449 | shift @$lcolors), | ||
| 1450 | $btnimg->line( | ||
| 1451 | $offsets->[$_ - $bottom], $_, | ||
| 1452 | $offsets->[$_ - $bottom] + 2, $_, | ||
| 1453 | $basecolor), | ||
| 1454 | $btnimg->line( | ||
| 1455 | $xh - $offsets->[$_ - $bottom] - 2, $_, | ||
| 1456 | $xh - $offsets->[$_ - $bottom], $_, | ||
| 1457 | $basecolor) | ||
| 1458 | foreach ($bottom..$top); | ||
| 1459 | |||
| 1460 | foreach ($top+1..$yh) { | ||
| 1461 | $btnimg->line( | ||
| 1462 | $offsets->[$_ - $bottom], $_, | ||
| 1463 | $xh - $offsets->[$_ - $bottom], $_, | ||
| 1464 | $basecolor) | ||
| 1465 | if defined $offsets->[$_ - $bottom]; | ||
| 1466 | } | ||
| 1467 | } | ||
| 1468 | else { | ||
| 1469 | # | ||
| 1470 | # need vert v horiz versions | ||
| 1471 | # | ||
| 1472 | my $poly = GD::Polygon->new(); | ||
| 1473 | my $i = 0; | ||
| 1474 | my $pad = 2; | ||
| 1475 | if ($vert) { | ||
| 1476 | # | ||
| 1477 | # rotate endpts | ||
| 1478 | # | ||
| 1479 | my @rotpts = $self->_rotateShape($xh, @endpts); | ||
| 1480 | |||
| 1481 | $poly->addPt($rotpts[$i++], $rotpts[$i++]) | ||
| 1482 | while ($i < scalar @rotpts); | ||
| 1483 | $btnimg->filledPolygon($poly, $basecolor); | ||
| 1484 | |||
| 1485 | my $lfactor = ($endpts[7] - $endpts[1])/$xh; | ||
| 1486 | my $rfactor = ($endpts[5] - $endpts[3])/$xh; | ||
| 1487 | my ($low, $hi) = ($rotpts[1], $rotpts[7]); | ||
| 1488 | |||
| 1489 | $low -= $lfactor, | ||
| 1490 | $hi += $rfactor, | ||
| 1491 | $btnimg->line( | ||
| 1492 | $_, _round($low) + $offsets->[$_ - $bottom] + $pad, | ||
| 1493 | $_, _round($hi) - $offsets->[$_ - $bottom] - ($pad - 1), | ||
| 1494 | shift @$lcolors) | ||
| 1495 | foreach ($bottom..$top); | ||
| 1496 | } | ||
| 1497 | else { | ||
| 1498 | $poly->addPt($endpts[$i++], $endpts[$i++]) | ||
| 1499 | while ($i < scalar @endpts); | ||
| 1500 | $btnimg->filledPolygon($poly, $basecolor); | ||
| 1501 | |||
| 1502 | my $lfactor = ($endpts[0] - $endpts[6])/$yh; | ||
| 1503 | my $rfactor = ($endpts[4] - $endpts[2])/$yh; | ||
| 1504 | my ($low, $hi) = ($endpts[0], $endpts[2]); | ||
| 1505 | |||
| 1506 | $low -= $lfactor, | ||
| 1507 | $hi += $rfactor, | ||
| 1508 | $btnimg->line( | ||
| 1509 | _round($low + $offsets->[$_ - $bottom]) + $pad, $_, | ||
| 1510 | _round($hi - $offsets->[$_ - $bottom]) - ($pad - 1), $_, | ||
| 1511 | shift @$lcolors) | ||
| 1512 | foreach ($bottom..$top); | ||
| 1513 | } | ||
| 1514 | } | ||
| 1515 | # | ||
| 1516 | # add image and/or text | ||
| 1517 | # copy in any embedded image first so text is on top | ||
| 1518 | # | ||
| 1519 | if ($image && (!$noimage)) { | ||
| 1520 | if ($shape eq 'bevel') { | ||
| 1521 | $imgy = int($imgy * $textfactor) | ||
| 1522 | if $vert; | ||
| 1523 | $imgx = int($imgx * $textfactor) | ||
| 1524 | unless $vert; | ||
| 1525 | } | ||
| 1526 | $btnimg->copy($image, $imgx, $imgy, 0,0, $image->width, $image->height); | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | unless ($notext) { | ||
| 1530 | if (defined($text) && ($text ne '') && | ||
| 1531 | (!$image || ($compound ne 'none'))) { | ||
| 1532 | |||
| 1533 | # | ||
| 1534 | # for bevel, move text away from bevel edge | ||
| 1535 | # | ||
| 1536 | if ($shape eq 'bevel') { | ||
| 1537 | $texty = int($texty * $textfactor) | ||
| 1538 | if $vert; | ||
| 1539 | $textx = int($textx * $textfactor) | ||
| 1540 | unless $vert; | ||
| 1541 | } | ||
| 1542 | # if ($self->cget('-underline')) { | ||
| 1543 | # $ulid = $self->_underlineText($textx, $texty, $text); | ||
| 1544 | # unshift @addons, $ulid | ||
| 1545 | # if $ulid; | ||
| 1546 | # } | ||
| 1547 | |||
| 1548 | $vert ? | ||
| 1549 | $self->_renderVerticalGdText($btnimg, $text, $textx, $texty, $textw, $gdfont) : | ||
| 1550 | $self->_renderGdText($btnimg, $text, $textx, $texty, $textw, $gdfont); | ||
| 1551 | |||
| 1552 | } | ||
| 1553 | } | ||
| 1554 | # | ||
| 1555 | # scale the image if we have explicit dimensions | ||
| 1556 | # | ||
| 1557 | my $ew = $self->{_width} || $btnimg->width; | ||
| 1558 | my $eh = $self->{_height} || $btnimg->height; | ||
| 1559 | |||
| 1560 | return ($btnimg, $self->_getBindCoords($ew, $eh)) | ||
| 1561 | unless ($self->{_width} || $self->{_height}); | ||
| 1562 | |||
| 1563 | my $scaledimg = GD::image->new($ew, $eh); | ||
| 1564 | # | ||
| 1565 | # we'll try resampling for now, maybe use resize later | ||
| 1566 | # | ||
| 1567 | $scaledimg->copyResampled($btnimg, 0, 0, 0, 0, $ew, $eh, | ||
| 1568 | $btnimg->width, $btnimg->height); | ||
| 1569 | return ($scaledimg, $self->_getBindCoords($ew, $eh)); | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | sub _getColorMap { | ||
| 1573 | my ($self, $xh, $yh, $r, $g, $b) = @_; | ||
| 1574 | # | ||
| 1575 | # if horizontal, compute white pixel row position | ||
| 1576 | # in height | ||
| 1577 | # | ||
| 1578 | my ($style, $shape, $angle, $slots) = | ||
| 1579 | ($self->{_style}, $self->{_shape}, $self->{_angle}, 15); | ||
| 1580 | |||
| 1581 | my $disperse = ($style eq 'gel') ? 1 : $self->{_dispersion} ; | ||
| 1582 | |||
| 1583 | my ($maxr, $maxg, $maxb) = ($style eq 'flat') ? | ||
| 1584 | (0, 0, 0) : (65535 - $r, 65535 - $g, 65535 - $b); | ||
| 1585 | |||
| 1586 | my $minfactor = ($shape eq 'oval') ? 0.50 : | ||
| 1587 | ($style eq 'flat') ? 1 : 0.3; | ||
| 1588 | |||
| 1589 | my ($minr, $ming, $minb) = | ||
| 1590 | (int($minfactor * $r), int($minfactor * $g), int($minfactor * $b)); | ||
| 1591 | |||
| 1592 | my ($pct, $white, $black); | ||
| 1593 | |||
| 1594 | my $offsets = ($shape eq 'oval') ? _makeOval($yh) : | ||
| 1595 | (($shape eq 'rectangle') || ($shape eq 'folio') || ($shape eq 'bevel')) ? | ||
| 1596 | _makeIndents($yh) : undef; | ||
| 1597 | # | ||
| 1598 | # compute the position of max brightness within the area | ||
| 1599 | # | ||
| 1600 | ($white, $black) = ($style eq 'gel') ? | ||
| 1601 | (($shape eq 'oval') ? | ||
| 1602 | (int(0.25 * $yh), int(0.40 * $yh)) : | ||
| 1603 | (int(0.25 * $yh), int(0.3 * $yh))) : | ||
| 1604 | (int($angle * $yh), int($angle * $yh)); | ||
| 1605 | # | ||
| 1606 | # and the area of dispersion around the angle: | ||
| 1607 | # take max of (top, bottom) from white, then | ||
| 1608 | # set the limit based on disperse | ||
| 1609 | # | ||
| 1610 | my ($bottom, $top) = (int($white - ($white * $disperse)), | ||
| 1611 | int($white + (($yh - $white) * $disperse))); | ||
| 1612 | # | ||
| 1613 | # setup methods for color and drawing based on API | ||
| 1614 | # | ||
| 1615 | my @colors = (); | ||
| 1616 | # | ||
| 1617 | # split colors between fade to white and fade to black | ||
| 1618 | # to support gel | ||
| 1619 | # | ||
| 1620 | $pct = ($_/$slots), | ||
| 1621 | push(@colors, | ||
| 1622 | [ | ||
| 1623 | $minr + int($pct * ($r - $minr)), | ||
| 1624 | $ming + int($pct * ($g - $ming)), | ||
| 1625 | $minb + int($pct * ($b - $minb)) | ||
| 1626 | ]) | ||
| 1627 | foreach (1..$slots); | ||
| 1628 | $pct = ($_/$slots), | ||
| 1629 | push(@colors, | ||
| 1630 | [ | ||
| 1631 | int($r + $maxr * $pct), | ||
| 1632 | int($g + $maxg * $pct), | ||
| 1633 | int($b + $maxb * $pct) | ||
| 1634 | ]) | ||
| 1635 | foreach (1..$slots); | ||
| 1636 | # | ||
| 1637 | # compute color increment per line from bottom to white, and white to top | ||
| 1638 | # | ||
| 1639 | my @lcolors = (); | ||
| 1640 | |||
| 1641 | if ($shape eq 'round') { | ||
| 1642 | unless ($style eq 'flat') { | ||
| 1643 | push @lcolors, _computeRGBComp->($style, | ||
| 1644 | $_ - $bottom, | ||
| 1645 | $white - $bottom, | ||
| 1646 | $r, $g, $b, | ||
| 1647 | $maxr, $maxg, $maxb, | ||
| 1648 | \@colors) | ||
| 1649 | foreach ($bottom..$white); | ||
| 1650 | |||
| 1651 | push @lcolors, _computeRGBComp->($style, | ||
| 1652 | $top - $_, | ||
| 1653 | $top - $black, | ||
| 1654 | $r, $g, $b, | ||
| 1655 | $maxr, $maxg, $maxb, | ||
| 1656 | \@colors) | ||
| 1657 | foreach ($black+1..$top); | ||
| 1658 | } | ||
| 1659 | } | ||
| 1660 | elsif ($black != $white) { | ||
| 1661 | push @lcolors, _computeRGB->('round', | ||
| 1662 | $white - $_, | ||
| 1663 | $white - $bottom, | ||
| 1664 | $r, $g, $b, | ||
| 1665 | $maxr, $maxg, $maxb, | ||
| 1666 | \@colors) | ||
| 1667 | foreach ($bottom..$white); | ||
| 1668 | |||
| 1669 | push @lcolors, _computeRGB->('round', | ||
| 1670 | $_ - $white, | ||
| 1671 | $black - $white, | ||
| 1672 | $r, $g, $b, | ||
| 1673 | -$r, -$g, -$b, | ||
| 1674 | \@colors) | ||
| 1675 | foreach ($white+1..$black); | ||
| 1676 | |||
| 1677 | push @lcolors, _computeRGB->('round', | ||
| 1678 | $_ - $black, | ||
| 1679 | $top - $black, | ||
| 1680 | $minr, $ming, $minb, | ||
| 1681 | $r, $g, $b, | ||
| 1682 | \@colors) | ||
| 1683 | foreach ($black+1..$top); | ||
| 1684 | } | ||
| 1685 | else { | ||
| 1686 | push @lcolors, _computeRGB->($style, | ||
| 1687 | $_ - $bottom, | ||
| 1688 | $white - $bottom, | ||
| 1689 | $r, $g, $b, | ||
| 1690 | $maxr, $maxg, $maxb, | ||
| 1691 | \@colors) | ||
| 1692 | foreach ($bottom..$white); | ||
| 1693 | |||
| 1694 | push @lcolors, _computeRGB->($style, | ||
| 1695 | $top - $_, | ||
| 1696 | $top - $black, | ||
| 1697 | $r, $g, $b, | ||
| 1698 | $maxr, $maxg, $maxb, | ||
| 1699 | \@colors) | ||
| 1700 | foreach ($black+1..$top); | ||
| 1701 | } | ||
| 1702 | |||
| 1703 | return (\@colors, \@lcolors, $offsets, $top, $bottom, $white, $black); | ||
| 1704 | } | ||
| 1705 | |||
| 1706 | sub _round { return (($_[0] - int($_[0])) > 0.5) ? int($_[0]) + 1 : int($_[0]); } | ||
| 1707 | |||
| 1708 | ################################################################# | ||
| 1709 | # | ||
| 1710 | # Color computations | ||
| 1711 | # | ||
| 1712 | ################################################################# | ||
| 1713 | sub _computeRGB { | ||
| 1714 | my ($style, $pos, $max, $r, $g, $b, $maxr, $maxg, $maxb, $slots) = @_; | ||
| 1715 | my $factor = | ||
| 1716 | ($style eq 'shiny') ? (1 - abs(sin(PI + (PI_OVER_2 * ($max - $pos)/$max)))) : | ||
| 1717 | ($style eq 'round') ? sin(PI_OVER_2 * $pos/$max) : | ||
| 1718 | (1 - abs(sin(PI + (PI_OVER_2 * ($max - $pos)/$max)))) ; | ||
| 1719 | |||
| 1720 | return _computeNearestColor( | ||
| 1721 | $slots, | ||
| 1722 | int($r + ($maxr * $factor)), | ||
| 1723 | int($g + ($maxg * $factor)), | ||
| 1724 | int($b + ($maxb * $factor))); | ||
| 1725 | } | ||
| 1726 | |||
| 1727 | sub _computeNearestColor { | ||
| 1728 | my $slots = shift; | ||
| 1729 | |||
| 1730 | my $closest = 0; | ||
| 1731 | my $closeval = 100000000; | ||
| 1732 | my $val = 0; | ||
| 1733 | |||
| 1734 | foreach my $slot (0..$#$slots) { | ||
| 1735 | $val = 0; | ||
| 1736 | |||
| 1737 | map $val += abs($_[$_] - $slots->[$slot][$_]), (0..2); | ||
| 1738 | |||
| 1739 | $closest = $slot, | ||
| 1740 | $closeval = $val | ||
| 1741 | if ($val < $closeval); | ||
| 1742 | } | ||
| 1743 | |||
| 1744 | return sprintf('#%04X%04X%04X', @{$slots->[$closest]}); | ||
| 1745 | } | ||
| 1746 | # | ||
| 1747 | # compute color and its brightness complement | ||
| 1748 | # | ||
| 1749 | sub _computeRGBComp { | ||
| 1750 | my ($style, $pos, $max, $r, $g, $b, $maxr, $maxg, $maxb, $slots) = @_; | ||
| 1751 | my $factor = | ||
| 1752 | ($style eq 'shiny') ? (1 - abs(sin(PI + (PI_OVER_2 * ($max - $pos)/$max)))) : | ||
| 1753 | ($style eq 'round') ? sin(PI_OVER_2 * $pos/$max) : | ||
| 1754 | (1 - abs(sin(PI + (PI_OVER_2 * ($max - $pos)/$max)))) ; | ||
| 1755 | |||
| 1756 | return _computeNearestColorComp( | ||
| 1757 | $slots, | ||
| 1758 | int($r + ($maxr * $factor)), | ||
| 1759 | int($g + ($maxg * $factor)), | ||
| 1760 | int($b + ($maxb * $factor))); | ||
| 1761 | } | ||
| 1762 | |||
| 1763 | sub _computeNearestColorComp { | ||
| 1764 | my $slots = shift; | ||
| 1765 | |||
| 1766 | my $closest = 0; | ||
| 1767 | my $closeval = 100000000; | ||
| 1768 | my $val = 0; | ||
| 1769 | |||
| 1770 | foreach my $slot (0..$#$slots) { | ||
| 1771 | $val = 0; | ||
| 1772 | |||
| 1773 | map $val += abs($_[$_] - $slots->[$slot][$_]), (0..2); | ||
| 1774 | |||
| 1775 | $closest = $slot, | ||
| 1776 | $closeval = $val | ||
| 1777 | if ($val < $closeval); | ||
| 1778 | } | ||
| 1779 | # | ||
| 1780 | # return the closest slot and its opposite | ||
| 1781 | # | ||
| 1782 | return (sprintf('#%04X%04X%04X', @{$slots->[$closest]}), | ||
| 1783 | sprintf('#%04X%04X%04X', @{$slots->[scalar @$slots - $closest]})); | ||
| 1784 | } | ||
| 1785 | ################################################################# | ||
| 1786 | # | ||
| 1787 | # Shape rendering | ||
| 1788 | # | ||
| 1789 | ################################################################# | ||
| 1790 | # | ||
| 1791 | # compute endpt delta for an oval within the width/height | ||
| 1792 | # | ||
| 1793 | sub _makeOval { | ||
| 1794 | my $h = shift; | ||
| 1795 | # | ||
| 1796 | # $h is diameter; hence the endpt delta ranges | ||
| 1797 | # between 0 and $h/2 | ||
| 1798 | # | ||
| 1799 | $h-- if ($h & 1); | ||
| 1800 | my $c = $h>>1; | ||
| 1801 | my $k = $c**2; | ||
| 1802 | my @offsets = (); | ||
| 1803 | push @offsets, $c - int(sqrt($k - (($c - $_) ** 2))) | ||
| 1804 | foreach (0..$h); | ||
| 1805 | |||
| 1806 | return \@offsets; | ||
| 1807 | } | ||
| 1808 | # | ||
| 1809 | # compute offsets for rectangle buttons w/ round shade | ||
| 1810 | # | ||
| 1811 | sub _makeIndents { | ||
| 1812 | my $h = shift; | ||
| 1813 | # | ||
| 1814 | # use a scale factor to compute circle diameter as some multiple | ||
| 1815 | # of button height | ||
| 1816 | # | ||
| 1817 | my $r = ($h * SQUARE_EDGE_FACTOR)/2; | ||
| 1818 | my $delta = cos(PI/6) * $r; | ||
| 1819 | my @offsets = (); | ||
| 1820 | my $theta = 0; | ||
| 1821 | my $theta_inc = (PI/6)/($h>>1); | ||
| 1822 | |||
| 1823 | push(@offsets, (cos($theta) * $r) - $delta), | ||
| 1824 | unshift(@offsets, $offsets[-1]), | ||
| 1825 | $theta += $theta_inc | ||
| 1826 | foreach (0..$h>>1); | ||
| 1827 | return \@offsets; | ||
| 1828 | } | ||
| 1829 | # | ||
| 1830 | # we must create a temp. Bitmap object to | ||
| 1831 | # get its bbox | ||
| 1832 | # | ||
| 1833 | sub _getBitmapSize { | ||
| 1834 | my ($self, $bitmap) = @_; | ||
| 1835 | my $bm = $self->createBitmap(0,0, '-bitmap' => $bitmap, -anchor => 'nw') | ||
| 1836 | or croak "Unable to create bitmap from $bitmap"; | ||
| 1837 | |||
| 1838 | my ($ox, $oy, $w, $h) = $self->bbox($bm); | ||
| 1839 | $self->delete($bm); | ||
| 1840 | return ($w, $h); | ||
| 1841 | } | ||
| 1842 | |||
| 1843 | sub _computeBBox { | ||
| 1844 | # | ||
| 1845 | # for a given string and font, and/or image, compute area required to hold it; | ||
| 1846 | # assumes the text is single line: NOTE!! Need to accomodate | ||
| 1847 | # wraplength!!! | ||
| 1848 | # | ||
| 1849 | my $self = shift; | ||
| 1850 | my ($compound, $bitmap, $image, $text, $shape, $orient) = ( | ||
| 1851 | $self->cget('-compound'), | ||
| 1852 | $self->cget('-bitmap'), | ||
| 1853 | $self->cget('-image'), | ||
| 1854 | $self->cget('-text'), | ||
| 1855 | $self->cget('-shape'), | ||
| 1856 | $self->cget('-orient'), | ||
| 1857 | ); | ||
| 1858 | # | ||
| 1859 | # check for image and compound setting | ||
| 1860 | # | ||
| 1861 | my ($w, $h) = $image ? ($image->width(), $image->height()) : | ||
| 1862 | $bitmap ? $self->_getBitmapSize($bitmap) : | ||
| 1863 | (0,0); | ||
| 1864 | |||
| 1865 | my ($hstrsz, $vstrsz) = (defined($text) && ($compound ne 'none')) ? | ||
| 1866 | $self->_computeTextBBox() : (0,0); | ||
| 1867 | |||
| 1868 | ($w, $h) = | ||
| 1869 | ($compound eq 'center') ? | ||
| 1870 | # | ||
| 1871 | # use larger of image or text | ||
| 1872 | # | ||
| 1873 | ((($hstrsz > $w) ? $hstrsz : $w), (($vstrsz > $h) ? $vstrsz : $h)) : | ||
| 1874 | |||
| 1875 | (($compound eq 'top') || ($compound eq 'bottom')) ? | ||
| 1876 | # | ||
| 1877 | # use larger of image or text width, and sum of heights | ||
| 1878 | # | ||
| 1879 | ((($hstrsz > $w) ? $hstrsz : $w), $h + $vstrsz + 4) : | ||
| 1880 | |||
| 1881 | (($compound eq 'left') || ($compound eq 'right')) ? | ||
| 1882 | # | ||
| 1883 | # use larger of image or text height, and sum of widths | ||
| 1884 | # | ||
| 1885 | ($w + $hstrsz + 4, (($vstrsz > $h) ? $vstrsz : $h)) : | ||
| 1886 | # | ||
| 1887 | # use image if compound eq none | ||
| 1888 | # | ||
| 1889 | ($w, $h); # compound eq 'none' | ||
| 1890 | |||
| 1891 | return (($shape eq 'bevel') || ($shape eq 'folio')) ? | ||
| 1892 | (int(1.2 * $w), $h) : ($w, $h); | ||
| 1893 | } | ||
| 1894 | |||
| 1895 | sub _computeTextBBox { | ||
| 1896 | # | ||
| 1897 | # for a given string and font, and/or image, compute area required to hold it; | ||
| 1898 | # assumes the text is single line | ||
| 1899 | # | ||
| 1900 | my $self = shift; | ||
| 1901 | |||
| 1902 | my $shape = $self->cget('-shape'); | ||
| 1903 | my $orient = $self->cget('-orient'); | ||
| 1904 | my $vert = (($shape eq 'bevel') || ($shape eq 'folio') || ($shape eq 'rectangle')) && | ||
| 1905 | ((index($orient, 'w') == 0) || (index($orient, 'e') == 0)); | ||
| 1906 | # | ||
| 1907 | # we compute for vertical, but then rotate back to horizontal for | ||
| 1908 | # our computations | ||
| 1909 | # | ||
| 1910 | if ($vert && $hasgd) { | ||
| 1911 | my $img = $self->_renderVerticalGdText($self->cget('-text')); | ||
| 1912 | return reverse $img->getBounds(); | ||
| 1913 | } | ||
| 1914 | # | ||
| 1915 | # compute the bbox by rendering in the canvas, then deleting | ||
| 1916 | # | ||
| 1917 | my $textid = $vert ? | ||
| 1918 | # | ||
| 1919 | # rearrange text to be vertically aligned | ||
| 1920 | # | ||
| 1921 | $self->createText(0, 0, | ||
| 1922 | -text => _rotateText($self->cget('-text')), | ||
| 1923 | -fill => $self->cget('-foreground'), | ||
| 1924 | # -width => $self->cget('-wraplength'), | ||
| 1925 | # -justify => $self->cget('-justify'), | ||
| 1926 | -anchor => 'sw', | ||
| 1927 | -font => $self->cget('-font')) : | ||
| 1928 | |||
| 1929 | $self->createText(0, 0, | ||
| 1930 | -text => $self->cget('-text'), | ||
| 1931 | -fill => $self->cget('-foreground'), | ||
| 1932 | -width => $self->cget('-wraplength'), | ||
| 1933 | -justify => $self->cget('-justify'), | ||
| 1934 | -anchor => 'sw', | ||
| 1935 | -font => $self->cget('-font')); | ||
| 1936 | |||
| 1937 | my ($xl, $yl, $w, $h) = $self->bbox($textid); | ||
| 1938 | |||
| 1939 | #print join(', ', $self->cget('-text'), $xl, $yl, $w, $h), "\n"; | ||
| 1940 | $w -= $xl; | ||
| 1941 | $h -= $yl; | ||
| 1942 | $self->delete($textid); | ||
| 1943 | |||
| 1944 | print "Text bbox: $xl, $yl, $w, $h\n" | ||
| 1945 | if $self->{_debug}; | ||
| 1946 | |||
| 1947 | return $vert ? ($h, $w) : ($w, $h); | ||
| 1948 | } | ||
| 1949 | # | ||
| 1950 | # compute start/end position of underline | ||
| 1951 | # NOTE: we have no way to do this since justify | ||
| 1952 | # could position us; ideally we could switch to | ||
| 1953 | # underlined font for 1 character, but canvas | ||
| 1954 | # text doesn't support that very well either | ||
| 1955 | # | ||
| 1956 | sub _underlineText { | ||
| 1957 | my $self = shift; | ||
| 1958 | my $ulch = $self->cget('-underline'); | ||
| 1959 | |||
| 1960 | croak "Invalid -underline option $ulch" | ||
| 1961 | unless (length($ulch) == 1); | ||
| 1962 | |||
| 1963 | my $pos = index($self->cget('-text'), $ulch); | ||
| 1964 | return undef | ||
| 1965 | unless ($pos >= 0); | ||
| 1966 | |||
| 1967 | return undef; | ||
| 1968 | } | ||
| 1969 | # | ||
| 1970 | # get binding coordinates | ||
| 1971 | # | ||
| 1972 | sub _getBindCoords { | ||
| 1973 | my ($self, $w, $h) = @_; | ||
| 1974 | my $shape = $self->cget('-shape'); | ||
| 1975 | |||
| 1976 | if ($shape eq 'oval') { | ||
| 1977 | my @lhtags = _computePolyPoints(0, 0, $h, $h); | ||
| 1978 | my @rhtags = _computePolyPoints($w - $h, 0, $w, $h); | ||
| 1979 | return [ @lhtags[6..19], @rhtags[18..23], @rhtags[0..7] ]; | ||
| 1980 | } | ||
| 1981 | |||
| 1982 | my $orient = $self->cget('-orient'); | ||
| 1983 | my $vert = (($shape eq 'bevel') || ($shape eq 'folio')) && | ||
| 1984 | ((substr($orient, 0, 1) eq 'w') || (substr($orient, 0, 1) eq 'e')); | ||
| 1985 | |||
| 1986 | return [ | ||
| 1987 | ($shape eq 'round') ? _computePolyPoints(0, 0, $w, $h) : | ||
| 1988 | ($shape eq 'rectangle') ? _drawRectangle($w,$h) : | ||
| 1989 | ($shape eq 'bevel') ? _drawBevel($w,$h, 6, $orient) : | ||
| 1990 | # ($shape eq 'folio') | ||
| 1991 | _drawFolio($w,$h, 6, $orient) ]; | ||
| 1992 | } | ||
| 1993 | |||
| 1994 | sub _bindFromImage { | ||
| 1995 | my ($self, $w, $h, $xscale, $yscale) = @_; | ||
| 1996 | # | ||
| 1997 | # embed image in center of canvas, then draw transparent overlay | ||
| 1998 | # to tag for binding | ||
| 1999 | # | ||
| 2000 | my @tags = (); | ||
| 2001 | my $shape = $self->cget('-shape'); | ||
| 2002 | if (($self->cget('-style') eq 'image') && (ref $shape) && (ref $shape eq 'ARRAY')) { | ||
| 2003 | # | ||
| 2004 | # binding coords are provided | ||
| 2005 | # | ||
| 2006 | @tags = @$shape; | ||
| 2007 | } | ||
| 2008 | elsif ($shape eq 'round') { | ||
| 2009 | push @tags, $self->createPolygon(_computePolyPoints(0, 0, $w, $h), | ||
| 2010 | -fill => 'white', | ||
| 2011 | -stipple => 'transparent'); | ||
| 2012 | } | ||
| 2013 | elsif (($shape eq 'rectangle') || ($shape eq 'bevel') || ($shape eq 'folio')) { | ||
| 2014 | my $orient = $self->cget('-orient'); | ||
| 2015 | my $vert = ((index($orient, 'w') == 0) || (index($orient, 'e') == 0)); | ||
| 2016 | |||
| 2017 | my @endpts = | ||
| 2018 | ($shape eq 'rectangle') ? _drawRectangle($w,$h) : | ||
| 2019 | ($shape eq 'bevel') ? _drawBevel($w, $h, 6, $orient) : | ||
| 2020 | _drawFolio($w, $h, 6, $orient); | ||
| 2021 | |||
| 2022 | @endpts = $self->_rotateShape($w, @endpts) | ||
| 2023 | if $vert; | ||
| 2024 | |||
| 2025 | push @tags, $self->createPolygon(@endpts, | ||
| 2026 | -fill => 'white', | ||
| 2027 | -stipple => 'transparent'); | ||
| 2028 | } | ||
| 2029 | else { # ($shape eq 'oval') | ||
| 2030 | # | ||
| 2031 | # compute offsets to the rectangle, and arc info for the ends | ||
| 2032 | # arcs overlap the rectangle; its more than needed, but gets the job done | ||
| 2033 | # | ||
| 2034 | push @tags, | ||
| 2035 | $self->createRectangle($h, 0, $w - $h, $h, | ||
| 2036 | -outline => '', | ||
| 2037 | -fill => 'white', | ||
| 2038 | -stipple => 'transparent'), | ||
| 2039 | $self->createPolygon(_computePolyPoints(0, 0, $h, $h), | ||
| 2040 | -fill => 'white', | ||
| 2041 | -stipple => 'transparent'), | ||
| 2042 | $self->createPolygon(_computePolyPoints($w - $h, 0, $w, $h), | ||
| 2043 | -fill => 'white', | ||
| 2044 | -stipple => 'transparent'); | ||
| 2045 | } | ||
| 2046 | # | ||
| 2047 | # returns a transparent group bound to our events | ||
| 2048 | # | ||
| 2049 | $self->{_bind_group} = $self->createGroup(0,0, -members => \@tags); | ||
| 2050 | # | ||
| 2051 | # move everybody a tad | ||
| 2052 | # | ||
| 2053 | $self->move($self->{_idle_group}, 2, 2); | ||
| 2054 | $self->move($self->{_active_group}, 2, 2); | ||
| 2055 | $self->move($self->{_bind_group}, 2, 2); | ||
| 2056 | # | ||
| 2057 | # bind to bind group only | ||
| 2058 | # | ||
| 2059 | $self->SUPER::bind($self->{_bind_group}, '<Enter>', sub { $self->_OnEnter; }); | ||
| 2060 | $self->SUPER::bind($self->{_bind_group}, '<Leave>', sub { $self->_OnLeave; }); | ||
| 2061 | $self->SUPER::bind($self->{_bind_group}, '<ButtonPress-1>', sub { $self->_OnPress; }); | ||
| 2062 | $self->SUPER::bind($self->{_bind_group}, '<ButtonRelease-1>', sub { $self->_OnRelease; }); | ||
| 2063 | $self->SUPER::bind($self->{_bind_group}, '<ButtonRelease-1>', sub { $self->_OnRelease; }); | ||
| 2064 | # | ||
| 2065 | # keyboard invokes when we've got focus; | ||
| 2066 | # also need to check for any add'l key binds ? | ||
| 2067 | # | ||
| 2068 | $self->SUPER::bind($self->{_bind_group},'<space>', 'invoke'); | ||
| 2069 | $self->SUPER::bind($self->{_bind_group},'<Return>', 'invoke'); | ||
| 2070 | # | ||
| 2071 | # make sure bind group is always on top, and start | ||
| 2072 | # with idle on top of active | ||
| 2073 | # | ||
| 2074 | $self->lower($self->{_active_group}, $self->{_bind_group}); | ||
| 2075 | $self->lower($self->{_idle_group}, $self->{_bind_group}); | ||
| 2076 | $self->lower($self->{_active_group}, $self->{_idle_group}); | ||
| 2077 | |||
| 2078 | return $self; | ||
| 2079 | } | ||
| 2080 | # | ||
| 2081 | # since transparent can't be used with createOval/createArc | ||
| 2082 | # on Win32, we'll dummy up a polygon that nearly fits | ||
| 2083 | # | ||
| 2084 | sub _computePolyPoints { | ||
| 2085 | my ($xl,$yl, $xh, $yh) = @_; | ||
| 2086 | my $r = ($xh - $xl) >> 1; | ||
| 2087 | my $i = 0; | ||
| 2088 | my @pts = (); | ||
| 2089 | push @pts, $xl + $r + int($r * $polyfactors[$i++]), | ||
| 2090 | $yl + $r + int($r * $polyfactors[$i++]) | ||
| 2091 | while ($i < scalar @polyfactors); | ||
| 2092 | return @pts; | ||
| 2093 | } | ||
| 2094 | |||
| 2095 | # | ||
| 2096 | # render text for GD capture image | ||
| 2097 | # | ||
| 2098 | sub _renderGdText { | ||
| 2099 | my ($self, $image, $text, $x, $y, $textw, $gdfont) = @_; | ||
| 2100 | |||
| 2101 | my $font = ($self->{_font}->Family eq 'MS Sans Serif') ? | ||
| 2102 | 'Microsoft Sans Serif' : $self->{_font}->Family ; | ||
| 2103 | my $size = $self->{_font}->Size(); | ||
| 2104 | |||
| 2105 | if ($gdfont) { | ||
| 2106 | ($font, $size) = (ref $gdfont eq 'CODE') ? | ||
| 2107 | $gdfont->($self->{_font}->actual()) : | ||
| 2108 | ($gdfont, $size); | ||
| 2109 | } | ||
| 2110 | elsif ($hasw32n2f) { | ||
| 2111 | $font = get_ttf_abs_path($font); | ||
| 2112 | } | ||
| 2113 | |||
| 2114 | $size = -$size if ($size < 0); | ||
| 2115 | # | ||
| 2116 | # compute a GD std. font based on weight/slant/size | ||
| 2117 | # NOTE: since Tk returns pixel size, *not* point size, | ||
| 2118 | # we have to approximate the right pt size for GD | ||
| 2119 | # | ||
| 2120 | my ($w, $texth, $spacew); | ||
| 2121 | if ($font) { | ||
| 2122 | ($size, $w, $texth, $spacew) = $self->_computeGdFontSize($font, $text); | ||
| 2123 | # print join(', ', $text, $w, $textw, $texth, $size), "\n"; | ||
| 2124 | } | ||
| 2125 | else { | ||
| 2126 | $font = ($size <= 8) ? GD::Font->Small : | ||
| 2127 | ($size <= 16) ? GD::Font->Large : | ||
| 2128 | GD::Font->Giant; | ||
| 2129 | |||
| 2130 | $size = undef; | ||
| 2131 | } | ||
| 2132 | # | ||
| 2133 | # allocate text color | ||
| 2134 | # | ||
| 2135 | my $rgb = $self->rgb($self->{_foreground}); | ||
| 2136 | my $textcolor = $image->colorAllocate(@$rgb); | ||
| 2137 | # | ||
| 2138 | # For some reason, stringTTF can't render whitespace, so | ||
| 2139 | # we have to pull apart the string and render each | ||
| 2140 | # piece one at a time | ||
| 2141 | # we should also keep track of any leading/trailing spaces | ||
| 2142 | # used to adjust the padding | ||
| 2143 | # | ||
| 2144 | my @p = split (/\s+/, $text); | ||
| 2145 | my $offs = $x - ($w >> 1); | ||
| 2146 | $offs += (length($1) * $spacew) | ||
| 2147 | if ($text=~/^(\s+)/); | ||
| 2148 | foreach (@p) { | ||
| 2149 | my @bb = $image->stringTTF($textcolor, $font, $size, 0, $offs, $y + ($texth >> 1), $_); | ||
| 2150 | my $tw = $bb[2] - $bb[0]; | ||
| 2151 | $offs += $tw + $spacew; # computed space width | ||
| 2152 | } | ||
| 2153 | return $self; | ||
| 2154 | } | ||
| 2155 | |||
| 2156 | # | ||
| 2157 | # render vertical text via GD | ||
| 2158 | # | ||
| 2159 | sub _renderVerticalGdText { | ||
| 2160 | my $self = shift; | ||
| 2161 | my ($image, $text, $x, $y, $textw, $gdfont) = @_; | ||
| 2162 | ($text, $image) = ($image, $text) | ||
| 2163 | unless defined $text; | ||
| 2164 | |||
| 2165 | my $font = ($self->{_font}->Family eq 'MS Sans Serif') ? | ||
| 2166 | 'Microsoft Sans Serif' : $self->{_font}->Family ; | ||
| 2167 | my $size = $self->{_font}->Size(); | ||
| 2168 | |||
| 2169 | if ($gdfont) { | ||
| 2170 | ($font, $size) = (ref $gdfont eq 'CODE') ? | ||
| 2171 | $gdfont->($self->{_font}->actual()) : | ||
| 2172 | ($gdfont, $size); | ||
| 2173 | } | ||
| 2174 | elsif ($hasw32n2f) { | ||
| 2175 | $font = get_ttf_abs_path($font); | ||
| 2176 | } | ||
| 2177 | |||
| 2178 | $size = -$size if ($size < 0); | ||
| 2179 | # | ||
| 2180 | # compute a GD std. font based on weight/slant/size | ||
| 2181 | # NOTE: since Tk returns pixel size, *not* point size, | ||
| 2182 | # we have to approximate the right pt size for GD | ||
| 2183 | # | ||
| 2184 | my ($w, $texth); | ||
| 2185 | $font = ($size <= 8) ? GD::Font->Small : | ||
| 2186 | ($size <= 16) ? GD::Font->Large : | ||
| 2187 | GD::Font->Giant | ||
| 2188 | unless $font; | ||
| 2189 | |||
| 2190 | ($size, $w, $texth) = $self->_computeGdFontSize($font, $text); | ||
| 2191 | |||
| 2192 | $size = undef | ||
| 2193 | unless $font; | ||
| 2194 | # | ||
| 2195 | # create image to write the text into | ||
| 2196 | # | ||
| 2197 | my $txtimage = GD::Image->new($w, $texth); | ||
| 2198 | my $transparent = $txtimage->colorAllocate(1, 1, 1); | ||
| 2199 | $txtimage->transparent($transparent); | ||
| 2200 | |||
| 2201 | $txtimage->filledRectangle(0,0,$w - 1,$texth - 1, $transparent); | ||
| 2202 | |||
| 2203 | my $rgb = $self->rgb($self->{_foreground}); | ||
| 2204 | my $textcolor = $txtimage->colorAllocate(@$rgb); | ||
| 2205 | |||
| 2206 | my $gdtext = GD::Text::Wrap->new($txtimage, | ||
| 2207 | color => $textcolor, | ||
| 2208 | text => $text, | ||
| 2209 | width => $w + 100000, | ||
| 2210 | preserve_nl => 1, # ($self->{_wraplength} == 0), | ||
| 2211 | align => $self->{_justify}, | ||
| 2212 | ); | ||
| 2213 | $gdtext->set(width => $self->{_wraplength}) | ||
| 2214 | if $self->{_wraplength}; | ||
| 2215 | $gdtext->set_font($font, $size); | ||
| 2216 | # | ||
| 2217 | # need to compute height to properly align the text | ||
| 2218 | # | ||
| 2219 | my $orient = $self->cget('-orient'); | ||
| 2220 | $gdtext->draw(0, 0); | ||
| 2221 | |||
| 2222 | $txtimage = (index($orient, 'e') == 0) ? | ||
| 2223 | $txtimage->copyRotate90() : | ||
| 2224 | $txtimage->copyRotate270(); | ||
| 2225 | |||
| 2226 | return $txtimage | ||
| 2227 | unless defined $image; | ||
| 2228 | |||
| 2229 | $image->copy($txtimage, $y, 14, 0,0, $txtimage->getBounds()); | ||
| 2230 | return $self; | ||
| 2231 | } | ||
| 2232 | |||
| 2233 | sub _computeGdFontSize { | ||
| 2234 | my ($self, $font, $text) = @_; | ||
| 2235 | my $needs = $self->{_font}->measure($text); | ||
| 2236 | # | ||
| 2237 | # just use brute force; we don't really have any | ||
| 2238 | # slick binary search solutions here | ||
| 2239 | # | ||
| 2240 | my ($size, $lastsize) = (12, 12); # assume 12 pt to start | ||
| 2241 | my $delta = 1000000; | ||
| 2242 | my ($w, $texth); | ||
| 2243 | my $p = $text; | ||
| 2244 | $p=~s/\s+//g; | ||
| 2245 | my $tlen = length($p); | ||
| 2246 | my $spacew = 0; | ||
| 2247 | while (1) { | ||
| 2248 | |||
| 2249 | my @bb = GD::Image->stringTTF(0, $font, $size, 0, 0, 0, $p); | ||
| 2250 | $w = ($bb[2] - $bb[0]); | ||
| 2251 | $texth = $bb[1] - $bb[7]; | ||
| 2252 | # | ||
| 2253 | # add add'l width for spaces, using a computed space width | ||
| 2254 | # | ||
| 2255 | $spacew = int($w/$tlen); | ||
| 2256 | $w += ((length($text) - $tlen) * $spacew); | ||
| 2257 | |||
| 2258 | return ($size, $w, $texth, $spacew) if ($w == $needs); | ||
| 2259 | |||
| 2260 | if ($w > $needs) { | ||
| 2261 | # | ||
| 2262 | # if we've gotten bigger, then return prior | ||
| 2263 | # | ||
| 2264 | return ($lastsize, $w, $texth, $spacew) | ||
| 2265 | if ($w - $needs >= $delta); | ||
| 2266 | |||
| 2267 | $lastsize = $size; | ||
| 2268 | $size--; | ||
| 2269 | $delta = $w - $needs; | ||
| 2270 | } | ||
| 2271 | else { | ||
| 2272 | return ($lastsize, $w, $texth, $spacew) | ||
| 2273 | if ($needs - $w >= $delta); | ||
| 2274 | |||
| 2275 | $lastsize = $size; | ||
| 2276 | $size++; | ||
| 2277 | $delta = $needs - $w; | ||
| 2278 | } | ||
| 2279 | } | ||
| 2280 | return ($size, $w, $texth, $spacew); | ||
| 2281 | } | ||
| 2282 | |||
| 2283 | # | ||
| 2284 | # for vertical orientation wo/ GD support, we must convert text into | ||
| 2285 | # vertical format | ||
| 2286 | # | ||
| 2287 | sub _rotateText { | ||
| 2288 | my $text = shift; | ||
| 2289 | my @segments = split /\n/, $text; | ||
| 2290 | my $maxchars = 0; | ||
| 2291 | foreach (@segments) { | ||
| 2292 | $maxchars = length($_) | ||
| 2293 | if ($maxchars < length($_)); | ||
| 2294 | } | ||
| 2295 | |||
| 2296 | $segments[$_] .= (' ' x ($maxchars - length($segments[$_]))) | ||
| 2297 | foreach (0..$#segments); | ||
| 2298 | |||
| 2299 | my @lines = ('') x $maxchars; | ||
| 2300 | my @chars; | ||
| 2301 | foreach my $segment (@segments) { | ||
| 2302 | @chars = split('', $segment); | ||
| 2303 | $lines[$_] .= $chars[$_] . ' ' | ||
| 2304 | foreach (0..$#chars); | ||
| 2305 | } | ||
| 2306 | return join("\n", @lines); | ||
| 2307 | } | ||
| 2308 | # | ||
| 2309 | # rotate the input shape +/- 90 degs, or flip it | ||
| 2310 | # | ||
| 2311 | sub _rotateShape { | ||
| 2312 | my ($self, $w, @endpts) = @_; | ||
| 2313 | my $orient = $self->cget('-orient'); | ||
| 2314 | $orient = substr($orient, 0, 1); | ||
| 2315 | return ($orient eq 'w') ? | ||
| 2316 | ($endpts[3], $w - $endpts[2], | ||
| 2317 | $endpts[5], $w - $endpts[4], | ||
| 2318 | $endpts[7], $w - $endpts[6], | ||
| 2319 | $endpts[1], $w - $endpts[0]) : | ||
| 2320 | |||
| 2321 | ($orient eq 'e') ? | ||
| 2322 | ($endpts[5], $w - $endpts[4], | ||
| 2323 | $endpts[3], $w - $endpts[2], | ||
| 2324 | $endpts[1], $w - $endpts[0], | ||
| 2325 | $endpts[7], $w - $endpts[6]) : | ||
| 2326 | |||
| 2327 | ($orient eq 's') ? | ||
| 2328 | ($endpts[6], $endpts[7], | ||
| 2329 | $endpts[4], $endpts[5], | ||
| 2330 | $endpts[2], $endpts[3], | ||
| 2331 | $endpts[0], $endpts[1]) : | ||
| 2332 | # | ||
| 2333 | # the original | ||
| 2334 | # | ||
| 2335 | @endpts; | ||
| 2336 | } | ||
| 2337 | |||
| 2338 | 1; |