Fixes to avoid crashes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5275 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-11-25 11:54:08 +00:00
parent d2759567a5
commit 1708e6cfe8
5 changed files with 291 additions and 267 deletions

View file

@ -1,3 +1,13 @@
Thu Nov 25 11:44:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* 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 <n.pero@mi.flashnet.it> Thu Nov 25 1999 Nicola Pero <n.pero@mi.flashnet.it>
* Source/NSMenuView.m ([-trackWithEvent:]): Fixed bug which was * Source/NSMenuView.m ([-trackWithEvent:]): Fixed bug which was

View file

@ -11,17 +11,17 @@ typedef enum {
@interface NSPopUpButtonCell : NSMenuItemCell @interface NSPopUpButtonCell : NSMenuItemCell
{ {
NSMenu *_menu; NSMenu *_menu;
int _selectedIndex; NSMenuItem *_selectedItem;
struct __pbcFlags { struct __pbcFlags {
unsigned int pullsDown:1; unsigned int pullsDown:1;
unsigned int preferredEdge:3; unsigned int preferredEdge:3;
unsigned int menuIsAttached:1; unsigned int menuIsAttached:1;
unsigned int usesItemFromMenu:1; unsigned int usesItemFromMenu:1;
unsigned int altersStateOfSelectedItem:1; unsigned int altersStateOfSelectedItem:1;
unsigned int decoding:1; unsigned int decoding:1;
unsigned int arrowPosition:2; unsigned int arrowPosition:2;
} _pbcFlags; } _pbcFlags;
} }
- (id)initTextCell:(NSString *)stringValue pullsDown:(BOOL)pullDown; - (id)initTextCell:(NSString *)stringValue pullsDown:(BOOL)pullDown;

View file

@ -91,11 +91,26 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
return menu_is_beholdenToPopUpButton; return menu_is_beholdenToPopUpButton;
} }
- (void)_setOwnedByPopUp:(BOOL)flag - (void)_setOwnedByPopUp: (BOOL)flag
{ {
menu_is_beholdenToPopUpButton = 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 - (id) initWithTitle: (NSString *)aTitle
{ {
NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter]; NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter];
@ -171,7 +186,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
return; return;
d = [NSDictionary d = [NSDictionary
dictionaryWithObject: [NSNumber numberWithInt:index] dictionaryWithObject: [NSNumber numberWithInt: index]
forKey: @"NSMenuItemIndex"]; forKey: @"NSMenuItemIndex"];
[menu_items insertObject: newItem atIndex: index]; [menu_items insertObject: newItem atIndex: index];
@ -224,7 +239,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
// Insert the new item into the stream. // Insert the new item into the stream.
[self insertItem:anItem atIndex:index]; [self insertItem: anItem atIndex: index];
// For returns sake. // For returns sake.
@ -248,21 +263,21 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
- (void) removeItem: (id <NSMenuItem>)anItem - (void) removeItem: (id <NSMenuItem>)anItem
{ {
[self removeItemAtIndex:[menu_items indexOfObject: anItem]]; [self removeItemAtIndex: [menu_items indexOfObjectIdenticalTo: anItem]];
} }
- (void) removeItemAtIndex: (int)index - (void) removeItemAtIndex: (int)index
{ {
NSNotification *removed; NSNotification *removed;
NSDictionary *d; NSDictionary *d;
id anItem = [menu_items objectAtIndex:index]; id anItem = [menu_items objectAtIndex: index];
if (!anItem) if (!anItem)
return; return;
if ([anItem isKindOfClass: [NSMenuItem class]]) if ([anItem isKindOfClass: [NSMenuItem class]])
{ {
d = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt:index] d = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: index]
forKey: @"NSMenuItemIndex"]; forKey: @"NSMenuItemIndex"];
[anItem setMenu: nil]; [anItem setMenu: nil];
@ -293,7 +308,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
d = [NSDictionary d = [NSDictionary
dictionaryWithObject: [NSNumber dictionaryWithObject: [NSNumber
numberWithInt:[self indexOfItem: anObject]] numberWithInt: [self indexOfItem: anObject]]
forKey: @"NSMenuItemIndex"]; forKey: @"NSMenuItemIndex"];
changed = [NSNotification changed = [NSNotification
@ -347,7 +362,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
{ {
if (index >= [menu_items count] || index < 0) if (index >= [menu_items count] || index < 0)
[NSException raise: NSRangeException [NSException raise: NSRangeException
format: @"Range error in method -itemAtIndex:"]; format: @"Range error in method -itemAtIndex: "];
return [menu_items objectAtIndex: index]; return [menu_items objectAtIndex: index];
} }
@ -367,7 +382,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
// //
- (int) indexOfItem: (id <NSMenuItem>)anObject - (int) indexOfItem: (id <NSMenuItem>)anObject
{ {
return [menu_items indexOfObject: anObject]; return [menu_items indexOfObjectIdenticalTo: anObject];
} }
- (int) indexOfItemWithTitle: (NSString *)aTitle - (int) indexOfItemWithTitle: (NSString *)aTitle
@ -375,7 +390,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
id anItem; id anItem;
if ((anItem = [self itemWithTitle: aTitle])) if ((anItem = [self itemWithTitle: aTitle]))
return [menu_items indexOfObject: anItem]; return [menu_items indexOfObjectIdenticalTo: anItem];
else else
return -1; return -1;
} }
@ -385,13 +400,13 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
id anItem; id anItem;
if ((anItem = [self itemWithTag: aTag])) if ((anItem = [self itemWithTag: aTag]))
return [menu_items indexOfObject: anItem]; return [menu_items indexOfObjectIdenticalTo: anItem];
else else
return -1; return -1;
} }
- (int) indexOfItemWithTarget: (id)anObject - (int) indexOfItemWithTarget: (id)anObject
andAction: (SEL)actionSelector andAction: (SEL)actionSelector
{ {
unsigned i; unsigned i;
unsigned count = [menu_items count]; unsigned count = [menu_items count];
@ -414,8 +429,8 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
if ([[[menu_items objectAtIndex:i] representedObject] if ([[[menu_items objectAtIndex: i] representedObject]
isEqual:anObject]) isEqual: anObject])
{ {
return i; return i;
} }
@ -430,8 +445,8 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
if ([[[menu_items objectAtIndex:i] title] if ([[[menu_items objectAtIndex: i] title]
isEqual:[anObject title]]) isEqual: [anObject title]])
{ {
return i; return i;
} }
@ -509,9 +524,9 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
if (NSInterfaceStyleForKey(@"NSMenuInterfaceStyle", nil) if (NSInterfaceStyleForKey(@"NSMenuInterfaceStyle", nil)
== GSWindowMakerInterfaceStyle) == GSWindowMakerInterfaceStyle)
{ {
NSRect aRect = [menu_view rectOfItemAtIndex: NSRect aRect = [menu_view rectOfItemAtIndex:
[self indexOfItemWithTitle:[aSubmenu title]]]; [self indexOfItemWithTitle: [aSubmenu title]]];
NSPoint subOrigin = [win_link convertBaseToScreen: NSPoint subOrigin = [win_link convertBaseToScreen:
NSMakePoint(aRect.origin.x, NSMakePoint(aRect.origin.x,
aRect.origin.y)]; aRect.origin.y)];
@ -871,13 +886,13 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
#define IS_OFFSCREEN(WINDOW) \ #define IS_OFFSCREEN(WINDOW) \
!(NSContainsRect([[NSScreen mainScreen] frame], [WINDOW frame])) !(NSContainsRect([[NSScreen mainScreen] frame], [WINDOW frame]))
- (void) _setTornOff:(BOOL)flag - (void) _setTornOff: (BOOL)flag
{ {
NSMenu *supermenu = [self supermenu]; NSMenu *supermenu = [self supermenu];
menu_is_tornoff = flag; menu_is_tornoff = flag;
[[supermenu menuRepresentation] setHighlightedItemIndex:-1]; [[supermenu menuRepresentation] setHighlightedItemIndex: -1];
supermenu->menu_attachedMenu = nil; supermenu->menu_attachedMenu = nil;
} }
@ -899,7 +914,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
if (array && [array isKindOfClass: [NSArray class]]) if (array && [array isKindOfClass: [NSArray class]])
{ {
[titleView windowBecomeTornOff]; [titleView windowBecomeTornOff];
[self _setTornOff:YES]; [self _setTornOff: YES];
[self display]; [self display];
} }
} }
@ -924,7 +939,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow); menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow);
} }
- (void) _performMenuClose:(id)sender - (void) _performMenuClose: (id)sender
{ {
NSUserDefaults* defaults; NSUserDefaults* defaults;
NSMutableDictionary* menuLocations; NSMutableDictionary* menuLocations;
@ -935,7 +950,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
[menu_view setHighlightedItemIndex: -1]; [menu_view setHighlightedItemIndex: -1];
[self _setTornOff:NO]; [self _setTornOff: NO];
[self close]; [self close];
[titleView _releaseCloseButton]; [titleView _releaseCloseButton];
@ -964,7 +979,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
if (menu_supermenu && ![self isTornOff]) // query super menu for if (menu_supermenu && ![self isTornOff]) // query super menu for
{ // position { // position
[aWindow setFrameOrigin:[menu_supermenu locationForSubmenu: self]]; [aWindow setFrameOrigin: [menu_supermenu locationForSubmenu: self]];
menu_supermenu->menu_attachedMenu = self; menu_supermenu->menu_attachedMenu = self;
} }
else else
@ -995,15 +1010,15 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
float aPoint = [[NSScreen mainScreen] frame].size.height float aPoint = [[NSScreen mainScreen] frame].size.height
- [aWindow frame].size.height; - [aWindow frame].size.height;
[aWindow setFrameOrigin:NSMakePoint(0,aPoint)]; [aWindow setFrameOrigin: NSMakePoint(0,aPoint)];
[bWindow setFrameOrigin:NSMakePoint(0,aPoint)]; [bWindow setFrameOrigin: NSMakePoint(0,aPoint)];
} }
} }
} }
menu_is_visible = YES; menu_is_visible = YES;
[aWindow orderFront:nil]; [aWindow orderFront: nil];
menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow); menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow);
} }
@ -1029,17 +1044,17 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
if (menu_is_tornoff) if (menu_is_tornoff)
[titleView _releaseCloseButton]; [titleView _releaseCloseButton];
[[bWindow contentView] addSubview:menu_view]; [[bWindow contentView] addSubview: menu_view];
[[bWindow contentView] addSubview:titleView]; [[bWindow contentView] addSubview: titleView];
[bWindow orderFront:self]; [bWindow orderFront: self];
menu_isPartlyOffScreen = IS_OFFSCREEN(bWindow); menu_isPartlyOffScreen = IS_OFFSCREEN(bWindow);
} }
- (void) close - (void) close
{ {
[aWindow orderOut:self]; [aWindow orderOut: self];
menu_is_visible = NO; menu_is_visible = NO;
if (menu_supermenu) if (menu_supermenu)
@ -1048,18 +1063,18 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
- (void) closeTransient - (void) closeTransient
{ {
[bWindow orderOut:self]; [bWindow orderOut: self];
[menu_view removeFromSuperviewWithoutNeedingDisplay]; [menu_view removeFromSuperviewWithoutNeedingDisplay];
[titleView removeFromSuperviewWithoutNeedingDisplay]; [titleView removeFromSuperviewWithoutNeedingDisplay];
[[aWindow contentView] addSubview:menu_view]; [[aWindow contentView] addSubview: menu_view];
if (menu_is_tornoff) if (menu_is_tornoff)
[titleView _addCloseButton]; [titleView _addCloseButton];
[[aWindow contentView] addSubview:titleView]; [[aWindow contentView] addSubview: titleView];
[[aWindow contentView] setNeedsDisplay:YES]; [[aWindow contentView] setNeedsDisplay: YES];
// Restore the old submenu. // Restore the old submenu.
menu_supermenu->menu_attachedMenu = _oldAttachedMenu; menu_supermenu->menu_attachedMenu = _oldAttachedMenu;
@ -1297,7 +1312,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
if (![menu isTornOff] && [menu supermenu]) if (![menu isTornOff] && [menu supermenu])
{ {
[self windowBecomeTornOff]; [self windowBecomeTornOff];
[menu _setTornOff:YES]; [menu _setTornOff: YES];
} }
while (!done) while (!done)
@ -1309,7 +1324,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
switch ([theEvent type]) switch ([theEvent type])
{ {
case NSLeftMouseUp: case NSLeftMouseUp:
done = YES; done = YES;
break; break;
case NSLeftMouseDragged: case NSLeftMouseDragged:
@ -1325,7 +1340,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
} }
break; break;
default: default:
break; break;
} }
} }
@ -1338,7 +1353,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
if (!menuLocations) if (!menuLocations)
menuLocations = [NSMutableDictionary dictionaryWithCapacity: 2]; menuLocations = [NSMutableDictionary dictionaryWithCapacity: 2];
origin = [[menu window] frame].origin; origin = [[menu window] frame].origin;
array = [NSArray arrayWithObjects: array = [NSArray arrayWithObjects:
[[NSNumber numberWithInt: origin.x] stringValue], [[NSNumber numberWithInt: origin.x] stringValue],
[[NSNumber numberWithInt: origin.y] stringValue], nil]; [[NSNumber numberWithInt: origin.y] stringValue], nil];
@ -1380,7 +1395,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
[self setAutoresizingMask: mask]; [self setAutoresizingMask: mask];
[button display]; [button display];
[self setNeedsDisplay:YES]; [self setNeedsDisplay: YES];
} }
} }
@ -1392,6 +1407,6 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
- (void) _addCloseButton - (void) _addCloseButton
{ {
[self addSubview:button]; [self addSubview: button];
} }
@end /* NSMenuWindowTitleView */ @end /* NSMenuWindowTitleView */

