dnd.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dnd.h
  3. // Purpose: interface of wxDropSource and wx*DropTarget
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. Possible flags for drag and drop operations.
  9. */
  10. enum
  11. {
  12. wxDrag_CopyOnly = 0, ///< Allow only copying.
  13. wxDrag_AllowMove = 1, ///< Allow moving too (copying is always allowed).
  14. wxDrag_DefaultMove = 3 ///< Allow moving and make it default operation.
  15. };
  16. /**
  17. Result returned from a wxDropSource::DoDragDrop() call.
  18. */
  19. enum wxDragResult
  20. {
  21. wxDragError, ///< Error prevented the D&D operation from completing.
  22. wxDragNone, ///< Drag target didn't accept the data.
  23. wxDragCopy, ///< The data was successfully copied.
  24. wxDragMove, ///< The data was successfully moved (MSW only).
  25. wxDragLink, ///< Operation is a drag-link.
  26. wxDragCancel ///< The operation was cancelled by user (not an error).
  27. };
  28. /**
  29. @class wxDropTarget
  30. This class represents a target for a drag and drop operation. A
  31. wxDataObject can be associated with it and by default, this object will be
  32. filled with the data from the drag source, if the data formats supported by
  33. the data object match the drag source data format.
  34. There are various virtual handler functions defined in this class which may
  35. be overridden to give visual feedback or react in a more fine-tuned way,
  36. e.g. by not accepting data on the whole window area, but only a small
  37. portion of it. The normal sequence of calls is OnEnter(), OnDragOver()
  38. possibly many times, OnDrop() and finally OnData().
  39. @library{wxcore}
  40. @category{dnd}
  41. @see @ref overview_dnd, @ref overview_dataobject, wxDropSource,
  42. wxTextDropTarget, wxFileDropTarget, wxDataFormat, wxDataObject
  43. */
  44. class wxDropTarget
  45. {
  46. public:
  47. /**
  48. Constructor. @a data is the data to be associated with the drop target.
  49. */
  50. wxDropTarget(wxDataObject* data = NULL);
  51. /**
  52. Destructor. Deletes the associated data object, if any.
  53. */
  54. virtual ~wxDropTarget();
  55. /**
  56. This method may only be called from within OnData(). By default, this
  57. method copies the data from the drop source to the wxDataObject
  58. associated with this drop target, calling its wxDataObject::SetData()
  59. method.
  60. */
  61. virtual bool GetData();
  62. /**
  63. Called after OnDrop() returns @true. By default this will usually
  64. GetData() and will return the suggested default value @a defResult.
  65. */
  66. virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult defResult) = 0;
  67. /**
  68. Called when the mouse is being dragged over the drop target. By
  69. default, this calls functions return the suggested return value @a defResult.
  70. @param x
  71. The x coordinate of the mouse.
  72. @param y
  73. The y coordinate of the mouse.
  74. @param defResult
  75. Suggested value for return value. Determined by SHIFT or CONTROL
  76. key states.
  77. @return The desired operation or wxDragNone. This is used for optical
  78. feedback from the side of the drop source, typically in form
  79. of changing the icon.
  80. */
  81. virtual wxDragResult OnDragOver(wxCoord x, wxCoord y, wxDragResult defResult);
  82. /**
  83. Called when the user drops a data object on the target. Return @false
  84. to veto the operation.
  85. @param x
  86. The x coordinate of the mouse.
  87. @param y
  88. The y coordinate of the mouse.
  89. @return @true to accept the data, or @false to veto the operation.
  90. */
  91. virtual bool OnDrop(wxCoord x, wxCoord y);
  92. /**
  93. Called when the mouse enters the drop target. By default, this calls
  94. OnDragOver().
  95. @param x
  96. The x coordinate of the mouse.
  97. @param y
  98. The y coordinate of the mouse.
  99. @param defResult
  100. Suggested default for return value. Determined by SHIFT or CONTROL
  101. key states.
  102. @return The desired operation or wxDragNone. This is used for optical
  103. feedback from the side of the drop source, typically in form
  104. of changing the icon.
  105. */
  106. virtual wxDragResult OnEnter(wxCoord x, wxCoord y, wxDragResult defResult);
  107. /**
  108. Called when the mouse leaves the drop target.
  109. */
  110. virtual void OnLeave();
  111. /**
  112. Returns the data wxDataObject associated with the drop target
  113. */
  114. wxDataObject *GetDataObject() const;
  115. /**
  116. Sets the data wxDataObject associated with the drop target and deletes
  117. any previously associated data object.
  118. */
  119. void SetDataObject(wxDataObject* data);
  120. /**
  121. Sets the default action for drag and drop. Use wxDragMove or
  122. wxDragCopy to set default action to move or copy and use wxDragNone
  123. (default) to set default action specified by initialization of draging
  124. (see wxDropSource::DoDragDrop())
  125. */
  126. void SetDefaultAction(wxDragResult action);
  127. /**
  128. Returns default action for drag and drop or wxDragNone if this not
  129. specified.
  130. */
  131. wxDragResult GetDefaultAction();
  132. };
  133. /**
  134. @class wxDropSource
  135. This class represents a source for a drag and drop operation.
  136. @library{wxcore}
  137. @category{dnd}
  138. @see @ref overview_dnd, @ref overview_dataobject, wxDropTarget,
  139. wxTextDropTarget, wxFileDropTarget
  140. */
  141. class wxDropSource
  142. {
  143. public:
  144. /**
  145. This constructor requires that you must call SetData() later.
  146. Note that the type of @a iconCopy and subsequent parameters
  147. differs between different ports: these are cursors under Windows and OS
  148. X but icons for GTK. You should use the macro wxDROP_ICON() in portable
  149. programs instead of directly using either of these types.
  150. @onlyfor{wxmsw,wxosx}
  151. @param win
  152. The window which initiates the drag and drop operation.
  153. @param iconCopy
  154. The icon or cursor used for feedback for copy operation.
  155. @param iconMove
  156. The icon or cursor used for feedback for move operation.
  157. @param iconNone
  158. The icon or cursor used for feedback when operation can't be done.
  159. */
  160. wxDropSource(wxWindow* win = NULL,
  161. const wxCursor& iconCopy = wxNullCursor,
  162. const wxCursor& iconMove = wxNullCursor,
  163. const wxCursor& iconNone = wxNullCursor);
  164. /**
  165. The constructor taking a wxDataObject.
  166. Note that the type of @a iconCopy and subsequent parameters
  167. differs between different ports: these are cursors under Windows and OS
  168. X but icons for GTK. You should use the macro wxDROP_ICON() in portable
  169. programs instead of directly using either of these types.
  170. @onlyfor{wxmsw,wxosx}
  171. @param data
  172. The data associated with the drop source.
  173. @param win
  174. The window which initiates the drag and drop operation.
  175. @param iconCopy
  176. The icon or cursor used for feedback for copy operation.
  177. @param iconMove
  178. The icon or cursor used for feedback for move operation.
  179. @param iconNone
  180. The icon or cursor used for feedback when operation can't be done.
  181. */
  182. wxDropSource(wxDataObject& data, wxWindow* win = NULL,
  183. const wxCursor& iconCopy = wxNullCursor,
  184. const wxCursor& iconMove = wxNullCursor,
  185. const wxCursor& iconNone = wxNullCursor);
  186. /**
  187. This constructor requires that you must call SetData() later.
  188. This is the wxGTK-specific version of the constructor taking wxIcon
  189. instead of wxCursor as the other ports.
  190. @onlyfor{wxgtk}
  191. @param win
  192. The window which initiates the drag and drop operation.
  193. @param iconCopy
  194. The icon or cursor used for feedback for copy operation.
  195. @param iconMove
  196. The icon or cursor used for feedback for move operation.
  197. @param iconNone
  198. The icon or cursor used for feedback when operation can't be done.
  199. */
  200. wxDropSource(wxWindow* win = NULL,
  201. const wxIcon& iconCopy = wxNullIcon,
  202. const wxIcon& iconMove = wxNullIcon,
  203. const wxIcon& iconNone = wxNullIcon);
  204. /**
  205. The constructor taking a wxDataObject.
  206. This is the wxGTK-specific version of the constructor taking wxIcon
  207. instead of wxCursor as the other ports.
  208. @onlyfor{wxgtk}
  209. @param data
  210. The data associated with the drop source.
  211. @param win
  212. The window which initiates the drag and drop operation.
  213. @param iconCopy
  214. The icon or cursor used for feedback for copy operation.
  215. @param iconMove
  216. The icon or cursor used for feedback for move operation.
  217. @param iconNone
  218. The icon or cursor used for feedback when operation can't be done.
  219. */
  220. wxDropSource(wxDataObject& data, wxWindow* win = NULL,
  221. const wxIcon& iconCopy = wxNullIcon,
  222. const wxIcon& iconMove = wxNullIcon,
  223. const wxIcon& iconNone = wxNullIcon);
  224. /**
  225. Starts the drag-and-drop operation which will terminate when the user
  226. releases the mouse. Call this in response to a mouse button press, for
  227. example.
  228. @param flags
  229. If ::wxDrag_AllowMove is included in the flags, data may be moved
  230. and not only copied as is the case for the default
  231. ::wxDrag_CopyOnly. If ::wxDrag_DefaultMove is specified
  232. (which includes the previous flag), moving is not only possible but
  233. becomes the default operation.
  234. @return The operation requested by the user, may be ::wxDragCopy,
  235. ::wxDragMove, ::wxDragLink, ::wxDragCancel or ::wxDragNone if
  236. an error occurred.
  237. */
  238. virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly);
  239. /**
  240. Returns the wxDataObject object that has been assigned previously.
  241. */
  242. wxDataObject* GetDataObject();
  243. /**
  244. You may give some custom UI feedback during the drag and drop operation
  245. by overriding this function. It is called on each mouse move, so your
  246. implementation must not be too slow.
  247. @param effect
  248. The effect to implement. One of ::wxDragCopy, ::wxDragMove,
  249. ::wxDragLink and ::wxDragNone.
  250. @return @false if you want default feedback, or @true if you implement
  251. your own feedback. The return value is ignored under GTK.
  252. */
  253. virtual bool GiveFeedback(wxDragResult effect);
  254. /**
  255. Set the icon to use for a certain drag result.
  256. @param res
  257. The drag result to set the icon for.
  258. @param cursor
  259. The icon to show when this drag result occurs.
  260. @onlyfor{wxmsw,wxosx}
  261. */
  262. void SetCursor(wxDragResult res, const wxCursor& cursor);
  263. /**
  264. Set the icon to use for a certain drag result.
  265. @param res
  266. The drag result to set the icon for.
  267. @param icon
  268. The icon to show when this drag result occurs.
  269. @onlyfor{wxgtk}
  270. */
  271. void SetIcon(wxDragResult res, const wxIcon& icon);
  272. /**
  273. Sets the data wxDataObject associated with the drop source. This will
  274. not delete any previously associated data.
  275. */
  276. void SetData(wxDataObject& data);
  277. };
  278. /**
  279. @class wxTextDropTarget
  280. A predefined drop target for dealing with text data.
  281. @library{wxcore}
  282. @category{dnd}
  283. @see @ref overview_dnd, wxDropSource, wxDropTarget, wxFileDropTarget
  284. */
  285. class wxTextDropTarget : public wxDropTarget
  286. {
  287. public:
  288. /**
  289. Constructor.
  290. */
  291. wxTextDropTarget();
  292. /**
  293. See wxDropTarget::OnDrop(). This function is implemented appropriately
  294. for text, and calls OnDropText().
  295. */
  296. virtual bool OnDrop(wxCoord x, wxCoord y);
  297. /**
  298. Override this function to receive dropped text.
  299. @param x
  300. The x coordinate of the mouse.
  301. @param y
  302. The y coordinate of the mouse.
  303. @param data
  304. The data being dropped: a wxString.
  305. Return @true to accept the data, or @false to veto the operation.
  306. */
  307. virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& data) = 0;
  308. };
  309. /**
  310. @class wxFileDropTarget
  311. This is a drop target which accepts files (dragged from File Manager or
  312. Explorer).
  313. @library{wxcore}
  314. @category{dnd}
  315. @see @ref overview_dnd, wxDropSource, wxDropTarget, wxTextDropTarget
  316. */
  317. class wxFileDropTarget : public wxDropTarget
  318. {
  319. public:
  320. /**
  321. Constructor.
  322. */
  323. wxFileDropTarget();
  324. /**
  325. See wxDropTarget::OnDrop(). This function is implemented appropriately
  326. for files, and calls OnDropFiles().
  327. */
  328. virtual bool OnDrop(wxCoord x, wxCoord y);
  329. /**
  330. Override this function to receive dropped files.
  331. @param x
  332. The x coordinate of the mouse.
  333. @param y
  334. The y coordinate of the mouse.
  335. @param filenames
  336. An array of filenames.
  337. Return @true to accept the data, or @false to veto the operation.
  338. */
  339. virtual bool OnDropFiles(wxCoord x, wxCoord y,
  340. const wxArrayString& filenames) = 0;
  341. };
  342. // ============================================================================
  343. // Global functions/macros
  344. // ============================================================================
  345. /** @addtogroup group_funcmacro_gdi */
  346. //@{
  347. /**
  348. This macro creates either a cursor (MSW) or an icon (elsewhere) with the
  349. given @a name (of type <tt>const char*</tt>). Under MSW, the cursor is
  350. loaded from the resource file and the icon is loaded from XPM file under
  351. other platforms.
  352. This macro should be used with wxDropSource::wxDropSource().
  353. @return wxCursor on MSW, otherwise returns a wxIcon
  354. @header{wx/dnd.h}
  355. */
  356. #define wxDROP_ICON(name)
  357. /**
  358. Returns true if res indicates that something was done during a DnD operation,
  359. i.e. is neither error nor none nor cancel.
  360. */
  361. bool wxIsDragResultOk(wxDragResult res);
  362. //@}