nsMenuFrame.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // nsMenuFrame
  7. //
  8. #ifndef nsMenuFrame_h__
  9. #define nsMenuFrame_h__
  10. #include "nsIAtom.h"
  11. #include "nsCOMPtr.h"
  12. #include "nsBoxFrame.h"
  13. #include "nsFrameList.h"
  14. #include "nsGkAtoms.h"
  15. #include "nsMenuParent.h"
  16. #include "nsXULPopupManager.h"
  17. #include "nsITimer.h"
  18. #include "mozilla/Attributes.h"
  19. nsIFrame* NS_NewMenuFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
  20. nsIFrame* NS_NewMenuItemFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
  21. class nsIContent;
  22. #define NS_STATE_ACCELTEXT_IS_DERIVED NS_STATE_BOX_CHILD_RESERVED
  23. // the type of menuitem
  24. enum nsMenuType {
  25. // a normal menuitem where a command is carried out when activated
  26. eMenuType_Normal = 0,
  27. // a menuitem with a checkmark that toggles when activated
  28. eMenuType_Checkbox = 1,
  29. // a radio menuitem where only one of it and its siblings with the same
  30. // name attribute can be checked at a time
  31. eMenuType_Radio = 2
  32. };
  33. enum nsMenuListType {
  34. eNotMenuList, // not a menulist
  35. eReadonlyMenuList, // <menulist/>
  36. eEditableMenuList // <menulist editable="true"/>
  37. };
  38. class nsMenuFrame;
  39. /**
  40. * nsMenuTimerMediator is a wrapper around an nsMenuFrame which can be safely
  41. * passed to timers. The class is reference counted unlike the underlying
  42. * nsMenuFrame, so that it will exist as long as the timer holds a reference
  43. * to it. The callback is delegated to the contained nsMenuFrame as long as
  44. * the contained nsMenuFrame has not been destroyed.
  45. */
  46. class nsMenuTimerMediator final : public nsITimerCallback
  47. {
  48. public:
  49. explicit nsMenuTimerMediator(nsMenuFrame* aFrame);
  50. NS_DECL_ISUPPORTS
  51. NS_DECL_NSITIMERCALLBACK
  52. void ClearFrame();
  53. private:
  54. ~nsMenuTimerMediator();
  55. // Pointer to the wrapped frame.
  56. nsMenuFrame* mFrame;
  57. };
  58. class nsMenuFrame final : public nsBoxFrame
  59. {
  60. public:
  61. explicit nsMenuFrame(nsStyleContext* aContext);
  62. NS_DECL_QUERYFRAME_TARGET(nsMenuFrame)
  63. NS_DECL_QUERYFRAME
  64. NS_DECL_FRAMEARENA_HELPERS
  65. NS_IMETHOD DoXULLayout(nsBoxLayoutState& aBoxLayoutState) override;
  66. virtual nsSize GetXULMinSize(nsBoxLayoutState& aBoxLayoutState) override;
  67. virtual nsSize GetXULPrefSize(nsBoxLayoutState& aBoxLayoutState) override;
  68. virtual void Init(nsIContent* aContent,
  69. nsContainerFrame* aParent,
  70. nsIFrame* aPrevInFlow) override;
  71. #ifdef DEBUG_LAYOUT
  72. virtual nsresult SetXULDebug(nsBoxLayoutState& aState, bool aDebug) override;
  73. #endif
  74. // The following methods are all overridden so that the menupopup
  75. // can be stored in a separate list, so that it doesn't impact reflow of the
  76. // actual menu item at all.
  77. virtual const nsFrameList& GetChildList(ChildListID aList) const override;
  78. virtual void GetChildLists(nsTArray<ChildList>* aLists) const override;
  79. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  80. // Overridden to prevent events from going to children of the menu.
  81. virtual void BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
  82. const nsDisplayListSet& aLists) override;
  83. // this method can destroy the frame
  84. virtual nsresult HandleEvent(nsPresContext* aPresContext,
  85. mozilla::WidgetGUIEvent* aEvent,
  86. nsEventStatus* aEventStatus) override;
  87. virtual void SetInitialChildList(ChildListID aListID,
  88. nsFrameList& aChildList) override;
  89. virtual void AppendFrames(ChildListID aListID,
  90. nsFrameList& aFrameList) override;
  91. virtual void InsertFrames(ChildListID aListID,
  92. nsIFrame* aPrevFrame,
  93. nsFrameList& aFrameList) override;
  94. virtual void RemoveFrame(ChildListID aListID,
  95. nsIFrame* aOldFrame) override;
  96. virtual nsIAtom* GetType() const override { return nsGkAtoms::menuFrame; }
  97. NS_IMETHOD SelectMenu(bool aActivateFlag);
  98. virtual nsIScrollableFrame* GetScrollTargetFrame() override;
  99. // Retrieve the element that the menu should be anchored to. By default this is
  100. // the menu itself. However, the anchor attribute may refer to the value of an
  101. // anonid within the menu's binding, or, if not found, the id of an element in
  102. // the document.
  103. nsIContent* GetAnchor();
  104. /**
  105. * NOTE: OpenMenu will open the menu asynchronously.
  106. */
  107. void OpenMenu(bool aSelectFirstItem);
  108. // CloseMenu closes the menu asynchronously
  109. void CloseMenu(bool aDeselectMenu);
  110. bool IsChecked() { return mChecked; }
  111. NS_IMETHOD GetActiveChild(nsIDOMElement** aResult);
  112. NS_IMETHOD SetActiveChild(nsIDOMElement* aChild);
  113. // called when the Enter key is pressed while the menuitem is the current
  114. // one in its parent popup. This will carry out the command attached to
  115. // the menuitem. If the menu should be opened, this frame will be returned,
  116. // otherwise null will be returned.
  117. nsMenuFrame* Enter(mozilla::WidgetGUIEvent* aEvent);
  118. // Return the nearest menu bar or menupopup ancestor frame.
  119. nsMenuParent* GetMenuParent() const;
  120. const nsAString& GetRadioGroupName() { return mGroupName; }
  121. nsMenuType GetMenuType() { return mType; }
  122. nsMenuPopupFrame* GetPopup();
  123. /**
  124. * @return true if this frame has a popup child frame.
  125. */
  126. bool HasPopup() const
  127. {
  128. return (GetStateBits() & NS_STATE_MENU_HAS_POPUP_LIST) != 0;
  129. }
  130. // nsMenuFrame methods
  131. bool IsOnMenuBar() const
  132. {
  133. nsMenuParent* menuParent = GetMenuParent();
  134. return menuParent && menuParent->IsMenuBar();
  135. }
  136. bool IsOnActiveMenuBar() const
  137. {
  138. nsMenuParent* menuParent = GetMenuParent();
  139. return menuParent && menuParent->IsMenuBar() && menuParent->IsActive();
  140. }
  141. virtual bool IsOpen();
  142. virtual bool IsMenu();
  143. nsMenuListType GetParentMenuListType();
  144. bool IsDisabled();
  145. void ToggleMenuState();
  146. // indiciate that the menu's popup has just been opened, so that the menu
  147. // can update its open state. This method modifies the open attribute on
  148. // the menu, so the frames could be gone after this call.
  149. void PopupOpened();
  150. // indiciate that the menu's popup has just been closed, so that the menu
  151. // can update its open state. The menu should be unhighlighted if
  152. // aDeselectedMenu is true. This method modifies the open attribute on
  153. // the menu, so the frames could be gone after this call.
  154. void PopupClosed(bool aDeselectMenu);
  155. // returns true if this is a menu on another menu popup. A menu is a submenu
  156. // if it has a parent popup or menupopup.
  157. bool IsOnMenu() const
  158. {
  159. nsMenuParent* menuParent = GetMenuParent();
  160. return menuParent && menuParent->IsMenu();
  161. }
  162. void SetIsMenu(bool aIsMenu) { mIsMenu = aIsMenu; }
  163. #ifdef DEBUG_FRAME_DUMP
  164. virtual nsresult GetFrameName(nsAString& aResult) const override
  165. {
  166. return MakeFrameName(NS_LITERAL_STRING("Menu"), aResult);
  167. }
  168. #endif
  169. static bool IsSizedToPopup(nsIContent* aContent, bool aRequireAlways);
  170. protected:
  171. friend class nsMenuTimerMediator;
  172. friend class nsASyncMenuInitialization;
  173. friend class nsMenuAttributeChangedEvent;
  174. /**
  175. * Initialize the popup list to the first popup frame within
  176. * aChildList. Removes the popup, if any, from aChildList.
  177. */
  178. void SetPopupFrame(nsFrameList& aChildList);
  179. /**
  180. * Get the popup frame list from the frame property.
  181. * @return the property value if it exists, nullptr otherwise.
  182. */
  183. nsFrameList* GetPopupList() const;
  184. /**
  185. * Destroy the popup list property. The list must exist and be empty.
  186. */
  187. void DestroyPopupList();
  188. // Update the menu's type (normal, checkbox, radio).
  189. // This method can destroy the frame.
  190. void UpdateMenuType();
  191. // Update the checked state of the menu, and for radios, clear any other
  192. // checked items. This method can destroy the frame.
  193. void UpdateMenuSpecialState();
  194. // Examines the key node and builds the accelerator.
  195. void BuildAcceleratorText(bool aNotify);
  196. // Called to execute our command handler. This method can destroy the frame.
  197. void Execute(mozilla::WidgetGUIEvent *aEvent);
  198. // This method can destroy the frame
  199. virtual nsresult AttributeChanged(int32_t aNameSpaceID,
  200. nsIAtom* aAttribute,
  201. int32_t aModType) override;
  202. virtual ~nsMenuFrame() { }
  203. bool SizeToPopup(nsBoxLayoutState& aState, nsSize& aSize);
  204. bool ShouldBlink();
  205. void StartBlinking(mozilla::WidgetGUIEvent* aEvent, bool aFlipChecked);
  206. void StopBlinking();
  207. void CreateMenuCommandEvent(mozilla::WidgetGUIEvent* aEvent,
  208. bool aFlipChecked);
  209. void PassMenuCommandEventToPopupManager();
  210. protected:
  211. #ifdef DEBUG_LAYOUT
  212. nsresult SetXULDebug(nsBoxLayoutState& aState, nsIFrame* aList, bool aDebug);
  213. #endif
  214. nsresult Notify(nsITimer* aTimer);
  215. bool mIsMenu; // Whether or not we can even have children or not.
  216. bool mChecked; // are we checked?
  217. bool mIgnoreAccelTextChange; // temporarily set while determining the accelerator key
  218. nsMenuType mType;
  219. // Reference to the mediator which wraps this frame.
  220. RefPtr<nsMenuTimerMediator> mTimerMediator;
  221. nsCOMPtr<nsITimer> mOpenTimer;
  222. nsCOMPtr<nsITimer> mBlinkTimer;
  223. uint8_t mBlinkState; // 0: not blinking, 1: off, 2: on
  224. RefPtr<nsXULMenuCommandEvent> mDelayedMenuCommandEvent;
  225. nsString mGroupName;
  226. }; // class nsMenuFrame
  227. #endif