0
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
|
13 |
View subclass:#PullDownMenu
|
|
14 |
instanceVariableNames:'menus titles activeMenuNumber
|
|
15 |
showSeparatingLines topMargin
|
|
16 |
fgColor bgColor activeFgColor activeBgColor
|
|
17 |
onLevel offLevel'
|
|
18 |
classVariableNames:''
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Views-Menus'
|
|
21 |
!
|
|
22 |
|
|
23 |
PullDownMenu comment:'
|
|
24 |
|
|
25 |
COPYRIGHT (c) 1989-93 by Claus Gittinger
|
|
26 |
All Rights Reserved
|
|
27 |
|
3
|
28 |
$Header: /cvs/stx/stx/libwidg/PullDownMenu.st,v 1.2 1993-10-13 01:03:09 claus Exp $
|
|
29 |
|
|
30 |
written summer 89 by claus
|
|
31 |
'!
|
0
|
32 |
|
3
|
33 |
!PullDownMenu class methodsFor:'documentation'!
|
|
34 |
|
|
35 |
documentation
|
|
36 |
"
|
0
|
37 |
PullDown menu provides the top (always visible) part of these menus. It controls
|
|
38 |
display of its menus, which become visible when one of the PullDownMenus entries is pressed.
|
|
39 |
|
|
40 |
Instance variables:
|
|
41 |
|
|
42 |
menus <aCollection> the sub menus
|
|
43 |
titles <aCollection> the strings in the menu
|
|
44 |
activeMenuNumber <Number> the index of the currently active menu
|
|
45 |
showSeparatingLines <Boolean> show separating lines between my menu-strings
|
|
46 |
topMargin <Number> number of pixels at top
|
|
47 |
fgColor <Color> color to draw passive menu-titles
|
|
48 |
bgColor <Color> color to draw passive menu-titles
|
|
49 |
activeFgColor <Color> color to draw activated menu-titles
|
|
50 |
activeBgColor <Color> color to draw activated menu-titles
|
3
|
51 |
"
|
|
52 |
! !
|
0
|
53 |
|
|
54 |
!PullDownMenu class methodsFor:'instance creation'!
|
|
55 |
|
|
56 |
labels:titleArray
|
|
57 |
"create and return a new PullDownMenu"
|
|
58 |
|
|
59 |
^ self new labels:titleArray
|
|
60 |
! !
|
|
61 |
|
|
62 |
!PullDownMenu methodsFor:'initialization'!
|
|
63 |
|
|
64 |
initialize
|
|
65 |
super initialize.
|
|
66 |
|
|
67 |
font := font on:device.
|
|
68 |
self origin:(0.0 @ 0.0)
|
3
|
69 |
extent:(1.0 @ (font height + (font descent * 2) + topMargin)).
|
0
|
70 |
!
|
|
71 |
|
|
72 |
initStyle
|
|
73 |
super initStyle.
|
|
74 |
|
|
75 |
showSeparatingLines := false.
|
|
76 |
fgColor := Black.
|
|
77 |
bgColor := viewBackground.
|
|
78 |
self is3D ifTrue:[
|
|
79 |
device hasColors ifTrue:[
|
|
80 |
activeFgColor := Color name:'yellow'
|
|
81 |
] ifFalse:[
|
|
82 |
activeFgColor := White
|
|
83 |
].
|
|
84 |
device hasGreyscales ifTrue:[
|
|
85 |
activeBgColor := bgColor.
|
|
86 |
] ifFalse:[
|
|
87 |
activeBgColor := fgColor.
|
|
88 |
].
|
|
89 |
topMargin := 2
|
|
90 |
] ifFalse:[
|
|
91 |
activeFgColor := bgColor.
|
|
92 |
activeBgColor := fgColor.
|
|
93 |
topMargin := 0
|
|
94 |
].
|
|
95 |
onLevel := -1.
|
|
96 |
offLevel := 1
|
|
97 |
!
|
|
98 |
|
|
99 |
initEvents
|
|
100 |
self enableButtonMotionEvents.
|
|
101 |
self enableButtonEvents
|
|
102 |
!
|
|
103 |
|
|
104 |
recreate
|
|
105 |
super create.
|
|
106 |
self setMenuOrigins
|
|
107 |
!
|
|
108 |
|
|
109 |
create
|
|
110 |
super create.
|
|
111 |
self setMenuOrigins
|
|
112 |
! !
|
|
113 |
|
|
114 |
!PullDownMenu methodsFor:'accessing'!
|
|
115 |
|
|
116 |
showSeparatingLines:aBoolean
|
|
117 |
"turn on/off drawing of separating lines"
|
|
118 |
|
|
119 |
showSeparatingLines := aBoolean.
|
|
120 |
shown ifTrue:[
|
|
121 |
self redraw
|
|
122 |
]
|
|
123 |
!
|
|
124 |
|
|
125 |
numberOfTitles:n
|
|
126 |
"setup blank title-space to be filled in later"
|
|
127 |
|
|
128 |
menus := Array new:n.
|
|
129 |
titles := Array new:n
|
|
130 |
!
|
|
131 |
|
|
132 |
labels:titleArray
|
|
133 |
"define the menu-titles (group-headers)"
|
|
134 |
|
|
135 |
|numberOfLabels|
|
|
136 |
|
|
137 |
numberOfLabels := titleArray size.
|
|
138 |
menus := Array new:numberOfLabels.
|
|
139 |
titles := Array new:numberOfLabels.
|
|
140 |
1 to:numberOfLabels do:[:index |
|
|
141 |
titles at:index put:(titleArray at:index) printString
|
|
142 |
].
|
|
143 |
shown ifTrue:[
|
|
144 |
self clear.
|
|
145 |
self redraw
|
|
146 |
]
|
|
147 |
!
|
|
148 |
|
|
149 |
labels
|
|
150 |
"return the menu-titles (group-headers)"
|
|
151 |
|
|
152 |
^ titles
|
|
153 |
!
|
|
154 |
|
|
155 |
font:aFont
|
|
156 |
"adjust menu-origins when font changes"
|
|
157 |
|
3
|
158 |
super font:(aFont on:device).
|
|
159 |
self height:(font height + (font descent * 2)).
|
0
|
160 |
self setMenuOrigins
|
|
161 |
!
|
|
162 |
|
|
163 |
menuAt:string
|
|
164 |
"return the menu with the title; return nil if not found"
|
|
165 |
|
|
166 |
|index|
|
|
167 |
|
|
168 |
index := self indexOf:string.
|
|
169 |
(index == 0) ifTrue:[^ nil].
|
|
170 |
^ menus at:index
|
|
171 |
!
|
|
172 |
|
|
173 |
at:aString putMenu:aMenu
|
|
174 |
"set the menu under the title, aString"
|
|
175 |
|
|
176 |
|index|
|
|
177 |
|
|
178 |
index := self indexOf:aString.
|
|
179 |
(index == 0) ifTrue:[^ nil].
|
|
180 |
aMenu origin:((left + (self titleLenUpTo:index))
|
|
181 |
@
|
|
182 |
(height + aMenu borderWidth)).
|
|
183 |
aMenu hidden:true.
|
|
184 |
menus at:index put:aMenu
|
|
185 |
!
|
|
186 |
|
|
187 |
at:aString putLabels:labels selectors:selectors args:args receiver:anObject
|
|
188 |
"create and set the menu under the title, aString"
|
|
189 |
|
|
190 |
|menuView|
|
|
191 |
|
|
192 |
menuView := MenuView labels:labels
|
|
193 |
selectors:selectors
|
|
194 |
args:args
|
|
195 |
receiver:anObject
|
|
196 |
for:self.
|
|
197 |
self at:aString putMenu:menuView
|
|
198 |
!
|
|
199 |
|
|
200 |
at:aString putLabels:labels selector:selector args:args receiver:anObject
|
|
201 |
"create and set the menu under the title, aString"
|
|
202 |
|
|
203 |
|menuView|
|
|
204 |
|
|
205 |
menuView := MenuView labels:labels
|
|
206 |
selector:selector
|
|
207 |
args:args
|
|
208 |
receiver:anObject
|
|
209 |
for:self.
|
|
210 |
self at:aString putMenu:menuView
|
|
211 |
!
|
|
212 |
|
|
213 |
at:aString putLabels:labels selectors:selectors receiver:anObject
|
|
214 |
"create and set the menu under the title, aString"
|
|
215 |
|
|
216 |
|menuView|
|
|
217 |
|
|
218 |
menuView := MenuView labels:labels
|
|
219 |
selectors:selectors
|
|
220 |
receiver:anObject
|
|
221 |
for:self.
|
|
222 |
self at:aString putMenu:menuView
|
|
223 |
! !
|
|
224 |
|
|
225 |
!PullDownMenu methodsFor:'private'!
|
|
226 |
|
|
227 |
titleLenUpTo:index
|
|
228 |
"answer len of all title-strings up-to (but excluding) title-index"
|
|
229 |
|
|
230 |
|len "{ Class: SmallInteger }" |
|
|
231 |
|
|
232 |
(index <= 1) ifTrue:[^ 0].
|
|
233 |
len := 0.
|
|
234 |
titles from:1 to:(index - 1) do:[:string |
|
|
235 |
len := len + (font widthOf:(' ' , string , ' ')).
|
|
236 |
showSeparatingLines ifTrue:[
|
|
237 |
self is3D ifTrue:[
|
|
238 |
len := len + 2
|
|
239 |
] ifFalse:[
|
|
240 |
len := len + 1
|
|
241 |
]
|
|
242 |
]
|
|
243 |
].
|
|
244 |
^ len
|
|
245 |
!
|
|
246 |
|
|
247 |
indexOf:stringOrNumber
|
|
248 |
"return the index of the menu with title; return 0 if not found"
|
|
249 |
|
|
250 |
(stringOrNumber isMemberOf:SmallInteger) ifTrue:[
|
|
251 |
^ stringOrNumber
|
|
252 |
].
|
|
253 |
^ titles indexOf:stringOrNumber
|
|
254 |
!
|
|
255 |
|
|
256 |
setMenuOrigins
|
|
257 |
"adjust origins of menus when font changes"
|
|
258 |
|
|
259 |
|index|
|
|
260 |
|
|
261 |
(font device == device) ifTrue:[
|
|
262 |
index := 1.
|
|
263 |
menus do:[:aMenu |
|
|
264 |
aMenu notNil ifTrue:[
|
|
265 |
aMenu origin:((left + (self titleLenUpTo:index))
|
|
266 |
@
|
|
267 |
(height + aMenu borderWidth))
|
|
268 |
].
|
|
269 |
index := index + 1
|
|
270 |
]
|
|
271 |
]
|
|
272 |
! !
|
|
273 |
|
|
274 |
!PullDownMenu methodsFor:'hiding/showing menus'!
|
|
275 |
|
|
276 |
drawTitle:string x:x selected:selected
|
|
277 |
|yText w|
|
|
278 |
|
3
|
279 |
yText := ((height - (font height)) // 2) + (font ascent) "+ topMargin".
|
0
|
280 |
w := font widthOf:string.
|
|
281 |
selected ifTrue:[
|
|
282 |
self paint:activeBgColor
|
|
283 |
] ifFalse:[
|
|
284 |
self paint:bgColor
|
|
285 |
].
|
|
286 |
self fillRectangleX:x y:0 width:w height:height.
|
|
287 |
|
|
288 |
self is3D ifTrue:[
|
|
289 |
self drawEdgesForX:x y:0
|
|
290 |
width:w
|
|
291 |
height:height
|
|
292 |
level:(selected ifTrue:[onLevel] ifFalse:[offLevel])
|
|
293 |
].
|
|
294 |
selected ifTrue:[
|
|
295 |
self paint:activeFgColor
|
|
296 |
] ifFalse:[
|
|
297 |
self paint:fgColor
|
|
298 |
].
|
|
299 |
self displayString:string x:x y:yText
|
|
300 |
!
|
|
301 |
|
|
302 |
highlightActiveTitle
|
|
303 |
|x string|
|
|
304 |
activeMenuNumber notNil ifTrue:[
|
|
305 |
x := self titleLenUpTo:activeMenuNumber.
|
|
306 |
string := ' ' , (titles at:activeMenuNumber) , ' '.
|
|
307 |
self drawTitle:string x:x selected:true
|
|
308 |
]
|
|
309 |
!
|
|
310 |
|
|
311 |
unHighlightActiveTitle
|
|
312 |
|x string|
|
|
313 |
activeMenuNumber notNil ifTrue:[
|
|
314 |
x := self titleLenUpTo:activeMenuNumber.
|
|
315 |
string := ' ' , (titles at:activeMenuNumber) , ' '.
|
|
316 |
self drawTitle:string x:x selected:false
|
|
317 |
]
|
|
318 |
!
|
|
319 |
|
|
320 |
hideActiveMenu
|
|
321 |
activeMenuNumber notNil ifTrue:[
|
|
322 |
(menus at:activeMenuNumber) unrealize.
|
|
323 |
self unHighlightActiveTitle.
|
|
324 |
activeMenuNumber := nil
|
|
325 |
]
|
|
326 |
!
|
|
327 |
|
|
328 |
pullMenu:aNumber
|
|
329 |
"activate a menu"
|
|
330 |
|
|
331 |
|subMenu|
|
|
332 |
|
|
333 |
activeMenuNumber notNil ifTrue:[self hideActiveMenu].
|
|
334 |
subMenu := menus at:aNumber.
|
|
335 |
subMenu notNil ifTrue:[
|
|
336 |
activeMenuNumber := aNumber.
|
|
337 |
self highlightActiveTitle.
|
|
338 |
subMenu deselect.
|
|
339 |
subMenu create.
|
|
340 |
subMenu saveUnder:true.
|
|
341 |
subMenu raise show
|
|
342 |
]
|
|
343 |
! !
|
|
344 |
|
|
345 |
!PullDownMenu methodsFor:'events'!
|
|
346 |
|
|
347 |
redraw
|
|
348 |
|string
|
|
349 |
x "{ Class: SmallInteger }"
|
|
350 |
y "{ Class: SmallInteger }"
|
|
351 |
index "{ Class: SmallInteger }" |
|
|
352 |
|
|
353 |
shown ifFalse: [ ^ self ].
|
|
354 |
titles isNil ifTrue:[^ self].
|
|
355 |
x := 0.
|
|
356 |
y := height "- 1".
|
|
357 |
index := 0.
|
|
358 |
titles do:[:title |
|
|
359 |
string := ' ' , title , ' '.
|
|
360 |
self drawTitle:string x:x selected:(index == activeMenuNumber).
|
|
361 |
x := x + (font widthOf:string).
|
|
362 |
showSeparatingLines ifTrue:[
|
|
363 |
self is3D ifTrue:[
|
|
364 |
self paint:shadowColor.
|
|
365 |
self displayLineFromX:x y:0 toX:x y:y.
|
|
366 |
x := x + 1.
|
|
367 |
self paint:lightColor.
|
|
368 |
self displayLineFromX:x y:0 toX:x y:y
|
|
369 |
] ifFalse:[
|
|
370 |
self paint:fgColor.
|
|
371 |
self displayLineFromX:x y:0 toX:x y:y
|
|
372 |
].
|
|
373 |
x := x + 1
|
|
374 |
].
|
|
375 |
index := index + 1
|
|
376 |
]
|
|
377 |
!
|
|
378 |
|
|
379 |
titleIndexForX:x
|
|
380 |
"given a click x-position, return index in title or nil"
|
|
381 |
|
|
382 |
|string
|
|
383 |
xstart "{ Class: SmallInteger }"
|
|
384 |
xend "{ Class: SmallInteger }" |
|
|
385 |
|
|
386 |
xstart := 0.
|
|
387 |
1 to:(titles size) do:[:index |
|
|
388 |
string := ' ' , (titles at:index) , ' '.
|
|
389 |
xend := xstart + (font widthOf:string).
|
|
390 |
showSeparatingLines ifTrue:[
|
|
391 |
self is3D ifTrue:[
|
|
392 |
xend := xend + 2
|
|
393 |
] ifFalse:[
|
|
394 |
xend := xend + 1
|
|
395 |
]
|
|
396 |
].
|
|
397 |
(x between:xstart and:xend) ifTrue:[^ index].
|
|
398 |
xstart := xend
|
|
399 |
].
|
|
400 |
^ nil
|
|
401 |
!
|
|
402 |
|
|
403 |
buttonPress:button x:x y:y
|
|
404 |
|titleIndex|
|
|
405 |
|
|
406 |
titleIndex := self titleIndexForX:x.
|
|
407 |
titleIndex notNil ifTrue:[
|
|
408 |
self pullMenu:titleIndex
|
|
409 |
]
|
|
410 |
!
|
|
411 |
|
|
412 |
buttonMotion:button x:x y:y
|
|
413 |
|titleIndex activeMenu activeLeft activeTop|
|
|
414 |
|
|
415 |
(y < height) ifTrue:[
|
|
416 |
"moving around in title line"
|
|
417 |
activeMenuNumber notNil ifTrue:[
|
|
418 |
(menus at:activeMenuNumber) selection:nil
|
|
419 |
].
|
|
420 |
titleIndex := self titleIndexForX:x.
|
|
421 |
titleIndex notNil ifTrue:[
|
|
422 |
(titleIndex ~~ activeMenuNumber) ifTrue:[
|
|
423 |
self hideActiveMenu.
|
|
424 |
self pullMenu:titleIndex
|
|
425 |
]
|
|
426 |
]
|
|
427 |
] ifFalse:[
|
|
428 |
"moving around below"
|
|
429 |
activeMenuNumber isNil ifTrue:[^self].
|
|
430 |
activeMenu := menus at:activeMenuNumber.
|
|
431 |
activeLeft := activeMenu left.
|
|
432 |
(x between:activeLeft and:(activeMenu right)) ifTrue:[
|
|
433 |
activeTop := activeMenu top.
|
|
434 |
(y between:activeTop and:(activeMenu bottom)) ifTrue:[
|
|
435 |
"moving around in menu"
|
|
436 |
activeMenu buttonMotion:button
|
|
437 |
x:(x - activeLeft)
|
|
438 |
y:(y - activeTop).
|
|
439 |
^ self
|
|
440 |
]
|
|
441 |
].
|
|
442 |
"moved outside menu"
|
|
443 |
activeMenu selection:nil
|
|
444 |
]
|
|
445 |
!
|
|
446 |
|
|
447 |
buttonRelease:button x:x y:y
|
|
448 |
|activeMenu activeLeft activeTop|
|
|
449 |
|
|
450 |
(y >= height) ifTrue:[
|
|
451 |
"release below title-line"
|
|
452 |
activeMenuNumber isNil ifTrue:[^self].
|
|
453 |
activeMenu := menus at:activeMenuNumber.
|
|
454 |
activeLeft := activeMenu left.
|
|
455 |
(x between:activeLeft and:(activeMenu right)) ifTrue:[
|
|
456 |
activeTop := activeMenu top.
|
|
457 |
(y between:activeTop and:(activeMenu bottom)) ifTrue:[
|
|
458 |
"release in menu"
|
|
459 |
self hideActiveMenu.
|
|
460 |
activeMenu buttonRelease:button
|
|
461 |
x:(x - activeLeft)
|
|
462 |
y:(y - activeTop).
|
|
463 |
^ self
|
|
464 |
]
|
|
465 |
]
|
|
466 |
].
|
|
467 |
self hideActiveMenu
|
|
468 |
! !
|