/* 
   NSFontManager.m

   Manages system and user fonts

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author:  Scott Christley <scottc@net-community.com>
   Date:	1996
   
   This file is part of the mySTEP Library and is provided
   under the terms of the GNU Library General Public License.
*/ 

#import <Foundation/NSException.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSString.h>

#import <AppKit/NSFontManager.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSNibLoading.h>
#import <AppKit/NSFontPanel.h>
#import <AppKit/NSFont.h>


//
// Class variables
//
static NSFontManager *__sharedFontManager = nil;
static NSFontPanel *__fontPanel = nil;
static Class __fontManagerClass = Nil;
static Class __fontPanelClass = Nil;


@implementation NSFontManager

+ (void) initialize
{
	if (self == [NSFontManager class])
		{
		NSDebugLog(@"Initialize NSFontManager class\n");
		__fontManagerClass = self;
		}
}

+ (void) setFontManagerFactory:(Class)class		{ __fontManagerClass = class; }
+ (void) setFontPanelFactory:(Class)class		{ __fontPanelClass = class; }

+ (NSFontManager *) sharedFontManager
{
	if (!__sharedFontManager)
		__sharedFontManager = [__fontManagerClass new];

	return __sharedFontManager;
}

- (id) init
{
	if((self = [super init]))
		{
		fontsList = [NSMutableArray array];				// Allocate the font list
#if 1	// populate with some values...
		[(NSMutableArray *) fontsList addObject:@"Geneva"];
		[(NSMutableArray *) fontsList addObject:@"Helvetica"];
		[(NSMutableArray *) fontsList addObject:@"Times"];
		[(NSMutableArray *) fontsList addObject:@"X11"];
#endif
		}
	return self;
}

//
// Converting Fonts
//

- (NSFont *) convertFont:(NSFont *)fontObject		{ return fontObject; }

- (NSFont *) convertFont:(NSFont *)fontObject 
				toFamily:(NSString *)family
{
	NSFont *f = fontObject;

//	if ([family compare:[fontObject familyName]] != NSOrderedSame)
//		[(f = [fontObject mutableCopy]) setFamilyName:family];

	return f;
}

- (NSFont *) convertFont:(NSFont *)fontObject 
				  toFace:(NSString *)typeface
{
	return fontObject;					// FIX ME How to do this conversion?
}

- (NSFont *) convertFont:(NSFont *)fontObject 
			 toHaveTrait:(NSFontTraitMask)trait
{
	return nil;	
}

- (NSFont *) convertFont:(NSFont *)fontObject 
			 toNotHaveTrait:(NSFontTraitMask)trait
{
	return nil;	
}


- (NSFont *) convertFont:(NSFont *)fontObject toSize:(float)size
{
	NSFont *f = fontObject;

//	if ([fontObject pointSize] != size)
//		[(f = [fontObject mutableCopy]) setPointSize:size];

	return f;
}

- (NSFont *) convertWeight:(BOOL)upFlag ofFont:(NSFont *)fontObject
{
	return nil;	
}

- (NSFont *) fontWithFamily:(NSString *)family
					 traits:(NSFontTraitMask)traits
					 weight:(int)weight
					 size:(float)size
{
int i, j = [fontsList count];
BOOL found = NO;
NSString *name;
NSFont *f;

	for (i = 0; i < j; ++i)
		{						// Make sure it is a legitimate family name
		name = [fontsList objectAtIndex:i];
		if ([family compare:name] == NSOrderedSame)
			{
			found = YES;
			break;
			}
    	}

	if (found)												// Create the font
		{
		f = [[NSFont alloc] init];
//		[f setFamilyName: family];
//		[f setTraits: traits];
//		[f setWeight: weight];
//		[f setPointSize: size];	
	
		return f;
		}
	else
		{
		NSLog(@"Invalid font request\n");
		return nil;
		}
}

- (NSFontPanel *) fontPanel:(BOOL)create
{
	if(![NSFontPanel sharedFontPanelExists] && !create)
			return nil;
	return [NSFontPanel sharedFontPanel];	// create
}

// FIXME: ask a NSFontDescriptor for fonts

- (NSArray *) availableFonts
{
#if 1
	NSLog(@"NSFontManager availableFonts");
#endif
	// should read from system!
	return fontsList;
}

- (BOOL) isEnabled								{ return NO; }
- (BOOL) isMultiple								{ return NO; }
- (NSFont *) selectedFont						{ return selected_font; }
- (void) setEnabled:(BOOL)flag					{}
- (void) setFontMenu:(NSMenu *)newMenu			{ font_menu=newMenu; }
- (NSMenu *) fontMenu:(BOOL)create				{ return font_menu; }
- (int) weightOfFont:(NSFont *)fontObject		{ return 0; }

