]>
Commit | Line | Data |
---|---|---|
e1ee679c VZ |
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 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWindows Team | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
34138703 JS |
12 | #ifndef _WX_DND_H_BASE_ |
13 | #define _WX_DND_H_BASE_ | |
c801d85f | 14 | |
e1ee679c VZ |
15 | #include "wx/defs.h" |
16 | ||
17 | #if wxUSE_DRAG_AND_DROP | |
18 | ||
3f480da3 | 19 | #include "wx/dataobj.h" |
974e8d94 | 20 | #include "wx/cursor.h" |
e1ee679c VZ |
21 | |
22 | // ---------------------------------------------------------------------------- | |
23 | // constants | |
24 | // ---------------------------------------------------------------------------- | |
25 | ||
26 | // result of wxDropSource::DoDragDrop() call | |
27 | enum wxDragResult | |
28 | { | |
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) | |
34 | }; | |
35 | ||
8ee9d618 VZ |
36 | inline WXDLLEXPORT bool wxIsDragResultOk(wxDragResult res) |
37 | { | |
38 | return res == wxDragCopy || res == wxDragMove; | |
39 | } | |
40 | ||
e1ee679c VZ |
41 | // ---------------------------------------------------------------------------- |
42 | // wxDropSource is the object you need to create (and call DoDragDrop on it) | |
43 | // to initiate a drag-and-drop operation | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | class WXDLLEXPORT wxDropSourceBase | |
47 | { | |
48 | public: | |
2d93e133 VZ |
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; } | |
e1ee679c VZ |
56 | virtual ~wxDropSourceBase() { } |
57 | ||
58 | // set the data which is transfered by drag and drop | |
6dddc146 | 59 | void SetData(wxDataObject& data) |
97c79de2 | 60 | { m_data = &data; } |
6dddc146 | 61 | |
97c79de2 RR |
62 | wxDataObject *GetDataObject() |
63 | { return m_data; } | |
e1ee679c | 64 | |
2d93e133 VZ |
65 | // set the icon corresponding to given drag result |
66 | void SetCursor(wxDragResult res, const wxCursor& cursor) | |
67 | { | |
68 | if ( res == wxDragCopy ) | |
69 | m_cursorCopy = cursor; | |
70 | else if ( res == wxDragMove ) | |
71 | m_cursorMove = cursor; | |
72 | else | |
73 | m_cursorStop = cursor; | |
74 | } | |
75 | ||
e1ee679c VZ |
76 | // start drag action, see enum wxDragResult for return value description |
77 | // | |
78 | // if bAllowMove is TRUE, data can be moved, if not - only copied | |
9e2896e5 | 79 | virtual wxDragResult DoDragDrop(bool bAllowMove = FALSE) = 0; |
e1ee679c VZ |
80 | |
81 | // override to give feedback depending on the current operation result | |
2d93e133 VZ |
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; } | |
85 | ||
86 | protected: | |
87 | const wxCursor& GetCursor(wxDragResult res) const | |
e1ee679c | 88 | { |
2d93e133 VZ |
89 | if ( res == wxDragCopy ) |
90 | return m_cursorCopy; | |
91 | else if ( res == wxDragMove ) | |
92 | return m_cursorMove; | |
93 | else | |
94 | return m_cursorStop; | |
e1ee679c VZ |
95 | } |
96 | ||
e1ee679c | 97 | wxDataObject *m_data; |
2d93e133 VZ |
98 | |
99 | // the cursors to use for feedback | |
100 | wxCursor m_cursorCopy, m_cursorMove, m_cursorStop; | |
e1ee679c VZ |
101 | }; |
102 | ||
103 | // ---------------------------------------------------------------------------- | |
104 | // wxDropTarget should be associated with a window if it wants to be able to | |
9e2896e5 VZ |
105 | // receive data via drag and drop. |
106 | // | |
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) | |
e1ee679c VZ |
112 | // ---------------------------------------------------------------------------- |
113 | ||
114 | class WXDLLEXPORT wxDropTargetBase | |
115 | { | |
116 | public: | |
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. | |
b068c4e8 | 120 | wxDropTargetBase(wxDataObject *dataObject = (wxDataObject*)NULL) |
e1ee679c VZ |
121 | { m_dataObject = dataObject; } |
122 | // dtor deletes our data object | |
123 | virtual ~wxDropTargetBase() | |
124 | { delete m_dataObject; } | |
125 | ||
126 | // get/set the associated wxDataObject | |
127 | wxDataObject *GetDataObject() const | |
128 | { return m_dataObject; } | |
129 | void SetDataObject(wxDataObject *dataObject) | |
6dddc146 DW |
130 | { if (m_dataObject) delete m_dataObject; |
131 | m_dataObject = dataObject; } | |
e1ee679c | 132 | |
c9057ae1 VZ |
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. | |
136 | // | |
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. | |
141 | ||
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); } | |
145 | ||
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), | |
149 | wxDragResult def) | |
150 | { return def; } | |
6dddc146 | 151 | |
c9057ae1 VZ |
152 | // called when mouse leaves the window: might be used to remove the |
153 | // feedback which was given in OnEnter() | |
154 | virtual void OnLeave() { } | |
6dddc146 | 155 | |
e1ee679c VZ |
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; | |
160 | ||
161 | // called after OnDrop() returns TRUE: you will usually just call | |
162 | // GetData() from here and, probably, also refresh something to update the | |
8ee9d618 VZ |
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; | |
e1ee679c VZ |
167 | |
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; | |
171 | ||
172 | protected: | |
173 | wxDataObject *m_dataObject; | |
174 | }; | |
175 | ||
176 | // ---------------------------------------------------------------------------- | |
177 | // include platform dependent class declarations | |
178 | // ---------------------------------------------------------------------------- | |
179 | ||
180 | #if defined(__WXMSW__) | |
e1ee679c VZ |
181 | #include "wx/msw/ole/dropsrc.h" |
182 | #include "wx/msw/ole/droptgt.h" | |
2049ba38 | 183 | #elif defined(__WXMOTIF__) |
e1ee679c | 184 | #include "wx/motif/dnd.h" |
2049ba38 | 185 | #elif defined(__WXGTK__) |
e1ee679c | 186 | #include "wx/gtk/dnd.h" |
b4e76e0d | 187 | #elif defined(__WXQT__) |
e1ee679c | 188 | #include "wx/qt/dnd.h" |
34138703 | 189 | #elif defined(__WXMAC__) |
e1ee679c | 190 | #include "wx/mac/dnd.h" |
1777b9bb | 191 | #elif defined(__WXPM__) |
e1ee679c | 192 | #include "wx/os2/dnd.h" |
34138703 | 193 | #elif defined(__WXSTUBS__) |
e1ee679c | 194 | #include "wx/stubs/dnd.h" |
c801d85f KB |
195 | #endif |
196 | ||
8ee9d618 VZ |
197 | // ---------------------------------------------------------------------------- |
198 | // standard wxDropTarget implementations (implemented in common/dobjcmn.cpp) | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
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 | |
204 | { | |
205 | public: | |
206 | wxTextDropTarget(); | |
207 | ||
208 | virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text) = 0; | |
209 | ||
210 | virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def); | |
211 | }; | |
212 | ||
213 | // A drop target which accepts files (dragged from File Manager or Explorer) | |
214 | class WXDLLEXPORT wxFileDropTarget : public wxDropTarget | |
215 | { | |
216 | public: | |
217 | wxFileDropTarget(); | |
218 | ||
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; | |
222 | ||
223 | virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def); | |
224 | }; | |
225 | ||
e1ee679c VZ |
226 | #endif // wxUSE_DRAG_AND_DROP |
227 | ||
228 | #endif // _WX_DND_H_BASE_ |