3 if {[lindex $argv end] != "spawn"} {
4 exec [info nameofexecutable] [info script] spawn &
7 # --------------------------------------------------------------------------
8 # Starting with Tcl 8.x, the BLT commands are stored in their own
9 # namespace called "blt". The idea is to prevent name clashes with
10 # Tcl commands and variables from other packages, such as a "table"
11 # command in two different packages.
13 # You can access the BLT commands in a couple of ways. You can prefix
14 # all the BLT commands with the namespace qualifier "blt::"
17 # blt::table . .g -resize both
19 # or you can import all the command into the global namespace.
21 # namespace import blt::*
23 # table . .g -resize both
25 # --------------------------------------------------------------------------
26 if { $tcl_version >= 8.0 } {
27 namespace import blt::*
28 namespace import -force blt::tile::*
30 source scripts/demo.tcl
32 if { ([info exists tcl_platform]) && ($tcl_platform(platform) == "windows") } {
33 error "This script works only under X11"
36 canvas .c -width 320 -height 320 -background white
38 blt::table . .c -fill both
43 proc RedrawWorld { canvas } {
44 global cells cellWidth cellHeight
48 set width [winfo width $canvas]
49 set height [winfo height $canvas]
51 set cellWidth [expr $width / 8]
52 set cellHeight [expr $height / 8]
54 for { set row 0 } { $row < 8 } { incr row } {
55 set y [expr $row * $cellHeight]
56 set h [expr $y + $cellHeight]
57 for { set column 0 } { $column < 8 } { incr column } {
58 set x [expr $column * $cellWidth]
59 set w [expr $x + $cellWidth]
60 $canvas create rectangle $x $y $w $h -fill white -outline "" \
65 for { set row 0 } { $row < 8 } { incr row } {
66 set y [expr $row * $cellHeight]
67 $canvas create line 0 $y $width $y
70 for { set column 0 } { $column < 8 } { incr column } {
71 set x [expr $column * $cellWidth]
72 $canvas create line $x 0 $x $height
74 foreach name [array names cells] {
75 set rc [split $name ,]
76 set row [lindex $rc 0]
77 set column [lindex $rc 1]
78 set x [expr ($column * $cellWidth) + 5]
79 set y [expr ($row * $cellHeight) + 5]
80 set w [expr $cellWidth - 10]
81 set h [expr $cellHeight - 10]
82 set color [lindex $cells($name) 0]
83 set type [lindex $cells($name) 1]
84 set pi1_2 [expr 3.14159265358979323846/180.0]
88 lappend points $x [expr $y + $h/2] [expr $x + $w * 1/3] \
89 $y [expr $x + $w * 2/3] $y [expr $x + $w] [expr $y + $h/2] \
90 [expr $x + $w * 2/3] [expr $y + $h] \
91 [expr $x + $w * 1/3] [expr $y + $h]
94 lappend points $x [expr $y + $h * 2/3] \
95 [expr $x + $w * 2/3] $y \
96 [expr $x + $w] [expr $y + $h * 1/3] \
97 [expr $x + $w * 1/3] [expr $y + $h]
102 [expr $x + $w * 1/2] $y \
103 [expr $x + $w] [expr $y + $h]
106 eval .c create polygon $points -fill $color -outline black
110 bind .c <Configure> { RedrawWorld %W }
112 # ----------------------------------------------------------------------
113 # USAGE: random ?<max>? ?<min>?
115 # Returns a random number in the range <min> to <max>.
116 # If <min> is not specified, the default is 0; if max is not
117 # specified, the default is 1.
118 # ----------------------------------------------------------------------
120 proc random {{max 1.0} {min 0.0}} {
123 set randomSeed [expr (7141*$randomSeed+54773) % 259200]
124 set num [expr $randomSeed/259200.0*($max-$min)+$min]
127 set randomSeed [clock clicks]
129 set itemTypes { parallelogram hexagon triangle }
130 set itemTypes { hexagon triangle parallelogram }
132 for { set i 0 } { $i < 20 } { incr i } {
134 set row [expr int([random 8])]
135 set column [expr int([random 8])]
136 set type [expr int([random 3])]
137 set type [lindex $itemTypes $type]
138 if { ![info exists cells($row,$column)] } {
139 set r [expr int([random 256 128])]
140 set g [expr int([random 256 128])]
141 set b [expr int([random 256 128])]
142 set cells($row,$column) [format "#%.2x%.2x%.2x %s" $r $g $b $type]
148 proc ScreenToCell { widget x y } {
149 global cellWidth cellHeight
150 set column [expr $x / $cellWidth]
151 set row [expr $y / $cellHeight]
157 foreach i [winfo interps] {
159 if { [string match "dnd2.tcl*" $i] } {
167 text .info.text -width 65 -height 12 -font { Helvetica 10 } -bg white \
169 .info.text insert end {
170 This is a more involved example of the new "dnd" command.
171 Run this script again to get another window. You can then drag
172 and drop symbols between the windows by clicking with the left
173 mouse button on a symbol.
175 It demonstates how to
176 o Drag-and-drop on specific areas (canvas items) of a widget.
177 o How to receive and handle Enter/Leave/Motion events in the target.
178 o How to send drag feedback to the source.
179 o Use a drag threshold.
181 button .info.quit -text "Dismiss" -command { destroy .info }
183 0,0 .info.text -fill both \
188 # -----------------------------------------------------------------
190 # Setup finished. Start of drag-and-drop code here.
193 # Set up the entire canvas as a drag&drop source.
195 dnd register .c -source yes -dragthreshold 5 -button 1
197 # Register code to pick up the information about a canvas item
199 dnd getdata .c color GetColor
201 proc GetColor { widget args } {
204 set id $itemInfo($info(timestamp))
205 set color [$widget itemcget $id -fill]
206 set ncoords [llength [$widget coords $id]]
207 if { $ncoords == 6 } {
209 } elseif { $ncoords == 8 } {
210 set type parallelogram
211 } elseif { $ncoords == 12 } {
214 error "unknown type n=$ncoords"
216 return [list $color $type]
219 dnd configure .c -package PackageSample
221 proc PackageSample { widget args } {
224 # Check if we're over a canvas item
225 set items [$widget find overlapping $info(x) $info(y) $info(x) $info(y)]
228 if { [$widget type $i] == "polygon" } {
233 if { $pickedItem == "" } {
235 puts "Cancel the drag x=$info(x) y=$info(y)"
238 set fill [$widget itemcget $pickedItem -fill]
239 set outline [$widget itemcget $pickedItem -outline]
241 set ncoords [llength [$widget coords $pickedItem]]
242 if { $ncoords == 6 } {
244 } elseif { $ncoords == 8 } {
245 set type parallelogram
246 } elseif { $ncoords == 12 } {
249 error "unknown type n=$ncoords"
251 set tag [ScreenToCell $widget $info(x) $info(y)]
252 $info(token).label configure -background $fill -foreground $outline \
257 set itemInfo($info(timestamp)) $pickedItem
261 # Configure a set of animated cursors.
263 dnd configure .c -cursors {
264 { @bitmaps/hand/hand01.xbm bitmaps/hand/hand01m.xbm black white }
265 { @bitmaps/hand/hand02.xbm bitmaps/hand/hand02m.xbm black white }
266 { @bitmaps/hand/hand03.xbm bitmaps/hand/hand03m.xbm black white }
267 { @bitmaps/hand/hand04.xbm bitmaps/hand/hand04m.xbm black white }
268 { @bitmaps/hand/hand05.xbm bitmaps/hand/hand05m.xbm black white }
269 { @bitmaps/hand/hand06.xbm bitmaps/hand/hand06m.xbm black white }
270 { @bitmaps/hand/hand07.xbm bitmaps/hand/hand07m.xbm black white }
271 { @bitmaps/hand/hand08.xbm bitmaps/hand/hand08m.xbm black white }
272 { @bitmaps/hand/hand09.xbm bitmaps/hand/hand09m.xbm black white }
273 { @bitmaps/hand/hand10.xbm bitmaps/hand/hand10m.xbm black white }
274 { @bitmaps/hand/hand11.xbm bitmaps/hand/hand11m.xbm black white }
275 { @bitmaps/hand/hand12.xbm bitmaps/hand/hand12m.xbm black white }
276 { @bitmaps/hand/hand13.xbm bitmaps/hand/hand13m.xbm black white }
277 { @bitmaps/hand/hand14.xbm bitmaps/hand/hand14m.xbm black white }
280 # Create a widget to place in the drag-and-drop token
282 set token [dnd token window .c]
284 label $token.label -bd 2 -highlightthickness 1
286 dnd token configure .c \
288 -relief raised -activerelief raised \
289 -outline pink -fill red \
293 dnd configure .c -target yes
295 dnd setdata .c color {
299 proc NewObject { widget args } {
301 set tag [ScreenToCell $widget $info(x) $info(y)]
303 if { [info exists cells($tag)] } {
304 error "Cell already exists"
306 set cells($tag) $info(value)
311 dnd configure .c -onmotion OnMotion -onenter OnMotion -onleave OnMotion
313 proc OnMotion { widget args } {
314 global cells lastCell
317 set tag [ScreenToCell $widget $info(x) $info(y)]
318 if { $lastCell != "" } {
319 $widget itemconfigure $lastCell -fill white -outline "" -width 1 \
322 # Check that we're not over a canvas item
323 if { ![info exists cells($tag)] } {
324 $widget itemconfigure $tag -outline lightblue -fill lightblue \
325 -width 2 -stipple BLT