2001-11-06 15:35:24 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-11-06 15:35:24 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-11-06 15:35:24 +00:00
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-11-06 15:35:24 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
2005-10-17 18:40:42 +00:00
|
|
|
* Josh Aas <josh@mozilla.com>
|
2001-11-06 15:35:24 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 22:01:16 +00:00
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
2001-11-06 15:35:24 +00:00
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 22:01:16 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-11-06 15:35:24 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 22:01:16 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-11-06 15:35:24 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIContent.h"
|
2003-03-26 07:41:30 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2001-11-06 15:35:24 +00:00
|
|
|
#include "nsIDocumentObserver.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "prinrval.h"
|
2006-06-21 22:02:23 +00:00
|
|
|
#include "nsIRollupListener.h"
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
#include "nsMenuX.h"
|
2007-04-22 01:42:17 +00:00
|
|
|
#include "nsMenuItemIconX.h"
|
2001-11-06 15:35:24 +00:00
|
|
|
#include "nsIMenu.h"
|
|
|
|
#include "nsIMenuBar.h"
|
|
|
|
#include "nsIMenuItem.h"
|
|
|
|
#include "nsIMenuCommandDispatcher.h"
|
2006-11-29 23:06:11 +00:00
|
|
|
#include "nsToolkit.h"
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2001-12-17 10:16:37 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2001-11-06 15:35:24 +00:00
|
|
|
#include "plstr.h"
|
|
|
|
|
|
|
|
#include "nsINameSpaceManager.h"
|
|
|
|
#include "nsWidgetAtoms.h"
|
|
|
|
#include "nsIXBLService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
#include "nsCRT.h"
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-02-21 23:37:34 +00:00
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
|
2006-06-21 22:02:23 +00:00
|
|
|
// externs defined in nsChildView.mm
|
|
|
|
extern nsIRollupListener * gRollupListener;
|
|
|
|
extern nsIWidget * gRollupWidget;
|
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
static PRBool gConstructingMenu = PR_FALSE;
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
// CIDs
|
|
|
|
#include "nsWidgetsCID.h"
|
|
|
|
static NS_DEFINE_CID(kMenuCID, NS_MENU_CID);
|
|
|
|
static NS_DEFINE_CID(kMenuItemCID, NS_MENUITEM_CID);
|
|
|
|
|
2007-11-09 19:51:27 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(nsMenuX, nsIMenu, nsIChangeObserver)
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
nsMenuX::nsMenuX()
|
2007-08-28 00:57:49 +00:00
|
|
|
: mVisibleItemsCount(0), mParent(nsnull), mManager(nsnull), mMacMenuID(0),
|
|
|
|
mMacMenu(nil), mNativeMenuItem(nil), mIsEnabled(PR_TRUE),
|
|
|
|
mDestroyHandlerCalled(PR_FALSE), mNeedsRebuild(PR_TRUE),
|
|
|
|
mConstructed(PR_FALSE), mVisible(PR_TRUE), mXBLAttached(PR_FALSE)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-01-20 05:01:20 +00:00
|
|
|
mMenuDelegate = [[MenuDelegate alloc] initWithGeckoMenu:this];
|
|
|
|
|
|
|
|
if (!nsMenuBarX::sNativeEventTarget)
|
|
|
|
nsMenuBarX::sNativeEventTarget = [[NativeMenuItemTarget alloc] init];
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsMenuX::~nsMenuX()
|
|
|
|
{
|
|
|
|
RemoveAll();
|
|
|
|
|
2006-10-31 10:29:30 +00:00
|
|
|
[mMacMenu release];
|
|
|
|
[mMenuDelegate release];
|
2007-08-02 19:08:40 +00:00
|
|
|
[mNativeMenuItem release];
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
// alert the change notifier we don't care no more
|
|
|
|
mManager->Unregister(mMenuContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-17 21:26:22 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:26:36 +00:00
|
|
|
nsMenuX::Create(nsISupports * aParent, const nsAString &aLabel, const nsAString &aAccessKey,
|
2007-09-14 22:04:59 +00:00
|
|
|
nsIChangeManager* aManager, nsIContent* aNode)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
mMenuContent = aNode;
|
|
|
|
|
|
|
|
// register this menu to be notified when changes are made to our content object
|
2005-10-17 18:40:42 +00:00
|
|
|
mManager = aManager; // weak ref
|
2007-07-08 07:08:04 +00:00
|
|
|
nsCOMPtr<nsIChangeObserver> changeObs(do_QueryInterface(static_cast<nsIChangeObserver*>(this)));
|
2001-11-06 15:35:24 +00:00
|
|
|
mManager->Register(mMenuContent, changeObs);
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
NS_ASSERTION(mMenuContent, "Menu not given a dom node at creation time");
|
|
|
|
NS_ASSERTION(mManager, "No change manager given, can't tell content model updates");
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
mParent = aParent;
|
|
|
|
// our parent could be either a menu bar (if we're toplevel) or a menu (if we're a submenu)
|
2005-10-17 18:40:42 +00:00
|
|
|
nsCOMPtr<nsIMenuBar> menubar = do_QueryInterface(aParent);
|
|
|
|
nsCOMPtr<nsIMenu> menu = do_QueryInterface(aParent);
|
|
|
|
NS_ASSERTION(menubar || menu, "Menu parent not a menu bar or menu!");
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-03-12 20:58:02 +00:00
|
|
|
// SetLabel will create the native menu if it has not been created yet
|
2001-11-06 15:35:24 +00:00
|
|
|
SetLabel(aLabel);
|
|
|
|
|
2007-03-13 22:28:08 +00:00
|
|
|
if (NodeIsHiddenOrCollapsed(mMenuContent))
|
2005-10-17 18:40:42 +00:00
|
|
|
mVisible = PR_FALSE;
|
|
|
|
|
|
|
|
if (menubar && mMenuContent->GetChildCount() == 0)
|
2001-11-06 15:35:24 +00:00
|
|
|
mVisible = PR_FALSE;
|
|
|
|
|
2007-08-16 20:26:04 +00:00
|
|
|
SetEnabled(!mMenuContent->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::disabled,
|
|
|
|
nsWidgetAtoms::_true, eCaseMatters));
|
|
|
|
|
|
|
|
NSString *newCocoaLabelString = MenuHelpersX::CreateTruncatedCocoaLabel(mLabel);
|
|
|
|
mNativeMenuItem = [[NSMenuItem alloc] initWithTitle:newCocoaLabelString action:nil keyEquivalent:@""];
|
|
|
|
[newCocoaLabelString release];
|
|
|
|
|
2007-09-17 21:29:42 +00:00
|
|
|
[mNativeMenuItem setEnabled:(BOOL)mIsEnabled];
|
|
|
|
|
2006-02-17 16:51:59 +00: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.
|
|
|
|
nsMenuEvent fake(PR_TRUE, 0, nsnull);
|
2007-09-17 15:06:59 +00:00
|
|
|
MenuConstruct(fake, nsnull, nsnull);
|
2006-02-17 16:51:59 +00:00
|
|
|
|
2007-04-22 01:42:17 +00:00
|
|
|
if (menu)
|
2007-08-16 20:26:04 +00:00
|
|
|
mIcon = new nsMenuItemIconX(static_cast<nsIMenu*>(this), menu, mMenuContent, mNativeMenuItem);
|
2007-04-22 01:42:17 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::GetParent(nsISupports*& aParent)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
aParent = mParent;
|
|
|
|
NS_IF_ADDREF(aParent);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::GetLabel(nsString &aText)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
aText = mLabel;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::SetLabel(const nsAString &aText)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
mLabel = aText;
|
2005-10-17 18:40:42 +00:00
|
|
|
// create an actual NSMenu if this is the first time
|
2006-10-10 21:15:45 +00:00
|
|
|
if (mMacMenu == nil)
|
2005-10-17 18:40:42 +00:00
|
|
|
mMacMenu = CreateMenuWithGeckoString(mLabel);
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::GetAccessKey(nsString &aText)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-09-14 17:47:24 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::SetAccessKey(const nsAString &aText)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-09-14 17:47:24 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
// This should only be used internally by our menu implementation. In all other
|
|
|
|
// cases menus and their items should be added and modified via the DOM.
|
2005-10-17 18:40:42 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::AddItem(nsISupports* aItem)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
2007-03-23 00:57:45 +00:00
|
|
|
|
|
|
|
if (!aItem)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// Figure out what we're adding
|
|
|
|
nsCOMPtr<nsIMenuItem> menuItem(do_QueryInterface(aItem, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = AddMenuItem(menuItem);
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
2007-03-23 00:57:45 +00:00
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIMenu> menu(do_QueryInterface(aItem, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = AddMenu(menu);
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
return rv;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-03-14 20:12:21 +00:00
|
|
|
nsresult nsMenuX::AddMenuItem(nsIMenuItem * aMenuItem)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
if (!aMenuItem)
|
2006-04-17 18:41:51 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
nsCOMPtr<nsIContent> menuItemContent;
|
|
|
|
aMenuItem->GetMenuItemContent(getter_AddRefs(menuItemContent));
|
2007-08-28 00:57:49 +00:00
|
|
|
mMenuItemsArray.AppendObject(aMenuItem); // owning ref
|
|
|
|
if (menuItemContent && NodeIsHiddenOrCollapsed(menuItemContent))
|
2007-03-23 00:57:45 +00:00
|
|
|
return NS_OK;
|
2007-08-28 00:57:49 +00:00
|
|
|
++mVisibleItemsCount;
|
|
|
|
|
2005-12-02 21:39:26 +00:00
|
|
|
// add the menu item to this menu
|
|
|
|
NSMenuItem* newNativeMenuItem;
|
|
|
|
aMenuItem->GetNativeData((void*&)newNativeMenuItem);
|
2007-03-12 20:58:02 +00:00
|
|
|
if (!newNativeMenuItem)
|
|
|
|
return NS_ERROR_FAILURE;
|
2005-12-02 21:39:26 +00:00
|
|
|
[mMacMenu addItem:newNativeMenuItem];
|
|
|
|
|
2005-11-28 18:33:48 +00:00
|
|
|
// set up target/action
|
2006-01-20 05:01:20 +00:00
|
|
|
[newNativeMenuItem setTarget:nsMenuBarX::sNativeEventTarget];
|
2005-11-28 18:33:48 +00:00
|
|
|
[newNativeMenuItem setAction:@selector(menuItemHit:)];
|
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
// set its command. we get the unique command id from the menubar
|
2005-10-17 18:40:42 +00:00
|
|
|
nsCOMPtr<nsIMenuCommandDispatcher> dispatcher(do_QueryInterface(mManager));
|
|
|
|
if (dispatcher) {
|
2001-11-06 15:35:24 +00:00
|
|
|
PRUint32 commandID = 0L;
|
|
|
|
dispatcher->Register(aMenuItem, &commandID);
|
2005-10-17 18:40:42 +00:00
|
|
|
if (commandID)
|
2005-11-28 18:33:48 +00:00
|
|
|
[newNativeMenuItem setTag:commandID];
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2005-10-19 21:10:08 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-03-14 20:12:21 +00:00
|
|
|
nsresult nsMenuX::AddMenu(nsIMenu * aMenu)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
// Add a submenu
|
2005-10-17 18:40:42 +00:00
|
|
|
if (!aMenu)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
nsCOMPtr<nsIContent> menuContent;
|
|
|
|
aMenu->GetMenuContent(getter_AddRefs(menuContent));
|
2007-08-28 00:57:49 +00:00
|
|
|
mMenuItemsArray.AppendObject(aMenu); // owning ref
|
|
|
|
if (menuContent && NodeIsHiddenOrCollapsed(menuContent))
|
2007-03-23 00:57:45 +00:00
|
|
|
return NS_OK;
|
2007-08-28 00:57:49 +00:00
|
|
|
++mVisibleItemsCount;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2005-12-02 21:39:26 +00:00
|
|
|
// We have to add a menu item and then associate the menu with it
|
2007-08-16 20:26:04 +00:00
|
|
|
NSMenuItem* newNativeMenuItem = (static_cast<nsMenuX*>(aMenu))->GetNativeMenuItem();
|
|
|
|
if (!newNativeMenuItem)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
[mMacMenu addItem:newNativeMenuItem];
|
2005-12-02 21:39:26 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
NSMenu* childMenu;
|
2007-03-12 20:58:02 +00:00
|
|
|
if (aMenu->GetNativeData((void**)&childMenu) == NS_OK)
|
2007-08-16 20:26:04 +00:00
|
|
|
[newNativeMenuItem setSubmenu:childMenu];
|
2007-03-12 20:58:02 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
// Includes all items, including hidden/collapsed ones
|
2005-10-17 18:40:42 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::GetItemCount(PRUint32 &aCount)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-11-28 18:33:48 +00:00
|
|
|
aCount = mMenuItemsArray.Count();
|
|
|
|
return NS_OK;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
// Includes all items, including hidden/collapsed ones
|
2005-10-17 18:40:42 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::GetItemAt(const PRUint32 aPos, nsISupports *& aMenuItem)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2007-08-28 00:57:49 +00:00
|
|
|
if (aPos >= (PRUint32)mMenuItemsArray.Count())
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
2005-11-28 18:33:48 +00:00
|
|
|
aMenuItem = mMenuItemsArray.ObjectAt(aPos);
|
|
|
|
NS_IF_ADDREF(aMenuItem);
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
// Checks both nsIMenus and nsIMenuItems. Not suitable for menus that are children
|
|
|
|
// of nsIMenuBar, which has slightly different rules for visiblity.
|
|
|
|
static PRBool MenuNodeIsVisible(nsISupports *item)
|
|
|
|
{
|
|
|
|
// Find the content for this item in the menu, be it a MenuItem or a Menu
|
|
|
|
nsCOMPtr<nsIContent> itemContent;
|
|
|
|
nsCOMPtr<nsIMenuItem> menuItem = do_QueryInterface(item);
|
2007-09-14 22:04:59 +00:00
|
|
|
if (menuItem) {
|
2007-08-28 00:57:49 +00:00
|
|
|
menuItem->GetMenuItemContent(getter_AddRefs(itemContent));
|
2007-09-04 04:49:04 +00:00
|
|
|
}
|
2007-08-28 00:57:49 +00:00
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIMenu> menu = do_QueryInterface(item);
|
|
|
|
if (menu)
|
|
|
|
menu->GetMenuContent(getter_AddRefs(itemContent));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the visibility of the item's content
|
|
|
|
return (itemContent && !NodeIsHiddenOrCollapsed(itemContent));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Only includes visible items
|
|
|
|
NS_IMETHODIMP nsMenuX::GetVisibleItemCount(PRUint32 &aCount)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
NS_IMETHODIMP nsMenuX::GetVisibleItemAt(const PRUint32 aPos, nsISupports *& aMenuItem)
|
|
|
|
{
|
|
|
|
PRUint32 count = mMenuItemsArray.Count();
|
|
|
|
if (aPos >= mVisibleItemsCount || aPos >= count)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// If there are no invisible items, can provide direct access
|
|
|
|
if (mVisibleItemsCount == count) {
|
|
|
|
nsCOMPtr<nsISupports> item = mMenuItemsArray.ObjectAt(aPos);
|
|
|
|
aMenuItem = item;
|
|
|
|
NS_IF_ADDREF(aMenuItem);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, traverse the array until we find the the item we're looking for.
|
|
|
|
nsCOMPtr<nsISupports> item;
|
|
|
|
PRUint32 visibleNodeIndex = 0;
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
item = mMenuItemsArray.ObjectAt(i);
|
|
|
|
if (MenuNodeIsVisible(item)) {
|
|
|
|
if (aPos == visibleNodeIndex) {
|
|
|
|
// we found the visible node we're looking for, return it
|
|
|
|
aMenuItem = item;
|
|
|
|
NS_IF_ADDREF(aMenuItem);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
visibleNodeIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::InsertItemAt(const PRUint32 aPos, nsISupports * aMenuItem)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-09-14 17:47:24 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::RemoveItem(const PRUint32 aPos)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-09-14 17:47:24 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::RemoveAll()
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-10-10 21:15:45 +00:00
|
|
|
if (mMacMenu != nil) {
|
2001-11-06 15:35:24 +00:00
|
|
|
// clear command id's
|
2005-10-17 18:40:42 +00:00
|
|
|
nsCOMPtr<nsIMenuCommandDispatcher> dispatcher(do_QueryInterface(mManager));
|
|
|
|
if (dispatcher) {
|
2005-11-28 18:33:48 +00:00
|
|
|
for (int i = 0; i < [mMacMenu numberOfItems]; i++)
|
|
|
|
dispatcher->Unregister((PRUint32)[[mMacMenu itemAtIndex:i] tag]);
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2005-11-28 18:33:48 +00:00
|
|
|
// get rid of Cocoa menu items
|
|
|
|
for (int i = [mMacMenu numberOfItems] - 1; i >= 0; i--)
|
2005-10-17 18:40:42 +00:00
|
|
|
[mMacMenu removeItemAtIndex:i];
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2005-11-28 18:33:48 +00:00
|
|
|
// get rid of Gecko menu items
|
|
|
|
mMenuItemsArray.Clear();
|
2007-08-28 00:57:49 +00:00
|
|
|
mVisibleItemsCount = 0;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::GetNativeData(void ** aData)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
*aData = mMacMenu;
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::SetNativeData(void * aData)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
[mMacMenu release];
|
|
|
|
mMacMenu = [(NSMenu*)aData retain];
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
nsEventStatus nsMenuX::MenuSelected(const nsMenuEvent & aMenuEvent)
|
|
|
|
{
|
2006-02-03 14:18:39 +00:00
|
|
|
// printf("JOSH: MenuSelected called for %s \n", NS_LossyConvertUTF16toASCII(mLabel).get());
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
// Determine if this is the correct menu to handle the event
|
2005-10-17 18:40:42 +00:00
|
|
|
MenuRef selectedMenuHandle = (MenuRef)aMenuEvent.mCommand;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
// at this point, the carbon event handler was installed so there
|
|
|
|
// must be a carbon MenuRef to be had
|
2006-01-20 05:01:20 +00:00
|
|
|
if (_NSGetCarbonMenu(mMacMenu) == selectedMenuHandle) {
|
2001-11-06 15:35:24 +00:00
|
|
|
// Open the node.
|
|
|
|
mMenuContent->SetAttr(kNameSpaceID_None, nsWidgetAtoms::open, NS_LITERAL_STRING("true"), PR_TRUE);
|
|
|
|
|
|
|
|
// Fire our oncreate handler. If we're told to stop, don't build the menu at all
|
|
|
|
PRBool keepProcessing = OnCreate();
|
|
|
|
|
2006-02-17 16:51:59 +00:00
|
|
|
if (!mNeedsRebuild || !keepProcessing)
|
2001-11-06 15:35:24 +00:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
if (!mConstructed || mNeedsRebuild) {
|
2001-11-06 15:35:24 +00:00
|
|
|
if (mNeedsRebuild)
|
|
|
|
RemoveAll();
|
|
|
|
|
2007-09-17 15:06:59 +00:00
|
|
|
MenuConstruct(aMenuEvent, nsnull, nsnull);
|
2006-02-17 16:51:59 +00:00
|
|
|
mConstructed = true;
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
OnCreated(); // Now that it's built, fire the popupShown event.
|
|
|
|
|
2007-09-19 22:15:48 +00:00
|
|
|
return nsEventStatus_eConsumeNoDefault;
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
else {
|
|
|
|
// Make sure none of our submenus are the ones that should be handling this
|
2005-11-28 18:33:48 +00:00
|
|
|
for (PRUint32 i = mMenuItemsArray.Count() - 1; i >= 0; i--) {
|
2007-11-09 17:38:33 +00:00
|
|
|
nsISupports* menuSupports = mMenuItemsArray.ObjectAt(i);
|
|
|
|
nsCOMPtr<nsIMenu> menu = do_QueryInterface(menuSupports);
|
|
|
|
if (menu) {
|
|
|
|
nsEventStatus status = menu->MenuSelected(aMenuEvent);
|
|
|
|
if (status != nsEventStatus_eIgnore)
|
|
|
|
return status;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-19 22:15:48 +00:00
|
|
|
return nsEventStatus_eIgnore;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-11-09 17:38:33 +00:00
|
|
|
void nsMenuX::MenuDeselected(const nsMenuEvent & aMenuEvent)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
// Destroy the menu
|
|
|
|
if (mConstructed) {
|
|
|
|
MenuDestruct(aMenuEvent);
|
|
|
|
mConstructed = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-11-09 17:38:33 +00:00
|
|
|
void nsMenuX::MenuConstruct(
|
2001-11-06 15:35:24 +00:00
|
|
|
const nsMenuEvent & aMenuEvent,
|
|
|
|
nsIWidget * aParentWindow,
|
2007-09-17 15:06:59 +00:00
|
|
|
void * aMenuNode)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
mConstructed = false;
|
|
|
|
gConstructingMenu = PR_TRUE;
|
|
|
|
|
|
|
|
// reset destroy handler flag so that we'll know to fire it next time this menu goes away.
|
|
|
|
mDestroyHandlerCalled = PR_FALSE;
|
|
|
|
|
2006-02-03 14:18:39 +00:00
|
|
|
//printf("nsMenuX::MenuConstruct called for %s = %d \n", NS_LossyConvertUTF16toASCII(mLabel).get(), mMacMenu);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
// Retrieve our menupopup.
|
|
|
|
nsCOMPtr<nsIContent> menuPopup;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(menuPopup));
|
2007-03-09 21:08:58 +00:00
|
|
|
if (!menuPopup) {
|
|
|
|
gConstructingMenu = PR_FALSE;
|
2007-11-09 17:38:33 +00:00
|
|
|
return;
|
2007-03-09 21:08:58 +00:00
|
|
|
}
|
2007-02-21 23:37:34 +00:00
|
|
|
|
|
|
|
// bug 365405: Manually wrap the menupopup node to make sure it's bounded
|
|
|
|
if (!mXBLAttached) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIXPConnect> xpconnect =
|
|
|
|
do_GetService(nsIXPConnect::GetCID(), &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsIDocument* ownerDoc = menuPopup->GetOwnerDoc();
|
|
|
|
nsIScriptGlobalObject* sgo;
|
|
|
|
if (ownerDoc && (sgo = ownerDoc->GetScriptGlobalObject())) {
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
JSObject* global = sgo->GetGlobalJSObject();
|
|
|
|
if (scriptContext && global) {
|
|
|
|
JSContext* cx = (JSContext*)scriptContext->GetNativeContext();
|
|
|
|
if (cx) {
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
|
|
|
xpconnect->WrapNative(cx, global,
|
|
|
|
menuPopup, NS_GET_IID(nsISupports),
|
|
|
|
getter_AddRefs(wrapper));
|
|
|
|
mXBLAttached = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
// Iterate over the kids
|
2003-09-30 21:04:27 +00:00
|
|
|
PRUint32 count = menuPopup->GetChildCount();
|
2006-10-10 21:15:45 +00:00
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
2003-09-30 21:04:27 +00:00
|
|
|
nsIContent *child = menuPopup->GetChildAt(i);
|
2005-10-17 18:40:42 +00:00
|
|
|
if (child) {
|
2001-11-06 15:35:24 +00:00
|
|
|
// depending on the type, create a menu item, separator, or submenu
|
2003-11-19 01:20:56 +00:00
|
|
|
nsIAtom *tag = child->Tag();
|
2007-09-14 22:04:59 +00:00
|
|
|
if (tag == nsWidgetAtoms::menuitem || tag == nsWidgetAtoms::menuseparator)
|
2007-03-23 00:57:45 +00:00
|
|
|
LoadMenuItem(child);
|
2005-10-17 18:40:42 +00:00
|
|
|
else if (tag == nsWidgetAtoms::menu)
|
2007-03-23 00:57:45 +00:00
|
|
|
LoadSubMenu(child);
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
} // for each menu item
|
2007-02-21 23:37:34 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
gConstructingMenu = PR_FALSE;
|
|
|
|
mNeedsRebuild = PR_FALSE;
|
2007-08-02 19:08:40 +00:00
|
|
|
// printf("Done building, mMenuItemsArray.Count() = %d \n", mMenuItemsArray.Count());
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-11-09 17:38:33 +00:00
|
|
|
void nsMenuX::MenuDestruct(const nsMenuEvent & aMenuEvent)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-02-03 14:18:39 +00:00
|
|
|
// printf("nsMenuX::MenuDestruct() called for %s \n", NS_LossyConvertUTF16toASCII(mLabel).get());
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-11-09 17:38:33 +00:00
|
|
|
// Fire our ondestroy handler. If we're told to stop, don't destroy the menu.
|
|
|
|
if (!OnDestroy())
|
|
|
|
return;
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-11-09 17:38:33 +00:00
|
|
|
if (mNeedsRebuild)
|
|
|
|
mConstructed = false;
|
|
|
|
// Close the node.
|
|
|
|
mMenuContent->UnsetAttr(kNameSpaceID_None, nsWidgetAtoms::open, PR_TRUE);
|
|
|
|
OnDestroyed();
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-11-09 17:38:33 +00:00
|
|
|
void nsMenuX::SetRebuild(PRBool aNeedsRebuild)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-11-28 18:33:48 +00:00
|
|
|
if (!gConstructingMenu)
|
2001-11-06 15:35:24 +00:00
|
|
|
mNeedsRebuild = aNeedsRebuild;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::SetEnabled(PRBool aIsEnabled)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2006-11-02 10:48:11 +00:00
|
|
|
if (aIsEnabled != mIsEnabled) {
|
|
|
|
// we always want to rebuild when this changes
|
|
|
|
SetRebuild(PR_TRUE);
|
|
|
|
mIsEnabled = aIsEnabled;
|
2006-10-10 21:15:45 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::GetEnabled(PRBool* aIsEnabled)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsEnabled);
|
|
|
|
*aIsEnabled = mIsEnabled;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsMenuX::GetMenuContent(nsIContent ** aMenuContent)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aMenuContent);
|
|
|
|
NS_IF_ADDREF(*aMenuContent = mMenuContent);
|
2005-10-17 18:40:42 +00:00
|
|
|
return NS_OK;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
NSMenu* nsMenuX::CreateMenuWithGeckoString(nsString& menuTitle)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
NSString* title = [NSString stringWithCharacters:(UniChar*)menuTitle.get() length:menuTitle.Length()];
|
|
|
|
NSMenu* myMenu = [[NSMenu alloc] initWithTitle:title];
|
|
|
|
[myMenu setDelegate:mMenuDelegate];
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2005-12-02 21:39:26 +00: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];
|
|
|
|
|
2005-10-17 18:40:42 +00: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.
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
return myMenu;
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
void nsMenuX::LoadMenuItem(nsIContent* inMenuItemContent)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
if (!inMenuItemContent)
|
2001-11-06 15:35:24 +00:00
|
|
|
return;
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
// create nsMenuItem
|
|
|
|
nsCOMPtr<nsIMenuItem> pnsMenuItem = do_CreateInstance(kMenuItemCID);
|
2007-03-14 20:12:21 +00:00
|
|
|
if (!pnsMenuItem)
|
|
|
|
return;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-03-14 20:12:21 +00:00
|
|
|
nsAutoString menuitemName;
|
|
|
|
inMenuItemContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, menuitemName);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-03-14 20:12:21 +00:00
|
|
|
// printf("menuitem %s \n", NS_LossyConvertUTF16toASCII(menuitemName).get());
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-03-14 20:12:21 +00:00
|
|
|
nsIMenuItem::EMenuItemType itemType = nsIMenuItem::eRegular;
|
2007-09-14 22:04:59 +00:00
|
|
|
if (inMenuItemContent->Tag() == nsWidgetAtoms::menuseparator) {
|
|
|
|
itemType = nsIMenuItem::eSeparator;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
static nsIContent::AttrValuesArray strings[] =
|
|
|
|
{&nsWidgetAtoms::checkbox, &nsWidgetAtoms::radio, nsnull};
|
|
|
|
switch (inMenuItemContent->FindAttrValueIn(kNameSpaceID_None, nsWidgetAtoms::type,
|
|
|
|
strings, eCaseMatters)) {
|
|
|
|
case 0: itemType = nsIMenuItem::eCheckbox; break;
|
|
|
|
case 1: itemType = nsIMenuItem::eRadio; break;
|
|
|
|
}
|
2007-03-14 20:12:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the item.
|
2007-09-14 22:04:59 +00:00
|
|
|
pnsMenuItem->Create(this, menuitemName, itemType, mManager, inMenuItemContent);
|
2007-03-14 20:12:21 +00:00
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
AddMenuItem(pnsMenuItem);
|
2007-04-22 01:42:17 +00:00
|
|
|
|
|
|
|
// This needs to happen after the nsIMenuItem object is inserted into
|
|
|
|
// our item array in AddMenuItem()
|
|
|
|
pnsMenuItem->SetupIcon();
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
void nsMenuX::LoadSubMenu(nsIContent* inMenuContent)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
nsAutoString menuName;
|
2007-03-13 22:28:08 +00:00
|
|
|
inMenuContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, menuName);
|
2006-02-03 14:18:39 +00:00
|
|
|
//printf("Creating Menu [%s] \n", NS_LossyConvertUTF16toASCII(menuName).get());
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
// Create nsMenu
|
2005-10-17 18:40:42 +00:00
|
|
|
nsCOMPtr<nsIMenu> pnsMenu(do_CreateInstance(kMenuCID));
|
2007-03-14 20:12:21 +00:00
|
|
|
if (!pnsMenu)
|
|
|
|
return;
|
|
|
|
|
2007-09-14 22:04:59 +00:00
|
|
|
pnsMenu->Create(reinterpret_cast<nsISupports*>(this), menuName, EmptyString(), mManager, inMenuContent);
|
2007-03-14 20:12:21 +00:00
|
|
|
|
2007-03-23 00:57:45 +00:00
|
|
|
AddMenu(pnsMenu);
|
2007-04-22 01:42:17 +00:00
|
|
|
|
|
|
|
// This needs to happen after the nsIMenu object is inserted into
|
|
|
|
// our item array in AddMenu()
|
|
|
|
pnsMenu->SetupIcon();
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fire our oncreate handler. Returns TRUE if we should keep processing the event,
|
|
|
|
// FALSE if the handler wants to stop the creation of the menu
|
2006-07-10 17:50:30 +00:00
|
|
|
PRBool nsMenuX::OnCreate()
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2005-04-28 23:48:28 +00:00
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_SHOWING, nsnull,
|
|
|
|
nsMouseEvent::eReal);
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
2006-06-28 22:41:33 +00:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mMenuContent;
|
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
// the menu is going to show and the oncreate handler has executed. We
|
|
|
|
// now need to walk our menu items, checking to see if any of them have
|
|
|
|
// a command attribute. If so, several apptributes must potentially
|
|
|
|
// be updated.
|
|
|
|
if (popupContent) {
|
2003-07-28 21:35:53 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc(do_QueryInterface(popupContent->GetDocument()));
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2003-09-30 21:04:27 +00:00
|
|
|
PRUint32 count = popupContent->GetChildCount();
|
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
|
|
|
nsIContent *grandChild = popupContent->GetChildAt(i);
|
2005-10-17 18:40:42 +00:00
|
|
|
if (grandChild->Tag() == nsWidgetAtoms::menuitem) {
|
2001-11-06 15:35:24 +00:00
|
|
|
// See if we have a command attribute.
|
|
|
|
nsAutoString command;
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::command, command);
|
|
|
|
if (!command.IsEmpty()) {
|
|
|
|
// We do! Look it up in our document
|
|
|
|
nsCOMPtr<nsIDOMElement> commandElt;
|
|
|
|
domDoc->GetElementById(command, getter_AddRefs(commandElt));
|
|
|
|
nsCOMPtr<nsIContent> commandContent(do_QueryInterface(commandElt));
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
if (commandContent) {
|
2001-11-06 15:35:24 +00:00
|
|
|
nsAutoString commandDisabled, menuDisabled;
|
|
|
|
commandContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, commandDisabled);
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, menuDisabled);
|
|
|
|
if (!commandDisabled.Equals(menuDisabled)) {
|
|
|
|
// The menu's disabled state needs to be updated to match the command.
|
|
|
|
if (commandDisabled.IsEmpty())
|
|
|
|
grandChild->UnsetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, PR_TRUE);
|
2005-11-28 18:33:48 +00:00
|
|
|
else
|
|
|
|
grandChild->SetAttr(kNameSpaceID_None, nsWidgetAtoms::disabled, commandDisabled, PR_TRUE);
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The menu's value and checked states need to be updated to match the command.
|
|
|
|
// Note that (unlike the disabled state) if the command has *no* value for either, we
|
|
|
|
// assume the menu is supplying its own.
|
|
|
|
nsAutoString commandChecked, menuChecked;
|
|
|
|
commandContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::checked, commandChecked);
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::checked, menuChecked);
|
|
|
|
if (!commandChecked.Equals(menuChecked)) {
|
|
|
|
if (!commandChecked.IsEmpty())
|
|
|
|
grandChild->SetAttr(kNameSpaceID_None, nsWidgetAtoms::checked, commandChecked, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString commandValue, menuValue;
|
|
|
|
commandContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, commandValue);
|
|
|
|
grandChild->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, menuValue);
|
|
|
|
if (!commandValue.Equals(menuValue)) {
|
|
|
|
if (!commandValue.IsEmpty())
|
|
|
|
grandChild->SetAttr(kNameSpaceID_None, nsWidgetAtoms::label, commandValue, PR_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
|
|
|
|
PRBool nsMenuX::OnCreated()
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2005-04-28 23:48:28 +00:00
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_SHOWN, nsnull, nsMouseEvent::eReal);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
2006-06-28 22:41:33 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mMenuContent;
|
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
// Fire our ondestroy handler. Returns TRUE if we should keep processing the event,
|
|
|
|
// FALSE if the handler wants to stop the destruction of the menu
|
2006-07-10 17:50:30 +00:00
|
|
|
PRBool nsMenuX::OnDestroy()
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
if (mDestroyHandlerCalled)
|
2001-11-06 15:35:24 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2005-04-28 23:48:28 +00:00
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_HIDING, nsnull,
|
|
|
|
nsMouseEvent::eReal);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
2006-06-28 22:41:33 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mMenuContent;
|
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
|
|
|
|
mDestroyHandlerCalled = PR_TRUE;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
|
|
|
|
PRBool nsMenuX::OnDestroyed()
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2005-04-28 23:48:28 +00:00
|
|
|
nsMouseEvent event(PR_TRUE, NS_XUL_POPUP_HIDDEN, nsnull,
|
|
|
|
nsMouseEvent::eReal);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> popupContent;
|
|
|
|
GetMenuPopupContent(getter_AddRefs(popupContent));
|
|
|
|
|
2006-06-28 22:41:33 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsIContent* dispatchTo = popupContent ? popupContent : mMenuContent;
|
|
|
|
rv = dispatchTo->DispatchDOMEvent(&event, nsnull, nsnull, &status);
|
|
|
|
|
|
|
|
mDestroyHandlerCalled = PR_TRUE;
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2006-07-10 17:50:30 +00:00
|
|
|
|
|
|
|
|
2001-11-06 15:35:24 +00: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 17:50:30 +00:00
|
|
|
void nsMenuX::GetMenuPopupContent(nsIContent** aResult)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
if (!aResult)
|
2001-11-06 15:35:24 +00:00
|
|
|
return;
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
nsresult rv;
|
2005-10-17 18:40:42 +00:00
|
|
|
nsCOMPtr<nsIXBLService> xblService = do_GetService("@mozilla.org/xbl;1", &rv);
|
|
|
|
if (!xblService)
|
2001-11-06 15:35:24 +00:00
|
|
|
return;
|
|
|
|
|
2003-09-30 21:04:27 +00:00
|
|
|
PRUint32 count = mMenuContent->GetChildCount();
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2003-09-30 21:04:27 +00:00
|
|
|
for (PRUint32 i = 0; i < count; i++) {
|
2001-11-06 15:35:24 +00:00
|
|
|
PRInt32 dummy;
|
2003-09-30 21:04:27 +00:00
|
|
|
nsIContent *child = mMenuContent->GetChildAt(i);
|
2001-11-06 15:35:24 +00:00
|
|
|
nsCOMPtr<nsIAtom> tag;
|
|
|
|
xblService->ResolveTag(child, &dummy, getter_AddRefs(tag));
|
2003-09-30 21:04:27 +00:00
|
|
|
if (tag == nsWidgetAtoms::menupopup) {
|
|
|
|
*aResult = child;
|
2001-11-06 15:35:24 +00:00
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Determines how many menus are visible among the siblings that are before me.
|
2006-05-04 21:15:47 +00:00
|
|
|
// It doesn't matter if I am visible. Note that this will always count the
|
|
|
|
// Application menu, since we always put it in there.
|
2006-07-10 17:50:30 +00:00
|
|
|
nsresult nsMenuX::CountVisibleBefore(PRUint32* outVisibleBefore)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2007-03-12 20:58:02 +00:00
|
|
|
NS_ASSERTION(outVisibleBefore, "bad index param in nsMenuX::CountVisibleBefore");
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIMenuBar> menubarParent = do_QueryInterface(mParent);
|
2007-08-28 00:57:49 +00:00
|
|
|
if (menubarParent) {
|
|
|
|
PRUint32 numMenus = 0;
|
|
|
|
menubarParent->GetMenuCount(numMenus);
|
|
|
|
|
|
|
|
// Find this menu among the children of my parent menubar
|
|
|
|
*outVisibleBefore = 1; // start at 1, the Application menu will always be there
|
|
|
|
for (PRUint32 i = 0; i < numMenus; i++) {
|
|
|
|
nsCOMPtr<nsIMenu> currMenu;
|
|
|
|
menubarParent->GetMenuAt(i, *getter_AddRefs(currMenu));
|
|
|
|
if (currMenu == static_cast<nsIMenu*>(this)) {
|
|
|
|
// we found ourselves, break out
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
if (currMenu) {
|
|
|
|
nsCOMPtr<nsIContent> menuContent;
|
|
|
|
currMenu->GetMenuContent(getter_AddRefs(menuContent));
|
|
|
|
if (menuContent &&
|
|
|
|
menuContent->GetChildCount() > 0 &&
|
|
|
|
!NodeIsHiddenOrCollapsed(menuContent)) {
|
|
|
|
++(*outVisibleBefore);
|
|
|
|
}
|
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2007-08-28 00:57:49 +00:00
|
|
|
} // if menubarParent
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIMenu> menuParent = do_QueryInterface(mParent);
|
|
|
|
if (!menuParent)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRUint32 numItems;
|
|
|
|
menuParent->GetItemCount(numItems);
|
|
|
|
|
|
|
|
// Find this menu among the children of my parent menu
|
|
|
|
for (PRUint32 i = 0; i < numItems; i++) {
|
|
|
|
// Using GetItemAt instead of GetVisibleItemAt to avoid O(N^2)
|
|
|
|
nsCOMPtr<nsISupports> currItem;
|
|
|
|
menuParent->GetItemAt(i, *getter_AddRefs(currItem));
|
|
|
|
nsCOMPtr<nsIMenu> currMenu = do_QueryInterface(currItem);
|
|
|
|
if (currMenu == static_cast<nsIMenu*>(this)) {
|
|
|
|
// we found ourselves, break out
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2007-03-12 20:58:02 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
// If the node is visible increment the outparam.
|
|
|
|
if (MenuNodeIsVisible(currItem))
|
2007-03-13 22:28:08 +00:00
|
|
|
++(*outVisibleBefore);
|
2007-08-28 00:57:49 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2007-03-13 22:28:08 +00:00
|
|
|
}
|
2007-08-28 00:57:49 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
|
2006-08-01 01:25:43 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMenuX::ChangeNativeEnabledStatusForMenuItem(nsIMenuItem* aMenuItem,
|
|
|
|
PRBool aEnabled)
|
2006-07-10 17:50:30 +00:00
|
|
|
{
|
2006-08-01 01:25:43 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMenuX::GetMenuRefAndItemIndexForMenuItem(nsISupports* aMenuItem,
|
|
|
|
void** aMenuRef,
|
|
|
|
PRUint16* aMenuItemIndex)
|
|
|
|
{
|
2007-04-22 01:42:17 +00:00
|
|
|
if (!mMacMenu)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
// look for the menu item given, and skip invisible elements
|
|
|
|
PRUint32 menuItemCount;
|
|
|
|
GetItemCount(menuItemCount);
|
|
|
|
PRUint32 visibleNodeIndex = 0;
|
2007-04-22 01:42:17 +00:00
|
|
|
for (PRUint32 i = 0; i < menuItemCount; i++) {
|
2007-08-28 00:57:49 +00:00
|
|
|
nsCOMPtr<nsISupports> currItem;
|
|
|
|
GetItemAt(i, *getter_AddRefs(currItem));
|
|
|
|
// Only check visible nodes
|
|
|
|
if (MenuNodeIsVisible(currItem)) {
|
|
|
|
if (currItem == aMenuItem) {
|
|
|
|
*aMenuRef = _NSGetCarbonMenu(mMacMenu);
|
|
|
|
// add 1 because carbon menu items are 1-indexed.
|
|
|
|
*aMenuItemIndex = visibleNodeIndex + 1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
visibleNodeIndex++;
|
2007-04-22 01:42:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2006-07-10 17:50:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-02 19:08:40 +00:00
|
|
|
id
|
|
|
|
nsMenuX::GetNativeMenuItem()
|
|
|
|
{
|
|
|
|
return mNativeMenuItem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
//
|
|
|
|
// nsIChangeObserver
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::AttributeChanged(nsIDocument *aDocument, PRInt32 aNameSpaceID,
|
|
|
|
nsIContent *aContent, nsIAtom *aAttribute)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
// ignore the |open| attribute, which is by far the most common
|
2005-10-17 18:40:42 +00:00
|
|
|
if (gConstructingMenu || (aAttribute == nsWidgetAtoms::open))
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
2007-08-28 00:57:49 +00:00
|
|
|
|
2001-11-06 15:35:24 +00:00
|
|
|
nsCOMPtr<nsIMenuBar> menubarParent = do_QueryInterface(mParent);
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
if (aAttribute == nsWidgetAtoms::disabled) {
|
2001-11-06 15:35:24 +00:00
|
|
|
SetRebuild(PR_TRUE);
|
2007-08-28 00:57:49 +00:00
|
|
|
SetEnabled(!mMenuContent->AttrValueIs(kNameSpaceID_None, nsWidgetAtoms::disabled,
|
|
|
|
nsWidgetAtoms::_true, eCaseMatters));
|
2005-11-28 18:33:48 +00:00
|
|
|
}
|
2005-10-17 18:40:42 +00:00
|
|
|
else if (aAttribute == nsWidgetAtoms::label) {
|
2001-11-06 15:35:24 +00:00
|
|
|
SetRebuild(PR_TRUE);
|
|
|
|
|
|
|
|
mMenuContent->GetAttr(kNameSpaceID_None, nsWidgetAtoms::label, mLabel);
|
|
|
|
|
2005-10-17 18:40:42 +00: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.
|
|
|
|
if (!menubarParent) {
|
2007-11-09 17:38:33 +00:00
|
|
|
nsCOMPtr<nsIMenu> parentMenu(do_QueryInterface(mParent));
|
|
|
|
parentMenu->SetRebuild(PR_TRUE);
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// reuse the existing menu, to avoid rebuilding the root menu bar.
|
2006-10-10 21:15:45 +00:00
|
|
|
NS_ASSERTION(mMacMenu != nil, "nsMenuX::AttributeChanged: invalid menu handle.");
|
2005-10-17 18:40:42 +00:00
|
|
|
RemoveAll();
|
2005-12-02 21:39:26 +00:00
|
|
|
NSString *newCocoaLabelString = MenuHelpersX::CreateTruncatedCocoaLabel(mLabel);
|
|
|
|
[mMacMenu setTitle:newCocoaLabelString];
|
|
|
|
[newCocoaLabelString release];
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2005-10-17 18:40:42 +00:00
|
|
|
else if (aAttribute == nsWidgetAtoms::hidden || aAttribute == nsWidgetAtoms::collapsed) {
|
2001-11-06 15:35:24 +00:00
|
|
|
SetRebuild(PR_TRUE);
|
2006-06-09 06:02:30 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
PRBool contentIsHiddenOrCollapsed = NodeIsHiddenOrCollapsed(mMenuContent);
|
|
|
|
|
|
|
|
// don't do anything if the state is correct already
|
|
|
|
if (contentIsHiddenOrCollapsed != mVisible)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMenu> menuParent = do_QueryInterface(mParent);
|
|
|
|
if (contentIsHiddenOrCollapsed) {
|
|
|
|
void *clientData = nsnull;
|
|
|
|
if (menubarParent)
|
|
|
|
menubarParent->GetNativeData(clientData);
|
|
|
|
else if (menuParent)
|
|
|
|
menuParent->GetNativeData(&clientData);
|
|
|
|
if (clientData) {
|
|
|
|
NSMenu* parentMenu = reinterpret_cast<NSMenu*>(clientData);
|
|
|
|
[parentMenu removeItem:mNativeMenuItem];
|
|
|
|
mVisible = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
2005-10-17 18:40:42 +00:00
|
|
|
else {
|
2007-08-28 00:57:49 +00:00
|
|
|
PRUint32 insertAfter = 0;
|
|
|
|
if (NS_SUCCEEDED(CountVisibleBefore(&insertAfter))) {
|
|
|
|
void *clientData = nsnull;
|
|
|
|
if (menubarParent)
|
|
|
|
menubarParent->GetNativeData(clientData);
|
|
|
|
else if (menuParent)
|
|
|
|
menuParent->GetNativeData(&clientData);
|
|
|
|
if (clientData) {
|
|
|
|
NSMenu* parentMenu = reinterpret_cast<NSMenu*>(clientData);
|
|
|
|
[parentMenu insertItem:mNativeMenuItem atIndex:insertAfter];
|
|
|
|
[mNativeMenuItem setSubmenu:mMacMenu];
|
|
|
|
mVisible = PR_TRUE;
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
2007-08-28 00:57:49 +00:00
|
|
|
}
|
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
}
|
2007-04-22 01:42:17 +00:00
|
|
|
else if (aAttribute == nsWidgetAtoms::image) {
|
|
|
|
SetupIcon();
|
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-08-28 00:57:49 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::ContentRemoved(nsIDocument *aDocument, nsIContent *aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
|
|
|
if (gConstructingMenu)
|
|
|
|
return NS_OK;
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
SetRebuild(PR_TRUE);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
RemoveItem(aIndexInContainer);
|
|
|
|
mManager->Unregister(aChild);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-08-28 00:57:49 +00:00
|
|
|
}
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
NS_IMETHODIMP nsMenuX::ContentInserted(nsIDocument *aDocument, nsIContent *aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
2001-11-06 15:35:24 +00:00
|
|
|
{
|
2005-10-17 18:40:42 +00:00
|
|
|
if (gConstructingMenu)
|
2001-11-06 15:35:24 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
SetRebuild(PR_TRUE);
|
2001-11-06 15:35:24 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2007-08-28 00:57:49 +00:00
|
|
|
}
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
|
2006-08-01 01:25:43 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsMenuX::SetupIcon()
|
|
|
|
{
|
2007-04-22 01:42:17 +00:00
|
|
|
// In addition to out-of-memory, menus that are children of the menu bar
|
|
|
|
// will not have mIcon set.
|
|
|
|
if (!mIcon) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return mIcon->SetupIcon();
|
2006-08-01 01:25:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
//
|
|
|
|
// Carbon event support
|
|
|
|
//
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
static pascal OSStatus MyMenuEventHandler(EventHandlerCallRef myHandler, EventRef event, void* userData)
|
|
|
|
{
|
|
|
|
UInt32 kind = ::GetEventKind(event);
|
2006-03-15 00:23:31 +00:00
|
|
|
if (kind == kEventMenuTargetItem) {
|
|
|
|
// get the position of the menu item we want
|
|
|
|
PRUint16 aPos;
|
|
|
|
::GetEventParameter(event, kEventParamMenuItemIndex, typeMenuItemIndex, NULL, sizeof(MenuItemIndex), NULL, &aPos);
|
|
|
|
aPos--; // subtract 1 from aPos because Carbon menu positions start at 1 not 0
|
|
|
|
|
2006-03-30 01:45:47 +00:00
|
|
|
// don't request a menu item that doesn't exist or we crash
|
|
|
|
// this might happen just due to some random quirks in the event system
|
|
|
|
PRUint32 itemCount;
|
2007-07-08 07:08:04 +00:00
|
|
|
nsIMenu* targetMenu = reinterpret_cast<nsIMenu*>(userData);
|
2007-08-28 00:57:49 +00:00
|
|
|
targetMenu->GetVisibleItemCount(itemCount);
|
2006-03-30 01:45:47 +00:00
|
|
|
if (aPos >= itemCount)
|
2006-06-21 22:02:23 +00:00
|
|
|
return eventNotHandledErr;
|
2006-03-30 01:45:47 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
nsCOMPtr<nsISupports> aTargetMenuItem;
|
|
|
|
targetMenu->GetVisibleItemAt((PRUint32)aPos, *getter_AddRefs(aTargetMenuItem));
|
2006-03-15 00:23:31 +00:00
|
|
|
|
|
|
|
// Send DOM event
|
|
|
|
// If the QI fails, we're over a submenu and we shouldn't send the event
|
|
|
|
nsCOMPtr<nsIMenuItem> bTargetMenuItem(do_QueryInterface(aTargetMenuItem));
|
|
|
|
if (bTargetMenuItem) {
|
|
|
|
PRBool handlerCalledPreventDefault; // but we don't actually care
|
|
|
|
bTargetMenuItem->DispatchDOMEvent(NS_LITERAL_STRING("DOMMenuItemActive"), &handlerCalledPreventDefault);
|
2006-06-21 22:02:23 +00:00
|
|
|
return noErr;
|
2006-03-15 00:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (kind == kEventMenuOpening || kind == kEventMenuClosed) {
|
2007-09-19 00:19:51 +00:00
|
|
|
if (kind == kEventMenuOpening && gRollupListener && gRollupWidget) {
|
2006-06-21 22:02:23 +00:00
|
|
|
gRollupListener->Rollup();
|
2007-07-18 00:07:36 +00:00
|
|
|
return userCanceledErr;
|
2006-06-21 22:02:23 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
nsISupports* supports = reinterpret_cast<nsISupports*>(userData);
|
2007-11-09 17:38:33 +00:00
|
|
|
nsCOMPtr<nsIMenu> menu(do_QueryInterface(supports));
|
|
|
|
if (menu) {
|
2005-10-17 18:40:42 +00:00
|
|
|
MenuRef menuRef;
|
|
|
|
::GetEventParameter(event, kEventParamDirectObject, typeMenuRef, NULL, sizeof(menuRef), NULL, &menuRef);
|
|
|
|
nsMenuEvent menuEvent(PR_TRUE, NS_MENU_SELECTED, nsnull);
|
|
|
|
menuEvent.time = PR_IntervalNow();
|
|
|
|
menuEvent.mCommand = (PRUint32)menuRef;
|
2007-11-09 17:38:33 +00:00
|
|
|
if (kind == kEventMenuOpening) {
|
|
|
|
menu->MenuSelected(menuEvent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
menu->MenuDeselected(menuEvent);
|
|
|
|
}
|
2006-06-21 22:02:23 +00:00
|
|
|
return noErr;
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-21 22:02:23 +00:00
|
|
|
return eventNotHandledErr;
|
2005-10-17 18:40:42 +00:00
|
|
|
}
|
|
|
|
|
2006-07-10 17:50:30 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
static OSStatus InstallMyMenuEventHandler(MenuRef menuRef, void* userData, EventHandlerRef* outHandler)
|
|
|
|
{
|
|
|
|
static EventTypeSpec eventList[] = {
|
2006-05-16 18:13:02 +00:00
|
|
|
{kEventClassMenu, kEventMenuOpening},
|
|
|
|
{kEventClassMenu, kEventMenuClosed},
|
|
|
|
{kEventClassMenu, kEventMenuTargetItem}
|
|
|
|
};
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
static EventHandlerUPP gMyMenuEventHandlerUPP = NewEventHandlerUPP(&MyMenuEventHandler);
|
|
|
|
OSStatus status = ::InstallMenuEventHandler(menuRef, gMyMenuEventHandlerUPP,
|
|
|
|
sizeof(eventList) / sizeof(EventTypeSpec), eventList,
|
|
|
|
userData, outHandler);
|
|
|
|
NS_ASSERTION(status == noErr,"Installing carbon menu events failed.");
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
//
|
2006-01-20 05:01:20 +00:00
|
|
|
// MenuDelegate Objective-C class, used to set up Carbon events
|
2007-08-28 00:57:49 +00:00
|
|
|
//
|
2005-10-17 18:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
@implementation MenuDelegate
|
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
- (id)initWithGeckoMenu:(nsMenuX*)geckoMenu
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
mGeckoMenu = geckoMenu;
|
|
|
|
mHaveInstalledCarbonEvents = FALSE;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
|
2006-10-31 10:29:30 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
RemoveEventHandler(mEventHandler);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
2005-10-17 18:40:42 +00:00
|
|
|
|
2007-08-28 00:57:49 +00:00
|
|
|
|
2005-10-17 18:40:42 +00:00
|
|
|
// You can get a MenuRef from an NSMenu*, but not until it has been made visible
|
|
|
|
// or added to the main menu bar. Basically, Cocoa is attempting lazy loading,
|
|
|
|
// and that doesn't work for us. We don't need any carbon events until after the
|
|
|
|
// first time the menu is shown, so when that happens we install the carbon
|
|
|
|
// event handler. This works because at this point we can get a MenuRef without
|
2007-07-18 00:07:36 +00:00
|
|
|
// much trouble.
|
2005-10-17 18:40:42 +00:00
|
|
|
- (void)menuNeedsUpdate:(NSMenu*)aMenu
|
|
|
|
{
|
|
|
|
if (!mHaveInstalledCarbonEvents) {
|
2006-01-20 05:01:20 +00:00
|
|
|
MenuRef myMenuRef = _NSGetCarbonMenu(aMenu);
|
2005-10-17 18:40:42 +00:00
|
|
|
if (myMenuRef) {
|
2006-10-31 10:29:30 +00:00
|
|
|
InstallMyMenuEventHandler(myMenuRef, mGeckoMenu, &mEventHandler);
|
2005-10-17 18:40:42 +00:00
|
|
|
mHaveInstalledCarbonEvents = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|