Initial implementation of NSColorList and NSColorWell.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@1971 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Scott Christley 1996-12-05 16:16:21 +00:00
parent 7f9dc52fb7
commit 910ea0d927
7 changed files with 300 additions and 38 deletions

View file

@ -1,7 +1,7 @@
/*
NSColorList.m
Description...
Manage named lists of NSColors.
Copyright (C) 1996 Free Software Foundation, Inc.
@ -27,9 +27,27 @@
*/
#include <gnustep/gui/NSColorList.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSLock.h>
#include <Foundation/NSArchiver.h>
#include <Foundation/NSException.h>
// NSColorList notifications
NSString *NSColorListChangedNotification;
NSString *NSColorListChangedNotification = @"NSColorListChange";
// NSColorList exceptions
NSString *NSColorListNotEditableException = @"NSColorListNotEditable";
// global variable
static NSMutableArray *gnustep_available_color_lists;
static NSLock *gnustep_color_list_lock;
@interface NSColorList (GNUstepPrivate)
- (void)setFileNameFromPath: (NSString *)path;
- (NSDictionary *)colorListDictionary;
@end
@implementation NSColorList
@ -42,6 +60,11 @@ NSString *NSColorListChangedNotification;
{
// Initial version
[self setVersion:1];
// Initialize the global array of color lists
gnustep_available_color_lists = [NSMutableArray array];
// And its access lock
gnustep_color_list_lock = [[NSLock alloc] init];
}
}
@ -50,7 +73,15 @@ NSString *NSColorListChangedNotification;
//
+ (NSArray *)availableColorLists
{
return nil;
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];
return a;
}
//
@ -58,7 +89,26 @@ NSString *NSColorListChangedNotification;
//
+ (NSColorList *)colorListNamed:(NSString *)name
{
return nil;
id o, e;
BOOL found = NO;
// Serialize access to color list
[gnustep_color_list_lock lock];
e = [gnustep_available_color_lists objectEnumerator];
o = [e nextObject];
while ((o) && (!found))
{
if ([name compare: [o name]] == NSOrderedSame)
found = YES;
else
o = [e nextObject];
}
[gnustep_color_list_lock unlock];
if (found)
return o;
else
return nil;
}
//
@ -69,13 +119,64 @@ NSString *NSColorListChangedNotification;
//
- (id)initWithName:(NSString *)name
{
return nil;
[super init];
// Initialize instance variables
list_name = name;
[list_name retain];
color_list = [NSMutableDictionary dictionary];
[color_list retain];
color_list_keys = [NSMutableArray array];
[color_list_keys retain];
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
fromFile:(NSString *)path
{
return nil;
id cl;
[super init];
// Initialize instance variables
list_name = name;
[list_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];
return self;
}
- (void)dealloc
{
[list_name release];
[color_list release];
[color_list_keys release];
[super dealloc];
}
//
@ -83,7 +184,7 @@ NSString *NSColorListChangedNotification;
//
- (NSString *)name
{
return nil;
return list_name;
}
//
@ -91,32 +192,77 @@ NSString *NSColorListChangedNotification;
//
- (NSArray *)allKeys
{
return nil;
return [[[NSArray alloc] initWithArray: color_list_keys]
autorelease];
}
- (NSColor *)colorWithKey:(NSString *)key
{
return nil;
return [color_list objectForKey: key];
}
- (void)insertColor:(NSColor *)color
key:(NSString *)key
atIndex:(unsigned)location
{}
atIndex:(unsigned)location
{
// Are we even editable?
if (!is_editable)
[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
[[NSNotificationCenter defaultCenter]
postNotificationName: NSColorListChangedNotification
object: self];
}
- (void)removeColorWithKey:(NSString *)key
{}
{
// Are we even editable?
if (!is_editable)
[NSException raise: NSColorListNotEditableException
format: @"Color list cannot be edited\n"];
[color_list removeObjectForKey: key];
[color_list_keys removeObject: key];
// post notification
[[NSNotificationCenter defaultCenter]
postNotificationName: NSColorListChangedNotification
object: self];
}
- (void)setColor:(NSColor *)aColor
forKey:(NSString *)key
{}
{
// Are we even editable?
if (!is_editable)
[NSException raise: NSColorListNotEditableException
format: @"Color list cannot be edited\n"];
[color_list setObject: aColor forKey: 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];
}
//
// Editing
//
- (BOOL)isEditable
{
return NO;
return is_editable;
}
//
@ -124,11 +270,21 @@ atIndex:(unsigned)location
//
- (BOOL)writeToFile:(NSString *)path
{
return NO;
[self setFileNameFromPath: path];
// Archive to the file
return [NSArchiver archiveRootObject: self toFile: file_name];
}
- (void)removeFile
{}
{
// xxx Tell NSWorkspace to remove the file
// Remove from global list of colors
[gnustep_color_list_lock lock];
[gnustep_available_color_lists removeObject: self];
[gnustep_color_list_lock unlock];
}
//
// NSCoding protocol
@ -136,13 +292,43 @@ atIndex:(unsigned)location
- (void)encodeWithCoder:aCoder
{
[super encodeWithCoder:aCoder];
[aCoder encodeObject: list_name];
[aCoder encodeObject: color_list];
[aCoder encodeObject: color_list_keys];
[aCoder encodeValueOfObjCType:@encode(BOOL) at: &is_editable];
}
- initWithCoder:aDecoder
{
[super initWithCoder:aDecoder];
list_name = [aDecoder decodeObject];
color_list = [aDecoder decodeObject];
color_list_keys = [aDecoder decodeObject];
[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