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