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/. */
15 #include "nsWidgetInitData.h" // for nsWindowType
16 #include "nsIWidgetListener.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/EventForwards.h"
27 } // namespace mozilla
30 * nsView's serve two main purposes: 1) a bridge between nsIFrame's and
31 * nsIWidget's, 2) linking the frame tree of a(n) (in-process) subdocument with
32 * its parent document frame tree. Historically views were used for more things,
33 * but their role has been reduced, and could be reduced to nothing in the
34 * future (bug 337801 tracks removing views). Views are generally associated
35 * with a frame. A view that does not have a frame is called an anonymous view.
36 * Some frames also have associated widgets (think os level windows). If a frame
37 * has a widget it must also have a view, but not all frames with views will
40 * Only four types of frames can have a view: root frames (ViewportFrame),
41 * subdocument frames (nsSubDocumentFrame),
42 * menu popup frames (nsMenuPopupFrame), and list control frames
43 * (nsListControlFrame). Root frames and subdocument frames have views to link
44 * the two documents together (the frame trees do not link up otherwise).
45 * Menu popup frames, and list control frames have views because
46 * they (sometimes) need to create widgets.
47 * Menu popup frames handles xul popups, which is anything
48 * where we need content to go over top the main window at an os level. List
49 * control frames handle select popups/dropdowns in non-e10s mode.
51 * The term "root view" refers to the root view of a document. Just like root
52 * frames, root views can have parent views. Only the root view of the root
53 * document in the process will not have a parent.
55 * All views are created by their frames except root views. Root views are
56 * special. Root views are created in nsDocumentViewer::MakeWindow before the
57 * root frame is created, so the root view will not have a frame very early in
60 * Subdocument frames have an anonymous (no frame associated
61 * with it) inner view that is a child of their "outer" view.
63 * On a subdocument frame the inner view serves as the parent of the
64 * root view of the subdocument. The outer and inner view of the subdocument
65 * frame belong to the subdocument frame and hence to the parent document. The
66 * root view of the subdocument belongs to the subdocument.
67 * nsLayoutUtils::GetCrossDocParentFrame and nsPresContext::GetParentPresContext
68 * depend on this view structure and are the main way that we traverse across
69 * the document boundary in layout.
71 * When the load of a new document is started in the subdocument, the creation
72 * of the new subdocument and destruction of the old subdocument are not
73 * linked. (This creation and destruction is handled in nsDocumentViewer.cpp.)
74 * This means that the old and new document will both exist at the same time
75 * during the loading of the new document. During this period the inner view of
76 * the subdocument parent will be the parent of two root views. This means that
77 * during this period there is a choice for which subdocument we draw,
78 * nsSubDocumentFrame::GetSubdocumentPresShellForPainting is what makes that
79 * choice. Note that this might not be a totally free choice, ie there might be
80 * hidden dependencies and bugs if the way we choose is changed.
82 * One thing that is special about the root view of a chrome window is that
83 * instead of creating a widget for the view, they can "attach" to the
84 * existing widget that was created by appshell code or something else. (see
85 * nsDocumentViewer::ShouldAttachToTopLevel)
88 // Enumerated type to indicate the visibility of a layer.
89 // hide - the layer is not shown.
90 // show - the layer is shown irrespective of the visibility of
91 // the layer's parent.
92 enum nsViewVisibility
{
93 nsViewVisibility_kHide
= 0,
94 nsViewVisibility_kShow
= 1
99 // Indicates that the view is using auto z-indexing
100 #define NS_VIEW_FLAG_AUTO_ZINDEX 0x0004
102 // Indicates that the view is a floating view.
103 #define NS_VIEW_FLAG_FLOATING 0x0008
105 //----------------------------------------------------------------------
110 * Views are NOT reference counted. Use the Destroy() member function to
113 * The lifetime of the view hierarchy is bounded by the lifetime of the
114 * view manager that owns the views.
116 * Most of the methods here are read-only. To set the corresponding properties
117 * of a view, go through nsViewManager.
120 class nsView final
: public nsIWidgetListener
{
122 friend class nsViewManager
;
124 typedef mozilla::LayoutDeviceIntRect LayoutDeviceIntRect
;
125 typedef mozilla::LayoutDeviceIntRegion LayoutDeviceIntRegion
;
127 void operator delete(void* ptr
) { ::operator delete(ptr
); }
130 * Get the view manager which "owns" the view.
131 * This method might require some expensive traversal work in the future. If
132 * you can get the view manager from somewhere else, do that instead.
133 * @result the view manager
135 nsViewManager
* GetViewManager() const { return mViewManager
; }
138 * Find the view for the given widget, if there is one.
139 * @return the view the widget belongs to, or null if the widget doesn't
140 * belong to any view.
142 static nsView
* GetViewFor(nsIWidget
* aWidget
);
147 * The view destroys its child views, and destroys and releases its
148 * widget (if it has one).
150 * Also informs the view manager that the view is destroyed by calling
151 * SetRootView(NULL) if the view is the root view and calling RemoveChild()
157 * Called to get the position of a view.
158 * The specified coordinates are relative to the parent view's origin, but
159 * are in appunits of this.
160 * This is the (0, 0) origin of the coordinate space established by this view.
161 * @param x out parameter for x position
162 * @param y out parameter for y position
164 nsPoint
GetPosition() const {
165 NS_ASSERTION(!IsRoot() || (mPosX
== 0 && mPosY
== 0),
166 "root views should always have explicit position of (0,0)");
167 return nsPoint(mPosX
, mPosY
);
171 * Called to get the dimensions and position of the view's bounds.
172 * The view's bounds (x,y) are relative to the origin of the parent view, but
173 * are in appunits of this.
174 * The view's bounds (x,y) might not be the same as the view's position,
175 * if the view has content above or to the left of its origin.
176 * @param aBounds out parameter for bounds
178 nsRect
GetBounds() const { return mDimBounds
; }
181 * The bounds of this view relative to this view. So this is the same as
182 * GetBounds except this is relative to this view instead of the parent view.
184 nsRect
GetDimensions() const {
185 nsRect r
= mDimBounds
;
186 r
.MoveBy(-mPosX
, -mPosY
);
191 * Get the offset between the coordinate systems of |this| and aOther.
192 * Adding the return value to a point in the coordinate system of |this|
193 * will transform the point to the coordinate system of aOther.
195 * The offset is expressed in appunits of |this|. So if you are getting the
196 * offset between views in different documents that might have different
197 * appunits per devpixel ratios you need to be careful how you use the
200 * If aOther is null, this will return the offset of |this| from the
201 * root of the viewmanager tree.
203 * This function is fastest when aOther is an ancestor of |this|.
205 * NOTE: this actually returns the offset from aOther to |this|, but
206 * that offset is added to transform _coordinates_ from |this| to aOther.
208 nsPoint
GetOffsetTo(const nsView
* aOther
) const;
211 * Get the offset between the origin of |this| and the origin of aWidget.
212 * Adding the return value to a point in the coordinate system of |this|
213 * will transform the point to the coordinate system of aWidget.
215 * The offset is expressed in appunits of |this|.
217 nsPoint
GetOffsetToWidget(nsIWidget
* aWidget
) const;
220 * Takes a point aPt that is in the coordinate system of |this|'s parent view
221 * and converts it to be in the coordinate system of |this| taking into
222 * account the offset and any app unit per dev pixel ratio differences.
224 nsPoint
ConvertFromParentCoords(nsPoint aPt
) const;
227 * Called to query the visibility state of a view.
228 * @result current visibility state
230 nsViewVisibility
GetVisibility() const { return mVis
; }
233 * Get whether the view "floats" above all other views,
234 * which tells the compositor not to consider higher views in
235 * the view hierarchy that would geometrically intersect with
236 * this view. This is a hack, but it fixes some problems with
237 * views that need to be drawn in front of all other views.
238 * @result true if the view floats, false otherwise.
240 bool GetFloating() const { return (mVFlags
& NS_VIEW_FLAG_FLOATING
) != 0; }
243 * Called to query the parent of the view.
244 * @result view's parent
246 nsView
* GetParent() const { return mParent
; }
249 * The view's first child is the child which is earliest in document order.
250 * @result first child
252 nsView
* GetFirstChild() const { return mFirstChild
; }
255 * Called to query the next sibling of the view.
256 * @result view's next sibling
258 nsView
* GetNextSibling() const { return mNextSibling
; }
261 * Set the view's frame.
263 void SetFrame(nsIFrame
* aRootFrame
) { mFrame
= aRootFrame
; }
266 * Retrieve the view's frame.
268 nsIFrame
* GetFrame() const { return mFrame
; }
271 * Get the nearest widget in this view or a parent of this view and
272 * the offset from the widget's origin to this view's origin
273 * @param aOffset - if non-null the offset from this view's origin to the
274 * widget's origin (usually positive) expressed in appunits of this will be
275 * returned in aOffset.
276 * @return the widget closest to this view; can be null because some view
277 * trees don't have widgets at all (e.g., printing), but if any view in the
278 * view tree has a widget, then it's safe to assume this will not return null
280 nsIWidget
* GetNearestWidget(nsPoint
* aOffset
) const;
283 * Create a widget to associate with this view. This variant of
284 * CreateWidget*() will look around in the view hierarchy for an
285 * appropriate parent widget for the view.
287 * @param aWidgetInitData data used to initialize this view's widget before
288 * its create is called.
289 * @return error status
291 nsresult
CreateWidget(nsWidgetInitData
* aWidgetInitData
= nullptr,
292 bool aEnableDragDrop
= true,
293 bool aResetVisibility
= true);
296 * Create a widget for this view with an explicit parent widget.
297 * |aParentWidget| must be nonnull. The other params are the same
298 * as for |CreateWidget()|.
300 nsresult
CreateWidgetForParent(nsIWidget
* aParentWidget
,
301 nsWidgetInitData
* aWidgetInitData
= nullptr,
302 bool aEnableDragDrop
= true,
303 bool aResetVisibility
= true);
306 * Create a popup widget for this view. Pass |aParentWidget| to
307 * explicitly set the popup's parent. If it's not passed, the view
308 * hierarchy will be searched for an appropriate parent widget. The
309 * other params are the same as for |CreateWidget()|, except that
310 * |aWidgetInitData| must be nonnull.
312 nsresult
CreateWidgetForPopup(nsWidgetInitData
* aWidgetInitData
,
313 nsIWidget
* aParentWidget
= nullptr,
314 bool aEnableDragDrop
= true,
315 bool aResetVisibility
= true);
318 * Destroys the associated widget for this view. If this method is
319 * not called explicitly, the widget when be destroyed when its
320 * view gets destroyed.
322 void DestroyWidget();
325 * Attach/detach a top level widget from this view. When attached, the view
326 * updates the widget's device context and allows the view to begin receiving
327 * gecko events. The underlying base window associated with the widget will
328 * continues to receive events it expects.
330 * An attached widget will not be destroyed when the view is destroyed,
331 * allowing the recycling of a single top level widget over multiple views.
333 * @param aWidget The widget to attach to / detach from.
335 nsresult
AttachToTopLevelWidget(nsIWidget
* aWidget
);
336 nsresult
DetachFromTopLevelWidget();
339 * Returns a flag indicating whether the view owns it's widget
340 * or is attached to an existing top level widget.
342 bool IsAttachedToTopLevel() const { return mWidgetIsTopLevel
; }
345 * In 4.0, the "cutout" nature of a view is queryable.
346 * If we believe that all cutout view have a native widget, this
347 * could be a replacement.
348 * @param aWidget out parameter for widget that this view contains,
349 * or nullptr if there is none.
351 nsIWidget
* GetWidget() const { return mWindow
; }
354 * The widget which we have attached a listener to can also have a "previous"
355 * listener set on it. This is to keep track of the last nsView when
356 * navigating to a new one so that we can continue to paint that if the new
357 * one isn't ready yet.
359 void SetPreviousWidget(nsIWidget
* aWidget
) { mPreviousWindow
= aWidget
; }
362 * Returns true if the view has a widget associated with it.
364 bool HasWidget() const { return mWindow
!= nullptr; }
366 void SetForcedRepaint(bool aForceRepaint
) { mForcedRepaint
= aForceRepaint
; }
368 void SetNeedsWindowPropertiesSync();
371 * Make aWidget direct its events to this view.
372 * The caller must call DetachWidgetEventHandler before this view
375 void AttachWidgetEventHandler(nsIWidget
* aWidget
);
377 * Stop aWidget directing its events to this view.
379 void DetachWidgetEventHandler(nsIWidget
* aWidget
);
383 * Output debug info to FILE
384 * @param out output file handle
385 * @param aIndent indentation depth
386 * NOTE: virtual so that debugging tools not linked into gklayout can access
389 virtual void List(FILE* out
, int32_t aIndent
= 0) const;
393 * @result true iff this is the root view for its view manager
397 LayoutDeviceIntRect
CalcWidgetBounds(nsWindowType aType
);
399 // This is an app unit offset to add when converting view coordinates to
400 // widget coordinates. It is the offset in view coordinates from widget
401 // origin (unlike views, widgets can't extend above or to the left of their
402 // origin) to view origin expressed in appunits of this.
403 nsPoint
ViewToWidgetOffset() const { return mViewToWidgetOffset
; }
406 * Called to indicate that the position of the view has been changed.
407 * The specified coordinates are in the parent view's coordinate space.
408 * @param x new x position
409 * @param y new y position
411 void SetPosition(nscoord aX
, nscoord aY
);
414 * Called to indicate that the z-index of a view has been changed.
415 * The z-index is relative to all siblings of the view.
416 * @param aAuto Indicate that the z-index of a view is "auto". An "auto"
417 * z-index means that the view does not define a new stacking
418 * context, which means that the z-indicies of the view's
419 * children are relative to the view's siblings.
420 * @param zindex new z depth
422 void SetZIndex(bool aAuto
, int32_t aZIndex
);
423 bool GetZIndexIsAuto() const {
424 return (mVFlags
& NS_VIEW_FLAG_AUTO_ZINDEX
) != 0;
426 int32_t GetZIndex() const { return mZIndex
; }
428 void SetParent(nsView
* aParent
) { mParent
= aParent
; }
429 void SetNextSibling(nsView
* aSibling
) {
430 NS_ASSERTION(aSibling
!= this, "Can't be our own sibling!");
431 mNextSibling
= aSibling
;
434 nsRegion
* GetDirtyRegion() {
436 NS_ASSERTION(!mParent
|| GetFloating(),
437 "Only display roots should have dirty regions");
438 mDirtyRegion
= new nsRegion();
439 NS_ASSERTION(mDirtyRegion
, "Out of memory!");
445 virtual mozilla::PresShell
* GetPresShell() override
;
446 virtual nsView
* GetView() override
{ return this; }
447 virtual bool WindowMoved(nsIWidget
* aWidget
, int32_t x
, int32_t y
) override
;
448 virtual bool WindowResized(nsIWidget
* aWidget
, int32_t aWidth
,
449 int32_t aHeight
) override
;
450 #if defined(MOZ_WIDGET_ANDROID)
451 virtual void DynamicToolbarMaxHeightChanged(
452 mozilla::ScreenIntCoord aHeight
) override
;
453 virtual void DynamicToolbarOffsetChanged(
454 mozilla::ScreenIntCoord aOffset
) override
;
456 virtual bool RequestWindowClose(nsIWidget
* aWidget
) override
;
457 MOZ_CAN_RUN_SCRIPT_BOUNDARY
458 virtual void WillPaintWindow(nsIWidget
* aWidget
) override
;
459 MOZ_CAN_RUN_SCRIPT_BOUNDARY
460 virtual bool PaintWindow(nsIWidget
* aWidget
,
461 LayoutDeviceIntRegion aRegion
) override
;
462 MOZ_CAN_RUN_SCRIPT_BOUNDARY
463 virtual void DidPaintWindow() override
;
464 virtual void DidCompositeWindow(
465 mozilla::layers::TransactionId aTransactionId
,
466 const mozilla::TimeStamp
& aCompositeStart
,
467 const mozilla::TimeStamp
& aCompositeEnd
) override
;
468 virtual void RequestRepaint() override
;
469 virtual bool ShouldNotBeVisible() override
;
470 MOZ_CAN_RUN_SCRIPT_BOUNDARY
471 virtual nsEventStatus
HandleEvent(mozilla::WidgetGUIEvent
* aEvent
,
472 bool aUseAttachedEvents
) override
;
473 virtual void SafeAreaInsetsChanged(const mozilla::ScreenIntMargin
&) override
;
477 nsPoint
GetOffsetTo(const nsView
* aOther
, const int32_t aAPD
) const;
478 nsIWidget
* GetNearestWidget(nsPoint
* aOffset
, const int32_t aAPD
) const;
480 bool IsPrimaryFramePaintSuppressed();
483 explicit nsView(nsViewManager
* aViewManager
= nullptr,
484 nsViewVisibility aVisibility
= nsViewVisibility_kShow
);
486 bool ForcedRepaint() { return mForcedRepaint
; }
488 // Do the actual work of ResetWidgetBounds, unconditionally. Don't
489 // call this method if we have no widget.
490 void DoResetWidgetBounds(bool aMoveOnly
, bool aInvalidateChangedSize
);
491 void InitializeWindow(bool aEnableDragDrop
, bool aResetVisibility
);
493 bool IsEffectivelyVisible();
496 * Called to indicate that the dimensions of the view have been changed.
497 * The x and y coordinates may be < 0, indicating that the view extends above
498 * or to the left of its origin position. The term 'dimensions' indicates it
499 * is relative to this view.
501 void SetDimensions(const nsRect
& aRect
, bool aPaint
= true,
502 bool aResizeWidget
= true);
505 * Called to indicate that the visibility of a view has been
507 * @param visibility new visibility state
509 void SetVisibility(nsViewVisibility visibility
);
512 * Set/Get whether the view "floats" above all other views,
513 * which tells the compositor not to consider higher views in
514 * the view hierarchy that would geometrically intersect with
515 * this view. This is a hack, but it fixes some problems with
516 * views that need to be drawn in front of all other views.
517 * @result true if the view floats, false otherwise.
519 void SetFloating(bool aFloatingView
);
521 // Helper function to get mouse grabbing off this view (by moving it to the
522 // parent, if we can)
523 void DropMouseGrabbing();
525 // Same as GetBounds but converts to parent appunits if they are different.
526 nsRect
GetBoundsInParentUnits() const;
528 bool HasNonEmptyDirtyRegion() {
529 return mDirtyRegion
&& !mDirtyRegion
->IsEmpty();
532 void InsertChild(nsView
* aChild
, nsView
* aSibling
);
533 void RemoveChild(nsView
* aChild
);
535 void ResetWidgetBounds(bool aRecurse
, bool aForceSync
);
536 void AssertNoWindow();
538 void NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible
);
540 // Update the cached RootViewManager for all view manager descendents.
541 void InvalidateHierarchy();
543 nsViewManager
* mViewManager
;
545 nsCOMPtr
<nsIWidget
> mWindow
;
546 nsCOMPtr
<nsIWidget
> mPreviousWindow
;
547 nsView
* mNextSibling
;
550 nsRegion
* mDirtyRegion
;
552 nsViewVisibility mVis
;
553 // position relative our parent view origin but in our appunits
554 nscoord mPosX
, mPosY
;
555 // relative to parent, but in our appunits
558 nsPoint mViewToWidgetOffset
;
560 bool mWidgetIsTopLevel
;
562 bool mNeedsWindowPropertiesSync
;