author | ca |
Thu, 28 May 1998 15:23:08 +0200 | |
changeset 950 | 01920811b83b |
parent 940 | 418cfd9b298d |
child 972 | 09bc0765dbc1 |
permissions | -rw-r--r-- |
451 | 1 |
Object subclass:#Menu |
579 | 2 |
instanceVariableNames:'items groupSizes receiver' |
451 | 3 |
classVariableNames:'' |
4 |
poolDictionaries:'' |
|
5 |
category:'Views-Support' |
|
6 |
! |
|
7 |
||
8 |
!Menu class methodsFor:'documentation'! |
|
9 |
||
10 |
documentation |
|
11 |
" |
|
12 |
not yet finished Menu class - this will eventually replace |
|
13 |
most of the MenuView and PopUpMenu stuff. |
|
14 |
(and hopefully be ST-80 compatible ...) |
|
15 |
||
16 |
For now, only a subset of the full protocol is implemented. |
|
17 |
||
18 |
[author:] |
|
740 | 19 |
Claus Gittinger |
451 | 20 |
|
21 |
[see also:] |
|
740 | 22 |
MenuItem |
23 |
PopUpMenu |
|
451 | 24 |
" |
25 |
! ! |
|
26 |
||
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
27 |
!Menu class methodsFor:'instance creation'! |
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
28 |
|
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
29 |
labelArray: arrayOfString lines: linesArray values: valueArrayOrNil |
737 | 30 |
"return a menu with menu items built with labels from arrayOfString (not Symbols). |
31 |
The linesArray describes which menu items are the last menu item in their group. |
|
32 |
The valueArray contains value objects for each menu item |
|
33 |
(or is nil if no value objects are specified)." |
|
34 |
||
35 |
| nLabel valueArray menuItems groupLengths | |
|
36 |
||
37 |
nLabel := arrayOfString size. |
|
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
38 |
|
737 | 39 |
valueArrayOrNil isNil ifTrue:[ |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
40 |
(valueArray := arrayOfString isEmpty) ifTrue: [ |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
41 |
valueArray := #() |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
42 |
] ifFalse:[ |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
43 |
valueArray := (1 to:nLabel) |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
44 |
] |
737 | 45 |
] ifFalse:[ |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
46 |
valueArray := valueArrayOrNil |
737 | 47 |
]. |
48 |
||
49 |
nLabel ~~ valueArray size ifTrue: [ |
|
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
50 |
^ self error: 'illegal menu combination' |
737 | 51 |
]. |
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
52 |
|
737 | 53 |
menuItems := Array new:nLabel. |
54 |
1 to:nLabel do:[:i | |
|
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
55 |
|mi v| |
737 | 56 |
|
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
57 |
mi := MenuItem labeled: (arrayOfString at:i) asString. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
58 |
v := valueArray at: i. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
59 |
(v isKindOf: Menu) ifTrue: [mi submenu: v]. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
60 |
menuItems at: i put: mi |
737 | 61 |
]. |
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
62 |
|
737 | 63 |
(linesArray == nil or:[linesArray isEmpty]) ifTrue:[ |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
64 |
groupLengths := (menuItems isEmpty) |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
65 |
ifTrue: [Array new: 0] |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
66 |
ifFalse: [Array with: menuItems size] |
737 | 67 |
] ifFalse:[ |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
68 |
groupLengths := Array new: linesArray size + 1. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
69 |
groupLengths at: 1 put: linesArray first. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
70 |
2 to: linesArray size do: [:i | |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
71 |
groupLengths at: i put: (linesArray at: i) - (linesArray at: i - 1) |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
72 |
]. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
73 |
groupLengths at: groupLengths size put: menuItems size - linesArray last. |
737 | 74 |
]. |
75 |
||
76 |
^ self new |
|
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
77 |
menuItems: menuItems |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
78 |
menuItemGroups: groupLengths |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
79 |
values: valueArray |
737 | 80 |
|
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
81 |
"Modified: / 19.4.1998 / 11:30:18 / cg" |
696 | 82 |
! |
83 |
||
84 |
labelList:arrayOfGroupStrings values:valueArrayOrNil |
|
85 |
|labels lines| |
|
86 |
||
87 |
lines := arrayOfGroupStrings collect:[:each | each size]. |
|
88 |
labels := OrderedCollection new. |
|
89 |
arrayOfGroupStrings do:[:group | labels addAll:group]. |
|
90 |
^ self labelArray:labels lines:lines values:valueArrayOrNil |
|
91 |
||
92 |
"Modified: 20.6.1997 / 10:46:45 / cg" |
|
93 |
"Created: 13.9.1997 / 10:35:46 / cg" |
|
737 | 94 |
! |
95 |
||
738 | 96 |
labels:aString lines:linesArray values:valueArrayOrNil |
737 | 97 |
^ self |
740 | 98 |
labelArray:(aString asCollectionOfLines) |
99 |
lines:linesArray |
|
100 |
values:valueArrayOrNil |
|
737 | 101 |
|
102 |
"Created: / 31.10.1997 / 03:12:20 / cg" |
|
738 | 103 |
"Modified: / 31.10.1997 / 03:23:42 / cg" |
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
104 |
! ! |
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
105 |
|
724 | 106 |
!Menu methodsFor:'ST-80 compatibility'! |
107 |
||
108 |
indexOfMenuItem:anItem |
|
109 |
||
110 |
^ items indexOf:anItem |
|
111 |
||
112 |
"Created: / 27.10.1997 / 16:34:19 / cg" |
|
113 |
! |
|
114 |
||
115 |
menuButtons |
|
116 |
"ST-80 seems to use a special menuButton class here. |
|
117 |
Here, kludge a collection of menuItems." |
|
118 |
||
119 |
^ items |
|
120 |
||
121 |
"Created: / 27.10.1997 / 16:33:35 / cg" |
|
122 |
! ! |
|
123 |
||
451 | 124 |
!Menu methodsFor:'accessing'! |
125 |
||
696 | 126 |
atNameKey:aNameKey |
724 | 127 |
"return the menuItem for the given nameKey; nil if no such item is in the menu. |
128 |
Searches in allItems (i.e. also in subMenus)" |
|
696 | 129 |
|
724 | 130 |
self allItemsDo:[:anItem| |
740 | 131 |
anItem nameKey == aNameKey ifTrue:[^ anItem] |
724 | 132 |
]. |
696 | 133 |
^ nil |
134 |
||
724 | 135 |
"Modified: / 27.10.1997 / 15:12:00 / cg" |
696 | 136 |
! |
137 |
||
578 | 138 |
groupSizes |
139 |
"return the value of the instance variable 'groupSizes' (automatically generated)" |
|
140 |
||
141 |
^ groupSizes! |
|
142 |
||
143 |
groupSizes:something |
|
144 |
"set the value of the instance variable 'groupSizes' (automatically generated)" |
|
145 |
||
146 |
groupSizes := something.! |
|
147 |
||
584 | 148 |
labelAt:anIndex |
149 |
"gets the label of the menu item at the given index or nil |
|
150 |
" |
|
151 |
|item| |
|
152 |
||
153 |
(item := self menuItemAt:anIndex) notNil ifTrue:[ |
|
801 | 154 |
^ item label |
584 | 155 |
]. |
801 | 156 |
^ nil |
584 | 157 |
|
801 | 158 |
"Modified: / 2.2.1998 / 13:28:32 / cg" |
584 | 159 |
! |
160 |
||
161 |
labelAtValue:aValue |
|
162 |
"gets the label of the menu item assigned to value |
|
163 |
" |
|
164 |
|item| |
|
165 |
||
801 | 166 |
item := self menuAndSubmenusDetectItem:[:anItem | anItem value == aValue ]. |
584 | 167 |
|
168 |
item notNil ifTrue:[ |
|
801 | 169 |
^ item label |
584 | 170 |
]. |
801 | 171 |
^ nil |
584 | 172 |
|
801 | 173 |
"Modified: / 2.2.1998 / 13:28:28 / cg" |
584 | 174 |
! |
175 |
||
451 | 176 |
labels |
177 |
"return a collection of labels from my items" |
|
178 |
||
179 |
^ items collect:[:anItem | anItem label] |
|
180 |
||
181 |
"Created: 25.2.1997 / 19:47:53 / cg" |
|
182 |
! |
|
183 |
||
940 | 184 |
lastItem |
185 |
"returns last item |
|
186 |
" |
|
187 |
^ items notNil ifTrue:[items last] ifFalse:[nil] |
|
188 |
! |
|
189 |
||
489 | 190 |
lines |
737 | 191 |
"return the indexes of the menu items that are the last menu item in their group (except the very last)." |
489 | 192 |
|
801 | 193 |
|lines groupSz| |
737 | 194 |
|
801 | 195 |
(groupSz := groupSizes size) <= 1 ifTrue: [^ Array new]. |
196 |
lines := Array new:(groupSz - 1). |
|
197 |
lines at:1 put:groupSizes first. |
|
198 |
2 to:(groupSz-1) do:[:i | |
|
199 |
lines at:i put:(lines at:(i - 1)) + (groupSizes at:i) |
|
200 |
]. |
|
201 |
^ lines |
|
489 | 202 |
|
801 | 203 |
"Modified: / 2.2.1998 / 13:28:19 / cg" |
489 | 204 |
! |
205 |
||
206 |
menuItemAt:index |
|
584 | 207 |
"gets the menu item at the given index. When the index is out of bounds |
208 |
nil is returned |
|
209 |
" |
|
210 |
(index > 0 and:[index <= items size]) ifTrue:[ |
|
740 | 211 |
^ items at:index |
584 | 212 |
]. |
213 |
^ nil |
|
489 | 214 |
! |
215 |
||
724 | 216 |
menuItemLabeled:anItemLabel |
217 |
"return the menuItem for the given nameKey; nil if no such item is in the menu. |
|
218 |
Searches all items (i.e. also submenu items)" |
|
219 |
||
220 |
self allItemsDo:[:anItem| |
|
740 | 221 |
|l| |
724 | 222 |
|
740 | 223 |
((l := anItem label) sameAs: anItemLabel) ifTrue:[ |
224 |
^ anItem |
|
225 |
]. |
|
226 |
(l includes:$&) ifTrue:[ |
|
227 |
((l copyWithout:$&) sameAs: anItemLabel) ifTrue:[ |
|
228 |
^ anItem |
|
229 |
] |
|
230 |
] |
|
231 |
]. |
|
724 | 232 |
^ nil |
233 |
||
234 |
"Created: / 13.9.1997 / 10:25:16 / cg" |
|
235 |
"Modified: / 27.10.1997 / 15:23:33 / cg" |
|
236 |
! |
|
237 |
||
489 | 238 |
menuItems |
239 |
^ items |
|
240 |
! |
|
241 |
||
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
242 |
menuItems:aCollectionOfMenuItems menuItemGroups:sizes values:values |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
243 |
|n| |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
244 |
|
451 | 245 |
items := aCollectionOfMenuItems. |
579 | 246 |
groupSizes := sizes. |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
247 |
sizes size > 0 ifTrue:[ |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
248 |
n := sizes inject:0 into:[:sumSoFar :this | sumSoFar + this]. |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
249 |
n = items size ifTrue:[ |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
250 |
groupSizes := sizes copyWithoutLast:1 |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
251 |
] |
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
252 |
]. |
451 | 253 |
|
579 | 254 |
values notNil ifTrue:[ |
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
255 |
items with:values do:[:anItem :aValue |anItem value:aValue] |
454 | 256 |
]. |
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
257 |
|
900
aa93395cdb55
care for empty group at the end (avoid line at end of menu)
Claus Gittinger <cg@exept.de>
parents:
801
diff
changeset
|
258 |
"Modified: / 19.4.1998 / 11:47:34 / cg" |
451 | 259 |
! |
260 |
||
494 | 261 |
numberOfItems |
801 | 262 |
"return the number of items in this menu" |
263 |
||
494 | 264 |
^ items size |
265 |
||
801 | 266 |
"Created: / 6.3.1997 / 15:15:53 / cg" |
267 |
"Modified: / 2.2.1998 / 13:26:40 / cg" |
|
494 | 268 |
! |
269 |
||
579 | 270 |
receiver |
801 | 271 |
"return the receiver of the menu messages" |
579 | 272 |
|
801 | 273 |
^ receiver |
274 |
||
275 |
"Modified: / 2.2.1998 / 13:26:20 / cg" |
|
276 |
! |
|
579 | 277 |
|
278 |
receiver:something |
|
801 | 279 |
"set the receiver of the menu messages" |
579 | 280 |
|
801 | 281 |
receiver := something. |
282 |
||
283 |
"Modified: / 2.2.1998 / 13:26:29 / cg" |
|
284 |
! |
|
579 | 285 |
|
451 | 286 |
valueAt:index |
287 |
"return a collection of values from my items" |
|
288 |
||
289 |
^ (items at:index) value |
|
290 |
||
291 |
"Created: 25.2.1997 / 19:49:41 / cg" |
|
292 |
! |
|
293 |
||
494 | 294 |
valueAt:anIndex put:aValue |
295 |
"put value an an index" |
|
296 |
||
297 |
(items at:anIndex) value:aValue |
|
298 |
||
299 |
"Created: 6.3.1997 / 15:15:48 / cg" |
|
300 |
! |
|
301 |
||
451 | 302 |
values |
303 |
"return a collection of values from my items" |
|
304 |
||
305 |
^ items collect:[:anItem | anItem value] |
|
306 |
||
307 |
"Created: 25.2.1997 / 19:49:29 / cg" |
|
724 | 308 |
! |
309 |
||
310 |
values:aCollectionOfValues |
|
311 |
"return a collection of values from my items" |
|
312 |
||
313 |
|s| |
|
314 |
||
315 |
s := aCollectionOfValues readStream. |
|
316 |
self itemsDo:[:item | |
|
740 | 317 |
|val| |
724 | 318 |
|
740 | 319 |
val := s next. |
320 |
item value:val |
|
724 | 321 |
]. |
322 |
s atEnd ifFalse:[self halt] |
|
323 |
||
324 |
"Created: / 27.10.1997 / 15:15:47 / cg" |
|
325 |
! |
|
326 |
||
327 |
visibleMenuItemGroups |
|
801 | 328 |
|itemGroups visibleItemGroups nextItem| |
724 | 329 |
|
801 | 330 |
itemGroups := OrderedCollection new. |
331 |
nextItem := 1. |
|
332 |
groupSizes do:[:groupSize | |
|
333 |
itemGroups addLast: (items copyFrom:nextItem to:nextItem + groupSize - 1). |
|
334 |
nextItem := nextItem + groupSize |
|
335 |
]. |
|
336 |
self hasHiddenItems ifFalse:[^ itemGroups]. |
|
724 | 337 |
|
801 | 338 |
"Remove the hidden items." |
339 |
visibleItemGroups := OrderedCollection new. |
|
340 |
itemGroups do:[:eachItemGroup | |
|
341 |
|visibleItemGroup| |
|
342 |
||
343 |
visibleItemGroup := eachItemGroup reject:[:eachMenuItem | eachMenuItem hidden]. |
|
344 |
visibleItemGroup isEmpty ifFalse:[ |
|
345 |
visibleItemGroups addLast: visibleItemGroup |
|
346 |
] |
|
347 |
]. |
|
348 |
^ visibleItemGroups |
|
724 | 349 |
|
350 |
"Created: / 27.10.1997 / 15:07:50 / cg" |
|
801 | 351 |
"Modified: / 2.2.1998 / 13:25:52 / cg" |
451 | 352 |
! ! |
353 |
||
582 | 354 |
!Menu methodsFor:'accessing resource'! |
355 |
||
584 | 356 |
findGuiResourcesIn:aResourceContainerOrApplication |
582 | 357 |
"setup a resource owner |
358 |
" |
|
584 | 359 |
aResourceContainerOrApplication notNil ifTrue:[ |
740 | 360 |
items notNil ifTrue:[ |
361 |
items do:[:anItem| anItem findGuiResourcesIn:aResourceContainerOrApplication ] |
|
362 |
] |
|
582 | 363 |
] |
364 |
||
365 |
! ! |
|
366 |
||
940 | 367 |
!Menu methodsFor:'adding & removing'! |
368 |
||
369 |
addItem:aMenuItem |
|
370 |
items isNil ifTrue:[ |
|
371 |
items := OrderedCollection new |
|
372 |
]. |
|
373 |
items add:aMenuItem. |
|
374 |
! |
|
375 |
||
376 |
addItem:aMenuItem beforeIndex:anIndex |
|
377 |
items isNil ifTrue:[ |
|
378 |
items := OrderedCollection new |
|
379 |
]. |
|
380 |
items add:aMenuItem beforeIndex:anIndex. |
|
381 |
! |
|
382 |
||
383 |
addItem:aMenuItem value:aValue |
|
384 |
aMenuItem value:aValue. |
|
385 |
self addItem:aMenuItem. |
|
386 |
! |
|
387 |
||
388 |
addItemGroup:aCollectionOfItems |
|
389 |
groupSizes isNil ifTrue:[ |
|
390 |
groupSizes := OrderedCollection new |
|
391 |
]. |
|
392 |
groupSizes add:aCollectionOfItems size. |
|
393 |
||
394 |
aCollectionOfItems do:[:item | |
|
395 |
self addItem:item |
|
396 |
]. |
|
397 |
||
398 |
"Created: / 27.10.1997 / 15:02:15 / cg" |
|
399 |
! |
|
400 |
||
401 |
addItemGroup:aGroup values:values |
|
402 |
groupSizes isNil ifTrue:[ |
|
403 |
groupSizes := OrderedCollection new |
|
404 |
]. |
|
405 |
groupSizes add:items size. |
|
406 |
||
407 |
aGroup with:values do:[:item :value | |
|
408 |
self addItem:item value:value |
|
409 |
]. |
|
410 |
! |
|
411 |
||
412 |
addItemGroupLabels:labels values:values |
|
413 |
|items| |
|
414 |
||
415 |
items := labels with:values |
|
416 |
collect:[:label :value | |
|
417 |
|item| |
|
418 |
||
419 |
item := MenuItem new. |
|
420 |
item label:label. |
|
421 |
item value:value. |
|
422 |
]. |
|
423 |
self addItemGroup:items |
|
424 |
||
425 |
"Created: / 27.10.1997 / 19:49:27 / cg" |
|
426 |
! |
|
427 |
||
428 |
addItemLabel:label value:value |
|
429 |
|item| |
|
430 |
||
431 |
item := MenuItem new. |
|
432 |
item label:label. |
|
433 |
item value:value. |
|
434 |
self addItem:item |
|
435 |
||
436 |
"Created: / 27.10.1997 / 19:47:12 / cg" |
|
437 |
! |
|
438 |
||
439 |
removeItem:aMenuItem |
|
440 |
"remove an item from the menu" |
|
441 |
||
442 |
|idx| |
|
443 |
||
444 |
items notNil ifTrue:[ |
|
445 |
idx := items identityIndexOf:aMenuItem. |
|
446 |
idx ~~ 0 ifTrue:[ |
|
447 |
items removeAtIndex:idx |
|
448 |
] |
|
449 |
]. |
|
450 |
||
451 |
"Created: / 13.9.1997 / 10:27:31 / cg" |
|
452 |
"Modified: / 2.2.1998 / 13:26:49 / cg" |
|
453 |
! |
|
454 |
||
455 |
removeItemAt:anIndex |
|
456 |
"remove item at an index |
|
457 |
" |
|
458 |
anIndex <= items size ifTrue:[ |
|
459 |
^ items removeAtIndex:anIndex |
|
460 |
]. |
|
461 |
^ nil |
|
462 |
! ! |
|
463 |
||
451 | 464 |
!Menu methodsFor:'converting'! |
465 |
||
466 |
fromLiteralArrayEncoding:aLiteralEncodedArray |
|
467 |
"read my contents from a aLiteralEncodedArray" |
|
468 |
||
469 |
|items groups values| |
|
470 |
||
471 |
items := (aLiteralEncodedArray at:2) collect:[:item | item decodeAsLiteralArray]. |
|
472 |
groups := aLiteralEncodedArray at:3. |
|
452 | 473 |
values := aLiteralEncodedArray at:4. |
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
474 |
self menuItems:items menuItemGroups:groups values:values. |
451 | 475 |
|
476 |
"extract from PD folder.st: |
|
477 |
#(#Menu #( |
|
740 | 478 |
#(#MenuItem |
479 |
#rawLabel: 'left' |
|
480 |
#value: #left ) |
|
481 |
#(#MenuItem |
|
482 |
#rawLabel: 'center' |
|
483 |
#value: #center ) |
|
484 |
#(#MenuItem |
|
485 |
#rawLabel: 'right' |
|
486 |
#value: #right ) |
|
487 |
) |
|
488 |
#(3 ) |
|
489 |
nil |
|
451 | 490 |
) decodeAsLiteralArray |
491 |
" |
|
452 | 492 |
" |
493 |
#(#Menu #( |
|
740 | 494 |
#(#MenuItem |
495 |
#label: 'Straighten Up' ) |
|
496 |
#(#MenuItem |
|
497 |
#label: 'Inspect' ) |
|
498 |
#(#MenuItem |
|
499 |
#label: 'Coredump' ) |
|
500 |
) |
|
501 |
#(3 ) |
|
502 |
#(#straightenUp #inspect #halt ) |
|
452 | 503 |
) decodeAsLiteralArray startUp |
504 |
" |
|
457 | 505 |
|
454 | 506 |
"extract from iconicBrowser.st: |
452 | 507 |
#(#Menu #( |
740 | 508 |
#(#MenuItem |
509 |
#label: 'Straighten Up' ) |
|
510 |
#(#MenuItem |
|
511 |
#label: 'Inspect' ) |
|
512 |
#(#MenuItem |
|
513 |
#label: 'Coredump' ) |
|
514 |
) |
|
515 |
#(3 ) |
|
516 |
#(1 2 3 ) |
|
452 | 517 |
) decodeAsLiteralArray startUp |
518 |
" |
|
451 | 519 |
|
454 | 520 |
"extract from refactory213.st: |
521 |
#(#Menu #( |
|
740 | 522 |
#(#MenuItem |
523 |
#label: 'File List' |
|
524 |
#accessCharacterPosition: 1 ) |
|
525 |
#(#MenuItem #label: 'File Editor...' |
|
526 |
#accessCharacterPosition: 6 ) |
|
527 |
#(#MenuItem #label: 'Refactoring Tool...' |
|
528 |
#accessCharacterPosition: 1 ) |
|
529 |
#(#MenuItem #label: 'Workspace' |
|
530 |
#accessCharacterPosition: 1 ) |
|
531 |
#(#MenuItem #label: 'New Canvas' |
|
532 |
#accessCharacterPosition: 1 ) |
|
533 |
#(#MenuItem #label: 'Palette' |
|
534 |
#accessCharacterPosition: 1 ) |
|
535 |
#(#MenuItem #label: 'Canvas Tool' |
|
536 |
#accessCharacterPosition: 1 ) |
|
537 |
#(#MenuItem #label: 'Image Editor' |
|
538 |
#accessCharacterPosition: 1 ) |
|
539 |
#(#MenuItem #label: 'Menu Editor' |
|
540 |
#accessCharacterPosition: 1 ) |
|
541 |
#(#MenuItem #label: 'Advanced' |
|
542 |
#accessCharacterPosition: 1 ) |
|
543 |
#(#MenuItem #label: 'DLL and C Connect' |
|
544 |
#accessCharacterPosition: 1 ) |
|
545 |
#(#MenuItem #label: 'System Transcript' |
|
546 |
#accessCharacterPosition: 8 ) |
|
547 |
) |
|
548 |
#(4 5 2 1 ) |
|
549 |
#(#openFileList #openFileEditor #openRefactoringTool #toolsNewWorkspace #toolsNewCanvas #toolsPalette #toolsCanvasTool #toolsMaskEditor #toolsMenuEditor nil #openExternalFinder #toggleSystemTranscript ) |
|
550 |
) decodeAsLiteralArray startUp |
|
454 | 551 |
" |
552 |
||
457 | 553 |
"submenus: |
554 |
#(#Menu #( |
|
740 | 555 |
#(#MenuItem |
556 |
#label: 'Foo' |
|
557 |
#submenu: #(#Menu #( |
|
558 |
#(#MenuItem #label: 'foo 1') |
|
559 |
#(#MenuItem #label: 'foo 2') |
|
560 |
) |
|
561 |
nil |
|
562 |
#(11 22) |
|
563 |
) |
|
564 |
) |
|
565 |
#(#MenuItem |
|
566 |
#label: 'Inspect' ) |
|
567 |
#(#MenuItem |
|
568 |
#label: 'Coredump' ) |
|
569 |
) |
|
570 |
#(3 ) |
|
571 |
#(1 2 3 ) |
|
457 | 572 |
) decodeAsLiteralArray startUp |
573 |
" |
|
574 |
||
610
72dbf392e888
ST80 compatible inst creation method..
Claus Gittinger <cg@exept.de>
parents:
589
diff
changeset
|
575 |
"Modified: 20.6.1997 / 10:45:51 / cg" |
489 | 576 |
! |
577 |
||
578 |
literalArrayEncoding |
|
579 |
"return myself encoded as a literal array" |
|
580 |
||
581 |
|coll| |
|
582 |
||
583 |
coll := OrderedCollection new. |
|
584 |
coll add:#Menu. |
|
585 |
coll add:(items literalArrayEncoding). |
|
586 |
coll add:(groupSizes literalArrayEncoding). |
|
587 |
coll add:nil. |
|
579 | 588 |
^ coll asArray |
451 | 589 |
! ! |
590 |
||
584 | 591 |
!Menu methodsFor:'enumerating'! |
592 |
||
665 | 593 |
allItemsDo:aOneArgBlock |
594 |
"evaluate block on each item and submenu items |
|
595 |
" |
|
596 |
self itemsDo:[:anItem| |
|
740 | 597 |
|sub| |
724 | 598 |
|
740 | 599 |
aOneArgBlock value:anItem. |
600 |
(sub := anItem submenu value) notNil ifTrue:[ |
|
601 |
sub allItemsDo:aOneArgBlock |
|
602 |
] |
|
665 | 603 |
] |
724 | 604 |
|
605 |
"Modified: / 27.10.1997 / 15:09:08 / cg" |
|
665 | 606 |
! |
607 |
||
940 | 608 |
detectItem:aBlock |
609 |
"evaluate the argument, aBlock for each item in the menu until the |
|
610 |
block returns true; in this case return the item which caused the |
|
611 |
true evaluation. |
|
612 |
If none of the evaluations returns true, return the result of the |
|
613 |
evaluation of the exceptionBlock |
|
614 |
" |
|
615 |
^ self detectItem:aBlock ifNone:[self errorNotFound] |
|
616 |
||
617 |
! |
|
618 |
||
619 |
detectItem:aBlock ifNone:exceptionBlock |
|
620 |
"evaluate the argument, aBlock for each item in the menu until the |
|
621 |
block returns true; in this case return the item which caused the |
|
622 |
true evaluation. |
|
623 |
If none of the evaluations returns true, return the result of the |
|
624 |
evaluation of the exceptionBlock |
|
625 |
" |
|
626 |
items notNil ifTrue:[ |
|
627 |
^ items detect:aBlock ifNone:exceptionBlock |
|
628 |
]. |
|
629 |
^ exceptionBlock value |
|
630 |
! |
|
631 |
||
589 | 632 |
itemsDo:aOneArgBlock |
633 |
"evaluate the block for each item in the current menu |
|
634 |
" |
|
635 |
items notNil ifTrue:[items do:aOneArgBlock] |
|
636 |
! |
|
637 |
||
584 | 638 |
menuAndSubmenusDetectItem:aOneArgBlock |
639 |
"evaluate the block for each item in the current menu and all |
|
640 |
submenus. In case that the block returns a non nil argument, |
|
641 |
the item will be returned |
|
642 |
" |
|
724 | 643 |
|item| |
584 | 644 |
|
645 |
items notNil ifTrue:[ |
|
740 | 646 |
items do:[:anItem| |
647 |
|sub| |
|
724 | 648 |
|
740 | 649 |
(aOneArgBlock value:anItem) ifTrue:[ |
650 |
^ anItem |
|
651 |
]. |
|
652 |
(sub := anItem submenu value) notNil ifTrue:[ |
|
653 |
item := sub menuAndSubmenusDetectItem:aOneArgBlock. |
|
654 |
item notNil ifTrue:[ |
|
655 |
^ item |
|
656 |
] |
|
657 |
] |
|
658 |
] |
|
584 | 659 |
]. |
724 | 660 |
^ nil |
661 |
||
662 |
"Modified: / 27.10.1997 / 15:09:43 / cg" |
|
584 | 663 |
! ! |
664 |
||
672
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
665 |
!Menu methodsFor:'kludged fixes'! |
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
666 |
|
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
667 |
destroy |
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
668 |
|
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
669 |
"Created: 28.7.1997 / 10:16:52 / cg" |
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
670 |
! ! |
2db4bb41aa93
added dummy #destroy (q&d hack)
Claus Gittinger <cg@exept.de>
parents:
665
diff
changeset
|
671 |
|
584 | 672 |
!Menu methodsFor:'menu items'! |
673 |
||
740 | 674 |
someMenuItemLabeled:aLabel |
675 |
"get the menu item with that label; in case that the label |
|
676 |
is not found, nil is returned |
|
677 |
" |
|
678 |
^ self someMenuItemLabeled:aLabel ifNone:nil |
|
679 |
||
680 |
"Created: / 14.11.1997 / 20:55:17 / cg" |
|
681 |
! |
|
682 |
||
683 |
someMenuItemLabeled:aLabel ifNone:exceptionBlock |
|
684 |
"get the menu item labeled aLabel; in case that the value |
|
685 |
is not found, the given exceptionBlock is executed and its value returned |
|
686 |
" |
|
687 |
|item| |
|
688 |
||
689 |
item := self menuAndSubmenusDetectItem:[:anItem| anItem label = aLabel]. |
|
690 |
||
691 |
item notNil ifTrue:[ |
|
692 |
^ item |
|
693 |
]. |
|
694 |
^ exceptionBlock value |
|
695 |
||
696 |
"Created: / 14.11.1997 / 20:56:13 / cg" |
|
697 |
! |
|
698 |
||
584 | 699 |
someMenuItemWithValue:aValue |
700 |
"get the menu item assigned with the value; in case that the value |
|
701 |
is not found nil is returned |
|
702 |
" |
|
703 |
^ self someMenuItemWithValue:aValue ifNone:nil |
|
704 |
! |
|
705 |
||
706 |
someMenuItemWithValue:aValue ifNone:exceptionBlock |
|
707 |
"get the menu item assigned with the value; in case that the value |
|
708 |
is not found, the given exceptionBlock is executed and returned |
|
709 |
" |
|
710 |
|item| |
|
711 |
||
712 |
item := self menuAndSubmenusDetectItem:[:anItem| anItem value == aValue]. |
|
713 |
||
714 |
item notNil ifTrue:[ |
|
740 | 715 |
^ item |
584 | 716 |
]. |
717 |
^ exceptionBlock value |
|
718 |
! ! |
|
719 |
||
720 |
!Menu methodsFor:'queries'! |
|
721 |
||
724 | 722 |
hasHiddenItems |
723 |
"test whether any item is hidden" |
|
724 |
||
725 |
self allItemsDo:[:anItem| |
|
740 | 726 |
anItem isHidden ifTrue:[^ true] |
724 | 727 |
]. |
728 |
^ false |
|
729 |
||
730 |
"Modified: / 27.10.1997 / 15:12:44 / cg" |
|
731 |
! |
|
732 |
||
584 | 733 |
hasSubMenuAt:anIndex |
734 |
"test whether the menu item at the given index has a submenu |
|
735 |
" |
|
736 |
^ (self menuItemAt:anIndex) hasSubmenu |
|
737 |
! ! |
|
738 |
||
451 | 739 |
!Menu methodsFor:'startup'! |
740 |
||
729 | 741 |
show |
742 |
"realize the menu at its last position; returns the value associated with the |
|
743 |
selected item, 0 if none was selected" |
|
744 |
||
745 |
^ (MenuPanel menu:self) show ? 0 |
|
746 |
||
747 |
||
748 |
! |
|
749 |
||
750 |
showAt:aPoint |
|
751 |
"realize the menu at aPoint; returns the value associated with the |
|
752 |
selected item, 0 if none was selected" |
|
753 |
||
754 |
^ self showAt:aPoint resizing:true |
|
755 |
||
756 |
||
757 |
! |
|
758 |
||
759 |
showAt:aPoint resizing:aBoolean |
|
760 |
"realize the menu at aPoint; returns the value associated with the |
|
451 | 761 |
selected item, 0 if none was selected" |
762 |
||
729 | 763 |
^ (MenuPanel menu:self) showAt:aPoint resizing:aBoolean ? 0 |
764 |
! |
|
765 |
||
766 |
showAtPointer |
|
767 |
"realize the menu at the current pointer position; returns the value associated with the |
|
768 |
selected item, 0 if none was selected" |
|
769 |
||
770 |
^ self startUp |
|
771 |
! |
|
457 | 772 |
|
729 | 773 |
showCenteredIn:aView |
774 |
"realize the menu visible at the aView center; returns the value associated with the |
|
775 |
selected item, 0 if none was selected" |
|
776 |
||
777 |
^ (MenuPanel menu:self) showCenteredIn:aView ? 0 |
|
778 |
||
779 |
||
780 |
! |
|
781 |
||
782 |
startUp |
|
783 |
"display the menu as a popUp; returns the value associated with the |
|
784 |
selected item, 0 if none was selected" |
|
785 |
||
786 |
^ (MenuPanel menu:self) startUp ? 0 |
|
451 | 787 |
|
579 | 788 |
" |
740 | 789 |
|m| |
579 | 790 |
|
740 | 791 |
m := #(#Menu #( |
792 |
#(#MenuItem |
|
793 |
#rawLabel: 'left' |
|
794 |
#value: #left ) |
|
795 |
#(#MenuItem |
|
796 |
#rawLabel: 'center' |
|
797 |
#value: #center ) |
|
798 |
#(#MenuItem |
|
799 |
#rawLabel: 'right' |
|
800 |
#value: #right ) ) |
|
801 |
#(2) |
|
802 |
nil |
|
803 |
) decodeAsLiteralArray. |
|
451 | 804 |
|
579 | 805 |
Transcript showCR:(m startUp) |
806 |
" |
|
938
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
807 |
! |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
808 |
|
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
809 |
startUpAt:aPoint |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
810 |
"display the menu as a popUp at aPoint; returns the value associated with the |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
811 |
selected item, 0 if none was selected" |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
812 |
|
939 | 813 |
^ ((MenuPanel menu:self) startUpAt:aPoint) ? 0 |
938
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
814 |
|
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
815 |
" |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
816 |
|m| |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
817 |
|
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
818 |
m := #(#Menu #( |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
819 |
#(#MenuItem |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
820 |
#rawLabel: 'left' |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
821 |
#value: #left ) |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
822 |
#(#MenuItem |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
823 |
#rawLabel: 'center' |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
824 |
#value: #center ) |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
825 |
#(#MenuItem |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
826 |
#rawLabel: 'right' |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
827 |
#value: #right ) ) |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
828 |
#(2) |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
829 |
nil |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
830 |
) decodeAsLiteralArray. |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
831 |
|
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
832 |
Transcript showCR:(m startUpAt:100@100) |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
833 |
" |
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
834 |
|
dfa1b1666cfa
added #startUpAt: (MenuView compatibility)
Claus Gittinger <cg@exept.de>
parents:
900
diff
changeset
|
835 |
"Created: / 21.5.1998 / 14:15:21 / cg" |
939 | 836 |
"Modified: / 21.5.1998 / 14:17:46 / cg" |
451 | 837 |
! ! |
838 |
||
839 |
!Menu class methodsFor:'documentation'! |
|
840 |
||
841 |
version |
|
950 | 842 |
^ '$Header: /cvs/stx/stx/libview2/Menu.st,v 1.31 1998-05-28 13:23:08 ca Exp $' |
451 | 843 |
! ! |