[PATCH] Working NSSearchFieldCell archiving support

View: New views
4 Messages — Rating Filter:   Alert me  

[PATCH] Working NSSearchFieldCell archiving support

by Quentin Mathé-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

Here is a patch to fix NSSearchFieldCell archiving and keyed archiving.
Non-keyed archiving was crashing because _max_recents ivar was declared as an unsigned char and encoded/decoded as an unsigned int. Keyed archiving was not crashing because keyed decoding was not implemented.  The patch adds keyed encoding to -encodeWithCoder:.

In a nib (a xib precisely), NSMaximumRecents is encoded as an int, so I switched to int as the encoded type (rather than unsigned int).
I also changed the ivar from unsigned char to int to keep the encoding/decoding as simple as possible. I doubt any application has more than 4 or 5 search fields in use at the same time so it doesn't seem worth to save some memory.

Cheers,
Quentin.

[NSSearchFieldCell-encoding.patch]

Index: Headers/AppKit/NSSearchFieldCell.h
===================================================================
--- Headers/AppKit/NSSearchFieldCell.h (revision 28600)
+++ Headers/AppKit/NSSearchFieldCell.h (working copy)
@@ -52,7 +52,7 @@
     NSMenu *_menu_template;
     BOOL _sends_whole_search_string;
     BOOL _sends_search_string_immediatly;
-    unsigned char _max_recents;
+    int _max_recents;
 }
 
 // Managing button cells
Index: Source/NSSearchFieldCell.m
===================================================================
--- Source/NSSearchFieldCell.m (revision 28600)
+++ Source/NSSearchFieldCell.m (working copy)
@@ -443,35 +443,56 @@
        [self searchButtonRectForBounds: [_control_view bounds]]
    inView: _control_view];
 }
+
 //
 // NSCoding protocol
 //
