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"
11 #include "nsPresContext.h"
12 #include "mozilla/ComputedStyle.h"
13 #include "nsCSSRendering.h"
14 #include "nsNameSpaceManager.h"
15 #include "mozilla/dom/Document.h"
16 #include "nsGkAtoms.h"
17 #include "nsMenuFrame.h"
18 #include "nsMenuPopupFrame.h"
19 #include "nsUnicharUtils.h"
20 #include "nsPIDOMWindow.h"
21 #include "nsIInterfaceRequestorUtils.h"
22 #include "nsCSSFrameConstructor.h"
24 # include "nsISound.h"
25 # include "nsWidgetsCID.h"
27 #include "nsUTF8Utils.h"
28 #include "mozilla/TextEvents.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(nsIPresShell
* 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 NS_NAMED_LITERAL_STRING(active
, "DOMMenuBarActive");
94 NS_NAMED_LITERAL_STRING(inactive
, "DOMMenuBarInactive");
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
=
194 do_CreateInstance("@mozilla.org/sound;1");
195 if (soundInterface
) soundInterface
->Beep();
198 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
200 nsIFrame
* popup
= pm
->GetTopPopup(ePopupTypeMenu
);
201 if (popup
) pm
->HidePopup(popup
->GetContent(), true, true, true, false);
204 SetCurrentMenuItem(nullptr);
208 #endif // #ifdef XP_WIN
214 nsMenuFrame
* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu
; }
217 nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame
* aMenuItem
) {
218 if (mCurrentMenu
== aMenuItem
) return NS_OK
;
220 if (mCurrentMenu
) mCurrentMenu
->SelectMenu(false);
222 if (aMenuItem
) aMenuItem
->SelectMenu(true);
224 mCurrentMenu
= aMenuItem
;
229 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
230 mCurrentMenu
->SelectMenu(false);
231 mCurrentMenu
= nullptr;
234 class nsMenuBarSwitchMenu
: public Runnable
{
236 nsMenuBarSwitchMenu(nsIContent
* aMenuBar
, nsIContent
* aOldMenu
,
237 nsIContent
* aNewMenu
, bool aSelectFirstItem
)
238 : mozilla::Runnable("nsMenuBarSwitchMenu"),
242 mSelectFirstItem(aSelectFirstItem
) {}
244 NS_IMETHOD
Run() override
{
245 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
246 if (!pm
) return NS_ERROR_UNEXPECTED
;
248 // if switching from one menu to another, set a flag so that the call to
249 // HidePopup doesn't deactivate the menubar when the first menu closes.
250 nsMenuBarFrame
* menubar
= nullptr;
251 if (mOldMenu
&& mNewMenu
) {
252 menubar
= do_QueryFrame(mMenuBar
->GetPrimaryFrame());
253 if (menubar
) menubar
->SetStayActive(true);
257 AutoWeakFrame
weakMenuBar(menubar
);
258 pm
->HidePopup(mOldMenu
, false, false, false, false);
259 // clear the flag again
260 if (mNewMenu
&& weakMenuBar
.IsAlive()) menubar
->SetStayActive(false);
263 if (mNewMenu
) pm
->ShowMenu(mNewMenu
, mSelectFirstItem
, false);
269 nsCOMPtr
<nsIContent
> mMenuBar
;
270 nsCOMPtr
<nsIContent
> mOldMenu
;
271 nsCOMPtr
<nsIContent
> mNewMenu
;
272 bool mSelectFirstItem
;
276 nsMenuBarFrame::ChangeMenuItem(nsMenuFrame
* aMenuItem
, bool aSelectFirstItem
,
278 if (mCurrentMenu
== aMenuItem
) return NS_OK
;
280 // check if there's an open context menu, we ignore this
281 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
282 if (pm
&& pm
->HasContextMenu(nullptr)) return NS_OK
;
284 nsIContent
* aOldMenu
= nullptr;
285 nsIContent
* aNewMenu
= nullptr;
287 // Unset the current child.
288 bool wasOpen
= false;
290 wasOpen
= mCurrentMenu
->IsOpen();
291 mCurrentMenu
->SelectMenu(false);
293 nsMenuPopupFrame
* popupFrame
= mCurrentMenu
->GetPopup();
294 if (popupFrame
) aOldMenu
= popupFrame
->GetContent();
298 // set to null first in case the IsAlive check below returns false
299 mCurrentMenu
= nullptr;
301 // Set the new child.
303 nsCOMPtr
<nsIContent
> content
= aMenuItem
->GetContent();
304 aMenuItem
->SelectMenu(true);
305 mCurrentMenu
= aMenuItem
;
306 if (wasOpen
&& !aMenuItem
->IsDisabled()) aNewMenu
= content
;
309 // use an event so that hiding and showing can be done synchronously, which
311 nsCOMPtr
<nsIRunnable
> event
= new nsMenuBarSwitchMenu(
312 GetContent(), aOldMenu
, aNewMenu
, aSelectFirstItem
);
313 return mContent
->OwnerDoc()->Dispatch(TaskCategory::Other
, event
.forget());
316 nsMenuFrame
* nsMenuBarFrame::Enter(WidgetGUIEvent
* aEvent
) {
317 if (!mCurrentMenu
) return nullptr;
319 if (mCurrentMenu
->IsOpen()) return mCurrentMenu
->Enter(aEvent
);
324 bool nsMenuBarFrame::MenuClosed() {
326 if (!mIsActive
&& mCurrentMenu
) {
327 mCurrentMenu
->SelectMenu(false);
328 mCurrentMenu
= nullptr;
334 void nsMenuBarFrame::InstallKeyboardNavigator() {
335 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
336 if (pm
) pm
->SetActiveMenuBar(this, true);
339 void nsMenuBarFrame::RemoveKeyboardNavigator() {
341 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
342 if (pm
) pm
->SetActiveMenuBar(this, false);
346 void nsMenuBarFrame::DestroyFrom(nsIFrame
* aDestructRoot
,
347 PostDestroyData
& aPostDestroyData
) {
348 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
349 if (pm
) pm
->SetActiveMenuBar(this, false);
351 mMenuBarListener
->OnDestroyMenuBarFrame();
352 mMenuBarListener
= nullptr;
354 nsBoxFrame::DestroyFrom(aDestructRoot
, aPostDestroyData
);