2008-06-28 11:55:30 +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/. */
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2014-03-05 04:37:43 +04:00
|
|
|
#include "mozilla/dom/Event.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsMenuUtilsX.h"
|
|
|
|
#include "nsMenuBarX.h"
|
2008-07-28 08:46:33 +04:00
|
|
|
#include "nsMenuX.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsMenuItemX.h"
|
2010-02-08 06:38:14 +03:00
|
|
|
#include "nsStandaloneNativeMenu.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
#include "nsObjCExceptions.h"
|
|
|
|
#include "nsCocoaUtils.h"
|
|
|
|
#include "nsCocoaWindow.h"
|
2011-10-14 22:11:22 +04:00
|
|
|
#include "nsGkAtoms.h"
|
2018-03-19 22:50:37 +03:00
|
|
|
#include "nsGlobalWindowInner.h"
|
2009-06-30 11:56:40 +04:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2015-04-15 19:47:03 +03:00
|
|
|
#include "nsQueryObject.h"
|
2018-03-19 22:50:37 +03:00
|
|
|
#include "mozilla/dom/XULCommandEvent.h"
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2013-04-20 02:18:32 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2009-06-30 11:56:40 +04:00
|
|
|
void nsMenuUtilsX::DispatchCommandTo(nsIContent* aTargetContent)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aTargetContent, "null ptr");
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aTargetContent->OwnerDoc();
|
2013-04-20 02:18:32 +04:00
|
|
|
if (doc) {
|
2018-03-19 22:50:37 +03:00
|
|
|
RefPtr<dom::XULCommandEvent> event =
|
|
|
|
new dom::XULCommandEvent(doc, doc->GetPresContext(), nullptr);
|
2008-06-28 11:55:30 +04:00
|
|
|
|
2018-03-19 22:50:37 +03:00
|
|
|
IgnoredErrorResult rv;
|
|
|
|
event->InitCommandEvent(NS_LITERAL_STRING("command"),
|
|
|
|
true, true,
|
|
|
|
nsGlobalWindowInner::Cast(doc->GetInnerWindow()),
|
|
|
|
0, false, false, false,
|
|
|
|
false, nullptr, 0, rv);
|
2009-06-30 11:56:40 +04:00
|
|
|
// FIXME: Should probably figure out how to init this with the actual
|
|
|
|
// pressed keys, but this is a big old edge case anyway. -dwh
|
2018-03-19 22:50:37 +03:00
|
|
|
if (!rv.Failed()) {
|
2012-06-11 03:44:50 +04:00
|
|
|
event->SetTrusted(true);
|
2018-04-05 20:42:41 +03:00
|
|
|
aTargetContent->DispatchEvent(*event);
|
2009-06-30 11:56:40 +04:00
|
|
|
}
|
|
|
|
}
|
2008-06-28 11:55:30 +04:00
|
|
|
}
|
|
|
|
|
2009-04-30 10:48:14 +04:00
|
|
|
NSString* nsMenuUtilsX::GetTruncatedCocoaLabel(const nsString& itemLabel)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2011-03-23 21:46:57 +03:00
|
|
|
// We want to truncate long strings to some reasonable pixel length but there is no
|
|
|
|
// good API for doing that which works for all OS versions and architectures. For now
|
|
|
|
// we'll do nothing for consistency and depend on good user interface design to limit
|
|
|
|
// string lengths.
|
2013-10-10 20:59:40 +04:00
|
|
|
return [NSString stringWithCharacters:reinterpret_cast<const unichar*>(itemLabel.get())
|
|
|
|
length:itemLabel.Length()];
|
2008-06-28 11:55:30 +04:00
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t nsMenuUtilsX::GeckoModifiersForNodeAttribute(const nsString& modifiersAttribute)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t modifiers = knsMenuItemNoModifier;
|
2008-06-28 11:55:30 +04:00
|
|
|
char* str = ToNewCString(modifiersAttribute);
|
|
|
|
char* newStr;
|
|
|
|
char* token = strtok_r(str, ", \t", &newStr);
|
|
|
|
while (token != NULL) {
|
|
|
|
if (strcmp(token, "shift") == 0)
|
|
|
|
modifiers |= knsMenuItemShiftModifier;
|
|
|
|
else if (strcmp(token, "alt") == 0)
|
|
|
|
modifiers |= knsMenuItemAltModifier;
|
|
|
|
else if (strcmp(token, "control") == 0)
|
|
|
|
modifiers |= knsMenuItemControlModifier;
|
|
|
|
else if ((strcmp(token, "accel") == 0) ||
|
|
|
|
(strcmp(token, "meta") == 0)) {
|
|
|
|
modifiers |= knsMenuItemCommandModifier;
|
|
|
|
}
|
|
|
|
token = strtok_r(newStr, ", \t", &newStr);
|
|
|
|
}
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return modifiers;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
unsigned int nsMenuUtilsX::MacModifiersForGeckoModifiers(uint8_t geckoModifiers)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
|
|
|
unsigned int macModifiers = 0;
|
|
|
|
|
|
|
|
if (geckoModifiers & knsMenuItemShiftModifier)
|
|
|
|
macModifiers |= NSShiftKeyMask;
|
|
|
|
if (geckoModifiers & knsMenuItemAltModifier)
|
|
|
|
macModifiers |= NSAlternateKeyMask;
|
|
|
|
if (geckoModifiers & knsMenuItemControlModifier)
|
|
|
|
macModifiers |= NSControlKeyMask;
|
|
|
|
if (geckoModifiers & knsMenuItemCommandModifier)
|
|
|
|
macModifiers |= NSCommandKeyMask;
|
|
|
|
|
|
|
|
return macModifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMenuBarX* nsMenuUtilsX::GetHiddenWindowMenuBar()
|
|
|
|
{
|
|
|
|
nsIWidget* hiddenWindowWidgetNoCOMPtr = nsCocoaUtils::GetHiddenWindowWidget();
|
|
|
|
if (hiddenWindowWidgetNoCOMPtr)
|
|
|
|
return static_cast<nsCocoaWindow*>(hiddenWindowWidgetNoCOMPtr)->GetMenuBar();
|
|
|
|
else
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-06-28 11:55:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// It would be nice if we could localize these edit menu names.
|
|
|
|
NSMenuItem* nsMenuUtilsX::GetStandardEditMenuItem()
|
|
|
|
{
|
|
|
|
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
|
|
|
|
|
2009-01-23 23:46:35 +03:00
|
|
|
// In principle we should be able to allocate this once and then always
|
2016-02-17 17:57:01 +03:00
|
|
|
// return the same object. But weird interactions happen between native
|
2009-01-23 23:46:35 +03:00
|
|
|
// app-modal dialogs and Gecko-modal dialogs that open above them. So what
|
|
|
|
// we return here isn't always released before it needs to be added to
|
|
|
|
// another menu. See bmo bug 468393.
|
|
|
|
NSMenuItem* standardEditMenuItem =
|
|
|
|
[[[NSMenuItem alloc] initWithTitle:@"Edit" action:nil keyEquivalent:@""] autorelease];
|
2008-06-28 11:55:30 +04:00
|
|
|
NSMenu* standardEditMenu = [[NSMenu alloc] initWithTitle:@"Edit"];
|
|
|
|
[standardEditMenuItem setSubmenu:standardEditMenu];
|
|
|
|
[standardEditMenu release];
|
|
|
|
|
|
|
|
// Add Undo
|
|
|
|
NSMenuItem* undoItem = [[NSMenuItem alloc] initWithTitle:@"Undo" action:@selector(undo:) keyEquivalent:@"z"];
|
|
|
|
[standardEditMenu addItem:undoItem];
|
|
|
|
[undoItem release];
|
|
|
|
|
|
|
|
// Add Redo
|
|
|
|
NSMenuItem* redoItem = [[NSMenuItem alloc] initWithTitle:@"Redo" action:@selector(redo:) keyEquivalent:@"Z"];
|
|
|
|
[standardEditMenu addItem:redoItem];
|
|
|
|
[redoItem release];
|
|
|
|
|
|
|
|
// Add separator
|
|
|
|
[standardEditMenu addItem:[NSMenuItem separatorItem]];
|
|
|
|
|
|
|
|
// Add Cut
|
|
|
|
NSMenuItem* cutItem = [[NSMenuItem alloc] initWithTitle:@"Cut" action:@selector(cut:) keyEquivalent:@"x"];
|
|
|
|
[standardEditMenu addItem:cutItem];
|
|
|
|
[cutItem release];
|
|
|
|
|
|
|
|
// Add Copy
|
|
|
|
NSMenuItem* copyItem = [[NSMenuItem alloc] initWithTitle:@"Copy" action:@selector(copy:) keyEquivalent:@"c"];
|
|
|
|
[standardEditMenu addItem:copyItem];
|
|
|
|
[copyItem release];
|
|
|
|
|
|
|
|
// Add Paste
|
|
|
|
NSMenuItem* pasteItem = [[NSMenuItem alloc] initWithTitle:@"Paste" action:@selector(paste:) keyEquivalent:@"v"];
|
|
|
|
[standardEditMenu addItem:pasteItem];
|
|
|
|
[pasteItem release];
|
|
|
|
|
|
|
|
// Add Delete
|
|
|
|
NSMenuItem* deleteItem = [[NSMenuItem alloc] initWithTitle:@"Delete" action:@selector(delete:) keyEquivalent:@""];
|
|
|
|
[standardEditMenu addItem:deleteItem];
|
|
|
|
[deleteItem release];
|
|
|
|
|
|
|
|
// Add Select All
|
|
|
|
NSMenuItem* selectAllItem = [[NSMenuItem alloc] initWithTitle:@"Select All" action:@selector(selectAll:) keyEquivalent:@"a"];
|
|
|
|
[standardEditMenu addItem:selectAllItem];
|
|
|
|
[selectAllItem release];
|
|
|
|
|
|
|
|
return standardEditMenuItem;
|
|
|
|
|
|
|
|
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsMenuUtilsX::NodeIsHiddenOrCollapsed(nsIContent* inContent)
|
2008-06-28 11:55:30 +04:00
|
|
|
{
|
2017-12-07 21:13:50 +03:00
|
|
|
return inContent->IsElement() &&
|
|
|
|
(inContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::hidden,
|
|
|
|
nsGkAtoms::_true, eCaseMatters) ||
|
|
|
|
inContent->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::collapsed,
|
|
|
|
nsGkAtoms::_true, eCaseMatters));
|
2008-06-28 11:55:30 +04:00
|
|
|
}
|
2008-07-28 08:46:33 +04:00
|
|
|
|
|
|
|
// Determines how many items are visible among the siblings in a menu that are
|
2009-01-23 18:45:03 +03:00
|
|
|
// before the given child. This will not count the application menu.
|
|
|
|
int nsMenuUtilsX::CalculateNativeInsertionPoint(nsMenuObjectX* aParent,
|
|
|
|
nsMenuObjectX* aChild)
|
2008-07-28 08:46:33 +04:00
|
|
|
{
|
2009-01-23 18:45:03 +03:00
|
|
|
int insertionPoint = 0;
|
|
|
|
nsMenuObjectTypeX parentType = aParent->MenuObjectType();
|
2008-07-28 08:46:33 +04:00
|
|
|
if (parentType == eMenuBarObjectType) {
|
2009-01-23 18:45:03 +03:00
|
|
|
nsMenuBarX* menubarParent = static_cast<nsMenuBarX*>(aParent);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numMenus = menubarParent->GetMenuCount();
|
|
|
|
for (uint32_t i = 0; i < numMenus; i++) {
|
2008-07-28 08:46:33 +04:00
|
|
|
nsMenuX* currMenu = menubarParent->GetMenuAt(i);
|
|
|
|
if (currMenu == aChild)
|
2009-01-23 18:45:03 +03:00
|
|
|
return insertionPoint; // we found ourselves, break out
|
|
|
|
if (currMenu && [currMenu->NativeMenuItem() menu])
|
|
|
|
insertionPoint++;
|
2008-07-28 08:46:33 +04:00
|
|
|
}
|
|
|
|
}
|
2010-02-08 06:38:14 +03:00
|
|
|
else if (parentType == eSubmenuObjectType ||
|
|
|
|
parentType == eStandaloneNativeMenuObjectType) {
|
|
|
|
nsMenuX* menuParent;
|
|
|
|
if (parentType == eSubmenuObjectType)
|
|
|
|
menuParent = static_cast<nsMenuX*>(aParent);
|
|
|
|
else
|
|
|
|
menuParent = static_cast<nsStandaloneNativeMenu*>(aParent)->GetMenuXObject();
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numItems = menuParent->GetItemCount();
|
|
|
|
for (uint32_t i = 0; i < numItems; i++) {
|
2008-07-28 08:46:33 +04:00
|
|
|
// Using GetItemAt instead of GetVisibleItemAt to avoid O(N^2)
|
|
|
|
nsMenuObjectX* currItem = menuParent->GetItemAt(i);
|
|
|
|
if (currItem == aChild)
|
2009-01-23 18:45:03 +03:00
|
|
|
return insertionPoint; // we found ourselves, break out
|
|
|
|
NSMenuItem* nativeItem = nil;
|
|
|
|
nsMenuObjectTypeX currItemType = currItem->MenuObjectType();
|
|
|
|
if (currItemType == eSubmenuObjectType)
|
|
|
|
nativeItem = static_cast<nsMenuX*>(currItem)->NativeMenuItem();
|
|
|
|
else
|
|
|
|
nativeItem = (NSMenuItem*)(currItem->NativeData());
|
|
|
|
if ([nativeItem menu])
|
|
|
|
insertionPoint++;
|
2008-07-28 08:46:33 +04:00
|
|
|
}
|
|
|
|
}
|
2009-01-23 18:45:03 +03:00
|
|
|
return insertionPoint;
|
2008-07-28 08:46:33 +04:00
|
|
|
}
|