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 |
|
105
|
13 |
'From Smalltalk/X, Version:2.10.5 on 14-mar-1995 at 11:13:27 am'!
|
|
14 |
|
0
|
15 |
View subclass:#Label
|
105
|
16 |
instanceVariableNames:'logo labelWidth labelHeight labelOriginX labelOriginY adjust
|
118
|
17 |
hSpace vSpace bgColor fgColor fixSize labelMsg'
|
105
|
18 |
classVariableNames:'DefaultFont DefaultForegroundColor DefaultBackgroundColor'
|
|
19 |
poolDictionaries:''
|
|
20 |
category:'Views-Layout'
|
0
|
21 |
!
|
|
22 |
|
|
23 |
Label comment:'
|
5
|
24 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
25 |
All Rights Reserved
|
0
|
26 |
|
125
|
27 |
$Header: /cvs/stx/stx/libwidg/Label.st,v 1.22 1995-05-09 01:56:01 claus Exp $
|
0
|
28 |
'!
|
|
29 |
|
3
|
30 |
!Label class methodsFor:'documentation'!
|
|
31 |
|
38
|
32 |
copyright
|
|
33 |
"
|
|
34 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
35 |
All Rights Reserved
|
38
|
36 |
|
|
37 |
This software is furnished under a license and may be used
|
|
38 |
only in accordance with the terms of that license and with the
|
|
39 |
inclusion of the above copyright notice. This software may not
|
|
40 |
be provided or otherwise made available to, or used by, any
|
|
41 |
other person. No title to or ownership of the software is
|
|
42 |
hereby transferred.
|
|
43 |
"
|
|
44 |
!
|
|
45 |
|
|
46 |
version
|
|
47 |
"
|
125
|
48 |
$Header: /cvs/stx/stx/libwidg/Label.st,v 1.22 1995-05-09 01:56:01 claus Exp $
|
38
|
49 |
"
|
|
50 |
!
|
|
51 |
|
3
|
52 |
documentation
|
|
53 |
"
|
38
|
54 |
This class implements labels, which are views to display a string or image.
|
|
55 |
The Label will try to do its best to make its contents fit into the
|
|
56 |
view. The contents can be a String, a collection of Strings (i.e.
|
118
|
57 |
a StringCollection) or a Form/Image.
|
63
|
58 |
|
|
59 |
The contents is drawn in fgColor/bgColor, which can be changed using:
|
|
60 |
|
59
|
61 |
aLabel foregroundColor:aColor
|
|
62 |
aLabel backgroundColor:aColor
|
3
|
63 |
|
38
|
64 |
When a label is assigned a contents, it will resize itself to fit
|
|
65 |
the required size. This resizing can be suppressed by setting the
|
|
66 |
fixsize attribute to true using:
|
63
|
67 |
|
59
|
68 |
aLabel sizeFixed:true
|
3
|
69 |
|
38
|
70 |
This can be used, if resizing of the label is not wanted.
|
63
|
71 |
However, in this case you have to make certain that the size is big enough
|
118
|
72 |
to hold any changed logos later. (usually, you create the label first with
|
|
73 |
the longest string first to have it compute its size, then set the fixSize
|
|
74 |
attribute to avoid resizing later).
|
125
|
75 |
Be careful when placing self-resizing labels into panels - by default,
|
|
76 |
panels do not react on the size change - leading to ugly looking geometry.
|
|
77 |
(but you can tell the panel to watch for changes with #elementsCHangeSize:)
|
38
|
78 |
|
|
79 |
The placement of the contents within the label is controlled by
|
63
|
80 |
the adjust attribute, it can be set with:
|
|
81 |
|
59
|
82 |
aLabel adjust:how
|
3
|
83 |
|
63
|
84 |
where how is one of the symbols left, #right, #center, #centerLeft or
|
|
85 |
#centerRight (see the comment in Label>>adjust:). The default is #center.
|
3
|
86 |
|
118
|
87 |
model-less operation:
|
|
88 |
if no model is set, the labels contents is set with:
|
|
89 |
|
|
90 |
aLabel label:aStringOrImage
|
|
91 |
|
|
92 |
and stays constant unless changed by new calls to #label:.
|
|
93 |
|
|
94 |
|
|
95 |
model operation:
|
|
96 |
labels with a model, aspectMsg and labelMsg react to
|
|
97 |
changes of the aspect, and send a labelMsg-message
|
|
98 |
to the model in order to aquire a new labelString or image.
|
|
99 |
The model should send 'self changed:aspectMsg' if it thinks the label
|
|
100 |
should change and return a string or image from the labelMsg-message.
|
|
101 |
|
|
102 |
label model:aModel.
|
|
103 |
label aspect:aspectSymbol.
|
|
104 |
label labelMessage:labelSymbol.
|
|
105 |
|
|
106 |
model sends #changed:aspectSymbol
|
|
107 |
---> label will redraw its label from value of model perform:labelSymbol
|
|
108 |
|
|
109 |
Having a labelSymbol different from the aspectSymbol allows for two labels
|
|
110 |
to react on the same aspect-change, but use different messages when asking
|
|
111 |
the model for a new label contents. By default, the labelMsg is nil,
|
|
112 |
so the label does NOT update its shown contents.
|
|
113 |
The aspectMsg defaults to #value.
|
|
114 |
|
125
|
115 |
|
38
|
116 |
Instance variables:
|
3
|
117 |
|
59
|
118 |
logo <Object> the logo, can be a Form, String or Text
|
|
119 |
labelWidth <Integer> the width of the logo in device units
|
|
120 |
labelHeight <Integer> the height of the logo in device units
|
|
121 |
labelOriginX <Integer> the x-position of the logo withing the Label
|
|
122 |
labelOriginY <Integer> the y-position of the logo withing the Label
|
|
123 |
adjust <Symbol> controls how the logo is positioned within the
|
|
124 |
label. Can be one of:#left,#right,#center,
|
|
125 |
#centerLeft or #centerRight (see comment in adjust:)
|
|
126 |
hSpace <Integer> number of horizontal pixels around logo
|
|
127 |
vSpace <Integer> number of vertical pixels around logo
|
3
|
128 |
|
59
|
129 |
bgColor <Color> background color
|
|
130 |
fgColor <Color> foreground color
|
38
|
131 |
|
59
|
132 |
fixSize <Boolean> if true, a change of the logo change will not
|
|
133 |
resize the label; otherwise, its size is adjusted.
|
|
134 |
default:false.
|
50
|
135 |
|
118
|
136 |
labelMsg <Symbol> if non-nil, this is sent to the model to
|
|
137 |
aquire the labelString or labelImage.
|
|
138 |
If nil, the label stays as is
|
113
|
139 |
|
118
|
140 |
styleSheet parameters:
|
50
|
141 |
|
118
|
142 |
labelForegroundColor <Color> color to draw foreground pixels (i.e. the string)
|
|
143 |
labelBackgroundColor <Color> color to draw background pixels
|
|
144 |
labelFont <Font> font to use for textual labels
|
3
|
145 |
"
|
63
|
146 |
!
|
|
147 |
|
|
148 |
examples
|
|
149 |
"
|
|
150 |
Notice, that Buttons and others inherit from Label;
|
|
151 |
therefore, the following geometry examples apply to all subclasses too.
|
|
152 |
|
|
153 |
|
113
|
154 |
simple (default position is 0@0):
|
63
|
155 |
|top l|
|
|
156 |
|
|
157 |
top := StandardSystemView new.
|
|
158 |
top extent:(200 @ 200).
|
|
159 |
|
|
160 |
l := Label in:top.
|
|
161 |
l label:'hello world'.
|
|
162 |
|
|
163 |
top open
|
|
164 |
|
113
|
165 |
|
|
166 |
there is also a creation message which sets the label:
|
|
167 |
|top l|
|
|
168 |
|
|
169 |
top := StandardSystemView new.
|
|
170 |
top extent:(200 @ 200).
|
|
171 |
|
|
172 |
l := Label label:'hello world' in:top.
|
|
173 |
|
|
174 |
top open
|
|
175 |
|
|
176 |
|
63
|
177 |
placement:
|
|
178 |
|top l|
|
|
179 |
|
|
180 |
top := StandardSystemView new.
|
|
181 |
top extent:(200 @ 200).
|
|
182 |
|
|
183 |
l := Label in:top.
|
|
184 |
l label:'hello world'.
|
|
185 |
l origin:50@100.
|
|
186 |
|
|
187 |
top open
|
|
188 |
|
113
|
189 |
|
63
|
190 |
level:
|
|
191 |
|top l|
|
|
192 |
|
|
193 |
top := StandardSystemView new.
|
|
194 |
top extent:(200 @ 200).
|
|
195 |
|
|
196 |
l := Label in:top.
|
|
197 |
l level:5.
|
|
198 |
l label:'hello world'.
|
|
199 |
l origin:50@100.
|
|
200 |
|
|
201 |
top open
|
|
202 |
|
113
|
203 |
|
63
|
204 |
another level:
|
|
205 |
|top l|
|
|
206 |
|
|
207 |
top := StandardSystemView new.
|
|
208 |
top extent:(200 @ 200).
|
|
209 |
|
|
210 |
l := Label in:top.
|
|
211 |
l level:-1.
|
|
212 |
l label:'hello world'.
|
|
213 |
l origin:50@100.
|
|
214 |
|
|
215 |
top open
|
|
216 |
|
113
|
217 |
|
118
|
218 |
colors & font:
|
|
219 |
|top l|
|
|
220 |
|
|
221 |
top := StandardSystemView new.
|
|
222 |
top extent:(200 @ 200).
|
|
223 |
|
|
224 |
l := Label in:top.
|
|
225 |
l level:-1.
|
|
226 |
l font:(Font family:'Times' size:18).
|
|
227 |
l foregroundColor:Color yellow.
|
|
228 |
l backgroundColor:Color red.
|
|
229 |
l label:'hello world'.
|
|
230 |
l origin:50@100.
|
|
231 |
|
|
232 |
top open
|
|
233 |
|
|
234 |
|
63
|
235 |
border & colors:
|
|
236 |
|top l|
|
|
237 |
|
|
238 |
top := StandardSystemView new.
|
|
239 |
top extent:(200 @ 200).
|
|
240 |
|
|
241 |
l := Label in:top.
|
|
242 |
l level:0.
|
|
243 |
l borderWidth:1.
|
|
244 |
l borderColor:Color red.
|
|
245 |
l foregroundColor:Color green darkened.
|
|
246 |
l backgroundColor:Color green lightened.
|
|
247 |
l label:'hello world'.
|
|
248 |
l origin:50@100.
|
|
249 |
|
|
250 |
top open
|
|
251 |
|
113
|
252 |
|
63
|
253 |
adjust (resize to see):
|
|
254 |
|top l|
|
|
255 |
|
|
256 |
top := StandardSystemView new.
|
|
257 |
top extent:(200 @ 200).
|
|
258 |
|
|
259 |
l := Label in:top.
|
|
260 |
l borderWidth:1.
|
|
261 |
l label:'default - centered'.
|
118
|
262 |
l origin:0.0@40.
|
63
|
263 |
l width:1.0.
|
|
264 |
|
|
265 |
l := Label in:top.
|
|
266 |
l borderWidth:1.
|
|
267 |
l adjust:#left.
|
|
268 |
l label:'left adjust'.
|
118
|
269 |
l origin:0.0@70.
|
|
270 |
l width:1.0.
|
|
271 |
|
|
272 |
l := Label in:top.
|
|
273 |
l borderWidth:1.
|
|
274 |
l adjust:#centerLeft.
|
|
275 |
l label:'centerLeft adjust and a bit too long'.
|
63
|
276 |
l origin:0.0@100.
|
|
277 |
l width:1.0.
|
|
278 |
|
|
279 |
l := Label in:top.
|
|
280 |
l borderWidth:1.
|
|
281 |
l adjust:#right.
|
|
282 |
l label:'right adjust'.
|
|
283 |
l origin:0.0@130.
|
|
284 |
l width:1.0.
|
|
285 |
|
118
|
286 |
l := Label in:top.
|
|
287 |
l borderWidth:1.
|
|
288 |
l adjust:#centerRight.
|
|
289 |
l label:'centerRight adjust and a bit too long'.
|
|
290 |
l origin:0.0@160.
|
|
291 |
l width:1.0.
|
|
292 |
|
63
|
293 |
top open
|
|
294 |
|
113
|
295 |
|
63
|
296 |
just a reminder, that instead of doing placement manually
|
113
|
297 |
as in ...:
|
118
|
298 |
|
63
|
299 |
|top l|
|
|
300 |
|
|
301 |
top := StandardSystemView new.
|
|
302 |
top extent:(200 @ 200).
|
|
303 |
|
|
304 |
l := Label in:top.
|
|
305 |
l borderWidth:1.
|
|
306 |
l label:'default - centered'.
|
|
307 |
l origin:0.1@70.
|
|
308 |
l width:0.8.
|
|
309 |
|
|
310 |
l := Label in:top.
|
|
311 |
l borderWidth:1.
|
|
312 |
l adjust:#left.
|
|
313 |
l label:'left adjust'.
|
|
314 |
l origin:0.1@100.
|
|
315 |
l width:0.8.
|
|
316 |
|
|
317 |
l := Label in:top.
|
|
318 |
l borderWidth:1.
|
|
319 |
l adjust:#right.
|
|
320 |
l label:'right adjust'.
|
|
321 |
l origin:0.1@130.
|
|
322 |
l width:0.8.
|
|
323 |
|
|
324 |
top open
|
|
325 |
|
113
|
326 |
|
|
327 |
... it is much easier, to use a geometry handler, such as
|
63
|
328 |
a VerticalPanel. Try:
|
113
|
329 |
|
63
|
330 |
|top panel l|
|
|
331 |
|
|
332 |
top := StandardSystemView new.
|
|
333 |
top extent:(200 @ 200).
|
|
334 |
|
|
335 |
panel := VerticalPanelView
|
|
336 |
origin:0.0 @ 0.0
|
|
337 |
corner:1.0 @ 1.0
|
|
338 |
in:top.
|
|
339 |
|
|
340 |
panel horizontalLayout:#fit.
|
|
341 |
panel verticalLayout:#center.
|
|
342 |
|
|
343 |
l := Label in:panel.
|
|
344 |
l borderWidth:1.
|
|
345 |
l label:'default - centered'.
|
|
346 |
|
|
347 |
l := Label in:panel.
|
|
348 |
l borderWidth:1.
|
|
349 |
l adjust:#left.
|
|
350 |
l label:'left adjust'.
|
|
351 |
|
|
352 |
l := Label in:panel.
|
|
353 |
l borderWidth:1.
|
|
354 |
l adjust:#right.
|
|
355 |
l label:'right adjust'.
|
|
356 |
|
|
357 |
top open
|
|
358 |
|
113
|
359 |
|
63
|
360 |
labels with bitmaps or images:
|
|
361 |
|
|
362 |
|top l|
|
|
363 |
|
|
364 |
top := StandardSystemView new.
|
|
365 |
top extent:(200 @ 200).
|
|
366 |
|
|
367 |
l := Label in:top.
|
|
368 |
l level:-1.
|
118
|
369 |
l label:((Image fromFile:'bitmaps/garfield.gif') magnifyBy:0.2 @ 0.2).
|
63
|
370 |
l origin:50@100.
|
|
371 |
|
|
372 |
top open
|
113
|
373 |
|
|
374 |
|
125
|
375 |
MVC operation
|
|
376 |
model provides the label):
|
|
377 |
(have to use a plug to simulate a model which responds to
|
|
378 |
the #someAspect message):
|
113
|
379 |
|
|
380 |
|top l model|
|
|
381 |
|
|
382 |
model := Plug new.
|
|
383 |
model respondTo:#someAspect with:['models labelString'].
|
|
384 |
|
|
385 |
top := StandardSystemView new.
|
|
386 |
top extent:(200 @ 200).
|
|
387 |
|
|
388 |
l := Label in:top.
|
118
|
389 |
l model:model; labelMessage:#someAspect.
|
113
|
390 |
|
|
391 |
top open
|
118
|
392 |
...
|
|
393 |
|
|
394 |
model changed:#someAspect
|
|
395 |
...
|
113
|
396 |
|
|
397 |
|
118
|
398 |
concrete example (track a counters value):
|
125
|
399 |
(here, the default aspect #value is used both to notify the label about
|
|
400 |
changes and to aquire a new value from the model).
|
113
|
401 |
|
|
402 |
|top l model|
|
|
403 |
|
118
|
404 |
model := ValueHolder new.
|
|
405 |
model value:0.
|
|
406 |
[
|
|
407 |
1 to:20 do:[:i |
|
|
408 |
(Delay forSeconds:1) wait.
|
|
409 |
model value:i
|
|
410 |
].
|
|
411 |
top destroy
|
|
412 |
] fork.
|
113
|
413 |
|
|
414 |
top := StandardSystemView new.
|
|
415 |
top extent:(200 @ 200).
|
|
416 |
|
|
417 |
l := Label in:top.
|
125
|
418 |
l level:-1.
|
118
|
419 |
l model:model; labelMessage:#value.
|
|
420 |
|
|
421 |
top open
|
|
422 |
|
|
423 |
|
125
|
424 |
model changes aspect after a while; two labels on the same model:
|
118
|
425 |
|
|
426 |
|top l model|
|
|
427 |
|
|
428 |
model := Plug new.
|
|
429 |
model respondTo:#labelValue1 with:['models labelString1'].
|
|
430 |
model respondTo:#labelValue2 with:['models labelString2'].
|
|
431 |
|
|
432 |
top := StandardSystemView new.
|
|
433 |
top extent:(200 @ 200).
|
|
434 |
|
|
435 |
l := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
|
|
436 |
l model:model; aspect:#someAspect; labelMessage:#labelValue1.
|
|
437 |
l := Label origin:0.0@0.5 corner:1.0@1.0 in:top.
|
|
438 |
l model:model; aspect:#someAspect; labelMessage:#labelValue2.
|
113
|
439 |
|
|
440 |
top open.
|
118
|
441 |
|
113
|
442 |
(Delay forSeconds:5) wait.
|
118
|
443 |
model respondTo:#labelValue1 with:['new string1'].
|
|
444 |
model respondTo:#labelValue2 with:['new string2'].
|
|
445 |
|
113
|
446 |
model changed:#someAspect
|
118
|
447 |
|
|
448 |
|
125
|
449 |
plugged MVC operation (getBlock returns the label):
|
118
|
450 |
|
|
451 |
|top l model|
|
|
452 |
|
|
453 |
model := PluggableAdaptor new
|
|
454 |
getBlock:[:m | 'hello']
|
|
455 |
putBlock:nil
|
|
456 |
updateBlock:nil.
|
|
457 |
|
|
458 |
top := StandardSystemView new.
|
|
459 |
top extent:(200 @ 200).
|
|
460 |
|
|
461 |
l := Label origin:0.0@0.0 corner:1.0@0.5 in:top.
|
|
462 |
l model:model; labelMessage:#value.
|
|
463 |
|
|
464 |
top open.
|
125
|
465 |
|
|
466 |
|
|
467 |
use different label-selectors to access fields of a complex model:
|
|
468 |
|
|
469 |
|top panel model|
|
|
470 |
|
|
471 |
model := Plug new.
|
|
472 |
model respondTo:#field1 with:['value1'].
|
|
473 |
model respondTo:#field2 with:['value2'].
|
|
474 |
model respondTo:#field3 with:['value3'].
|
|
475 |
model respondTo:#field4 with:['value4'].
|
|
476 |
|
|
477 |
top := StandardSystemView new.
|
|
478 |
|
|
479 |
panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
|
|
480 |
panel elementsChangeSize:true.
|
|
481 |
|
|
482 |
panel addSubView:((Label on:model) labelMessage:#field1).
|
|
483 |
panel addSubView:((Label on:model) labelMessage:#field2).
|
|
484 |
panel addSubView:((Label on:model) labelMessage:#field3).
|
|
485 |
panel addSubView:((Label on:model) labelMessage:#field4).
|
|
486 |
|
|
487 |
top extent:(200 @ 200).
|
|
488 |
top open.
|
|
489 |
|
|
490 |
(Delay forSeconds:5) wait.
|
|
491 |
|
|
492 |
model respondTo:#field2 with:['new value2'].
|
|
493 |
model changed:#value
|
|
494 |
|
|
495 |
|
|
496 |
same as above, using default aspects in the label, and an adaptor
|
|
497 |
to translate aspects:
|
|
498 |
|
|
499 |
|top panel model v1|
|
|
500 |
|
|
501 |
model := Plug new.
|
|
502 |
model respondTo:#field1 with:[v1].
|
|
503 |
model respondTo:#field1: with:[:arg | v1 := arg. model changed:#field1].
|
|
504 |
model respondTo:#field2 with:['value2'].
|
|
505 |
model respondTo:#field2: with:[:arg |].
|
|
506 |
model respondTo:#field3 with:['value3'].
|
|
507 |
model respondTo:#field3: with:[:arg |].
|
|
508 |
model respondTo:#field4 with:['value4'].
|
|
509 |
model respondTo:#field4: with:[:arg |].
|
|
510 |
|
|
511 |
top := StandardSystemView new.
|
|
512 |
|
|
513 |
panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
|
|
514 |
panel elementsChangeSize:true.
|
|
515 |
|
|
516 |
panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field1)) labelMessage:#value).
|
|
517 |
panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field2)) labelMessage:#value).
|
|
518 |
panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field3)) labelMessage:#value).
|
|
519 |
panel addSubView:((Label on:((AspectAdaptor subject:model) forAspect:#field4)) labelMessage:#value).
|
|
520 |
|
|
521 |
top extent:(200 @ 200).
|
|
522 |
top open.
|
|
523 |
|
|
524 |
(Delay forSeconds:5) wait.
|
|
525 |
|
|
526 |
model field1:'new value1'.
|
|
527 |
|
|
528 |
|
|
529 |
use an adapter to access fields of a complex model:
|
|
530 |
|
|
531 |
|top l panel model|
|
|
532 |
|
|
533 |
model := #('one' 'two' 'three') asValue.
|
|
534 |
|
|
535 |
top := StandardSystemView new.
|
|
536 |
|
|
537 |
panel := VerticalPanelView origin:0.0@0.0 corner:1.0@1.0 in:top.
|
|
538 |
panel elementsChangeSize:true.
|
|
539 |
|
|
540 |
panel addSubView:((Label on:(ProtocolAdaptor
|
|
541 |
subjectChannel:model
|
|
542 |
accessPath:#(1))) labelMessage:#value).
|
|
543 |
panel addSubView:((Label on:(ProtocolAdaptor
|
|
544 |
subjectChannel:model
|
|
545 |
accessPath:#(2))) labelMessage:#value).
|
|
546 |
panel addSubView:((Label on:(ProtocolAdaptor
|
|
547 |
subjectChannel:model
|
|
548 |
accessPath:#(3))) labelMessage:#value).
|
|
549 |
|
|
550 |
top extent:(200 @ 200).
|
|
551 |
top open.
|
|
552 |
|
|
553 |
(Delay forSeconds:5) wait.
|
|
554 |
|
|
555 |
model value:#('oneone' 'twotwo' 'threethree').
|
63
|
556 |
"
|
3
|
557 |
! !
|
|
558 |
|
105
|
559 |
!Label class methodsFor:'instance creation'!
|
|
560 |
|
|
561 |
form:aForm
|
118
|
562 |
"return a new Label showing a form.
|
|
563 |
OBSOLETE: you should now use #label: for both text and bitmap labels."
|
105
|
564 |
|
|
565 |
^ (self on:Display) form:aForm
|
|
566 |
!
|
|
567 |
|
|
568 |
form:aForm in:aView
|
118
|
569 |
"return a new Label showing a form.
|
|
570 |
OBSOLETE: you should now use #label:in: for both text and bitmap labels."
|
105
|
571 |
|
|
572 |
^ (self in:aView) form:aForm
|
|
573 |
! !
|
|
574 |
|
0
|
575 |
!Label class methodsFor:'defaults'!
|
|
576 |
|
|
577 |
defaultExtent
|
3
|
578 |
"return default extent"
|
0
|
579 |
|
|
580 |
^ 16 @ 16
|
59
|
581 |
!
|
|
582 |
|
118
|
583 |
defaultFont
|
|
584 |
^ DefaultFont notNil ifTrue:[DefaultFont] ifFalse:[super defaultFont]
|
|
585 |
!
|
|
586 |
|
59
|
587 |
updateStyleCache
|
77
|
588 |
DefaultForegroundColor := StyleSheet colorAt:'labelForegroundColor'.
|
|
589 |
DefaultForegroundColor isNil ifTrue:[
|
|
590 |
DefaultForegroundColor := StyleSheet colorAt:'foregroundColor' default:Black.
|
|
591 |
].
|
60
|
592 |
DefaultBackgroundColor := StyleSheet colorAt:'labelBackgroundColor'.
|
77
|
593 |
DefaultBackgroundColor isNil ifTrue:[
|
|
594 |
DefaultForegroundColor := StyleSheet colorAt:'backgroundColor'.
|
|
595 |
].
|
60
|
596 |
DefaultFont := StyleSheet fontAt:'labelFont'.
|
|
597 |
|
|
598 |
"
|
|
599 |
self updateStyleCache
|
|
600 |
"
|
0
|
601 |
! !
|
|
602 |
|
105
|
603 |
!Label methodsFor:'event handling'!
|
|
604 |
|
|
605 |
sizeChanged:how
|
|
606 |
"sent whenever size is changed by someone else - recompute the
|
|
607 |
logos position within the View."
|
|
608 |
|
|
609 |
|prevPosition|
|
|
610 |
|
|
611 |
super sizeChanged:how.
|
|
612 |
prevPosition := labelOriginX.
|
|
613 |
self computeLabelOrigin
|
|
614 |
shown ifTrue:[
|
110
|
615 |
labelOriginX ~~ prevPosition ifTrue:[
|
|
616 |
self redraw
|
|
617 |
]
|
105
|
618 |
]
|
|
619 |
! !
|
|
620 |
|
125
|
621 |
!
|
|
622 |
|
|
623 |
!Label methodsFor:'accessing-mvc'!
|
|
624 |
|
|
625 |
model:aModel
|
|
626 |
super model:aModel.
|
|
627 |
self getLabelFromModel.
|
|
628 |
!
|
|
629 |
|
|
630 |
labelMessage
|
|
631 |
"return the symbol used to aquire the labelString/image from the model
|
|
632 |
when the aspect changes.
|
|
633 |
The default is nil, which means: leave the label unchanged."
|
|
634 |
|
|
635 |
^ labelMsg
|
|
636 |
!
|
|
637 |
|
|
638 |
labelMessage:aSymbol
|
|
639 |
"set the symbol used to aquire the labelString/image from the model.
|
|
640 |
The default is nil, which means: leave the label unchanged."
|
|
641 |
|
|
642 |
labelMsg ~~ aSymbol ifTrue:[
|
|
643 |
labelMsg := aSymbol.
|
|
644 |
self getLabelFromModel
|
|
645 |
]
|
|
646 |
!
|
|
647 |
|
|
648 |
addModelInterfaceTo:aDictionary
|
|
649 |
"see comment in View>>modelInterface"
|
|
650 |
|
|
651 |
super addModelInterfaceTo:aDictionary.
|
|
652 |
aDictionary at:#labelMessage put:labelMsg
|
|
653 |
! !
|
|
654 |
|
105
|
655 |
!Label methodsFor:'accessing'!
|
|
656 |
|
|
657 |
foregroundColor:aColor
|
|
658 |
"set the foreground color"
|
|
659 |
|
|
660 |
fgColor := aColor on:device.
|
|
661 |
self redraw
|
|
662 |
!
|
|
663 |
|
|
664 |
foregroundColor
|
|
665 |
"return the foreground color"
|
|
666 |
|
|
667 |
^ fgColor
|
|
668 |
!
|
|
669 |
|
|
670 |
backgroundColor:aColor
|
|
671 |
"set the background color"
|
|
672 |
|
|
673 |
bgColor := aColor on:device.
|
|
674 |
self redraw
|
|
675 |
!
|
|
676 |
|
|
677 |
backgroundColor
|
|
678 |
"return the background color"
|
|
679 |
|
|
680 |
^ bgColor
|
|
681 |
!
|
|
682 |
|
|
683 |
foregroundColor:fg backgroundColor:bg
|
|
684 |
"set the colors to be used for drawing"
|
|
685 |
|
|
686 |
fgColor := fg on:device.
|
|
687 |
bgColor := bg on:device.
|
|
688 |
self redraw
|
|
689 |
!
|
0
|
690 |
|
118
|
691 |
labelString:aString
|
|
692 |
"for ST-80 compatibility: same as #label:
|
|
693 |
set the label-string; adjust extent if not already realized and not fixedSize"
|
|
694 |
|
|
695 |
self label:aString
|
|
696 |
!
|
105
|
697 |
|
118
|
698 |
label:aStringOrFormOrImage
|
|
699 |
"set the labelString or image; adjust extent if not already realized and
|
|
700 |
not fixedSize"
|
|
701 |
|
|
702 |
(aStringOrFormOrImage ~~ logo) ifTrue:[
|
|
703 |
"/
|
|
704 |
"/ avoid recompute of size, if its an image with
|
|
705 |
"/ the same size
|
|
706 |
"/
|
|
707 |
aStringOrFormOrImage isImageOrForm ifTrue:[
|
|
708 |
logo notNil ifTrue:[
|
|
709 |
logo isImageOrForm ifTrue:[
|
|
710 |
logo extent = aStringOrFormOrImage extent ifTrue:[
|
|
711 |
logo := aStringOrFormOrImage.
|
|
712 |
^ self
|
|
713 |
]
|
105
|
714 |
]
|
|
715 |
]
|
118
|
716 |
] ifFalse:[
|
|
717 |
(logo = aStringOrFormOrImage) ifTrue:[
|
|
718 |
^ self
|
|
719 |
]
|
105
|
720 |
].
|
118
|
721 |
logo := aStringOrFormOrImage.
|
105
|
722 |
self newLayout
|
|
723 |
]
|
|
724 |
!
|
0
|
725 |
|
118
|
726 |
form:aForm
|
|
727 |
"set the labels form; adjust extent if not already realized.
|
|
728 |
OBSOLETE: you should now use #label: for both strings and images"
|
|
729 |
|
|
730 |
self label:aForm
|
|
731 |
!
|
|
732 |
|
|
733 |
logo:something
|
|
734 |
"set the labels form or string.
|
|
735 |
OBSOLETE: the old version used #form: for images and #label: for strings.
|
|
736 |
you should now use #label: for any."
|
|
737 |
|
|
738 |
self label:something
|
|
739 |
!
|
|
740 |
|
105
|
741 |
label
|
118
|
742 |
"return the labels string or image"
|
105
|
743 |
|
|
744 |
^ logo
|
|
745 |
!
|
|
746 |
|
|
747 |
sizeFixed:aBoolean
|
|
748 |
"set/clear the fix-size attribute (will not change size on label-change)"
|
|
749 |
|
|
750 |
fixSize := aBoolean
|
|
751 |
!
|
|
752 |
|
|
753 |
sizeFixed
|
|
754 |
"return the fix-size attribute"
|
|
755 |
|
|
756 |
^ fixSize
|
0
|
757 |
!
|
|
758 |
|
105
|
759 |
labelWidth
|
|
760 |
"return the logos width in pixels"
|
|
761 |
|
|
762 |
^ labelWidth
|
|
763 |
!
|
|
764 |
|
125
|
765 |
layout:how
|
|
766 |
"for protocol compatibility: alias for #adjust:"
|
|
767 |
|
|
768 |
self adjust:how
|
|
769 |
!
|
|
770 |
|
|
771 |
layout
|
|
772 |
"for protocol compatibility: alias for #adjust"
|
|
773 |
|
|
774 |
^ self adjust
|
|
775 |
!
|
|
776 |
|
105
|
777 |
adjust:how
|
|
778 |
"set the adjust, how which must be one of
|
0
|
779 |
|
105
|
780 |
#left -> left adjust logo
|
|
781 |
#right -> right adjust logo
|
|
782 |
#center -> center logo
|
118
|
783 |
#centerRight -> center logo; if no fit, right adjust
|
|
784 |
(use with filenames, where the interresting part is
|
|
785 |
at the right if the label is too small)
|
105
|
786 |
#centerLeft -> center logo; if it does not fit, left adjust it
|
118
|
787 |
(use with strings where the interresting part is at the
|
|
788 |
left if the label is too small)
|
105
|
789 |
"
|
118
|
790 |
|
105
|
791 |
(adjust ~~ how) ifTrue:[
|
|
792 |
adjust := how.
|
|
793 |
self newLayout
|
|
794 |
]
|
|
795 |
!
|
|
796 |
|
125
|
797 |
adjust
|
|
798 |
"return the adjust symbol"
|
|
799 |
|
|
800 |
^ adjust
|
|
801 |
!
|
|
802 |
|
105
|
803 |
font:aFont
|
118
|
804 |
"set the font - if I'm not realized and not fixedSize, adjust my size"
|
105
|
805 |
|
|
806 |
(aFont ~~ font) ifTrue:[
|
|
807 |
super font:(aFont on:device).
|
|
808 |
self newLayout
|
|
809 |
]
|
0
|
810 |
! !
|
|
811 |
|
|
812 |
!Label methodsFor:'initialization'!
|
|
813 |
|
|
814 |
initStyle
|
|
815 |
super initStyle.
|
|
816 |
|
59
|
817 |
DefaultFont notNil ifTrue:[font := DefaultFont on:device].
|
|
818 |
DefaultBackgroundColor notNil ifTrue:[
|
|
819 |
bgColor := DefaultBackgroundColor on:device
|
38
|
820 |
] ifFalse:[
|
59
|
821 |
bgColor := viewBackground on:device.
|
|
822 |
].
|
|
823 |
DefaultForegroundColor notNil ifTrue:[
|
|
824 |
fgColor := DefaultForegroundColor on:device
|
|
825 |
] ifFalse:[
|
|
826 |
fgColor := Black on:device.
|
38
|
827 |
]
|
0
|
828 |
!
|
|
829 |
|
|
830 |
realize
|
|
831 |
super realize.
|
118
|
832 |
"/ fgColor := fgColor on:device.
|
|
833 |
"/ bgColor := bgColor on:device.
|
0
|
834 |
!
|
|
835 |
|
105
|
836 |
initialize
|
|
837 |
super initialize.
|
|
838 |
|
|
839 |
font := font on:device.
|
|
840 |
self height:(font height + font descent).
|
|
841 |
adjust := #center.
|
118
|
842 |
labelOriginX := labelOriginY := 0.
|
|
843 |
labelWidth := labelHeight := 0.
|
105
|
844 |
logo := nil.
|
|
845 |
fixSize := false.
|
|
846 |
hSpace := (self horizontalPixelPerMillimeter:0.5) rounded.
|
|
847 |
vSpace := (self verticalPixelPerMillimeter:0.5) rounded
|
|
848 |
!
|
|
849 |
|
0
|
850 |
recreate
|
|
851 |
"after snapin, labels dimensions may have changed due to
|
|
852 |
different font parameters"
|
|
853 |
|
|
854 |
super recreate.
|
3
|
855 |
font := font on:device.
|
0
|
856 |
self computeLabelSize.
|
|
857 |
self computeLabelOrigin
|
|
858 |
! !
|
|
859 |
|
|
860 |
!Label methodsFor:'redrawing'!
|
|
861 |
|
77
|
862 |
clearInsideWith:bg
|
|
863 |
|cutOff mustClear|
|
0
|
864 |
|
|
865 |
cutOff := margin * 2.
|
|
866 |
|
3
|
867 |
mustClear := true.
|
|
868 |
|
87
|
869 |
(logo notNil and:[logo isImageOrForm]) ifTrue:[
|
59
|
870 |
(labelOriginX == 0 and:[labelOriginY == 0]) ifTrue:[
|
|
871 |
logo width >= (width - cutOff) ifTrue:[
|
|
872 |
logo height >= (height - cutOff) ifTrue:[
|
|
873 |
"no need to clear before - avoid flicker"
|
|
874 |
mustClear := false
|
|
875 |
]
|
|
876 |
]
|
|
877 |
].
|
3
|
878 |
].
|
|
879 |
|
|
880 |
mustClear ifTrue:[
|
59
|
881 |
self paint:bg.
|
|
882 |
self fillRectangleX:margin y:margin
|
|
883 |
width:(width - cutOff)
|
|
884 |
height:(height - cutOff).
|
3
|
885 |
].
|
77
|
886 |
!
|
|
887 |
|
|
888 |
drawWith:fg and:bg
|
|
889 |
"redraw my label with fg/bg - this generic method is also used by subclasses
|
|
890 |
(especially Button) to redraw the logo in different colors."
|
|
891 |
|
|
892 |
|x y|
|
|
893 |
|
|
894 |
self clearInsideWith:bg.
|
0
|
895 |
|
|
896 |
logo notNil ifTrue:[
|
59
|
897 |
self paint:fg on:bg.
|
87
|
898 |
logo isImageOrForm ifTrue:[
|
|
899 |
logo := logo on:device.
|
59
|
900 |
"/ self background:bg.
|
|
901 |
self displayOpaqueForm:logo x:labelOriginX y:labelOriginY
|
|
902 |
] ifFalse:[
|
|
903 |
x := labelOriginX + hSpace.
|
|
904 |
y := labelOriginY + (font ascent) + vSpace.
|
0
|
905 |
|
87
|
906 |
logo isString ifTrue:[
|
59
|
907 |
self displayString:logo x:x y:y
|
|
908 |
] ifFalse:[
|
|
909 |
logo do:[ :line |
|
|
910 |
self displayString:(line printString) x:x y:y.
|
|
911 |
y := y + (font height)
|
|
912 |
]
|
|
913 |
]
|
|
914 |
]
|
0
|
915 |
]
|
|
916 |
!
|
|
917 |
|
|
918 |
redraw
|
|
919 |
"redraw my label"
|
|
920 |
|
|
921 |
shown ifTrue:[
|
59
|
922 |
self drawWith:fgColor and:bgColor
|
0
|
923 |
]
|
|
924 |
! !
|
105
|
925 |
|
|
926 |
!Label methodsFor:'private'!
|
|
927 |
|
110
|
928 |
getLabelFromModel
|
118
|
929 |
"ask my model for the label to show.
|
|
930 |
Here, we use labelMsg.
|
|
931 |
This allows multiple labels to react on the same aspect,
|
|
932 |
but show different labels when changed (also, constant labels
|
|
933 |
which have a nil labelMsg will not try to aquire a labelString)."
|
110
|
934 |
|
125
|
935 |
(model notNil
|
|
936 |
and:[labelMsg notNil]) ifTrue:[
|
|
937 |
self label:(model perform:labelMsg) printString.
|
110
|
938 |
].
|
|
939 |
!
|
|
940 |
|
105
|
941 |
newLayout
|
|
942 |
"recompute position/size after a change
|
|
943 |
- helper for form:/font: etc."
|
|
944 |
|
|
945 |
self computeLabelSize.
|
|
946 |
fixSize ifFalse:[
|
|
947 |
self resize
|
|
948 |
] ifTrue:[
|
|
949 |
self computeLabelOrigin
|
|
950 |
].
|
|
951 |
shown ifTrue:[
|
|
952 |
self redraw
|
|
953 |
]
|
|
954 |
!
|
|
955 |
|
|
956 |
computeLabelOrigin
|
|
957 |
"(re)compute the origin of the label whenever label or font changes"
|
|
958 |
|
|
959 |
|x y|
|
|
960 |
|
|
961 |
labelHeight isNil ifTrue:[^ self].
|
|
962 |
|
|
963 |
"if it does not fit, should we make the origin visible,
|
|
964 |
or the center (for text, the center seems better. For images,
|
|
965 |
I dont really know ehich is better ...
|
|
966 |
The commented code below makes the origin visible
|
|
967 |
"
|
|
968 |
"/ (labelHeight < height) ifTrue:[
|
|
969 |
"/ y := (height - labelHeight) // 2
|
|
970 |
"/ ] ifFalse:[
|
|
971 |
"/ y := 0
|
|
972 |
"/ ].
|
|
973 |
|
|
974 |
"always center vertically"
|
|
975 |
y := (height - labelHeight) // 2.
|
|
976 |
|
|
977 |
labelOriginY := y.
|
|
978 |
|
|
979 |
(((adjust == #center)
|
|
980 |
or:[adjust == #centerRight])
|
|
981 |
or:[adjust == #centerLeft]) ifTrue:[
|
|
982 |
" center text/form in button "
|
|
983 |
x := (width - labelWidth) // 2.
|
|
984 |
(width < labelWidth) ifTrue:[
|
|
985 |
"no fit"
|
|
986 |
(adjust == #centerLeft) ifTrue:[
|
|
987 |
x := margin
|
|
988 |
] ifFalse:[
|
|
989 |
(adjust == #centerRight) ifTrue:[
|
|
990 |
x := width - labelWidth - margin
|
|
991 |
]
|
|
992 |
]
|
|
993 |
]
|
|
994 |
] ifFalse:[
|
|
995 |
(adjust == #left) ifTrue:[
|
|
996 |
x := margin
|
|
997 |
] ifFalse:[
|
|
998 |
x := width - labelWidth - margin
|
|
999 |
]
|
|
1000 |
].
|
|
1001 |
labelOriginX := x
|
|
1002 |
!
|
|
1003 |
|
|
1004 |
computeLabelSize
|
|
1005 |
"compute the extent needed to hold the label; aForm or aString"
|
|
1006 |
|
|
1007 |
|numberOfLines textHeight textWidth|
|
|
1008 |
|
|
1009 |
logo isNil ifTrue:[^ self].
|
|
1010 |
|
|
1011 |
logo isImageOrForm ifTrue:[
|
|
1012 |
labelWidth := logo width.
|
|
1013 |
labelHeight := logo height.
|
|
1014 |
^ self
|
|
1015 |
].
|
|
1016 |
|
|
1017 |
"must be a String or collection of strings"
|
|
1018 |
logo isString ifTrue:[
|
|
1019 |
numberOfLines := 1 + (logo occurrencesOf:(Character cr)).
|
|
1020 |
(numberOfLines ~~ 1) ifTrue:[
|
|
1021 |
logo := logo asStringCollection
|
|
1022 |
]
|
|
1023 |
] ifFalse:[
|
|
1024 |
numberOfLines := logo size.
|
|
1025 |
(numberOfLines == 1) ifTrue:[
|
|
1026 |
logo := logo asString
|
|
1027 |
]
|
|
1028 |
].
|
|
1029 |
|
|
1030 |
textHeight := font height * numberOfLines + font descent.
|
|
1031 |
textWidth := font widthOf:logo.
|
|
1032 |
labelWidth := textWidth + (hSpace * 2) .
|
|
1033 |
labelHeight := textHeight + (vSpace * 2)
|
110
|
1034 |
! !
|
|
1035 |
|
|
1036 |
!Label methodsFor:'resizing'!
|
|
1037 |
|
|
1038 |
forceResize
|
118
|
1039 |
"resize myself to make text fit into myself. Here, this is done even if
|
|
1040 |
fixSize is set."
|
110
|
1041 |
|
|
1042 |
logo notNil ifTrue:[
|
|
1043 |
self extent:self preferedExtent.
|
|
1044 |
self computeLabelOrigin
|
|
1045 |
]
|
105
|
1046 |
!
|
|
1047 |
|
|
1048 |
resize
|
|
1049 |
"resize myself to make text fit into myself.
|
118
|
1050 |
but only do so, if I am not fixedSize and I have NOT been
|
|
1051 |
given a relative extent or an extend computation block."
|
105
|
1052 |
|
110
|
1053 |
(logo notNil
|
|
1054 |
and:[fixSize not
|
|
1055 |
and:[relativeExtent isNil
|
|
1056 |
and:[extentRule isNil
|
|
1057 |
and:[relativeCorner isNil
|
|
1058 |
and:[cornerRule isNil]]]]]) ifTrue:[
|
|
1059 |
self forceResize
|
|
1060 |
] ifFalse:[
|
105
|
1061 |
self computeLabelOrigin
|
|
1062 |
]
|
|
1063 |
! !
|
|
1064 |
|
|
1065 |
!Label methodsFor:'change & update'!
|
|
1066 |
|
118
|
1067 |
update:something with:aParameter from:changedObject
|
105
|
1068 |
"the MVC way of changing the label ..."
|
|
1069 |
|
118
|
1070 |
changedObject == model ifTrue:[
|
|
1071 |
something == aspectMsg ifTrue:[
|
125
|
1072 |
self getLabelFromModel.
|
118
|
1073 |
^ self.
|
|
1074 |
]
|
105
|
1075 |
].
|
125
|
1076 |
^ super update:something with:aParameter from:changedObject
|
105
|
1077 |
! !
|
|
1078 |
|
|
1079 |
!Label methodsFor:'queries'!
|
|
1080 |
|
|
1081 |
preferedExtent
|
|
1082 |
"return my prefered extent - this is the minimum size I would like to have"
|
|
1083 |
|
|
1084 |
|extra|
|
|
1085 |
|
|
1086 |
logo notNil ifTrue:[
|
|
1087 |
extra := margin * 2.
|
|
1088 |
^ (labelWidth + extra) @ (labelHeight + extra)
|
|
1089 |
].
|
|
1090 |
|
|
1091 |
^ super preferedExtent
|
|
1092 |
! !
|