Backed out changeset 735d82e60fb6 (bug 1824913) for causing test_dap.js xpcshell...
[gecko.git] / view / nsView.h
blob832c6358ade0c6753245f01b4e22bedfed211c72
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/. */
6 #ifndef nsView_h__
7 #define nsView_h__
9 #include "nsCoord.h"
10 #include "nsRect.h"
11 #include "nsPoint.h"
12 #include "nsRegion.h"
13 #include "nsCRT.h"
14 #include "nsCOMPtr.h"
15 #include "nsIWidgetListener.h"
16 #include "Units.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/EventForwards.h"
19 #include "mozilla/UniquePtr.h"
21 class nsViewManager;
22 class nsIWidget;
23 class nsIFrame;
25 namespace mozilla {
26 class PresShell;
27 namespace widget {
28 struct InitData;
29 enum class TransparencyMode : uint8_t;
30 enum class WindowType : uint8_t;
31 } // namespace widget
32 } // namespace mozilla
34 /**
35 * nsView's serve two main purposes: 1) a bridge between nsIFrame's and
36 * nsIWidget's, 2) linking the frame tree of a(n) (in-process) subdocument with
37 * its parent document frame tree. Historically views were used for more things,
38 * but their role has been reduced, and could be reduced to nothing in the
39 * future (bug 337801 tracks removing views). Views are generally associated
40 * with a frame. A view that does not have a frame is called an anonymous view.
41 * Some frames also have associated widgets (think os level windows). If a frame
42 * has a widget it must also have a view, but not all frames with views will
43 * have widgets.
45 * Only four types of frames can have a view: root frames (ViewportFrame),
46 * subdocument frames (nsSubDocumentFrame),
47 * menu popup frames (nsMenuPopupFrame), and list control frames
48 * (nsListControlFrame). Root frames and subdocument frames have views to link
49 * the two documents together (the frame trees do not link up otherwise).
50 * Menu popup frames, and list control frames have views because
51 * they (sometimes) need to create widgets.
52 * Menu popup frames handles xul popups, which is anything
53 * where we need content to go over top the main window at an os level. List
54 * control frames handle select popups/dropdowns in non-e10s mode.
56 * The term "root view" refers to the root view of a document. Just like root
57 * frames, root views can have parent views. Only the root view of the root
58 * document in the process will not have a parent.
60 * All views are created by their frames except root views. Root views are
61 * special. Root views are created in nsDocumentViewer::MakeWindow before the
62 * root frame is created, so the root view will not have a frame very early in
63 * document creation.
65 * Subdocument frames have an anonymous (no frame associated
66 * with it) inner view that is a child of their "outer" view.
68 * On a subdocument frame the inner view serves as the parent of the
69 * root view of the subdocument. The outer and inner view of the subdocument
70 * frame belong to the subdocument frame and hence to the parent document. The
71 * root view of the subdocument belongs to the subdocument.
72 * nsLayoutUtils::GetCrossDocParentFrame and nsPresContext::GetParentPresContext
73 * depend on this view structure and are the main way that we traverse across
74 * the document boundary in layout.
76 * When the load of a new document is started in the subdocument, the creation
77 * of the new subdocument and destruction of the old subdocument are not
78 * linked. (This creation and destruction is handled in nsDocumentViewer.cpp.)
79 * This means that the old and new document will both exist at the same time
80 * during the loading of the new document. During this period the inner view of
81 * the subdocument parent will be the parent of two root views. This means that
82 * during this period there is a choice for which subdocument we draw,
83 * nsSubDocumentFrame::GetSubdocumentPresShellForPainting is what makes that
84 * choice. Note that this might not be a totally free choice, ie there might be
85 * hidden dependencies and bugs if the way we choose is changed.
87 * One thing that is special about the root view of a chrome window is that
88 * instead of creating a widget for the view, they can "attach" to the
89 * existing widget that was created by appshell code or something else. (see
90 * nsDocumentViewer::ShouldAttachToTopLevel)
93 // Enumerated type to indicate the visibility of a layer.
94 // hide - the layer is not shown.
95 // show - the layer is shown irrespective of the visibility of
96 // the layer's parent.
97 enum class ViewVisibility : uint8_t { Hide = 0, Show = 1 };
99 // Public view flags
101 // Indicates that the view is using auto z-indexing
102 #define NS_VIEW_FLAG_AUTO_ZINDEX 0x0004
104 // Indicates that the view is a floating view.
105 #define NS_VIEW_FLAG_FLOATING 0x0008
107 //----------------------------------------------------------------------
110 * View interface
112 * Views are NOT reference counted. Use the Destroy() member function to
113 * destroy a view.
115 * The lifetime of the view hierarchy is bounded by the lifetime of the
116 * view manager that owns the views.
118 * Most of the methods here are read-only. To set the corresponding properties
119 * of a view, go through nsViewManager.
122 class nsView final : public nsIWidgetListener {
123 public:
124 friend class nsViewManager;
126 typedef mozilla::LayoutDeviceIntRect LayoutDeviceIntRect;
127 typedef mozilla::LayoutDeviceIntRegion LayoutDeviceIntRegion;
129 void operator delete(void* ptr) { ::operator delete(ptr); }
132 * Get the view manager which "owns" the view.
133 * This method might require some expensive traversal work in the future. If
134 * you can get the view manager from somewhere else, do that instead.
135 * @result the view manager
137 nsViewManager* GetViewManager() const { return mViewManager; }
140 * Find the view for the given widget, if there is one.
141 * @return the view the widget belongs to, or null if the widget doesn't
142 * belong to any view.
144 static nsView* GetViewFor(const nsIWidget* aWidget);
147 * Destroy the view.
149 * The view destroys its child views, and destroys and releases its
150 * widget (if it has one).
152 * Also informs the view manager that the view is destroyed by calling
153 * SetRootView(NULL) if the view is the root view and calling RemoveChild()
154 * otherwise.
156 void Destroy();
159 * Called to get the position of a view.
160 * The specified coordinates are relative to the parent view's origin, but
161 * are in appunits of this.
162 * This is the (0, 0) origin of the coordinate space established by this view.
163 * @param x out parameter for x position
164 * @param y out parameter for y position
166 nsPoint GetPosition() const {
167 NS_ASSERTION(!IsRoot() || (mPosX == 0 && mPosY == 0),
168 "root views should always have explicit position of (0,0)");
169 return nsPoint(mPosX, mPosY);
173 * Called to get the dimensions and position of the view's bounds.
174 * The view's bounds (x,y) are relative to the origin of the parent view, but
175 * are in appunits of this.
176 * The view's bounds (x,y) might not be the same as the view's position,
177 * if the view has content above or to the left of its origin.
178 * @param aBounds out parameter for bounds
180 nsRect GetBounds() const { return mDimBounds; }
183 * The bounds of this view relative to this view. So this is the same as
184 * GetBounds except this is relative to this view instead of the parent view.
186 nsRect GetDimensions() const {
187 nsRect r = mDimBounds;
188 r.MoveBy(-mPosX, -mPosY);
189 return r;
193 * Get the offset between the coordinate systems of |this| and aOther.
194 * Adding the return value to a point in the coordinate system of |this|
195 * will transform the point to the coordinate system of aOther.
197 * The offset is expressed in appunits of |this|. So if you are getting the
198 * offset between views in different documents that might have different
199 * appunits per devpixel ratios you need to be careful how you use the
200 * result.
202 * If aOther is null, this will return the offset of |this| from the
203 * root of the viewmanager tree.
205 * This function is fastest when aOther is an ancestor of |this|.
207 * NOTE: this actually returns the offset from aOther to |this|, but
208 * that offset is added to transform _coordinates_ from |this| to aOther.
210 nsPoint GetOffsetTo(const nsView* aOther) const;
213 * Get the offset between the origin of |this| and the origin of aWidget.
214 * Adding the return value to a point in the coordinate system of |this|
215 * will transform the point to the coordinate system of aWidget.
217 * The offset is expressed in appunits of |this|.
219 nsPoint GetOffsetToWidget(nsIWidget* aWidget) const;
222 * Takes a point aPt that is in the coordinate system of |this|'s parent view
223 * and converts it to be in the coordinate system of |this| taking into
224 * account the offset and any app unit per dev pixel ratio differences.
226 nsPoint ConvertFromParentCoords(nsPoint aPt) const;
229 * Called to query the visibility state of a view.
230 * @result current visibility state
232 ViewVisibility GetVisibility() const { return mVis; }
235 * Get whether the view "floats" above all other views,
236 * which tells the compositor not to consider higher views in
237 * the view hierarchy that would geometrically intersect with
238 * this view. This is a hack, but it fixes some problems with
239 * views that need to be drawn in front of all other views.
240 * @result true if the view floats, false otherwise.
242 bool GetFloating() const { return (mVFlags & NS_VIEW_FLAG_FLOATING) != 0; }
245 * Called to query the parent of the view.
246 * @result view's parent
248 nsView* GetParent() const { return mParent; }
251 * The view's first child is the child which is earliest in document order.
252 * @result first child
254 nsView* GetFirstChild() const { return mFirstChild; }
257 * Called to query the next sibling of the view.
258 * @result view's next sibling
260 nsView* GetNextSibling() const { return mNextSibling; }
263 * Set the view's frame.
265 void SetFrame(nsIFrame* aRootFrame) { mFrame = aRootFrame; }
268 * Retrieve the view's frame.
270 nsIFrame* GetFrame() const { return mFrame; }
273 * Get the nearest widget in this view or a parent of this view and
274 * the offset from the widget's origin to this view's origin
275 * @param aOffset - if non-null the offset from this view's origin to the
276 * widget's origin (usually positive) expressed in appunits of this will be
277 * returned in aOffset.
278 * @return the widget closest to this view; can be null because some view
279 * trees don't have widgets at all (e.g., printing), but if any view in the
280 * view tree has a widget, then it's safe to assume this will not return null
282 nsIWidget* GetNearestWidget(nsPoint* aOffset) const;
285 * Create a widget to associate with this view. This variant of
286 * CreateWidget*() will look around in the view hierarchy for an
287 * appropriate parent widget for the view.
289 * @param aWidgetInitData data used to initialize this view's widget before
290 * its create is called.
291 * @return error status
293 nsresult CreateWidget(mozilla::widget::InitData* aWidgetInitData = nullptr,
294 bool aEnableDragDrop = true,
295 bool aResetVisibility = true);
298 * Create a widget for this view with an explicit parent widget.
299 * |aParentWidget| must be nonnull. The other params are the same
300 * as for |CreateWidget()|.
302 nsresult CreateWidgetForParent(nsIWidget* aParentWidget,
303 mozilla::widget::InitData* = nullptr,
304 bool aEnableDragDrop = true,
305 bool aResetVisibility = true);
308 * Create a popup widget for this view. Pass |aParentWidget| to
309 * explicitly set the popup's parent. If it's not passed, the view
310 * hierarchy will be searched for an appropriate parent widget. The
311 * other params are the same as for |CreateWidget()|, except that
312 * |aWidgetInitData| must be nonnull.
314 nsresult CreateWidgetForPopup(mozilla::widget::InitData*,
315 nsIWidget* aParentWidget = nullptr);
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
373 * is destroyed.
375 void AttachWidgetEventHandler(nsIWidget* aWidget);
377 * Stop aWidget directing its events to this view.
379 void DetachWidgetEventHandler(nsIWidget* aWidget);
381 #ifdef DEBUG
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
387 * it
389 virtual void List(FILE* out, int32_t aIndent = 0) const;
390 #endif // DEBUG
393 * @result true iff this is the root view for its view manager
395 bool IsRoot() const;
397 LayoutDeviceIntRect CalcWidgetBounds(mozilla::widget::WindowType,
398 mozilla::widget::TransparencyMode);
400 LayoutDeviceIntRect RecalcWidgetBounds();
402 // This is an app unit offset to add when converting view coordinates to
403 // widget coordinates. It is the offset in view coordinates from widget
404 // origin (unlike views, widgets can't extend above or to the left of their
405 // origin) to view origin expressed in appunits of this.
406 nsPoint ViewToWidgetOffset() const { return mViewToWidgetOffset; }
409 * Called to indicate that the position of the view has been changed.
410 * The specified coordinates are in the parent view's coordinate space.
411 * @param x new x position
412 * @param y new y position
414 void SetPosition(nscoord aX, nscoord aY);
417 * Called to indicate that the z-index of a view has been changed.
418 * The z-index is relative to all siblings of the view.
419 * @param aAuto Indicate that the z-index of a view is "auto". An "auto"
420 * z-index means that the view does not define a new stacking
421 * context, which means that the z-indicies of the view's
422 * children are relative to the view's siblings.
423 * @param zindex new z depth
425 void SetZIndex(bool aAuto, int32_t aZIndex);
426 bool GetZIndexIsAuto() const {
427 return (mVFlags & NS_VIEW_FLAG_AUTO_ZINDEX) != 0;
429 int32_t GetZIndex() const { return mZIndex; }
431 void SetParent(nsView* aParent) { mParent = aParent; }
432 void SetNextSibling(nsView* aSibling) {
433 NS_ASSERTION(aSibling != this, "Can't be our own sibling!");
434 mNextSibling = aSibling;
437 nsRegion& GetDirtyRegion() {
438 if (!mDirtyRegion) {
439 NS_ASSERTION(!mParent || GetFloating(),
440 "Only display roots should have dirty regions");
441 mDirtyRegion = mozilla::MakeUnique<nsRegion>();
443 return *mDirtyRegion;
446 // nsIWidgetListener
447 virtual mozilla::PresShell* GetPresShell() override;
448 virtual nsView* GetView() override { return this; }
449 virtual bool WindowMoved(nsIWidget* aWidget, int32_t x, int32_t y,
450 ByMoveToRect) override;
451 virtual bool WindowResized(nsIWidget* aWidget, int32_t aWidth,
452 int32_t aHeight) override;
453 #if defined(MOZ_WIDGET_ANDROID)
454 virtual void DynamicToolbarMaxHeightChanged(
455 mozilla::ScreenIntCoord aHeight) override;
456 virtual void DynamicToolbarOffsetChanged(
457 mozilla::ScreenIntCoord aOffset) override;
458 #endif
459 virtual bool RequestWindowClose(nsIWidget* aWidget) override;
460 MOZ_CAN_RUN_SCRIPT_BOUNDARY
461 virtual void WillPaintWindow(nsIWidget* aWidget) override;
462 MOZ_CAN_RUN_SCRIPT_BOUNDARY
463 virtual bool PaintWindow(nsIWidget* aWidget,
464 LayoutDeviceIntRegion aRegion) override;
465 MOZ_CAN_RUN_SCRIPT_BOUNDARY
466 virtual void DidPaintWindow() override;
467 virtual void DidCompositeWindow(
468 mozilla::layers::TransactionId aTransactionId,
469 const mozilla::TimeStamp& aCompositeStart,
470 const mozilla::TimeStamp& aCompositeEnd) override;
471 virtual void RequestRepaint() override;
472 virtual bool ShouldNotBeVisible() override;
473 MOZ_CAN_RUN_SCRIPT_BOUNDARY
474 virtual nsEventStatus HandleEvent(mozilla::WidgetGUIEvent* aEvent,
475 bool aUseAttachedEvents) override;
476 virtual void SafeAreaInsetsChanged(const mozilla::ScreenIntMargin&) override;
478 virtual ~nsView();
480 nsPoint GetOffsetTo(const nsView* aOther, const int32_t aAPD) const;
481 nsIWidget* GetNearestWidget(nsPoint* aOffset, const int32_t aAPD) const;
483 bool IsPrimaryFramePaintSuppressed();
485 private:
486 explicit nsView(nsViewManager* = nullptr,
487 ViewVisibility = ViewVisibility::Show);
489 bool ForcedRepaint() { return mForcedRepaint; }
491 // Do the actual work of ResetWidgetBounds, unconditionally. Don't
492 // call this method if we have no widget.
493 void DoResetWidgetBounds(bool aMoveOnly, bool aInvalidateChangedSize);
494 void InitializeWindow(bool aEnableDragDrop, bool aResetVisibility);
496 bool IsEffectivelyVisible();
499 * Called to indicate that the dimensions of the view have been changed.
500 * The x and y coordinates may be < 0, indicating that the view extends above
501 * or to the left of its origin position. The term 'dimensions' indicates it
502 * is relative to this view.
504 void SetDimensions(const nsRect& aRect, bool aPaint = true,
505 bool aResizeWidget = true);
508 * Called to indicate that the visibility of a view has been
509 * changed.
510 * @param visibility new visibility state
512 void SetVisibility(ViewVisibility visibility);
515 * Set/Get whether the view "floats" above all other views,
516 * which tells the compositor not to consider higher views in
517 * the view hierarchy that would geometrically intersect with
518 * this view. This is a hack, but it fixes some problems with
519 * views that need to be drawn in front of all other views.
520 * @result true if the view floats, false otherwise.
522 void SetFloating(bool aFloatingView);
524 // Helper function to get mouse grabbing off this view (by moving it to the
525 // parent, if we can)
526 void DropMouseGrabbing();
528 // Same as GetBounds but converts to parent appunits if they are different.
529 nsRect GetBoundsInParentUnits() const;
531 bool HasNonEmptyDirtyRegion() {
532 return mDirtyRegion && !mDirtyRegion->IsEmpty();
535 void InsertChild(nsView* aChild, nsView* aSibling);
536 void RemoveChild(nsView* aChild);
538 void ResetWidgetBounds(bool aRecurse, bool aForceSync);
539 void AssertNoWindow();
541 void NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible);
543 // Update the cached RootViewManager for all view manager descendents.
544 void InvalidateHierarchy();
546 nsViewManager* mViewManager;
547 nsView* mParent;
548 nsCOMPtr<nsIWidget> mWindow;
549 nsCOMPtr<nsIWidget> mPreviousWindow;
550 nsView* mNextSibling;
551 nsView* mFirstChild;
552 nsIFrame* mFrame;
553 mozilla::UniquePtr<nsRegion> mDirtyRegion;
554 int32_t mZIndex;
555 ViewVisibility mVis;
556 // position relative our parent view origin but in our appunits
557 nscoord mPosX, mPosY;
558 // relative to parent, but in our appunits
559 nsRect mDimBounds;
560 // in our appunits
561 nsPoint mViewToWidgetOffset;
562 uint32_t mVFlags;
563 bool mWidgetIsTopLevel;
564 bool mForcedRepaint;
565 bool mNeedsWindowPropertiesSync;
568 #endif