1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop classes declarations
4 // Author: Vadim Zeitlin, Robert Roebling
8 // Copyright: (c) wxWindows Team
9 // Licence: wxWindows license
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 // result of wxDropSource::DoDragDrop() call
29 wxDragError
, // error prevented the d&d operation from completing
30 wxDragNone
, // drag target didn't accept the data
31 wxDragCopy
, // the data was successfully copied
32 wxDragMove
, // the data was successfully moved (MSW only)
33 wxDragCancel
// the operation was cancelled by user (not an error)
36 inline WXDLLEXPORT
bool wxIsDragResultOk(wxDragResult res
)
38 return res
== wxDragCopy
|| res
== wxDragMove
;
41 // ----------------------------------------------------------------------------
42 // wxDropSource is the object you need to create (and call DoDragDrop on it)
43 // to initiate a drag-and-drop operation
44 // ----------------------------------------------------------------------------
46 class WXDLLEXPORT wxDropSourceBase
49 wxDropSourceBase(const wxCursor
&cursorCopy
= wxNullCursor
,
50 const wxCursor
&cursorMove
= wxNullCursor
,
51 const wxCursor
&cursorStop
= wxNullCursor
)
52 : m_cursorCopy(cursorCopy
),
53 m_cursorMove(cursorMove
),
54 m_cursorStop(cursorStop
)
55 { m_data
= (wxDataObject
*)NULL
; }
56 virtual ~wxDropSourceBase() { }
58 // set the data which is transfered by drag and drop
59 void SetData(wxDataObject
& data
)
62 wxDataObject
*GetDataObject()
65 // set the icon corresponding to given drag result
66 void SetCursor(wxDragResult res
, const wxCursor
& cursor
)
68 if ( res
== wxDragCopy
)
69 m_cursorCopy
= cursor
;
70 else if ( res
== wxDragMove
)
71 m_cursorMove
= cursor
;
73 m_cursorStop
= cursor
;
76 // start drag action, see enum wxDragResult for return value description
78 // if bAllowMove is TRUE, data can be moved, if not - only copied
79 virtual wxDragResult
DoDragDrop(bool bAllowMove
= FALSE
) = 0;
81 // override to give feedback depending on the current operation result
82 // "effect" and return TRUE if you did something, FALSE to let the library
83 // give the default feedback
84 virtual bool GiveFeedback(wxDragResult
WXUNUSED(effect
)) { return FALSE
; }
87 const wxCursor
& GetCursor(wxDragResult res
) const
89 if ( res
== wxDragCopy
)
91 else if ( res
== wxDragMove
)
99 // the cursors to use for feedback
100 wxCursor m_cursorCopy
, m_cursorMove
, m_cursorStop
;
103 // ----------------------------------------------------------------------------
104 // wxDropTarget should be associated with a window if it wants to be able to
105 // receive data via drag and drop.
107 // To use this class, you should derive from wxDropTarget and implement
108 // OnData() pure virtual method. You may also wish to override OnDrop() if you
109 // want to accept the data only inside some region of the window (this may
110 // avoid having to copy the data to this application which happens only when
111 // OnData() is called)
112 // ----------------------------------------------------------------------------
114 class WXDLLEXPORT wxDropTargetBase
117 // ctor takes a pointer to heap-allocated wxDataObject which will be owned
118 // by wxDropTarget and deleted by it automatically. If you don't give it
119 // here, you can use SetDataObject() later.
120 wxDropTargetBase(wxDataObject
*dataObject
= (wxDataObject
*)NULL
)
121 { m_dataObject
= dataObject
; }
122 // dtor deletes our data object
123 virtual ~wxDropTargetBase()
124 { delete m_dataObject
; }
126 // get/set the associated wxDataObject
127 wxDataObject
*GetDataObject() const
128 { return m_dataObject
; }
129 void SetDataObject(wxDataObject
*dataObject
)
130 { if (m_dataObject
) delete m_dataObject
;
131 m_dataObject
= dataObject
; }
133 // these functions are called when data is moved over position (x, y) and
134 // may return either wxDragCopy, wxDragMove or wxDragNone depending on
135 // what would happen if the data were dropped here.
137 // the last parameter is what would happen by default and is determined by
138 // the platform-specific logic (for example, under Windows it's wxDragCopy
139 // if Ctrl key is pressed and wxDragMove otherwise) except that it will
140 // always be wxDragNone if the carried data is in an unsupported format.
142 // called when the mouse enters the window (only once until OnLeave())
143 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
144 { return OnDragOver(x
, y
, def
); }
146 // called when the mouse moves in the window - shouldn't take long to
147 // execute or otherwise mouse movement would be too slow
148 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
152 // called when mouse leaves the window: might be used to remove the
153 // feedback which was given in OnEnter()
154 virtual void OnLeave() { }
156 // this function is called when data is dropped at position (x, y) - if it
157 // returns TRUE, OnData() will be called immediately afterwards which will
158 // allow to retrieve the data dropped.
159 virtual bool OnDrop(wxCoord x
, wxCoord y
) = 0;
161 // called after OnDrop() returns TRUE: you will usually just call
162 // GetData() from here and, probably, also refresh something to update the
163 // new data and, finally, return the code indicating how did the operation
164 // complete (returning default value in case of success and wxDragError on
165 // failure is usually ok)
166 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
) = 0;
168 // may be called *only* from inside OnData() and will fill m_dataObject
169 // with the data from the drop source if it returns TRUE
170 virtual bool GetData() = 0;
173 wxDataObject
*m_dataObject
;
176 // ----------------------------------------------------------------------------
177 // include platform dependent class declarations
178 // ----------------------------------------------------------------------------
180 #if defined(__WXMSW__)
181 #include "wx/msw/ole/dropsrc.h"
182 #include "wx/msw/ole/droptgt.h"
183 #elif defined(__WXMOTIF__)
184 #include "wx/motif/dnd.h"
185 #elif defined(__WXGTK__)
186 #include "wx/gtk/dnd.h"
187 #elif defined(__WXQT__)
188 #include "wx/qt/dnd.h"
189 #elif defined(__WXMAC__)
190 #include "wx/mac/dnd.h"
191 #elif defined(__WXPM__)
192 #include "wx/os2/dnd.h"
193 #elif defined(__WXSTUBS__)
194 #include "wx/stubs/dnd.h"
197 // ----------------------------------------------------------------------------
198 // standard wxDropTarget implementations (implemented in common/dobjcmn.cpp)
199 // ----------------------------------------------------------------------------
201 // A simple wxDropTarget derived class for text data: you only need to
202 // override OnDropText() to get something working
203 class WXDLLEXPORT wxTextDropTarget
: public wxDropTarget
208 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
) = 0;
210 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
);
213 // A drop target which accepts files (dragged from File Manager or Explorer)
214 class WXDLLEXPORT wxFileDropTarget
: public wxDropTarget
219 // parameters are the number of files and the array of file names
220 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
221 const wxArrayString
& filenames
) = 0;
223 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
);
226 #endif // wxUSE_DRAG_AND_DROP
228 #endif // _WX_DND_H_BASE_