Bwidget Source Code
Check-in [9f462bd2fa]
Not logged in
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2019 Conference, Houston/TX, US, Nov 4-8
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Sep 9.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:REVISIONS TO color.tcl

1. Remove option -variable 2. Remove variable _varName which held the value of option -variable 3. Use variable _unsavedSelection in place of $_varName when a value must be stored 4. Add option -command 5. Add variable _command which holds the value of option -command 6. Set the default value for option -help to 1 7. Add command _userCommand to evaluate $_command at stack level #0. _userCommand is called whenever the selected color changes, i.e. instead of setting $_varName. 8. To avoid issues with trace, fully qualify ::SelectColor::_unsavedSelection when its value is set or it is used in a trace command. 9. Use 8.4-compatible syntax for string indices in "string range" commands. 10. Amend SelectColor::_SetWithoutTrace so it sets the colors in the Color Selectors when the value in the entry widget changes.

PURPOSE Tracing the variable set by the "-variable" option is too awkward in practice, and so the "-variable" option has been replaced with "-command" which allows the caller to specify a command to be executed whenever the selected color changes.

The use of the -command option is now included in the demo.

The properties of DynamicHelp balloon help have been changed in the demo, to make the text easier to read.

The manual page has been updated to reflect the changes in color.tcl.

Timelines: family | ancestors | descendants | both | patch-75101
Files: files | file ages | folders
SHA3-256:9f462bd2fa819d8c5c63dad21184aff6940c6dd4457ff21cd3d7890c92d86d92
User & Date: kjnash 2017-10-28 00:18:47
References
2017-10-28
00:25 Ticket [75101bf5ce] Enhancements to the SelectColor dialog status still Pending with 3 other changes artifact: 5804cb3c83 user: kjnash
Context
2018-01-09
17:45
Rudimentary French localisation, help not on on by default (compatibility), ChangeLog Closed-Leaf check-in: 31afcaf544 user: oehhar tags: patch-75101
2017-10-28
00:18
REVISIONS TO color.tcl

1. Remove option -variable 2. Remove variable _varName which held the value of option -variable 3. Use variable _unsavedSelection in place of $_varName when a value must be stored 4. Add option -command 5. Add variable _command which holds the value of option -command 6. Set the default value for option -help to 1 7. Add command _userCommand to evaluate $_command at stack level #0. _userCommand is called whenever the selected color changes, i.e. instead of setting $_varName. 8. To avoid issues with trace, fully qualify ::SelectColor::_unsavedSelection when its value is set or it is used in a trace command. 9. Use 8.4-compatible syntax for string indices in "string range" commands. 10. Amend SelectColor::_SetWithoutTrace so it sets the colors in the Color Selectors when the value in the entry widget changes.

PURPOSE Tracing the variable set by the "-variable" option is too awkward in practice, and so the "-variable" option has been replaced with "-command" which allows the caller to specify a command to be executed whenever the selected color changes.

The use of the -command option is now included in the demo.

The properties of DynamicHelp balloon help have been changed in the demo, to make the text easier to read.

The manual page has been updated to reflect the changes in color.tcl. check-in: 9f462bd2fa user: kjnash tags: patch-75101

2017-10-26
15:22
merge from branch bwidget check-in: 8fe69fee99 user: kjnash tags: patch-75101
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to BWman/SelectColor.html.

12
13
14
15
16
17
18




19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
..
71
72
73
74
75
76
77
78






















79
80
81
82
83
84
85
...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<DL>
<DT><I><A HREF="#wso">WIDGET-SPECIFIC OPTIONS</A></I></DT>
<DD><TABLE CELLSPACING=0 CELLSPACING=0 BORDER=0>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-background">-background</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-color">-color</A></td>
</TR>




<TR>
<TD>&nbsp;&nbsp;<A HREF="#-parent">-parent</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-placement">-placement</A></td>
</TR>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-title">-title</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-type">-type</A></td>
</TR>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-variable">-variable</A></td>
</TR>
</TABLE></DD>
</DL>
<DL>
<DT><I><A HREF="#wc">WIDGET COMMAND</A></I></DT>
<DD><I>pathName</I> <A HREF="#cget"><B>cget</B></A>
 <I>option</I>
</DD>
................................................................................
Specifies the background color of the widget. 
 
</DD>
</DL>
<DL><DT><A NAME="-color"><B>-color</B></A></DT>
<DD>

