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