2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsWindowMap.h"
|
2008-02-20 15:47:05 -08:00
|
|
|
#include "nsObjCExceptions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsChildView.h"
|
|
|
|
#include "nsCocoaWindow.h"
|
|
|
|
|
|
|
|
@interface WindowDataMap(Private)
|
|
|
|
|
|
|
|
- (NSString*)keyForWindow:(NSWindow*)inWindow;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface TopLevelWindowData(Private)
|
|
|
|
|
|
|
|
- (void)windowResignedKey:(NSNotification*)inNotification;
|
|
|
|
- (void)windowBecameKey:(NSNotification*)inNotification;
|
|
|
|
- (void)windowWillClose:(NSNotification*)inNotification;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
@implementation WindowDataMap
|
|
|
|
|
|
|
|
+ (WindowDataMap*)sharedWindowDataMap
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static WindowDataMap* sWindowMap = nil;
|
|
|
|
if (!sWindowMap)
|
|
|
|
sWindowMap = [[WindowDataMap alloc] init];
|
|
|
|
|
|
|
|
return sWindowMap;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)init
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if ((self = [super init])) {
|
|
|
|
mWindowMap = [[NSMutableDictionary alloc] initWithCapacity:10];
|
|
|
|
}
|
|
|
|
return self;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
[mWindowMap release];
|
|
|
|
[super dealloc];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-06-17 11:13:12 -07:00
|
|
|
- (void)ensureDataForWindow:(NSWindow*)inWindow
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
if (!inWindow || [self dataForWindow:inWindow])
|
|
|
|
return;
|
|
|
|
|
|
|
|
TopLevelWindowData* windowData = [[TopLevelWindowData alloc] initWithWindow:inWindow];
|
|
|
|
[self setData:windowData forWindow:inWindow]; // takes ownership
|
|
|
|
[windowData release];
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
- (id)dataForWindow:(NSWindow*)inWindow
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return [mWindowMap objectForKey:[self keyForWindow:inWindow]];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setData:(id)inData forWindow:(NSWindow*)inWindow
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
[mWindowMap setObject:inData forKey:[self keyForWindow:inWindow]];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)removeDataForWindow:(NSWindow*)inWindow
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
[mWindowMap removeObjectForKey:[self keyForWindow:inWindow]];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString*)keyForWindow:(NSWindow*)inWindow
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return [NSString stringWithFormat:@"%p", inWindow];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// TopLevelWindowData
|
|
|
|
//
|
|
|
|
// This class holds data about top-level windows. We can't use a window
|
|
|
|
// delegate, because an embedder may already have one.
|
|
|
|
|
|
|
|
@implementation TopLevelWindowData
|
|
|
|
|
|
|
|
- (id)initWithWindow:(NSWindow*)inWindow
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if ((self = [super init])) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(windowBecameKey:)
|
|
|
|
name:NSWindowDidBecomeKeyNotification
|
|
|
|
object:inWindow];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(windowResignedKey:)
|
|
|
|
name:NSWindowDidResignKeyNotification
|
|
|
|
object:inWindow];
|
|
|
|
|
2008-05-02 03:40:49 -07:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(windowBecameMain:)
|
|
|
|
name:NSWindowDidBecomeMainNotification
|
|
|
|
object:inWindow];
|
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(windowResignedMain:)
|
|
|
|
name:NSWindowDidResignMainNotification
|
|
|
|
object:inWindow];
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(windowWillClose:)
|
|
|
|
name:NSWindowWillCloseNotification
|
|
|
|
object:inWindow];
|
|
|
|
}
|
|
|
|
return self;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
[super dealloc];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-09-04 07:48:12 -07:00
|
|
|
// As best I can tell, if the notification's object has a corresponding
|
|
|
|
// top-level widget (an nsCocoaWindow object), it has a delegate (set in
|
2008-05-02 03:40:49 -07:00
|
|
|
// nsCocoaWindow::StandardCreate()) of class WindowDelegate, and otherwise
|
|
|
|
// not (Camino doesn't use top-level widgets (nsCocoaWindow objects) --
|
|
|
|
// only child widgets (nsChildView objects)). (The notification is sent
|
|
|
|
// to windowBecameKey: or windowBecameMain: below.)
|
2007-09-04 07:48:12 -07:00
|
|
|
//
|
2008-05-02 03:40:49 -07:00
|
|
|
// For use with clients that (like Firefox) do use top-level widgets (and
|
|
|
|
// have NSWindow delegates of class WindowDelegate).
|
|
|
|
+ (void)activateInWindow:(NSWindow*)aWindow
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-05-02 03:40:49 -07:00
|
|
|
WindowDelegate* delegate = (WindowDelegate*) [aWindow delegate];
|
|
|
|
if (!delegate || ![delegate isKindOfClass:[WindowDelegate class]])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ([delegate toplevelActiveState])
|
|
|
|
return;
|
|
|
|
[delegate sendToplevelActivateEvents];
|
|
|
|
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-05-02 03:40:49 -07:00
|
|
|
// See comments above activateInWindow:
|
2007-09-04 07:48:12 -07:00
|
|
|
//
|
|
|
|
// If we're using top-level widgets (nsCocoaWindow objects), we send them
|
|
|
|
// NS_DEACTIVATE events (which propagate to child widgets (nsChildView
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
// objects) via nsWebShellWindow::HandleEvent()).
|
2008-05-02 03:40:49 -07:00
|
|
|
//
|
|
|
|
// For use with clients that (like Firefox) do use top-level widgets (and
|
|
|
|
// have NSWindow delegates of class WindowDelegate).
|
|
|
|
+ (void)deactivateInWindow:(NSWindow*)aWindow
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-05-02 03:40:49 -07:00
|
|
|
WindowDelegate* delegate = (WindowDelegate*) [aWindow delegate];
|
|
|
|
if (!delegate || ![delegate isKindOfClass:[WindowDelegate class]])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (![delegate toplevelActiveState])
|
|
|
|
return;
|
|
|
|
[delegate sendToplevelDeactivateEvents];
|
|
|
|
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-05-02 03:40:49 -07:00
|
|
|
// For use with clients that (like Camino) don't use top-level widgets (and
|
|
|
|
// don't have NSWindow delegates of class WindowDelegate).
|
|
|
|
+ (void)activateInWindowViews:(NSWindow*)aWindow
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
id firstResponder = [aWindow firstResponder];
|
|
|
|
if ([firstResponder isKindOfClass:[ChildView class]])
|
|
|
|
[firstResponder viewsWindowDidBecomeKey];
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For use with clients that (like Camino) don't use top-level widgets (and
|
|
|
|
// don't have NSWindow delegates of class WindowDelegate).
|
|
|
|
+ (void)deactivateInWindowViews:(NSWindow*)aWindow
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
id firstResponder = [aWindow firstResponder];
|
|
|
|
if ([firstResponder isKindOfClass:[ChildView class]])
|
|
|
|
[firstResponder viewsWindowDidResignKey];
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We make certain exceptions for top-level windows in non-embedders (see
|
|
|
|
// comment above windowBecameMain below). And we need (elsewhere) to guard
|
Bug 178324, refactor focus by moving all focus handling into one place and simplifying it, add many tests, fixes many other bugs too numerous to mention in this small checkin comment, r=josh,smichaud,ere,dbaron,marco,neil,gavin,smaug,sr=smaug (CLOSED TREE)
2009-06-10 11:00:39 -07:00
|
|
|
// against sending duplicate events. But in general the NS_ACTIVATE event
|
|
|
|
// should be sent when a native window becomes key, and the NS_DEACTIVATE
|
|
|
|
// event should be sent when it resignes key.
|
2008-05-02 03:40:49 -07:00
|
|
|
- (void)windowBecameKey:(NSNotification*)inNotification
|
|
|
|
{
|
|
|
|
NSWindow* window = (NSWindow*)[inNotification object];
|
|
|
|
|
|
|
|
id delegate = [window delegate];
|
|
|
|
if (!delegate || ![delegate isKindOfClass:[WindowDelegate class]]) {
|
|
|
|
[TopLevelWindowData activateInWindowViews:window];
|
|
|
|
} else if ([window isSheet]) {
|
|
|
|
[TopLevelWindowData activateInWindow:window];
|
|
|
|
}
|
2008-08-26 05:41:23 -07:00
|
|
|
|
|
|
|
[[window contentView] setNeedsDisplay:YES];
|
2008-05-02 03:40:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowResignedKey:(NSNotification*)inNotification
|
|
|
|
{
|
|
|
|
NSWindow* window = (NSWindow*)[inNotification object];
|
|
|
|
|
|
|
|
id delegate = [window delegate];
|
|
|
|
if (!delegate || ![delegate isKindOfClass:[WindowDelegate class]]) {
|
|
|
|
[TopLevelWindowData deactivateInWindowViews:window];
|
|
|
|
} else if ([window isSheet]) {
|
|
|
|
[TopLevelWindowData deactivateInWindow:window];
|
|
|
|
}
|
2008-08-26 05:41:23 -07:00
|
|
|
|
|
|
|
[[window contentView] setNeedsDisplay:YES];
|
2008-05-02 03:40:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The appearance of a top-level window depends on its main state (not its key
|
|
|
|
// state). So (for non-embedders) we need to ensure that a top-level window
|
|
|
|
// is main when an NS_ACTIVATE event is sent to Gecko for it.
|
|
|
|
- (void)windowBecameMain:(NSNotification*)inNotification
|
|
|
|
{
|
|
|
|
NSWindow* window = (NSWindow*)[inNotification object];
|
|
|
|
|
|
|
|
id delegate = [window delegate];
|
|
|
|
// Don't send events to a top-level window that has a sheet open above it --
|
|
|
|
// as far as Gecko is concerned, it's inactive, and stays so until the sheet
|
|
|
|
// closes.
|
|
|
|
if (delegate && [delegate isKindOfClass:[WindowDelegate class]] && ![window attachedSheet])
|
|
|
|
[TopLevelWindowData activateInWindow:window];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowResignedMain:(NSNotification*)inNotification
|
|
|
|
{
|
|
|
|
NSWindow* window = (NSWindow*)[inNotification object];
|
|
|
|
|
|
|
|
id delegate = [window delegate];
|
|
|
|
if (delegate && [delegate isKindOfClass:[WindowDelegate class]] && ![window attachedSheet])
|
|
|
|
[TopLevelWindowData deactivateInWindow:window];
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
- (void)windowWillClose:(NSNotification*)inNotification
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// postpone our destruction
|
|
|
|
[[self retain] autorelease];
|
|
|
|
|
|
|
|
// remove ourselves from the window map (which owns us)
|
|
|
|
[[WindowDataMap sharedWindowDataMap] removeDataForWindow:[inNotification object]];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|