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 "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"
22 # include "nsISound.h"
23 # include "nsWidgetsCID.h"
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
;
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
),
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
);
71 nsMenuBarFrame::SetActive(bool aActiveFlag
) {
72 // If the activity is not changed, there is nothing to do.
73 if (mIsActive
== aActiveFlag
) return NS_OK
;
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
;
88 InstallKeyboardNavigator();
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
);
102 nsMenuFrame
* nsMenuBarFrame::ToggleMenuActiveState() {
104 // Deactivate the menu bar
107 nsMenuFrame
* closeframe
= mCurrentMenu
;
108 closeframe
->SelectMenu(false);
109 mCurrentMenu
= nullptr;
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
119 nsMenuFrame
* firstFrame
=
120 nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
122 // Activate the menu bar
124 firstFrame
->SelectMenu(true);
126 // Track this item for keyboard navigation.
127 mCurrentMenu
= firstFrame
;
134 nsMenuFrame
* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent
* aKeyEvent
,
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();
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
,
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
;
182 currFrame
= currFrame
->GetNextSibling();
185 return do_QueryFrame(foundMenu
);
188 // didn't find a matching menu item
191 // behavior on Windows - this item is on the menu bar, beep and deactivate
194 nsCOMPtr
<nsISound
> soundInterface
=
195 do_CreateInstance("@mozilla.org/sound;1");
196 if (soundInterface
) soundInterface
->Beep();
199 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
201 nsIFrame
* popup
= pm
->GetTopPopup(ePopupTypeMenu
);
202 if (popup
) pm
->HidePopup(popup
->GetContent(), true, true, true, false);
205 SetCurrentMenuItem(nullptr);
209 #endif // #ifdef XP_WIN
215 nsMenuFrame
* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu
; }
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
;
230 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
231 mCurrentMenu
->SelectMenu(false);
232 mCurrentMenu
= nullptr;
235 class nsMenuBarSwitchMenu
: public Runnable
{
237 nsMenuBarSwitchMenu(nsIContent
* aMenuBar
, nsIContent
* aOldMenu
,
238 nsIContent
* aNewMenu
, bool aSelectFirstItem
)
239 : mozilla::Runnable("nsMenuBarSwitchMenu"),
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);
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);
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
);