1. wxDropTarget::OnData() returns wxDragResult now, not bool
[wxWidgets.git] / include / wx / dnd.h
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
12 #ifndef _WX_DND_H_BASE_
13 #define _WX_DND_H_BASE_
14
15 #include "wx/defs.h"
16
17 #if wxUSE_DRAG_AND_DROP
18
19 #include "wx/dataobj.h"
20
21 // ----------------------------------------------------------------------------
22 // constants
23 // ----------------------------------------------------------------------------
24
25 // result of wxDropSource::DoDragDrop() call
26 enum wxDragResult
27 {
28 wxDragError, // error prevented the d&d operation from completing
29 wxDragNone, // drag target didn't accept the data
30 wxDragCopy, // the data was successfully copied
31 wxDragMove, // the data was successfully moved (MSW only)
32 wxDragCancel // the operation was cancelled by user (not an error)
33 };
34
35 inline WXDLLEXPORT bool wxIsDragResultOk(wxDragResult res)
36 {
37 return res == wxDragCopy || res == wxDragMove;
38 }
39
40 // ----------------------------------------------------------------------------
41 // wxDropSource is the object you need to create (and call DoDragDrop on it)
42 // to initiate a drag-and-drop operation
43 // ----------------------------------------------------------------------------
44
45 class WXDLLEXPORT wxDropSourceBase
46 {
47 public:
48 wxDropSourceBase() { m_data = (wxDataObject *)NULL; }
49 virtual ~wxDropSourceBase() { }
50
51 // set the data which is transfered by drag and drop
52 void SetData(wxDataObject& data)
53 { m_data = &data; }
54
55 wxDataObject *GetDataObject()
56 { return m_data; }
57
58 // start drag action, see enum wxDragResult for return value description
59 //
60 // if bAllowMove is TRUE, data can be moved, if not - only copied
61 virtual wxDragResult DoDragDrop(bool bAllowMove = FALSE) = 0;
62
63 // override to give feedback depending on the current operation result
64 // "effect"
65 virtual bool GiveFeedback( wxDragResult WXUNUSED(effect),
66 bool WXUNUSED(bScrolling) )
67 {
68 return TRUE;
69 }
70
71 protected:
72 wxDataObject *m_data;
73 };
74
75 // ----------------------------------------------------------------------------
76 // wxDropTarget should be associated with a window if it wants to be able to
77 // receive data via drag and drop.
78 //
79 // To use this class, you should derive from wxDropTarget and implement
80 // OnData() pure virtual method. You may also wish to override OnDrop() if you
81 // want to accept the data only inside some region of the window (this may
82 // avoid having to copy the data to this application which happens only when
83 // OnData() is called)
84 // ----------------------------------------------------------------------------
85
86 class WXDLLEXPORT wxDropTargetBase
87 {
88 public:
89 // ctor takes a pointer to heap-allocated wxDataObject which will be owned
90 // by wxDropTarget and deleted by it automatically. If you don't give it
91 // here, you can use SetDataObject() later.
92 wxDropTargetBase(wxDataObject *dataObject = (wxDataObject*)NULL)
93 { m_dataObject = dataObject; }
94 // dtor deletes our data object
95 virtual ~wxDropTargetBase()
96 { delete m_dataObject; }
97
98 // get/set the associated wxDataObject
99 wxDataObject *GetDataObject() const
100 { return m_dataObject; }
101 void SetDataObject(wxDataObject *dataObject)
102 { if (m_dataObject) delete m_dataObject;
103 m_dataObject = dataObject; }
104
105 // these functions are called when data is moved over position (x, y) and
106 // may return either wxDragCopy, wxDragMove or wxDragNone depending on
107 // what would happen if the data were dropped here.
108 //
109 // the last parameter is what would happen by default and is determined by
110 // the platform-specific logic (for example, under Windows it's wxDragCopy
111 // if Ctrl key is pressed and wxDragMove otherwise) except that it will
112 // always be wxDragNone if the carried data is in an unsupported format.
113
114 // called when the mouse enters the window (only once until OnLeave())
115 virtual wxDragResult OnEnter(wxCoord x, wxCoord y, wxDragResult def)
116 { return OnDragOver(x, y, def); }
117
118 // called when the mouse moves in the window - shouldn't take long to
119 // execute or otherwise mouse movement would be too slow
120 virtual wxDragResult OnDragOver(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
121 wxDragResult def)
122 { return def; }
123
124 // called when mouse leaves the window: might be used to remove the
125 // feedback which was given in OnEnter()
126 virtual void OnLeave() { }
127
128 // this function is called when data is dropped at position (x, y) - if it
129 // returns TRUE, OnData() will be called immediately afterwards which will
130 // allow to retrieve the data dropped.
131 virtual bool OnDrop(wxCoord x, wxCoord y) = 0;
132
133 // called after OnDrop() returns TRUE: you will usually just call
134 // GetData() from here and, probably, also refresh something to update the
135 // new data and, finally, return the code indicating how did the operation
136 // complete (returning default value in case of success and wxDragError on
137 // failure is usually ok)
138 virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def) = 0;
139
140 // may be called *only* from inside OnData() and will fill m_dataObject
141 // with the data from the drop source if it returns TRUE
142 virtual bool GetData() = 0;
143
144 protected:
145 wxDataObject *m_dataObject;
146 };
147
148 // ----------------------------------------------------------------------------
149 // include platform dependent class declarations
150 // ----------------------------------------------------------------------------
151
152 #if defined(__WXMSW__)
153 #include "wx/msw/ole/dropsrc.h"
154 #include "wx/msw/ole/droptgt.h"
155 #elif defined(__WXMOTIF__)
156 #include "wx/motif/dnd.h"
157 #elif defined(__WXGTK__)
158 #include "wx/gtk/dnd.h"
159 #elif defined(__WXQT__)
160 #include "wx/qt/dnd.h"
161 #elif defined(__WXMAC__)
162 #include "wx/mac/dnd.h"
163 #elif defined(__WXPM__)
164 #include "wx/os2/dnd.h"
165 #elif defined(__WXSTUBS__)
166 #include "wx/stubs/dnd.h"
167 #endif
168
169 // ----------------------------------------------------------------------------
170 // standard wxDropTarget implementations (implemented in common/dobjcmn.cpp)
171 // ----------------------------------------------------------------------------
172
173 // A simple wxDropTarget derived class for text data: you only need to
174 // override OnDropText() to get something working
175 class WXDLLEXPORT wxTextDropTarget : public wxDropTarget
176 {
177 public:
178 wxTextDropTarget();
179
180 virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text) = 0;
181
182 virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def);
183 };
184
185 // A drop target which accepts files (dragged from File Manager or Explorer)
186 class WXDLLEXPORT wxFileDropTarget : public wxDropTarget
187 {
188 public:
189 wxFileDropTarget();
190
191 // parameters are the number of files and the array of file names
192 virtual bool OnDropFiles(wxCoord x, wxCoord y,
193 const wxArrayString& filenames) = 0;
194
195 virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def);
196 };
197
198 #endif // wxUSE_DRAG_AND_DROP
199
200 #endif // _WX_DND_H_BASE_