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
|
|
|
|
2009-11-04 08:13:00 -08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
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"
|
2010-02-07 19:38:14 -08:00
|
|
|
#include "nsStandaloneNativeMenu.h"
|
2008-02-20 15:47:05 -08:00
|
|
|
|
|
|
|
#include "nsObjCExceptions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsToolkit.h"
|
2012-05-17 00:53:20 -07:00
|
|
|
#include "nsCocoaFeatures.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"
|
2011-10-14 11:11:22 -07:00
|
|
|
#include "nsGkAtoms.h"
|
2008-06-26 23:52:18 -07:00
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsCRT.h"
|
2012-10-26 06:15:22 -07:00
|
|
|
#include "nsBaseWidget.h"
|
2008-06-26 23:52:18 -07:00
|
|
|
|
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"
|
2012-05-23 11:46:04 -07:00
|
|
|
#include "nsBindingManager.h"
|
2008-06-28 00:55:30 -07:00
|
|
|
#include "nsIServiceManager.h"
|
2012-10-19 13:04:07 -07:00
|
|
|
#include "nsXULPopupManager.h"
|
2013-02-26 11:04:13 -08:00
|
|
|
#include "nsContentUtils.h"
|
2013-05-22 09:05:26 -07:00
|
|
|
#include "nsCxPusher.h"
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool gConstructingMenu = false;
|
|
|
|
static bool gMenuMethodsSwizzled = false;
|
2008-03-27 09:30:13 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nsMenuX::sIndexingMenuLevel = 0;
|
2013-02-26 11:04:13 -08:00
|
|
|
using mozilla::AutoPushJSContext;
|
2008-04-24 08:30:20 -07:00
|
|
|
|
2010-01-26 22:14:40 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Objective-C class used for representedObject
|
|
|
|
//
|
|
|
|
|
|
|
|
@implementation MenuItemInfo
|
|
|
|
|
|
|
|
- (id) initWithMenuGroupOwner:(nsMenuGroupOwnerX *)aMenuGroupOwner
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mMenuGroupOwner = nullptr;
|
2010-01-26 22:14:40 -08:00
|
|
|
[self setMenuGroupOwner:aMenuGroupOwner];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
[self setMenuGroupOwner:nullptr];
|
2010-01-26 22:14:40 -08:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nsMenuGroupOwnerX *) menuGroupOwner
|
|
|
|
{
|
|
|
|
return mMenuGroupOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setMenuGroupOwner:(nsMenuGroupOwnerX *)aMenuGroupOwner
|
|
|
|
{
|
|
|
|
// weak reference as the nsMenuGroupOwnerX owns all of its sub-objects
|
|
|
|
mMenuGroupOwner = aMenuGroupOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsMenuX
|
|
|
|
//
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMenuX::nsMenuX()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mVisibleItemsCount(0), mParent(nullptr), mMenuGroupOwner(nullptr),
|
2011-09-30 17:20:33 -07:00
|
|
|
mNativeMenu(nil), mNativeMenuItem(nil), mIsEnabled(true),
|
|
|
|
mDestroyHandlerCalled(false), mNeedsRebuild(true),
|
|
|
|
mConstructed(false), mVisible(true), mXBLAttached(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) {
|
2010-02-23 11:45:53 -08:00
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(_addItem:toTable:),
|
2011-09-30 17:20:33 -07:00
|
|
|
@selector(nsMenuX_NSMenu_addItem:toTable:), true);
|
2010-02-23 11:45:53 -08:00
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(_removeItem:fromTable:),
|
2011-09-30 17:20:33 -07:00
|
|
|
@selector(nsMenuX_NSMenu_removeItem:fromTable:), true);
|
2010-02-23 11:45:53 -08:00
|
|
|
// On SnowLeopard the Shortcut framework (which contains the
|
|
|
|
// SCTGRLIndex class) is loaded on demand, whenever the user first opens
|
|
|
|
// a menu (which normally hasn't happened yet). So we need to load it
|
|
|
|
// here explicitly.
|
2012-11-09 02:22:36 -08:00
|
|
|
dlopen("/System/Library/PrivateFrameworks/Shortcut.framework/Shortcut", RTLD_LAZY);
|
2010-02-23 11:45:53 -08:00
|
|
|
Class SCTGRLIndexClass = ::NSClassFromString(@"SCTGRLIndex");
|
|
|
|
nsToolkit::SwizzleMethods(SCTGRLIndexClass, @selector(indexMenuBarDynamically),
|
|
|
|
@selector(nsMenuX_SCTGRLIndex_indexMenuBarDynamically));
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
gMenuMethodsSwizzled = true;
|
2008-03-27 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2009-07-06 13:59:46 -07:00
|
|
|
// Prevent the icon object from outliving us.
|
|
|
|
if (mIcon)
|
|
|
|
mIcon->Destroy();
|
|
|
|
|
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)
|
2010-01-26 22:14:40 -08:00
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(mContent);
|
2008-06-28 00:55:30 -07:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-26 22:14:40 -08:00
|
|
|
nsresult nsMenuX::Create(nsMenuObjectX* aParent, nsMenuGroupOwnerX* aMenuGroupOwner, 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;
|
2011-10-14 11:11:22 -07:00
|
|
|
mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::label, mLabel);
|
2008-07-27 21:46:33 -07:00
|
|
|
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
|
2010-01-26 22:14:40 -08:00
|
|
|
mMenuGroupOwner = aMenuGroupOwner; // weak ref
|
|
|
|
NS_ASSERTION(mMenuGroupOwner, "No menu owner given, must have one");
|
|
|
|
mMenuGroupOwner->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)
|
2010-07-11 05:58:34 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsMenuObjectTypeX parentType =
|
|
|
|
#endif
|
|
|
|
mParent->MenuObjectType();
|
2010-02-07 19:38:14 -08:00
|
|
|
NS_ASSERTION((parentType == eMenuBarObjectType || parentType == eSubmenuObjectType || parentType == eStandaloneNativeMenuObjectType),
|
|
|
|
"Menu parent not a menu bar, menu, or native menu!");
|
2008-06-26 23:52:18 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent))
|
2011-09-30 17:20:33 -07:00
|
|
|
mVisible = false;
|
2008-06-28 00:55:30 -07:00
|
|
|
if (mContent->GetChildCount() == 0)
|
2011-09-30 17:20:33 -07:00
|
|
|
mVisible = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
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
|
|
|
|
2011-10-14 11:11:22 -07:00
|
|
|
SetEnabled(!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_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
|
2010-01-26 22:14:40 -08:00
|
|
|
[newNativeMenuItem setTag:mMenuGroupOwner->RegisterForCommand(aMenuItem)];
|
|
|
|
MenuItemInfo * info = [[MenuItemInfo alloc] initWithMenuGroupOwner:mMenuGroupOwner];
|
|
|
|
[newNativeMenuItem setRepresentedObject:info];
|
|
|
|
[info release];
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t 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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMenuObjectX* nsMenuX::GetItemAt(uint32_t aPos)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aPos >= (uint32_t)mMenuObjectsArray.Length())
|
2008-06-28 00:55:30 -07:00
|
|
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult nsMenuX::GetVisibleItemCount(uint32_t &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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMenuObjectX* nsMenuX::GetVisibleItemAt(uint32_t aPos)
|
2007-08-27 17:57:49 -07:00
|
|
|
{
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t 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;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t visibleNodeIndex = 0;
|
|
|
|
for (uint32_t 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++)
|
2012-08-22 08:56:38 -07:00
|
|
|
mMenuGroupOwner->UnregisterCommand((uint32_t)[[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
|
|
|
}
|
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
nsEventStatus nsMenuX::MenuOpened()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-13 17:01:57 -07:00
|
|
|
// Open the node.
|
2011-10-14 11:11:22 -07:00
|
|
|
mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::open, NS_LITERAL_STRING("true"), true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
// Fire a handler. If we're told to stop, don't build the menu at all
|
2011-09-28 23:19:26 -07:00
|
|
|
bool keepProcessing = OnOpen();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
if (!mNeedsRebuild || !keepProcessing)
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
if (!mConstructed || mNeedsRebuild) {
|
|
|
|
if (mNeedsRebuild)
|
|
|
|
RemoveAll();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
MenuConstruct();
|
|
|
|
mConstructed = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsMouseEvent event(true, NS_XUL_POPUP_SHOWN, nullptr, nsMouseEvent::eReal);
|
2008-02-20 15:47:05 -08:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2012-07-30 07:20:58 -07:00
|
|
|
dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
|
2009-08-13 17:01:57 -07:00
|
|
|
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
void nsMenuX::MenuClosed()
|
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;
|
|
|
|
|
2011-10-14 11:11:22 -07:00
|
|
|
mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::open, true);
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsMouseEvent event(true, NS_XUL_POPUP_HIDDEN, nullptr, nsMouseEvent::eReal);
|
2008-06-28 00:55:30 -07:00
|
|
|
|
2009-08-13 17:01:57 -07:00
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2012-07-30 07:20:58 -07:00
|
|
|
dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
|
2009-08-13 17:01:57 -07:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
mDestroyHandlerCalled = true;
|
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;
|
2011-09-30 17:20:33 -07:00
|
|
|
gConstructingMenu = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// reset destroy handler flag so that we'll know to fire it next time this menu goes away.
|
2011-09-30 17:20:33 -07:00
|
|
|
mDestroyHandlerCalled = 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) {
|
2011-09-30 17:20:33 -07:00
|
|
|
gConstructingMenu = 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)) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* ownerDoc = menuPopup->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIScriptGlobalObject* sgo;
|
|
|
|
if (ownerDoc && (sgo = ownerDoc->GetScriptGlobalObject())) {
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
JSObject* global = sgo->GetGlobalJSObject();
|
|
|
|
if (scriptContext && global) {
|
2013-02-26 11:04:13 -08:00
|
|
|
AutoPushJSContext cx(scriptContext->GetNativeContext());
|
2007-03-22 10:30:00 -07:00
|
|
|
if (cx) {
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
|
|
|
xpconnect->WrapNative(cx, global,
|
|
|
|
menuPopup, NS_GET_IID(nsISupports),
|
|
|
|
getter_AddRefs(wrapper));
|
2011-09-30 17:20:33 -07:00
|
|
|
mXBLAttached = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate over the kids
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = menuPopup->GetChildCount();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIContent *child = menuPopup->GetChildAt(i);
|
|
|
|
if (child) {
|
|
|
|
// depending on the type, create a menu item, separator, or submenu
|
|
|
|
nsIAtom *tag = child->Tag();
|
2011-10-14 11:11:22 -07:00
|
|
|
if (tag == nsGkAtoms::menuitem || tag == nsGkAtoms::menuseparator)
|
2007-03-22 17:57:45 -07:00
|
|
|
LoadMenuItem(child);
|
2011-10-14 11:11:22 -07:00
|
|
|
else if (tag == nsGkAtoms::menu)
|
2007-03-22 17:57:45 -07:00
|
|
|
LoadSubMenu(child);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} // for each menu item
|
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
gConstructingMenu = false;
|
|
|
|
mNeedsRebuild = 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
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
void nsMenuX::SetRebuild(bool aNeedsRebuild)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!gConstructingMenu)
|
|
|
|
mNeedsRebuild = aNeedsRebuild;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult nsMenuX::SetEnabled(bool 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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
nsresult nsMenuX::GetEnabled(bool* 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;
|
2011-10-14 11:11:22 -07:00
|
|
|
inMenuItemContent->GetAttr(kNameSpaceID_None, nsGkAtoms::label, menuitemName);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// printf("menuitem %s \n", NS_LossyConvertUTF16toASCII(menuitemName).get());
|
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
EMenuItemType itemType = eRegularMenuItemType;
|
2011-10-14 11:11:22 -07:00
|
|
|
if (inMenuItemContent->Tag() == nsGkAtoms::menuseparator) {
|
2008-06-28 00:55:30 -07:00
|
|
|
itemType = eSeparatorMenuItemType;
|
2007-09-14 15:04:59 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
static nsIContent::AttrValuesArray strings[] =
|
2012-07-30 07:20:58 -07:00
|
|
|
{&nsGkAtoms::checkbox, &nsGkAtoms::radio, nullptr};
|
2011-10-14 11:11:22 -07:00
|
|
|
switch (inMenuItemContent->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type,
|
2007-09-14 15:04:59 -07:00
|
|
|
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
|
|
|
|
2010-01-26 22:14:40 -08:00
|
|
|
nsresult rv = menuItem->Create(this, menuitemName, itemType, mMenuGroupOwner, inMenuItemContent);
|
2008-06-28 00:55:30 -07:00
|
|
|
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;
|
|
|
|
|
2010-01-26 22:14:40 -08:00
|
|
|
nsresult rv = menu->Create(this, mMenuGroupOwner, 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.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsMenuX::OnOpen()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsMouseEvent event(true, NS_XUL_POPUP_SHOWING, nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-06-28 00:55:30 -07:00
|
|
|
// If the open is going to succeed we need to walk our menu items, checking to
|
2012-10-19 13:04:07 -07:00
|
|
|
// see if any of them have a command attribute. If so, several attributes
|
2008-06-28 00:55:30 -07:00
|
|
|
// 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)
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2008-07-18 10:38:03 -07:00
|
|
|
|
2012-10-19 13:04:07 -07:00
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
|
|
|
if (pm) {
|
|
|
|
pm->UpdateMenuItems(popupContent);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-07-18 10:38:03 -07:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
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.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsMenuX::OnClose()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mDestroyHandlerCalled)
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsMouseEvent event(true, NS_XUL_POPUP_HIDING, nullptr,
|
2007-03-22 10:30:00 -07:00
|
|
|
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;
|
2012-07-30 07:20:58 -07:00
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
mDestroyHandlerCalled = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
2011-09-30 17:20:33 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-02-07 19:38:14 -08:00
|
|
|
// Check to see if we are a "menupopup" node (if we are a native menu).
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t dummy;
|
2012-05-23 11:46:04 -07:00
|
|
|
nsCOMPtr<nsIAtom> tag = mContent->OwnerDoc()->BindingManager()->ResolveTag(mContent, &dummy);
|
2011-10-14 11:11:22 -07:00
|
|
|
if (tag == nsGkAtoms::menupopup) {
|
2010-02-07 19:38:14 -08:00
|
|
|
*aResult = mContent;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check our child nodes.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t count = mContent->GetChildCount();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
|
|
int32_t dummy;
|
2008-06-28 00:55:30 -07:00
|
|
|
nsIContent *child = mContent->GetChildAt(i);
|
2012-05-23 11:46:04 -07:00
|
|
|
nsCOMPtr<nsIAtom> tag = child->OwnerDoc()->BindingManager()->ResolveTag(child, &dummy);
|
2011-10-14 11:11:22 -07:00
|
|
|
if (tag == nsGkAtoms::menupopup) {
|
2007-03-22 10:30:00 -07:00
|
|
|
*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;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsMenuX::IsXULHelpMenu(nsIContent* aMenuContent)
|
2010-01-08 14:34:51 -08:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool retval = false;
|
2010-01-08 14:34:51 -08:00
|
|
|
if (aMenuContent) {
|
|
|
|
nsAutoString id;
|
2011-10-14 11:11:22 -07:00
|
|
|
aMenuContent->GetAttr(kNameSpaceID_None, nsGkAtoms::id, id);
|
2010-01-08 14:34:51 -08:00
|
|
|
if (id.Equals(NS_LITERAL_STRING("helpMenu")))
|
2011-09-30 17:20:33 -07:00
|
|
|
retval = true;
|
2010-01-08 14:34:51 -08:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
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
|
2011-10-14 11:11:22 -07:00
|
|
|
if (gConstructingMenu || (aAttribute == nsGkAtoms::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
|
|
|
|
2011-10-14 11:11:22 -07:00
|
|
|
if (aAttribute == nsGkAtoms::disabled) {
|
|
|
|
SetEnabled(!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-14 11:11:22 -07:00
|
|
|
else if (aAttribute == nsGkAtoms::label) {
|
|
|
|
mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::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
|
|
|
}
|
2010-02-07 19:38:14 -08:00
|
|
|
else if (parentType == eSubmenuObjectType) {
|
2011-09-30 17:20:33 -07:00
|
|
|
static_cast<nsMenuX*>(mParent)->SetRebuild(true);
|
2007-12-03 22:10:51 -08:00
|
|
|
}
|
2010-02-07 19:38:14 -08:00
|
|
|
else if (parentType == eStandaloneNativeMenuObjectType) {
|
2011-09-30 17:20:33 -07:00
|
|
|
static_cast<nsStandaloneNativeMenu*>(mParent)->GetMenuXObject()->SetRebuild(true);
|
2010-02-07 19:38:14 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-14 11:11:22 -07:00
|
|
|
else if (aAttribute == nsGkAtoms::hidden || aAttribute == nsGkAtoms::collapsed) {
|
2011-09-30 17:20:33 -07:00
|
|
|
SetRebuild(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool 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) {
|
2010-02-07 19:38:14 -08:00
|
|
|
if (parentType == eMenuBarObjectType ||
|
|
|
|
parentType == eSubmenuObjectType ||
|
|
|
|
parentType == eStandaloneNativeMenuObjectType) {
|
2008-06-28 00:55:30 -07:00
|
|
|
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];
|
2011-09-30 17:20:33 -07:00
|
|
|
mVisible = false;
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
else {
|
2010-02-07 19:38:14 -08:00
|
|
|
if (parentType == eMenuBarObjectType ||
|
|
|
|
parentType == eSubmenuObjectType ||
|
|
|
|
parentType == eStandaloneNativeMenuObjectType) {
|
2009-01-23 07:45:03 -08:00
|
|
|
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];
|
2011-09-30 17:20:33 -07:00
|
|
|
mVisible = true;
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-14 11:11:22 -07:00
|
|
|
else if (aAttribute == nsGkAtoms::image) {
|
2007-04-21 18:42:17 -07:00
|
|
|
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,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer)
|
2008-06-28 00:55:30 -07:00
|
|
|
{
|
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
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
SetRebuild(true);
|
2010-01-26 22:14:40 -08:00
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(aChild);
|
2007-08-27 17:57:49 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-02 15:37:18 -07:00
|
|
|
void nsMenuX::ObserveContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
|
|
|
|
nsIContent *aChild)
|
2008-06-28 00:55:30 -07:00
|
|
|
{
|
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
|
|
|
|
2011-09-30 17:20:33 -07:00
|
|
|
SetRebuild(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
|
|
|
}
|
|
|
|
|
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])) {
|
2009-08-13 17:01:57 -07:00
|
|
|
NS_ASSERTION(geckoMenu, "Cannot initialize native menu delegate with NULL gecko menu! Will crash!");
|
2007-03-22 10:30:00 -07:00
|
|
|
mGeckoMenu = geckoMenu;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-08-13 21:04:22 -07:00
|
|
|
- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
|
|
|
|
{
|
|
|
|
if (!menu || !item || !mGeckoMenu)
|
|
|
|
return;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsMenuObjectX* target = mGeckoMenu->GetVisibleItemAt((uint32_t)[menu indexOfItem:item]);
|
2009-08-13 21:04:22 -07:00
|
|
|
if (target && (target->MenuObjectType() == eMenuItemObjectType)) {
|
|
|
|
nsMenuItemX* targetMenuItem = static_cast<nsMenuItemX*>(target);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool handlerCalledPreventDefault; // but we don't actually care
|
2009-08-13 21:04:22 -07:00
|
|
|
targetMenuItem->DispatchDOMEvent(NS_LITERAL_STRING("DOMMenuItemActive"), &handlerCalledPreventDefault);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)menuWillOpen:(NSMenu *)menu
|
|
|
|
{
|
|
|
|
if (!mGeckoMenu)
|
|
|
|
return;
|
|
|
|
|
2009-11-04 08:13:00 -08: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.
|
|
|
|
if (nsMenuX::sIndexingMenuLevel > 0)
|
|
|
|
return;
|
|
|
|
|
2012-10-26 06:15:22 -07:00
|
|
|
nsIRollupListener* rollupListener = nsBaseWidget::GetActiveRollupListener();
|
2012-12-31 09:27:04 -08:00
|
|
|
if (rollupListener) {
|
|
|
|
nsCOMPtr<nsIWidget> rollupWidget = rollupListener->GetRollupWidget();
|
|
|
|
if (rollupWidget) {
|
|
|
|
rollupListener->Rollup(0, nullptr);
|
|
|
|
[menu cancelTracking];
|
|
|
|
return;
|
|
|
|
}
|
2009-08-13 21:04:22 -07:00
|
|
|
}
|
|
|
|
mGeckoMenu->MenuOpened();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)menuDidClose:(NSMenu *)menu
|
|
|
|
{
|
|
|
|
if (!mGeckoMenu)
|
|
|
|
return;
|
|
|
|
|
2009-11-04 08:13:00 -08: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.
|
|
|
|
if (nsMenuX::sIndexingMenuLevel > 0)
|
|
|
|
return;
|
|
|
|
|
2009-08-13 21:04:22 -07:00
|
|
|
mGeckoMenu->MenuClosed();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
@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
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
2012-08-22 08:56:38 -07:00
|
|
|
// menus. sIndexingMenuLevel is a int32_t just in case it might be
|
2008-04-24 08:30:20 -07:00
|
|
|
// 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
|