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