home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Examples / AppKit / Draw / propertyList.m < prev    next >
Text File  |  1996-04-18  |  8KB  |  170 lines

  1. #import "draw.h"
  2.  
  3. id propertyListFromArray(NSArray *array)
  4. {
  5.     id realObject;
  6.     NSMutableArray *plistArray;
  7.     NSEnumerator *enumerator;
  8.  
  9.     plistArray = [NSMutableArray arrayWithCapacity:[array count]];
  10.     enumerator = [array objectEnumerator];
  11.     while ((realObject = [enumerator nextObject])) {
  12.         if ([realObject respondsToSelector:@selector(propertyList)]) {
  13.             [plistArray addObject:[realObject propertyList]];
  14.     } else {
  15.         /* Should probably raise here. */
  16.     }
  17.     }
  18.  
  19.     return plistArray;
  20. }
  21.  
  22. id propertyListFromFloat(float f)
  23. {
  24.     return [NSString stringWithFormat:@"%g", f];
  25. }
  26.  
  27. id propertyListFromInt(int i)
  28. {
  29.     return [NSString stringWithFormat:@"%d", i];
  30. }
  31.  
  32. id propertyListFromNSColor(NSColor *color)
  33. {
  34.     NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:5];
  35.     if ([[color colorSpaceName] isEqualToString:NSCalibratedWhiteColorSpace]) {
  36.         [dictionary setObject:@"NSCalibratedWhiteColorSpace" forKey:@"ColorSpace"];
  37.         [dictionary setObject:[NSNumber numberWithFloat:[color alphaComponent]] forKey:@"Alpha"];
  38.         [dictionary setObject:[NSNumber numberWithFloat:[color whiteComponent]] forKey:@"White"];
  39.     } else if ([[color colorSpaceName] isEqualToString:NSCalibratedRGBColorSpace]) {
  40.         [dictionary setObject:@"NSCalibratedRGBColorSpace" forKey:@"ColorSpace"];
  41.         [dictionary setObject:[NSNumber numberWithFloat:[color alphaComponent]] forKey:@"Alpha"];
  42.         [dictionary setObject:[NSNumber numberWithFloat:[color blueComponent]] forKey:@"Blue"];
  43.         [dictionary setObject:[NSNumber numberWithFloat:[color greenComponent]] forKey:@"Green"];
  44.         [dictionary setObject:[NSNumber numberWithFloat:[color redComponent]] forKey:@"Red"];
  45.     } else if ([[color colorSpaceName] isEqualToString:NSDeviceWhiteColorSpace]) {
  46.         [dictionary setObject:@"NSDeviceWhiteColorSpace" forKey:@"ColorSpace"];
  47.         [dictionary setObject:[NSNumber numberWithFloat:[color alphaComponent]] forKey:@"Alpha"];
  48.         [dictionary setObject:[NSNumber numberWithFloat:[color whiteComponent]] forKey:@"White"];
  49.     } else if ([[color colorSpaceName] isEqualToString:NSDeviceRGBColorSpace]) {
  50.         [dictionary setObject:@"NSDeviceRGBColorSpace" forKey:@"ColorSpace"];
  51.         [dictionary setObject:[NSNumber numberWithFloat:[color alphaComponent]] forKey:@"Alpha"];
  52.         [dictionary setObject:[NSNumber numberWithFloat:[color blueComponent]] forKey:@"Blue"];
  53.         [dictionary setObject:[NSNumber numberWithFloat:[color greenComponent]] forKey:@"Green"];
  54.         [dictionary setObject:[NSNumber numberWithFloat:[color redComponent]] forKey:@"Red"];
  55.     } else if ([[color colorSpaceName] isEqualToString:NSDeviceCMYKColorSpace]) {
  56.         [dictionary setObject:@"NSDeviceCMYKColorSpace" forKey:@"ColorSpace"];
  57.         [dictionary setObject:[NSNumber numberWithFloat:[color alphaComponent]] forKey:@"Alpha"];
  58.         [dictionary setObject:[NSNumber numberWithFloat:[color cyanComponent]] forKey:@"Cyan"];
  59.         [dictionary setObject:[NSNumber numberWithFloat:[color magentaComponent]] forKey:@"Magenta"];
  60.         [dictionary setObject:[NSNumber numberWithFloat:[color yellowComponent]] forKey:@"Yellow"];
  61.         [dictionary setObject:[NSNumber numberWithFloat:[color blackComponent]] forKey:@"Black"];
  62.     } else if ([[color colorSpaceName] isEqualToString:NSNamedColorSpace]) {
  63.         [dictionary setObject:@"NSNamedColorSpace" forKey:@"ColorSpace"];
  64.         [dictionary setObject:[color catalogNameComponent] forKey:@"CId"];
  65.         [dictionary setObject:[color colorNameComponent] forKey:@"NId"];
  66.         [dictionary setObject:[color localizedCatalogNameComponent] forKey:@"Catalog"];
  67.         [dictionary setObject:[color localizedColorNameComponent] forKey:@"Name"];
  68.     } else {
  69.         [dictionary setObject:@"Unknown" forKey:@"ColorSpace"];
  70.         [dictionary setObject:[NSArchiver archivedDataWithRootObject:color] forKey:@"Data"];
  71.     }
  72.     return dictionary;
  73. }
  74.  
  75. id propertyListFromNSRect(NSRect rect)
  76. {
  77.     return [NSString stringWithFormat:@"%g %g %g %g", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height];
  78. }
  79.  
  80. id propertyListFromNSSize(NSSize size)
  81. {
  82.     return [NSString stringWithFormat:@"%g %g", size.width, size.height];
  83. }
  84.  
  85. id propertyListFromNSPoint(NSPoint point)
  86. {
  87.     return [NSString stringWithFormat:@"%g %g", point.x, point.y];
  88. }
  89.  
  90. NSMutableArray *arrayFromPropertyList(id plist, NSString *directory, NSZone *zone)
  91. {
  92.     id realObject;
  93.     NSMutableArray *realArray;
  94.     NSDictionary *plistObject;
  95.     NSEnumerator *enumerator;
  96.     NSString *className;
  97.  
  98.     realArray = [[NSMutableArray allocWithZone:zone] initWithCapacity:[plist count]];
  99.     enumerator = [plist objectEnumerator];
  100.     while ((plistObject = [enumerator nextObject])) {
  101.         className = [plistObject objectForKey:@"Class"];
  102.         if (className) {
  103.             realObject = [[NSClassFromString(className) allocWithZone:zone] initFromPropertyList:plistObject inDirectory:directory];
  104.             [realArray addObject:realObject];
  105.         } else {
  106.             /* Should probably raise in this case. */
  107.         }
  108.     }
  109.  
  110.     return realArray;
  111. }
  112.  
  113. NSColor *colorFromPropertyList(id plist, NSZone *zone)
  114. {
  115.     if ([plist isKindOfClass:[NSDictionary class]]) {
  116.         NSString *colorSpaceName = [plist objectForKey:@"ColorSpace"];
  117.         if ([colorSpaceName isEqualToString:@"NSCalibratedWhiteColorSpace"]) {
  118.             return [[NSColor colorWithCalibratedWhite:[[plist objectForKey:@"White"] floatValue] alpha:[[plist objectForKey:@"Alpha"] floatValue]] retain];
  119.         } else if ([colorSpaceName isEqualToString:@"NSCalibratedRGBColorSpace"]) {
  120.             return [[NSColor colorWithCalibratedRed:[[plist objectForKey:@"Red"] floatValue] green:[[plist objectForKey:@"Green"] floatValue] blue:[[plist objectForKey:@"Blue"] floatValue] alpha:[[plist objectForKey:@"Alpha"] floatValue]] retain];
  121.         } else if ([colorSpaceName isEqualToString:@"NSDeviceWhiteColorSpace"]) {
  122.             return [[NSColor colorWithDeviceWhite:[[plist objectForKey:@"White"] floatValue] alpha:[[plist objectForKey:@"Alpha"] floatValue]] retain];
  123.         } else if ([colorSpaceName isEqualToString:@"NSDeviceRGBColorSpace"]) {
  124.             return [[NSColor colorWithDeviceRed:[[plist objectForKey:@"Red"] floatValue] green:[[plist objectForKey:@"Green"] floatValue] blue:[[plist objectForKey:@"Blue"] floatValue] alpha:[[plist objectForKey:@"Alpha"] floatValue]] retain];
  125.         } else if ([colorSpaceName isEqualToString:@"NSDeviceCMYKColorSpace"]) {
  126.             return [[NSColor colorWithDeviceCyan:[[plist objectForKey:@"Cyan"] floatValue] magenta:[[plist objectForKey:@"Magenta"] floatValue] yellow:[[plist objectForKey:@"Yellow"] floatValue] black:[[plist objectForKey:@"Black"] floatValue] alpha:[[plist objectForKey:@"Alpha"] floatValue]] retain];
  127.         } else if ([colorSpaceName isEqualToString:@"NSNamedColorSpace"]) {
  128.             return [[NSColor colorWithCatalogName:[plist objectForKey:@"CId"] colorName:[plist objectForKey:@"NId"]] retain];
  129.         } else if ([colorSpaceName isEqualToString:@"Unknown"]) {
  130.             return [[NSUnarchiver unarchiveObjectWithData:[plist objectForKey:@"Data"]] retain];
  131.         } else { // should never happen, maybe raise?
  132.             return nil;
  133.         }
  134.     } else if ([plist isKindOfClass:[NSData class]]) {
  135.         return plist ? [[NSUnarchiver unarchiveObjectWithData:plist] retain] : nil;
  136.     } else { // should never happen, maybe raise?
  137.         return nil;
  138.     }
  139. }
  140.  
  141. NSRect rectFromPropertyList(id plist)
  142. {
  143.     NSRect retval;
  144.     NSArray *components = [plist componentsSeparatedByString:@" "];
  145.     retval.origin.x = [[components objectAtIndex:0] floatValue];
  146.     retval.origin.y = [[components objectAtIndex:1] floatValue];
  147.     retval.size.width = [[components objectAtIndex:2] floatValue];
  148.     retval.size.height = [[components objectAtIndex:3] floatValue];
  149.     return retval;
  150. }
  151.  
  152. NSSize sizeFromPropertyList(id plist)
  153. {
  154.     NSSize retval;
  155.     NSArray *components = [plist componentsSeparatedByString:@" "];
  156.     retval.width = [[components objectAtIndex:0] floatValue];
  157.     retval.height = [[components objectAtIndex:1] floatValue];
  158.     return retval;
  159. }
  160.  
  161. NSPoint pointFromPropertyList(id plist)
  162. {
  163.     NSPoint retval;
  164.     NSArray *components = [plist componentsSeparatedByString:@" "];
  165.     retval.x = [[components objectAtIndex:0] floatValue];
  166.     retval.y = [[components objectAtIndex:1] floatValue];
  167.     return retval;
  168. }
  169.  
  170.