Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/layout/xul/nsMenuBarFrame.cpp
Line
Count
Source (jump to first uncovered line)
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 "mozilla/ComputedStyle.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 "nsUTF8Utils.h"
28
#include "mozilla/TextEvents.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
//
40
nsIFrame*
41
NS_NewMenuBarFrame(nsIPresShell* aPresShell, ComputedStyle* aStyle)
42
0
{
43
0
  return new (aPresShell) nsMenuBarFrame(aStyle);
44
0
}
45
46
NS_IMPL_FRAMEARENA_HELPERS(nsMenuBarFrame)
47
48
0
NS_QUERYFRAME_HEAD(nsMenuBarFrame)
49
0
  NS_QUERYFRAME_ENTRY(nsMenuBarFrame)
50
0
NS_QUERYFRAME_TAIL_INHERITING(nsBoxFrame)
51
52
//
53
// nsMenuBarFrame cntr
54
//
55
nsMenuBarFrame::nsMenuBarFrame(ComputedStyle* aStyle)
56
  : nsBoxFrame(aStyle, kClassID)
57
  , mStayActive(false)
58
  , mIsActive(false)
59
  , mActiveByKeyboard(false)
60
  , mCurrentMenu(nullptr)
61
0
{
62
0
} // cntr
63
64
void
65
nsMenuBarFrame::Init(nsIContent*       aContent,
66
                     nsContainerFrame* aParent,
67
                     nsIFrame*         aPrevInFlow)
68
0
{
69
0
  nsBoxFrame::Init(aContent, aParent, aPrevInFlow);
70
0
71
0
  // Create the menu bar listener.
72
0
  mMenuBarListener = new nsMenuBarListener(this, aContent);
73
0
}
74
75
NS_IMETHODIMP
76
nsMenuBarFrame::SetActive(bool aActiveFlag)
77
0
{
78
0
  // If the activity is not changed, there is nothing to do.
79
0
  if (mIsActive == aActiveFlag)
80
0
    return NS_OK;
81
0
82
0
  if (!aActiveFlag) {
83
0
    // Don't deactivate when switching between menus on the menubar.
84
0
    if (mStayActive)
85
0
      return NS_OK;
86
0
87
0
    // if there is a request to deactivate the menu bar, check to see whether
88
0
    // there is a menu popup open for the menu bar. In this case, don't
89
0
    // deactivate the menu bar.
90
0
    nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
91
0
    if (pm && pm->IsPopupOpenForMenuParent(this))
92
0
      return NS_OK;
93
0
  }
94
0
95
0
  mIsActive = aActiveFlag;
96
0
  if (mIsActive) {
97
0
    InstallKeyboardNavigator();
98
0
  }
99
0
  else {
100
0
    mActiveByKeyboard = false;
101
0
    RemoveKeyboardNavigator();
102
0
  }
103
0
104
0
  NS_NAMED_LITERAL_STRING(active, "DOMMenuBarActive");
105
0
  NS_NAMED_LITERAL_STRING(inactive, "DOMMenuBarInactive");
106
0
107
0
  FireDOMEvent(mIsActive ? active : inactive, mContent);
108
0
109
0
  return NS_OK;
110
0
}
111
112
nsMenuFrame*
113
nsMenuBarFrame::ToggleMenuActiveState()
114
0
{
115
0
  if (mIsActive) {
116
0
    // Deactivate the menu bar
117
0
    SetActive(false);
118
0
    if (mCurrentMenu) {
119
0
      nsMenuFrame* closeframe = mCurrentMenu;
120
0
      closeframe->SelectMenu(false);
121
0
      mCurrentMenu = nullptr;
122
0
      return closeframe;
123
0
    }
124
0
  }
125
0
  else {
126
0
    // if the menu bar is already selected (eg. mouseover), deselect it
127
0
    if (mCurrentMenu)
128
0
      mCurrentMenu->SelectMenu(false);
129
0
130
0
    // Set the active menu to be the top left item (e.g., the File menu).
131
0
    // We use an attribute called "menuactive" to track the current
132
0
    // active menu.
133
0
    nsMenuFrame* firstFrame = nsXULPopupManager::GetNextMenuItem(this, nullptr, false, false);
134
0
    if (firstFrame) {
135
0
      // Activate the menu bar
136
0
      SetActive(true);
137
0
      firstFrame->SelectMenu(true);
138
0
139
0
      // Track this item for keyboard navigation.
140
0
      mCurrentMenu = firstFrame;
141
0
    }
142
0
  }
143
0
144
0
  return nullptr;
145
0
}
146
147
nsMenuFrame*
148
nsMenuBarFrame::FindMenuWithShortcut(KeyboardEvent* aKeyEvent, bool aPeek)
149
0
{
150
0
  uint32_t charCode = aKeyEvent->CharCode();
151
0
152
0
  AutoTArray<uint32_t, 10> accessKeys;
153
0
  WidgetKeyboardEvent* nativeKeyEvent =
154
0
    aKeyEvent->WidgetEventPtr()->AsKeyboardEvent();
155
0
  if (nativeKeyEvent) {
156
0
    nativeKeyEvent->GetAccessKeyCandidates(accessKeys);
157
0
  }
158
0
  if (accessKeys.IsEmpty() && charCode)
159
0
    accessKeys.AppendElement(charCode);
160
0
161
0
  if (accessKeys.IsEmpty())
162
0
    return nullptr; // no character was pressed so just return
163
0
164
0
  // Enumerate over our list of frames.
165
0
  nsContainerFrame* immediateParent =
166
0
    nsXULPopupManager::ImmediateParentFrame(this);
167
0
168
0
  // Find a most preferred accesskey which should be returned.
169
0
  nsIFrame* foundMenu = nullptr;
170
0
  size_t foundIndex = accessKeys.NoIndex;
171
0
  nsIFrame* currFrame = immediateParent->PrincipalChildList().FirstChild();
172
0
173
0
  while (currFrame) {
174
0
    nsIContent* current = currFrame->GetContent();
175
0
176
0
    // See if it's a menu item.
177
0
    if (nsXULPopupManager::IsValidMenuItem(current, false)) {
178
0
      // Get the shortcut attribute.
179
0
      nsAutoString shortcutKey;
180
0
      if (current->IsElement()) {
181
0
        current->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey,
182
0
                                      shortcutKey);
183
0
      }
184
0
      if (!shortcutKey.IsEmpty()) {
185
0
        ToLowerCase(shortcutKey);
186
0
        const char16_t* start = shortcutKey.BeginReading();
187
0
        const char16_t* end = shortcutKey.EndReading();
188
0
        uint32_t ch = UTF16CharEnumerator::NextChar(&start, end);
189
0
        size_t index = accessKeys.IndexOf(ch);
190
0
        if (index != accessKeys.NoIndex &&
191
0
            (foundIndex == accessKeys.NoIndex || index < foundIndex)) {
192
0
          foundMenu = currFrame;
193
0
          foundIndex = index;
194
0
        }
195
0
      }
196
0
    }
197
0
    currFrame = currFrame->GetNextSibling();
198
0
  }
