1996-05-30 20:03:15 +00:00
|
|
|
/*
|
|
|
|
NSColorList.m
|
|
|
|
|
1996-12-05 16:16:21 +00:00
|
|
|
Manage named lists of NSColors.
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
|
|
|
Date: 1996
|
2000-01-08 02:30:13 +00:00
|
|
|
Author: Nicola Pero <n.pero@mi.flashnet.it>
|
|
|
|
Date: January 2000
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
This file is part of the GNUstep GUI Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
1996-10-18 17:14:13 +00:00
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
|
|
|
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
1996-05-30 20:03:15 +00:00
|
|
|
*/
|
|
|
|
|
1997-09-23 22:43:24 +00:00
|
|
|
#include <gnustep/gui/config.h>
|
1996-12-05 16:16:21 +00:00
|
|
|
#include <Foundation/NSNotification.h>
|
|
|
|
#include <Foundation/NSLock.h>
|
1997-02-18 00:29:25 +00:00
|
|
|
#include <Foundation/NSDictionary.h>
|
1996-12-05 16:16:21 +00:00
|
|
|
#include <Foundation/NSArchiver.h>
|
|
|
|
#include <Foundation/NSException.h>
|
2000-01-08 02:30:13 +00:00
|
|
|
#include <Foundation/NSFileManager.h>
|
1997-10-09 22:55:31 +00:00
|
|
|
|
1997-02-18 00:29:25 +00:00
|
|
|
#include <AppKit/NSColorList.h>
|
1997-10-09 22:55:31 +00:00
|
|
|
#include <AppKit/NSColor.h>
|
1997-02-18 00:29:25 +00:00
|
|
|
#include <AppKit/AppKitExceptions.h>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
2000-01-08 02:30:13 +00:00
|
|
|
// 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;
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
@implementation NSColorList
|
|
|
|
|
|
|
|
//
|
|
|
|
// Class methods
|
|
|
|
//
|
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
if (self == [NSColorList class])
|
|
|
|
{
|
|
|
|
// Initial version
|
2000-01-08 02:30:13 +00:00
|
|
|
[self setVersion:2];
|
1996-12-05 16:16:21 +00:00
|
|
|
|
|
|
|
// Initialize the global array of color lists
|
2000-01-08 02:30:13 +00:00
|
|
|
_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.
|
|
|
|
|
1996-12-05 16:16:21 +00:00
|
|
|
// And its access lock
|
2000-01-08 02:30:13 +00:00
|
|
|
_gnustep_color_list_lock = [[NSLock alloc] init];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Getting All Color Lists
|
|
|
|
//
|
|
|
|
+ (NSArray *)availableColorLists
|
|
|
|
{
|
1996-12-05 16:16:21 +00:00
|
|
|
NSArray *a;
|
|
|
|
|
|
|
|
// Serialize access to color list
|
2000-01-08 02:30:13 +00:00
|
|
|
[_gnustep_color_list_lock lock];
|
|
|
|
|
|
|
|
a = [NSArray arrayWithArray: _gnustep_available_color_lists];
|
|
|
|
|
|
|
|
[_gnustep_color_list_lock unlock];
|
1996-12-05 16:16:21 +00:00
|
|
|
|
|
|
|
return a;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Getting a Color List by Name
|
|
|
|
//
|
|
|
|
+ (NSColorList *)colorListNamed:(NSString *)name
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
NSColorList* r;
|
|
|
|
NSEnumerator* e;
|
|
|
|
BOOL found = NO;
|
1996-12-05 16:16:21 +00:00
|
|
|
|
|
|
|
// Serialize access to color list
|
2000-01-08 02:30:13 +00:00
|
|
|
[_gnustep_color_list_lock lock];
|
|
|
|
|
|
|
|
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;
|
1997-10-09 22:55:31 +00:00
|
|
|
else
|
2000-01-08 02:30:13 +00:00
|
|
|
return nil;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Instance methods
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Initializing an NSColorList
|
|
|
|
//
|
|
|
|
- (id)initWithName:(NSString *)name
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
return [self initWithName: name
|
|
|
|
fromFile: nil];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithName:(NSString *)name
|
|
|
|
fromFile:(NSString *)path
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
NSColorList* cl;
|
|
|
|
BOOL could_load = NO;
|
1996-12-05 16:16:21 +00:00
|
|
|
|
2000-01-08 02:30:13 +00:00
|
|
|
ASSIGN (_name, name);
|
1996-12-05 16:16:21 +00:00
|
|
|
|
2000-01-08 02:30:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1996-12-05 16:16:21 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
RELEASE (_name);
|
|
|
|
TEST_RELEASE (_fullFileName);
|
|
|
|
RELEASE (_colorDictionary);
|
|
|
|
RELEASE (_orderedColorKeys);
|
1996-12-05 16:16:21 +00:00
|
|
|
[super dealloc];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Getting a Color List by Name
|
|
|
|
//
|
|
|
|
- (NSString *)name
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
return _name;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Managing Colors by Key
|
|
|
|
//
|
|
|
|
- (NSArray *)allKeys
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
return [NSArray arrayWithArray: _orderedColorKeys];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSColor *)colorWithKey:(NSString *)key
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
return [_colorDictionary objectForKey: key];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertColor:(NSColor *)color
|
|
|
|
key:(NSString *)key
|
1996-12-05 16:16:21 +00:00
|
|
|
atIndex:(unsigned)location
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
if (_is_editable == NO)
|
1996-12-05 16:16:21 +00:00
|
|
|
[NSException raise: NSColorListNotEditableException
|
|
|
|
format: @"Color list cannot be edited\n"];
|
|
|
|
|
2000-01-08 02:30:13 +00:00
|
|
|
[_colorDictionary setObject: color forKey: key];
|
|
|
|
[_orderedColorKeys removeObject: key];
|
|
|
|
[_orderedColorKeys insertObject: key atIndex: location];
|
|
|
|
|
1996-12-05 16:16:21 +00:00
|
|
|
[[NSNotificationCenter defaultCenter]
|
2000-01-08 02:30:13 +00:00
|
|
|
postNotificationName: NSColorListChangedNotification
|
|
|
|
object: self];
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
- (void)removeColorWithKey:(NSString *)key
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
if (_is_editable == NO)
|
1996-12-05 16:16:21 +00:00
|
|
|
[NSException raise: NSColorListNotEditableException
|
|
|
|
format: @"Color list cannot be edited\n"];
|
2000-01-08 02:30:13 +00:00
|
|
|
|
|
|
|
[_colorDictionary removeObjectForKey: key];
|
|
|
|
[_orderedColorKeys removeObject: key];
|
1996-12-05 16:16:21 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: NSColorListChangedNotification
|
|
|
|
object: self];
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
- (void)setColor:(NSColor *)aColor
|
|
|
|
forKey:(NSString *)key
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
if (_is_editable == NO)
|
1996-12-05 16:16:21 +00:00
|
|
|
[NSException raise: NSColorListNotEditableException
|
|
|
|
format: @"Color list cannot be edited\n"];
|
2000-01-08 02:30:13 +00:00
|
|
|
|
|
|
|
[_colorDictionary setObject: aColor forKey: key];
|
1996-12-05 16:16:21 +00:00
|
|
|
|
2000-01-08 02:30:13 +00:00
|
|
|
if ([_orderedColorKeys containsObject: key] == NO)
|
|
|
|
[_orderedColorKeys addObject: key];
|
1996-12-05 16:16:21 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter]
|
|
|
|
postNotificationName: NSColorListChangedNotification
|
|
|
|
object: self];
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Editing
|
|
|
|
//
|
|
|
|
- (BOOL)isEditable
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
return _is_editable;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Writing and Removing Files
|
|
|
|
//
|
|
|
|
- (BOOL)writeToFile:(NSString *)path
|
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
NSFileManager* fm = [NSFileManager defaultManager];
|
|
|
|
BOOL isDir;
|
|
|
|
BOOL success;
|
|
|
|
|
|
|
|
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];
|
1996-12-05 16:16:21 +00:00
|
|
|
|
2000-01-08 02:30:13 +00:00
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
[_gnustep_color_list_lock lock];
|
|
|
|
[_gnustep_available_color_lists addObject: self];
|
|
|
|
[_gnustep_color_list_lock unlock];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeFile
|
1996-12-05 16:16:21 +00:00
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
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];
|
|
|
|
|
|
|
|
// Reset file name
|
|
|
|
_fullFileName = nil;
|
|
|
|
}
|
1996-12-05 16:16:21 +00:00
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// NSCoding protocol
|
|
|
|
//
|
1999-03-02 08:58:30 +00:00
|
|
|
- (void) encodeWithCoder: (NSCoder*)aCoder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
[aCoder encodeObject: _name];
|
|
|
|
[aCoder encodeObject: _colorDictionary];
|
|
|
|
[aCoder encodeObject: _orderedColorKeys];
|
|
|
|
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:58:30 +00:00
|
|
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
2000-01-08 02:30:13 +00:00
|
|
|
[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];
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
1996-12-05 16:16:21 +00:00
|
|
|
|