Added some dimensions checking

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@3899 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 1999-03-11 12:34:04 +00:00
parent c51f88e0a8
commit b5e5ca3ae4
2 changed files with 174 additions and 144 deletions

View file

@ -1,7 +1,9 @@
Thu Mar 11 11:35:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk> Thu Mar 11 11:57:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSBox.m: Tidied and added code to prevent attempts to set * Source/NSBox.m: Tidied and added code to prevent attempts to set
contentview to negative dimensions. contentview to negative dimensions.
* Source/NSMenu.m: Tidied and added code to prevent attempts to set
mwnu matrix frame to negative dimensions.
Wed Mar 10 12:56:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk> Wed Mar 10 12:56:00 1999 Richard Frith-Macdonald <richard@brainstorm.co.uk>

View file

@ -28,6 +28,7 @@
#include <gnustep/gui/config.h> #include <gnustep/gui/config.h>
#include <Foundation/NSCoder.h> #include <Foundation/NSCoder.h>
#include <Foundation/NSArray.h> #include <Foundation/NSArray.h>
#include <Foundation/NSException.h>
#include <Foundation/NSProcessInfo.h> #include <Foundation/NSProcessInfo.h>
#include <Foundation/NSString.h> #include <Foundation/NSString.h>
#include <Foundation/NSNotification.h> #include <Foundation/NSNotification.h>
@ -49,11 +50,11 @@
@interface NSMenu (PrivateMethods2) @interface NSMenu (PrivateMethods2)
- (void)_menuChanged; - (void) _menuChanged;
@end @end
@interface NSMenuMatrix (PrivateMethods2) @interface NSMenuMatrix (PrivateMethods2)
- (void)_resizeMenuForCellSize; - (void) _resizeMenuForCellSize;
@end @end
//***************************************************************************** //*****************************************************************************
@ -67,21 +68,21 @@
// Class variables // Class variables
static NSFont* menuFont = nil; static NSFont* menuFont = nil;
- initWithFrame:(NSRect)rect - (id) initWithFrame: (NSRect)rect
{ {
[super initWithFrame:rect]; [super initWithFrame: rect];
cells = [NSMutableArray new]; cells = [NSMutableArray new];
/* Don't initialize menuFont in +initialize since we don't know if the /* Don't initialize menuFont in +initialize since we don't know if the
DGS process knows anything about the fonts yet. */ DGS process knows anything about the fonts yet. */
if (!menuFont) if (!menuFont)
menuFont = [[NSFont systemFontOfSize:0] retain]; menuFont = [[NSFont systemFontOfSize: 0] retain];
cellSize = NSMakeSize (1, [menuFont pointSize] - [menuFont descender] + 6); cellSize = NSMakeSize (1, [menuFont pointSize] - [menuFont descender] + 6);
return self; return self;
} }
- (void)dealloc - (void) dealloc
{ {
NSDebugLog (@"NSMenuMatrix of menu '%@' dealloc", [menu title]); NSDebugLog (@"NSMenuMatrix of menu '%@' dealloc", [menu title]);
@ -89,32 +90,34 @@ static NSFont* menuFont = nil;
[super dealloc]; [super dealloc];
} }
- (id)copyWithZone:(NSZone*)zone - (id) copyWithZone: (NSZone*)zone
{ {
NSMenuMatrix* copy = [[isa alloc] initWithFrame:[self frame]]; NSMenuMatrix* copy = [[isa allocWithZone: zone] initWithFrame: [self frame]];
int i, count; int i, count;
NSDebugLog (@"copy menu matrix of menu with title '%@'", [menu title]); NSDebugLog (@"copy menu matrix of menu with title '%@'", [menu title]);
for (i = 0, count = [cells count]; i < count; i++) { for (i = 0, count = [cells count]; i < count; i++)
id aCell = [cells objectAtIndex:i]; {
id cellCopy = [[aCell copyWithZone:zone] autorelease]; id aCell = [cells objectAtIndex: i];
id cellCopy = [[aCell copyWithZone: zone] autorelease];
[copy->cells addObject:cellCopy]; [copy->cells addObject: cellCopy];
} }
copy->cellSize = cellSize; copy->cellSize = cellSize;
copy->menu = menu; copy->menu = menu;
if (selectedCell) { if (selectedCell)
int index = [cells indexOfObject:selectedCell]; {
int index = [cells indexOfObject: selectedCell];
copy->selectedCell = [[cells objectAtIndex:index] retain]; copy->selectedCell = [[cells objectAtIndex: index] retain];
} }
copy->selectedCellRect = selectedCellRect; copy->selectedCellRect = selectedCellRect;
return copy; return copy;
} }
- (void)_resizeMenuForCellSize - (void) _resizeMenuForCellSize
{ {
int i, count; int i, count;
float titleWidth; float titleWidth;
@ -122,91 +125,99 @@ static NSFont* menuFont = nil;
/* Compute the new width of the menu cells matrix */ /* Compute the new width of the menu cells matrix */
cellSize.width = 0; cellSize.width = 0;
count = [cells count]; count = [cells count];
for (i = 0; i < count; i++) { for (i = 0; i < count; i++)
titleWidth = [menuFont widthOfString: {
[[cells objectAtIndex:i] stringValue]]; titleWidth = [menuFont widthOfString:
cellSize.width = MAX(titleWidth + ADDITIONAL_WIDTH, cellSize.width); [[cells objectAtIndex: i] stringValue]];
} cellSize.width = MAX(titleWidth + ADDITIONAL_WIDTH, cellSize.width);
cellSize.width = MAX([menuFont widthOfString:[menu title]] }
cellSize.width = MAX([menuFont widthOfString: [menu title]]
+ ADDITIONAL_WIDTH, + ADDITIONAL_WIDTH,
cellSize.width); cellSize.width);
/* Resize the frame to hold all the menu cells */ /* Resize the frame to hold all the menu cells */
[super setFrameSize:NSMakeSize (cellSize.width, [super setFrameSize: NSMakeSize (cellSize.width,
(cellSize.height + INTERCELL_SPACE) * [cells count] - INTERCELL_SPACE)]; count ? (cellSize.height + INTERCELL_SPACE)*count - INTERCELL_SPACE : 0)];
} }
- (id <NSMenuItem>)insertItemWithTitle:(NSString*)aString - (id <NSMenuItem>)insertItemWithTitle: (NSString*)aString
action:(SEL)aSelector action: (SEL)aSelector
keyEquivalent:(NSString*)charCode keyEquivalent: (NSString*)charCode
atIndex:(unsigned int)index atIndex: (unsigned int)index
{ {
id menuCell = [[[NSMenu cellClass] new] autorelease]; id menuCell = [[[NSMenu cellClass] new] autorelease];
[menuCell setFont:menuFont]; // set font first in order to avoid [menuCell setFont: menuFont];
// recalc of some cached params in xraw [menuCell setTitle: aString];
[menuCell setTitle:aString]; [menuCell setAction: aSelector];
[menuCell setAction:aSelector]; [menuCell setKeyEquivalent: charCode];
[menuCell setKeyEquivalent:charCode];
[cells insertObject:menuCell atIndex:index]; [cells insertObject: menuCell atIndex: index];
return menuCell; return menuCell;
} }
- (void)removeItem:(id <NSMenuItem>)anItem - (void) removeItem: (id <NSMenuItem>)anItem
{ {
int row = [cells indexOfObject:anItem]; int row = [cells indexOfObject: anItem];
if (row == -1) if (row == -1)
return; return;
[cells removeObjectAtIndex:row]; [cells removeObjectAtIndex: row];
} }
- (NSArray*)itemArray { return cells; } - (NSArray*) itemArray
- (id <NSMenuItem>)itemWithTitle:(NSString*)aString
{ {
int i, count = [cells count]; return cells;
}
- (id <NSMenuItem>) itemWithTitle: (NSString*)aString
{
unsigned i, count = [cells count];
id menuCell; id menuCell;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++)
menuCell = [cells objectAtIndex:i]; {
if ([[menuCell title] isEqual:aString]) menuCell = [cells objectAtIndex: i];
return menuCell; if ([[menuCell title] isEqual: aString])
} return menuCell;
}
return nil; return nil;
} }
- (id <NSMenuItem>)itemWithTag:(int)aTag - (id <NSMenuItem>) itemWithTag: (int)aTag
{ {
int i, count = [cells count]; unsigned i, count = [cells count];
id menuCell; id menuCell;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++)
menuCell = [cells objectAtIndex:i]; {
if ([menuCell tag] == aTag) menuCell = [cells objectAtIndex: i];
return menuCell; if ([menuCell tag] == aTag)
} return menuCell;
}
return nil; return nil;
} }
- (NSRect)cellFrameAtRow:(int)index - (NSRect) cellFrameAtRow: (int)index
{ {
unsigned count = [cells count];
NSRect rect; NSRect rect;
NSAssert(index >= 0 && index < count+1, @"invalid row coordinate");
rect.origin.x = 0; rect.origin.x = 0;
rect.origin.y = ([cells count] - index - 1) rect.origin.y = (count - index - 1)
* (cellSize.height + INTERCELL_SPACE); * (cellSize.height + INTERCELL_SPACE);
rect.size = cellSize; rect.size = cellSize;
return rect; return rect;
} }
- (void)drawRect:(NSRect)rect - (void)drawRect: (NSRect)rect
{ {
int i, count = [cells count]; unsigned i, count = [cells count];
int max, howMany; int max, howMany;
NSRect intRect = {{0, 0}, {0, 0}}; NSRect intRect = {{0, 0}, {0, 0}};
@ -218,19 +229,39 @@ static NSFont* menuFont = nil;
intRect.origin.y = (count - max) * (cellSize.height + INTERCELL_SPACE); intRect.origin.y = (count - max) * (cellSize.height + INTERCELL_SPACE);
intRect.size = cellSize; intRect.size = cellSize;
for (i = max - 1; howMany > 0; i--, howMany--) { for (i = max - 1; howMany > 0; i--, howMany--)
id aCell = [cells objectAtIndex:i]; {
id aCell = [cells objectAtIndex: i];
[aCell drawWithFrame:intRect inView:self]; [aCell drawWithFrame: intRect inView: self];
intRect.origin.y += cellSize.height + INTERCELL_SPACE; intRect.origin.y += cellSize.height + INTERCELL_SPACE;
} }
} }
- (NSSize)cellSize { return cellSize; } - (NSSize) cellSize
- (void)setMenu:(NSMenu*)anObject { menu = anObject; } {
- (void)setSelectedCell:(id)aCell { selectedCell = aCell; } return cellSize;
- (id)selectedCell { return selectedCell; } }
- (NSRect)selectedCellRect { return selectedCellRect; }
- (void) setMenu: (NSMenu*)anObject
{
menu = anObject;
}
- (void) setSelectedCell: (id)aCell
{
selectedCell = aCell;
}
- (id) selectedCell
{
return selectedCell;
}
- (NSRect) selectedCellRect
{
return selectedCellRect;
}
@end /* NSMenuMatrix */ @end /* NSMenuMatrix */
@ -247,51 +278,51 @@ static NSFont* menuFont = nil;
static NSZone *menuZone = NULL; static NSZone *menuZone = NULL;
static Class menuCellClass = nil; static Class menuCellClass = nil;
+ (void)initialize + (void) initialize
{ {
menuCellClass = [NSMenuItem class]; menuCellClass = [NSMenuItem class];
} }
+ (void)setMenuZone:(NSZone*)zone + (void) setMenuZone: (NSZone*)zone
{ {
menuZone = zone; menuZone = zone;
} }
+ (NSZone*)menuZone + (NSZone*) menuZone
{ {
return menuZone; return menuZone;
} }
+ (void)setCellClass:(Class)aClass + (void) setCellClass: (Class)aClass
{ {
menuCellClass = aClass; menuCellClass = aClass;
} }
+ (Class)cellClass + (Class) cellClass
{ {
return menuCellClass; return menuCellClass;
} }
- init - (id) init
{ {
return [self initWithTitle: return [self initWithTitle:
[[[NSProcessInfo processInfo] processName] lastPathComponent]]; [[[NSProcessInfo processInfo] processName] lastPathComponent]];
} }
- (id)initWithTitle:(NSString*)aTitle - (id) initWithTitle: (NSString*)aTitle
{ {
// SUBCLASS to initialize other "instance variables" // SUBCLASS to initialize other "instance variables"
NSRect rect = {{0, 0}, {80, 20}}; NSRect rect = {{0, 0}, {80, 20}};
ASSIGN(title, aTitle); ASSIGN(title, aTitle);
menuCells = [[NSMenuMatrix alloc] initWithFrame:rect]; menuCells = [[NSMenuMatrix alloc] initWithFrame: rect];
[menuCells setMenu:self]; [menuCells setMenu: self];
menuChangedMessagesEnabled = YES; menuChangedMessagesEnabled = YES;
autoenablesItems = YES; autoenablesItems = YES;
return self; return self;
} }
- (void)dealloc - (void) dealloc
{ {
NSDebugLog (@"NSMenu '%@' dealloc", title); NSDebugLog (@"NSMenu '%@' dealloc", title);
@ -300,7 +331,7 @@ static Class menuCellClass = nil;
[super dealloc]; [super dealloc];
} }
- (id)copyWithZone:(NSZone*)zone - (id) copyWithZone: (NSZone*)zone
{ {
NSMenu *copy = NSAllocateObject (isa, 0, zone); NSMenu *copy = NSAllocateObject (isa, 0, zone);
unsigned i, count; unsigned i, count;
@ -316,7 +347,7 @@ static Class menuCellClass = nil;
/* Change the supermenu object of the new cells to the new menu */ /* Change the supermenu object of the new cells to the new menu */
cells = [copy->menuCells itemArray]; cells = [copy->menuCells itemArray];
for (i = 0, count = [cells count]; i < count; i++) { for (i = 0, count = [cells count]; i < count; i++) {
id cell = [cells objectAtIndex:i]; id cell = [cells objectAtIndex: i];
if ([cell hasSubmenu]) { if ([cell hasSubmenu]) {
NSMenu* submenu = [cell target]; NSMenu* submenu = [cell target];
@ -325,7 +356,7 @@ static Class menuCellClass = nil;
} }
} }
[copy->menuCells setFrame:[menuCells frame]]; [copy->menuCells setFrame: [menuCells frame]];
copy->supermenu = supermenu; copy->supermenu = supermenu;
copy->attachedMenu = nil; copy->attachedMenu = nil;
@ -336,109 +367,106 @@ static Class menuCellClass = nil;
return copy; return copy;
} }
- (id <NSMenuItem>)addItemWithTitle:(NSString*)aString - (id <NSMenuItem>) addItemWithTitle: (NSString*)aString
action:(SEL)aSelector action: (SEL)aSelector
keyEquivalent:(NSString*)charCode keyEquivalent: (NSString*)charCode
{ {
return [self insertItemWithTitle:aString return [self insertItemWithTitle: aString
action:aSelector action: aSelector
keyEquivalent:charCode keyEquivalent: charCode
atIndex:[[menuCells itemArray] count]]; atIndex: [[menuCells itemArray] count]];
} }
- (id <NSMenuItem>)insertItemWithTitle:(NSString*)aString - (id <NSMenuItem>) insertItemWithTitle: (NSString*)aString
action:(SEL)aSelector action: (SEL)aSelector
keyEquivalent:(NSString*)charCode keyEquivalent: (NSString*)charCode
atIndex:(unsigned int)index atIndex: (unsigned int)index
{ {
id menuCell = [menuCells insertItemWithTitle:aString id menuCell = [menuCells insertItemWithTitle: aString
action:aSelector action: aSelector
keyEquivalent:charCode keyEquivalent: charCode
atIndex:index]; atIndex: index];
menuHasChanged = YES; // menu needs update menuHasChanged = YES;
return menuCell; return menuCell;
} }
- (void)removeItem:(id <NSMenuItem>)anItem - (void) removeItem: (id <NSMenuItem>)anItem
{ {
[menuCells removeItem:anItem]; [menuCells removeItem: anItem];
menuHasChanged = YES; // menu needs update menuHasChanged = YES;
} }
- (NSArray*)itemArray - (NSArray*) itemArray
{ {
return [menuCells itemArray]; return [menuCells itemArray];
} }
- (id <NSMenuItem>)itemWithTag:(int)aTag - (id <NSMenuItem>) itemWithTag: (int)aTag
{ {
return [menuCells itemWithTag:aTag]; return [menuCells itemWithTag: aTag];
} }
- (id <NSMenuItem>)itemWithTitle:(NSString*)aString - (id <NSMenuItem>) itemWithTitle: (NSString*)aString
{ {
return [menuCells itemWithTitle:aString]; return [menuCells itemWithTitle: aString];
} }
- (void)setSubmenu:(NSMenu*)aMenu forItem:(id <NSMenuItem>)anItem - (void) setSubmenu: (NSMenu*)aMenu forItem: (id <NSMenuItem>)anItem
{ {
NSString* itemTitle = [anItem title]; NSString *itemTitle = [anItem title];
[anItem setTarget:aMenu]; [anItem setTarget: aMenu];
[anItem setAction:@selector(submenuAction:)]; [anItem setAction: @selector(submenuAction:)];
if (aMenu) if (aMenu)
aMenu->supermenu = self; aMenu->supermenu = self;
[itemTitle retain]; ASSIGN(aMenu->title, itemTitle);
// [aMenu->title release];
aMenu->title = itemTitle;
[self _menuChanged]; [self _menuChanged];
} }
- (void)submenuAction:(id)sender - (void) submenuAction: (id)sender
{ {
} }
- (NSMenu*)attachedMenu - (NSMenu*) attachedMenu
{ {
return attachedMenu; return attachedMenu;
} }
- (BOOL)isAttached - (BOOL) isAttached
{ {
return supermenu && [supermenu attachedMenu] == self; return supermenu && [supermenu attachedMenu] == self;
} }
- (BOOL)isTornOff - (BOOL) isTornOff
{ {
// SUBCLASS // SUBCLASS
return NO; return NO;
} }
- (NSPoint)locationForSubmenu:(NSMenu*)aSubmenu - (NSPoint) locationForSubmenu: (NSMenu*)aSubmenu
{ {
// SUBCLASS // SUBCLASS
return NSZeroPoint; return NSZeroPoint;
} }
- (NSMenu*)supermenu - (NSMenu*) supermenu
{ {
return supermenu; return supermenu;
} }
- (void)setAutoenablesItems:(BOOL)flag - (void) setAutoenablesItems: (BOOL)flag
{ {
autoenablesItems = flag; autoenablesItems = flag;
} }
- (BOOL)autoenablesItems - (BOOL) autoenablesItems
{ {
return autoenablesItems; return autoenablesItems;
} }
- (void)update - (void) update
{ {
// SUBCLASS to redisplay the menu if necessary // SUBCLASS to redisplay the menu if necessary
@ -453,7 +481,7 @@ static Class menuCellClass = nil;
count = [cells count]; count = [cells count];
/* Temporary disable automatic displaying of menu */ /* Temporary disable automatic displaying of menu */
[self setMenuChangedMessagesEnabled:NO]; [self setMenuChangedMessagesEnabled: NO];
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
@ -513,10 +541,10 @@ static Class menuCellClass = nil;
[self sizeToFit]; [self sizeToFit];
/* Reenable displaying of menus */ /* Reenable displaying of menus */
[self setMenuChangedMessagesEnabled:YES]; [self setMenuChangedMessagesEnabled: YES];
} }
- (void) performActionForItem: (id <NSMenuItem>)cell - (void) performActionForItem: (id <NSMenuItem>)cell
{ {
NSNotificationCenter *nc; NSNotificationCenter *nc;
NSDictionary *d; NSDictionary *d;
@ -537,7 +565,7 @@ static Class menuCellClass = nil;
userInfo: d]; userInfo: d];
} }
- (BOOL)performKeyEquivalent:(NSEvent*)theEvent - (BOOL) performKeyEquivalent: (NSEvent*)theEvent
{ {
id cells = [menuCells itemArray]; id cells = [menuCells itemArray];
int i, count = [cells count]; int i, count = [cells count];
@ -547,18 +575,18 @@ static Class menuCellClass = nil;
return NO; return NO;
for (i = 0; i < count; i++) { for (i = 0; i < count; i++) {
id<NSMenuItem> cell = [cells objectAtIndex:i]; id<NSMenuItem> cell = [cells objectAtIndex: i];
if ([cell hasSubmenu]) { if ([cell hasSubmenu]) {
if ([[cell target] performKeyEquivalent:theEvent]) if ([[cell target] performKeyEquivalent: theEvent])
/* The event has been handled by a cell in submenu */ /* The event has been handled by a cell in submenu */
return YES; return YES;
} }
else { else {
if ([[cell keyEquivalent] isEqual:[theEvent charactersIgnoringModifiers]] if ([[cell keyEquivalent] isEqual: [theEvent charactersIgnoringModifiers]]
&& [cell keyEquivalentModifierMask] == [theEvent modifierFlags]) { && [cell keyEquivalentModifierMask] == [theEvent modifierFlags]) {
[menuCells lockFocus]; [menuCells lockFocus];
[(id)cell performClick:self]; [(id)cell performClick: self];
[menuCells unlockFocus]; [menuCells unlockFocus];
return YES; return YES;
} }
@ -568,51 +596,51 @@ static Class menuCellClass = nil;
return NO; return NO;
} }
- (void)setMenuChangedMessagesEnabled:(BOOL)flag - (void) setMenuChangedMessagesEnabled: (BOOL)flag
{ {
menuChangedMessagesEnabled = flag; menuChangedMessagesEnabled = flag;
} }
- (BOOL)menuChangedMessagesEnabled - (BOOL) menuChangedMessagesEnabled
{ {
return menuChangedMessagesEnabled; return menuChangedMessagesEnabled;
} }
- (void)sizeToFit - (void) sizeToFit
{ {
// SUBCLASS // SUBCLASS
[menuCells _resizeMenuForCellSize]; [menuCells _resizeMenuForCellSize];
[menuCells setNeedsDisplay:YES]; [menuCells setNeedsDisplay: YES];
menuHasChanged = NO; menuHasChanged = NO;
} }
- (void)setTitle:(NSString*)aTitle - (void) setTitle: (NSString*)aTitle
{ {
ASSIGN(title, aTitle); ASSIGN(title, aTitle);
[self sizeToFit]; [self sizeToFit];
} }
- (NSString*)title - (NSString*) title
{ {
return title; return title;
} }
- (NSMenuMatrix*)menuCells - (NSMenuMatrix*) menuCells
{ {
return menuCells; return menuCells;
} }
- (id) initWithCoder: (NSCoder*)aDecoder - (id) initWithCoder: (NSCoder*)aDecoder
{ {
return self; return self;
} }
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
} }
// non OS spec methods // non OS spec methods
- (void)_rightMouseDisplay - (void) _rightMouseDisplay
{ {
} }
@ -621,7 +649,7 @@ static Class menuCellClass = nil;
@implementation NSMenu (PrivateMethods2) @implementation NSMenu (PrivateMethods2)
- (void)_menuChanged - (void) _menuChanged
{ {
menuHasChanged = YES; menuHasChanged = YES;
if (menuChangedMessagesEnabled) if (menuChangedMessagesEnabled)