mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-04-25 02:41:01 +00:00
Undid last change. Rewrote [setMenu:] to make the popup button
cell listen to change notifications of the menu. New method [itemChanged:] gets called when a menu item changes. Added hack to [initWithCoder:] to set menu correctly and read old menu items correctly. Check in [itemAtIndex:] that the index is vaild and return nil otherwise. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@17661 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
2882fa9d28
commit
aa2a156b1c
1 changed files with 75 additions and 10 deletions
|
@ -83,15 +83,57 @@ static NSImage *_pbc_image[2];
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void) dealloc
|
||||||
|
{
|
||||||
|
[self selectItem: nil];
|
||||||
|
[self setMenu: nil];
|
||||||
|
[super dealloc];
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Notification Method
|
||||||
|
*/
|
||||||
|
|
||||||
|
- (void) itemChanged: (NSNotification*)notification
|
||||||
|
{
|
||||||
|
int index = [[[notification userInfo] objectForKey: @"NSMenuItemIndex"]
|
||||||
|
intValue];
|
||||||
|
id <NSMenuItem> item = [self itemAtIndex: index];
|
||||||
|
|
||||||
|
if (item == _menuItem)
|
||||||
|
{
|
||||||
|
[self setEnabled: [[self menuItem] isEnabled]];
|
||||||
|
[[self controlView] setNeedsDisplay: YES];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- (void) setMenu: (NSMenu *)menu
|
- (void) setMenu: (NSMenu *)menu
|
||||||
{
|
{
|
||||||
[_menu _setOwnedByPopUp: nil];
|
if (_menu == menu)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_menu != nil)
|
||||||
|
{
|
||||||
|
[_menu _setOwnedByPopUp: nil];
|
||||||
|
[[NSNotificationCenter defaultCenter] removeObserver: self
|
||||||
|
name: nil
|
||||||
|
object: _menu];
|
||||||
|
}
|
||||||
ASSIGN(_menu, menu);
|
ASSIGN(_menu, menu);
|
||||||
[_menu _setOwnedByPopUp: self];
|
if (_menu != nil)
|
||||||
/* We need to set the menu view so we trigger the special case
|
{
|
||||||
* popupbutton code in super class NSMenuItemCell
|
[_menu _setOwnedByPopUp: self];
|
||||||
*/
|
[[NSNotificationCenter defaultCenter] addObserver: self
|
||||||
[self setMenuView: [_menu menuRepresentation]];
|
selector: @selector(itemChanged:)
|
||||||
|
name: NSMenuDidChangeItemNotification
|
||||||
|
object: _menu];
|
||||||
|
/* We need to set the menu view so we trigger the special case
|
||||||
|
* popupbutton code in super class NSMenuItemCell
|
||||||
|
*/
|
||||||
|
[self setMenuView: [_menu menuRepresentation]];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSMenu *) menu
|
- (NSMenu *) menu
|
||||||
|
@ -305,7 +347,14 @@ static NSImage *_pbc_image[2];
|
||||||
|
|
||||||
- (id <NSMenuItem>) itemAtIndex: (int)index
|
- (id <NSMenuItem>) itemAtIndex: (int)index
|
||||||
{
|
{
|
||||||
return [_menu itemAtIndex: index];
|
if ((index > 0) && (index < [_menu numberOfItems]))
|
||||||
|
{
|
||||||
|
return [_menu itemAtIndex: index];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
retun nil;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (id <NSMenuItem>) itemWithTitle: (NSString *)title
|
- (id <NSMenuItem>) itemWithTitle: (NSString *)title
|
||||||
|
@ -374,6 +423,8 @@ static NSImage *_pbc_image[2];
|
||||||
{
|
{
|
||||||
[_menuItem setImage: image];
|
[_menuItem setImage: image];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[self setEnabled: [_menuItem isEnabled]];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dealing with selection
|
// Dealing with selection
|
||||||
|
@ -676,9 +727,6 @@ static NSImage *_pbc_image[2];
|
||||||
new = YES;
|
new = YES;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Enable the menu item cell */
|
|
||||||
[self setEnabled: [[self menuItem] isEnabled]];
|
|
||||||
|
|
||||||
/* We need to calc our size to get images placed correctly */
|
/* We need to calc our size to get images placed correctly */
|
||||||
[self calcSize];
|
[self calcSize];
|
||||||
[super drawInteriorWithFrame: cellFrame inView: controlView];
|
[super drawInteriorWithFrame: cellFrame inView: controlView];
|
||||||
|
@ -786,6 +834,13 @@ static NSImage *_pbc_image[2];
|
||||||
|
|
||||||
self = [super initWithCoder: aDecoder];
|
self = [super initWithCoder: aDecoder];
|
||||||
menu = [aDecoder decodeObject];
|
menu = [aDecoder decodeObject];
|
||||||
|
/*
|
||||||
|
FIXME: This same ivar already gets set in NSCell initWithCoder,
|
||||||
|
but there it is used directly not via a method call. So here we first
|
||||||
|
unset it and than set it again as our setMenu: method tries to optimize
|
||||||
|
duplicate calls.
|
||||||
|
*/
|
||||||
|
[self setMenu: nil];
|
||||||
[self setMenu: menu];
|
[self setMenu: menu];
|
||||||
selectedItem = [aDecoder decodeObject];
|
selectedItem = [aDecoder decodeObject];
|
||||||
[aDecoder decodeValueOfObjCType: @encode(int) at: &flag];
|
[aDecoder decodeValueOfObjCType: @encode(int) at: &flag];
|
||||||
|
@ -801,11 +856,21 @@ static NSImage *_pbc_image[2];
|
||||||
|
|
||||||
if (version < 2)
|
if (version < 2)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
// Not the stored format did change but the interpretation of it.
|
// Not the stored format did change but the interpretation of it.
|
||||||
// in version 1 most of the ivars were not used, so their values may
|
// in version 1 most of the ivars were not used, so their values may
|
||||||
// be arbitray. We overwrite them with valid settings.
|
// be arbitray. We overwrite them with valid settings.
|
||||||
[self setPullsDown: _pbcFlags.pullsDown];
|
[self setPullsDown: _pbcFlags.pullsDown];
|
||||||
_pbcFlags.usesItemFromMenu = YES;
|
_pbcFlags.usesItemFromMenu = YES;
|
||||||
|
|
||||||
|
for (i = 0; i < [_menu numberOfItems]; i++)
|
||||||
|
{
|
||||||
|
id <NSMenuItem> anItem = [menu itemAtIndex: i];
|
||||||
|
|
||||||
|
[anItem setOnStateImage: nil];
|
||||||
|
[anItem setMixedStateImage: nil];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[self selectItem: selectedItem];
|
[self selectItem: selectedItem];
|
||||||
|
|
Loading…
Reference in a new issue