2001-11-06 18:35:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsMenuItemX.h"
|
|
|
|
#include "nsMenuBarX.h"
|
|
|
|
#include "nsMenuX.h"
|
|
|
|
#include "nsMenuItemIconX.h"
|
|
|
|
#include "nsMenuUtilsX.h"
|
2013-07-10 18:11:07 +04:00
|
|
|
#include "nsCocoaUtils.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
#include "nsCOMPtr.h"
|
2011-10-14 22:11:22 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2010-06-24 01:35:57 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2018-01-31 23:18:11 +03:00
|
|
|
#include "mozilla/dom/Event.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2001-11-06 18:35:24 +03:00
|
|
|
#include "nsIWidget.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-01-31 23:18:11 +03:00
|
|
|
|
2018-11-14 20:03:36 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2018-01-31 23:18:11 +03:00
|
|
|
using mozilla::dom::Event;
|
|
|
|
using mozilla::dom::CallerType;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2021-03-04 04:24:53 +03:00
|
|
|
nsMenuItemX::nsMenuItemX(nsMenuX* aParent, const nsString& aLabel, EMenuItemType aItemType,
|
|
|
|
nsMenuGroupOwnerX* aMenuGroupOwner, nsIContent* aNode)
|
2021-03-04 04:24:57 +03:00
|
|
|
: mContent(aNode), mType(aItemType), mMenuParent(aParent), mMenuGroupOwner(aMenuGroupOwner) {
|
2021-03-02 05:24:54 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
2021-03-04 04:24:53 +03:00
|
|
|
MOZ_COUNT_CTOR(nsMenuItemX);
|
2021-03-02 05:24:54 +03:00
|
|
|
|
2021-04-07 03:39:45 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mContent->IsElement(), "nsMenuItemX should only be created for elements");
|
2010-01-27 09:14:40 +03:00
|
|
|
NS_ASSERTION(mMenuGroupOwner, "No menu owner given, must have one!");
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2010-01-27 09:14:40 +03:00
|
|
|
mMenuGroupOwner->RegisterForContentChanges(mContent, this);
|
2008-01-23 07:04:15 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
dom::Document* doc = mContent->GetUncomposedDoc();
|
2007-08-17 00:26:04 +04:00
|
|
|
|
2006-06-15 19:54:58 +04:00
|
|
|
// if we have a command associated with this menu item, register for changes
|
|
|
|
// to the command DOM node
|
2010-06-24 01:35:57 +04:00
|
|
|
if (doc) {
|
2007-08-17 00:26:04 +04:00
|
|
|
nsAutoString ourCommand;
|
2021-04-07 03:39:45 +03:00
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::command, ourCommand);
|
2007-08-17 00:26:04 +04:00
|
|
|
|
|
|
|
if (!ourCommand.IsEmpty()) {
|
2021-02-11 22:43:59 +03:00
|
|
|
dom::Element* commandElement = doc->GetElementById(ourCommand);
|
2007-08-17 00:26:04 +04:00
|
|
|
|
|
|
|
if (commandElement) {
|
2017-12-05 20:05:51 +03:00
|
|
|
mCommandElement = commandElement;
|
2007-08-17 00:26:04 +04:00
|
|
|
// register to observe the command DOM element
|
2017-12-05 20:05:51 +03:00
|
|
|
mMenuGroupOwner->RegisterForContentChanges(mCommandElement, this);
|
2006-06-15 19:54:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
// decide enabled state based on command content if it exists, otherwise do it based
|
2006-06-16 19:05:43 +04:00
|
|
|
// on our own content
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEnabled;
|
2021-02-11 22:43:59 +03:00
|
|
|
if (mCommandElement) {
|
2017-12-05 20:05:51 +03:00
|
|
|
isEnabled = !mCommandElement->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
|
|
|
nsGkAtoms::_true, eCaseMatters);
|
2021-02-11 22:43:59 +03:00
|
|
|
} else {
|
2021-04-07 03:39:45 +03:00
|
|
|
isEnabled = !mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
|
2017-12-07 21:13:50 +03:00
|
|
|
nsGkAtoms::_true, eCaseMatters);
|
2021-02-11 22:43:59 +03:00
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2005-12-03 00:39:26 +03:00
|
|
|
// set up the native menu item
|
2008-06-28 11:55:30 +04:00
|
|
|
if (mType == eSeparatorMenuItemType) {
|
2005-12-03 00:39:26 +03:00
|
|
|
mNativeMenuItem = [[NSMenuItem separatorItem] retain];
|
|
|
|
} else {
|
2009-04-30 10:48:14 +04:00
|
|
|
NSString* newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(aLabel);
|
2005-12-03 00:39:26 +03:00
|
|
|
mNativeMenuItem = [[NSMenuItem alloc] initWithTitle:newCocoaLabelString
|
|
|
|
action:nil
|
|
|
|
keyEquivalent:@""];
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2021-04-07 03:39:46 +03:00
|
|
|
mIsChecked = mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::checked,
|
|
|
|
nsGkAtoms::_true, eCaseMatters);
|
|
|
|
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.enabled = isEnabled;
|
2021-04-07 03:39:46 +03:00
|
|
|
mNativeMenuItem.state = mIsChecked ? NSOnState : NSOffState;
|
2007-08-17 00:26:04 +04:00
|
|
|
|
2011-02-13 15:28:00 +03:00
|
|
|
SetKeyEquiv();
|
2005-12-03 00:39:26 +03:00
|
|
|
}
|
2007-04-22 05:42:17 +04:00
|
|
|
|
2021-03-18 05:30:36 +03:00
|
|
|
mIcon = MakeUnique<nsMenuItemIconX>(this);
|
2021-03-02 03:17:37 +03:00
|
|
|
|
2021-04-20 02:08:32 +03:00
|
|
|
mIsVisible = !nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
|
|
|
|
|
2021-04-20 02:08:32 +03:00
|
|
|
// All menu items share the same target and action, and are differentiated
|
|
|
|
// be a unique (representedObject, tag) pair.
|
|
|
|
mNativeMenuItem.target = nsMenuBarX::sNativeEventTarget;
|
|
|
|
mNativeMenuItem.action = @selector(menuItemHit:);
|
|
|
|
mNativeMenuItem.representedObject = mMenuGroupOwner->GetRepresentedObject();
|
|
|
|
mNativeMenuItem.tag = mMenuGroupOwner->RegisterForCommand(this);
|
|
|
|
|
2021-04-20 02:08:34 +03:00
|
|
|
if (mIsVisible) {
|
|
|
|
SetupIcon();
|
|
|
|
}
|
|
|
|
|
2021-03-04 04:24:53 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMenuItemX::~nsMenuItemX() {
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
|
|
|
|
|
|
|
// autorelease the native menu item so that anything else happening to this
|
|
|
|
// object happens before the native menu item actually dies
|
|
|
|
[mNativeMenuItem autorelease];
|
|
|
|
|
2021-03-22 22:55:45 +03:00
|
|
|
DetachFromGroupOwner();
|
2021-03-04 04:24:53 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(nsMenuItemX);
|
2008-06-27 10:52:18 +04:00
|
|
|
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-06-27 10:52:18 +04:00
|
|
|
}
|
|
|
|
|
2021-03-22 22:55:45 +03:00
|
|
|
void nsMenuItemX::DetachFromGroupOwner() {
|
|
|
|
if (mMenuGroupOwner) {
|
2021-04-20 02:08:32 +03:00
|
|
|
mMenuGroupOwner->UnregisterCommand(mNativeMenuItem.tag);
|
|
|
|
|
2021-03-22 22:55:45 +03:00
|
|
|
if (mContent) {
|
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(mContent);
|
|
|
|
}
|
|
|
|
if (mCommandElement) {
|
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(mCommandElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mMenuGroupOwner = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsMenuItemX::SetChecked(bool aIsChecked) {
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2005-12-03 00:39:26 +03:00
|
|
|
mIsChecked = aIsChecked;
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
// update the content model. This will also handle unchecking our siblings
|
|
|
|
// if we are a radiomenu
|
2021-04-07 03:39:46 +03:00
|
|
|
if (mIsChecked) {
|
|
|
|
mContent->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::checked, u"true"_ns, true);
|
|
|
|
} else {
|
|
|
|
mContent->AsElement()->UnsetAttr(kNameSpaceID_None, nsGkAtoms::checked, true);
|
|
|
|
}
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2005-12-03 00:39:26 +03:00
|
|
|
// update native menu item
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.state = mIsChecked ? NSOnState : NSOffState;
|
2001-11-06 18:35:24 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
|
|
|
|
2008-06-28 11:55:30 +04:00
|
|
|
EMenuItemType nsMenuItemX::GetMenuItemType() { return mType; }
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2007-08-31 12:08:03 +04:00
|
|
|
// Executes the "cached" javaScript command.
|
|
|
|
// Returns NS_OK if the command was executed properly, otherwise an error code.
|
2021-03-24 19:51:32 +03:00
|
|
|
void nsMenuItemX::DoCommand(NSEventModifierFlags aModifierFlags) {
|
2006-12-18 23:14:47 +03:00
|
|
|
// flip "checked" state if we're a checkbox menu, or an un-checked radio menu
|
2008-06-28 11:55:30 +04:00
|
|
|
if (mType == eCheckboxMenuItemType || (mType == eRadioMenuItemType && !mIsChecked)) {
|
2021-04-07 03:39:45 +03:00
|
|
|
if (!mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::autocheck,
|
2021-02-11 22:43:59 +03:00
|
|
|
nsGkAtoms::_false, eCaseMatters)) {
|
2006-12-18 23:14:47 +03:00
|
|
|
SetChecked(!mIsChecked);
|
2021-02-11 22:43:59 +03:00
|
|
|
}
|
2006-12-18 23:14:47 +03:00
|
|
|
/* the AttributeChanged code will update all the internal state */
|
|
|
|
}
|
|
|
|
|
2021-03-24 19:51:32 +03:00
|
|
|
nsMenuUtilsX::DispatchCommandTo(mContent, aModifierFlags);
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2006-03-15 02:58:57 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsMenuItemX::DispatchDOMEvent(const nsString& eventName, bool* preventDefaultCalled) {
|
2021-02-11 22:43:59 +03:00
|
|
|
if (!mContent) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2006-03-15 03:23:31 +03:00
|
|
|
// get owner document for content
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<dom::Document> parentDoc = mContent->OwnerDoc();
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2006-03-15 03:23:31 +03:00
|
|
|
// create DOM event
|
2018-01-31 23:18:11 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<Event> event = parentDoc->CreateEvent(u"Events"_ns, CallerType::System, rv);
|
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to create Event");
|
|
|
|
return rv.StealNSResult();
|
2006-03-15 19:52:31 +03:00
|
|
|
}
|
2011-10-01 04:20:33 +04:00
|
|
|
event->InitEvent(eventName, true, true);
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2006-03-15 03:23:31 +03:00
|
|
|
// mark DOM event as trusted
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2006-03-15 03:23:31 +03:00
|
|
|
// send DOM event
|
2018-04-05 20:42:41 +03:00
|
|
|
*preventDefaultCalled = mContent->DispatchEvent(*event, CallerType::System, rv);
|
|
|
|
if (rv.Failed()) {
|
2013-04-22 05:25:28 +04:00
|
|
|
NS_WARNING("Failed to send DOM event via EventTarget");
|
2018-04-05 20:42:41 +03:00
|
|
|
return rv.StealNSResult();
|
2006-03-15 03:23:31 +03:00
|
|
|
}
|
2007-08-31 12:08:03 +04:00
|
|
|
|
2013-04-20 02:18:32 +04:00
|
|
|
return NS_OK;
|
2008-06-27 10:52:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-31 12:08:03 +04:00
|
|
|
// Walk the sibling list looking for nodes with the same name and
|
2001-11-06 18:35:24 +03:00
|
|
|
// uncheck them all.
|
2021-03-04 04:24:55 +03:00
|
|
|
void nsMenuItemX::UncheckRadioSiblings(nsIContent* aCheckedContent) {
|
2001-11-06 18:35:24 +03:00
|
|
|
nsAutoString myGroupName;
|
2021-04-07 03:39:45 +03:00
|
|
|
aCheckedContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::name, myGroupName);
|
2021-02-11 22:43:59 +03:00
|
|
|
if (!myGroupName.Length()) { // no groupname, nothing to do
|
2001-11-06 18:35:24 +03:00
|
|
|
return;
|
2021-02-11 22:43:59 +03:00
|
|
|
}
|
2013-04-20 02:18:32 +04:00
|
|
|
|
2021-03-04 04:24:55 +03:00
|
|
|
nsCOMPtr<nsIContent> parent = aCheckedContent->GetParent();
|
2021-02-11 22:43:59 +03:00
|
|
|
if (!parent) {
|
|
|
|
return;
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
|
|
|
// loop over siblings
|
2018-01-09 13:44:54 +03:00
|
|
|
for (nsIContent* sibling = parent->GetFirstChild(); sibling;
|
|
|
|
sibling = sibling->GetNextSibling()) {
|
2021-03-04 04:24:55 +03:00
|
|
|
if (sibling != aCheckedContent && sibling->IsElement()) { // skip this node
|
2017-12-05 20:05:51 +03:00
|
|
|
// if the current sibling is in the same group, clear it
|
|
|
|
if (sibling->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name, myGroupName,
|
|
|
|
eCaseMatters)) {
|
|
|
|
sibling->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::checked, u"false"_ns, true);
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2016-07-22 11:56:13 +03:00
|
|
|
}
|
2007-08-31 12:08:03 +04:00
|
|
|
}
|
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2011-02-13 15:28:00 +03:00
|
|
|
void nsMenuItemX::SetKeyEquiv() {
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2011-02-13 15:28:00 +03:00
|
|
|
// Set key shortcut and modifiers
|
|
|
|
nsAutoString keyValue;
|
2021-04-07 03:39:45 +03:00
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::key, keyValue);
|
2017-12-07 21:13:50 +03:00
|
|
|
|
2016-03-31 14:46:32 +03:00
|
|
|
if (!keyValue.IsEmpty() && mContent->GetUncomposedDoc()) {
|
2021-02-11 22:43:59 +03:00
|
|
|
dom::Element* keyContent = mContent->GetUncomposedDoc()->GetElementById(keyValue);
|
2011-02-13 15:28:00 +03:00
|
|
|
if (keyContent) {
|
2010-01-22 22:24:00 +03:00
|
|
|
nsAutoString keyChar;
|
|
|
|
bool hasKey = keyContent->GetAttr(kNameSpaceID_None, nsGkAtoms::key, keyChar);
|
|
|
|
|
|
|
|
if (!hasKey || keyChar.IsEmpty()) {
|
|
|
|
nsAutoString keyCodeName;
|
|
|
|
keyContent->GetAttr(kNameSpaceID_None, nsGkAtoms::keycode, keyCodeName);
|
2013-07-10 18:11:07 +04:00
|
|
|
uint32_t charCode = nsCocoaUtils::ConvertGeckoNameToMacCharCode(keyCodeName);
|
|
|
|
if (charCode) {
|
|
|
|
keyChar.Assign(charCode);
|
2010-01-22 22:24:00 +03:00
|
|
|
} else {
|
2017-09-06 11:43:13 +03:00
|
|
|
keyChar.AssignLiteral(u" ");
|
2010-01-22 22:24:00 +03:00
|
|
|
}
|
|
|
|
}
|
2011-02-13 15:28:00 +03:00
|
|
|
|
|
|
|
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 modifiers = nsMenuUtilsX::GeckoModifiersForNodeAttribute(modifiersStr);
|
2011-02-13 15:28:00 +03:00
|
|
|
|
|
|
|
unsigned int macModifiers = nsMenuUtilsX::MacModifiersForGeckoModifiers(modifiers);
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.keyEquivalentModifierMask = macModifiers;
|
2011-02-13 15:28:00 +03:00
|
|
|
|
|
|
|
NSString* keyEquivalent = [[NSString stringWithCharacters:(unichar*)keyChar.get()
|
|
|
|
length:keyChar.Length()] lowercaseString];
|
2021-02-11 22:43:59 +03:00
|
|
|
if ([keyEquivalent isEqualToString:@" "]) {
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.keyEquivalent = @"";
|
2021-02-11 22:43:59 +03:00
|
|
|
} else {
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.keyEquivalent = keyEquivalent;
|
2021-02-11 22:43:59 +03:00
|
|
|
}
|
2011-02-13 15:28:00 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2008-01-29 09:11:06 +03:00
|
|
|
|
2011-02-13 15:28:00 +03:00
|
|
|
// if the key was removed, clear the key
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.keyEquivalent = @"";
|
2008-01-29 09:11:06 +03:00
|
|
|
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2008-01-29 09:11:06 +03:00
|
|
|
}
|
|
|
|
|
2021-03-18 18:37:38 +03:00
|
|
|
void nsMenuItemX::Dump(uint32_t aIndent) const {
|
|
|
|
printf("%*s - item [%p] %-16s <%s>\n", aIndent * 2, "", this,
|
|
|
|
mType == eSeparatorMenuItemType ? "----" : [mNativeMenuItem.title UTF8String],
|
|
|
|
NS_ConvertUTF16toUTF8(mContent->NodeName()).get());
|
|
|
|
}
|
|
|
|
|
2001-11-06 18:35:24 +03:00
|
|
|
//
|
2008-01-23 07:04:15 +03:00
|
|
|
// nsChangeObserver
|
2001-11-06 18:35:24 +03:00
|
|
|
//
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void nsMenuItemX::ObserveAttributeChanged(dom::Document* aDocument, nsIContent* aContent,
|
|
|
|
nsAtom* aAttribute) {
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2021-02-11 22:43:59 +03:00
|
|
|
if (!aContent) {
|
|
|
|
return;
|
|
|
|
}
|
2016-07-22 11:56:13 +03:00
|
|
|
|
2006-06-15 19:54:58 +04:00
|
|
|
if (aContent == mContent) { // our own content node changed
|
2011-10-14 22:11:22 +04:00
|
|
|
if (aAttribute == nsGkAtoms::checked) {
|
2006-06-15 19:54:58 +04:00
|
|
|
// if we're a radio menu, uncheck our sibling radio items. No need to
|
|
|
|
// do any of this if we're just a normal check menu.
|
2021-04-07 03:39:45 +03:00
|
|
|
if (mType == eRadioMenuItemType &&
|
2017-12-07 21:13:50 +03:00
|
|
|
mContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::checked,
|
|
|
|
nsGkAtoms::_true, eCaseMatters)) {
|
|
|
|
UncheckRadioSiblings(mContent);
|
2006-06-15 19:54:58 +04:00
|
|
|
}
|
2011-10-01 04:20:33 +04:00
|
|
|
mMenuParent->SetRebuild(true);
|
2021-03-19 03:15:33 +03:00
|
|
|
} else if (aAttribute == nsGkAtoms::hidden || aAttribute == nsGkAtoms::collapsed) {
|
2021-04-20 02:08:33 +03:00
|
|
|
bool isVisible = !nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
|
|
|
|
if (isVisible != mIsVisible) {
|
2021-04-20 02:08:34 +03:00
|
|
|
mIsVisible = isVisible;
|
2021-04-20 02:08:33 +03:00
|
|
|
RefPtr<nsMenuItemX> self = this;
|
|
|
|
mMenuParent->MenuChildChangedVisibility(nsMenuParentX::MenuChild(self), isVisible);
|
2021-04-20 02:08:34 +03:00
|
|
|
if (mIsVisible) {
|
|
|
|
SetupIcon();
|
|
|
|
}
|
2021-04-20 02:08:33 +03:00
|
|
|
}
|
2011-10-01 04:20:33 +04:00
|
|
|
mMenuParent->SetRebuild(true);
|
2021-03-19 03:15:33 +03:00
|
|
|
} else if (aAttribute == nsGkAtoms::label) {
|
|
|
|
if (mType != eSeparatorMenuItemType) {
|
|
|
|
nsAutoString newLabel;
|
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::label, newLabel);
|
|
|
|
mNativeMenuItem.title = nsMenuUtilsX::GetTruncatedCocoaLabel(newLabel);
|
|
|
|
}
|
2011-10-14 22:11:22 +04:00
|
|
|
} else if (aAttribute == nsGkAtoms::key) {
|
2011-02-13 15:28:00 +03:00
|
|
|
SetKeyEquiv();
|
2011-10-14 22:11:22 +04:00
|
|
|
} else if (aAttribute == nsGkAtoms::image) {
|
2007-04-22 05:42:17 +04:00
|
|
|
SetupIcon();
|
2011-10-14 22:11:22 +04:00
|
|
|
} else if (aAttribute == nsGkAtoms::disabled) {
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.enabled = !aContent->AsElement()->AttrValueIs(
|
|
|
|
kNameSpaceID_None, nsGkAtoms::disabled, nsGkAtoms::_true, eCaseMatters);
|
2006-10-11 19:53:24 +04:00
|
|
|
}
|
2017-12-05 20:05:51 +03:00
|
|
|
} else if (aContent == mCommandElement) {
|
2006-06-15 19:54:58 +04:00
|
|
|
// the only thing that really matters when the menu isn't showing is the
|
|
|
|
// enabled state since it enables/disables keyboard commands
|
2011-10-14 22:11:22 +04:00
|
|
|
if (aAttribute == nsGkAtoms::disabled) {
|
2006-06-15 19:54:58 +04:00
|
|
|
// first we sync our menu item DOM node with the command DOM node
|
|
|
|
nsAutoString commandDisabled;
|
|
|
|
nsAutoString menuDisabled;
|
2017-12-05 20:05:51 +03:00
|
|
|
aContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::disabled, commandDisabled);
|
|
|
|
mContent->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::disabled, menuDisabled);
|
2006-06-15 19:54:58 +04:00
|
|
|
if (!commandDisabled.Equals(menuDisabled)) {
|
|
|
|
// The menu's disabled state needs to be updated to match the command.
|
2021-02-11 22:43:59 +03:00
|
|
|
if (commandDisabled.IsEmpty()) {
|
2017-12-05 20:05:51 +03:00
|
|
|
mContent->AsElement()->UnsetAttr(kNameSpaceID_None, nsGkAtoms::disabled, true);
|
2021-02-11 22:43:59 +03:00
|
|
|
} else {
|
2017-12-05 20:05:51 +03:00
|
|
|
mContent->AsElement()->SetAttr(kNameSpaceID_None, nsGkAtoms::disabled, commandDisabled,
|
|
|
|
true);
|
2021-02-11 22:43:59 +03:00
|
|
|
}
|
2006-06-15 19:54:58 +04:00
|
|
|
}
|
|
|
|
// now we sync our native menu item with the command DOM node
|
2021-03-04 04:24:55 +03:00
|
|
|
mNativeMenuItem.enabled = !aContent->AsElement()->AttrValueIs(
|
|
|
|
kNameSpaceID_None, nsGkAtoms::disabled, nsGkAtoms::_true, eCaseMatters);
|
2006-06-15 19:54:58 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
}
|
2008-02-21 02:47:05 +03:00
|
|
|
|
2021-02-18 02:07:13 +03:00
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK;
|
2007-08-31 12:08:03 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2019-09-16 08:03:32 +03:00
|
|
|
bool IsMenuStructureElement(nsIContent* aContent) {
|
|
|
|
return aContent->IsAnyOfXULElements(nsGkAtoms::menu, nsGkAtoms::menuitem,
|
|
|
|
nsGkAtoms::menuseparator);
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void nsMenuItemX::ObserveContentRemoved(dom::Document* aDocument, nsIContent* aContainer,
|
2017-07-27 16:49:52 +03:00
|
|
|
nsIContent* aChild, nsIContent* aPreviousSibling) {
|
2021-03-22 22:55:45 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mMenuGroupOwner);
|
|
|
|
MOZ_RELEASE_ASSERT(mMenuParent);
|
|
|
|
|
2017-12-05 20:05:51 +03:00
|
|
|
if (aChild == mCommandElement) {
|
|
|
|
mMenuGroupOwner->UnregisterForContentChanges(mCommandElement);
|
|
|
|
mCommandElement = nullptr;
|
2006-06-15 19:54:58 +04:00
|
|
|
}
|
2019-09-16 08:03:32 +03:00
|
|
|
if (IsMenuStructureElement(aChild)) {
|
|
|
|
mMenuParent->SetRebuild(true);
|
|
|
|
}
|
2007-08-31 12:08:03 +04:00
|
|
|
}
|
2001-11-06 18:35:24 +03:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void nsMenuItemX::ObserveContentInserted(dom::Document* aDocument, nsIContent* aContainer,
|
2011-06-03 02:37:18 +04:00
|
|
|
nsIContent* aChild) {
|
2021-03-22 22:55:45 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mMenuParent);
|
|
|
|
|
2019-09-16 08:03:32 +03:00
|
|
|
// The child node could come from the custom element that is for display, so
|
|
|
|
// only rebuild the menu if the child is related to the structure of the
|
|
|
|
// menu.
|
|
|
|
if (IsMenuStructureElement(aChild)) {
|
|
|
|
mMenuParent->SetRebuild(true);
|
|
|
|
}
|
2007-08-31 12:08:03 +04:00
|
|
|
}
|
2006-08-01 05:25:43 +04:00
|
|
|
|
2021-03-18 05:30:35 +03:00
|
|
|
void nsMenuItemX::SetupIcon() {
|
|
|
|
if (mType != eRegularMenuItemType) {
|
|
|
|
// Don't support icons on checkbox and radio menuitems, for consistency with Windows & Linux.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-18 05:30:35 +03:00
|
|
|
mIcon->SetupIcon(mContent);
|
2021-03-18 05:30:36 +03:00
|
|
|
mNativeMenuItem.image = mIcon->GetIconImage();
|
2021-03-18 05:30:35 +03:00
|
|
|
}
|
2021-03-18 05:30:36 +03:00
|
|
|
|
|
|
|
void nsMenuItemX::IconUpdated() { mNativeMenuItem.image = mIcon->GetIconImage(); }
|