Specifies the color value of the widget.























</DD>
</DL>
<DL><DT><A NAME="-parent"><B>-parent</B></A></DT>
<DD>

Parent of the Dialog. Dialog is centered in its parent. If empty, it is centered in
................................................................................
SelectColor::<B>create</B> directly creates the dialog, displays it and
return an empty string if cancel button is pressed or if dialog is destroyed,
and the selected color if ok button is pressed. In all cases, dialog is
destroyed.  <BR>If <B>type</B> option is <I>popup</I>,
SelectColor::<B>create</B> creates a small, popup dialog with a small set of
predefined colors and a button to activate a full color dialog.

</DD>
</DL>
<DL><DT><A NAME="-variable"><B>-variable</B></A></DT>
<DD>

This is the name of a variable that the caller can trace, to follow the color that the user selects in the dialog (typically to modify a color in the calling GUI).

</DD>
</DL>
<HR WIDTH="50%"><BR>
<B><A NAME="wc">WIDGET COMMAND</A></B><BR>
<DL><DT><A NAME="cget"><I>pathName</I> <B>cget</B></A>
 <I>option</I>
</DT><DD>







>
>
>
>








<
<
<







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<
<
<
<
<







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30



31
32
33
34
35
36
37
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
...
133
134
135
136
137
138
139







140
141
142
143
144
145
146
<DL>
<DT><I><A HREF="#wso">WIDGET-SPECIFIC OPTIONS</A></I></DT>
<DD><TABLE CELLSPACING=0 CELLSPACING=0 BORDER=0>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-background">-background</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-color">-color</A></td>
</TR>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-command">-command</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-help">-help</A></td>
</TR>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-parent">-parent</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-placement">-placement</A></td>
</TR>
<TR>
<TD>&nbsp;&nbsp;<A HREF="#-title">-title</A></td>
<TD>&nbsp;&nbsp;<A HREF="#-type">-type</A></td>
</TR>



</TABLE></DD>
</DL>
<DL>
<DT><I><A HREF="#wc">WIDGET COMMAND</A></I></DT>
<DD><I>pathName</I> <A HREF="#cget"><B>cget</B></A>
 <I>option</I>
</DD>
................................................................................
Specifies the background color of the widget. 
 
</DD>
</DL>
<DL><DT><A NAME="-color"><B>-color</B></A></DT>
<DD>

Specifies the initial color used in the widget's color selectors.  When modifying a
color that is used in the GUI, the value supplied is typically the existing value of
that color.

</DD>
</DL>
<DL><DT><A NAME="-command"><B>-command</B></A></DT>
<DD>

Specifies a command to be evaluated, with a color value appended, whenever
the color selected in the dialog changes.  This facility can be used to modify
a color in the calling GUI and preview the change before deciding whether or not
to accept it.  If the user selects "Cancel" in the dialog, the command is called
a final time to restore the initial color (supplied by option <B>-color</B>) that was
used before the dialog was opened.

</DD>
</DL>
<DL><DT><A NAME="-help"><B>-help</B></A></DT>
<DD>

This option takes a Boolean value.  If the value is Boolean true, the SelectColor
dialog will include a panel that offers dynamic help to the user.

</DD>
</DL>
<DL><DT><A NAME="-parent"><B>-parent</B></A></DT>
<DD>

Parent of the Dialog. Dialog is centered in its parent. If empty, it is centered in
................................................................................
SelectColor::<B>create</B> directly creates the dialog, displays it and
return an empty string if cancel button is pressed or if dialog is destroyed,
and the selected color if ok button is pressed. In all cases, dialog is
destroyed.  <BR>If <B>type</B> option is <I>popup</I>,
SelectColor::<B>create</B> creates a small, popup dialog with a small set of
predefined colors and a button to activate a full color dialog.








</DD>
</DL>
<HR WIDTH="50%"><BR>
<B><A NAME="wc">WIDGET COMMAND</A></B><BR>
<DL><DT><A NAME="cget"><I>pathName</I> <B>cget</B></A>
 <I>option</I>
</DT><DD>

Changes to color.tcl.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
..
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
...
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200










201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
...
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
...
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
...
438
439
440
441
442
443
444
445

446
447
448
449
450
451
452
453
454
455
456
457
...
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
...
507
508
509
510
511
512
513
514
515
516

