| 1 |
package Tk::ColorEditor; |
| 2 |
|
| 3 |
use vars qw($VERSION $SET_PALETTE); |
| 4 |
$VERSION = '4.014'; |
| 5 |
|
| 6 |
use Tk qw(lsearch Ev); |
| 7 |
use Tk::Toplevel; |
| 8 |
use base qw(Tk::Toplevel); |
| 9 |
use Tk::widgets qw(Pixmap); |
| 10 |
Construct Tk::Widget 'ColorEditor'; |
| 11 |
|
| 12 |
use Tk::Dialog; |
| 13 |
use Tk::Pretty; |
| 14 |
|
| 15 |
use Tk::ColorSelect (); |
| 16 |
use Tk::ColorDialog (); |
| 17 |
|
| 18 |
BEGIN { $SET_PALETTE = 'Set Palette' }; |
| 19 |
|
| 20 |
use subs qw(color_space hsvToRgb rgbToHsv); |
| 21 |
|
| 22 |
# ColorEditor public methods. |
| 23 |
|
| 24 |
sub add_menu_item |
| 25 |
{ |
| 26 |
my $objref = shift; |
| 27 |
my $value; |
| 28 |
foreach $value (@_) |
| 29 |
{ |
| 30 |
if ($value eq 'SEP') |
| 31 |
{ |
| 32 |
$objref->{'mcm2'}->separator; |
| 33 |
} |
| 34 |
else |
| 35 |
{ |
| 36 |
$objref->{'mcm2'}->command( -label => $value, |
| 37 |
-command => [ 'configure', $objref, '-highlight' => $value ] ); |
| 38 |
push @{$objref->{'highlight_list'}}, $value; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
sub set_title |
| 44 |
{ |
| 45 |
my ($w) = @_; |
| 46 |
my $t = $w->{Configure}{'-title'} || '' ; |
| 47 |
my $h = $w->{Configure}{'-highlight'} || ''; |
| 48 |
$w->SUPER::title("$t $h Color Editor"); |
| 49 |
} |
| 50 |
|
| 51 |
sub highlight |
| 52 |
{ |
| 53 |
my ($w,$h) = @_; |
| 54 |
if (@_ > 1) |
| 55 |
{ |
| 56 |
$w->{'update'}->configure( -text => "Apply $h Color" ); |
| 57 |
my $state = ($h eq 'background') ? 'normal' : 'disabled'; |
| 58 |
$w->{'palette'}->entryconfigure( $SET_PALETTE, -state => $state); |
| 59 |
$w->{'highlight'} = $h; |
| 60 |
$w->configure(-color => $w->Palette->{$h}); |
| 61 |
$w->set_title; |
| 62 |
} |
| 63 |
return $w->{'highlight'}; |
| 64 |
} |
| 65 |
|
| 66 |
sub title |
| 67 |
{ |
| 68 |
my ($w,$val) = @_; |
| 69 |
$w->set_title if (@_ > 1); |
| 70 |
return $w->{Configure}{'-title'}; |
| 71 |
} |
| 72 |
|
| 73 |
sub delete_menu_item |
| 74 |
{ |
| 75 |
my $objref = shift; |
| 76 |
my $value; |
| 77 |
foreach $value (@_) |
| 78 |
{ |
| 79 |
$objref->{'mcm2'}->delete($value); |
| 80 |
my $list_ord = $value =~ /\d+/ ? $value : lsearch($objref->{'highlight_list'}, $value); |
| 81 |
splice(@{$objref->{'highlight_list'}}, $list_ord, 1) if $list_ord != -1; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
sub delete_widgets { |
| 86 |
|
| 87 |
# Remove widgets from consideration by the color configurator. |
| 88 |
# $widgets_ref points to widgets previously added via `configure'. |
| 89 |
|
| 90 |
my($objref, $widgets_ref) = @_; |
| 91 |
|
| 92 |
my($i, $found, $r1, $r2, @wl) = (0, 0, 0, 0, @{$objref->cget(-widgets)}); |
| 93 |
foreach $r1 (@{$widgets_ref}) { |
| 94 |
$i = -1; |
| 95 |
$found = 0; |
| 96 |
foreach $r2 (@wl) { |
| 97 |
$i++; |
| 98 |
next if $r1 != $r2; |
| 99 |
$found = 1; |
| 100 |
last; |
| 101 |
} |
| 102 |
splice(@wl, $i, 1) if $found; |
| 103 |
} |
| 104 |
$objref->configure(-widgets => [@wl]); |
| 105 |
|
| 106 |
} # end delete_widgets |
| 107 |
|
| 108 |
sub ApplyDefault |
| 109 |
{ |
| 110 |
my($objref) = @_; |
| 111 |
my $cb = $objref->cget('-command'); |
| 112 |
my $h; |
| 113 |
foreach $h (@{$objref->{'highlight_list'}}) |
| 114 |
{ |
| 115 |
next if $h =~ /TEAR_SEP|SEP/; |
| 116 |
$cb->Call($h); |
| 117 |
die unless (defined $cb); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
sub Populate |
| 122 |
{ |
| 123 |
|
| 124 |
# ColorEditor constructor. |
| 125 |
|
| 126 |
my($cw, $args) = @_; |
| 127 |
|
| 128 |
$cw->SUPER::Populate($args); |
| 129 |
$cw->withdraw; |
| 130 |
|
| 131 |
my $color_space = 'hsb'; # rgb, cmy, hsb |
| 132 |
my(@highlight_list) = qw( |
| 133 |
TEAR_SEP |
| 134 |
foreground background SEP |
| 135 |
activeForeground activeBackground SEP |
| 136 |
highlightColor highlightBackground SEP |
| 137 |
selectForeground selectBackground SEP |
| 138 |
disabledForeground insertBackground selectColor troughColor |
| 139 |
); |
| 140 |
|
| 141 |
# Create the Usage Dialog; |
| 142 |
|
| 143 |
my $usage = $cw->Dialog( '-title' => 'ColorEditor Usage', |
| 144 |
-justify => 'left', |
| 145 |
-wraplength => '6i', |
| 146 |
-text => "The Colors menu allows you to:\n\nSelect a color attribute such as \"background\" that you wish to colorize. Click on \"Apply\" to update that single color attribute.\n\nSelect one of three color spaces. All color spaces display a color value as a hexadecimal number under the oval color swatch that can be directly supplied on widget commands.\n\nApply Tk's default color scheme to the application. Useful if you've made a mess of things and want to start over!\n\nChange the application's color palette. Make sure \"background\" is selected as the color attribute, find a pleasing background color to apply to all current and future application widgets, then select \"Set Palette\".", |
| 147 |
); |
| 148 |
|
| 149 |
# Create the menu bar at the top of the window for the File, Colors |
| 150 |
# and Help menubuttons. |
| 151 |
|
| 152 |
my $m0 = $cw->Frame(-relief => 'raised', -borderwidth => 2); |
| 153 |
$m0->pack(-side => 'top', -fill => 'x'); |
| 154 |
my $mf = $m0->Menubutton( |
| 155 |
-text => 'File', |
| 156 |
-underline => 0, |
| 157 |
-bd => 1, |
| 158 |
-relief => 'raised', |
| 159 |
); |
| 160 |
$mf->pack(-side => 'left'); |
| 161 |
my $close_command = [sub {shift->withdraw}, $cw]; |
| 162 |
$mf->command( |
| 163 |
-label => 'Close', |
| 164 |
-underline => 0, |
| 165 |
-command => $close_command, |
| 166 |
-accelerator => 'Ctrl-w', |
| 167 |
); |
| 168 |
$cw->bind('<Control-Key-w>' => $close_command); |
| 169 |
$cw->protocol(WM_DELETE_WINDOW => $close_command); |
| 170 |
|
| 171 |
my $mc = $m0->Menubutton( |
| 172 |
-text => 'Colors', |
| 173 |
-underline => 0, |
| 174 |
-bd => 1, |
| 175 |
-relief => 'raised', |
| 176 |
); |
| 177 |
$mc->pack(-side => 'left'); |
| 178 |
my $color_attributes = 'Color Attributes'; |
| 179 |
$mc->cascade(-label => $color_attributes, -underline => 6); |
| 180 |
$mc->separator; |
| 181 |
|
| 182 |
$mc->command( |
| 183 |
-label => 'Apply Default Colors', |
| 184 |
-underline => 6, |
| 185 |
-command => ['ApplyDefault',$cw] |
| 186 |
); |
| 187 |
$mc->separator; |
| 188 |
$mc->command( |
| 189 |
-label => $SET_PALETTE, |
| 190 |
-underline => 0, |
| 191 |
-command => sub { $cw->setPalette($cw->cget('-color'))} |
| 192 |
); |
| 193 |
|
| 194 |
my $m1 = $mc->cget(-menu); |
| 195 |
|
| 196 |
my $mcm2 = $m1->Menu; |
| 197 |
$m1->entryconfigure($color_attributes, -menu => $mcm2); |
| 198 |
my $mh = $m0->Menubutton( |
| 199 |
-text => 'Help', |
| 200 |
-underline => 0, |
| 201 |
-bd => 1, |
| 202 |
-relief => 'raised', |
| 203 |
); |
| 204 |
$mh->pack(-side => 'right'); |
| 205 |
$mh->command( |
| 206 |
-label => 'Usage', |
| 207 |
-underline => 0, |
| 208 |
-command => [sub {shift->Show}, $usage], |
| 209 |
); |
| 210 |
|
| 211 |
# Create the Apply button. |
| 212 |
|
| 213 |
my $bot = $cw->Frame(-relief => 'raised', -bd => 2); |
| 214 |
$bot->pack(-side => 'bottom', -fill =>'x'); |
| 215 |
my $update = $bot->Button( |
| 216 |
-command => [ |
| 217 |
sub { |
| 218 |
my ($objref) = @_; |
| 219 |
$objref->Callback(-command => ($objref->{'highlight'}, $objref->cget('-color'))); |
| 220 |
$cw->{'done'} = 1; |
| 221 |
}, $cw, |
| 222 |
], |
| 223 |
); |
| 224 |
$update->pack(-pady => 1, -padx => '0.25c'); |
| 225 |
|
| 226 |
# Create the listbox that holds all of the color names in rgb.txt, if an |
| 227 |
# rgb.txt file can be found. |
| 228 |
|
| 229 |
my $middle = $cw->ColorSelect(-relief => 'raised', -borderwidth => 2); |
| 230 |
$middle->pack(-side => 'top', -fill => 'both'); |
| 231 |
# Create the status window. |
| 232 |
|
| 233 |
my $status = $cw->Toplevel; |
| 234 |
$status->withdraw; |
| 235 |
$status->geometry('+0+0'); |
| 236 |
my $status_l = $status->Label(-width => 50, -anchor => 'w'); |
| 237 |
$status_l->pack(-side => 'top'); |
| 238 |
|
| 239 |
$cw->{'highlight_list'} = [@highlight_list]; |
| 240 |
$cw->{'mcm2'} = $mcm2; |
| 241 |
|
| 242 |
foreach (@highlight_list) |
| 243 |
{ |
| 244 |
next if /^TEAR_SEP$/; |
| 245 |
$cw->add_menu_item($_); |
| 246 |
} |
| 247 |
|
| 248 |
$cw->{'updating'} = 0; |
| 249 |
$cw->{'pending'} = 0; |
| 250 |
$cw->{'Status'} = $status; |
| 251 |
$cw->{'Status_l'} = $status_l; |
| 252 |
$cw->{'update'} = $update; |
| 253 |
$cw->{'gwt_depth'} = 0; |
| 254 |
$cw->{'palette'} = $mc; |
| 255 |
|
| 256 |
my $pixmap = $cw->Pixmap('-file' => Tk->findINC('ColorEdit.xpm')); |
| 257 |
$cw->Icon(-image => $pixmap); |
| 258 |
|
| 259 |
$cw->ConfigSpecs( |
| 260 |
DEFAULT => [$middle], |
| 261 |
-widgets => ['PASSIVE', undef, undef, |
| 262 |
[$cw->parent->Descendants]], |
| 263 |
-display_status => ['PASSIVE', undef, undef, 0], |
| 264 |
'-title' => ['METHOD', undef, undef, ''], |
| 265 |
-command => ['CALLBACK', undef, undef, ['set_colors',$cw]], |
| 266 |
'-highlight' => ['METHOD', undef, undef, 'background'], |
| 267 |
-cursor => ['DESCENDANTS', 'cursor', 'Cursor', 'left_ptr'], |
| 268 |
); |
| 269 |
|
| 270 |
} # end Populate, ColorEditor constructor |
| 271 |
|
| 272 |
sub Show { |
| 273 |
|
| 274 |
my($objref, @args) = @_; |
| 275 |
|
| 276 |
Tk::ColorDialog::Show(@_); |
| 277 |
|
| 278 |
} # end show |
| 279 |
|
| 280 |
# ColorEditor default configurator procedure - can be redefined by the |
| 281 |
# application. |
| 282 |
|
| 283 |
sub set_colors { |
| 284 |
|
| 285 |
# Configure all the widgets in $widgets for attribute $type and color |
| 286 |
# $color. If $color is undef then reset all colors |
| 287 |
# to the Tk defaults. |
| 288 |
|
| 289 |
my($objref, $type, $color) = @_; |
| 290 |
my $display = $objref->cget('-display_status'); |
| 291 |
|
| 292 |
$objref->{'Status'}->title("Configure $type"); |
| 293 |
$objref->{'Status'}->deiconify if $display; |
| 294 |
my $widget; |
| 295 |
my $reset = !defined($color); |
| 296 |
|
| 297 |
foreach $widget (@{$objref->cget('-widgets')}) { |
| 298 |
if ($display) { |
| 299 |
$objref->{'Status_l'}->configure( |
| 300 |
-text => 'WIDGET: ' . $widget->PathName |
| 301 |
); |
| 302 |
$objref->update; |
| 303 |
} |
| 304 |
eval {local $SIG{'__DIE__'}; $color = ($widget->configure("-\L${type}"))[3]} if $reset; |
| 305 |
eval {local $SIG{'__DIE__'}; $widget->configure("-\L${type}" => $color)}; |
| 306 |
} |
| 307 |
|
| 308 |
$objref->{'Status'}->withdraw if $display; |
| 309 |
|
| 310 |
} # end set_colors |
| 311 |
|
| 312 |
# ColorEditor private methods. |
| 313 |
|
| 314 |
1; |
| 315 |
|
| 316 |
__END__ |
| 317 |
|