Menu location save/restore fixes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5685 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
richard 2000-01-07 16:46:35 +00:00
parent 16976fdfb4
commit 662ef633d3
4 changed files with 176 additions and 219 deletions

View file

@ -1,3 +1,10 @@
Fri Jan 7 16:40:00 2000 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/NSWindow.m: Removed spurious code for setting menu window
locations.
* Source/NSMenu.m: Implemented 'correct' save/restore of menu locations
conforming to MacOS-X implementation details.
Fri Jan 7 10:32:00 2000 Richard Frith-Macdonald <richard@brainstorm.co.uk> Fri Jan 7 10:32:00 2000 Richard Frith-Macdonald <richard@brainstorm.co.uk>
* Source/Functions.m: NSApplicationMain() added suggestions from * Source/Functions.m: NSApplicationMain() added suggestions from

View file

@ -55,6 +55,42 @@ static NSZone *menuZone = NULL;
static NSString *NSMenuLocationsKey = @"NSMenuLocations"; static NSString *NSMenuLocationsKey = @"NSMenuLocations";
@interface NSMenu (GNUstepPrivate)
- (NSString*) _locationKey;
@end
@implementation NSMenu (GNUstepPrivate)
- (NSString*) _locationKey
{
if (menu_is_beholdenToPopUpButton == YES)
{
return nil; /* Can't save */
}
if (menu_supermenu == nil)
{
if ([NSApp mainMenu] == self)
{
return @"\033"; /* Root menu. */
}
else
{
return nil; /* Unused menu. */
}
}
else if (menu_supermenu->menu_supermenu == nil)
{
return [NSString stringWithFormat: @"\033%@", [self title]];
}
else
{
return [[menu_supermenu _locationKey] stringByAppendingFormat: @"\033%@",
[self title]];
}
}
@end
@implementation NSMenu @implementation NSMenu
/* /*
@ -119,7 +155,6 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
- (id) initWithTitle: (NSString*)aTitle - (id) initWithTitle: (NSString*)aTitle
{ {
NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter]; NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter];
NSApplication *theApp = [NSApplication sharedApplication];
NSRect winRect = {{0,0},{20,23}}; NSRect winRect = {{0,0},{20,23}};
[super init]; [super init];
@ -167,7 +202,7 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
[theCenter addObserver: self [theCenter addObserver: self
selector: @selector(_showTornOffMenuIfAny:) selector: @selector(_showTornOffMenuIfAny:)
name: NSApplicationWillFinishLaunchingNotification name: NSApplicationWillFinishLaunchingNotification
object: theApp]; object: NSApp];
return self; return self;
} }
@ -571,7 +606,6 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
if ([self autoenablesItems]) if ([self autoenablesItems])
{ {
unsigned i, count; unsigned i, count;
id theApp = [NSApplication sharedApplication];
count = [menu_items count]; count = [menu_items count];
@ -604,7 +638,7 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
} }
else else
{ {
validator = [theApp targetForAction: action]; validator = [NSApp targetForAction: action];
} }
} }
@ -807,17 +841,17 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
menu_changed = NO; menu_changed = NO;
} }
// /*
// Displaying Context Sensitive Help * Displaying Context Sensitive Help
// */
- (void) helpRequested: (NSEvent *)event - (void) helpRequested: (NSEvent *)event
{ {
// TODO: Won't be implemented until we have NSHelp* // TODO: Won't be implemented until we have NSHelp*
} }
// /*
// NSCoding Protocol * NSCoding Protocol
// */
- (void) encodeWithCoder: (NSCoder*)encoder - (void) encodeWithCoder: (NSCoder*)encoder
{ {
[encoder encodeObject: menu_title]; [encoder encodeObject: menu_title];
@ -834,7 +868,6 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
- (id) initWithCoder: (NSCoder*)decoder - (id) initWithCoder: (NSCoder*)decoder
{ {
NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter]; NSNotificationCenter *theCenter = [NSNotificationCenter defaultCenter];
NSApplication *theApp = [NSApplication sharedApplication];
NSRect winRect = {{0,0},{20,23}}; NSRect winRect = {{0,0},{20,23}};
menu_title = [[decoder decodeObject] retain]; menu_title = [[decoder decodeObject] retain];
@ -881,14 +914,14 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
[theCenter addObserver: self [theCenter addObserver: self
selector: @selector(_showTornOffMenuIfAny:) selector: @selector(_showTornOffMenuIfAny:)
name: NSApplicationWillFinishLaunchingNotification name: NSApplicationWillFinishLaunchingNotification
object: theApp]; object: NSApp];
return self; return self;
} }
// /*
// NSCopying Protocol * NSCopying Protocol
// */
- (id) copyWithZone: (NSZone*)zone - (id) copyWithZone: (NSZone*)zone
{ {
return self; return self;
@ -902,30 +935,33 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
- (void) _setTornOff: (BOOL)flag - (void) _setTornOff: (BOOL)flag
{ {
NSMenu *supermenu = [self supermenu]; NSMenu *supermenu;
menu_is_tornoff = flag; menu_is_tornoff = flag;
[[supermenu menuRepresentation] setHighlightedItemIndex: -1]; supermenu = [self supermenu];
supermenu->menu_attachedMenu = nil; if (supermenu != nil)
{
[[supermenu menuRepresentation] setHighlightedItemIndex: -1];
supermenu->menu_attachedMenu = nil;
}
} }
- (void)_showTornOffMenuIfAny: (NSNotification*)notification - (void) _showTornOffMenuIfAny: (NSNotification*)notification
{ {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if ([NSApp mainMenu] != self)
NSDictionary *menuLocations = [defaults objectForKey: NSMenuLocationsKey];
NSString *key;
NSArray *array;
if ([[NSApplication sharedApplication] mainMenu] == self)
key = nil; // Ignore the main menu.
else
key = [self title];
if (key)
{ {
array = [menuLocations objectForKey: key]; NSString *key;
if (array && [array isKindOfClass: [NSArray class]]) NSString *location;
NSUserDefaults *defaults;
NSDictionary *menuLocations;
key = [self _locationKey];
defaults = [NSUserDefaults standardUserDefaults];
menuLocations = [defaults objectForKey: NSMenuLocationsKey];
location = [menuLocations objectForKey: key];
if (location && [location isKindOfClass: [NSString class]])
{ {
[titleView windowBecomeTornOff]; [titleView windowBecomeTornOff];
[self _setTornOff: YES]; [self _setTornOff: YES];
@ -955,30 +991,29 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
- (void) _performMenuClose: (id)sender - (void) _performMenuClose: (id)sender
{ {
NSUserDefaults* defaults; NSString *key;
NSMutableDictionary* menuLocations;
NSString* key;
if (menu_attachedMenu) if (menu_attachedMenu)
[menu_view detachSubmenu]; [menu_view detachSubmenu];
[menu_view setHighlightedItemIndex: -1]; key = [self _locationKey];
if (key != nil)
{
NSUserDefaults *defaults;
NSMutableDictionary *menuLocations;
defaults = [NSUserDefaults standardUserDefaults];
menuLocations = [[defaults objectForKey: NSMenuLocationsKey] mutableCopy];
[menuLocations removeObjectForKey: key];
[defaults setObject: menuLocations forKey: NSMenuLocationsKey];
RELEASE(menuLocations);
[defaults synchronize];
}
[menu_view setHighlightedItemIndex: -1];
[self _setTornOff: NO]; [self _setTornOff: NO];
[self close]; [self close];
[titleView _releaseCloseButton]; [titleView _releaseCloseButton];
defaults = [NSUserDefaults standardUserDefaults];
menuLocations = [[[defaults objectForKey: NSMenuLocationsKey]
mutableCopy] autorelease];
key = [self title]; // Remove window's position$
if (key) // info from defaults db
{
[menuLocations removeObjectForKey: key];
[defaults setObject: menuLocations forKey: NSMenuLocationsKey];
[defaults synchronize];
}
} }
- (void) _rightMouseDisplay - (void) _rightMouseDisplay
@ -1005,30 +1040,24 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
} }
else else
{ {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; NSString *key = [self _locationKey];
NSDictionary* menuLocations = [defaults
objectForKey: NSMenuLocationsKey];
NSString* key;
NSArray* array;
NSPoint origin;
if ([[NSApplication sharedApplication] mainMenu] == self) if (key != nil)
key = @"Main menu";
else
key = [self title];
if (key)
{ {
array = [menuLocations objectForKey: key]; NSUserDefaults *defaults;
if (array && [array isKindOfClass: [NSArray class]]) NSDictionary *menuLocations;
NSString *location;
defaults = [NSUserDefaults standardUserDefaults];
menuLocations = [defaults objectForKey: NSMenuLocationsKey];
location = [menuLocations objectForKey: key];
if (location && [location isKindOfClass: [NSString class]])
{ {
origin.x = [[array objectAtIndex: 0] floatValue]; [aWindow setFrameFromString: location];
origin.y = [[array objectAtIndex: 1] floatValue];
[aWindow setFrameOrigin: origin];
} }
else else
{ {
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)];
@ -1039,7 +1068,7 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
menu_is_visible = YES; menu_is_visible = YES;
[aWindow orderFront: nil]; [aWindow orderFrontRegardless];
menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow); menu_isPartlyOffScreen = IS_OFFSCREEN(aWindow);
@ -1277,17 +1306,18 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
@end @end
@implementation NSMenuWindowTitleView @implementation NSMenuWindowTitleView
- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent - (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
{ {
return YES; return YES;
} }
- (void)setMenu: (NSMenu*)aMenu - (void) setMenu: (NSMenu*)aMenu
{ {
menu = aMenu; menu = aMenu;
} }
- (NSMenu*)menu - (NSMenu*) menu
{ {
return menu; return menu;
} }
@ -1332,19 +1362,15 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
- (void) mouseDown: (NSEvent*)theEvent - (void) mouseDown: (NSEvent*)theEvent
{ {
NSUserDefaults *defaults; NSUserDefaults *defaults;
NSMutableDictionary *menuLocations; NSMutableDictionary *menuLocations;
NSMenu *appMainMenu; NSString *key;
NSPoint origin; NSString *locString;
NSArray* array; NSPoint lastLocation;
NSString* key; NSPoint location;
unsigned eventMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
NSPoint lastLocation; BOOL done = NO;
NSPoint location; NSDate *theDistantFuture = [NSDate distantFuture];
unsigned eventMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
BOOL done = NO;
NSApplication *theApp = [NSApplication sharedApplication];
NSDate *theDistantFuture = [NSDate distantFuture];
lastLocation = [theEvent locationInWindow]; lastLocation = [theEvent locationInWindow];
@ -1356,7 +1382,7 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
while (!done) while (!done)
{ {
theEvent = [theApp nextEventMatchingMask: eventMask theEvent = [NSApp nextEventMatchingMask: eventMask
untilDate: theDistantFuture untilDate: theDistantFuture
inMode: NSEventTrackingRunLoopMode inMode: NSEventTrackingRunLoopMode
dequeue: YES]; dequeue: YES];
@ -1384,25 +1410,18 @@ static NSString *NSMenuLocationsKey = @"NSMenuLocations";
} }
} }
// Save position code goes here. FIXME. /*
appMainMenu = [NSApp mainMenu]; * Same current menu frame in defaults database.
*/
defaults = [NSUserDefaults standardUserDefaults]; defaults = [NSUserDefaults standardUserDefaults];
menuLocations = [[[defaults objectForKey: NSMenuLocationsKey] mutableCopy] autorelease]; menuLocations = [[defaults objectForKey: NSMenuLocationsKey] mutableCopy];
if (menuLocations == nil)
if (!menuLocations)
menuLocations = [NSMutableDictionary dictionaryWithCapacity: 2]; menuLocations = [NSMutableDictionary dictionaryWithCapacity: 2];
origin = [[menu window] frame].origin; locString = [[menu window] stringWithSavedFrame];
array = [NSArray arrayWithObjects: key = [menu _locationKey];
[[NSNumber numberWithInt: origin.x] stringValue], [menuLocations setObject: locString forKey: key];
[[NSNumber numberWithInt: origin.y] stringValue], nil];
if (menu == appMainMenu)
key = @"Main menu";
else
key = [menu title]; // Save menu window pos
[menuLocations setObject: array forKey: key]; // in defaults databa
[defaults setObject: menuLocations forKey: NSMenuLocationsKey]; [defaults setObject: menuLocations forKey: NSMenuLocationsKey];
RELEASE(menuLocations);
[defaults synchronize]; [defaults synchronize];
} }

View file

@ -52,24 +52,26 @@ _screen_numbers(void)
@implementation NSScreen @implementation NSScreen
// /*
// Class variables * Class variables
// */
static NSScreen *mainScreen = nil; static NSScreen *mainScreen = nil;
// /*
// Class methods * Class methods
// */
+ (void)initialize + (void) initialize
{ {
if (self == [NSScreen class]) if (self == [NSScreen class])
[self setVersion:1]; {
[self setVersion:1];
}
} }
// /*
// Creating NSScreen Instances * Creating NSScreen Instances
// */
+ (NSScreen *)mainScreen + (NSScreen*) mainScreen
{ {
NSMutableDictionary *dict; NSMutableDictionary *dict;
@ -82,20 +84,20 @@ static NSScreen *mainScreen = nil;
return mainScreen; return mainScreen;
} }
+ (NSScreen *)deepestScreen + (NSScreen*) deepestScreen
{ {
return [self mainScreen]; return [self mainScreen];
} }
+ (NSArray *)screens + (NSArray*) screens
{ {
return [NSArray arrayWithObject: [self mainScreen]]; return [NSArray arrayWithObject: [self mainScreen]];
} }
// /*
// Instance methods * Instance methods
// */
- initWithDeviceDescription: (NSDictionary *)dict - (id) initWithDeviceDescription: (NSDictionary*)dict
{ {
int screen; int screen;
float x, y, w, h; float x, y, w, h;
@ -145,29 +147,29 @@ static NSScreen *mainScreen = nil;
return self; return self;
} }
- init - (id) init
{ {
return [self initWithDeviceDescription: NULL]; return [self initWithDeviceDescription: NULL];
} }
// /*
// Reading Screen Information * Reading Screen Information
// */
- (NSWindowDepth)depth - (NSWindowDepth) depth
{ {
return depth; return depth;
} }
- (NSRect)frame - (NSRect) frame
{ {
return frame; return frame;
} }
- (NSDictionary *)deviceDescription // Make a copy of device - (NSDictionary*) deviceDescription
{ // dictionary and return it {
NSDictionary *d = [[NSDictionary alloc] initWithDictionary: device_desc]; NSDictionary *d = [[NSDictionary alloc] initWithDictionary: device_desc];
return d; return d;
} }
// Mac OS X methods // Mac OS X methods
@ -180,20 +182,21 @@ NSDictionary *d = [[NSDictionary alloc] initWithDictionary: device_desc];
return retval; return retval;
} }
-(NSRect) visibleFrame - (NSRect) visibleFrame
{ {
NSRect visFrame = frame; NSRect visFrame = frame;
switch ([NSApp interfaceStyle]) switch ([NSApp interfaceStyle])
{ {
case NSMacintoshInterfaceStyle: case NSMacintoshInterfaceStyle:
// What is the size of the Mac menubar? // What is the size of the Mac menubar?
visFrame.size.height -= 25; visFrame.size.height -= 25;
return visFrame; return visFrame;
case NSWindows95InterfaceStyle: case NSWindows95InterfaceStyle:
case NSNextStepInterfaceStyle: case NSNextStepInterfaceStyle:
case NSNoInterfaceStyle: case NSNoInterfaceStyle:
default: default:
return frame; return frame;
} }
} }

View file

@ -63,8 +63,6 @@
#include <AppKit/NSGraphicsContext.h> #include <AppKit/NSGraphicsContext.h>
#include <AppKit/GSWraps.h> #include <AppKit/GSWraps.h>
@class NSMenuWindow;
BOOL GSViewAcceptsDrag(NSView *v, id<NSDraggingInfo> dragInfo); BOOL GSViewAcceptsDrag(NSView *v, id<NSDraggingInfo> dragInfo);
@interface GSWindowView : NSView @interface GSWindowView : NSView
@ -2311,32 +2309,7 @@ resetCursorRectsForView(NSView *theView)
[windowsLock lock]; [windowsLock lock];
defs = [NSUserDefaults standardUserDefaults]; defs = [NSUserDefaults standardUserDefaults];
obj = [self stringWithSavedFrame]; obj = [self stringWithSavedFrame];
if ([self isKindOfClass: [NSMenuWindow class]]) key = [NSString stringWithFormat: @"NSWindow Frame %@", name];
{
id dict;
key = @"NSMenuLocations";
dict = [defs objectForKey: key];
if (dict == nil)
{
dict = [NSMutableDictionary dictionaryWithCapacity: 1];
}
else if ([dict isKindOfClass: [NSDictionary class]] == NO)
{
NSLog(@"NSMenuLocations default is not a dictionary - overwriting");
dict = [NSMutableDictionary dictionaryWithCapacity: 1];
}
else
{
dict = AUTORELEASE([dict mutableCopy]);
}
[dict setObject: obj forKey: name];
obj = dict;
}
else
{
key = [NSString stringWithFormat: @"NSWindow Frame %@", name];
}
[defs setObject: obj forKey: key]; [defs setObject: obj forKey: key];
[windowsLock unlock]; [windowsLock unlock];
} }
@ -2381,33 +2354,8 @@ resetCursorRectsForView(NSView *theView)
* Autosave name cleared - remove from defaults database. * Autosave name cleared - remove from defaults database.
*/ */
defs = [NSUserDefaults standardUserDefaults]; defs = [NSUserDefaults standardUserDefaults];
if ([self isKindOfClass: [NSMenuWindow class]]) key = [NSString stringWithFormat: @"NSWindow Frame %@", nameToRemove];
{ [defs removeObjectForKey: key];
id dict;
key = @"NSMenuLocations";
dict = [defs objectForKey: key];
if (dict == nil)
{
dict = [NSMutableDictionary dictionaryWithCapacity: 1];
}
else if ([dict isKindOfClass: [NSDictionary class]] == NO)
{
NSLog(@"NSMenuLocations is not a dictionary - overwriting");
dict = [NSMutableDictionary dictionaryWithCapacity: 1];
}
else
{
dict = AUTORELEASE([dict mutableCopy]);
}
[dict removeObjectForKey: nameToRemove];
[defs setObject: dict forKey: key];
}
else
{
key = [NSString stringWithFormat: @"NSWindow Frame %@", nameToRemove];
[defs removeObjectForKey: key];
}
RELEASE(nameToRemove); RELEASE(nameToRemove);
} }
[windowsLock unlock]; [windowsLock unlock];
@ -2531,32 +2479,12 @@ resetCursorRectsForView(NSView *theView)
{ {
NSUserDefaults *defs; NSUserDefaults *defs;
id obj; id obj;
NSString *key;
[windowsLock lock]; [windowsLock lock];
defs = [NSUserDefaults standardUserDefaults]; defs = [NSUserDefaults standardUserDefaults];
if ([self isKindOfClass: [NSMenuWindow class]] == YES) key = [NSString stringWithFormat: @"NSWindow Frame %@", name];
{ obj = [defs objectForKey: key];
obj = [defs objectForKey: @"NSMenuLocations"];
if (obj != nil)
{
if ([obj isKindOfClass: [NSDictionary class]] == YES)
{
obj = [obj objectForKey: name];
}
else
{
NSLog(@"NSMenuLocations default is not a dictionary");
obj = nil;
}
}
}
else
{
NSString *key;
key = [NSString stringWithFormat: @"NSWindow Frame %@", name];
obj = [defs objectForKey: key];
}
[windowsLock unlock]; [windowsLock unlock];
if (obj == nil) if (obj == nil)
return NO; return NO;
@ -2564,7 +2492,7 @@ resetCursorRectsForView(NSView *theView)
return YES; return YES;
} }
- (NSString *) stringWithSavedFrame - (NSString*) stringWithSavedFrame
{ {
NSRect fRect; NSRect fRect;
NSRect sRect; NSRect sRect;