diff --git a/ChangeLog b/ChangeLog index 277b3bccf..b3471dff1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +Thu Nov 25 11:44:00 1999 Richard Frith-Macdonald + + * Source/NSMenu.m: Tidying, performnce tweaks, memory leak fixes. + * Source/NSPopUpButton.m: Tidied. + * Headers/AppKit/NSPopUpButtonCell.h: Use _selectedItem ivar rather + than _selectedIndex. + * Source/NSPopUpButtonCell.m: Use _selectedItem ivar rather than + _selectedIndex, fix memory leaks, and various hacks to stop things + crashing. + Thu Nov 25 1999 Nicola Pero * Source/NSMenuView.m ([-trackWithEvent:]): Fixed bug which was diff --git a/Headers/gnustep/gui/NSPopUpButtonCell.h b/Headers/gnustep/gui/NSPopUpButtonCell.h index bcaf13968..e4261c6ba 100644 --- a/Headers/gnustep/gui/NSPopUpButtonCell.h +++ b/Headers/gnustep/gui/NSPopUpButtonCell.h @@ -11,17 +11,17 @@ typedef enum { @interface NSPopUpButtonCell : NSMenuItemCell { - NSMenu *_menu; - int _selectedIndex; - struct __pbcFlags { - unsigned int pullsDown:1; - unsigned int preferredEdge:3; - unsigned int menuIsAttached:1; - unsigned int usesItemFromMenu:1; - unsigned int altersStateOfSelectedItem:1; - unsigned int decoding:1; - unsigned int arrowPosition:2; - } _pbcFlags; + NSMenu *_menu; + NSMenuItem *_selectedItem; + struct __pbcFlags { + unsigned int pullsDown:1; + unsigned int preferredEdge:3; + unsigned int menuIsAttached:1; + unsigned int usesItemFromMenu:1; + unsigned int altersStateOfSelectedItem:1; + unsigned int decoding:1; + unsigned int arrowPosition:2; + } _pbcFlags; } - (id)initTextCell:(NSString *)stringValue pullsDown:(BOOL)pullDown; diff --git a/Source/NSMenu.m b/Source/NSMenu.m index 97b1cca6e..50f944c0b 100644 --- a/Source/NSMenu.m +++ b/Source/NSMenu.m @@ -91,11 +91,26 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; return menu_is_beholdenToPopUpButton; } -- (void)_setOwnedByPopUp:(BOOL)flag +- (void)_setOwnedByPopUp: (BOOL)flag { menu_is_beholdenToPopUpButton = flag; } +- (void) dealloc +{ + [[NSNotificationCenter defaultCenter] removeObserver: self]; + + RELEASE(menu_notifications); + RELEASE(menu_title); + RELEASE(menu_items); + RELEASE(menu_view); + RELEASE(aWindow); + RELEASE(bWindow); + RELEASE(titleView); + + [super dealloc]; +} + - (id) initWithTitle: (NSString *)aTitle { NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter]; @@ -171,7 +186,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; return; d = [NSDictionary - dictionaryWithObject: [NSNumber numberWithInt:index] + dictionaryWithObject: [NSNumber numberWithInt: index] forKey: @"NSMenuItemIndex"]; [menu_items insertObject: newItem atIndex: index]; @@ -224,7 +239,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; // Insert the new item into the stream. - [self insertItem:anItem atIndex:index]; + [self insertItem: anItem atIndex: index]; // For returns sake. @@ -248,21 +263,21 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; - (void) removeItem: (id )anItem { - [self removeItemAtIndex:[menu_items indexOfObject: anItem]]; + [self removeItemAtIndex: [menu_items indexOfObjectIdenticalTo: anItem]]; } - (void) removeItemAtIndex: (int)index { - NSNotification *removed; - NSDictionary *d; - id anItem = [menu_items objectAtIndex:index]; + NSNotification *removed; + NSDictionary *d; + id anItem = [menu_items objectAtIndex: index]; if (!anItem) return; if ([anItem isKindOfClass: [NSMenuItem class]]) { - d = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:index] + d = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: index] forKey: @"NSMenuItemIndex"]; [anItem setMenu: nil]; @@ -293,7 +308,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; d = [NSDictionary dictionaryWithObject: [NSNumber - numberWithInt:[self indexOfItem: anObject]] + numberWithInt: [self indexOfItem: anObject]] forKey: @"NSMenuItemIndex"]; changed = [NSNotification @@ -347,7 +362,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; { if (index >= [menu_items count] || index < 0) [NSException raise: NSRangeException - format: @"Range error in method -itemAtIndex:"]; + format: @"Range error in method -itemAtIndex: "]; return [menu_items objectAtIndex: index]; } @@ -367,7 +382,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; // - (int) indexOfItem: (id )anObject { - return [menu_items indexOfObject: anObject]; + return [menu_items indexOfObjectIdenticalTo: anObject]; } - (int) indexOfItemWithTitle: (NSString *)aTitle @@ -375,7 +390,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; id anItem; if ((anItem = [self itemWithTitle: aTitle])) - return [menu_items indexOfObject: anItem]; + return [menu_items indexOfObjectIdenticalTo: anItem]; else return -1; } @@ -385,13 +400,13 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; id anItem; if ((anItem = [self itemWithTag: aTag])) - return [menu_items indexOfObject: anItem]; + return [menu_items indexOfObjectIdenticalTo: anItem]; else return -1; } - (int) indexOfItemWithTarget: (id)anObject - andAction: (SEL)actionSelector + andAction: (SEL)actionSelector { unsigned i; unsigned count = [menu_items count]; @@ -414,8 +429,8 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; for (i = 0; i < count; i++) { - if ([[[menu_items objectAtIndex:i] representedObject] - isEqual:anObject]) + if ([[[menu_items objectAtIndex: i] representedObject] + isEqual: anObject]) { return i; } @@ -430,8 +445,8 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; for (i = 0; i < count; i++) { - if ([[[menu_items objectAtIndex:i] title] - isEqual:[anObject title]]) + if ([[[menu_items objectAtIndex: i] title] + isEqual: [anObject title]]) { return i; } @@ -509,9 +524,9 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; if (NSInterfaceStyleForKey(@"NSMenuInterfaceStyle", nil) == GSWindowMakerInterfaceStyle) { - NSRect aRect = [menu_view rectOfItemAtIndex: - [self indexOfItemWithTitle:[aSubmenu title]]]; - NSPoint subOrigin = [win_link convertBaseToScreen: + NSRect aRect = [menu_view rectOfItemAtIndex: + [self indexOfItemWithTitle: [aSubmenu title]]]; + NSPoint subOrigin = [win_link convertBaseToScreen: NSMakePoint(aRect.origin.x, aRect.origin.y)]; @@ -871,13 +886,13 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; #define IS_OFFSCREEN(WINDOW) \ !(NSContainsRect([[NSScreen mainScreen] frame], [WINDOW frame])) -- (void) _setTornOff:(BOOL)flag +- (void) _setTornOff: (BOOL)flag { NSMenu *supermenu = [self supermenu]; menu_is_tornoff = flag; - [[supermenu menuRepresentation] setHighlightedItemIndex:-1]; + [[supermenu menuRepresentation] setHighlightedItemIndex: -1]; supermenu->menu_attachedMenu = nil; } @@ -899,7 +914,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; if (array && [array isKindOfClass: [NSArray class]]) { [titleView windowBecomeTornOff]; - [self _setTornOff:YES]; + [self _setTornOff: YES]; [self display]; } } @@ -924,7 +939,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow); } -- (void) _performMenuClose:(id)sender +- (void) _performMenuClose: (id)sender { NSUserDefaults* defaults; NSMutableDictionary* menuLocations; @@ -935,7 +950,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; [menu_view setHighlightedItemIndex: -1]; - [self _setTornOff:NO]; + [self _setTornOff: NO]; [self close]; [titleView _releaseCloseButton]; @@ -964,7 +979,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; if (menu_supermenu && ![self isTornOff]) // query super menu for { // position - [aWindow setFrameOrigin:[menu_supermenu locationForSubmenu: self]]; + [aWindow setFrameOrigin: [menu_supermenu locationForSubmenu: self]]; menu_supermenu->menu_attachedMenu = self; } else @@ -995,15 +1010,15 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; float aPoint = [[NSScreen mainScreen] frame].size.height - [aWindow frame].size.height; - [aWindow setFrameOrigin:NSMakePoint(0,aPoint)]; - [bWindow setFrameOrigin:NSMakePoint(0,aPoint)]; + [aWindow setFrameOrigin: NSMakePoint(0,aPoint)]; + [bWindow setFrameOrigin: NSMakePoint(0,aPoint)]; } } } menu_is_visible = YES; - [aWindow orderFront:nil]; + [aWindow orderFront: nil]; menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow); } @@ -1029,17 +1044,17 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; if (menu_is_tornoff) [titleView _releaseCloseButton]; - [[bWindow contentView] addSubview:menu_view]; - [[bWindow contentView] addSubview:titleView]; + [[bWindow contentView] addSubview: menu_view]; + [[bWindow contentView] addSubview: titleView]; - [bWindow orderFront:self]; + [bWindow orderFront: self]; menu_isPartlyOffScreen = IS_OFFSCREEN(bWindow); } - (void) close { - [aWindow orderOut:self]; + [aWindow orderOut: self]; menu_is_visible = NO; if (menu_supermenu) @@ -1048,18 +1063,18 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; - (void) closeTransient { - [bWindow orderOut:self]; + [bWindow orderOut: self]; [menu_view removeFromSuperviewWithoutNeedingDisplay]; [titleView removeFromSuperviewWithoutNeedingDisplay]; - [[aWindow contentView] addSubview:menu_view]; + [[aWindow contentView] addSubview: menu_view]; if (menu_is_tornoff) [titleView _addCloseButton]; - [[aWindow contentView] addSubview:titleView]; + [[aWindow contentView] addSubview: titleView]; - [[aWindow contentView] setNeedsDisplay:YES]; + [[aWindow contentView] setNeedsDisplay: YES]; // Restore the old submenu. menu_supermenu->menu_attachedMenu = _oldAttachedMenu; @@ -1297,7 +1312,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; if (![menu isTornOff] && [menu supermenu]) { [self windowBecomeTornOff]; - [menu _setTornOff:YES]; + [menu _setTornOff: YES]; } while (!done) @@ -1309,7 +1324,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; switch ([theEvent type]) { - case NSLeftMouseUp: + case NSLeftMouseUp: done = YES; break; case NSLeftMouseDragged: @@ -1325,7 +1340,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; } break; - default: + default: break; } } @@ -1338,7 +1353,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; if (!menuLocations) menuLocations = [NSMutableDictionary dictionaryWithCapacity: 2]; origin = [[menu window] frame].origin; - array = [NSArray arrayWithObjects: + array = [NSArray arrayWithObjects: [[NSNumber numberWithInt: origin.x] stringValue], [[NSNumber numberWithInt: origin.y] stringValue], nil]; @@ -1380,7 +1395,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; [self setAutoresizingMask: mask]; [button display]; - [self setNeedsDisplay:YES]; + [self setNeedsDisplay: YES]; } } @@ -1392,6 +1407,6 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations"; - (void) _addCloseButton { - [self addSubview:button]; + [self addSubview: button]; } @end /* NSMenuWindowTitleView */ diff --git a/Source/NSPopUpButton.m b/Source/NSPopUpButton.m index 8a24a6991..8b5444e5f 100644 --- a/Source/NSPopUpButton.m +++ b/Source/NSPopUpButton.m @@ -52,12 +52,12 @@ id _nspopupbuttonCellClass = nil; // // Class methods // -+ (void)initialize ++ (void) initialize { if (self == [NSPopUpButton class]) { // Initial version - [self setVersion:1]; + [self setVersion: 1]; [self setCellClass: [NSPopUpButtonCell class]]; } } @@ -65,22 +65,22 @@ id _nspopupbuttonCellClass = nil; // // Initializing an NSPopUpButton // -- (id)init +- (id) init { - return [self initWithFrame:NSZeroRect pullsDown:NO]; + return [self initWithFrame: NSZeroRect pullsDown: NO]; } -- (id)initWithFrame:(NSRect)frameRect +- (id) initWithFrame: (NSRect)frameRect { - return [self initWithFrame:frameRect pullsDown:NO]; + return [self initWithFrame: frameRect pullsDown: NO]; } -- (id)initWithFrame:(NSRect)frameRect - pullsDown:(BOOL)flag +- (id) initWithFrame: (NSRect)frameRect + pullsDown: (BOOL)flag { - NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; + NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter]; - [super initWithFrame:frameRect]; + [super initWithFrame: frameRect]; [defaultCenter addObserver: self selector: @selector(_popup:) @@ -90,208 +90,208 @@ id _nspopupbuttonCellClass = nil; return self; } -- (void)setMenu:(NSMenu *)menu +- (void) setMenu: (NSMenu *)menu { [cell setMenu: menu]; } -- (NSMenu *)menu +- (NSMenu *) menu { return [cell menu]; } -- (void)setPullsDown:(BOOL)flag +- (void) setPullsDown: (BOOL)flag { [cell setPullsDown: flag]; } -- (BOOL)pullsDown +- (BOOL) pullsDown { return [cell pullsDown]; } -- (void)setAutoenablesItems:(BOOL)flag +- (void) setAutoenablesItems: (BOOL)flag { [cell setAutoenablesItems: flag]; } -- (BOOL)autoenablesItems +- (BOOL) autoenablesItems { return [cell autoenablesItems]; } -- (void)addItemWithTitle:(NSString *)title +- (void) addItemWithTitle: (NSString *)title { [cell addItemWithTitle: title]; [self synchronizeTitleAndSelectedItem]; } -- (void)addItemsWithTitles:(NSArray *)itemTitles +- (void) addItemsWithTitles: (NSArray *)itemTitles { [cell addItemWithTitles: itemTitles]; [self synchronizeTitleAndSelectedItem]; } -- (void)insertItemWithTitle:(NSString *)title - atIndex:(int)index +- (void) insertItemWithTitle: (NSString *)title + atIndex: (int)index { [cell insertItemWithTitle: title adIndex: index]; [self synchronizeTitleAndSelectedItem]; } -- (void)removeAllItems +- (void) removeAllItems { [cell removeAllItems]; [self synchronizeTitleAndSelectedItem]; } -- (void)removeItemWithTitle:(NSString *)title +- (void) removeItemWithTitle: (NSString *)title { [cell removeItemWithTitle]; [self synchronizeTitleAndSelectedItem]; } -- (void)removeItemAtIndex:(int)index +- (void) removeItemAtIndex: (int)index { [cell removeItemAtIndex: index]; [self synchronizeTitleAndSelectedItem]; } -- (id )selectedItem +- (id ) selectedItem { return [cell selectedItem]; } -- (NSString *)titleOfSelectedItem +- (NSString *) titleOfSelectedItem { return [cell titleOfSelectedItem]; } -- (int)indexOfSelectedItem +- (int) indexOfSelectedItem { return [cell indexOfSelectedItem]; } -- (void)selectItem:(id )anObject +- (void) selectItem: (id )anObject { [cell selectedItem: anObject]; } -- (void)selectItemAtIndex:(int)index +- (void) selectItemAtIndex: (int)index { [cell selectItemAtIndex: index]; [self synchronizeTitleAndSelectedItem]; } -- (void)selectItemWithTitle:(NSString *)title +- (void) selectItemWithTitle: (NSString *)title { [cell selectItemWithTitle: title]; [self synchronizeTitleAndSelectedItem]; } -- (int)numberOfItems +- (int) numberOfItems { return [cell numberOfItems]; } -- (NSArray *)itemArray +- (NSArray *) itemArray { return [cell itemArray]; } -- (id )itemAtIndex:(int)index +- (id ) itemAtIndex: (int)index { return [cell itemAtIndex: index]; } -- (NSString *)itemTitleAtIndex:(int)index +- (NSString *) itemTitleAtIndex: (int)index { return [cell itemTitleAtIndex: index]; } -- (NSArray *)itemTitles +- (NSArray *) itemTitles { return [cell itemTitles]; } -- (id )itemWithTitle:(NSString *)title +- (id ) itemWithTitle: (NSString *)title { return [cell itemWithTitle: title]; } -- (id )lastItem +- (id ) lastItem { return [cell lastItem]; } -- (int)indexOfItem:(id )anObject +- (int) indexOfItem: (id )anObject { return [cell indexOfItem: anObject]; } -- (int)indexOfItemWithTag:(int)tag +- (int) indexOfItemWithTag: (int)tag { return [cell indexOfItemWithTag: tag]; } -- (int)indexOfItemWithTitle:(NSString *)title +- (int) indexOfItemWithTitle: (NSString *)title { return [cell indexOfItemWithTitle: title]; } -- (int)indexOfItemWithRepresentedObject:(id)anObject +- (int) indexOfItemWithRepresentedObject: (id)anObject { return [cell indexOfItemWithRepresentedObject: anObject]; } -- (int)indexOfItemWithTarget:(id)target - andAction:(SEL)actionSelector +- (int) indexOfItemWithTarget: (id)target + andAction: (SEL)actionSelector { return [cell indexOfItemWithTarget: target andAction: actionSelector]; } -- (void)setPreferredEdge:(NSRectEdge)edge +- (void) setPreferredEdge: (NSRectEdge)edge { [cell setPreferredEdge: edge]; } -- (NSRectEdge)preferredEdge +- (NSRectEdge) preferredEdge { return [cell preferredEdge]; } -- (void)setTitle:(NSString *)aString +- (void) setTitle: (NSString *)aString { [cell setTitle: aString]; } -- (void)synchronizeTitleAndSelectedItem +- (void) synchronizeTitleAndSelectedItem { [cell synchronizeTitleAndSelectedItem]; [self sizeToFit]; } -- (void)sizeToFit +- (void) sizeToFit { [[popb_menu menuRepresentation] sizeToFit]; } -- (void)_popup:(NSNotification*)notification +- (void) _popup: (NSNotification*)notification { [cell performClickWithFrame: [[notification object] frame] inView: self]; } -- (void)mouseDown:(NSEvent *)theEvent +- (void) mouseDown: (NSEvent *)theEvent { NSNotificationCenter *nc; diff --git a/Source/NSPopUpButtonCell.m b/Source/NSPopUpButtonCell.m index 21e9af1f6..fea088f3e 100644 --- a/Source/NSPopUpButtonCell.m +++ b/Source/NSPopUpButtonCell.m @@ -36,17 +36,23 @@ #include @interface NSPopUpButtonCell (GNUstepPrivate) -- (void)_popUpItemAction:(id)sender; +- (void) _popUpItemAction: (id)sender; @end @implementation NSPopUpButtonCell -- (id)initTextCell:(NSString *)stringValue +- (void) dealloc +{ + RELEASE(_menu); + [super dealloc]; +} + +- (id) initTextCell: (NSString *)stringValue { return [self initTextCell: stringValue pullsDown: NO]; } -- (id)initTextCell:(NSString *)stringValue - pullsDown:(BOOL)pullDown +- (id) initTextCell: (NSString *)stringValue + pullsDown: (BOOL)pullDown { [super initTextCell: stringValue]; @@ -58,46 +64,46 @@ { id anItem; - [self insertItemWithTitle:stringValue atIndex:0]; + [self insertItemWithTitle: stringValue atIndex: 0]; - anItem = [self itemAtIndex:0]; + anItem = [self itemAtIndex: 0]; [anItem setTarget: self]; [anItem setAction: @selector(_popUpItemAction:)]; } - _menu = [NSMenu initWithTitle:@""]; + _menu = [[NSMenu alloc] initWithTitle: @""]; [_menu _setOwnedByPopUp: YES]; return self; } -- (void)setMenu:(NSMenu *)menu +- (void) setMenu: (NSMenu *)menu { ASSIGN(_menu, menu); } -- (NSMenu *)menu +- (NSMenu *) menu { return _menu; } // Behavior settings -- (void)setPullsDown:(BOOL)flag +- (void) setPullsDown: (BOOL)flag { _pbcFlags.pullsDown = flag; } -- (BOOL)pullsDown +- (BOOL) pullsDown { return _pbcFlags.pullsDown; } -- (void)setAutoenablesItems:(BOOL)flag +- (void) setAutoenablesItems: (BOOL)flag { [_menu setAutoenablesItems: flag]; } -- (BOOL)autoenablesItems +- (BOOL) autoenablesItems { return [_menu autoenablesItems]; } @@ -106,28 +112,28 @@ // severe screen position restrictions. It indicates what edge of the // cell the menu should pop out from. -- (void)setPreferredEdge:(NSRectEdge)edge +- (void) setPreferredEdge: (NSRectEdge)edge { _pbcFlags.preferredEdge = edge; } -- (NSRectEdge)preferredEdge +- (NSRectEdge) preferredEdge { return _pbcFlags.preferredEdge; } // If YES (the default) the popup button will display an item from the // menu. This will be the selected item for a popup or the first item for -// a pull-down. If this is NO, then the menu item set with -setMenuItem: +// a pull-down. If this is NO, then the menu item set with -setMenuItem: // is always displayed. This can be useful for a popup button that is an // icon button that pops up a menu full of textual items, for example. -- (void)setUsesItemFromMenu:(BOOL)flag +- (void) setUsesItemFromMenu: (BOOL)flag { _pbcFlags.usesItemFromMenu = flag; } -- (BOOL)usesItemFromMenu +- (BOOL) usesItemFromMenu { return _pbcFlags.usesItemFromMenu; } @@ -136,295 +142,288 @@ // If YES (the default) then the selected item gets its state set to // NSOnState. If NO the items in the menu are left alone. -- (void)setAltersStateOfSelectedItem:(BOOL)flag +- (void) setAltersStateOfSelectedItem: (BOOL)flag { _pbcFlags.altersStateOfSelectedItem = flag; } -- (BOOL)altersStateOfSelectedItem +- (BOOL) altersStateOfSelectedItem { return _pbcFlags.altersStateOfSelectedItem; } // Adding and removing items -- (void)addItemWithTitle:(NSString *)title +- (void) addItemWithTitle: (NSString *)title { NSMenuItem *anItem = [NSMenuItem new]; - [anItem setTitle:title]; + [anItem setTitle: title]; [anItem setTarget: self]; [anItem setAction: @selector(_popUpItemAction:)]; [_menu insertItem: anItem atIndex: [_menu numberOfItems]]; + RELEASE(anItem); } -- (void)addItemsWithTitles:(NSArray *)itemTitles +- (void) addItemsWithTitles: (NSArray *)itemTitles { - int i; + unsigned c = [itemTitles count]; + unsigned i; - for (i=0; i<[itemTitles count]; i++) + for (i = 0; i < c; i++) { - [self addItemWithTitle: [itemTitles objectAtIndex:i]]; + [self addItemWithTitle: [itemTitles objectAtIndex: i]]; } } -- (void)insertItemWithTitle:(NSString *)title atIndex:(int)index +- (void) insertItemWithTitle: (NSString *)title atIndex: (int)index { NSMenuItem *anItem = [NSMenuItem new]; - [anItem setTitle:title]; + if (index < 0) + index = 0; + if (index > [_menu numberOfItems]) + index = [_menu numberOfitems]; + + [anItem setTitle: title]; [anItem setTarget: self]; [anItem setAction: @selector(_popUpItemAction:)]; - [[_menu itemArray] insertObject: anItem atIndex: index]; + [_menu insertItem: anItem atIndex: index]; + RELEASE(anItem); } -- (void)removeItemWithTitle:(NSString *)title +- (void) removeItemWithTitle: (NSString *)title { - [_menu removeItemWithTitle: title]; + [_menu removeItemAtIndex: [_menu indexOfItemWithTitle: title]]; } -- (void)removeItemAtIndex:(int)index +- (void) removeItemAtIndex: (int)index { - [[_menu itemArray] removeObjectAtIndex: index]; + [_menu removeItemAtIndex: index]; } -- (void)removeAllItems +- (void) removeAllItems { - [_menu removeAllItems]; + while ([_menu numberOfItems] > 0) + { + [_menu removeItemAtIndex: 0]; + } } // Accessing the items -- (NSArray *)itemArray +- (NSArray *) itemArray { return [_menu itemArray]; } -- (int)numberOfItems +- (int) numberOfItems { return [_menu numberOfItems]; } -- (int)indexOfItem:(id )item +- (int) indexOfItem: (id )item { return [_menu indexOfItem: item]; } -- (int)indexOfItemWithTitle:(NSString *)title +- (int) indexOfItemWithTitle: (NSString *)title { return [_menu indexOfItemWithTitle: title]; } -- (int)indexOfItemWithTag:(int)tag +- (int) indexOfItemWithTag: (int)aTag { - return [_menu indexOfItemWithTag: tag]; + return [_menu indexOfItemWithTag: aTag]; } -- (int)indexOfItemWithRepresentedObject:(id)obj +- (int) indexOfItemWithRepresentedObject: (id)obj { return [_menu indexOfItemWithRepresentedObject: obj]; } -- (int)indexOfItemWithTarget:(id)target andAction:(SEL)actionSelector +- (int) indexOfItemWithTarget: (id)aTarget andAction: (SEL)actionSelector { - return [_menu indexOfItemWithTarget: target andAction: actionSelector]; + return [_menu indexOfItemWithTarget: aTarget andAction: actionSelector]; } -- (id )itemAtIndex:(int)index +- (id ) itemAtIndex: (int)index { return [_menu itemAtIndex: index]; } -- (id )itemWithTitle:(NSString *)title +- (id ) itemWithTitle: (NSString *)title { return [_menu itemWithTitle: title]; } -- (id )lastItem +- (id ) lastItem { - return [_menu lastItem]; + int end = [_menu numberOfItems] - 1; + + if (end < 0) + return nil; + return [_menu itemAtIndex: end]; } // Dealing with selection -- (void)selectItem:(id )item +- (void) selectItem: (id )item { if (!item) { if (_pbcFlags.altersStateOfSelectedItem) - [[self itemAtIndex: _selectedIndex] setState: NSOffState]; + [_selectedItem setState: NSOffState]; - _selectedIndex = -1; - } - else - { - int aIndex = [self indexOfItem: item]; - - if (_pbcFlags.altersStateOfSelectedItem) - { - [[self itemAtIndex: _selectedIndex] setState: NSOffState]; - [[self itemAtIndex: aIndex] setState: NSOnState]; - } - - _selectedIndex = aIndex; - } -} - -- (void)selectItemAtIndex:(int)index -{ - if (index == -1) - { - if (_pbcFlags.altersStateOfSelectedItem) - [[self itemAtIndex: _selectedIndex] setState: NSOffState]; - - _selectedIndex = -1; + _selectedItem = nil; } else { if (_pbcFlags.altersStateOfSelectedItem) { - [[self itemAtIndex: _selectedIndex] setState: NSOffState]; - [[self itemAtIndex: index] setState: NSOnState]; - } - - _selectedIndex = index; - } -} - -- (void)selectItemWithTitle:(NSString *)title -{ - - if ([title length] < 1) - { - if (_pbcFlags.altersStateOfSelectedItem) - [[self itemAtIndex: _selectedIndex] setState: NSOffState]; - - _selectedIndex = -1; - } - else - { - int aIndex = [self indexOfItemWithTitle: title]; - + [_selectedItem setState: NSOffState]; + } + _selectedItem = item; if (_pbcFlags.altersStateOfSelectedItem) { - [[self itemAtIndex: _selectedIndex] setState: NSOffState]; - [[self itemAtIndex: aIndex] setState: NSOnState]; + [_selectedItem setState: NSOnState]; } - - _selectedIndex = aIndex; } } -- (void)setTitle:(NSString *)aString +- (void) selectItemAtIndex: (int)index { + NSMenuItem *anItem = (index == -1) ? nil : [self itemAtIndex: index]; + + [self selectItem: anItem]; +} + +- (void) selectItemWithTitle: (NSString *)title +{ + NSMenuItem *anItem = [self itemWithTitle: title]; + + [self selectItem: anItem]; +} + +- (void) setTitle: (NSString *)aString +{ + NSMenuItem *anItem; + if (_pbcFlags.pullsDown) { + if ([_menu numberOfItems] == 0) + { + anItem = nil; + } + else + { + anItem = [_menu itemAtIndex: 0]; + } } else { - int aIndex; - - if (aIndex = [self indexOfItemWithTitle: aString]) - { - [self selectItemAtIndex: aIndex]; - } - else - { + anItem = [_menu itemWithTitle: aString]; + if (anItem == nil) + { [self addItemWithTitle: aString]; - [self selectItemWithTitle: aString]; + anItem = [_menu itemWithTitle: aString]; } } + [self selectItem: anItem]; } -- (id )selectedItem +- (id ) selectedItem { - if (_selectedIndex != -1) - return [self itemAtIndex: _selectedIndex]; - else - return nil; + return _selectedItem; } -- (int)indexOfSelectedItem +- (int) indexOfSelectedItem { - return _selectedIndex; + return [_menu indexOfItem: _selectedItem]; } -- (void)synchronizeTitleAndSelectedItem +- (void) synchronizeTitleAndSelectedItem { if (!_pbcFlags.usesItemFromMenu) return; - // Add test for null menus. - - if (_pbcFlags.pullsDown) + if ([_menu numberOfItems] == 0) + { + _selectedItem = nil; + } + else if (_pbcFlags.pullsDown) { [self selectItem: [self itemAtIndex: 0]]; } else { - if (_selectedIndex >= 0) - [self selectItem: [self itemAtIndex: _selectedIndex]]; - else - [self selectItem: [self itemAtIndex: 0]]; + int index = [[_menu menuRepresentation] highlightedItemIndex]; + + if (index < 0) + index = 0; + [self selectItemAtIndex: index]; } } // Title conveniences -- (NSString *)itemTitleAtIndex:(int)index +- (NSString *) itemTitleAtIndex: (int)index { return [[self itemAtIndex: index] title]; } -- (NSArray *)itemTitles +- (NSArray *) itemTitles { - NSMutableArray *anArray = [NSMutableArray new]; - int i; + unsigned count = [_menu numberOfItems]; + id items[count]; + unsigned i; - for (i=0; i<[[_menu itemArray] count]; i++) + [[_menu itemArray] getObjects: items]; + for (i = 0; i < count; i++) { - [anArray addObject: [[[_menu itemArray] objectAtIndex: i] title]]; + items[i] = [items[i] title]; } - return (NSArray *)anArray; + return [NSArray arrayWithObjects: items count: count]; } -- (NSString *)titleOfSelectedItem +- (NSString *) titleOfSelectedItem { - if (_selectedIndex >= 0) - return [[self itemAtIndex: _selectedIndex] title]; + if (_selectedItem != nil) + return [_selectedItem title]; else return @""; } -- (void)attachPopUpWithFrame:(NSRect)cellFrame - inView:(NSView *)controlView +- (void) attachPopUpWithFrame: (NSRect)cellFrame + inView: (NSView *)controlView { - NSNotificationCenter *_aCenter = [NSNotificationCenter defaultCenter]; - NSNotification *_aNotif; - NSRect scratchRect = cellFrame; - NSRect winf; + NSNotificationCenter *_aCenter = [NSNotificationCenter defaultCenter]; + NSNotification *_aNotif; + NSRect scratchRect = cellFrame; + NSRect winf; _aNotif = [NSNotification - notificationWithName: NSPopUpButtonCellWillPopUpNotification - object: controlView - userInfo: nil]; + notificationWithName: NSPopUpButtonCellWillPopUpNotification + object: controlView + userInfo: nil]; [_aCenter postNotification: _aNotif]; _aNotif = [NSNotification - notificationWithName: NSPopUpButtonCellWillPopUpNotification - object: self - userInfo: nil]; + notificationWithName: NSPopUpButtonCellWillPopUpNotification + object: self + userInfo: nil]; [_aCenter postNotification: _aNotif]; - scratchRect.origin = [[controlView window] convertBaseToScreen: cellFrame.origin]; + scratchRect.origin + = [[controlView window] convertBaseToScreen: cellFrame.origin]; [[_menu menuRepresentation] _setCellSize: cellFrame.size]; [_menu sizeToFit]; winf = [NSMenuWindow - frameRectForContentRect: [[_menu menuRepresentation] frame] - styleMask: [[_menu window] styleMask]]; + frameRectForContentRect: [[_menu menuRepresentation] frame] + styleMask: [[_menu window] styleMask]]; /* * Set popup window frame origin so that the top-left corner of the * window lines up with the top-left corner of this button. @@ -443,7 +442,7 @@ if (!_pbcFlags.pullsDown) { - winf.origin.y += (_selectedIndex * scratchRect.size.height); + winf.origin.y += ([self indexOfSelectedItem] * scratchRect.size.height); } NSLog(@"winf %@", NSStringFromRect(winf)); @@ -454,20 +453,20 @@ NSLog(@"winf %@", NSStringFromRect(winf)); [[_menu window] orderFrontRegardless]; } -- (void)dismissPopUp +- (void) dismissPopUp { [[_menu window] orderOut: nil]; } -- (BOOL)trackMouse:(NSEvent *)theEvent - inRect:(NSRect)cellFrame - ofView:(NSView *)controlView - untilMouseUp:(BOOL)untilMouseUp +- (BOOL) trackMouse: (NSEvent *)theEvent + inRect: (NSRect)cellFrame + ofView: (NSView *)controlView + untilMouseUp: (BOOL)untilMouseUp { } -- (void)performClickWithFrame:(NSRect)frame - inView:(NSView *)controlView +- (void) performClickWithFrame: (NSRect)frame + inView: (NSView *)controlView { int indexToClick; @@ -482,12 +481,12 @@ NSLog(@"winf %@", NSStringFromRect(winf)); } // Arrow position for bezel style and borderless popups. -- (NSPopUpArrowPosition)arrowPosition +- (NSPopUpArrowPosition) arrowPosition { return _pbcFlags.arrowPosition; } -- (void)setArrowPosition:(NSPopUpArrowPosition)position +- (void) setArrowPosition: (NSPopUpArrowPosition)position { _pbcFlags.arrowPosition = position; } @@ -508,11 +507,11 @@ NSLog(@"winf %@", NSStringFromRect(winf)); if (_pbcFlags.pullsDown) { - aImage = [NSImage imageNamed:@"common_3DArrowDown"]; + aImage = [NSImage imageNamed: @"common_3DArrowDown"]; } else { - aImage = [NSImage imageNamed:@"common_Nibble"]; + aImage = [NSImage imageNamed: @"common_Nibble"]; } size = [aImage size]; @@ -529,7 +528,7 @@ NSLog(@"winf %@", NSStringFromRect(winf)); [view unlockFocus]; } -- (void)_popUpItemAction:(id)sender +- (void) _popUpItemAction: (id)sender { [self selectItemWithTitle: [sender title]]; NSLog(@"%@", [sender title]);