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/. */
8 #include "mozilla/Attributes.h"
9 #include "mozilla/BasicEvents.h"
10 #include "mozilla/DebugOnly.h"
11 #include "mozilla/IntegerPrintfMacros.h"
12 #include "mozilla/Likely.h"
13 #include "mozilla/Poison.h"
14 #include "nsIWidget.h"
15 #include "nsViewManager.h"
17 #include "nsPresArena.h"
18 #include "nsXULPopupManager.h"
19 #include "nsIWidgetListener.h"
20 #include "nsContentUtils.h" // for nsAutoScriptBlocker
21 #include "mozilla/TimelineConsumers.h"
22 #include "mozilla/CompositeTimelineMarker.h"
24 using namespace mozilla
;
26 static bool sShowPreviousPage
= true;
28 nsView::nsView(nsViewManager
* aViewManager
, nsViewVisibility aVisibility
)
29 : mViewManager(aViewManager
),
31 mNextSibling(nullptr),
34 mDirtyRegion(nullptr),
40 mWidgetIsTopLevel(false),
41 mForcedRepaint(false),
42 mNeedsWindowPropertiesSync(false) {
43 MOZ_COUNT_CTOR(nsView
);
45 // Views should be transparent by default. Not being transparent is
46 // a promise that the view will paint all its pixels opaquely. Views
47 // should make this promise explicitly by calling
48 // SetViewContentTransparency.
50 static bool sShowPreviousPageInitialized
= false;
51 if (!sShowPreviousPageInitialized
) {
52 Preferences::AddBoolVarCache(&sShowPreviousPage
,
53 "layout.show_previous_page", true);
54 sShowPreviousPageInitialized
= true;
58 void nsView::DropMouseGrabbing() {
59 nsIPresShell
* presShell
= mViewManager
->GetPresShell();
60 if (presShell
) presShell
->ClearMouseCaptureOnView(this);
64 MOZ_COUNT_DTOR(nsView
);
66 while (GetFirstChild()) {
67 nsView
* child
= GetFirstChild();
68 if (child
->GetViewManager() == mViewManager
) {
71 // just unhook it. Someone else will want to destroy this.
79 nsView
* rootView
= mViewManager
->GetRootView();
82 // Root views can have parents!
84 mViewManager
->RemoveChild(this);
87 if (rootView
== this) {
88 // Inform the view manager that the root view has gone away...
89 mViewManager
->SetRootView(nullptr);
92 mParent
->RemoveChild(this);
95 mViewManager
= nullptr;
97 mParent
->RemoveChild(this);
100 if (mPreviousWindow
) {
101 mPreviousWindow
->SetPreviouslyAttachedWidgetListener(nullptr);
104 // Destroy and release the widget
110 class DestroyWidgetRunnable
: public Runnable
{
114 explicit DestroyWidgetRunnable(nsIWidget
* aWidget
)
115 : mozilla::Runnable("DestroyWidgetRunnable"), mWidget(aWidget
) {}
118 nsCOMPtr
<nsIWidget
> mWidget
;
121 NS_IMETHODIMP
DestroyWidgetRunnable::Run() {
127 void nsView::DestroyWidget() {
129 // If we are not attached to a base window, we're going to tear down our
130 // widget here. However, if we're attached to somebody elses widget, we
131 // want to leave the widget alone: don't reset the client data or call
132 // Destroy. Just clear our event view ptr and free our reference to it.
133 if (mWidgetIsTopLevel
) {
134 mWindow
->SetAttachedWidgetListener(nullptr);
136 mWindow
->SetWidgetListener(nullptr);
138 nsCOMPtr
<nsIRunnable
> widgetDestroyer
=
139 new DestroyWidgetRunnable(mWindow
);
141 // Don't leak if we happen to arrive here after the main thread
143 nsCOMPtr
<nsIThread
> mainThread
= do_GetMainThread();
145 mainThread
->Dispatch(widgetDestroyer
.forget(), NS_DISPATCH_NORMAL
);
153 nsView
* nsView::GetViewFor(nsIWidget
* aWidget
) {
154 MOZ_ASSERT(nullptr != aWidget
, "null widget ptr");
156 nsIWidgetListener
* listener
= aWidget
->GetWidgetListener();
158 nsView
* view
= listener
->GetView();
159 if (view
) return view
;
162 listener
= aWidget
->GetAttachedWidgetListener();
163 return listener
? listener
->GetView() : nullptr;
166 void nsView::Destroy() {
168 mozWritePoison(this, sizeof(*this));
169 nsView::operator delete(this);
172 void nsView::SetPosition(nscoord aX
, nscoord aY
) {
173 mDimBounds
.MoveBy(aX
- mPosX
, aY
- mPosY
);
177 NS_ASSERTION(GetParent() || (aX
== 0 && aY
== 0),
178 "Don't try to move the root widget to something non-zero");
180 ResetWidgetBounds(true, false);
183 void nsView::ResetWidgetBounds(bool aRecurse
, bool aForceSync
) {
186 // Don't change widget geometry synchronously, since that can
187 // cause synchronous painting.
188 mViewManager
->PostPendingUpdate();
190 DoResetWidgetBounds(false, true);
196 // reposition any widgets under this view
197 for (nsView
* v
= GetFirstChild(); v
; v
= v
->GetNextSibling()) {
198 v
->ResetWidgetBounds(true, aForceSync
);
203 bool nsView::IsEffectivelyVisible() {
204 for (nsView
* v
= this; v
; v
= v
->mParent
) {
205 if (v
->GetVisibility() == nsViewVisibility_kHide
) return false;
210 LayoutDeviceIntRect
nsView::CalcWidgetBounds(nsWindowType aType
) {
211 int32_t p2a
= mViewManager
->AppUnitsPerDevPixel();
213 nsRect
viewBounds(mDimBounds
);
215 nsView
* parent
= GetParent();
216 nsIWidget
* parentWidget
= nullptr;
219 parentWidget
= parent
->GetNearestWidget(&offset
, p2a
);
220 // make viewBounds be relative to the parent widget, in appunits
221 viewBounds
+= offset
;
223 if (parentWidget
&& aType
== eWindowType_popup
&& IsEffectivelyVisible()) {
224 // put offset into screen coordinates. (based on client area origin)
225 LayoutDeviceIntPoint screenPoint
= parentWidget
->WidgetToScreenOffset();
226 viewBounds
+= nsPoint(NSIntPixelsToAppUnits(screenPoint
.x
, p2a
),
227 NSIntPixelsToAppUnits(screenPoint
.y
, p2a
));
231 // Compute widget bounds in device pixels
232 LayoutDeviceIntRect newBounds
=
233 LayoutDeviceIntRect::FromUnknownRect(viewBounds
.ToNearestPixels(p2a
));
235 #if defined(XP_MACOSX) || defined(MOZ_WIDGET_GTK)
236 // cocoa and GTK round widget coordinates to the nearest global "display
237 // pixel" integer value. So we avoid fractional display pixel values by
238 // rounding to the nearest value that won't yield a fractional display pixel.
239 nsIWidget
* widget
= parentWidget
? parentWidget
: mWindow
.get();
241 if (aType
== eWindowType_popup
&& widget
&&
242 ((round
= widget
->RoundsWidgetCoordinatesTo()) > 1)) {
243 LayoutDeviceIntSize pixelRoundedSize
= newBounds
.Size();
244 // round the top left and bottom right to the nearest round pixel
246 NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds
.x
, p2a
) / round
) *
249 NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds
.y
, p2a
) / round
) *
252 NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds
.XMost(), p2a
) /
257 NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds
.YMost(), p2a
) /
261 // but if that makes the widget larger then our frame may not paint the
262 // extra pixels, so reduce the size to the nearest round value
263 if (newBounds
.width
> pixelRoundedSize
.width
) {
264 newBounds
.width
-= round
;
266 if (newBounds
.height
> pixelRoundedSize
.height
) {
267 newBounds
.height
-= round
;
272 // Compute where the top-left of our widget ended up relative to the parent
273 // widget, in appunits.
274 nsPoint
roundedOffset(NSIntPixelsToAppUnits(newBounds
.X(), p2a
),
275 NSIntPixelsToAppUnits(newBounds
.Y(), p2a
));
277 // mViewToWidgetOffset is added to coordinates relative to the view origin
278 // to get coordinates relative to the widget.
279 // The view origin, relative to the parent widget, is at
280 // (mPosX,mPosY) - mDimBounds.TopLeft() + viewBounds.TopLeft().
281 // Our widget, relative to the parent widget, is roundedOffset.
282 mViewToWidgetOffset
= nsPoint(mPosX
, mPosY
) - mDimBounds
.TopLeft() +
283 viewBounds
.TopLeft() - roundedOffset
;
288 void nsView::DoResetWidgetBounds(bool aMoveOnly
, bool aInvalidateChangedSize
) {
289 // The geometry of a root view's widget is controlled externally,
290 // NOT by sizing or positioning the view
291 if (mViewManager
->GetRootView() == this) {
295 MOZ_ASSERT(mWindow
, "Why was this called??");
297 // Hold this ref to make sure it stays alive.
298 nsCOMPtr
<nsIWidget
> widget
= mWindow
;
300 // Stash a copy of these and use them so we can handle this being deleted (say
301 // from sync painting/flushing from Show/Move/Resize on the widget).
302 LayoutDeviceIntRect newBounds
;
304 nsWindowType type
= widget
->WindowType();
306 LayoutDeviceIntRect curBounds
= widget
->GetClientBounds();
307 bool invisiblePopup
= type
== eWindowType_popup
&&
308 ((curBounds
.IsEmpty() && mDimBounds
.IsEmpty()) ||
309 mVis
== nsViewVisibility_kHide
);
311 if (invisiblePopup
) {
312 // We're going to hit the early exit below, avoid calling CalcWidgetBounds.
314 newBounds
= CalcWidgetBounds(type
);
315 invisiblePopup
= newBounds
.IsEmpty();
318 bool curVisibility
= widget
->IsVisible();
319 bool newVisibility
= IsEffectivelyVisible();
320 if (curVisibility
&& !newVisibility
) {
324 if (invisiblePopup
) {
325 // Don't manipulate empty or hidden popup widgets. For example there's no
326 // point moving hidden comboboxes around, or doing X server roundtrips
327 // to compute their true screen position. This could mean that
328 // WidgetToScreen operations on these widgets don't return up-to-date
329 // values, but popup positions aren't reliable anyway because of correction
330 // to be on or off-screen.
334 bool changedPos
= curBounds
.TopLeft() != newBounds
.TopLeft();
335 bool changedSize
= curBounds
.Size() != newBounds
.Size();
337 // Child views are never attached to top level widgets, this is safe.
339 // Coordinates are converted to desktop pixels for window Move/Resize APIs,
340 // because of the potential for device-pixel coordinate spaces for mixed
341 // hidpi/lodpi screens to overlap each other and result in bad placement
344 DesktopToLayoutDeviceScale scale
= widget
->GetDesktopToDeviceScaleByScreen();
346 DesktopRect deskRect
= newBounds
/ scale
;
348 if (changedSize
&& !aMoveOnly
) {
349 widget
->ResizeClient(deskRect
.X(), deskRect
.Y(), deskRect
.Width(),
350 deskRect
.Height(), aInvalidateChangedSize
);
352 widget
->MoveClient(deskRect
.X(), deskRect
.Y());
355 if (changedSize
&& !aMoveOnly
) {
356 widget
->ResizeClient(deskRect
.Width(), deskRect
.Height(),
357 aInvalidateChangedSize
);
358 } // else do nothing!
361 if (!curVisibility
&& newVisibility
) {
366 void nsView::SetDimensions(const nsRect
& aRect
, bool aPaint
,
367 bool aResizeWidget
) {
369 dims
.MoveBy(mPosX
, mPosY
);
371 // Don't use nsRect's operator== here, since it returns true when
372 // both rects are empty even if they have different widths and we
373 // have cases where that sort of thing matters to us.
374 if (mDimBounds
.TopLeft() == dims
.TopLeft() &&
375 mDimBounds
.Size() == dims
.Size()) {
382 ResetWidgetBounds(false, false);
386 void nsView::NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible
) {
387 if (!aEffectivelyVisible
) {
391 SetForcedRepaint(true);
393 if (nullptr != mWindow
) {
394 ResetWidgetBounds(false, false);
397 for (nsView
* child
= mFirstChild
; child
; child
= child
->mNextSibling
) {
398 if (child
->mVis
== nsViewVisibility_kHide
) {
399 // It was effectively hidden and still is
402 // Our child is visible if we are
403 child
->NotifyEffectiveVisibilityChanged(aEffectivelyVisible
);
407 void nsView::SetVisibility(nsViewVisibility aVisibility
) {
409 NotifyEffectiveVisibilityChanged(IsEffectivelyVisible());
412 void nsView::SetFloating(bool aFloatingView
) {
414 mVFlags
|= NS_VIEW_FLAG_FLOATING
;
416 mVFlags
&= ~NS_VIEW_FLAG_FLOATING
;
419 void nsView::InvalidateHierarchy() {
420 if (mViewManager
->GetRootView() == this) mViewManager
->InvalidateHierarchy();
422 for (nsView
* child
= mFirstChild
; child
; child
= child
->GetNextSibling())
423 child
->InvalidateHierarchy();
426 void nsView::InsertChild(nsView
* aChild
, nsView
* aSibling
) {
427 MOZ_ASSERT(nullptr != aChild
, "null ptr");
429 if (nullptr != aChild
) {
430 if (nullptr != aSibling
) {
432 NS_ASSERTION(aSibling
->GetParent() == this,
433 "tried to insert view with invalid sibling");
435 // insert after sibling
436 aChild
->SetNextSibling(aSibling
->GetNextSibling());
437 aSibling
->SetNextSibling(aChild
);
439 aChild
->SetNextSibling(mFirstChild
);
440 mFirstChild
= aChild
;
442 aChild
->SetParent(this);
444 // If we just inserted a root view, then update the RootViewManager
445 // on all view managers in the new subtree.
447 nsViewManager
* vm
= aChild
->GetViewManager();
448 if (vm
->GetRootView() == aChild
) {
449 aChild
->InvalidateHierarchy();
454 void nsView::RemoveChild(nsView
* child
) {
455 MOZ_ASSERT(nullptr != child
, "null ptr");
457 if (nullptr != child
) {
458 nsView
* prevKid
= nullptr;
459 nsView
* kid
= mFirstChild
;
460 DebugOnly
<bool> found
= false;
461 while (nullptr != kid
) {
463 if (nullptr != prevKid
) {
464 prevKid
->SetNextSibling(kid
->GetNextSibling());
466 mFirstChild
= kid
->GetNextSibling();
468 child
->SetParent(nullptr);
473 kid
= kid
->GetNextSibling();
475 NS_ASSERTION(found
, "tried to remove non child");
477 // If we just removed a root view, then update the RootViewManager
478 // on all view managers in the removed subtree.
480 nsViewManager
* vm
= child
->GetViewManager();
481 if (vm
->GetRootView() == child
) {
482 child
->InvalidateHierarchy();
487 // Native widgets ultimately just can't deal with the awesome power of
488 // CSS2 z-index. However, we set the z-index on the widget anyway
489 // because in many simple common cases the widgets do end up in the
490 // right order. We set each widget's z-index to the z-index of the
491 // nearest ancestor that has non-auto z-index.
492 static void UpdateNativeWidgetZIndexes(nsView
* aView
, int32_t aZIndex
) {
493 if (aView
->HasWidget()) {
494 nsIWidget
* widget
= aView
->GetWidget();
495 if (widget
->GetZIndex() != aZIndex
) {
496 widget
->SetZIndex(aZIndex
);
499 for (nsView
* v
= aView
->GetFirstChild(); v
; v
= v
->GetNextSibling()) {
500 if (v
->GetZIndexIsAuto()) {
501 UpdateNativeWidgetZIndexes(v
, aZIndex
);
507 static int32_t FindNonAutoZIndex(nsView
* aView
) {
509 if (!aView
->GetZIndexIsAuto()) {
510 return aView
->GetZIndex();
512 aView
= aView
->GetParent();
517 struct DefaultWidgetInitData
: public nsWidgetInitData
{
518 DefaultWidgetInitData() : nsWidgetInitData() {
519 mWindowType
= eWindowType_child
;
525 nsresult
nsView::CreateWidget(nsWidgetInitData
* aWidgetInitData
,
526 bool aEnableDragDrop
, bool aResetVisibility
) {
529 !aWidgetInitData
|| aWidgetInitData
->mWindowType
!= eWindowType_popup
,
530 "Use CreateWidgetForPopup");
532 DefaultWidgetInitData defaultInitData
;
533 bool initDataPassedIn
= !!aWidgetInitData
;
534 aWidgetInitData
= aWidgetInitData
? aWidgetInitData
: &defaultInitData
;
535 defaultInitData
.mListenForResizes
=
536 (!initDataPassedIn
&& GetParent() &&
537 GetParent()->GetViewManager() != mViewManager
);
539 LayoutDeviceIntRect trect
= CalcWidgetBounds(aWidgetInitData
->mWindowType
);
541 nsIWidget
* parentWidget
=
542 GetParent() ? GetParent()->GetNearestWidget(nullptr) : nullptr;
544 NS_ERROR("nsView::CreateWidget without suitable parent widget??");
545 return NS_ERROR_FAILURE
;
548 // XXX: using aForceUseIWidgetParent=true to preserve previous
549 // semantics. It's not clear that it's actually needed.
550 mWindow
= parentWidget
->CreateChild(trect
, aWidgetInitData
, true);
552 return NS_ERROR_FAILURE
;
555 InitializeWindow(aEnableDragDrop
, aResetVisibility
);
560 nsresult
nsView::CreateWidgetForParent(nsIWidget
* aParentWidget
,
561 nsWidgetInitData
* aWidgetInitData
,
562 bool aEnableDragDrop
,
563 bool aResetVisibility
) {
566 !aWidgetInitData
|| aWidgetInitData
->mWindowType
!= eWindowType_popup
,
567 "Use CreateWidgetForPopup");
568 MOZ_ASSERT(aParentWidget
, "Parent widget required");
570 DefaultWidgetInitData defaultInitData
;
571 aWidgetInitData
= aWidgetInitData
? aWidgetInitData
: &defaultInitData
;
573 LayoutDeviceIntRect trect
= CalcWidgetBounds(aWidgetInitData
->mWindowType
);
575 mWindow
= aParentWidget
->CreateChild(trect
, aWidgetInitData
);
577 return NS_ERROR_FAILURE
;
580 InitializeWindow(aEnableDragDrop
, aResetVisibility
);
585 nsresult
nsView::CreateWidgetForPopup(nsWidgetInitData
* aWidgetInitData
,
586 nsIWidget
* aParentWidget
,
587 bool aEnableDragDrop
,
588 bool aResetVisibility
) {
590 MOZ_ASSERT(aWidgetInitData
, "Widget init data required");
591 MOZ_ASSERT(aWidgetInitData
->mWindowType
== eWindowType_popup
,
592 "Use one of the other CreateWidget methods");
594 LayoutDeviceIntRect trect
= CalcWidgetBounds(aWidgetInitData
->mWindowType
);
596 // XXX/cjones: having these two separate creation cases seems ... um
597 // ... unnecessary, but it's the way the old code did it. Please
598 // unify them by first finding a suitable parent nsIWidget, then
599 // getting rid of aForceUseIWidgetParent.
601 // XXX: using aForceUseIWidgetParent=true to preserve previous
602 // semantics. It's not clear that it's actually needed.
603 mWindow
= aParentWidget
->CreateChild(trect
, aWidgetInitData
, true);
605 nsIWidget
* nearestParent
=
606 GetParent() ? GetParent()->GetNearestWidget(nullptr) : nullptr;
607 if (!nearestParent
) {
608 // Without a parent, we can't make a popup. This can happen
610 return NS_ERROR_FAILURE
;
613 mWindow
= nearestParent
->CreateChild(trect
, aWidgetInitData
);
616 return NS_ERROR_FAILURE
;
619 InitializeWindow(aEnableDragDrop
, aResetVisibility
);
624 void nsView::InitializeWindow(bool aEnableDragDrop
, bool aResetVisibility
) {
625 MOZ_ASSERT(mWindow
, "Must have a window to initialize");
627 mWindow
->SetWidgetListener(this);
629 if (aEnableDragDrop
) {
630 mWindow
->EnableDragDrop(true);
633 // propagate the z-index to the widget.
634 UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this));
636 // make sure visibility state is accurate
638 if (aResetVisibility
) {
639 SetVisibility(GetVisibility());
643 void nsView::SetNeedsWindowPropertiesSync() {
644 mNeedsWindowPropertiesSync
= true;
646 mViewManager
->PostPendingUpdate();
650 // Attach to a top level widget and start receiving mirrored events.
651 nsresult
nsView::AttachToTopLevelWidget(nsIWidget
* aWidget
) {
652 MOZ_ASSERT(nullptr != aWidget
, "null widget ptr");
654 /// XXXjimm This is a temporary workaround to an issue w/document
655 // viewer (bug 513162).
656 nsIWidgetListener
* listener
= aWidget
->GetAttachedWidgetListener();
658 nsView
* oldView
= listener
->GetView();
660 oldView
->DetachFromTopLevelWidget();
664 // Note, the previous device context will be released. Detaching
665 // will not restore the old one.
666 aWidget
->AttachViewToTopLevel(!nsIWidget::UsePuppetWidgets());
670 mWindow
->SetAttachedWidgetListener(this);
671 if (mWindow
->WindowType() != eWindowType_invisible
) {
672 nsresult rv
= mWindow
->AsyncEnableDragDrop(true);
673 NS_ENSURE_SUCCESS(rv
, rv
);
675 mWidgetIsTopLevel
= true;
677 // Refresh the view bounds
678 CalcWidgetBounds(mWindow
->WindowType());
683 // Detach this view from an attached widget.
684 nsresult
nsView::DetachFromTopLevelWidget() {
685 MOZ_ASSERT(mWidgetIsTopLevel
, "Not attached currently!");
686 MOZ_ASSERT(mWindow
, "null mWindow for DetachFromTopLevelWidget!");
688 mWindow
->SetAttachedWidgetListener(nullptr);
689 nsIWidgetListener
* listener
= mWindow
->GetPreviouslyAttachedWidgetListener();
691 if (listener
&& listener
->GetView()) {
692 // Ensure the listener doesn't think it's being used anymore
693 listener
->GetView()->SetPreviousWidget(nullptr);
696 // If the new view's frame is paint suppressed then the window
697 // will want to use us instead until that's done
698 mWindow
->SetPreviouslyAttachedWidgetListener(this);
700 mPreviousWindow
= mWindow
;
703 mWidgetIsTopLevel
= false;
708 void nsView::SetZIndex(bool aAuto
, int32_t aZIndex
) {
709 bool oldIsAuto
= GetZIndexIsAuto();
710 mVFlags
= (mVFlags
& ~NS_VIEW_FLAG_AUTO_ZINDEX
) |
711 (aAuto
? NS_VIEW_FLAG_AUTO_ZINDEX
: 0);
714 if (HasWidget() || !oldIsAuto
|| !aAuto
) {
715 UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this));
719 void nsView::AssertNoWindow() {
720 // XXX: it would be nice to make this a strong assert
721 if (MOZ_UNLIKELY(mWindow
)) {
722 NS_ERROR("We already have a window for this view? BAD");
723 mWindow
->SetWidgetListener(nullptr);
730 // internal window creation functions
732 void nsView::AttachWidgetEventHandler(nsIWidget
* aWidget
) {
734 NS_ASSERTION(!aWidget
->GetWidgetListener(), "Already have a widget listener");
737 aWidget
->SetWidgetListener(this);
740 void nsView::DetachWidgetEventHandler(nsIWidget
* aWidget
) {
741 NS_ASSERTION(!aWidget
->GetWidgetListener() ||
742 aWidget
->GetWidgetListener()->GetView() == this,
744 aWidget
->SetWidgetListener(nullptr);
748 void nsView::List(FILE* out
, int32_t aIndent
) const {
750 for (i
= aIndent
; --i
>= 0;) fputs(" ", out
);
751 fprintf(out
, "%p ", (void*)this);
752 if (nullptr != mWindow
) {
753 nscoord p2a
= mViewManager
->AppUnitsPerDevPixel();
754 LayoutDeviceIntRect rect
= mWindow
->GetClientBounds();
755 nsRect windowBounds
= LayoutDeviceIntRect::ToAppUnits(rect
, p2a
);
756 rect
= mWindow
->GetBounds();
757 nsRect nonclientBounds
= LayoutDeviceIntRect::ToAppUnits(rect
, p2a
);
758 nsrefcnt widgetRefCnt
= mWindow
.get()->AddRef() - 1;
759 mWindow
.get()->Release();
760 int32_t Z
= mWindow
->GetZIndex();
761 fprintf(out
, "(widget=%p[%" PRIuPTR
"] z=%d pos={%d,%d,%d,%d}) ",
762 (void*)mWindow
, widgetRefCnt
, Z
, nonclientBounds
.X(),
763 nonclientBounds
.Y(), windowBounds
.Width(), windowBounds
.Height());
765 nsRect brect
= GetBounds();
766 fprintf(out
, "{%d,%d,%d,%d}", brect
.X(), brect
.Y(), brect
.Width(),
768 fprintf(out
, " z=%d vis=%d frame=%p <\n", mZIndex
, mVis
,
769 static_cast<void*>(mFrame
));
770 for (nsView
* kid
= mFirstChild
; kid
; kid
= kid
->GetNextSibling()) {
771 NS_ASSERTION(kid
->GetParent() == this, "incorrect parent");
772 kid
->List(out
, aIndent
+ 1);
774 for (i
= aIndent
; --i
>= 0;) fputs(" ", out
);
779 nsPoint
nsView::GetOffsetTo(const nsView
* aOther
) const {
780 return GetOffsetTo(aOther
, GetViewManager()->AppUnitsPerDevPixel());
783 nsPoint
nsView::GetOffsetTo(const nsView
* aOther
, const int32_t aAPD
) const {
784 MOZ_ASSERT(GetParent() || !aOther
|| aOther
->GetParent() || this == aOther
,
785 "caller of (outer) GetOffsetTo must not pass unrelated views");
786 // We accumulate the final result in offset
787 nsPoint
offset(0, 0);
788 // The offset currently accumulated at the current APD
789 nsPoint
docOffset(0, 0);
790 const nsView
* v
= this;
791 nsViewManager
* currVM
= v
->GetViewManager();
792 int32_t currAPD
= currVM
->AppUnitsPerDevPixel();
793 const nsView
* root
= nullptr;
794 for (; v
!= aOther
&& v
; root
= v
, v
= v
->GetParent()) {
795 nsViewManager
* newVM
= v
->GetViewManager();
796 if (newVM
!= currVM
) {
797 int32_t newAPD
= newVM
->AppUnitsPerDevPixel();
798 if (newAPD
!= currAPD
) {
799 offset
+= docOffset
.ScaleToOtherAppUnits(currAPD
, aAPD
);
800 docOffset
.x
= docOffset
.y
= 0;
805 docOffset
+= v
->GetPosition();
807 offset
+= docOffset
.ScaleToOtherAppUnits(currAPD
, aAPD
);
810 // Looks like aOther wasn't an ancestor of |this|. So now we have
811 // the root-VM-relative position of |this| in |offset|. Get the
812 // root-VM-relative position of aOther and subtract it.
813 nsPoint negOffset
= aOther
->GetOffsetTo(root
, aAPD
);
820 nsPoint
nsView::GetOffsetToWidget(nsIWidget
* aWidget
) const {
822 // Get the view for widget
823 nsView
* widgetView
= GetViewFor(aWidget
);
828 // Get the offset to the widget view in the widget view's APD
829 // We get the offset in the widget view's APD first and then convert to our
830 // APD afterwards so that we can include the widget view's ViewToWidgetOffset
831 // in the sum in its native APD, and then convert the whole thing to our APD
832 // so that we don't have to convert the APD of the relatively small
833 // ViewToWidgetOffset by itself with a potentially large relative rounding
835 pt
= -widgetView
->GetOffsetTo(this);
836 // Add in the offset to the widget.
837 pt
+= widgetView
->ViewToWidgetOffset();
839 // Convert to our appunits.
840 int32_t widgetAPD
= widgetView
->GetViewManager()->AppUnitsPerDevPixel();
841 int32_t ourAPD
= GetViewManager()->AppUnitsPerDevPixel();
842 pt
= pt
.ScaleToOtherAppUnits(widgetAPD
, ourAPD
);
846 nsIWidget
* nsView::GetNearestWidget(nsPoint
* aOffset
) const {
847 return GetNearestWidget(aOffset
, GetViewManager()->AppUnitsPerDevPixel());
850 nsIWidget
* nsView::GetNearestWidget(nsPoint
* aOffset
,
851 const int32_t aAPD
) const {
852 // aOffset is based on the view's position, which ignores any chrome on
853 // attached parent widgets.
855 // We accumulate the final result in pt
857 // The offset currently accumulated at the current APD
859 const nsView
* v
= this;
860 nsViewManager
* currVM
= v
->GetViewManager();
861 int32_t currAPD
= currVM
->AppUnitsPerDevPixel();
862 for (; v
&& !v
->HasWidget(); v
= v
->GetParent()) {
863 nsViewManager
* newVM
= v
->GetViewManager();
864 if (newVM
!= currVM
) {
865 int32_t newAPD
= newVM
->AppUnitsPerDevPixel();
866 if (newAPD
!= currAPD
) {
867 pt
+= docPt
.ScaleToOtherAppUnits(currAPD
, aAPD
);
868 docPt
.x
= docPt
.y
= 0;
873 docPt
+= v
->GetPosition();
877 pt
+= docPt
.ScaleToOtherAppUnits(currAPD
, aAPD
);
883 // pt is now the offset from v's origin to this view's origin.
884 // We add the ViewToWidgetOffset to get the offset to the widget.
886 docPt
+= v
->ViewToWidgetOffset();
887 pt
+= docPt
.ScaleToOtherAppUnits(currAPD
, aAPD
);
890 return v
->GetWidget();
893 bool nsView::IsRoot() const {
894 NS_ASSERTION(mViewManager
!= nullptr,
895 " View manager is null in nsView::IsRoot()");
896 return mViewManager
->GetRootView() == this;
899 nsRect
nsView::GetBoundsInParentUnits() const {
900 nsView
* parent
= GetParent();
901 nsViewManager
* VM
= GetViewManager();
902 if (this != VM
->GetRootView() || !parent
) {
905 int32_t ourAPD
= VM
->AppUnitsPerDevPixel();
906 int32_t parentAPD
= parent
->GetViewManager()->AppUnitsPerDevPixel();
907 return mDimBounds
.ScaleToOtherAppUnitsRoundOut(ourAPD
, parentAPD
);
910 nsPoint
nsView::ConvertFromParentCoords(nsPoint aPt
) const {
911 const nsView
* parent
= GetParent();
913 aPt
= aPt
.ScaleToOtherAppUnits(
914 parent
->GetViewManager()->AppUnitsPerDevPixel(),
915 GetViewManager()->AppUnitsPerDevPixel());
917 aPt
-= GetPosition();
921 static bool IsPopupWidget(nsIWidget
* aWidget
) {
922 return (aWidget
->WindowType() == eWindowType_popup
);
925 nsIPresShell
* nsView::GetPresShell() {
926 return GetViewManager()->GetPresShell();
929 bool nsView::WindowMoved(nsIWidget
* aWidget
, int32_t x
, int32_t y
) {
930 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
931 if (pm
&& IsPopupWidget(aWidget
)) {
932 pm
->PopupMoved(mFrame
, nsIntPoint(x
, y
));
939 bool nsView::WindowResized(nsIWidget
* aWidget
, int32_t aWidth
,
941 // The root view may not be set if this is the resize associated with
943 SetForcedRepaint(true);
944 if (this == mViewManager
->GetRootView()) {
945 RefPtr
<nsDeviceContext
> devContext
= mViewManager
->GetDeviceContext();
946 // ensure DPI is up-to-date, in case of window being opened and sized
947 // on a non-default-dpi display (bug 829963)
948 devContext
->CheckDPIChange();
949 int32_t p2a
= devContext
->AppUnitsPerDevPixel();
950 mViewManager
->SetWindowDimensions(NSIntPixelsToAppUnits(aWidth
, p2a
),
951 NSIntPixelsToAppUnits(aHeight
, p2a
));
953 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
955 nsIPresShell
* presShell
= mViewManager
->GetPresShell();
956 if (presShell
&& presShell
->GetDocument()) {
957 pm
->AdjustPopupsOnWindowChange(presShell
);
963 if (IsPopupWidget(aWidget
)) {
964 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
966 pm
->PopupResized(mFrame
, LayoutDeviceIntSize(aWidth
, aHeight
));
974 bool nsView::RequestWindowClose(nsIWidget
* aWidget
) {
975 if (mFrame
&& IsPopupWidget(aWidget
) && mFrame
->IsMenuPopupFrame()) {
976 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
978 pm
->HidePopup(mFrame
->GetContent(), false, true, false, false);
986 void nsView::WillPaintWindow(nsIWidget
* aWidget
) {
987 RefPtr
<nsViewManager
> vm
= mViewManager
;
988 vm
->WillPaintWindow(aWidget
);
991 bool nsView::PaintWindow(nsIWidget
* aWidget
, LayoutDeviceIntRegion aRegion
) {
992 NS_ASSERTION(this == nsView::GetViewFor(aWidget
), "wrong view for widget?");
994 RefPtr
<nsViewManager
> vm
= mViewManager
;
995 bool result
= vm
->PaintWindow(aWidget
, aRegion
);
999 void nsView::DidPaintWindow() {
1000 RefPtr
<nsViewManager
> vm
= mViewManager
;
1001 vm
->DidPaintWindow();
1004 void nsView::DidCompositeWindow(mozilla::layers::TransactionId aTransactionId
,
1005 const TimeStamp
& aCompositeStart
,
1006 const TimeStamp
& aCompositeEnd
) {
1007 nsIPresShell
* presShell
= mViewManager
->GetPresShell();
1009 nsAutoScriptBlocker scriptBlocker
;
1011 nsPresContext
* context
= presShell
->GetPresContext();
1012 nsRootPresContext
* rootContext
= context
->GetRootPresContext();
1014 rootContext
->NotifyDidPaintForSubtree(aTransactionId
, aCompositeEnd
);
1017 // If the two timestamps are identical, this was likely a fake composite
1018 // event which wouldn't be terribly useful to display.
1019 if (aCompositeStart
== aCompositeEnd
) {
1023 nsIDocShell
* docShell
= context
->GetDocShell();
1024 RefPtr
<TimelineConsumers
> timelines
= TimelineConsumers::Get();
1026 if (timelines
&& timelines
->HasConsumer(docShell
)) {
1027 timelines
->AddMarkerForDocShell(
1028 docShell
, MakeUnique
<CompositeTimelineMarker
>(
1029 aCompositeStart
, MarkerTracingType::START
));
1030 timelines
->AddMarkerForDocShell(
1031 docShell
, MakeUnique
<CompositeTimelineMarker
>(
1032 aCompositeEnd
, MarkerTracingType::END
));
1037 void nsView::RequestRepaint() {
1038 nsIPresShell
* presShell
= mViewManager
->GetPresShell();
1040 presShell
->ScheduleViewManagerFlush();
1044 nsEventStatus
nsView::HandleEvent(WidgetGUIEvent
* aEvent
,
1045 bool aUseAttachedEvents
) {
1046 MOZ_ASSERT(nullptr != aEvent
->mWidget
, "null widget ptr");
1048 nsEventStatus result
= nsEventStatus_eIgnore
;
1050 if (aUseAttachedEvents
) {
1051 nsIWidgetListener
* listener
= aEvent
->mWidget
->GetAttachedWidgetListener();
1052 view
= listener
? listener
->GetView() : nullptr;
1054 view
= GetViewFor(aEvent
->mWidget
);
1058 RefPtr
<nsViewManager
> vm
= view
->GetViewManager();
1059 vm
->DispatchEvent(aEvent
, view
, &result
);
1065 bool nsView::IsPrimaryFramePaintSuppressed() {
1066 return sShowPreviousPage
&& mFrame
&&
1067 mFrame
->PresShell()->IsPaintingSuppressed();