517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533

534
535
536
537
538
539
540
...
781
782
783
784
785
786
787




788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815

816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
...
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
...
893
894
895
896
897
898
899
900
901
902
903
904

905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925



926
927
928
namespace eval SelectColor {
    Widget::define SelectColor color Dialog

    Widget::declare SelectColor {
        {-title      String     "Select a color" 0}
        {-parent     String     ""               0}
        {-variable   String     ""               1}
        {-help       Boolean    0                1}
        {-color      TkResource ""               0 {label -background}}
	{-type       Enum       "dialog"         1 {dialog popup}}
	{-placement  String     "center"         1}
        {-background TkResource ""               0 {label -background}}
    }

    variable _baseColors {
................................................................................

    variable _selectype
    variable _selection
    variable _wcolor
    variable _image
    variable _hsv

    variable _varName
    variable _unsavedSelection
    variable _oldColor
    variable _entryColor
    variable _bgColor
    variable _fgColor
    variable _rounds
    variable _help
................................................................................

proc SelectColor::menu {path placement args} {
    variable _baseColors
    variable _userColors
    variable _wcolor
    variable _selectype
    variable _selection
    variable _varName
    variable _unsavedSelection
    variable _bgColor
    variable _rounds

    Widget::init SelectColor $path $args
    set top [toplevel $path]
    set parent [winfo toplevel [winfo parent $top]]
    wm withdraw  $top
    wm transient $top $parent
    wm overrideredirect $top 1
    catch { wm attributes $top -topmost 1 }

    set _varName [Widget::cget $path -variable]
    if {! [string equal [string range $_varName 0 1] {::}]} {
        set _varName ::SelectColor::_unsavedSelection
    }

    set _bgColor  [Widget::cget $path -background]
    set _rounds   {}

    set frame [frame $top.frame \
                   -highlightthickness 0 \
                   -relief raised -borderwidth 2]
    set col    0
................................................................................
	    }
	}
	# Call BWidget dialog
	return [eval [list dialog $path] $args]
    } else {
	# The user has either selected one of the palette colors, or has
	# cancelled.  The full BWidget/native dialog was not called.
	# Unless the user has cancelled, record the selected
	# color in $_varName which may be traced by the caller.
	set tmpCol [lindex $colors $_selection]
	if {[string equal $tmpCol ""]} {
	    # User has cancelled - no need to set $_varName
	} else {
	    set $_varName $tmpCol
	}
	return $tmpCol
    }
}












