From a0ccec39d17a4da3d3d50c02c407c80d4cf8e648 Mon Sep 17 00:00:00 2001 From: scottc Date: Mon, 20 Oct 1997 22:01:25 +0000 Subject: [PATCH] Fill out NSBox implementation. Minor bug fixes. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@2536 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 17 +- Headers/gnustep/gui/NSCell.h | 10 ++ Source/NSBox.m | 302 +++++++++++++++++++++++++++++++++-- Source/NSCell.m | 13 ++ Source/NSTextField.m | 10 +- Source/NSTextFieldCell.m | 31 ++++ Source/NSView.m | 2 +- 7 files changed, 372 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index ee49d996e..27565de3d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,19 @@ -Thu Oct 16 12:28:00 1997 Scott Christley +Mon Oct 20 14:32:55 1997 Scott Christley + + * Fill out NSBox implementation. + * Headers/gnustep/gui/NSCell.h (+sizeForBorderType:): New method. + * Source/NSCell.m (+sizeForBorderType:): New method. + * Source/NSBox.m: Fill out implementation. + + * Source/NSTextField.m (-initWithFrame:,-setTextCursor:,-dealloc): + Correct retain/release behavior of cursor. + + * Source/NSTextFieldCell.m (-cellSize): Implement. + + * Source/NSView.m (-removeTrackingRect:): Comment out release + of object as we haven't retained it. + +Thu Oct 16 12:28:00 1997 Scott Christley * config.mak.in: Delete. * Headers/gnustep/gui/NSScrollView.h: Correct datatype. diff --git a/Headers/gnustep/gui/NSCell.h b/Headers/gnustep/gui/NSCell.h index f4320f232..e75ef8725 100644 --- a/Headers/gnustep/gui/NSCell.h +++ b/Headers/gnustep/gui/NSCell.h @@ -328,5 +328,15 @@ enum { @end +// +// Methods the backend should implement +// +@interface NSCell (GNUstepBackend) + +// Returns the size of a border ++ (NSSize)sizeForBorderType:(NSBorderType)aType; + +@end + #endif // _GNUstep_H_NSCell diff --git a/Source/NSBox.m b/Source/NSBox.m index 8554aff91..91c896daa 100644 --- a/Source/NSBox.m +++ b/Source/NSBox.m @@ -33,6 +33,10 @@ #include #include +@interface NSBox (Private) +- (NSRect)calcSizes; +@end + @implementation NSBox // @@ -55,6 +59,11 @@ [super initWithFrame:frameRect]; cell = [[NSTextFieldCell alloc] initTextCell:@"Title"]; + [cell setAlignment: NSCenterTextAlignment]; + [cell setBordered: NO]; + [cell setEditable: NO]; + [cell setDrawsBackground: YES]; + [cell setBackgroundColor: [window backgroundColor]]; offsets.width = 5; offsets.height = 5; border_rect = bounds; @@ -88,22 +97,36 @@ - (void)setBorderType:(NSBorderType)aType { - border_type = aType; + if (border_type != aType) + { + border_type = aType; + [content_view setFrame: [self calcSizes]]; + [self setNeedsDisplay: YES]; + } } - (void)setTitle:(NSString *)aString { [cell setStringValue:aString]; + [content_view setFrame: [self calcSizes]]; + [self setNeedsDisplay: YES]; } - (void)setTitleFont:(NSFont *)fontObj { [cell setFont:fontObj]; + [content_view setFrame: [self calcSizes]]; + [self setNeedsDisplay: YES]; } - (void)setTitlePosition:(NSTitlePosition)aPosition { - title_position = aPosition; + if (title_position != aPosition) + { + title_position = aPosition; + [content_view setFrame: [self calcSizes]]; + [self setNeedsDisplay: YES]; + } } - (NSString *)title @@ -146,8 +169,6 @@ - (void)setContentView:(NSView *)aView { - NSRect r; - if (content_view) { // Tell view that it is no longer in a window @@ -168,16 +189,14 @@ [content_view setSuperview:self]; [content_view setNextResponder:self]; [content_view viewWillMoveToWindow:window]; - r.origin.x = bounds.origin.x + offsets.width; - r.origin.y = bounds.origin.y + offsets.height; - r.size.width = bounds.size.width - (2 * offsets.width); - r.size.height = bounds.size.height - (2 * offsets.height); - [content_view setFrame:r]; + [content_view setFrame: [self calcSizes]]; } - (void)setContentViewMargins:(NSSize)offsetSize { offsets = offsetSize; + [content_view setFrame: [self calcSizes]]; + [self setNeedsDisplay: YES]; } // @@ -185,10 +204,38 @@ // - (void)setFrameFromContentFrame:(NSRect)contentFrame { + // First calc the sizes to see how much we are off by + NSRect r = [self calcSizes]; + NSRect f = [self frame]; + + // Add the difference to the frame + f.size.width = f.size.width + (contentFrame.size.width - r.size.width); + f.size.height = f.size.height + (contentFrame.size.height - r.size.height); + + [self setFrame: f]; } - (void)sizeToFit -{} +{ + NSRect r = NSZeroRect; + id o, e = [[content_view subviews] objectEnumerator]; + + // Loop through subviews and calculate rect to encompass all + while ((o = [e nextObject])) + { + NSRect f = [o frame]; + if (f.origin.x < r.origin.x) + r.origin.x = f.origin.x; + if (f.origin.y < f.origin.y) + r.origin.y = f.origin.y; + if ((f.origin.x + f.size.width) > (r.origin.x + r.size.width)) + r.size.width = (f.origin.x + f.size.width) - r.origin.x; + if ((f.origin.y + f.size.height) > (r.origin.y + r.size.height)) + r.size.height = (f.origin.y + f.size.height) - r.origin.y; + } + + [self setFrameFromContentFrame: r]; +} // // Managing the NSView Hierarchy @@ -239,3 +286,238 @@ } @end + +@implementation NSBox (Private) + +- (NSRect)calcSizes +{ + NSRect r = NSZeroRect; + + switch (title_position) + { + case NSNoTitle: + { + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + border_rect = bounds; + title_rect = NSZeroRect; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + break; + } + case NSAboveTop: + { + NSSize titleSize = [cell cellSize]; + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + float c; + + // Add spacer around title + titleSize.width += 1; + titleSize.height += 1; + + // Adjust border rect by title cell + border_rect = bounds; + border_rect.size.height -= titleSize.height + borderSize.height; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + // center the title cell + c = (bounds.size.width - titleSize.width) / 2; + if (c < 0) c = 0; + title_rect.origin.x = bounds.origin.x + c; + title_rect.origin.y = bounds.origin.y + border_rect.size.height + + borderSize.height; + title_rect.size = titleSize; + + break; + } + case NSBelowTop: + { + NSSize titleSize = [cell cellSize]; + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + float c; + + // Add spacer around title + titleSize.width += 1; + titleSize.height += 1; + + // Adjust border rect by title cell + border_rect = bounds; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + // Adjust by the title size + r.size.height -= titleSize.height + borderSize.height; + + // center the title cell + c = (border_rect.size.width - titleSize.width) / 2; + if (c < 0) c = 0; + title_rect.origin.x = border_rect.origin.x + c; + title_rect.origin.y = border_rect.origin.y + border_rect.size.height + - titleSize.height - borderSize.height; + title_rect.size = titleSize; + + break; + } + case NSAtTop: + { + NSSize titleSize = [cell cellSize]; + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + float c; + + // Add spacer around title + titleSize.width += 1; + titleSize.height += 1; + + border_rect = bounds; + + // Adjust by the title size + border_rect.size.height -= titleSize.height / 2; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + // Adjust by the title size + r.size.height -= (titleSize.height / 2) + borderSize.height; + + // center the title cell + c = (border_rect.size.width - titleSize.width) / 2; + if (c < 0) c = 0; + title_rect.origin.x = border_rect.origin.x + c; + title_rect.origin.y = border_rect.origin.y + border_rect.size.height + - (titleSize.height / 2); + title_rect.size = titleSize; + + break; + } + case NSAtBottom: + { + NSSize titleSize = [cell cellSize]; + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + float c; + + // Add spacer around title + titleSize.width += 1; + titleSize.height += 1; + + border_rect = bounds; + + // Adjust by the title size + border_rect.origin.y += titleSize.height / 2; + border_rect.size.height -= titleSize.height / 2; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + // Adjust by the title size + r.origin.y += (titleSize.height / 2) + borderSize.height; + r.size.height -= (titleSize.height / 2) + borderSize.height; + + // center the title cell + c = (border_rect.size.width - titleSize.width) / 2; + if (c < 0) c = 0; + title_rect.origin.x = c; + title_rect.origin.y = 0; + title_rect.size = titleSize; + + break; + } + case NSBelowBottom: + { + NSSize titleSize = [cell cellSize]; + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + float c; + + // Add spacer around title + titleSize.width += 1; + titleSize.height += 1; + + // Adjust by the title + border_rect = bounds; + border_rect.origin.y += titleSize.height + borderSize.height; + border_rect.size.height -= titleSize.height + borderSize.height; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + // center the title cell + c = (border_rect.size.width - titleSize.width) / 2; + if (c < 0) c = 0; + title_rect.origin.x = c; + title_rect.origin.y = 0; + title_rect.size = titleSize; + + break; + } + case NSAboveBottom: + { + NSSize titleSize = [cell cellSize]; + NSSize borderSize = [NSCell sizeForBorderType: border_type]; + float c; + + // Add spacer around title + titleSize.width += 1; + titleSize.height += 1; + + border_rect = bounds; + + // Add the offsets to the border rect + r.origin.x = border_rect.origin.x + offsets.width + borderSize.width; + r.origin.y = border_rect.origin.y + offsets.height + borderSize.height; + r.size.width = border_rect.size.width - (2 * offsets.width) + - (2 * borderSize.width); + r.size.height = border_rect.size.height - (2 * offsets.height) + - (2 * borderSize.height); + + // Adjust by the title size + r.origin.y += titleSize.height + borderSize.height; + r.size.height -= titleSize.height + borderSize.height; + + // center the title cell + c = (border_rect.size.width - titleSize.width) / 2; + if (c < 0) c = 0; + title_rect.origin.x = border_rect.origin.x + c; + title_rect.origin.y = border_rect.origin.y + borderSize.height; + title_rect.size = titleSize; + + break; + } + } + + return r; +} + +@end diff --git a/Source/NSCell.m b/Source/NSCell.m index 3824cbcd1..e0c23222a 100644 --- a/Source/NSCell.m +++ b/Source/NSCell.m @@ -883,3 +883,16 @@ } @end + +// +// Methods the backend should implement +// +@implementation NSCell (GNUstepBackend) + +// Returns the size of a border ++ (NSSize)sizeForBorderType:(NSBorderType)aType +{ + return NSZeroSize; +} + +@end diff --git a/Source/NSTextField.m b/Source/NSTextField.m index 897427da6..42e731025 100644 --- a/Source/NSTextField.m +++ b/Source/NSTextField.m @@ -84,16 +84,24 @@ id gnustep_gui_nstextfield_cell_class = nil; // set our cell [self setCell:[[gnustep_gui_nstextfield_cell_class new] autorelease]]; [cell setState:1]; - text_cursor = [NSCursor IBeamCursor]; + text_cursor = [[NSCursor IBeamCursor] retain]; return self; } +- (void)dealloc +{ + [text_cursor release]; + [super dealloc]; +} + // // Creating copies // - (void)setTextCursor:(NSCursor *)aCursor { + [aCursor retain]; + [text_cursor release]; text_cursor = aCursor; } diff --git a/Source/NSTextFieldCell.m b/Source/NSTextFieldCell.m index 656661b68..b3284cf57 100644 --- a/Source/NSTextFieldCell.m +++ b/Source/NSTextFieldCell.m @@ -103,6 +103,37 @@ return c; } +// +// Determining Component Sizes +// +- (NSSize)cellSize +{ + NSFont *f; + NSSize borderSize, s; + + // Get border size + if ([self isBordered]) + { + if ([self isBezeled]) + borderSize = [NSCell sizeForBorderType: NSBezelBorder]; + else + borderSize = [NSCell sizeForBorderType: NSLineBorder]; + } + else + borderSize = [NSCell sizeForBorderType: NSNoBorder]; + + // Get size of text with a little buffer space + f = [self font]; + s = NSMakeSize([f widthOfString: [self stringValue]] + 2, + [f pointSize] + 2); + + // Add in border size + s.width += 2 * borderSize.width; + s.height += 2 * borderSize.height; + + return s; +} + // // Modifying Graphic Attributes // diff --git a/Source/NSView.m b/Source/NSView.m index 0544a6ff3..004ead5d2 100644 --- a/Source/NSView.m +++ b/Source/NSView.m @@ -1125,7 +1125,7 @@ static NSRecursiveLock *gnustep_gui_nsview_lock = nil; m = (TrackingRectangle *)[tracking_rects objectAtIndex:i]; if ([m tag] == tag) { - [m release]; + //[m release]; [tracking_rects removeObjectAtIndex:i]; return; }