From 688aed72cb5d5b01f34162a830543abcd3a5e7d9 Mon Sep 17 00:00:00 2001 From: Francisco Ryan Tolmasky I Date: Tue, 18 Jan 2011 23:09:43 -0800 Subject: [PATCH] Fix for CPPopUpButton's encoded prior to CPResponder/menu change. Reviewed by me. --- AppKit/CPControl.j | 6 ++- AppKit/CPMenuItem/_CPMenuItemView.j | 1 - AppKit/CPPopUpButton.j | 62 +++++++++++++++-------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/AppKit/CPControl.j b/AppKit/CPControl.j index 806091ed4..1f6bb8269 100644 --- a/AppKit/CPControl.j +++ b/AppKit/CPControl.j @@ -753,8 +753,10 @@ var __Deprecated__CPImageViewImageKey = @"CPImageViewImageKey"; if (_sendsActionOnEndEditing) [aCoder encodeBool:_sendsActionOnEndEditing forKey:CPControlSendsActionOnEndEditingKey]; - if (_value !== nil) - [aCoder encodeObject:_value forKey:CPControlValueKey]; + var objectValue = [self objectValue]; + + if (objectValue !== nil) + [aCoder encodeObject:objectValue forKey:CPControlValueKey]; if (_target !== nil) [aCoder encodeConditionalObject:_target forKey:CPControlTargetKey]; diff --git a/AppKit/CPMenuItem/_CPMenuItemView.j b/AppKit/CPMenuItem/_CPMenuItemView.j index 645ae004e..ad25e138f 100644 --- a/AppKit/CPMenuItem/_CPMenuItemView.j +++ b/AppKit/CPMenuItem/_CPMenuItemView.j @@ -34,7 +34,6 @@ var _CPMenuItemSelectionColor = nil, BOOL _isDirty; BOOL _showsStateColumn; - CPImageView _stateView; _CPImageAndTextView _imageAndTextView; CPView _submenuView; } diff --git a/AppKit/CPPopUpButton.j b/AppKit/CPPopUpButton.j index d38387beb..1ff5e1d3e 100644 --- a/AppKit/CPPopUpButton.j +++ b/AppKit/CPPopUpButton.j @@ -64,7 +64,8 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); if (self) { - _selectedIndex = CPNotFound; + [self selectItemAtIndex:CPNotFound]; + _preferredEdge = CPMaxYEdge; [self setValue:CPImageLeft forThemeAttribute:@"image-position"]; @@ -238,15 +239,6 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); return _selectedIndex; } -// For us, CPNumber is toll-free bridged to Number, so just return the selected index. -/*! - Returns the selected item's index. If no item is selected, it returns CPNotFound. -*/ -- (id)objectValue -{ - return _selectedIndex; -} - /*! @ignore */ @@ -277,21 +269,23 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); Selects the item at the specified index @param anIndex the index of the item to select */ -- (void)selectItemAtIndex:(int)anIndex +- (void)selectItemAtIndex:(CPUInteger)anIndex { - anIndex = +anIndex; + var indexOfSelectedItem = [self indexOfSelectedItem]; - if (_selectedIndex === anIndex) + anIndex = parseInt(+anIndex, 10); + + if (indexOfSelectedItem === anIndex) return; [self willChangeValueForKey:@"selectedIndex"]; - if (_selectedIndex >= 0 && ![self pullsDown]) + if (indexOfSelectedItem >= 0 && ![self pullsDown]) [[self selectedItem] setState:CPOffState]; _selectedIndex = anIndex; - if (_selectedIndex >= 0 && ![self pullsDown]) + if (indexOfSelectedItem >= 0 && ![self pullsDown]) [[self selectedItem] setState:CPOnState]; [self synchronizeTitleAndSelectedItem]; @@ -299,6 +293,20 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); [self didChangeValueForKey:@"selectedIndex"]; } +/*! + Selects the item at the specified index + @param anIndex the index of the item to select +*/ +- (void)setObjectValue:(int)anIndex +{ + [self selectItemAtIndex:anIndex]; +} + +- (id)objectValue +{ + return [self indexOfSelectedItem]; +} + /*! Selects the menu item with the specified tag @param the tag of the item to select @@ -317,15 +325,6 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); [self selectItemAtIndex:[self indexOfItemWithTitle:aTitle]]; } -/*! - Sets the object for the selected item. If no item is selected, then this method has no effect. - @param the object set for the selected item -*/ -- (void)setObjectValue:(id)aValue -{ - [self selectItemAtIndex:[aValue intValue]]; -} - // Getting Menu Items /*! @@ -608,7 +607,7 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); { [self _firstItemDidChange]; - _selectedIndex = -2; + [self selectItemAtIndex:CPNotFound]; [self selectItemAtIndex:MIN([newItems count] - 1, indexOfSelectedItem)]; } @@ -763,7 +762,7 @@ CPPopUpButtonStatePullsDown = CPThemeState("pulls-down"); @end var CPPopUpButtonSelectedIndexKey = @"CPPopUpButtonSelectedIndexKey", - CPPopUpButtonPullsDownKey = @"CPPopUpButtonPullsDownKey"; + DEPRECATED_CPPopUpButtonMenuKey = @"CPPopUpButtonMenuKey"; @implementation CPPopUpButton (CPCoding) /*! @@ -779,15 +778,20 @@ var CPPopUpButtonSelectedIndexKey = @"CPPopUpButtonSelectedIndexKey", if (self) { - // Nothing is currently selected - _selectedIndex = CPNotFound; + // FIXME: Remove for 1.0 + if ([aCoder containsValueForKey:DEPRECATED_CPPopUpButtonMenuKey]) + { + CPLog.warn(self + " was encoded with an older version of Cappuccino. Please nib2cib the original nib again or open and re-save in Atlas."); + [self setMenu:[aCoder decodeObjectForKey:DEPRECATED_CPPopUpButtonMenuKey]]; + } - [self selectItemAtIndex:[aCoder decodeObjectForKey:CPPopUpButtonSelectedIndexKey]]; + [self selectItemAtIndex:[aCoder decodeIntForKey:CPPopUpButtonSelectedIndexKey]]; var options = CPKeyValueObservingOptionNew | CPKeyValueObservingOptionOld;/* | CPKeyValueObservingOptionInitial; */ + [self addObserver:self forKeyPath:@"menu.items" options:options context:nil]; [self addObserver:self forKeyPath:@"_firstItem.changeCount" options:options context:nil]; [self addObserver:self forKeyPath:@"selectedItem.changeCount" options:options context:nil];