michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: //
michael@0: // nsMenuFrame
michael@0: //
michael@0:
michael@0: #ifndef nsMenuFrame_h__
michael@0: #define nsMenuFrame_h__
michael@0:
michael@0: #include "nsIAtom.h"
michael@0: #include "nsCOMPtr.h"
michael@0:
michael@0: #include "nsBoxFrame.h"
michael@0: #include "nsFrameList.h"
michael@0: #include "nsGkAtoms.h"
michael@0: #include "nsMenuParent.h"
michael@0: #include "nsXULPopupManager.h"
michael@0: #include "nsITimer.h"
michael@0: #include "mozilla/Attributes.h"
michael@0:
michael@0: nsIFrame* NS_NewMenuFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
michael@0: nsIFrame* NS_NewMenuItemFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
michael@0:
michael@0: class nsIContent;
michael@0: class nsMenuBarFrame;
michael@0:
michael@0: #define NS_STATE_ACCELTEXT_IS_DERIVED NS_STATE_BOX_CHILD_RESERVED
michael@0:
michael@0: // the type of menuitem
michael@0: enum nsMenuType {
michael@0: // a normal menuitem where a command is carried out when activated
michael@0: eMenuType_Normal = 0,
michael@0: // a menuitem with a checkmark that toggles when activated
michael@0: eMenuType_Checkbox = 1,
michael@0: // a radio menuitem where only one of it and its siblings with the same
michael@0: // name attribute can be checked at a time
michael@0: eMenuType_Radio = 2
michael@0: };
michael@0:
michael@0: enum nsMenuListType {
michael@0: eNotMenuList, // not a menulist
michael@0: eReadonlyMenuList, //
michael@0: eEditableMenuList //
michael@0: };
michael@0:
michael@0: class nsMenuFrame;
michael@0:
michael@0: /**
michael@0: * nsMenuTimerMediator is a wrapper around an nsMenuFrame which can be safely
michael@0: * passed to timers. The class is reference counted unlike the underlying
michael@0: * nsMenuFrame, so that it will exist as long as the timer holds a reference
michael@0: * to it. The callback is delegated to the contained nsMenuFrame as long as
michael@0: * the contained nsMenuFrame has not been destroyed.
michael@0: */
michael@0: class nsMenuTimerMediator MOZ_FINAL : public nsITimerCallback
michael@0: {
michael@0: public:
michael@0: nsMenuTimerMediator(nsMenuFrame* aFrame);
michael@0: ~nsMenuTimerMediator();
michael@0:
michael@0: NS_DECL_ISUPPORTS
michael@0: NS_DECL_NSITIMERCALLBACK
michael@0:
michael@0: void ClearFrame();
michael@0:
michael@0: private:
michael@0:
michael@0: // Pointer to the wrapped frame.
michael@0: nsMenuFrame* mFrame;
michael@0: };
michael@0:
michael@0: class nsMenuFrame : public nsBoxFrame
michael@0: {
michael@0: public:
michael@0: nsMenuFrame(nsIPresShell* aShell, nsStyleContext* aContext);
michael@0:
michael@0: NS_DECL_QUERYFRAME_TARGET(nsMenuFrame)
michael@0: NS_DECL_QUERYFRAME
michael@0: NS_DECL_FRAMEARENA_HELPERS
michael@0:
michael@0: NS_IMETHOD DoLayout(nsBoxLayoutState& aBoxLayoutState) MOZ_OVERRIDE;
michael@0: virtual nsSize GetMinSize(nsBoxLayoutState& aBoxLayoutState) MOZ_OVERRIDE;
michael@0: virtual nsSize GetPrefSize(nsBoxLayoutState& aBoxLayoutState) MOZ_OVERRIDE;
michael@0:
michael@0: virtual void Init(nsIContent* aContent,
michael@0: nsIFrame* aParent,
michael@0: nsIFrame* aPrevInFlow) MOZ_OVERRIDE;
michael@0:
michael@0: #ifdef DEBUG_LAYOUT
michael@0: virtual nsresult SetDebug(nsBoxLayoutState& aState, bool aDebug) MOZ_OVERRIDE;
michael@0: #endif
michael@0:
michael@0: // The following methods are all overridden so that the menupopup
michael@0: // can be stored in a separate list, so that it doesn't impact reflow of the
michael@0: // actual menu item at all.
michael@0: virtual const nsFrameList& GetChildList(ChildListID aList) const MOZ_OVERRIDE;
michael@0: virtual void GetChildLists(nsTArray* aLists) const MOZ_OVERRIDE;
michael@0: virtual nsresult SetInitialChildList(ChildListID aListID,
michael@0: nsFrameList& aChildList) MOZ_OVERRIDE;
michael@0: virtual void DestroyFrom(nsIFrame* aDestructRoot) MOZ_OVERRIDE;
michael@0:
michael@0: // Overridden to prevent events from going to children of the menu.
michael@0: virtual void BuildDisplayListForChildren(nsDisplayListBuilder* aBuilder,
michael@0: const nsRect& aDirtyRect,
michael@0: const nsDisplayListSet& aLists) MOZ_OVERRIDE;
michael@0:
michael@0: // this method can destroy the frame
michael@0: virtual nsresult HandleEvent(nsPresContext* aPresContext,
michael@0: mozilla::WidgetGUIEvent* aEvent,
michael@0: nsEventStatus* aEventStatus) MOZ_OVERRIDE;
michael@0:
michael@0: virtual nsresult AppendFrames(ChildListID aListID,
michael@0: nsFrameList& aFrameList) MOZ_OVERRIDE;
michael@0:
michael@0: virtual nsresult InsertFrames(ChildListID aListID,
michael@0: nsIFrame* aPrevFrame,
michael@0: nsFrameList& aFrameList) MOZ_OVERRIDE;
michael@0:
michael@0: virtual nsresult RemoveFrame(ChildListID aListID,
michael@0: nsIFrame* aOldFrame) MOZ_OVERRIDE;
michael@0:
michael@0: virtual nsIAtom* GetType() const MOZ_OVERRIDE { return nsGkAtoms::menuFrame; }
michael@0:
michael@0: NS_IMETHOD SelectMenu(bool aActivateFlag);
michael@0:
michael@0: virtual nsIScrollableFrame* GetScrollTargetFrame() MOZ_OVERRIDE;
michael@0:
michael@0: // Retrieve the element that the menu should be anchored to. By default this is
michael@0: // the menu itself. However, the anchor attribute may refer to the value of an
michael@0: // anonid within the menu's binding, or, if not found, the id of an element in
michael@0: // the document.
michael@0: nsIContent* GetAnchor();
michael@0:
michael@0: /**
michael@0: * NOTE: OpenMenu will open the menu asynchronously.
michael@0: */
michael@0: void OpenMenu(bool aSelectFirstItem);
michael@0: // CloseMenu closes the menu asynchronously
michael@0: void CloseMenu(bool aDeselectMenu);
michael@0:
michael@0: bool IsChecked() { return mChecked; }
michael@0:
michael@0: NS_IMETHOD GetActiveChild(nsIDOMElement** aResult);
michael@0: NS_IMETHOD SetActiveChild(nsIDOMElement* aChild);
michael@0:
michael@0: // called when the Enter key is pressed while the menuitem is the current
michael@0: // one in its parent popup. This will carry out the command attached to
michael@0: // the menuitem. If the menu should be opened, this frame will be returned,
michael@0: // otherwise null will be returned.
michael@0: nsMenuFrame* Enter(mozilla::WidgetGUIEvent* aEvent);
michael@0:
michael@0: virtual void SetParent(nsIFrame* aParent) MOZ_OVERRIDE;
michael@0:
michael@0: virtual nsMenuParent *GetMenuParent() { return mMenuParent; }
michael@0: const nsAString& GetRadioGroupName() { return mGroupName; }
michael@0: nsMenuType GetMenuType() { return mType; }
michael@0: nsMenuPopupFrame* GetPopup();
michael@0:
michael@0: /**
michael@0: * @return true if this frame has a popup child frame.
michael@0: */
michael@0: bool HasPopup() const
michael@0: {
michael@0: return (GetStateBits() & NS_STATE_MENU_HAS_POPUP_LIST) != 0;
michael@0: }
michael@0:
michael@0:
michael@0: // nsMenuFrame methods
michael@0:
michael@0: bool IsOnMenuBar() { return mMenuParent && mMenuParent->IsMenuBar(); }
michael@0: bool IsOnActiveMenuBar() { return IsOnMenuBar() && mMenuParent->IsActive(); }
michael@0: virtual bool IsOpen();
michael@0: virtual bool IsMenu();
michael@0: nsMenuListType GetParentMenuListType();
michael@0: bool IsDisabled();
michael@0: void ToggleMenuState();
michael@0:
michael@0: // indiciate that the menu's popup has just been opened, so that the menu
michael@0: // can update its open state. This method modifies the open attribute on
michael@0: // the menu, so the frames could be gone after this call.
michael@0: void PopupOpened();
michael@0: // indiciate that the menu's popup has just been closed, so that the menu
michael@0: // can update its open state. The menu should be unhighlighted if
michael@0: // aDeselectedMenu is true. This method modifies the open attribute on
michael@0: // the menu, so the frames could be gone after this call.
michael@0: void PopupClosed(bool aDeselectMenu);
michael@0:
michael@0: // returns true if this is a menu on another menu popup. A menu is a submenu
michael@0: // if it has a parent popup or menupopup.
michael@0: bool IsOnMenu() { return mMenuParent && mMenuParent->IsMenu(); }
michael@0: void SetIsMenu(bool aIsMenu) { mIsMenu = aIsMenu; }
michael@0:
michael@0: #ifdef DEBUG_FRAME_DUMP
michael@0: virtual nsresult GetFrameName(nsAString& aResult) const MOZ_OVERRIDE
michael@0: {
michael@0: return MakeFrameName(NS_LITERAL_STRING("Menu"), aResult);
michael@0: }
michael@0: #endif
michael@0:
michael@0: static bool IsSizedToPopup(nsIContent* aContent, bool aRequireAlways);
michael@0:
michael@0: protected:
michael@0: friend class nsMenuTimerMediator;
michael@0: friend class nsASyncMenuInitialization;
michael@0: friend class nsMenuAttributeChangedEvent;
michael@0:
michael@0: /**
michael@0: * Initialize the popup list to the first popup frame within
michael@0: * aChildList. Removes the popup, if any, from aChildList.
michael@0: */
michael@0: void SetPopupFrame(nsFrameList& aChildList);
michael@0:
michael@0: /**
michael@0: * Get the popup frame list from the frame property.
michael@0: * @return the property value if it exists, nullptr otherwise.
michael@0: */
michael@0: nsFrameList* GetPopupList() const;
michael@0:
michael@0: /**
michael@0: * Destroy the popup list property. The list must exist and be empty.
michael@0: */
michael@0: void DestroyPopupList();
michael@0:
michael@0: // set mMenuParent to the nearest enclosing menu bar or menupopup frame of
michael@0: // aParent (or aParent itself). This is called when initializing the frame,
michael@0: // so aParent should be the expected parent of this frame.
michael@0: void InitMenuParent(nsIFrame* aParent);
michael@0:
michael@0: // Update the menu's type (normal, checkbox, radio).
michael@0: // This method can destroy the frame.
michael@0: void UpdateMenuType(nsPresContext* aPresContext);
michael@0: // Update the checked state of the menu, and for radios, clear any other
michael@0: // checked items. This method can destroy the frame.
michael@0: void UpdateMenuSpecialState(nsPresContext* aPresContext);
michael@0:
michael@0: // Examines the key node and builds the accelerator.
michael@0: void BuildAcceleratorText(bool aNotify);
michael@0:
michael@0: // Called to execute our command handler. This method can destroy the frame.
michael@0: void Execute(mozilla::WidgetGUIEvent *aEvent);
michael@0:
michael@0: // This method can destroy the frame
michael@0: virtual nsresult AttributeChanged(int32_t aNameSpaceID,
michael@0: nsIAtom* aAttribute,
michael@0: int32_t aModType) MOZ_OVERRIDE;
michael@0: virtual ~nsMenuFrame() { }
michael@0:
michael@0: bool SizeToPopup(nsBoxLayoutState& aState, nsSize& aSize);
michael@0:
michael@0: bool ShouldBlink();
michael@0: void StartBlinking(mozilla::WidgetGUIEvent* aEvent, bool aFlipChecked);
michael@0: void StopBlinking();
michael@0: void CreateMenuCommandEvent(mozilla::WidgetGUIEvent* aEvent,
michael@0: bool aFlipChecked);
michael@0: void PassMenuCommandEventToPopupManager();
michael@0:
michael@0: protected:
michael@0: #ifdef DEBUG_LAYOUT
michael@0: nsresult SetDebug(nsBoxLayoutState& aState, nsIFrame* aList, bool aDebug);
michael@0: #endif
michael@0: NS_HIDDEN_(nsresult) Notify(nsITimer* aTimer);
michael@0:
michael@0: bool mIsMenu; // Whether or not we can even have children or not.
michael@0: bool mChecked; // are we checked?
michael@0: bool mIgnoreAccelTextChange; // temporarily set while determining the accelerator key
michael@0: nsMenuType mType;
michael@0:
michael@0: nsMenuParent* mMenuParent; // Our parent menu.
michael@0:
michael@0: // Reference to the mediator which wraps this frame.
michael@0: nsRefPtr mTimerMediator;
michael@0:
michael@0: nsCOMPtr mOpenTimer;
michael@0: nsCOMPtr mBlinkTimer;
michael@0:
michael@0: uint8_t mBlinkState; // 0: not blinking, 1: off, 2: on
michael@0: nsRefPtr mDelayedMenuCommandEvent;
michael@0:
michael@0: nsString mGroupName;
michael@0:
michael@0: }; // class nsMenuFrame
michael@0:
michael@0: #endif