A miriad of changes, look at ChangeLogs.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@4497 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
michael 1999-06-30 03:10:38 +00:00
parent 1b6925558e
commit 6df630c781
19 changed files with 1658 additions and 561 deletions

View file

@ -1,30 +1,3 @@
/*
NSMenu.m
Copyright (C) 1996 Free Software Foundation, Inc.
Author: Ovidiu Predescu <ovidiu@net-community.com>
Date: May 1997
A completely rewritten version of the original source by Scott Christley.
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <gnustep/gui/config.h>
#include <Foundation/NSCoder.h>
#include <Foundation/NSArray.h>
@ -39,463 +12,306 @@
#include <AppKit/NSEvent.h>
#include <AppKit/NSFont.h>
#include <AppKit/NSMenu.h>
#include <math.h>
#include <AppKit/NSMenuView.h>
#include <AppKit/NSMenuItemCell.h>
#ifdef MAX
# undef MAX
#endif
#define MAX(a, b) \
({typeof(a) _a = (a); typeof(b) _b = (b); \
_a > _b ? _a : _b; })
@interface NSMenu (PrivateMethods2)
- (void) _menuChanged;
@end
@interface NSMenuMatrix (PrivateMethods2)
- (void) _resizeMenuForCellSize;
@end
//*****************************************************************************
//
// NSMenuMatrix
//
//*****************************************************************************
@implementation NSMenuMatrix
// Class variables
static NSFont* menuFont = nil;
- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
{
return YES;
}
- (id) initWithFrame: (NSRect)rect
{
[super initWithFrame: rect];
cells = [NSMutableArray new];
/* Don't initialize menuFont in +initialize since we don't know if the
DGS process knows anything about the fonts yet. */
if (!menuFont)
menuFont = [[NSFont systemFontOfSize: 0] retain];
cellSize = NSMakeSize (1, [menuFont pointSize] + [menuFont descender] + 6);
return self;
}
- (void) dealloc
{
NSDebugLog (@"NSMenuMatrix of menu '%@' dealloc", [menu title]);
[cells release];
[super dealloc];
}
- (id) copyWithZone: (NSZone*)zone
{
NSMenuMatrix* copy = [[isa allocWithZone: zone] initWithFrame: [self frame]];
int i, count;
NSDebugLog (@"copy menu matrix of menu with title '%@'", [menu title]);
for (i = 0, count = [cells count]; i < count; i++)
{
id aCell = [cells objectAtIndex: i];
id cellCopy = [[aCell copyWithZone: zone] autorelease];
[copy->cells addObject: cellCopy];
}
copy->cellSize = cellSize;
copy->menu = menu;
if (selectedCell)
{
int index = [cells indexOfObject: selectedCell];
copy->selectedCell = [[cells objectAtIndex: index] retain];
}
copy->selectedCellRect = selectedCellRect;
return copy;
}
- (void) _resizeMenuForCellSize
{
int i, count;
float titleWidth;
/* Compute the new width of the menu cells matrix */
cellSize.width = 0;
count = [cells count];
for (i = 0; i < count; i++)
{
titleWidth = [menuFont widthOfString:
[[cells objectAtIndex: i] stringValue]];
cellSize.width = MAX(titleWidth + ADDITIONAL_WIDTH, cellSize.width);
}
cellSize.width = MAX([menuFont widthOfString: [menu title]]
+ ADDITIONAL_WIDTH,
cellSize.width);
/* Resize the frame to hold all the menu cells */
[super setFrameSize: NSMakeSize (cellSize.width,
count ? (cellSize.height + INTERCELL_SPACE)*count - INTERCELL_SPACE : 0)];
}
- (id <NSMenuItem>)insertItemWithTitle: (NSString*)aString
action: (SEL)aSelector
keyEquivalent: (NSString*)charCode
atIndex: (unsigned int)index
{
id menuCell = [[[NSMenu cellClass] new] autorelease];
[menuCell setFont: menuFont];
[menuCell setTitle: aString];
[menuCell setAction: aSelector];
[menuCell setKeyEquivalent: charCode];
[cells insertObject: menuCell atIndex: index];
return menuCell;
}
- (BOOL) performKeyEquivalent: (NSEvent*)theEvent
{
return [menu performKeyEquivalent: theEvent];
}
- (void) removeItem: (id <NSMenuItem>)anItem
{
int row = [cells indexOfObject: anItem];
if (row == -1)
return;
[cells removeObjectAtIndex: row];
}
- (NSArray*) itemArray
{
return cells;
}
- (id <NSMenuItem>) itemWithTitle: (NSString*)aString
{
unsigned i, count = [cells count];
id menuCell;
for (i = 0; i < count; i++)
{
menuCell = [cells objectAtIndex: i];
if ([[menuCell title] isEqual: aString])
return menuCell;
}
return nil;
}
- (id <NSMenuItem>) itemWithTag: (int)aTag
{
unsigned i, count = [cells count];
id menuCell;
for (i = 0; i < count; i++)
{
menuCell = [cells objectAtIndex: i];
if ([menuCell tag] == aTag)
return menuCell;
}
return nil;
}
- (NSRect) cellFrameAtRow: (int)index
{
unsigned count = [cells count];
NSRect rect;
NSAssert(index >= 0 && index < count+1, @"invalid row coordinate");
rect.origin.x = 0;
rect.origin.y = (count - index - 1)
* (cellSize.height + INTERCELL_SPACE);
rect.size = cellSize;
return rect;
}
- (void)drawRect: (NSRect)rect
{
unsigned i, count = [cells count];
int max, howMany;
NSRect intRect = {{0, 0}, {0, 0}};
// If there are no cells then just return
if (count == 0) return;
max = ceil((float)count - rect.origin.y / (cellSize.height + INTERCELL_SPACE));
howMany = ceil(rect.size.height / (cellSize.height + INTERCELL_SPACE));
intRect.origin.y = (count - max) * (cellSize.height + INTERCELL_SPACE);
intRect.size = cellSize;
for (i = max - 1; howMany > 0; i--, howMany--)
{
id aCell = [cells objectAtIndex: i];
[aCell drawWithFrame: intRect inView: self];
intRect.origin.y += cellSize.height + INTERCELL_SPACE;
}
}
- (NSSize) cellSize
{
return cellSize;
}
- (void) setMenu: (NSMenu*)anObject
{
menu = anObject;
}
- (void) setSelectedCell: (id)aCell
{
selectedCell = aCell;
}
- (id) selectedCell
{
return selectedCell;
}
- (NSRect) selectedCellRect
{
return selectedCellRect;
}
@end /* NSMenuMatrix */
//*****************************************************************************
//
// NSMenu
//
//*****************************************************************************
static NSZone *menuZone = NULL;
@implementation NSMenu
// Class variables
static NSZone *menuZone = NULL;
static Class menuCellClass = nil;
// Class Methods
+ (void) initialize
{
menuCellClass = [NSMenuItem class];
if (self == [NSMenu class])
{
[self setVersion: 1];
}
}
+ (void) setMenuZone: (NSZone*)zone
+ (void)setMenuZone:(NSZone *)zone
{
menuZone = zone;
}
+ (NSZone*) menuZone
// Methods.
- (id)init
{
return menuZone;
[self initWithTitle:@"Menu"];
}
+ (void) setCellClass: (Class)aClass
- (id)initWithTitle:(NSString *)aTitle
{
menuCellClass = aClass;
}
[super init];
+ (Class) cellClass
{
return menuCellClass;
}
// Keep the title.
ASSIGN(menu_title, aTitle);
- (id) init
{
return [self initWithTitle:
[[[NSProcessInfo processInfo] processName] lastPathComponent]];
}
// Create an array to store out cells.
menu_items = [NSMutableArray new];
- (id) initWithTitle: (NSString*)aTitle
{
// SUBCLASS to initialize other "instance variables"
NSRect rect = {{0, 0}, {80, 20}};
// Create a NSMenuView to draw our cells.
menu_view = [[NSMenuView alloc] initWithFrame:NSMakeRect(0,0,50,50)];
// Set ourself as the menu for this view.
[menu_view setMenu:self];
// We have no supermenu.
menu_supermenu = nil;
menu_is_tornoff = NO;
menu_changed = YES;
ASSIGN(title, aTitle);
menuCells = [[NSMenuMatrix alloc] initWithFrame: rect];
[menuCells setMenu: self];
menuChangedMessagesEnabled = YES;
autoenablesItems = YES;
return self;
}
- (void) dealloc
- (void)insertItem:(id <NSMenuItem>)newItem
atIndex:(int)index
{
NSDebugLog (@"NSMenu '%@' dealloc", title);
[title release];
[menuCells release];
[super dealloc];
if ([(NSMenuItemCell *)newItem isKindOfClass:[NSMenuItemCell class]])
[menu_items insertObject:newItem atIndex:index];
else
NSLog(@"You must use an NSMenuItemCell, or a derivative thereof.\n");
}
- (id) copyWithZone: (NSZone*)zone
- (id <NSMenuItem>)insertItemWithTitle:(NSString *)aString
action:(SEL)aSelector
keyEquivalent:(NSString *)charCode
atIndex:(unsigned int)index
{
NSMenu *copy = NSAllocateObject (isa, 0, zone);
unsigned i, count;
NSArray *cells;
id anItem = [NSMenuItemCell new];
[anItem setTitle:aString];
[anItem setAction:aSelector];
[anItem setKeyEquivalent:charCode];
NSDebugLog (@"copy menu with title '%@'", [self title]);
[menu_items insertObject:anItem atIndex:index];
copy->title = [title copyWithZone: zone];
menu_changed = YES;
copy->menuCells = [menuCells copyWithZone: zone];
[copy->menuCells setMenu: copy];
return anItem;
}
/* Change the supermenu object of the new cells to the new menu */
cells = [copy->menuCells itemArray];
for (i = 0, count = [cells count]; i < count; i++) {
id cell = [cells objectAtIndex: i];
- (void)addItem:(id <NSMenuItem>)newItem
{
// The spec says we call [self insertItem]; but why waste the overhead?
if ([(NSMenuItemCell *)newItem isKindOfClass:[NSMenuItemCell class]])
[menu_items insertObject:newItem atIndex:[menu_items count]];
else
NSLog(@"You must use an NSMenuItemCell, or a derivative thereof.\n");
if ([cell hasSubmenu]) {
NSMenu* submenu = [cell target];
menu_changed = YES;
}
submenu->supermenu = copy;
}
- (id <NSMenuItem>)addItemWithTitle:(NSString *)aString
action:(SEL)aSelector
keyEquivalent:(NSString *)keyEquiv
{
return [self insertItemWithTitle:aString
action:aSelector
keyEquivalent:keyEquiv
atIndex:[menu_items count]];
}
- (void)removeItem:(id <NSMenuItem>)anItem
{
if ([(NSMenuItemCell *)anItem isKindOfClass:[NSMenuItemCell class]])
{
int _index = [menu_items indexOfObject:anItem];
if (_index == -1)
return;
[menu_items removeObjectAtIndex:_index];
} else {
NSLog(@"You must use an NSMenuItemCell, or a derivative thereof.\n");
}
[copy->menuCells setFrame: [menuCells frame]];
copy->supermenu = supermenu;
copy->attachedMenu = nil;
copy->autoenablesItems = autoenablesItems;
copy->menuChangedMessagesEnabled = menuChangedMessagesEnabled;
copy->menuHasChanged = menuHasChanged;
return copy;
menu_changed = YES;
}
- (id <NSMenuItem>) addItemWithTitle: (NSString*)aString
action: (SEL)aSelector
keyEquivalent: (NSString*)charCode
- (void)removeItemAtIndex:(int)index
{
return [self insertItemWithTitle: aString
action: aSelector
keyEquivalent: charCode
atIndex: [[menuCells itemArray] count]];
[menu_items removeObjectAtIndex:index];
menu_changed = YES;
}
- (id <NSMenuItem>) insertItemWithTitle: (NSString*)aString
action: (SEL)aSelector
keyEquivalent: (NSString*)charCode
atIndex: (unsigned int)index
- (void)itemChanged:(id <NSMenuItem>)anObject
{
id menuCell = [menuCells insertItemWithTitle: aString
action: aSelector
keyEquivalent: charCode
atIndex: index];
menuHasChanged = YES;
return menuCell;
}
- (void) removeItem: (id <NSMenuItem>)anItem
{
[menuCells removeItem: anItem];
menuHasChanged = YES;
}
- (NSArray*) itemArray
{
return [menuCells itemArray];
// another nebulous method in NSMenu.
}
- (id <NSMenuItem>) itemWithTag: (int)aTag
{
return [menuCells itemWithTag: aTag];
unsigned i, count = [menu_items count];
id menuCell;
for (i = 0; i < count; i++)
{
menuCell = [menu_items objectAtIndex:i];
if ([menuCell tag] == aTag)
return menuCell;
}
return nil;
}
- (id <NSMenuItem>) itemWithTitle: (NSString*)aString
{
return [menuCells itemWithTitle: aString];
unsigned i, count = [menu_items count];
id menuCell;
for (i = 0; i < count; i++)
{
menuCell = [menu_items objectAtIndex: i];
if ([[menuCell title] isEqual: aString])
return menuCell;
}
return nil;
}
- (void) setSubmenu: (NSMenu*)aMenu forItem: (id <NSMenuItem>)anItem
- (id <NSMenuItem>)itemAtIndex:(int)index
{
NSString *itemTitle = [anItem title];
// FIXME should raise an exception if out of range.
return [menu_items objectAtIndex:index];
}
[anItem setTarget: aMenu];
- (int)numberOfItems
{
return [menu_items count];
}
- (NSArray *)itemArray
{
return (NSArray *)menu_items;
}
- (int)indexOfItem:(id <NSMenuItem>)anObject
{
if (![(NSMenuItemCell *)anObject isKindOfClass:[NSMenuItemCell class]])
{
NSLog(@"You must use an NSMenuItemCell, or a derivative thereof.\n");
return -1;
}
return [menu_items indexOfObject:anObject];
}
- (int)indexOfItemWithTitle:(NSString *)aTitle
{
id anItem;
if ((anItem = [self itemWithTitle:aTitle]))
return [menu_items indexOfObject:anItem];
else
return -1;
}
- (int)indexOfItemWithTag:(int)aTag
{
id anItem;
if ((anItem = [self itemWithTag:aTag]))
return [menu_items indexOfObject:anItem];
else
return -1;
}
- (int)indexOfItemWithTarget:(id)anObject
andAction:(SEL)actionSelector
{
return -1;
}
- (int)indexOfItemWithRepresentedObject:(id)anObject
{
return -1;
}
- (int)indexOfItemWithSubmenu:(NSMenu *)anObject
{
return -1;
}
// Dealing with submenus.
- (void)setSubmenu:(NSMenu *)aMenu
forItem:(id <NSMenuItem>) anItem
{
[anItem setTarget:aMenu];
[anItem setAction: @selector(submenuAction:)];
if (aMenu)
aMenu->supermenu = self;
aMenu->menu_supermenu = self;
ASSIGN(aMenu->title, itemTitle);
[self _menuChanged];
ASSIGN(aMenu->menu_title, [anItem title]);
// notification that the menu has changed.
}
- (void) submenuAction: (id)sender
- (void)submenuAction:(id)sender
{
}
- (NSMenu*) attachedMenu
- (NSMenu *)attachedMenu
{
return attachedMenu;
return menu_attached_menu;
}
- (BOOL) isAttached
{
return supermenu && [supermenu attachedMenu] == self;
// eh?
return menu_supermenu && [menu_supermenu attachedMenu] == self;
}
- (BOOL) isTornOff
- (BOOL)isTornOff
{
// SUBCLASS
return NO;
return menu_is_tornoff;
}
- (NSPoint) locationForSubmenu: (NSMenu*)aSubmenu
- (NSPoint)locationForSubmenu:(NSMenu *)aSubmenu
{
// SUBCLASS
return NSZeroPoint;
}
- (NSMenu*) supermenu
- (NSMenu *)supermenu
{
return supermenu;
return menu_supermenu;
}
- (void) setAutoenablesItems: (BOOL)flag
- (void)setSupermenu:(NSMenu *)supermenu
{
autoenablesItems = flag;
ASSIGN(menu_supermenu, supermenu);
}
- (BOOL) autoenablesItems
- (void)setAutoenablesItems:(BOOL)flag
{
return autoenablesItems;
menu_autoenable = flag;
}
- (void) update
- (BOOL)autoenablesItems
{
// SUBCLASS to redisplay the menu if necessary
return menu_autoenable;
}
- (void)update
{
// FIXME: needs to be checked.
id cells;
int i, count;
id theApp = [NSApplication sharedApplication];
if (menu_changed)
[self sizeToFit];
if (![self autoenablesItems])
return;
cells = [menuCells itemArray];
count = [cells count];
count = [menu_items count];
/* Temporary disable automatic displaying of menu */
[self setMenuChangedMessagesEnabled: NO];
for (i = 0; i < count; i++)
{
id<NSMenuItem> cell = [cells objectAtIndex: i];
id<NSMenuItem> cell = [menu_items objectAtIndex: i];
SEL action = [cell action];
id target;
NSWindow* keyWindow;
@ -505,55 +321,92 @@ static Class menuCellClass = nil;
id validator = nil;
BOOL wasEnabled = [cell isEnabled];
BOOL shouldBeEnabled;
/* Update the submenu items if any */
if ([cell hasSubmenu])
[[cell target] update];
[[cell target] update];
/* If there is no action - there can be no validator for the cell */
if (action)
{
/* If there is a target use that for validation (or nil). */
if ((target = [cell target]))
{
if ([target respondsToSelector: action])
{
validator = target;
}
}
else
{
validator = [theApp targetForAction: action];
}
}
{
/* If there is a target use that for validation (or nil). */
if ((target = [cell target]))
{
if ([target respondsToSelector: action])
{
validator = target;
}
}
else
{
validator = [theApp targetForAction: action];
}
}
if (validator == nil)
{
shouldBeEnabled = NO;
}
else if ([validator respondsToSelector: @selector(validateMenuItem:)])
{
shouldBeEnabled = [validator validateMenuItem: cell];
}
{
shouldBeEnabled = NO;
}
else if ([validator respondsToSelector:@selector(validateMenuItem:)])
{
shouldBeEnabled = [validator validateMenuItem: cell];
}
else
{
shouldBeEnabled = YES;
}
{
shouldBeEnabled = YES;
}
if (shouldBeEnabled != wasEnabled)
{
[cell setEnabled: shouldBeEnabled];
[menuCells setNeedsDisplayInRect: [menuCells cellFrameAtRow: i]];
}
{
[cell setEnabled: shouldBeEnabled];
// FIXME
// [menuCells setNeedsDisplayInRect: [menuCells cellFrameAtRow: i]];
}
}
if (menuHasChanged)
if (menu_changed)
[self sizeToFit];
/* Reenable displaying of menus */
[self setMenuChangedMessagesEnabled: YES];
}
- (BOOL) performKeyEquivalent: (NSEvent*)theEvent
{
unsigned i;
unsigned count = [menu_items count];
NSEventType type = [theEvent type];
if (type != NSKeyDown && type != NSKeyUp)
return NO;
for (i = 0; i < count; i++)
{
id<NSMenuItem> cell = [menu_items objectAtIndex: i];
if ([cell hasSubmenu])
{
if ([[cell target] performKeyEquivalent: theEvent])
{
/* The event has been handled by a cell in submenu */
return YES;
}
}
else
{
if ([[cell keyEquivalent] isEqual:
[theEvent charactersIgnoringModifiers]])
{
[menu_view lockFocus];
[(id)cell performClick: self];
[menu_view unlockFocus];
return YES;
}
}
}
return NO;
}
- (void) performActionForItem: (id <NSMenuItem>)cell
{
NSNotificationCenter *nc;
@ -561,7 +414,7 @@ static Class menuCellClass = nil;
if (![cell isEnabled])
return;
nc = [NSNotificationCenter defaultCenter];
d = [NSDictionary dictionaryWithObject: cell forKey: @"MenuItem"];
[nc postNotificationName: NSMenuWillSendActionNotification
@ -571,81 +424,56 @@ static Class menuCellClass = nil;
to: [cell target]
from: cell];
[nc postNotificationName: NSMenuDidSendActionNotification
object: self
userInfo: d];
object: self
userInfo: d];
}
- (BOOL) performKeyEquivalent: (NSEvent*)theEvent
- (void)setTitle: (NSString*)aTitle
{
id cells = [menuCells itemArray];
unsigned i;
unsigned count = [cells count];
NSEventType type = [theEvent type];
if (type != NSKeyDown && type != NSKeyUp)
return NO;
for (i = 0; i < count; i++)
{
id<NSMenuItem> cell = [cells objectAtIndex: i];
if ([cell hasSubmenu])
{
if ([[cell target] performKeyEquivalent: theEvent])
{
/* The event has been handled by a cell in submenu */
return YES;
}
}
else
{
if ([[cell keyEquivalent] isEqual:
[theEvent charactersIgnoringModifiers]])
{
[menuCells lockFocus];
[(id)cell performClick: self];
[menuCells unlockFocus];
return YES;
}
}
}
return NO;
ASSIGN(menu_title, aTitle);
[self sizeToFit];
}
- (NSString*)title
{
return menu_title;
}
- (void) setMenuChangedMessagesEnabled: (BOOL)flag
- (void)setMenuRepresentation:(id)menuRep
{
menuChangedMessagesEnabled = flag;
ASSIGN(menu_rep, menuRep);
}
- (BOOL) menuChangedMessagesEnabled
- (id)menuRepresentation
{
return menuChangedMessagesEnabled;
return menu_rep;
}
- (void)setMenuChangedMessagesEnabled: (BOOL)flag
{
menu_ChangedMessagesEnabled = flag;
}
- (BOOL)menuChangedMessagesEnabled
{
return menu_ChangedMessagesEnabled;
}
- (void) sizeToFit
{
// SUBCLASS
[menuCells _resizeMenuForCellSize];
[menuCells setNeedsDisplay: YES];
menuHasChanged = NO;
{
// NSLog(@"- sizeToFit called in NSMenu\n");
//
// [menu_view sizeToFit];
// [menu_view setNeedsDisplay:YES];
// menu_changed = NO;
}
- (void) setTitle: (NSString*)aTitle
- (void)helpRequested:(NSEvent *)event
{
ASSIGN(title, aTitle);
[self sizeToFit];
}
- (NSString*) title
{
return title;
}
- (NSMenuMatrix*) menuCells
{
return menuCells;
// Won't be implemented until we have NSHelp*
}
// NSCoding
- (id) initWithCoder: (NSCoder*)aDecoder
{
return self;
@ -655,21 +483,9 @@ static Class menuCellClass = nil;
{
}
// non OS spec methods
- (void) _rightMouseDisplay
//NSCopying
- (id) copyWithZone: (NSZone*)zone
{
return self;
}
@end /* NSMenu */
@implementation NSMenu (PrivateMethods2)
- (void) _menuChanged
{
menuHasChanged = YES;
if (menuChangedMessagesEnabled)
[self sizeToFit];
}
@end