Rewritten drawing code to allow editing, minor optimizations.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5186 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
nico 1999-11-15 14:28:27 +00:00
parent 432c3244c8
commit 11203b2f3c

View file

@ -3,10 +3,12 @@
The cell class for the NSForm control The cell class for the NSForm control
Copyright (C) 1996 Free Software Foundation, Inc. Copyright (C) 1996, 1999 Free Software Foundation, Inc.
Author: Ovidiu Predescu <ovidiu@net-community.com> Author: Ovidiu Predescu <ovidiu@net-community.com>
Date: March 1997 Date: March 1997
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: November 1999
This file is part of the GNUstep GUI Library. This file is part of the GNUstep GUI Library.
@ -27,97 +29,133 @@
*/ */
#include <gnustep/gui/config.h> #include <gnustep/gui/config.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSFormCell.h> #include <AppKit/NSFormCell.h>
#include <AppKit/NSFont.h> #include <AppKit/NSFont.h>
#include <AppKit/NSGraphics.h>
#include <AppKit/NSTextFieldCell.h> #include <AppKit/NSTextFieldCell.h>
static NSColor *shadowCol;
@interface NSFormCell (PrivateColor)
+ (void) _systemColorsChanged: (NSNotification*)n;
@end
@implementation NSFormCell (PrivateColor)
+ (void) _systemColorsChanged: (NSNotification*)n
{
ASSIGN(shadowCol, [NSColor controlDarkShadowColor]);
}
@end
@implementation NSFormCell @implementation NSFormCell
+ (void) initialize
{
if (self == [NSFormCell class])
{
[self setVersion: 1];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_systemColorsChanged:)
name: NSSystemColorsDidChangeNotification
object: nil];
[self _systemColorsChanged: nil];
}
}
/* The title attributes are those inherited from the NSActionCell class. */ /* The title attributes are those inherited from the NSActionCell class. */
- init - init
{ {
return [self initTextCell:@"Field:"]; return [self initTextCell: @"Field:"];
} }
- initTextCell: (NSString *)aString - initTextCell: (NSString *)aString
{ {
self = [super initTextCell:@""]; self = [super initTextCell: @""];
[self setBezeled:YES]; [self setBezeled: YES];
[self setAlignment:NSLeftTextAlignment]; [self setAlignment: NSLeftTextAlignment];
titleWidth = -1; _titleCell = [[NSCell alloc] initTextCell: aString];
titleCell = [[NSCell alloc] initTextCell:aString]; [_titleCell setBordered: NO];
[titleCell setBordered:NO]; [_titleCell setBezeled: NO];
[titleCell setBezeled:NO]; [_titleCell setAlignment: NSRightTextAlignment];
[titleCell setAlignment:NSRightTextAlignment]; _autoTitleWidth = YES;
_titleWidth = [[self titleFont] widthOfString: aString];
return self; return self;
} }
- (void)dealloc - (void)dealloc
{ {
[titleCell release]; [_titleCell release];
[super dealloc]; [super dealloc];
} }
- (BOOL)isOpaque - (BOOL)isOpaque
{ {
return [super isOpaque] && [titleCell isOpaque]; return [super isOpaque] && [_titleCell isOpaque];
} }
- (void)setTitle:(NSString*)aString - (void)setTitle: (NSString*)aString
{ {
[titleCell setStringValue:aString]; [_titleCell setStringValue: aString];
if (_autoTitleWidth)
_titleWidth = [[self titleFont] widthOfString: aString];
} }
- (void)setTitleAlignment:(NSTextAlignment)mode - (void)setTitleAlignment:(NSTextAlignment)mode
{ {
[titleCell setAlignment:mode]; [_titleCell setAlignment: mode];
} }
- (void)setTitleFont:(NSFont*)fontObject - (void)setTitleFont: (NSFont*)fontObject
{ {
[titleCell setFont:fontObject]; [_titleCell setFont: fontObject];
} }
- (void)setTitleWidth:(float)width - (void)setTitleWidth: (float)width
{ {
titleWidth = width; if (_titleWidth >= 0)
{
_autoTitleWidth = NO;
_titleWidth = width;
}
else
{
_autoTitleWidth = YES;
_titleWidth = [[self titleFont] widthOfString: [self title]];
}
} }
- (NSString*)title - (NSString*)title
{ {
return [titleCell stringValue]; return [_titleCell stringValue];
} }
- (NSTextAlignment)titleAlignment - (NSTextAlignment)titleAlignment
{ {
return [titleCell alignment]; return [_titleCell alignment];
} }
- (NSFont*)titleFont - (NSFont*)titleFont
{ {
return [titleCell font]; return [_titleCell font];
} }
- (float)titleWidth - (float)titleWidth
{ {
if (titleWidth < 0) return _titleWidth;
return [[titleCell font] widthOfString:[self title]];
else
return titleWidth;
} }
- (float)titleWidth:(NSSize)size - (float)titleWidth: (NSSize)size
{ {
// TODO // Minor TODO -- what is this supposed to do?
return 0; return 0;
} }
- (NSSize)cellSize - (NSSize)cellSize
{ {
NSSize returnedSize; NSSize returnedSize;
NSSize titleSize = [titleCell cellSize]; NSSize titleSize = [_titleCell cellSize];
NSSize textSize = [super cellSize]; NSSize textSize;
textSize.width = [cell_font widthOfString: @"minimum"]; textSize.width = [cell_font widthOfString: @"minimum"];
textSize.height = [cell_font pointSize] + (2 * yDist) textSize.height = [cell_font pointSize] + (2 * yDist)
@ -132,26 +170,72 @@
return returnedSize; return returnedSize;
} }
- (NSRect) drawingRectForBounds: (NSRect)theRect
{
theRect.origin.x += _titleWidth + 4;
theRect.size.width -= _titleWidth + 4;
return [super drawingRectForBounds: theRect];
}
- (void) drawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView - (void) drawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{ {
NSRect titleFrame; NSRect titleFrame = cellFrame;
NSRect textFrame; NSRect borderedFrame = cellFrame;
NSDivideRect(cellFrame, &titleFrame, &textFrame, // Save last view drawn to
[self titleWidth] + 4, NSMinXEdge); [self setControlView: controlView];
titleFrame.size.width -= 4;
// do nothing if cell's frame rect is zero
if (NSIsEmptyRect(cellFrame))
return;
[titleCell drawWithFrame: titleFrame inView: controlView]; //
[super drawWithFrame: textFrame inView: controlView]; // Draw title
//
titleFrame.size.width = _titleWidth;
[_titleCell drawWithFrame: titleFrame inView: controlView];
//
// Leave unfilled the space between titlecell and editable text.
//
//
// Draw border
//
borderedFrame.origin.x += _titleWidth + 4;
borderedFrame.size.width -= _titleWidth + 4;
if (NSIsEmptyRect(borderedFrame))
return;
[controlView lockFocus];
if ([self isBordered])
{
[shadowCol set];
NSFrameRect(borderedFrame);
}
else if ([self isBezeled])
{
NSDrawWhiteBezel(borderedFrame, NSZeroRect);
}
[controlView unlockFocus];
//
// Draw interior
//
[self drawInteriorWithFrame: cellFrame inView: controlView];
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
// TODO
[super encodeWithCoder: aCoder]; [super encodeWithCoder: aCoder];
} }
- (id) initWithCoder: (NSCoder*)aDecoder - (id) initWithCoder: (NSCoder*)aDecoder
{ {
// TODO
[super initWithCoder: aDecoder]; [super initWithCoder: aDecoder];
return self; return self;