author | Claus Gittinger <cg@exept.de> |
Thu, 25 Apr 1996 19:17:58 +0200 | |
changeset 616 | 56cf67c82664 |
parent 321 | f31da2a1d387 |
child 621 | decaeae1910d |
permissions | -rw-r--r-- |
0 | 1 |
" |
6 | 2 |
COPYRIGHT (c) 1989 by Claus Gittinger |
72 | 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 |
||
135 | 13 |
SimpleView subclass:#View |
616 | 14 |
instanceVariableNames:'model aspectMsg changeMsg menuMsg' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'Views-Basic' |
|
0 | 18 |
! |
19 |
||
2 | 20 |
!View class methodsFor:'documentation'! |
41 | 21 |
|
46 | 22 |
copyright |
23 |
" |
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
72 | 25 |
All Rights Reserved |
46 | 26 |
|
27 |
This software is furnished under a license and may be used |
|
28 |
only in accordance with the terms of that license and with the |
|
29 |
inclusion of the above copyright notice. This software may not |
|
30 |
be provided or otherwise made available to, or used by, any |
|
31 |
other person. No title to or ownership of the software is |
|
32 |
hereby transferred. |
|
33 |
" |
|
34 |
! |
|
35 |
||
41 | 36 |
documentation |
2 | 37 |
" |
135 | 38 |
this class adds provisions for views which show or work on a model. |
39 |
This functionality used to be in View, but has been extracted into SimpleView and |
|
40 |
this new View class, to take some of the stuff out of views which do not need |
|
41 |
this functionality (i.e. all views which do only geometry management). |
|
82 | 42 |
Instances of View are seldom used, most views in the system inherit |
43 |
from this class. |
|
41 | 44 |
|
616 | 45 |
[Instance variables:] |
46 |
model <nil | any> the model (if any) |
|
41 | 47 |
|
616 | 48 |
aspectMsg <nil | Symbol> the aspect; typically |
49 |
dependentViews react on changes |
|
50 |
of this aspect and update their contents. |
|
51 |
||
52 |
changeMsg <nil | Symbol> the changeMessage; typically |
|
53 |
dependentViews send this message to |
|
54 |
the model to tell it about changes. |
|
140 | 55 |
|
616 | 56 |
menuMsg <nil | Symbol> the menuMessage; typically |
57 |
dependentViews send this message to |
|
58 |
the model to ask for a popup menu. |
|
59 |
Some classes allow setting an explicit |
|
60 |
menuHolder and menuPerformer. |
|
61 |
||
62 |
[see also:] |
|
63 |
StandardSystemView TopView |
|
64 |
WindowGroup |
|
65 |
( introduction to view programming :html: programming/viewintro.html ) |
|
66 |
||
140 | 67 |
|
616 | 68 |
[author:] |
69 |
Claus Gittinger |
|
70 |
" |
|
71 |
! |
|
72 |
||
73 |
examples |
|
74 |
" |
|
75 |
a subView in a topView: |
|
76 |
||
77 |
|top v| |
|
140 | 78 |
|
616 | 79 |
topView := StandardSystemView new. |
80 |
v := View new. |
|
81 |
v origin:0.25 @ 0.25 corner:0.75 @ 0.75. |
|
82 |
top addSubView:v. |
|
83 |
top open |
|
84 |
||
85 |
||
86 |
||
87 |
the same, a bit more compact: |
|
88 |
||
89 |
|top v| |
|
90 |
||
91 |
topView := StandardSystemView new. |
|
92 |
v := View origin:0.25 @ 0.25 corner:0.75 @ 0.75 in:topView. |
|
93 |
top open |
|
2 | 94 |
" |
95 |
! ! |
|
0 | 96 |
|
118 | 97 |
!View class methodsFor:'instance creation'! |
98 |
||
269 | 99 |
model:aModel |
100 |
"st-80 style view creation: create a new view and set its model" |
|
101 |
||
102 |
^ self new model:aModel |
|
103 |
! |
|
104 |
||
140 | 105 |
on:aModel aspect:aspectMsg |
106 |
"st-80 style view creation: create a new view, set its model |
|
107 |
and selectors for aspect" |
|
108 |
||
109 |
^ self new |
|
110 |
on:aModel |
|
111 |
aspect:aspectMsg |
|
112 |
! |
|
113 |
||
114 |
on:aModel aspect:aspectMsg change:changeMsg |
|
115 |
"st-80 style view creation: create a new view, set its model |
|
116 |
and selectors for aspect and change" |
|
117 |
||
118 |
^ self new |
|
119 |
on:aModel |
|
120 |
aspect:aspectMsg |
|
121 |
change:changeMsg |
|
122 |
! |
|
123 |
||
135 | 124 |
on:aModel aspect:aspectMsg change:changeMsg menu:menuMsg |
118 | 125 |
"st-80 style view creation: create a new view, set its model |
140 | 126 |
and selectors for aspect, change and menu" |
118 | 127 |
|
135 | 128 |
^ self new |
129 |
on:aModel |
|
130 |
aspect:aspectMsg |
|
131 |
change:changeMsg |
|
132 |
menu:menuMsg |
|
118 | 133 |
! |
134 |
||
269 | 135 |
on:aModel aspect:aspectMsg menu:menuMsg |
136 |
"st-80 style view creation: create a new view, set its model |
|
137 |
and selectors for aspect and menu" |
|
118 | 138 |
|
269 | 139 |
^ self new |
140 |
on:aModel |
|
141 |
aspect:aspectMsg |
|
142 |
menu:menuMsg |
|
118 | 143 |
! ! |
144 |
||
123 | 145 |
!View class methodsFor:'defaults'! |
146 |
||
140 | 147 |
defaultAspectMessage |
135 | 148 |
"subclasses which by default do NOT want to be informed about changed |
149 |
models should redefine this to return nil" |
|
118 | 150 |
|
135 | 151 |
^ #value |
0 | 152 |
! |
153 |
||
140 | 154 |
defaultChangeMessage |
135 | 155 |
"subclasses which by default do NOT want to inform the model |
156 |
should redefine this to return nil" |
|
0 | 157 |
|
135 | 158 |
^ #value: |
0 | 159 |
! ! |
160 |
||
170 | 161 |
!View methodsFor:'accessing-channels'! |
162 |
||
163 |
setupChannel:newChannel for:changeSelector withOld:oldChannel |
|
164 |
"common code to change a channel" |
|
165 |
||
166 |
|oldValue| |
|
167 |
||
168 |
oldChannel notNil ifTrue:[ |
|
321
f31da2a1d387
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
269
diff
changeset
|
169 |
oldChannel retractInterestsFor:self. |
170 | 170 |
oldValue := oldChannel value. |
171 |
]. |
|
172 |
newChannel onChangeSend:changeSelector to:self. |
|
173 |
self perform:changeSelector. |
|
174 |
^ newChannel |
|
175 |
! ! |
|
176 |
||
269 | 177 |
!View methodsFor:'accessing-menus'! |
178 |
||
179 |
menuHolder |
|
180 |
"who has the menu ? |
|
181 |
By default, its the model if I have one." |
|
182 |
||
183 |
model notNil ifTrue:[^ model]. |
|
184 |
^ self |
|
185 |
! |
|
186 |
||
187 |
menuPerformer |
|
188 |
"who should perform the menu actions ? |
|
189 |
By default, its the model if I have one." |
|
190 |
||
191 |
model notNil ifTrue:[^ model]. |
|
192 |
^ self |
|
193 |
! ! |
|
194 |
||
118 | 195 |
!View methodsFor:'accessing-mvc'! |
196 |
||
269 | 197 |
addModelInterfaceTo:aDictionary |
198 |
"this adds entries for all messages sent to my model |
|
199 |
to aDictionary" |
|
200 |
||
201 |
aDictionary at:#aspectMessage put:aspectMsg. |
|
202 |
aDictionary at:#changeMessage put:changeMsg. |
|
203 |
aDictionary at:#menuMessage put:menuMsg. |
|
204 |
||
205 |
" |
|
206 |
Button new modelInterface |
|
207 |
" |
|
208 |
! |
|
135 | 209 |
|
269 | 210 |
aspect |
211 |
"Return the aspect used with changes from/to the model" |
|
212 |
||
213 |
^ aspectMsg |
|
214 |
! |
|
215 |
||
216 |
aspectMessage |
|
217 |
"Return the aspect used with changes from/to the model" |
|
218 |
||
219 |
^ aspectMsg |
|
220 |
! |
|
221 |
||
222 |
aspectMessage:aspectSymbol |
|
223 |
"ST-80 style updating: If a views aspectSymbol is nonNil, |
|
224 |
it will respond to changes of this aspect from the model." |
|
225 |
||
226 |
aspectMsg := aspectSymbol |
|
135 | 227 |
! |
228 |
||
269 | 229 |
change:changeSymbol |
230 |
"ST-80 style change notification: If a views changeSymbol is nonNil, |
|
231 |
it will send it to its model when something changes. |
|
232 |
Alias for changeMessage: for ST-80 compatibility." |
|
233 |
||
234 |
self changeMessage:changeSymbol |
|
235 |
! |
|
236 |
||
237 |
changeMessage |
|
238 |
"Return the symbol sent to the model if nonNil when something changes." |
|
135 | 239 |
|
269 | 240 |
^ changeMsg |
241 |
! |
|
242 |
||
243 |
changeMessage:aSymbol |
|
244 |
"ST-80 style change notification: If a views changeSymbol is nonNil, |
|
245 |
it will send it to its model when something changes. |
|
246 |
This is the same as change: which was added for ST-80 compatibility." |
|
247 |
||
248 |
changeMsg := aSymbol |
|
249 |
! |
|
250 |
||
251 |
controller:aController |
|
252 |
"set the controller" |
|
118 | 253 |
|
269 | 254 |
super controller:aController. |
255 |
controller notNil ifTrue:[ |
|
256 |
controller model:model |
|
0 | 257 |
] |
258 |
! |
|
259 |
||
269 | 260 |
menu:menuSymbol |
261 |
"ST-80 style menus: If a views menuSymbol is nonNil, it |
|
262 |
will send it to its model when the middleButton is pressed. |
|
263 |
That method should return nil or the menu to be shown. |
|
264 |
This is useful for very dynamic menus, where it does not |
|
265 |
make sense to define an initial menu. |
|
266 |
Alias for #menuMessage:, for ST-80 compatibility." |
|
267 |
||
268 |
menuMsg := menuSymbol |
|
269 |
! |
|
270 |
||
271 |
menuMessage |
|
272 |
"Return the symbol sent to the model to aquire the menu" |
|
273 |
||
274 |
^ menuMsg |
|
275 |
! |
|
276 |
||
277 |
menuMessage:aSymbol |
|
278 |
"ST-80 style menus: If a views menuSymbol is nonNil, it |
|
279 |
will send it to its model when the middleButton is pressed. |
|
280 |
That method should return nil or the menu to be shown. |
|
281 |
This is useful for very dynamic menus, where it does not |
|
282 |
make sense to define an initial menu. |
|
283 |
This is the same as #menu: which was added for ST-80 compatibility." |
|
284 |
||
285 |
menuMsg := aSymbol |
|
286 |
! |
|
287 |
||
118 | 288 |
model |
289 |
"return the model, for non-MVC views, |
|
290 |
this is nil or the receiver" |
|
291 |
||
292 |
^ model |
|
293 |
! |
|
294 |
||
295 |
model:aModel |
|
296 |
"set the model" |
|
297 |
||
298 |
model notNil ifTrue:[ |
|
299 |
model removeDependent:self |
|
89 | 300 |
]. |
118 | 301 |
model := aModel. |
135 | 302 |
|
118 | 303 |
model notNil ifTrue:[ |
304 |
aModel addDependent:self |
|
305 |
]. |
|
306 |
controller notNil ifTrue:[ |
|
307 |
controller model:aModel |
|
308 |
] |
|
309 |
! |
|
310 |
||
140 | 311 |
modelInterface |
312 |
"this returns a dictionary of messages sent to my model. |
|
313 |
It can be used for builders and wrappers to get information |
|
314 |
about the messages sent to my model. |
|
315 |
The returned dictionary contains one entry for each get-Msg, |
|
316 |
and the recevier will implement corresponding messages (with a colon) |
|
317 |
to set the message symbol." |
|
318 |
||
319 |
|d| |
|
320 |
||
321 |
d := IdentityDictionary new. |
|
322 |
self addModelInterfaceTo:d. |
|
323 |
^ d |
|
324 |
||
325 |
" |
|
326 |
Button new modelInterface |
|
327 |
Label new modelInterface |
|
328 |
" |
|
329 |
||
330 |
" |
|
331 |
does the view support setting the menuMessage ? |
|
332 |
||
333 |
SelectionInListView new modelInterface includesKey:#menuMessage |
|
334 |
" |
|
335 |
||
336 |
" |
|
337 |
turn off all interaction to the model: |
|
338 |
||
339 |
|m v if| |
|
340 |
||
341 |
m := SelectionInList new. |
|
342 |
m list:#('one' 'two' 'tree' 'four') asValue. |
|
343 |
m selection:1 asValue. |
|
344 |
v := SelectionInListView on:m. |
|
345 |
v open. |
|
346 |
v inspect. |
|
347 |
||
348 |
if := v modelInterface. |
|
349 |
if inspect. |
|
350 |
||
351 |
if keysAndValuesDo:[:what :msg | |
|
352 |
v perform:(what , ':') asSymbol with:nil. |
|
353 |
]. |
|
354 |
" |
|
269 | 355 |
! |
356 |
||
357 |
on:aModel aspect:aspectSymbol |
|
358 |
"ST-80 compatibility: set model and aspect |
|
359 |
messages - needs a view which uses these" |
|
360 |
||
361 |
aspectMsg := aspectSymbol. |
|
362 |
self model:aModel. |
|
363 |
! |
|
364 |
||
365 |
on:aModel aspect:aspectSymbol change:changeSymbol |
|
366 |
"ST-80 compatibility: set model, aspect and change |
|
367 |
messages - needs a view which uses these" |
|
368 |
||
369 |
aspectMsg := aspectSymbol. |
|
370 |
changeMsg := changeSymbol. |
|
371 |
self model:aModel. |
|
372 |
! |
|
373 |
||
374 |
on:aModel aspect:aspectSymbol change:changeSymbol menu:menuSymbol |
|
375 |
"ST-80 compatibility: set model, aspect, change and menu |
|
376 |
messages - needs a view which uses these" |
|
377 |
||
378 |
aspectMsg := aspectSymbol. |
|
379 |
changeMsg := changeSymbol. |
|
380 |
menuMsg := menuSymbol. |
|
381 |
self model:aModel. |
|
382 |
! |
|
383 |
||
384 |
on:aModel aspect:aspectSymbol menu:menuSymbol |
|
385 |
"ST-80 compatibility: set model, aspect and menu |
|
386 |
messages - needs a view which uses these" |
|
387 |
||
388 |
aspectMsg := aspectSymbol. |
|
389 |
menuMsg := menuSymbol. |
|
390 |
self model:aModel. |
|
391 |
! |
|
392 |
||
393 |
sendChangeMessage:aSelector with:arg |
|
394 |
"tell the model about a change" |
|
395 |
||
396 |
|n selector| |
|
397 |
||
398 |
"/ |
|
399 |
"/ MVC way of doing it: |
|
400 |
"/ if the model is a block, evaluate it, optionally |
|
401 |
"/ passing the arg and the receiver as arguments. |
|
402 |
"/ |
|
403 |
"/ otherwise (the common case) send it a changeMsg message |
|
404 |
"/ also with arg and the receiver (depending on the number of arguments |
|
405 |
"/ as defined by the selector). |
|
406 |
"/ |
|
407 |
(model notNil and:[aSelector notNil]) ifTrue:[ |
|
408 |
n := aSelector numArgs. |
|
409 |
model isBlock ifTrue:[ |
|
410 |
n := model numArgs. |
|
411 |
n == 0 ifTrue:[ |
|
412 |
selector := #value |
|
413 |
] ifFalse:[ |
|
414 |
n == 1 ifTrue:[ |
|
415 |
selector := #value: |
|
416 |
] ifFalse:[ |
|
417 |
selector := #value:value: |
|
418 |
] |
|
419 |
] |
|
420 |
] ifFalse:[ |
|
421 |
selector := aSelector |
|
422 |
]. |
|
423 |
n == 0 ifTrue:[ |
|
424 |
model perform:selector |
|
425 |
] ifFalse:[ |
|
426 |
n == 1 ifTrue:[ |
|
427 |
model perform:selector with:arg |
|
428 |
] ifFalse:[ |
|
429 |
model perform:selector with:arg with:self |
|
430 |
] |
|
431 |
] |
|
432 |
] |
|
433 |
! |
|
434 |
||
435 |
sendChangeMessageWith:arg |
|
436 |
"tell the model about a change" |
|
437 |
||
438 |
self sendChangeMessage:changeMsg with:arg |
|
72 | 439 |
! ! |
440 |
||
269 | 441 |
!View methodsFor:'drawing'! |
118 | 442 |
|
269 | 443 |
redraw |
444 |
"redraw myself |
|
445 |
if there is a model, this one shall redraw itself, |
|
446 |
otherwise we cannot do much here - has to be redefined in subclasses" |
|
118 | 447 |
|
269 | 448 |
model notNil ifTrue:[ |
449 |
model update:self |
|
450 |
] |
|
118 | 451 |
! ! |
452 |
||
453 |
!View methodsFor:'initialization'! |
|
454 |
||
455 |
initialize |
|
456 |
super initialize. |
|
140 | 457 |
|
458 |
aspectMsg := self class defaultAspectMessage. |
|
459 |
changeMsg := self class defaultChangeMessage. |
|
460 |
||
135 | 461 |
model notNil ifTrue:[ |
462 |
controller notNil ifTrue:[ |
|
118 | 463 |
controller model:model |
464 |
] |
|
81 | 465 |
]. |
118 | 466 |
! ! |
467 |
||
468 |
!View methodsFor:'realization'! |
|
469 |
||
470 |
destroy |
|
471 |
"unrealize & destroy - make me invisible, destroy subviews then |
|
472 |
make me unknown to the device" |
|
473 |
||
474 |
model notNil ifTrue:[ |
|
475 |
model removeDependent:self. |
|
476 |
model := nil. |
|
477 |
]. |
|
478 |
super destroy. |
|
479 |
! ! |
|
269 | 480 |
|
481 |
!View class methodsFor:'documentation'! |
|
482 |
||
483 |
version |
|
616 | 484 |
^ '$Header: /cvs/stx/stx/libview/View.st,v 1.50 1996-04-25 17:15:42 cg Exp $' |
269 | 485 |
! ! |