diff --git a/AppKit/CPPopUpButton.j b/AppKit/CPPopUpButton.j index 77b3474c6..7c29c19e7 100644 --- a/AppKit/CPPopUpButton.j +++ b/AppKit/CPPopUpButton.j @@ -49,6 +49,11 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); return "popup-button"; } ++ (CPSet)keyPathsForValuesAffectingSelectedTag +{ + return [CPSet setWithObject:CPValueBinding]; +} + /*! Initializes the pop-up button to the specified size. @param aFrame the size for the button @@ -233,6 +238,32 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); return _selectedIndex; } +/*! + @ignore +*/ +- (int)selectedTag +{ + return [[self selectedItem] tag]; +} + +/*! + @ignore +*/ +- (void)setSelectedTag:(int)aTag +{ + [self selectItemWithTag:aTag]; +} + +/*! + @ignore +*/ +- (void)_reverseSetBinding +{ + var theBinding = [CPKeyValueBinding getBinding:@"selectedTag" forObject:self]; + [theBinding reverseSetValueFor:@"selectedTag"]; + [super _reverseSetBinding]; +} + // Setting the Current Selection /*! Selects the specified menu item. @@ -252,6 +283,8 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); if (_selectedIndex == anIndex) return; + [self willChangeValueForKey:CPValueBinding]; + if (_selectedIndex >= 0 && ![self pullsDown]) [[self selectedItem] setState:CPOffState]; @@ -261,6 +294,8 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); [[self selectedItem] setState:CPOnState]; [self synchronizeTitleAndSelectedItem]; + + [self didChangeValueForKey:CPValueBinding]; } /*! diff --git a/Tests/AppKit/CPPopUpButtonTest.j b/Tests/AppKit/CPPopUpButtonTest.j index c45085633..e00e14471 100644 --- a/Tests/AppKit/CPPopUpButtonTest.j +++ b/Tests/AppKit/CPPopUpButtonTest.j @@ -20,4 +20,33 @@ [self assert:["Option A", "Option B"] equals:[button itemTitles]]; } +- (void)testBindingSelectedTag +{ + var dict = [CPDictionary dictionary], + item = nil; + + item = [[CPMenuItem alloc] initWithTitle:@"Mouse" action:nil keyEquivalent:nil]; + [item setTag:20]; + [button addItem:item]; + + item = [[CPMenuItem alloc] initWithTitle:@"Elephant" action:nil keyEquivalent:nil]; + [item setTag:4990000]; + [button addItem:item]; + + [dict setObject:20 forKey:@"weight"]; + + [button bind:@"selectedTag" + toObject:dict + withKeyPath:@"weight" + options:nil]; + + [self assert:20 equals:[button selectedTag]]; + + [button selectItemAtIndex:1]; + // simulate selection by the user + [button sendAction:[button action] to:[button target]]; + + [self assert:4990000 equals:[dict objectForKey:@"weight"]]; +} + @end