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/. */
6
7 #include "nsMenuBarFrame.h"
8 #include "nsIServiceManager.h"
9 #include "nsIContent.h"
10 #include "nsAtom.h"
11 #include "nsPresContext.h"
12 #include "nsStyleContext.h"
13 #include "nsCSSRendering.h"
14 #include "nsNameSpaceManager.h"
15 #include "nsIDocument.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"
23 #ifdef XP_WIN
24 #include "nsISound.h"
25 #include "nsWidgetsCID.h"
26 #endif
27 #include "nsContentUtils.h"
28 #include "nsUTF8Utils.h"
29 #include "mozilla/TextEvents.h"
30 #include "mozilla/dom/Event.h"
31 #include "mozilla/dom/KeyboardEvent.h"
32
33 using namespace mozilla;
34 using mozilla::dom::KeyboardEvent;
35
36 //
37 // NS_NewMenuBarFrame
38 //
39 // Wrapper for creating a new menu Bar container
40 //
NS_NewMenuBarFrame(nsIPresShell * aPresShell,nsStyleContext * aContext)41 nsIFrame* NS_NewMenuBarFrame(nsIPresShell* aPresShell,
42 nsStyleContext* aContext) {
43 return new (aPresShell) nsMenuBarFrame(aContext);
44 }
45
46 NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame)
47
NS_QUERYFRAME_HEAD(nsMenuBarFrame)48 NS_QUERYFRAME_HEAD(nsMenuBarFrame)
49 NS_QUERYFRAME_ENTRY(nsMenuBarFrame)
50 NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
51
52 //
53 // nsMenuBarFrame cntr
54 //
55 nsMenuBarFrame::nsMenuBarFrame(nsStyleContext* aContext)
56 : nsBoxFrame(aContext, kClassID),
57 mStayActive(false),
58 mIsActive(false),
59 mActiveByKeyboard(false),
60 mCurrentMenu(nullptr) {} // cntr
61
Init(nsIContent * aContent,nsContainerFrame * aParent,nsIFrame * aPrevInFlow)62 void nsMenuBarFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
63 nsIFrame* aPrevInFlow) {
64 nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
65
66 // Create the menu bar listener.
67 mMenuBarListener = new nsMenuBarListener(this, aContent);
68 }
69
70 NS_IMETHODIMP
SetActive(bool aActiveFlag)71 nsMenuBarFrame::SetActive(bool aActiveFlag) {
72 // If the activity is not changed, there is nothing to do.
73 if (mIsActive == aActiveFlag) return NS_OK;
74
75 if (!aActiveFlag) {
76 // Don't deactivate when switching between menus on the menubar.
77 if (mStayActive) return NS_OK;
78
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;
84 }
85
86 mIsActive = aActiveFlag;
87 if (mIsActive) {
88 InstallKeyboardNavigator();
89 } else {
90 mActiveByKeyboard = false;
91 RemoveKeyboardNavigator();
92 }
93
94 NS_NAMED_LITERAL_STRING(active, "DOMMenuBarActive");
95 NS_NAMED_LITERAL_STRING(inactive, "DOMMenuBarInactive");
96
97 FireDOMEvent(mIsActive ? active : inactive, mContent);
98
99 return NS_OK;
100 }
101
ToggleMenuActiveState()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;
111 }
112 } else {
113 // if the menu bar is already selected (eg. mouseover), deselect it
114 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
115
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);
125
126 // Track this item for keyboard navigation.
127 mCurrentMenu = firstFrame;
128 }
129 }
130
131 return nullptr;
132 }
133
FindMenuWithShortcut(KeyboardEvent * aKeyEvent,bool aPeek)134 nsMenuFrame* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent* aKeyEvent,
135 bool aPeek) {
136 uint32_t charCode = aKeyEvent->CharCode();
137
138 AutoTArray<uint32_t, 10> accessKeys;
139 WidgetKeyboardEvent* nativeKeyEvent =
140 aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
141 if (nativeKeyEvent) {
142 nativeKeyEvent->GetAccessKeyCandidates(accessKeys);
143 }
144 if (accessKeys.IsEmpty() && charCode) accessKeys.AppendElement(charCode);
145
146 if (accessKeys.IsEmpty())
147 return nullptr; // no character was pressed so just return
148
149 // Enumerate over our list of frames.
150 nsContainerFrame* immediateParent =
151 nsXULPopupManager::ImmediateParentFrame(this);
152
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();
157
158 while (currFrame) {
159 nsIContent* current = currFrame->GetContent();
160
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);
168 }
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;
179 }
180 }
181 }
182 currFrame = currFrame->GetNextSibling();
183 }
184 if (foundMenu) {
185 return do_QueryFrame(foundMenu);
186 }
187
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();
197 }
198
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);
203 }
204
205 SetCurrentMenuItem(nullptr);
206 SetActive(false);
207 }
208
209 #endif // #ifdef XP_WIN
210
211 return nullptr;
212 }
213
GetCurrentMenuItem()214 /* virtual */ nsMenuFrame* nsMenuBarFrame::GetCurrentMenuItem() {
215 return mCurrentMenu;
216 }
217
218 NS_IMETHODIMP
SetCurrentMenuItem(nsMenuFrame * aMenuItem)219 nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame* aMenuItem) {
220 if (mCurrentMenu == aMenuItem) return NS_OK;
221
222 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
223
224 if (aMenuItem) aMenuItem->SelectMenu(true);
225
226 mCurrentMenu = aMenuItem;
227
228 return NS_OK;
229 }
230
CurrentMenuIsBeingDestroyed()231 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
232 mCurrentMenu->SelectMenu(false);
233 mCurrentMenu = nullptr;
234 }
235
236 class nsMenuBarSwitchMenu : public Runnable {
237 public:
nsMenuBarSwitchMenu(nsIContent * aMenuBar,nsIContent * aOldMenu,nsIContent * aNewMenu,bool aSelectFirstItem)238 nsMenuBarSwitchMenu(nsIContent* aMenuBar, nsIContent* aOldMenu,
239 nsIContent* aNewMenu, bool aSelectFirstItem)
240 : mozilla::Runnable("nsMenuBarSwitchMenu"),
241 mMenuBar(aMenuBar),
242 mOldMenu(aOldMenu),
243 mNewMenu(aNewMenu),
244 mSelectFirstItem(aSelectFirstItem) {}
245
Run()246 NS_IMETHOD Run() override {
247 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
248 if (!pm) return NS_ERROR_UNEXPECTED;
249
250 // if switching from one menu to another, set a flag so that the call to
251 // HidePopup doesn't deactivate the menubar when the first menu closes.
252 nsMenuBarFrame* menubar = nullptr;
253 if (mOldMenu && mNewMenu) {
254 menubar = do_QueryFrame(mMenuBar->GetPrimaryFrame());
255 if (menubar) menubar->SetStayActive(true);
256 }
257
258 if (mOldMenu) {
259 AutoWeakFrame weakMenuBar(menubar);
260 pm->HidePopup(mOldMenu, false, false, false, false);
261 // clear the flag again
262 if (mNewMenu && weakMenuBar.IsAlive()) menubar->SetStayActive(false);
263 }
264
265 if (mNewMenu) pm->ShowMenu(mNewMenu, mSelectFirstItem, false);
266
267 return NS_OK;
268 }
269
270 private:
271 nsCOMPtr<nsIContent> mMenuBar;
272 nsCOMPtr<nsIContent> mOldMenu;
273 nsCOMPtr<nsIContent> mNewMenu;
274 bool mSelectFirstItem;
275 };
276
277 NS_IMETHODIMP
ChangeMenuItem(nsMenuFrame * aMenuItem,bool aSelectFirstItem,bool aFromKey)278 nsMenuBarFrame::ChangeMenuItem(nsMenuFrame* aMenuItem, bool aSelectFirstItem,
279 bool aFromKey) {
280 if (mCurrentMenu == aMenuItem) return NS_OK;
281
282 // check if there's an open context menu, we ignore this
283 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
284 if (pm && pm->HasContextMenu(nullptr)) return NS_OK;
285
286 nsIContent* aOldMenu = nullptr;
287 nsIContent* aNewMenu = nullptr;
288
289 // Unset the current child.
290 bool wasOpen = false;
291 if (mCurrentMenu) {
292 wasOpen = mCurrentMenu->IsOpen();
293 mCurrentMenu->SelectMenu(false);
294 if (wasOpen) {
295 nsMenuPopupFrame* popupFrame = mCurrentMenu->GetPopup();
296 if (popupFrame) aOldMenu = popupFrame->GetContent();
297 }
298 }
299
300 // set to null first in case the IsAlive check below returns false
301 mCurrentMenu = nullptr;
302
303 // Set the new child.
304 if (aMenuItem) {
305 nsCOMPtr<nsIContent> content = aMenuItem->GetContent();
306 aMenuItem->SelectMenu(true);
307 mCurrentMenu = aMenuItem;
308 if (wasOpen && !aMenuItem->IsDisabled()) aNewMenu = content;
309 }
310
311 // use an event so that hiding and showing can be done synchronously, which
312 // avoids flickering
313 nsCOMPtr<nsIRunnable> event = new nsMenuBarSwitchMenu(
314 GetContent(), aOldMenu, aNewMenu, aSelectFirstItem);
315 return mContent->OwnerDoc()->Dispatch(TaskCategory::Other, event.forget());
316 }
317
Enter(WidgetGUIEvent * aEvent)318 nsMenuFrame* nsMenuBarFrame::Enter(WidgetGUIEvent* aEvent) {
319 if (!mCurrentMenu) return nullptr;
320
321 if (mCurrentMenu->IsOpen()) return mCurrentMenu->Enter(aEvent);
322
323 return mCurrentMenu;
324 }
325
MenuClosed()326 bool nsMenuBarFrame::MenuClosed() {
327 SetActive(false);
328 if (!mIsActive && mCurrentMenu) {
329 mCurrentMenu->SelectMenu(false);
330 mCurrentMenu = nullptr;
331 return true;
332 }
333 return false;
334 }
335
InstallKeyboardNavigator()336 void nsMenuBarFrame::InstallKeyboardNavigator() {
337 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
338 if (pm) pm->SetActiveMenuBar(this, true);
339 }
340
RemoveKeyboardNavigator()341 void nsMenuBarFrame::RemoveKeyboardNavigator() {
342 if (!mIsActive) {
343 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
344 if (pm) pm->SetActiveMenuBar(this, false);
345 }
346 }
347
DestroyFrom(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)348 void nsMenuBarFrame::DestroyFrom(nsIFrame* aDestructRoot,
349 PostDestroyData& aPostDestroyData) {
350 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
351 if (pm) pm->SetActiveMenuBar(this, false);
352
353 mMenuBarListener->OnDestroyMenuBarFrame();
354 mMenuBarListener = nullptr;
355
356 nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
357 }
358