1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop classes declarations
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_DND_H_BASE_
13 #define _WX_DND_H_BASE_
17 #if wxUSE_DRAG_AND_DROP
19 #include "wx/dataobj.h"
20 #include "wx/cursor.h"
22 // ----------------------------------------------------------------------------
24 // ----------------------------------------------------------------------------
26 // flags for wxDropSource::DoDragDrop()
28 // NB: wxDrag_CopyOnly must be 0 (== FALSE) and wxDrag_AllowMove must be 1
29 // (== TRUE) for compatibility with the old DoDragDrop(bool) method!
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
37 // result of wxDropSource::DoDragDrop() call
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)
48 inline WXDLLEXPORT
bool wxIsDragResultOk(wxDragResult res
)
50 return res
== wxDragCopy
|| res
== wxDragMove
|| res
== wxDragLink
;
53 // ----------------------------------------------------------------------------
54 // wxDropSource is the object you need to create (and call DoDragDrop on it)
55 // to initiate a drag-and-drop operation
56 // ----------------------------------------------------------------------------
58 class WXDLLEXPORT wxDropSourceBase
61 wxDropSourceBase(const wxCursor
&cursorCopy
= wxNullCursor
,
62 const wxCursor
&cursorMove
= wxNullCursor
,
63 const wxCursor
&cursorStop
= wxNullCursor
)
64 : m_cursorCopy(cursorCopy
),
65 m_cursorMove(cursorMove
),
66 m_cursorStop(cursorStop
)
67 { m_data
= (wxDataObject
*)NULL
; }
68 virtual ~wxDropSourceBase() { }
70 // set the data which is transfered by drag and drop
71 void SetData(wxDataObject
& data
)
74 wxDataObject
*GetDataObject()
77 // set the icon corresponding to given drag result
78 void SetCursor(wxDragResult res
, const wxCursor
& cursor
)
80 if ( res
== wxDragCopy
)
81 m_cursorCopy
= cursor
;
82 else if ( res
== wxDragMove
)
83 m_cursorMove
= cursor
;
85 m_cursorStop
= cursor
;
88 // start drag action, see enum wxDragResult for return value description
90 // if flags contains wxDrag_AllowMove, moving (and only copying) data is
91 // allowed, if it contains wxDrag_DefaultMove (which includes the previous
92 // flag), it is even the default operation
93 virtual wxDragResult
DoDragDrop(int flags
= wxDrag_CopyOnly
) = 0;
95 // override to give feedback depending on the current operation result
96 // "effect" and return true if you did something, false to let the library
97 // give the default feedback
98 virtual bool GiveFeedback(wxDragResult
WXUNUSED(effect
)) { return false; }
101 const wxCursor
& GetCursor(wxDragResult res
) const
103 if ( res
== wxDragCopy
)
105 else if ( res
== wxDragMove
)
111 // the data we're dragging
112 wxDataObject
*m_data
;
114 // the cursors to use for feedback
115 wxCursor m_cursorCopy
,
119 DECLARE_NO_COPY_CLASS(wxDropSourceBase
)
122 // ----------------------------------------------------------------------------
123 // wxDropTarget should be associated with a window if it wants to be able to
124 // receive data via drag and drop.
126 // To use this class, you should derive from wxDropTarget and implement
127 // OnData() pure virtual method. You may also wish to override OnDrop() if you
128 // want to accept the data only inside some region of the window (this may
129 // avoid having to copy the data to this application which happens only when
130 // OnData() is called)
131 // ----------------------------------------------------------------------------
133 class WXDLLEXPORT wxDropTargetBase
136 // ctor takes a pointer to heap-allocated wxDataObject which will be owned
137 // by wxDropTarget and deleted by it automatically. If you don't give it
138 // here, you can use SetDataObject() later.
139 wxDropTargetBase(wxDataObject
*dataObject
= (wxDataObject
*)NULL
)
140 { m_dataObject
= dataObject
; m_defaultAction
= wxDragNone
; }
141 // dtor deletes our data object
142 virtual ~wxDropTargetBase()
143 { delete m_dataObject
; }
145 // get/set the associated wxDataObject
146 wxDataObject
*GetDataObject() const
147 { return m_dataObject
; }
148 void SetDataObject(wxDataObject
*dataObject
)
149 { if (m_dataObject
) delete m_dataObject
;
150 m_dataObject
= dataObject
; }
152 // these functions are called when data is moved over position (x, y) and
153 // may return either wxDragCopy, wxDragMove or wxDragNone depending on
154 // what would happen if the data were dropped here.
156 // the last parameter is what would happen by default and is determined by
157 // the platform-specific logic (for example, under Windows it's wxDragCopy
158 // if Ctrl key is pressed and wxDragMove otherwise) except that it will
159 // always be wxDragNone if the carried data is in an unsupported format.
161 // called when the mouse enters the window (only once until OnLeave())
162 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
163 { return OnDragOver(x
, y
, def
); }
165 // called when the mouse moves in the window - shouldn't take long to
166 // execute or otherwise mouse movement would be too slow
167 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
171 // called when mouse leaves the window: might be used to remove the
172 // feedback which was given in OnEnter()
173 virtual void OnLeave() { }
175 // this function is called when data is dropped at position (x, y) - if it
176 // returns true, OnData() will be called immediately afterwards which will
177 // allow to retrieve the data dropped.
178 virtual bool OnDrop(wxCoord x
, wxCoord y
) = 0;
180 // called after OnDrop() returns TRUE: you will usually just call
181 // GetData() from here and, probably, also refresh something to update the
182 // new data and, finally, return the code indicating how did the operation
183 // complete (returning default value in case of success and wxDragError on
184 // failure is usually ok)
185 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
) = 0;
187 // may be called *only* from inside OnData() and will fill m_dataObject
188 // with the data from the drop source if it returns true
189 virtual bool GetData() = 0;
191 // sets the default action for drag and drop:
192 // use wxDragMove or wxDragCopy to set deafult action to move or copy
193 // and use wxDragNone (default) to set default action specified by
194 // initialization of draging (see wxDropSourceBase::DoDragDrop())
195 void SetDefaultAction(wxDragResult action
)
196 { m_defaultAction
= action
; }
198 // returns default action for drag and drop or
199 // wxDragNone if this not specified
200 wxDragResult
GetDefaultAction()
201 { return m_defaultAction
; }
204 wxDataObject
*m_dataObject
;
205 wxDragResult m_defaultAction
;
207 DECLARE_NO_COPY_CLASS(wxDropTargetBase
)
210 // ----------------------------------------------------------------------------
211 // include platform dependent class declarations
212 // ----------------------------------------------------------------------------
214 #if defined(__WXMSW__)
215 #include "wx/msw/ole/dropsrc.h"
216 #include "wx/msw/ole/droptgt.h"
217 #elif defined(__WXMOTIF__)
218 #include "wx/motif/dnd.h"
219 #elif defined(__WXX11__)
220 #include "wx/x11/dnd.h"
221 #elif defined(__WXGTK__)
222 #include "wx/gtk/dnd.h"
223 #elif defined(__WXMAC__)
224 #include "wx/mac/dnd.h"
225 #elif defined(__WXPM__)
226 #include "wx/os2/dnd.h"
229 // ----------------------------------------------------------------------------
230 // standard wxDropTarget implementations (implemented in common/dobjcmn.cpp)
231 // ----------------------------------------------------------------------------
233 // A simple wxDropTarget derived class for text data: you only need to
234 // override OnDropText() to get something working
235 class WXDLLEXPORT wxTextDropTarget
: public wxDropTarget
240 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
) = 0;
242 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
);
245 DECLARE_NO_COPY_CLASS(wxTextDropTarget
)
248 // A drop target which accepts files (dragged from File Manager or Explorer)
249 class WXDLLEXPORT wxFileDropTarget
: public wxDropTarget
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;
258 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
);
261 DECLARE_NO_COPY_CLASS(wxFileDropTarget
)
264 #endif // wxUSE_DRAG_AND_DROP
266 #endif // _WX_DND_H_BASE_