author | Claus Gittinger <cg@exept.de> |
Thu, 23 Nov 1995 01:50:10 +0100 | |
changeset 193 | 6ccc226ce3a6 |
parent 174 | d80a6cc3f9b2 |
child 202 | 01f3cbb8e20e |
permissions | -rw-r--r-- |
0 | 1 |
" |
5 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
59 | 3 |
All Rights Reserved |
0 | 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 |
|
105 | 15 |
actionLabels actionLines actionValues |
16 |
hideOnRelease defaultHideOnRelease' |
|
119 | 17 |
classVariableNames:'DefaultHideOnRelease' |
0 | 18 |
poolDictionaries:'' |
19 |
category:'Views-Menus' |
|
20 |
! |
|
21 |
||
38 | 22 |
!PopUpMenu class methodsFor:'documentation'! |
23 |
||
24 |
copyright |
|
25 |
" |
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
59 | 27 |
All Rights Reserved |
38 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
38 |
version |
|
174
d80a6cc3f9b2
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
161
diff
changeset
|
39 |
^ '$Header: /cvs/stx/stx/libwidg/PopUpMenu.st,v 1.26 1995-11-11 16:22:11 cg Exp $' |
38 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
This class provides PopUpMenu functionality; Actually, this class |
|
45 |
only provides the popup and shadow functionality and wraps another |
|
46 |
view, which is the actual menu-list (usually an instance of MenuView). |
|
47 |
||
48 |
PopUpMenus are usually created with a list of labels, selectors and a |
|
49 |
receivier. Once activated, the specified receiver will be sent a |
|
50 |
'selector'-message. |
|
112 | 51 |
|
52 |
PopupMenus may be either assigned statically to a view (via the #middleButtonMenu: |
|
53 |
message) or created dynamically as required. |
|
54 |
Static definition makes sense, if the menu stays constant and you want to |
|
55 |
assign it once for the lifetime of the view. |
|
56 |
||
57 |
Dynamic menus are easier to use, if the number of or look of the entries has to |
|
58 |
change according the internal state of some model. Also, this is the ST-80 way |
|
59 |
of using popupMenus. For dynamic popups, the views model is asked for a menu |
|
60 |
via the #menuSelector each time a button press occurs. |
|
61 |
||
62 |
See examples section for more. |
|
63 | 63 |
" |
64 |
! |
|
38 | 65 |
|
63 | 66 |
examples |
67 |
" |
|
38 | 68 |
Examples: |
69 |
||
59 | 70 |
|p| |
71 |
p := PopUpMenu |
|
72 |
labels:#('foo' |
|
73 |
'bar' |
|
74 |
'baz') |
|
75 |
selectors:#( |
|
76 |
#foo |
|
77 |
#bar |
|
78 |
#baz) |
|
79 |
receiver:nil. |
|
80 |
p showAtPointer |
|
38 | 81 |
|
82 |
||
83 |
sometimes, you want to specify both selectors and some arguments |
|
84 |
to be sent; this is done by: |
|
85 |
||
59 | 86 |
|p| |
87 |
p := PopUpMenu |
|
88 |
labels:#('foo' 'bar' 'baz') |
|
89 |
selectors:#(#foo: #bar: #foo:) |
|
90 |
args:#(1 2 3) |
|
91 |
receiver:nil. |
|
92 |
p showAtPointer |
|
38 | 93 |
|
94 |
or, the same selector but different arguments: |
|
95 |
||
59 | 96 |
|p| |
97 |
p := PopUpMenu |
|
98 |
labels:#('foo' 'bar' 'baz') |
|
99 |
selectors:#foo: |
|
100 |
args:#(1 2 3) |
|
101 |
receiver:nil. |
|
102 |
p showAtPointer |
|
38 | 103 |
|
104 |
Normally, you do not show the menu explicitely, but install |
|
112 | 105 |
it as a either as middleButtonMenu of some view or return it from |
106 |
a model. |
|
107 |
(Views/Controllers button-event handler will show it when the middle |
|
108 |
button is pressed ...) |
|
109 |
Static menu: |
|
38 | 110 |
|
59 | 111 |
|v m| |
38 | 112 |
|
59 | 113 |
v := View new. |
114 |
m := PopUpMenu |
|
115 |
labels:#('lower' |
|
116 |
'raise' |
|
117 |
'-' |
|
118 |
'destroy') |
|
119 |
selectors:#(#lower #raise nil #destroy) |
|
120 |
receiver:v. |
|
121 |
v middleButtonMenu:m. |
|
122 |
v open |
|
38 | 123 |
|
112 | 124 |
Dynamic menu: |
125 |
(since we need some model which responds to a menu-message, |
|
126 |
we use a plug in the example below; normally, this would be your model) |
|
127 |
||
128 |
|v model| |
|
129 |
||
130 |
model := Plug new. |
|
119 | 131 |
model respondTo:#getMenu with:[PopUpMenu labels:#('foo' 'bar') |
132 |
selectors:#(foo bar)]. |
|
112 | 133 |
model respondTo:#foo with:[Transcript showCr:'models foo called']. |
134 |
model respondTo:#bar with:[Transcript showCr:'models bar called']. |
|
135 |
||
136 |
v := View new. |
|
137 |
v model:model; menu:#getMenu. |
|
138 |
v open |
|
139 |
||
140 |
Dynamic menus are the MVC-way (i.e. ST-80) way of doing things. |
|
141 |
They are usually easier to use, if the menu changes depending on the models |
|
142 |
state. (for example, see the systemBrowsers menus being different when |
|
143 |
things are selected ...) |
|
144 |
||
38 | 145 |
It is also possible, to add check-mark entries, with an entry string |
146 |
starting with the special sequence '\c' (for check-mark). The value |
|
147 |
passed will be the truth-state of the check-mark. |
|
148 |
||
59 | 149 |
|m v| |
38 | 150 |
|
59 | 151 |
v := View new. |
152 |
m := PopUpMenu |
|
153 |
labels:#('\c foo' |
|
154 |
'\c bar') |
|
155 |
selectors:#(#value: #value:) |
|
156 |
receiver:[:v | Transcript show:'arg: '; showCr:v]. |
|
157 |
v middleButtonMenu:m. |
|
158 |
v open |
|
38 | 159 |
|
160 |
Finally, you can wrap other views into a popup menu (for example, |
|
161 |
to implement menus with icons or other components). |
|
162 |
The view should respond to some messages sent from here (for |
|
163 |
example: #hideSubmenus, #deselectWithoutRedraw and others). |
|
164 |
Currently there is only one class in the system, which can be used |
|
165 |
this way (PatternMenu in the DrawTool demo): |
|
166 |
||
59 | 167 |
|v p| |
38 | 168 |
|
59 | 169 |
v := View new. |
170 |
p := PatternMenu new. |
|
171 |
p patterns:(Array with:Color red |
|
172 |
with:Color green |
|
173 |
with:Color blue). |
|
174 |
v middleButtonMenu:(PopUpMenu forMenu:p). |
|
175 |
v open |
|
38 | 176 |
|
177 |
or try: |
|
178 |
||
59 | 179 |
|v p| |
38 | 180 |
|
59 | 181 |
v := View new. |
182 |
p := PatternMenu new. |
|
183 |
p patterns:(Array with:Color red |
|
184 |
with:Color green |
|
185 |
with:Color blue). |
|
186 |
p selectors:#value:. |
|
187 |
p receiver:[:val | v viewBackground:val. v clear]. |
|
188 |
p args:(Array with:Color red |
|
189 |
with:Color green |
|
190 |
with:Color blue). |
|
191 |
v middleButtonMenu:(PopUpMenu forMenu:p). |
|
192 |
v open |
|
38 | 193 |
|
194 |
||
195 |
ST-80 style: |
|
196 |
||
197 |
The above menus all did some message send on selection; it is |
|
198 |
also possible, to use Smalltalk-80 style menus (which return some value |
|
199 |
from their startup method): |
|
200 |
||
59 | 201 |
|m selection| |
38 | 202 |
|
59 | 203 |
m := PopUpMenu |
204 |
labels:#('one' 'two' 'three'). |
|
205 |
selection := m startUp. |
|
206 |
Transcript show:'the selection was: '; showCr:selection |
|
38 | 207 |
|
208 |
startUp will return the entries index, or 0 if there was no selection. |
|
209 |
You can also specify an array of values to be returned instead of the |
|
210 |
index: |
|
211 |
||
59 | 212 |
|m selection| |
38 | 213 |
|
59 | 214 |
m := PopUpMenu |
215 |
labels:#('one' 'two' 'three') |
|
216 |
values:#(10 20 30). |
|
217 |
selection := m startUp. |
|
218 |
Transcript show:'the value was: '; showCr:selection |
|
38 | 219 |
|
220 |
In ST/X style menus, separating lines between entries are created |
|
221 |
by a '-'-string as its label text (and corresponding nil-entries in the |
|
222 |
selectors- and args-arrays). |
|
223 |
In ST-80, you have to pass the indices of the lines in an extra array: |
|
224 |
||
59 | 225 |
|m selection| |
38 | 226 |
|
59 | 227 |
m := PopUpMenu |
228 |
labels:#('one' 'two' 'three' 'four' 'five') |
|
229 |
lines:#(2 4). |
|
230 |
selection := m startUp. |
|
231 |
Transcript show:'the value was: '; showCr:selection |
|
38 | 232 |
|
233 |
or: |
|
59 | 234 |
|m selection| |
38 | 235 |
|
59 | 236 |
m := PopUpMenu |
237 |
labels:#('one' 'two' 'three') |
|
238 |
lines:#(2) |
|
239 |
values:#(10 20 30). |
|
240 |
selection := m startUp. |
|
241 |
Transcript show:'the value was: '; showCr:selection |
|
38 | 242 |
|
243 |
Use whichever interface you prefer. |
|
244 |
" |
|
245 |
! ! |
|
246 |
||
119 | 247 |
!PopUpMenu class methodsFor:'defaults'! |
38 | 248 |
|
119 | 249 |
updateStyleCache |
250 |
DefaultHideOnRelease := StyleSheet at:#popupHideOnRelease default:true. |
|
251 |
! ! |
|
252 |
||
253 |
!PopUpMenu class methodsFor:'instance creation'! |
|
38 | 254 |
|
255 |
forMenu:aMenuView |
|
256 |
"this wraps an already existing menu - allowing to put any |
|
257 |
view (not just MenuViews) into popups (for example, menus |
|
258 |
with icons, or other components). |
|
259 |
Currently, there is only one example of different menus in |
|
260 |
the system (PatternMenu in the DrawTool) which could be used |
|
261 |
this way. |
|
262 |
The view should respond to some of the menuView messages |
|
263 |
(such as hideSubmenu, deselectWithoutRedraw etc.)" |
|
264 |
||
265 |
|newMenu| |
|
266 |
||
267 |
newMenu := self onSameDeviceAs:aMenuView. |
|
268 |
newMenu addSubView:aMenuView. |
|
269 |
newMenu menu:aMenuView. |
|
270 |
^ newMenu |
|
271 |
! |
|
272 |
||
273 |
labels:labels selectors:selectors args:args receiver:anObject for:aView |
|
63 | 274 |
"create and return a popup menu with labels as entries. |
275 |
Each item will send a corresponding selector:argument from the selectors- |
|
276 |
and args array to anObject. The menu is created on the same physical device |
|
277 |
as aView (which is only of interrest in multi-Display applications; |
|
278 |
typical applications can use the sibbling message without the for: argument)." |
|
279 |
||
38 | 280 |
|newMenu| |
281 |
||
282 |
newMenu := self onSameDeviceAs:aView. |
|
283 |
newMenu menu:(MenuView |
|
59 | 284 |
labels:labels |
285 |
selectors:selectors |
|
286 |
args:args |
|
287 |
receiver:anObject |
|
288 |
in:newMenu). |
|
38 | 289 |
^ newMenu |
290 |
! |
|
291 |
||
119 | 292 |
labels:labels selectors:selectors receiver:anObject for:aView |
63 | 293 |
"create and return a popup menu with labels as entries. |
119 | 294 |
Each item will send a corresponding selector from the selectors-array |
295 |
to anObject. The menu is created on the same physical device |
|
296 |
as aView (which is only of interrest in multi-Display applications; |
|
297 |
typical applications can use the sibbling message without the for: argument)." |
|
63 | 298 |
|
119 | 299 |
^ self labels:labels selectors:selectors args:nil receiver:anObject for:aView |
38 | 300 |
! |
301 |
||
63 | 302 |
labels:labels selector:aSelector args:args receiver:anObject for:aView |
303 |
"create and return a popup menu with labels as entries. |
|
304 |
Each item will send aSelector with a corresponding argument from the |
|
305 |
args array to anObject. The menu is created on the same physical device |
|
306 |
as aView (which is only of interrest in multi-Display applications; |
|
307 |
typical applications can use the sibbling message without the for: argument)." |
|
38 | 308 |
|
119 | 309 |
" |
310 |
OBSOLETE protocol: #labels:selectors:... knows how to handle single-symbol selectors-arg |
|
311 |
" |
|
312 |
^ self labels:labels selectors:aSelector args:args receiver:anObject for:aView |
|
313 |
! |
|
38 | 314 |
|
119 | 315 |
labels:labels selectors:selectors args:args receiver:anObject |
316 |
"create and return a popup menu with labels as entries. |
|
317 |
Each item will send a corresponding selector:argument from the selectors- |
|
318 |
and args array to anObject. The menu is created on the default Display" |
|
319 |
||
320 |
^ self labels:labels selectors:selectors args:args receiver:anObject for:nil |
|
63 | 321 |
! |
322 |
||
323 |
labels:labels selector:aSelector args:args receiver:anObject |
|
324 |
"create and return a popup menu with labels as entries. |
|
325 |
Each item will send aSelector with a corresponding argument from the |
|
326 |
args array to anObject. The menu is created on the default DIsplay" |
|
327 |
||
119 | 328 |
" |
329 |
OBSOLETE protocol: #labels:selectors:... knows how to handle single-symbol selectors-arg |
|
330 |
" |
|
331 |
^ self labels:labels selectors:aSelector args:args receiver:anObject |
|
332 |
! |
|
333 |
||
334 |
labels:labels selectors:selectors receiver:anObject |
|
335 |
"create and return a popup menu with labels as entries. |
|
336 |
Each item will send a message with a selector from the corresponding |
|
337 |
selectors-array. |
|
338 |
The menu is created on the default Display." |
|
339 |
||
340 |
^ self labels:labels selectors:selectors args:nil receiver:anObject for:nil |
|
63 | 341 |
! |
342 |
||
343 |
labels:labels selectors:selectors |
|
344 |
"create and return a menu with label-items and selectors. The receiver |
|
345 |
will either be defined later, or not used at all (if opened via startUp)" |
|
346 |
||
119 | 347 |
^ self labels:labels selectors:selectors args:nil receiver:nil for:nil |
348 |
! |
|
349 |
||
350 |
labels:labels selectors:selectors args:argArray |
|
351 |
"create and return a menu with label-items and selectors. The receiver |
|
352 |
will either be defined later, or not used at all (if opened via startUp)" |
|
353 |
||
354 |
^ self labels:labels selectors:selectors args:argArray receiver:nil for:nil |
|
0 | 355 |
! ! |
356 |
||
357 |
!PopUpMenu class methodsFor:'ST-80 instance creation'! |
|
358 |
||
359 |
labels:labels |
|
119 | 360 |
"ST80R2 compatibility" |
361 |
||
0 | 362 |
^ self labels:labels lines:nil values:nil |
363 |
! |
|
364 |
||
365 |
labels:labels values:values |
|
119 | 366 |
"ST80R2 compatibility" |
367 |
||
0 | 368 |
^ self labels:labels lines:nil values:values |
369 |
! |
|
370 |
||
371 |
labels:labels lines:lines |
|
119 | 372 |
"ST80R2 compatibility" |
373 |
||
0 | 374 |
^ self labels:labels lines:lines values:nil |
375 |
! |
|
376 |
||
377 |
labels:labels lines:lines values:values |
|
119 | 378 |
"ST80R2 compatibility" |
379 |
||
0 | 380 |
^ (self new) labels:labels lines:lines values:values |
381 |
! |
|
382 |
||
119 | 383 |
labelArray:labels values:values |
384 |
"ST80R4 compatibility" |
|
385 |
||
386 |
^ self labels:labels lines:nil values:values |
|
387 |
! |
|
388 |
||
389 |
labelArray:labels lines:lines values:values |
|
390 |
"ST80R4 compatibility" |
|
391 |
||
392 |
^ self labels:labels lines:lines values:values |
|
393 |
! |
|
394 |
||
0 | 395 |
labelList:labels values:values |
119 | 396 |
"ST80R4 compatibility: |
397 |
given a list consisting of group label entries (to be separated by |
|
398 |
lines), convert into standard form (using '-' for lines. |
|
399 |
" |
|
400 |
||
401 |
|newLabels newValues lS vS first| |
|
402 |
||
403 |
newLabels := OrderedCollection new. |
|
404 |
newValues := OrderedCollection new. |
|
405 |
lS := ReadStream on:labels. |
|
406 |
values notNil ifTrue:[vS := ReadStream on:values]. |
|
407 |
first := true. |
|
408 |
[lS atEnd] whileFalse:[ |
|
409 |
|entry| |
|
410 |
||
411 |
entry := lS next. |
|
412 |
entry isCollection ifTrue:[ |
|
413 |
first ifFalse:[ |
|
414 |
newLabels add:'-'. |
|
415 |
values notNil ifTrue:[newValues add:nil] |
|
416 |
]. |
|
417 |
newLabels addAll:entry. |
|
418 |
values notNil ifTrue:[newValues addAll:(vS next:entry size)] |
|
419 |
] ifFalse:[ |
|
420 |
newLabels add:entry. |
|
421 |
values notNil ifTrue:[newValues add:(vS next)] |
|
422 |
]. |
|
423 |
first := false. |
|
424 |
]. |
|
425 |
values isNil ifTrue:[ |
|
426 |
^ self labels:newLabels |
|
427 |
]. |
|
428 |
^ self labels:newLabels values:newValues |
|
429 |
||
430 |
" |
|
431 |
(PopUpMenu labels:#('1' '2' '3') values:#(1 2 3)) showAtPointer |
|
432 |
(PopUpMenu labelList:#(('1') ('2' '3')) values:#(1 2 3)) showAtPointer |
|
433 |
(PopUpMenu labelList:#(('1') ('2') ('3')) values:#(1 2 3)) showAtPointer |
|
434 |
" |
|
435 |
! |
|
436 |
||
437 |
labelList:labels |
|
438 |
"ST80R4 compatibility: |
|
439 |
given a list consisting of group label entries (to be separated by |
|
440 |
lines), convert into standard form (using '-' for lines. |
|
441 |
" |
|
442 |
||
443 |
^ self labelList:labels values:nil |
|
444 |
||
445 |
" |
|
446 |
(PopUpMenu labels:#('1' '2' '3')) showAtPointer |
|
447 |
(PopUpMenu labelList:#(('1') ('2' '3'))) showAtPointer |
|
448 |
(PopUpMenu labelList:#(('1') ('2') ('3'))) showAtPointer |
|
449 |
" |
|
0 | 450 |
! |
451 |
||
452 |
labelList:labels lines:lines values:values |
|
119 | 453 |
"mhmh what is that ?" |
454 |
||
0 | 455 |
^ (self new) labels:labels lines:lines values:values |
456 |
! ! |
|
457 |
||
458 |
!PopUpMenu methodsFor:'initialization'! |
|
459 |
||
460 |
initialize |
|
461 |
super initialize. |
|
59 | 462 |
|
0 | 463 |
memorize := true. |
21 | 464 |
hideOnLeave := false. |
119 | 465 |
defaultHideOnRelease := DefaultHideOnRelease. |
0 | 466 |
! |
467 |
||
468 |
initEvents |
|
469 |
super initEvents. |
|
470 |
self enableEnterLeaveEvents. |
|
471 |
self enableMotionEvents. |
|
472 |
! ! |
|
473 |
||
474 |
!PopUpMenu methodsFor:'realization'! |
|
475 |
||
476 |
fixSize |
|
119 | 477 |
"called right before the view is made visible. |
478 |
adjust my size to the size of the actual menu" |
|
0 | 479 |
|
480 |
|extra newWidth newHeight| |
|
481 |
||
482 |
extra := margin * 2. |
|
119 | 483 |
menuView resizeIfChanged. |
0 | 484 |
newWidth := menuView width + extra. |
485 |
newHeight := menuView height + extra. |
|
486 |
((newWidth ~~ width) or:[newHeight ~~ height]) ifTrue:[ |
|
59 | 487 |
self extent:(menuView width + extra) @ (menuView height + extra) |
0 | 488 |
]. |
489 |
super fixSize |
|
490 |
! |
|
491 |
||
492 |
realize |
|
493 |
menuView deselectWithoutRedraw. |
|
494 |
super realize. |
|
105 | 495 |
hideOnRelease := defaultHideOnRelease. |
0 | 496 |
! ! |
497 |
||
498 |
!PopUpMenu methodsFor:'private accessing'! |
|
499 |
||
500 |
menu:aMenu |
|
501 |
"set the actual menu" |
|
502 |
||
503 |
menuView := aMenu. |
|
504 |
menuView origin:(margin @ margin). |
|
70 | 505 |
menuView borderWidth:0. |
506 |
menuView masterView:self |
|
0 | 507 |
! |
508 |
||
509 |
menuView |
|
510 |
"return the actual menu" |
|
511 |
||
512 |
^ menuView |
|
7 | 513 |
! |
514 |
||
515 |
superMenu:aMenu |
|
516 |
"return the superMenu" |
|
517 |
||
518 |
menuView superMenu:aMenu |
|
127 | 519 |
! ! |
520 |
||
521 |
!PopUpMenu methodsFor:'accessing-mvc'! |
|
77 | 522 |
|
127 | 523 |
changeMessage:aSymbol |
524 |
"forward to my menu" |
|
525 |
||
526 |
menuView changeMessage:aSymbol |
|
77 | 527 |
! |
528 |
||
120 | 529 |
changeMessage |
127 | 530 |
"forward from my menu" |
531 |
||
120 | 532 |
^ menuView changeMessage |
77 | 533 |
! |
534 |
||
535 |
model |
|
536 |
^ menuView model |
|
537 |
! |
|
538 |
||
539 |
model:aModel |
|
540 |
menuView model:aModel |
|
0 | 541 |
! ! |
542 |
||
59 | 543 |
!PopUpMenu methodsFor:'menuview messages'! |
544 |
||
545 |
doesNotUnderstand:aMessage |
|
546 |
"forward all menu-view messages" |
|
547 |
||
548 |
(menuView respondsTo:(aMessage selector)) ifTrue:[ |
|
549 |
^ aMessage sendTo:menuView |
|
550 |
]. |
|
551 |
^ super doesNotUnderstand:aMessage |
|
552 |
! ! |
|
553 |
||
127 | 554 |
!PopUpMenu methodsFor:'accessing-behavior'! |
555 |
||
556 |
hideOnLeave:aBoolean |
|
557 |
"set/clear the hideOnLeave attribute, which controls |
|
558 |
if the menu should be hidden when the pointer leaves |
|
559 |
the view (used with multiple-menus)" |
|
560 |
||
561 |
hideOnLeave := aBoolean |
|
137 | 562 |
! |
563 |
||
564 |
hideOnRelease:aBoolean |
|
565 |
"set/clear the hideOnRelease attribute, which controls |
|
566 |
if the menu should be hidden when the button is released" |
|
567 |
||
568 |
hideOnRelease := aBoolean. |
|
569 |
menuView hideOnRelease:aBoolean |
|
127 | 570 |
! ! |
571 |
||
572 |
!PopUpMenu methodsFor:'accessing-look'! |
|
0 | 573 |
|
574 |
viewBackground:aColor |
|
575 |
"this is a kludge and will vanish ..." |
|
576 |
||
577 |
super viewBackground:aColor. |
|
578 |
menuView viewBackground:aColor |
|
579 |
! |
|
580 |
||
21 | 581 |
font:aFont |
582 |
menuView font:aFont |
|
127 | 583 |
! ! |
584 |
||
585 |
!PopUpMenu methodsFor:'accessing-items'! |
|
21 | 586 |
|
587 |
labels |
|
588 |
"return the list of labels" |
|
589 |
||
590 |
actionLabels notNil ifTrue:[ |
|
81 | 591 |
^ actionLabels asStringCollection |
21 | 592 |
]. |
593 |
^ menuView list |
|
594 |
! |
|
595 |
||
38 | 596 |
indexOf:indexOrName |
597 |
"return the index of a submenu - or 0 if there is none" |
|
598 |
||
599 |
^ menuView indexOf:indexOrName |
|
600 |
! |
|
601 |
||
602 |
remove:indexOrName |
|
603 |
"remove a menu entry" |
|
604 |
||
605 |
menuView remove:indexOrName |
|
606 |
! |
|
607 |
||
0 | 608 |
subMenuAt:indexOrName put:aMenu |
609 |
"define a submenu to be shown for entry indexOrName" |
|
610 |
||
7 | 611 |
" |
0 | 612 |
aMenu hideOnLeave:true. |
7 | 613 |
" |
614 |
menuView subMenuAt:indexOrName put:aMenu. |
|
615 |
"tell the submenu to notify me when action is performed" |
|
616 |
aMenu superMenu:self. |
|
0 | 617 |
|
112 | 618 |
" |
619 |
|v m someObject| |
|
620 |
||
0 | 621 |
v := View new. |
622 |
m := PopUpMenu labels:#('1' '2' '3') |
|
59 | 623 |
selectors:#(one two nil) |
112 | 624 |
receiver:someObject |
59 | 625 |
for:nil. |
0 | 626 |
m subMenuAt:3 put:(PopUpMenu |
59 | 627 |
labels:#('a' 'b' 'c') |
628 |
selectors:#(a b c) |
|
112 | 629 |
receiver:someObject |
59 | 630 |
for:nil). |
0 | 631 |
v middleButtonMenu:m. |
112 | 632 |
v realize |
633 |
" |
|
0 | 634 |
! |
635 |
||
636 |
numberOfItems |
|
21 | 637 |
"return the number of items in the menu" |
0 | 638 |
|
21 | 639 |
actionLabels notNil ifTrue:[ |
81 | 640 |
^ actionLabels asStringCollection size |
21 | 641 |
]. |
642 |
^ menuView list size |
|
0 | 643 |
! |
644 |
||
645 |
values |
|
21 | 646 |
"st-80 compatibility" |
647 |
||
0 | 648 |
^ actionValues |
649 |
! |
|
650 |
||
133 | 651 |
values:aValueArray |
652 |
"st-80 compatibility" |
|
653 |
||
654 |
actionValues := aValueArray |
|
655 |
! |
|
656 |
||
0 | 657 |
lines |
21 | 658 |
"st-80 compatibility" |
659 |
||
0 | 660 |
^ actionLines |
661 |
! |
|
662 |
||
663 |
labels:labelString lines:lineArray values:valueArray |
|
21 | 664 |
"define the menu the ST-80 way (with labels and lines defined separately)" |
0 | 665 |
|
666 |
|labelArray argArray convertedLabels |
|
667 |
offs dstOffs linePos| |
|
668 |
||
669 |
actionLabels := labelString. |
|
670 |
actionLines := lineArray. |
|
671 |
actionValues := valueArray. |
|
672 |
||
81 | 673 |
labelArray := labelString asStringCollection. |
0 | 674 |
|
675 |
convertedLabels := Array new:(labelArray size + lineArray size). |
|
676 |
argArray := Array new:(labelArray size + lineArray size). |
|
677 |
||
678 |
offs := 1. |
|
679 |
dstOffs := 1. |
|
680 |
1 to:lineArray size do:[:lineIndex | |
|
59 | 681 |
linePos := lineArray at:lineIndex. |
682 |
[offs <= linePos] whileTrue:[ |
|
683 |
convertedLabels at:dstOffs put:(labelArray at:offs). |
|
684 |
argArray at:dstOffs put:offs. |
|
685 |
offs := offs + 1. |
|
686 |
dstOffs := dstOffs + 1 |
|
687 |
]. |
|
688 |
convertedLabels at:dstOffs put:'-'. |
|
689 |
argArray at:dstOffs put:nil. |
|
690 |
dstOffs := dstOffs + 1 |
|
0 | 691 |
]. |
692 |
[offs <= labelArray size] whileTrue:[ |
|
59 | 693 |
convertedLabels at:dstOffs put:(labelArray at:offs). |
694 |
argArray at:dstOffs put:offs. |
|
695 |
offs := offs + 1. |
|
696 |
dstOffs := dstOffs + 1 |
|
0 | 697 |
]. |
698 |
self menu:(MenuView |
|
59 | 699 |
labels:convertedLabels |
126 | 700 |
selectors:nil |
59 | 701 |
args:argArray |
702 |
receiver:nil |
|
703 |
in:self) |
|
0 | 704 |
! ! |
705 |
||
105 | 706 |
!PopUpMenu methodsFor:'deactivation'! |
0 | 707 |
|
708 |
hide |
|
7 | 709 |
"hide the menu - if there are any pop-up-submenus, hide them also" |
710 |
||
711 |
menuView hideSubmenu. |
|
38 | 712 |
windowGroup notNil ifTrue:[ |
70 | 713 |
windowGroup removeView:menuView. |
38 | 714 |
]. |
77 | 715 |
super hide. |
70 | 716 |
menuView superMenu notNil ifTrue:[ |
717 |
menuView superMenu regainControl |
|
718 |
]. |
|
0 | 719 |
! ! |
720 |
||
721 |
!PopUpMenu methodsFor:'ST-80 activation'! |
|
722 |
||
723 |
startUp |
|
38 | 724 |
"start the menu modal - return the selected value, |
725 |
or - if no values where specified - return the index. |
|
0 | 726 |
If nothing was selected, return 0. |
38 | 727 |
Modal - i.e. stay in the menu until finished. |
728 |
This is the ST-80 way of launching a menu." |
|
0 | 729 |
|
96 | 730 |
|return rec sel0 sel1 arg| |
63 | 731 |
|
119 | 732 |
return := 0. |
733 |
||
0 | 734 |
menuView action:[:selected | |
63 | 735 |
|actionIndex value sel retVal| |
38 | 736 |
|
63 | 737 |
retVal := 0. |
95 | 738 |
|
59 | 739 |
menuView args isNil ifTrue:[ |
63 | 740 |
menuView selectors notNil ifTrue:[ |
119 | 741 |
"/ sel := menuView selectors at:selected. |
742 |
"/ sel notNil ifTrue:[sel0 := sel]. |
|
743 |
||
744 |
"/ (arg := menuView checkFlags at:selected) isNil ifTrue:[ |
|
745 |
sel0 := menuView selectors at:selected. |
|
746 |
"/ ] ifFalse:[ |
|
747 |
"/ sel1 := menuView selectors at:selected. |
|
748 |
"/ ]. |
|
749 |
"/ retVal := nil. |
|
63 | 750 |
] |
751 |
] ifFalse:[ |
|
752 |
actionIndex := menuView args at:selected. |
|
753 |
actionIndex notNil ifTrue:[ |
|
754 |
actionValues isNil ifTrue:[ |
|
95 | 755 |
menuView selectors notNil ifTrue:[ |
756 |
"/ mhmh an ST/X menu started the ST-80 way |
|
96 | 757 |
sel1 := menuView selectors at:selected. |
758 |
arg := actionIndex. |
|
119 | 759 |
"/ retVal := nil. |
95 | 760 |
] ifFalse:[ |
761 |
retVal := actionIndex |
|
762 |
] |
|
63 | 763 |
] ifFalse:[ |
764 |
retVal := actionValues at:actionIndex. |
|
765 |
(retVal isKindOf:PopUpMenu) ifTrue:[ |
|
766 |
retVal := retVal startUp |
|
767 |
] |
|
768 |
] |
|
95 | 769 |
] ifFalse:[ |
770 |
"/ mhmh an ST/X menu started the ST-80 way |
|
771 |
menuView selectors notNil ifTrue:[ |
|
119 | 772 |
"/ (arg := menuView checkFlags at:selected) isNil ifTrue:[ |
773 |
sel0 := menuView selectors at:selected. |
|
774 |
"/ ] ifFalse:[ |
|
775 |
"/ sel1 := menuView selectors at:selected. |
|
776 |
"/ ]. |
|
777 |
"/ retVal := nil. |
|
95 | 778 |
] |
63 | 779 |
] |
59 | 780 |
]. |
63 | 781 |
return := retVal |
0 | 782 |
]. |
783 |
self showAtPointer. |
|
96 | 784 |
|
785 |
"/ |
|
786 |
"/ mhmh an ST/X menu started the ST-80 way |
|
787 |
"/ |
|
788 |
(sel0 notNil or:[sel1 notNil]) ifTrue:[ |
|
789 |
rec := menuView receiver. |
|
790 |
sel0 notNil ifTrue:[ |
|
131 | 791 |
^ sel0 |
792 |
"/ rec perform:sel0 |
|
96 | 793 |
] ifFalse:[ |
131 | 794 |
^ Array with:sel1 with:arg. |
795 |
"/ rec perform:sel1 with:arg. |
|
96 | 796 |
] |
95 | 797 |
]. |
63 | 798 |
^ return |
38 | 799 |
|
800 |
" |
|
63 | 801 |
Transcript showCr:(PopUpMenu labels:#('foo' 'bar' 'baz')) startUp |
38 | 802 |
Transcript showCr:(PopUpMenu labels:#('foo' 'bar' 'baz') |
59 | 803 |
values:#(foo bar baz)) startUp |
38 | 804 |
" |
110 | 805 |
! |
806 |
||
807 |
startUpWithHeading:aString |
|
808 |
"start the menu modal - return the selected value, |
|
809 |
or - if no values where specified - return the index. |
|
810 |
If nothing was selected, return 0. |
|
811 |
Modal - i.e. stay in the menu until finished. |
|
812 |
This is the ST-80 way of launching a menu." |
|
813 |
||
814 |
defaultHideOnRelease := false. |
|
815 |
menuView labels notNil ifTrue:[ |
|
816 |
menuView labels:(Array with:aString with:'=') , menuView labels. |
|
817 |
]. |
|
818 |
menuView selectors notNil ifTrue:[ |
|
819 |
menuView selectors:(Array with:nil with:nil) , menuView selectors. |
|
820 |
]. |
|
821 |
"/ menuView disable:1; disable:2. |
|
822 |
||
823 |
^ self startUp. |
|
824 |
||
825 |
" |
|
826 |
(PopUpMenu |
|
827 |
labels:#('foo' 'bar')) |
|
828 |
startUp |
|
829 |
" |
|
830 |
" |
|
831 |
(PopUpMenu |
|
832 |
labels:#('foo' 'bar')) |
|
833 |
startUpWithHeading:'hello' |
|
834 |
" |
|
0 | 835 |
! ! |
836 |
||
70 | 837 |
!PopUpMenu methodsFor:'event handling'! |
0 | 838 |
|
161 | 839 |
buttonMotion:state x:x y:y |
70 | 840 |
|p superMenu| |
7 | 841 |
|
161 | 842 |
state == 0 ifTrue:[^ self]. |
843 |
||
38 | 844 |
((x >= 0) and:[x < width]) ifTrue:[ |
59 | 845 |
((y >= 0) and:[y < height]) ifTrue:[ |
105 | 846 |
hideOnRelease := true. |
161 | 847 |
menuView buttonMotion:state x:x y:y. |
59 | 848 |
^ self |
849 |
] |
|
0 | 850 |
]. |
7 | 851 |
|
852 |
"outside of myself" |
|
70 | 853 |
superMenu := menuView superMenu. |
854 |
superMenu notNil ifTrue:[ |
|
59 | 855 |
p := device translatePoint:(x @ y) |
856 |
from:drawableId |
|
857 |
to:(menuView superMenu id). |
|
161 | 858 |
superMenu buttonMotion:state x:p x y:p y |
7 | 859 |
]. |
860 |
||
861 |
menuView subMenuShown isNil ifTrue:[ |
|
161 | 862 |
menuView pointerLeave:state. |
7 | 863 |
]. |
864 |
||
0 | 865 |
hideOnLeave ifTrue:[ |
59 | 866 |
self hide |
38 | 867 |
]. |
0 | 868 |
! |
869 |
||
7 | 870 |
pointerEnter:state x:x y:y |
871 |
"catch quick release of button" |
|
872 |
||
112 | 873 |
hideOnLeave ifTrue:[ |
874 |
state == 0 ifTrue:[^ self hide]. |
|
875 |
] |
|
7 | 876 |
! |
877 |
||
0 | 878 |
pointerLeave:state |
38 | 879 |
"/ menuView pointerLeave:state. |
880 |
"/ hideOnLeave ifTrue:[ |
|
881 |
"/ self hide |
|
882 |
"/ ]. |
|
883 |
"/ menuView superMenu notNil ifTrue:[ |
|
884 |
"/ menuView superMenu regainControl |
|
885 |
"/ ] |
|
0 | 886 |
! |
887 |
||
105 | 888 |
buttonPress:button x:x y:y |
889 |
hideOnRelease ifTrue:[ |
|
890 |
self hide. |
|
891 |
" |
|
892 |
menuView buttonRelease:button x:x y:y. |
|
893 |
" |
|
894 |
menuView superMenu notNil ifTrue:[ |
|
895 |
menuView superMenu submenuTriggered |
|
896 |
]. |
|
897 |
menuView buttonRelease:button x:x y:y. |
|
898 |
] ifFalse:[ |
|
899 |
hideOnRelease := true. |
|
900 |
((x >= 0) and:[x < width]) ifTrue:[ |
|
901 |
((y >= 0) and:[y < height]) ifTrue:[ |
|
902 |
menuView buttonPress:button x:x y:y. |
|
903 |
^ self |
|
904 |
] |
|
905 |
]. |
|
906 |
]. |
|
907 |
! |
|
908 |
||
0 | 909 |
buttonRelease:button x:x y:y |
105 | 910 |
hideOnRelease ifFalse:[ |
911 |
^ self |
|
912 |
]. |
|
913 |
||
0 | 914 |
self hide. |
7 | 915 |
" |
916 |
menuView buttonRelease:button x:x y:y. |
|
917 |
" |
|
918 |
menuView superMenu notNil ifTrue:[ |
|
59 | 919 |
menuView superMenu submenuTriggered |
7 | 920 |
]. |
921 |
menuView buttonRelease:button x:x y:y. |
|
112 | 922 |
! |
923 |
||
924 |
keyPress:key x:x y:y |
|
925 |
"/ hideOnRelease := true. |
|
926 |
menuView keyPress:key x:x y:y. |
|
0 | 927 |
! ! |