2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Josh Aas <josh@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2008-02-20 15:47:05 -08:00
|
|
|
#include "nsMenuX.h"
|
2008-06-28 00:55:30 -07:00
|
|
|
#include "nsMenuItemX.h"
|
|
|
|
#include "nsMenuUtilsX.h"
|
|
|
|
#include "nsMenuItemIconX.h"
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
#include "nsObjCExceptions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsToolkit.h"
|
2008-03-27 09:30:13 -07:00
|
|
|
#include "nsCocoaUtils.h"
|
2008-06-28 00:55:30 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prinrval.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "plstr.h"
|
2008-06-26 23:52:18 -07:00
|
|
|
#include "nsWidgetAtoms.h"
|
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDocumentObserver.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIRollupListener.h"
|
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIXBLService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
|
|
|
// externs defined in nsChildView.mm
|
|
|
|
extern nsIRollupListener * gRollupListener;
|
|
|
|
extern nsIWidget * gRollupWidget;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
static PRBool gConstructingMenu = PR_FALSE;
|
2008-03-27 09:30:13 -07:00
|
|
|
static PRBool gMenuMethodsSwizzled = PR_FALSE;
|
|
|
|
|
2008-04-29 09:04:25 -07:00
|
|
|
PRInt32 nsMenuX::sIndexingMenuLevel = 0;
|
2008-04-24 08:30:20 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMenuX::nsMenuX()
|
2008-06-28 00:55:30 -07:00
|
|
|
: mVisibleItemsCount(0), mParent(nsnull), mMenuBar(nsnull),
|
2008-07-27 21:46:33 -07:00
|
|
|
mNativeMenu(nil), mNativeMenuItem(nil), mIsEnabled(PR_TRUE),
|
2007-08-27 17:57:49 -07:00
|
|
|
mDestroyHandlerCalled(PR_FALSE), mNeedsRebuild(PR_TRUE),
|
|
|
|
mConstructed(PR_FALSE), mVisible(PR_TRUE), mXBLAttached(PR_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-12-10 13:42:00 -08:00
|
|
|
if (!gMenuMethodsSwizzled) {
|
|
|
|
if (nsToolkit::OnLeopardOrLater()) {
|
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(_addItem:toTable:),
|
|
|
|
@selector(nsMenuX_NSMenu_addItem:toTable:), PR_TRUE);
|
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(_removeItem:fromTable:),
|
|
|
|
@selector(nsMenuX_NSMenu_removeItem:fromTable:), PR_TRUE);
|
|
|
|
Class SCTGRLIndexClass = ::NSClassFromString(@"SCTGRLIndex");
|
|
|
|
nsToolkit::SwizzleMethods(SCTGRLIndexClass, @selector(indexMenuBarDynamically),
|
|
|
|
@selector(nsMenuX_SCTGRLIndex_indexMenuBarDynamically));
|
|
|
|
} else {
|
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(performKeyEquivalent:),
|
|
|
|
@selector(nsMenuX_NSMenu_performKeyEquivalent:));
|
|
|
|
}
|
2008-03-27 09:30:13 -07:00
|
|
|
gMenuMethodsSwizzled = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mMenuDelegate = [[MenuDelegate alloc] initWithGeckoMenu:this];
|
|
|
|
|
|
|
|
if (!nsMenuBarX::sNativeEventTarget)
|
|
|
|
nsMenuBarX::sNativeEventTarget = [[NativeMenuItemTarget alloc] init];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
MOZ_COUNT_CTOR(nsMenuX);
|
|
|
|
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMenuX::~nsMenuX()
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
RemoveAll();
|
|
|
|
|
2008-07-27 21:46:33 -07:00
|
|
|
[mNativeMenu setDelegate:nil];
|
|
|
|
[mNativeMenu release];
|
2007-03-22 10:30:00 -07:00
|
|
|
[mMenuDelegate release];
|
2009-02-20 13:55:25 -08:00
|
|
|
// autorelease the native menu item so that anything else happening to this
|
|
|
|
// object happens before the native menu item actually dies
|
|
|
|
[mNativeMenuItem autorelease];
|
2008-01-22 20:04:15 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// alert the change notifier we don't care no more
|
2008-06-28 00:55:30 -07:00
|
|
|
if (mContent)
|
|
|
|
mMenuBar->UnregisterForContentChanges(mContent);
|
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsMenuX);
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-07-27 21:46:33 -07:00
|
|
|
nsresult nsMenuX::Create(nsMenuObjectX* aParent, nsMenuBarX* aMenuBar, nsIContent* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
mContent = aNode;
|
2008-07-27 21:46:33 -07:00
|
|
|
mContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, mLabel);
|
|
|
|
mNativeMenu = CreateMenuWithGeckoString(mLabel);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// register this menu to be notified when changes are made to our content object
|
2008-01-22 20:04:15 -08:00
|
|
|
mMenuBar = aMenuBar; // weak ref
|
|
|
|
NS_ASSERTION(mMenuBar, "No menu bar given, must have one");
|
2008-06-28 00:55:30 -07:00
|
|
|
mMenuBar->RegisterForContentChanges(mContent, this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mParent = aParent;
|
|
|
|
// our parent could be either a menu bar (if we're toplevel) or a menu (if we're a submenu)
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuObjectTypeX parentType = mParent->MenuObjectType();
|
|
|
|
NS_ASSERTION((parentType == eMenuBarObjectType || parentType == eSubmenuObjectType),
|
|
|
|
"Menu parent not a menu bar or menu!");
|
2008-06-26 23:52:18 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent))
|
2007-03-22 10:30:00 -07:00
|
|
|
mVisible = PR_FALSE;
|
2008-06-28 00:55:30 -07:00
|
|
|
if (mContent->GetChildCount() == 0)
|
2007-03-22 10:30:00 -07:00
|
|
|
mVisible = PR_FALSE;
|
|
|
|
|
2009-04-29 23:48:14 -07:00
|
|
|
NSString *newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
|
2007-08-16 13:26:04 -07:00
|
|
|
mNativeMenuItem = [[NSMenuItem alloc] initWithTitle:newCocoaLabelString action:nil keyEquivalent:@""];
|
2008-07-27 21:46:33 -07:00
|
|
|
[mNativeMenuItem setSubmenu:mNativeMenu];
|
2007-08-16 13:26:04 -07:00
|
|
|
|
2008-10-22 09:07:45 -07:00
|
|
|
SetEnabled(!mContent->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::disabled,
|
|
|
|
nsWidgetAtoms::_true, eCaseMatters));
|
2007-09-17 14:29:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// We call MenuConstruct here because keyboard commands are dependent upon
|
|
|
|
// native menu items being created. If we only call MenuConstruct when a menu
|
|
|
|
// is actually selected, then we can't access keyboard commands until the
|
|
|
|
// menu gets selected, which is bad.
|
2008-07-27 21:46:33 -07:00
|
|
|
MenuConstruct();
|
2008-06-26 23:52:18 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
mIcon = new nsMenuItemIconX(this, mContent, mNativeMenuItem);
|
2008-06-26 23:52:18 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2008-06-26 23:52:18 -07:00
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::AddMenuItem(nsMenuItemX* aMenuItem)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!aMenuItem)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
mMenuObjectsArray.AppendElement(aMenuItem);
|
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(aMenuItem->Content()))
|
2007-03-22 17:57:45 -07:00
|
|
|
return NS_OK;
|
2007-08-27 17:57:49 -07:00
|
|
|
++mVisibleItemsCount;
|
2008-06-28 00:55:30 -07:00
|
|
|
|
|
|
|
NSMenuItem* newNativeMenuItem = (NSMenuItem*)aMenuItem->NativeData();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// add the menu item to this menu
|
2008-07-27 21:46:33 -07:00
|
|
|
[mNativeMenu addItem:newNativeMenuItem];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// set up target/action
|
|
|
|
[newNativeMenuItem setTarget:nsMenuBarX::sNativeEventTarget];
|
|
|
|
[newNativeMenuItem setAction:@selector(menuItemHit:)];
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// set its command. we get the unique command id from the menubar
|
2008-01-22 20:04:15 -08:00
|
|
|
[newNativeMenuItem setTag:mMenuBar->RegisterForCommand(aMenuItem)];
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::AddMenu(nsMenuX* aMenu)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Add a submenu
|
|
|
|
if (!aMenu)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
mMenuObjectsArray.AppendElement(aMenu);
|
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(aMenu->Content()))
|
2007-03-22 17:57:45 -07:00
|
|
|
return NS_OK;
|
2007-08-27 17:57:49 -07:00
|
|
|
++mVisibleItemsCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// We have to add a menu item and then associate the menu with it
|
2008-07-27 21:46:33 -07:00
|
|
|
NSMenuItem* newNativeMenuItem = aMenu->NativeMenuItem();
|
2007-08-16 13:26:04 -07:00
|
|
|
if (!newNativeMenuItem)
|
|
|
|
return NS_ERROR_FAILURE;
|
2008-07-27 21:46:33 -07:00
|
|
|
[mNativeMenu addItem:newNativeMenuItem];
|
2008-06-28 00:55:30 -07:00
|
|
|
|
|
|
|
[newNativeMenuItem setSubmenu:(NSMenu*)aMenu->NativeData()];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-27 17:57:49 -07:00
|
|
|
// Includes all items, including hidden/collapsed ones
|
2008-06-28 00:55:30 -07:00
|
|
|
PRUint32 nsMenuX::GetItemCount()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-06-28 00:55:30 -07:00
|
|
|
return mMenuObjectsArray.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-27 17:57:49 -07:00
|
|
|
// Includes all items, including hidden/collapsed ones
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuObjectX* nsMenuX::GetItemAt(PRUint32 aPos)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-06-28 00:55:30 -07:00
|
|
|
if (aPos >= (PRUint32)mMenuObjectsArray.Length())
|
|
|
|
return NULL;
|
2007-08-27 17:57:49 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
return mMenuObjectsArray[aPos];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-27 17:57:49 -07:00
|
|
|
// Only includes visible items
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::GetVisibleItemCount(PRUint32 &aCount)
|
2007-08-27 17:57:49 -07:00
|
|
|
{
|
|
|
|
aCount = mVisibleItemsCount;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only includes visible items. Note that this is provides O(N) access
|
|
|
|
// If you need to iterate or search, consider using GetItemAt and doing your own filtering
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuObjectX* nsMenuX::GetVisibleItemAt(PRUint32 aPos)
|
2007-08-27 17:57:49 -07:00
|
|
|
{
|
2008-06-28 00:55:30 -07:00
|
|
|
|
|
|
|
PRUint32 count = mMenuObjectsArray.Length();
|
2007-08-27 17:57:49 -07:00
|
|
|
if (aPos >= mVisibleItemsCount || aPos >= count)
|
2008-06-28 00:55:30 -07:00
|
|
|
return NULL;
|
2007-08-27 17:57:49 -07:00
|
|
|
|
|
|
|
// If there are no invisible items, can provide direct access
|
2008-06-28 00:55:30 -07:00
|
|
|
if (mVisibleItemsCount == count)
|
|
|
|
return mMenuObjectsArray[aPos];
|
2007-08-27 17:57:49 -07:00
|
|
|
|
|
|
|
// Otherwise, traverse the array until we find the the item we're looking for.
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuObjectX* item;
|
2007-08-27 17:57:49 -07:00
|
|
|
PRUint32 visibleNodeIndex = 0;
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
2008-06-28 00:55:30 -07:00
|
|
|
item = mMenuObjectsArray[i];
|
2008-07-27 21:46:33 -07:00
|
|
|
if (!nsMenuUtilsX::NodeIsHiddenOrCollapsed(item->Content())) {
|
2007-08-27 17:57:49 -07:00
|
|
|
if (aPos == visibleNodeIndex) {
|
|
|
|
// we found the visible node we're looking for, return it
|
2008-06-28 00:55:30 -07:00
|
|
|
return item;
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
|
|
|
visibleNodeIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
return NULL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::RemoveAll()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2008-07-27 21:46:33 -07:00
|
|
|
if (mNativeMenu) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// clear command id's
|
2008-07-27 21:46:33 -07:00
|
|
|
int itemCount = [mNativeMenu numberOfItems];
|
2008-01-22 20:04:15 -08:00
|
|
|
for (int i = 0; i < itemCount; i++)
|
2008-07-27 21:46:33 -07:00
|
|
|
mMenuBar->UnregisterCommand((PRUint32)[[mNativeMenu itemAtIndex:i] tag]);
|
2007-03-22 10:30:00 -07:00
|
|
|
// get rid of Cocoa menu items
|
2008-07-27 21:46:33 -07:00
|
|
|
for (int i = [mNativeMenu numberOfItems] - 1; i >= 0; i--)
|
|
|
|
[mNativeMenu removeItemAtIndex:i];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-06-26 23:52:18 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
mMenuObjectsArray.Clear();
|
|
|
|
mVisibleItemsCount = 0;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsEventStatus nsMenuX::MenuOpened(const nsMenuEvent & aMenuEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Determine if this is the correct menu to handle the event
|
|
|
|
MenuRef selectedMenuHandle = (MenuRef)aMenuEvent.mCommand;
|
|
|
|
|
|
|
|
// at this point, the carbon event handler was installed so there
|
|
|
|
// must be a carbon MenuRef to be had
|
2008-07-27 21:46:33 -07:00
|
|
|
if (_NSGetCarbonMenu(mNativeMenu) == selectedMenuHandle) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Open the node.
|
2008-06-28 00:55:30 -07:00
|
|
|
mContent->SetAttr(kNameSpaceID_None, nsWidgetAtoms::open, NS_LITERAL_STRING("true"), PR_TRUE);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
// Fire a handler. If we're told to stop, don't build the menu at all
|
|
|
|
PRBool keepProcessing = OnOpen();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mNeedsRebuild || !keepProcessing)
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
|
|
|
|
if (!mConstructed || mNeedsRebuild) {
|
|
|
|
if (mNeedsRebuild)
|
|
|
|
RemoveAll();
|
|
|
|
|
2008-07-27 21:46:33 -07:00
|
|
|
MenuConstruct();
|
2007-03-22 10:30:00 -07:00
|
|
|
mConstructed = true;
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
OnOpened();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-09-19 15:15:48 -07:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Make sure none of our submenus are the ones that should be handling this
|
2008-06-28 00:55:30 -07:00
|
|
|
PRUint32 count = mMenuObjectsArray.Length();
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
nsMenuObjectX* menuObject = mMenuObjectsArray[i];
|
|
|
|
if (menuObject->MenuObjectType() == eSubmenuObjectType) {
|
|
|
|
nsEventStatus status = static_cast<nsMenuX*>(menuObject)->MenuOpened(aMenuEvent);
|
2007-11-09 09:38:33 -08:00
|
|
|
if (status != nsEventStatus_eIgnore)
|
|
|
|
return status;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-19 15:15:48 -07:00
|
|
|
return nsEventStatus_eIgnore;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(nsEventStatus_eIgnore);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
void nsMenuX::MenuClosed(const nsMenuEvent & aMenuEvent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mConstructed) {
|
2008-06-28 00:55:30 -07:00
|
|
|
// Don't close if a handler tells us to stop.
|
|
|
|
if (!OnClose())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mNeedsRebuild)
|
|
|
|
mConstructed = false;
|
|
|
|
|
|
|
|
mContent->UnsetAttr(kNameSpaceID_None, nsWidgetAtoms::open, PR_TRUE);
|
|
|
|
|
|
|
|
OnClosed();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mConstructed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-27 21:46:33 -07:00
|
|
|
void nsMenuX::MenuConstruct()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
mConstructed = false;
|
|
|
|
gConstructingMenu = PR_TRUE;
|
|
|
|
|
|
|
|
// reset destroy handler flag so that we'll know to fire it next time this menu goes away.
|
|
|
|
mDestroyHandlerCalled = PR_FALSE;
|
2008-07-27 21:46:33 -07:00
|
|
|
|
|
|
|
//printf("nsMenuX::MenuConstruct called for %s = %d \n", NS_LossyConvertUTF16toASCII(mLabel).get(), mNativeMenu);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Retrieve our menupopup.
|
|
|
|
nsCOMPtr<nsIContent> menuPopup;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(menuPopup));
|
|
|
|
if (!menuPopup) {
|
|
|
|
gConstructingMenu = PR_FALSE;
|
2007-11-09 09:38:33 -08:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// bug 365405: Manually wrap the menupopup node to make sure it's bounded
|
|
|
|
if (!mXBLAttached) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIXPConnect> xpconnect =
|
|
|
|
do_GetService(nsIXPConnect::GetCID(), &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsIDocument* ownerDoc = menuPopup->GetOwnerDoc();
|
|
|
|
nsIScriptGlobalObject* sgo;
|
|
|
|
if (ownerDoc && (sgo = ownerDoc->GetScriptGlobalObject())) {
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
JSObject* global = sgo->GetGlobalJSObject();
|
|
|
|
if (scriptContext && global) {
|
|
|
|
JSContext* cx = (JSContext*)scriptContext->GetNativeContext();
|
|
|
|
if (cx) {
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
|
|
|
xpconnect->WrapNative(cx, global,
|
|
|
|
menuPopup, NS_GET_IID(nsISupports),
|
|
|
|
getter_AddRefs(wrapper));
|
|
|
|
mXBLAttached = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the kids
|
|
|
|
PRUint32 count = menuPopup->GetChildCount();
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
nsIContent *child = menuPopup->GetChildAt(i);
|
|
|
|
if (child) {
|
|
|
|
// depending on the type, create a menu item, separator, or submenu
|
|
|
|
nsIAtom *tag = child->Tag();
|
2007-09-14 15:04:59 -07:00
|
|
|
if (tag == nsWidgetAtoms::menuitem || tag == nsWidgetAtoms::menuseparator)
|
2007-03-22 17:57:45 -07:00
|
|
|
LoadMenuItem(child);
|
2007-03-22 10:30:00 -07:00
|
|
|
else if (tag == nsWidgetAtoms::menu)
|
2007-03-22 17:57:45 -07:00
|
|
|
LoadSubMenu(child);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} // for each menu item
|
|
|
|
|
|
|
|
gConstructingMenu = PR_FALSE;
|
|
|
|
mNeedsRebuild = PR_FALSE;
|
2008-06-28 00:55:30 -07:00
|
|
|
// printf("Done building, mMenuObjectsArray.Count() = %d \n", mMenuObjectsArray.Count());
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-11-09 09:38:33 -08:00
|
|
|
void nsMenuX::SetRebuild(PRBool aNeedsRebuild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gConstructingMenu)
|
|
|
|
mNeedsRebuild = aNeedsRebuild;
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::SetEnabled(PRBool aIsEnabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (aIsEnabled != mIsEnabled) {
|
|
|
|
// we always want to rebuild when this changes
|
|
|
|
mIsEnabled = aIsEnabled;
|
2008-10-22 09:07:45 -07:00
|
|
|
[mNativeMenuItem setEnabled:(BOOL)mIsEnabled];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::GetEnabled(PRBool* aIsEnabled)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsEnabled);
|
|
|
|
*aIsEnabled = mIsEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-04-07 21:38:52 -07:00
|
|
|
GeckoNSMenu* nsMenuX::CreateMenuWithGeckoString(nsString& menuTitle)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NSString* title = [NSString stringWithCharacters:(UniChar*)menuTitle.get() length:menuTitle.Length()];
|
2008-04-07 21:38:52 -07:00
|
|
|
GeckoNSMenu* myMenu = [[GeckoNSMenu alloc] initWithTitle:title];
|
2007-03-22 10:30:00 -07:00
|
|
|
[myMenu setDelegate:mMenuDelegate];
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// We don't want this menu to auto-enable menu items because then Cocoa
|
|
|
|
// overrides our decisions and things get incorrectly enabled/disabled.
|
|
|
|
[myMenu setAutoenablesItems:NO];
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// we used to install Carbon event handlers here, but since NSMenu* doesn't
|
|
|
|
// create its underlying MenuRef until just before display, we delay until
|
|
|
|
// that happens. Now we install the event handlers when Cocoa notifies
|
|
|
|
// us that a menu is about to display - see the Cocoa MenuDelegate class.
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return myMenu;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 17:57:45 -07:00
|
|
|
void nsMenuX::LoadMenuItem(nsIContent* inMenuItemContent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!inMenuItemContent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsAutoString menuitemName;
|
|
|
|
inMenuItemContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, menuitemName);
|
|
|
|
|
|
|
|
// printf("menuitem %s \n", NS_LossyConvertUTF16toASCII(menuitemName).get());
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
EMenuItemType itemType = eRegularMenuItemType;
|
2007-09-14 15:04:59 -07:00
|
|
|
if (inMenuItemContent->Tag() == nsWidgetAtoms::menuseparator) {
|
2008-06-28 00:55:30 -07:00
|
|
|
itemType = eSeparatorMenuItemType;
|
2007-09-14 15:04:59 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
static nsIContent::AttrValuesArray strings[] =
|
|
|
|
{&nsWidgetAtoms::checkbox, &nsWidgetAtoms::radio, nsnull};
|
|
|
|
switch (inMenuItemContent->FindAttrValueIn(kNameSpaceID_None, nsWidgetAtoms::type,
|
|
|
|
strings, eCaseMatters)) {
|
2008-06-28 00:55:30 -07:00
|
|
|
case 0: itemType = eCheckboxMenuItemType; break;
|
|
|
|
case 1: itemType = eRadioMenuItemType; break;
|
2007-09-14 15:04:59 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the item.
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuItemX* menuItem = new nsMenuItemX();
|
|
|
|
if (!menuItem)
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult rv = menuItem->Create(this, menuitemName, itemType, mMenuBar, inMenuItemContent);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete menuItem;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddMenuItem(menuItem);
|
2007-04-21 18:42:17 -07:00
|
|
|
|
|
|
|
// This needs to happen after the nsIMenuItem object is inserted into
|
|
|
|
// our item array in AddMenuItem()
|
2008-06-28 00:55:30 -07:00
|
|
|
menuItem->SetupIcon();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 17:57:45 -07:00
|
|
|
void nsMenuX::LoadSubMenu(nsIContent* inMenuContent)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-06-28 00:55:30 -07:00
|
|
|
nsAutoPtr<nsMenuX> menu(new nsMenuX());
|
|
|
|
if (!menu)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
2008-07-27 21:46:33 -07:00
|
|
|
nsresult rv = menu->Create(this, mMenuBar, inMenuContent);
|
2008-06-28 00:55:30 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
AddMenu(menu);
|
2007-04-21 18:42:17 -07:00
|
|
|
|
|
|
|
// This needs to happen after the nsIMenu object is inserted into
|
|
|
|
// our item array in AddMenu()
|
2008-06-28 00:55:30 -07:00
|
|
|
menu->SetupIcon();
|
|
|
|
|
|
|
|
menu.forget();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
// This menu is about to open. Returns TRUE if we should keep processing the event,
|
|
|
|
// FALSE if the handler wants to stop the opening of the menu.
|
|
|
|
PRBool nsMenuX::OnOpen()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_SHOWING, nsnull,
|
|
|
|
nsMouseEvent::eReal);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2008-06-28 00:55:30 -07:00
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
// If the open is going to succeed we need to walk our menu items, checking to
|
|
|
|
// see if any of them have a command attribute. If so, several apptributes
|
|
|
|
// must potentially be updated.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-07-18 10:38:03 -07:00
|
|
|
// Get new popup content first since it might have changed as a result of the
|
|
|
|
// NS_XUL_POPUP_SHOWING event above.
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
if (!popupContent)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(popupContent->GetDocument()));
|
|
|
|
if (!domDoc)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
PRUint32 count = popupContent->GetChildCount();
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
nsIContent *grandChild = popupContent->GetChildAt(i);
|
|
|
|
if (grandChild->Tag() == nsWidgetAtoms::menuitem) {
|
|
|
|
// See if we have a command attribute.
|
|
|
|
nsAutoString command;
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::command, command);
|
|
|
|
if (!command.IsEmpty()) {
|
|
|
|
// We do! Look it up in our document
|
|
|
|
nsCOMPtr<nsIDOMElement> commandElt;
|
|
|
|
domDoc->GetElementById(command, getter_AddRefs(commandElt));
|
|
|
|
nsCOMPtr<nsIContent> commandContent(do_QueryInterface(commandElt));
|
|
|
|
|
|
|
|
if (commandContent) {
|
|
|
|
nsAutoString commandDisabled, menuDisabled;
|
|
|
|
commandContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, commandDisabled);
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, menuDisabled);
|
|
|
|
if (!commandDisabled.Equals(menuDisabled)) {
|
|
|
|
// The menu's disabled state needs to be updated to match the command.
|
|
|
|
if (commandDisabled.IsEmpty())
|
|
|
|
grandChild->UnsetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, PR_TRUE);
|
|
|
|
else
|
|
|
|
grandChild->SetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, commandDisabled, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The menu's value and checked states need to be updated to match the command.
|
|
|
|
// Note that (unlike the disabled state) if the command has *no* value for either, we
|
|
|
|
// assume the menu is supplying its own.
|
|
|
|
nsAutoString commandChecked, menuChecked;
|
|
|
|
commandContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::checked, commandChecked);
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::checked, menuChecked);
|
|
|
|
if (!commandChecked.Equals(menuChecked)) {
|
|
|
|
if (!commandChecked.IsEmpty())
|
|
|
|
grandChild->SetAttr(kNameSpaceID_None, nsWidgetAtoms::checked, commandChecked, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString commandValue, menuValue;
|
|
|
|
commandContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, commandValue);
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, menuValue);
|
|
|
|
if (!commandValue.Equals(menuValue)) {
|
|
|
|
if (!commandValue.IsEmpty())
|
|
|
|
grandChild->SetAttr(kNameSpaceID_None, nsWidgetAtoms::label, commandValue, PR_TRUE);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-07-18 10:38:03 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
PRBool nsMenuX::OnOpened()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_SHOWN, nsnull, nsMouseEvent::eReal);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2008-06-28 00:55:30 -07:00
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
// Returns TRUE if we should keep processing the event, FALSE if the handler
|
|
|
|
// wants to stop the closing of the menu.
|
|
|
|
PRBool nsMenuX::OnClose()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mDestroyHandlerCalled)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_HIDING, nsnull,
|
|
|
|
nsMouseEvent::eReal);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2008-06-28 00:55:30 -07:00
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
|
|
|
|
mDestroyHandlerCalled = PR_TRUE;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
PRBool nsMenuX::OnClosed()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_HIDDEN, nsnull,
|
|
|
|
nsMouseEvent::eReal);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
2008-06-28 00:55:30 -07:00
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
|
|
|
|
mDestroyHandlerCalled = PR_TRUE;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the |menupopup| child in the |popup| representing this menu. It should be one
|
|
|
|
// of a very few children so we won't be iterating over a bazillion menu items to find
|
|
|
|
// it (so the strcmp won't kill us).
|
|
|
|
void nsMenuX::GetMenuPopupContent(nsIContent** aResult)
|
|
|
|
{
|
|
|
|
if (!aResult)
|
|
|
|
return;
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIXBLService> xblService = do_GetService("@mozilla.org/xbl;1", &rv);
|
|
|
|
if (!xblService)
|
|
|
|
return;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
PRUint32 count = mContent->GetChildCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
PRInt32 dummy;
|
2008-06-28 00:55:30 -07:00
|
|
|
nsIContent *child = mContent->GetChildAt(i);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
xblService->ResolveTag(child, &dummy, getter_AddRefs(tag));
|
|
|
|
if (tag == nsWidgetAtoms::menupopup) {
|
|
|
|
*aResult = child;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
NSMenuItem* nsMenuX::NativeMenuItem()
|
2007-08-02 12:08:40 -07:00
|
|
|
{
|
|
|
|
return mNativeMenuItem;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
2008-01-22 20:04:15 -08:00
|
|
|
// nsChangeObserver
|
2007-03-22 10:30:00 -07:00
|
|
|
//
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
void nsMenuX::ObserveAttributeChanged(nsIDocument *aDocument, nsIContent *aContent,
|
|
|
|
nsIAtom *aAttribute)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// ignore the |open| attribute, which is by far the most common
|
|
|
|
if (gConstructingMenu || (aAttribute == nsWidgetAtoms::open))
|
2008-01-22 20:04:15 -08:00
|
|
|
return;
|
2007-08-27 17:57:49 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuObjectTypeX parentType = mParent->MenuObjectType();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aAttribute == nsWidgetAtoms::disabled) {
|
2008-06-28 00:55:30 -07:00
|
|
|
SetEnabled(!mContent->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::disabled,
|
|
|
|
nsWidgetAtoms::_true, eCaseMatters));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (aAttribute == nsWidgetAtoms::label) {
|
2008-06-28 00:55:30 -07:00
|
|
|
mContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, mLabel);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// invalidate my parent. If we're a submenu parent, we have to rebuild
|
|
|
|
// the parent menu in order for the changes to be picked up. If we're
|
|
|
|
// a regular menu, just change the title and redraw the menubar.
|
2008-06-28 00:55:30 -07:00
|
|
|
if (parentType == eMenuBarObjectType) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// reuse the existing menu, to avoid rebuilding the root menu bar.
|
2008-07-27 21:46:33 -07:00
|
|
|
NS_ASSERTION(mNativeMenu, "nsMenuX::AttributeChanged: invalid menu handle.");
|
2009-04-29 23:48:14 -07:00
|
|
|
NSString *newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
|
2008-07-27 21:46:33 -07:00
|
|
|
[mNativeMenu setTitle:newCocoaLabelString];
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-12-03 22:10:51 -08:00
|
|
|
else {
|
2008-06-28 00:55:30 -07:00
|
|
|
static_cast<nsMenuX*>(mParent)->SetRebuild(PR_TRUE);
|
2007-12-03 22:10:51 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else if (aAttribute == nsWidgetAtoms::hidden || aAttribute == nsWidgetAtoms::collapsed) {
|
|
|
|
SetRebuild(PR_TRUE);
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
PRBool contentIsHiddenOrCollapsed = nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
|
2007-08-27 17:57:49 -07:00
|
|
|
|
|
|
|
// don't do anything if the state is correct already
|
|
|
|
if (contentIsHiddenOrCollapsed != mVisible)
|
2008-01-22 20:04:15 -08:00
|
|
|
return;
|
2007-08-27 17:57:49 -07:00
|
|
|
|
|
|
|
if (contentIsHiddenOrCollapsed) {
|
2008-06-28 00:55:30 -07:00
|
|
|
if (parentType == eMenuBarObjectType || parentType == eSubmenuObjectType) {
|
|
|
|
NSMenu* parentMenu = (NSMenu*)mParent->NativeData();
|
2008-03-04 00:11:29 -08:00
|
|
|
// An exception will get thrown if we try to remove an item that isn't
|
|
|
|
// in the menu.
|
|
|
|
if ([parentMenu indexOfItem:mNativeMenuItem] != -1)
|
|
|
|
[parentMenu removeItem:mNativeMenuItem];
|
2007-08-27 17:57:49 -07:00
|
|
|
mVisible = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else {
|
2009-01-23 07:45:03 -08:00
|
|
|
if (parentType == eMenuBarObjectType || parentType == eSubmenuObjectType) {
|
|
|
|
int insertionIndex = nsMenuUtilsX::CalculateNativeInsertionPoint(mParent, this);
|
|
|
|
if (parentType == eMenuBarObjectType) {
|
|
|
|
// Before inserting we need to figure out if we should take the native
|
|
|
|
// application menu into account.
|
|
|
|
nsMenuBarX* mb = static_cast<nsMenuBarX*>(mParent);
|
|
|
|
if (mb->MenuContainsAppMenu())
|
|
|
|
insertionIndex++;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-01-23 07:45:03 -08:00
|
|
|
NSMenu* parentMenu = (NSMenu*)mParent->NativeData();
|
|
|
|
[parentMenu insertItem:mNativeMenuItem atIndex:insertionIndex];
|
|
|
|
[mNativeMenuItem setSubmenu:mNativeMenu];
|
|
|
|
mVisible = PR_TRUE;
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-04-21 18:42:17 -07:00
|
|
|
else if (aAttribute == nsWidgetAtoms::image) {
|
|
|
|
SetupIcon();
|
2008-01-22 20:04:15 -08:00
|
|
|
}
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
void nsMenuX::ObserveContentRemoved(nsIDocument *aDocument, nsIContent *aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gConstructingMenu)
|
2008-01-22 20:04:15 -08:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SetRebuild(PR_TRUE);
|
2008-01-22 20:04:15 -08:00
|
|
|
mMenuBar->UnregisterForContentChanges(aChild);
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
void nsMenuX::ObserveContentInserted(nsIDocument *aDocument, nsIContent *aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
if (gConstructingMenu)
|
2008-01-22 20:04:15 -08:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
SetRebuild(PR_TRUE);
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsresult nsMenuX::SetupIcon()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-04-21 18:42:17 -07:00
|
|
|
// In addition to out-of-memory, menus that are children of the menu bar
|
|
|
|
// will not have mIcon set.
|
2008-06-28 00:55:30 -07:00
|
|
|
if (!mIcon)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2007-04-21 18:42:17 -07:00
|
|
|
return mIcon->SetupIcon();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Carbon event support
|
|
|
|
//
|
|
|
|
|
|
|
|
static pascal OSStatus MyMenuEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData)
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2008-04-24 08:30:20 -07:00
|
|
|
// Don't do anything while the OS is (re)indexing our menus (on Leopard and
|
|
|
|
// higher). This stops the Help menu from being able to search in our
|
|
|
|
// menus, but it also resolves many other problems -- including crashes and
|
|
|
|
// long delays while opening the Help menu. Once we know better which
|
|
|
|
// operations are safe during (re)indexing, we can start allowing some
|
|
|
|
// operations here while it's happening. This change resolves bmo bugs
|
|
|
|
// 426499 and 414699.
|
|
|
|
if (nsMenuX::sIndexingMenuLevel > 0)
|
|
|
|
return noErr;
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
nsMenuX* targetMenu = static_cast<nsMenuX*>(userData);
|
2007-03-22 10:30:00 -07:00
|
|
|
UInt32 kind = ::GetEventKind(event);
|
|
|
|
if (kind == kEventMenuTargetItem) {
|
|
|
|
// get the position of the menu item we want
|
|
|
|
PRUint16 aPos;
|
|
|
|
::GetEventParameter(event, kEventParamMenuItemIndex, typeMenuItemIndex, NULL, sizeof(MenuItemIndex), NULL, &aPos);
|
|
|
|
aPos--; // subtract 1 from aPos because Carbon menu positions start at 1 not 0
|
|
|
|
|
|
|
|
// don't request a menu item that doesn't exist or we crash
|
|
|
|
// this might happen just due to some random quirks in the event system
|
|
|
|
PRUint32 itemCount;
|
2007-08-27 17:57:49 -07:00
|
|
|
targetMenu->GetVisibleItemCount(itemCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aPos >= itemCount)
|
|
|
|
return eventNotHandledErr;
|
2008-06-28 00:55:30 -07:00
|
|
|
|
|
|
|
// Send DOM event if we're over a menu item
|
|
|
|
nsMenuObjectX* target = targetMenu->GetVisibleItemAt((PRUint32)aPos);
|
|
|
|
if (target->MenuObjectType() == eMenuItemObjectType) {
|
|
|
|
nsMenuItemX* targetMenuItem = static_cast<nsMenuItemX*>(target);
|
2007-03-22 10:30:00 -07:00
|
|
|
PRBool handlerCalledPreventDefault; // but we don't actually care
|
2008-06-28 00:55:30 -07:00
|
|
|
targetMenuItem->DispatchDOMEvent(NS_LITERAL_STRING("DOMMenuItemActive"), &handlerCalledPreventDefault);
|
2007-03-22 10:30:00 -07:00
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (kind == kEventMenuOpening || kind == kEventMenuClosed) {
|
2007-09-18 17:19:51 -07:00
|
|
|
if (kind == kEventMenuOpening && gRollupListener && gRollupWidget) {
|
2009-06-12 11:23:16 -07:00
|
|
|
gRollupListener->Rollup(nsnull, nsnull);
|
2007-07-17 17:07:36 -07:00
|
|
|
return userCanceledErr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-06-28 00:55:30 -07:00
|
|
|
MenuRef menuRef;
|
|
|
|
::GetEventParameter(event, kEventParamDirectObject, typeMenuRef, NULL, sizeof(menuRef), NULL, &menuRef);
|
|
|
|
nsMenuEvent menuEvent(PR_TRUE, NS_MENU_SELECTED, nsnull);
|
|
|
|
menuEvent.time = PR_IntervalNow();
|
|
|
|
menuEvent.mCommand = (PRUint32)menuRef;
|
|
|
|
if (kind == kEventMenuOpening)
|
|
|
|
targetMenu->MenuOpened(menuEvent);
|
|
|
|
else
|
|
|
|
targetMenu->MenuClosed(menuEvent);
|
|
|
|
return noErr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return eventNotHandledErr;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(noErr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static OSStatus InstallMyMenuEventHandler(MenuRef menuRef, void* userData, EventHandlerRef* outHandler)
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static EventTypeSpec eventList[] = {
|
|
|
|
{kEventClassMenu, kEventMenuOpening},
|
|
|
|
{kEventClassMenu, kEventMenuClosed},
|
|
|
|
{kEventClassMenu, kEventMenuTargetItem}
|
|
|
|
};
|
|
|
|
|
|
|
|
static EventHandlerUPP gMyMenuEventHandlerUPP = NewEventHandlerUPP(&MyMenuEventHandler);
|
|
|
|
OSStatus status = ::InstallMenuEventHandler(menuRef, gMyMenuEventHandlerUPP,
|
|
|
|
sizeof(eventList) / sizeof(EventTypeSpec), eventList,
|
|
|
|
userData, outHandler);
|
|
|
|
NS_ASSERTION(status == noErr,"Installing carbon menu events failed.");
|
|
|
|
return status;
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(noErr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-27 17:57:49 -07:00
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
// MenuDelegate Objective-C class, used to set up Carbon events
|
2007-08-27 17:57:49 -07:00
|
|
|
//
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
@implementation MenuDelegate
|
|
|
|
|
|
|
|
- (id)initWithGeckoMenu:(nsMenuX*)geckoMenu
|
|
|
|
{
|
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])) {
|
|
|
|
mGeckoMenu = geckoMenu;
|
|
|
|
mHaveInstalledCarbonEvents = FALSE;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-08-27 17:57:49 -07:00
|
|
|
|
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
|
|
|
RemoveEventHandler(mEventHandler);
|
|
|
|
[super dealloc];
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// You can get a MenuRef from an NSMenu*, but not until it has been made visible
|
|
|
|
// or added to the main menu bar. Basically, Cocoa is attempting lazy loading,
|
|
|
|
// and that doesn't work for us. We don't need any carbon events until after the
|
|
|
|
// first time the menu is shown, so when that happens we install the carbon
|
|
|
|
// event handler. This works because at this point we can get a MenuRef without
|
2007-07-17 17:07:36 -07:00
|
|
|
// much trouble.
|
2007-03-22 10:30:00 -07:00
|
|
|
- (void)menuNeedsUpdate:(NSMenu*)aMenu
|
|
|
|
{
|
2008-02-20 15:47:05 -08:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!mHaveInstalledCarbonEvents) {
|
|
|
|
MenuRef myMenuRef = _NSGetCarbonMenu(aMenu);
|
|
|
|
if (myMenuRef) {
|
|
|
|
InstallMyMenuEventHandler(myMenuRef, mGeckoMenu, &mEventHandler);
|
|
|
|
mHaveInstalledCarbonEvents = TRUE;
|
|
|
|
}
|
|
|
|
}
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2008-03-27 09:30:13 -07:00
|
|
|
|
|
|
|
// OS X Leopard (at least as of 10.5.2) has an obscure bug triggered by some
|
|
|
|
// behavior that's present in Mozilla.org browsers but not (as best I can
|
|
|
|
// tell) in Apple products like Safari. (It's not yet clear exactly what this
|
|
|
|
// behavior is.)
|
|
|
|
//
|
|
|
|
// The bug is that sometimes you crash on quit in nsMenuX::RemoveAll(), on a
|
|
|
|
// call to [NSMenu removeItemAtIndex:]. The crash is caused by trying to
|
|
|
|
// access a deleted NSMenuItem object (sometimes (perhaps always?) by trying
|
|
|
|
// to send it a _setChangedFlags: message). Though this object was deleted
|
|
|
|
// some time ago, it remains registered as a potential target for a particular
|
|
|
|
// key equivalent. So when [NSMenu removeItemAtIndex:] removes the current
|
|
|
|
// target for that same key equivalent, the OS tries to "activate" the
|
|
|
|
// previous target.
|
|
|
|
//
|
|
|
|
// The underlying reason appears to be that NSMenu's _addItem:toTable: and
|
|
|
|
// _removeItem:fromTable: methods (which are used to keep a hashtable of
|
|
|
|
// registered key equivalents) don't properly "retain" and "release"
|
|
|
|
// NSMenuItem objects as they are added to and removed from the hashtable.
|
|
|
|
//
|
|
|
|
// Our (hackish) workaround is to shadow the OS's hashtable with another
|
|
|
|
// hastable of our own (gShadowKeyEquivDB), and use it to "retain" and
|
|
|
|
// "release" NSMenuItem objects as needed. This resolves bmo bugs 422287 and
|
|
|
|
// 423669. When (if) Apple fixes this bug, we can remove this workaround.
|
|
|
|
|
|
|
|
static NSMutableDictionary *gShadowKeyEquivDB = nil;
|
|
|
|
|
|
|
|
// Class for values in gShadowKeyEquivDB.
|
|
|
|
|
|
|
|
@interface KeyEquivDBItem : NSObject
|
|
|
|
{
|
|
|
|
NSMenuItem *mItem;
|
2008-05-02 11:35:04 -07:00
|
|
|
NSMutableSet *mTables;
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithItem:(NSMenuItem *)aItem table:(NSMapTable *)aTable;
|
|
|
|
- (BOOL)hasTable:(NSMapTable *)aTable;
|
|
|
|
- (int)addTable:(NSMapTable *)aTable;
|
|
|
|
- (int)removeTable:(NSMapTable *)aTable;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation KeyEquivDBItem
|
|
|
|
|
|
|
|
- (id)initWithItem:(NSMenuItem *)aItem table:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 11:35:04 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
if (!gShadowKeyEquivDB)
|
|
|
|
gShadowKeyEquivDB = [[NSMutableDictionary alloc] init];
|
|
|
|
self = [super init];
|
|
|
|
if (aItem && aTable) {
|
2008-05-02 11:35:04 -07:00
|
|
|
mTables = [[NSMutableSet alloc] init];
|
2008-03-27 09:30:13 -07:00
|
|
|
mItem = [aItem retain];
|
2008-05-02 11:35:04 -07:00
|
|
|
[mTables addObject:[NSValue valueWithPointer:aTable]];
|
2008-03-27 09:30:13 -07:00
|
|
|
} else {
|
|
|
|
mTables = nil;
|
|
|
|
mItem = nil;
|
|
|
|
}
|
|
|
|
return self;
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2008-05-02 11:35:04 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
if (mTables)
|
|
|
|
[mTables release];
|
|
|
|
if (mItem)
|
|
|
|
[mItem release];
|
|
|
|
[super dealloc];
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)hasTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 11:35:04 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
return [mTables member:[NSValue valueWithPointer:aTable]] ? YES : NO;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Does nothing if aTable (its index value) is already present in mTables.
|
|
|
|
- (int)addTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 11:35:04 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
if (aTable)
|
2008-05-02 11:35:04 -07:00
|
|
|
[mTables addObject:[NSValue valueWithPointer:aTable]];
|
2008-03-27 09:30:13 -07:00
|
|
|
return [mTables count];
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)removeTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 11:35:04 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
if (aTable) {
|
|
|
|
NSValue *objectToRemove =
|
|
|
|
[mTables member:[NSValue valueWithPointer:aTable]];
|
|
|
|
if (objectToRemove)
|
|
|
|
[mTables removeObject:objectToRemove];
|
|
|
|
}
|
2008-03-27 09:30:13 -07:00
|
|
|
return [mTables count];
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSMenu (MethodSwizzling)
|
|
|
|
+ (void)nsMenuX_NSMenu_addItem:(NSMenuItem *)aItem toTable:(NSMapTable *)aTable;
|
|
|
|
+ (void)nsMenuX_NSMenu_removeItem:(NSMenuItem *)aItem fromTable:(NSMapTable *)aTable;
|
2008-12-10 13:42:00 -08:00
|
|
|
- (BOOL)nsMenuX_NSMenu_performKeyEquivalent:(NSEvent *)theEvent;
|
2008-03-27 09:30:13 -07:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSMenu (MethodSwizzling)
|
|
|
|
|
|
|
|
+ (void)nsMenuX_NSMenu_addItem:(NSMenuItem *)aItem toTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 11:35:04 -07:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
if (aItem && aTable) {
|
2008-05-02 11:35:04 -07:00
|
|
|
NSValue *key = [NSValue valueWithPointer:aItem];
|
|
|
|
KeyEquivDBItem *shadowItem = [gShadowKeyEquivDB objectForKey:key];
|
2008-03-27 09:30:13 -07:00
|
|
|
if (shadowItem) {
|
|
|
|
[shadowItem addTable:aTable];
|
|
|
|
} else {
|
|
|
|
shadowItem = [[KeyEquivDBItem alloc] initWithItem:aItem table:aTable];
|
2008-05-02 11:35:04 -07:00
|
|
|
[gShadowKeyEquivDB setObject:shadowItem forKey:key];
|
2008-03-27 09:30:13 -07:00
|
|
|
// Release after [NSMutableDictionary setObject:forKey:] retains it (so
|
|
|
|
// that it will get dealloced when removeObjectForKey: is called).
|
|
|
|
[shadowItem release];
|
|
|
|
}
|
|
|
|
}
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
[self nsMenuX_NSMenu_addItem:aItem toTable:aTable];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void)nsMenuX_NSMenu_removeItem:(NSMenuItem *)aItem fromTable:(NSMapTable *)aTable
|
|
|
|
{
|
|
|
|
[self nsMenuX_NSMenu_removeItem:aItem fromTable:aTable];
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
if (aItem && aTable) {
|
2008-05-02 11:35:04 -07:00
|
|
|
NSValue *key = [NSValue valueWithPointer:aItem];
|
|
|
|
KeyEquivDBItem *shadowItem = [gShadowKeyEquivDB objectForKey:key];
|
2008-03-27 09:30:13 -07:00
|
|
|
if (shadowItem && [shadowItem hasTable:aTable]) {
|
|
|
|
if (![shadowItem removeTable:aTable])
|
2008-05-02 11:35:04 -07:00
|
|
|
[gShadowKeyEquivDB removeObjectForKey:key];
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
}
|
2008-05-02 11:35:04 -07:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
2008-12-10 13:42:00 -08:00
|
|
|
- (BOOL)nsMenuX_NSMenu_performKeyEquivalent:(NSEvent *)theEvent
|
|
|
|
{
|
|
|
|
// On OS X 10.4.X (Tiger), Objective-C exceptions can occur during calls to
|
|
|
|
// [NSMenu performKeyEquivalent:] (from [GeckoNSMenu performKeyEquivalent:]
|
|
|
|
// or otherwise) that shouldn't be fatal (see bmo bug 461381). So on Tiger
|
|
|
|
// we hook this system call to eat (and log) all Objective-C exceptions that
|
|
|
|
// occur during its execution. Since we don't call XPCOM code from here,
|
|
|
|
// this will never cause XPCOM objects to be left on the stack without
|
|
|
|
// cleanup.
|
|
|
|
NS_OBJC_BEGIN_TRY_LOGONLY_BLOCK_RETURN;
|
|
|
|
return [self nsMenuX_NSMenu_performKeyEquivalent:theEvent];
|
|
|
|
NS_OBJC_END_TRY_LOGONLY_BLOCK_RETURN(NO);
|
|
|
|
}
|
|
|
|
|
2008-03-27 09:30:13 -07:00
|
|
|
@end
|
2008-04-24 08:30:20 -07:00
|
|
|
|
|
|
|
// This class is needed to keep track of when the OS is (re)indexing all of
|
|
|
|
// our menus. This appears to only happen on Leopard and higher, and can
|
|
|
|
// be triggered by opening the Help menu. Some operations are unsafe while
|
|
|
|
// this is happening -- notably the calls to [[NSImage alloc]
|
|
|
|
// initWithSize:imageRect.size] and [newImage lockFocus] in nsMenuItemIconX::
|
|
|
|
// OnStopFrame(). But we don't yet have a complete list, and Apple doesn't
|
|
|
|
// yet have any documentation on this subject. (Apple also doesn't yet have
|
|
|
|
// any documented way to find the information we seek here.) The "original"
|
|
|
|
// of this class (the one whose indexMenuBarDynamically method we hook) is
|
|
|
|
// defined in the Shortcut framework in /System/Library/PrivateFrameworks.
|
|
|
|
@interface NSObject (SCTGRLIndexMethodSwizzling)
|
|
|
|
- (void)nsMenuX_SCTGRLIndex_indexMenuBarDynamically;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSObject (SCTGRLIndexMethodSwizzling)
|
|
|
|
|
|
|
|
- (void)nsMenuX_SCTGRLIndex_indexMenuBarDynamically
|
|
|
|
{
|
|
|
|
// This method appears to be called (once) whenever the OS (re)indexes our
|
|
|
|
// menus. sIndexingMenuLevel is a PRInt32 just in case it might be
|
|
|
|
// reentered. As it's running, it spawns calls to two undocumented
|
|
|
|
// HIToolbox methods (_SimulateMenuOpening() and _SimulateMenuClosed()),
|
|
|
|
// which "simulate" the opening and closing of our menus without actually
|
|
|
|
// displaying them.
|
|
|
|
++nsMenuX::sIndexingMenuLevel;
|
|
|
|
[self nsMenuX_SCTGRLIndex_indexMenuBarDynamically];
|
|
|
|
--nsMenuX::sIndexingMenuLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|