Bug 1591736 - Fix AddonManagerWebAPI::IsAPIEnabled in out-of-process iframes r=mixedpuppy
[gecko.git] / layout / xul / nsMenuBarFrame.cpp
blob4da38e3d1187046a98a6094eb055e2a441c4087a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsMenuBarFrame.h"
8 #include "nsIServiceManager.h"
9 #include "nsIContent.h"
10 #include "nsAtom.h"
11 #include "nsPresContext.h"
12 #include "nsCSSRendering.h"
13 #include "nsNameSpaceManager.h"
14 #include "nsGkAtoms.h"
15 #include "nsMenuFrame.h"
16 #include "nsMenuPopupFrame.h"
17 #include "nsUnicharUtils.h"
18 #include "nsPIDOMWindow.h"
19 #include "nsIInterfaceRequestorUtils.h"
20 #include "nsCSSFrameConstructor.h"
21 #ifdef XP_WIN
22 # include "nsISound.h"
23 # include "nsWidgetsCID.h"
24 #endif
25 #include "nsUTF8Utils.h"
26 #include "mozilla/ComputedStyle.h"
27 #include "mozilla/PresShell.h"
28 #include "mozilla/TextEvents.h"
29 #include "mozilla/dom/Document.h"
30 #include "mozilla/dom/Event.h"
31 #include "mozilla/dom/KeyboardEvent.h"
33 using namespace mozilla;
34 using mozilla::dom::KeyboardEvent;
37 // NS_NewMenuBarFrame
39 // Wrapper for creating a new menu Bar container
41 nsIFrame* NS_NewMenuBarFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
42 return new (aPresShell) nsMenuBarFrame(aStyle, aPresShell->GetPresContext());
45 NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame)
47 NS_QUERYFRAME_HEAD(nsMenuBarFrame)
48 NS_QUERYFRAME_ENTRY(nsMenuBarFrame)
49 NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
52 // nsMenuBarFrame cntr
54 nsMenuBarFrame::nsMenuBarFrame(ComputedStyle* aStyle,
55 nsPresContext* aPresContext)
56 : nsBoxFrame(aStyle, aPresContext, kClassID),
57 mStayActive(false),
58 mIsActive(false),
59 mActiveByKeyboard(false),
60 mCurrentMenu(nullptr) {} // cntr
62 void nsMenuBarFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
63 nsIFrame* aPrevInFlow) {
64 nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
66 // Create the menu bar listener.
67 mMenuBarListener = new nsMenuBarListener(this, aContent);
70 NS_IMETHODIMP
71 nsMenuBarFrame::SetActive(bool aActiveFlag) {
72 // If the activity is not changed, there is nothing to do.
73 if (mIsActive == aActiveFlag) return NS_OK;
75 if (!aActiveFlag) {
76 // Don't deactivate when switching between menus on the menubar.
77 if (mStayActive) return NS_OK;
79 // if there is a request to deactivate the menu bar, check to see whether
80 // there is a menu popup open for the menu bar. In this case, don't
81 // deactivate the menu bar.
82 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
83 if (pm && pm->IsPopupOpenForMenuParent(this)) return NS_OK;
86 mIsActive = aActiveFlag;
87 if (mIsActive) {
88 InstallKeyboardNavigator();
89 } else {
90 mActiveByKeyboard = false;
91 RemoveKeyboardNavigator();
94 NS_NAMED_LITERAL_STRING(active, "DOMMenuBarActive");
95 NS_NAMED_LITERAL_STRING(inactive, "DOMMenuBarInactive");
97 FireDOMEvent(mIsActive ? active : inactive, mContent);
99 return NS_OK;
102 nsMenuFrame* nsMenuBarFrame::ToggleMenuActiveState() {
103 if (mIsActive) {
104 // Deactivate the menu bar
105 SetActive(false);
106 if (mCurrentMenu) {
107 nsMenuFrame* closeframe = mCurrentMenu;
108 closeframe->SelectMenu(false);
109 mCurrentMenu = nullptr;
110 return closeframe;
112 } else {
113 // if the menu bar is already selected (eg. mouseover), deselect it
114 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
116 // Set the active menu to be the top left item (e.g., the File menu).
117 // We use an attribute called "menuactive" to track the current
118 // active menu.
119 nsMenuFrame* firstFrame =
120 nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
121 if (firstFrame) {
122 // Activate the menu bar
123 SetActive(true);
124 firstFrame->SelectMenu(true);
126 // Track this item for keyboard navigation.
127 mCurrentMenu = firstFrame;
131 return nullptr;
134 nsMenuFrame* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent* aKeyEvent,
135 bool aPeek) {
136 uint32_t charCode = aKeyEvent->CharCode();
138 AutoTArray<uint32_t, 10> accessKeys;
139 WidgetKeyboardEvent* nativeKeyEvent =
140 aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
141 if (nativeKeyEvent) {
142 nativeKeyEvent->GetAccessKeyCandidates(accessKeys);
144 if (accessKeys.IsEmpty() && charCode) accessKeys.AppendElement(charCode);
146 if (accessKeys.IsEmpty())
147 return nullptr; // no character was pressed so just return
149 // Enumerate over our list of frames.
150 nsContainerFrame* immediateParent =
151 nsXULPopupManager::ImmediateParentFrame(this);
153 // Find a most preferred accesskey which should be returned.
154 nsIFrame* foundMenu = nullptr;
155 size_t foundIndex = accessKeys.NoIndex;
156 nsIFrame* currFrame = immediateParent->PrincipalChildList().FirstChild();
158 while (currFrame) {
159 nsIContent* current = currFrame->GetContent();
161 // See if it's a menu item.
162 if (nsXULPopupManager::IsValidMenuItem(current, false)) {
163 // Get the shortcut attribute.
164 nsAutoString shortcutKey;
165 if (current->IsElement()) {
166 current->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey,
167 shortcutKey);
169 if (!shortcutKey.IsEmpty()) {
170 ToLowerCase(shortcutKey);
171 const char16_t* start = shortcutKey.BeginReading();
172 const char16_t* end = shortcutKey.EndReading();
173 uint32_t ch = UTF16CharEnumerator::NextChar(&start, end);
174 size_t index = accessKeys.IndexOf(ch);
175 if (index != accessKeys.NoIndex &&
176 (foundIndex == accessKeys.NoIndex || index < foundIndex)) {
177 foundMenu = currFrame;
178 foundIndex = index;
182 currFrame = currFrame->GetNextSibling();
184 if (foundMenu) {
185 return do_QueryFrame(foundMenu);
188 // didn't find a matching menu item
189 #ifdef XP_WIN
190 if (!aPeek) {
191 // behavior on Windows - this item is on the menu bar, beep and deactivate
192 // the menu bar
193 if (mIsActive) {
194 nsCOMPtr<nsISound> soundInterface =
195 do_CreateInstance("@mozilla.org/sound;1");
196 if (soundInterface) soundInterface->Beep();
199 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
200 if (pm) {
201 nsIFrame* popup = pm->GetTopPopup(ePopupTypeMenu);
202 if (popup) pm->HidePopup(popup->GetContent(), true, true, true, false);
205 SetCurrentMenuItem(nullptr);
206 SetActive(false);
209 #endif // #ifdef XP_WIN
211 return nullptr;
214 /* virtual */
215 nsMenuFrame* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu; }
217 NS_IMETHODIMP
218 nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame* aMenuItem) {
219 if (mCurrentMenu == aMenuItem) return NS_OK;
221 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
223 if (aMenuItem) aMenuItem->SelectMenu(true);
225 mCurrentMenu = aMenuItem;
227 return NS_OK;
230 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
231 mCurrentMenu->SelectMenu(false);
232 mCurrentMenu = nullptr;
235 class nsMenuBarSwitchMenu : public Runnable {
236 public:
237 nsMenuBarSwitchMenu(nsIContent* aMenuBar, nsIContent* aOldMenu,
238 nsIContent* aNewMenu, bool aSelectFirstItem)
239 : mozilla::Runnable("nsMenuBarSwitchMenu"),
240 mMenuBar(aMenuBar),
241 mOldMenu(aOldMenu),
242 mNewMenu(aNewMenu),
243 mSelectFirstItem(aSelectFirstItem) {}
245 NS_IMETHOD Run() override {
246 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
247 if (!pm) return NS_ERROR_UNEXPECTED;
249 // if switching from one menu to another, set a flag so that the call to
250 // HidePopup doesn't deactivate the menubar when the first menu closes.
251 nsMenuBarFrame* menubar = nullptr;
252 if (mOldMenu && mNewMenu) {
253 menubar = do_QueryFrame(mMenuBar->GetPrimaryFrame());
254 if (menubar) menubar->SetStayActive(true);
257 if (mOldMenu) {
258 AutoWeakFrame weakMenuBar(menubar);
259 pm->HidePopup(mOldMenu, false, false, false, false);
260 // clear the flag again
261 if (mNewMenu && weakMenuBar.IsAlive()) menubar->SetStayActive(false);
264 if (mNewMenu) pm->ShowMenu(mNewMenu, mSelectFirstItem, false);
266 return NS_OK;
269 private:
270 nsCOMPtr<nsIContent> mMenuBar;
271 nsCOMPtr<nsIContent> mOldMenu;
272 nsCOMPtr<nsIContent> mNewMenu;
273 bool mSelectFirstItem;
276 NS_IMETHODIMP
277 nsMenuBarFrame::ChangeMenuItem(nsMenuFrame* aMenuItem, bool aSelectFirstItem,
278 bool aFromKey) {
279 if (mCurrentMenu == aMenuItem) return NS_OK;
281 // check if there's an open context menu, we ignore this
282 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
283 if (pm && pm->HasContextMenu(nullptr)) return NS_OK;
285 nsIContent* aOldMenu = nullptr;
286 nsIContent* aNewMenu = nullptr;
288 // Unset the current child.
289 bool wasOpen = false;
290 if (mCurrentMenu) {
291 wasOpen = mCurrentMenu->IsOpen();
292 mCurrentMenu->SelectMenu(false);
293 if (wasOpen) {
294 nsMenuPopupFrame* popupFrame = mCurrentMenu->GetPopup();
295 if (popupFrame) aOldMenu = popupFrame->GetContent();
299 // set to null first in case the IsAlive check below returns false
300 mCurrentMenu = nullptr;
302 // Set the new child.
303 if (aMenuItem) {
304 nsCOMPtr<nsIContent> content = aMenuItem->GetContent();
305 aMenuItem->SelectMenu(true);
306 mCurrentMenu = aMenuItem;
307 if (wasOpen && !aMenuItem->IsDisabled()) aNewMenu = content;
310 // use an event so that hiding and showing can be done synchronously, which
311 // avoids flickering
312 nsCOMPtr<nsIRunnable> event = new nsMenuBarSwitchMenu(
313 GetContent(), aOldMenu, aNewMenu, aSelectFirstItem);
314 return mContent->OwnerDoc()->Dispatch(TaskCategory::Other, event.forget());
317 nsMenuFrame* nsMenuBarFrame::Enter(WidgetGUIEvent* aEvent) {
318 if (!mCurrentMenu) return nullptr;
320 if (mCurrentMenu->IsOpen()) return mCurrentMenu->Enter(aEvent);
322 return mCurrentMenu;
325 bool nsMenuBarFrame::MenuClosed() {
326 SetActive(false);
327 if (!mIsActive && mCurrentMenu) {
328 mCurrentMenu->SelectMenu(false);
329 mCurrentMenu = nullptr;
330 return true;
332 return false;
335 void nsMenuBarFrame::InstallKeyboardNavigator() {
336 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
337 if (pm) pm->SetActiveMenuBar(this, true);
340 void nsMenuBarFrame::RemoveKeyboardNavigator() {
341 if (!mIsActive) {
342 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
343 if (pm) pm->SetActiveMenuBar(this, false);
347 void nsMenuBarFrame::DestroyFrom(nsIFrame* aDestructRoot,
348 PostDestroyData& aPostDestroyData) {
349 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
350 if (pm) pm->SetActiveMenuBar(this, false);
352 mMenuBarListener->OnDestroyMenuBarFrame();
353 mMenuBarListener = nullptr;
355 nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);