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: #ifndef nsView_h__ michael@0: #define nsView_h__ michael@0: michael@0: #include "nsCoord.h" michael@0: #include "nsRect.h" michael@0: #include "nsPoint.h" michael@0: #include "nsRegion.h" michael@0: #include "nsCRT.h" michael@0: #include "nsWidgetInitData.h" // for nsWindowType michael@0: #include "nsIWidgetListener.h" michael@0: #include "mozilla/EventForwards.h" michael@0: michael@0: class nsViewManager; michael@0: class nsIWidget; michael@0: class nsIFrame; michael@0: michael@0: // Enumerated type to indicate the visibility of a layer. michael@0: // hide - the layer is not shown. michael@0: // show - the layer is shown irrespective of the visibility of michael@0: // the layer's parent. michael@0: enum nsViewVisibility { michael@0: nsViewVisibility_kHide = 0, michael@0: nsViewVisibility_kShow = 1 michael@0: }; michael@0: michael@0: // Public view flags michael@0: michael@0: // Indicates that the view is using auto z-indexing michael@0: #define NS_VIEW_FLAG_AUTO_ZINDEX 0x0004 michael@0: michael@0: // Indicates that the view is a floating view. michael@0: #define NS_VIEW_FLAG_FLOATING 0x0008 michael@0: michael@0: //---------------------------------------------------------------------- michael@0: michael@0: /** michael@0: * View interface michael@0: * michael@0: * Views are NOT reference counted. Use the Destroy() member function to michael@0: * destroy a view. michael@0: * michael@0: * The lifetime of the view hierarchy is bounded by the lifetime of the michael@0: * view manager that owns the views. michael@0: * michael@0: * Most of the methods here are read-only. To set the corresponding properties michael@0: * of a view, go through nsViewManager. michael@0: */ michael@0: michael@0: class nsView MOZ_FINAL : public nsIWidgetListener michael@0: { michael@0: public: michael@0: friend class nsViewManager; michael@0: michael@0: NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW michael@0: michael@0: /** michael@0: * Get the view manager which "owns" the view. michael@0: * This method might require some expensive traversal work in the future. If you can get the michael@0: * view manager from somewhere else, do that instead. michael@0: * @result the view manager michael@0: */ michael@0: nsViewManager* GetViewManager() const { return mViewManager; } michael@0: michael@0: /** michael@0: * Find the view for the given widget, if there is one. michael@0: * @return the view the widget belongs to, or null if the widget doesn't michael@0: * belong to any view. michael@0: */ michael@0: static nsView* GetViewFor(nsIWidget* aWidget); michael@0: michael@0: /** michael@0: * Destroy the view. michael@0: * michael@0: * The view destroys its child views, and destroys and releases its michael@0: * widget (if it has one). michael@0: * michael@0: * Also informs the view manager that the view is destroyed by calling michael@0: * SetRootView(NULL) if the view is the root view and calling RemoveChild() michael@0: * otherwise. michael@0: */ michael@0: void Destroy(); michael@0: michael@0: /** michael@0: * Called to get the position of a view. michael@0: * The specified coordinates are relative to the parent view's origin, but michael@0: * are in appunits of this. michael@0: * This is the (0, 0) origin of the coordinate space established by this view. michael@0: * @param x out parameter for x position michael@0: * @param y out parameter for y position michael@0: */ michael@0: nsPoint GetPosition() const { michael@0: NS_ASSERTION(!IsRoot() || (mPosX == 0 && mPosY == 0), michael@0: "root views should always have explicit position of (0,0)"); michael@0: return nsPoint(mPosX, mPosY); michael@0: } michael@0: michael@0: /** michael@0: * Called to get the dimensions and position of the view's bounds. michael@0: * The view's bounds (x,y) are relative to the origin of the parent view, but michael@0: * are in appunits of this. michael@0: * The view's bounds (x,y) might not be the same as the view's position, michael@0: * if the view has content above or to the left of its origin. michael@0: * @param aBounds out parameter for bounds michael@0: */ michael@0: nsRect GetBounds() const { return mDimBounds; } michael@0: michael@0: /** michael@0: * The bounds of this view relative to this view. So this is the same as michael@0: * GetBounds except this is relative to this view instead of the parent view. michael@0: */ michael@0: nsRect GetDimensions() const { michael@0: nsRect r = mDimBounds; r.MoveBy(-mPosX, -mPosY); return r; michael@0: } michael@0: michael@0: /** michael@0: * Get the offset between the coordinate systems of |this| and aOther. michael@0: * Adding the return value to a point in the coordinate system of |this| michael@0: * will transform the point to the coordinate system of aOther. michael@0: * michael@0: * The offset is expressed in appunits of |this|. So if you are getting the michael@0: * offset between views in different documents that might have different michael@0: * appunits per devpixel ratios you need to be careful how you use the michael@0: * result. michael@0: * michael@0: * If aOther is null, this will return the offset of |this| from the michael@0: * root of the viewmanager tree. michael@0: * michael@0: * This function is fastest when aOther is an ancestor of |this|. michael@0: * michael@0: * NOTE: this actually returns the offset from aOther to |this|, but michael@0: * that offset is added to transform _coordinates_ from |this| to aOther. michael@0: */ michael@0: nsPoint GetOffsetTo(const nsView* aOther) const; michael@0: michael@0: /** michael@0: * Get the offset between the origin of |this| and the origin of aWidget. michael@0: * Adding the return value to a point in the coordinate system of |this| michael@0: * will transform the point to the coordinate system of aWidget. michael@0: * michael@0: * The offset is expressed in appunits of |this|. michael@0: */ michael@0: nsPoint GetOffsetToWidget(nsIWidget* aWidget) const; michael@0: michael@0: /** michael@0: * Takes a point aPt that is in the coordinate system of |this|'s parent view michael@0: * and converts it to be in the coordinate system of |this| taking into michael@0: * account the offset and any app unit per dev pixel ratio differences. michael@0: */ michael@0: nsPoint ConvertFromParentCoords(nsPoint aPt) const; michael@0: michael@0: /** michael@0: * Called to query the visibility state of a view. michael@0: * @result current visibility state michael@0: */ michael@0: nsViewVisibility GetVisibility() const { return mVis; } michael@0: michael@0: /** michael@0: * Get whether the view "floats" above all other views, michael@0: * which tells the compositor not to consider higher views in michael@0: * the view hierarchy that would geometrically intersect with michael@0: * this view. This is a hack, but it fixes some problems with michael@0: * views that need to be drawn in front of all other views. michael@0: * @result true if the view floats, false otherwise. michael@0: */ michael@0: bool GetFloating() const { return (mVFlags & NS_VIEW_FLAG_FLOATING) != 0; } michael@0: michael@0: /** michael@0: * Called to query the parent of the view. michael@0: * @result view's parent michael@0: */ michael@0: nsView* GetParent() const { return mParent; } michael@0: michael@0: /** michael@0: * The view's first child is the child which is earliest in document order. michael@0: * @result first child michael@0: */ michael@0: nsView* GetFirstChild() const { return mFirstChild; } michael@0: michael@0: /** michael@0: * Called to query the next sibling of the view. michael@0: * @result view's next sibling michael@0: */ michael@0: nsView* GetNextSibling() const { return mNextSibling; } michael@0: michael@0: /** michael@0: * Set the view's frame. michael@0: */ michael@0: void SetFrame(nsIFrame* aRootFrame) { mFrame = aRootFrame; } michael@0: michael@0: /** michael@0: * Retrieve the view's frame. michael@0: */ michael@0: nsIFrame* GetFrame() const { return mFrame; } michael@0: michael@0: /** michael@0: * Get the nearest widget in this view or a parent of this view and michael@0: * the offset from the widget's origin to this view's origin michael@0: * @param aOffset - if non-null the offset from this view's origin to the michael@0: * widget's origin (usually positive) expressed in appunits of this will be michael@0: * returned in aOffset. michael@0: * @return the widget closest to this view; can be null because some view trees michael@0: * don't have widgets at all (e.g., printing), but if any view in the view tree michael@0: * has a widget, then it's safe to assume this will not return null michael@0: */ michael@0: nsIWidget* GetNearestWidget(nsPoint* aOffset) const; michael@0: michael@0: /** michael@0: * Create a widget to associate with this view. This variant of michael@0: * CreateWidget*() will look around in the view hierarchy for an michael@0: * appropriate parent widget for the view. michael@0: * michael@0: * @param aWidgetInitData data used to initialize this view's widget before michael@0: * its create is called. michael@0: * @return error status michael@0: */ michael@0: nsresult CreateWidget(nsWidgetInitData *aWidgetInitData = nullptr, michael@0: bool aEnableDragDrop = true, michael@0: bool aResetVisibility = true); michael@0: michael@0: /** michael@0: * Create a widget for this view with an explicit parent widget. michael@0: * |aParentWidget| must be nonnull. The other params are the same michael@0: * as for |CreateWidget()|. michael@0: */ michael@0: nsresult CreateWidgetForParent(nsIWidget* aParentWidget, michael@0: nsWidgetInitData *aWidgetInitData = nullptr, michael@0: bool aEnableDragDrop = true, michael@0: bool aResetVisibility = true); michael@0: michael@0: /** michael@0: * Create a popup widget for this view. Pass |aParentWidget| to michael@0: * explicitly set the popup's parent. If it's not passed, the view michael@0: * hierarchy will be searched for an appropriate parent widget. The michael@0: * other params are the same as for |CreateWidget()|, except that michael@0: * |aWidgetInitData| must be nonnull. michael@0: */ michael@0: nsresult CreateWidgetForPopup(nsWidgetInitData *aWidgetInitData, michael@0: nsIWidget* aParentWidget = nullptr, michael@0: bool aEnableDragDrop = true, michael@0: bool aResetVisibility = true); michael@0: michael@0: /** michael@0: * Destroys the associated widget for this view. If this method is michael@0: * not called explicitly, the widget when be destroyed when its michael@0: * view gets destroyed. michael@0: */ michael@0: void DestroyWidget(); michael@0: michael@0: /** michael@0: * Attach/detach a top level widget from this view. When attached, the view michael@0: * updates the widget's device context and allows the view to begin receiving michael@0: * gecko events. The underlying base window associated with the widget will michael@0: * continues to receive events it expects. michael@0: * michael@0: * An attached widget will not be destroyed when the view is destroyed, michael@0: * allowing the recycling of a single top level widget over multiple views. michael@0: * michael@0: * @param aWidget The widget to attach to / detach from. michael@0: */ michael@0: nsresult AttachToTopLevelWidget(nsIWidget* aWidget); michael@0: nsresult DetachFromTopLevelWidget(); michael@0: michael@0: /** michael@0: * Returns a flag indicating whether the view owns it's widget michael@0: * or is attached to an existing top level widget. michael@0: */ michael@0: bool IsAttachedToTopLevel() const { return mWidgetIsTopLevel; } michael@0: michael@0: /** michael@0: * In 4.0, the "cutout" nature of a view is queryable. michael@0: * If we believe that all cutout view have a native widget, this michael@0: * could be a replacement. michael@0: * @param aWidget out parameter for widget that this view contains, michael@0: * or nullptr if there is none. michael@0: */ michael@0: nsIWidget* GetWidget() const { return mWindow; } michael@0: michael@0: /** michael@0: * Returns true if the view has a widget associated with it. michael@0: */ michael@0: bool HasWidget() const { return mWindow != nullptr; } michael@0: michael@0: void SetForcedRepaint(bool aForceRepaint) { michael@0: mForcedRepaint = aForceRepaint; michael@0: } michael@0: michael@0: /** michael@0: * Make aWidget direct its events to this view. michael@0: * The caller must call DetachWidgetEventHandler before this view michael@0: * is destroyed. michael@0: */ michael@0: void AttachWidgetEventHandler(nsIWidget* aWidget); michael@0: /** michael@0: * Stop aWidget directing its events to this view. michael@0: */ michael@0: void DetachWidgetEventHandler(nsIWidget* aWidget); michael@0: michael@0: #ifdef DEBUG michael@0: /** michael@0: * Output debug info to FILE michael@0: * @param out output file handle michael@0: * @param aIndent indentation depth michael@0: * NOTE: virtual so that debugging tools not linked into gklayout can access it michael@0: */ michael@0: virtual void List(FILE* out, int32_t aIndent = 0) const; michael@0: #endif // DEBUG michael@0: michael@0: /** michael@0: * @result true iff this is the root view for its view manager michael@0: */ michael@0: bool IsRoot() const; michael@0: michael@0: nsIntRect CalcWidgetBounds(nsWindowType aType); michael@0: michael@0: // This is an app unit offset to add when converting view coordinates to michael@0: // widget coordinates. It is the offset in view coordinates from widget michael@0: // origin (unlike views, widgets can't extend above or to the left of their michael@0: // origin) to view origin expressed in appunits of this. michael@0: nsPoint ViewToWidgetOffset() const { return mViewToWidgetOffset; } michael@0: michael@0: /** michael@0: * Called to indicate that the position of the view has been changed. michael@0: * The specified coordinates are in the parent view's coordinate space. michael@0: * @param x new x position michael@0: * @param y new y position michael@0: */ michael@0: void SetPosition(nscoord aX, nscoord aY); michael@0: michael@0: /** michael@0: * Called to indicate that the z-index of a view has been changed. michael@0: * The z-index is relative to all siblings of the view. michael@0: * @param aAuto Indicate that the z-index of a view is "auto". An "auto" z-index michael@0: * means that the view does not define a new stacking context, michael@0: * which means that the z-indicies of the view's children are michael@0: * relative to the view's siblings. michael@0: * @param zindex new z depth michael@0: */ michael@0: void SetZIndex(bool aAuto, int32_t aZIndex); michael@0: bool GetZIndexIsAuto() const { return (mVFlags & NS_VIEW_FLAG_AUTO_ZINDEX) != 0; } michael@0: int32_t GetZIndex() const { return mZIndex; } michael@0: michael@0: void SetParent(nsView *aParent) { mParent = aParent; } michael@0: void SetNextSibling(nsView *aSibling) michael@0: { michael@0: NS_ASSERTION(aSibling != this, "Can't be our own sibling!"); michael@0: mNextSibling = aSibling; michael@0: } michael@0: michael@0: nsRegion* GetDirtyRegion() { michael@0: if (!mDirtyRegion) { michael@0: NS_ASSERTION(!mParent || GetFloating(), michael@0: "Only display roots should have dirty regions"); michael@0: mDirtyRegion = new nsRegion(); michael@0: NS_ASSERTION(mDirtyRegion, "Out of memory!"); michael@0: } michael@0: return mDirtyRegion; michael@0: } michael@0: michael@0: // nsIWidgetListener michael@0: virtual nsIPresShell* GetPresShell() MOZ_OVERRIDE; michael@0: virtual nsView* GetView() MOZ_OVERRIDE { return this; } michael@0: virtual bool WindowMoved(nsIWidget* aWidget, int32_t x, int32_t y) MOZ_OVERRIDE; michael@0: virtual bool WindowResized(nsIWidget* aWidget, int32_t aWidth, int32_t aHeight) MOZ_OVERRIDE; michael@0: virtual bool RequestWindowClose(nsIWidget* aWidget) MOZ_OVERRIDE; michael@0: virtual void WillPaintWindow(nsIWidget* aWidget) MOZ_OVERRIDE; michael@0: virtual bool PaintWindow(nsIWidget* aWidget, nsIntRegion aRegion) MOZ_OVERRIDE; michael@0: virtual void DidPaintWindow() MOZ_OVERRIDE; michael@0: virtual void DidCompositeWindow() MOZ_OVERRIDE; michael@0: virtual void RequestRepaint() MOZ_OVERRIDE; michael@0: virtual nsEventStatus HandleEvent(mozilla::WidgetGUIEvent* aEvent, michael@0: bool aUseAttachedEvents) MOZ_OVERRIDE; michael@0: michael@0: virtual ~nsView(); michael@0: michael@0: nsPoint GetOffsetTo(const nsView* aOther, const int32_t aAPD) const; michael@0: nsIWidget* GetNearestWidget(nsPoint* aOffset, const int32_t aAPD) const; michael@0: michael@0: private: michael@0: nsView(nsViewManager* aViewManager = nullptr, michael@0: nsViewVisibility aVisibility = nsViewVisibility_kShow); michael@0: michael@0: bool ForcedRepaint() { return mForcedRepaint; } michael@0: michael@0: // Do the actual work of ResetWidgetBounds, unconditionally. Don't michael@0: // call this method if we have no widget. michael@0: void DoResetWidgetBounds(bool aMoveOnly, bool aInvalidateChangedSize); michael@0: void InitializeWindow(bool aEnableDragDrop, bool aResetVisibility); michael@0: michael@0: bool IsEffectivelyVisible(); michael@0: michael@0: /** michael@0: * Called to indicate that the dimensions of the view have been changed. michael@0: * The x and y coordinates may be < 0, indicating that the view extends above michael@0: * or to the left of its origin position. The term 'dimensions' indicates it michael@0: * is relative to this view. michael@0: */ michael@0: void SetDimensions(const nsRect &aRect, bool aPaint = true, michael@0: bool aResizeWidget = true); michael@0: michael@0: /** michael@0: * Called to indicate that the visibility of a view has been michael@0: * changed. michael@0: * @param visibility new visibility state michael@0: */ michael@0: void SetVisibility(nsViewVisibility visibility); michael@0: michael@0: /** michael@0: * Set/Get whether the view "floats" above all other views, michael@0: * which tells the compositor not to consider higher views in michael@0: * the view hierarchy that would geometrically intersect with michael@0: * this view. This is a hack, but it fixes some problems with michael@0: * views that need to be drawn in front of all other views. michael@0: * @result true if the view floats, false otherwise. michael@0: */ michael@0: void SetFloating(bool aFloatingView); michael@0: michael@0: // Helper function to get mouse grabbing off this view (by moving it to the michael@0: // parent, if we can) michael@0: void DropMouseGrabbing(); michael@0: michael@0: // Same as GetBounds but converts to parent appunits if they are different. michael@0: nsRect GetBoundsInParentUnits() const; michael@0: michael@0: bool HasNonEmptyDirtyRegion() { michael@0: return mDirtyRegion && !mDirtyRegion->IsEmpty(); michael@0: } michael@0: michael@0: void InsertChild(nsView *aChild, nsView *aSibling); michael@0: void RemoveChild(nsView *aChild); michael@0: michael@0: void ResetWidgetBounds(bool aRecurse, bool aForceSync); michael@0: void AssertNoWindow(); michael@0: michael@0: void NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible); michael@0: michael@0: // Update the cached RootViewManager for all view manager descendents, michael@0: // If the hierarchy is being removed, aViewManagerParent points to the view michael@0: // manager for the hierarchy's old parent, and will have its mouse grab michael@0: // released if it points to any view in this view hierarchy. michael@0: void InvalidateHierarchy(nsViewManager *aViewManagerParent); michael@0: michael@0: nsViewManager *mViewManager; michael@0: nsView *mParent; michael@0: nsIWidget *mWindow; michael@0: nsView *mNextSibling; michael@0: nsView *mFirstChild; michael@0: nsIFrame *mFrame; michael@0: nsRegion *mDirtyRegion; michael@0: int32_t mZIndex; michael@0: nsViewVisibility mVis; michael@0: // position relative our parent view origin but in our appunits michael@0: nscoord mPosX, mPosY; michael@0: // relative to parent, but in our appunits michael@0: nsRect mDimBounds; michael@0: // in our appunits michael@0: nsPoint mViewToWidgetOffset; michael@0: uint32_t mVFlags; michael@0: bool mWidgetIsTopLevel; michael@0: bool mForcedRepaint; michael@0: }; michael@0: michael@0: #endif