3 # Color selection dialog.
4 # standard color selection dialog.
6 # Copyright (c) 1996 Sun Microsystems, Inc.
7 # Copyright (c) 1999 Christian Werner
9 # See the file "license.terms" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14 # Create a color dialog and let the user choose a color. This function
15 # should not be called directly. It is called by the tk_chooseColor
16 # function when a native color selector widget does not exist
18 proc ck_chooseColor args {
22 if {[winfo depth .] == 1} {
23 set data(colors) {black white}
24 set data(rcolors) {white black}
26 set data(colors) {black blue cyan green magenta red white yellow}
27 set data(rcolors) {white white white white white white black black}
29 ckColorDialog_Config $w $args
31 if {![winfo exists $w]} {
32 toplevel $w -class CkColorDialog -border {
33 ulcorner hline urcorner vline lrcorner hline llcorner vline
35 ckColorDialog_BuildDialog $w
37 place $w -relx 0.5 -rely 0.5 -anchor center
44 # Wait for the user to respond, then restore the focus and
45 # return the index of the selected button. Restore the focus
46 # before deleting the window, since otherwise the window manager
47 # may take the focus away so we can't redirect it. Finally,
48 # restore any grab that was in effect.
50 tkwait variable ckPriv(selectColor)
51 catch {focus $oldFocus}
54 return $ckPriv(selectColor)
57 # ckColorDialog_Config --
59 # Parses the command line arguments to tk_chooseColor
61 proc ckColorDialog_Config {w argList} {
65 {-initialcolor "" "" ""}
67 {-title "" "" "Color"}
69 tclParseConfigSpec $w $specs "" $argList
70 if {![string compare $data(-initialcolor) ""]} {
71 if {[info exists ckPriv(selectColor)] && \
72 [string compare $ckPriv(selectColor) ""]} {
73 set data(-initialcolor) $ckPriv(selectColor)
75 set data(-initialcolor) [. cget -background]
77 } elseif {[lsearch -exact $data(colors) $data(-initialcolor)] <= 0} {
78 error "illegal -initialcolor"
80 if {![winfo exists $data(-parent)]} {
81 error "bad window path name \"$data(-parent)\""
85 # ckColorDialog_BuildDialog --
89 proc ckColorDialog_BuildDialog w {
91 label $w.title -text "Select Color"
92 pack $w.title -side top -fill x -pady 1
94 pack $w.top -side top -fill x -padx 1
96 foreach i $data(colors) {
97 radiobutton $w.top.$i -background $i -text $i -value $i \
98 -variable ${w}(finalColor) \
99 -foreground [lindex $data(rcolors) $count] \
100 -selectcolor [lindex $data(rcolors) $count]
101 if {[winfo depth .] > 1} {
102 $w.top.$i configure -activeforeground \
103 [$w.top.$i cget -background] -activeattributes bold \
104 -activebackground [$w.top.$i cget -foreground]
106 pack $w.top.$i -side top -fill x
110 pack $w.bot -side top -fill x -padx 1 -pady 1
111 button $w.bot.ok -text OK -width 8 -underline 0 \
112 -command [list ckColorDialog_OkCmd $w]
113 button $w.bot.cancel -text Cancel -width 8 -underline 0 \
114 -command [list ckColorDialog_CancelCmd $w]
115 pack $w.bot.ok $w.bot.cancel -side left -expand 1
116 # Accelerator bindings
117 bind $w <Escape> [list ckButtonInvoke $w.bot.cancel]
118 bind $w <c> [list ckButtonInvoke $w.bot.cancel]
119 bind $w <C> [list ckButtonInvoke $w.bot.cancel]
120 bind $w <o> [list ckButtonInvoke $w.bot.ok]
121 bind $w <O> [list ckButtonInvoke $w.bot.ok]
122 set data(finalColor) $data(-initialcolor)
125 proc ckColorDialog_OkCmd {w} {
128 set ckPriv(selectColor) $data(finalColor)
131 proc ckColorDialog_CancelCmd {w} {
133 set ckPriv(selectColor) ""