+
+
+static NSString *NSSearchButtonCell = @"NSSearchButtonCell";
+static NSString *NSCancelButtonCell = @"NSCancelButtonCell";
+static NSString *NSRecentsAutosaveName = @"NSRecentsAutosaveName";
+static NSString *NSSendsWholeSearchString = @"NSSendsWholeSearchString";
+static NSString *NSMaximumRecents = @"NSMaximumRecents";
+
+
 - (void) encodeWithCoder: (NSCoder*)aCoder
 {
   [super encodeWithCoder: aCoder];
 
-  [aCoder encodeObject: _search_button_cell];
-  [aCoder encodeObject: _cancel_button_cell];
-  [aCoder encodeObject: _recents_autosave_name];
-  [aCoder encodeValueOfObjCType: @encode(BOOL)
-     at: &_sends_whole_search_string];
-  [aCoder encodeValueOfObjCType: @encode(unsigned int)
-     at: &_max_recents];
+  if ([aCoder allowsKeyedCoding])
+    {
+      [aCoder encodeObject: _search_button_cell forKey: NSSearchButtonCell];
+      [aCoder encodeObject: _cancel_button_cell forKey: NSCancelButtonCell];
+      [aCoder encodeObject: _recents_autosave_name forKey: NSRecentsAutosaveName];
+      [aCoder encodeBool: _sends_whole_search_string forKey: NSSendsWholeSearchString];
+      [aCoder encodeInt: _max_recents forKey: NSMaximumRecents];
+    }
+  else
+    {
+      [aCoder encodeObject: _search_button_cell];
+      [aCoder encodeObject: _cancel_button_cell];
+      [aCoder encodeObject: _recents_autosave_name];
+      [aCoder encodeValueOfObjCType: @encode(BOOL)
+                                 at: &_sends_whole_search_string];
+      [aCoder encodeValueOfObjCType: @encode(int)
+                                 at: &_max_recents];
+    }
 }
 
 - (id) initWithCoder: (NSCoder*)aDecoder
 {
   self = [super initWithCoder: aDecoder];
 
-  if(self != nil)
+  if (self != nil)
     {
       if ([aDecoder allowsKeyedCoding])
  {
-  [self setSearchButtonCell: [aDecoder decodeObjectForKey: @"NSSearchButtonCell"]];
-  [self setCancelButtonCell: [aDecoder decodeObjectForKey: @"NSCancelButtonCell"]];
-  [self setRecentsAutosaveName: [aDecoder decodeObjectForKey: @"NSRecentsAutosaveName"]];
-  [self setSendsWholeSearchString: [aDecoder decodeBoolForKey: @"NSSendsWholeSearchString"]];
-  [self setMaximumRecents: [aDecoder decodeIntForKey: @"NSMaximumRecents"]];
+  [self setSearchButtonCell: [aDecoder decodeObjectForKey: NSSearchButtonCell]];
+  [self setCancelButtonCell: [aDecoder decodeObjectForKey: NSCancelButtonCell]];
+  [self setRecentsAutosaveName: [aDecoder decodeObjectForKey: NSRecentsAutosaveName]];
+  [self setSendsWholeSearchString: [aDecoder decodeBoolForKey: NSSendsWholeSearchString]];
+  [self setMaximumRecents: [aDecoder decodeIntForKey: NSMaximumRecents]];
  }
       else
  {
@@ -479,7 +500,7 @@
   [self setCancelButtonCell: [aDecoder decodeObject]];
   [self setRecentsAutosaveName: [aDecoder decodeObject]];
   [aDecoder decodeValueOfObjCType: @encode(BOOL) at: &_sends_whole_search_string];
-  [aDecoder decodeValueOfObjCType: @encode(unsigned int) at: &_max_recents];
+  [aDecoder decodeValueOfObjCType: @encode(int) at: &_max_recents];
  }
       
       [self resetCancelButtonCell];


_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Re: [PATCH] Working NSSearchFieldCell archiving support

by Fred Kiefer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I committed a slightly different patch, to keep the size of the class
the same. Please check that I didn't break anything.

Fred

Quentin Mathé schrieb:

> Hi,
>
> Here is a patch to fix NSSearchFieldCell archiving and keyed archiving.
> Non-keyed archiving was crashing because _max_recents ivar was declared as
> an unsigned char and encoded/decoded as an unsigned int. Keyed archiving was
> not crashing because keyed decoding was not implemented.  The patch adds
> keyed encoding to -encodeWithCoder:.
>
> In a nib (a xib precisely), NSMaximumRecents is encoded as an int, so I
> switched to int as the encoded type (rather than unsigned int).
> I also changed the ivar from unsigned char to int to keep the
> encoding/decoding as simple as possible. I doubt any application has more
> than 4 or 5 search fields in use at the same time so it doesn't seem worth
> to save some memory.
>
> Cheers,
> Quentin.


_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Re: [PATCH] Working NSSearchFieldCell archiving support

by Quentin Mathé-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Le 7 sept. 2009 à 16:10, Fred Kiefer a écrit :

> I committed a slightly different patch, to keep the size of the class
> the same. Please check that I didn't break anything.

I just tested it. Both keyed and non-keyed archiving works fine.  
Thanks :-)

The only thing I'm not convinced is the change of the NSMenuItem type  
to id <NSMenuItem> if it's only a purpose is to eliminate the  
NSValidatedUserInterfaceItem related warning.
The change is fine if GNUstep is going to continue to support  
NSMenuItem protocol though.

I'd like to suggest to make NSMenuItem protocol conforms to  
NSValidatedUserInterfaceItemProtocol as Apple did:
http://developer.apple.com/legacy/mac/library/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSMenuItem_Protocol/Reference/Reference.html

The change is really simple:
@protocol NSMenuItem <NSValidatedUserInterfaceItem, NSCopying,  
NSCoding, NSObject>

This would eliminate warnings in many GNUstep applications (e.g. Gorm)  
which use the NSMenuItem class type.

Quentin.

_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev

Re: [PATCH] Working NSSearchFieldCell archiving support

by Fred Kiefer :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Quentin Mathé schrieb:

> Le 7 sept. 2009 à 16:10, Fred Kiefer a écrit :
>
>> I committed a slightly different patch, to keep the size of the class
>> the same. Please check that I didn't break anything.
>
> I just tested it. Both keyed and non-keyed archiving works fine. Thanks :-)
>
> The only thing I'm not convinced is the change of the NSMenuItem type to
> id <NSMenuItem> if it's only a purpose is to eliminate the
> NSValidatedUserInterfaceItem related warning.
> The change is fine if GNUstep is going to continue to support NSMenuItem
> protocol though.
>
> I'd like to suggest to make NSMenuItem protocol conforms to
> NSValidatedUserInterfaceItemProtocol as Apple did:
> http://developer.apple.com/legacy/mac/library/documentation/Cocoa/Reference/ApplicationKit/Protocols/NSMenuItem_Protocol/Reference/Reference.html
>
>
> The change is really simple:
> @protocol NSMenuItem <NSValidatedUserInterfaceItem, NSCopying, NSCoding,
> NSObject>
>
> This would eliminate warnings in many GNUstep applications (e.g. Gorm)
> which use the NSMenuItem class type.

Thank you, done.

Fred


_______________________________________________
Gnustep-dev mailing list
Gnustep-dev@...
http://lists.gnu.org/mailman/listinfo/gnustep-dev