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