/
LayoutFrame.class.st
375 lines (301 loc) · 9.45 KB
/
LayoutFrame.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"
I define a transformation frame relative to some rectangle. I'm basic data structure used for graphics.
Do not use fractions: fractionsOrNil offsets: offsetsOrNil or frations: fractionsOrNil if you do not have already the rectangles that should be passed as arguments.
If you are creating the rectangles representing the numbers you need, better use the accessors. For example like this.
(LayoutFrame identity
topOffset: topHeight;
bottomFraction: 0;
bottomOffset: self buttonsBarHeight;
leftOffset: -1;
rightOffset: 1)
Instance variables:
leftFraction
topFraction
rightFraction
bottomFraction <Float> The fractional distance (between 0 and 1) to place the morph in its owner's bounds
leftOffset
topOffset
rightOffset
bottomOffset <Integer> Fixed pixel offset to apply after fractional positioning (e.g., ""10 pixel right of the center of the owner"")
"
Class {
#name : #LayoutFrame,
#superclass : #Object,
#instVars : [
'leftFraction',
'leftOffset',
'topFraction',
'topOffset',
'rightFraction',
'rightOffset',
'bottomFraction',
'bottomOffset'
],
#category : #'Morphic-Base-Layouts'
}
{ #category : #'instance creation' }
LayoutFrame class >> identity [
"by default a layout frame is initialized to represent the identity transformation"
^ self new
]
{ #category : #comparing }
LayoutFrame >> = aFrame [
^self species = aFrame species
and:[ self leftFraction == aFrame leftFraction
and:[ self leftOffset == aFrame leftOffset
and:[ self topFraction == aFrame topFraction
and:[ self topOffset == aFrame topOffset
and:[ self rightFraction == aFrame rightFraction
and:[ self rightOffset == aFrame rightOffset
and:[ self bottomFraction == aFrame bottomFraction
and:[ self bottomOffset == aFrame bottomOffset]]]]]]]]
]
{ #category : #converting }
LayoutFrame >> asArray [
^ { leftFraction .
topFraction .
rightFraction .
bottomFraction .
leftOffset .
topOffset .
rightOffset .
bottomOffset }
]
{ #category : #converting }
LayoutFrame >> asLayoutFrame [
^self
]
{ #category : #accessing }
LayoutFrame >> bottomFraction [
^ bottomFraction
]
{ #category : #accessing }
LayoutFrame >> bottomFraction: aNumber [
bottomFraction := aNumber
]
{ #category : #accessing }
LayoutFrame >> bottomFraction: aNumber offset: anInteger [
bottomFraction := aNumber.
bottomOffset := anInteger
]
{ #category : #accessing }
LayoutFrame >> bottomOffset [
^bottomOffset
]
{ #category : #accessing }
LayoutFrame >> bottomOffset: anInteger [
bottomOffset := anInteger
]
{ #category : #accessing }
LayoutFrame >> bottomRightOffset: aPoint [
"If you construct a point on purpose to call this method, better use directly the correct accessors, i.e.
bottomRightOffset: 100 @ 200
=>
bottomOffset: 200.
rightOffset: 100.
"
bottomOffset := aPoint y.
rightOffset := aPoint x.
]
{ #category : #layout }
LayoutFrame >> center: targetMorph with: anotherMorph [
"Change the receiver to center the targetMorph with anotherMorph.
Note the targetMorph is the morph on which the receiver will be applied.
anotherMorph is often the morph containing the targetMorph.
"
self topOffset: (anotherMorph height - targetMorph height) // 2.
self leftOffset: (anotherMorph width - targetMorph width) // 2.
]
{ #category : #accessing }
LayoutFrame >> fractionRectangle [
"Return a rectangle representing the fraction part of layout frame: i.e left@top corner: right@bottom"
^ leftFraction @ topFraction corner: rightFraction @ bottomFraction
]
{ #category : #deprecated }
LayoutFrame >> fractions: fractionsOrNil [
self deprecated: 'Do not use this method. It forces to create spurious objects for nothing.'.
^ self fractions: fractionsOrNil offsets: nil
]
{ #category : #deprecated }
LayoutFrame >> fractions: fractionsOrNil offsets: offsetsOrNil [
| fractions offsets |
self deprecated: 'Do not use this method. It forces to create spurious objects (rectangle or points) for nothing.'.
fractions := fractionsOrNil ifNil: [0@0 extent: 0@0].
offsets := offsetsOrNil ifNil: [0@0 extent: 0@0].
^self
topFraction: fractions top offset: offsets top;
leftFraction: fractions left offset: offsets left;
bottomFraction: fractions bottom offset: offsets bottom;
rightFraction: fractions right offset: offsets right
]
{ #category : #accessing }
LayoutFrame >> fromArray: anArray [
| str |
str := anArray readStream.
#( leftFraction: topFraction: rightFraction: bottomFraction: leftOffset: topOffset: rightOffset: bottomOffset: ) do: [:sel |
str next ifNil: [ ^ self ] ifNotNil: [:value | self perform: sel with: value ]
]
]
{ #category : #testing }
LayoutFrame >> hasNoOffsets [
^ leftOffset = 0 and: [rightOffset = 0 and: [ topOffset = 0 and: [ bottomOffset = 0 ]]].
]
{ #category : #comparing }
LayoutFrame >> hash [
^(((((((( self species hash
+ leftFraction hash) hashMultiply
+ leftOffset hash) hashMultiply
+ topFraction hash) hashMultiply
+ topOffset hash) hashMultiply
+ rightFraction hash) hashMultiply
+ rightOffset hash) hashMultiply
+ bottomFraction hash) hashMultiply
+ bottomOffset hash) hashMultiply
]
{ #category : #initialization }
LayoutFrame >> initialize [
"initialize defaults:
- all offsets are zero
- fraction frame is maximum"
leftOffset := rightOffset := topOffset := bottomOffset := 0.
leftFraction := topFraction := 0.
rightFraction := bottomFraction := 1.
]
{ #category : #printing }
LayoutFrame >> isSelfEvaluating [
^ false
]
{ #category : #layout }
LayoutFrame >> layout: oldBounds in: newBounds [
"Return the proportional rectangle insetting the given bounds"
^ self transform: newBounds
]
{ #category : #accessing }
LayoutFrame >> leftFraction [
^leftFraction
]
{ #category : #accessing }
LayoutFrame >> leftFraction: aNumber [
leftFraction := aNumber
]
{ #category : #accessing }
LayoutFrame >> leftFraction: aNumber offset: anInteger [
leftFraction := aNumber.
leftOffset := anInteger
]
{ #category : #accessing }
LayoutFrame >> leftOffset [
^leftOffset
]
{ #category : #accessing }
LayoutFrame >> leftOffset: anInteger [
leftOffset := anInteger
]
{ #category : #'objects from disk' }
LayoutFrame >> negateBottomRightOffsets [
bottomOffset := bottomOffset negated.
rightOffset := rightOffset negated.
]
{ #category : #printing }
LayoutFrame >> printOn: aStream [
super printOn: aStream.
aStream nextPutAll: ' ( '.
aStream
print: self leftFraction @ self topFraction;
nextPutAll: ' + ';
print: self leftOffset @ self topOffset ;
nextPutAll: 'px corner: ';
print: self rightFraction @ self bottomFraction;
nextPutAll: ' + ';
print: self rightOffset @ self bottomOffset ;
nextPutAll: 'px ) '
]
{ #category : #printing }
LayoutFrame >> printOnForHelpingMigration: aStream [
aStream
nextPutAll: '(' ;
nextPutAll: self className;
nextPutAll: ' identity';
nextPutAll: String cr;
nextPutAll: 'topOffset: ';
print: self topOffset;
nextPutAll: String cr;
nextPutAll: 'bottomFraction: ';
print: self bottomFraction;
nextPutAll: String cr;
nextPutAll: 'bottomOffset: ';
print: self bottomOffset;
nextPutAll: String cr;
nextPutAll: 'leftOffset: ';
print: self leftOffset;
nextPutAll: String cr;
nextPutAll: 'rightOffset: ';
print: self rightOffset;
nextPutAll: ')'
]
{ #category : #accessing }
LayoutFrame >> rightFraction [
^rightFraction
]
{ #category : #accessing }
LayoutFrame >> rightFraction: aNumber [
rightFraction := aNumber
]
{ #category : #accessing }
LayoutFrame >> rightFraction: aNumber offset: anInteger [
rightFraction := aNumber.
rightOffset := anInteger
]
{ #category : #accessing }
LayoutFrame >> rightOffset [
^rightOffset
]
{ #category : #accessing }
LayoutFrame >> rightOffset: anInteger [
rightOffset := anInteger
]
{ #category : #accessing }
LayoutFrame >> topFraction [
^topFraction
]
{ #category : #accessing }
LayoutFrame >> topFraction: aNumber [
topFraction := aNumber
]
{ #category : #accessing }
LayoutFrame >> topFraction: aNumber offset: anInteger [
topFraction := aNumber.
topOffset := anInteger
]
{ #category : #accessing }
LayoutFrame >> topLeftOffset: aPoint [
"If you construct a point on purpose to call this method, better use directly the correct accessors, i.e.
topLeftOffset: 100 @ 200
=>
topOffset: 200.
leftOffset: 100.
"
topOffset := aPoint y.
leftOffset := aPoint x.
]
{ #category : #accessing }
LayoutFrame >> topOffset [
^topOffset
]
{ #category : #accessing }
LayoutFrame >> topOffset: anInteger [
topOffset := anInteger
]
{ #category : #transforming }
LayoutFrame >> transform: outerReferenceRectangle [
"Answer a new rectangle which transformed by receiver relative to outerReferenceRectangle.
If it is not possible to place a outerReferenceRectangle *inside* this reference rectangle, then result will be zero-width (or zero-height) rectangle. Basically you transform to obtain a subrectangle area inside the reference rectangle"
| left right top bottom |
left := (outerReferenceRectangle left + (outerReferenceRectangle width * leftFraction) + leftOffset) rounded asInteger.
right := (outerReferenceRectangle right - (outerReferenceRectangle width * (1 - rightFraction)) + rightOffset) rounded asInteger.
top:= (outerReferenceRectangle top + (outerReferenceRectangle height * topFraction) + topOffset ) rounded asInteger.
bottom:= (outerReferenceRectangle bottom - (outerReferenceRectangle height * (1 - bottomFraction)) + bottomOffset) rounded asInteger.
right < left ifTrue: [ right := left ].
bottom < top ifTrue: [ bottom := top ].
^ Rectangle left: left right: right top: top bottom: bottom
]