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 "nsIContent.h"
9 #include "nsAtom.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"
20 #ifdef XP_WIN
21 # include "nsISound.h"
22 # include "nsWidgetsCID.h"
23 #endif
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"
31
32 using namespace mozilla;
33 using mozilla::dom::KeyboardEvent;
34
35 //
36 // NS_NewMenuBarFrame
37 //
38 // Wrapper for creating a new menu Bar container
39 //
NS_NewMenuBarFrame(PresShell * aPresShell,ComputedStyle * aStyle)40 nsIFrame* NS_NewMenuBarFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
41 return new (aPresShell) nsMenuBarFrame(aStyle, aPresShell->GetPresContext());
42 }
43
44 NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame)
45
NS_QUERYFRAME_HEAD(nsMenuBarFrame)46 NS_QUERYFRAME_HEAD(nsMenuBarFrame)
47 NS_QUERYFRAME_ENTRY(nsMenuBarFrame)
48 NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
49
50 //
51 // nsMenuBarFrame cntr
52 //
53 nsMenuBarFrame::nsMenuBarFrame(ComputedStyle* aStyle,
54 nsPresContext* aPresContext)
55 : nsBoxFrame(aStyle, aPresContext, kClassID),
56 mStayActive(false),
57 mIsActive(false),
58 mActiveByKeyboard(false),
59 mCurrentMenu(nullptr) {} // cntr
60
Init(nsIContent * aContent,nsContainerFrame * aParent,nsIFrame * aPrevInFlow)61 void nsMenuBarFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
62 nsIFrame* aPrevInFlow) {
63 nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
64
65 // Create the menu bar listener.
66 mMenuBarListener = new nsMenuBarListener(this, aContent);
67 }
68
69 NS_IMETHODIMP
SetActive(bool aActiveFlag)70 nsMenuBarFrame::SetActive(bool aActiveFlag) {
71 // If the activity is not changed, there is nothing to do.
72 if (mIsActive == aActiveFlag) return NS_OK;
73
74 if (!aActiveFlag) {
75 // Don't deactivate when switching between menus on the menubar.
76 if (mStayActive) return NS_OK;
77
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;
83 }
84
85 mIsActive = aActiveFlag;
86 if (mIsActive) {
87 InstallKeyboardNavigator();
88 } else {
89 mActiveByKeyboard = false;
90 RemoveKeyboardNavigator();
91 }
92
93 NS_NAMED_LITERAL_STRING(active, "DOMMenuBarActive");
94 NS_NAMED_LITERAL_STRING(inactive, "DOMMenuBarInactive");
95
96 FireDOMEvent(mIsActive ? active : inactive, mContent);
97
98 return NS_OK;
99 }
100
ToggleMenuActiveState()101 nsMenuFrame* nsMenuBarFrame::ToggleMenuActiveState() {
102 if (mIsActive) {
103 // Deactivate the menu bar
104 SetActive(false);
105 if (mCurrentMenu) {
106 nsMenuFrame* closeframe = mCurrentMenu;
107 closeframe->SelectMenu(false);
108 mCurrentMenu = nullptr;
109 return closeframe;
110 }
111 } else {
112 // if the menu bar is already selected (eg. mouseover), deselect it
113 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
114
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
117 // active menu.
118 nsMenuFrame* firstFrame =
119 nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
120 if (firstFrame) {
121 // Activate the menu bar
122 SetActive(true);
123 firstFrame->SelectMenu(true);
124
125 // Track this item for keyboard navigation.
126 mCurrentMenu = firstFrame;
127 }
128 }
129
130 return nullptr;
131 }
132
FindMenuWithShortcut(KeyboardEvent * aKeyEvent,bool aPeek)133 nsMenuFrame* nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent* aKeyEvent,
134 bool aPeek) {
135 uint32_t charCode = aKeyEvent->CharCode();
136
137 AutoTArray<uint32_t, 10> accessKeys;
138 WidgetKeyboardEvent* nativeKeyEvent =
139 aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
140 if (nativeKeyEvent) {
141 nativeKeyEvent->GetAccessKeyCandidates(accessKeys);
142 }
143 if (accessKeys.IsEmpty() && charCode) accessKeys.AppendElement(charCode);
144
145 if (accessKeys.IsEmpty())
146 return nullptr; // no character was pressed so just return
147
148 // Enumerate over our list of frames.
149 nsContainerFrame* immediateParent =
150 nsXULPopupManager::ImmediateParentFrame(this);
151
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();
156
157 while (currFrame) {
158 nsIContent* current = currFrame->GetContent();
159
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,
166 shortcutKey);
167 }
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;
177 foundIndex = index;
178 }
179 }
180 }
181 currFrame = currFrame->GetNextSibling();
182 }
183 if (foundMenu) {
184 return do_QueryFrame(foundMenu);
185 }
186
187 // didn't find a matching menu item
188 #ifdef XP_WIN
189 if (!aPeek) {
190 // behavior on Windows - this item is on the menu bar, beep and deactivate
191 // the menu bar
192 if (mIsActive) {
193 nsCOMPtr<nsISound> soundInterface =
194 do_CreateInstance("@mozilla.org/sound;1");
195 if (soundInterface) soundInterface->Beep();
196 }
197
198 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
199 if (pm) {
200 nsIFrame* popup = pm->GetTopPopup(ePopupTypeMenu);
201 if (popup) pm->HidePopup(popup->GetContent(), true, true, true, false);
202 }
203
204 SetCurrentMenuItem(nullptr);
205 SetActive(false);
206 }
207
208 #endif // #ifdef XP_WIN
209
210 return nullptr;
211 }
212
213 /* virtual */
GetCurrentMenuItem()214 nsMenuFrame* nsMenuBarFrame::GetCurrentMenuItem() { return mCurrentMenu; }
215
216 NS_IMETHODIMP
SetCurrentMenuItem(nsMenuFrame * aMenuItem)217 nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame* aMenuItem) {
218 if (mCurrentMenu == aMenuItem) return NS_OK;
219
220 if (mCurrentMenu) mCurrentMenu->SelectMenu(false);
221
222 if (aMenuItem) aMenuItem->SelectMenu(true);
223
224 mCurrentMenu = aMenuItem;
225
226 return NS_OK;
227 }
228
CurrentMenuIsBeingDestroyed()229 void nsMenuBarFrame::CurrentMenuIsBeingDestroyed() {
230 mCurrentMenu->SelectMenu(false);
231 mCurrentMenu = nullptr;
232 }
233
234 class nsMenuBarSwitchMenu : public Runnable {
235 public:
nsMenuBarSwitchMenu(nsIContent * aMenuBar,nsIContent * aOldMenu,nsIContent * aNewMenu,bool aSelectFirstItem)236 nsMenuBarSwitchMenu(nsIContent* aMenuBar, nsIContent* aOldMenu,
237 nsIContent* aNewMenu, bool aSelectFirstItem)
238 : mozilla::Runnable("nsMenuBarSwitchMenu"),
239 mMenuBar(aMenuBar),
240 mOldMenu(aOldMenu),
241 mNewMenu(aNewMenu),
242 mSelectFirstItem(aSelectFirstItem) {}
243
Run()244 NS_IMETHOD Run() override {
245 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
246 if (!pm) return NS_ERROR_UNEXPECTED;
247
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);
254 }
255
256 if (mOldMenu) {
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);
261 }
262
263 if (mNewMenu) pm->ShowMenu(mNewMenu, mSelectFirstItem, false);
264
265 return NS_OK;
266 }
267
268 private:
269 nsCOMPtr<nsIContent> mMenuBar;
270 nsCOMPtr<nsIContent> mOldMenu;
271 nsCOMPtr<nsIContent> mNewMenu;
272 bool mSelectFirstItem;
273 };
274
275 NS_IMETHODIMP
ChangeMenuItem(nsMenuFrame * aMenuItem,bool aSelectFirstItem,bool aFromKey)276 nsMenuBarFrame::ChangeMenuItem(nsMenuFrame* aMenuItem, bool aSelectFirstItem,
277 bool aFromKey) {
278 if (mCurrentMenu == aMenuItem) return NS_OK;
279
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;
283
284 nsIContent* aOldMenu = nullptr;
285 nsIContent* aNewMenu = nullptr;
286
287 // Unset the current child.
288 bool wasOpen = false;
289 if (mCurrentMenu) {
290 wasOpen = mCurrentMenu->IsOpen();
291 mCurrentMenu->SelectMenu(false);
292 if (wasOpen) {
293 nsMenuPopupFrame* popupFrame = mCurrentMenu->GetPopup();
294 if (popupFrame) aOldMenu = popupFrame->GetContent();
295 }
296 }
297
298 // set to null first in case the IsAlive check below returns false
299 mCurrentMenu = nullptr;
300
301 // Set the new child.
302 if (aMenuItem) {
303 nsCOMPtr<nsIContent> content = aMenuItem->GetContent();
304 aMenuItem->SelectMenu(true);
305 mCurrentMenu = aMenuItem;
306 if (wasOpen && !aMenuItem->IsDisabled()) aNewMenu = content;
307 }
308
309 // use an event so that hiding and showing can be done synchronously, which
310 // avoids flickering
311 nsCOMPtr<nsIRunnable> event = new nsMenuBarSwitchMenu(
312 GetContent(), aOldMenu, aNewMenu, aSelectFirstItem);
313 return mContent->OwnerDoc()->Dispatch(TaskCategory::Other, event.forget());
314 }
315
Enter(WidgetGUIEvent * aEvent)316 nsMenuFrame* nsMenuBarFrame::Enter(WidgetGUIEvent* aEvent) {
317 if (!mCurrentMenu) return nullptr;
318
319 if (mCurrentMenu->IsOpen()) return mCurrentMenu->Enter(aEvent);
320
321 return mCurrentMenu;
322 }
323
MenuClosed()324 bool nsMenuBarFrame::MenuClosed() {
325 SetActive(false);
326 if (!mIsActive && mCurrentMenu) {
327 mCurrentMenu->SelectMenu(false);
328 mCurrentMenu = nullptr;
329 return true;
330 }
331 return false;
332 }
333
InstallKeyboardNavigator()334 void nsMenuBarFrame::InstallKeyboardNavigator() {
335 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
336 if (pm) pm->SetActiveMenuBar(this, true);
337 }
338
RemoveKeyboardNavigator()339 void nsMenuBarFrame::RemoveKeyboardNavigator() {
340 if (!mIsActive) {
341 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
342 if (pm) pm->SetActiveMenuBar(this, false);
343 }
344 }
345
DestroyFrom(nsIFrame * aDestructRoot,PostDestroyData & aPostDestroyData)346 void nsMenuBarFrame::DestroyFrom(nsIFrame* aDestructRoot,
347 PostDestroyData& aPostDestroyData) {
348 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
349 if (pm) pm->SetActiveMenuBar(this, false);
350
351 mMenuBarListener->OnDestroyMenuBarFrame();
352 mMenuBarListener = nullptr;
353
354 nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
355 }
356