mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-05-31 19:50:48 +00:00
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:
parent
d2759567a5
commit
1708e6cfe8
5 changed files with 291 additions and 267 deletions
10
ChangeLog
10
ChangeLog
|
@ -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>
|
||||
|
||||
* Source/NSMenuView.m ([-trackWithEvent:]): Fixed bug which was
|
||||
|
|
|
@ -12,7 +12,7 @@ typedef enum {
|
|||
@interface NSPopUpButtonCell : NSMenuItemCell
|
||||
{
|
||||
NSMenu *_menu;
|
||||
int _selectedIndex;
|
||||
NSMenuItem *_selectedItem;
|
||||
struct __pbcFlags {
|
||||
unsigned int pullsDown:1;
|
||||
unsigned int preferredEdge:3;
|
||||
|
|
|
@ -96,6 +96,21 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
|
|||
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];
|
||||
|
@ -248,7 +263,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
|
|||
|
||||
- (void) removeItem: (id <NSMenuItem>)anItem
|
||||
{
|
||||
[self removeItemAtIndex:[menu_items indexOfObject: anItem]];
|
||||
[self removeItemAtIndex: [menu_items indexOfObjectIdenticalTo: anItem]];
|
||||
}
|
||||
|
||||
- (void) removeItemAtIndex: (int)index
|
||||
|
@ -367,7 +382,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
|
|||
//
|
||||
- (int) indexOfItem: (id <NSMenuItem>)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,7 +400,7 @@ static NSString* NSMenuLocationsKey = @"NSMenuLocations";
|
|||
id anItem;
|
||||
|
||||
if ((anItem = [self itemWithTag: aTag]))
|
||||
return [menu_items indexOfObject: anItem];
|
||||
return [menu_items indexOfObjectIdenticalTo: anItem];
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
|
|
@ -40,6 +40,12 @@
|
|||
@end
|
||||
|
||||
@implementation NSPopUpButtonCell
|
||||
- (void) dealloc
|
||||
{
|
||||
RELEASE(_menu);
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
- (id) initTextCell: (NSString *)stringValue
|
||||
{
|
||||
return [self initTextCell: stringValue pullsDown: NO];
|
||||
|
@ -65,7 +71,7 @@
|
|||
[anItem setAction: @selector(_popUpItemAction:)];
|
||||
}
|
||||
|
||||
_menu = [NSMenu initWithTitle:@""];
|
||||
_menu = [[NSMenu alloc] initWithTitle: @""];
|
||||
[_menu _setOwnedByPopUp: YES];
|
||||
|
||||
return self;
|
||||
|
@ -156,13 +162,15 @@
|
|||
[anItem setAction: @selector(_popUpItemAction:)];
|
||||
|
||||
[_menu insertItem: anItem atIndex: [_menu numberOfItems]];
|
||||
RELEASE(anItem);
|
||||
}
|
||||
|
||||
- (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]];
|
||||
}
|
||||
|
@ -172,26 +180,35 @@
|
|||
{
|
||||
NSMenuItem *anItem = [NSMenuItem new];
|
||||
|
||||
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
|
||||
{
|
||||
[_menu removeItemWithTitle: title];
|
||||
[_menu removeItemAtIndex: [_menu indexOfItemWithTitle: title]];
|
||||
}
|
||||
|
||||
- (void) removeItemAtIndex: (int)index
|
||||
{
|
||||
[[_menu itemArray] removeObjectAtIndex: index];
|
||||
[_menu removeItemAtIndex: index];
|
||||
}
|
||||
|
||||
- (void) removeAllItems
|
||||
{
|
||||
[_menu removeAllItems];
|
||||
while ([_menu numberOfItems] > 0)
|
||||
{
|
||||
[_menu removeItemAtIndex: 0];
|
||||
}
|
||||
}
|
||||
|
||||
// Accessing the items
|
||||
|
@ -215,9 +232,9 @@
|
|||
return [_menu indexOfItemWithTitle: title];
|
||||
}
|
||||
|
||||
- (int)indexOfItemWithTag:(int)tag
|
||||
- (int) indexOfItemWithTag: (int)aTag
|
||||
{
|
||||
return [_menu indexOfItemWithTag: tag];
|
||||
return [_menu indexOfItemWithTag: aTag];
|
||||
}
|
||||
|
||||
- (int) indexOfItemWithRepresentedObject: (id)obj
|
||||
|
@ -225,9 +242,9 @@
|
|||
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
|
||||
|
@ -242,7 +259,11 @@
|
|||
|
||||
- (id <NSMenuItem>) lastItem
|
||||
{
|
||||
return [_menu lastItem];
|
||||
int end = [_menu numberOfItems] - 1;
|
||||
|
||||
if (end < 0)
|
||||
return nil;
|
||||
return [_menu itemAtIndex: end];
|
||||
}
|
||||
|
||||
// Dealing with selection
|
||||
|
@ -251,101 +272,73 @@
|
|||
if (!item)
|
||||
{
|
||||
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];
|
||||
[_selectedItem setState: NSOffState];
|
||||
}
|
||||
_selectedItem = item;
|
||||
if (_pbcFlags.altersStateOfSelectedItem)
|
||||
{
|
||||
[_selectedItem setState: NSOnState];
|
||||
}
|
||||
|
||||
_selectedIndex = aIndex;
|
||||
}
|
||||
}
|
||||
|
||||
- (void) selectItemAtIndex: (int)index
|
||||
{
|
||||
if (index == -1)
|
||||
{
|
||||
if (_pbcFlags.altersStateOfSelectedItem)
|
||||
[[self itemAtIndex: _selectedIndex] setState: NSOffState];
|
||||
NSMenuItem *anItem = (index == -1) ? nil : [self itemAtIndex: index];
|
||||
|
||||
_selectedIndex = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_pbcFlags.altersStateOfSelectedItem)
|
||||
{
|
||||
[[self itemAtIndex: _selectedIndex] setState: NSOffState];
|
||||
[[self itemAtIndex: index] setState: NSOnState];
|
||||
}
|
||||
|
||||
_selectedIndex = index;
|
||||
}
|
||||
[self selectItem: anItem];
|
||||
}
|
||||
|
||||
- (void) selectItemWithTitle: (NSString *)title
|
||||
{
|
||||
NSMenuItem *anItem = [self itemWithTitle: title];
|
||||
|
||||
if ([title length] < 1)
|
||||
{
|
||||
if (_pbcFlags.altersStateOfSelectedItem)
|
||||
[[self itemAtIndex: _selectedIndex] setState: NSOffState];
|
||||
|
||||
_selectedIndex = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
int aIndex = [self indexOfItemWithTitle: title];
|
||||
|
||||
if (_pbcFlags.altersStateOfSelectedItem)
|
||||
{
|
||||
[[self itemAtIndex: _selectedIndex] setState: NSOffState];
|
||||
[[self itemAtIndex: aIndex] setState: NSOnState];
|
||||
}
|
||||
|
||||
_selectedIndex = aIndex;
|
||||
}
|
||||
[self selectItem: anItem];
|
||||
}
|
||||
|
||||
- (void) setTitle: (NSString *)aString
|
||||
{
|
||||
NSMenuItem *anItem;
|
||||
|
||||
if (_pbcFlags.pullsDown)
|
||||
{
|
||||
if ([_menu numberOfItems] == 0)
|
||||
{
|
||||
anItem = nil;
|
||||
}
|
||||
else
|
||||
{
|
||||
int aIndex;
|
||||
|
||||
if (aIndex = [self indexOfItemWithTitle: aString])
|
||||
{
|
||||
[self selectItemAtIndex: aIndex];
|
||||
anItem = [_menu itemAtIndex: 0];
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
anItem = [_menu itemWithTitle: aString];
|
||||
if (anItem == nil)
|
||||
{
|
||||
[self addItemWithTitle: aString];
|
||||
[self selectItemWithTitle: aString];
|
||||
anItem = [_menu itemWithTitle: aString];
|
||||
}
|
||||
}
|
||||
[self selectItem: anItem];
|
||||
}
|
||||
|
||||
- (id <NSMenuItem>) selectedItem
|
||||
{
|
||||
if (_selectedIndex != -1)
|
||||
return [self itemAtIndex: _selectedIndex];
|
||||
else
|
||||
return nil;
|
||||
return _selectedItem;
|
||||
}
|
||||
|
||||
- (int) indexOfSelectedItem
|
||||
{
|
||||
return _selectedIndex;
|
||||
return [_menu indexOfItem: _selectedItem];
|
||||
}
|
||||
|
||||
- (void) synchronizeTitleAndSelectedItem
|
||||
|
@ -353,18 +346,21 @@
|
|||
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];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -376,21 +372,23 @@
|
|||
|
||||
- (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
|
||||
{
|
||||
if (_selectedIndex >= 0)
|
||||
return [[self itemAtIndex: _selectedIndex] title];
|
||||
if (_selectedItem != nil)
|
||||
return [_selectedItem title];
|
||||
else
|
||||
return @"";
|
||||
}
|
||||
|
@ -417,7 +415,8 @@
|
|||
|
||||
[_aCenter postNotification: _aNotif];
|
||||
|
||||
scratchRect.origin = [[controlView window] convertBaseToScreen: cellFrame.origin];
|
||||
scratchRect.origin
|
||||
= [[controlView window] convertBaseToScreen: cellFrame.origin];
|
||||
|
||||
[[_menu menuRepresentation] _setCellSize: cellFrame.size];
|
||||
[_menu sizeToFit];
|
||||
|
@ -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));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue