2001-11-06 18:35:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04: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/. */
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2009-11-04 19:13:00 +03:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
2008-02-21 02:47:05 +03:00
|
|
|
#include "nsMenuX.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsMenuItemX.h"
|
|
|
|
#include "nsMenuUtilsX.h"
|
|
|
|
#include "nsMenuItemIconX.h"
|
2010-02-08 06:38:14 +03:00
|
|
|
#include "nsStandaloneNativeMenu.h"
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
#include "nsObjCExceptions.h"
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2006-11-30 02:06:11 +03:00
|
|
|
#include "nsToolkit.h"
|
2008-03-27 19:30:13 +03:00
|
|
|
#include "nsCocoaUtils.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "prinrval.h"
|
2001-11-06 18:35:24 +03:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2001-12-17 13:16:37 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2001-11-06 18:35:24 +03:00
|
|
|
#include "plstr.h"
|
2011-10-14 22:11:22 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2008-06-27 10:52:18 +04:00
|
|
|
#include "nsCRT.h"
|
2012-10-26 17:15:22 +04:00
|
|
|
#include "nsBaseWidget.h"
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDocumentObserver.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIRollupListener.h"
|
2012-05-23 22:46:04 +04:00
|
|
|
#include "nsBindingManager.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsIServiceManager.h"
|
2012-10-20 00:04:07 +04:00
|
|
|
#include "nsXULPopupManager.h"
|
2014-06-26 15:16:27 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2017-08-06 20:28:52 +03:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2007-02-22 02:37:34 +03:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2013-09-25 15:21:18 +04:00
|
|
|
#include "mozilla/MouseEvents.h"
|
|
|
|
|
2013-10-02 10:38:27 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool gConstructingMenu = false;
|
|
|
|
static bool gMenuMethodsSwizzled = false;
|
2008-03-27 19:30:13 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nsMenuX::sIndexingMenuLevel = 0;
|
2008-04-24 19:30:20 +04:00
|
|
|
|
2010-01-27 09:14:40 +03:00
|
|
|
|
|
|
|
//
|
|
|
|
// Objective-C class used for representedObject
|
|
|
|
//
|
|
|
|
|
|
|
|
@implementation MenuItemInfo
|
|
|
|
|
|
|
|
- (id) initWithMenuGroupOwner:(nsMenuGroupOwnerX *)aMenuGroupOwner
|
|
|
|
{
|
|
|
|
if ((self = [super init]) != nil) {
|
|
|
|
[self setMenuGroupOwner:aMenuGroupOwner];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
[self setMenuGroupOwner:nullptr];
|
2010-01-27 09:14:40 +03:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (nsMenuGroupOwnerX *) menuGroupOwner
|
|
|
|
{
|
|
|
|
return mMenuGroupOwner;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) setMenuGroupOwner:(nsMenuGroupOwnerX *)aMenuGroupOwner
|
|
|
|
{
|
|
|
|
// weak reference as the nsMenuGroupOwnerX owns all of its sub-objects
|
|
|
|
mMenuGroupOwner = aMenuGroupOwner;
|
2015-08-27 23:54:15 +03:00
|
|
|
if (aMenuGroupOwner) {
|
|
|
|
aMenuGroupOwner->AddMenuItemInfoToSet(self);
|
|
|
|
}
|
2010-01-27 09:14:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// nsMenuX
|
|
|
|
//
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
nsMenuX::nsMenuX()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mVisibleItemsCount(0), mParent(nullptr), mMenuGroupOwner(nullptr),
|
2011-10-01 04:20:33 +04:00
|
|
|
mNativeMenu(nil), mNativeMenuItem(nil), mIsEnabled(true),
|
|
|
|
mDestroyHandlerCalled(false), mNeedsRebuild(true),
|
|
|
|
mConstructed(false), mVisible(true), mXBLAttached(false)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-12-11 00:42:00 +03:00
|
|
|
if (!gMenuMethodsSwizzled) {
|
2010-02-23 22:45:53 +03:00
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(_addItem:toTable:),
|
2011-10-01 04:20:33 +04:00
|
|
|
@selector(nsMenuX_NSMenu_addItem:toTable:), true);
|
2010-02-23 22:45:53 +03:00
|
|
|
nsToolkit::SwizzleMethods([NSMenu class], @selector(_removeItem:fromTable:),
|
2011-10-01 04:20:33 +04:00
|
|
|
@selector(nsMenuX_NSMenu_removeItem:fromTable:), true);
|
2010-02-23 22:45:53 +03: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 14:22:36 +04:00
|
|
|
dlopen("/System/Library/PrivateFrameworks/Shortcut.framework/Shortcut", RTLD_LAZY);
|
2010-02-23 22:45:53 +03:00
|
|
|
Class SCTGRLIndexClass = ::NSClassFromString(@"SCTGRLIndex");
|
|
|
|
nsToolkit::SwizzleMethods(SCTGRLIndexClass, @selector(indexMenuBarDynamically),
|
|
|
|
@selector(nsMenuX_SCTGRLIndex_indexMenuBarDynamically));
|
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
gMenuMethodsSwizzled = true;
|
2008-03-27 19:30:13 +03:00
|
|
|
}
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
mMenuDelegate = [[MenuDelegate alloc] initWithGeckoMenu:this];
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
if (!nsMenuBarX::sNativeEventTarget)
|
|
|
|
nsMenuBarX::sNativeEventTarget = [[NativeMenuItemTarget alloc] init];
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
MOZ_COUNT_CTOR(nsMenuX);
|
|
|
|
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMenuX::~nsMenuX()
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2009-07-07 00:59:46 +04:00
|
|
|
// Prevent the icon object from outliving us.
|
|
|
|
if (mIcon)
|
|
|
|
mIcon->Destroy();
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
RemoveAll();
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
[mNativeMenu setDelegate:nil];
|
|
|
|
[mNativeMenu release];
|
2006-10-31 13:29:30 +03:00
|
|
|
[mMenuDelegate release];
|
2009-02-21 00:55:25 +03: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-23 07:04:15 +03:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// alert the change notifier we don't care no more
|
2008-06-28 11:55:30 +04:00
|
|
|
if (mContent)
|
2010-01-27 09:14:40 +03:00
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(mContent);
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsMenuX);
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
nsresult nsMenuX::Create(nsMenuObjectX* aParent, nsMenuGroupOwnerX* aMenuGroupOwner, nsIContent* aContent)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
mContent = aContent;
|
|
|
|
if (mContent->IsElement()) {
|
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, mLabel);
|
|
|
|
}
|
2008-07-28 08:46:33 +04:00
|
|
|
mNativeMenu = CreateMenuWithGeckoString(mLabel);
|
2001-11-06 18:35:24 +03:00
|
|
|
|
|
|
|
// register this menu to be notified when changes are made to our content object
|
2010-01-27 09:14:40 +03:00
|
|
|
mMenuGroupOwner = aMenuGroupOwner; // weak ref
|
|
|
|
NS_ASSERTION(mMenuGroupOwner, "No menu owner given, must have one");
|
|
|
|
mMenuGroupOwner->RegisterForContentChanges(mContent, this);
|
2001-11-06 18:35:24 +03: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 16:58:34 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsMenuObjectTypeX parentType =
|
|
|
|
#endif
|
|
|
|
mParent->MenuObjectType();
|
2010-02-08 06:38:14 +03:00
|
|
|
NS_ASSERTION((parentType == eMenuBarObjectType || parentType == eSubmenuObjectType || parentType == eStandaloneNativeMenuObjectType),
|
|
|
|
"Menu parent not a menu bar, menu, or native menu!");
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent))
|
2011-10-01 04:20:33 +04:00
|
|
|
mVisible = false;
|
2008-06-28 11:55:30 +04:00
|
|
|
if (mContent->GetChildCount() == 0)
|
2011-10-01 04:20:33 +04:00
|
|
|
mVisible = false;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2009-04-30 10:48:14 +04:00
|
|
|
NSString *newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
|
2007-08-17 00:26:04 +04:00
|
|
|
mNativeMenuItem = [[NSMenuItem alloc] initWithTitle:newCocoaLabelString action:nil keyEquivalent:@""];
|
2008-07-28 08:46:33 +04:00
|
|
|
[mNativeMenuItem setSubmenu:mNativeMenu];
|
2007-08-17 00:26:04 +04:00
|
|
|
|
2017-12-07 21:13:50 +03:00
|
|
|
SetEnabled(!mContent->IsElement() ||
|
|
|
|
!mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters));
|
2007-09-18 01:29:42 +04:00
|
|
|
|
2006-02-17 19:51:59 +03: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-28 08:46:33 +04:00
|
|
|
MenuConstruct();
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
mIcon = new nsMenuItemIconX(this, mContent, mNativeMenuItem);
|
2008-06-27 10:52:18 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2008-06-27 10:52:18 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
nsresult nsMenuX::AddMenuItem(nsMenuItemX* aMenuItem)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
if (!aMenuItem)
|
2006-04-17 22:41:51 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
mMenuObjectsArray.AppendElement(aMenuItem);
|
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(aMenuItem->Content()))
|
2007-03-23 03:57:45 +03:00
|
|
|
return NS_OK;
|
2007-08-28 04:57:49 +04:00
|
|
|
++mVisibleItemsCount;
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
NSMenuItem* newNativeMenuItem = (NSMenuItem*)aMenuItem->NativeData();
|
|
|
|
|
2005-12-03 00:39:26 +03:00
|
|
|
// add the menu item to this menu
|
2008-07-28 08:46:33 +04:00
|
|
|
[mNativeMenu addItem:newNativeMenuItem];
|
2005-12-03 00:39:26 +03:00
|
|
|
|
2005-11-28 21:33:48 +03:00
|
|
|
// set up target/action
|
2006-01-20 08:01:20 +03:00
|
|
|
[newNativeMenuItem setTarget:nsMenuBarX::sNativeEventTarget];
|
2005-11-28 21:33:48 +03:00
|
|
|
[newNativeMenuItem setAction:@selector(menuItemHit:)];
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// set its command. we get the unique command id from the menubar
|
2010-01-27 09:14:40 +03:00
|
|
|
[newNativeMenuItem setTag:mMenuGroupOwner->RegisterForCommand(aMenuItem)];
|
|
|
|
MenuItemInfo * info = [[MenuItemInfo alloc] initWithMenuGroupOwner:mMenuGroupOwner];
|
|
|
|
[newNativeMenuItem setRepresentedObject:info];
|
|
|
|
[info release];
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2016-04-20 01:51:49 +03:00
|
|
|
nsMenuX* nsMenuX::AddMenu(UniquePtr<nsMenuX> aMenu)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2016-04-20 01:51:49 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2016-04-20 01:51:49 +03:00
|
|
|
// aMenu transfers ownership to mMenuObjectsArray and becomes nullptr, so
|
|
|
|
// we need to keep a raw pointer to access it conveniently.
|
|
|
|
nsMenuX* menu = aMenu.get();
|
2018-05-30 22:15:35 +03:00
|
|
|
mMenuObjectsArray.AppendElement(std::move(aMenu));
|
2016-04-20 01:51:49 +03:00
|
|
|
|
|
|
|
if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(menu->Content())) {
|
|
|
|
return menu;
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2007-08-28 04:57:49 +04:00
|
|
|
++mVisibleItemsCount;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2005-12-03 00:39:26 +03:00
|
|
|
// We have to add a menu item and then associate the menu with it
|
2016-04-20 01:51:49 +03:00
|
|
|
NSMenuItem* newNativeMenuItem = menu->NativeMenuItem();
|
|
|
|
if (newNativeMenuItem) {
|
|
|
|
[mNativeMenu addItem:newNativeMenuItem];
|
|
|
|
[newNativeMenuItem setSubmenu:(NSMenu*)menu->NativeData()];
|
|
|
|
}
|
2007-03-12 23:58:02 +03:00
|
|
|
|
2016-04-20 01:51:49 +03:00
|
|
|
return menu;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2016-04-20 01:51:49 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(nullptr);
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2007-08-28 04:57:49 +04:00
|
|
|
// Includes all items, including hidden/collapsed ones
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t nsMenuX::GetItemCount()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-06-28 11:55:30 +04:00
|
|
|
return mMenuObjectsArray.Length();
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2007-08-28 04:57:49 +04:00
|
|
|
// Includes all items, including hidden/collapsed ones
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMenuObjectX* nsMenuX::GetItemAt(uint32_t aPos)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
if (aPos >= (uint32_t)mMenuObjectsArray.Length())
|
2008-06-28 11:55:30 +04:00
|
|
|
return NULL;
|
2007-08-28 04:57:49 +04:00
|
|
|
|
2016-04-20 01:29:16 +03:00
|
|
|
return mMenuObjectsArray[aPos].get();
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2007-08-28 04:57:49 +04:00
|
|
|
// Only includes visible items
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsMenuX::GetVisibleItemCount(uint32_t &aCount)
|
2007-08-28 04:57:49 +04: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 19:56:38 +04:00
|
|
|
nsMenuObjectX* nsMenuX::GetVisibleItemAt(uint32_t aPos)
|
2007-08-28 04:57:49 +04:00
|
|
|
{
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = mMenuObjectsArray.Length();
|
2007-08-28 04:57:49 +04:00
|
|
|
if (aPos >= mVisibleItemsCount || aPos >= count)
|
2008-06-28 11:55:30 +04:00
|
|
|
return NULL;
|
2007-08-28 04:57:49 +04:00
|
|
|
|
|
|
|
// If there are no invisible items, can provide direct access
|
2008-06-28 11:55:30 +04:00
|
|
|
if (mVisibleItemsCount == count)
|
2016-04-20 01:29:16 +03:00
|
|
|
return mMenuObjectsArray[aPos].get();
|
2007-08-28 04:57:49 +04:00
|
|
|
|
|
|
|
// Otherwise, traverse the array until we find the the item we're looking for.
|
2008-06-28 11:55:30 +04:00
|
|
|
nsMenuObjectX* item;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t visibleNodeIndex = 0;
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2016-04-20 01:29:16 +03:00
|
|
|
item = mMenuObjectsArray[i].get();
|
2008-07-28 08:46:33 +04:00
|
|
|
if (!nsMenuUtilsX::NodeIsHiddenOrCollapsed(item->Content())) {
|
2007-08-28 04:57:49 +04:00
|
|
|
if (aPos == visibleNodeIndex) {
|
|
|
|
// we found the visible node we're looking for, return it
|
2008-06-28 11:55:30 +04:00
|
|
|
return item;
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
|
|
|
visibleNodeIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
return NULL;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
nsresult nsMenuX::RemoveAll()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
if (mNativeMenu) {
|
2001-11-06 18:35:24 +03:00
|
|
|
// clear command id's
|
2008-07-28 08:46:33 +04:00
|
|
|
int itemCount = [mNativeMenu numberOfItems];
|
2008-01-23 07:04:15 +03:00
|
|
|
for (int i = 0; i < itemCount; i++)
|
2012-08-22 19:56:38 +04:00
|
|
|
mMenuGroupOwner->UnregisterCommand((uint32_t)[[mNativeMenu itemAtIndex:i] tag]);
|
2005-11-28 21:33:48 +03:00
|
|
|
// get rid of Cocoa menu items
|
2008-07-28 08:46:33 +04:00
|
|
|
for (int i = [mNativeMenu numberOfItems] - 1; i >= 0; i--)
|
|
|
|
[mNativeMenu removeItemAtIndex:i];
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
mMenuObjectsArray.Clear();
|
|
|
|
mVisibleItemsCount = 0;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
nsEventStatus nsMenuX::MenuOpened()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2009-08-14 04:01:57 +04:00
|
|
|
// Open the node.
|
2017-12-05 20:05:51 +03:00
|
|
|
if (mContent->IsElement()) {
|
|
|
|
mContent->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::open,
|
|
|
|
NS_LITERAL_STRING("true"), true);
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
// Fire a handler. If we're told to stop, don't build the menu at all
|
2011-09-29 10:19:26 +04:00
|
|
|
bool keepProcessing = OnOpen();
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
if (!mNeedsRebuild || !keepProcessing)
|
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
if (!mConstructed || mNeedsRebuild) {
|
|
|
|
if (mNeedsRebuild)
|
|
|
|
RemoveAll();
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
MenuConstruct();
|
|
|
|
mConstructed = true;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2015-09-04 13:54:11 +03:00
|
|
|
WidgetMouseEvent event(true, eXULPopupShown, nullptr,
|
2013-10-02 10:38:27 +04:00
|
|
|
WidgetMouseEvent::eReal);
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2017-08-06 20:28:52 +03:00
|
|
|
EventDispatcher::Dispatch(dispatchTo, nullptr, &event, nullptr, &status);
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
void nsMenuX::MenuClosed()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
|
|
|
if (mConstructed) {
|
2008-06-28 11:55:30 +04:00
|
|
|
// Don't close if a handler tells us to stop.
|
|
|
|
if (!OnClose())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mNeedsRebuild)
|
|
|
|
mConstructed = false;
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
if (mContent->IsElement()) {
|
2017-12-06 18:40:50 +03:00
|
|
|
mContent->AsElement()->UnsetAttr(kNameSpaceID_None, nsGkAtoms::open, true);
|
2017-12-05 20:05:51 +03:00
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2015-09-04 13:54:11 +03:00
|
|
|
WidgetMouseEvent event(true, eXULPopupHidden, nullptr,
|
2013-10-02 10:38:27 +04:00
|
|
|
WidgetMouseEvent::eReal);
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2009-08-14 04:01:57 +04:00
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2017-08-06 20:28:52 +03:00
|
|
|
EventDispatcher::Dispatch(dispatchTo, nullptr, &event, nullptr, &status);
|
2009-08-14 04:01:57 +04:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
mDestroyHandlerCalled = true;
|
2001-11-06 18:35:24 +03:00
|
|
|
mConstructed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
void nsMenuX::MenuConstruct()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
|
|
|
mConstructed = false;
|
2011-10-01 04:20:33 +04:00
|
|
|
gConstructingMenu = true;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// reset destroy handler flag so that we'll know to fire it next time this menu goes away.
|
2011-10-01 04:20:33 +04:00
|
|
|
mDestroyHandlerCalled = false;
|
2008-07-28 08:46:33 +04:00
|
|
|
|
|
|
|
//printf("nsMenuX::MenuConstruct called for %s = %d \n", NS_LossyConvertUTF16toASCII(mLabel).get(), mNativeMenu);
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// Retrieve our menupopup.
|
|
|
|
nsCOMPtr<nsIContent> menuPopup;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(menuPopup));
|
2007-03-10 00:08:58 +03:00
|
|
|
if (!menuPopup) {
|
2011-10-01 04:20:33 +04:00
|
|
|
gConstructingMenu = false;
|
2007-11-09 20:38:33 +03:00
|
|
|
return;
|
2007-03-10 00:08:58 +03:00
|
|
|
}
|
2007-02-22 02:37:34 +03:00
|
|
|
|
|
|
|
// bug 365405: Manually wrap the menupopup node to make sure it's bounded
|
|
|
|
if (!mXBLAttached) {
|
2018-09-06 17:01:58 +03:00
|
|
|
nsCOMPtr<nsIXPConnect> xpconnect = nsIXPConnect::XPConnect();
|
|
|
|
nsIDocument* ownerDoc = menuPopup->OwnerDoc();
|
|
|
|
dom::AutoJSAPI jsapi;
|
|
|
|
if (ownerDoc && jsapi.Init(ownerDoc->GetInnerWindow())) {
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::RootedObject ignoredObj(cx);
|
|
|
|
xpconnect->WrapNative(cx, JS::CurrentGlobalOrNull(cx), menuPopup,
|
|
|
|
NS_GET_IID(nsISupports), ignoredObj.address());
|
|
|
|
mXBLAttached = true;
|
2007-02-22 02:37:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// Iterate over the kids
|
2018-01-09 13:44:54 +03:00
|
|
|
for (nsIContent* child = menuPopup->GetFirstChild();
|
|
|
|
child; child = child->GetNextSibling()) {
|
|
|
|
// depending on the type, create a menu item, separator, or submenu
|
|
|
|
if (child->IsAnyOfXULElements(nsGkAtoms::menuitem,
|
|
|
|
nsGkAtoms::menuseparator)) {
|
|
|
|
LoadMenuItem(child);
|
|
|
|
} else if (child->IsXULElement(nsGkAtoms::menu)) {
|
|
|
|
LoadSubMenu(child);
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
} // for each menu item
|
2007-02-22 02:37:34 +03:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
gConstructingMenu = false;
|
|
|
|
mNeedsRebuild = false;
|
2008-06-28 11:55:30 +04:00
|
|
|
// printf("Done building, mMenuObjectsArray.Count() = %d \n", mMenuObjectsArray.Count());
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsMenuX::SetRebuild(bool aNeedsRebuild)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2016-11-30 05:22:13 +03:00
|
|
|
if (!gConstructingMenu) {
|
2001-11-06 18:35:24 +03:00
|
|
|
mNeedsRebuild = aNeedsRebuild;
|
2016-11-30 05:22:13 +03:00
|
|
|
if (mParent->MenuObjectType() == eMenuBarObjectType) {
|
|
|
|
nsMenuBarX* mb = static_cast<nsMenuBarX*>(mParent);
|
|
|
|
mb->SetNeedsRebuild();
|
|
|
|
}
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsMenuX::SetEnabled(bool aIsEnabled)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2006-11-02 13:48:11 +03:00
|
|
|
if (aIsEnabled != mIsEnabled) {
|
|
|
|
// we always want to rebuild when this changes
|
|
|
|
mIsEnabled = aIsEnabled;
|
2008-10-22 20:07:45 +04:00
|
|
|
[mNativeMenuItem setEnabled:(BOOL)mIsEnabled];
|
2006-10-11 01:15:45 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsMenuX::GetEnabled(bool* aIsEnabled)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsEnabled);
|
|
|
|
*aIsEnabled = mIsEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-04-08 08:38:52 +04:00
|
|
|
GeckoNSMenu* nsMenuX::CreateMenuWithGeckoString(nsString& menuTitle)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
NSString* title = [NSString stringWithCharacters:(UniChar*)menuTitle.get() length:menuTitle.Length()];
|
2008-04-08 08:38:52 +04:00
|
|
|
GeckoNSMenu* myMenu = [[GeckoNSMenu alloc] initWithTitle:title];
|
2005-10-17 22:40:42 +04:00
|
|
|
[myMenu setDelegate:mMenuDelegate];
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2005-12-03 00:39:26 +03: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 11:55:30 +04:00
|
|
|
|
2005-10-17 22:40:42 +04: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 11:55:30 +04:00
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
return myMenu;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2007-03-23 03:57:45 +03:00
|
|
|
void nsMenuX::LoadMenuItem(nsIContent* inMenuItemContent)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2005-10-17 22:40:42 +04:00
|
|
|
if (!inMenuItemContent)
|
2001-11-06 18:35:24 +03:00
|
|
|
return;
|
|
|
|
|
2007-03-14 23:12:21 +03:00
|
|
|
nsAutoString menuitemName;
|
2017-12-07 21:13:50 +03:00
|
|
|
if (inMenuItemContent->IsElement()) {
|
|
|
|
inMenuItemContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, menuitemName);
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2007-03-14 23:12:21 +03:00
|
|
|
// printf("menuitem %s \n", NS_LossyConvertUTF16toASCII(menuitemName).get());
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
EMenuItemType itemType = eRegularMenuItemType;
|
2015-03-03 14:09:00 +03:00
|
|
|
if (inMenuItemContent->IsXULElement(nsGkAtoms::menuseparator)) {
|
2008-06-28 11:55:30 +04:00
|
|
|
itemType = eSeparatorMenuItemType;
|
2017-12-07 21:13:50 +03:00
|
|
|
} else if (inMenuItemContent->IsElement()) {
|
|
|
|
static Element::AttrValuesArray strings[] =
|
2018-04-03 06:21:06 +03:00
|
|
|
{nsGkAtoms::checkbox, nsGkAtoms::radio, nullptr};
|
2017-12-07 21:13:50 +03:00
|
|
|
switch (inMenuItemContent->AsElement()->FindAttrValueIn(kNameSpaceID_None,
|
|
|
|
nsGkAtoms::type,
|
|
|
|
strings, eCaseMatters)) {
|
2008-06-28 11:55:30 +04:00
|
|
|
case 0: itemType = eCheckboxMenuItemType; break;
|
|
|
|
case 1: itemType = eRadioMenuItemType; break;
|
2007-09-15 02:04:59 +04:00
|
|
|
}
|
2007-03-14 23:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the item.
|
2008-06-28 11:55:30 +04:00
|
|
|
nsMenuItemX* menuItem = new nsMenuItemX();
|
|
|
|
if (!menuItem)
|
|
|
|
return;
|
2007-03-14 23:12:21 +03:00
|
|
|
|
2010-01-27 09:14:40 +03:00
|
|
|
nsresult rv = menuItem->Create(this, menuitemName, itemType, mMenuGroupOwner, inMenuItemContent);
|
2008-06-28 11:55:30 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete menuItem;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddMenuItem(menuItem);
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
// This needs to happen after the nsIMenuItem object is inserted into
|
|
|
|
// our item array in AddMenuItem()
|
2008-06-28 11:55:30 +04:00
|
|
|
menuItem->SetupIcon();
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2007-03-23 03:57:45 +03:00
|
|
|
void nsMenuX::LoadSubMenu(nsIContent* inMenuContent)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2016-04-20 01:51:49 +03:00
|
|
|
auto menu = MakeUnique<nsMenuX>();
|
2008-06-28 11:55:30 +04:00
|
|
|
if (!menu)
|
2007-03-14 23:12:21 +03:00
|
|
|
return;
|
|
|
|
|
2010-01-27 09:14:40 +03:00
|
|
|
nsresult rv = menu->Create(this, mMenuGroupOwner, inMenuContent);
|
2008-06-28 11:55:30 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return;
|
2007-03-14 23:12:21 +03:00
|
|
|
|
2016-04-20 01:51:49 +03:00
|
|
|
// |menu|'s ownership is transfer to AddMenu but, if it is successfully
|
|
|
|
// added, we can access it via the returned raw pointer.
|
2018-05-30 22:15:35 +03:00
|
|
|
nsMenuX* menu_ptr = AddMenu(std::move(menu));
|
2007-04-22 05:42:17 +04:00
|
|
|
|
|
|
|
// This needs to happen after the nsIMenu object is inserted into
|
|
|
|
// our item array in AddMenu()
|
2016-04-20 01:51:49 +03:00
|
|
|
if (menu_ptr) {
|
|
|
|
menu_ptr->SetupIcon();
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04: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-29 10:19:26 +04:00
|
|
|
bool nsMenuX::OnOpen()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2015-09-04 13:54:11 +03:00
|
|
|
WidgetMouseEvent event(true, eXULPopupShowing, nullptr,
|
2013-10-02 10:38:27 +04:00
|
|
|
WidgetMouseEvent::eReal);
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2006-06-29 02:41:33 +04:00
|
|
|
nsresult rv = NS_OK;
|
2008-06-28 11:55:30 +04:00
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2017-08-06 20:28:52 +03:00
|
|
|
rv = EventDispatcher::Dispatch(dispatchTo, nullptr, &event, nullptr, &status);
|
2006-06-29 02:41:33 +04:00
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
2011-10-01 04:20:33 +04:00
|
|
|
return false;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
// If the open is going to succeed we need to walk our menu items, checking to
|
2012-10-20 00:04:07 +04:00
|
|
|
// see if any of them have a command attribute. If so, several attributes
|
2008-06-28 11:55:30 +04:00
|
|
|
// must potentially be updated.
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2008-07-18 21:38:03 +04:00
|
|
|
// Get new popup content first since it might have changed as a result of the
|
2015-09-04 13:54:11 +03:00
|
|
|
// eXULPopupShowing event above.
|
2008-07-18 21:38:03 +04:00
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
if (!popupContent)
|
2011-10-01 04:20:33 +04:00
|
|
|
return true;
|
2008-07-18 21:38:03 +04:00
|
|
|
|
2012-10-20 00:04:07 +04:00
|
|
|
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
|
|
|
|
if (pm) {
|
|
|
|
pm->UpdateMenuItems(popupContent);
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2008-07-18 21:38:03 +04:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
return true;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
// Returns TRUE if we should keep processing the event, FALSE if the handler
|
|
|
|
// wants to stop the closing of the menu.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMenuX::OnClose()
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2005-10-17 22:40:42 +04:00
|
|
|
if (mDestroyHandlerCalled)
|
2011-10-01 04:20:33 +04:00
|
|
|
return true;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2015-09-04 13:54:11 +03:00
|
|
|
WidgetMouseEvent event(true, eXULPopupHiding, nullptr,
|
2013-10-02 10:38:27 +04:00
|
|
|
WidgetMouseEvent::eReal);
|
2001-11-06 18:35:24 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
2006-06-29 02:41:33 +04:00
|
|
|
nsresult rv = NS_OK;
|
2008-06-28 11:55:30 +04:00
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mContent;
|
2017-08-06 20:28:52 +03:00
|
|
|
rv = EventDispatcher::Dispatch(dispatchTo, nullptr, &event, nullptr, &status);
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
mDestroyHandlerCalled = true;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2006-06-29 02:41:33 +04:00
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
2011-10-01 04:20:33 +04:00
|
|
|
return false;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
return true;
|
2001-11-06 18:35:24 +03: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).
|
2006-07-10 21:50:30 +04:00
|
|
|
void nsMenuX::GetMenuPopupContent(nsIContent** aResult)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2005-10-17 22:40:42 +04:00
|
|
|
if (!aResult)
|
2001-11-06 18:35:24 +03:00
|
|
|
return;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2018-01-09 13:44:54 +03:00
|
|
|
|
2010-02-08 06:38:14 +03:00
|
|
|
// Check to see if we are a "menupopup" node (if we are a native menu).
|
2018-04-02 14:29:58 +03:00
|
|
|
if (mContent->IsXULElement(nsGkAtoms::menupopup)) {
|
|
|
|
NS_ADDREF(*aResult = mContent);
|
|
|
|
return;
|
2010-02-08 06:38:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise check our child nodes.
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2018-01-09 13:44:54 +03:00
|
|
|
for (nsIContent* child = mContent->GetFirstChild();
|
|
|
|
child; child = child->GetNextSibling()) {
|
2018-04-02 14:29:58 +03:00
|
|
|
if (child->IsXULElement(nsGkAtoms::menupopup)) {
|
|
|
|
NS_ADDREF(*aResult = child);
|
2001-11-06 18:35:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
NSMenuItem* nsMenuX::NativeMenuItem()
|
2007-08-02 23:08:40 +04:00
|
|
|
{
|
|
|
|
return mNativeMenuItem;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMenuX::IsXULHelpMenu(nsIContent* aMenuContent)
|
2010-01-09 01:34:51 +03:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool retval = false;
|
2017-12-07 21:13:50 +03:00
|
|
|
if (aMenuContent && aMenuContent->IsElement()) {
|
2010-01-09 01:34:51 +03:00
|
|
|
nsAutoString id;
|
2017-12-07 21:13:50 +03:00
|
|
|
aMenuContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::id, id);
|
2010-01-09 01:34:51 +03:00
|
|
|
if (id.Equals(NS_LITERAL_STRING("helpMenu")))
|
2011-10-01 04:20:33 +04:00
|
|
|
retval = true;
|
2010-01-09 01:34:51 +03:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
//
|
2008-01-23 07:04:15 +03:00
|
|
|
// nsChangeObserver
|
2001-11-06 18:35:24 +03:00
|
|
|
//
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
void nsMenuX::ObserveAttributeChanged(nsIDocument *aDocument, nsIContent *aContent,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom *aAttribute)
|
2001-11-06 18:35:24 +03:00
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// ignore the |open| attribute, which is by far the most common
|
2011-10-14 22:11:22 +04:00
|
|
|
if (gConstructingMenu || (aAttribute == nsGkAtoms::open))
|
2008-01-23 07:04:15 +03:00
|
|
|
return;
|
2007-08-28 04:57:49 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
nsMenuObjectTypeX parentType = mParent->MenuObjectType();
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2011-10-14 22:11:22 +04:00
|
|
|
if (aAttribute == nsGkAtoms::disabled) {
|
2017-12-07 21:13:50 +03:00
|
|
|
SetEnabled(!mContent->AsElement()->AttrValueIs(
|
|
|
|
kNameSpaceID_None, nsGkAtoms::disabled, nsGkAtoms::_true, eCaseMatters));
|
2005-11-28 21:33:48 +03:00
|
|
|
}
|
2011-10-14 22:11:22 +04:00
|
|
|
else if (aAttribute == nsGkAtoms::label) {
|
2017-12-05 20:05:51 +03:00
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, mLabel);
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2005-10-17 22:40:42 +04: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 11:55:30 +04:00
|
|
|
if (parentType == eMenuBarObjectType) {
|
2005-10-17 22:40:42 +04:00
|
|
|
// reuse the existing menu, to avoid rebuilding the root menu bar.
|
2008-07-28 08:46:33 +04:00
|
|
|
NS_ASSERTION(mNativeMenu, "nsMenuX::AttributeChanged: invalid menu handle.");
|
2009-04-30 10:48:14 +04:00
|
|
|
NSString *newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
|
2008-07-28 08:46:33 +04:00
|
|
|
[mNativeMenu setTitle:newCocoaLabelString];
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
2010-02-08 06:38:14 +03:00
|
|
|
else if (parentType == eSubmenuObjectType) {
|
2011-10-01 04:20:33 +04:00
|
|
|
static_cast<nsMenuX*>(mParent)->SetRebuild(true);
|
2016-07-22 11:56:13 +03:00
|
|
|
}
|
2010-02-08 06:38:14 +03:00
|
|
|
else if (parentType == eStandaloneNativeMenuObjectType) {
|
2011-10-01 04:20:33 +04:00
|
|
|
static_cast<nsStandaloneNativeMenu*>(mParent)->GetMenuXObject()->SetRebuild(true);
|
2010-02-08 06:38:14 +03:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2011-10-14 22:11:22 +04:00
|
|
|
else if (aAttribute == nsGkAtoms::hidden || aAttribute == nsGkAtoms::collapsed) {
|
2011-10-01 04:20:33 +04:00
|
|
|
SetRebuild(true);
|
2006-06-09 10:02:30 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool contentIsHiddenOrCollapsed = nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
|
2007-08-28 04:57:49 +04:00
|
|
|
|
|
|
|
// don't do anything if the state is correct already
|
|
|
|
if (contentIsHiddenOrCollapsed != mVisible)
|
2008-01-23 07:04:15 +03:00
|
|
|
return;
|
2007-08-28 04:57:49 +04:00
|
|
|
|
|
|
|
if (contentIsHiddenOrCollapsed) {
|
2010-02-08 06:38:14 +03:00
|
|
|
if (parentType == eMenuBarObjectType ||
|
|
|
|
parentType == eSubmenuObjectType ||
|
|
|
|
parentType == eStandaloneNativeMenuObjectType) {
|
2008-06-28 11:55:30 +04:00
|
|
|
NSMenu* parentMenu = (NSMenu*)mParent->NativeData();
|
2008-03-04 11:11:29 +03: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-10-01 04:20:33 +04:00
|
|
|
mVisible = false;
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
else {
|
2010-02-08 06:38:14 +03:00
|
|
|
if (parentType == eMenuBarObjectType ||
|
|
|
|
parentType == eSubmenuObjectType ||
|
|
|
|
parentType == eStandaloneNativeMenuObjectType) {
|
2009-01-23 18:45:03 +03: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++;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
2009-01-23 18:45:03 +03:00
|
|
|
NSMenu* parentMenu = (NSMenu*)mParent->NativeData();
|
|
|
|
[parentMenu insertItem:mNativeMenuItem atIndex:insertionIndex];
|
|
|
|
[mNativeMenuItem setSubmenu:mNativeMenu];
|
2011-10-01 04:20:33 +04:00
|
|
|
mVisible = true;
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2011-10-14 22:11:22 +04:00
|
|
|
else if (aAttribute == nsGkAtoms::image) {
|
2007-04-22 05:42:17 +04:00
|
|
|
SetupIcon();
|
2008-01-23 07:04:15 +03:00
|
|
|
}
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2017-07-27 16:49:52 +03:00
|
|
|
void nsMenuX::ObserveContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
nsIContent* aPreviousSibling)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
2001-11-06 18:35:24 +03:00
|
|
|
if (gConstructingMenu)
|
2008-01-23 07:04:15 +03:00
|
|
|
return;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
SetRebuild(true);
|
2010-01-27 09:14:40 +03:00
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(aChild);
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2011-06-03 02:37:18 +04:00
|
|
|
void nsMenuX::ObserveContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
|
|
|
|
nsIContent *aChild)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
2005-10-17 22:40:42 +04:00
|
|
|
if (gConstructingMenu)
|
2008-01-23 07:04:15 +03:00
|
|
|
return;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2011-10-01 04:20:33 +04:00
|
|
|
SetRebuild(true);
|
2007-08-28 04:57:49 +04:00
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
nsresult nsMenuX::SetupIcon()
|
2006-08-01 05:25:43 +04:00
|
|
|
{
|
2007-04-22 05:42:17 +04:00
|
|
|
// In addition to out-of-memory, menus that are children of the menu bar
|
|
|
|
// will not have mIcon set.
|
2008-06-28 11:55:30 +04:00
|
|
|
if (!mIcon)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2007-04-22 05:42:17 +04:00
|
|
|
return mIcon->SetupIcon();
|
2006-08-01 05:25:43 +04:00
|
|
|
}
|
|
|
|
|
2007-08-28 04:57:49 +04:00
|
|
|
//
|
2006-01-20 08:01:20 +03:00
|
|
|
// MenuDelegate Objective-C class, used to set up Carbon events
|
2007-08-28 04:57:49 +04:00
|
|
|
//
|
2005-10-17 22:40:42 +04:00
|
|
|
|
|
|
|
@implementation MenuDelegate
|
|
|
|
|
|
|
|
- (id)initWithGeckoMenu:(nsMenuX*)geckoMenu
|
|
|
|
{
|
2008-02-21 02:47:05 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
if ((self = [super init])) {
|
2009-08-14 04:01:57 +04:00
|
|
|
NS_ASSERTION(geckoMenu, "Cannot initialize native menu delegate with NULL gecko menu! Will crash!");
|
2005-10-17 22:40:42 +04:00
|
|
|
mGeckoMenu = geckoMenu;
|
|
|
|
}
|
|
|
|
return self;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2009-08-14 08:04:22 +04:00
|
|
|
- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
|
|
|
|
{
|
|
|
|
if (!menu || !item || !mGeckoMenu)
|
|
|
|
return;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMenuObjectX* target = mGeckoMenu->GetVisibleItemAt((uint32_t)[menu indexOfItem:item]);
|
2009-08-14 08:04:22 +04:00
|
|
|
if (target && (target->MenuObjectType() == eMenuItemObjectType)) {
|
|
|
|
nsMenuItemX* targetMenuItem = static_cast<nsMenuItemX*>(target);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool handlerCalledPreventDefault; // but we don't actually care
|
2009-08-14 08:04:22 +04:00
|
|
|
targetMenuItem->DispatchDOMEvent(NS_LITERAL_STRING("DOMMenuItemActive"), &handlerCalledPreventDefault);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)menuWillOpen:(NSMenu *)menu
|
|
|
|
{
|
|
|
|
if (!mGeckoMenu)
|
|
|
|
return;
|
|
|
|
|
2009-11-04 19:13:00 +03: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 17:15:22 +04:00
|
|
|
nsIRollupListener* rollupListener = nsBaseWidget::GetActiveRollupListener();
|
2012-12-31 21:27:04 +04:00
|
|
|
if (rollupListener) {
|
|
|
|
nsCOMPtr<nsIWidget> rollupWidget = rollupListener->GetRollupWidget();
|
|
|
|
if (rollupWidget) {
|
2015-01-08 04:52:20 +03:00
|
|
|
rollupListener->Rollup(0, true, nullptr, nullptr);
|
2012-12-31 21:27:04 +04:00
|
|
|
[menu cancelTracking];
|
|
|
|
return;
|
|
|
|
}
|
2009-08-14 08:04:22 +04:00
|
|
|
}
|
|
|
|
mGeckoMenu->MenuOpened();
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)menuDidClose:(NSMenu *)menu
|
|
|
|
{
|
|
|
|
if (!mGeckoMenu)
|
|
|
|
return;
|
|
|
|
|
2009-11-04 19:13:00 +03: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-14 08:04:22 +04:00
|
|
|
mGeckoMenu->MenuClosed();
|
|
|
|
}
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
@end
|
2008-03-27 19:30:13 +03: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 22:35:04 +04:00
|
|
|
NSMutableSet *mTables;
|
2008-03-27 19:30:13 +03: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 22:35:04 +04:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2008-03-27 19:30:13 +03:00
|
|
|
if (!gShadowKeyEquivDB)
|
|
|
|
gShadowKeyEquivDB = [[NSMutableDictionary alloc] init];
|
|
|
|
self = [super init];
|
|
|
|
if (aItem && aTable) {
|
2008-05-02 22:35:04 +04:00
|
|
|
mTables = [[NSMutableSet alloc] init];
|
2008-03-27 19:30:13 +03:00
|
|
|
mItem = [aItem retain];
|
2008-05-02 22:35:04 +04:00
|
|
|
[mTables addObject:[NSValue valueWithPointer:aTable]];
|
2008-03-27 19:30:13 +03:00
|
|
|
} else {
|
|
|
|
mTables = nil;
|
|
|
|
mItem = nil;
|
|
|
|
}
|
|
|
|
return self;
|
2008-05-02 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2008-03-27 19:30:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2008-05-02 22:35:04 +04:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 19:30:13 +03:00
|
|
|
if (mTables)
|
|
|
|
[mTables release];
|
|
|
|
if (mItem)
|
|
|
|
[mItem release];
|
|
|
|
[super dealloc];
|
2008-05-02 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-03-27 19:30:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)hasTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 22:35:04 +04: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 19:30:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Does nothing if aTable (its index value) is already present in mTables.
|
|
|
|
- (int)addTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 22:35:04 +04:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
2008-03-27 19:30:13 +03:00
|
|
|
if (aTable)
|
2008-05-02 22:35:04 +04:00
|
|
|
[mTables addObject:[NSValue valueWithPointer:aTable]];
|
2008-03-27 19:30:13 +03:00
|
|
|
return [mTables count];
|
2008-05-02 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2008-03-27 19:30:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)removeTable:(NSMapTable *)aTable
|
|
|
|
{
|
2008-05-02 22:35:04 +04: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 19:30:13 +03:00
|
|
|
return [mTables count];
|
2008-05-02 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
|
2008-03-27 19:30:13 +03: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 22:35:04 +04:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 19:30:13 +03:00
|
|
|
if (aItem && aTable) {
|
2008-05-02 22:35:04 +04:00
|
|
|
NSValue *key = [NSValue valueWithPointer:aItem];
|
|
|
|
KeyEquivDBItem *shadowItem = [gShadowKeyEquivDB objectForKey:key];
|
2008-03-27 19:30:13 +03:00
|
|
|
if (shadowItem) {
|
|
|
|
[shadowItem addTable:aTable];
|
|
|
|
} else {
|
|
|
|
shadowItem = [[KeyEquivDBItem alloc] initWithItem:aItem table:aTable];
|
2008-05-02 22:35:04 +04:00
|
|
|
[gShadowKeyEquivDB setObject:shadowItem forKey:key];
|
2008-03-27 19:30:13 +03:00
|
|
|
// Release after [NSMutableDictionary setObject:forKey:] retains it (so
|
|
|
|
// that it will get dealloced when removeObjectForKey: is called).
|
|
|
|
[shadowItem release];
|
|
|
|
}
|
|
|
|
}
|
2008-05-02 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 19:30:13 +03: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 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-03-27 19:30:13 +03:00
|
|
|
if (aItem && aTable) {
|
2008-05-02 22:35:04 +04:00
|
|
|
NSValue *key = [NSValue valueWithPointer:aItem];
|
|
|
|
KeyEquivDBItem *shadowItem = [gShadowKeyEquivDB objectForKey:key];
|
2008-03-27 19:30:13 +03:00
|
|
|
if (shadowItem && [shadowItem hasTable:aTable]) {
|
|
|
|
if (![shadowItem removeTable:aTable])
|
2008-05-02 22:35:04 +04:00
|
|
|
[gShadowKeyEquivDB removeObjectForKey:key];
|
2008-03-27 19:30:13 +03:00
|
|
|
}
|
|
|
|
}
|
2008-05-02 22:35:04 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-03-27 19:30:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2008-04-24 19:30:20 +04: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 19:56:38 +04:00
|
|
|
// menus. sIndexingMenuLevel is a int32_t just in case it might be
|
2008-04-24 19:30:20 +04: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
|