dnd.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dnd.h
  3. // Purpose: Drag and drop classes declarations
  4. // Author: Vadim Zeitlin, Robert Roebling
  5. // Modified by:
  6. // Created: 26.05.99
  7. // Copyright: (c) wxWidgets Team
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DND_H_BASE_
  11. #define _WX_DND_H_BASE_
  12. #include "wx/defs.h"
  13. #if wxUSE_DRAG_AND_DROP
  14. #include "wx/dataobj.h"
  15. #include "wx/cursor.h"
  16. // ----------------------------------------------------------------------------
  17. // constants
  18. // ----------------------------------------------------------------------------
  19. // flags for wxDropSource::DoDragDrop()
  20. //
  21. // NB: wxDrag_CopyOnly must be 0 (== FALSE) and wxDrag_AllowMove must be 1
  22. // (== TRUE) for compatibility with the old DoDragDrop(bool) method!
  23. enum
  24. {
  25. wxDrag_CopyOnly = 0, // allow only copying
  26. wxDrag_AllowMove = 1, // allow moving (copying is always allowed)
  27. wxDrag_DefaultMove = 3 // the default operation is move, not copy
  28. };
  29. // result of wxDropSource::DoDragDrop() call
  30. enum wxDragResult
  31. {
  32. wxDragError, // error prevented the d&d operation from completing
  33. wxDragNone, // drag target didn't accept the data
  34. wxDragCopy, // the data was successfully copied
  35. wxDragMove, // the data was successfully moved (MSW only)
  36. wxDragLink, // operation is a drag-link
  37. wxDragCancel // the operation was cancelled by user (not an error)
  38. };
  39. // return true if res indicates that something was done during a dnd operation,
  40. // i.e. is neither error nor none nor cancel
  41. WXDLLIMPEXP_CORE bool wxIsDragResultOk(wxDragResult res);
  42. // ----------------------------------------------------------------------------
  43. // wxDropSource is the object you need to create (and call DoDragDrop on it)
  44. // to initiate a drag-and-drop operation
  45. // ----------------------------------------------------------------------------
  46. class WXDLLIMPEXP_CORE wxDropSourceBase
  47. {
  48. public:
  49. wxDropSourceBase(const wxCursor &cursorCopy = wxNullCursor,
  50. const wxCursor &cursorMove = wxNullCursor,
  51. const wxCursor &cursorStop = wxNullCursor)
  52. : m_cursorCopy(cursorCopy),
  53. m_cursorMove(cursorMove),
  54. m_cursorStop(cursorStop)
  55. { m_data = NULL; }
  56. virtual ~wxDropSourceBase() { }
  57. // set the data which is transferred by drag and drop
  58. void SetData(wxDataObject& data)
  59. { m_data = &data; }
  60. wxDataObject *GetDataObject()
  61. { return m_data; }
  62. // set the icon corresponding to given drag result
  63. void SetCursor(wxDragResult res, const wxCursor& cursor)
  64. {
  65. if ( res == wxDragCopy )
  66. m_cursorCopy = cursor;
  67. else if ( res == wxDragMove )
  68. m_cursorMove = cursor;
  69. else
  70. m_cursorStop = cursor;
  71. }
  72. // start drag action, see enum wxDragResult for return value description
  73. //
  74. // if flags contains wxDrag_AllowMove, moving (and only copying) data is
  75. // allowed, if it contains wxDrag_DefaultMove (which includes the previous
  76. // flag), it is even the default operation
  77. virtual wxDragResult DoDragDrop(int flags = wxDrag_CopyOnly) = 0;
  78. // override to give feedback depending on the current operation result
  79. // "effect" and return true if you did something, false to let the library
  80. // give the default feedback
  81. virtual bool GiveFeedback(wxDragResult WXUNUSED(effect)) { return false; }
  82. protected:
  83. const wxCursor& GetCursor(wxDragResult res) const
  84. {
  85. if ( res == wxDragCopy )
  86. return m_cursorCopy;
  87. else if ( res == wxDragMove )
  88. return m_cursorMove;
  89. else
  90. return m_cursorStop;
  91. }
  92. // the data we're dragging
  93. wxDataObject *m_data;
  94. // the cursors to use for feedback
  95. wxCursor m_cursorCopy,
  96. m_cursorMove,
  97. m_cursorStop;
  98. wxDECLARE_NO_COPY_CLASS(wxDropSourceBase);
  99. };
  100. // ----------------------------------------------------------------------------
  101. // wxDropTarget should be associated with a window if it wants to be able to
  102. // receive data via drag and drop.
  103. //
  104. // To use this class, you should derive from wxDropTarget and implement
  105. // OnData() pure virtual method. You may also wish to override OnDrop() if you
  106. // want to accept the data only inside some region of the window (this may
  107. // avoid having to copy the data to this application which happens only when
  108. // OnData() is called)
  109. // ----------------------------------------------------------------------------
  110. class WXDLLIMPEXP_CORE wxDropTargetBase
  111. {
  112. public:
  113. // ctor takes a pointer to heap-allocated wxDataObject which will be owned
  114. // by wxDropTarget and deleted by it automatically. If you don't give it
  115. // here, you can use SetDataObject() later.
  116. wxDropTargetBase(wxDataObject *dataObject = NULL)
  117. { m_dataObject = dataObject; m_defaultAction = wxDragNone; }
  118. // dtor deletes our data object
  119. virtual ~wxDropTargetBase()
  120. { delete m_dataObject; }
  121. // get/set the associated wxDataObject
  122. wxDataObject *GetDataObject() const
  123. { return m_dataObject; }
  124. void SetDataObject(wxDataObject *dataObject)
  125. { if (m_dataObject) delete m_dataObject;
  126. m_dataObject = dataObject; }
  127. // these functions are called when data is moved over position (x, y) and
  128. // may return either wxDragCopy, wxDragMove or wxDragNone depending on
  129. // what would happen if the data were dropped here.
  130. //
  131. // the last parameter is what would happen by default and is determined by
  132. // the platform-specific logic (for example, under Windows it's wxDragCopy
  133. // if Ctrl key is pressed and wxDragMove otherwise) except that it will
  134. // always be wxDragNone if the carried data is in an unsupported format.
  135. // called when the mouse enters the window (only once until OnLeave())
  136. virtual wxDragResult OnEnter(wxCoord x, wxCoord y, wxDragResult def)
  137. { return OnDragOver(x, y, def); }
  138. // called when the mouse moves in the window - shouldn't take long to
  139. // execute or otherwise mouse movement would be too slow
  140. virtual wxDragResult OnDragOver(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
  141. wxDragResult def)
  142. { return def; }
  143. // called when mouse leaves the window: might be used to remove the
  144. // feedback which was given in OnEnter()
  145. virtual void OnLeave() { }
  146. // this function is called when data is dropped at position (x, y) - if it
  147. // returns true, OnData() will be called immediately afterwards which will
  148. // allow to retrieve the data dropped.
  149. virtual bool OnDrop(wxCoord x, wxCoord y) = 0;
  150. // called after OnDrop() returns TRUE: you will usually just call
  151. // GetData() from here and, probably, also refresh something to update the
  152. // new data and, finally, return the code indicating how did the operation
  153. // complete (returning default value in case of success and wxDragError on
  154. // failure is usually ok)
  155. virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) = 0;
  156. // may be called *only* from inside OnData() and will fill m_dataObject
  157. // with the data from the drop source if it returns true
  158. virtual bool GetData() = 0;
  159. // sets the default action for drag and drop:
  160. // use wxDragMove or wxDragCopy to set deafult action to move or copy
  161. // and use wxDragNone (default) to set default action specified by
  162. // initialization of draging (see wxDropSourceBase::DoDragDrop())
  163. void SetDefaultAction(wxDragResult action)
  164. { m_defaultAction = action; }
  165. // returns default action for drag and drop or
  166. // wxDragNone if this not specified
  167. wxDragResult GetDefaultAction()
  168. { return m_defaultAction; }
  169. protected:
  170. wxDataObject *m_dataObject;
  171. wxDragResult m_defaultAction;
  172. wxDECLARE_NO_COPY_CLASS(wxDropTargetBase);
  173. };
  174. // ----------------------------------------------------------------------------
  175. // include platform dependent class declarations
  176. // ----------------------------------------------------------------------------
  177. #if defined(__WXMSW__)
  178. #include "wx/msw/ole/dropsrc.h"
  179. #include "wx/msw/ole/droptgt.h"
  180. #elif defined(__WXMOTIF__)
  181. #include "wx/motif/dnd.h"
  182. #elif defined(__WXX11__)
  183. #include "wx/x11/dnd.h"
  184. #elif defined(__WXGTK20__)
  185. #include "wx/gtk/dnd.h"
  186. #elif defined(__WXGTK__)
  187. #include "wx/gtk1/dnd.h"
  188. #elif defined(__WXMAC__)
  189. #include "wx/osx/dnd.h"
  190. #elif defined(__WXPM__)
  191. #include "wx/os2/dnd.h"
  192. #endif
  193. // ----------------------------------------------------------------------------
  194. // standard wxDropTarget implementations (implemented in common/dobjcmn.cpp)
  195. // ----------------------------------------------------------------------------
  196. // A simple wxDropTarget derived class for text data: you only need to
  197. // override OnDropText() to get something working
  198. class WXDLLIMPEXP_CORE wxTextDropTarget : public wxDropTarget
  199. {
  200. public:
  201. wxTextDropTarget();
  202. virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text) = 0;
  203. virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def);
  204. private:
  205. wxDECLARE_NO_COPY_CLASS(wxTextDropTarget);
  206. };
  207. // A drop target which accepts files (dragged from File Manager or Explorer)
  208. class WXDLLIMPEXP_CORE wxFileDropTarget : public wxDropTarget
  209. {
  210. public:
  211. wxFileDropTarget();
  212. // parameters are the number of files and the array of file names
  213. virtual bool OnDropFiles(wxCoord x, wxCoord y,
  214. const wxArrayString& filenames) = 0;
  215. virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def);
  216. private:
  217. wxDECLARE_NO_COPY_CLASS(wxFileDropTarget);
  218. };
  219. #endif // wxUSE_DRAG_AND_DROP
  220. #endif // _WX_DND_H_BASE_