/* 
 * RZTextToken - support object for the RZBrowserCell that represents
 * 	a piece of text with a particular font and color
 *
 * You may freely copy, distribute and reuse the code in this example.
 * This code is provided AS IS without warranty of any kind, expressed 
 * or implied, as to its fitness for any particular use.
 *
 * Copyright 1995 Ralph Zazula (rzazula@next.com).  All Rights Reserved.
 *
 */

#import "RZTextToken.h"
#import "RZSimpleString.h"
#import <appkit/NXImage.h>
#import <stdio.h>
#import <stdarg.h>

#define FONT_STYLE_NORM 0
#define FONT_STYLE_BOLD 1
#define FONT_STYLE_ITALIC 2
#define FONT_STYLE_BOLD_ITALIC 3

#define DEFAULT_FONT FONT_STYLE_NORM
#define DEFAULT_COLOR NX_BLACK

@implementation RZTextToken

- init
{
	return [self notImplemented:_cmd];
}

- initText:(const char *)format at:(unsigned)i,...
{
	va_list args;
	static char buf[1024];
	
	va_start(args, format);
	vsprintf(buf, format, args);
	
	return [self initText:buf at:i font:DEFAULT_FONT color:DEFAULT_COLOR];
}

- initText:(const char *)format at:(unsigned)i font:(char)font color:(char)color,...
{
	va_list args;
	static char buf[1024];
	
	va_start(args, format);
	vsprintf(buf, format, args);

	return [self initData:buf at:i isText:YES font:font color:color];
}

- initImage:(const char *)d at:(unsigned)i
{
	return [self initData:d at:i isText:NO font:0 color:0];
}

- initData:(const char *)d at:(unsigned)i isText:(BOOL)isText font:(char)font color:(char)color
{
	if(self = [super init]) {
		if(isText) {
			data = [[RZSimpleString alloc] initWith:d];
		} else {
			data = [NXImage findImageNamed:d];
			if(!data) {
				/* see if it's a filename */
				data = [[NXImage alloc] init];
				if(![data loadFromFile:d]) {
					data = [data free];
				}
			}
		}
		if(data) {
			index = i;
			_flags.text = isText;
			_flags.font = font;
			_flags.color = color;
		}
	}
	return self;
}

- (unsigned)position	{ return index; }
- data					{ return data; }
- (BOOL)isText			{ return _flags.text; }
- (char)font			{ return _flags.font; }
- (char)color			{ return _flags.color; }

- write:(NXTypedStream *)ts
{
	[super write:ts];
	NXWriteTypes(ts, "i@i", &index, &data, _flags);
	return self;
}

- read:(NXTypedStream *)ts
{
	[super read:ts];
	NXReadTypes(ts, "i@i", &index, &data, _flags);
	return self;
}

@end

@implementation RZTextToken(RZSortableObjects)
- (unsigned)sortIndex
{ 
	return index; 
}
@end