proc SelectColor::dialog {path args} {
    variable _baseColors
    variable _userColors
    variable _widget
    variable _selection
    variable _image
    variable _hsv
    variable _varName
    variable _unsavedSelection
    variable _oldColor
    variable _entryColor
    variable _bgColor
    variable _fgColor
    variable _rounds
    variable _help
................................................................................
    wm resizable $top 0 0
    set dlgf  [$top getframe]
    set fg    [frame $dlgf.fg]
    set desc  [list \
                   base _baseColors "Base colors" \
                   user _userColors "User colors"]

    set _help [Widget::cget $path:SelectColor -help]

    # Initialize _varName
    # The initial value of [$w cget -variable] is ignored and is
    # overwritten with [$w cget -color].
    set _varName [Widget::cget $path:SelectColor -variable]
    if {! [string equal [string range $_varName 0 1] {::}]} {
        set _varName ::SelectColor::_unsavedSelection
    }

    # Initialize _bgColor and _rounds
    set _bgColor [Widget::cget $path:SelectColor -background]
    set _rounds  {}

    set count 0
    foreach {type varcol defTitle} $desc {
        set col   0
        set lin   0
................................................................................
    set _widget(cv)     $c2
    set rgb             [winfo rgb $path [Widget::cget $path:SelectColor -color]]
    set _hsv            [eval rgbToHsv $rgb]
    _set_rgb     [eval [list format "\#%04x%04x%04x"] $rgb]
    _set_hue_sat [lindex $_hsv 0] [lindex $_hsv 1]
    _set_value   [lindex $_hsv 2]

    # Initialize _oldColor which is used to reset the traced -variable
    # if the user cancels.
    set _oldColor [set $_varName]
    set tmp24     [::SelectColor::_24BitRgb $_oldColor]
    if {[_ValidateColorEntry forced $tmp24]} {
        set ::SelectColor::_entryColor $tmp24
    } else {
        # Value $tmp24 does not pass entry widget validation and if used
        # would disable validation.  Use this default instead.
        set _entryColor #
    }

    # Validate input to the entry field.
    # To avoid conflict with the entry -variable (_entryColor), do not set the
    # latter directly (because a failed validation will switch off subsequent
    # validations).  Either call _SetEntryValue, or set $_varName which triggers
    # the trace.

    $fg.value configure -validate all -validatecommand \
            [list SelectColor::_ValidateColorEntry %V %P]

    # Trace $_varName
    # Subsequent modifications to $_varName will update the entry widget, if the
    # value is valid.
    # From now on, this is the only way that:
    # (1) ::SelectColor::_SetEntryValue is called
    # (2) ::SelectColor::_entryColor is modified (except by the user typing in
    #     the entry widget)

    trace add variable $_varName write ::SelectColor::_SetEntryValue

    $top add -text [lindex [BWidget::getname ok] 0]
    $top add -text [lindex [BWidget::getname cancel] 0]

    if {$_help} {
        # Add labels to offer DynamicHelp
        set helpWords         [lindex [BWidget::getname hoverHelp] 0]
................................................................................
    # Override background color
    ReColor $path $_bgColor

    set res [$top draw]
    if {$res == 0} {
        set color [$fg.color cget -background]
    } else {
        # User has cancelled - reset $_varName in case it is being traced

        set $_varName $_oldColor
        set color ""
    }

    trace remove variable $_varName write ::SelectColor::_SetEntryValue

    destroy $top
    return $color
}


# ----------------------------------------------------------------------------
................................................................................

proc SelectColor::_select_rgb {count} {
    variable _baseColors
    variable _userColors
    variable _selection
    variable _widget
    variable _hsv
    variable _varName
    variable _bgColor
    variable _fgColor

    set frame $_widget(fcolor)

    # Use highlight color instead of focus to identify the selected
    # palette color. Tab traversal of focus now works correctly.
................................................................................
        set _userColors [lreplace $_userColors $user $user $bg]
    } else {
        set _hsv [eval rgbToHsv [winfo rgb $frame.color$count $bg]]
        _set_hue_sat [lindex $_hsv 0] [lindex $_hsv 1]
        _set_value   [lindex $_hsv 2]
        $frame.color configure -background $bg

        # Display selected color in entry widget, and notify
        # caller (if the variable is being traced).
        set $_varName $bg

    }
}


proc SelectColor::_set_rgb {rgb} {
    variable _selection
    variable _baseColors
    variable _userColors
    variable _widget
    variable _varName

    set frame $_widget(fcolor)
    $frame.color configure -background $rgb

    # Display selected color in entry widget, and notify
    # aller (if the variable is being traced).
    set $_varName $rgb

    set user [expr {$_selection-[llength $_baseColors]}]
    if {$user >= 0} {
        $frame.color$_selection configure -background $rgb
        set _userColors [lreplace $_userColors $user $user $rgb]
    }
}

................................................................................
    } elseif {$lenny == 6} {
        # 24-bit, return unchanged
        return $col
    } else {
        # Truncate to 24-bit
        set delta  [expr {$lenny / 3}]
        set delta2 [expr {$delta * 2}]




        set result #
        append result [string range $col 1 2]
        append result [string range $col $delta+1  $delta+2]
        append result [string range $col $delta2+1 $delta2+2]
        return $result
    }
}


# ------------------------------------------------------------------------------
# Command SelectColor::_SetEntryValue
# ------------------------------------------------------------------------------
# Command to update the (hexadecimal color displayed in the) entry widget
# when there is a change in the color currently selected in the GUI, which is
# stored in $::SelectColor::_varName.
#
# This command is called by a write trace on $::SelectColor::_varName; if the
# value of this variable is a valid color (i.e. "#" followed by 3N hex digits),
# this command converts the value to 24 bits and sets ::SelectColor::_entryColor
# to the result, thereby displaying it in the entry widget.  Therefore,
# when the user chooses a color by means other than the entry widget, this
# command updates the entry widget.
#
# This command does not update the GUI when the user changes the value in the
# entry widget: that is done instead by the -vcmd of the entry widget, which
# is SelectColor::_ValidateColorEntry.  When the user chooses a color by typing
# in the entry widget, the command _ValidateColorEntry copies the value to
# $_varName if a keystroke in the widget makes its contents 3N hex digits long.

