author | Claus Gittinger <cg@exept.de> |
Sun, 23 Feb 2020 16:01:31 +0100 | |
changeset 4450 | c07db2570587 |
parent 4357 | 07a48f0f4989 |
permissions | -rw-r--r-- |
4285 | 1 |
"{ Encoding: utf8 }" |
2 |
||
88 | 3 |
" |
4 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
93 | 5 |
All Rights Reserved |
88 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
1600
ac585461747e
computed rectangle is truncated here.
Claus Gittinger <cg@exept.de>
parents:
1039
diff
changeset
|
14 |
"{ Package: 'stx:libview2' }" |
ac585461747e
computed rectangle is truncated here.
Claus Gittinger <cg@exept.de>
parents:
1039
diff
changeset
|
15 |
|
3794 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
77 | 18 |
Layout subclass:#LayoutOrigin |
191 | 19 |
instanceVariableNames:'leftFraction topFraction leftOffset topOffset' |
20 |
classVariableNames:'' |
|
21 |
poolDictionaries:'' |
|
22 |
category:'Graphics-Geometry' |
|
77 | 23 |
! |
24 |
||
88 | 25 |
!LayoutOrigin class methodsFor:'documentation'! |
26 |
||
27 |
copyright |
|
28 |
" |
|
29 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
93 | 30 |
All Rights Reserved |
88 | 31 |
|
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
93 | 41 |
documentation |
42 |
" |
|
43 |
This class is provided to make porting of existing ST-80 applications |
|
44 |
easier. Instances can be used to control the geometry of a subview, within |
|
45 |
its superview. It provides the same functionality as a relative origin |
|
46 |
combined with insets. |
|
47 |
A layoutOrigin controls the origin of a subcomponent, given a fractional |
|
48 |
component and an offset component. |
|
49 |
||
229 | 50 |
Notice: |
51 |
this class was implemented using protocol information |
|
52 |
from alpha testers - it may not be complete or compatible to |
|
53 |
the corresponding ST-80 class. |
|
54 |
If you encounter any incompatibilities, please forward a note |
|
55 |
describing the incompatibility verbal (i.e. no code) to the ST/X team. |
|
219 | 56 |
|
57 |
[author:] |
|
58 |
Claus Gittinger |
|
229 | 59 |
|
60 |
[see also:] |
|
61 |
View |
|
62 |
LayoutFrame AlignmentOrigin Layout |
|
63 |
Rectangle Point |
|
94 | 64 |
" |
93 | 65 |
! |
66 |
||
67 |
examples |
|
68 |
" |
|
229 | 69 |
Although the examples below use a button as component, |
70 |
they work of course with any type of subview .... |
|
93 | 71 |
|
229 | 72 |
using a LayoutOrigin, to arrange for |
73 |
the TOPLEFT of a component be positions |
|
74 |
at the center (i.e. buttons origin at:0.5 @ 0.5): |
|
75 |
[exBegin] |
|
76 |
|top button| |
|
93 | 77 |
|
229 | 78 |
top := StandardSystemView new. |
79 |
top extent:300@300. |
|
93 | 80 |
|
229 | 81 |
button := Button label:'component'. |
82 |
top add:button in:(LayoutOrigin new |
|
83 |
leftFraction:0.5; |
|
84 |
topFraction:0.5). |
|
93 | 85 |
|
229 | 86 |
top open |
87 |
[exEnd] |
|
93 | 88 |
|
89 |
||
90 |
like above, but adds an additional offset: |
|
229 | 91 |
(i.e. center of button at:0.5 @ 0.5 OFFSET by 10 @ 20): |
92 |
[exBegin] |
|
93 |
|top button| |
|
93 | 94 |
|
229 | 95 |
top := StandardSystemView new. |
96 |
top extent:300@300. |
|
93 | 97 |
|
229 | 98 |
button := Button label:'component'. |
99 |
top add:button in:(LayoutOrigin new |
|
100 |
leftFraction:0.5; |
|
101 |
topFraction:0.5; |
|
102 |
leftOffset:10; |
|
103 |
topOffset:20). |
|
93 | 104 |
|
229 | 105 |
top open |
106 |
[exEnd] |
|
93 | 107 |
" |
88 | 108 |
! ! |
109 |
||
77 | 110 |
!LayoutOrigin class methodsFor:'instance creation'! |
111 |
||
112 |
fractionalFromPoint:aPoint |
|
93 | 113 |
"given a point, create a layoutOrigin representing the same |
114 |
relative origin." |
|
115 |
||
83 | 116 |
^ (self new) leftFraction:aPoint x topFraction:aPoint y |
93 | 117 |
|
118 |
" |
|
119 |
LayoutOrigin fractionalFromPoint:0.5@0.5 |
|
120 |
" |
|
77 | 121 |
! |
122 |
||
123 |
fromPoint:aPoint |
|
1821 | 124 |
"return a new LayoutOrigin from aPoint. |
125 |
If the coordinates are between 0 and 1, |
|
126 |
take them as fractional parts (relative to superview). |
|
77 | 127 |
Otherwise, treat them as absolute offsets." |
128 |
||
129 |
|x y layout| |
|
130 |
||
131 |
x := aPoint x. |
|
132 |
y := aPoint y. |
|
133 |
layout := self new. |
|
134 |
((x between:0 and:1) |
|
135 |
and:[y between:0 and:1]) ifTrue:[ |
|
1821 | 136 |
layout leftFraction:x topFraction:y |
77 | 137 |
] ifFalse:[ |
1821 | 138 |
layout leftOffset:x topOffset:y |
77 | 139 |
]. |
140 |
^ layout |
|
93 | 141 |
|
142 |
" |
|
143 |
LayoutOrigin fromPoint:100@100 |
|
144 |
LayoutOrigin fromPoint:0.5@0.5 |
|
145 |
LayoutOrigin fromPoint:0.5@100 |
|
146 |
" |
|
126 | 147 |
! |
148 |
||
149 |
offsetFromPoint:aPoint |
|
150 |
"given a point, create a layoutOrigin representing the same |
|
151 |
absolute (pixel) origin." |
|
152 |
||
153 |
^ self new leftOffset:aPoint x topOffset:aPoint y |
|
154 |
||
155 |
" |
|
156 |
LayoutOrigin offsetFromPoint:100@100 |
|
157 |
" |
|
158 |
! ! |
|
159 |
||
160 |
!LayoutOrigin methodsFor:'accessing'! |
|
161 |
||
162 |
leftFraction |
|
163 |
"return leftFraction" |
|
164 |
||
165 |
^ leftFraction |
|
166 |
! |
|
167 |
||
4357 | 168 |
leftFraction:aFraction |
169 |
"set leftFraction (0..1)" |
|
126 | 170 |
|
4357 | 171 |
leftFraction := aFraction. |
126 | 172 |
! |
173 |
||
4357 | 174 |
leftFraction:aFraction offset:o |
175 |
"set leftFraction (0..1) and offset in pixels" |
|
126 | 176 |
|
4357 | 177 |
leftFraction := aFraction. |
126 | 178 |
leftOffset := o |
179 |
! |
|
180 |
||
543
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
181 |
leftFraction:lF offset:lO topFraction:tF offset:tO |
4357 | 182 |
"set leftFraction (0..1), leftOffset, topFraction (0..1) and topOffset" |
543
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
183 |
|
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
184 |
leftFraction := lF. leftOffset := lO. |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
185 |
topFraction := tF. topOffset := tO. |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
186 |
|
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
187 |
"Created: 18.4.1997 / 20:07:15 / cg" |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
188 |
! |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
189 |
|
126 | 190 |
leftFraction:newLeft topFraction:newTop |
4357 | 191 |
"set leftFraction (0..1) and topFraction (0..1)" |
126 | 192 |
|
193 |
leftFraction := newLeft. |
|
194 |
topFraction := newTop. |
|
195 |
! |
|
196 |
||
275 | 197 |
leftInset:pixels |
198 |
"set leftOffset for an inset at the left" |
|
199 |
||
200 |
leftOffset := pixels. |
|
201 |
||
202 |
"Created: 26.5.1996 / 17:38:55 / cg" |
|
203 |
"Modified: 26.5.1996 / 17:42:16 / cg" |
|
204 |
! |
|
205 |
||
126 | 206 |
leftOffset |
207 |
"return leftOffset" |
|
208 |
||
209 |
^ leftOffset |
|
210 |
! |
|
211 |
||
212 |
leftOffset:something |
|
213 |
"set leftOffset" |
|
214 |
||
215 |
leftOffset := something. |
|
216 |
! |
|
217 |
||
218 |
leftOffset:newLeft topOffset:newTop |
|
219 |
"set leftOffset and topOffset" |
|
220 |
||
221 |
leftOffset := newLeft. |
|
222 |
topOffset := newTop. |
|
223 |
! |
|
224 |
||
225 |
topFraction |
|
226 |
"return topFraction" |
|
227 |
||
228 |
^ topFraction |
|
229 |
! |
|
230 |
||
4357 | 231 |
topFraction:aFraction |
232 |
"set topFraction (0..1)" |
|
126 | 233 |
|
4357 | 234 |
topFraction := aFraction. |
126 | 235 |
! |
236 |
||
4357 | 237 |
topFraction:aFraction offset:o |
238 |
"set topFraction (0..1) and offset" |
|
126 | 239 |
|
4357 | 240 |
topFraction := aFraction. |
126 | 241 |
topOffset := o |
242 |
! |
|
243 |
||
275 | 244 |
topInset:pixels |
245 |
"set topOffset for an inset at the top" |
|
246 |
||
247 |
topOffset := pixels. |
|
248 |
||
249 |
"Created: 26.5.1996 / 17:39:00 / cg" |
|
250 |
"Modified: 26.5.1996 / 17:42:12 / cg" |
|
251 |
! |
|
252 |
||
126 | 253 |
topOffset |
254 |
"return topOffset" |
|
255 |
||
256 |
^ topOffset |
|
257 |
! |
|
258 |
||
259 |
topOffset:something |
|
260 |
"set topOffset" |
|
261 |
||
262 |
topOffset := something. |
|
263 |
! ! |
|
264 |
||
3330 | 265 |
!LayoutOrigin methodsFor:'comparing'! |
266 |
||
267 |
= anObject |
|
3331 | 268 |
^ anObject class == self class |
3330 | 269 |
and:[ anObject leftFraction = leftFraction |
270 |
and:[ anObject topFraction = topFraction |
|
271 |
and:[ anObject leftOffset = leftOffset |
|
272 |
and:[ anObject topOffset = topOffset ]]]] |
|
273 |
! |
|
274 |
||
275 |
hash |
|
276 |
^ leftFraction hash + topFraction hash + leftOffset hash + topOffset hash |
|
277 |
! ! |
|
278 |
||
4294 | 279 |
!LayoutOrigin methodsFor:'computing'! |
280 |
||
281 |
rectangleRelativeTo:superRectangle preferred:prefRectHolder |
|
282 |
"compute the rectangle represented by the receiver, |
|
283 |
given the superView's rectangle and the view's preferredExtent." |
|
284 |
||
285 |
|x y prefRect| |
|
286 |
||
287 |
prefRect := prefRectHolder value. |
|
288 |
||
289 |
x := superRectangle left. |
|
290 |
y := superRectangle top. |
|
291 |
||
292 |
leftOffset notNil ifTrue:[ |
|
293 |
x := x + leftOffset value |
|
294 |
]. |
|
295 |
topOffset notNil ifTrue:[ |
|
296 |
y := y + topOffset value |
|
297 |
]. |
|
298 |
leftFraction notNil ifTrue:[ |
|
299 |
x := x + (superRectangle width * leftFraction value) |
|
300 |
]. |
|
301 |
topFraction notNil ifTrue:[ |
|
302 |
y := y + (superRectangle height * topFraction value) |
|
303 |
]. |
|
304 |
^ Rectangle left:x top:y extent:prefRect extent |
|
305 |
||
306 |
" |
|
307 |
|superRect lO| |
|
308 |
||
309 |
superRect := 0@0 corner:100@100. |
|
310 |
lO := (LayoutOrigin new). |
|
311 |
lO leftFraction:0.5; |
|
312 |
topFraction:0.5. |
|
313 |
lO rectangleRelativeTo:superRect preferred:(0@0 corner:30@30) |
|
314 |
" |
|
315 |
||
316 |
"Modified: / 13-08-1998 / 18:39:49 / cg" |
|
317 |
"Modified (comment): / 17-07-2019 / 10:02:59 / Claus Gittinger" |
|
318 |
! ! |
|
319 |
||
126 | 320 |
!LayoutOrigin methodsFor:'converting'! |
321 |
||
543
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
322 |
asAlignmentOrigin |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
323 |
"return an equivalent alignmentOrigin" |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
324 |
|
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
325 |
^ AlignmentOrigin new |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
326 |
leftFraction:leftFraction offset:leftOffset; |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
327 |
topFraction:topFraction offset:topOffset; |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
328 |
leftAlignmentFraction:0 topAlignmentFraction: 0; |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
329 |
yourself |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
330 |
|
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
331 |
"Created: 18.4.1997 / 20:08:14 / cg" |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
332 |
! |
1e730ed96b0b
added conversion to alignmentOrigin and another access method.
Claus Gittinger <cg@exept.de>
parents:
415
diff
changeset
|
333 |
|
126 | 334 |
fromLiteralArrayEncoding:encoding |
335 |
"read my values from an encoding. |
|
336 |
The encoding is supposed to be of the form: |
|
337 |
(#LayoutOrigin orgOffsX relOrgX orgOffsY relOrgY) |
|
338 |
This is the reverse operation to #literalArrayEncoding." |
|
339 |
||
340 |
leftOffset := encoding at:2. |
|
341 |
leftFraction := encoding at:3. |
|
342 |
topOffset := encoding at:4. |
|
343 |
topFraction := encoding at:5. |
|
344 |
||
345 |
||
346 |
" |
|
347 |
LayoutOrigin new fromLiteralArrayEncoding:#(#LayoutOrigin 70 0 2 0) |
|
348 |
" |
|
349 |
! |
|
350 |
||
351 |
literalArrayEncoding |
|
191 | 352 |
"encode myself as an array, from which a copy of the receiver |
353 |
can be reconstructed with #decodeAsLiteralArray. |
|
126 | 354 |
The encoding is: |
191 | 355 |
(#LayoutOrigin orgOffsX relOrgX orgOffsY relOrgY) |
126 | 356 |
" |
357 |
||
358 |
^ Array |
|
2134 | 359 |
with:self class name |
191 | 360 |
with:leftOffset |
361 |
with:leftFraction |
|
362 |
with:topOffset |
|
363 |
with:topFraction |
|
126 | 364 |
|
365 |
" |
|
366 |
LayoutOrigin new fromLiteralArrayEncoding:#(#LayoutOrigin 70 0 2 0) |
|
367 |
(LayoutOrigin new leftOffset:10; leftFraction:0.2; |
|
191 | 368 |
topOffset:20; topFraction:0.4) literalArrayEncoding |
126 | 369 |
" |
370 |
||
371 |
"Modified: 1.9.1995 / 02:43:48 / claus" |
|
191 | 372 |
"Modified: 22.4.1996 / 13:00:21 / cg" |
126 | 373 |
! ! |
374 |
||
375 |
!LayoutOrigin methodsFor:'initialization'! |
|
376 |
||
377 |
initialize |
|
378 |
leftOffset := topOffset := 0. |
|
379 |
leftFraction := topFraction := 0. |
|
3865 | 380 |
|
381 |
"Modified: / 08-02-2017 / 00:44:18 / cg" |
|
77 | 382 |
! ! |
383 |
||
83 | 384 |
!LayoutOrigin methodsFor:'printing & storing'! |
385 |
||
3016
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
386 |
displayOn:aGCOrStream |
700 | 387 |
"return a printed representation of the receiver for displaying" |
388 |
||
3016
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
389 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
3923 | 390 |
"/ old ST80 means: draw-yourself on a GC. |
3016
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
391 |
(aGCOrStream isStream) ifFalse:[ |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
392 |
^ super displayOn:aGCOrStream |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
393 |
]. |
700 | 394 |
|
3016
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
395 |
aGCOrStream |
4285 | 396 |
nextPutAll:self className; |
3016
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
397 |
nextPutAll:'(l: '. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
398 |
|
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
399 |
leftFraction displayOn:aGCOrStream. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
400 |
aGCOrStream nextPut:$+. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
401 |
leftOffset displayOn:aGCOrStream. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
402 |
aGCOrStream nextPutAll:' t: '. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
403 |
topFraction displayOn:aGCOrStream. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
404 |
aGCOrStream nextPut:$+. |
e2374dddc45f
Implement #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2134
diff
changeset
|
405 |
topOffset displayOn:aGCOrStream. |
3923 | 406 |
aGCOrStream nextPut:$). |
407 |
||
408 |
"Modified (comment): / 22-02-2017 / 16:49:31 / cg" |
|
4285 | 409 |
"Modified: / 28-06-2019 / 09:04:01 / Claus Gittinger" |
83 | 410 |
! ! |
411 |
||
126 | 412 |
!LayoutOrigin methodsFor:'queries'! |
77 | 413 |
|
415 | 414 |
isLayoutOrigin |
415 |
"return true, if this is a layoutOrigin" |
|
416 |
||
417 |
^ true |
|
418 |
||
419 |
! |
|
420 |
||
126 | 421 |
origin |
422 |
^ leftFraction asFloat @ topFraction asFloat |
|
77 | 423 |
! ! |
100 | 424 |
|
126 | 425 |
!LayoutOrigin class methodsFor:'documentation'! |
100 | 426 |
|
126 | 427 |
version |
3794 | 428 |
^ '$Header$' |
3331 | 429 |
! |
430 |
||
431 |
version_CVS |
|
3794 | 432 |
^ '$Header$' |
100 | 433 |
! ! |
3330 | 434 |