View file

@ -52,12 +52,12 @@ id _nspopupbuttonCellClass = nil;
// //
// Class methods // Class methods
// //
+ (void)initialize + (void) initialize
{ {
if (self == [NSPopUpButton class]) if (self == [NSPopUpButton class])
{ {
// Initial version // Initial version
[self setVersion:1]; [self setVersion: 1];
[self setCellClass: [NSPopUpButtonCell class]]; [self setCellClass: [NSPopUpButtonCell class]];
} }
} }
@ -65,22 +65,22 @@ id _nspopupbuttonCellClass = nil;
// //
// Initializing an NSPopUpButton // 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 - (id) initWithFrame: (NSRect)frameRect
pullsDown:(BOOL)flag pullsDown: (BOOL)flag
{ {
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
[super initWithFrame:frameRect]; [super initWithFrame: frameRect];
[defaultCenter addObserver: self [defaultCenter addObserver: self
selector: @selector(_popup:) selector: @selector(_popup:)
@ -90,208 +90,208 @@ id _nspopupbuttonCellClass = nil;
return self; return self;
} }
- (void)setMenu:(NSMenu *)menu - (void) setMenu: (NSMenu *)menu
{ {
[cell setMenu: menu]; [cell setMenu: menu];
} }
- (NSMenu *)menu - (NSMenu *) menu
{ {
return [cell menu]; return [cell menu];
} }
- (void)setPullsDown:(BOOL)flag - (void) setPullsDown: (BOOL)flag
{ {
[cell setPullsDown: flag]; [cell setPullsDown: flag];
} }
- (BOOL)pullsDown - (BOOL) pullsDown
{ {
return [cell pullsDown]; return [cell pullsDown];
} }
- (void)setAutoenablesItems:(BOOL)flag - (void) setAutoenablesItems: (BOOL)flag
{ {
[cell setAutoenablesItems: flag]; [cell setAutoenablesItems: flag];
} }
- (BOOL)autoenablesItems - (BOOL) autoenablesItems
{ {
return [cell autoenablesItems]; return [cell autoenablesItems];
} }
- (void)addItemWithTitle:(NSString *)title - (void) addItemWithTitle: (NSString *)title
{ {
[cell addItemWithTitle: title]; [cell addItemWithTitle: title];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (void)addItemsWithTitles:(NSArray *)itemTitles - (void) addItemsWithTitles: (NSArray *)itemTitles
{ {
[cell addItemWithTitles: itemTitles]; [cell addItemWithTitles: itemTitles];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (void)insertItemWithTitle:(NSString *)title - (void) insertItemWithTitle: (NSString *)title
atIndex:(int)index atIndex: (int)index
{ {
[cell insertItemWithTitle: title adIndex: index]; [cell insertItemWithTitle: title adIndex: index];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (void)removeAllItems - (void) removeAllItems
{ {
[cell removeAllItems]; [cell removeAllItems];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (void)removeItemWithTitle:(NSString *)title - (void) removeItemWithTitle: (NSString *)title
{ {
[cell removeItemWithTitle]; [cell removeItemWithTitle];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (void)removeItemAtIndex:(int)index - (void) removeItemAtIndex: (int)index
{ {
[cell removeItemAtIndex: index]; [cell removeItemAtIndex: index];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (id <NSMenuItem>)selectedItem - (id <NSMenuItem>) selectedItem
{ {
return [cell selectedItem]; return [cell selectedItem];
} }
- (NSString *)titleOfSelectedItem - (NSString *) titleOfSelectedItem
{ {
return [cell titleOfSelectedItem]; return [cell titleOfSelectedItem];
} }
- (int)indexOfSelectedItem - (int) indexOfSelectedItem
{ {
return [cell indexOfSelectedItem]; return [cell indexOfSelectedItem];
} }
- (void)selectItem:(id <NSMenuItem>)anObject - (void) selectItem: (id <NSMenuItem>)anObject
{ {
[cell selectedItem: anObject]; [cell selectedItem: anObject];
} }
- (void)selectItemAtIndex:(int)index - (void) selectItemAtIndex: (int)index
{ {
[cell selectItemAtIndex: index]; [cell selectItemAtIndex: index];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (void)selectItemWithTitle:(NSString *)title - (void) selectItemWithTitle: (NSString *)title
{ {
[cell selectItemWithTitle: title]; [cell selectItemWithTitle: title];
[self synchronizeTitleAndSelectedItem]; [self synchronizeTitleAndSelectedItem];
} }
- (int)numberOfItems - (int) numberOfItems
{ {
return [cell numberOfItems]; return [cell numberOfItems];
} }
- (NSArray *)itemArray - (NSArray *) itemArray
{ {
return [cell itemArray]; return [cell itemArray];
} }
- (id <NSMenuItem>)itemAtIndex:(int)index - (id <NSMenuItem>) itemAtIndex: (int)index
{ {
return [cell itemAtIndex: index]; return [cell itemAtIndex: index];
} }
- (NSString *)itemTitleAtIndex:(int)index - (NSString *) itemTitleAtIndex: (int)index
{ {
return [cell itemTitleAtIndex: index]; return [cell itemTitleAtIndex: index];
} }
- (NSArray *)itemTitles - (NSArray *) itemTitles
{ {
return [cell itemTitles]; return [cell itemTitles];
} }
- (id <NSMenuItem>)itemWithTitle:(NSString *)title - (id <NSMenuItem>) itemWithTitle: (NSString *)title
{ {
return [cell itemWithTitle: title]; return [cell itemWithTitle: title];
} }
- (id <NSMenuItem>)lastItem - (id <NSMenuItem>) lastItem
{ {
return [cell lastItem]; return [cell lastItem];
} }
- (int)indexOfItem:(id <NSMenuItem>)anObject - (int) indexOfItem: (id <NSMenuItem>)anObject
{ {
return [cell indexOfItem: anObject]; return [cell indexOfItem: anObject];
} }
- (int)indexOfItemWithTag:(int)tag - (int) indexOfItemWithTag: (int)tag
{ {
return [cell indexOfItemWithTag: tag]; return [cell indexOfItemWithTag: tag];
} }
- (int)indexOfItemWithTitle:(NSString *)title - (int) indexOfItemWithTitle: (NSString *)title
{ {
return [cell indexOfItemWithTitle: title]; return [cell indexOfItemWithTitle: title];
} }
- (int)indexOfItemWithRepresentedObject:(id)anObject - (int) indexOfItemWithRepresentedObject: (id)anObject
{ {
return [cell indexOfItemWithRepresentedObject: anObject]; return [cell indexOfItemWithRepresentedObject: anObject];
} }
- (int)indexOfItemWithTarget:(id)target - (int) indexOfItemWithTarget: (id)target
andAction:(SEL)actionSelector andAction: (SEL)actionSelector
{ {
return [cell indexOfItemWithTarget: target andAction: actionSelector]; return [cell indexOfItemWithTarget: target andAction: actionSelector];
} }
- (void)setPreferredEdge:(NSRectEdge)edge - (void) setPreferredEdge: (NSRectEdge)edge
{ {
[cell setPreferredEdge: edge]; [cell setPreferredEdge: edge];
} }
- (NSRectEdge)preferredEdge - (NSRectEdge) preferredEdge
{ {
return [cell preferredEdge]; return [cell preferredEdge];
} }
- (void)setTitle:(NSString *)aString - (void) setTitle: (NSString *)aString
{ {
[cell setTitle: aString]; [cell setTitle: aString];
} }
- (void)synchronizeTitleAndSelectedItem - (void) synchronizeTitleAndSelectedItem
{ {
[cell synchronizeTitleAndSelectedItem]; [cell synchronizeTitleAndSelectedItem];
[self sizeToFit]; [self sizeToFit];
} }
- (void)sizeToFit - (void) sizeToFit
{ {
[[popb_menu menuRepresentation] sizeToFit]; [[popb_menu menuRepresentation] sizeToFit];
} }
- (void)_popup:(NSNotification*)notification - (void) _popup: (NSNotification*)notification
{ {
[cell performClickWithFrame: [[notification object] frame] [cell performClickWithFrame: [[notification object] frame]
inView: self]; inView: self];
} }
- (void)mouseDown:(NSEvent *)theEvent - (void) mouseDown: (NSEvent *)theEvent
{ {
NSNotificationCenter *nc; NSNotificationCenter *nc;

View file

@ -36,17 +36,23 @@
#include <AppKit/PSOperators.h> #include <AppKit/PSOperators.h>
@interface NSPopUpButtonCell (GNUstepPrivate) @interface NSPopUpButtonCell (GNUstepPrivate)
- (void)_popUpItemAction:(id)sender; - (void) _popUpItemAction: (id)sender;
@end @end
@implementation NSPopUpButtonCell @implementation NSPopUpButtonCell
- (id)initTextCell:(NSString *)stringValue - (void) dealloc
{
RELEASE(_menu);
[super dealloc];
}
- (id) initTextCell: (NSString *)stringValue
{ {
return [self initTextCell: stringValue pullsDown: NO]; return [self initTextCell: stringValue pullsDown: NO];
} }
- (id)initTextCell:(NSString *)stringValue - (id) initTextCell: (NSString *)stringValue
pullsDown:(BOOL)pullDown pullsDown: (BOOL)pullDown
{ {
[super initTextCell: stringValue]; [super initTextCell: stringValue];
@ -58,46 +64,46 @@
{ {
id anItem; id anItem;
[self insertItemWithTitle:stringValue atIndex:0]; [self insertItemWithTitle: stringValue atIndex: 0];
anItem = [self itemAtIndex:0]; anItem = [self itemAtIndex: 0];
[anItem setTarget: self]; [anItem setTarget: self];
[anItem setAction: @selector(_popUpItemAction:)]; [anItem setAction: @selector(_popUpItemAction:)];
} }
_menu = [NSMenu initWithTitle:@""]; _menu = [[NSMenu alloc] initWithTitle: @""];
[_menu _setOwnedByPopUp: YES]; [_menu _setOwnedByPopUp: YES];
return self; return self;
} }
- (void)setMenu:(NSMenu *)menu - (void) setMenu: (NSMenu *)menu
{ {
ASSIGN(_menu, menu); ASSIGN(_menu, menu);
} }
- (NSMenu *)menu - (NSMenu *) menu
{ {
return _menu; return _menu;
} }
// Behavior settings // Behavior settings
- (void)setPullsDown:(BOOL)flag - (void) setPullsDown: (BOOL)flag
{ {
_pbcFlags.pullsDown = flag; _pbcFlags.pullsDown = flag;
} }
- (BOOL)pullsDown - (BOOL) pullsDown
{ {
return _pbcFlags.pullsDown; return _pbcFlags.pullsDown;
} }
- (void)setAutoenablesItems:(BOOL)flag - (void) setAutoenablesItems: (BOOL)flag
{ {
[_menu setAutoenablesItems: flag]; [_menu setAutoenablesItems: flag];
} }
- (BOOL)autoenablesItems - (BOOL) autoenablesItems
{ {
return [_menu autoenablesItems]; return [_menu autoenablesItems];
} }
@ -106,28 +112,28 @@
// severe screen position restrictions. It indicates what edge of the // severe screen position restrictions. It indicates what edge of the
// cell the menu should pop out from. // cell the menu should pop out from.
- (void)setPreferredEdge:(NSRectEdge)edge - (void) setPreferredEdge: (NSRectEdge)edge
{ {
_pbcFlags.preferredEdge = edge; _pbcFlags.preferredEdge = edge;
} }
- (NSRectEdge)preferredEdge - (NSRectEdge) preferredEdge
{ {
return _pbcFlags.preferredEdge; return _pbcFlags.preferredEdge;
} }
// If YES (the default) the popup button will display an item from the // 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 // 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 // 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. // icon button that pops up a menu full of textual items, for example.
- (void)setUsesItemFromMenu:(BOOL)flag - (void) setUsesItemFromMenu: (BOOL)flag
{ {
_pbcFlags.usesItemFromMenu = flag; _pbcFlags.usesItemFromMenu = flag;
} }
- (BOOL)usesItemFromMenu - (BOOL) usesItemFromMenu
{ {
return _pbcFlags.usesItemFromMenu; return _pbcFlags.usesItemFromMenu;
} }
@ -136,295 +142,288 @@
// If YES (the default) then the selected item gets its state set to // If YES (the default) then the selected item gets its state set to
// NSOnState. If NO the items in the menu are left alone. // NSOnState. If NO the items in the menu are left alone.
- (void)setAltersStateOfSelectedItem:(BOOL)flag - (void) setAltersStateOfSelectedItem: (BOOL)flag
{ {
_pbcFlags.altersStateOfSelectedItem = flag; _pbcFlags.altersStateOfSelectedItem = flag;
} }
- (BOOL)altersStateOfSelectedItem - (BOOL) altersStateOfSelectedItem
{ {
return _pbcFlags.altersStateOfSelectedItem; return _pbcFlags.altersStateOfSelectedItem;
} }
// Adding and removing items // Adding and removing items
- (void)addItemWithTitle:(NSString *)title - (void) addItemWithTitle: (NSString *)title
{ {
NSMenuItem *anItem = [NSMenuItem new]; NSMenuItem *anItem = [NSMenuItem new];
[anItem setTitle:title]; [anItem setTitle: title];
[anItem setTarget: self]; [anItem setTarget: self];
[anItem setAction: @selector(_popUpItemAction:)]; [anItem setAction: @selector(_popUpItemAction:)];
[_menu insertItem: anItem atIndex: [_menu numberOfItems]]; [_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]; 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 setTarget: self];
[anItem setAction: @selector(_popUpItemAction:)]; [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 // Accessing the items
- (NSArray *)itemArray - (NSArray *) itemArray
{ {
return [_menu itemArray]; return [_menu itemArray];
} }
- (int)numberOfItems - (int) numberOfItems
{ {
return [_menu numberOfItems]; return [_menu numberOfItems];
} }
- (int)indexOfItem:(id <NSMenuItem>)item - (int) indexOfItem: (id <NSMenuItem>)item
{ {
return [_menu indexOfItem: item]; return [_menu indexOfItem: item];
} }
- (int)indexOfItemWithTitle:(NSString *)title - (int) indexOfItemWithTitle: (NSString *)title
{ {
return [_menu indexOfItemWithTitle: 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]; 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 <NSMenuItem>)itemAtIndex:(int)index - (id <NSMenuItem>) itemAtIndex: (int)index
{ {
return [_menu itemAtIndex: index]; return [_menu itemAtIndex: index];
} }
- (id <NSMenuItem>)itemWithTitle:(NSString *)title - (id <NSMenuItem>) itemWithTitle: (NSString *)title
{ {
return [_menu itemWithTitle: title]; return [_menu itemWithTitle: title];
} }
- (id <NSMenuItem>)lastItem - (id <NSMenuItem>) lastItem
{ {
return [_menu lastItem]; int end = [_menu numberOfItems] - 1;
if (end < 0)
return nil;
return [_menu itemAtIndex: end];
} }
// Dealing with selection // Dealing with selection
- (void)selectItem:(id <NSMenuItem>)item - (void) selectItem: (id <NSMenuItem>)item
{ {
if (!item) if (!item)
{ {
if (_pbcFlags.altersStateOfSelectedItem) if (_pbcFlags.altersStateOfSelectedItem)
[[self itemAtIndex: _selectedIndex] setState: NSOffState]; [_selectedItem setState: NSOffState];
_selectedIndex = -1; _selectedItem = nil;
}
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;
} }
else else
{ {
if (_pbcFlags.altersStateOfSelectedItem) if (_pbcFlags.altersStateOfSelectedItem)
{ {
[[self itemAtIndex: _selectedIndex] setState: NSOffState]; [_selectedItem setState: NSOffState];
[[self itemAtIndex: index] setState: NSOnState]; }
} _selectedItem = item;
_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];
if (_pbcFlags.altersStateOfSelectedItem) if (_pbcFlags.altersStateOfSelectedItem)
{ {
[[self itemAtIndex: _selectedIndex] setState: NSOffState]; [_selectedItem setState: NSOnState];
[[self itemAtIndex: aIndex] 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 (_pbcFlags.pullsDown)
{ {
if ([_menu numberOfItems] == 0)
{
anItem = nil;
}
else
{
anItem = [_menu itemAtIndex: 0];
}
} }
else else
{ {
int aIndex; anItem = [_menu itemWithTitle: aString];
if (anItem == nil)
if (aIndex = [self indexOfItemWithTitle: aString]) {
{
[self selectItemAtIndex: aIndex];
}
else
{
[self addItemWithTitle: aString]; [self addItemWithTitle: aString];
[self selectItemWithTitle: aString]; anItem = [_menu itemWithTitle: aString];
} }
} }
[self selectItem: anItem];
} }
- (id <NSMenuItem>)selectedItem - (id <NSMenuItem>) selectedItem
{ {
if (_selectedIndex != -1) return _selectedItem;
return [self itemAtIndex: _selectedIndex];
else
return nil;
} }
- (int)indexOfSelectedItem - (int) indexOfSelectedItem
{ {
return _selectedIndex; return [_menu indexOfItem: _selectedItem];
} }
- (void)synchronizeTitleAndSelectedItem - (void) synchronizeTitleAndSelectedItem
{ {
if (!_pbcFlags.usesItemFromMenu) if (!_pbcFlags.usesItemFromMenu)
return; return;
// Add test for null menus. if ([_menu numberOfItems] == 0)
{
if (_pbcFlags.pullsDown) _selectedItem = nil;
}
else if (_pbcFlags.pullsDown)
{ {
[self selectItem: [self itemAtIndex: 0]]; [self selectItem: [self itemAtIndex: 0]];
} }
else else
{ {
if (_selectedIndex >= 0) int index = [[_menu menuRepresentation] highlightedItemIndex];
[self selectItem: [self itemAtIndex: _selectedIndex]];
else if (index < 0)
[self selectItem: [self itemAtIndex: 0]]; index = 0;
[self selectItemAtIndex: index];
} }
} }
// Title conveniences // Title conveniences
- (NSString *)itemTitleAtIndex:(int)index - (NSString *) itemTitleAtIndex: (int)index
{ {
return [[self itemAtIndex: index] title]; return [[self itemAtIndex: index] title];
} }
- (NSArray *)itemTitles - (NSArray *) itemTitles
{ {
NSMutableArray *anArray = [NSMutableArray new]; unsigned count = [_menu numberOfItems];
int i; 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) if (_selectedItem != nil)
return [[self itemAtIndex: _selectedIndex] title]; return [_selectedItem title];
else else
return @""; return @"";
} }
- (void)attachPopUpWithFrame:(NSRect)cellFrame - (void) attachPopUpWithFrame: (NSRect)cellFrame
inView:(NSView *)controlView inView: (NSView *)controlView
{ {
NSNotificationCenter *_aCenter = [NSNotificationCenter defaultCenter]; NSNotificationCenter *_aCenter = [NSNotificationCenter defaultCenter];
NSNotification *_aNotif; NSNotification *_aNotif;
NSRect scratchRect = cellFrame; NSRect scratchRect = cellFrame;
NSRect winf; NSRect winf;
_aNotif = [NSNotification _aNotif = [NSNotification
notificationWithName: NSPopUpButtonCellWillPopUpNotification notificationWithName: NSPopUpButtonCellWillPopUpNotification
object: controlView object: controlView
userInfo: nil]; userInfo: nil];
[_aCenter postNotification: _aNotif]; [_aCenter postNotification: _aNotif];
_aNotif = [NSNotification _aNotif = [NSNotification
notificationWithName: NSPopUpButtonCellWillPopUpNotification notificationWithName: NSPopUpButtonCellWillPopUpNotification
object: self object: self
userInfo: nil]; userInfo: nil];
[_aCenter postNotification: _aNotif]; [_aCenter postNotification: _aNotif];
scratchRect.origin = [[controlView window] convertBaseToScreen: cellFrame.origin]; scratchRect.origin
= [[controlView window] convertBaseToScreen: cellFrame.origin];
[[_menu menuRepresentation] _setCellSize: cellFrame.size]; [[_menu menuRepresentation] _setCellSize: cellFrame.size];
[_menu sizeToFit]; [_menu sizeToFit];
winf = [NSMenuWindow winf = [NSMenuWindow
frameRectForContentRect: [[_menu menuRepresentation] frame] frameRectForContentRect: [[_menu menuRepresentation] frame]
styleMask: [[_menu window] styleMask]]; styleMask: [[_menu window] styleMask]];
/* /*
* Set popup window frame origin so that the top-left corner of the * Set popup window frame origin so that the top-left corner of the
* window lines up with the top-left corner of this button. * window lines up with the top-left corner of this button.
@ -443,7 +442,7 @@
if (!_pbcFlags.pullsDown) if (!_pbcFlags.pullsDown)
{ {
winf.origin.y += (_selectedIndex * scratchRect.size.height); winf.origin.y += ([self indexOfSelectedItem] * scratchRect.size.height);
} }
NSLog(@"winf %@", NSStringFromRect(winf)); NSLog(@"winf %@", NSStringFromRect(winf));
@ -454,20 +453,20 @@ NSLog(@"winf %@", NSStringFromRect(winf));
[[_menu window] orderFrontRegardless]; [[_menu window] orderFrontRegardless];
} }
- (void)dismissPopUp - (void) dismissPopUp
{ {
[[_menu window] orderOut: nil]; [[_menu window] orderOut: nil];
} }
- (BOOL)trackMouse:(NSEvent *)theEvent - (BOOL) trackMouse: (NSEvent *)theEvent
inRect:(NSRect)cellFrame inRect: (NSRect)cellFrame
ofView:(NSView *)controlView ofView: (NSView *)controlView
untilMouseUp:(BOOL)untilMouseUp untilMouseUp: (BOOL)untilMouseUp
{ {
} }
- (void)performClickWithFrame:(NSRect)frame - (void) performClickWithFrame: (NSRect)frame
inView:(NSView *)controlView inView: (NSView *)controlView
{ {
int indexToClick; int indexToClick;
@ -482,12 +481,12 @@ NSLog(@"winf %@", NSStringFromRect(winf));
} }
// Arrow position for bezel style and borderless popups. // Arrow position for bezel style and borderless popups.
- (NSPopUpArrowPosition)arrowPosition - (NSPopUpArrowPosition) arrowPosition
{ {
return _pbcFlags.arrowPosition; return _pbcFlags.arrowPosition;
} }
- (void)setArrowPosition:(NSPopUpArrowPosition)position - (void) setArrowPosition: (NSPopUpArrowPosition)position
{ {
_pbcFlags.arrowPosition = position; _pbcFlags.arrowPosition = position;
} }
@ -508,11 +507,11 @@ NSLog(@"winf %@", NSStringFromRect(winf));
if (_pbcFlags.pullsDown) if (_pbcFlags.pullsDown)
{ {
aImage = [NSImage imageNamed:@"common_3DArrowDown"]; aImage = [NSImage imageNamed: @"common_3DArrowDown"];
} }
else else
{ {
aImage = [NSImage imageNamed:@"common_Nibble"]; aImage = [NSImage imageNamed: @"common_Nibble"];
} }
size = [aImage size]; size = [aImage size];
@ -529,7 +528,7 @@ NSLog(@"winf %@", NSStringFromRect(winf));
[view unlockFocus]; [view unlockFocus];
} }
- (void)_popUpItemAction:(id)sender - (void) _popUpItemAction: (id)sender
{ {
[self selectItemWithTitle: [sender title]]; [self selectItemWithTitle: [sender title]];
NSLog(@"%@", [sender title]); NSLog(@"%@", [sender title]);