DragEvent.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- Mode: C++; tab-width: 8; 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/. */
  5. #include "mozilla/dom/DragEvent.h"
  6. #include "mozilla/MouseEvents.h"
  7. #include "nsContentUtils.h"
  8. #include "prtime.h"
  9. namespace mozilla {
  10. namespace dom {
  11. DragEvent::DragEvent(EventTarget* aOwner,
  12. nsPresContext* aPresContext,
  13. WidgetDragEvent* aEvent)
  14. : MouseEvent(aOwner, aPresContext,
  15. aEvent ? aEvent :
  16. new WidgetDragEvent(false, eVoidEvent, nullptr))
  17. {
  18. if (aEvent) {
  19. mEventIsInternal = false;
  20. }
  21. else {
  22. mEventIsInternal = true;
  23. mEvent->mTime = PR_Now();
  24. mEvent->mRefPoint = LayoutDeviceIntPoint(0, 0);
  25. mEvent->AsMouseEvent()->inputSource = nsIDOMMouseEvent::MOZ_SOURCE_UNKNOWN;
  26. }
  27. }
  28. NS_IMPL_ADDREF_INHERITED(DragEvent, MouseEvent)
  29. NS_IMPL_RELEASE_INHERITED(DragEvent, MouseEvent)
  30. NS_INTERFACE_MAP_BEGIN(DragEvent)
  31. NS_INTERFACE_MAP_ENTRY(nsIDOMDragEvent)
  32. NS_INTERFACE_MAP_END_INHERITING(MouseEvent)
  33. void
  34. DragEvent::InitDragEvent(const nsAString& aType,
  35. bool aCanBubble,
  36. bool aCancelable,
  37. nsGlobalWindow* aView,
  38. int32_t aDetail,
  39. int32_t aScreenX,
  40. int32_t aScreenY,
  41. int32_t aClientX,
  42. int32_t aClientY,
  43. bool aCtrlKey,
  44. bool aAltKey,
  45. bool aShiftKey,
  46. bool aMetaKey,
  47. uint16_t aButton,
  48. EventTarget* aRelatedTarget,
  49. DataTransfer* aDataTransfer)
  50. {
  51. NS_ENSURE_TRUE_VOID(!mEvent->mFlags.mIsBeingDispatched);
  52. MouseEvent::InitMouseEvent(aType, aCanBubble, aCancelable,
  53. aView, aDetail, aScreenX, aScreenY,
  54. aClientX, aClientY, aCtrlKey, aAltKey,
  55. aShiftKey, aMetaKey, aButton, aRelatedTarget);
  56. if (mEventIsInternal) {
  57. mEvent->AsDragEvent()->mDataTransfer = aDataTransfer;
  58. }
  59. }
  60. NS_IMETHODIMP
  61. DragEvent::GetDataTransfer(nsIDOMDataTransfer** aDataTransfer)
  62. {
  63. NS_IF_ADDREF(*aDataTransfer = GetDataTransfer());
  64. return NS_OK;
  65. }
  66. DataTransfer*
  67. DragEvent::GetDataTransfer()
  68. {
  69. // the dataTransfer field of the event caches the DataTransfer associated
  70. // with the drag. It is initialized when an attempt is made to retrieve it
  71. // rather that when the event is created to avoid duplicating the data when
  72. // no listener ever uses it.
  73. if (!mEvent || mEvent->mClass != eDragEventClass) {
  74. NS_WARNING("Tried to get dataTransfer from non-drag event!");
  75. return nullptr;
  76. }
  77. WidgetDragEvent* dragEvent = mEvent->AsDragEvent();
  78. // for synthetic events, just use the supplied data transfer object even if null
  79. if (!mEventIsInternal) {
  80. nsresult rv = nsContentUtils::SetDataTransferInEvent(dragEvent);
  81. NS_ENSURE_SUCCESS(rv, nullptr);
  82. }
  83. return dragEvent->mDataTransfer;
  84. }
  85. // static
  86. already_AddRefed<DragEvent>
  87. DragEvent::Constructor(const GlobalObject& aGlobal,
  88. const nsAString& aType,
  89. const DragEventInit& aParam,
  90. ErrorResult& aRv)
  91. {
  92. nsCOMPtr<EventTarget> t = do_QueryInterface(aGlobal.GetAsSupports());
  93. RefPtr<DragEvent> e = new DragEvent(t, nullptr, nullptr);
  94. bool trusted = e->Init(t);
  95. e->InitDragEvent(aType, aParam.mBubbles, aParam.mCancelable,
  96. aParam.mView, aParam.mDetail, aParam.mScreenX,
  97. aParam.mScreenY, aParam.mClientX, aParam.mClientY,
  98. aParam.mCtrlKey, aParam.mAltKey, aParam.mShiftKey,
  99. aParam.mMetaKey, aParam.mButton, aParam.mRelatedTarget,
  100. aParam.mDataTransfer);
  101. e->InitializeExtraMouseEventDictionaryMembers(aParam);
  102. e->SetTrusted(trusted);
  103. e->SetComposed(aParam.mComposed);
  104. return e.forget();
  105. }
  106. } // namespace dom
  107. } // namespace mozilla
  108. using namespace mozilla;
  109. using namespace mozilla::dom;
  110. already_AddRefed<DragEvent>
  111. NS_NewDOMDragEvent(EventTarget* aOwner,
  112. nsPresContext* aPresContext,
  113. WidgetDragEvent* aEvent)
  114. {
  115. RefPtr<DragEvent> event =
  116. new DragEvent(aOwner, aPresContext, aEvent);
  117. return event.forget();
  118. }