-
Notifications
You must be signed in to change notification settings - Fork 10
/
CPViewSize.j
executable file
·206 lines (163 loc) · 5.36 KB
/
CPViewSize.j
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
/* -*-objc-*-
Author: Nicola Pero <[email protected]>
Date: January 2003
Author of Cappuccino port: Daniel Boehringer (2012)
This file is part of GNUstep Renaissance
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
@import <AppKit/CPView.j>
@import <AppKit/CPControl.j>
@import <AppKit/CPBox.j>
@import <AppKit/CPSplitView.j>
@import <AppKit/CPTextField.j>
@import <AppKit/CPImageView.j>
@import <AppKit/CPColorWell.j>
@implementation CPView (sizeToContent)
- (void) sizeToFitContent
{
/* In the general case of a general view, this is a no-op. We have
* no idea how to fit the size to the content; we trust that whoever
* set up the view set up the width and height to be the right ones
* for the content. This has the additional benefit that
* -minimuSizeForContent will return the current size of the view as
* the minimum; ie, unless we know otherwise, we assume that the
* view has been sized to display its content properly and we don't
* want to shrink it.
*/
}
- (CPSize) minimumSizeForContent
{
var oldFrame;
var minimumSize;
/* Save the oldFrame. */
oldFrame = [self frame];
/* Resize the view to fit the contents ... this is the only
* way available in the AppKit to get the minimum size. */
[self sizeToFitContent];
/* Get the minimum size by reading the frame. */
minimumSize = [self frame].size;
/* Restore the original frame. */
[self setFrame: oldFrame];
return minimumSize;
}
@end
@implementation CPControl (sizeToContent)
- (void) sizeToFitContent
{
if([self respondsToSelector:@selector(sizeToFit) ]) [self sizeToFit];
}
@end
/* CPBox comments - make sure you realize that CPBox -sizeToFit calls
* the contentView's -sizeToFit method. If you want the CPBox
contentview to have a hardcoded frame which is not the minimum
* size, you need to embed it in a [hv]box before putting it in
* the CPBox, to make sure this hardcoded frame overrides the minimum
* size.
*/
@implementation CPBox (sizeToContent)
- (void) sizeToFitContent
{
[self sizeToFit];
}
@end
/* CPSplitView's sizeToContent makes the splitview just big enough
* to display its subviews, plus the dividers.
*
* NB: (not really relevant any longer, but for future memories of
* past problems) the default implementation of setting a CPZeroSize
would not work because resizing a splitview resizes all subviews,
* and resizing a splitview to a zero size, at least on GNUstep,
* resizes all subviews to have zero size, losing all size
* relationships between them ... it's an unrecoverable operation on
* GNUstep.
*/
@implementation CPSplitView (sizeToContent)
- (void) sizeToFitContent
{
var newSize = CPMakeSize(0,0);
var subviews = [self subviews];
var i, count = [subviews count];
var dividerThickness = [self dividerThickness];
if (count == 0)
{
[self setFrameSize: newSize];
return;
}
if ([self isVertical])
{
var subview = [subviews objectAtIndex: 0];
var subviewRect = [subview frame];
newSize.height = subviewRect.size.height;
for (i = 0; i < count; i++)
{
subview = [subviews objectAtIndex: i];
subviewRect = [subview frame];
newSize.width += subviewRect.size.width;
}
newSize.width += dividerThickness * (count - 1);
}
else
{
var subview = [subviews objectAtIndex: 0];
var subviewRect = [subview frame];
newSize.width = subviewRect.size.width;
for (i = 0; i < count; i++)
{
subview = [subviews objectAtIndex: i];
subviewRect = [subview frame];
newSize.height += subviewRect.size.height;
}
newSize.height += dividerThickness * (count - 1);
}
[self setFrameSize: newSize];
}
@end
@implementation CPTextField (sizeToContent)
/* We want text fields to get a reasonable size when empty. */
- (void) sizeToFitContent
{
var stringValue = [self stringValue];
if (stringValue == nil || [stringValue length] == 0)
{
[self setStringValue: @"Nicola"];
[self sizeToFit];
[self setStringValue: @""];
}
else
{
[self sizeToFit];
}
}
@end
/* CPImageView does not support sizeToFit on Apple! The following
* hack only works after the image has just been set. */
@implementation CPImageView (sizeToContent)
- (void) sizeToFitContent
{
[self setFrameSize: [[self image] size]];
}
@end
/* CPColorWell does not have a working sizeToFit; let's just implement
* it to use a minimum size of 52x30, which is roughly Gorm's default
* color well size. */
@implementation CPColorWell (sizeToContent)
- (void) sizeToFitContent
{
[self setFrameSize: [self minimumSizeForContent]];
}
- (CPSize) minimumSizeForContent
{
return CPMakeSize (52, 30);
}
@end