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 // return true if res indicates that something was done during a dnd operation,
49 // i.e. is neither error nor none nor cancel
50 WXDLLIMPEXP_CORE
bool wxIsDragResultOk(wxDragResult res
);
52 // ----------------------------------------------------------------------------
53 // wxDropSource is the object you need to create (and call DoDragDrop on it)
54 // to initiate a drag-and-drop operation
55 // ----------------------------------------------------------------------------
57 class WXDLLIMPEXP_CORE wxDropSourceBase
60 wxDropSourceBase(const wxCursor
&cursorCopy
= wxNullCursor
,
61 const wxCursor
&cursorMove
= wxNullCursor
,
62 const wxCursor
&cursorStop
= wxNullCursor
)
63 : m_cursorCopy(cursorCopy
),
64 m_cursorMove(cursorMove
),
65 m_cursorStop(cursorStop
)
67 virtual ~wxDropSourceBase() { }
69 // set the data which is transferred by drag and drop
70 void SetData(wxDataObject
& data
)
73 wxDataObject
*GetDataObject()
76 // set the icon corresponding to given drag result
77 void SetCursor(wxDragResult res
, const wxCursor
& cursor
)
79 if ( res
== wxDragCopy
)
80 m_cursorCopy
= cursor
;
81 else if ( res
== wxDragMove
)
82 m_cursorMove
= cursor
;
84 m_cursorStop
= cursor
;
87 // start drag action, see enum wxDragResult for return value description
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;
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
97 virtual bool GiveFeedback(wxDragResult
WXUNUSED(effect
)) { return false; }
100 const wxCursor
& GetCursor(wxDragResult res
) const
102 if ( res
== wxDragCopy
)
104 else if ( res
== wxDragMove
)
110 // the data we're dragging
111 wxDataObject
*m_data
;
113 // the cursors to use for feedback
114 wxCursor m_cursorCopy
,
118 wxDECLARE_NO_COPY_CLASS(wxDropSourceBase
);
121 // ----------------------------------------------------------------------------
122 // wxDropTarget should be associated with a window if it wants to be able to
123 // receive data via drag and drop.
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 // ----------------------------------------------------------------------------
132 class WXDLLIMPEXP_CORE wxDropTargetBase
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
= NULL
)
139 { m_dataObject
= dataObject
; m_defaultAction
= wxDragNone
; }
140 // dtor deletes our data object
141 virtual ~wxDropTargetBase()
142 { delete m_dataObject
; }
144 // get/set the associated wxDataObject
145 wxDataObject
*GetDataObject() const
146 { return m_dataObject
; }
147 void SetDataObject(wxDataObject
*dataObject
)
148 { if (m_dataObject
) delete m_dataObject
;
149 m_dataObject
= dataObject
; }
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.
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.
160 // called when the mouse enters the window (only once until OnLeave())
161 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
162 { return OnDragOver(x
, y
, def
); }
164 // called when the mouse moves in the window - shouldn't take long to
165 // execute or otherwise mouse movement would be too slow
166 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
170 // called when mouse leaves the window: might be used to remove the
171 // feedback which was given in OnEnter()
172 virtual void OnLeave() { }
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;
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;
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;
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())
194 void SetDefaultAction(wxDragResult action
)
195 { m_defaultAction
= action
; }
197 // returns default action for drag and drop or
198 // wxDragNone if this not specified
199 wxDragResult
GetDefaultAction()
200 { return m_defaultAction
; }
203 wxDataObject
*m_dataObject
;
204 wxDragResult m_defaultAction
;
206 wxDECLARE_NO_COPY_CLASS(wxDropTargetBase
);
209 // ----------------------------------------------------------------------------
210 // include platform dependent class declarations
211 // ----------------------------------------------------------------------------
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/osx/dnd.h"
226 #elif defined(__WXPM__)
227 #include "wx/os2/dnd.h"
230 // ----------------------------------------------------------------------------
231 // standard wxDropTarget implementations (implemented in common/dobjcmn.cpp)
232 // ----------------------------------------------------------------------------
234 // A simple wxDropTarget derived class for text data: you only need to
235 // override OnDropText() to get something working
236 class WXDLLIMPEXP_CORE wxTextDropTarget
: public wxDropTarget
241 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
) = 0;
243 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
);
246 wxDECLARE_NO_COPY_CLASS(wxTextDropTarget
);
249 // A drop target which accepts files (dragged from File Manager or Explorer)
250 class WXDLLIMPEXP_CORE wxFileDropTarget
: public wxDropTarget
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;
259 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
);
262 wxDECLARE_NO_COPY_CLASS(wxFileDropTarget
);
265 #endif // wxUSE_DRAG_AND_DROP
267 #endif // _WX_DND_H_BASE_