
#import "../Alexandra.h"
#import "PMArticleText.h"
#import <dbkit/DBTableView.h>

@implementation PreferencesManager(ArticleText)

//-----------------------------------------------------------
// FONT STUFF
//-----------------------------------------------------------

- showFont:sender;
	{
	char	nameAndSize[150];
	
	sprintf(nameAndSize,"%s %.0fpt",[font name],[font pointSize]);
	[aFontField setFont:font];
	[aFontField setStringValue:nameAndSize];
	return self;
	}


- openFontPanel:sender;
	{
	[[aFontField window] makeFirstResponder:aFontField];
	[[FontPanel new] panelConvertFont:[aFontField font]];
	[[FontManager new] orderFrontFontPanel:sender];	
	return self;
	}


- changeFont:sender;
	{
	font=[[FontManager new] convertFont:font];
	[self showFont:self];
	[thePanel setDocEdited:YES];
	return self;
    }



//-----------------------------------------------------------
// DB TABLE VIEW 
//-----------------------------------------------------------

// Achtung diese Methode kann von beiden MiscTableControllern aufgerufen werden.
// Macht aber nichts weiter, dass diese Methode nur (oder gerade) in dieser
// Katergorie steht.

- miscTableController:sender valueDidChangeFor:identifier at:(unsigned int)position;
	{
	[self markDirty:sender];
	return self;
	}


//-----------------------------------------------------------
// HEADER SELECTION
//-----------------------------------------------------------

/*	
	if(strpbrk([hName stringValue],":\t ")!=NULL){
	   NXRunAlertPanel("Alexandra","Not a valid header.",NULL,NULL,NULL);
		return self;
	}
*/	


//-----------------------------------------------------------
// READ/WRITE 
//-----------------------------------------------------------

- readArticleTextPrefs
	{
	char		*headerList,*header;
	Font		*newFont;
	float		fSize;
	const char	*fontNameSize;
	char		fName[150];
	int			i;
	MiscList	*rows;
	
	[aMode setState:[NXApp defaultIntValue:DEFAULT_HEADER_MODE]==NEWSPAPER_HEADER];
	[aFancyBody setState: [NXApp defaultBoolValue:DEFAULT_REWRAP_ARTICLE_TEXT]];
	[aPostscriptInline setState:[NXApp defaultBoolValue:DEFAULT_ALLOW_PS_INLINE]];

	EM_DURING
		rows=[[MiscList alloc] init];
		if([NXApp defaultIntValue:DEFAULT_HEADER_MODE]==FULL_HEADER)
			[rows addObject:[[KVPair alloc] initKey:"all" value:""]];
		else
			{
			headerList=(char *)[NXApp defaultValue:DEFAULT_HEADER_FILTER];
			EM_CONDERROR(!headerList,EPREFHeaderPrefMissing,NULL,NULL);
			headerList=NXCopyStringBufferFromZone(headerList,[self zone]);
	
			for(header=strtok(headerList,":");header!=NULL;header=strtok(NULL,":"))
				[rows addObject:[[KVPair alloc] initKey:header value:""]];
			NXZoneFree([self zone],headerList);
			}
		
		for(i=[rows count];i<20;i++)
			[rows addObject:[[KVPair alloc] init]];
		
		[aHeaderController empty:nil];
		[aHeaderController addRowsFrom:rows];
	EM_EMPTYHANDLER

	rows=[rows free];	

	EM_DURING
		fontNameSize=[NXApp defaultValue:DEFAULT_ARTICLE_FONT];
		EM_CONDERROR(!fontNameSize,EPREFFontPrefMissing,NULL,NULL);
		sscanf(fontNameSize,"%s %f",fName,&fSize);
		EM_CONDERROR(fSize<8,EPREFFontTooSmall,(void *)8,NULL);
		newFont=[Font newFont:fName size:fSize];
		EM_CONDERROR(!newFont,EPREFFontNotFound,fName,NULL);
		font=newFont;
		[self showFont:self];
	EM_EMPTYHANDLER

	return self;	
	}


- writeArticleTextPrefs;
	{
	MiscList *pairList=[aHeaderController rows];
	char	*p,headerList[1000]; // ## HARDCODED LIMIT!
	const char *header;
	KVPair	*pair;
	int		hmode;

	[aHeaders endEditing];
	
	hmode=NO_HEADER;
	*headerList='\0';
	p=headerList;
	for(pair=[pairList setFirstObject]; pair; pair=[pairList setNextObject])
		if(![pair isNull])
			{
			header=[pair key];
			hmode=SMALL_HEADER;
			if(!strcasecmp(header,"all"))
				{
				strcpy(headerList,"all");
				hmode=FULL_HEADER;
				break;
				}
			sprintf(p,"%s:", header);
			p+=strlen(header)+1;
			}

	if([aMode state])
		hmode=NEWSPAPER_HEADER;

	[NXApp setDefault:DEFAULT_HEADER_MODE toInt:hmode];
	[NXApp setDefault:DEFAULT_HEADER_FILTER to:headerList];
	[NXApp setDefault:DEFAULT_REWRAP_ARTICLE_TEXT toBool:[aFancyBody state]];
	[NXApp setDefault:DEFAULT_ALLOW_PS_INLINE toBool:[aPostscriptInline state]];
	[NXApp setDefault:DEFAULT_ARTICLE_FONT to:[aFontField stringValue]];

	return self;
	}


//-----------------------------------------------------------
// THAT'S IT
//-----------------------------------------------------------

@end
