Debian Codegraph @

demos/dnd1.tcl

raw
1 #!../src/bltwish
2
3 if {[lindex $argv end] != "spawn"} {
4 exec [info nameofexecutable] [info script] spawn &
5 }
6
7 package require BLT
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 proc OnEnter { widget args } {
41 array set info $args
42 $widget configure -highlightbackground red
43 return 1
44 }
45
46 proc OnMotion { widget args } {
47 array set info $args
48 set x1 [$widget cget -bd]
49 set x1 20
50 set y1 $x1
51 set x2 [expr [winfo width $widget] - $x1]
52 set y2 [expr [winfo height $widget] - $y1]
53 if { ($info(x) >= $x1) && ($info(x) <= $x2) &&
54 ($info(y) >= $y1) && ($info(y) <= $y2) } {
55 $widget configure -highlightbackground red
56 return 1
57 }
58 $widget configure -highlightbackground grey
59 return 0
60 }
61
62 proc OnLeave { widget args } {
63 $widget configure -highlightbackground grey
64 return 0
65 }
66
67 option add *OnEnter OnEnter
68 option add *OnLeave OnLeave
69 option add *OnMotion OnMotion
70
71 # ----------------------------------------------------------------------
72 # This procedure is invoked each time a token is grabbed from the
73 # sample window. It configures the token to display the current
74 # color, and returns the color value that is later passed to the
75 # target handler.
76 # ----------------------------------------------------------------------
77
78 proc PackageSample { widget args } {
79 array set info $args
80 set bg [.sample cget -background]
81 set fg [.sample cget -foreground]
82 $info(token).label configure -background $bg -foreground $fg
83 return 1
84 }
85
86 proc ShowResult { widget args } {
87 array set info $args
88 puts "drop transaction($info(timestamp)) completed: result was $info(action)"
89 }
90
91
92 # ----------------------------------------------------------------------
93 # Main application window...
94 # ----------------------------------------------------------------------
95 image create photo openFolder -format gif -data {
96 R0lGODdhEAAOAPIAAP///wAAAH9/f9nZ2f//AAAAAAAAAAAAACwAAAAAEAAOAAADOwgqzPoQ
97 iDjjAoPkIZuTgCZykBCA2ziaXusRrFUGQ5zeRMCcE76xvJBPozuBVCmT0eUKGAHOqFQqqwIS
98 ADs=
99 }
100 label .sample -text "Color" -height 12 -width 20 -bd 2 -relief raised \
101 -highlightthickness 2
102
103 set cursors {
104 { @bitmaps/hand/hand01.xbm bitmaps/hand/hand01m.xbm black white }
105 { @bitmaps/hand/hand02.xbm bitmaps/hand/hand02m.xbm black white }
106 { @bitmaps/hand/hand03.xbm bitmaps/hand/hand03m.xbm black white }
107 { @bitmaps/hand/hand04.xbm bitmaps/hand/hand04m.xbm black white }
108 { @bitmaps/hand/hand05.xbm bitmaps/hand/hand05m.xbm black white }
109 { @bitmaps/hand/hand06.xbm bitmaps/hand/hand06m.xbm black white }
110 { @bitmaps/hand/hand07.xbm bitmaps/hand/hand07m.xbm black white }
111 { @bitmaps/hand/hand08.xbm bitmaps/hand/hand08m.xbm black white }
112 { @bitmaps/hand/hand09.xbm bitmaps/hand/hand09m.xbm black white }
113 { @bitmaps/hand/hand10.xbm bitmaps/hand/hand10m.xbm black white }
114 { @bitmaps/hand/hand11.xbm bitmaps/hand/hand11m.xbm black white }
115 { @bitmaps/hand/hand12.xbm bitmaps/hand/hand12m.xbm black white }
116 { @bitmaps/hand/hand13.xbm bitmaps/hand/hand13m.xbm black white }
117 { @bitmaps/hand/hand14.xbm bitmaps/hand/hand14m.xbm black white }
118 }
119
120
121 # Set up the color sample as a drag&drop source and target for "color" values:
122 dnd register .sample -source yes -target yes \
123 -package PackageSample \
124 -result ShowResult \
125 -cursors $cursors
126
127 dnd getdata .sample color GetColor
128 dnd setdata .sample color SetColor
129
130 # Establish the appearance of the token window:
131 set token [dnd token window .sample]
132 label $token.label -text "Color" -bd 2 -highlightthickness 1
133 pack $token.label
134 dnd token configure .sample -borderwidth 2 \
135 -relief raised -activerelief raised \
136 -outline pink -fill red \
137 -anchor s
138
139 if 1 {
140 scale .redScale -label "Red" -orient horizontal \
141 -from 0 -to 255 -command adjust_color
142 frame .red -width 20 -height 20 -borderwidth 3 -relief sunken
143
144 scale .greenScale -label "Green" -orient horizontal \
145 -from 0 -to 255 -command adjust_color
146 frame .green -width 20 -height 20 -borderwidth 3 -relief sunken
147
148 scale .blueScale -label "Blue" -orient horizontal \
149 -from 0 -to 255 -command adjust_color
150 frame .blue -width 20 -height 20 -borderwidth 3 -relief sunken
151
152 # ----------------------------------------------------------------------
153 # This procedure loads a new color value into this editor.
154 # ----------------------------------------------------------------------
155 proc GetColor { widget args } {
156 return [$widget cget -bg]
157 }
158
159 proc SetColor { widget args } {
160 array set info $args
161 set rgb [winfo rgb . $info(value)]
162 set r [lindex $rgb 0]
163 set g [lindex $rgb 1]
164 set b [lindex $rgb 2]
165
166 .redScale set [expr round($r/65535.0 * 255)]
167 .greenScale set [expr round($g/65535.0 * 255)]
168 .blueScale set [expr round($b/65535.0 * 255)]
169 }
170
171 # ----------------------------------------------------------------------
172 # This procedure is invoked whenever an RGB slider changes to
173 # update the color samples in this display.
174 # ----------------------------------------------------------------------
175 proc adjust_color {args} {
176 set rval [.redScale get]
177 .red configure -background [format "#%.2x0000" $rval]
178 set gval [.greenScale get]
179 .green configure -background [format "#00%.2x00" $gval]
180 set bval [.blueScale get]
181 .blue configure -background [format "#0000%.2x" $bval]
182
183 .sample configure -background \
184 [format "#%.2x%.2x%.2x" $rval $gval $bval]
185 if {$rval+$gval+$bval < 1.5*255} {
186 .sample configure -foreground white
187 } else {
188 .sample configure -foreground black
189 }
190 }
191 table . .redScale 1,0 -fill both
192 table . .red 1,1 -fill both
193 table . .greenScale 2,0 -fill both
194 table . .green 2,1 -fill both
195 table . .blueScale 3,0 -fill both
196 table . .blue 3,1 -fill both
197
198 }
199 table . .sample 0,0 -columnspan 2 -fill both -pady {0 4}
200
201 proc random {{max 1.0} {min 0.0}} {
202 global randomSeed
203
204 set randomSeed [expr (7141*$randomSeed+54773) % 259200]
205 set num [expr $randomSeed/259200.0*($max-$min)+$min]
206 return $num
207 }
208 set randomSeed [clock clicks]
209
210 .redScale set [expr round([random 255.0])]
211 .blueScale set [expr round([random 255.0])]
212 .greenScale set [expr round([random 255.0])]
213 bind .sample <KeyPress-Escape> { dnd cancel .sample }
214 focus .sample
215
216