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 |
View subclass:#Label
|
|
14 |
instanceVariableNames:'logo
|
59
|
15 |
labelWidth labelHeight
|
|
16 |
labelOriginX labelOriginY
|
|
17 |
adjust hSpace vSpace
|
|
18 |
bgColor fgColor fixSize'
|
|
19 |
classVariableNames:'DefaultFont
|
|
20 |
DefaultForegroundColor DefaultBackgroundColor'
|
0
|
21 |
poolDictionaries:''
|
|
22 |
category:'Views-Layout'
|
|
23 |
!
|
|
24 |
|
|
25 |
Label comment:'
|
5
|
26 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
27 |
All Rights Reserved
|
0
|
28 |
|
60
|
29 |
$Header: /cvs/stx/stx/libwidg/Label.st,v 1.10 1994-10-28 03:25:05 claus Exp $
|
0
|
30 |
'!
|
|
31 |
|
3
|
32 |
!Label class methodsFor:'documentation'!
|
|
33 |
|
38
|
34 |
copyright
|
|
35 |
"
|
|
36 |
COPYRIGHT (c) 1989 by Claus Gittinger
|
59
|
37 |
All Rights Reserved
|
38
|
38 |
|
|
39 |
This software is furnished under a license and may be used
|
|
40 |
only in accordance with the terms of that license and with the
|
|
41 |
inclusion of the above copyright notice. This software may not
|
|
42 |
be provided or otherwise made available to, or used by, any
|
|
43 |
other person. No title to or ownership of the software is
|
|
44 |
hereby transferred.
|
|
45 |
"
|
|
46 |
!
|
|
47 |
|
|
48 |
version
|
|
49 |
"
|
60
|
50 |
$Header: /cvs/stx/stx/libwidg/Label.st,v 1.10 1994-10-28 03:25:05 claus Exp $
|
38
|
51 |
"
|
|
52 |
!
|
|
53 |
|
3
|
54 |
documentation
|
|
55 |
"
|
38
|
56 |
This class implements labels, which are views to display a string or image.
|
|
57 |
The Label will try to do its best to make its contents fit into the
|
|
58 |
view. The contents can be a String, a collection of Strings (i.e.
|
|
59 |
Text) or a Form/Image. The contents is drawn in fgColor/bgColor,
|
|
60 |
which can be changed using:
|
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:
|
59
|
67 |
aLabel sizeFixed:true
|
3
|
68 |
|
38
|
69 |
This can be used, if resizing of the label is not wanted.
|
|
70 |
|
|
71 |
The placement of the contents within the label is controlled by
|
|
72 |
the adjust attribute, it can be set with
|
59
|
73 |
aLabel adjust:how
|
38
|
74 |
, where how is one of the symbols left, #right, #center, #centerLeft or
|
|
75 |
#centerRight (see the comment in Label>>adjust:).
|
3
|
76 |
|
38
|
77 |
example:
|
59
|
78 |
l := Label in:aView.
|
|
79 |
l label:'hello world'.
|
3
|
80 |
|
38
|
81 |
Instance variables:
|
3
|
82 |
|
59
|
83 |
logo <Object> the logo, can be a Form, String or Text
|
|
84 |
labelWidth <Integer> the width of the logo in device units
|
|
85 |
labelHeight <Integer> the height of the logo in device units
|
|
86 |
labelOriginX <Integer> the x-position of the logo withing the Label
|
|
87 |
labelOriginY <Integer> the y-position of the logo withing the Label
|
|
88 |
adjust <Symbol> controls how the logo is positioned within the
|
|
89 |
label. Can be one of:#left,#right,#center,
|
|
90 |
#centerLeft or #centerRight (see comment in adjust:)
|
|
91 |
hSpace <Integer> number of horizontal pixels around logo
|
|
92 |
vSpace <Integer> number of vertical pixels around logo
|
3
|
93 |
|
59
|
94 |
bgColor <Color> background color
|
|
95 |
fgColor <Color> foreground color
|
38
|
96 |
|
59
|
97 |
fixSize <Boolean> if true, a change of the logo change will not
|
|
98 |
resize the label; otherwise, its size is adjusted.
|
|
99 |
default:false.
|
50
|
100 |
|
|
101 |
Model-View behavior:
|
59
|
102 |
label model:aModel.
|
|
103 |
label aspect:aspectSymbol.
|
50
|
104 |
|
59
|
105 |
model sends #changed:aspectSymbol
|
|
106 |
---> label will redraw its label from value of model>>aspectSymbol
|
3
|
107 |
"
|
|
108 |
! !
|
|
109 |
|
0
|
110 |
!Label class methodsFor:'defaults'!
|
|
111 |
|
|
112 |
defaultExtent
|
3
|
113 |
"return default extent"
|
0
|
114 |
|
|
115 |
^ 16 @ 16
|
59
|
116 |
!
|
|
117 |
|
|
118 |
updateStyleCache
|
60
|
119 |
DefaultForegroundColor := StyleSheet colorAt:'labelForegroundColor' default:Black.
|
|
120 |
DefaultBackgroundColor := StyleSheet colorAt:'labelBackgroundColor'.
|
|
121 |
DefaultFont := StyleSheet fontAt:'labelFont'.
|
|
122 |
|
|
123 |
"
|
|
124 |
self updateStyleCache
|
|
125 |
"
|
0
|
126 |
! !
|
|
127 |
|
|
128 |
!Label class methodsFor:'instance creation'!
|
|
129 |
|
|
130 |
form:aForm
|
3
|
131 |
"return a new Label showing a form"
|
0
|
132 |
|
|
133 |
^ (self on:Display) form:aForm
|
|
134 |
!
|
|
135 |
|
|
136 |
form:aForm in:aView
|
3
|
137 |
"return a new Label showing a form"
|
0
|
138 |
|
|
139 |
^ (self in:aView) form:aForm
|
|
140 |
! !
|
|
141 |
|
|
142 |
!Label methodsFor:'initialization'!
|
|
143 |
|
|
144 |
initialize
|
|
145 |
super initialize.
|
|
146 |
|
|
147 |
font := font on:device.
|
|
148 |
self height:(font height + font descent).
|
|
149 |
adjust := #center.
|
|
150 |
labelOriginX := 0.
|
|
151 |
labelOriginY := 0.
|
|
152 |
labelWidth := 0.
|
|
153 |
labelHeight := 0.
|
|
154 |
logo := nil.
|
|
155 |
fixSize := false.
|
|
156 |
hSpace := (self horizontalPixelPerMillimeter:0.5) rounded.
|
|
157 |
vSpace := (self verticalPixelPerMillimeter:0.5) rounded
|
|
158 |
!
|
|
159 |
|
|
160 |
initStyle
|
|
161 |
super initStyle.
|
|
162 |
|
59
|
163 |
DefaultFont notNil ifTrue:[font := DefaultFont on:device].
|
|
164 |
DefaultBackgroundColor notNil ifTrue:[
|
|
165 |
bgColor := DefaultBackgroundColor on:device
|
38
|
166 |
] ifFalse:[
|
59
|
167 |
bgColor := viewBackground on:device.
|
|
168 |
].
|
|
169 |
DefaultForegroundColor notNil ifTrue:[
|
|
170 |
fgColor := DefaultForegroundColor on:device
|
|
171 |
] ifFalse:[
|
|
172 |
fgColor := Black on:device.
|
38
|
173 |
]
|
0
|
174 |
!
|
|
175 |
|
|
176 |
realize
|
|
177 |
super realize.
|
|
178 |
fgColor := fgColor on:device.
|
|
179 |
bgColor := bgColor on:device.
|
|
180 |
!
|
|
181 |
|
|
182 |
recreate
|
|
183 |
"after snapin, labels dimensions may have changed due to
|
|
184 |
different font parameters"
|
|
185 |
|
|
186 |
super recreate.
|
3
|
187 |
font := font on:device.
|
0
|
188 |
self computeLabelSize.
|
|
189 |
self computeLabelOrigin
|
|
190 |
! !
|
|
191 |
|
|
192 |
!Label methodsFor:'accessing'!
|
|
193 |
|
|
194 |
foregroundColor
|
|
195 |
"return the foreground color"
|
|
196 |
|
|
197 |
^ fgColor
|
|
198 |
!
|
|
199 |
|
|
200 |
foregroundColor:aColor
|
|
201 |
"set the foreground color"
|
|
202 |
|
|
203 |
fgColor := aColor on:device.
|
|
204 |
self redraw
|
|
205 |
!
|
|
206 |
|
|
207 |
backgroundColor
|
|
208 |
"return the background color"
|
|
209 |
|
|
210 |
^ bgColor
|
|
211 |
!
|
|
212 |
|
|
213 |
backgroundColor:aColor
|
|
214 |
"set the background color"
|
|
215 |
|
|
216 |
bgColor := aColor on:device.
|
|
217 |
self redraw
|
|
218 |
!
|
|
219 |
|
|
220 |
foregroundColor:fg backgroundColor:bg
|
|
221 |
"set the colors to be used for drawing"
|
|
222 |
|
|
223 |
fgColor := fg on:device.
|
|
224 |
bgColor := bg on:device.
|
|
225 |
self redraw
|
|
226 |
!
|
|
227 |
|
|
228 |
sizeFixed:aBoolean
|
|
229 |
"set/clear the fix-size attribute (will not change size on label-change)"
|
|
230 |
|
|
231 |
fixSize := aBoolean
|
|
232 |
!
|
|
233 |
|
38
|
234 |
sizeFixed
|
|
235 |
"return the fix-size attribute"
|
|
236 |
|
|
237 |
^ fixSize
|
|
238 |
!
|
|
239 |
|
0
|
240 |
label:aString
|
|
241 |
"set the label-string; adjust extent if not already realized"
|
|
242 |
|
|
243 |
(logo = aString) ifFalse:[
|
59
|
244 |
logo := aString.
|
|
245 |
self newLayout
|
0
|
246 |
]
|
|
247 |
!
|
|
248 |
|
|
249 |
label
|
3
|
250 |
"return the labels string"
|
0
|
251 |
|
|
252 |
^ logo
|
|
253 |
!
|
|
254 |
|
|
255 |
labelWidth
|
3
|
256 |
"return the logos width in pixels"
|
0
|
257 |
|
|
258 |
^ labelWidth
|
|
259 |
!
|
|
260 |
|
|
261 |
font:aFont
|
|
262 |
"set the font - if I'm not realized, adjust my size"
|
|
263 |
|
|
264 |
(aFont ~~ font) ifTrue:[
|
59
|
265 |
super font:(aFont on:device).
|
|
266 |
self newLayout
|
0
|
267 |
]
|
|
268 |
!
|
|
269 |
|
|
270 |
adjust:how
|
|
271 |
"set the adjust, how which must be one of
|
|
272 |
|
|
273 |
#left -> left adjust logo
|
|
274 |
#right -> right adjust logo
|
|
275 |
#center -> center logo
|
|
276 |
#centerLeft -> center logo; if it does not fit, left adjust it
|
|
277 |
#centerRight -> center logo; if no fit, right adjust
|
|
278 |
"
|
|
279 |
(adjust ~~ how) ifTrue:[
|
59
|
280 |
adjust := how.
|
|
281 |
self newLayout
|
0
|
282 |
]
|
|
283 |
!
|
|
284 |
|
|
285 |
form:aForm
|
|
286 |
"set the labels form; adjust extent if not already realized"
|
|
287 |
|
|
288 |
aForm isNil ifFalse:[
|
59
|
289 |
aForm ~~ logo ifTrue:[
|
|
290 |
logo notNil ifTrue:[
|
|
291 |
logo extent = aForm extent ifTrue:[
|
|
292 |
logo := aForm.
|
|
293 |
^ self
|
|
294 |
]
|
|
295 |
].
|
|
296 |
logo := aForm.
|
|
297 |
self newLayout
|
|
298 |
]
|
|
299 |
]
|
|
300 |
!
|
|
301 |
|
|
302 |
logo:something
|
|
303 |
"set the labels form or string"
|
|
304 |
|
|
305 |
(logo isKindOf:Collection) ifFalse:[
|
|
306 |
self form:something
|
|
307 |
] ifTrue:[
|
|
308 |
self label:something
|
0
|
309 |
]
|
|
310 |
! !
|
|
311 |
|
50
|
312 |
!Label methodsFor:'change & update'!
|
|
313 |
|
|
314 |
update:something
|
|
315 |
"the MVC way of changing the label ..."
|
|
316 |
|
|
317 |
(something == aspectSymbol) ifTrue:[
|
59
|
318 |
model notNil ifTrue:[
|
|
319 |
self label:(model perform: aspectSymbol) printString.
|
|
320 |
].
|
|
321 |
^ self.
|
50
|
322 |
].
|
|
323 |
super update:something
|
|
324 |
! !
|
|
325 |
|
38
|
326 |
!Label methodsFor:'queries'!
|
|
327 |
|
59
|
328 |
preferedExtent
|
|
329 |
"return my prefered extent - this is the minimum size I would like to have"
|
38
|
330 |
|
|
331 |
|extra|
|
|
332 |
|
|
333 |
logo notNil ifTrue:[
|
59
|
334 |
extra := margin * 2.
|
|
335 |
^ (labelWidth + extra) @ (labelHeight + extra)
|
38
|
336 |
].
|
|
337 |
|
59
|
338 |
^ super preferedExtent
|
38
|
339 |
! !
|
|
340 |
|
0
|
341 |
!Label methodsFor:'private'!
|
|
342 |
|
|
343 |
newLayout
|
|
344 |
"recompute position/size after a change
|
|
345 |
- helper for form:/font: etc."
|
|
346 |
|
|
347 |
self computeLabelSize.
|
|
348 |
fixSize ifFalse:[
|
59
|
349 |
self resize
|
0
|
350 |
] ifTrue:[
|
59
|
351 |
self computeLabelOrigin
|
0
|
352 |
].
|
3
|
353 |
shown ifTrue:[
|
59
|
354 |
self redraw
|
3
|
355 |
]
|
0
|
356 |
!
|
|
357 |
|
|
358 |
resize
|
|
359 |
"resize myself to make text fit into myself"
|
|
360 |
|
|
361 |
|extra|
|
|
362 |
|
3
|
363 |
logo notNil ifTrue:[
|
59
|
364 |
(relativeExtent isNil and:[extentRule isNil]) ifTrue:[
|
|
365 |
extra := margin * 2.
|
|
366 |
self extent:(labelWidth + extra) @ (labelHeight + extra)
|
|
367 |
].
|
|
368 |
self computeLabelOrigin
|
0
|
369 |
]
|
|
370 |
!
|
|
371 |
|
|
372 |
computeLabelSize
|
|
373 |
"compute the extent needed to hold the label; aForm or aString"
|
|
374 |
|
|
375 |
|numberOfLines textHeight textWidth|
|
|
376 |
|
3
|
377 |
(logo isKindOf:Collection) ifFalse:[
|
59
|
378 |
logo notNil ifTrue:[
|
|
379 |
labelWidth := logo width.
|
|
380 |
labelHeight := logo height
|
|
381 |
].
|
|
382 |
^ self
|
0
|
383 |
].
|
|
384 |
|
|
385 |
"must be a String or Text"
|
38
|
386 |
(logo isString) ifTrue:[
|
59
|
387 |
numberOfLines := 1 + (logo occurrencesOf:(Character cr)).
|
|
388 |
(numberOfLines ~~ 1) ifTrue:[
|
|
389 |
logo := logo asText
|
|
390 |
]
|
0
|
391 |
] ifFalse:[
|
59
|
392 |
numberOfLines := logo size.
|
|
393 |
(numberOfLines == 1) ifTrue:[
|
|
394 |
logo := logo asString
|
|
395 |
]
|
0
|
396 |
].
|
|
397 |
|
|
398 |
textHeight := font height * numberOfLines + font descent.
|
|
399 |
textWidth := font widthOf:logo.
|
25
|
400 |
labelWidth := textWidth + (hSpace * 2) .
|
|
401 |
labelHeight := textHeight + (vSpace * 2)
|
0
|
402 |
!
|
|
403 |
|
|
404 |
computeLabelOrigin
|
|
405 |
"(re)compute the origin of the label whenever label or font changes"
|
|
406 |
|
|
407 |
|x y|
|
|
408 |
|
|
409 |
labelHeight isNil ifTrue:[^ self].
|
|
410 |
|
|
411 |
" always center vertically "
|
|
412 |
(labelHeight < height) ifTrue:[
|
59
|
413 |
y := (height - labelHeight) // 2
|
0
|
414 |
] ifFalse:[
|
59
|
415 |
y := 0
|
0
|
416 |
].
|
|
417 |
labelOriginY := y.
|
|
418 |
|
|
419 |
(((adjust == #center)
|
|
420 |
or:[adjust == #centerRight])
|
|
421 |
or:[adjust == #centerLeft]) ifTrue:[
|
59
|
422 |
" center text/form in button "
|
|
423 |
x := (width - labelWidth) // 2.
|
|
424 |
(width < labelWidth) ifTrue:[
|
|
425 |
"no fit"
|
|
426 |
(adjust == #centerLeft) ifTrue:[
|
|
427 |
x := margin
|
|
428 |
] ifFalse:[
|
|
429 |
(adjust == #centerRight) ifTrue:[
|
|
430 |
x := width - labelWidth - margin
|
|
431 |
]
|
|
432 |
]
|
|
433 |
]
|
0
|
434 |
] ifFalse:[
|
59
|
435 |
(adjust == #left) ifTrue:[
|
|
436 |
x := margin
|
|
437 |
] ifFalse:[
|
|
438 |
x := width - labelWidth - margin
|
|
439 |
]
|
0
|
440 |
].
|
|
441 |
labelOriginX := x
|
|
442 |
! !
|
|
443 |
|
|
444 |
!Label methodsFor:'events'!
|
|
445 |
|
|
446 |
sizeChanged:how
|
3
|
447 |
"sent whenever size is changed by someone else - recompute the
|
|
448 |
logos position within the View."
|
0
|
449 |
|
|
450 |
self computeLabelOrigin
|
|
451 |
! !
|
|
452 |
|
|
453 |
!Label methodsFor:'redrawing'!
|
|
454 |
|
|
455 |
drawWith:fg and:bg
|
3
|
456 |
"redraw my label with fg/bg - this generic method is also used by subclasses
|
|
457 |
(especially Button) to redraw the logo in different colors."
|
0
|
458 |
|
3
|
459 |
|x y cutOff mustClear|
|
0
|
460 |
|
|
461 |
cutOff := margin * 2.
|
|
462 |
|
3
|
463 |
mustClear := true.
|
|
464 |
|
|
465 |
(logo notNil and:[(logo isKindOf:Form) or:[logo isKindOf:Image]]) ifTrue:[
|
59
|
466 |
(labelOriginX == 0 and:[labelOriginY == 0]) ifTrue:[
|
|
467 |
logo width >= (width - cutOff) ifTrue:[
|
|
468 |
logo height >= (height - cutOff) ifTrue:[
|
|
469 |
"no need to clear before - avoid flicker"
|
|
470 |
mustClear := false
|
|
471 |
]
|
|
472 |
]
|
|
473 |
].
|
3
|
474 |
].
|
|
475 |
|
|
476 |
mustClear ifTrue:[
|
59
|
477 |
self paint:bg.
|
|
478 |
self fillRectangleX:margin y:margin
|
|
479 |
width:(width - cutOff)
|
|
480 |
height:(height - cutOff).
|
3
|
481 |
].
|
0
|
482 |
|
|
483 |
logo notNil ifTrue:[
|
59
|
484 |
self paint:fg on:bg.
|
|
485 |
(logo isKindOf:Image) ifTrue:[
|
|
486 |
logo := logo on:device
|
|
487 |
].
|
|
488 |
((logo isKindOf:Form) or:[logo isKindOf:Image]) ifTrue:[
|
|
489 |
"/ self background:bg.
|
|
490 |
self displayOpaqueForm:logo x:labelOriginX y:labelOriginY
|
|
491 |
] ifFalse:[
|
|
492 |
x := labelOriginX + hSpace.
|
|
493 |
y := labelOriginY + (font ascent) + vSpace.
|
0
|
494 |
|
59
|
495 |
(logo isString) ifTrue:[
|
|
496 |
self displayString:logo x:x y:y
|
|
497 |
] ifFalse:[
|
|
498 |
logo do:[ :line |
|
|
499 |
self displayString:(line printString) x:x y:y.
|
|
500 |
y := y + (font height)
|
|
501 |
]
|
|
502 |
]
|
|
503 |
]
|
0
|
504 |
]
|
|
505 |
!
|
|
506 |
|
|
507 |
redraw
|
|
508 |
"redraw my label"
|
|
509 |
|
|
510 |
shown ifTrue:[
|
59
|
511 |
self drawWith:fgColor and:bgColor
|
0
|
512 |
]
|
|
513 |
! !
|