Bug 1890277: part 4) Add CSPParser support for the `trusted-types` directive, guarded...
[gecko.git] / view / nsView.h
blob7e1617265f97babae6e5204fce0685803bb954d1
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 * Called to query the visibility state of a view.
223 * @result current visibility state
225 ViewVisibility GetVisibility() const { return mVis; }
228 * Get whether the view "floats" above all other views,
229 * which tells the compositor not to consider higher views in
230 * the view hierarchy that would geometrically intersect with
231 * this view. This is a hack, but it fixes some problems with
232 * views that need to be drawn in front of all other views.
233 * @result true if the view floats, false otherwise.
235 bool GetFloating() const { return (mVFlags & NS_VIEW_FLAG_FLOATING) != 0; }
238 * Called to query the parent of the view.
239 * @result view's parent
241 nsView* GetParent() const { return mParent; }
244 * The view's first child is the child which is earliest in document order.
245 * @result first child
247 nsView* GetFirstChild() const { return mFirstChild; }
250 * Called to query the next sibling of the view.
251 * @result view's next sibling
253 nsView* GetNextSibling() const { return mNextSibling; }
256 * Set the view's frame.
258 void SetFrame(nsIFrame* aRootFrame) { mFrame = aRootFrame; }
261 * Retrieve the view's frame.
263 nsIFrame* GetFrame() const { return mFrame; }
266 * Get the nearest widget in this view or a parent of this view and
267 * the offset from the widget's origin to this view's origin
268 * @param aOffset - if non-null the offset from this view's origin to the
269 * widget's origin (usually positive) expressed in appunits of this will be
270 * returned in aOffset.
271 * @return the widget closest to this view; can be null because some view
272 * trees don't have widgets at all (e.g., printing), but if any view in the
273 * view tree has a widget, then it's safe to assume this will not return null
275 nsIWidget* GetNearestWidget(nsPoint* aOffset) const;
278 * Create a widget to associate with this view. This variant of
279 * CreateWidget*() will look around in the view hierarchy for an
280 * appropriate parent widget for the view.
282 * @param aWidgetInitData data used to initialize this view's widget before
283 * its create is called.
284 * @return error status
286 nsresult CreateWidget(mozilla::widget::InitData* aWidgetInitData = nullptr,
287 bool aEnableDragDrop = true,
288 bool aResetVisibility = true);
291 * Create a widget for this view with an explicit parent widget.
292 * |aParentWidget| must be nonnull. The other params are the same
293 * as for |CreateWidget()|.
295 nsresult CreateWidgetForParent(nsIWidget* aParentWidget,
296 mozilla::widget::InitData* = nullptr,
297 bool aEnableDragDrop = true,
298 bool aResetVisibility = true);
301 * Create a popup widget for this view. Pass |aParentWidget| to
302 * explicitly set the popup's parent. If it's not passed, the view
303 * hierarchy will be searched for an appropriate parent widget. The
304 * other params are the same as for |CreateWidget()|, except that
305 * |aWidgetInitData| must be nonnull.
307 nsresult CreateWidgetForPopup(mozilla::widget::InitData*,
308 nsIWidget* aParentWidget = nullptr);
311 * Destroys the associated widget for this view. If this method is
312 * not called explicitly, the widget when be destroyed when its
313 * view gets destroyed.
315 void DestroyWidget();
318 * Attach/detach a top level widget from this view. When attached, the view
319 * updates the widget's device context and allows the view to begin receiving
320 * gecko events. The underlying base window associated with the widget will
321 * continues to receive events it expects.
323 * An attached widget will not be destroyed when the view is destroyed,
324 * allowing the recycling of a single top level widget over multiple views.
326 * @param aWidget The widget to attach to / detach from.
328 nsresult AttachToTopLevelWidget(nsIWidget* aWidget);
329 nsresult DetachFromTopLevelWidget();
332 * Returns a flag indicating whether the view owns it's widget
333 * or is attached to an existing top level widget.
335 bool IsAttachedToTopLevel() const { return mWidgetIsTopLevel; }
338 * In 4.0, the "cutout" nature of a view is queryable.
339 * If we believe that all cutout view have a native widget, this
340 * could be a replacement.
341 * @param aWidget out parameter for widget that this view contains,
342 * or nullptr if there is none.
344 nsIWidget* GetWidget() const { return mWindow; }
347 * The widget which we have attached a listener to can also have a "previous"
348 * listener set on it. This is to keep track of the last nsView when
349 * navigating to a new one so that we can continue to paint that if the new
350 * one isn't ready yet.
352 void SetPreviousWidget(nsIWidget* aWidget) { mPreviousWindow = aWidget; }
355 * Returns true if the view has a widget associated with it.
357 bool HasWidget() const { return mWindow != nullptr; }
359 void SetForcedRepaint(bool aForceRepaint) { mForcedRepaint = aForceRepaint; }
361 void SetNeedsWindowPropertiesSync();
364 * Make aWidget direct its events to this view.
365 * The caller must call DetachWidgetEventHandler before this view
366 * is destroyed.
368 void AttachWidgetEventHandler(nsIWidget* aWidget);
370 * Stop aWidget directing its events to this view.
372 void DetachWidgetEventHandler(nsIWidget* aWidget);
374 #ifdef DEBUG
376 * Output debug info to FILE
377 * @param out output file handle
378 * @param aIndent indentation depth
379 * NOTE: virtual so that debugging tools not linked into gklayout can access
380 * it
382 virtual void List(FILE* out, int32_t aIndent = 0) const;
383 #endif // DEBUG
386 * @result true iff this is the root view for its view manager
388 bool IsRoot() const;
390 LayoutDeviceIntRect CalcWidgetBounds(mozilla::widget::WindowType,
391 mozilla::widget::TransparencyMode);
393 LayoutDeviceIntRect RecalcWidgetBounds();
395 // This is an app unit offset to add when converting view coordinates to
396 // widget coordinates. It is the offset in view coordinates from widget
397 // origin (unlike views, widgets can't extend above or to the left of their
398 // origin) to view origin expressed in appunits of this.
399 nsPoint ViewToWidgetOffset() const { return mViewToWidgetOffset; }
402 * Called to indicate that the position of the view has been changed.
403 * The specified coordinates are in the parent view's coordinate space.
404 * @param x new x position
405 * @param y new y position
407 void SetPosition(nscoord aX, nscoord aY);
410 * Called to indicate that the z-index of a view has been changed.
411 * The z-index is relative to all siblings of the view.
412 * @param aAuto Indicate that the z-index of a view is "auto". An "auto"
413 * z-index means that the view does not define a new stacking
414 * context, which means that the z-indicies of the view's
415 * children are relative to the view's siblings.
416 * @param zindex new z depth
418 void SetZIndex(bool aAuto, int32_t aZIndex);
419 bool GetZIndexIsAuto() const {
420 return (mVFlags & NS_VIEW_FLAG_AUTO_ZINDEX) != 0;
422 int32_t GetZIndex() const { return mZIndex; }
424 void SetParent(nsView* aParent) { mParent = aParent; }
425 void SetNextSibling(nsView* aSibling) {
426 NS_ASSERTION(aSibling != this, "Can't be our own sibling!");
427 mNextSibling = aSibling;
430 nsRegion& GetDirtyRegion() {
431 if (!mDirtyRegion) {
432 NS_ASSERTION(!mParent || GetFloating(),
433 "Only display roots should have dirty regions");
434 mDirtyRegion = mozilla::MakeUnique<nsRegion>();
436 return *mDirtyRegion;
439 // nsIWidgetListener
440 virtual mozilla::PresShell* GetPresShell() override;
441 virtual nsView* GetView() override { return this; }
442 virtual bool WindowMoved(nsIWidget* aWidget, int32_t x, int32_t y,
443 ByMoveToRect) override;
444 virtual bool WindowResized(nsIWidget* aWidget, int32_t aWidth,
445 int32_t aHeight) override;
446 #if defined(MOZ_WIDGET_ANDROID)
447 virtual void DynamicToolbarMaxHeightChanged(
448 mozilla::ScreenIntCoord aHeight) override;
449 virtual void DynamicToolbarOffsetChanged(
450 mozilla::ScreenIntCoord aOffset) override;
451 #endif
452 virtual bool RequestWindowClose(nsIWidget* aWidget) override;
453 MOZ_CAN_RUN_SCRIPT_BOUNDARY
454 virtual void WillPaintWindow(nsIWidget* aWidget) override;
455 MOZ_CAN_RUN_SCRIPT_BOUNDARY
456 virtual bool PaintWindow(nsIWidget* aWidget,
457 LayoutDeviceIntRegion aRegion) override;
458 MOZ_CAN_RUN_SCRIPT_BOUNDARY
459 virtual void DidPaintWindow() override;
460 virtual void DidCompositeWindow(
461 mozilla::layers::TransactionId aTransactionId,
462 const mozilla::TimeStamp& aCompositeStart,
463 const mozilla::TimeStamp& aCompositeEnd) override;
464 virtual void RequestRepaint() override;
465 virtual bool ShouldNotBeVisible() override;
466 MOZ_CAN_RUN_SCRIPT_BOUNDARY
467 virtual nsEventStatus HandleEvent(mozilla::WidgetGUIEvent* aEvent,
468 bool aUseAttachedEvents) override;
469 virtual void SafeAreaInsetsChanged(const mozilla::ScreenIntMargin&) override;
471 virtual ~nsView();
473 nsPoint GetOffsetTo(const nsView* aOther, const int32_t aAPD) const;
474 nsIWidget* GetNearestWidget(nsPoint* aOffset, const int32_t aAPD) const;
476 bool IsPrimaryFramePaintSuppressed();
478 private:
479 explicit nsView(nsViewManager* = nullptr,
480 ViewVisibility = ViewVisibility::Show);
482 bool ForcedRepaint() { return mForcedRepaint; }
484 // Do the actual work of ResetWidgetBounds, unconditionally. Don't
485 // call this method if we have no widget.
486 void DoResetWidgetBounds(bool aMoveOnly, bool aInvalidateChangedSize);
487 void InitializeWindow(bool aEnableDragDrop, bool aResetVisibility);
489 bool IsEffectivelyVisible();
492 * Called to indicate that the dimensions of the view have been changed.
493 * The x and y coordinates may be < 0, indicating that the view extends above
494 * or to the left of its origin position. The term 'dimensions' indicates it
495 * is relative to this view.
497 void SetDimensions(const nsRect& aRect, bool aPaint = true,
498 bool aResizeWidget = true);
501 * Called to indicate that the visibility of a view has been
502 * changed.
503 * @param visibility new visibility state
505 void SetVisibility(ViewVisibility visibility);
508 * Set/Get whether the view "floats" above all other views,
509 * which tells the compositor not to consider higher views in
510 * the view hierarchy that would geometrically intersect with
511 * this view. This is a hack, but it fixes some problems with
512 * views that need to be drawn in front of all other views.
513 * @result true if the view floats, false otherwise.
515 void SetFloating(bool aFloatingView);
517 // Helper function to get mouse grabbing off this view (by moving it to the
518 // parent, if we can)
519 void DropMouseGrabbing();
521 bool HasNonEmptyDirtyRegion() {
522 return mDirtyRegion && !mDirtyRegion->IsEmpty();
525 void InsertChild(nsView* aChild, nsView* aSibling);
526 void RemoveChild(nsView* aChild);
528 void ResetWidgetBounds(bool aRecurse, bool aForceSync);
529 void AssertNoWindow();
531 void NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible);
533 // Update the cached RootViewManager for all view manager descendents.
534 void InvalidateHierarchy();
536 nsViewManager* mViewManager;
537 nsView* mParent;
538 nsCOMPtr<nsIWidget> mWindow;
539 nsCOMPtr<nsIWidget> mPreviousWindow;
540 nsView* mNextSibling;
541 nsView* mFirstChild;
542 nsIFrame* mFrame;
543 mozilla::UniquePtr<nsRegion> mDirtyRegion;
544 int32_t mZIndex;
545 ViewVisibility mVis;
546 // position relative our parent view origin but in our appunits
547 nscoord mPosX, mPosY;
548 // relative to parent, but in our appunits
549 nsRect mDimBounds;
550 // in our appunits
551 nsPoint mViewToWidgetOffset;
552 uint32_t mVFlags;
553 bool mWidgetIsTopLevel;
554 bool mForcedRepaint;
555 bool mNeedsWindowPropertiesSync;
558 #endif