3 # Color selection dialog for platforms that do not support a
4 # standard color selection dialog.
6 # RCS: @(#) $Id: clrpick.tcl,v 1.22 2006/03/17 11:13:15 patthoyts Exp $
8 # Copyright (c) 1996 Sun Microsystems, Inc.
10 # See the file "license.terms" for information on usage and redistribution
11 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 # (1): Find out how many free colors are left in the colormap and
16 # don't allocate too many colors.
17 # (2): Implement HSV color selection.
20 # Make sure namespaces exist
21 namespace eval ::tk {}
22 namespace eval ::tk::dialog {}
23 namespace eval ::tk::dialog::color {
24 namespace import
::tk::msgcat::*
27 # ::tk::dialog::color:: --
29 # Create a color dialog and let the user choose a color. This function
30 # should not be called directly. It is called by the tk_chooseColor
31 # function when a native color selector widget does not exist
33 proc ::tk::dialog::color:: {args
} {
35 set dataName __tk__color
36 upvar ::tk::dialog::color::$dataName data
39 # The lines variables track the start and end indices of the line
40 # elements in the colorbar canvases.
41 set data
(lines
,red
,start
) 0
42 set data
(lines
,red
,last
) -1
43 set data
(lines
,green
,start
) 0
44 set data
(lines
,green
,last
) -1
45 set data
(lines
,blue
,start
) 0
46 set data
(lines
,blue
,last
) -1
48 # This is the actual number of lines that are drawn in each color strip.
49 # Note that the bars may be of any width.
50 # However, NUM_COLORBARS must be a number that evenly divides 256.
51 # Such as 256, 128, 64, etc.
52 set data
(NUM_COLORBARS
) 16
54 # BARS_WIDTH is the number of pixels wide the color bar portion of the
55 # canvas is. This number must be a multiple of NUM_COLORBARS
56 set data
(BARS_WIDTH
) 160
58 # PLGN_WIDTH is the number of pixels wide of the triangular selection
59 # polygon. This also results in the definition of the padding on the
60 # left and right sides which is half of PLGN_WIDTH. Make this number even.
61 set data
(PLGN_HEIGHT
) 10
63 # PLGN_HEIGHT is the height of the selection polygon and the height of the
64 # selection rectangle at the bottom of the color bar. No restrictions.
65 set data
(PLGN_WIDTH
) 10
67 Config
$dataName $args
70 set sc
[winfo screen
$data(-parent)]
71 set winExists
[winfo exists
$w]
72 if {!$winExists ||
$sc ne
[winfo screen
$w]} {
76 toplevel $w -class TkColorDialog
-screen $sc
80 # Dialog boxes should be transient with respect to their parent,
81 # so that they will always stay on top of their parent window. However,
82 # some window managers will create the window as withdrawn if the parent
83 # window is withdrawn or iconified. Combined with the grab we put on the
84 # window, this can hang the entire application. Therefore we only make
85 # the dialog transient if the parent is viewable.
87 if {[winfo viewable
[winfo toplevel $data(-parent)]] } {
88 wm transient
$w $data(-parent)
91 # 5. Withdraw the window, then update all the geometry information
92 # so we know how big it wants to be, then center the window in the
93 # display and de-iconify it.
95 ::tk::PlaceWindow $w widget
$data(-parent)
96 wm title
$w $data(-title)
98 # 6. Set a grab and claim the focus too.
100 ::tk::SetFocusGrab $w $data(okBtn
)
102 # 7. Wait for the user to respond, then restore the focus and
103 # return the index of the selected button. Restore the focus
104 # before deleting the window, since otherwise the window manager
105 # may take the focus away so we can't redirect it. Finally,
106 # restore any grab that was in effect.
108 vwait ::tk::Priv(selectColor
)
109 set result
$Priv(selectColor
)
110 ::tk::RestoreFocusGrab $w $data(okBtn
)
116 # ::tk::dialog::color::InitValues --
118 # Get called during initialization or when user resets NUM_COLORBARS
120 proc ::tk::dialog::color::InitValues {dataName
} {
121 upvar ::tk::dialog::color::$dataName data
123 # IntensityIncr is the difference in color intensity between a colorbar
125 set data
(intensityIncr
) [expr {256 / $data(NUM_COLORBARS
)}]
127 # ColorbarWidth is the width of each colorbar
128 set data
(colorbarWidth
) [expr {$data(BARS_WIDTH
) / $data(NUM_COLORBARS
)}]
130 # Indent is the width of the space at the left and right side of the
131 # colorbar. It is always half the selector polygon width, because the
132 # polygon extends into the space.
133 set data
(indent
) [expr {$data(PLGN_WIDTH
) / 2}]
136 set data
(selPad
) [expr {$data(PLGN_WIDTH
) / 2}]
139 # minX is the x coordinate of the first colorbar
141 set data
(minX
) $data(indent
)
144 # maxX is the x coordinate of the last colorbar
146 set data
(maxX
) [expr {$data(BARS_WIDTH
) + $data(indent
)-1}]
149 # canvasWidth is the width of the entire canvas, including the indents
151 set data
(canvasWidth
) [expr {$data(BARS_WIDTH
) + $data(PLGN_WIDTH
)}]
153 # Set the initial color, specified by -initialcolor, or the
154 # color chosen by the user the last time.
155 set data
(selection) $data(-initialcolor)
156 set data
(finalColor
) $data(-initialcolor)
157 set rgb
[winfo rgb .
$data(selection)]
159 set data
(red
,intensity
) [expr {[lindex $rgb 0]/0x100}]
160 set data
(green
,intensity
) [expr {[lindex $rgb 1]/0x100}]
161 set data
(blue
,intensity
) [expr {[lindex $rgb 2]/0x100}]
164 # ::tk::dialog::color::Config --
166 # Parses the command line arguments to tk_chooseColor
168 proc ::tk::dialog::color::Config {dataName argList
} {
170 upvar ::tk::dialog::color::$dataName data
172 # 1: the configuration specs
174 if {[info exists Priv
(selectColor
)] && $Priv(selectColor
) ne
""} {
175 set defaultColor
$Priv(selectColor
)
177 set defaultColor
[. cget
-background]
181 [list -initialcolor "" "" $defaultColor] \
182 [list -parent "" "" "."] \
183 [list -title "" "" [mc
"Color"]] \
186 # 2: parse the arguments
188 tclParseConfigSpec
::tk::dialog::color::$dataName $specs "" $argList
190 if {$data(-title) eq
""} {
193 if {[catch {winfo rgb .
$data(-initialcolor)} err
]} {
197 if {![winfo exists
$data(-parent)]} {
198 error "bad window path name \"$data(-parent)\""
202 # ::tk::dialog::color::BuildDialog --
206 proc ::tk::dialog::color::BuildDialog {w
} {
207 upvar ::tk::dialog::color::[winfo name
$w] data
209 # TopFrame contains the color strips and the color selection
211 set topFrame
[frame $w.top
-relief raised
-bd 1]
213 # StripsFrame contains the colorstrips and the individual RGB entries
214 set stripsFrame
[frame $topFrame.colorStrip
]
216 set maxWidth
[::tk::mcmaxamp &Red
&Green
&Blue
]
217 set maxWidth
[expr {$maxWidth<6 ?
6 : $maxWidth}]
223 foreach {color l
} $colorList {
224 # each f frame contains an [R|G|B] entry and the equiv. color strip.
225 set f
[frame $stripsFrame.
$color]
227 # The box frame contains the label and entry widget for an [R|G|B]
228 set box
[frame $f.box
]
230 ::tk::AmpWidget label $box.
label -text "[mc $l]:" \
231 -width $maxWidth -anchor ne
232 bind $box.
label <<AltUnderlined
>> [list focus $box.
entry]
234 entry $box.
entry -textvariable \
235 ::tk::dialog::color::[winfo name
$w]($color,intensity
) \
237 pack $box.
label -side left
-fill y
-padx 2 -pady 3
238 pack $box.
entry -side left
-anchor n
-pady 0
239 pack $box -side left
-fill both
242 [winfo reqheight
$box.
entry] -
243 2*([$box.
entry cget
-highlightthickness] + [$box.
entry cget
-bd])
246 canvas $f.color
-height $height \
247 -width $data(BARS_WIDTH
) -relief sunken
-bd 2
248 canvas $f.sel
-height $data(PLGN_HEIGHT
) \
249 -width $data(canvasWidth
) -highlightthickness 0
250 pack $f.color
-expand yes
-fill both
251 pack $f.sel
-expand yes
-fill both
253 pack $f -side top
-fill x
-padx 0 -pady 2
255 set data
($color,entry) $box.
entry
256 set data
($color,col
) $f.color
257 set data
($color,sel
) $f.sel
259 bind $data($color,col
) <Configure
> \
260 [list tk::dialog::color::DrawColorScale $w $color 1]
261 bind $data($color,col
) <Enter
> \
262 [list tk::dialog::color::EnterColorBar $w $color]
263 bind $data($color,col
) <Leave
> \
264 [list tk::dialog::color::LeaveColorBar $w $color]
266 bind $data($color,sel
) <Enter
> \
267 [list tk::dialog::color::EnterColorBar $w $color]
268 bind $data($color,sel
) <Leave
> \
269 [list tk::dialog::color::LeaveColorBar $w $color]
271 bind $box.
entry <Return
> [list tk::dialog::color::HandleRGBEntry $w]
274 pack $stripsFrame -side left
-fill both
-padx 4 -pady 10
276 # The selFrame contains a frame that demonstrates the currently
279 set selFrame
[frame $topFrame.sel
]
280 set lab
[::tk::AmpWidget label $selFrame.lab
\
281 -text [mc
"&Selection:"] -anchor sw
]
282 set ent
[entry $selFrame.ent
\
283 -textvariable ::tk::dialog::color::[winfo name
$w](selection) \
285 set f1
[frame $selFrame.f1
-relief sunken
-bd 2]
286 set data
(finalCanvas
) [frame $f1.demo
-bd 0 -width 100 -height 70]
288 pack $lab $ent -side top
-fill x
-padx 4 -pady 2
289 pack $f1 -expand yes
-anchor nw
-fill both
-padx 6 -pady 10
290 pack $data(finalCanvas
) -expand yes
-fill both
292 bind $ent <Return
> [list tk::dialog::color::HandleSelEntry $w]
294 pack $selFrame -side left
-fill none
-anchor nw
295 pack $topFrame -side top
-expand yes
-fill both
-anchor nw
297 # the botFrame frame contains the buttons
299 set botFrame
[frame $w.bot
-relief raised
-bd 1]
301 ::tk::AmpWidget button $botFrame.ok
-text [mc
"&OK"] \
302 -command [list tk::dialog::color::OkCmd $w]
303 ::tk::AmpWidget button $botFrame.cancel
-text [mc
"&Cancel"] \
304 -command [list tk::dialog::color::CancelCmd $w]
306 set data
(okBtn
) $botFrame.ok
307 set data
(cancelBtn
) $botFrame.cancel
309 grid x
$botFrame.ok x
$botFrame.cancel x
-sticky ew
310 grid configure
$botFrame.ok
$botFrame.cancel
-padx 10 -pady 10
311 grid columnconfigure
$botFrame {0 4} -weight 1 -uniform space
312 grid columnconfigure
$botFrame {1 3} -weight 1 -uniform button
313 grid columnconfigure
$botFrame 2 -weight 2 -uniform space
314 pack $botFrame -side bottom
-fill x
316 # Accelerator bindings
317 bind $lab <<AltUnderlined
>> [list focus $ent]
318 bind $w <KeyPress-Escape
> [list tk::ButtonInvoke $data(cancelBtn
)]
319 bind $w <Alt-Key
> [list tk::AltKeyInDialog $w %A
]
321 wm protocol
$w WM_DELETE_WINDOW
[list tk::dialog::color::CancelCmd $w]
322 bind $lab <Destroy
> [list tk::dialog::color::CancelCmd $w]
325 # ::tk::dialog::color::SetRGBValue --
327 # Sets the current selection of the dialog box
329 proc ::tk::dialog::color::SetRGBValue {w color
} {
330 upvar ::tk::dialog::color::[winfo name
$w] data
332 set data
(red
,intensity
) [lindex $color 0]
333 set data
(green
,intensity
) [lindex $color 1]
334 set data
(blue
,intensity
) [lindex $color 2]
336 RedrawColorBars
$w all
338 # Now compute the new x value of each colorbars pointer polygon
339 foreach color
{red green blue
} {
340 set x
[RgbToX
$w $data($color,intensity
)]
341 MoveSelector
$w $data($color,sel
) $color $x 0
345 # ::tk::dialog::color::XToRgb --
347 # Converts a screen coordinate to intensity
349 proc ::tk::dialog::color::XToRgb {w x
} {
350 upvar ::tk::dialog::color::[winfo name
$w] data
352 set x
[expr {($x * $data(intensityIncr
))/ $data(colorbarWidth
)}]
359 # ::tk::dialog::color::RgbToX
361 # Converts an intensity to screen coordinate.
363 proc ::tk::dialog::color::RgbToX {w color
} {
364 upvar ::tk::dialog::color::[winfo name
$w] data
366 return [expr {($color * $data(colorbarWidth
)/ $data(intensityIncr
))}]
369 # ::tk::dialog::color::DrawColorScale --
371 # Draw color scale is called whenever the size of one of the color
372 # scale canvases is changed.
374 proc ::tk::dialog::color::DrawColorScale {w c
{create
0}} {
375 upvar ::tk::dialog::color::[winfo name
$w] data
377 # col: color bar canvas
378 # sel: selector canvas
379 set col
$data($c,col
)
380 set sel
$data($c,sel
)
382 # First handle the case that we are creating everything for the first time.
384 # First remove all the lines that already exist.
385 if { $data(lines
,$c,last
) > $data(lines
,$c,start
)} {
386 for {set i
$data(lines
,$c,start
)} \
387 {$i <= $data(lines
,$c,last
)} {incr i
} {
391 # Delete the selector if it exists
392 if {[info exists data
($c,index
)]} {
393 $sel delete
$data($c,index
)
396 # Draw the selection polygons
397 CreateSelector
$w $sel $c
398 $sel bind $data($c,index
) <ButtonPress-1
> \
399 [list tk::dialog::color::StartMove $w $sel $c %x
$data(selPad
) 1]
400 $sel bind $data($c,index
) <B1-Motion
> \
401 [list tk::dialog::color::MoveSelector $w $sel $c %x
$data(selPad
)]
402 $sel bind $data($c,index
) <ButtonRelease-1
> \
403 [list tk::dialog::color::ReleaseMouse $w $sel $c %x
$data(selPad
)]
405 set height
[winfo height
$col]
406 # Create an invisible region under the colorstrip to catch mouse clicks
407 # that aren't on the selector.
408 set data
($c,clickRegion
) [$sel create rectangle
0 0 \
409 $data(canvasWidth
) $height -fill {} -outline {}]
411 bind $col <ButtonPress-1
> \
412 [list tk::dialog::color::StartMove $w $sel $c %x
$data(colorPad
)]
413 bind $col <B1-Motion
> \
414 [list tk::dialog::color::MoveSelector $w $sel $c %x
$data(colorPad
)]
415 bind $col <ButtonRelease-1
> \
416 [list tk::dialog::color::ReleaseMouse $w $sel $c %x
$data(colorPad
)]
418 $sel bind $data($c,clickRegion
) <ButtonPress-1
> \
419 [list tk::dialog::color::StartMove $w $sel $c %x
$data(selPad
)]
420 $sel bind $data($c,clickRegion
) <B1-Motion
> \
421 [list tk::dialog::color::MoveSelector $w $sel $c %x
$data(selPad
)]
422 $sel bind $data($c,clickRegion
) <ButtonRelease-1
> \
423 [list tk::dialog::color::ReleaseMouse $w $sel $c %x
$data(selPad
)]
425 # l is the canvas index of the first colorbar.
426 set l
$data(lines
,$c,start
)
429 # Draw the color bars.
430 set highlightW
[expr {[$col cget
-highlightthickness] + [$col cget
-bd]}]
431 for {set i
0} { $i < $data(NUM_COLORBARS
)} { incr i
} {
432 set intensity
[expr {$i * $data(intensityIncr
)}]
433 set startx
[expr {$i * $data(colorbarWidth
) + $highlightW}]
435 set color
[format "#%02x%02x%02x" \
436 $intensity $data(green
,intensity
) $data(blue
,intensity
)]
437 } elseif
{$c eq
"green"} {
438 set color
[format "#%02x%02x%02x" \
439 $data(red
,intensity
) $intensity $data(blue
,intensity
)]
441 set color
[format "#%02x%02x%02x" \
442 $data(red
,intensity
) $data(green
,intensity
) $intensity]
446 set index
[$col create rect
$startx $highlightW \
447 [expr {$startx +$data(colorbarWidth
)}] \
448 [expr {[winfo height
$col] + $highlightW}] \
449 -fill $color -outline $color]
451 $col itemconfigure
$l -fill $color -outline $color
455 $sel raise $data($c,index
)
458 set data
(lines
,$c,last
) $index
459 set data
(lines
,$c,start
) [expr {$index - $data(NUM_COLORBARS
) + 1}]
465 # ::tk::dialog::color::CreateSelector --
467 # Creates and draws the selector polygon at the position
468 # $data($c,intensity).
470 proc ::tk::dialog::color::CreateSelector {w sel c
} {
471 upvar ::tk::dialog::color::[winfo name
$w] data
472 set data
($c,index
) [$sel create polygon
\
473 0 $data(PLGN_HEIGHT
) \
474 $data(PLGN_WIDTH
) $data(PLGN_HEIGHT
) \
476 set data
($c,x
) [RgbToX
$w $data($c,intensity
)]
477 $sel move
$data($c,index
) $data($c,x
) 0
480 # ::tk::dialog::color::RedrawFinalColor
482 # Combines the intensities of the three colors into the final color
484 proc ::tk::dialog::color::RedrawFinalColor {w
} {
485 upvar ::tk::dialog::color::[winfo name
$w] data
487 set color
[format "#%02x%02x%02x" $data(red
,intensity
) \
488 $data(green
,intensity
) $data(blue
,intensity
)]
490 $data(finalCanvas
) configure
-bg $color
491 set data
(finalColor
) $color
492 set data
(selection) $color
493 set data
(finalRGB
) [list \
494 $data(red
,intensity
) \
495 $data(green
,intensity
) \
496 $data(blue
,intensity
)]
499 # ::tk::dialog::color::RedrawColorBars --
501 # Only redraws the colors on the color strips that were not manipulated.
502 # Params: color of colorstrip that changed. If color is not [red|green|blue]
503 # Then all colorstrips will be updated
505 proc ::tk::dialog::color::RedrawColorBars {w colorChanged
} {
506 upvar ::tk::dialog::color::[winfo name
$w] data
508 switch $colorChanged {
510 DrawColorScale
$w green
511 DrawColorScale
$w blue
514 DrawColorScale
$w red
515 DrawColorScale
$w blue
518 DrawColorScale
$w red
519 DrawColorScale
$w green
522 DrawColorScale
$w red
523 DrawColorScale
$w green
524 DrawColorScale
$w blue
530 #----------------------------------------------------------------------
532 #----------------------------------------------------------------------
534 # ::tk::dialog::color::StartMove --
536 # Handles a mousedown button event over the selector polygon.
537 # Adds the bindings for moving the mouse while the button is
538 # pressed. Sets the binding for the button-release event.
540 # Params: sel is the selector canvas window, color is the color of the strip.
542 proc ::tk::dialog::color::StartMove {w sel color x delta
{dontMove
0}} {
543 upvar ::tk::dialog::color::[winfo name
$w] data
546 MoveSelector
$w $sel $color $x $delta
550 # ::tk::dialog::color::MoveSelector --
552 # Moves the polygon selector so that its middle point has the same
553 # x value as the specified x. If x is outside the bounds [0,255],
554 # the selector is set to the closest endpoint.
556 # Params: sel is the selector canvas, c is [red|green|blue]
557 # x is a x-coordinate.
559 proc ::tk::dialog::color::MoveSelector {w sel color x delta
} {
560 upvar ::tk::dialog::color::[winfo name
$w] data
566 } elseif
{ $x > $data(BARS_WIDTH
)} {
567 set x
$data(BARS_WIDTH
)
569 set diff
[expr {$x - $data($color,x
)}]
570 $sel move
$data($color,index
) $diff 0
571 set data
($color,x
) [expr {$data($color,x
) + $diff}]
573 # Return the x value that it was actually set at
577 # ::tk::dialog::color::ReleaseMouse
579 # Removes mouse tracking bindings, updates the colorbars.
581 # Params: sel is the selector canvas, color is the color of the strip,
582 # x is the x-coord of the mouse.
584 proc ::tk::dialog::color::ReleaseMouse {w sel color x delta
} {
585 upvar ::tk::dialog::color::[winfo name
$w] data
587 set x
[MoveSelector
$w $sel $color $x $delta]
589 # Determine exactly what color we are looking at.
590 set data
($color,intensity
) [XToRgb
$w $x]
592 RedrawColorBars
$w $color
595 # ::tk::dialog::color::ResizeColorbars --
597 # Completely redraws the colorbars, including resizing the
600 proc ::tk::dialog::color::ResizeColorBars {w
} {
601 upvar ::tk::dialog::color::[winfo name
$w] data
604 ($data(BARS_WIDTH
) < $data(NUM_COLORBARS
)) ||
605 (($data(BARS_WIDTH
) % $data(NUM_COLORBARS
)) != 0)
607 set data
(BARS_WIDTH
) $data(NUM_COLORBARS
)
609 InitValues
[winfo name
$w]
610 foreach color
{red green blue
} {
611 $data($color,col
) configure
-width $data(canvasWidth
)
612 DrawColorScale
$w $color 1
616 # ::tk::dialog::color::HandleSelEntry --
618 # Handles the return keypress event in the "Selection:" entry
620 proc ::tk::dialog::color::HandleSelEntry {w
} {
621 upvar ::tk::dialog::color::[winfo name
$w] data
623 set text [string trim
$data(selection)]
624 # Check to make sure that the color is valid
625 if {[catch {set color
[winfo rgb .
$text]} ]} {
626 set data
(selection) $data(finalColor
)
630 set R
[expr {[lindex $color 0]/0x100}]
631 set G
[expr {[lindex $color 1]/0x100}]
632 set B
[expr {[lindex $color 2]/0x100}]
634 SetRGBValue
$w "$R $G $B"
635 set data
(selection) $text
638 # ::tk::dialog::color::HandleRGBEntry --
640 # Handles the return keypress event in the R, G or B entry
642 proc ::tk::dialog::color::HandleRGBEntry {w
} {
643 upvar ::tk::dialog::color::[winfo name
$w] data
645 foreach c
{red green blue
} {
647 set data
($c,intensity
) [expr {int
($data($c,intensity
))}]
649 set data
($c,intensity
) 0
652 if {$data($c,intensity
) < 0} {
653 set data
($c,intensity
) 0
655 if {$data($c,intensity
) > 255} {
656 set data
($c,intensity
) 255
660 SetRGBValue
$w "$data(red,intensity) \
661 $data(green,intensity) $data(blue,intensity)"
664 # mouse cursor enters a color bar
666 proc ::tk::dialog::color::EnterColorBar {w color
} {
667 upvar ::tk::dialog::color::[winfo name
$w] data
669 $data($color,sel
) itemconfigure
$data($color,index
) -fill red
672 # mouse leaves enters a color bar
674 proc ::tk::dialog::color::LeaveColorBar {w color
} {
675 upvar ::tk::dialog::color::[winfo name
$w] data
677 $data($color,sel
) itemconfigure
$data($color,index
) -fill black
680 # user hits OK button
682 proc ::tk::dialog::color::OkCmd {w
} {
684 upvar ::tk::dialog::color::[winfo name
$w] data
686 set Priv
(selectColor
) $data(finalColor
)
689 # user hits Cancel button or destroys window
691 proc ::tk::dialog::color::CancelCmd {w
} {
693 set Priv
(selectColor
) ""