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