gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Keyed decoding of geometry


From: Fred Kiefer
Subject: Keyed decoding of geometry
Date: Sat, 24 Jan 2004 18:06:39 +0100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030821

Hi Richard,

are you sure about the way you are decoding points, rects and size? I only have one keyed NIB file, that sombody has mailed me and here the geometry looks different. For example a rectangle is stored as:

<string>{{332, 76}, {78, 32}}</string>

What I would have expected were methods in the style:



- (NSSize) decodePointForKey: (NSString*)aKey
{
  NSString *string = [self decodeObjectForKey: aKey];

  if (string == nil)
    {
      retrun NSMakePoint(0, 0);
    }
  else
    {
      return NSPointFromString(string);
    }
}

- (NSSize) decodeSizeForKey: (NSString*)aKey
{
  NSString *string = [self decodeObjectForKey: aKey];

  if (string == nil)
    {
      retrun NSMakeSize(0, 0);
    }
  else
    {
      return NSSizeFromString(string);
    }
}

- (NSRect) decodeRectForKey: (NSString*)aKey
{
  NSString *string = [self decodeObjectForKey: aKey];

  if (string == nil)
    {
      retrun NSMakeRect(0, 0, 0, 0);
    }
  else
    {
      return NSRectFromString(string);
    }
}

Or are your methods for something completely different? If so, could we still add the above methods as a GNUstep extension?

And I also tried to write a few sample decoding methods based on that NIB file. I made specific methods out of them, so that it will be easier to keep them a bit apart from our current decoding code. Actually I would like to see some method like the follwoing on NSObject so we dont have to mix the different code in one method:


- (id) initWithCoder: (NSCoder*)aCoder
{
  if ([aCoder allowsKeyedCoding])
    {
        self = [self initWithKeyedCoder: aCoder];
    }
    else
    {
        // call old decoder code, for which I don't have a great name
    }
}


And now to show you how easy some base classes could be decoded:

@implementation NSArray (NSKeyedCoding)

- (id) initWithKeyedCoder: (NSCoder*)aCoder
{
  NSArray *array = [aCoder decodeObjectForKey: @"NS.objects"];

  [self initWithArray: array];
  return self;
}

@end

@implementation NSSet (NSKeyedCoding)

- (id) initWithKeyedCoder: (NSCoder*)aCoder
{
  NSArray *array = [aCoder decodeObjectForKey: @"NS.objects"];

  [self initWithArray: array];
  return self;
}

@end

@implementation NSString (NSKeyedCoding)

- (id) initWithKeyedCoder: (NSCoder*)aCoder
{
  NSString *string = [aCoder decodeObjectForKey: @"NS.string"];

  self = [self initWithString: string];
  return self;
}

@end

@implementation NSDictionary (NSKeyedCoding)

- (id) initWithKeyedCoder: (NSCoder*)aCoder
{
  NSArray *keys = [aCoder decodeObjectForKey: @"NS.keys"];
  NSArray *objects = [aCoder decodeObjectForKey: @"NS.objects"];

  self = [self initWithObjects: objects
               forKeys: keys
               count: [keys count]];
  return self;
}

@end

In the one NIB file I have these are the only Foundation classes that occure. The AppKit classes look a bit harder and will require a lot of testing to get them fully implemented. Here are some examples of my current code (again as the one above untested).

@implementation NSColor (NSKeyedCoding)

- (id) initWithKeyedCoder: (NSCoder*)aCoder
{
  int colorSpace = [aCoder decodeIntForKey: @"NSColorSpace"];

  if (colorSpace == NSCalibratedWhiteColorSpace)
    {
      unsigned length;
      const uint8_t *data = [aCoder decodeBytesForKey: @"NSWhite"
                                    returnedLength: &length];
      // FIXME decoding of data is missing
      float white;
      float alpha;

      RELEASE(self);
      self = [NSColor colorWithDeviceWhite: white
                      alpha: alpha];
    }
  if (colorSpace == NSCalibratedRGBColorSpace)
    {
      unsigned length;
      const uint8_t *data = [aCoder decodeBytesForKey: @"NSRGB"
                                    returnedLength: &length];
      // FIXME decoding of data is missing
      float red;
      float green;
      float blue;
      float alpha;

      RELEASE(self);
      self = [NSColor colorWithCalibratedRed: red
                      green: green
                      blue: blue
                      alpha: alpha];
    }
  else if (colorSpace == NSNamedColorSpace)
    {
      NSString *catalog = [aCoder decodeObjectForKey: @"NSCatalogName"];
      NSString *name = [aCoder decodeObjectForKey: @"NSColorName"];
      NSColor *color = [aCoder decodeObjectForKey: @"NSColor"];

      RELEASE(self);
      self = [NSColor colorWithCatalogName: catalog
                      colorName: name];
    }

  return self;
}

@end

@implementation NSView (NSKeyedCoding)

- (id) initWithKeyedCoder: (NSCoder*)aCoder
{
  NSSize frameSize = [aCoder _decodeNSSizeForKey: @"NSFrameSize"];
  id next = [aCoder decodeObjectForKey: @"NSNextResponder"];
  NSView *superView = [aCoder decodeObjectForKey: @"NSSuperview"];
  int vFlags = [aCoder decodeIntForKey: @"NSvFlags"];

  self = [self initWithFrame: frameSize];
  [self setNextResponder: next];
  if (superView != nil)
    {
      [superView addSubView: self];
    }
  // FIXME set the flags

  return self;
}


I will keep you informed onmy progress.

Fred







reply via email to

[Prev in Thread] Current Thread [Next in Thread]