mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-04-23 15:11:37 +00:00
Corrected some methods on this class so that subclasses may use
them. Moved all the class methods to the front of the file. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@10113 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
5cf3aef11e
commit
3b035626b2
1 changed files with 200 additions and 179 deletions
|
@ -44,7 +44,8 @@
|
|||
#include <AppKit/NSColor.h>
|
||||
#include <AppKit/DPSOperators.h>
|
||||
|
||||
static NSMutableArray* imageReps = NULL;
|
||||
static NSMutableArray *imageReps = nil;
|
||||
static Class NSImageRep_class = NULL;
|
||||
|
||||
@implementation NSImageRep
|
||||
|
||||
|
@ -55,6 +56,8 @@ static NSMutableArray* imageReps = NULL;
|
|||
if (self == [NSImageRep class])
|
||||
{
|
||||
id obj;
|
||||
|
||||
NSImageRep_class = self;
|
||||
imageReps = [[NSMutableArray alloc] initWithCapacity: 2];
|
||||
obj = [[NSUserDefaults standardUserDefaults]
|
||||
stringForKey: @"ImageCompositing"];
|
||||
|
@ -64,6 +67,89 @@ static NSMutableArray* imageReps = NULL;
|
|||
}
|
||||
}
|
||||
|
||||
// Managing NSImageRep Subclasses
|
||||
+ (Class) imageRepClassForData: (NSData *)data
|
||||
{
|
||||
int i, count;
|
||||
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
if ([rep canInitWithData: data])
|
||||
return rep;
|
||||
}
|
||||
return Nil;
|
||||
}
|
||||
|
||||
+ (Class) imageRepClassForFileType: (NSString *)type
|
||||
{
|
||||
int i, count;
|
||||
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
if ([[rep imageFileTypes] indexOfObject: type] != NSNotFound)
|
||||
{
|
||||
return rep;
|
||||
}
|
||||
}
|
||||
return Nil;
|
||||
}
|
||||
|
||||
+ (Class) imageRepClassForPasteboardType: (NSString *)type
|
||||
{
|
||||
int i, count;
|
||||
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
|
||||
if ([[rep imagePasteboardTypes] indexOfObject: type] != NSNotFound)
|
||||
{
|
||||
return rep;
|
||||
}
|
||||
}
|
||||
return Nil;
|
||||
}
|
||||
|
||||
+ (void) registerImageRepClass: (Class)imageRepClass
|
||||
{
|
||||
if ([imageReps containsObject: imageRepClass] == NO)
|
||||
{
|
||||
Class c = imageRepClass;
|
||||
|
||||
while (c != nil && c != [NSObject class] && c != [NSImageRep class])
|
||||
{
|
||||
c = [c superclass];
|
||||
}
|
||||
if (c != [NSImageRep class])
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"Attempt to register non-imagerep class"];
|
||||
}
|
||||
[imageReps addObject: imageRepClass];
|
||||
}
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
postNotificationName: NSImageRepRegistryChangedNotification
|
||||
object: self];
|
||||
}
|
||||
|
||||
+ (NSArray *) registeredImageRepClasses
|
||||
{
|
||||
return (NSArray *)imageReps;
|
||||
}
|
||||
|
||||
+ (void) unregisterImageRepClass: (Class)imageRepClass
|
||||
{
|
||||
[imageReps removeObject: imageRepClass];
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
postNotificationName: NSImageRepRegistryChangedNotification
|
||||
object: self];
|
||||
}
|
||||
|
||||
// Creating an NSImageRep
|
||||
+ (id) imageRepWithContentsOfFile: (NSString *)filename
|
||||
{
|
||||
|
@ -77,46 +163,91 @@ static NSMutableArray* imageReps = NULL;
|
|||
|
||||
+ (NSArray *) imageRepsWithContentsOfFile: (NSString *)filename
|
||||
{
|
||||
int i, count;
|
||||
NSString *ext;
|
||||
NSMutableArray *array;
|
||||
Class rep;
|
||||
|
||||
// Is the file extension already the file type?
|
||||
ext = [filename pathExtension];
|
||||
// FIXME: Should this be an exception? Should we even check this?
|
||||
if (!ext)
|
||||
{
|
||||
NSLog(@"Extension missing from filename - '%@'", filename);
|
||||
// FIXME: Should this be an exception?
|
||||
NSLog(@"Extension missing from image filename - '%@'", filename);
|
||||
return nil;
|
||||
}
|
||||
|
||||
array = [NSMutableArray arrayWithCapacity: 1];
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
if (self == NSImageRep_class)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
if ([[rep imageFileTypes] indexOfObject: ext] != NSNotFound)
|
||||
{
|
||||
// This is some GNUstep extension for special file types
|
||||
if ([rep respondsToSelector: @selector(imageRepsWithFile:)])
|
||||
[array addObjectsFromArray: [rep imageRepsWithFile: filename]];
|
||||
else
|
||||
{
|
||||
NSData* data;
|
||||
rep = [self imageRepClassForFileType: ext];
|
||||
}
|
||||
else if ([[self imageFileTypes] containsObject: ext])
|
||||
{
|
||||
rep = self;
|
||||
}
|
||||
else
|
||||
return nil;
|
||||
|
||||
data = [NSData dataWithContentsOfFile: filename];
|
||||
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
|
||||
[array addObjectsFromArray: [rep imageRepsWithData: data]];
|
||||
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
|
||||
{
|
||||
NSImageRep *imageRep = [rep imageRepWithData: data];
|
||||
|
||||
if (rep != nil)
|
||||
[array addObject: imageRep];
|
||||
}
|
||||
}
|
||||
// This is a GNUstep extension for special file types
|
||||
if ([rep respondsToSelector: @selector(imageRepsWithFile:)])
|
||||
return [rep imageRepsWithFile: filename];
|
||||
else
|
||||
{
|
||||
NSData* data;
|
||||
|
||||
data = [NSData dataWithContentsOfFile: filename];
|
||||
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
|
||||
return [rep imageRepsWithData: data];
|
||||
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
|
||||
{
|
||||
NSImageRep *imageRep = [rep imageRepWithData: data];
|
||||
|
||||
if (imageRep != nil)
|
||||
return [NSArray arrayWithObject: imageRep];
|
||||
}
|
||||
}
|
||||
return (NSArray *)array;
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (id)imageRepWithContentsOfURL:(NSURL *)anURL
|
||||
{
|
||||
NSArray* array;
|
||||
|
||||
array = [self imageRepsWithContentsOfURL: anURL];
|
||||
if ([array count])
|
||||
return [array objectAtIndex: 0];
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSArray *)imageRepsWithContentsOfURL:(NSURL *)anURL
|
||||
{
|
||||
Class rep;
|
||||
NSData* data;
|
||||
|
||||
// FIXME: Should we use the file type for URLs or only check the data?
|
||||
data = [anURL resourceDataUsingCache: YES];
|
||||
|
||||
if (self == NSImageRep_class)
|
||||
{
|
||||
rep = [self imageRepClassForData: data];
|
||||
}
|
||||
else if ([self canInitWithData: data])
|
||||
{
|
||||
rep = self;
|
||||
}
|
||||
else
|
||||
return nil;
|
||||
|
||||
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
|
||||
return [rep imageRepsWithData: data];
|
||||
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
|
||||
{
|
||||
NSImageRep *imageRep = [rep imageRepWithData: data];
|
||||
|
||||
if (imageRep != nil)
|
||||
return [NSArray arrayWithObject: imageRep];
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (id) imageRepWithPasteboard: (NSPasteboard *)pasteboard
|
||||
|
@ -133,14 +264,24 @@ static NSMutableArray* imageReps = NULL;
|
|||
{
|
||||
int i, count;
|
||||
NSMutableArray* array;
|
||||
NSArray *reps;
|
||||
|
||||
if (self == NSImageRep_class)
|
||||
{
|
||||
reps = imageReps;
|
||||
}
|
||||
else
|
||||
{
|
||||
reps = [NSArray arrayWithObject: self];
|
||||
}
|
||||
|
||||
array = [NSMutableArray arrayWithCapacity: 1];
|
||||
|
||||
count = [imageReps count];
|
||||
count = [reps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
NSString* ptype;
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
Class rep = [reps objectAtIndex: i];
|
||||
|
||||
ptype = [pasteboard availableTypeFromArray: [rep imagePasteboardTypes]];
|
||||
if (ptype != nil)
|
||||
|
@ -158,72 +299,13 @@ static NSMutableArray* imageReps = NULL;
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ([array count] == 0)
|
||||
return nil;
|
||||
|
||||
return (NSArray *)array;
|
||||
}
|
||||
|
||||
+ (id)imageRepWithContentsOfURL:(NSURL *)anURL
|
||||
{
|
||||
NSArray* array;
|
||||
|
||||
array = [self imageRepsWithContentsOfURL: anURL];
|
||||
if ([array count])
|
||||
return [array objectAtIndex: 0];
|
||||
return nil;
|
||||
}
|
||||
|
||||
+ (NSArray *)imageRepsWithContentsOfURL:(NSURL *)anURL
|
||||
{
|
||||
int i, count;
|
||||
NSString *ext;
|
||||
NSMutableArray *array;
|
||||
|
||||
ext = [[anURL path] pathExtension];
|
||||
// FIXME: Should this be an exception? Should we even check this?
|
||||
if (!ext)
|
||||
{
|
||||
NSLog(@"Extension missing from URL - '%@'", [anURL path]);
|
||||
return nil;
|
||||
}
|
||||
|
||||
array = [NSMutableArray arrayWithCapacity: 1];
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
if ([[rep imageFileTypes] indexOfObject: ext] != NSNotFound)
|
||||
{
|
||||
NSData* data;
|
||||
|
||||
data = [anURL resourceDataUsingCache: YES];
|
||||
if ([rep respondsToSelector: @selector(imageRepsWithData:)])
|
||||
[array addObjectsFromArray: [rep imageRepsWithData: data]];
|
||||
else if ([rep respondsToSelector: @selector(imageRepWithData:)])
|
||||
{
|
||||
NSImageRep *imageRep = [rep imageRepWithData: data];
|
||||
|
||||
if (rep != nil)
|
||||
[array addObject: imageRep];
|
||||
}
|
||||
}
|
||||
}
|
||||
return (NSArray *)array;
|
||||
}
|
||||
|
||||
- (id) copyWithZone: (NSZone *)zone
|
||||
{
|
||||
NSImageRep *copy;
|
||||
|
||||
copy = (NSImageRep*)NSCopyObject(self, 0, zone);
|
||||
copy->_colorSpace = RETAIN(_colorSpace);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_colorSpace);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
// Checking Data Types
|
||||
+ (BOOL) canInitWithData: (NSData *)data
|
||||
|
@ -234,20 +316,22 @@ static NSMutableArray* imageReps = NULL;
|
|||
|
||||
+ (BOOL) canInitWithPasteboard: (NSPasteboard *)pasteboard
|
||||
{
|
||||
/* Subclass responsibility */
|
||||
return NO;
|
||||
NSArray *pbTypes = [pasteboard types];
|
||||
NSArray *myTypes = [self imageUnfilteredPasteboardTypes];
|
||||
|
||||
return ([pbTypes firstObjectCommonWithArray: myTypes] != nil);
|
||||
}
|
||||
|
||||
+ (NSArray *) imageFileTypes
|
||||
{
|
||||
/* Subclass responsibility */
|
||||
return nil;
|
||||
// FIXME: We should check what conversions are defined by services.
|
||||
return [self imageUnfilteredFileTypes];
|
||||
}
|
||||
|
||||
+ (NSArray *) imagePasteboardTypes
|
||||
{
|
||||
/* Subclass responsibility */
|
||||
return nil;
|
||||
// FIXME: We should check what conversions are defined by services.
|
||||
return [self imageUnfilteredPasteboardTypes];
|
||||
}
|
||||
|
||||
+ (NSArray *) imageUnfilteredFileTypes
|
||||
|
@ -262,6 +346,14 @@ static NSMutableArray* imageReps = NULL;
|
|||
return nil;
|
||||
}
|
||||
|
||||
|
||||
// Instance methods
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_colorSpace);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
// Setting the Size of the Image
|
||||
- (void) setSize: (NSSize)aSize
|
||||
{
|
||||
|
@ -337,7 +429,8 @@ static NSMutableArray* imageReps = NULL;
|
|||
// Drawing the Image
|
||||
- (BOOL) draw
|
||||
{
|
||||
return YES; /* Subclass should implement this. */
|
||||
/* Subclass should implement this. */
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL) drawAtPoint: (NSPoint)aPoint
|
||||
|
@ -389,87 +482,15 @@ static NSMutableArray* imageReps = NULL;
|
|||
return ok;
|
||||
}
|
||||
|
||||
// Managing NSImageRep Subclasses
|
||||
+ (Class) imageRepClassForData: (NSData *)data
|
||||
// NSCopying protocol
|
||||
- (id) copyWithZone: (NSZone *)zone
|
||||
{
|
||||
int i, count;
|
||||
NSImageRep *copy;
|
||||
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
if ([rep canInitWithData: data])
|
||||
return rep;
|
||||
}
|
||||
return Nil;
|
||||
}
|
||||
copy = (NSImageRep*)NSCopyObject(self, 0, zone);
|
||||
copy->_colorSpace = RETAIN(_colorSpace);
|
||||
|
||||
+ (Class) imageRepClassForFileType: (NSString *)type
|
||||
{
|
||||
int i, count;
|
||||
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
if ([[rep imageFileTypes] indexOfObject: type] != NSNotFound)
|
||||
{
|
||||
return rep;
|
||||
}
|
||||
}
|
||||
return Nil;
|
||||
}
|
||||
|
||||
+ (Class) imageRepClassForPasteboardType: (NSString *)type
|
||||
{
|
||||
int i, count;
|
||||
|
||||
count = [imageReps count];
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
Class rep = [imageReps objectAtIndex: i];
|
||||
|
||||
if ([[rep imagePasteboardTypes] indexOfObject: type] != NSNotFound)
|
||||
{
|
||||
return rep;
|
||||
}
|
||||
}
|
||||
return Nil;
|
||||
}
|
||||
|
||||
+ (void) registerImageRepClass: (Class)imageRepClass
|
||||
{
|
||||
if ([imageReps containsObject: imageRepClass] == NO)
|
||||
{
|
||||
Class c = imageRepClass;
|
||||
|
||||
while (c != nil && c != [NSObject class] && c != [NSImageRep class])
|
||||
{
|
||||
c = [c superclass];
|
||||
}
|
||||
if (c != [NSImageRep class])
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"Attempt to register non-imagerep class"];
|
||||
}
|
||||
[imageReps addObject: imageRepClass];
|
||||
}
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
postNotificationName: NSImageRepRegistryChangedNotification
|
||||
object: self];
|
||||
}
|
||||
|
||||
+ (NSArray *) registeredImageRepClasses
|
||||
{
|
||||
return (NSArray *)imageReps;
|
||||
}
|
||||
|
||||
+ (void) unregisterImageRepClass: (Class)imageRepClass
|
||||
{
|
||||
[imageReps removeObject: imageRepClass];
|
||||
[[NSNotificationCenter defaultCenter]
|
||||
postNotificationName: NSImageRepRegistryChangedNotification
|
||||
object: self];
|
||||
return copy;
|
||||
}
|
||||
|
||||
// NSCoding protocol
|
||||
|
|
Loading…
Reference in a new issue