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