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:
FredKiefer 2001-06-07 21:55:46 +00:00
parent 7a995b5964
commit e8448db22a

View file

@ -44,7 +44,8 @@
#include <AppKit/NSColor.h> #include <AppKit/NSColor.h>
#include <AppKit/DPSOperators.h> #include <AppKit/DPSOperators.h>
static NSMutableArray* imageReps = NULL; static NSMutableArray *imageReps = nil;
static Class NSImageRep_class = NULL;
@implementation NSImageRep @implementation NSImageRep
@ -55,6 +56,8 @@ static NSMutableArray* imageReps = NULL;
if (self == [NSImageRep class]) if (self == [NSImageRep class])
{ {
id obj; id obj;
NSImageRep_class = self;
imageReps = [[NSMutableArray alloc] initWithCapacity: 2]; imageReps = [[NSMutableArray alloc] initWithCapacity: 2];
obj = [[NSUserDefaults standardUserDefaults] obj = [[NSUserDefaults standardUserDefaults]
stringForKey: @"ImageCompositing"]; 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 // Creating an NSImageRep
+ (id) imageRepWithContentsOfFile: (NSString *)filename + (id) imageRepWithContentsOfFile: (NSString *)filename
{ {
@ -77,46 +163,91 @@ static NSMutableArray* imageReps = NULL;
+ (NSArray *) imageRepsWithContentsOfFile: (NSString *)filename + (NSArray *) imageRepsWithContentsOfFile: (NSString *)filename
{ {
int i, count;
NSString *ext; NSString *ext;
NSMutableArray *array; Class rep;
// Is the file extension already the file type?
ext = [filename pathExtension]; ext = [filename pathExtension];
// FIXME: Should this be an exception? Should we even check this?
if (!ext) if (!ext)
{ {
NSLog(@"Extension missing from filename - '%@'", filename); // FIXME: Should this be an exception?
NSLog(@"Extension missing from image filename - '%@'", filename);
return nil; return nil;
} }
array = [NSMutableArray arrayWithCapacity: 1]; if (self == NSImageRep_class)
count = [imageReps count];
for (i = 0; i < count; i++)
{ {
Class rep = [imageReps objectAtIndex: i]; rep = [self imageRepClassForFileType: ext];
if ([[rep imageFileTypes] indexOfObject: ext] != NSNotFound) }
{ else if ([[self imageFileTypes] containsObject: ext])
// This is some GNUstep extension for special file types {
if ([rep respondsToSelector: @selector(imageRepsWithFile:)]) rep = self;
[array addObjectsFromArray: [rep imageRepsWithFile: filename]]; }
else else
{ return nil;
NSData* data;
data = [NSData dataWithContentsOfFile: filename]; // This is a GNUstep extension for special file types
if ([rep respondsToSelector: @selector(imageRepsWithData:)]) if ([rep respondsToSelector: @selector(imageRepsWithFile:)])
[array addObjectsFromArray: [rep imageRepsWithData: data]]; return [rep imageRepsWithFile: filename];
else if ([rep respondsToSelector: @selector(imageRepWithData:)]) else
{ {
NSImageRep *imageRep = [rep imageRepWithData: data]; NSData* data;
if (rep != nil) data = [NSData dataWithContentsOfFile: filename];
[array addObject: imageRep]; 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 + (id) imageRepWithPasteboard: (NSPasteboard *)pasteboard
@ -133,14 +264,24 @@ static NSMutableArray* imageReps = NULL;
{ {
int i, count; int i, count;
NSMutableArray* array; NSMutableArray* array;
NSArray *reps;
if (self == NSImageRep_class)
{
reps = imageReps;
}
else
{
reps = [NSArray arrayWithObject: self];
}
array = [NSMutableArray arrayWithCapacity: 1]; array = [NSMutableArray arrayWithCapacity: 1];
count = [imageReps count]; count = [reps count];
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
NSString* ptype; NSString* ptype;
Class rep = [imageReps objectAtIndex: i]; Class rep = [reps objectAtIndex: i];
ptype = [pasteboard availableTypeFromArray: [rep imagePasteboardTypes]]; ptype = [pasteboard availableTypeFromArray: [rep imagePasteboardTypes]];
if (ptype != nil) if (ptype != nil)
@ -158,72 +299,13 @@ static NSMutableArray* imageReps = NULL;
} }
} }
} }
if ([array count] == 0)
return nil;
return (NSArray *)array; 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 // Checking Data Types
+ (BOOL) canInitWithData: (NSData *)data + (BOOL) canInitWithData: (NSData *)data
@ -234,20 +316,22 @@ static NSMutableArray* imageReps = NULL;
+ (BOOL) canInitWithPasteboard: (NSPasteboard *)pasteboard + (BOOL) canInitWithPasteboard: (NSPasteboard *)pasteboard
{ {
/* Subclass responsibility */ NSArray *pbTypes = [pasteboard types];
return NO; NSArray *myTypes = [self imageUnfilteredPasteboardTypes];
return ([pbTypes firstObjectCommonWithArray: myTypes] != nil);
} }
+ (NSArray *) imageFileTypes + (NSArray *) imageFileTypes
{ {
/* Subclass responsibility */ // FIXME: We should check what conversions are defined by services.
return nil; return [self imageUnfilteredFileTypes];
} }
+ (NSArray *) imagePasteboardTypes + (NSArray *) imagePasteboardTypes
{ {
/* Subclass responsibility */ // FIXME: We should check what conversions are defined by services.
return nil; return [self imageUnfilteredPasteboardTypes];
} }
+ (NSArray *) imageUnfilteredFileTypes + (NSArray *) imageUnfilteredFileTypes
@ -262,6 +346,14 @@ static NSMutableArray* imageReps = NULL;
return nil; return nil;
} }
// Instance methods
- (void) dealloc
{
RELEASE(_colorSpace);
[super dealloc];
}
// Setting the Size of the Image // Setting the Size of the Image
- (void) setSize: (NSSize)aSize - (void) setSize: (NSSize)aSize
{ {
@ -337,7 +429,8 @@ static NSMutableArray* imageReps = NULL;
// Drawing the Image // Drawing the Image
- (BOOL) draw - (BOOL) draw
{ {
return YES; /* Subclass should implement this. */ /* Subclass should implement this. */
return YES;
} }
- (BOOL) drawAtPoint: (NSPoint)aPoint - (BOOL) drawAtPoint: (NSPoint)aPoint
@ -389,87 +482,15 @@ static NSMutableArray* imageReps = NULL;
return ok; return ok;
} }
// Managing NSImageRep Subclasses // NSCopying protocol
+ (Class) imageRepClassForData: (NSData *)data - (id) copyWithZone: (NSZone *)zone
{ {
int i, count; NSImageRep *copy;
count = [imageReps count]; copy = (NSImageRep*)NSCopyObject(self, 0, zone);
for (i = 0; i < count; i++) copy->_colorSpace = RETAIN(_colorSpace);
{
Class rep = [imageReps objectAtIndex: i];
if ([rep canInitWithData: data])
return rep;
}
return Nil;
}
+ (Class) imageRepClassForFileType: (NSString *)type return copy;
{
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];
} }
// NSCoding protocol // NSCoding protocol