2008-08-27 05:07:27 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2008-08-27 05:07:27 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
#include "mozilla/dom/DragEvent.h"
|
|
|
|
#include "mozilla/MouseEvents.h"
|
2008-08-27 05:07:27 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-29 14:18:25 -07:00
|
|
|
#include "prtime.h"
|
2008-08-27 05:07:27 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2013-10-01 00:23:02 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
DragEvent::DragEvent(EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
WidgetDragEvent* aEvent)
|
2014-02-27 02:51:15 -08:00
|
|
|
: MouseEvent(aOwner, aPresContext,
|
|
|
|
aEvent ? aEvent : new WidgetDragEvent(false, 0, nullptr))
|
2008-08-27 05:07:27 -07:00
|
|
|
{
|
|
|
|
if (aEvent) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventIsInternal = false;
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
else {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEventIsInternal = true;
|
2008-08-27 05:07:27 -07:00
|
|
|
mEvent->time = PR_Now();
|
|
|
|
mEvent->refPoint.x = mEvent->refPoint.y = 0;
|
2013-10-22 01:55:20 -07:00
|
|
|
mEvent->AsMouseEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-27 02:51:15 -08:00
|
|
|
NS_IMPL_ADDREF_INHERITED(DragEvent, MouseEvent)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(DragEvent, MouseEvent)
|
2008-08-27 05:07:27 -07:00
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN(DragEvent)
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent)
|
2014-02-27 02:51:15 -08:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(MouseEvent)
|
2008-08-27 05:07:27 -07:00
|
|
|
|
2014-02-26 19:23:31 -08:00
|
|
|
void
|
2014-02-27 02:51:13 -08:00
|
|
|
DragEvent::InitDragEvent(const nsAString& aType,
|
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
|
|
|
nsIDOMWindow* aView,
|
|
|
|
int32_t aDetail,
|
|
|
|
int32_t aScreenX,
|
|
|
|
int32_t aScreenY,
|
|
|
|
int32_t aClientX,
|
|
|
|
int32_t aClientY,
|
|
|
|
bool aCtrlKey,
|
|
|
|
bool aAltKey,
|
|
|
|
bool aShiftKey,
|
|
|
|
bool aMetaKey,
|
|
|
|
uint16_t aButton,
|
|
|
|
EventTarget* aRelatedTarget,
|
|
|
|
DataTransfer* aDataTransfer,
|
|
|
|
ErrorResult& aError)
|
2014-02-26 19:23:31 -08:00
|
|
|
{
|
|
|
|
aError =
|
2014-02-27 02:51:15 -08:00
|
|
|
MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
|
|
|
|
aView, aDetail, aScreenX, aScreenY,
|
|
|
|
aClientX, aClientY, aCtrlKey, aAltKey,
|
|
|
|
aShiftKey, aMetaKey, aButton, aRelatedTarget);
|
2014-02-26 19:23:31 -08:00
|
|
|
if (aError.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEventIsInternal && mEvent) {
|
|
|
|
mEvent->AsDragEvent()->dataTransfer = aDataTransfer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
DragEvent::InitDragEvent(const nsAString& aType,
|
|
|
|
bool aCanBubble,
|
|
|
|
bool aCancelable,
|
|
|
|
nsIDOMWindow* aView,
|
|
|
|
int32_t aDetail,
|
|
|
|
int32_t aScreenX,
|
|
|
|
int32_t aScreenY,
|
|
|
|
int32_t aClientX,
|
|
|
|
int32_t aClientY,
|
|
|
|
bool aCtrlKey,
|
|
|
|
bool aAltKey,
|
|
|
|
bool aShiftKey,
|
|
|
|
bool aMetaKey,
|
|
|
|
uint16_t aButton,
|
|
|
|
nsIDOMEventTarget* aRelatedTarget,
|
|
|
|
nsIDOMDataTransfer* aDataTransfer)
|
2008-08-27 05:07:27 -07:00
|
|
|
{
|
2014-02-26 19:23:31 -08:00
|
|
|
nsCOMPtr<DataTransfer> dataTransfer = do_QueryInterface(aDataTransfer);
|
|
|
|
|
2014-02-27 02:51:15 -08:00
|
|
|
nsresult rv =
|
|
|
|
MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail,
|
|
|
|
aScreenX, aScreenY, aClientX, aClientY,
|
|
|
|
aCtrlKey, aAltKey, aShiftKey, aMetaKey, aButton,
|
|
|
|
aRelatedTarget);
|
2008-08-27 05:07:27 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mEventIsInternal && mEvent) {
|
2014-02-26 19:23:31 -08:00
|
|
|
mEvent->AsDragEvent()->dataTransfer = dataTransfer;
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-02-27 02:51:13 -08:00
|
|
|
DragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
|
2013-04-18 16:13:35 -07:00
|
|
|
{
|
|
|
|
NS_IF_ADDREF(*aDataTransfer = GetDataTransfer());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-26 19:23:31 -08:00
|
|
|
DataTransfer*
|
2014-02-27 02:51:13 -08:00
|
|
|
DragEvent::GetDataTransfer()
|
2008-08-27 05:07:27 -07:00
|
|
|
{
|
|
|
|
// the dataTransfer field of the event caches the DataTransfer associated
|
|
|
|
// with the drag. It is initialized when an attempt is made to retrieve it
|
|
|
|
// rather that when the event is created to avoid duplicating the data when
|
|
|
|
// no listener ever uses it.
|
2014-08-03 22:28:50 -07:00
|
|
|
if (!mEvent || mEvent->mClass != eDragEventClass) {
|
2009-06-24 10:12:33 -07:00
|
|
|
NS_WARNING("Tried to get dataTransfer from non-drag event!");
|
2013-04-18 16:13:35 -07:00
|
|
|
return nullptr;
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
|
2013-10-17 23:10:26 -07:00
|
|
|
WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
|
2009-06-24 10:12:33 -07:00
|
|
|
// for synthetic events, just use the supplied data transfer object even if null
|
|
|
|
if (!mEventIsInternal) {
|
|
|
|
nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
|
2013-04-18 16:13:35 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
|
2013-04-18 16:13:35 -07:00
|
|
|
return dragEvent->dataTransfer;
|
2008-08-27 05:07:27 -07:00
|
|
|
}
|
|
|
|
|
2015-03-04 06:52:03 -08:00
|
|
|
// static
|
|
|
|
already_AddRefed<DragEvent>
|
|
|
|
DragEvent::Constructor(const GlobalObject& aGlobal,
|
|
|
|
const nsAString& aType,
|
|
|
|
const DragEventInit& aParam,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
nsRefPtr<DragEvent> e = new DragEvent(t, nullptr, nullptr);
|
|
|
|
bool trusted = e->Init(t);
|
|
|
|
aRv = e->InitDragEvent(aType, aParam.mBubbles, aParam.mCancelable,
|
|
|
|
aParam.mView, aParam.mDetail, aParam.mScreenX,
|
|
|
|
aParam.mScreenY, aParam.mClientX, aParam.mClientY,
|
|
|
|
aParam.mCtrlKey, aParam.mAltKey, aParam.mShiftKey,
|
|
|
|
aParam.mMetaKey, aParam.mButton, aParam.mRelatedTarget,
|
|
|
|
aParam.mDataTransfer);
|
|
|
|
e->SetTrusted(trusted);
|
|
|
|
return e.forget();
|
|
|
|
}
|
|
|
|
|
2014-02-27 02:51:13 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult,
|
|
|
|
EventTarget* aOwner,
|
|
|
|
nsPresContext* aPresContext,
|
|
|
|
WidgetDragEvent* aEvent)
|
2008-08-27 05:07:27 -07:00
|
|
|
{
|
2014-02-27 02:51:13 -08:00
|
|
|
DragEvent* event = new DragEvent(aOwner, aPresContext, aEvent);
|
2008-08-27 05:07:27 -07:00
|
|
|
return CallQueryInterface(event, aInstancePtrResult);
|
|
|
|
}
|