2005-10-17 22:40:42 +04: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/. */
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2009-12-30 07:40:54 +03:00
|
|
|
#include <objc/objc-runtime.h>
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
#include "nsMenuBarX.h"
|
|
|
|
#include "nsMenuX.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsMenuItemX.h"
|
|
|
|
#include "nsMenuUtilsX.h"
|
2012-05-17 11:53:20 +04:00
|
|
|
#include "nsCocoaFeatures.h"
|
2008-03-27 06:42:57 +03:00
|
|
|
#include "nsCocoaUtils.h"
|
|
|
|
#include "nsCocoaWindow.h"
|
2010-08-17 07:26:17 +04:00
|
|
|
#include "nsChildView.h"
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2005-10-17 22:40:42 +04:00
|
|
|
#include "nsString.h"
|
2011-10-14 22:11:22 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMElement.h"
|
2015-02-18 18:34:00 +03:00
|
|
|
#include "nsIAppStartup.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsToolkitCompsCID.h"
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
NativeMenuItemTarget* nsMenuBarX::sNativeEventTarget = nil;
|
2014-01-14 02:01:52 +04:00
|
|
|
nsMenuBarX* nsMenuBarX::sLastGeckoMenuBarPainted = nullptr; // Weak
|
|
|
|
nsMenuBarX* nsMenuBarX::sCurrentPaintDelayedMenuBar = nullptr; // Weak
|
2006-12-19 22:26:41 +03:00
|
|
|
NSMenu* sApplicationMenu = nil;
|
2006-04-07 08:51:16 +04:00
|
|
|
BOOL gSomeMenuBarPainted = NO;
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2007-03-14 02:09:36 +03:00
|
|
|
// We keep references to the first quit and pref item content nodes we find, which
|
|
|
|
// will be from the hidden window. We use these when the document for the current
|
|
|
|
// window does not have a quit or pref item. We don't need strong refs here because
|
|
|
|
// these items are always strong ref'd by their owning menu bar (instance variable).
|
2012-07-30 18:20:58 +04:00
|
|
|
static nsIContent* sAboutItemContent = nullptr;
|
|
|
|
static nsIContent* sPrefItemContent = nullptr;
|
|
|
|
static nsIContent* sQuitItemContent = nullptr;
|
2006-10-06 21:50:47 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsNativeMenuServiceX, nsINativeMenuService)
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsNativeMenuServiceX::CreateNativeMenuBar(nsIWidget* aParent, nsIContent* aMenuBarNode)
|
2007-03-14 01:28:08 +03:00
|
|
|
{
|
2008-06-28 11:55:30 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Attempting to create native menu bar on wrong thread!");
|
|
|
|
|
|
|
|
nsRefPtr<nsMenuBarX> mb = new nsMenuBarX();
|
|
|
|
if (!mb)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return mb->Create(aParent, aMenuBarNode);
|
2007-03-14 01:28:08 +03:00
|
|
|
}
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
nsMenuBarX::nsMenuBarX()
|
2014-01-14 02:01:52 +04:00
|
|
|
: nsMenuGroupOwnerX(), mParentWindow(nullptr), mAwaitingDelayedPaint(false)
|
2005-10-17 22:40:42 +04:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
mNativeMenu = [[GeckoNSMenu alloc] initWithTitle:@"MainMenuBar" andMenuBarOwner:this];
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMenuBarX::~nsMenuBarX()
|
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2008-04-18 19:05:37 +04:00
|
|
|
if (nsMenuBarX::sLastGeckoMenuBarPainted == this)
|
2012-07-30 18:20:58 +04:00
|
|
|
nsMenuBarX::sLastGeckoMenuBarPainted = nullptr;
|
2008-04-18 19:05:37 +04:00
|
|
|
|
2007-03-14 02:09:36 +03:00
|
|
|
// the quit/pref items of a random window might have been used if there was no
|
|
|
|
// hidden window, thus we need to invalidate the weak references.
|
2007-03-28 02:40:27 +04:00
|
|
|
if (sAboutItemContent == mAboutItemContent)
|
2012-07-30 18:20:58 +04:00
|
|
|
sAboutItemContent = nullptr;
|
2007-03-14 02:09:36 +03:00
|
|
|
if (sQuitItemContent == mQuitItemContent)
|
2012-07-30 18:20:58 +04:00
|
|
|
sQuitItemContent = nullptr;
|
2007-03-14 02:09:36 +03:00
|
|
|
if (sPrefItemContent == mPrefItemContent)
|
2012-07-30 18:20:58 +04:00
|
|
|
sPrefItemContent = nullptr;
|
2007-03-14 02:09:36 +03:00
|
|
|
|
2010-01-29 04:06:54 +03:00
|
|
|
// make sure we unregister ourselves as a content observer
|
2015-02-18 18:34:00 +03:00
|
|
|
if (mContent) {
|
|
|
|
UnregisterForContentChanges(mContent);
|
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
// We have to manually clear the array here because clearing causes menu items
|
|
|
|
// to call back into the menu bar to unregister themselves. We don't want to
|
|
|
|
// depend on member variable ordering to ensure that the array gets cleared
|
|
|
|
// before the registration hash table is destroyed.
|
|
|
|
mMenuArray.Clear();
|
2007-11-09 20:38:33 +03:00
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
[mNativeMenu resetMenuBarOwner];
|
2008-07-28 08:46:33 +04:00
|
|
|
[mNativeMenu release];
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-06-27 10:52:18 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
nsresult nsMenuBarX::Create(nsIWidget* aParent, nsIContent* aContent)
|
2008-06-27 10:52:18 +04:00
|
|
|
{
|
2015-02-18 18:34:00 +03:00
|
|
|
if (!aParent)
|
2008-06-28 11:55:30 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
mParentWindow = aParent;
|
2008-06-28 11:55:30 +04:00
|
|
|
mContent = aContent;
|
2007-11-09 20:38:33 +03:00
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
if (mContent) {
|
|
|
|
AquifyMenuBar();
|
2007-09-17 19:06:59 +04:00
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
nsresult rv = nsMenuGroupOwnerX::Create(mContent);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2010-01-29 04:06:54 +03:00
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
RegisterForContentChanges(mContent, this);
|
|
|
|
ConstructNativeMenus();
|
|
|
|
} else {
|
|
|
|
ConstructFallbackNativeMenus();
|
|
|
|
}
|
2008-07-28 08:46:33 +04:00
|
|
|
|
|
|
|
// Give this to the parent window. The parent takes ownership.
|
2009-04-08 22:39:58 +04:00
|
|
|
static_cast<nsCocoaWindow*>(mParentWindow)->SetMenuBar(this);
|
|
|
|
|
|
|
|
return NS_OK;
|
2008-07-28 08:46:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsMenuBarX::ConstructNativeMenus()
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = mContent->GetChildCount();
|
|
|
|
for (uint32_t i = 0; i < count; i++) {
|
2008-06-28 11:55:30 +04:00
|
|
|
nsIContent *menuContent = mContent->GetChildAt(i);
|
2008-07-28 08:46:33 +04:00
|
|
|
if (menuContent &&
|
2015-03-03 14:09:00 +03:00
|
|
|
menuContent->IsXULElement(nsGkAtoms::menu)) {
|
2008-07-28 08:46:33 +04:00
|
|
|
nsMenuX* newMenu = new nsMenuX();
|
|
|
|
if (newMenu) {
|
|
|
|
nsresult rv = newMenu->Create(this, this, menuContent);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
InsertMenuAtIndex(newMenu, GetMenuCount());
|
|
|
|
else
|
|
|
|
delete newMenu;
|
2008-06-28 11:55:30 +04:00
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
2008-07-28 08:46:33 +04:00
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
void nsMenuBarX::ConstructFallbackNativeMenus()
|
|
|
|
{
|
|
|
|
if (sApplicationMenu) {
|
|
|
|
// Menu has already been built.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> stringBundle;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleSvc = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
bundleSvc->CreateBundle("chrome://global/locale/fallbackMenubar.properties", getter_AddRefs(stringBundle));
|
|
|
|
|
|
|
|
if (!stringBundle) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLString labelUTF16;
|
|
|
|
nsXPIDLString keyUTF16;
|
|
|
|
|
|
|
|
const char16_t* labelProp = MOZ_UTF16("quitMenuitem.label");
|
|
|
|
const char16_t* keyProp = MOZ_UTF16("quitMenuitem.key");
|
|
|
|
|
|
|
|
stringBundle->GetStringFromName(labelProp, getter_Copies(labelUTF16));
|
|
|
|
stringBundle->GetStringFromName(keyProp, getter_Copies(keyUTF16));
|
|
|
|
|
|
|
|
NSString* labelStr = [NSString stringWithUTF8String:
|
|
|
|
NS_ConvertUTF16toUTF8(labelUTF16).get()];
|
|
|
|
NSString* keyStr= [NSString stringWithUTF8String:
|
|
|
|
NS_ConvertUTF16toUTF8(keyUTF16).get()];
|
|
|
|
|
|
|
|
if (!nsMenuBarX::sNativeEventTarget) {
|
|
|
|
nsMenuBarX::sNativeEventTarget = [[NativeMenuItemTarget alloc] init];
|
|
|
|
}
|
|
|
|
|
|
|
|
sApplicationMenu = [[[[NSApp mainMenu] itemAtIndex:0] submenu] retain];
|
|
|
|
NSMenuItem* quitMenuItem = [[[NSMenuItem alloc] initWithTitle:labelStr
|
|
|
|
action:@selector(menuItemHit:)
|
|
|
|
keyEquivalent:keyStr] autorelease];
|
|
|
|
[quitMenuItem setTarget:nsMenuBarX::sNativeEventTarget];
|
|
|
|
[quitMenuItem setTag:eCommand_ID_Quit];
|
|
|
|
[sApplicationMenu addItem:quitMenuItem];
|
2015-04-23 17:55:20 +03:00
|
|
|
|
|
|
|
// Add debug logging to help decipher bug 1151345.
|
|
|
|
#if !defined(RELEASE_BUILD) || defined(DEBUG)
|
|
|
|
NSLog(@"nsMenuBarX::ConstructFallbackNativeMenus(): labelUTF16 %s, keyUTF16 %s",
|
|
|
|
NS_ConvertUTF16toUTF8(labelUTF16).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(keyUTF16).get());
|
|
|
|
#endif
|
2015-02-18 18:34:00 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t nsMenuBarX::GetMenuCount()
|
2005-10-17 22:40:42 +04:00
|
|
|
{
|
2008-06-28 11:55:30 +04:00
|
|
|
return mMenuArray.Length();
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
bool nsMenuBarX::MenuContainsAppMenu()
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
|
|
|
|
|
|
|
|
return ([mNativeMenu numberOfItems] > 0 &&
|
|
|
|
[[mNativeMenu itemAtIndex:0] submenu] == sApplicationMenu);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(false);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult nsMenuBarX::InsertMenuAtIndex(nsMenuX* aMenu, uint32_t aIndex)
|
2005-10-17 22:40:42 +04:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2007-11-15 09:28:33 +03:00
|
|
|
// If we haven't created a global Application menu yet, do it.
|
|
|
|
if (!sApplicationMenu) {
|
|
|
|
nsresult rv = NS_OK; // avoid warning about rv being unused
|
|
|
|
rv = CreateApplicationMenu(aMenu);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Can't create Application menu");
|
|
|
|
|
|
|
|
// Hook the new Application menu up to the menu bar.
|
|
|
|
NSMenu* mainMenu = [NSApp mainMenu];
|
|
|
|
NS_ASSERTION([mainMenu numberOfItems] > 0, "Main menu does not have any items, something is terribly wrong!");
|
|
|
|
[[mainMenu itemAtIndex:0] setSubmenu:sApplicationMenu];
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
// add menu to array that owns our menus
|
|
|
|
mMenuArray.InsertElementAt(aIndex, aMenu);
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
// hook up submenus
|
2008-06-28 11:55:30 +04:00
|
|
|
nsIContent* menuContent = aMenu->Content();
|
|
|
|
if (menuContent->GetChildCount() > 0 &&
|
|
|
|
!nsMenuUtilsX::NodeIsHiddenOrCollapsed(menuContent)) {
|
2009-01-23 18:45:03 +03:00
|
|
|
int insertionIndex = nsMenuUtilsX::CalculateNativeInsertionPoint(this, aMenu);
|
2008-07-28 08:46:33 +04:00
|
|
|
if (MenuContainsAppMenu())
|
2009-01-23 18:45:03 +03:00
|
|
|
insertionIndex++;
|
|
|
|
[mNativeMenu insertItem:aMenu->NativeMenuItem() atIndex:insertionIndex];
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsMenuBarX::RemoveMenuAtIndex(uint32_t aIndex)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
NS_ASSERTION(aIndex < mMenuArray.Length(), "Attempting submenu removal with bad index!");
|
|
|
|
|
|
|
|
// Our native menu and our internal menu object array might be out of sync.
|
|
|
|
// This happens, for example, when a submenu is hidden. Because of this we
|
|
|
|
// should not assume that a native submenu is hooked up.
|
2008-07-28 08:46:33 +04:00
|
|
|
NSMenuItem* nativeMenuItem = mMenuArray[aIndex]->NativeMenuItem();
|
|
|
|
int nativeMenuItemIndex = [mNativeMenu indexOfItem:nativeMenuItem];
|
|
|
|
if (nativeMenuItemIndex != -1)
|
|
|
|
[mNativeMenu removeItemAtIndex:nativeMenuItemIndex];
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
mMenuArray.RemoveElementAt(aIndex);
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
2010-01-29 04:06:54 +03:00
|
|
|
void nsMenuBarX::ObserveAttributeChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
nsIAtom* aAttribute)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsMenuBarX::ObserveContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer)
|
2010-01-29 04:06:54 +03:00
|
|
|
{
|
|
|
|
RemoveMenuAtIndex(aIndexInContainer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsMenuBarX::ObserveContentInserted(nsIDocument* aDocument,
|
2011-06-03 02:37:18 +04:00
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild)
|
2010-01-29 04:06:54 +03:00
|
|
|
{
|
|
|
|
nsMenuX* newMenu = new nsMenuX();
|
|
|
|
if (newMenu) {
|
|
|
|
nsresult rv = newMenu->Create(this, this, aChild);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-06-03 02:37:18 +04:00
|
|
|
InsertMenuAtIndex(newMenu, aContainer->IndexOf(aChild));
|
2010-01-29 04:06:54 +03:00
|
|
|
else
|
|
|
|
delete newMenu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-30 08:36:01 +03:00
|
|
|
void nsMenuBarX::ForceUpdateNativeMenuAt(const nsAString& indexString)
|
|
|
|
{
|
2013-10-10 20:59:40 +04:00
|
|
|
NSString* locationString = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(indexString.BeginReading())
|
|
|
|
length:indexString.Length()];
|
2008-10-30 08:36:01 +03:00
|
|
|
NSArray* indexes = [locationString componentsSeparatedByString:@"|"];
|
|
|
|
unsigned int indexCount = [indexes count];
|
|
|
|
if (indexCount == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsMenuX* currentMenu = NULL;
|
|
|
|
int targetIndex = [[indexes objectAtIndex:0] intValue];
|
|
|
|
int visible = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length = mMenuArray.Length();
|
2008-10-30 08:36:01 +03:00
|
|
|
// first find a menu in the menu bar
|
|
|
|
for (unsigned int i = 0; i < length; i++) {
|
|
|
|
nsMenuX* menu = mMenuArray[i];
|
|
|
|
if (!nsMenuUtilsX::NodeIsHiddenOrCollapsed(menu->Content())) {
|
|
|
|
visible++;
|
|
|
|
if (visible == (targetIndex + 1)) {
|
|
|
|
currentMenu = menu;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!currentMenu)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// fake open/close to cause lazy update to happen so submenus populate
|
2009-08-14 04:01:57 +04:00
|
|
|
currentMenu->MenuOpened();
|
|
|
|
currentMenu->MenuClosed();
|
2008-10-30 08:36:01 +03:00
|
|
|
|
|
|
|
// now find the correct submenu
|
|
|
|
for (unsigned int i = 1; currentMenu && i < indexCount; i++) {
|
|
|
|
targetIndex = [[indexes objectAtIndex:i] intValue];
|
|
|
|
visible = 0;
|
|
|
|
length = currentMenu->GetItemCount();
|
|
|
|
for (unsigned int j = 0; j < length; j++) {
|
|
|
|
nsMenuObjectX* targetMenu = currentMenu->GetItemAt(j);
|
|
|
|
if (!targetMenu)
|
|
|
|
return;
|
|
|
|
if (!nsMenuUtilsX::NodeIsHiddenOrCollapsed(targetMenu->Content())) {
|
|
|
|
visible++;
|
|
|
|
if (targetMenu->MenuObjectType() == eSubmenuObjectType && visible == (targetIndex + 1)) {
|
|
|
|
currentMenu = static_cast<nsMenuX*>(targetMenu);
|
|
|
|
// fake open/close to cause lazy update to happen
|
2009-08-14 04:01:57 +04:00
|
|
|
currentMenu->MenuOpened();
|
|
|
|
currentMenu->MenuClosed();
|
2008-10-30 08:36:01 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
// Calling this forces a full reload of the menu system, reloading all native
|
|
|
|
// menus and their items.
|
|
|
|
// Without this testing is hard because changes to the DOM affect the native
|
|
|
|
// menu system lazily.
|
|
|
|
void nsMenuBarX::ForceNativeMenuReload()
|
|
|
|
{
|
|
|
|
// tear down everything
|
|
|
|
while (GetMenuCount() > 0)
|
|
|
|
RemoveMenuAtIndex(0);
|
|
|
|
|
|
|
|
// construct everything
|
|
|
|
ConstructNativeMenus();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMenuX* nsMenuBarX::GetMenuAt(uint32_t aIndex)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
|
|
|
if (mMenuArray.Length() <= aIndex) {
|
|
|
|
NS_ERROR("Requesting menu at invalid index!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return mMenuArray[aIndex];
|
|
|
|
}
|
|
|
|
|
2010-01-27 19:25:01 +03:00
|
|
|
nsMenuX* nsMenuBarX::GetXULHelpMenu()
|
|
|
|
{
|
|
|
|
// The Help menu is usually (always?) the last one, so we start there and
|
|
|
|
// count back.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = GetMenuCount() - 1; i >= 0; --i) {
|
2010-01-27 19:25:01 +03:00
|
|
|
nsMenuX* aMenu = GetMenuAt(i);
|
|
|
|
if (aMenu && nsMenuX::IsXULHelpMenu(aMenu->Content()))
|
|
|
|
return aMenu;
|
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
// On SnowLeopard and later we must tell the OS which is our Help menu.
|
|
|
|
// Otherwise it will only add Spotlight for Help (the Search item) to our
|
|
|
|
// Help menu if its label/title is "Help" -- i.e. if the menu is in English.
|
|
|
|
// This resolves bugs 489196 and 539317.
|
|
|
|
void nsMenuBarX::SetSystemHelpMenu()
|
|
|
|
{
|
|
|
|
nsMenuX* xulHelpMenu = GetXULHelpMenu();
|
|
|
|
if (xulHelpMenu) {
|
|
|
|
NSMenu* helpMenu = (NSMenu*)xulHelpMenu->NativeData();
|
|
|
|
if (helpMenu)
|
|
|
|
[NSApp setHelpMenu:helpMenu];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
nsresult nsMenuBarX::Paint(bool aDelayed)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
if (!aDelayed && mAwaitingDelayedPaint) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mAwaitingDelayedPaint = false;
|
|
|
|
|
2008-07-28 08:46:33 +04:00
|
|
|
// Don't try to optimize anything in this painting by checking
|
|
|
|
// sLastGeckoMenuBarPainted because the menubar can be manipulated by
|
|
|
|
// native dialogs and sheet code and other things besides this paint method.
|
|
|
|
|
|
|
|
// We have to keep the same menu item for the Application menu so we keep
|
|
|
|
// passing it along.
|
|
|
|
NSMenu* outgoingMenu = [NSApp mainMenu];
|
|
|
|
NS_ASSERTION([outgoingMenu numberOfItems] > 0, "Main menu does not have any items, something is terribly wrong!");
|
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
// To work around bug 722676, we sometimes need to delay making mNativeMenu
|
|
|
|
// the main menu. This is an Apple bug that sometimes causes a top-level
|
|
|
|
// menu item to remain highlighted after pressing a Cmd+key combination that
|
|
|
|
// opens a new window, then closing the window. The OS temporarily
|
|
|
|
// highlights the appropriate top-level menu item whenever you press the
|
|
|
|
// Cmd+key combination for one of its submenus. (It does this by setting a
|
|
|
|
// "pressed" attribute on it.) The OS then uses a timer to remove this
|
|
|
|
// "pressed" attribute. But without our workaround we sometimes change the
|
|
|
|
// main menu before the timer has fired, so when it fires the menu item it
|
|
|
|
// was intended to unhighlight is no longer present in the main menu. This
|
|
|
|
// causes the item to remain semi-permanently highlighted (until you quit
|
|
|
|
// Firefox or navigate the main menu by hand).
|
|
|
|
if ((outgoingMenu != mNativeMenu) &&
|
|
|
|
[outgoingMenu isKindOfClass:[GeckoNSMenu class]]) {
|
|
|
|
if (aDelayed) {
|
|
|
|
[(GeckoNSMenu *)outgoingMenu setDelayResignMainMenu:false];
|
|
|
|
} else if ([(GeckoNSMenu *)outgoingMenu delayResignMainMenu]) {
|
|
|
|
PaintMenuBarAfterDelay();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
if (outgoingMenu != mNativeMenu) {
|
|
|
|
NSMenuItem* appMenuItem = [[outgoingMenu itemAtIndex:0] retain];
|
|
|
|
[outgoingMenu removeItemAtIndex:0];
|
|
|
|
[mNativeMenu insertItem:appMenuItem atIndex:0];
|
|
|
|
[appMenuItem release];
|
|
|
|
// Set menu bar and event target.
|
|
|
|
[NSApp setMainMenu:mNativeMenu];
|
|
|
|
}
|
2010-01-27 19:25:01 +03:00
|
|
|
SetSystemHelpMenu();
|
2008-07-25 08:32:41 +04:00
|
|
|
nsMenuBarX::sLastGeckoMenuBarPainted = this;
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
gSomeMenuBarPainted = YES;
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
return NS_OK;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
// Used to delay a call to nsMenuBarX::Paint(). Needed to work around
|
|
|
|
// bug 722676.
|
|
|
|
void nsMenuBarX::PaintMenuBarAfterDelay()
|
|
|
|
{
|
|
|
|
mAwaitingDelayedPaint = true;
|
|
|
|
nsMenuBarX::sCurrentPaintDelayedMenuBar = this;
|
|
|
|
[mNativeMenu retain];
|
|
|
|
// The delay for Apple's unhighlight timer is 0.1f, so we make ours a bit longer.
|
|
|
|
[mNativeMenu performSelector:@selector(delayedPaintMenuBar:)
|
|
|
|
withObject:nil
|
|
|
|
afterDelay:0.15f];
|
|
|
|
}
|
|
|
|
|
2009-09-22 01:48:55 +04:00
|
|
|
// Returns the 'key' attribute of the 'shortcutID' object (if any) in the
|
|
|
|
// currently active menubar's DOM document. 'shortcutID' should be the id
|
|
|
|
// (i.e. the name) of a component that defines a commonly used (and
|
|
|
|
// localized) cmd+key shortcut, and belongs to a keyset containing similar
|
|
|
|
// objects. For example "key_selectAll". Returns a value that can be
|
|
|
|
// compared to the first character of [NSEvent charactersIgnoringModifiers]
|
|
|
|
// when [NSEvent modifierFlags] == NSCommandKeyMask.
|
2009-10-01 08:15:05 +04:00
|
|
|
char nsMenuBarX::GetLocalizedAccelKey(const char *shortcutID)
|
2009-09-22 01:48:55 +04:00
|
|
|
{
|
|
|
|
if (!sLastGeckoMenuBarPainted)
|
|
|
|
return 0;
|
|
|
|
|
2014-09-09 19:14:46 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(sLastGeckoMenuBarPainted->mContent->OwnerDoc()));
|
2009-09-22 01:48:55 +04:00
|
|
|
if (!domDoc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
NS_ConvertASCIItoUTF16 shortcutIDStr((const char *)shortcutID);
|
|
|
|
nsCOMPtr<nsIDOMElement> shortcutElement;
|
|
|
|
domDoc->GetElementById(shortcutIDStr, getter_AddRefs(shortcutElement));
|
|
|
|
nsCOMPtr<nsIContent> shortcutContent = do_QueryInterface(shortcutElement);
|
|
|
|
if (!shortcutContent)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nsAutoString key;
|
2011-10-14 22:11:22 +04:00
|
|
|
shortcutContent->GetAttr(kNameSpaceID_None, nsGkAtoms::key, key);
|
2009-09-22 01:48:55 +04:00
|
|
|
NS_LossyConvertUTF16toASCII keyASC(key.get());
|
|
|
|
const char *keyASCPtr = keyASC.get();
|
|
|
|
if (!keyASCPtr)
|
|
|
|
return 0;
|
|
|
|
// If keyID's 'key' attribute isn't exactly one character long, it's not
|
|
|
|
// what we're looking for.
|
|
|
|
if (strlen(keyASCPtr) != sizeof(char))
|
|
|
|
return 0;
|
|
|
|
// Make sure retval is lower case.
|
|
|
|
char retval = tolower(keyASCPtr[0]);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
// Hide the item in the menu by setting the 'hidden' attribute. Returns it in |outHiddenNode| so
|
|
|
|
// the caller can hang onto it if they so choose. It is acceptable to pass nsull
|
|
|
|
// for |outHiddenNode| if the caller doesn't care about the hidden node.
|
|
|
|
void nsMenuBarX::HideItem(nsIDOMDocument* inDoc, const nsAString & inID, nsIContent** outHiddenNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> menuItem;
|
|
|
|
inDoc->GetElementById(inID, getter_AddRefs(menuItem));
|
|
|
|
nsCOMPtr<nsIContent> menuContent(do_QueryInterface(menuItem));
|
|
|
|
if (menuContent) {
|
2011-10-14 22:11:22 +04:00
|
|
|
menuContent->SetAttr(kNameSpaceID_None, nsGkAtoms::hidden, NS_LITERAL_STRING("true"), false);
|
2008-06-28 11:55:30 +04:00
|
|
|
if (outHiddenNode) {
|
|
|
|
*outHiddenNode = menuContent.get();
|
|
|
|
NS_IF_ADDREF(*outHiddenNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do what is necessary to conform to the Aqua guidelines for menus.
|
|
|
|
void nsMenuBarX::AquifyMenuBar()
|
|
|
|
{
|
2014-08-23 00:11:27 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(mContent->GetComposedDoc()));
|
2008-06-28 11:55:30 +04:00
|
|
|
if (domDoc) {
|
|
|
|
// remove the "About..." item and its separator
|
2012-07-30 18:20:58 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("aboutSeparator"), nullptr);
|
2008-06-28 11:55:30 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("aboutName"), getter_AddRefs(mAboutItemContent));
|
|
|
|
if (!sAboutItemContent)
|
|
|
|
sAboutItemContent = mAboutItemContent;
|
|
|
|
|
|
|
|
// remove quit item and its separator
|
2012-07-30 18:20:58 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_FileQuitSeparator"), nullptr);
|
2008-06-28 11:55:30 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_FileQuitItem"), getter_AddRefs(mQuitItemContent));
|
|
|
|
if (!sQuitItemContent)
|
|
|
|
sQuitItemContent = mQuitItemContent;
|
|
|
|
|
|
|
|
// remove prefs item and its separator, but save off the pref content node
|
|
|
|
// so we can invoke its command later.
|
2012-07-30 18:20:58 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_PrefsSeparator"), nullptr);
|
2008-06-28 11:55:30 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_preferences"), getter_AddRefs(mPrefItemContent));
|
|
|
|
if (!sPrefItemContent)
|
|
|
|
sPrefItemContent = mPrefItemContent;
|
2010-09-14 01:13:06 +04:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
// hide items that we use for the Application menu
|
2012-07-30 18:20:58 +04:00
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_mac_services"), nullptr);
|
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_mac_hide_app"), nullptr);
|
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_mac_hide_others"), nullptr);
|
|
|
|
HideItem(domDoc, NS_LITERAL_STRING("menu_mac_show_all"), nullptr);
|
2008-06-28 11:55:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// for creating menu items destined for the Application menu
|
2008-06-28 11:55:30 +04:00
|
|
|
NSMenuItem* nsMenuBarX::CreateNativeAppMenuItem(nsMenuX* inMenu, const nsAString& nodeID, SEL action,
|
2006-01-20 08:01:20 +03:00
|
|
|
int tag, NativeMenuItemTarget* target)
|
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2015-04-23 17:55:20 +03:00
|
|
|
// Add debug logging to help decipher bug 1151345.
|
|
|
|
#if !defined(RELEASE_BUILD) || defined(DEBUG)
|
|
|
|
NS_ConvertUTF16toUTF8 nodeID_UTF8(nodeID);
|
|
|
|
#endif
|
|
|
|
|
2014-08-23 00:11:27 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = inMenu->Content()->GetUncomposedDoc();
|
2015-04-23 17:55:20 +03:00
|
|
|
if (!doc) {
|
|
|
|
#if !defined(RELEASE_BUILD) || defined(DEBUG)
|
|
|
|
NSLog(@"nsMenuBarX::CreateNativeAppMenuItem(1): nodeID %s, doc is null!",
|
|
|
|
nodeID_UTF8.get());
|
|
|
|
#endif
|
2006-01-20 08:01:20 +03:00
|
|
|
return nil;
|
2015-04-23 17:55:20 +03:00
|
|
|
}
|
2006-01-20 08:01:20 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc(do_QueryInterface(doc));
|
2015-04-23 17:55:20 +03:00
|
|
|
if (!domdoc) {
|
|
|
|
#if !defined(RELEASE_BUILD) || defined(DEBUG)
|
|
|
|
NSLog(@"nsMenuBarX::CreateNativeAppMenuItem(2): nodeID %s, domdoc is null!",
|
|
|
|
nodeID_UTF8.get());
|
|
|
|
#endif
|
2006-01-20 08:01:20 +03:00
|
|
|
return nil;
|
2015-04-23 17:55:20 +03:00
|
|
|
}
|
2006-01-20 08:01:20 +03:00
|
|
|
|
2006-01-20 12:02:22 +03:00
|
|
|
// Get information from the gecko menu item
|
2006-01-20 08:01:20 +03:00
|
|
|
nsAutoString label;
|
|
|
|
nsAutoString modifiers;
|
2006-01-20 12:02:22 +03:00
|
|
|
nsAutoString key;
|
2006-01-20 08:01:20 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> menuItem;
|
|
|
|
domdoc->GetElementById(nodeID, getter_AddRefs(menuItem));
|
|
|
|
if (menuItem) {
|
|
|
|
menuItem->GetAttribute(NS_LITERAL_STRING("label"), label);
|
|
|
|
menuItem->GetAttribute(NS_LITERAL_STRING("modifiers"), modifiers);
|
2006-01-20 12:02:22 +03:00
|
|
|
menuItem->GetAttribute(NS_LITERAL_STRING("key"), key);
|
2006-01-20 08:01:20 +03:00
|
|
|
}
|
|
|
|
else {
|
2015-04-23 17:55:20 +03:00
|
|
|
#if !defined(RELEASE_BUILD) || defined(DEBUG)
|
|
|
|
NSLog(@"nsMenuBarX::CreateNativeAppMenuItem(3): nodeID %s, menuItem is null!",
|
|
|
|
nodeID_UTF8.get());
|
|
|
|
#endif
|
2006-01-20 08:01:20 +03:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2006-01-20 12:02:22 +03:00
|
|
|
// Get more information about the key equivalent. Start by
|
|
|
|
// finding the key node we need.
|
2006-10-31 13:29:30 +03:00
|
|
|
NSString* keyEquiv = nil;
|
2006-02-13 23:12:36 +03:00
|
|
|
unsigned int macKeyModifiers = 0;
|
|
|
|
if (!key.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIDOMElement> keyElement;
|
|
|
|
domdoc->GetElementById(key, getter_AddRefs(keyElement));
|
|
|
|
if (keyElement) {
|
|
|
|
nsCOMPtr<nsIContent> keyContent (do_QueryInterface(keyElement));
|
|
|
|
// first grab the key equivalent character
|
|
|
|
nsAutoString keyChar(NS_LITERAL_STRING(" "));
|
2011-10-14 22:11:22 +04:00
|
|
|
keyContent->GetAttr(kNameSpaceID_None, nsGkAtoms::key, keyChar);
|
2006-02-13 23:12:36 +03:00
|
|
|
if (!keyChar.EqualsLiteral(" ")) {
|
2013-10-10 20:59:40 +04:00
|
|
|
keyEquiv = [[NSString stringWithCharacters:reinterpret_cast<const unichar*>(keyChar.get())
|
|
|
|
length:keyChar.Length()] lowercaseString];
|
2006-02-13 23:12:36 +03:00
|
|
|
}
|
|
|
|
// now grab the key equivalent modifiers
|
|
|
|
nsAutoString modifiersStr;
|
2011-10-14 22:11:22 +04:00
|
|
|
keyContent->GetAttr(kNameSpaceID_None, nsGkAtoms::modifiers, modifiersStr);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t geckoModifiers = nsMenuUtilsX::GeckoModifiersForNodeAttribute(modifiersStr);
|
2008-06-28 11:55:30 +04:00
|
|
|
macKeyModifiers = nsMenuUtilsX::MacModifiersForGeckoModifiers(geckoModifiers);
|
2006-01-20 12:02:22 +03:00
|
|
|
}
|
|
|
|
}
|
2006-10-31 13:29:30 +03:00
|
|
|
// get the label into NSString-form
|
2013-10-10 20:59:40 +04:00
|
|
|
NSString* labelString = [NSString stringWithCharacters:reinterpret_cast<const unichar*>(label.get())
|
|
|
|
length:label.Length()];
|
|
|
|
|
2006-10-31 13:29:30 +03:00
|
|
|
if (!labelString)
|
|
|
|
labelString = @"";
|
|
|
|
if (!keyEquiv)
|
|
|
|
keyEquiv = @"";
|
2006-01-20 12:02:22 +03:00
|
|
|
|
|
|
|
// put together the actual NSMenuItem
|
|
|
|
NSMenuItem* newMenuItem = [[NSMenuItem alloc] initWithTitle:labelString action:action keyEquivalent:keyEquiv];
|
2006-01-20 08:01:20 +03:00
|
|
|
|
|
|
|
[newMenuItem setTag:tag];
|
|
|
|
[newMenuItem setTarget:target];
|
2006-01-20 12:02:22 +03:00
|
|
|
[newMenuItem setKeyEquivalentModifierMask:macKeyModifiers];
|
2010-01-27 09:14:40 +03:00
|
|
|
|
|
|
|
MenuItemInfo * info = [[MenuItemInfo alloc] initWithMenuGroupOwner:this];
|
|
|
|
[newMenuItem setRepresentedObject:info];
|
|
|
|
[info release];
|
2015-04-23 17:55:20 +03:00
|
|
|
|
|
|
|
#if !defined(RELEASE_BUILD) || defined(DEBUG)
|
|
|
|
if (!newMenuItem) {
|
|
|
|
NSLog(@"nsMenuBarX::CreateNativeAppMenuItem(4): nodeID %s, label %s, modifiers %s, key %s, newMenuItem is null!",
|
|
|
|
nodeID_UTF8.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(label).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(modifiers).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(key).get());
|
|
|
|
}
|
|
|
|
#endif
|
2006-01-20 08:01:20 +03:00
|
|
|
return newMenuItem;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
2006-01-20 08:01:20 +03:00
|
|
|
}
|
|
|
|
|
2007-08-30 23:47:20 +04:00
|
|
|
// build the Application menu shared by all menu bars
|
2008-06-28 11:55:30 +04:00
|
|
|
nsresult nsMenuBarX::CreateApplicationMenu(nsMenuX* inMenu)
|
2005-10-17 22:40:42 +04:00
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
|
|
|
|
|
2006-01-10 02:02:06 +03:00
|
|
|
// At this point, the application menu is the application menu from
|
|
|
|
// the nib in cocoa widgets. We do not have a way to create an application
|
|
|
|
// menu manually, so we grab the one from the nib and use that.
|
|
|
|
sApplicationMenu = [[[[NSApp mainMenu] itemAtIndex:0] submenu] retain];
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
/*
|
|
|
|
We support the following menu items here:
|
|
|
|
|
2010-09-14 01:13:06 +04:00
|
|
|
Menu Item DOM Node ID Notes
|
2006-01-20 08:01:20 +03:00
|
|
|
|
2010-09-14 01:13:06 +04:00
|
|
|
========================
|
|
|
|
= About This App = <- aboutName
|
|
|
|
========================
|
|
|
|
= Preferences... = <- menu_preferences
|
|
|
|
========================
|
|
|
|
= Services > = <- menu_mac_services <- (do not define key equivalent)
|
|
|
|
========================
|
|
|
|
= Hide App = <- menu_mac_hide_app
|
|
|
|
= Hide Others = <- menu_mac_hide_others
|
|
|
|
= Show All = <- menu_mac_show_all
|
|
|
|
========================
|
|
|
|
= Quit = <- menu_FileQuitItem
|
|
|
|
========================
|
2006-01-20 08:01:20 +03:00
|
|
|
|
|
|
|
If any of them are ommitted from the application's DOM, we just don't add
|
|
|
|
them. We always add a "Quit" item, but if an app developer does not provide a
|
|
|
|
DOM node with the right ID for the Quit item, we add it in English. App
|
2006-01-20 12:12:06 +03:00
|
|
|
developers need only add each node with a label and a key equivalent (if they
|
|
|
|
want one). Other attributes are optional. Like so:
|
2006-01-20 08:01:20 +03:00
|
|
|
|
|
|
|
<menuitem id="menu_preferences"
|
|
|
|
label="&preferencesCmdMac.label;"
|
2006-01-20 12:12:06 +03:00
|
|
|
key="open_prefs_key"/>
|
2006-01-20 08:01:20 +03:00
|
|
|
|
|
|
|
We need to use this system for localization purposes, until we have a better way
|
|
|
|
to define the Application menu to be used on Mac OS X.
|
|
|
|
*/
|
|
|
|
|
2005-10-17 22:40:42 +04:00
|
|
|
if (sApplicationMenu) {
|
2006-01-20 08:01:20 +03:00
|
|
|
// This code reads attributes we are going to care about from the DOM elements
|
2010-09-14 01:13:06 +04:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
NSMenuItem *itemBeingAdded = nil;
|
2010-09-14 01:13:06 +04:00
|
|
|
BOOL addAboutSeparator = FALSE;
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// Add the About menu item
|
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("aboutName"), @selector(menuItemHit:),
|
2006-10-06 21:56:47 +04:00
|
|
|
eCommand_ID_About, nsMenuBarX::sNativeEventTarget);
|
2006-01-20 08:01:20 +03:00
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
[itemBeingAdded release];
|
2006-10-31 13:29:30 +03:00
|
|
|
itemBeingAdded = nil;
|
2010-09-14 01:13:06 +04:00
|
|
|
|
|
|
|
addAboutSeparator = TRUE;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
2010-09-14 01:13:06 +04:00
|
|
|
|
|
|
|
// Add separator if either the About item or software update item exists
|
|
|
|
if (addAboutSeparator)
|
|
|
|
[sApplicationMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// Add the Preferences menu item
|
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("menu_preferences"), @selector(menuItemHit:),
|
2006-10-06 21:56:47 +04:00
|
|
|
eCommand_ID_Prefs, nsMenuBarX::sNativeEventTarget);
|
2006-01-20 08:01:20 +03:00
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
[itemBeingAdded release];
|
2006-10-31 13:29:30 +03:00
|
|
|
itemBeingAdded = nil;
|
2010-09-14 01:13:06 +04:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// Add separator after Preferences menu
|
2010-09-14 01:13:06 +04:00
|
|
|
[sApplicationMenu addItem:[NSMenuItem separatorItem]];
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
2006-01-20 08:01:20 +03:00
|
|
|
|
|
|
|
// Add Services menu item
|
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("menu_mac_services"), nil,
|
|
|
|
0, nil);
|
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
|
|
|
|
// set this menu item up as the Mac OS X Services menu
|
2009-12-30 07:40:54 +03:00
|
|
|
NSMenu* servicesMenu = [[GeckoServicesNSMenu alloc] initWithTitle:@""];
|
2006-01-20 08:01:20 +03:00
|
|
|
[itemBeingAdded setSubmenu:servicesMenu];
|
|
|
|
[NSApp setServicesMenu:servicesMenu];
|
|
|
|
|
2006-11-01 14:21:20 +03:00
|
|
|
[itemBeingAdded release];
|
|
|
|
itemBeingAdded = nil;
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// Add separator after Services menu
|
|
|
|
[sApplicationMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL addHideShowSeparator = FALSE;
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// Add menu item to hide this application
|
2010-08-17 07:26:17 +04:00
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("menu_mac_hide_app"), @selector(menuItemHit:),
|
|
|
|
eCommand_ID_HideApp, nsMenuBarX::sNativeEventTarget);
|
2006-01-20 08:01:20 +03:00
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
[itemBeingAdded release];
|
2006-10-31 13:29:30 +03:00
|
|
|
itemBeingAdded = nil;
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
addHideShowSeparator = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add menu item to hide other applications
|
2010-08-17 07:26:17 +04:00
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("menu_mac_hide_others"), @selector(menuItemHit:),
|
|
|
|
eCommand_ID_HideOthers, nsMenuBarX::sNativeEventTarget);
|
2006-01-20 08:01:20 +03:00
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
[itemBeingAdded release];
|
2006-10-31 13:29:30 +03:00
|
|
|
itemBeingAdded = nil;
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
addHideShowSeparator = TRUE;
|
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
// Add menu item to show all applications
|
2010-08-17 07:26:17 +04:00
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("menu_mac_show_all"), @selector(menuItemHit:),
|
|
|
|
eCommand_ID_ShowAll, nsMenuBarX::sNativeEventTarget);
|
2006-01-20 08:01:20 +03:00
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
[itemBeingAdded release];
|
2006-10-31 13:29:30 +03:00
|
|
|
itemBeingAdded = nil;
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
addHideShowSeparator = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a separator after the hide/show menus if at least one exists
|
|
|
|
if (addHideShowSeparator)
|
|
|
|
[sApplicationMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
|
|
|
// Add quit menu item
|
|
|
|
itemBeingAdded = CreateNativeAppMenuItem(inMenu, NS_LITERAL_STRING("menu_FileQuitItem"), @selector(menuItemHit:),
|
2006-10-06 21:56:47 +04:00
|
|
|
eCommand_ID_Quit, nsMenuBarX::sNativeEventTarget);
|
2006-01-20 08:01:20 +03:00
|
|
|
if (itemBeingAdded) {
|
|
|
|
[sApplicationMenu addItem:itemBeingAdded];
|
|
|
|
[itemBeingAdded release];
|
2006-10-31 13:29:30 +03:00
|
|
|
itemBeingAdded = nil;
|
2006-01-20 08:01:20 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// the current application does not have a DOM node for "Quit". Add one
|
|
|
|
// anyway, in English.
|
2006-01-20 12:02:22 +03:00
|
|
|
NSMenuItem* defaultQuitItem = [[[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(menuItemHit:)
|
|
|
|
keyEquivalent:@"q"] autorelease];
|
|
|
|
[defaultQuitItem setTarget:nsMenuBarX::sNativeEventTarget];
|
2006-10-06 21:56:47 +04:00
|
|
|
[defaultQuitItem setTag:eCommand_ID_Quit];
|
2006-01-20 12:02:22 +03:00
|
|
|
[sApplicationMenu addItem:defaultQuitItem];
|
2006-01-20 08:01:20 +03:00
|
|
|
}
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return (sApplicationMenu) ? NS_OK : NS_ERROR_FAILURE;
|
2008-02-19 23:58:01 +03:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
void nsMenuBarX::SetParent(nsIWidget* aParent)
|
2005-10-17 22:40:42 +04:00
|
|
|
{
|
2008-07-28 08:46:33 +04:00
|
|
|
mParentWindow = aParent;
|
2005-10-17 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
2008-04-08 08:38:52 +04:00
|
|
|
//
|
|
|
|
// Objective-C class used to allow us to have keyboard commands
|
|
|
|
// look like they are doing something but actually do nothing.
|
|
|
|
// We allow mouse actions to work normally.
|
|
|
|
//
|
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
// Controls whether or not native menu items should invoke their commands.
|
|
|
|
static BOOL gMenuItemsExecuteCommands = YES;
|
2008-04-08 08:38:52 +04:00
|
|
|
|
|
|
|
@implementation GeckoNSMenu
|
|
|
|
|
2014-01-14 02:01:52 +04:00
|
|
|
- (id)initWithTitle:(NSString *)aTitle
|
|
|
|
{
|
2015-02-06 11:09:39 +03:00
|
|
|
if ((self = [super initWithTitle:aTitle])) {
|
2014-01-14 02:01:52 +04:00
|
|
|
mMenuBarOwner = nullptr;
|
|
|
|
mDelayResignMainMenu = false;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithTitle:(NSString *)aTitle andMenuBarOwner:(nsMenuBarX *)aMenuBarOwner
|
|
|
|
{
|
2015-02-06 11:09:39 +03:00
|
|
|
if ((self = [super initWithTitle:aTitle])) {
|
2014-01-14 02:01:52 +04:00
|
|
|
mMenuBarOwner = aMenuBarOwner;
|
|
|
|
mDelayResignMainMenu = false;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)resetMenuBarOwner
|
|
|
|
{
|
|
|
|
mMenuBarOwner = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (bool)delayResignMainMenu
|
|
|
|
{
|
|
|
|
return mDelayResignMainMenu;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setDelayResignMainMenu:(bool)aShouldDelay
|
|
|
|
{
|
|
|
|
mDelayResignMainMenu = aShouldDelay;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to delay a call to nsMenuBarX::Paint(). Needed to work around
|
|
|
|
// bug 722676.
|
|
|
|
- (void)delayedPaintMenuBar:(id)unused
|
|
|
|
{
|
|
|
|
if (mMenuBarOwner) {
|
|
|
|
if (mMenuBarOwner == nsMenuBarX::sCurrentPaintDelayedMenuBar) {
|
|
|
|
mMenuBarOwner->Paint(true);
|
|
|
|
nsMenuBarX::sCurrentPaintDelayedMenuBar = nullptr;
|
|
|
|
} else {
|
|
|
|
mMenuBarOwner->ResetAwaitingDelayedPaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
[self release];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Undocumented method, present unchanged since OS X 10.6, used to temporarily
|
|
|
|
// highlight a top-level menu item when an appropriate Cmd+key combination is
|
|
|
|
// pressed.
|
2015-02-06 11:09:39 +03:00
|
|
|
- (void)_performActionWithHighlightingForItemAtIndex:(NSInteger)index
|
2014-01-14 02:01:52 +04:00
|
|
|
{
|
|
|
|
NSMenu *mainMenu = [NSApp mainMenu];
|
|
|
|
if ([mainMenu isKindOfClass:[GeckoNSMenu class]]) {
|
|
|
|
[(GeckoNSMenu *)mainMenu setDelayResignMainMenu:true];
|
|
|
|
}
|
|
|
|
[super _performActionWithHighlightingForItemAtIndex:index];
|
|
|
|
}
|
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
// Keyboard commands should not cause menu items to invoke their
|
|
|
|
// commands when there is a key window because we'd rather send
|
|
|
|
// the keyboard command to the window. We still have the menus
|
|
|
|
// go through the mechanics so they'll give the proper visual
|
|
|
|
// feedback.
|
2008-04-08 08:38:52 +04:00
|
|
|
- (BOOL)performKeyEquivalent:(NSEvent *)theEvent
|
|
|
|
{
|
2010-08-17 07:26:17 +04:00
|
|
|
// We've noticed that Mac OS X expects this check in subclasses before
|
|
|
|
// calling NSMenu's "performKeyEquivalent:".
|
|
|
|
//
|
|
|
|
// There is no case in which we'd need to do anything or return YES
|
|
|
|
// when we have no items so we can just do this check first.
|
|
|
|
if ([self numberOfItems] <= 0) {
|
|
|
|
return NO;
|
|
|
|
}
|
2008-04-08 08:38:52 +04:00
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
NSWindow *keyWindow = [NSApp keyWindow];
|
2008-04-08 08:38:52 +04:00
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
// If there is no key window then just behave normally. This
|
|
|
|
// probably means that this menu is associated with Gecko's
|
|
|
|
// hidden window.
|
|
|
|
if (!keyWindow) {
|
|
|
|
return [super performKeyEquivalent:theEvent];
|
|
|
|
}
|
2008-04-08 08:38:52 +04:00
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
NSResponder *firstResponder = [keyWindow firstResponder];
|
2010-08-13 23:47:35 +04:00
|
|
|
|
2014-12-11 17:44:07 +03:00
|
|
|
gMenuItemsExecuteCommands = NO;
|
2010-08-17 07:26:17 +04:00
|
|
|
[super performKeyEquivalent:theEvent];
|
|
|
|
gMenuItemsExecuteCommands = YES; // return to default
|
2008-04-28 21:43:34 +04:00
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
// Return YES if we invoked a command and there is now no key window or we changed
|
|
|
|
// the first responder. In this case we do not want to propagate the event because
|
|
|
|
// we don't want it handled again.
|
2014-12-11 17:44:07 +03:00
|
|
|
if (![NSApp keyWindow] || [[NSApp keyWindow] firstResponder] != firstResponder) {
|
|
|
|
return YES;
|
2010-08-17 07:26:17 +04:00
|
|
|
}
|
2010-08-13 23:47:35 +04:00
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
// Return NO so that we can handle the event via NSView's "keyDown:".
|
|
|
|
return NO;
|
2008-04-08 08:38:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-08-30 23:47:20 +04:00
|
|
|
//
|
2006-01-20 08:01:20 +03:00
|
|
|
// Objective-C class used as action target for menu items
|
2007-08-30 23:47:20 +04:00
|
|
|
//
|
|
|
|
|
2006-01-20 08:01:20 +03:00
|
|
|
@implementation NativeMenuItemTarget
|
|
|
|
|
|
|
|
// called when some menu item in this menu gets hit
|
|
|
|
-(IBAction)menuItemHit:(id)sender
|
|
|
|
{
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2011-11-30 20:33:00 +04:00
|
|
|
// menuGroupOwner below is an nsMenuBarX object, which we sometimes access
|
|
|
|
// after it's been deleted, causing crashes (see bug 704866 and bug 670914).
|
|
|
|
// To fix this "correctly", in nsMenuBarX::~nsMenuBarX() we'd need to
|
|
|
|
// iterate through every NSMenuItem in nsMenuBarX::mNativeMenu and its
|
|
|
|
// submenus, which might be quite time consuming. (For every NSMenuItem
|
|
|
|
// that has a "representedObject" that's a MenuItemInfo object, we'd need
|
|
|
|
// need to null out its "menuGroupOwner" if it's the same as the nsMenuBarX
|
|
|
|
// object being destroyed.) But if the nsMenuBarX object being destroyed
|
|
|
|
// corresponds to the currently focused window, it's likely that the
|
|
|
|
// nsMenuBarX destructor will null out sLastGeckoMenuBarPainted. So we can
|
|
|
|
// probably eliminate most of these crashes if we use this variable being
|
|
|
|
// null as an indicator that we're likely to crash below when we dereference
|
|
|
|
// menuGroupOwner.
|
|
|
|
if (!nsMenuBarX::sLastGeckoMenuBarPainted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
int tag = [sender tag];
|
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
if (!gMenuItemsExecuteCommands) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
nsMenuGroupOwnerX* menuGroupOwner = nullptr;
|
|
|
|
nsMenuBarX* menuBar = nullptr;
|
2010-01-27 09:14:40 +03:00
|
|
|
MenuItemInfo* info = [sender representedObject];
|
|
|
|
|
2015-02-18 18:34:00 +03:00
|
|
|
if (info) {
|
|
|
|
menuGroupOwner = [info menuGroupOwner];
|
|
|
|
if (!menuGroupOwner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (menuGroupOwner->MenuObjectType() == eMenuBarObjectType) {
|
|
|
|
menuBar = static_cast<nsMenuBarX*>(menuGroupOwner);
|
|
|
|
}
|
|
|
|
}
|
2010-01-27 09:14:40 +03:00
|
|
|
|
2010-08-17 07:26:17 +04:00
|
|
|
// Do special processing if this is for an app-global command.
|
|
|
|
if (tag == eCommand_ID_About) {
|
|
|
|
nsIContent* mostSpecificContent = sAboutItemContent;
|
|
|
|
if (menuBar && menuBar->mAboutItemContent)
|
|
|
|
mostSpecificContent = menuBar->mAboutItemContent;
|
|
|
|
nsMenuUtilsX::DispatchCommandTo(mostSpecificContent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tag == eCommand_ID_Prefs) {
|
|
|
|
nsIContent* mostSpecificContent = sPrefItemContent;
|
|
|
|
if (menuBar && menuBar->mPrefItemContent)
|
|
|
|
mostSpecificContent = menuBar->mPrefItemContent;
|
|
|
|
nsMenuUtilsX::DispatchCommandTo(mostSpecificContent);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tag == eCommand_ID_HideApp) {
|
|
|
|
[NSApp hide:sender];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tag == eCommand_ID_HideOthers) {
|
|
|
|
[NSApp hideOtherApplications:sender];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tag == eCommand_ID_ShowAll) {
|
|
|
|
[NSApp unhideAllApplications:sender];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (tag == eCommand_ID_Quit) {
|
|
|
|
nsIContent* mostSpecificContent = sQuitItemContent;
|
|
|
|
if (menuBar && menuBar->mQuitItemContent)
|
|
|
|
mostSpecificContent = menuBar->mQuitItemContent;
|
|
|
|
// If we have some content for quit we execute it. Otherwise we send a native app terminate
|
|
|
|
// message. If you want to stop a quit from happening, provide quit content and return
|
|
|
|
// the event as unhandled.
|
|
|
|
if (mostSpecificContent) {
|
2010-08-13 23:47:35 +04:00
|
|
|
nsMenuUtilsX::DispatchCommandTo(mostSpecificContent);
|
2010-08-13 10:38:40 +04:00
|
|
|
}
|
2010-08-17 07:26:17 +04:00
|
|
|
else {
|
2015-02-18 18:34:00 +03:00
|
|
|
nsCOMPtr<nsIAppStartup> appStartup = do_GetService(NS_APPSTARTUP_CONTRACTID);
|
|
|
|
if (appStartup) {
|
|
|
|
appStartup->Quit(nsIAppStartup::eAttemptQuit);
|
|
|
|
}
|
2010-08-13 23:47:35 +04:00
|
|
|
}
|
2008-04-08 08:38:52 +04:00
|
|
|
return;
|
2010-08-17 07:26:17 +04:00
|
|
|
}
|
2008-04-08 08:38:52 +04:00
|
|
|
|
|
|
|
// given the commandID, look it up in our hashtable and dispatch to
|
2008-06-28 11:55:30 +04:00
|
|
|
// that menu item.
|
2010-01-27 09:14:40 +03:00
|
|
|
if (menuGroupOwner) {
|
2012-08-22 19:56:38 +04:00
|
|
|
nsMenuItemX* menuItem = menuGroupOwner->GetMenuItemForCommandID(static_cast<uint32_t>(tag));
|
2008-06-28 11:55:30 +04:00
|
|
|
if (menuItem)
|
|
|
|
menuItem->DoCommand();
|
2008-04-08 08:38:52 +04:00
|
|
|
}
|
|
|
|
|
2008-02-19 23:58:01 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2006-01-20 08:01:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
2009-12-30 07:40:54 +03:00
|
|
|
|
|
|
|
// Objective-C class used for menu items on the Services menu to allow Gecko
|
|
|
|
// to override their standard behavior in order to stop key equivalents from
|
2010-08-17 07:26:17 +04:00
|
|
|
// firing in certain instances. When gMenuItemsExecuteCommands is NO, we return
|
2009-12-30 07:40:54 +03:00
|
|
|
// a dummy target and action instead of the actual target and action.
|
|
|
|
|
|
|
|
@implementation GeckoServicesNSMenuItem
|
|
|
|
|
|
|
|
- (id) target
|
|
|
|
{
|
|
|
|
id realTarget = [super target];
|
2010-08-17 07:26:17 +04:00
|
|
|
if (gMenuItemsExecuteCommands)
|
2009-12-30 07:40:54 +03:00
|
|
|
return realTarget;
|
|
|
|
else
|
2010-08-17 07:26:17 +04:00
|
|
|
return realTarget ? self : nil;
|
2009-12-30 07:40:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (SEL) action
|
|
|
|
{
|
|
|
|
SEL realAction = [super action];
|
2010-08-17 07:26:17 +04:00
|
|
|
if (gMenuItemsExecuteCommands)
|
2009-12-30 07:40:54 +03:00
|
|
|
return realAction;
|
|
|
|
else
|
2010-08-17 07:26:17 +04:00
|
|
|
return realAction ? @selector(_doNothing:) : NULL;
|
2009-12-30 07:40:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _doNothing:(id)sender
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// Objective-C class used as the Services menu so that Gecko can override the
|
|
|
|
// standard behavior of the Services menu in order to stop key equivalents
|
|
|
|
// from firing in certain instances.
|
|
|
|
|
|
|
|
@implementation GeckoServicesNSMenu
|
|
|
|
|
|
|
|
- (void)addItem:(NSMenuItem *)newItem
|
|
|
|
{
|
|
|
|
[self _overrideClassOfMenuItem:newItem];
|
|
|
|
[super addItem:newItem];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMenuItem *)addItemWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)keyEquiv
|
|
|
|
{
|
|
|
|
NSMenuItem * newItem = [super addItemWithTitle:aString action:aSelector keyEquivalent:keyEquiv];
|
|
|
|
[self _overrideClassOfMenuItem:newItem];
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertItem:(NSMenuItem *)newItem atIndex:(NSInteger)index
|
|
|
|
{
|
|
|
|
[self _overrideClassOfMenuItem:newItem];
|
|
|
|
[super insertItem:newItem atIndex:index];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSMenuItem *)insertItemWithTitle:(NSString *)aString action:(SEL)aSelector keyEquivalent:(NSString *)keyEquiv atIndex:(NSInteger)index
|
|
|
|
{
|
|
|
|
NSMenuItem * newItem = [super insertItemWithTitle:aString action:aSelector keyEquivalent:keyEquiv atIndex:index];
|
|
|
|
[self _overrideClassOfMenuItem:newItem];
|
|
|
|
return newItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _overrideClassOfMenuItem:(NSMenuItem *)menuItem
|
|
|
|
{
|
|
|
|
if ([menuItem class] == [NSMenuItem class])
|
|
|
|
object_setClass(menuItem, [GeckoServicesNSMenuItem class]);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|