Allow binding to CPPopUpButton's selectedTag.

This commit is contained in:
Brian Donovan
2010-12-03 16:12:22 -08:00
parent 175f1a9acd
commit 79c2edca2d
2 changed files with 64 additions and 0 deletions
+35
View File
@@ -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];
}
/*!
+29
View File
@@ -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