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:
nico 2000-01-08 02:30:13 +00:00
parent 6a3efdf390
commit 9fa0d4b137

View file

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