mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-06-02 23:10:43 +00:00
Added loading of available color lists; bug fixes
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5705 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
99c3774b7f
commit
4d4bc2ae29
1 changed files with 120 additions and 45 deletions
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
Manage named lists of NSColors.
|
Manage named lists of NSColors.
|
||||||
|
|
||||||
Copyright (C) 1996 Free Software Foundation, Inc.
|
Copyright (C) 1996, 2000 Free Software Foundation, Inc.
|
||||||
|
|
||||||
Author: Scott Christley <scottc@net-community.com>
|
Author: Scott Christley <scottc@net-community.com>
|
||||||
Date: 1996
|
Date: 1996
|
||||||
|
@ -35,6 +35,8 @@
|
||||||
#include <Foundation/NSArchiver.h>
|
#include <Foundation/NSArchiver.h>
|
||||||
#include <Foundation/NSException.h>
|
#include <Foundation/NSException.h>
|
||||||
#include <Foundation/NSFileManager.h>
|
#include <Foundation/NSFileManager.h>
|
||||||
|
#include <Foundation/NSPathUtilities.h>
|
||||||
|
#include <Foundation/NSString.h>
|
||||||
|
|
||||||
#include <AppKit/NSColorList.h>
|
#include <AppKit/NSColorList.h>
|
||||||
#include <AppKit/NSColor.h>
|
#include <AppKit/NSColor.h>
|
||||||
|
@ -43,8 +45,8 @@
|
||||||
// The list of available color lists is created only once -- this has
|
// 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
|
// a drawback, that you need to restart your program to get the color
|
||||||
// lists read again.
|
// lists read again.
|
||||||
static NSMutableArray *_gnustep_available_color_lists;
|
static NSMutableArray *_gnustep_available_color_lists = nil;
|
||||||
static NSLock *_gnustep_color_list_lock;
|
static NSLock *_gnustep_color_list_lock = nil;
|
||||||
|
|
||||||
@implementation NSColorList
|
@implementation NSColorList
|
||||||
|
|
||||||
|
@ -55,20 +57,52 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
{
|
{
|
||||||
if (self == [NSColorList class])
|
if (self == [NSColorList class])
|
||||||
{
|
{
|
||||||
// Initial version
|
|
||||||
[self setVersion:2];
|
[self setVersion:2];
|
||||||
|
|
||||||
// Initialize the global array of color lists
|
|
||||||
_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];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// Private Method which loads the color lists.
|
||||||
|
// Invoke if _gnustep_available_color_lists == nil
|
||||||
|
// before any operation with that object or its lock.
|
||||||
|
//
|
||||||
|
// The aim is to defer reading the available color lists
|
||||||
|
// till we really need to, so that only programs which really use
|
||||||
|
// this feature get the overhead.
|
||||||
|
+ (void) _loadAvailableColorLists
|
||||||
|
{
|
||||||
|
NSString *dir, *file;
|
||||||
|
NSEnumerator *e;
|
||||||
|
NSFileManager *fm = [NSFileManager defaultManager];
|
||||||
|
NSDirectoryEnumerator *de;
|
||||||
|
NSColorList *newList;
|
||||||
|
|
||||||
|
// Create the global array of color lists
|
||||||
|
_gnustep_available_color_lists = [[NSMutableArray alloc] init];
|
||||||
|
|
||||||
|
// Load color lists found in standard paths into the array
|
||||||
|
// FIXME: Check exactly where in the directory tree we should scan.
|
||||||
|
e = [GSStandardPathPrefixes() objectEnumerator];
|
||||||
|
while ((dir = (NSString *)[e nextObject]))
|
||||||
|
{
|
||||||
|
dir = [dir stringByAppendingPathComponent: @"Library/Colors"];
|
||||||
|
de = [fm enumeratorAtPath: dir];
|
||||||
|
while ((file = [de nextObject]))
|
||||||
|
{
|
||||||
|
if ([[file pathExtension] isEqualToString: @"clr"])
|
||||||
|
{
|
||||||
|
file = [file stringByDeletingPathExtension];
|
||||||
|
newList = AUTORELEASE ([[NSColorList alloc] initWithName: file
|
||||||
|
fromFile: dir]);
|
||||||
|
[_gnustep_available_color_lists addObject: newList];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// And create its access lock
|
||||||
|
_gnustep_color_list_lock = [[NSLock alloc] init];
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Getting All Color Lists
|
// Getting All Color Lists
|
||||||
//
|
//
|
||||||
|
@ -76,6 +110,9 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
{
|
{
|
||||||
NSArray *a;
|
NSArray *a;
|
||||||
|
|
||||||
|
if (_gnustep_available_color_lists == nil)
|
||||||
|
[NSColorList _loadAvailableColorLists];
|
||||||
|
|
||||||
// Serialize access to color list
|
// Serialize access to color list
|
||||||
[_gnustep_color_list_lock lock];
|
[_gnustep_color_list_lock lock];
|
||||||
|
|
||||||
|
@ -91,9 +128,12 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
//
|
//
|
||||||
+ (NSColorList *)colorListNamed:(NSString *)name
|
+ (NSColorList *)colorListNamed:(NSString *)name
|
||||||
{
|
{
|
||||||
NSColorList* r;
|
NSColorList *r;
|
||||||
NSEnumerator* e;
|
NSEnumerator *e;
|
||||||
BOOL found = NO;
|
BOOL found = NO;
|
||||||
|
|
||||||
|
if (_gnustep_available_color_lists == nil)
|
||||||
|
[NSColorList _loadAvailableColorLists];
|
||||||
|
|
||||||
// Serialize access to color list
|
// Serialize access to color list
|
||||||
[_gnustep_color_list_lock lock];
|
[_gnustep_color_list_lock lock];
|
||||||
|
@ -132,8 +172,8 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
- (id)initWithName:(NSString *)name
|
- (id)initWithName:(NSString *)name
|
||||||
fromFile:(NSString *)path
|
fromFile:(NSString *)path
|
||||||
{
|
{
|
||||||
NSColorList* cl;
|
NSColorList *cl;
|
||||||
BOOL could_load = NO;
|
BOOL could_load = NO;
|
||||||
|
|
||||||
ASSIGN (_name, name);
|
ASSIGN (_name, name);
|
||||||
|
|
||||||
|
@ -144,7 +184,8 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
|
|
||||||
// Unarchive the color list
|
// Unarchive the color list
|
||||||
|
|
||||||
// TODO: Unarchive from other formats as well?
|
// TODO [Optm]: Rewrite to initialize directly without unarchiving
|
||||||
|
// in another object
|
||||||
|
|
||||||
cl = (NSColorList*)[NSUnarchiver unarchiveObjectWithFile: _fullFileName];
|
cl = (NSColorList*)[NSUnarchiver unarchiveObjectWithFile: _fullFileName];
|
||||||
|
|
||||||
|
@ -155,22 +196,14 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
_is_editable = [[NSFileManager defaultManager]
|
_is_editable = [[NSFileManager defaultManager]
|
||||||
isWritableFileAtPath: _fullFileName];
|
isWritableFileAtPath: _fullFileName];
|
||||||
|
|
||||||
if (_is_editable)
|
ASSIGN(_colorDictionary, [NSMutableDictionary
|
||||||
{
|
dictionaryWithDictionary:
|
||||||
_colorDictionary = [NSMutableDictionary dictionaryWithDictionary:
|
cl->_colorDictionary]);
|
||||||
cl->_colorDictionary];
|
|
||||||
_orderedColorKeys = [NSMutableArray arrayWithArray:
|
ASSIGN(_orderedColorKeys, [NSMutableArray
|
||||||
cl->_orderedColorKeys];
|
arrayWithArray:
|
||||||
}
|
cl->_orderedColorKeys]);
|
||||||
else
|
|
||||||
{
|
|
||||||
_colorDictionary = [NSDictionary dictionaryWithDictionary:
|
|
||||||
cl->_colorDictionary];
|
|
||||||
_orderedColorKeys = [NSArray arrayWithArray:
|
|
||||||
cl->_orderedColorKeys];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
[cl release];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (could_load == NO)
|
if (could_load == NO)
|
||||||
|
@ -275,14 +308,22 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
//
|
//
|
||||||
- (BOOL)writeToFile:(NSString *)path
|
- (BOOL)writeToFile:(NSString *)path
|
||||||
{
|
{
|
||||||
NSFileManager* fm = [NSFileManager defaultManager];
|
NSFileManager *fm = [NSFileManager defaultManager];
|
||||||
BOOL isDir;
|
NSString *tmpPath;
|
||||||
BOOL success;
|
BOOL isDir;
|
||||||
|
BOOL success;
|
||||||
|
BOOL path_is_standard = YES;
|
||||||
|
|
||||||
|
// We need to initialize before saving, to avoid the new file being
|
||||||
|
// counted as a different list thus making us appear twice
|
||||||
|
if (_gnustep_available_color_lists == nil)
|
||||||
|
[NSColorList _loadAvailableColorLists];
|
||||||
|
|
||||||
if (path == nil || ([fm fileExistsAtPath: path isDirectory: &isDir] == NO))
|
if (path == nil || ([fm fileExistsAtPath: path isDirectory: &isDir] == NO))
|
||||||
{
|
{
|
||||||
// FIXME the standard path for saving color lists
|
// FIXME the standard path for saving color lists?
|
||||||
path = @"~/GNUstep/Library/Colors/";
|
path = [NSHomeDirectory () stringByAppendingPathComponent:
|
||||||
|
@"GNUstep/Library/Colors"];
|
||||||
isDir = YES;
|
isDir = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -305,13 +346,46 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if the path is a standard path
|
||||||
|
if ([[path lastPathComponent] isEqualToString: @"Colors"] == NO)
|
||||||
|
path_is_standard = NO;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tmpPath = [path stringByDeletingLastPathComponent];
|
||||||
|
if ([[tmpPath lastPathComponent] isEqualToString: @"Library"] == NO)
|
||||||
|
path_is_standard = NO;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tmpPath = [tmpPath stringByDeletingLastPathComponent];
|
||||||
|
if ([GSStandardPathPrefixes() containsObject: tmpPath] == NO)
|
||||||
|
path_is_standard = NO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If path is standard and it does not exist, try to create it.
|
||||||
|
// System standard paths should always be assumed to exist;
|
||||||
|
// this will normally then only try to create user paths.
|
||||||
|
if (path_is_standard && ([fm fileExistsAtPath: path] == NO))
|
||||||
|
{
|
||||||
|
if([fm createDirectoryAtPath: path
|
||||||
|
attributes: nil])
|
||||||
|
{
|
||||||
|
NSLog (@"Created standard directory %@", path);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
NSLog (@"Failed attemp to create directory %@", path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
success = [NSArchiver archiveRootObject: self
|
success = [NSArchiver archiveRootObject: self
|
||||||
toFile: _fullFileName];
|
toFile: _fullFileName];
|
||||||
|
|
||||||
if (success)
|
if (success && path_is_standard)
|
||||||
{
|
{
|
||||||
[_gnustep_color_list_lock lock];
|
[_gnustep_color_list_lock lock];
|
||||||
[_gnustep_available_color_lists addObject: self];
|
if ([_gnustep_available_color_lists containsObject: self] == NO)
|
||||||
|
[_gnustep_available_color_lists addObject: self];
|
||||||
[_gnustep_color_list_lock unlock];
|
[_gnustep_color_list_lock unlock];
|
||||||
return YES;
|
return YES;
|
||||||
}
|
}
|
||||||
|
@ -328,6 +402,9 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
handler: nil];
|
handler: nil];
|
||||||
|
|
||||||
// Remove the color list from the global list of colors
|
// Remove the color list from the global list of colors
|
||||||
|
if (_gnustep_available_color_lists == nil)
|
||||||
|
[NSColorList _loadAvailableColorLists];
|
||||||
|
|
||||||
[_gnustep_color_list_lock lock];
|
[_gnustep_color_list_lock lock];
|
||||||
[_gnustep_available_color_lists removeObject: self];
|
[_gnustep_available_color_lists removeObject: self];
|
||||||
[_gnustep_color_list_lock unlock];
|
[_gnustep_color_list_lock unlock];
|
||||||
|
@ -345,7 +422,6 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
[aCoder encodeObject: _name];
|
[aCoder encodeObject: _name];
|
||||||
[aCoder encodeObject: _colorDictionary];
|
[aCoder encodeObject: _colorDictionary];
|
||||||
[aCoder encodeObject: _orderedColorKeys];
|
[aCoder encodeObject: _orderedColorKeys];
|
||||||
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id) initWithCoder: (NSCoder*)aDecoder
|
- (id) initWithCoder: (NSCoder*)aDecoder
|
||||||
|
@ -353,7 +429,6 @@ static NSLock *_gnustep_color_list_lock;
|
||||||
[aDecoder decodeValueOfObjCType: @encode(id) at: &_name];
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_name];
|
||||||
[aDecoder decodeValueOfObjCType: @encode(id) at: &_colorDictionary];
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_colorDictionary];
|
||||||
[aDecoder decodeValueOfObjCType: @encode(id) at: &_orderedColorKeys];
|
[aDecoder decodeValueOfObjCType: @encode(id) at: &_orderedColorKeys];
|
||||||
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_is_editable];
|
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue