Debian Codegraph @

demos/dragdrop2.tcl

raw
1 #!../src/bltwish
2 if {[lindex $argv end] != "spawn"} {
3 exec [info nameofexecutable] [info script] spawn &
4 }
5
6 package require BLT
7
8
9 # --------------------------------------------------------------------------
10 # Starting with Tcl 8.x, the BLT commands are stored in their own
11 # namespace called "blt". The idea is to prevent name clashes with
12 # Tcl commands and variables from other packages, such as a "table"
13 # command in two different packages.
14 #
15 # You can access the BLT commands in a couple of ways. You can prefix
16 # all the BLT commands with the namespace qualifier "blt::"
17 #
18 # blt::graph .g
19 # blt::table . .g -resize both
20 #
21 # or you can import all the command into the global namespace.
22 #
23 # namespace import blt::*
24 # graph .g
25 # table . .g -resize both
26 #
27 # --------------------------------------------------------------------------
28 if { $tcl_version >= 8.0 } {
29 namespace import blt::*
30 namespace import -force blt::tile::*
31 }
32 source scripts/demo.tcl
33
34 if { ([info exists tcl_platform]) && ($tcl_platform(platform) == "windows") } {
35 source scripts/send.tcl
36 SendInit
37 SendVerify
38 }
39
40 # ----------------------------------------------------------------------
41 # This procedure is invoked each time a token is grabbed from the
42 # sample window. It configures the token to display the current
43 # color, and returns the color value that is later passed to the
44 # target handler.
45 # ----------------------------------------------------------------------
46
47 proc package_color {token} {
48 set bg [.sample cget -background]
49 set fg [.sample cget -foreground]
50
51 $token.label configure -text "Color" -background $bg -foreground $fg
52 return $bg
53 }
54
55 # ----------------------------------------------------------------------
56 # This procedure is invoked each time a token is grabbed from an
57 # entry widget. It configures the token to display the current
58 # string, and returns the string that is later passed to the target
59 # handler.
60 # ----------------------------------------------------------------------
61 proc package_string {str token} {
62 if {[string length $str] > 20} {
63 set mesg "[string range $str 0 19]..."
64 } else {
65 set mesg $str
66 }
67 $token.label configure -text $mesg
68 return $str
69 }
70
71 # ----------------------------------------------------------------------
72 # Main application window...
73 # ----------------------------------------------------------------------
74 label .sample -text "Color" -height 2 -borderwidth 3 -relief sunken
75
76 #
77 # Set up the color sample as a drag&drop source for "color" values
78 # and "string" values
79 #
80 drag&drop source .sample -packagecmd {package_color %t}
81 drag&drop source .sample handler color
82 drag&drop source .sample handler string
83
84 #
85 # Set up the color sample as a drag&drop target for "color" values:
86 #
87 drag&drop target .sample handler color {set_color %v}
88
89 #
90 # Establish the appearance of the token window:
91 #
92 set token [drag&drop token .sample -activebackground yellow ]
93 label $token.label -text "Color"
94 pack $token.label
95
96 scale .redScale -label "Red" -orient horizontal \
97 -from 0 -to 255 -command adjust_color
98 frame .redSample -width 20 -height 20 -borderwidth 3 -relief sunken
99
100 scale .greenScale -label "Green" -orient horizontal \
101 -from 0 -to 255 -command adjust_color
102 frame .greenSample -width 20 -height 20 -borderwidth 3 -relief sunken
103
104 scale .blueScale -label "Blue" -orient horizontal \
105 -from 0 -to 255 -command adjust_color
106 frame .blueSample -width 20 -height 20 -borderwidth 3 -relief sunken
107
108 frame .color
109 label .color.label -text "Color:"
110 pack .color.label -side left
111 entry .color.value -width 10
112 pack .color.value -side left -expand yes -fill both
113
114 bind .color.value <KeyPress-Return> {set_color [%W get]}
115
116 #
117 # Set up the entry widget as a drag&drop source for "string" values:
118 #
119 drag&drop source .color.value \
120 -packagecmd {package_string [%W get] %t} \
121 -selftarget yes
122 drag&drop source .color.value handler string
123
124 #
125 # Set up the entry widget as a drag&drop target for "string" values:
126 #
127 drag&drop target .color.value handler string {
128 %W delete 0 end
129 %W insert 0 "%v"
130 }
131
132 #
133 # Establish the appearance of the token window:
134 #
135 set token [drag&drop token .color.value]
136 label $token.label
137 pack $token.label
138
139 # ----------------------------------------------------------------------
140 # This procedure loads a new color value into this editor.
141 # ----------------------------------------------------------------------
142 proc set_color {cval} {
143 set rgb [winfo rgb . $cval]
144
145 set rval [expr round([lindex $rgb 0]/65535.0*255)]
146 .redScale set $rval
147
148 set gval [expr round([lindex $rgb 1]/65535.0*255)]
149 .greenScale set $gval
150
151 set bval [expr round([lindex $rgb 2]/65535.0*255)]
152 .blueScale set $bval
153 }
154
155 # ----------------------------------------------------------------------
156 # This procedure is invoked whenever an RGB slider changes to
157 # update the color samples in this display.
158 # ----------------------------------------------------------------------
159 proc adjust_color {args} {
160 set rval [.redScale get]
161 .redSample configure -background [format "#%.2x0000" $rval]
162 set gval [.greenScale get]
163 .greenSample configure -background [format "#00%.2x00" $gval]
164 set bval [.blueScale get]
165 .blueSample configure -background [format "#0000%.2x" $bval]
166
167 .sample configure -background \
168 [format "#%.2x%.2x%.2x" $rval $gval $bval]
169 if {$rval+$gval+$bval < 1.5*255} {
170 .sample configure -foreground white
171 } else {
172 .sample configure -foreground black
173 }
174 }
175
176 table . \
177 0,0 .sample -columnspan 2 -pady {0 4} \
178 1,0 .color -columnspan 2 -padx 4 -pady 4 \
179 2,0 .redScale \
180 2,1 .redSample \
181 3,0 .greenScale \
182 3,1 .greenSample \
183 4,0 .blueScale \
184 4,1 .blueSample
185
186 eval table configure . [winfo children .] -fill both