199
0
  if (foundMenu) {
200
0
    return do_QueryFrame(foundMenu);
201
0
  }
202
0
203
0
  // didn't find a matching menu item
204
#ifdef XP_WIN
205
  if (!aPeek) {
206
    // behavior on Windows - this item is on the menu bar, beep and deactivate the menu bar
207
    if (mIsActive) {
208
      nsCOMPtr<nsISound> soundInterface = do_CreateInstance("@mozilla.org/sound;1");
209
      if (soundInterface)
210
        soundInterface->Beep();
211
    }
212
213
    nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
214
    if (pm) {
215
      nsIFrame* popup = pm->GetTopPopup(ePopupTypeMenu);
216
      if (popup)
217
        pm->HidePopup(popup->GetContent(), true, true, true, false);
218
    }
219
220
    SetCurrentMenuItem(nullptr);
221
    SetActive(false);
222
  }
223
224
#endif  // #ifdef XP_WIN
225
226
0
  return nullptr;
227
0
}
228
229
/* virtual */ nsMenuFrame*
230
nsMenuBarFrame::GetCurrentMenuItem()
231
0
{
232
0
  return mCurrentMenu;
233
0
}
234
235
NS_IMETHODIMP
236
nsMenuBarFrame::SetCurrentMenuItem(nsMenuFrame* aMenuItem)
237
0
{
238
0
  if (mCurrentMenu == aMenuItem)
239
0
    return NS_OK;
240
0
241
0
  if (mCurrentMenu)
242
0
    mCurrentMenu->SelectMenu(false);
243
0
244
0
  if (aMenuItem)
245
0
    aMenuItem->SelectMenu(true);
246
0
247
0
  mCurrentMenu = aMenuItem;
248
0
249
0
  return NS_OK;
250
0
}
251
252
void
253
nsMenuBarFrame::CurrentMenuIsBeingDestroyed()
254
0
{
255
0
  mCurrentMenu->SelectMenu(false);
256
0
  mCurrentMenu = nullptr;
257
0
}
258
259
class nsMenuBarSwitchMenu : public Runnable
260
{
261
public:
262
  nsMenuBarSwitchMenu(nsIContent* aMenuBar,
263
                      nsIContent* aOldMenu,
264
                      nsIContent* aNewMenu,
265
                      bool aSelectFirstItem)
266
    : mozilla::Runnable("nsMenuBarSwitchMenu")
267
    , mMenuBar(aMenuBar)
268
    , mOldMenu(aOldMenu)
269
    , mNewMenu(aNewMenu)
270
    , mSelectFirstItem(aSelectFirstItem)
271
0
  {
272
0
  }
273
274
  NS_IMETHOD Run() override
275
0
  {
276
0
    nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
277
0
    if (!pm)
278
0
      return NS_ERROR_UNEXPECTED;
279
0
280
0
    // if switching from one menu to another, set a flag so that the call to
281
0
    // HidePopup doesn't deactivate the menubar when the first menu closes.
282
0
    nsMenuBarFrame* menubar = nullptr;
283
0
    if (mOldMenu && mNewMenu) {
284
0
      menubar = do_QueryFrame(mMenuBar->GetPrimaryFrame());
285
0
      if (menubar)
286
0
        menubar->SetStayActive(true);
287
0
    }
288
0
289
0
    if (mOldMenu) {
290
0
      AutoWeakFrame weakMenuBar(menubar);
291
0
      pm->HidePopup(mOldMenu, false, false, false, false);
292
0
      // clear the flag again
293
0
      if (mNewMenu && weakMenuBar.IsAlive())
294
0
        menubar->SetStayActive(false);
295
0
    }
296
0
297
0
    if (mNewMenu)
298
0
      pm->ShowMenu(mNewMenu, mSelectFirstItem, false);
299
0
300
0
    return NS_OK;
301
0
  }
302
303
private:
304
  nsCOMPtr<nsIContent> mMenuBar;
305
  nsCOMPtr<nsIContent> mOldMenu;
306
  nsCOMPtr<nsIContent> mNewMenu;
307
  bool mSelectFirstItem;
308
};
309
310
NS_IMETHODIMP
311
nsMenuBarFrame::ChangeMenuItem(nsMenuFrame* aMenuItem,
312
                               bool aSelectFirstItem,
313
                               bool aFromKey)
