4 # --------------------------------------------------------------------------
5 # Starting with Tcl 8.x, the BLT commands are stored in their own
6 # namespace called "blt". The idea is to prevent name clashes with
7 # Tcl commands and variables from other packages, such as a "table"
8 # command in two different packages.
10 # You can access the BLT commands in a couple of ways. You can prefix
11 # all the BLT commands with the namespace qualifier "blt::"
14 # blt::table . .g -resize both
16 # or you can import all the command into the global namespace.
18 # namespace import blt::*
20 # table . .g -resize both
22 # --------------------------------------------------------------------------
23 if { $tcl_version >= 8.0 } {
24 namespace import blt::*
25 namespace import -force blt::tile::*
27 source scripts/demo.tcl
29 if { ([info exists tcl_platform]) && ($tcl_platform(platform) == "windows") } {
30 error "This script works only under X11"
33 canvas .c -width 320 -height 320 -background white
35 blt::table . .c -fill both
40 proc RedrawWorld { canvas } {
41 global cells cellWidth cellHeight
45 set width [winfo width $canvas]
46 set height [winfo height $canvas]
48 set cellWidth [expr $width / 8]
49 set cellHeight [expr $height / 8]
51 for { set row 0 } { $row < 8 } { incr row } {
52 set y [expr $row * $cellHeight]
53 set h [expr $y + $cellHeight]
54 for { set column 0 } { $column < 8 } { incr column } {
55 set x [expr $column * $cellWidth]
56 set w [expr $x + $cellWidth]
57 $canvas create rectangle $x $y $w $h -fill white -outline "" \
62 for { set row 0 } { $row < 8 } { incr row } {
63 set y [expr $row * $cellHeight]
64 $canvas create line 0 $y $width $y
67 for { set column 0 } { $column < 8 } { incr column } {
68 set x [expr $column * $cellWidth]
69 $canvas create line $x 0 $x $height
71 foreach name [array names cells] {
72 set rc [split $name ,]
73 set row [lindex $rc 0]
74 set column [lindex $rc 1]
75 set x [expr ($column * $cellWidth) + 5]
76 set y [expr ($row * $cellHeight) + 5]
77 set w [expr $cellWidth - 10]
78 set h [expr $cellHeight - 10]
79 set color [lindex $cells($name) 0]
80 set type [lindex $cells($name) 1]
81 set pi1_2 [expr 3.14159265358979323846/180.0]
85 lappend points $x [expr $y + $h/2] [expr $x + $w * 1/3] \
86 $y [expr $x + $w * 2/3] $y [expr $x + $w] [expr $y + $h/2] \
87 [expr $x + $w * 2/3] [expr $y + $h] \
88 [expr $x + $w * 1/3] [expr $y + $h]
91 lappend points $x [expr $y + $h * 2/3] \
92 [expr $x + $w * 2/3] $y \
93 [expr $x + $w] [expr $y + $h * 1/3] \
94 [expr $x + $w * 1/3] [expr $y + $h]
99 [expr $x + $w * 1/2] $y \
100 [expr $x + $w] [expr $y + $h]
103 eval .c create polygon $points -fill $color -outline black
107 bind .c <Configure> { RedrawWorld %W }
109 # ----------------------------------------------------------------------
110 # USAGE: random ?<max>? ?<min>?
112 # Returns a random number in the range <min> to <max>.
113 # If <min> is not specified, the default is 0; if max is not
114 # specified, the default is 1.
115 # ----------------------------------------------------------------------
117 proc random {{max 1.0} {min 0.0}} {
120 set randomSeed [expr (7141*$randomSeed+54773) % 259200]
121 set num [expr $randomSeed/259200.0*($max-$min)+$min]
124 set randomSeed [clock clicks]
126 set itemTypes { parallelogram hexagon triangle }
127 set itemTypes { hexagon triangle parallelogram }
129 for { set i 0 } { $i < 20 } { incr i } {
131 set row [expr int([random 8])]
132 set column [expr int([random 8])]
133 set type [expr int([random 3])]
134 set type [lindex $itemTypes $type]
135 if { ![info exists cells($row,$column)] } {
136 set r [expr int([random 256 128])]
137 set g [expr int([random 256 128])]
138 set b [expr int([random 256 128])]
139 set cells($row,$column) [format "#%.2x%.2x%.2x %s" $r $g $b $type]
145 proc ScreenToCell { widget x y } {
146 global cellWidth cellHeight
147 set column [expr $x / $cellWidth]
148 set row [expr $y / $cellHeight]
154 foreach i [winfo interps] {
156 if { [string match "dnd2.tcl*" $i] } {
164 text .info.text -width 65 -height 12 -font { Helvetica 10 } -bg white \
166 .info.text insert end {
167 This is a more involved example of the new "dnd" command.
168 Run this script again to get another window. You can then drag
169 and drop symbols between the windows by clicking with the left
170 mouse button on a symbol.
172 It demonstates how to
173 o Drag-and-drop on specific areas (canvas items) of a widget.
174 o How to receive and handle Enter/Leave/Motion events in the target.
175 o How to send drag feedback to the source.
176 o Use a drag threshold.
178 button .info.quit -text "Dismiss" -command { destroy .info }
180 0,0 .info.text -fill both \
185 # -----------------------------------------------------------------
187 # Setup finished. Start of drag-and-drop code here.
190 # Set up the entire canvas as a drag&drop source.
192 dnd register .c -source yes -dragthreshold 5 -button 1
194 # Register code to pick up the information about a canvas item
196 dnd getdata .c color GetColor
198 proc GetColor { widget args } {
201 set id $itemInfo($info(timestamp))
202 set color [$widget itemcget $id -fill]
203 set ncoords [llength [$widget coords $id]]
204 if { $ncoords == 6 } {
206 } elseif { $ncoords == 8 } {
207 set type parallelogram
208 } elseif { $ncoords == 12 } {
211 error "unknown type n=$ncoords"
213 return [list $color $type]
216 dnd configure .c -package PackageSample
218 proc PackageSample { widget args } {
221 # Check if we're over a canvas item
222 set items [$widget find overlapping $info(x) $info(y) $info(x) $info(y)]
225 if { [$widget type $i] == "polygon" } {
230 if { $pickedItem == "" } {
232 puts "Cancel the drag x=$info(x) y=$info(y)"
235 set fill [$widget itemcget $pickedItem -fill]
236 set outline [$widget itemcget $pickedItem -outline]
238 set ncoords [llength [$widget coords $pickedItem]]
239 if { $ncoords == 6 } {
241 } elseif { $ncoords == 8 } {
242 set type parallelogram
243 } elseif { $ncoords == 12 } {
246 error "unknown type n=$ncoords"
248 set tag [ScreenToCell $widget $info(x) $info(y)]
249 $info(token).label configure -background $fill -foreground $outline \
254 set itemInfo($info(timestamp)) $pickedItem
258 # Configure a set of animated cursors.
260 dnd configure .c -cursors {
261 { @bitmaps/hand/hand01.xbm bitmaps/hand/hand01m.xbm black white }
262 { @bitmaps/hand/hand02.xbm bitmaps/hand/hand02m.xbm black white }
263 { @bitmaps/hand/hand03.xbm bitmaps/hand/hand03m.xbm black white }
264 { @bitmaps/hand/hand04.xbm bitmaps/hand/hand04m.xbm black white }
265 { @bitmaps/hand/hand05.xbm bitmaps/hand/hand05m.xbm black white }
266 { @bitmaps/hand/hand06.xbm bitmaps/hand/hand06m.xbm black white }
267 { @bitmaps/hand/hand07.xbm bitmaps/hand/hand07m.xbm black white }
268 { @bitmaps/hand/hand08.xbm bitmaps/hand/hand08m.xbm black white }
269 { @bitmaps/hand/hand09.xbm bitmaps/hand/hand09m.xbm black white }
270 { @bitmaps/hand/hand10.xbm bitmaps/hand/hand10m.xbm black white }
271 { @bitmaps/hand/hand11.xbm bitmaps/hand/hand11m.xbm black white }
272 { @bitmaps/hand/hand12.xbm bitmaps/hand/hand12m.xbm black white }
273 { @bitmaps/hand/hand13.xbm bitmaps/hand/hand13m.xbm black white }
274 { @bitmaps/hand/hand14.xbm bitmaps/hand/hand14m.xbm black white }
277 # Create a widget to place in the drag-and-drop token
279 set token [dnd token window .c]
281 label $token.label -bd 2 -highlightthickness 1
283 dnd token configure .c \
285 -relief raised -activerelief raised \
286 -outline pink -fill red \
290 dnd configure .c -target yes
292 dnd setdata .c color {
296 proc NewObject { widget args } {
298 set tag [ScreenToCell $widget $info(x) $info(y)]
300 if { [info exists cells($tag)] } {
301 error "Cell already exists"
303 set cells($tag) $info(value)
308 dnd configure .c -onmotion OnMotion -onenter OnMotion -onleave OnMotion
310 proc OnMotion { widget args } {
311 global cells lastCell
314 set tag [ScreenToCell $widget $info(x) $info(y)]
315 if { $lastCell != "" } {
316 $widget itemconfigure $lastCell -fill white -outline "" -width 1 \
319 # Check that we're not over a canvas item
320 if { ![info exists cells($tag)] } {
321 $widget itemconfigure $tag -outline lightblue -fill lightblue \
322 -width 2 -stipple BLT