1996-05-30 20:03:15 +00:00
|
|
|
/*
|
|
|
|
NSMenu.m
|
|
|
|
|
|
|
|
The menu class
|
|
|
|
|
|
|
|
Copyright (C) 1996 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
|
|
|
Date: 1996
|
|
|
|
|
|
|
|
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
|
1996-10-18 17:14:13 +00:00
|
|
|
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.
|
1996-05-30 20:03:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gnustep/gui/NSMenu.h>
|
1996-09-12 19:24:32 +00:00
|
|
|
#include <gnustep/gui/NSMenuPrivate.h>
|
1996-05-30 20:03:15 +00:00
|
|
|
#include <Foundation/NSLock.h>
|
|
|
|
#include <gnustep/base/NSCoder.h>
|
1996-10-03 18:45:41 +00:00
|
|
|
#include <gnustep/gui/NSApplication.h>
|
1996-05-30 20:03:15 +00:00
|
|
|
|
1996-09-12 19:24:32 +00:00
|
|
|
NSZone *gnustep_gui_nsmenu_zone = NULL;
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
@implementation NSMenu
|
|
|
|
|
|
|
|
//
|
|
|
|
// Class methods
|
|
|
|
//
|
|
|
|
+ (void)initialize
|
|
|
|
{
|
|
|
|
if (self == [NSMenu class])
|
|
|
|
{
|
|
|
|
// Initial version
|
|
|
|
[self setVersion:1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Controlling Allocation Zones
|
|
|
|
//
|
|
|
|
+ (NSZone *)menuZone
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return gnustep_gui_nsmenu_zone;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)setMenuZone:(NSZone *)zone
|
1996-09-12 19:24:32 +00:00
|
|
|
{
|
|
|
|
gnustep_gui_nsmenu_zone = zone;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Instance methods
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Initializing a New NSMenu
|
|
|
|
//
|
|
|
|
- init
|
|
|
|
{
|
|
|
|
return [self initWithTitle:@""];
|
|
|
|
}
|
|
|
|
|
1996-09-12 19:24:32 +00:00
|
|
|
// Default initializer
|
1996-05-30 20:03:15 +00:00
|
|
|
- (id)initWithTitle:(NSString *)aTitle
|
|
|
|
{
|
1996-10-03 18:45:41 +00:00
|
|
|
NSApplication *theApp = [NSApplication sharedApplication];
|
|
|
|
|
1996-09-12 19:24:32 +00:00
|
|
|
// Init our superclass but skip any of its backend implementation
|
|
|
|
[super cleanInit];
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
window_title = aTitle;
|
|
|
|
menu_items = [NSMutableArray array];
|
1996-09-12 19:24:32 +00:00
|
|
|
super_menu = nil;
|
|
|
|
autoenables_items = NO;
|
1996-10-03 18:45:41 +00:00
|
|
|
// [self setContentView:[[NSView alloc] initWithFrame:frame]];
|
|
|
|
menu_matrix = [[NSMatrix alloc] initWithFrame: NSZeroRect];
|
|
|
|
[menu_matrix setCellClass: [NSMenuCell class]];
|
|
|
|
[menu_matrix setIntercellSpacing: NSZeroSize];
|
|
|
|
[self setContentView: menu_matrix];
|
1996-09-12 19:24:32 +00:00
|
|
|
is_torn_off = NO;
|
|
|
|
|
1996-10-03 18:45:41 +00:00
|
|
|
// Register ourselves with the Application object
|
|
|
|
[theApp addWindowsItem:self title:window_title filename:NO];
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Setting Up the Menu Commands
|
|
|
|
//
|
|
|
|
- (id)addItemWithTitle:(NSString *)aString
|
|
|
|
action:(SEL)aSelector
|
1996-09-12 19:24:32 +00:00
|
|
|
keyEquivalent:(NSString *)charCode
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
|
|
|
NSMenuCell *m;
|
|
|
|
unsigned int mi;
|
|
|
|
|
|
|
|
m = [[NSMenuCell alloc] initTextCell:aString];
|
|
|
|
[m setAction:aSelector];
|
|
|
|
[menu_items addObject:m];
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)insertItemWithTitle:(NSString *)aString
|
|
|
|
action:(SEL)aSelector
|
1996-09-12 19:24:32 +00:00
|
|
|
keyEquivalent:(NSString *)charCode
|
|
|
|
atIndex:(unsigned int)index
|
1996-05-30 20:03:15 +00:00
|
|
|
{
|
|
|
|
NSMenuCell *m;
|
|
|
|
unsigned int mi;
|
|
|
|
|
|
|
|
m = [[NSMenuCell alloc] initTextCell:aString];
|
|
|
|
[m setAction:aSelector];
|
|
|
|
[menu_items insertObject:m atIndex:index];
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)itemArray
|
|
|
|
{
|
|
|
|
return menu_items;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMatrix *)itemMatrix
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return menu_matrix;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setItemMatrix:(NSMatrix *)aMatrix
|
1996-09-12 19:24:32 +00:00
|
|
|
{
|
|
|
|
menu_matrix = aMatrix;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Finding Menu Items
|
|
|
|
//
|
|
|
|
- (id)cellWithTag:(int)aTag
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
NSMenuCell *m, *found;
|
|
|
|
|
1996-09-12 19:24:32 +00:00
|
|
|
// Recursively find the menu cell with the tag
|
1996-05-30 20:03:15 +00:00
|
|
|
found = nil;
|
|
|
|
j = [menu_items count];
|
|
|
|
for (i = 0;i < j; ++i)
|
|
|
|
{
|
|
|
|
m = [menu_items objectAtIndex:i];
|
|
|
|
if ([m tag] == aTag) return m;
|
|
|
|
if ([m hasSubmenu])
|
|
|
|
found = [[m submenu] cellWithTag:aTag];
|
|
|
|
if (found) return found;
|
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Building Submenus
|
|
|
|
//
|
|
|
|
- (NSMenuCell *)setSubmenu:(NSMenu *)aMenu
|
|
|
|
forItem:(NSMenuCell *)aCell
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
NSMenuCell *m;
|
|
|
|
|
|
|
|
j = [menu_items count];
|
|
|
|
for (i = 0;i < j; ++i)
|
|
|
|
{
|
|
|
|
m = [menu_items objectAtIndex:i];
|
|
|
|
if (m == aCell)
|
|
|
|
{
|
|
|
|
// Set the menucell's submenu
|
|
|
|
[m setSubmenu:aMenu];
|
|
|
|
|
1996-09-12 19:24:32 +00:00
|
|
|
// Tell the submenu we are its supermenu
|
|
|
|
[aMenu setSupermenu: self];
|
|
|
|
|
1996-05-30 20:03:15 +00:00
|
|
|
// Return the menucell
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)submenuAction:(id)sender
|
|
|
|
{}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Managing NSMenu Windows
|
|
|
|
//
|
|
|
|
- (NSMenu *)attachedMenu
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return self;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isAttached
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return !is_torn_off;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isTornOff
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return is_torn_off;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSPoint)locationForSubmenu:(NSMenu *)aSubmenu
|
|
|
|
{
|
|
|
|
return NSZeroPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sizeToFit
|
|
|
|
{}
|
|
|
|
|
|
|
|
- (NSMenu *)supermenu
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return super_menu;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Displaying the Menu
|
|
|
|
//
|
|
|
|
- (BOOL)autoenablesItems
|
|
|
|
{
|
1996-09-12 19:24:32 +00:00
|
|
|
return autoenables_items;
|
1996-05-30 20:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setAutoenablesItems:(BOOL)flag
|
1996-09-12 19:24:32 +00:00
|
|
|
{
|
|
|
|
autoenables_items = flag;
|
|
|
|
}
|
1996-05-30 20:03:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// NSCoding protocol
|
|
|
|
//
|
|
|
|
- (void)encodeWithCoder:aCoder
|
|
|
|
{
|
|
|
|
[super encodeWithCoder:aCoder];
|
|
|
|
|
|
|
|
[aCoder encodeObject: menu_items];
|
|
|
|
}
|
|
|
|
|
|
|
|
- initWithCoder:aDecoder
|
|
|
|
{
|
|
|
|
[super initWithCoder:aDecoder];
|
|
|
|
|
|
|
|
menu_items = [aDecoder decodeObject];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
1996-09-12 19:24:32 +00:00
|
|
|
|
|
|
|
@implementation NSMenu (GNUstepPrivate)
|
|
|
|
|
|
|
|
- (void)setSupermenu:(NSMenu *)obj
|
|
|
|
{
|
|
|
|
super_menu = obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|