314
0
{
315
0
  if (mCurrentMenu == aMenuItem)
316
0
    return NS_OK;
317
0
318
0
  // check if there's an open context menu, we ignore this
319
0
  nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
320
0
  if (pm && pm->HasContextMenu(nullptr))
321
0
    return NS_OK;
322
0
323
0
  nsIContent* aOldMenu = nullptr;
324
0
  nsIContent* aNewMenu = nullptr;
325
0
326
0
  // Unset the current child.
327
0
  bool wasOpen = false;
328
0
  if (mCurrentMenu) {
329
0
    wasOpen = mCurrentMenu->IsOpen();
330
0
    mCurrentMenu->SelectMenu(false);
331
0
    if (wasOpen) {
332
0
      nsMenuPopupFrame* popupFrame = mCurrentMenu->GetPopup();
333
0
      if (popupFrame)
334
0
        aOldMenu = popupFrame->GetContent();
335
0
    }
336
0
  }
337
0
338
0
  // set to null first in case the IsAlive check below returns false
339
0
  mCurrentMenu = nullptr;
340
0
341
0
  // Set the new child.
342
0
  if (aMenuItem) {
343
0
    nsCOMPtr<nsIContent> content = aMenuItem->GetContent();
344
0
    aMenuItem->SelectMenu(true);
345
0
    mCurrentMenu = aMenuItem;
346
0
    if (wasOpen && !aMenuItem->IsDisabled())
347
0
      aNewMenu = content;
348
0
  }
349
0
350
0
  // use an event so that hiding and showing can be done synchronously, which
351
0
  // avoids flickering
352
0
  nsCOMPtr<nsIRunnable> event =
353
0
    new nsMenuBarSwitchMenu(GetContent(), aOldMenu, aNewMenu, aSelectFirstItem);
354
0
  return mContent->OwnerDoc()->Dispatch(TaskCategory::Other,
355
0
                                        event.forget());
356
0
}
357
358
nsMenuFrame*
359
nsMenuBarFrame::Enter(WidgetGUIEvent* aEvent)
360
0
{
361
0
  if (!mCurrentMenu)
362
0
    return nullptr;
363
0
364
0
  if (mCurrentMenu->IsOpen())
365
0
    return mCurrentMenu->Enter(aEvent);
366
0
367
0
  return mCurrentMenu;
368
0
}
369
370
bool
371
nsMenuBarFrame::MenuClosed()
372
0
{
373
0
  SetActive(false);
374
0
  if (!mIsActive && mCurrentMenu) {
375
0
    mCurrentMenu->SelectMenu(false);
376
0
    mCurrentMenu = nullptr;
377
0
    return true;
378
0
  }
379
0
  return false;
380
0
}
381
382
void
383
nsMenuBarFrame::InstallKeyboardNavigator()
384
0
{
385
0
  nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
386
0
  if (pm)
387
0
    pm->SetActiveMenuBar(this, true);
388
0
}
389
390
void
391
nsMenuBarFrame::RemoveKeyboardNavigator()
392
0
{
393
0
  if (!mIsActive) {
394
0
    nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
395
0
    if (pm)
396
0
      pm->SetActiveMenuBar(this, false);
397
0
  }
398
0
}
399
400
void
401
nsMenuBarFrame::DestroyFrom(nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData)
402
0
{
403
0
  nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
404
0
  if (pm)
405
0
    pm->SetActiveMenuBar(this, false);
406
0
407
0
  mMenuBarListener->OnDestroyMenuBarFrame();
408
0
  mMenuBarListener = nullptr;
409
0
410
0
  nsBoxFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
411
0
}