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