- (void) setSelectedFont:(NSFont *)fontObject isMultiple:(BOOL)flag
{
	selected_font = fontObject;
}

- (NSFontTraitMask) traitsOfFont:(NSFont *)fontObject
{
	return (NSFontTraitMask)0;
}
															// Target / Action
- (BOOL) sendAction								{ return NO; }
- (void) setAction:(SEL)aSelector				{ action = aSelector; }
- (SEL) action									{ return action; }

- (id) delegate									{ return delegate; }
- (void) setDelegate:(id)anObject				{ delegate = anObject; }

- (BOOL) addCollection:(NSString *) name options:(int) options; { return NO; }
- (void) addFontDescriptors:(NSArray *) descriptors toCollection:(NSString *) collection; { NIMP; }
- (void) addFontTrait:(id) sender; { NIMP; }
- (NSArray *) availableFontFamilies; { return nil; }
- (NSArray *) availableFontNamesMatchingFontDescriptor:(NSFontDescriptor *) descriptor; { return nil; }
- (NSArray *) availableFontNamesWithTraits:(NSFontTraitMask) mask; { return NIMP; }
- (NSArray *) availableMembersOfFontFamily:(NSString *) family; { return NIMP; }
- (NSArray *) collectionNames; { return nil; }
- (NSDictionary *) convertAttributes:(NSDictionary *) attributes; { return attributes; }
- (NSArray *) fontDescriptorsInCollection:(NSString *) collection; { return NIMP; }
- (BOOL) fontNamed:(NSString *) typeface hasTraits:(NSFontTraitMask) mask; { return NO; }
- (NSString *) localizedNameForFamily:(NSString *) family face:(NSString *) face; { return family; }
- (void) modifyFont:(id) sender; { NIMP; }
- (void) modifyFontViaPanel:(id) sender; { NIMP; }
- (void) orderFrontFontPanel:(id) sender; { NIMP; }
- (void) orderFrontStylesPanel:(id) sender; { NIMP; }
- (BOOL) removeCollection:(NSString *) collection; { return NO; }
- (void) removeFontDescriptor:(NSFontDescriptor *) descriptor fromCollection:(NSString *) collection; { NIMP; }
- (void) removeFontTrait:(id) sender; { NIMP; }
- (void) setSelectedAttributes:(NSDictionary *) attributes isMultiple:(BOOL) flag; { NIMP; }

@end /* NSFontManager */

//*****************************************************************************
//
// 		NSFontPanel 
//
//*****************************************************************************

@implementation NSFontPanel

+ (NSFontPanel *) sharedFontPanel
{
	// FIXME: who is the owner of the NIB file? And which NIB do we load?
	if ((!__fontPanel) && ![NSBundle loadNibNamed:@"FontPanel" owner:NSApp])	// looks for FontPanel in ressources of NSApp's bundle
		[NSException raise: NSInternalInconsistencyException 
					 format: @"Unable to open font panel mib model file."];
    return __fontPanel;
}

+ (id) alloc
{ 
	return __fontPanel ? __fontPanel : (__fontPanel = (NSFontPanel *) NSAllocateObject(self, 0, NSDefaultMallocZone())); 
}

+ (BOOL) sharedFontPanelExists; { return __fontPanel != nil; }

- (void) reloadDefaultFontFamilies; { NIMP; }

- (NSFont *) panelConvertFont:(NSFont *)fontObject	{ return panel_font; }

- (void) setPanelFont:(NSFont *)fontObject 
		   isMultiple:(BOOL)flag					{ panel_font = fontObject;}
- (BOOL) isEnabled									{ return NO; }
- (BOOL) worksWhenModal								{ return YES; }
- (NSView *) accessoryView;							{ return NIMP; }
- (void) setAccessoryView:(NSView *)aView			{ NIMP; }
- (void) setEnabled:(BOOL)flag						{ NIMP; }

// NSCoding protocol

- (void) encodeWithCoder:(NSCoder *) aCoder
{
	[super encodeWithCoder:aCoder];
	
	[aCoder encodeObject: panel_font];
}

- (id) initWithCoder:(NSCoder *) aDecoder
{
	self=[super initWithCoder:aDecoder];
	if([aDecoder allowsKeyedCoding])
		return self;
	
	panel_font = [[aDecoder decodeObject] retain];
	
	return self;
}

@end /* NSFontPanel */
