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 "nsIContent.h"
10 #include "nsPresContext.h"
11 #include "nsCSSRendering.h"
12 #include "nsNameSpaceManager.h"
13 #include "nsGkAtoms.h"
14 #include "nsMenuFrame.h"
15 #include "nsMenuPopupFrame.h"
16 #include "nsUnicharUtils.h"
17 #include "nsPIDOMWindow.h"
18 #include "nsIInterfaceRequestorUtils.h"
19 #include "nsCSSFrameConstructor.h"
21 # include "nsISound.h"
22 # include "nsWidgetsCID.h"
24 #include "nsUTF8Utils.h"
25 #include "mozilla/ComputedStyle.h"
26 #include "mozilla/PresShell.h"
27 #include "mozilla/TextEvents.h"
28 #include "mozilla/dom/Document.h"
29 #include "mozilla/dom/Event.h"
30 #include "mozilla/dom/KeyboardEvent.h"
32 using namespace mozilla
;
33 using mozilla::dom::KeyboardEvent
;
38 // Wrapper for creating a new menu Bar container
40 nsIFrame
* NS_NewMenuBarFrame(PresShell
* aPresShell
, ComputedStyle
* aStyle
) {
41 return new (aPresShell
) nsMenuBarFrame(aStyle
, aPresShell
->GetPresContext());
44 NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame
)
46 NS_QUERYFRAME_HEAD(nsMenuBarFrame
)
47 NS_QUERYFRAME_ENTRY(nsMenuBarFrame
)
48 NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame
)
51 // nsMenuBarFrame cntr
53 nsMenuBarFrame::nsMenuBarFrame(ComputedStyle
* aStyle
,
54 nsPresContext
* aPresContext
)
55 : nsBoxFrame(aStyle
, aPresContext
, kClassID
),
58 mActiveByKeyboard(false),
59 mCurrentMenu(nullptr) {} // cntr
61 void nsMenuBarFrame::Init(nsIContent
* aContent
, nsContainerFrame
* aParent
,
62 nsIFrame
* aPrevInFlow
) {
63 nsBoxFrame::Init(aContent
, aParent
, aPrevInFlow
);
65 // Create the menu bar listener.
66 mMenuBarListener
= new nsMenuBarListener(this, aContent
);
70 nsMenuBarFrame::SetActive(bool aActiveFlag
) {
71 // If the activity is not changed, there is nothing to do.
72 if (mIsActive
== aActiveFlag
) return NS_OK
;
75 // Don't deactivate when switching between menus on the menubar.
76 if (mStayActive
) return NS_OK
;
78 // if there is a request to deactivate the menu bar, check to see whether
79 // there is a menu popup open for the menu bar. In this case, don't
80 // deactivate the menu bar.
81 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
82 if (pm
&& pm
->IsPopupOpenForMenuParent(this)) return NS_OK
;
85 mIsActive
= aActiveFlag
;
87 InstallKeyboardNavigator();
89 mActiveByKeyboard
= false;
90 RemoveKeyboardNavigator();
93 constexpr auto active
= u
"DOMMenuBarActive"_ns
;
94 constexpr auto inactive
= u
"DOMMenuBarInactive"_ns
;
96 FireDOMEvent(mIsActive
? active
: inactive
, mContent
);
101 nsMenuFrame
* nsMenuBarFrame::ToggleMenuActiveState() {
103 // Deactivate the menu bar
106 nsMenuFrame
* closeframe
= mCurrentMenu
;
107 closeframe
->SelectMenu(false);
108 mCurrentMenu
= nullptr;
112 // if the menu bar is already selected (eg. mouseover), deselect it
113 if (mCurrentMenu
) mCurrentMenu
->SelectMenu(false);
115 // Set the active menu to be the top left item (e.g., the File menu).
116 // We use an attribute called "menuactive" to track the current
118 nsMenuFrame
* firstFrame
=
119 nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
121 // Activate the menu bar
123 firstFrame
->SelectMenu(true);
125 // Track this item for keyboard navigation.
126 mCurrentMenu
= firstFrame
;
133 nsMenuFrame
* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent
* aKeyEvent
,
135 uint32_t charCode
= aKeyEvent
->CharCode();
137 AutoTArray
<uint32_t, 10> accessKeys
;
138 WidgetKeyboardEvent
* nativeKeyEvent
=
139 aKeyEvent
->WidgetEventPtr()->AsKeyboardEvent();
140 if (nativeKeyEvent
) {
141 nativeKeyEvent
->GetAccessKeyCandidates(accessKeys
);
143 if (accessKeys
.IsEmpty() && charCode
) accessKeys
.AppendElement(charCode
);
145 if (accessKeys
.IsEmpty())
146 return nullptr; // no character was pressed so just return
148 // Enumerate over our list of frames.
149 nsContainerFrame
* immediateParent
=
150 nsXULPopupManager::ImmediateParentFrame(this);
152 // Find a most preferred accesskey which should be returned.
153 nsIFrame
* foundMenu
= nullptr;
154 size_t foundIndex
= accessKeys
.NoIndex
;
155 nsIFrame
* currFrame
= immediateParent
->PrincipalChildList().FirstChild();
158 nsIContent
* current
= currFrame
->GetContent();
160 // See if it's a menu item.
161 if (nsXULPopupManager::IsValidMenuItem(current
, false)) {
162 // Get the shortcut attribute.
163 nsAutoString shortcutKey
;
164 if (current
->IsElement()) {
165 current
->AsElement()->GetAttr(kNameSpaceID_None
, nsGkAtoms::accesskey
,
168 if (!shortcutKey
.IsEmpty()) {
169 ToLowerCase(shortcutKey
);
170 const char16_t
* start
= shortcutKey
.BeginReading();
171 const char16_t
* end
= shortcutKey
.EndReading();
172 uint32_t ch
= UTF16CharEnumerator::NextChar(&start
, end
);
173 size_t index
= accessKeys
.IndexOf(ch
);
174 if (index
!= accessKeys
.NoIndex
&&
175 (foundIndex
== accessKeys
.NoIndex
|| index
< foundIndex
)) {
176 foundMenu
= currFrame
;
181 currFrame
= currFrame
->GetNextSibling();
184 return do_QueryFrame(foundMenu
);
187 // didn't find a matching menu item
190 // behavior on Windows - this item is on the menu bar, beep and deactivate
193 nsCOMPtr
<nsISound
> soundInterface
= do_GetService("@mozilla.org/sound;1");
194 if (soundInterface
) soundInterface
->Beep();
197 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
199 nsIFrame
* popup
= pm
->GetTopPopup(ePopupTypeMenu
);
200 if (popup
) pm
->HidePopup(popup
->GetContent(), true, true, true, false);
203 SetCurrentMenuItem(nullptr);
207 #endif // #ifdef XP_WIN
213 nsMenuFrame
* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu
; }
216 nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame
* aMenuItem
) {
217 if (mCurrentMenu
== aMenuItem
) return NS_OK
;
219 if (mCurrentMenu
) mCurrentMenu
->SelectMenu(false);
221 if (aMenuItem
) aMenuItem
->SelectMenu(true);
223 mCurrentMenu
= aMenuItem
;
228 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
229 mCurrentMenu
->SelectMenu(false);
230 mCurrentMenu
= nullptr;
233 class nsMenuBarSwitchMenu
: public Runnable
{
235 nsMenuBarSwitchMenu(nsIContent
* aMenuBar
, nsIContent
* aOldMenu
,
236 nsIContent
* aNewMenu
, bool aSelectFirstItem
)
237 : mozilla::Runnable("nsMenuBarSwitchMenu"),
241 mSelectFirstItem(aSelectFirstItem
) {}
243 NS_IMETHOD
Run() override
{
244 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
245 if (!pm
) return NS_ERROR_UNEXPECTED
;
247 // if switching from one menu to another, set a flag so that the call to
248 // HidePopup doesn't deactivate the menubar when the first menu closes.
249 nsMenuBarFrame
* menubar
= nullptr;
250 if (mOldMenu
&& mNewMenu
) {
251 menubar
= do_QueryFrame(mMenuBar
->GetPrimaryFrame());
252 if (menubar
) menubar
->SetStayActive(true);
256 AutoWeakFrame
weakMenuBar(menubar
);
257 pm
->HidePopup(mOldMenu
, false, false, false, false);
258 // clear the flag again
259 if (mNewMenu
&& weakMenuBar
.IsAlive()) menubar
->SetStayActive(false);
263 pm
->ShowMenu(mNewMenu
, mSelectFirstItem
);
270 nsCOMPtr
<nsIContent
> mMenuBar
;
271 nsCOMPtr
<nsIContent
> mOldMenu
;
272 nsCOMPtr
<nsIContent
> mNewMenu
;
273 bool mSelectFirstItem
;
277 nsMenuBarFrame::ChangeMenuItem(nsMenuFrame
* aMenuItem
, bool aSelectFirstItem
,
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;
291 wasOpen
= mCurrentMenu
->IsOpen();
292 mCurrentMenu
->SelectMenu(false);
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.
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
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
);
325 bool nsMenuBarFrame::MenuClosed() {
327 if (!mIsActive
&& mCurrentMenu
) {
328 mCurrentMenu
->SelectMenu(false);
329 mCurrentMenu
= nullptr;
335 void nsMenuBarFrame::InstallKeyboardNavigator() {
336 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
337 if (pm
) pm
->SetActiveMenuBar(this, true);
340 void nsMenuBarFrame::RemoveKeyboardNavigator() {
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
);