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
|
/* -*-objc-*-
NSViewSize.m
Copyright (C) 2002 Free Software Foundation, Inc.
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: March 2002, November 2002
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.
*/
#include <AutoLayoutCommonInclude.h>
#include "NSViewSize.h"
@implementation NSView (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.
*/
}
- (NSSize) minimumSizeForContent
{
NSRect oldFrame;
NSSize 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 NSControl (sizeToContent)
- (void) sizeToFitContent
{
[self sizeToFit];
}
@end
/* NSBox comments - make sure you realize that NSBox -sizeToFit calls
* the contentView's -sizeToFit method. If you want the NSBox
* 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 NSBox, to make sure this hardcoded frame overrides the minimum
* size.
*/
@implementation NSBox (sizeToContent)
- (void) sizeToFitContent
{
[self sizeToFit];
}
@end
/* NSSplitView'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 NSZeroSize
* 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 NSSplitView (sizeToContent)
- (void) sizeToFitContent
{
NSSize newSize = NSZeroSize;
NSArray *subviews = [self subviews];
int i, count = [subviews count];
float dividerThickness = [self dividerThickness];
if (count == 0)
{
[self setFrameSize: newSize];
return;
}
if ([self isVertical])
{
NSView *subview = [subviews objectAtIndex: 0];
NSRect 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
{
NSView *subview = [subviews objectAtIndex: 0];
NSRect 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 NSTextField (sizeToContent)
/* We want text fields to get a reasonable size when empty. */
- (void) sizeToFitContent
{
NSString *stringValue = [self stringValue];
if (stringValue == nil || [stringValue length] == 0)
{
[self setStringValue: @"Nicola"];
[self sizeToFit];
[self setStringValue: @""];
}
else
{
[self sizeToFit];
}
}
@end
@implementation NSMatrix (sizeToContent)
- (void) sizeToFitContent
{
/* Unbelievable how it may be, -sizeToFit or -sizeToCells on Apple
* don't seem to compute the cell size. This is taken from code I
* originally wrote for gnustep-gui. */
NSSize newSize = NSZeroSize;
int numRows = [self numberOfRows];
int numCols = [self numberOfColumns];
int i, j;
for (i = 0; i < numRows; i++)
{
for (j = 0; j < numCols; j++)
{
NSCell *cell = [self cellAtRow: i column: j];
if (cell != nil)
{
NSSize tempSize = [cell cellSize];
if (tempSize.width > newSize.width)
{
newSize.width = tempSize.width;
}
if (tempSize.height > newSize.height)
{
newSize.height = tempSize.height;
}
}
}
}
[self setCellSize: newSize];
[self sizeToCells];
}
@end
/* NSImageView does not support sizeToFit on Apple! The following
* hack only works after the image has just been set. */
#ifndef GNUSTEP
@implementation NSImageView (sizeToContent)
- (void) sizeToFitContent
{
[self setFrameSize: [[self image] size]];
}
@end
#endif
/* NSColorWell 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 NSColorWell (sizeToContent)
- (void) sizeToFitContent
{
[self setFrameSize: [self minimumSizeForContent]];
}
- (NSSize) minimumSizeForContent
{
return NSMakeSize (52, 30);
}
@end
|