0
|
1 |
"
|
5
|
2 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
0
|
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 |
PopUpView subclass:#PopUpMenu
|
|
14 |
instanceVariableNames:'menuView lastSelection memorize hideOnLeave
|
|
15 |
actionLabels actionLines actionValues'
|
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Views-Menus'
|
|
19 |
!
|
|
20 |
|
|
21 |
PopUpMenu comment:'
|
|
22 |
|
5
|
23 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
0
|
24 |
All Rights Reserved
|
|
25 |
|
7
|
26 |
$Header: /cvs/stx/stx/libwidg/PopUpMenu.st,v 1.4 1993-12-11 01:47:42 claus Exp $
|
0
|
27 |
|
|
28 |
written summer 89 by claus;
|
|
29 |
ST-80 compatibility added Dec 92;
|
|
30 |
'!
|
|
31 |
|
|
32 |
!PopUpMenu class methodsFor:'instance creation'!
|
|
33 |
|
|
34 |
labels:labels selectors:selectors receiver:anObject for:aView
|
|
35 |
|newMenu|
|
|
36 |
|
|
37 |
aView isNil ifTrue:[
|
|
38 |
newMenu := self on:Display
|
|
39 |
] ifFalse:[
|
|
40 |
newMenu := self on:(aView device)
|
|
41 |
].
|
|
42 |
^ newMenu menu:(MenuView
|
|
43 |
labels:labels
|
|
44 |
selectors:selectors
|
|
45 |
receiver:anObject
|
|
46 |
in:newMenu)
|
|
47 |
!
|
|
48 |
|
|
49 |
labels:labels selectors:selectors receiver:anObject
|
|
50 |
^ self labels:labels selectors:selectors receiver:anObject for:nil
|
|
51 |
!
|
|
52 |
|
|
53 |
labels:labels selectors:selectors args:args receiver:anObject for:aView
|
|
54 |
|newMenu|
|
|
55 |
|
|
56 |
aView isNil ifTrue:[
|
|
57 |
newMenu := self on:Display
|
|
58 |
] ifFalse:[
|
|
59 |
newMenu := self on:(aView device)
|
|
60 |
].
|
|
61 |
^ newMenu menu:(MenuView
|
|
62 |
labels:labels
|
|
63 |
selectors:selectors
|
|
64 |
args:args
|
|
65 |
receiver:anObject
|
|
66 |
in:newMenu)
|
|
67 |
!
|
|
68 |
|
|
69 |
labels:labels selectors:selectors args:args receiver:anObject
|
|
70 |
^ self labels:labels
|
|
71 |
selectors:selectors
|
|
72 |
args:args
|
|
73 |
receiver:anObject
|
|
74 |
for:nil
|
|
75 |
! !
|
|
76 |
|
|
77 |
!PopUpMenu class methodsFor:'ST-80 instance creation'!
|
|
78 |
|
|
79 |
labels:labels
|
|
80 |
^ self labels:labels lines:nil values:nil
|
|
81 |
!
|
|
82 |
|
|
83 |
labels:labels values:values
|
|
84 |
^ self labels:labels lines:nil values:values
|
|
85 |
!
|
|
86 |
|
|
87 |
labels:labels lines:lines
|
|
88 |
^ self labels:labels lines:lines values:nil
|
|
89 |
!
|
|
90 |
|
|
91 |
labels:labels lines:lines values:values
|
|
92 |
^ (self new) labels:labels lines:lines values:values
|
|
93 |
!
|
|
94 |
|
|
95 |
labelList:labels values:values
|
|
96 |
^ self labels:labels lines:nil values:values
|
|
97 |
!
|
|
98 |
|
|
99 |
labelList:labels lines:lines values:values
|
|
100 |
^ (self new) labels:labels lines:lines values:values
|
|
101 |
! !
|
|
102 |
|
|
103 |
!PopUpMenu methodsFor:'initialization'!
|
|
104 |
|
|
105 |
initialize
|
|
106 |
super initialize.
|
|
107 |
memorize := true.
|
|
108 |
hideOnLeave := false
|
|
109 |
!
|
|
110 |
|
|
111 |
initEvents
|
|
112 |
super initEvents.
|
|
113 |
self enableEnterLeaveEvents.
|
|
114 |
self enableButtonMotionEvents.
|
|
115 |
self enableMotionEvents.
|
|
116 |
self enableButtonEvents
|
|
117 |
! !
|
|
118 |
|
|
119 |
!PopUpMenu methodsFor:'realization'!
|
|
120 |
|
|
121 |
fixSize
|
|
122 |
"adjust my size to the size of the actual menu"
|
|
123 |
|
|
124 |
|extra newWidth newHeight|
|
|
125 |
|
|
126 |
extra := margin * 2.
|
|
127 |
newWidth := menuView width + extra.
|
|
128 |
newHeight := menuView height + extra.
|
|
129 |
((newWidth ~~ width) or:[newHeight ~~ height]) ifTrue:[
|
|
130 |
self extent:(menuView width + extra) @ (menuView height + extra)
|
|
131 |
].
|
|
132 |
super fixSize
|
|
133 |
!
|
|
134 |
|
|
135 |
realize
|
|
136 |
menuView deselectWithoutRedraw.
|
7
|
137 |
self enableEnterLeaveEvents.
|
0
|
138 |
super realize.
|
|
139 |
|
|
140 |
menuView disableButtonMotionEvents.
|
|
141 |
menuView disableMotionEvents.
|
7
|
142 |
menuView disableButtonEvents.
|
|
143 |
menuView disableEnterLeaveEvents
|
0
|
144 |
! !
|
|
145 |
|
|
146 |
!PopUpMenu methodsFor:'private accessing'!
|
|
147 |
|
|
148 |
menu:aMenu
|
|
149 |
"set the actual menu"
|
|
150 |
|
|
151 |
menuView := aMenu.
|
|
152 |
menuView origin:(margin @ margin).
|
|
153 |
menuView borderWidth:0
|
|
154 |
!
|
|
155 |
|
|
156 |
menuView
|
|
157 |
"return the actual menu"
|
|
158 |
|
|
159 |
^ menuView
|
7
|
160 |
!
|
|
161 |
|
|
162 |
superMenu:aMenu
|
|
163 |
"return the superMenu"
|
|
164 |
|
|
165 |
menuView superMenu:aMenu
|
0
|
166 |
! !
|
|
167 |
|
|
168 |
!PopUpMenu methodsFor:'accessing'!
|
|
169 |
|
|
170 |
viewBackground:aColor
|
|
171 |
"this is a kludge and will vanish ..."
|
|
172 |
|
|
173 |
super viewBackground:aColor.
|
|
174 |
menuView viewBackground:aColor
|
|
175 |
!
|
|
176 |
|
|
177 |
hideOnLeave:aBoolean
|
|
178 |
"set/clear the hideOnLeave attribute, which controls
|
|
179 |
if the menu should be hidden when the pointer leaves
|
|
180 |
the view (used with multiple-menus)"
|
|
181 |
|
|
182 |
hideOnLeave := aBoolean
|
|
183 |
!
|
|
184 |
|
|
185 |
enable:anEntry
|
|
186 |
"enable a menu entry"
|
|
187 |
|
|
188 |
menuView enable:anEntry
|
|
189 |
!
|
|
190 |
|
|
191 |
disable:anEntry
|
|
192 |
"disable a menu entry"
|
|
193 |
|
|
194 |
menuView disable:anEntry
|
|
195 |
!
|
|
196 |
|
|
197 |
receiver:anObject
|
|
198 |
menuView receiver:anObject
|
|
199 |
!
|
|
200 |
|
|
201 |
addLabel:aLabel selector:aSelector
|
|
202 |
"add a new menu entry to the end"
|
|
203 |
|
|
204 |
menuView addLabel:aLabel selector:aSelector
|
|
205 |
!
|
|
206 |
|
|
207 |
addLabel:aLabel selector:aSelector arg:anArg
|
|
208 |
"add a new menu entry to the end"
|
|
209 |
|
|
210 |
menuView addLabel:aLabel selector:aSelector arg:anArg
|
|
211 |
!
|
|
212 |
|
|
213 |
labelAt:index put:aString
|
|
214 |
"change a menu entry"
|
|
215 |
|
|
216 |
menuView labelAt:index put:aString
|
|
217 |
!
|
|
218 |
|
|
219 |
selectorAt:index put:aSymbol
|
|
220 |
"change a selector entry"
|
|
221 |
|
|
222 |
menuView selectorAt:index put:aSymbol
|
|
223 |
!
|
|
224 |
|
|
225 |
subMenuAt:indexOrName put:aMenu
|
|
226 |
"define a submenu to be shown for entry indexOrName"
|
|
227 |
|
7
|
228 |
"
|
0
|
229 |
aMenu hideOnLeave:true.
|
7
|
230 |
"
|
|
231 |
menuView subMenuAt:indexOrName put:aMenu.
|
|
232 |
"tell the submenu to notify me when action is performed"
|
|
233 |
aMenu superMenu:self.
|
0
|
234 |
|
|
235 |
"|v m|
|
|
236 |
v := View new.
|
|
237 |
m := PopUpMenu labels:#('1' '2' '3')
|
|
238 |
selectors:#(one two nil)
|
|
239 |
receiver:v
|
|
240 |
for:nil.
|
|
241 |
m subMenuAt:3 put:(PopUpMenu
|
|
242 |
labels:#('a' 'b' 'c')
|
|
243 |
selectors:#(a b c)
|
|
244 |
receiver:v
|
|
245 |
for:nil).
|
|
246 |
v middleButtonMenu:m.
|
|
247 |
v realize"
|
|
248 |
!
|
|
249 |
|
|
250 |
checkToggleAt:index
|
|
251 |
"return a checkToggles state"
|
|
252 |
|
|
253 |
^ menuView checkToggleAt:index
|
|
254 |
!
|
|
255 |
|
|
256 |
checkToggleAt:index put:aBoolean
|
|
257 |
"set/clear a checkToggle"
|
|
258 |
|
|
259 |
^ menuView checkToggleAt:index put:aBoolean
|
|
260 |
! !
|
|
261 |
|
|
262 |
!PopUpMenu methodsFor:'ST-80 accessing'!
|
|
263 |
|
|
264 |
numberOfItems
|
|
265 |
^ actionLabels asText size
|
|
266 |
!
|
|
267 |
|
|
268 |
labels
|
|
269 |
^ actionLabels asText
|
|
270 |
!
|
|
271 |
|
|
272 |
values
|
|
273 |
^ actionValues
|
|
274 |
!
|
|
275 |
|
|
276 |
lines
|
|
277 |
^ actionLines
|
|
278 |
!
|
|
279 |
|
|
280 |
labels:labelString lines:lineArray values:valueArray
|
|
281 |
"define the menu the ST-80 way (with labels and lines
|
|
282 |
defined separately)"
|
|
283 |
|
|
284 |
|labelArray argArray convertedLabels
|
|
285 |
offs dstOffs linePos|
|
|
286 |
|
|
287 |
actionLabels := labelString.
|
|
288 |
actionLines := lineArray.
|
|
289 |
actionValues := valueArray.
|
|
290 |
|
|
291 |
labelArray := labelString asText.
|
|
292 |
|
|
293 |
convertedLabels := Array new:(labelArray size + lineArray size).
|
|
294 |
argArray := Array new:(labelArray size + lineArray size).
|
|
295 |
|
|
296 |
offs := 1.
|
|
297 |
dstOffs := 1.
|
|
298 |
1 to:lineArray size do:[:lineIndex |
|
|
299 |
linePos := lineArray at:lineIndex.
|
|
300 |
[offs <= linePos] whileTrue:[
|
|
301 |
convertedLabels at:dstOffs put:(labelArray at:offs).
|
|
302 |
argArray at:dstOffs put:offs.
|
|
303 |
offs := offs + 1.
|
|
304 |
dstOffs := dstOffs + 1
|
|
305 |
].
|
|
306 |
convertedLabels at:dstOffs put:'-'.
|
|
307 |
argArray at:dstOffs put:nil.
|
|
308 |
dstOffs := dstOffs + 1
|
|
309 |
].
|
|
310 |
[offs <= labelArray size] whileTrue:[
|
|
311 |
convertedLabels at:dstOffs put:(labelArray at:offs).
|
|
312 |
argArray at:dstOffs put:offs.
|
|
313 |
offs := offs + 1.
|
|
314 |
dstOffs := dstOffs + 1
|
|
315 |
].
|
|
316 |
self menu:(MenuView
|
|
317 |
labels:convertedLabels
|
|
318 |
selector:nil
|
|
319 |
args:argArray
|
|
320 |
receiver:nil
|
|
321 |
in:self)
|
|
322 |
|
|
323 |
! !
|
|
324 |
|
|
325 |
!PopUpMenu methodsFor:'activation'!
|
|
326 |
|
|
327 |
showAt:aPoint
|
|
328 |
"realize the menu at aPoint - return control"
|
|
329 |
|
|
330 |
self fixSize.
|
|
331 |
self origin:aPoint.
|
|
332 |
((top + height) > (device height)) ifTrue:[
|
|
333 |
self top:(device height - height)
|
|
334 |
].
|
|
335 |
((left + width) > (device width)) ifTrue:[
|
|
336 |
self left:(device width - width)
|
|
337 |
].
|
|
338 |
self realize
|
|
339 |
!
|
|
340 |
|
|
341 |
showAtPointer
|
|
342 |
"realize the menu at the current pointer position - return control"
|
|
343 |
|
|
344 |
self showAt:(device pointerPosition)
|
|
345 |
!
|
|
346 |
|
|
347 |
show
|
|
348 |
"realize the menu at its last position - return control"
|
|
349 |
|
|
350 |
self fixSize.
|
|
351 |
self realize
|
|
352 |
!
|
|
353 |
|
|
354 |
hide
|
7
|
355 |
"hide the menu - if there are any pop-up-submenus, hide them also"
|
|
356 |
|
|
357 |
menuView hideSubmenu.
|
|
358 |
^ self unrealize
|
|
359 |
!
|
0
|
360 |
|
7
|
361 |
regainControl
|
|
362 |
"
|
|
363 |
device ungrabPointer.
|
|
364 |
device grabPointerIn:drawableId
|
|
365 |
"
|
0
|
366 |
! !
|
|
367 |
|
|
368 |
!PopUpMenu methodsFor:'ST-80 activation'!
|
|
369 |
|
|
370 |
startUp
|
|
371 |
"start the menu modal - return the selected selector,
|
|
372 |
or - if no selectors where specified - the index.
|
|
373 |
If nothing was selected, return 0.
|
|
374 |
Modal - i.e. stay in the menu until finished"
|
|
375 |
|
|
376 |
|actionIndex value|
|
|
377 |
|
|
378 |
menuView action:[:selected |
|
|
379 |
menuView args isNil ifTrue:[
|
|
380 |
menuView selectors isNil ifTrue:[
|
|
381 |
^ 0
|
|
382 |
].
|
|
383 |
^ menuView receiver perform:(menuView selectors at:selected)
|
|
384 |
].
|
|
385 |
actionIndex := menuView args at:selected.
|
|
386 |
actionIndex isNil ifTrue:[^ 0].
|
|
387 |
actionValues isNil ifTrue:[^ actionIndex].
|
|
388 |
value := actionValues at:actionIndex.
|
|
389 |
(value isKindOf:PopUpMenu) ifTrue:[
|
|
390 |
^ value startUp
|
|
391 |
].
|
|
392 |
^ value
|
|
393 |
].
|
|
394 |
self showAtPointer.
|
|
395 |
self modalLoop.
|
|
396 |
^ 0
|
|
397 |
! !
|
|
398 |
|
|
399 |
!PopUpMenu methodsFor:'events'!
|
|
400 |
|
|
401 |
buttonMotion:button x:x y:y
|
7
|
402 |
|p|
|
|
403 |
|
0
|
404 |
(x >= 0) ifTrue:[
|
|
405 |
(x < width) ifTrue:[
|
|
406 |
(y >= 0) ifTrue:[
|
|
407 |
(y < height) ifTrue:[
|
|
408 |
menuView buttonMotion:button x:x y:y.
|
|
409 |
^ self
|
|
410 |
]
|
|
411 |
]
|
|
412 |
]
|
|
413 |
].
|
7
|
414 |
|
|
415 |
"outside of myself"
|
|
416 |
menuView superMenu notNil ifTrue:[
|
|
417 |
p := device translatePoint:(x @ y)
|
|
418 |
from:(self id)
|
|
419 |
to:(menuView superMenu id).
|
|
420 |
menuView superMenu buttonMotion:button x:p x y:p y
|
|
421 |
].
|
|
422 |
|
|
423 |
menuView subMenuShown isNil ifTrue:[
|
|
424 |
menuView pointerLeave:button.
|
|
425 |
].
|
|
426 |
|
0
|
427 |
hideOnLeave ifTrue:[
|
|
428 |
self hide
|
|
429 |
]
|
|
430 |
!
|
|
431 |
|
7
|
432 |
pointerEnter:state x:x y:y
|
|
433 |
"catch quick release of button"
|
|
434 |
|
|
435 |
state == 0 ifTrue:[^ self hide].
|
|
436 |
!
|
|
437 |
|
0
|
438 |
pointerLeave:state
|
|
439 |
menuView pointerLeave:state.
|
|
440 |
hideOnLeave ifTrue:[
|
|
441 |
self hide
|
7
|
442 |
].
|
|
443 |
menuView superMenu notNil ifTrue:[
|
|
444 |
menuView superMenu regainControl
|
0
|
445 |
]
|
|
446 |
!
|
|
447 |
|
|
448 |
buttonRelease:button x:x y:y
|
|
449 |
self hide.
|
7
|
450 |
"
|
|
451 |
menuView buttonRelease:button x:x y:y.
|
|
452 |
"
|
|
453 |
menuView superMenu notNil ifTrue:[
|
|
454 |
menuView superMenu submenuTriggered
|
|
455 |
].
|
|
456 |
menuView buttonRelease:button x:x y:y.
|
0
|
457 |
! !
|