dom/events/DragEvent.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "mozilla/dom/DragEvent.h"
michael@0 7 #include "mozilla/MouseEvents.h"
michael@0 8 #include "nsContentUtils.h"
michael@0 9 #include "prtime.h"
michael@0 10
michael@0 11 namespace mozilla {
michael@0 12 namespace dom {
michael@0 13
michael@0 14 DragEvent::DragEvent(EventTarget* aOwner,
michael@0 15 nsPresContext* aPresContext,
michael@0 16 WidgetDragEvent* aEvent)
michael@0 17 : MouseEvent(aOwner, aPresContext,
michael@0 18 aEvent ? aEvent : new WidgetDragEvent(false, 0, nullptr))
michael@0 19 {
michael@0 20 if (aEvent) {
michael@0 21 mEventIsInternal = false;
michael@0 22 }
michael@0 23 else {
michael@0 24 mEventIsInternal = true;
michael@0 25 mEvent->time = PR_Now();
michael@0 26 mEvent->refPoint.x = mEvent->refPoint.y = 0;
michael@0 27 mEvent->AsMouseEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
michael@0 28 }
michael@0 29 }
michael@0 30
michael@0 31 NS_IMPL_ADDREF_INHERITED(DragEvent, MouseEvent)
michael@0 32 NS_IMPL_RELEASE_INHERITED(DragEvent, MouseEvent)
michael@0 33
michael@0 34 NS_INTERFACE_MAP_BEGIN(DragEvent)
michael@0 35 NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent)
michael@0 36 NS_INTERFACE_MAP_END_INHERITING(MouseEvent)
michael@0 37
michael@0 38 void
michael@0 39 DragEvent::InitDragEvent(const nsAString& aType,
michael@0 40 bool aCanBubble,
michael@0 41 bool aCancelable,
michael@0 42 nsIDOMWindow* aView,
michael@0 43 int32_t aDetail,
michael@0 44 int32_t aScreenX,
michael@0 45 int32_t aScreenY,
michael@0 46 int32_t aClientX,
michael@0 47 int32_t aClientY,
michael@0 48 bool aCtrlKey,
michael@0 49 bool aAltKey,
michael@0 50 bool aShiftKey,
michael@0 51 bool aMetaKey,
michael@0 52 uint16_t aButton,
michael@0 53 EventTarget* aRelatedTarget,
michael@0 54 DataTransfer* aDataTransfer,
michael@0 55 ErrorResult& aError)
michael@0 56 {
michael@0 57 aError =
michael@0 58 MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
michael@0 59 aView, aDetail, aScreenX, aScreenY,
michael@0 60 aClientX, aClientY, aCtrlKey, aAltKey,
michael@0 61 aShiftKey, aMetaKey, aButton, aRelatedTarget);
michael@0 62 if (aError.Failed()) {
michael@0 63 return;
michael@0 64 }
michael@0 65
michael@0 66 if (mEventIsInternal && mEvent) {
michael@0 67 mEvent->AsDragEvent()->dataTransfer = aDataTransfer;
michael@0 68 }
michael@0 69 }
michael@0 70
michael@0 71 NS_IMETHODIMP
michael@0 72 DragEvent::InitDragEvent(const nsAString& aType,
michael@0 73 bool aCanBubble,
michael@0 74 bool aCancelable,
michael@0 75 nsIDOMWindow* aView,
michael@0 76 int32_t aDetail,
michael@0 77 int32_t aScreenX,
michael@0 78 int32_t aScreenY,
michael@0 79 int32_t aClientX,
michael@0 80 int32_t aClientY,
michael@0 81 bool aCtrlKey,
michael@0 82 bool aAltKey,
michael@0 83 bool aShiftKey,
michael@0 84 bool aMetaKey,
michael@0 85 uint16_t aButton,
michael@0 86 nsIDOMEventTarget* aRelatedTarget,
michael@0 87 nsIDOMDataTransfer* aDataTransfer)
michael@0 88 {
michael@0 89 nsCOMPtr<DataTransfer> dataTransfer = do_QueryInterface(aDataTransfer);
michael@0 90
michael@0 91 nsresult rv =
michael@0 92 MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable, aView, aDetail,
michael@0 93 aScreenX, aScreenY, aClientX, aClientY,
michael@0 94 aCtrlKey, aAltKey, aShiftKey, aMetaKey, aButton,
michael@0 95 aRelatedTarget);
michael@0 96 NS_ENSURE_SUCCESS(rv, rv);
michael@0 97
michael@0 98 if (mEventIsInternal && mEvent) {
michael@0 99 mEvent->AsDragEvent()->dataTransfer = dataTransfer;
michael@0 100 }
michael@0 101
michael@0 102 return NS_OK;
michael@0 103 }
michael@0 104
michael@0 105 NS_IMETHODIMP
michael@0 106 DragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
michael@0 107 {
michael@0 108 NS_IF_ADDREF(*aDataTransfer = GetDataTransfer());
michael@0 109 return NS_OK;
michael@0 110 }
michael@0 111
michael@0 112 DataTransfer*
michael@0 113 DragEvent::GetDataTransfer()
michael@0 114 {
michael@0 115 // the dataTransfer field of the event caches the DataTransfer associated
michael@0 116 // with the drag. It is initialized when an attempt is made to retrieve it
michael@0 117 // rather that when the event is created to avoid duplicating the data when
michael@0 118 // no listener ever uses it.
michael@0 119 if (!mEvent || mEvent->eventStructType != NS_DRAG_EVENT) {
michael@0 120 NS_WARNING("Tried to get dataTransfer from non-drag event!");
michael@0 121 return nullptr;
michael@0 122 }
michael@0 123
michael@0 124 WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
michael@0 125 // for synthetic events, just use the supplied data transfer object even if null
michael@0 126 if (!mEventIsInternal) {
michael@0 127 nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
michael@0 128 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 129 }
michael@0 130
michael@0 131 return dragEvent->dataTransfer;
michael@0 132 }
michael@0 133
michael@0 134 } // namespace dom
michael@0 135 } // namespace mozilla
michael@0 136
michael@0 137 using namespace mozilla;
michael@0 138 using namespace mozilla::dom;
michael@0 139
michael@0 140 nsresult
michael@0 141 NS_NewDOMDragEvent(nsIDOMEvent** aInstancePtrResult,
michael@0 142 EventTarget* aOwner,
michael@0 143 nsPresContext* aPresContext,
michael@0 144 WidgetDragEvent* aEvent)
michael@0 145 {
michael@0 146 DragEvent* event = new DragEvent(aOwner, aPresContext, aEvent);
michael@0 147 return CallQueryInterface(event, aInstancePtrResult);
michael@0 148 }

mercurial