1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop classes declarations
4 // Author: Vadim Zeitlin, Robert Roebling
7 // Copyright: (c) wxWidgets Team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_DND_H_BASE_
12 #define _WX_DND_H_BASE_
16 #if wxUSE_DRAG_AND_DROP
18 #include "wx/dataobj.h"
19 #include "wx/cursor.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 // flags for wxDropSource::DoDragDrop()
27 // NB: wxDrag_CopyOnly must be 0 (== FALSE) and wxDrag_AllowMove must be 1
28 // (== TRUE) for compatibility with the old DoDragDrop(bool) method!
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
36 // result of wxDropSource::DoDragDrop() call
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)
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
);
51 // ----------------------------------------------------------------------------
52 // wxDropSource is the object you need to create (and call DoDragDrop on it)
53 // to initiate a drag-and-drop operation
54 // ----------------------------------------------------------------------------
56 class WXDLLIMPEXP_CORE wxDropSourceBase
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
)
66 virtual ~wxDropSourceBase() { }
68 // set the data which is transferred by drag and drop
69 void SetData(wxDataObject
& data
)
72 wxDataObject
*GetDataObject()
75 // set the icon corresponding to given drag result
76 void SetCursor(wxDragResult res
, const wxCursor
& cursor
)
78 if ( res
== wxDragCopy
)
79 m_cursorCopy
= cursor
;
80 else if ( res
== wxDragMove
)
81 m_cursorMove
= cursor
;
83 m_cursorStop
= cursor
;
86 // start drag action, see enum wxDragResult for return value description
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;
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; }
99 const wxCursor
& GetCursor(wxDragResult res
) const
101 if ( res
== wxDragCopy
)
103 else if ( res
== wxDragMove
)
109 // the data we're dragging
110 wxDataObject
*m_data
;
112 // the cursors to use for feedback
113 wxCursor m_cursorCopy
,
117 wxDECLARE_NO_COPY_CLASS(wxDropSourceBase
);
120 // ----------------------------------------------------------------------------
121 // wxDropTarget should be associated with a window if it wants to be able to
122 // receive data via drag and drop.
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 // ----------------------------------------------------------------------------
131 class WXDLLIMPEXP_CORE wxDropTargetBase
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
; }
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
; }
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.
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.
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
); }
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
),
169 // called when mouse leaves the window: might be used to remove the
170 // feedback which was given in OnEnter()
171 virtual void OnLeave() { }
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;
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;
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;
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
; }
196 // returns default action for drag and drop or
197 // wxDragNone if this not specified
198 wxDragResult
GetDefaultAction()
199 { return m_defaultAction
; }
202 wxDataObject
*m_dataObject
;
203 wxDragResult m_defaultAction
;
205 wxDECLARE_NO_COPY_CLASS(wxDropTargetBase
);
208 // ----------------------------------------------------------------------------
209 // include platform dependent class declarations
210 // ----------------------------------------------------------------------------
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"
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 WXDLLIMPEXP_CORE 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 wxDECLARE_NO_COPY_CLASS(wxTextDropTarget
);
248 // A drop target which accepts files (dragged from File Manager or Explorer)
249 class WXDLLIMPEXP_CORE 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 wxDECLARE_NO_COPY_CLASS(wxFileDropTarget
);
264 #endif // wxUSE_DRAG_AND_DROP
266 #endif // _WX_DND_H_BASE_