From 2dee33a74e58808b456d7ba245b6f2db519a2abe Mon Sep 17 00:00:00 2001 From: fredkiefer Date: Sun, 23 Dec 2007 22:13:24 +0000 Subject: [PATCH] Add KVB for menu item and popup button. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@25777 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 +++++ Source/NSMenuItem.m | 37 ++++++++++++++++++++++++++++++++++--- Source/NSPopUpButton.m | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index fad403ea7..fc84d3147 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-12-23 Fred Kiefer + + * Source/NSMenuItem.m: Add KVB for enable. + * Source/NSPopUpButton.m: Add KVB for selectedIndex. + 2007-12-22 Fred Kiefer * Source/NSView.m (-visibleRect:, -setHidden:, -hitTest:): Full diff --git a/Source/NSMenuItem.m b/Source/NSMenuItem.m index 4e3d4229e..12f32b0bc 100644 --- a/Source/NSMenuItem.m +++ b/Source/NSMenuItem.m @@ -34,11 +34,13 @@ #include #include #include +#include "AppKit/NSCell.h" +#include "AppKit/NSEvent.h" +#include "AppKit/NSImage.h" +#include "AppKit/NSKeyValueBinding.h" #include "AppKit/NSMenuItem.h" #include "AppKit/NSMenu.h" -#include "AppKit/NSImage.h" -#include "AppKit/NSEvent.h" -#include "AppKit/NSCell.h" +#include "GSBindingHelpers.h" static BOOL usesUserKeyEquivalents = NO; static Class imageClass; @@ -76,6 +78,8 @@ static Class imageClass; { [self setVersion: 4]; imageClass = [NSImage class]; + + [self exposeBinding: NSEnabledBinding]; } } @@ -103,6 +107,9 @@ static Class imageClass; - (void) dealloc { + // Remove all key value bindings for this view. + [GSKeyValueBinding unbindAllForObject: self]; + TEST_RELEASE(_title); TEST_RELEASE(_keyEquivalent); TEST_RELEASE(_image); @@ -648,6 +655,30 @@ static Class imageClass; return self; } +- (void) bind: (NSString *)binding + toObject: (id)anObject + withKeyPath: (NSString *)keyPath + options: (NSDictionary *)options +{ + if ([binding hasPrefix: NSEnabledBinding]) + { + [self unbind: binding]; + [[GSKeyValueAndBinding alloc] initWithBinding: NSEnabledBinding + withName: binding + toObject: anObject + withKeyPath: keyPath + options: options + fromObject: self]; + } + else + { + [super bind: binding + toObject: anObject + withKeyPath: keyPath + options: options]; + } +} + @end @implementation NSMenuItem (GNUstepExtra) diff --git a/Source/NSPopUpButton.m b/Source/NSPopUpButton.m index 9a2bdd246..1a366d2c9 100644 --- a/Source/NSPopUpButton.m +++ b/Source/NSPopUpButton.m @@ -29,8 +29,10 @@ */ #include +#include #include "AppKit/NSApplication.h" #include "AppKit/NSEvent.h" +#include "AppKit/NSKeyValueBinding.h" #include "AppKit/NSPopUpButton.h" #include "AppKit/NSPopUpButtonCell.h" #include "AppKit/NSMenu.h" @@ -58,6 +60,8 @@ Class _nspopupbuttonCellClass = 0; // Initial version [self setVersion: 1]; [self setCellClass: [NSPopUpButtonCell class]]; + + [self exposeBinding: NSSelectedIndexBinding]; } } @@ -249,7 +253,9 @@ this to return nil to indicate that we have no context menu. - (void) selectItem: (id )anObject { + [self willChangeValueForKey: NSSelectedIndexBinding]; [_cell selectItem: anObject]; + [self didChangeValueForKey: NSSelectedIndexBinding]; [self synchronizeTitleAndSelectedItem]; } @@ -259,7 +265,9 @@ this to return nil to indicate that we have no context menu. */ - (void) selectItemAtIndex: (int)index { + [self willChangeValueForKey: NSSelectedIndexBinding]; [_cell selectItemAtIndex: index]; + [self didChangeValueForKey: NSSelectedIndexBinding]; [self synchronizeTitleAndSelectedItem]; } @@ -270,7 +278,9 @@ this to return nil to indicate that we have no context menu. */ - (void) selectItemWithTitle: (NSString*)title { + [self willChangeValueForKey: NSSelectedIndexBinding]; [_cell selectItemWithTitle: title]; + [self didChangeValueForKey: NSSelectedIndexBinding]; [self synchronizeTitleAndSelectedItem]; } @@ -517,4 +527,28 @@ this to return nil to indicate that we have no context menu. [super keyDown: theEvent]; } +- (void) setValue: (id)anObject forKey: (NSString*)aKey +{ + if ([aKey isEqual: NSSelectedIndexBinding]) + { + [self selectItemAtIndex: [anObject intValue]]; + } + else + { + [super setValue: anObject forKey: aKey]; + } +} + +- (id) valueForKey: (NSString*)aKey +{ + if ([aKey isEqual: NSSelectedIndexBinding]) + { + return [NSNumber numberWithInt: [self indexOfSelectedItem]]; + } + else + { + return [super valueForKey: aKey]; + } +} + @end