gecko-dev/layout/xul/nsMenuBarFrame.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

356 строки
10 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
1999-07-18 11:07:55 +04:00
#include "nsMenuBarFrame.h"
#include "nsIContent.h"
#include "nsAtom.h"
#include "nsPresContext.h"
1999-07-18 11:07:55 +04:00
#include "nsCSSRendering.h"
#include "nsNameSpaceManager.h"
#include "nsGkAtoms.h"
#include "nsMenuFrame.h"
#include "nsMenuPopupFrame.h"
#include "nsUnicharUtils.h"
#include "nsPIDOMWindow.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsCSSFrameConstructor.h"
#ifdef XP_WIN
# include "nsISound.h"
# include "nsWidgetsCID.h"
#endif
#include "nsUTF8Utils.h"
#include "mozilla/ComputedStyle.h"
#include "mozilla/PresShell.h"
#include "mozilla/TextEvents.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Event.h"
#include "mozilla/dom/KeyboardEvent.h"
1999-07-19 13:09:36 +04:00
using namespace mozilla;
using mozilla::dom::KeyboardEvent;
1999-09-21 05:03:00 +04:00
1999-07-18 11:07:55 +04:00
//
// NS_NewMenuBarFrame
//
// Wrapper for creating a new menu Bar container
//
nsIFrame* NS_NewMenuBarFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
return new (aPresShell) nsMenuBarFrame(aStyle, aPresShell->GetPresContext());
1999-07-18 11:07:55 +04:00
}
NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame)
NS_QUERYFRAME_HEAD(nsMenuBarFrame)
NS_QUERYFRAME_ENTRY(nsMenuBarFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
1999-07-18 11:07:55 +04:00
//
// nsMenuBarFrame cntr
//
nsMenuBarFrame::nsMenuBarFrame(ComputedStyle* aStyle,
nsPresContext* aPresContext)
: nsBoxFrame(aStyle, aPresContext, kClassID),
mStayActive(false),
mIsActive(false),
mActiveByKeyboard(false),
mCurrentMenu(nullptr) {} // cntr
void nsMenuBarFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
nsIFrame* aPrevInFlow) {
nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
1999-07-19 06:02:38 +04:00
// Create the menu bar listener.
mMenuBarListener = new nsMenuBarListener(this, aContent);
1999-07-19 06:02:38 +04:00
}
1999-07-22 13:49:35 +04:00
NS_IMETHODIMP
nsMenuBarFrame::SetActive(bool aActiveFlag) {
// If the activity is not changed, there is nothing to do.
if (mIsActive == aActiveFlag) return NS_OK;
if (!aActiveFlag) {
// Don't deactivate when switching between menus on the menubar.
if (mStayActive) return NS_OK;
// if there is a request to deactivate the menu bar, check to see whether
// there is a menu popup open for the menu bar. In this case, don't
// deactivate the menu bar.
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm && pm->IsPopupOpenForMenuParent(this)) return NS_OK;
}
1999-07-23 12:36:39 +04:00
mIsActive = aActiveFlag;
if (mIsActive) {
InstallKeyboardNavigator();
} else {
mActiveByKeyboard = false;
RemoveKeyboardNavigator();
2000-02-18 11:57:28 +03:00
}
NS_NAMED_LITERAL_STRING(active, "DOMMenuBarActive");
NS_NAMED_LITERAL_STRING(inactive, "DOMMenuBarInactive");
FireDOMEvent(mIsActive ? active : inactive, mContent);
1999-07-22 13:49:35 +04:00
return NS_OK;
}
nsMenuFrame* nsMenuBarFrame::ToggleMenuActiveState() {
1999-07-21 11:42:16 +04:00
if (mIsActive) {
// Deactivate the menu bar
SetActive(false);
if (mCurrentMenu) {
nsMenuFrame* closeframe = mCurrentMenu;
closeframe->SelectMenu(false);
mCurrentMenu = nullptr;
return closeframe;
}
} else {
// if the menu bar is already selected (eg. mouseover), deselect it
if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
// Set the active menu to be the top left item (e.g., the File menu).
// We use an attribute called "menuactive" to track the current
// active menu.
nsMenuFrame* firstFrame =
nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
if (firstFrame) {
// Activate the menu bar
SetActive(true);
firstFrame->SelectMenu(true);
// Track this item for keyboard navigation.
mCurrentMenu = firstFrame;
}
}
return nullptr;
}
nsMenuFrame* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent* aKeyEvent,
bool aPeek) {
uint32_t charCode = aKeyEvent->CharCode();
AutoTArray<uint32_t, 10> accessKeys;
WidgetKeyboardEvent* nativeKeyEvent =
aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
if (nativeKeyEvent) {
nativeKeyEvent->GetAccessKeyCandidates(accessKeys);
}
if (accessKeys.IsEmpty() && charCode) accessKeys.AppendElement(charCode);
if (accessKeys.IsEmpty())
return nullptr; // no character was pressed so just return
// Enumerate over our list of frames.
Bug 1427677: Get rid of nsContentUtils::HasDistributedChildren. r=bz The whole function doesn't have much sense. I killed its only DOM use in bug 1427511. Now it only has two callers in nsCSSFrameConstructor, which basically only want to know whether the children of the same node can have different flattened tree parents. So let's check that directly instead (checking whether the element has a binding or a shadow root), and simplify a bit other surrounding code while at it. Leave the XUL popup / menubar code doing the broken thing they were doing beforehand, because it doesn't look to me like it's trivial to fix... They're effectively assuming that the children of the menupopup end up in a single insertion point, which is true, but doesn't need to be. Maybe they should walk the DOM tree? Don't want to dig into that right now, since XUL insertion points can be filtered and all that... Not fun. Also, this removes the broken optimization that used to check mParentFrame->GetContent()->HasChildren(), because it's pretty broken. It used to be relevant before bug 653881, because <children> element used to not exist, but now the insertion point at least needs to contain the <children> element all the time. There even used to be a XXX comment saying that the optimization didn't work, which was removed in: https://hg.mozilla.org/mozilla-central/rev/2d8585ec74b3 We could still check for "no insertion points", and optimize that, but it doesn't seem worth it. MozReview-Commit-ID: L4lspkxKENr
2018-01-02 17:04:38 +03:00
nsContainerFrame* immediateParent =
nsXULPopupManager::ImmediateParentFrame(this);
// Find a most preferred accesskey which should be returned.
nsIFrame* foundMenu = nullptr;
size_t foundIndex = accessKeys.NoIndex;
nsIFrame* currFrame = immediateParent->PrincipalChildList().FirstChild();
while (currFrame) {
nsIContent* current = currFrame->GetContent();
// See if it's a menu item.
if (nsXULPopupManager::IsValidMenuItem(current, false)) {
// Get the shortcut attribute.
nsAutoString shortcutKey;
if (current->IsElement()) {
current->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey,
shortcutKey);
}
if (!shortcutKey.IsEmpty()) {
ToLowerCase(shortcutKey);
const char16_t* start = shortcutKey.BeginReading();
const char16_t* end = shortcutKey.EndReading();
uint32_t ch = UTF16CharEnumerator::NextChar(&start, end);
size_t index = accessKeys.IndexOf(ch);
if (index != accessKeys.NoIndex &&
(foundIndex == accessKeys.NoIndex || index < foundIndex)) {
foundMenu = currFrame;
foundIndex = index;
}
}
}
currFrame = currFrame->GetNextSibling();
}
if (foundMenu) {
return do_QueryFrame(foundMenu);
}
// didn't find a matching menu item
#ifdef XP_WIN
if (!aPeek) {
// behavior on Windows - this item is on the menu bar, beep and deactivate
// the menu bar
if (mIsActive) {
nsCOMPtr<nsISound> soundInterface =
do_CreateInstance("@mozilla.org/sound;1");
if (soundInterface) soundInterface->Beep();
}
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) {
nsIFrame* popup = pm->GetTopPopup(ePopupTypeMenu);
if (popup) pm->HidePopup(popup->GetContent(), true, true, true, false);
}
SetCurrentMenuItem(nullptr);
SetActive(false);
}
#endif // #ifdef XP_WIN
return nullptr;
}
/* virtual */
nsMenuFrame* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu; }
NS_IMETHODIMP
nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame* aMenuItem) {
if (mCurrentMenu == aMenuItem) return NS_OK;
if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
if (aMenuItem) aMenuItem->SelectMenu(true);
mCurrentMenu = aMenuItem;
return NS_OK;
}
void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
mCurrentMenu->SelectMenu(false);
mCurrentMenu = nullptr;
}
class nsMenuBarSwitchMenu : public Runnable {
public:
nsMenuBarSwitchMenu(nsIContent* aMenuBar, nsIContent* aOldMenu,
nsIContent* aNewMenu, bool aSelectFirstItem)
: mozilla::Runnable("nsMenuBarSwitchMenu"),
mMenuBar(aMenuBar),
mOldMenu(aOldMenu),
mNewMenu(aNewMenu),
mSelectFirstItem(aSelectFirstItem) {}
NS_IMETHOD Run() override {
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (!pm) return NS_ERROR_UNEXPECTED;
// if switching from one menu to another, set a flag so that the call to
// HidePopup doesn't deactivate the menubar when the first menu closes.
nsMenuBarFrame* menubar = nullptr;
if (mOldMenu && mNewMenu) {
menubar = do_QueryFrame(mMenuBar->GetPrimaryFrame());
if (menubar) menubar->SetStayActive(true);
}
if (mOldMenu) {
AutoWeakFrame weakMenuBar(menubar);
pm->HidePopup(mOldMenu, false, false, false, false);
// clear the flag again
if (mNewMenu && weakMenuBar.IsAlive()) menubar->SetStayActive(false);
}
if (mNewMenu) pm->ShowMenu(mNewMenu, mSelectFirstItem, false);
return NS_OK;
}
private:
nsCOMPtr<nsIContent> mMenuBar;
nsCOMPtr<nsIContent> mOldMenu;
nsCOMPtr<nsIContent> mNewMenu;
bool mSelectFirstItem;
};
NS_IMETHODIMP
nsMenuBarFrame::ChangeMenuItem(nsMenuFrame* aMenuItem, bool aSelectFirstItem,
bool aFromKey) {
1999-07-19 13:09:36 +04:00
if (mCurrentMenu == aMenuItem) return NS_OK;
// check if there's an open context menu, we ignore this
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm && pm->HasContextMenu(nullptr)) return NS_OK;
nsIContent* aOldMenu = nullptr;
nsIContent* aNewMenu = nullptr;
// Unset the current child.
bool wasOpen = false;
if (mCurrentMenu) {
wasOpen = mCurrentMenu->IsOpen();
mCurrentMenu->SelectMenu(false);
if (wasOpen) {
nsMenuPopupFrame* popupFrame = mCurrentMenu->GetPopup();
if (popupFrame) aOldMenu = popupFrame->GetContent();
}
}
// set to null first in case the IsAlive check below returns false
mCurrentMenu = nullptr;
1999-07-19 13:09:36 +04:00
// Set the new child.
if (aMenuItem) {
nsCOMPtr<nsIContent> content = aMenuItem->GetContent();
aMenuItem->SelectMenu(true);
mCurrentMenu = aMenuItem;
if (wasOpen && !aMenuItem->IsDisabled()) aNewMenu = content;
}
// use an event so that hiding and showing can be done synchronously, which
// avoids flickering
nsCOMPtr<nsIRunnable> event = new nsMenuBarSwitchMenu(
GetContent(), aOldMenu, aNewMenu, aSelectFirstItem);
return mContent->OwnerDoc()->Dispatch(TaskCategory::Other, event.forget());
}
nsMenuFrame* nsMenuBarFrame::Enter(WidgetGUIEvent* aEvent) {
if (!mCurrentMenu) return nullptr;
if (mCurrentMenu->IsOpen()) return mCurrentMenu->Enter(aEvent);
return mCurrentMenu;
}
bool nsMenuBarFrame::MenuClosed() {
SetActive(false);
if (!mIsActive && mCurrentMenu) {
mCurrentMenu->SelectMenu(false);
mCurrentMenu = nullptr;
return true;
}
return false;
}
void nsMenuBarFrame::InstallKeyboardNavigator() {
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) pm->SetActiveMenuBar(this, true);
}
void nsMenuBarFrame::RemoveKeyboardNavigator() {
if (!mIsActive) {
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) pm->SetActiveMenuBar(this, false);
}
}
1999-08-22 11:44:00 +04:00
void nsMenuBarFrame::DestroyFrom(nsIFrame* aDestructRoot,
PostDestroyData& aPostDestroyData) {
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) pm->SetActiveMenuBar(this, false);
mMenuBarListener->OnDestroyMenuBarFrame();
mMenuBarListener = nullptr;
1999-08-22 11:44:00 +04:00
nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
1999-08-24 04:32:35 +04:00
}