# ------------------------------------------------------------------------------

proc SelectColor::_SetEntryValue {argVarName var2 op} {
    variable _entryColor
    variable _varName

    if {0} {
    } elseif {[string equal $argVarName $_varName] &&
            [string equal $var2 {}] && [string equal $op "write"]} {
        # OK
    } else {
        # Unexpected call
        return -code error "Unexpected trace of variable\
                \"$argVarName\", \"$var2\", \"$op\""
    }
................................................................................
# Command SelectColor::_ValidateColorEntry
# ------------------------------------------------------------------------------
# This command is the "-validate all -vcmd" of the entry widget.
# It is also called by SelectColor::dialog and SelectColor::_SetEntryValue to
# check values assigned to _entryColor.
#
# When the user chooses a color by typing in the entry widget, this command
# copies the value to $_varName if a keystroke in the widget makes its contents
# 3N hex digits long.
# ------------------------------------------------------------------------------

proc SelectColor::_ValidateColorEntry {percentV percentP} {
    variable _varName

    set result [regexp -- {^#[0-9a-fA-F]*$} $percentP]
    set lenny  [string length $percentP]

    if {$result} {
        if {[string equal $percentV "forced"]} {
            # Validation only.  Don't want a loop.
................................................................................
            if {($lenny - 1) % 3 || $lenny == 1} {
                # Not a valid color, which needs 3n+1 characters, n > 0
            } else {
                after idle [list SelectColor::_SetWithoutTrace $percentP]
            }
        } elseif {[string equal $percentV "focusout"]} {
            # If the color is valid it will already have been copied to the GUI
            # by the "key" validation above.
            #
            # The code below only needs to reset the value in the entry widget.
            # Remove an invalid value, convert a valid one to 24-bit.
            # Ignore $percentP, just fire the trace on $_varName.

            after idle [list set $_varName [set $_varName]]
        }
    }

    return $result
}


# ------------------------------------------------------------------------------
# Command SelectColor::_SetWithoutTrace
# ------------------------------------------------------------------------------
# This command sets $_varName (using _set_rgb) without firing the trace that
# copies the value to _entryColor.
# The command is called by SelectColor::_ValidateColorEntry to avoid a loop.
# ------------------------------------------------------------------------------

proc SelectColor::_SetWithoutTrace {value} {
    variable _varName

    trace remove variable $_varName write ::SelectColor::_SetEntryValue
    _set_rgb $value



    trace add variable $_varName write ::SelectColor::_SetEntryValue
    return
}






|
|







 







|







 







|
<











|
<
<
<
<







 







|
|


|

|





>
>
>
>
>
>
>
>
>
>








|







 







|
<
<
<
<
|
<
<
<
<
<







 







|
|
|












|
|




|
|
|





|







 







|
>
|



|







 







|







 







|
|
|
>









|




|
|
|
>







 







>
>
>
>


|
|










|

|










|
>




|


|







 







|
|



|







 







|



|
>
|










|
|




<
<
|

>
>
>
|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
..
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
..
78
79
80
81
82
83
84
85

86
87
88
89
90
91
92
93
94
95
96
97




98
99
100
101
102
103
104
...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
...
228
229
230
231
232
233
234
235




236





237
238
239
240
241
242
243
...
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
...
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
...
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
...
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
...
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
...
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
...
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926


927
928
929
930
931
932
933
934
namespace eval SelectColor {
    Widget::define SelectColor color Dialog

    Widget::declare SelectColor {
        {-title      String     "Select a color" 0}
        {-parent     String     ""               0}
        {-command    String     ""               0}
        {-help       Boolean    1                1}
        {-color      TkResource ""               0 {label -background}}
	{-type       Enum       "dialog"         1 {dialog popup}}
	{-placement  String     "center"         1}
        {-background TkResource ""               0 {label -background}}
    }

    variable _baseColors {
................................................................................

    variable _selectype
    variable _selection
    variable _wcolor
    variable _image
    variable _hsv

    variable _command
    variable _unsavedSelection
    variable _oldColor
    variable _entryColor
    variable _bgColor
    variable _fgColor
    variable _rounds
    variable _help
................................................................................

proc SelectColor::menu {path placement args} {
    variable _baseColors
    variable _userColors
    variable _wcolor
    variable _selectype
    variable _selection
    variable _command

    variable _bgColor
    variable _rounds

    Widget::init SelectColor $path $args
    set top [toplevel $path]
    set parent [winfo toplevel [winfo parent $top]]
    wm withdraw  $top
    wm transient $top $parent
    wm overrideredirect $top 1
    catch { wm attributes $top -topmost 1 }

    set _command  [Widget::cget $path -command]




    set _bgColor  [Widget::cget $path -background]
    set _rounds   {}

    set frame [frame $top.frame \
                   -highlightthickness 0 \
                   -relief raised -borderwidth 2]
    set col    0
................................................................................
	    }
	}
	# Call BWidget dialog
	return [eval [list dialog $path] $args]
    } else {
	# The user has either selected one of the palette colors, or has
	# cancelled.  The full BWidget/native dialog was not called.
	# Unless the user has cancelled, pass the selected
	# color to _userCommand.
	set tmpCol [lindex $colors $_selection]
	if {[string equal $tmpCol ""]} {
	    # User has cancelled - no need to call _userCommand.
	} else {
	    _userCommand $tmpCol
	}
	return $tmpCol
    }
}


proc SelectColor::_userCommand {color} {
    variable _command
    if {[string equal $_command {}]} {
        return
    }
    uplevel #0 $_command [list $color]
    return
}


proc SelectColor::dialog {path args} {
    variable _baseColors
    variable _userColors
    variable _widget
    variable _selection
    variable _image
    variable _hsv
    variable _command
    variable _unsavedSelection
    variable _oldColor
    variable _entryColor
    variable _bgColor
    variable _fgColor
    variable _rounds
    variable _help
................................................................................
    wm resizable $top 0 0
    set dlgf  [$top getframe]
    set fg    [frame $dlgf.fg]
    set desc  [list \
                   base _baseColors "Base colors" \
                   user _userColors "User colors"]

    set _help    [Widget::cget $path:SelectColor -help]




    set _command [Widget::cget $path:SelectColor -command]





    set _bgColor [Widget::cget $path:SelectColor -background]
    set _rounds  {}

    set count 0
    foreach {type varcol defTitle} $desc {
        set col   0
        set lin   0
................................................................................
    set _widget(cv)     $c2
    set rgb             [winfo rgb $path [Widget::cget $path:SelectColor -color]]
    set _hsv            [eval rgbToHsv $rgb]
    _set_rgb     [eval [list format "\#%04x%04x%04x"] $rgb]
    _set_hue_sat [lindex $_hsv 0] [lindex $_hsv 1]
    _set_value   [lindex $_hsv 2]

    # Initialize _oldColor which is used to reset the color supplied to
    # _userCommand if the user cancels.
    set _oldColor [set _unsavedSelection]
    set tmp24     [::SelectColor::_24BitRgb $_oldColor]
    if {[_ValidateColorEntry forced $tmp24]} {
        set ::SelectColor::_entryColor $tmp24
    } else {
        # Value $tmp24 does not pass entry widget validation and if used
        # would disable validation.  Use this default instead.
        set _entryColor #
    }

    # Validate input to the entry field.
    # To avoid conflict with the entry -variable (_entryColor), do not set the
    # latter directly (because a failed validation will switch off subsequent
    # validations).  Either call _SetEntryValue, or set _unsavedSelection which
    # triggers the trace.

    $fg.value configure -validate all -validatecommand \
            [list SelectColor::_ValidateColorEntry %V %P]

    # Trace _unsavedSelection
    # Subsequent modifications to _unsavedSelection will update the entry
    # widget, if the value is valid.
    # From now on, this is the only way that:
    # (1) ::SelectColor::_SetEntryValue is called
    # (2) ::SelectColor::_entryColor is modified (except by the user typing in
    #     the entry widget)

    trace add variable ::SelectColor::_unsavedSelection write ::SelectColor::_SetEntryValue

    $top add -text [lindex [BWidget::getname ok] 0]
    $top add -text [lindex [BWidget::getname cancel] 0]

    if {$_help} {
        # Add labels to offer DynamicHelp
        set helpWords         [lindex [BWidget::getname hoverHelp] 0]
................................................................................
    # Override background color
    ReColor $path $_bgColor

    set res [$top draw]
    if {$res == 0} {
        set color [$fg.color cget -background]
    } else {
        # User has cancelled - call _userCommand to undo any changes made
        # in the caller.
        _userCommand $_oldColor
        set color ""
    }

    trace remove variable ::SelectColor::_unsavedSelection write ::SelectColor::_SetEntryValue

    destroy $top
    return $color
}


# ----------------------------------------------------------------------------
................................................................................

proc SelectColor::_select_rgb {count} {
    variable _baseColors
    variable _userColors
    variable _selection
    variable _widget
    variable _hsv
    variable _unsavedSelection
    variable _bgColor
    variable _fgColor

    set frame $_widget(fcolor)

    # Use highlight color instead of focus to identify the selected
    # palette color. Tab traversal of focus now works correctly.
................................................................................
        set _userColors [lreplace $_userColors $user $user $bg]
    } else {
        set _hsv [eval rgbToHsv [winfo rgb $frame.color$count $bg]]
        _set_hue_sat [lindex $_hsv 0] [lindex $_hsv 1]
        _set_value   [lindex $_hsv 2]
        $frame.color configure -background $bg

        # Display selected color in entry widget (via trace on
        # ::SelectColor::_unsavedSelection), and notify caller.
        set ::SelectColor::_unsavedSelection $bg
        _userCommand $bg
    }
}


proc SelectColor::_set_rgb {rgb} {
    variable _selection
    variable _baseColors
    variable _userColors
    variable _widget
    variable _unsavedSelection

    set frame $_widget(fcolor)
    $frame.color configure -background $rgb

    # Display selected color in entry widget (via trace on
    # ::SelectColor::_unsavedSelection), and notify caller.
    set ::SelectColor::_unsavedSelection $rgb
    _userCommand $rgb
    set user [expr {$_selection-[llength $_baseColors]}]
    if {$user >= 0} {
        $frame.color$_selection configure -background $rgb
        set _userColors [lreplace $_userColors $user $user $rgb]
    }
}

................................................................................
    } elseif {$lenny == 6} {
        # 24-bit, return unchanged
        return $col
    } else {
        # Truncate to 24-bit
        set delta  [expr {$lenny / 3}]
        set delta2 [expr {$delta * 2}]
        set deltaP1  [incr delta]
        set deltaP2  [incr delta]
        set delta2P1 [incr delta2]
        set delta2P2 [incr delta2]
        set result #
        append result [string range $col 1 2]
        append result [string range $col $deltaP1  $deltaP2]
        append result [string range $col $delta2P1 $delta2P2]
        return $result
    }
}


# ------------------------------------------------------------------------------
# Command SelectColor::_SetEntryValue
# ------------------------------------------------------------------------------
# Command to update the (hexadecimal color displayed in the) entry widget
# when there is a change in the color currently selected in the GUI, which is
# stored in _unsavedSelection.
#
# This command is called by a write trace on _unsavedSelection; if the
# value of this variable is a valid color (i.e. "#" followed by 3N hex digits),
# this command converts the value to 24 bits and sets ::SelectColor::_entryColor
# to the result, thereby displaying it in the entry widget.  Therefore,
# when the user chooses a color by means other than the entry widget, this
# command updates the entry widget.
#
# This command does not update the GUI when the user changes the value in the
# entry widget: that is done instead by the -vcmd of the entry widget, which
# is SelectColor::_ValidateColorEntry.  When the user chooses a color by typing
# in the entry widget, the command _ValidateColorEntry copies the value to
# _unsavedSelection if a keystroke in the widget makes its contents 3N hex
# digits long.
# ------------------------------------------------------------------------------

proc SelectColor::_SetEntryValue {argVarName var2 op} {
    variable _entryColor
    variable _unsavedSelection

    if {0} {
    } elseif {[string equal $argVarName ::SelectColor::_unsavedSelection] &&
            [string equal $var2 {}] && [string equal $op "write"]} {
        # OK
    } else {
        # Unexpected call
        return -code error "Unexpected trace of variable\
                \"$argVarName\", \"$var2\", \"$op\""
    }
................................................................................
# Command SelectColor::_ValidateColorEntry
# ------------------------------------------------------------------------------
# This command is the "-validate all -vcmd" of the entry widget.
# It is also called by SelectColor::dialog and SelectColor::_SetEntryValue to
# check values assigned to _entryColor.
#
# When the user chooses a color by typing in the entry widget, this command
# copies the value to _unsavedSelection if a keystroke in the widget makes its
# contents 3N hex digits long.
# ------------------------------------------------------------------------------

proc SelectColor::_ValidateColorEntry {percentV percentP} {
    variable _unsavedSelection

    set result [regexp -- {^#[0-9a-fA-F]*$} $percentP]
    set lenny  [string length $percentP]

    if {$result} {
        if {[string equal $percentV "forced"]} {
            # Validation only.  Don't want a loop.
................................................................................
            if {($lenny - 1) % 3 || $lenny == 1} {
                # Not a valid color, which needs 3n+1 characters, n > 0
            } else {
                after idle [list SelectColor::_SetWithoutTrace $percentP]
            }
        } elseif {[string equal $percentV "focusout"]} {
            # If the color is valid it will already have been copied to the GUI
            # and to _userCommand by the "key" validation above.
            #
            # The code below only needs to reset the value in the entry widget.
            # Remove an invalid value, convert a valid one to 24-bit.
            # Ignore $percentP, just fire the trace on _unsavedSelection.
            set color $_unsavedSelection
            after idle [list set ::SelectColor::_unsavedSelection $color]
        }
    }

    return $result
}


# ------------------------------------------------------------------------------
# Command SelectColor::_SetWithoutTrace
# ------------------------------------------------------------------------------
# This command sets _unsavedSelection (using _set_rgb) without firing the trace
# that copies the value to _entryColor.
# The command is called by SelectColor::_ValidateColorEntry to avoid a loop.
# ------------------------------------------------------------------------------

proc SelectColor::_SetWithoutTrace {value} {


    trace remove variable ::SelectColor::_unsavedSelection write ::SelectColor::_SetEntryValue
    _set_rgb $value
    set _hsv [eval rgbToHsv [winfo rgb . $value]]
    _set_hue_sat [lindex $_hsv 0] [lindex $_hsv 1]
    _set_value   [lindex $_hsv 2]
    trace add variable ::SelectColor::_unsavedSelection write ::SelectColor::_SetEntryValue
    return
}

Changes to demo/demo.tcl.

194
195
196
197
198
199
200












201
202
203
204
205
206
207
208
209
210
211
212
    variable DEMODIR

    lappend ::auto_path [file dirname $DEMODIR]
    package require BWidget

    option add *TitleFrame.l.font {helvetica 11 bold italic}













    wm withdraw .
    wm title . "BWidget demo"

    Demo::create
    BWidget::place . 0 0 center
    wm deiconify .
    raise .
    focus -force .
}

Demo::main
wm geom . [wm geom .]







>
>
>
>
>
>
>
>
>
>
>
>












194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    variable DEMODIR

    lappend ::auto_path [file dirname $DEMODIR]
    package require BWidget

    option add *TitleFrame.l.font {helvetica 11 bold italic}

    if {$::tk_version < 8.5} {
        set helpFont {helvetica 12}
    } else {
        set helpFont {TkDefaultFont 10}
    }

    DynamicHelp::configure      \
            -background #FFFFC0 \
            -foreground #141312 \
            -padx       3       \
            -font       $helpFont

    wm withdraw .
    wm title . "BWidget demo"

    Demo::create
    BWidget::place . 0 0 center
    wm deiconify .
    raise .
    focus -force .
}

Demo::main
wm geom . [wm geom .]

Changes to demo/tmpldlg.tcl.

138
139
140
141
142
143
144
145

146
147
148
149
150
151
152
                   -command DemoDlg::_show_passdlg]

    pack $but0 $but1 $but2 $but3 -side left -padx 5 -anchor w
}

proc DemoDlg::_show_color {w} {
    set color [SelectColor::menu $w.color [list below $w] \
                       -color [$w cget -background]]

    if {[string length $color]} {
        $w configure -background $color
    }
}

proc DemoDlg::_show_tmpldlg { } {
    variable tmpl







|
>







138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                   -command DemoDlg::_show_passdlg]

    pack $but0 $but1 $but2 $but3 -side left -padx 5 -anchor w
}

proc DemoDlg::_show_color {w} {
    set color [SelectColor::menu $w.color [list below $w] \
                       -color   [$w cget -background]     \
                       -command [list $w configure -background]]
    if {[string length $color]} {
        $w configure -background $color
    }
}

proc DemoDlg::_show_tmpldlg { } {
    variable tmpl