nsBaseDragService.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* -*- Mode: C++; tab-width: 2; 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. #ifndef nsBaseDragService_h__
  6. #define nsBaseDragService_h__
  7. #include "nsIDragService.h"
  8. #include "nsIDragSession.h"
  9. #include "nsITransferable.h"
  10. #include "nsIDOMDocument.h"
  11. #include "nsIDOMDataTransfer.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsRect.h"
  14. #include "nsPoint.h"
  15. #include "mozilla/RefPtr.h"
  16. #include "mozilla/dom/ContentParent.h"
  17. #include "mozilla/dom/HTMLCanvasElement.h"
  18. #include "nsTArray.h"
  19. #include "Units.h"
  20. // translucency level for drag images
  21. #define DRAG_TRANSLUCENCY 0.65
  22. class nsIContent;
  23. class nsIDOMNode;
  24. class nsPresContext;
  25. class nsIImageLoadingContent;
  26. namespace mozilla {
  27. namespace gfx {
  28. class SourceSurface;
  29. } // namespace gfx
  30. } // namespace mozilla
  31. /**
  32. * XP DragService wrapper base class
  33. */
  34. class nsBaseDragService : public nsIDragService,
  35. public nsIDragSession
  36. {
  37. public:
  38. typedef mozilla::gfx::SourceSurface SourceSurface;
  39. nsBaseDragService();
  40. //nsISupports
  41. NS_DECL_ISUPPORTS
  42. //nsIDragSession and nsIDragService
  43. NS_DECL_NSIDRAGSERVICE
  44. NS_DECL_NSIDRAGSESSION
  45. void SetDragEndPoint(nsIntPoint aEndDragPoint)
  46. {
  47. mEndDragPoint = mozilla::LayoutDeviceIntPoint::FromUnknownPoint(aEndDragPoint);
  48. }
  49. void SetDragEndPoint(mozilla::LayoutDeviceIntPoint aEndDragPoint)
  50. {
  51. mEndDragPoint = aEndDragPoint;
  52. }
  53. uint16_t GetInputSource() { return mInputSource; }
  54. int32_t TakeChildProcessDragAction();
  55. protected:
  56. virtual ~nsBaseDragService();
  57. /**
  58. * Called from nsBaseDragService to initiate a platform drag from a source
  59. * in this process. This is expected to ensure that StartDragSession() and
  60. * EndDragSession() get called if the platform drag is successfully invoked.
  61. */
  62. virtual nsresult InvokeDragSessionImpl(nsIArray* aTransferableArray,
  63. nsIScriptableRegion* aDragRgn,
  64. uint32_t aActionType) = 0;
  65. /**
  66. * Draw the drag image, if any, to a surface and return it. The drag image
  67. * is constructed from mImage if specified, or aDOMNode if mImage is null.
  68. *
  69. * aRegion may be used to draw only a subset of the element. This region
  70. * should be supplied using x and y coordinates measured in css pixels
  71. * that are relative to the upper-left corner of the window.
  72. *
  73. * aScreenPosition should be the screen coordinates of the mouse click
  74. * for the drag. These are in CSS pixels.
  75. *
  76. * On return, aScreenDragRect will contain the screen coordinates of the
  77. * area being dragged. This is used by the platform-specific part of the
  78. * drag service to determine the drag feedback. This rect will be in the
  79. * device pixels of the presContext.
  80. *
  81. * If there is no drag image, the returned surface will be null, but
  82. * aScreenDragRect will still be set to the drag area.
  83. *
  84. * aPresContext will be set to the nsPresContext used determined from
  85. * whichever of mImage or aDOMNode is used.
  86. */
  87. nsresult DrawDrag(nsIDOMNode* aDOMNode,
  88. nsIScriptableRegion* aRegion,
  89. mozilla::CSSIntPoint aScreenPosition,
  90. mozilla::LayoutDeviceIntRect* aScreenDragRect,
  91. RefPtr<SourceSurface>* aSurface,
  92. nsPresContext **aPresContext);
  93. /**
  94. * Draw a drag image for an image node specified by aImageLoader or aCanvas.
  95. * This is called by DrawDrag.
  96. */
  97. nsresult DrawDragForImage(nsPresContext *aPresContext,
  98. nsIImageLoadingContent* aImageLoader,
  99. mozilla::dom::HTMLCanvasElement* aCanvas,
  100. mozilla::LayoutDeviceIntRect* aScreenDragRect,
  101. RefPtr<SourceSurface>* aSurface);
  102. /**
  103. * Convert aScreenPosition from CSS pixels into unscaled device pixels.
  104. */
  105. mozilla::LayoutDeviceIntPoint
  106. ConvertToUnscaledDevPixels(nsPresContext* aPresContext,
  107. mozilla::CSSIntPoint aScreenPosition);
  108. /**
  109. * If the drag image is a popup, open the popup when the drag begins.
  110. */
  111. void OpenDragPopup();
  112. /**
  113. * Free resources contained in DataTransferItems that aren't needed by JS.
  114. */
  115. void DiscardInternalTransferData();
  116. // Returns true if a drag event was dispatched to a child process after
  117. // the previous TakeDragEventDispatchedToChildProcess() call.
  118. bool TakeDragEventDispatchedToChildProcess()
  119. {
  120. bool retval = mDragEventDispatchedToChildProcess;
  121. mDragEventDispatchedToChildProcess = false;
  122. return retval;
  123. }
  124. bool mCanDrop;
  125. bool mOnlyChromeDrop;
  126. bool mDoingDrag;
  127. // true if mImage should be used to set a drag image
  128. bool mHasImage;
  129. // true if the user cancelled the drag operation
  130. bool mUserCancelled;
  131. bool mDragEventDispatchedToChildProcess;
  132. uint32_t mDragAction;
  133. uint32_t mDragActionFromChildProcess;
  134. nsSize mTargetSize;
  135. nsCOMPtr<nsIDOMNode> mSourceNode;
  136. nsCOMPtr<nsIDOMDocument> mSourceDocument; // the document at the drag source. will be null
  137. // if it came from outside the app.
  138. nsContentPolicyType mContentPolicyType; // the contentpolicy type passed to the channel
  139. // when initiating the drag session
  140. nsCOMPtr<nsIDOMDataTransfer> mDataTransfer;
  141. // used to determine the image to appear on the cursor while dragging
  142. nsCOMPtr<nsIDOMNode> mImage;
  143. // offset of cursor within the image
  144. mozilla::CSSIntPoint mImageOffset;
  145. // set if a selection is being dragged
  146. nsCOMPtr<nsISelection> mSelection;
  147. // set if the image in mImage is a popup. If this case, the popup will be opened
  148. // and moved instead of using a drag image.
  149. nsCOMPtr<nsIContent> mDragPopup;
  150. // the screen position where drag gesture occurred, used for positioning the
  151. // drag image.
  152. mozilla::CSSIntPoint mScreenPosition;
  153. // the screen position where the drag ended
  154. mozilla::LayoutDeviceIntPoint mEndDragPoint;
  155. uint32_t mSuppressLevel;
  156. // The input source of the drag event. Possible values are from nsIDOMMouseEvent.
  157. uint16_t mInputSource;
  158. nsTArray<RefPtr<mozilla::dom::ContentParent>> mChildProcesses;
  159. };
  160. #endif // nsBaseDragService_h__