Class rewritten

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5702 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Nicola Pero 2000-01-08 02:30:13 +00:00
parent 2ce6c7069c
commit cbd5c55d3f

View file

@ -7,6 +7,8 @@
Author: Scott Christley <scottc@net-community.com>
Date: 1996
Author: Nicola Pero <n.pero@mi.flashnet.it>
Date: January 2000
This file is part of the GNUstep GUI Library.
@ -32,21 +34,17 @@
#include <Foundation/NSDictionary.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSException.h>
#include <Foundation/NSFileManager.h>
#include <AppKit/NSColorList.h>
#include <AppKit/NSColor.h>
#include <AppKit/AppKitExceptions.h>
// global variable
static NSMutableArray *gnustep_available_color_lists;
static NSLock *gnustep_color_list_lock;
@interface NSColorList (GNUstepPrivate)
- (void)setFileNameFromPath: (NSString *)path;
- (NSDictionary *)colorListDictionary;
@end
// The list of available color lists is created only once -- this has
// a drawback, that you need to restart your program to get the color
// lists read again.
static NSMutableArray *_gnustep_available_color_lists;
static NSLock *_gnustep_color_list_lock;
@implementation NSColorList
@ -58,12 +56,16 @@ static NSLock *gnustep_color_list_lock;
if (self == [NSColorList class])
{
// Initial version
[self setVersion:1];
[self setVersion:2];
// Initialize the global array of color lists
gnustep_available_color_lists = [NSMutableArray new];
_gnustep_available_color_lists = [[NSMutableArray alloc] init];
/*** TODO: Load color lists in the array !! [fairly easy]***/
// Sorry, I [NP] am asleep now -- going to work on this tomorrow.
// And its access lock
gnustep_color_list_lock = [[NSLock alloc] init];
_gnustep_color_list_lock = [[NSLock alloc] init];
}
}
@ -75,10 +77,11 @@ static NSLock *gnustep_color_list_lock;
NSArray *a;
// Serialize access to color list
[gnustep_color_list_lock lock];
a = [[[NSArray alloc] initWithArray: gnustep_available_color_lists]
autorelease];
[gnustep_color_list_lock unlock];
[_gnustep_color_list_lock lock];
a = [NSArray arrayWithArray: _gnustep_available_color_lists];
[_gnustep_color_list_lock unlock];
return a;
}
@ -88,22 +91,30 @@ static NSLock *gnustep_color_list_lock;
//
+ (NSColorList *)colorListNamed:(NSString *)name
{
int i, count;
NSColorList* color = nil;
NSColorList* r;
NSEnumerator* e;
BOOL found = NO;
// Serialize access to color list
[gnustep_color_list_lock lock];
for (i = 0, count = [gnustep_available_color_lists count]; i < count; i++) {
color = [gnustep_available_color_lists objectAtIndex:i];
if ([name compare:[color name]] == NSOrderedSame)
break;
}
[gnustep_color_list_lock unlock];
[_gnustep_color_list_lock lock];
if (i == count)
return nil;
e = [_gnustep_available_color_lists objectEnumerator];
while ((r = (NSColorList *)[e nextObject]))
{
if ([[r name] isEqualToString: name])
{
found = YES;
break;
}
}
[_gnustep_color_list_lock unlock];
if (found)
return r;
else
return color;
return nil;
}
//
@ -114,59 +125,71 @@ static NSLock *gnustep_color_list_lock;
//
- (id)initWithName:(NSString *)name
{
[super init];
// Initialize instance variables
list_name = [name retain];
color_list = [NSMutableDictionary new];
color_list_keys = [NSMutableArray new];
is_editable = YES;
file_name = @"";
// Add to global list of colors
[gnustep_color_list_lock lock];
[gnustep_available_color_lists addObject: self];
[gnustep_color_list_lock unlock];
return self;
return [self initWithName: name
fromFile: nil];
}
- (id)initWithName:(NSString *)name
fromFile:(NSString *)path
{
id cl;
NSColorList* cl;
BOOL could_load = NO;
[super init];
ASSIGN (_name, name);
// Initialize instance variables
list_name = [name retain];
[self setFileNameFromPath: path];
// Unarchive the color list
cl = [NSUnarchiver unarchiveObjectWithFile:file_name];
// Copy the color list elements to self
is_editable = [cl isEditable];
color_list = [NSMutableDictionary alloc];
[color_list initWithDictionary: [cl colorListDictionary]];
color_list_keys = [NSMutableArray alloc];
[color_list_keys initWithArray: [cl allKeys]];
[cl release];
// Add to global list of colors
[gnustep_color_list_lock lock];
[gnustep_available_color_lists addObject: self];
[gnustep_color_list_lock unlock];
if (path)
{
ASSIGN (_fullFileName, [[path stringByAppendingPathComponent: name]
stringByAppendingPathExtension: @"clr"]);
// Unarchive the color list
// TODO: Unarchive from other formats as well?
cl = (NSColorList*)[NSUnarchiver unarchiveObjectWithFile: _fullFileName];
if (cl && [cl isKindOfClass: [NSColorList class]])
{
could_load = YES;
_is_editable = [[NSFileManager defaultManager]
isWritableFileAtPath: _fullFileName];
if (_is_editable)
{
_colorDictionary = [NSMutableDictionary dictionaryWithDictionary:
cl->_colorDictionary];
_orderedColorKeys = [NSMutableArray arrayWithArray:
cl->_orderedColorKeys];
}
else
{
_colorDictionary = [NSDictionary dictionaryWithDictionary:
cl->_colorDictionary];
_orderedColorKeys = [NSArray arrayWithArray:
cl->_orderedColorKeys];
}
}
[cl release];
}
if (could_load == NO)
{
_fullFileName = nil;
_colorDictionary = [[NSMutableDictionary alloc] init];
_orderedColorKeys = [[NSMutableArray alloc] init];
_is_editable = YES;
}
return self;
}
- (void)dealloc
{
[list_name release];
[color_list release];
[color_list_keys release];
RELEASE (_name);
TEST_RELEASE (_fullFileName);
RELEASE (_colorDictionary);
RELEASE (_orderedColorKeys);
[super dealloc];
}
@ -175,7 +198,7 @@ static NSLock *gnustep_color_list_lock;
//
- (NSString *)name
{
return list_name;
return _name;
}
//
@ -183,46 +206,40 @@ static NSLock *gnustep_color_list_lock;
//
- (NSArray *)allKeys
{
return [[[NSArray alloc] initWithArray: color_list_keys]
autorelease];
return [NSArray arrayWithArray: _orderedColorKeys];
}
- (NSColor *)colorWithKey:(NSString *)key
{
return [color_list objectForKey: key];
return [_colorDictionary objectForKey: key];
}
- (void)insertColor:(NSColor *)color
key:(NSString *)key
atIndex:(unsigned)location
{
// Are we even editable?
if (!is_editable)
if (_is_editable == NO)
[NSException raise: NSColorListNotEditableException
format: @"Color list cannot be edited\n"];
// add color
[color_list setObject: color forKey: key];
[color_list_keys removeObject: key];
[color_list_keys insertObject: key atIndex: location];
// post notification
[_colorDictionary setObject: color forKey: key];
[_orderedColorKeys removeObject: key];
[_orderedColorKeys insertObject: key atIndex: location];
[[NSNotificationCenter defaultCenter]
postNotificationName: NSColorListChangedNotification
object: self];
postNotificationName: NSColorListChangedNotification
object: self];
}
- (void)removeColorWithKey:(NSString *)key
{
// Are we even editable?
if (!is_editable)
if (_is_editable == NO)
[NSException raise: NSColorListNotEditableException
format: @"Color list cannot be edited\n"];
[_colorDictionary removeObjectForKey: key];
[_orderedColorKeys removeObject: key];
[color_list removeObjectForKey: key];
[color_list_keys removeObject: key];
// post notification
[[NSNotificationCenter defaultCenter]
postNotificationName: NSColorListChangedNotification
object: self];
@ -231,18 +248,15 @@ static NSLock *gnustep_color_list_lock;
- (void)setColor:(NSColor *)aColor
forKey:(NSString *)key
{
// Are we even editable?
if (!is_editable)
if (_is_editable == NO)
[NSException raise: NSColorListNotEditableException
format: @"Color list cannot be edited\n"];
[_colorDictionary setObject: aColor forKey: key];
[color_list setObject: aColor forKey: key];
if ([_orderedColorKeys containsObject: key] == NO)
[_orderedColorKeys addObject: key];
// Add to list if doesn't already exist
if (![color_list_keys containsObject: key])
[color_list_keys addObject: key];
// post notification
[[NSNotificationCenter defaultCenter]
postNotificationName: NSColorListChangedNotification
object: self];
@ -253,7 +267,7 @@ static NSLock *gnustep_color_list_lock;
//
- (BOOL)isEditable
{
return is_editable;
return _is_editable;
}
//
@ -261,20 +275,66 @@ static NSLock *gnustep_color_list_lock;
//
- (BOOL)writeToFile:(NSString *)path
{
[self setFileNameFromPath: path];
NSFileManager* fm = [NSFileManager defaultManager];
BOOL isDir;
BOOL success;
// Archive to the file
return [NSArchiver archiveRootObject: self toFile: file_name];
if (path == nil || ([fm fileExistsAtPath: path isDirectory: &isDir] == NO))
{
// FIXME the standard path for saving color lists
path = @"~/GNUstep/Library/Colors/";
isDir = YES;
}
if (isDir)
{
ASSIGN (_fullFileName, [[path stringByAppendingPathComponent: _name]
stringByAppendingPathExtension: @"clr"]);
}
else // it is a file
{
_fullFileName = path;
if ([path pathExtension] == @"clr")
{
ASSIGN (_fullFileName, path);
}
else
{
ASSIGN (_fullFileName, [[path stringByDeletingPathExtension]
stringByAppendingPathExtension: @"clr"]);
}
}
success = [NSArchiver archiveRootObject: self
toFile: _fullFileName];
if (success)
{
[_gnustep_color_list_lock lock];
[_gnustep_available_color_lists addObject: self];
[_gnustep_color_list_lock unlock];
return YES;
}
return NO;
}
- (void)removeFile
{
// xxx Tell NSWorkspace to remove the file
if (_fullFileName && _is_editable)
{
// Remove the file
[[NSFileManager defaultManager] removeFileAtPath: _fullFileName
handler: nil];
// Remove the color list from the global list of colors
[_gnustep_color_list_lock lock];
[_gnustep_available_color_lists removeObject: self];
[_gnustep_color_list_lock unlock];
// Remove from global list of colors
[gnustep_color_list_lock lock];
[gnustep_available_color_lists removeObject: self];
[gnustep_color_list_lock unlock];
// Reset file name
_fullFileName = nil;
}
}
//
@ -282,42 +342,21 @@ static NSLock *gnustep_color_list_lock;
//
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[aCoder encodeObject: list_name];
[aCoder encodeObject: color_list];
[aCoder encodeObject: color_list_keys];
[aCoder encodeValueOfObjCType:@encode(BOOL) at: &is_editable];
[aCoder encodeObject: _name];
[aCoder encodeObject: _colorDictionary];
[aCoder encodeObject: _orderedColorKeys];
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
[aDecoder decodeValueOfObjCType: @encode(id) at: &list_name];
[aDecoder decodeValueOfObjCType: @encode(id) at: &color_list];
[aDecoder decodeValueOfObjCType: @encode(id) at: &color_list_keys];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &is_editable];
[aDecoder decodeValueOfObjCType: @encode(id) at: &_name];
[aDecoder decodeValueOfObjCType: @encode(id) at: &_colorDictionary];
[aDecoder decodeValueOfObjCType: @encode(id) at: &_orderedColorKeys];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
return self;
}
@end
@implementation NSColorList (GNUstepPrivate)
- (void)setFileNameFromPath: (NSString *)path
{
NSMutableString *s = [NSMutableString stringWithCString: ""];
// Construct filename
// xxx Need to determine if path already contains filename
[s appendString: path];
[s appendString: @"/"];
[s appendString: list_name];
[s appendString: @".clr"];
file_name = s;
}
- (NSDictionary *)colorListDictionary
{
return color_list;
}
@end