nsMenuBarFrame.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. //
  6. // nsMenuBarFrame
  7. //
  8. #ifndef nsMenuBarFrame_h__
  9. #define nsMenuBarFrame_h__
  10. #include "mozilla/Attributes.h"
  11. #include "nsIAtom.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsBoxFrame.h"
  14. #include "nsMenuFrame.h"
  15. #include "nsMenuBarListener.h"
  16. #include "nsMenuParent.h"
  17. class nsIContent;
  18. nsIFrame* NS_NewMenuBarFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
  19. class nsMenuBarFrame final : public nsBoxFrame, public nsMenuParent
  20. {
  21. public:
  22. NS_DECL_QUERYFRAME_TARGET(nsMenuBarFrame)
  23. NS_DECL_QUERYFRAME
  24. NS_DECL_FRAMEARENA_HELPERS
  25. explicit nsMenuBarFrame(nsStyleContext* aContext);
  26. // nsMenuParent interface
  27. virtual nsMenuFrame* GetCurrentMenuItem() override;
  28. NS_IMETHOD SetCurrentMenuItem(nsMenuFrame* aMenuItem) override;
  29. virtual void CurrentMenuIsBeingDestroyed() override;
  30. NS_IMETHOD ChangeMenuItem(nsMenuFrame* aMenuItem,
  31. bool aSelectFirstItem,
  32. bool aFromKey) override;
  33. NS_IMETHOD SetActive(bool aActiveFlag) override;
  34. virtual bool IsMenuBar() override { return true; }
  35. virtual bool IsContextMenu() override { return false; }
  36. virtual bool IsActive() override { return mIsActive; }
  37. virtual bool IsMenu() override { return false; }
  38. virtual bool IsOpen() override { return true; } // menubars are considered always open
  39. bool IsMenuOpen() { return mCurrentMenu && mCurrentMenu->IsOpen(); }
  40. void InstallKeyboardNavigator();
  41. void RemoveKeyboardNavigator();
  42. virtual void Init(nsIContent* aContent,
  43. nsContainerFrame* aParent,
  44. nsIFrame* aPrevInFlow) override;
  45. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  46. virtual void LockMenuUntilClosed(bool aLock) override {}
  47. virtual bool IsMenuLocked() override { return false; }
  48. // Non-interface helpers
  49. // The 'stay active' flag is set when navigating from one top-level menu
  50. // to another, to prevent the menubar from deactivating and submenus from
  51. // firing extra DOMMenuItemActive events.
  52. bool GetStayActive() { return mStayActive; }
  53. void SetStayActive(bool aStayActive) { mStayActive = aStayActive; }
  54. // Called when a menu on the menu bar is clicked on. Returns a menu if one
  55. // needs to be closed.
  56. nsMenuFrame* ToggleMenuActiveState();
  57. bool IsActiveByKeyboard() { return mActiveByKeyboard; }
  58. void SetActiveByKeyboard() { mActiveByKeyboard = true; }
  59. // indicate that a menu on the menubar was closed. Returns true if the caller
  60. // may deselect the menuitem.
  61. virtual bool MenuClosed() override;
  62. // Called when Enter is pressed while the menubar is focused. If the current
  63. // menu is open, let the child handle the key.
  64. nsMenuFrame* Enter(mozilla::WidgetGUIEvent* aEvent);
  65. // Used to handle ALT+key combos
  66. nsMenuFrame* FindMenuWithShortcut(nsIDOMKeyEvent* aKeyEvent);
  67. virtual bool IsFrameOfType(uint32_t aFlags) const override
  68. {
  69. // Override bogus IsFrameOfType in nsBoxFrame.
  70. if (aFlags & (nsIFrame::eReplacedContainsBlock | nsIFrame::eReplaced))
  71. return false;
  72. return nsBoxFrame::IsFrameOfType(aFlags);
  73. }
  74. #ifdef DEBUG_FRAME_DUMP
  75. virtual nsresult GetFrameName(nsAString& aResult) const override
  76. {
  77. return MakeFrameName(NS_LITERAL_STRING("MenuBar"), aResult);
  78. }
  79. #endif
  80. protected:
  81. RefPtr<nsMenuBarListener> mMenuBarListener; // The listener that tells us about key and mouse events.
  82. // flag that is temporarily set when switching from one menu on the menubar to another
  83. // to indicate that the menubar should not be deactivated.
  84. bool mStayActive;
  85. bool mIsActive; // Whether or not the menu bar is active (a menu item is highlighted or shown).
  86. // whether the menubar was made active via the keyboard.
  87. bool mActiveByKeyboard;
  88. // The current menu that is active (highlighted), which may not be open. This will
  89. // be null if no menu is active.
  90. nsMenuFrame* mCurrentMenu;
  91. mozilla::dom::EventTarget* mTarget;
  92. }; // class nsMenuBarFrame
  93. #endif