michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsView.h" michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/BasicEvents.h" michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/IntegerPrintfMacros.h" michael@0: #include "mozilla/Likely.h" michael@0: #include "mozilla/Poison.h" michael@0: #include "nsIWidget.h" michael@0: #include "nsViewManager.h" michael@0: #include "nsIFrame.h" michael@0: #include "nsPresArena.h" michael@0: #include "nsXULPopupManager.h" michael@0: #include "nsIWidgetListener.h" michael@0: #include "nsContentUtils.h" // for nsAutoScriptBlocker michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsView::nsView(nsViewManager* aViewManager, nsViewVisibility aVisibility) michael@0: { michael@0: MOZ_COUNT_CTOR(nsView); michael@0: michael@0: mVis = aVisibility; michael@0: // Views should be transparent by default. Not being transparent is michael@0: // a promise that the view will paint all its pixels opaquely. Views michael@0: // should make this promise explicitly by calling michael@0: // SetViewContentTransparency. michael@0: mVFlags = 0; michael@0: mViewManager = aViewManager; michael@0: mDirtyRegion = nullptr; michael@0: mWidgetIsTopLevel = false; michael@0: } michael@0: michael@0: void nsView::DropMouseGrabbing() michael@0: { michael@0: nsIPresShell* presShell = mViewManager->GetPresShell(); michael@0: if (presShell) michael@0: presShell->ClearMouseCaptureOnView(this); michael@0: } michael@0: michael@0: nsView::~nsView() michael@0: { michael@0: MOZ_COUNT_DTOR(nsView); michael@0: michael@0: while (GetFirstChild()) michael@0: { michael@0: nsView* child = GetFirstChild(); michael@0: if (child->GetViewManager() == mViewManager) { michael@0: child->Destroy(); michael@0: } else { michael@0: // just unhook it. Someone else will want to destroy this. michael@0: RemoveChild(child); michael@0: } michael@0: } michael@0: michael@0: if (mViewManager) michael@0: { michael@0: DropMouseGrabbing(); michael@0: michael@0: nsView *rootView = mViewManager->GetRootView(); michael@0: michael@0: if (rootView) michael@0: { michael@0: // Root views can have parents! michael@0: if (mParent) michael@0: { michael@0: mViewManager->RemoveChild(this); michael@0: } michael@0: michael@0: if (rootView == this) michael@0: { michael@0: // Inform the view manager that the root view has gone away... michael@0: mViewManager->SetRootView(nullptr); michael@0: } michael@0: } michael@0: else if (mParent) michael@0: { michael@0: mParent->RemoveChild(this); michael@0: } michael@0: michael@0: mViewManager = nullptr; michael@0: } michael@0: else if (mParent) michael@0: { michael@0: mParent->RemoveChild(this); michael@0: } michael@0: michael@0: // Destroy and release the widget michael@0: DestroyWidget(); michael@0: michael@0: delete mDirtyRegion; michael@0: } michael@0: michael@0: class DestroyWidgetRunnable : public nsRunnable { michael@0: public: michael@0: NS_DECL_NSIRUNNABLE michael@0: michael@0: explicit DestroyWidgetRunnable(nsIWidget* aWidget) : mWidget(aWidget) {} michael@0: michael@0: private: michael@0: nsCOMPtr mWidget; michael@0: }; michael@0: michael@0: NS_IMETHODIMP DestroyWidgetRunnable::Run() michael@0: { michael@0: mWidget->Destroy(); michael@0: mWidget = nullptr; michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: void nsView::DestroyWidget() michael@0: { michael@0: if (mWindow) michael@0: { michael@0: // If we are not attached to a base window, we're going to tear down our michael@0: // widget here. However, if we're attached to somebody elses widget, we michael@0: // want to leave the widget alone: don't reset the client data or call michael@0: // Destroy. Just clear our event view ptr and free our reference to it. michael@0: if (mWidgetIsTopLevel) { michael@0: mWindow->SetAttachedWidgetListener(nullptr); michael@0: } michael@0: else { michael@0: mWindow->SetWidgetListener(nullptr); michael@0: michael@0: nsCOMPtr widgetDestroyer = michael@0: new DestroyWidgetRunnable(mWindow); michael@0: michael@0: NS_DispatchToMainThread(widgetDestroyer); michael@0: } michael@0: michael@0: NS_RELEASE(mWindow); michael@0: } michael@0: } michael@0: michael@0: nsView* nsView::GetViewFor(nsIWidget* aWidget) michael@0: { michael@0: NS_PRECONDITION(nullptr != aWidget, "null widget ptr"); michael@0: michael@0: nsIWidgetListener* listener = aWidget->GetWidgetListener(); michael@0: if (listener) { michael@0: nsView* view = listener->GetView(); michael@0: if (view) michael@0: return view; michael@0: } michael@0: michael@0: listener = aWidget->GetAttachedWidgetListener(); michael@0: return listener ? listener->GetView() : nullptr; michael@0: } michael@0: michael@0: void nsView::Destroy() michael@0: { michael@0: this->~nsView(); michael@0: mozWritePoison(this, sizeof(*this)); michael@0: nsView::operator delete(this); michael@0: } michael@0: michael@0: void nsView::SetPosition(nscoord aX, nscoord aY) michael@0: { michael@0: mDimBounds.x += aX - mPosX; michael@0: mDimBounds.y += aY - mPosY; michael@0: mPosX = aX; michael@0: mPosY = aY; michael@0: michael@0: NS_ASSERTION(GetParent() || (aX == 0 && aY == 0), michael@0: "Don't try to move the root widget to something non-zero"); michael@0: michael@0: ResetWidgetBounds(true, false); michael@0: } michael@0: michael@0: void nsView::ResetWidgetBounds(bool aRecurse, bool aForceSync) michael@0: { michael@0: if (mWindow) { michael@0: if (!aForceSync) { michael@0: // Don't change widget geometry synchronously, since that can michael@0: // cause synchronous painting. michael@0: mViewManager->PostPendingUpdate(); michael@0: } else { michael@0: DoResetWidgetBounds(false, true); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: if (aRecurse) { michael@0: // reposition any widgets under this view michael@0: for (nsView* v = GetFirstChild(); v; v = v->GetNextSibling()) { michael@0: v->ResetWidgetBounds(true, aForceSync); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool nsView::IsEffectivelyVisible() michael@0: { michael@0: for (nsView* v = this; v; v = v->mParent) { michael@0: if (v->GetVisibility() == nsViewVisibility_kHide) michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: nsIntRect nsView::CalcWidgetBounds(nsWindowType aType) michael@0: { michael@0: int32_t p2a = mViewManager->AppUnitsPerDevPixel(); michael@0: michael@0: nsRect viewBounds(mDimBounds); michael@0: michael@0: nsView* parent = GetParent(); michael@0: nsIWidget* parentWidget = nullptr; michael@0: if (parent) { michael@0: nsPoint offset; michael@0: parentWidget = parent->GetNearestWidget(&offset, p2a); michael@0: // make viewBounds be relative to the parent widget, in appunits michael@0: viewBounds += offset; michael@0: michael@0: if (parentWidget && aType == eWindowType_popup && michael@0: IsEffectivelyVisible()) { michael@0: // put offset into screen coordinates. (based on client area origin) michael@0: nsIntPoint screenPoint = parentWidget->WidgetToScreenOffset(); michael@0: viewBounds += nsPoint(NSIntPixelsToAppUnits(screenPoint.x, p2a), michael@0: NSIntPixelsToAppUnits(screenPoint.y, p2a)); michael@0: } michael@0: } michael@0: michael@0: // Compute widget bounds in device pixels michael@0: nsIntRect newBounds = viewBounds.ToNearestPixels(p2a); michael@0: michael@0: #ifdef XP_MACOSX michael@0: // cocoa rounds widget coordinates to the nearest global "display pixel" michael@0: // integer value. So we avoid fractional display pixel values by rounding michael@0: // to the nearest value that won't yield a fractional display pixel. michael@0: nsIWidget* widget = parentWidget ? parentWidget : mWindow; michael@0: uint32_t round; michael@0: if (aType == eWindowType_popup && widget && michael@0: ((round = widget->RoundsWidgetCoordinatesTo()) > 1)) { michael@0: nsIntSize pixelRoundedSize = newBounds.Size(); michael@0: // round the top left and bottom right to the nearest round pixel michael@0: newBounds.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.x, p2a) / round) * round; michael@0: newBounds.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.y, p2a) / round) * round; michael@0: newBounds.width = michael@0: NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.XMost(), p2a) / round) * round - newBounds.x; michael@0: newBounds.height = michael@0: NSToIntRoundUp(NSAppUnitsToDoublePixels(viewBounds.YMost(), p2a) / round) * round - newBounds.y; michael@0: // but if that makes the widget larger then our frame may not paint the michael@0: // extra pixels, so reduce the size to the nearest round value michael@0: if (newBounds.width > pixelRoundedSize.width) { michael@0: newBounds.width -= round; michael@0: } michael@0: if (newBounds.height > pixelRoundedSize.height) { michael@0: newBounds.height -= round; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // Compute where the top-left of our widget ended up relative to the parent michael@0: // widget, in appunits. michael@0: nsPoint roundedOffset(NSIntPixelsToAppUnits(newBounds.x, p2a), michael@0: NSIntPixelsToAppUnits(newBounds.y, p2a)); michael@0: michael@0: // mViewToWidgetOffset is added to coordinates relative to the view origin michael@0: // to get coordinates relative to the widget. michael@0: // The view origin, relative to the parent widget, is at michael@0: // (mPosX,mPosY) - mDimBounds.TopLeft() + viewBounds.TopLeft(). michael@0: // Our widget, relative to the parent widget, is roundedOffset. michael@0: mViewToWidgetOffset = nsPoint(mPosX, mPosY) michael@0: - mDimBounds.TopLeft() + viewBounds.TopLeft() - roundedOffset; michael@0: michael@0: return newBounds; michael@0: } michael@0: michael@0: void nsView::DoResetWidgetBounds(bool aMoveOnly, michael@0: bool aInvalidateChangedSize) { michael@0: // The geometry of a root view's widget is controlled externally, michael@0: // NOT by sizing or positioning the view michael@0: if (mViewManager->GetRootView() == this) { michael@0: return; michael@0: } michael@0: michael@0: NS_PRECONDITION(mWindow, "Why was this called??"); michael@0: michael@0: // Hold this ref to make sure it stays alive. michael@0: nsCOMPtr widget = mWindow; michael@0: michael@0: // Stash a copy of these and use them so we can handle this being deleted (say michael@0: // from sync painting/flushing from Show/Move/Resize on the widget). michael@0: nsIntRect newBounds; michael@0: nsRefPtr dx = mViewManager->GetDeviceContext(); michael@0: michael@0: nsWindowType type = widget->WindowType(); michael@0: michael@0: nsIntRect curBounds; michael@0: widget->GetClientBounds(curBounds); michael@0: bool invisiblePopup = type == eWindowType_popup && michael@0: ((curBounds.IsEmpty() && mDimBounds.IsEmpty()) || michael@0: mVis == nsViewVisibility_kHide); michael@0: michael@0: if (invisiblePopup) { michael@0: // We're going to hit the early exit below, avoid calling CalcWidgetBounds. michael@0: } else { michael@0: newBounds = CalcWidgetBounds(type); michael@0: } michael@0: michael@0: bool curVisibility = widget->IsVisible(); michael@0: bool newVisibility = IsEffectivelyVisible(); michael@0: if (curVisibility && !newVisibility) { michael@0: widget->Show(false); michael@0: } michael@0: michael@0: if (invisiblePopup) { michael@0: // Don't manipulate empty or hidden popup widgets. For example there's no michael@0: // point moving hidden comboboxes around, or doing X server roundtrips michael@0: // to compute their true screen position. This could mean that WidgetToScreen michael@0: // operations on these widgets don't return up-to-date values, but popup michael@0: // positions aren't reliable anyway because of correction to be on or off-screen. michael@0: return; michael@0: } michael@0: michael@0: bool changedPos = curBounds.TopLeft() != newBounds.TopLeft(); michael@0: bool changedSize = curBounds.Size() != newBounds.Size(); michael@0: michael@0: // Child views are never attached to top level widgets, this is safe. michael@0: michael@0: // Coordinates are converted to display pixels for window Move/Resize APIs, michael@0: // because of the potential for device-pixel coordinate spaces for mixed michael@0: // hidpi/lodpi screens to overlap each other and result in bad placement michael@0: // (bug 814434). michael@0: double invScale; michael@0: michael@0: // Bug 861270: for correct widget manipulation at arbitrary scale factors, michael@0: // prefer to base scaling on widget->GetDefaultScale(). But only do this if michael@0: // it matches the view manager's device context scale after allowing for the michael@0: // quantization to app units, because of OS X multiscreen issues (where the michael@0: // only two scales are 1.0 or 2.0, and so the quantization doesn't actually michael@0: // cause problems anyhow). michael@0: // In the case of a mismatch, fall back to scaling based on the dev context's michael@0: // unscaledAppUnitsPerDevPixel value. On platforms where the device-pixel michael@0: // scale is uniform across all displays (currently all except OS X), we'll michael@0: // always use the precise value from mWindow->GetDefaultScale here. michael@0: CSSToLayoutDeviceScale scale = widget->GetDefaultScale(); michael@0: if (NSToIntRound(60.0 / scale.scale) == dx->UnscaledAppUnitsPerDevPixel()) { michael@0: invScale = 1.0 / scale.scale; michael@0: } else { michael@0: invScale = dx->UnscaledAppUnitsPerDevPixel() / 60.0; michael@0: } michael@0: michael@0: if (changedPos) { michael@0: if (changedSize && !aMoveOnly) { michael@0: widget->ResizeClient(newBounds.x * invScale, michael@0: newBounds.y * invScale, michael@0: newBounds.width * invScale, michael@0: newBounds.height * invScale, michael@0: aInvalidateChangedSize); michael@0: } else { michael@0: widget->MoveClient(newBounds.x * invScale, michael@0: newBounds.y * invScale); michael@0: } michael@0: } else { michael@0: if (changedSize && !aMoveOnly) { michael@0: widget->ResizeClient(newBounds.width * invScale, michael@0: newBounds.height * invScale, michael@0: aInvalidateChangedSize); michael@0: } // else do nothing! michael@0: } michael@0: michael@0: if (!curVisibility && newVisibility) { michael@0: widget->Show(true); michael@0: } michael@0: } michael@0: michael@0: void nsView::SetDimensions(const nsRect& aRect, bool aPaint, bool aResizeWidget) michael@0: { michael@0: nsRect dims = aRect; michael@0: dims.MoveBy(mPosX, mPosY); michael@0: michael@0: // Don't use nsRect's operator== here, since it returns true when michael@0: // both rects are empty even if they have different widths and we michael@0: // have cases where that sort of thing matters to us. michael@0: if (mDimBounds.TopLeft() == dims.TopLeft() && michael@0: mDimBounds.Size() == dims.Size()) { michael@0: return; michael@0: } michael@0: michael@0: mDimBounds = dims; michael@0: michael@0: if (aResizeWidget) { michael@0: ResetWidgetBounds(false, false); michael@0: } michael@0: } michael@0: michael@0: void nsView::NotifyEffectiveVisibilityChanged(bool aEffectivelyVisible) michael@0: { michael@0: if (!aEffectivelyVisible) michael@0: { michael@0: DropMouseGrabbing(); michael@0: } michael@0: michael@0: SetForcedRepaint(true); michael@0: michael@0: if (nullptr != mWindow) michael@0: { michael@0: ResetWidgetBounds(false, false); michael@0: } michael@0: michael@0: for (nsView* child = mFirstChild; child; child = child->mNextSibling) { michael@0: if (child->mVis == nsViewVisibility_kHide) { michael@0: // It was effectively hidden and still is michael@0: continue; michael@0: } michael@0: // Our child is visible if we are michael@0: child->NotifyEffectiveVisibilityChanged(aEffectivelyVisible); michael@0: } michael@0: } michael@0: michael@0: void nsView::SetVisibility(nsViewVisibility aVisibility) michael@0: { michael@0: mVis = aVisibility; michael@0: NotifyEffectiveVisibilityChanged(IsEffectivelyVisible()); michael@0: } michael@0: michael@0: void nsView::SetFloating(bool aFloatingView) michael@0: { michael@0: if (aFloatingView) michael@0: mVFlags |= NS_VIEW_FLAG_FLOATING; michael@0: else michael@0: mVFlags &= ~NS_VIEW_FLAG_FLOATING; michael@0: } michael@0: michael@0: void nsView::InvalidateHierarchy(nsViewManager *aViewManagerParent) michael@0: { michael@0: if (mViewManager->GetRootView() == this) michael@0: mViewManager->InvalidateHierarchy(); michael@0: michael@0: for (nsView *child = mFirstChild; child; child = child->GetNextSibling()) michael@0: child->InvalidateHierarchy(aViewManagerParent); michael@0: } michael@0: michael@0: void nsView::InsertChild(nsView *aChild, nsView *aSibling) michael@0: { michael@0: NS_PRECONDITION(nullptr != aChild, "null ptr"); michael@0: michael@0: if (nullptr != aChild) michael@0: { michael@0: if (nullptr != aSibling) michael@0: { michael@0: #ifdef DEBUG michael@0: NS_ASSERTION(aSibling->GetParent() == this, "tried to insert view with invalid sibling"); michael@0: #endif michael@0: //insert after sibling michael@0: aChild->SetNextSibling(aSibling->GetNextSibling()); michael@0: aSibling->SetNextSibling(aChild); michael@0: } michael@0: else michael@0: { michael@0: aChild->SetNextSibling(mFirstChild); michael@0: mFirstChild = aChild; michael@0: } michael@0: aChild->SetParent(this); michael@0: michael@0: // If we just inserted a root view, then update the RootViewManager michael@0: // on all view managers in the new subtree. michael@0: michael@0: nsViewManager *vm = aChild->GetViewManager(); michael@0: if (vm->GetRootView() == aChild) michael@0: { michael@0: aChild->InvalidateHierarchy(nullptr); // don't care about releasing grabs michael@0: } michael@0: } michael@0: } michael@0: michael@0: void nsView::RemoveChild(nsView *child) michael@0: { michael@0: NS_PRECONDITION(nullptr != child, "null ptr"); michael@0: michael@0: if (nullptr != child) michael@0: { michael@0: nsView* prevKid = nullptr; michael@0: nsView* kid = mFirstChild; michael@0: DebugOnly found = false; michael@0: while (nullptr != kid) { michael@0: if (kid == child) { michael@0: if (nullptr != prevKid) { michael@0: prevKid->SetNextSibling(kid->GetNextSibling()); michael@0: } else { michael@0: mFirstChild = kid->GetNextSibling(); michael@0: } michael@0: child->SetParent(nullptr); michael@0: found = true; michael@0: break; michael@0: } michael@0: prevKid = kid; michael@0: kid = kid->GetNextSibling(); michael@0: } michael@0: NS_ASSERTION(found, "tried to remove non child"); michael@0: michael@0: // If we just removed a root view, then update the RootViewManager michael@0: // on all view managers in the removed subtree. michael@0: michael@0: nsViewManager *vm = child->GetViewManager(); michael@0: if (vm->GetRootView() == child) michael@0: { michael@0: child->InvalidateHierarchy(GetViewManager()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Native widgets ultimately just can't deal with the awesome power of michael@0: // CSS2 z-index. However, we set the z-index on the widget anyway michael@0: // because in many simple common cases the widgets do end up in the michael@0: // right order. We set each widget's z-index to the z-index of the michael@0: // nearest ancestor that has non-auto z-index. michael@0: static void UpdateNativeWidgetZIndexes(nsView* aView, int32_t aZIndex) michael@0: { michael@0: if (aView->HasWidget()) { michael@0: nsIWidget* widget = aView->GetWidget(); michael@0: if (widget->GetZIndex() != aZIndex) { michael@0: widget->SetZIndex(aZIndex); michael@0: } michael@0: } else { michael@0: for (nsView* v = aView->GetFirstChild(); v; v = v->GetNextSibling()) { michael@0: if (v->GetZIndexIsAuto()) { michael@0: UpdateNativeWidgetZIndexes(v, aZIndex); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: static int32_t FindNonAutoZIndex(nsView* aView) michael@0: { michael@0: while (aView) { michael@0: if (!aView->GetZIndexIsAuto()) { michael@0: return aView->GetZIndex(); michael@0: } michael@0: aView = aView->GetParent(); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: struct DefaultWidgetInitData : public nsWidgetInitData { michael@0: DefaultWidgetInitData() : nsWidgetInitData() michael@0: { michael@0: mWindowType = eWindowType_child; michael@0: clipChildren = true; michael@0: clipSiblings = true; michael@0: } michael@0: }; michael@0: michael@0: nsresult nsView::CreateWidget(nsWidgetInitData *aWidgetInitData, michael@0: bool aEnableDragDrop, michael@0: bool aResetVisibility) michael@0: { michael@0: AssertNoWindow(); michael@0: NS_ABORT_IF_FALSE(!aWidgetInitData || michael@0: aWidgetInitData->mWindowType != eWindowType_popup, michael@0: "Use CreateWidgetForPopup"); michael@0: michael@0: DefaultWidgetInitData defaultInitData; michael@0: bool initDataPassedIn = !!aWidgetInitData; michael@0: aWidgetInitData = aWidgetInitData ? aWidgetInitData : &defaultInitData; michael@0: defaultInitData.mListenForResizes = michael@0: (!initDataPassedIn && GetParent() && michael@0: GetParent()->GetViewManager() != mViewManager); michael@0: michael@0: nsIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType); michael@0: michael@0: nsRefPtr dx = mViewManager->GetDeviceContext(); michael@0: michael@0: nsIWidget* parentWidget = michael@0: GetParent() ? GetParent()->GetNearestWidget(nullptr) : nullptr; michael@0: if (!parentWidget) { michael@0: NS_ERROR("nsView::CreateWidget without suitable parent widget??"); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: // XXX: using aForceUseIWidgetParent=true to preserve previous michael@0: // semantics. It's not clear that it's actually needed. michael@0: mWindow = parentWidget->CreateChild(trect, dx, aWidgetInitData, michael@0: true).take(); michael@0: if (!mWindow) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: InitializeWindow(aEnableDragDrop, aResetVisibility); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult nsView::CreateWidgetForParent(nsIWidget* aParentWidget, michael@0: nsWidgetInitData *aWidgetInitData, michael@0: bool aEnableDragDrop, michael@0: bool aResetVisibility) michael@0: { michael@0: AssertNoWindow(); michael@0: NS_ABORT_IF_FALSE(!aWidgetInitData || michael@0: aWidgetInitData->mWindowType != eWindowType_popup, michael@0: "Use CreateWidgetForPopup"); michael@0: NS_ABORT_IF_FALSE(aParentWidget, "Parent widget required"); michael@0: michael@0: DefaultWidgetInitData defaultInitData; michael@0: aWidgetInitData = aWidgetInitData ? aWidgetInitData : &defaultInitData; michael@0: michael@0: nsIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType); michael@0: michael@0: nsRefPtr dx = mViewManager->GetDeviceContext(); michael@0: michael@0: mWindow = michael@0: aParentWidget->CreateChild(trect, dx, aWidgetInitData).take(); michael@0: if (!mWindow) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: InitializeWindow(aEnableDragDrop, aResetVisibility); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult nsView::CreateWidgetForPopup(nsWidgetInitData *aWidgetInitData, michael@0: nsIWidget* aParentWidget, michael@0: bool aEnableDragDrop, michael@0: bool aResetVisibility) michael@0: { michael@0: AssertNoWindow(); michael@0: NS_ABORT_IF_FALSE(aWidgetInitData, "Widget init data required"); michael@0: NS_ABORT_IF_FALSE(aWidgetInitData->mWindowType == eWindowType_popup, michael@0: "Use one of the other CreateWidget methods"); michael@0: michael@0: nsIntRect trect = CalcWidgetBounds(aWidgetInitData->mWindowType); michael@0: michael@0: nsRefPtr dx = mViewManager->GetDeviceContext(); michael@0: michael@0: // XXX/cjones: having these two separate creation cases seems ... um michael@0: // ... unnecessary, but it's the way the old code did it. Please michael@0: // unify them by first finding a suitable parent nsIWidget, then michael@0: // getting rid of aForceUseIWidgetParent. michael@0: if (aParentWidget) { michael@0: // XXX: using aForceUseIWidgetParent=true to preserve previous michael@0: // semantics. It's not clear that it's actually needed. michael@0: mWindow = aParentWidget->CreateChild(trect, dx, aWidgetInitData, michael@0: true).take(); michael@0: } michael@0: else { michael@0: nsIWidget* nearestParent = GetParent() ? GetParent()->GetNearestWidget(nullptr) michael@0: : nullptr; michael@0: if (!nearestParent) { michael@0: // Without a parent, we can't make a popup. This can happen michael@0: // when printing michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: mWindow = michael@0: nearestParent->CreateChild(trect, dx, aWidgetInitData).take(); michael@0: } michael@0: if (!mWindow) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: InitializeWindow(aEnableDragDrop, aResetVisibility); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsView::InitializeWindow(bool aEnableDragDrop, bool aResetVisibility) michael@0: { michael@0: NS_ABORT_IF_FALSE(mWindow, "Must have a window to initialize"); michael@0: michael@0: mWindow->SetWidgetListener(this); michael@0: michael@0: if (aEnableDragDrop) { michael@0: mWindow->EnableDragDrop(true); michael@0: } michael@0: michael@0: // propagate the z-index to the widget. michael@0: UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this)); michael@0: michael@0: //make sure visibility state is accurate michael@0: michael@0: if (aResetVisibility) { michael@0: SetVisibility(GetVisibility()); michael@0: } michael@0: } michael@0: michael@0: // Attach to a top level widget and start receiving mirrored events. michael@0: nsresult nsView::AttachToTopLevelWidget(nsIWidget* aWidget) michael@0: { michael@0: NS_PRECONDITION(nullptr != aWidget, "null widget ptr"); michael@0: /// XXXjimm This is a temporary workaround to an issue w/document michael@0: // viewer (bug 513162). michael@0: nsIWidgetListener* listener = aWidget->GetAttachedWidgetListener(); michael@0: if (listener) { michael@0: nsView *oldView = listener->GetView(); michael@0: if (oldView) { michael@0: oldView->DetachFromTopLevelWidget(); michael@0: } michael@0: } michael@0: michael@0: nsRefPtr dx = mViewManager->GetDeviceContext(); michael@0: michael@0: // Note, the previous device context will be released. Detaching michael@0: // will not restore the old one. michael@0: nsresult rv = aWidget->AttachViewToTopLevel(!nsIWidget::UsePuppetWidgets(), dx); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: mWindow = aWidget; michael@0: NS_ADDREF(mWindow); michael@0: michael@0: mWindow->SetAttachedWidgetListener(this); michael@0: mWindow->EnableDragDrop(true); michael@0: mWidgetIsTopLevel = true; michael@0: michael@0: // Refresh the view bounds michael@0: CalcWidgetBounds(mWindow->WindowType()); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Detach this view from an attached widget. michael@0: nsresult nsView::DetachFromTopLevelWidget() michael@0: { michael@0: NS_PRECONDITION(mWidgetIsTopLevel, "Not attached currently!"); michael@0: NS_PRECONDITION(mWindow, "null mWindow for DetachFromTopLevelWidget!"); michael@0: michael@0: mWindow->SetAttachedWidgetListener(nullptr); michael@0: NS_RELEASE(mWindow); michael@0: michael@0: mWidgetIsTopLevel = false; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void nsView::SetZIndex(bool aAuto, int32_t aZIndex) michael@0: { michael@0: bool oldIsAuto = GetZIndexIsAuto(); michael@0: mVFlags = (mVFlags & ~NS_VIEW_FLAG_AUTO_ZINDEX) | (aAuto ? NS_VIEW_FLAG_AUTO_ZINDEX : 0); michael@0: mZIndex = aZIndex; michael@0: michael@0: if (HasWidget() || !oldIsAuto || !aAuto) { michael@0: UpdateNativeWidgetZIndexes(this, FindNonAutoZIndex(this)); michael@0: } michael@0: } michael@0: michael@0: void nsView::AssertNoWindow() michael@0: { michael@0: // XXX: it would be nice to make this a strong assert michael@0: if (MOZ_UNLIKELY(mWindow)) { michael@0: NS_ERROR("We already have a window for this view? BAD"); michael@0: mWindow->SetWidgetListener(nullptr); michael@0: mWindow->Destroy(); michael@0: NS_RELEASE(mWindow); michael@0: } michael@0: } michael@0: michael@0: // michael@0: // internal window creation functions michael@0: // michael@0: void nsView::AttachWidgetEventHandler(nsIWidget* aWidget) michael@0: { michael@0: #ifdef DEBUG michael@0: NS_ASSERTION(!aWidget->GetWidgetListener(), "Already have a widget listener"); michael@0: #endif michael@0: michael@0: aWidget->SetWidgetListener(this); michael@0: } michael@0: michael@0: void nsView::DetachWidgetEventHandler(nsIWidget* aWidget) michael@0: { michael@0: NS_ASSERTION(!aWidget->GetWidgetListener() || michael@0: aWidget->GetWidgetListener()->GetView() == this, "Wrong view"); michael@0: aWidget->SetWidgetListener(nullptr); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void nsView::List(FILE* out, int32_t aIndent) const michael@0: { michael@0: int32_t i; michael@0: for (i = aIndent; --i >= 0; ) fputs(" ", out); michael@0: fprintf(out, "%p ", (void*)this); michael@0: if (nullptr != mWindow) { michael@0: nscoord p2a = mViewManager->AppUnitsPerDevPixel(); michael@0: nsIntRect rect; michael@0: mWindow->GetClientBounds(rect); michael@0: nsRect windowBounds = rect.ToAppUnits(p2a); michael@0: mWindow->GetBounds(rect); michael@0: nsRect nonclientBounds = rect.ToAppUnits(p2a); michael@0: nsrefcnt widgetRefCnt = mWindow->AddRef() - 1; michael@0: mWindow->Release(); michael@0: int32_t Z = mWindow->GetZIndex(); michael@0: fprintf(out, "(widget=%p[%" PRIuPTR "] z=%d pos={%d,%d,%d,%d}) ", michael@0: (void*)mWindow, widgetRefCnt, Z, michael@0: nonclientBounds.x, nonclientBounds.y, michael@0: windowBounds.width, windowBounds.height); michael@0: } michael@0: nsRect brect = GetBounds(); michael@0: fprintf(out, "{%d,%d,%d,%d}", michael@0: brect.x, brect.y, brect.width, brect.height); michael@0: fprintf(out, " z=%d vis=%d frame=%p <\n", michael@0: mZIndex, mVis, static_cast(mFrame)); michael@0: for (nsView* kid = mFirstChild; kid; kid = kid->GetNextSibling()) { michael@0: NS_ASSERTION(kid->GetParent() == this, "incorrect parent"); michael@0: kid->List(out, aIndent + 1); michael@0: } michael@0: for (i = aIndent; --i >= 0; ) fputs(" ", out); michael@0: fputs(">\n", out); michael@0: } michael@0: #endif // DEBUG michael@0: michael@0: nsPoint nsView::GetOffsetTo(const nsView* aOther) const michael@0: { michael@0: return GetOffsetTo(aOther, GetViewManager()->AppUnitsPerDevPixel()); michael@0: } michael@0: michael@0: nsPoint nsView::GetOffsetTo(const nsView* aOther, const int32_t aAPD) const michael@0: { michael@0: NS_ABORT_IF_FALSE(GetParent() || !aOther || aOther->GetParent() || michael@0: this == aOther, "caller of (outer) GetOffsetTo must not " michael@0: "pass unrelated views"); michael@0: // We accumulate the final result in offset michael@0: nsPoint offset(0, 0); michael@0: // The offset currently accumulated at the current APD michael@0: nsPoint docOffset(0, 0); michael@0: const nsView* v = this; michael@0: nsViewManager* currVM = v->GetViewManager(); michael@0: int32_t currAPD = currVM->AppUnitsPerDevPixel(); michael@0: const nsView* root = nullptr; michael@0: for ( ; v != aOther && v; root = v, v = v->GetParent()) { michael@0: nsViewManager* newVM = v->GetViewManager(); michael@0: if (newVM != currVM) { michael@0: int32_t newAPD = newVM->AppUnitsPerDevPixel(); michael@0: if (newAPD != currAPD) { michael@0: offset += docOffset.ConvertAppUnits(currAPD, aAPD); michael@0: docOffset.x = docOffset.y = 0; michael@0: currAPD = newAPD; michael@0: } michael@0: currVM = newVM; michael@0: } michael@0: docOffset += v->GetPosition(); michael@0: } michael@0: offset += docOffset.ConvertAppUnits(currAPD, aAPD); michael@0: michael@0: if (v != aOther) { michael@0: // Looks like aOther wasn't an ancestor of |this|. So now we have michael@0: // the root-VM-relative position of |this| in |offset|. Get the michael@0: // root-VM-relative position of aOther and subtract it. michael@0: nsPoint negOffset = aOther->GetOffsetTo(root, aAPD); michael@0: offset -= negOffset; michael@0: } michael@0: michael@0: return offset; michael@0: } michael@0: michael@0: nsPoint nsView::GetOffsetToWidget(nsIWidget* aWidget) const michael@0: { michael@0: nsPoint pt; michael@0: // Get the view for widget michael@0: nsView* widgetView = GetViewFor(aWidget); michael@0: if (!widgetView) { michael@0: return pt; michael@0: } michael@0: michael@0: // Get the offset to the widget view in the widget view's APD michael@0: // We get the offset in the widget view's APD first and then convert to our michael@0: // APD afterwards so that we can include the widget view's ViewToWidgetOffset michael@0: // in the sum in its native APD, and then convert the whole thing to our APD michael@0: // so that we don't have to convert the APD of the relatively small michael@0: // ViewToWidgetOffset by itself with a potentially large relative rounding michael@0: // error. michael@0: pt = -widgetView->GetOffsetTo(this); michael@0: // Add in the offset to the widget. michael@0: pt += widgetView->ViewToWidgetOffset(); michael@0: michael@0: // Convert to our appunits. michael@0: int32_t widgetAPD = widgetView->GetViewManager()->AppUnitsPerDevPixel(); michael@0: int32_t ourAPD = GetViewManager()->AppUnitsPerDevPixel(); michael@0: pt = pt.ConvertAppUnits(widgetAPD, ourAPD); michael@0: return pt; michael@0: } michael@0: michael@0: nsIWidget* nsView::GetNearestWidget(nsPoint* aOffset) const michael@0: { michael@0: return GetNearestWidget(aOffset, GetViewManager()->AppUnitsPerDevPixel()); michael@0: } michael@0: michael@0: nsIWidget* nsView::GetNearestWidget(nsPoint* aOffset, const int32_t aAPD) const michael@0: { michael@0: // aOffset is based on the view's position, which ignores any chrome on michael@0: // attached parent widgets. michael@0: michael@0: // We accumulate the final result in pt michael@0: nsPoint pt(0, 0); michael@0: // The offset currently accumulated at the current APD michael@0: nsPoint docPt(0,0); michael@0: const nsView* v = this; michael@0: nsViewManager* currVM = v->GetViewManager(); michael@0: int32_t currAPD = currVM->AppUnitsPerDevPixel(); michael@0: for ( ; v && !v->HasWidget(); v = v->GetParent()) { michael@0: nsViewManager* newVM = v->GetViewManager(); michael@0: if (newVM != currVM) { michael@0: int32_t newAPD = newVM->AppUnitsPerDevPixel(); michael@0: if (newAPD != currAPD) { michael@0: pt += docPt.ConvertAppUnits(currAPD, aAPD); michael@0: docPt.x = docPt.y = 0; michael@0: currAPD = newAPD; michael@0: } michael@0: currVM = newVM; michael@0: } michael@0: docPt += v->GetPosition(); michael@0: } michael@0: if (!v) { michael@0: if (aOffset) { michael@0: pt += docPt.ConvertAppUnits(currAPD, aAPD); michael@0: *aOffset = pt; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: // pt is now the offset from v's origin to this view's origin. michael@0: // We add the ViewToWidgetOffset to get the offset to the widget. michael@0: if (aOffset) { michael@0: docPt += v->ViewToWidgetOffset(); michael@0: pt += docPt.ConvertAppUnits(currAPD, aAPD); michael@0: *aOffset = pt; michael@0: } michael@0: return v->GetWidget(); michael@0: } michael@0: michael@0: bool nsView::IsRoot() const michael@0: { michael@0: NS_ASSERTION(mViewManager != nullptr," View manager is null in nsView::IsRoot()"); michael@0: return mViewManager->GetRootView() == this; michael@0: } michael@0: michael@0: nsRect michael@0: nsView::GetBoundsInParentUnits() const michael@0: { michael@0: nsView* parent = GetParent(); michael@0: nsViewManager* VM = GetViewManager(); michael@0: if (this != VM->GetRootView() || !parent) { michael@0: return mDimBounds; michael@0: } michael@0: int32_t ourAPD = VM->AppUnitsPerDevPixel(); michael@0: int32_t parentAPD = parent->GetViewManager()->AppUnitsPerDevPixel(); michael@0: return mDimBounds.ConvertAppUnitsRoundOut(ourAPD, parentAPD); michael@0: } michael@0: michael@0: nsPoint michael@0: nsView::ConvertFromParentCoords(nsPoint aPt) const michael@0: { michael@0: const nsView* parent = GetParent(); michael@0: if (parent) { michael@0: aPt = aPt.ConvertAppUnits(parent->GetViewManager()->AppUnitsPerDevPixel(), michael@0: GetViewManager()->AppUnitsPerDevPixel()); michael@0: } michael@0: aPt -= GetPosition(); michael@0: return aPt; michael@0: } michael@0: michael@0: static bool michael@0: IsPopupWidget(nsIWidget* aWidget) michael@0: { michael@0: return (aWidget->WindowType() == eWindowType_popup); michael@0: } michael@0: michael@0: nsIPresShell* michael@0: nsView::GetPresShell() michael@0: { michael@0: return GetViewManager()->GetPresShell(); michael@0: } michael@0: michael@0: bool michael@0: nsView::WindowMoved(nsIWidget* aWidget, int32_t x, int32_t y) michael@0: { michael@0: nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); michael@0: if (pm && IsPopupWidget(aWidget)) { michael@0: pm->PopupMoved(mFrame, nsIntPoint(x, y)); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsView::WindowResized(nsIWidget* aWidget, int32_t aWidth, int32_t aHeight) michael@0: { michael@0: // The root view may not be set if this is the resize associated with michael@0: // window creation michael@0: SetForcedRepaint(true); michael@0: if (this == mViewManager->GetRootView()) { michael@0: nsRefPtr devContext = mViewManager->GetDeviceContext(); michael@0: // ensure DPI is up-to-date, in case of window being opened and sized michael@0: // on a non-default-dpi display (bug 829963) michael@0: devContext->CheckDPIChange(); michael@0: int32_t p2a = devContext->AppUnitsPerDevPixel(); michael@0: mViewManager->SetWindowDimensions(NSIntPixelsToAppUnits(aWidth, p2a), michael@0: NSIntPixelsToAppUnits(aHeight, p2a)); michael@0: michael@0: nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); michael@0: if (pm) { michael@0: nsIPresShell* presShell = mViewManager->GetPresShell(); michael@0: if (presShell && presShell->GetDocument()) { michael@0: pm->AdjustPopupsOnWindowChange(presShell); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: else if (IsPopupWidget(aWidget)) { michael@0: nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); michael@0: if (pm) { michael@0: pm->PopupResized(mFrame, nsIntSize(aWidth, aHeight)); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: nsView::RequestWindowClose(nsIWidget* aWidget) michael@0: { michael@0: if (mFrame && IsPopupWidget(aWidget) && michael@0: mFrame->GetType() == nsGkAtoms::menuPopupFrame) { michael@0: nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); michael@0: if (pm) { michael@0: pm->HidePopup(mFrame->GetContent(), false, true, false, false); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: nsView::WillPaintWindow(nsIWidget* aWidget) michael@0: { michael@0: nsRefPtr vm = mViewManager; michael@0: vm->WillPaintWindow(aWidget); michael@0: } michael@0: michael@0: bool michael@0: nsView::PaintWindow(nsIWidget* aWidget, nsIntRegion aRegion) michael@0: { michael@0: NS_ASSERTION(this == nsView::GetViewFor(aWidget), "wrong view for widget?"); michael@0: michael@0: nsRefPtr vm = mViewManager; michael@0: bool result = vm->PaintWindow(aWidget, aRegion); michael@0: return result; michael@0: } michael@0: michael@0: void michael@0: nsView::DidPaintWindow() michael@0: { michael@0: nsRefPtr vm = mViewManager; michael@0: vm->DidPaintWindow(); michael@0: } michael@0: michael@0: void michael@0: nsView::DidCompositeWindow() michael@0: { michael@0: nsIPresShell* presShell = mViewManager->GetPresShell(); michael@0: if (presShell) { michael@0: nsAutoScriptBlocker scriptBlocker; michael@0: presShell->GetPresContext()->GetDisplayRootPresContext()->GetRootPresContext()->NotifyDidPaintForSubtree(nsIPresShell::PAINT_COMPOSITE); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsView::RequestRepaint() michael@0: { michael@0: nsIPresShell* presShell = mViewManager->GetPresShell(); michael@0: if (presShell) { michael@0: presShell->ScheduleViewManagerFlush(); michael@0: } michael@0: } michael@0: michael@0: nsEventStatus michael@0: nsView::HandleEvent(WidgetGUIEvent* aEvent, michael@0: bool aUseAttachedEvents) michael@0: { michael@0: NS_PRECONDITION(nullptr != aEvent->widget, "null widget ptr"); michael@0: michael@0: nsEventStatus result = nsEventStatus_eIgnore; michael@0: nsView* view; michael@0: if (aUseAttachedEvents) { michael@0: nsIWidgetListener* listener = aEvent->widget->GetAttachedWidgetListener(); michael@0: view = listener ? listener->GetView() : nullptr; michael@0: } michael@0: else { michael@0: view = GetViewFor(aEvent->widget); michael@0: } michael@0: michael@0: if (view) { michael@0: nsRefPtr vm = view->GetViewManager(); michael@0: vm->DispatchEvent(aEvent, view, &result); michael@0: } michael@0: michael@0: return result; michael@0: }