0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
24
|
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 |
|
49
|
13 |
SimpleView subclass:#Ruler
|
33
|
14 |
instanceVariableNames:'fgColor metric paperWidth paperHeight scale showUnit'
|
24
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
26
|
17 |
category:'Views-Misc'
|
0
|
18 |
!
|
|
19 |
|
|
20 |
Ruler comment:'
|
4
|
21 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
24
|
22 |
All Rights Reserved
|
0
|
23 |
|
62
|
24 |
$Header: /cvs/stx/stx/libwidg2/Ruler.st,v 1.16 1995-06-06 04:17:42 claus Exp $
|
33
|
25 |
'!
|
|
26 |
|
|
27 |
!Ruler class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1991 by Claus Gittinger
|
|
32 |
All Rights Reserved
|
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
62
|
45 |
$Header: /cvs/stx/stx/libwidg2/Ruler.st,v 1.16 1995-06-06 04:17:42 claus Exp $
|
33
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
a shows some unit scale; See example use in DrawTool.
|
36
|
52 |
|
33
|
53 |
instance variables:
|
|
54 |
|
36
|
55 |
fgColor <Color> color to draw text and marks with
|
|
56 |
metric <Symbol> inch or mm
|
|
57 |
paperWidth <Number> width of paper
|
|
58 |
paperHeight <Number> height of paper
|
|
59 |
scale <Number> scale factor for zoom
|
33
|
60 |
showUnit <Boolean> if true, a unit string is displayed
|
|
61 |
"
|
|
62 |
!
|
|
63 |
|
|
64 |
examples
|
|
65 |
"
|
|
66 |
|top ruler|
|
|
67 |
|
|
68 |
top := StandardSystemView new.
|
|
69 |
ruler := Ruler origin:0.0@0.0 corner:1.0@30 in:top.
|
|
70 |
top open
|
|
71 |
|
|
72 |
|
|
73 |
defining paperWidth:
|
0
|
74 |
|
33
|
75 |
|top ruler|
|
|
76 |
|
|
77 |
top := StandardSystemView new.
|
|
78 |
ruler := Ruler origin:0.0@0.0 corner:1.0@30 in:top.
|
|
79 |
ruler paperWidthInch:5.
|
|
80 |
top open
|
|
81 |
|
|
82 |
|
|
83 |
hide unit string:
|
|
84 |
|
|
85 |
|top ruler|
|
|
86 |
|
|
87 |
top := StandardSystemView new.
|
|
88 |
ruler := Ruler origin:0.0@0.0 corner:1.0@30 in:top.
|
|
89 |
ruler showUnit:false.
|
|
90 |
top open
|
|
91 |
|
|
92 |
|
|
93 |
both horizontal and vertical rulers (as in DrawTool):
|
|
94 |
|
|
95 |
|top hRuler vRuler|
|
0
|
96 |
|
33
|
97 |
top := StandardSystemView new.
|
|
98 |
hRuler := Ruler origin:30@0.0 corner:1.0@30 in:top.
|
|
99 |
vRuler := VerticalRuler origin:0.0@30 corner:30@1.0 in:top.
|
|
100 |
vRuler showUnit:false.
|
|
101 |
top open
|
|
102 |
|
|
103 |
|
|
104 |
with some 3D effects:
|
|
105 |
|
|
106 |
|top hRuler vRuler|
|
|
107 |
|
|
108 |
top := StandardSystemView new.
|
|
109 |
hRuler := Ruler origin:30@0.0 corner:1.0@30 in:top.
|
|
110 |
vRuler := VerticalRuler origin:0.0@30 corner:30@1.0 in:top.
|
|
111 |
vRuler showUnit:false.
|
|
112 |
hRuler level:1.
|
|
113 |
vRuler level:1.
|
|
114 |
top open
|
|
115 |
|
|
116 |
see the DrawTool, for how to make it scroll in sync with some
|
|
117 |
other view.
|
|
118 |
"
|
|
119 |
! !
|
0
|
120 |
|
|
121 |
!Ruler methodsFor:'accessing'!
|
|
122 |
|
|
123 |
metric:aSymbol
|
24
|
124 |
"set the metric. The argument may be either #inch or #mm"
|
|
125 |
|
|
126 |
aSymbol ~~ metric ifTrue:[
|
|
127 |
metric := aSymbol.
|
|
128 |
shown ifTrue:[
|
|
129 |
self redraw
|
|
130 |
]
|
|
131 |
]
|
|
132 |
!
|
|
133 |
|
26
|
134 |
scale:aFactor
|
|
135 |
"set the scale factor. 1 is identity."
|
|
136 |
|
|
137 |
aFactor ~= scale ifTrue:[
|
|
138 |
scale := aFactor.
|
|
139 |
shown ifTrue:[
|
|
140 |
self redraw
|
|
141 |
]
|
|
142 |
]
|
|
143 |
!
|
|
144 |
|
24
|
145 |
paperWidthInch:inches
|
|
146 |
"set the width of the document"
|
0
|
147 |
|
24
|
148 |
paperWidth := inches.
|
0
|
149 |
shown ifTrue:[
|
24
|
150 |
self redraw
|
|
151 |
]
|
|
152 |
!
|
|
153 |
|
|
154 |
paperWidthMM:millis
|
|
155 |
"set the width of the document"
|
|
156 |
|
|
157 |
paperWidth := self millimeterToInch:millis.
|
|
158 |
shown ifTrue:[
|
|
159 |
self redraw
|
0
|
160 |
]
|
26
|
161 |
!
|
|
162 |
|
33
|
163 |
showUnit:aBoolean
|
|
164 |
"set/clear the showUnit flag. If false, the unit string
|
|
165 |
(i.e. 'inch' or 'mm') is not schown. Default is true."
|
|
166 |
|
|
167 |
showUnit := aBoolean.
|
|
168 |
!
|
|
169 |
|
26
|
170 |
viewOrigin:origin
|
33
|
171 |
origin = self viewOrigin ifTrue:[^ self].
|
26
|
172 |
super setViewOrigin:origin.
|
|
173 |
self redraw
|
28
|
174 |
!
|
|
175 |
|
|
176 |
widthOfContents
|
|
177 |
^ (device horizontalPixelPerInch * paperWidth) rounded
|
|
178 |
!
|
|
179 |
|
|
180 |
heightOfContents
|
|
181 |
^ (device verticalPixelPerInch * paperHeight) rounded
|
0
|
182 |
! !
|
|
183 |
|
|
184 |
!Ruler methodsFor:'redrawing'!
|
|
185 |
|
|
186 |
redraw
|
28
|
187 |
"redraw the scale"
|
0
|
188 |
|
|
189 |
|x pixelPerMM pixelPerInch mod pos shortLen veryShortLen longLen charY
|
26
|
190 |
top paperWidthMM paperWidthPixel xOrigin labelRight stringRight marg|
|
0
|
191 |
|
24
|
192 |
shown ifFalse:[^ self].
|
|
193 |
|
|
194 |
self clear.
|
0
|
195 |
|
26
|
196 |
xOrigin := self viewOrigin x.
|
6
|
197 |
|
26
|
198 |
paperWidthPixel := ((self inchToPixel:paperWidth) * scale) rounded.
|
|
199 |
|
6
|
200 |
(xOrigin + width > paperWidthPixel) ifTrue:[
|
24
|
201 |
self paint:(Color darkGrey).
|
34
|
202 |
self fillRectangleX:paperWidthPixel y:0
|
|
203 |
width:(xOrigin + width - paperWidthPixel)
|
|
204 |
height:height.
|
24
|
205 |
self paint:fgColor.
|
34
|
206 |
self displayLineFromX:paperWidthPixel y:0
|
|
207 |
toX:paperWidthPixel y:height
|
0
|
208 |
].
|
|
209 |
|
|
210 |
self paint:fgColor.
|
|
211 |
|
|
212 |
top := height - font height - font ascent.
|
|
213 |
longLen := font height.
|
|
214 |
shortLen := longLen // 2.
|
|
215 |
charY := top + (font ascent) + shortLen.
|
|
216 |
mod := 1.
|
6
|
217 |
marg := 3. "character shift"
|
0
|
218 |
|
|
219 |
(metric == #mm) ifTrue:[
|
24
|
220 |
"centimeter - long blibs every centimeter; short ones every half"
|
0
|
221 |
|
24
|
222 |
paperWidthMM := self inchToMillimeter:paperWidth.
|
26
|
223 |
pixelPerMM := (self millimeterToPixel:1) * scale.
|
24
|
224 |
pos := 5.
|
33
|
225 |
showUnit ifTrue:[
|
34
|
226 |
labelRight := stringRight := marg + (font widthOf:'cm') + 3 + xOrigin.
|
33
|
227 |
] ifFalse:[
|
34
|
228 |
labelRight := stringRight := xOrigin.
|
33
|
229 |
].
|
6
|
230 |
|
34
|
231 |
x := (pixelPerMM * pos) rounded.
|
|
232 |
[(x < (width+xOrigin)) and:[pos <= paperWidthMM]] whileTrue:[
|
26
|
233 |
|l|
|
|
234 |
|
28
|
235 |
l := shortLen.
|
|
236 |
(mod ~~ 1) ifTrue:[
|
|
237 |
x < stringRight ifFalse:[
|
26
|
238 |
l := longLen
|
|
239 |
].
|
28
|
240 |
].
|
|
241 |
self displayLineFromX:x y:top
|
|
242 |
toX:x y:(top + l).
|
26
|
243 |
|
28
|
244 |
((x < stringRight) or:[mod == 1]) ifFalse:[
|
|
245 |
self displayString:(pos // 10) printString
|
|
246 |
x:(x + marg)
|
|
247 |
y:charY.
|
|
248 |
stringRight := x + marg
|
|
249 |
+ (font widthOf:(pos // 10) printString)
|
24
|
250 |
].
|
|
251 |
mod := (mod + 1) \\ 2.
|
|
252 |
pos := pos + 5.
|
34
|
253 |
x := (pixelPerMM * pos) rounded
|
24
|
254 |
].
|
34
|
255 |
self displayString:'cm ' x:marg+xOrigin y:charY.
|
0
|
256 |
].
|
|
257 |
(metric == #inch) ifTrue:[
|
24
|
258 |
"inches - long blibs every inch; short ones every half; very
|
|
259 |
short ones every quarter"
|
0
|
260 |
|
26
|
261 |
pixelPerInch := (self inchToPixel:1) * scale.
|
24
|
262 |
pos := 0.25.
|
33
|
263 |
showUnit ifTrue:[
|
34
|
264 |
labelRight := marg + (font widthOf:'inch') + 3 + xOrigin.
|
33
|
265 |
] ifFalse:[
|
34
|
266 |
labelRight := xOrigin
|
33
|
267 |
].
|
6
|
268 |
|
34
|
269 |
x := (pixelPerInch * pos) rounded.
|
24
|
270 |
veryShortLen := longLen // 4.
|
34
|
271 |
[(x < (xOrigin+width)) and:[pos <= paperWidth]] whileTrue:[
|
28
|
272 |
|l|
|
|
273 |
|
|
274 |
l := shortLen.
|
24
|
275 |
(mod == 0) ifTrue:[
|
|
276 |
x < labelRight ifFalse:[
|
28
|
277 |
l := longLen
|
24
|
278 |
]
|
|
279 |
] ifFalse:[
|
28
|
280 |
(mod == 2) ifFalse:[
|
|
281 |
l := veryShortLen
|
24
|
282 |
]
|
|
283 |
].
|
28
|
284 |
self displayLineFromX:x y:top
|
|
285 |
toX:x y:(top + l).
|
|
286 |
|
|
287 |
(mod == 0 and:[x >= labelRight]) ifTrue:[
|
|
288 |
self displayString:pos asInteger printString
|
|
289 |
x:(x + marg)
|
|
290 |
y:charY
|
|
291 |
].
|
24
|
292 |
mod := (mod + 1) \\ 4.
|
|
293 |
pos := pos + 0.25.
|
34
|
294 |
x := (pixelPerInch * pos) rounded
|
24
|
295 |
].
|
34
|
296 |
self displayString:'inch ' x:marg+xOrigin y:charY.
|
0
|
297 |
].
|
|
298 |
self redrawEdges
|
|
299 |
! !
|
|
300 |
|
|
301 |
!Ruler methodsFor:'initialization'!
|
|
302 |
|
|
303 |
initialize
|
|
304 |
super initialize.
|
|
305 |
|
62
|
306 |
viewBackground := styleSheet colorAt:#rulerBackgroundColor default:viewBackground.
|
|
307 |
fgColor := styleSheet colorAt:#rulerForegroundColor.
|
33
|
308 |
fgColor isNil ifTrue:[
|
62
|
309 |
fgColor := styleSheet colorAt:#foregroundColor.
|
33
|
310 |
].
|
|
311 |
fgColor isNil ifTrue:[
|
|
312 |
viewBackground brightness > 0.5 ifTrue:[
|
|
313 |
fgColor := Black.
|
|
314 |
] ifFalse:[
|
|
315 |
fgColor := White
|
|
316 |
].
|
|
317 |
].
|
|
318 |
fgColor := fgColor on:device.
|
|
319 |
|
26
|
320 |
bitGravity := #NorthWest.
|
|
321 |
|
28
|
322 |
self height:(font height + (2 * font descent)).
|
42
|
323 |
|
30
|
324 |
(Smalltalk language == #english) ifTrue:[
|
24
|
325 |
metric := #inch
|
0
|
326 |
] ifFalse:[
|
24
|
327 |
metric := #mm
|
0
|
328 |
].
|
62
|
329 |
metric := styleSheet at:#rulerMetric default:metric.
|
42
|
330 |
|
33
|
331 |
showUnit := true.
|
|
332 |
|
26
|
333 |
scale := 1.
|
|
334 |
|
0
|
335 |
paperWidth := 8.5.
|
24
|
336 |
paperHeight := 11.
|
0
|
337 |
|
24
|
338 |
"
|
|
339 |
take a smaller font
|
|
340 |
"
|
|
341 |
font := (Font family:(font family)
|
|
342 |
face:(font face)
|
|
343 |
style:(font style)
|
|
344 |
size:8) on:device.
|
|
345 |
|
|
346 |
"
|
|
347 |
Ruler new open
|
|
348 |
"
|
|
349 |
!
|
|
350 |
|
|
351 |
reinitialize
|
|
352 |
super reinitialize.
|
|
353 |
font := font on:device.
|
0
|
354 |
! !
|
|
355 |
|
|
356 |
!Ruler methodsFor:'metric conversions'!
|
|
357 |
|
|
358 |
inchToMillimeter:inches
|
|
359 |
"convert inches to mm"
|
|
360 |
|
|
361 |
^ inches * 25.4
|
|
362 |
!
|
|
363 |
|
|
364 |
inchToPixel:inches
|
|
365 |
"convert inches to screen pixels"
|
|
366 |
|
|
367 |
^ inches * self horizontalPixelPerInch
|
|
368 |
!
|
|
369 |
|
|
370 |
inchToTwip:inches
|
|
371 |
"convert inches to twips"
|
|
372 |
|
|
373 |
^ inches * 1440
|
|
374 |
|
|
375 |
!
|
|
376 |
|
|
377 |
millimeterToPixel:mm
|
|
378 |
"convert mms to screen pixels"
|
|
379 |
|
|
380 |
^ mm * self horizontalPixelPerMillimeter
|
|
381 |
|
|
382 |
!
|
|
383 |
|
|
384 |
millimeterToInch:mm
|
|
385 |
"convert mm to inches"
|
|
386 |
|
|
387 |
^ mm / 25.4
|
|
388 |
!
|
|
389 |
|
|
390 |
pointToTwip:points
|
|
391 |
"convert points to twips"
|
|
392 |
|
|
393 |
^ points * 20
|
|
394 |
!
|
|
395 |
|
|
396 |
twipToInch:twips
|
|
397 |
"convert twips to inches"
|
|
398 |
|
|
399 |
^ twips / 1440.0
|
|
400 |
!
|
|
401 |
|
|
402 |
pixelToInch:pixels
|
|
403 |
"convert pixels to inches"
|
|
404 |
|
|
405 |
^ pixels / self horizontalPixelPerInch
|
|
406 |
!
|
|
407 |
|
|
408 |
twipToPixel:twips
|
|
409 |
"convert twips to screen pixels"
|
|
410 |
|
|
411 |
^ (twips / 1440.0) * self horizontalPixelPerInch
|
|
412 |
|
|
413 |
!
|
|
414 |
|
|
415 |
twipToPoint:twips
|
|
416 |
"convert twips to points"
|
|
417 |
|
|
418 |
^ twips / 20.0
|
|
419 |
! !
|