OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / HP / util / HP / lib / tk8.3 / demos / items.tcl
1 # items.tcl --
2 #
3 # This demonstration script creates a canvas that displays the
4 # canvas item types.
5 #
6 # RCS: @(#) $Id: items.tcl,v 1.2 1998/09/14 18:23:29 stanton Exp $
7
8 if {![info exists widgetDemo]} {
9     error "This script should be run from the \"widget\" demo."
10 }
11
12 set w .items
13 catch {destroy $w}
14 toplevel $w
15 wm title $w "Canvas Item Demonstration"
16 wm iconname $w "Items"
17 positionWindow $w
18 set c $w.frame.c
19
20 label $w.msg -font $font -wraplength 5i -justify left -text "This window contains a canvas widget with examples of the various kinds of items supported by canvases.  The following operations are supported:\n  Button-1 drag:\tmoves item under pointer.\n  Button-2 drag:\trepositions view.\n  Button-3 drag:\tstrokes out area.\n  Ctrl+f:\t\tprints items under area."
21 pack $w.msg -side top
22
23 frame $w.buttons
24 pack $w.buttons -side bottom -fill x -pady 2m
25 button $w.buttons.dismiss -text Dismiss -command "destroy $w"
26 button $w.buttons.code -text "See Code" -command "showCode $w"
27 pack $w.buttons.dismiss $w.buttons.code -side left -expand 1
28
29 frame $w.frame
30 pack $w.frame -side top -fill both -expand yes
31
32 canvas $c -scrollregion {0c 0c 30c 24c} -width 15c -height 10c \
33         -relief sunken -borderwidth 2 \
34         -xscrollcommand "$w.frame.hscroll set" \
35         -yscrollcommand "$w.frame.vscroll set"
36 scrollbar $w.frame.vscroll -command "$c yview"
37 scrollbar $w.frame.hscroll -orient horiz -command "$c xview"
38
39 grid $c -in $w.frame \
40     -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
41 grid $w.frame.vscroll \
42     -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
43 grid $w.frame.hscroll \
44     -row 1 -column 0 -rowspan 1 -columnspan 1 -sticky news
45 grid rowconfig    $w.frame 0 -weight 1 -minsize 0
46 grid columnconfig $w.frame 0 -weight 1 -minsize 0
47
48 # Display a 3x3 rectangular grid.
49
50 $c create rect 0c 0c 30c 24c -width 2
51 $c create line 0c 8c 30c 8c -width 2
52 $c create line 0c 16c 30c 16c -width 2
53 $c create line 10c 0c 10c 24c -width 2
54 $c create line 20c 0c 20c 24c -width 2
55
56 set font1 {Helvetica 12}
57 set font2 {Helvetica 24 bold}
58 if {[winfo depth $c] > 1} {
59     set blue DeepSkyBlue3
60     set red red
61     set bisque bisque3
62     set green SeaGreen3
63 } else {
64     set blue black
65     set red black
66     set bisque black
67     set green black
68 }
69
70 # Set up demos within each of the areas of the grid.
71
72 $c create text 5c .2c -text Lines -anchor n
73 $c create line 1c 1c 3c 1c 1c 4c 3c 4c -width 2m -fill $blue \
74         -cap butt -join miter -tags item
75 $c create line 4.67c 1c 4.67c 4c -arrow last -tags item
76 $c create line 6.33c 1c 6.33c 4c -arrow both -tags item
77 $c create line 5c 6c 9c 6c 9c 1c 8c 1c 8c 4.8c 8.8c 4.8c 8.8c 1.2c \
78         8.2c 1.2c 8.2c 4.6c 8.6c 4.6c 8.6c 1.4c 8.4c 1.4c 8.4c 4.4c \
79         -width 3 -fill $red -tags item
80 $c create line 1c 5c 7c 5c 7c 7c 9c 7c -width .5c \
81         -stipple @[file join $tk_library demos images gray25.bmp] \
82         -arrow both -arrowshape {15 15 7} -tags item
83 $c create line 1c 7c 1.75c 5.8c 2.5c 7c 3.25c 5.8c 4c 7c -width .5c \
84         -cap round -join round -tags item
85
86 $c create text 15c .2c -text "Curves (smoothed lines)" -anchor n
87 $c create line 11c 4c 11.5c 1c 13.5c 1c 14c 4c -smooth on \
88         -fill $blue -tags item
89 $c create line 15.5c 1c 19.5c 1.5c 15.5c 4.5c 19.5c 4c -smooth on \
90         -arrow both -width 3 -tags item
91 $c create line 12c 6c 13.5c 4.5c 16.5c 7.5c 18c 6c \
92         16.5c 4.5c 13.5c 7.5c 12c 6c -smooth on -width 3m -cap round \
93         -stipple @[file join $tk_library demos images gray25.bmp] \
94         -fill $red -tags item
95
96 $c create text 25c .2c -text Polygons -anchor n
97 $c create polygon 21c 1.0c 22.5c 1.75c 24c 1.0c 23.25c 2.5c \
98         24c 4.0c 22.5c 3.25c 21c 4.0c 21.75c 2.5c -fill $green \
99         -outline black -width 4 -tags item
100 $c create polygon 25c 4c 25c 4c 25c 1c 26c 1c 27c 4c 28c 1c \
101         29c 1c 29c 4c 29c 4c -fill $red -smooth on -tags item
102 $c create polygon 22c 4.5c 25c 4.5c 25c 6.75c 28c 6.75c \
103         28c 5.25c 24c 5.25c 24c 6.0c 26c 6c 26c 7.5c 22c 7.5c \
104         -stipple @[file join $tk_library demos images gray25.bmp] \
105         -outline black -tags item
106
107 $c create text 5c 8.2c -text Rectangles -anchor n
108 $c create rectangle 1c 9.5c 4c 12.5c -outline $red -width 3m -tags item
109 $c create rectangle 0.5c 13.5c 4.5c 15.5c -fill $green -tags item
110 $c create rectangle 6c 10c 9c 15c -outline {} \
111         -stipple @[file join $tk_library demos images gray25.bmp] \
112         -fill $blue -tags item
113
114 $c create text 15c 8.2c -text Ovals -anchor n
115 $c create oval 11c 9.5c 14c 12.5c -outline $red -width 3m -tags item
116 $c create oval 10.5c 13.5c 14.5c 15.5c -fill $green -tags item
117 $c create oval 16c 10c 19c 15c -outline {} \
118         -stipple @[file join $tk_library demos images gray25.bmp] \
119         -fill $blue -tags item
120
121 $c create text 25c 8.2c -text Text -anchor n
122 $c create rectangle 22.4c 8.9c 22.6c 9.1c
123 $c create text 22.5c 9c -anchor n -font $font1 -width 4c \
124         -text "A short string of text, word-wrapped, justified left, and anchored north (at the top).  The rectangles show the anchor points for each piece of text." -tags item
125 $c create rectangle 25.4c 10.9c 25.6c 11.1c
126 $c create text 25.5c 11c -anchor w -font $font1 -fill $blue \
127         -text "Several lines,\n each centered\nindividually,\nand all anchored\nat the left edge." \
128         -justify center -tags item
129 $c create rectangle 24.9c 13.9c 25.1c 14.1c
130 $c create text 25c 14c -font $font2 -anchor c -fill $red -stipple gray50 \
131         -text "Stippled characters" -tags item
132
133 $c create text 5c 16.2c -text Arcs -anchor n
134 $c create arc 0.5c 17c 7c 20c -fill $green -outline black \
135         -start 45 -extent 270 -style pieslice -tags item
136 $c create arc 6.5c 17c 9.5c 20c -width 4m -style arc \
137         -outline $blue -start -135 -extent 270 -tags item \
138         -outlinestipple @[file join $tk_library demos images gray25.bmp]
139 $c create arc 0.5c 20c 9.5c 24c -width 4m -style pieslice \
140         -fill {} -outline $red -start 225 -extent -90 -tags item
141 $c create arc 5.5c 20.5c 9.5c 23.5c -width 4m -style chord \
142         -fill $blue -outline {} -start 45 -extent 270  -tags item
143
144 $c create text 15c 16.2c -text Bitmaps -anchor n
145 $c create bitmap 13c 20c -tags item \
146         -bitmap @[file join $tk_library demos images face.bmp]
147 $c create bitmap 17c 18.5c -tags item \
148         -bitmap @[file join $tk_library demos images noletter.bmp]
149 $c create bitmap 17c 21.5c -tags item \
150         -bitmap @[file join $tk_library demos images letters.bmp]
151
152 $c create text 25c 16.2c -text Windows -anchor n
153 button $c.button -text "Press Me" -command "butPress $c $red"
154 $c create window 21c 18c -window $c.button -anchor nw -tags item
155 entry $c.entry -width 20 -relief sunken
156 $c.entry insert end "Edit this text"
157 $c create window 21c 21c -window $c.entry -anchor nw -tags item
158 scale $c.scale -from 0 -to 100 -length 6c -sliderlength .4c \
159         -width .5c -tickinterval 0
160 $c create window 28.5c 17.5c -window $c.scale -anchor n -tags item
161 $c create text 21c 17.9c -text Button: -anchor sw
162 $c create text 21c 20.9c -text Entry: -anchor sw
163 $c create text 28.5c 17.4c -text Scale: -anchor s
164
165 # Set up event bindings for canvas:
166
167 $c bind item <Any-Enter> "itemEnter $c"
168 $c bind item <Any-Leave> "itemLeave $c"
169 bind $c <2> "$c scan mark %x %y"
170 bind $c <B2-Motion> "$c scan dragto %x %y"
171 bind $c <3> "itemMark $c %x %y"
172 bind $c <B3-Motion> "itemStroke $c %x %y"
173 bind $c <Control-f> "itemsUnderArea $c"
174 bind $c <1> "itemStartDrag $c %x %y"
175 bind $c <B1-Motion> "itemDrag $c %x %y"
176
177 # Utility procedures for highlighting the item under the pointer:
178
179 proc itemEnter {c} {
180     global restoreCmd
181
182     if {[winfo depth $c] == 1} {
183         set restoreCmd {}
184         return
185     }
186     set type [$c type current]
187     if {$type == "window"} {
188         set restoreCmd {}
189         return
190     }
191     if {$type == "bitmap"} {
192         set bg [lindex [$c itemconf current -background] 4]
193         set restoreCmd [list $c itemconfig current -background $bg]
194         $c itemconfig current -background SteelBlue2
195         return
196     }
197     set fill [lindex [$c itemconfig current -fill] 4]
198     if {(($type == "rectangle") || ($type == "oval") || ($type == "arc"))
199             && ($fill == "")} {
200         set outline [lindex [$c itemconfig current -outline] 4]
201         set restoreCmd "$c itemconfig current -outline $outline"
202         $c itemconfig current -outline SteelBlue2
203     } else {
204         set restoreCmd "$c itemconfig current -fill $fill"
205         $c itemconfig current -fill SteelBlue2
206     }
207 }
208
209 proc itemLeave {c} {
210     global restoreCmd
211
212     eval $restoreCmd
213 }
214
215 # Utility procedures for stroking out a rectangle and printing what's
216 # underneath the rectangle's area.
217
218 proc itemMark {c x y} {
219     global areaX1 areaY1
220     set areaX1 [$c canvasx $x]
221     set areaY1 [$c canvasy $y]
222     $c delete area
223 }
224
225 proc itemStroke {c x y} {
226     global areaX1 areaY1 areaX2 areaY2
227     set x [$c canvasx $x]
228     set y [$c canvasy $y]
229     if {($areaX1 != $x) && ($areaY1 != $y)} {
230         $c delete area
231         $c addtag area withtag [$c create rect $areaX1 $areaY1 $x $y \
232                 -outline black]
233         set areaX2 $x
234         set areaY2 $y
235     }
236 }
237
238 proc itemsUnderArea {c} {
239     global areaX1 areaY1 areaX2 areaY2
240     set area [$c find withtag area]
241     set items ""
242     foreach i [$c find enclosed $areaX1 $areaY1 $areaX2 $areaY2] {
243         if {[lsearch [$c gettags $i] item] != -1} {
244             lappend items $i
245         }
246     }
247     puts stdout "Items enclosed by area: $items"
248     set items ""
249     foreach i [$c find overlapping $areaX1 $areaY1 $areaX2 $areaY2] {
250         if {[lsearch [$c gettags $i] item] != -1} {
251             lappend items $i
252         }
253     }
254     puts stdout "Items overlapping area: $items"
255 }
256
257 set areaX1 0
258 set areaY1 0
259 set areaX2 0
260 set areaY2 0
261
262 # Utility procedures to support dragging of items.
263
264 proc itemStartDrag {c x y} {
265     global lastX lastY
266     set lastX [$c canvasx $x]
267     set lastY [$c canvasy $y]
268 }
269
270 proc itemDrag {c x y} {
271     global lastX lastY
272     set x [$c canvasx $x]
273     set y [$c canvasy $y]
274     $c move current [expr $x-$lastX] [expr $y-$lastY]
275     set lastX $x
276     set lastY $y
277 }
278
279 # Procedure that's invoked when the button embedded in the canvas
280 # is invoked.
281
282 proc butPress {w color} {
283     set i [$w create text 25c 18.1c -text "Ouch!!" -fill $color -anchor n]
284     after 500 "$w delete $i"
285 }