Files
palemoon27/widget/nsBaseDragService.cpp
T
roytam1 a7ea204c19 import changes from `dev' branch of rmottola/Arctic-Fox:
- fix restore button on MacOS 10.6 fullscreen (08151078bd)
- Bug 1216580 - Make background-position:fixed clip stealing work with display items that create their own layer. r=mattwoodrow (7760c8ddd8)
- Bug 1222661 - Replace ToUntyped(obj) calls with obj.ToUnknown*(). r=nical (76e36824f0)
- Bug 1223310 (part 1) - Pass a LayoutDeviceIntPoint instead of an nsIntPoint to InitEvent(). r=kats. (ea95207afe)
- Bug 1223310 (part 2) - Use LayoutDeviceIntRect for bounds-related functions in nsIWidget. r=kats. (4993cccff8)
- Bug 1218552 - Fix GTK drag-and-drop coordinate scaling on HiDPI displays (r=karlt) (d03cf04136)
- fix preprocessing (2f4c1a1c94)
- Bug 1205923: Make VectorImage::GetWidth/GetHeight set outparam to 0 (not -1) on failure, to accomodate callers that don't check error codes. r=seth (487de31340)
- Bug 1212954 - Make BaseSize::IsEmpty return true for negative sizes to avoid NS_ERROR in VectorImage::GetFrameAtSize when root svg doesn't have intrinsic width or height. r=roc (402b154f56)
- fix ordering (ac8483c966)
- Bug 1216056 - Always sent OpRemoveTextureAsync as pending async Messages r=nica (66c9ee8708)
- Bug 121976 - Add a destruction handshake to PCompositable so as to avoid races between messages and the protocol. r=sotaro (636a669e44)
- Bug 1209724 - Cancel ImageBridge proxy functions if they are called too late. r=sotao (5bf81453ef)
- Bug 1221371 - Switch chromium IPC code to use mozilla::Tuple (r=jld,cpearce,kats) (eebcd419e9)
- Bug 1221371 - Remove unused Chromium callback code (r=jld) (3f2e882485)
- Bug 1221368 - Change MakeTuple to decay the types of its arguments (r=froydnj) (bed635f565)
- Bug 1181303. Set composition time earlier so that computing invalid regions works properly. r=mattwoodrow (7a3e41cd96)
- Bug 1150552 - Fix partial present bug with FPS counter. r=mattwoodrow (6e6ae92909)
- Bug 1216287 - Properly invalidate the debug overlay. r=mattwoodrow (60d4f3451a)
- Compute the compositor's damage region before composites, rather than layers updates. (bug 1217560, r=mattwoodrow) (6efcac085b)
- Bug 1216248 - Recomposite when the window overlay changes. r=mattwoodrow (8a8c5ccd07)
- Bug 1224021 - Support fractional async scroll offsets in the reftest harness. r=tn (353f75d3aa)
- Bug 1121072 - Always notify when layers cleared (r=dvander) (aa6ffc67e2)
- Bug 1224403 (part 1) - Make Configuration::mBounds a LayoutDeviceIntRect. r=kats. (7617b9da7e)
- Bug 1224403 (part 2) - Split GetClientOffset() into typed and untyped versions. r=kats. (06916479b1)
- Bug 1224403 (part 3) - Make mNonClientOffset a LayoutDeviceIntMargin. r=kats. (76efba1fb6)
- Bug 1224403 (part 4) - Make {Get,Set}NonClientMargins() return/take aLayoutDeviceIntMargin. r=kats. (99cfc2fdbc)
- Bug 1224403 (part 5) - Make OnDefaultButtonLoaded() return/take a LayoutDeviceIntMargin. r=kats (c402452f97)
- Bug 1224403 (part 6) - Remove one WidgetToScreenOffsetUntyped() call. r=kats. (8e4485a404)
- Bug 1224403 (part 7) - Use LayoutDeviceIntMargin more in IMMHandler. r=kats. (6b5e9124fc)
- Bug 1224403 (part 8) - Use LayoutDeviceIntMargin more in HyperTextAccessible. r=kats. (5bc1a04cc7)
- Bug 1224403 (part 9) - Remove GetRestoredBoundsUntyped(). r=kats. (20e2b28335)
- Bug 1224403 (part 10) - Make nsScreenGonk::GetNaturalBoundsUntyped() typed. r=kats. (9d19986f22)
- Bug 1224403 (part 11) - Remove a GetBoundsUntyped() call. r=kats. (58ce962157)
- Bug 1214267 - Followup to fix missing propagation of 'fixedPositionSides' to the compositor, and add it to the layers dump. r=mattwoodrow (7dc122eb62)
- Use compositor clip rects, not client clip rects, when computing the compositor's backbuffer damage region. (bug 1189159, r=mattwoodrow) (b70d71b1b7)
- Bug 1210784 - Layer tree invalidation with Preserves3D. r=roc (8a7bfa1a22)
- Bug 1166165 - Check if the timestamp is null to prevent assertion. r=nical (189a8d42d0)
- Bug 1137004 - Changed the location of the counter so when it is used in a maximized window it is no longer cut off, and added a little left padding. r=mattwoodrow (140d294445)
- Bug 1222661 - Replace FromUntyped(obj) calls with FromUnknown*(obj). r=nical (b399ccef2e)
- Bug 1216506. Properly initialize min inset box shadow color to black. r=mstange (335259200c)
- Bug 1223690 - Remove implicit Rect conversions. r=jrmuizel. (5aef25158f)
- Bug 1056356 - Allow calling NewRunnableMethod() with a const pointer to the callee object. r=froydnj (145279b91c)
- Bug 1209649. Part 2: Reftest to test box shadows and border radii. r=mstange (9405ccfe23)
- Bug 1211363. Part 2: reftest with int border radius. r=mstange (9edb37635e)
- Bug 1216200. Inset box shadow reftest with negative spread value. r=mstange (8350478107)
- Bug 1211264. Fallback to render dest rect with outer box shadows on non-int transforms. r=mstange (a244775d24)
- Bug 1216200. Correct for negative spread values with inset box shadows. r=mstange (4851e9c792)
- cleanup (1f78332ad8)
- Bug 1213970 - Enable APZ on iOS r=kats (1d170e32e3)
- Bug 1217939 - part 1 - remove nsContentUtils::GetViewportInfo; r=smaug (8c25236851)
- Bug 1217939 - part 2 - move nsContentUtils::GetSelectionBoundingRect to nsLayoutUtils; r=smaug (6beb37157c)
- Bug 1146713 - Don't export NO_PKG_FILES (fix ssltunnel in b2g); r=glandium (a21152c79c)
- Bug 1222323 - Avoid passing extra defines to the compiler from config.mk. r=gps (2d1076d513)
- Bug 1222321 - Avoid the duplication of OS_CPPFLAGS on the compiler command line. r=gps (cf762f1644)
- Bug 1210687 - Separate out jar.mn parsing in a separate class. r=gps (d54cc5576b)
- Bug 1219147 - Use addEntriesToListFile in mozbuild.jar.JarMaker.updateManifest. r=mshal (3cd74ff478)
- Bug 1208160 - Show information when Xcode requires accepting license agreement; r=gps (352bb43d73)
- Bug 1174524 - Make objdir path comparison case-insensitive on Windows. r=mshal (6f74f10845)
- Bug 1158898 - Cache config.guess output; r=mshal (a7b01184fa)
- Bug 1187245 - Make .mozconfig.mk environment variables available to mach valgrind-test. r=gps (2033642ceb)
2022-12-20 12:06:53 +08:00

747 lines
22 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsBaseDragService.h"
#include "nsITransferable.h"
#include "nsIServiceManager.h"
#include "nsITransferable.h"
#include "nsISupportsArray.h"
#include "nsSize.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsCOMPtr.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIFrame.h"
#include "nsIDocument.h"
#include "nsIContent.h"
#include "nsIPresShell.h"
#include "nsViewManager.h"
#include "nsIDOMNode.h"
#include "nsIDOMDragEvent.h"
#include "nsISelection.h"
#include "nsISelectionPrivate.h"
#include "nsPresContext.h"
#include "nsIDOMDataTransfer.h"
#include "nsIImageLoadingContent.h"
#include "imgIContainer.h"
#include "imgIRequest.h"
#include "ImageRegion.h"
#include "nsRegion.h"
#include "nsXULPopupManager.h"
#include "nsMenuPopupFrame.h"
#include "SVGImageContext.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/Preferences.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/unused.h"
#include "nsFrameLoader.h"
#include "TabParent.h"
#include "gfxContext.h"
#include "gfxPlatform.h"
#include <algorithm>
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::gfx;
using namespace mozilla::image;
#define DRAGIMAGES_PREF "nglayout.enable_drag_images"
nsBaseDragService::nsBaseDragService()
: mCanDrop(false), mOnlyChromeDrop(false), mDoingDrag(false),
mHasImage(false), mUserCancelled(false),
mDragEventDispatchedToChildProcess(false),
mDragAction(DRAGDROP_ACTION_NONE),
mDragActionFromChildProcess(DRAGDROP_ACTION_UNINITIALIZED), mTargetSize(0,0),
mScreenX(-1), mScreenY(-1), mSuppressLevel(0),
mInputSource(nsIDOMMouseEvent::MOZ_SOURCE_MOUSE),
mEndingSession(false)
{
}
nsBaseDragService::~nsBaseDragService()
{
}
NS_IMPL_ISUPPORTS(nsBaseDragService, nsIDragService, nsIDragSession)
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::SetCanDrop(bool aCanDrop)
{
mCanDrop = aCanDrop;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetCanDrop(bool * aCanDrop)
{
*aCanDrop = mCanDrop;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::SetOnlyChromeDrop(bool aOnlyChrome)
{
mOnlyChromeDrop = aOnlyChrome;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetOnlyChromeDrop(bool* aOnlyChrome)
{
*aOnlyChrome = mOnlyChromeDrop;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::SetDragAction(uint32_t anAction)
{
mDragAction = anAction;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetDragAction(uint32_t * anAction)
{
*anAction = mDragAction;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::SetTargetSize(nsSize aDragTargetSize)
{
mTargetSize = aDragTargetSize;
return NS_OK;
}
//---------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetTargetSize(nsSize * aDragTargetSize)
{
*aDragTargetSize = mTargetSize;
return NS_OK;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetNumDropItems(uint32_t * aNumItems)
{
*aNumItems = 0;
return NS_ERROR_FAILURE;
}
//
// GetSourceDocument
//
// Returns the DOM document where the drag was initiated. This will be
// nullptr if the drag began outside of our application.
//
NS_IMETHODIMP
nsBaseDragService::GetSourceDocument(nsIDOMDocument** aSourceDocument)
{
*aSourceDocument = mSourceDocument.get();
NS_IF_ADDREF(*aSourceDocument);
return NS_OK;
}
//
// GetSourceNode
//
// Returns the DOM node where the drag was initiated. This will be
// nullptr if the drag began outside of our application.
//
NS_IMETHODIMP
nsBaseDragService::GetSourceNode(nsIDOMNode** aSourceNode)
{
*aSourceNode = mSourceNode.get();
NS_IF_ADDREF(*aSourceNode);
return NS_OK;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetData(nsITransferable * aTransferable,
uint32_t aItemIndex)
{
return NS_ERROR_FAILURE;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::IsDataFlavorSupported(const char *aDataFlavor,
bool *_retval)
{
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsBaseDragService::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
{
*aDataTransfer = mDataTransfer;
NS_IF_ADDREF(*aDataTransfer);
return NS_OK;
}
NS_IMETHODIMP
nsBaseDragService::SetDataTransfer(nsIDOMDataTransfer* aDataTransfer)
{
mDataTransfer = aDataTransfer;
return NS_OK;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::InvokeDragSession(nsIDOMNode *aDOMNode,
nsISupportsArray* aTransferableArray,
nsIScriptableRegion* aDragRgn,
uint32_t aActionType)
{
NS_ENSURE_TRUE(aDOMNode, NS_ERROR_INVALID_ARG);
NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
// stash the document of the dom node
aDOMNode->GetOwnerDocument(getter_AddRefs(mSourceDocument));
mSourceNode = aDOMNode;
mEndDragPoint = LayoutDeviceIntPoint(0, 0);
// When the mouse goes down, the selection code starts a mouse
// capture. However, this gets in the way of determining drag
// feedback for things like trees because the event coordinates
// are in the wrong coord system, so turn off mouse capture.
nsIPresShell::ClearMouseCapture(nullptr);
nsresult rv = InvokeDragSessionImpl(aTransferableArray,
aDragRgn, aActionType);
if (NS_FAILED(rv)) {
mSourceNode = nullptr;
mSourceDocument = nullptr;
}
return rv;
}
NS_IMETHODIMP
nsBaseDragService::InvokeDragSessionWithImage(nsIDOMNode* aDOMNode,
nsISupportsArray* aTransferableArray,
nsIScriptableRegion* aRegion,
uint32_t aActionType,
nsIDOMNode* aImage,
int32_t aImageX, int32_t aImageY,
nsIDOMDragEvent* aDragEvent,
nsIDOMDataTransfer* aDataTransfer)
{
NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(aDataTransfer, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
mDataTransfer = aDataTransfer;
mSelection = nullptr;
mHasImage = true;
mDragPopup = nullptr;
mImage = aImage;
mImageOffset = CSSIntPoint(aImageX, aImageY);
aDragEvent->GetScreenX(&mScreenX);
aDragEvent->GetScreenY(&mScreenY);
aDragEvent->GetMozInputSource(&mInputSource);
return InvokeDragSession(aDOMNode, aTransferableArray, aRegion, aActionType);
}
NS_IMETHODIMP
nsBaseDragService::InvokeDragSessionWithSelection(nsISelection* aSelection,
nsISupportsArray* aTransferableArray,
uint32_t aActionType,
nsIDOMDragEvent* aDragEvent,
nsIDOMDataTransfer* aDataTransfer)
{
NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(aDragEvent, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(mSuppressLevel == 0, NS_ERROR_FAILURE);
mDataTransfer = aDataTransfer;
mSelection = aSelection;
mHasImage = true;
mDragPopup = nullptr;
mImage = nullptr;
mImageOffset = CSSIntPoint();
aDragEvent->GetScreenX(&mScreenX);
aDragEvent->GetScreenY(&mScreenY);
aDragEvent->GetMozInputSource(&mInputSource);
// just get the focused node from the selection
// XXXndeakin this should actually be the deepest node that contains both
// endpoints of the selection
nsCOMPtr<nsIDOMNode> node;
aSelection->GetFocusNode(getter_AddRefs(node));
return InvokeDragSession(node, aTransferableArray, nullptr, aActionType);
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::GetCurrentSession(nsIDragSession ** aSession)
{
if (!aSession)
return NS_ERROR_INVALID_ARG;
// "this" also implements a drag session, so say we are one but only
// if there is currently a drag going on.
if (!mSuppressLevel && mDoingDrag) {
*aSession = this;
NS_ADDREF(*aSession); // addRef because we're a "getter"
}
else
*aSession = nullptr;
return NS_OK;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::StartDragSession()
{
if (mDoingDrag) {
return NS_ERROR_FAILURE;
}
mDoingDrag = true;
// By default dispatch drop also to content.
mOnlyChromeDrop = false;
return NS_OK;
}
void
nsBaseDragService::OpenDragPopup()
{
if (mDragPopup) {
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) {
pm->ShowPopupAtScreen(mDragPopup, mScreenX - mImageOffset.x,
mScreenY - mImageOffset.y, false, nullptr);
}
}
}
int32_t
nsBaseDragService::TakeChildProcessDragAction()
{
// If the last event was dispatched to the child process, use the drag action
// assigned from it instead and return it. DRAGDROP_ACTION_UNINITIALIZED is
// returned otherwise.
int32_t retval = DRAGDROP_ACTION_UNINITIALIZED;
if (TakeDragEventDispatchedToChildProcess() &&
mDragActionFromChildProcess != DRAGDROP_ACTION_UNINITIALIZED) {
retval = mDragActionFromChildProcess;
}
return retval;
}
//-------------------------------------------------------------------------
NS_IMETHODIMP
nsBaseDragService::EndDragSession(bool aDoneDrag)
{
if (!mDoingDrag || mEndingSession) {
return NS_ERROR_FAILURE;
}
mEndingSession = true;
if (aDoneDrag && !mSuppressLevel)
FireDragEventAtSource(eDragEnd);
if (mDragPopup) {
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) {
pm->HidePopup(mDragPopup, false, true, false, false);
}
}
for (uint32_t i = 0; i < mChildProcesses.Length(); ++i) {
mozilla::Unused << mChildProcesses[i]->SendEndDragSession(aDoneDrag,
mUserCancelled);
}
mChildProcesses.Clear();
mDoingDrag = false;
mCanDrop = false;
mEndingSession = false;
// release the source we've been holding on to.
mSourceDocument = nullptr;
mSourceNode = nullptr;
mSelection = nullptr;
mDataTransfer = nullptr;
mHasImage = false;
mUserCancelled = false;
mDragPopup = nullptr;
mImage = nullptr;
mImageOffset = CSSIntPoint();
mScreenX = -1;
mScreenY = -1;
mEndDragPoint = LayoutDeviceIntPoint(0, 0);
mInputSource = nsIDOMMouseEvent::MOZ_SOURCE_MOUSE;
return NS_OK;
}
NS_IMETHODIMP
nsBaseDragService::FireDragEventAtSource(EventMessage aEventMessage)
{
if (mSourceNode && !mSuppressLevel) {
nsCOMPtr<nsIDocument> doc = do_QueryInterface(mSourceDocument);
if (doc) {
nsCOMPtr<nsIPresShell> presShell = doc->GetShell();
if (presShell) {
nsEventStatus status = nsEventStatus_eIgnore;
WidgetDragEvent event(true, aEventMessage, nullptr);
event.inputSource = mInputSource;
if (aEventMessage == eDragEnd) {
event.refPoint = mEndDragPoint;
event.userCancelled = mUserCancelled;
}
nsCOMPtr<nsIContent> content = do_QueryInterface(mSourceNode);
return presShell->HandleDOMEventWithTarget(content, &event, &status);
}
}
}
return NS_OK;
}
/* This is used by Windows and Mac to update the position of a popup being
* used as a drag image during the drag. This isn't used on GTK as it manages
* the drag popup itself.
*/
NS_IMETHODIMP
nsBaseDragService::DragMoved(int32_t aX, int32_t aY)
{
if (mDragPopup) {
nsIFrame* frame = mDragPopup->GetPrimaryFrame();
if (frame && frame->GetType() == nsGkAtoms::menuPopupFrame) {
CSSIntPoint cssPos = RoundedToInt(LayoutDeviceIntPoint(aX, aY) /
frame->PresContext()->CSSToDevPixelScale()) - mImageOffset;
(static_cast<nsMenuPopupFrame *>(frame))->MoveTo(cssPos, true);
}
}
return NS_OK;
}
static nsIPresShell*
GetPresShellForContent(nsIDOMNode* aDOMNode)
{
nsCOMPtr<nsIContent> content = do_QueryInterface(aDOMNode);
if (!content)
return nullptr;
nsCOMPtr<nsIDocument> document = content->GetCurrentDoc();
if (document) {
document->FlushPendingNotifications(Flush_Display);
return document->GetShell();
}
return nullptr;
}
nsresult
nsBaseDragService::DrawDrag(nsIDOMNode* aDOMNode,
nsIScriptableRegion* aRegion,
int32_t aScreenX, int32_t aScreenY,
nsIntRect* aScreenDragRect,
RefPtr<SourceSurface>* aSurface,
nsPresContext** aPresContext)
{
*aSurface = nullptr;
*aPresContext = nullptr;
// use a default size, in case of an error.
aScreenDragRect->x = aScreenX - mImageOffset.x;
aScreenDragRect->y = aScreenY - mImageOffset.y;
aScreenDragRect->width = 1;
aScreenDragRect->height = 1;
// if a drag image was specified, use that, otherwise, use the source node
nsCOMPtr<nsIDOMNode> dragNode = mImage ? mImage.get() : aDOMNode;
// get the presshell for the node being dragged. If the drag image is not in
// a document or has no frame, get the presshell from the source drag node
nsIPresShell* presShell = GetPresShellForContent(dragNode);
if (!presShell && mImage)
presShell = GetPresShellForContent(aDOMNode);
if (!presShell)
return NS_ERROR_FAILURE;
*aPresContext = presShell->GetPresContext();
nsCOMPtr<nsIFrameLoaderOwner> flo = do_QueryInterface(dragNode);
if (flo) {
RefPtr<nsFrameLoader> fl = flo->GetFrameLoader();
if (fl) {
mozilla::dom::TabParent* tp =
static_cast<mozilla::dom::TabParent*>(fl->GetRemoteBrowser());
if (tp) {
int32_t x, y;
tp->TakeDragVisualization(*aSurface, x, y);
if (*aSurface) {
if (mImage) {
// Just clear the surface if chrome has overridden it with an image.
*aSurface = nullptr;
} else {
nsIFrame* f = fl->GetOwnerContent()->GetPrimaryFrame();
if (f) {
aScreenDragRect->x = x;
aScreenDragRect->y = y;
aScreenDragRect->width = (*aSurface)->GetSize().width;
aScreenDragRect->height = (*aSurface)->GetSize().height;
}
return NS_OK;
}
}
}
}
}
// convert mouse position to dev pixels of the prescontext
int32_t sx = aScreenX, sy = aScreenY;
ConvertToUnscaledDevPixels(*aPresContext, &sx, &sy);
aScreenDragRect->x = sx - mImageOffset.x;
aScreenDragRect->y = sy - mImageOffset.y;
// check if drag images are disabled
bool enableDragImages = Preferences::GetBool(DRAGIMAGES_PREF, true);
// didn't want an image, so just set the screen rectangle to the frame size
if (!enableDragImages || !mHasImage) {
// if a region was specified, set the screen rectangle to the area that
// the region occupies
if (aRegion) {
// the region's coordinates are relative to the root frame
nsIFrame* rootFrame = presShell->GetRootFrame();
if (rootFrame && *aPresContext) {
nsIntRect dragRect;
aRegion->GetBoundingBox(&dragRect.x, &dragRect.y, &dragRect.width, &dragRect.height);
dragRect = ToAppUnits(dragRect, nsPresContext::AppUnitsPerCSSPixel()).
ToOutsidePixels((*aPresContext)->AppUnitsPerDevPixel());
nsIntRect screenRect = rootFrame->GetScreenRectExternal();
aScreenDragRect->SetRect(screenRect.x + dragRect.x, screenRect.y + dragRect.y,
dragRect.width, dragRect.height);
}
}
else {
// otherwise, there was no region so just set the rectangle to
// the size of the primary frame of the content.
nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
nsIFrame* frame = content->GetPrimaryFrame();
if (frame) {
nsIntRect screenRect = frame->GetScreenRectExternal();
aScreenDragRect->SetRect(screenRect.x, screenRect.y,
screenRect.width, screenRect.height);
}
}
return NS_OK;
}
// draw the image for selections
if (mSelection) {
nsIntPoint pnt(aScreenDragRect->x, aScreenDragRect->y);
*aSurface = presShell->RenderSelection(mSelection, pnt, aScreenDragRect,
mImage ? 0 : nsIPresShell::RENDER_AUTO_SCALE);
return NS_OK;
}
// if a custom image was specified, check if it is an image node and draw
// using the source rather than the displayed image. But if mImage isn't
// an image or canvas, fall through to RenderNode below.
if (mImage) {
nsCOMPtr<nsIContent> content = do_QueryInterface(dragNode);
HTMLCanvasElement *canvas = HTMLCanvasElement::FromContentOrNull(content);
if (canvas) {
return DrawDragForImage(*aPresContext, nullptr, canvas, sx, sy,
aScreenDragRect, aSurface);
}
nsCOMPtr<nsIImageLoadingContent> imageLoader = do_QueryInterface(dragNode);
// for image nodes, create the drag image from the actual image data
if (imageLoader) {
return DrawDragForImage(*aPresContext, imageLoader, nullptr, sx, sy,
aScreenDragRect, aSurface);
}
// If the image is a popup, use that as the image. This allows custom drag
// images that can change during the drag, but means that any platform
// default image handling won't occur.
// XXXndeakin this should be chrome-only
nsIFrame* frame = content->GetPrimaryFrame();
if (frame && frame->GetType() == nsGkAtoms::menuPopupFrame) {
mDragPopup = content;
}
}
if (!mDragPopup) {
// otherwise, just draw the node
nsIntRegion clipRegion;
if (aRegion) {
aRegion->GetRegion(&clipRegion);
}
nsIntPoint pnt(aScreenDragRect->x, aScreenDragRect->y);
*aSurface = presShell->RenderNode(dragNode, aRegion ? &clipRegion : nullptr,
pnt, aScreenDragRect,
mImage ? 0 : nsIPresShell::RENDER_AUTO_SCALE);
}
// if an image was specified, reposition the drag rectangle to
// the supplied offset in mImageX and mImageY.
if (mImage) {
aScreenDragRect->x = sx - mImageOffset.x;
aScreenDragRect->y = sy - mImageOffset.y;
}
return NS_OK;
}
nsresult
nsBaseDragService::DrawDragForImage(nsPresContext* aPresContext,
nsIImageLoadingContent* aImageLoader,
HTMLCanvasElement* aCanvas,
int32_t aScreenX, int32_t aScreenY,
nsIntRect* aScreenDragRect,
RefPtr<SourceSurface>* aSurface)
{
nsCOMPtr<imgIContainer> imgContainer;
if (aImageLoader) {
nsCOMPtr<imgIRequest> imgRequest;
nsresult rv = aImageLoader->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
getter_AddRefs(imgRequest));
NS_ENSURE_SUCCESS(rv, rv);
if (!imgRequest)
return NS_ERROR_NOT_AVAILABLE;
rv = imgRequest->GetImage(getter_AddRefs(imgContainer));
NS_ENSURE_SUCCESS(rv, rv);
if (!imgContainer)
return NS_ERROR_NOT_AVAILABLE;
// use the size of the image as the size of the drag image
imgContainer->GetWidth(&aScreenDragRect->width);
imgContainer->GetHeight(&aScreenDragRect->height);
}
else {
NS_ASSERTION(aCanvas, "both image and canvas are null");
nsIntSize sz = aCanvas->GetSize();
aScreenDragRect->width = sz.width;
aScreenDragRect->height = sz.height;
}
nsIntSize srcSize = aScreenDragRect->Size();
nsIntSize destSize = srcSize;
if (destSize.width == 0 || destSize.height == 0)
return NS_ERROR_FAILURE;
nsresult result = NS_OK;
if (aImageLoader) {
RefPtr<DrawTarget> dt =
gfxPlatform::GetPlatform()->
CreateOffscreenContentDrawTarget(destSize,
SurfaceFormat::B8G8R8A8);
if (!dt)
return NS_ERROR_FAILURE;
RefPtr<gfxContext> ctx = new gfxContext(dt);
if (!ctx)
return NS_ERROR_FAILURE;
imgContainer->Draw(ctx, destSize, ImageRegion::Create(destSize),
imgIContainer::FRAME_CURRENT,
Filter::GOOD, Nothing(),
imgIContainer::FLAG_SYNC_DECODE);
*aSurface = dt->Snapshot();
} else {
*aSurface = aCanvas->GetSurfaceSnapshot();
}
return result;
}
void
nsBaseDragService::ConvertToUnscaledDevPixels(nsPresContext* aPresContext,
int32_t* aScreenX, int32_t* aScreenY)
{
int32_t adj = aPresContext->DeviceContext()->AppUnitsPerDevPixelAtUnitFullZoom();
*aScreenX = nsPresContext::CSSPixelsToAppUnits(*aScreenX) / adj;
*aScreenY = nsPresContext::CSSPixelsToAppUnits(*aScreenY) / adj;
}
NS_IMETHODIMP
nsBaseDragService::Suppress()
{
EndDragSession(false);
++mSuppressLevel;
return NS_OK;
}
NS_IMETHODIMP
nsBaseDragService::Unsuppress()
{
--mSuppressLevel;
return NS_OK;
}
NS_IMETHODIMP
nsBaseDragService::UserCancelled()
{
mUserCancelled = true;
return NS_OK;
}
NS_IMETHODIMP
nsBaseDragService::UpdateDragEffect()
{
mDragActionFromChildProcess = mDragAction;
return NS_OK;
}
NS_IMETHODIMP
nsBaseDragService::DragEventDispatchedToChildProcess()
{
mDragEventDispatchedToChildProcess = true;
return NS_OK;
}
bool
nsBaseDragService::MaybeAddChildProcess(mozilla::dom::ContentParent* aChild)
{
if (!mChildProcesses.Contains(aChild)) {
mChildProcesses.AppendElement(aChild);
return true;
}
return false;
}