ftp.nice.ch/Attic/openStep/developer/resources/OICTableView.1.0.NIS.bs.tgz#/OICTableView/Source/Framework/OICInterface/OICNSTableHeaderCell.m

This is OICNSTableHeaderCell.m in view mode; [Download] [Up]

//************************************************************************
//
//	OICNSTableHeaderCell.m
//
//	by Juergen Moellenhoff <jm@oic.de>		
//
//
//	This code is supplied "as is" the author makes no warranty as to its 
//	suitability for any purpose.  This code is free and may be distributed 
//	in accordance with the terms of the:
//		
//			GNU GENERAL PUBLIC LICENSE
//			Version 2, June 1991
//			copyright (C) 1989, 1991 Free Software Foundation, Inc.
// 			675 Mass Ave, Cambridge, MA 02139, USA
//
//************************************************************************

#import "OICNSTableHeaderCell.h"

#import	<AppKit/AppKit.h>

static NSString	*OICHigh =		@"OICHigh";
static NSString	*OICLow =		@"OICLow";
static NSString	*OICNoOrder =	@"OICNoOrder";

#define	OICNSTABLEHEADERCELL_CURRENT_VERSION	3

@implementation OICNSTableHeaderCell

//
// Private Methods
//
- _createImageCell
{
	NSButtonCell		*cell;

	cell = [[NSButtonCell alloc] initImageCell:[NSImage imageNamed:OICNoOrder]];
	[cell setBordered:YES];
	[cell setImagePosition:NSImageOnly];
	[cell setButtonType:NSMomentaryPushButton];

	return [cell autorelease];
}

- (void)_finishInitializing
{
	NSString	*pathForResource;
	NSBundle	*bundle;	
	NSImage		*image;

	bundle = [NSBundle bundleForClass:[self class]];

	if ([NSImage imageNamed:OICHigh] == nil)
	{
		pathForResource = [bundle pathForResource:OICHigh ofType:@"tiff"];
		image = [[NSImage alloc] initByReferencingFile:pathForResource];
		[image setName:OICHigh];
	}

	if ([NSImage imageNamed:OICLow] == nil)
	{
		pathForResource = [bundle pathForResource:OICLow ofType:@"tiff"];
		image = [[NSImage alloc] initByReferencingFile:pathForResource];
		[image setName:OICLow];
	}

	if ([NSImage imageNamed:OICNoOrder] == nil)
	{
		pathForResource = [bundle pathForResource:OICNoOrder ofType:@"tiff"];
		image = [[NSImage alloc] initByReferencingFile:pathForResource];
		[image setName:OICNoOrder];
	}
}

//
// Public methods
//

//
// This bumps the class version so that we can compatibly read
// old objects out of an archive.
//
+ (void)initialize
{
	if (self == [OICNSTableHeaderCell class])
		[OICNSTableHeaderCell setVersion:OICNSTABLEHEADERCELL_CURRENT_VERSION];
}

//
// Init
//
- initTextCell:(NSString *)aString
{
	[super initTextCell:aString];

	// Set the ButtonCell for the image
	[self setOrderingCell:[self _createImageCell]];
	
	// finish initializing :-)
	[self _finishInitializing];

	// Set the default sort order
	[self setOrdering:NSOrderedSame];
	
	return self;
}

//
// Destroy
//
- (void)dealloc
{
	if (_orderingCell != nil)
	{
		[_orderingCell release];
		_orderingCell = nil;
	}

	[super dealloc]; 
}

//
// NSCoding protocol
//
- (id)initWithCoder:(NSCoder *)aDecoder
{
	unsigned			version;
	NSComparisonResult	tempOrdering;

	[super initWithCoder:aDecoder];

	version = [aDecoder versionForClassName:@"OICNSTableHeaderCell"];	
	if (version < OICNSTABLEHEADERCELL_CURRENT_VERSION)
	{
		id dummy;
	
		if (version == 2)
		{	
			// read code for old version
			dummy = [aDecoder decodeObject];
			[aDecoder decodeValueOfObjCType:@encode(typeof(tempOrdering)) at:&tempOrdering];
		}
		else
		{	
			// read code for old version
			dummy = [aDecoder decodeObject];
			tempOrdering = NSOrderedSame;
		}
	}
	else
	{
		// read code for current version
		[aDecoder decodeValueOfObjCType:@encode(typeof(tempOrdering)) at:&tempOrdering];
	}

	// Set the ButtonCell for the image
	[self setOrderingCell:[self _createImageCell]];
	
	// finish initializing :-)
	[self _finishInitializing];

	// Set the sort order
	[self setOrdering:tempOrdering];

	return self;
}

//
// NSCoding protocol
//
- (void)encodeWithCoder:(NSCoder *)aCoder
{
	[super encodeWithCoder:aCoder];

	// It is not really necessary to encode this variable, 
	// but maybe I need it later for something... who knows...
	[aCoder encodeValueOfObjCType:@encode(typeof(_ordering)) at:&_ordering];
}

//
// NSCopying protocol
//
- copyWithZone:(NSZone *)z
{
	OICNSTableHeaderCell	*theCopy;
	
	theCopy = [super copyWithZone:z];

	// Important!! I must clear the
	// variable, before I can use it.
	theCopy->_orderingCell = nil;

	// Now I can set the value.
	[theCopy setOrderingCell:[self _createImageCell]];
	[theCopy setOrdering:[self ordering]];

	return theCopy;
}

//
// Display
//
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
	NSRect	aNewRect;
	
	aNewRect = cellFrame;
	aNewRect.size.width -= aNewRect.size.height;
	
	[super drawInteriorWithFrame:aNewRect inView:controlView];

	aNewRect.origin.x = aNewRect.origin.x + aNewRect.size.width + 1.0;
	aNewRect.size.width = aNewRect.size.height;
	
	// Draw the ordering image
	[[self orderingCell] drawWithFrame:aNewRect inView:controlView];
}

//
// Get/Set methods
//

- (void)setOrderingCell:theCell
{
	if (_orderingCell == theCell)
		return;

	[_orderingCell release];
	_orderingCell = nil;

	_orderingCell = [theCell retain];
}	

- orderingCell
{
	return _orderingCell;
}

- (void)setOrdering:(NSComparisonResult)newOrdering
{
	NSString	*imageName = nil;
	
	_ordering = newOrdering;
	switch(_ordering)
	{
	case NSOrderedAscending:
		imageName = OICHigh;
		break;

	case NSOrderedDescending:
		imageName = OICLow;
		break;

	case NSOrderedSame:
	default:
		imageName = OICNoOrder;
		break;
	}
	
	[[self orderingCell] setImage:[NSImage imageNamed:imageName]];
}

- (NSComparisonResult)ordering
{
	return _ordering;
}

@end

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.