3 # Color selection dialog for platforms that do not support a
4 # standard color selection dialog.
6 # RCS: @(#) $Id: clrpick.tcl,v 1.22.4.1 2010/01/20 23:43:51 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
77 if {[tk windowingsystem
] eq
"x11"} {wm attributes
$w -type dialog
}
81 # Dialog boxes should be transient with respect to their parent,
82 # so that they will always stay on top of their parent window. However,
83 # some window managers will create the window as withdrawn if the parent
84 # window is withdrawn or iconified. Combined with the grab we put on the
85 # window, this can hang the entire application. Therefore we only make
86 # the dialog transient if the parent is viewable.
88 if {[winfo viewable
[winfo toplevel $data(-parent)]] } {
89 wm transient
$w $data(-parent)
92 # 5. Withdraw the window, then update all the geometry information
93 # so we know how big it wants to be, then center the window in the
94 # display and de-iconify it.
96 ::tk::PlaceWindow $w widget
$data(-parent)
97 wm title
$w $data(-title)
99 # 6. Set a grab and claim the focus too.
101 ::tk::SetFocusGrab $w $data(okBtn
)
103 # 7. Wait for the user to respond, then restore the focus and
104 # return the index of the selected button. Restore the focus
105 # before deleting the window, since otherwise the window manager
106 # may take the focus away so we can't redirect it. Finally,
107 # restore any grab that was in effect.
109 vwait ::tk::Priv(selectColor
)
110 set result
$Priv(selectColor
)
111 ::tk::RestoreFocusGrab $w $data(okBtn
)
117 # ::tk::dialog::color::InitValues --
119 # Get called during initialization or when user resets NUM_COLORBARS
121 proc ::tk::dialog::color::InitValues {dataName
} {
122 upvar ::tk::dialog::color::$dataName data
124 # IntensityIncr is the difference in color intensity between a colorbar
126 set data
(intensityIncr
) [expr {256 / $data(NUM_COLORBARS
)}]
128 # ColorbarWidth is the width of each colorbar
129 set data
(colorbarWidth
) [expr {$data(BARS_WIDTH
) / $data(NUM_COLORBARS
)}]
131 # Indent is the width of the space at the left and right side of the
132 # colorbar. It is always half the selector polygon width, because the
133 # polygon extends into the space.
134 set data
(indent
) [expr {$data(PLGN_WIDTH
) / 2}]
137 set data
(selPad
) [expr {$data(PLGN_WIDTH
) / 2}]
140 # minX is the x coordinate of the first colorbar
142 set data
(minX
) $data(indent
)
145 # maxX is the x coordinate of the last colorbar
147 set data
(maxX
) [expr {$data(BARS_WIDTH
) + $data(indent
)-1}]
150 # canvasWidth is the width of the entire canvas, including the indents
152 set data
(canvasWidth
) [expr {$data(BARS_WIDTH
) + $data(PLGN_WIDTH
)}]
154 # Set the initial color, specified by -initialcolor, or the
155 # color chosen by the user the last time.
156 set data
(selection) $data(-initialcolor)
157 set data
(finalColor
) $data(-initialcolor)
158 set rgb
[winfo rgb .
$data(selection)]
160 set data
(red
,intensity
) [expr {[lindex $rgb 0]/0x100}]
161 set data
(green
,intensity
) [expr {[lindex $rgb 1]/0x100}]
162 set data
(blue
,intensity
) [expr {[lindex $rgb 2]/0x100}]
165 # ::tk::dialog::color::Config --
167 # Parses the command line arguments to tk_chooseColor
169 proc ::tk::dialog::color::Config {dataName argList
} {
171 upvar ::tk::dialog::color::$dataName data
173 # 1: the configuration specs
175 if {[info exists Priv
(selectColor
)] && $Priv(selectColor
) ne
""} {
176 set defaultColor
$Priv(selectColor
)
178 set defaultColor
[. cget
-background]
182 [list -initialcolor "" "" $defaultColor] \
183 [list -parent "" "" "."] \
184 [list -title "" "" [mc
"Color"]] \
187 # 2: parse the arguments
189 tclParseConfigSpec
::tk::dialog::color::$dataName $specs "" $argList
191 if {$data(-title) eq
""} {
194 if {[catch {winfo rgb .
$data(-initialcolor)} err
]} {
198 if {![winfo exists
$data(-parent)]} {
199 error "bad window path name \"$data(-parent)\""
203 # ::tk::dialog::color::BuildDialog --
207 proc ::tk::dialog::color::BuildDialog {w
} {
208 upvar ::tk::dialog::color::[winfo name
$w] data
210 # TopFrame contains the color strips and the color selection
212 set topFrame
[frame $w.top
-relief raised
-bd 1]
214 # StripsFrame contains the colorstrips and the individual RGB entries
215 set stripsFrame
[frame $topFrame.colorStrip
]
217 set maxWidth
[::tk::mcmaxamp &Red
&Green
&Blue
]
218 set maxWidth
[expr {$maxWidth<6 ?
6 : $maxWidth}]
224 foreach {color l
} $colorList {
225 # each f frame contains an [R|G|B] entry and the equiv. color strip.
226 set f
[frame $stripsFrame.
$color]
228 # The box frame contains the label and entry widget for an [R|G|B]
229 set box
[frame $f.box
]
231 ::tk::AmpWidget label $box.
label -text "[mc $l]:" \
232 -width $maxWidth -anchor ne
233 bind $box.
label <<AltUnderlined
>> [list focus $box.
entry]
235 entry $box.
entry -textvariable \
236 ::tk::dialog::color::[winfo name
$w]($color,intensity
) \
238 pack $box.
label -side left
-fill y
-padx 2 -pady 3
239 pack $box.
entry -side left
-anchor n
-pady 0
240 pack $box -side left
-fill both
243 [winfo reqheight
$box.
entry] -
244 2*([$box.
entry cget
-highlightthickness] + [$box.
entry cget
-bd])
247 canvas $f.color
-height $height \
248 -width $data(BARS_WIDTH
) -relief sunken
-bd 2
249 canvas $f.sel
-height $data(PLGN_HEIGHT
) \
250 -width $data(canvasWidth
) -highlightthickness 0
251 pack $f.color
-expand yes
-fill both
252 pack $f.sel
-expand yes
-fill both
254 pack $f -side top
-fill x
-padx 0 -pady 2
256 set data
($color,entry) $box.
entry
257 set data
($color,col
) $f.color
258 set data
($color,sel
) $f.sel
260 bind $data($color,col
) <Configure
> \
261 [list tk::dialog::color::DrawColorScale $w $color 1]
262 bind $data($color,col
) <Enter
> \
263 [list tk::dialog::color::EnterColorBar $w $color]
264 bind $data($color,col
) <Leave
> \
265 [list tk::dialog::color::LeaveColorBar $w $color]
267 bind $data($color,sel
) <Enter
> \
268 [list tk::dialog::color::EnterColorBar $w $color]
269 bind $data($color,sel
) <Leave
> \
270 [list tk::dialog::color::LeaveColorBar $w $color]
272 bind $box.
entry <Return
> [list tk::dialog::color::HandleRGBEntry $w]
275 pack $stripsFrame -side left
-fill both
-padx 4 -pady 10
277 # The selFrame contains a frame that demonstrates the currently
280 set selFrame
[frame $topFrame.sel
]
281 set lab
[::tk::AmpWidget label $selFrame.lab
\
282 -text [mc
"&Selection:"] -anchor sw
]
283 set ent
[entry $selFrame.ent
\
284 -textvariable ::tk::dialog::color::[winfo name
$w](selection) \
286 set f1
[frame $selFrame.f1
-relief sunken
-bd 2]
287 set data
(finalCanvas
) [frame $f1.demo
-bd 0 -width 100 -height 70]
289 pack $lab $ent -side top
-fill x
-padx 4 -pady 2
290 pack $f1 -expand yes
-anchor nw
-fill both
-padx 6 -pady 10
291 pack $data(finalCanvas
) -expand yes
-fill both
293 bind $ent <Return
> [list tk::dialog::color::HandleSelEntry $w]
295 pack $selFrame -side left
-fill none
-anchor nw
296 pack $topFrame -side top
-expand yes
-fill both
-anchor nw
298 # the botFrame frame contains the buttons
300 set botFrame
[frame $w.bot
-relief raised
-bd 1]
302 ::tk::AmpWidget button $botFrame.ok
-text [mc
"&OK"] \
303 -command [list tk::dialog::color::OkCmd $w]
304 ::tk::AmpWidget button $botFrame.cancel
-text [mc
"&Cancel"] \
305 -command [list tk::dialog::color::CancelCmd $w]
307 set data
(okBtn
) $botFrame.ok
308 set data
(cancelBtn
) $botFrame.cancel
310 grid x
$botFrame.ok x
$botFrame.cancel x
-sticky ew
311 grid configure
$botFrame.ok
$botFrame.cancel
-padx 10 -pady 10
312 grid columnconfigure
$botFrame {0 4} -weight 1 -uniform space
313 grid columnconfigure
$botFrame {1 3} -weight 1 -uniform button
314 grid columnconfigure
$botFrame 2 -weight 2 -uniform space
315 pack $botFrame -side bottom
-fill x
317 # Accelerator bindings
318 bind $lab <<AltUnderlined
>> [list focus $ent]
319 bind $w <KeyPress-Escape
> [list tk::ButtonInvoke $data(cancelBtn
)]
320 bind $w <Alt-Key
> [list tk::AltKeyInDialog $w %A
]
322 wm protocol
$w WM_DELETE_WINDOW
[list tk::dialog::color::CancelCmd $w]
323 bind $lab <Destroy
> [list tk::dialog::color::CancelCmd $w]
326 # ::tk::dialog::color::SetRGBValue --
328 # Sets the current selection of the dialog box
330 proc ::tk::dialog::color::SetRGBValue {w color
} {
331 upvar ::tk::dialog::color::[winfo name
$w] data
333 set data
(red
,intensity
) [lindex $color 0]
334 set data
(green
,intensity
) [lindex $color 1]
335 set data
(blue
,intensity
) [lindex $color 2]
337 RedrawColorBars
$w all
339 # Now compute the new x value of each colorbars pointer polygon
340 foreach color
{red green blue
} {
341 set x
[RgbToX
$w $data($color,intensity
)]
342 MoveSelector
$w $data($color,sel
) $color $x 0
346 # ::tk::dialog::color::XToRgb --
348 # Converts a screen coordinate to intensity
350 proc ::tk::dialog::color::XToRgb {w x
} {
351 upvar ::tk::dialog::color::[winfo name
$w] data
353 set x
[expr {($x * $data(intensityIncr
))/ $data(colorbarWidth
)}]
360 # ::tk::dialog::color::RgbToX
362 # Converts an intensity to screen coordinate.
364 proc ::tk::dialog::color::RgbToX {w color
} {
365 upvar ::tk::dialog::color::[winfo name
$w] data
367 return [expr {($color * $data(colorbarWidth
)/ $data(intensityIncr
))}]
370 # ::tk::dialog::color::DrawColorScale --
372 # Draw color scale is called whenever the size of one of the color
373 # scale canvases is changed.
375 proc ::tk::dialog::color::DrawColorScale {w c
{create
0}} {
376 upvar ::tk::dialog::color::[winfo name
$w] data
378 # col: color bar canvas
379 # sel: selector canvas
380 set col
$data($c,col
)
381 set sel
$data($c,sel
)
383 # First handle the case that we are creating everything for the first time.
385 # First remove all the lines that already exist.
386 if { $data(lines
,$c,last
) > $data(lines
,$c,start
)} {
387 for {set i
$data(lines
,$c,start
)} \
388 {$i <= $data(lines
,$c,last
)} {incr i
} {
392 # Delete the selector if it exists
393 if {[info exists data
($c,index
)]} {
394 $sel delete
$data($c,index
)
397 # Draw the selection polygons
398 CreateSelector
$w $sel $c
399 $sel bind $data($c,index
) <ButtonPress-1
> \
400 [list tk::dialog::color::StartMove $w $sel $c %x
$data(selPad
) 1]
401 $sel bind $data($c,index
) <B1-Motion
> \
402 [list tk::dialog::color::MoveSelector $w $sel $c %x
$data(selPad
)]
403 $sel bind $data($c,index
) <ButtonRelease-1
> \
404 [list tk::dialog::color::ReleaseMouse $w $sel $c %x
$data(selPad
)]
406 set height
[winfo height
$col]
407 # Create an invisible region under the colorstrip to catch mouse clicks
408 # that aren't on the selector.
409 set data
($c,clickRegion
) [$sel create rectangle
0 0 \
410 $data(canvasWidth
) $height -fill {} -outline {}]
412 bind $col <ButtonPress-1
> \
413 [list tk::dialog::color::StartMove $w $sel $c %x
$data(colorPad
)]
414 bind $col <B1-Motion
> \
415 [list tk::dialog::color::MoveSelector $w $sel $c %x
$data(colorPad
)]
416 bind $col <ButtonRelease-1
> \
417 [list tk::dialog::color::ReleaseMouse $w $sel $c %x
$data(colorPad
)]
419 $sel bind $data($c,clickRegion
) <ButtonPress-1
> \
420 [list tk::dialog::color::StartMove $w $sel $c %x
$data(selPad
)]
421 $sel bind $data($c,clickRegion
) <B1-Motion
> \
422 [list tk::dialog::color::MoveSelector $w $sel $c %x
$data(selPad
)]
423 $sel bind $data($c,clickRegion
) <ButtonRelease-1
> \
424 [list tk::dialog::color::ReleaseMouse $w $sel $c %x
$data(selPad
)]
426 # l is the canvas index of the first colorbar.
427 set l
$data(lines
,$c,start
)
430 # Draw the color bars.
431 set highlightW
[expr {[$col cget
-highlightthickness] + [$col cget
-bd]}]
432 for {set i
0} { $i < $data(NUM_COLORBARS
)} { incr i
} {
433 set intensity
[expr {$i * $data(intensityIncr
)}]
434 set startx
[expr {$i * $data(colorbarWidth
) + $highlightW}]
436 set color
[format "#%02x%02x%02x" \
437 $intensity $data(green
,intensity
) $data(blue
,intensity
)]
438 } elseif
{$c eq
"green"} {
439 set color
[format "#%02x%02x%02x" \
440 $data(red
,intensity
) $intensity $data(blue
,intensity
)]
442 set color
[format "#%02x%02x%02x" \
443 $data(red
,intensity
) $data(green
,intensity
) $intensity]
447 set index
[$col create rect
$startx $highlightW \
448 [expr {$startx +$data(colorbarWidth
)}] \
449 [expr {[winfo height
$col] + $highlightW}] \
450 -fill $color -outline $color]
452 $col itemconfigure
$l -fill $color -outline $color
456 $sel raise $data($c,index
)
459 set data
(lines
,$c,last
) $index
460 set data
(lines
,$c,start
) [expr {$index - $data(NUM_COLORBARS
) + 1}]
466 # ::tk::dialog::color::CreateSelector --
468 # Creates and draws the selector polygon at the position
469 # $data($c,intensity).
471 proc ::tk::dialog::color::CreateSelector {w sel c
} {
472 upvar ::tk::dialog::color::[winfo name
$w] data
473 set data
($c,index
) [$sel create polygon
\
474 0 $data(PLGN_HEIGHT
) \
475 $data(PLGN_WIDTH
) $data(PLGN_HEIGHT
) \
477 set data
($c,x
) [RgbToX
$w $data($c,intensity
)]
478 $sel move
$data($c,index
) $data($c,x
) 0
481 # ::tk::dialog::color::RedrawFinalColor
483 # Combines the intensities of the three colors into the final color
485 proc ::tk::dialog::color::RedrawFinalColor {w
} {
486 upvar ::tk::dialog::color::[winfo name
$w] data
488 set color
[format "#%02x%02x%02x" $data(red
,intensity
) \
489 $data(green
,intensity
) $data(blue
,intensity
)]
491 $data(finalCanvas
) configure
-bg $color
492 set data
(finalColor
) $color
493 set data
(selection) $color
494 set data
(finalRGB
) [list \
495 $data(red
,intensity
) \
496 $data(green
,intensity
) \
497 $data(blue
,intensity
)]
500 # ::tk::dialog::color::RedrawColorBars --
502 # Only redraws the colors on the color strips that were not manipulated.
503 # Params: color of colorstrip that changed. If color is not [red|green|blue]
504 # Then all colorstrips will be updated
506 proc ::tk::dialog::color::RedrawColorBars {w colorChanged
} {
507 upvar ::tk::dialog::color::[winfo name
$w] data
509 switch $colorChanged {
511 DrawColorScale
$w green
512 DrawColorScale
$w blue
515 DrawColorScale
$w red
516 DrawColorScale
$w blue
519 DrawColorScale
$w red
520 DrawColorScale
$w green
523 DrawColorScale
$w red
524 DrawColorScale
$w green
525 DrawColorScale
$w blue
531 #----------------------------------------------------------------------
533 #----------------------------------------------------------------------
535 # ::tk::dialog::color::StartMove --
537 # Handles a mousedown button event over the selector polygon.
538 # Adds the bindings for moving the mouse while the button is
539 # pressed. Sets the binding for the button-release event.
541 # Params: sel is the selector canvas window, color is the color of the strip.
543 proc ::tk::dialog::color::StartMove {w sel color x delta
{dontMove
0}} {
544 upvar ::tk::dialog::color::[winfo name
$w] data
547 MoveSelector
$w $sel $color $x $delta
551 # ::tk::dialog::color::MoveSelector --
553 # Moves the polygon selector so that its middle point has the same
554 # x value as the specified x. If x is outside the bounds [0,255],
555 # the selector is set to the closest endpoint.
557 # Params: sel is the selector canvas, c is [red|green|blue]
558 # x is a x-coordinate.
560 proc ::tk::dialog::color::MoveSelector {w sel color x delta
} {
561 upvar ::tk::dialog::color::[winfo name
$w] data
567 } elseif
{ $x > $data(BARS_WIDTH
)} {
568 set x
$data(BARS_WIDTH
)
570 set diff
[expr {$x - $data($color,x
)}]
571 $sel move
$data($color,index
) $diff 0
572 set data
($color,x
) [expr {$data($color,x
) + $diff}]
574 # Return the x value that it was actually set at
578 # ::tk::dialog::color::ReleaseMouse
580 # Removes mouse tracking bindings, updates the colorbars.
582 # Params: sel is the selector canvas, color is the color of the strip,
583 # x is the x-coord of the mouse.
585 proc ::tk::dialog::color::ReleaseMouse {w sel color x delta
} {
586 upvar ::tk::dialog::color::[winfo name
$w] data
588 set x
[MoveSelector
$w $sel $color $x $delta]
590 # Determine exactly what color we are looking at.
591 set data
($color,intensity
) [XToRgb
$w $x]
593 RedrawColorBars
$w $color
596 # ::tk::dialog::color::ResizeColorbars --
598 # Completely redraws the colorbars, including resizing the
601 proc ::tk::dialog::color::ResizeColorBars {w
} {
602 upvar ::tk::dialog::color::[winfo name
$w] data
605 ($data(BARS_WIDTH
) < $data(NUM_COLORBARS
)) ||
606 (($data(BARS_WIDTH
) % $data(NUM_COLORBARS
)) != 0)
608 set data
(BARS_WIDTH
) $data(NUM_COLORBARS
)
610 InitValues
[winfo name
$w]
611 foreach color
{red green blue
} {
612 $data($color,col
) configure
-width $data(canvasWidth
)
613 DrawColorScale
$w $color 1
617 # ::tk::dialog::color::HandleSelEntry --
619 # Handles the return keypress event in the "Selection:" entry
621 proc ::tk::dialog::color::HandleSelEntry {w
} {
622 upvar ::tk::dialog::color::[winfo name
$w] data
624 set text [string trim
$data(selection)]
625 # Check to make sure that the color is valid
626 if {[catch {set color
[winfo rgb .
$text]} ]} {
627 set data
(selection) $data(finalColor
)
631 set R
[expr {[lindex $color 0]/0x100}]
632 set G
[expr {[lindex $color 1]/0x100}]
633 set B
[expr {[lindex $color 2]/0x100}]
635 SetRGBValue
$w "$R $G $B"
636 set data
(selection) $text
639 # ::tk::dialog::color::HandleRGBEntry --
641 # Handles the return keypress event in the R, G or B entry
643 proc ::tk::dialog::color::HandleRGBEntry {w
} {
644 upvar ::tk::dialog::color::[winfo name
$w] data
646 foreach c
{red green blue
} {
648 set data
($c,intensity
) [expr {int
($data($c,intensity
))}]
650 set data
($c,intensity
) 0
653 if {$data($c,intensity
) < 0} {
654 set data
($c,intensity
) 0
656 if {$data($c,intensity
) > 255} {
657 set data
($c,intensity
) 255
661 SetRGBValue
$w "$data(red,intensity) \
662 $data(green,intensity) $data(blue,intensity)"
665 # mouse cursor enters a color bar
667 proc ::tk::dialog::color::EnterColorBar {w color
} {
668 upvar ::tk::dialog::color::[winfo name
$w] data
670 $data($color,sel
) itemconfigure
$data($color,index
) -fill red
673 # mouse leaves enters a color bar
675 proc ::tk::dialog::color::LeaveColorBar {w color
} {
676 upvar ::tk::dialog::color::[winfo name
$w] data
678 $data($color,sel
) itemconfigure
$data($color,index
) -fill black
681 # user hits OK button
683 proc ::tk::dialog::color::OkCmd {w
} {
685 upvar ::tk::dialog::color::[winfo name
$w] data
687 set Priv
(selectColor
) $data(finalColor
)
690 # user hits Cancel button or destroys window
692 proc ::tk::dialog::color::CancelCmd {w
} {
694 set Priv
(selectColor
) ""