]> git.saurik.com Git - wxWidgets.git/blob - include/wx/dnd.h
changed format of wxFontMapper entries: facename_encoding instead of encoding as key
[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 #include "wx/cursor.h"
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
36 inline WXDLLEXPORT bool wxIsDragResultOk(wxDragResult res)
37 {
38 return res == wxDragCopy || res == wxDragMove;
39 }
40
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:
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() { }
57
58 // set the data which is transfered by drag and drop
59 void SetData(wxDataObject& data)
60 { m_data = &data; }
61
62 wxDataObject *GetDataObject()
63 { return m_data; }
64
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
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
79 virtual wxDragResult DoDragDrop(bool bAllowMove = FALSE) = 0;
80
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; }
85
86 protected:
87 const wxCursor& GetCursor(wxDragResult res) const
88 {
89 if ( res == wxDragCopy )
90 return m_cursorCopy;
91 else if ( res == wxDragMove )
92 return m_cursorMove;
93 else
94 return m_cursorStop;
95 }
96
97 wxDataObject *m_data;
98
99 // the cursors to use for feedback
100 wxCursor m_cursorCopy, m_cursorMove, m_cursorStop;
101 };
102
103 // ----------------------------------------------------------------------------
104 // wxDropTarget should be associated with a window if it wants to be able to
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)
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.
120 wxDropTargetBase(wxDataObject *dataObject = (wxDataObject*)NULL)
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)
130 { if (m_dataObject) delete m_dataObject;
131 m_dataObject = dataObject; }
132
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; }
151
152 // called when mouse leaves the window: might be used to remove the
153 // feedback which was given in OnEnter()
154 virtual void OnLeave() { }
155
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
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;
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__)
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"
195 #endif
196
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
226 #endif // wxUSE_DRAG_AND_DROP
227
228 #endif // _WX_DND_H_BASE_