
#import "MColCell.h"

typedef	struct	_Column
{
	unsigned short	offset;
	BOOL			copy;
	const char		*value;
} Column;
#define	Column_desc		"SC*"

@implementation MColCell

- init
{
	if (![super init])
		return nil;
	[self initMColCell];

	return self;
}

- initTextCell:(const char *)string
{
	Column	col;

	if (![super initTextCell:string])
		return nil;
	[self initMColCell];
	col.offset = 0;
	col.copy = YES;
	if (string)
		col.value = NXCopyStringBuffer(string);
	else
		col.value = NULL;
	[stringValues addElement:&col];

	return self;
}

- initIconCell:(const char *)string
{
	if (![super initIconCell:string])
		return nil;
	[self initMColCell];

	return self;
}

- initMColCell
{
	stringValues = [[Storage alloc] initCount:0 elementSize:sizeof(Column)
		description:Column_desc];
	return self;
}

- free
{
	int		count = [stringValues count];
	int		ind;
	Column	*col;

	for (ind=0; ind<count; ind++)
	{
		col = [stringValues elementAt:ind];
		if (col->copy)
			free((char *)col->value);
	}
	[stringValues free];

	return [super free];
}

- setStringValue:(const char *)string at:(int)col copy:(BOOL)copy
{
	int		count = [stringValues count];
	Column	*column;

	if (col>=count || col<0)
		return nil;
	column = [stringValues elementAt:col];
	if (column->copy)
		free((char *)column->value);
	if (copy && string!=NULL)
		column->value = NXCopyStringBuffer(string);
	else
		column->value = string;
	column->copy = copy;
		col = count;

	return self;
}

- (const char *)stringValueAt:(int)col
{
	int		count = [stringValues count];
	Column	*column;

	if (col>=count || col<0)
		return NULL;
	column = [stringValues elementAt:col];
	return column->value;
}

- insertColAt:(int)col offset:(int)offset
{
	int		count = [stringValues count];
	Column	column;

	if (col > count)
		col = count;
	else if (col < 0)
		col = 0;
	column.offset = offset;
	column.copy = NO;
	column.value = NULL;
	[stringValues insertElement:&column at:col];

	return self;
}

- addColWithOffset:(int)offset
{
	return [self insertColAt:[stringValues count] offset:offset];
}

- removeColAt:(int)col
{
	int		count = [stringValues count];
	Column	*column;

	if (col>=count || col<0)
		return nil;
	column = [stringValues elementAt:col];
	if (column->copy)
		free((char *)column->value);
	[stringValues removeElementAt:col];

	return self;
}

- (int)colCount
{
	return [stringValues count];
}

- drawInside:(const NXRect *)cellFrame inView:controlView
{
	NXCoord		baseX,baseY;
	Column		*column;
	int			count = [stringValues count];
	int			ind;

	baseX = NX_X(cellFrame) + 4;
	baseY = NX_Y(cellFrame) + 12;

	PSsetgray((cFlags1.state || cFlags1.highlighted) ? NX_WHITE : NX_LTGRAY);
	NXRectFill(cellFrame);
	PSsetgray(NX_BLACK);
	[[support screenFont] set];

	for (ind=0; ind<count; ind++)
	{
		column = [stringValues elementAt:ind];
		if (column->value != NULL)
		{
			PSmoveto(baseX+column->offset,baseY);
			PSshow(column->value);
		}
	}

	return self;
}

- copyFromZone:(NXZone *)zone
{
	MColCell	*copy;
	int			count;
	int			ind;
	Column		*column;

	copy = [super copyFromZone:zone];
	copy->stringValues = [stringValues copyFromZone:zone];

	count = [copy->stringValues count];
	for (ind=0; ind<count; ind++)
	{
		column = [copy->stringValues elementAt:ind];
		if (column->copy && column->value)
			column->value = NXCopyStringBuffer(column->value);
	}

	return copy;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	stringValues = NXReadObject(stream);
	return self;
}

- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteObject(stream,stringValues);
	return self;
}

@end
