libs-gui/Source/NSTableColumn.m
Nicola Pero fcd1bbc9cf Fixed dataCellForRow:
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@10806 72102866-910b-0410-8b05-ffd578937521
2001-08-30 09:31:27 +00:00

280 lines
5.4 KiB
Objective-C

/*
NSTableColumn.m
Copyright (C) 1999 Free Software Foundation, Inc.
Author: Michael Hanni <mhanni@sprintmail.com>
Date: 1999
First Implementation.
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: December 1999
Completely Rewritten.
This file is part of the GNUstep GUI Library.
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 <Foundation/NSNotification.h>
#include <AppKit/NSTableHeaderCell.h>
#include <AppKit/NSTableColumn.h>
#include <AppKit/NSTableView.h>
@implementation NSTableColumn
/*
*
* Class methods
*
*/
+ (void) initialize
{
if (self == [NSTableColumn class])
[self setVersion: 1];
}
/*
*
* Instance methods
*
*/
/*
* Initializing an NSTableColumn instance
*/
- (NSTableColumn*)initWithIdentifier: (id)anObject
{
self = [super init];
_width = 0;
_min_width = 0;
_max_width = 100000;
_is_resizable = YES;
_is_editable = YES;
_tableView = nil;
_headerCell = [NSTableHeaderCell new];
_dataCell = [NSTextFieldCell new];
ASSIGN (_identifier, anObject);
return self;
}
- (void)dealloc
{
RELEASE (_headerCell);
RELEASE (_dataCell);
TEST_RELEASE (_identifier);
[super dealloc];
}
/*
* Managing the Identifier
*/
- (void)setIdentifier: (id)anObject
{
ASSIGN (_identifier, anObject);
}
- (id)identifier
{
return _identifier;
}
/*
* Setting the NSTableView
*/
- (void)setTableView: (NSTableView*)aTableView
{
// We do *not* RETAIN aTableView.
// On the contrary, aTableView is supposed to RETAIN us.
_tableView = aTableView;
}
- (NSTableView *)tableView
{
return _tableView;
}
/*
* Controlling size
*/
- (void)setWidth: (float)newWidth
{
float oldWidth = _width;
if (newWidth > _max_width)
newWidth = _max_width;
else if (newWidth < _min_width)
newWidth = _min_width;
if (_width == newWidth)
return;
_width = newWidth;
if (_tableView)
{
// Tiling also marks it as needing redisplay
[_tableView tile];
[[NSNotificationCenter defaultCenter]
postNotificationName: NSTableViewColumnDidResizeNotification
object: _tableView
userInfo: [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat: oldWidth],
@"NSOldWidth", nil]];
}
}
- (float)width
{
return _width;
}
- (void)setMinWidth: (float)minWidth
{
_min_width = minWidth;
if (_width < _min_width)
[self setWidth: _min_width];
}
- (float)minWidth
{
return _min_width;
}
- (void)setMaxWidth: (float)maxWidth
{
_max_width = maxWidth;
if (_width > _max_width)
[self setWidth: _max_width];
}
- (float)maxWidth
{
return _max_width;
}
- (void)setResizable: (BOOL)flag
{
_is_resizable = flag;
}
- (BOOL)isResizable
{
return _is_resizable;
}
- (void)sizeToFit
{
float new_width;
new_width = [_headerCell cellSize].width;
if (new_width > _max_width)
_max_width = new_width;
if (new_width < _min_width)
_min_width = new_width;
// For easier subclassing we dont do it directly
[self setWidth: new_width];
}
/*
* Controlling editability
*/
- (void)setEditable: (BOOL)flag
{
_is_editable = flag;
}
- (BOOL)isEditable
{
return _is_editable;
}
/*
* Setting component cells
*/
- (void)setHeaderCell: (NSCell*)aCell
{
if (aCell == nil)
{
NSLog (@"Attempt to set a nil headerCell for NSTableColumn");
return;
}
ASSIGN (_headerCell, aCell);
}
- (NSCell*)headerCell
{
return _headerCell;
}
- (void)setDataCell: (NSCell*)aCell
{
if (aCell == nil)
{
NSLog (@"Attempt to set a nil dataCell for NSTableColumn");
return;
}
ASSIGN (_dataCell, aCell);
}
- (NSCell*)dataCell
{
return _dataCell;
}
- (NSCell*)dataCellForRow: (int)row
{
return [self dataCell];
}
/*
* Encoding/Decoding
*/
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[aCoder encodeObject: _identifier];
[aCoder encodeObject: _headerCell];
[aCoder encodeObject: _dataCell];
[aCoder encodeValueOfObjCType: @encode(float) at: &_width];
[aCoder encodeValueOfObjCType: @encode(float) at: &_min_width];
[aCoder encodeValueOfObjCType: @encode(float) at: &_max_width];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_resizable];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
self = [super init];
_identifier = RETAIN([aDecoder decodeObject]);
_headerCell = RETAIN([aDecoder decodeObject]);
_dataCell = RETAIN([aDecoder decodeObject]);
[aDecoder decodeValueOfObjCType: @encode(float) at: &_width];
[aDecoder decodeValueOfObjCType: @encode(float) at: &_min_width];
[aDecoder decodeValueOfObjCType: @encode(float) at: &_max_width];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_resizable];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
return self;
}
@end