]>
Commit | Line | Data |
---|---|---|
269a5a34 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/ole/dropsrc.cpp | |
3 | // Purpose: implementation of wxIDropSource and wxDropSource | |
4 | // Author: Vadim Zeitlin | |
3f480da3 | 5 | // Modified by: |
269a5a34 VZ |
6 | // Created: 10.05.98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
9 | // Licence: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "dropsrc.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #if defined(__BORLANDC__) | |
28 | #pragma hdrstop | |
29 | #endif | |
7dee726c RS |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/window.h" | |
32 | #endif | |
269a5a34 | 33 | |
3f480da3 | 34 | #include "wx/setup.h" |
269a5a34 | 35 | |
21709999 | 36 | #if wxUSE_OLE && wxUSE_DRAG_AND_DROP |
269a5a34 | 37 | |
3f480da3 | 38 | #include "wx/log.h" |
9e2896e5 | 39 | #include "wx/dnd.h" |
269a5a34 | 40 | |
17b74d79 | 41 | #include <windows.h> |
9e2896e5 | 42 | |
b64f0a5f | 43 | #if wxUSE_NORLANDER_HEADERS |
9e2896e5 | 44 | #include <ole2.h> |
7dee726c | 45 | #endif |
17b74d79 | 46 | |
269a5a34 | 47 | #ifndef __WIN32__ |
9e2896e5 VZ |
48 | #include <ole2.h> |
49 | #include <olestd.h> | |
269a5a34 VZ |
50 | #endif |
51 | ||
17b74d79 JS |
52 | #include <oleauto.h> |
53 | ||
3f480da3 | 54 | #include "wx/msw/ole/oleutils.h" |
17b74d79 | 55 | |
269a5a34 VZ |
56 | // ---------------------------------------------------------------------------- |
57 | // wxIDropSource implementation of IDropSource interface | |
58 | // ---------------------------------------------------------------------------- | |
59 | ||
60 | class wxIDropSource : public IDropSource | |
61 | { | |
62 | public: | |
63 | wxIDropSource(wxDropSource *pDropSource); | |
64 | ||
65 | DECLARE_IUNKNOWN_METHODS; | |
66 | ||
67 | // IDropSource | |
68 | STDMETHODIMP QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState); | |
69 | STDMETHODIMP GiveFeedback(DWORD dwEffect); | |
70 | ||
71 | private: | |
72 | DWORD m_grfInitKeyState; // button which started the d&d operation | |
73 | wxDropSource *m_pDropSource; // pointer to C++ class we belong to | |
22f3361e VZ |
74 | |
75 | DECLARE_NO_COPY_CLASS(wxIDropSource) | |
269a5a34 VZ |
76 | }; |
77 | ||
78 | // ============================================================================ | |
79 | // Implementation | |
80 | // ============================================================================ | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxIDropSource implementation | |
84 | // ---------------------------------------------------------------------------- | |
85 | BEGIN_IID_TABLE(wxIDropSource) | |
86 | ADD_IID(Unknown) | |
87 | ADD_IID(DropSource) | |
88 | END_IID_TABLE; | |
89 | ||
90 | IMPLEMENT_IUNKNOWN_METHODS(wxIDropSource) | |
91 | ||
92 | wxIDropSource::wxIDropSource(wxDropSource *pDropSource) | |
93 | { | |
94 | wxASSERT( pDropSource != NULL ); | |
95 | ||
96 | m_pDropSource = pDropSource; | |
97 | m_grfInitKeyState = 0; | |
269a5a34 VZ |
98 | } |
99 | ||
100 | // Name : wxIDropSource::QueryContinueDrag | |
101 | // Purpose : decide if drag operation must be continued or not | |
102 | // Returns : HRESULT: S_OK if we should continue | |
103 | // DRAGDROP_S_DROP to drop right now | |
104 | // DRAGDROP_S_CANCEL to cancel everything | |
105 | // Params : [in] BOOL fEscapePressed <Esc> pressed since last call? | |
106 | // [in] DWORD grfKeyState mask containing state of kbd keys | |
107 | // Notes : as there is no reasonably simple portable way to implement this | |
108 | // function, we currently don't give the possibility to override the | |
109 | // default behaviour implemented here | |
110 | STDMETHODIMP wxIDropSource::QueryContinueDrag(BOOL fEscapePressed, | |
111 | DWORD grfKeyState) | |
112 | { | |
113 | if ( fEscapePressed ) | |
114 | return DRAGDROP_S_CANCEL; | |
115 | ||
116 | // initialize ourself with the drag begin button | |
117 | if ( m_grfInitKeyState == 0 ) { | |
118 | m_grfInitKeyState = grfKeyState & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON); | |
119 | } | |
120 | ||
121 | if ( !(grfKeyState & m_grfInitKeyState) ) { | |
122 | // button which started d&d released, go! | |
123 | return DRAGDROP_S_DROP; | |
124 | } | |
125 | ||
126 | return S_OK; | |
127 | } | |
128 | ||
129 | // Name : wxIDropSource::GiveFeedback | |
130 | // Purpose : give UI feedback according to current state of operation | |
3f480da3 | 131 | // Returns : STDMETHODIMP |
269a5a34 VZ |
132 | // Params : [in] DWORD dwEffect - what would happen if we dropped now |
133 | // Notes : default implementation is ok in more than 99% of cases | |
134 | STDMETHODIMP wxIDropSource::GiveFeedback(DWORD dwEffect) | |
135 | { | |
46ccb510 | 136 | wxDragResult effect; |
269a5a34 | 137 | if ( dwEffect & DROPEFFECT_COPY ) |
46ccb510 | 138 | effect = wxDragCopy; |
269a5a34 | 139 | else if ( dwEffect & DROPEFFECT_MOVE ) |
46ccb510 | 140 | effect = wxDragMove; |
269a5a34 | 141 | else |
46ccb510 | 142 | effect = wxDragNone; |
269a5a34 | 143 | |
2d93e133 | 144 | if ( m_pDropSource->GiveFeedback(effect) ) |
269a5a34 VZ |
145 | return S_OK; |
146 | ||
147 | return DRAGDROP_S_USEDEFAULTCURSORS; | |
148 | } | |
149 | ||
150 | // ---------------------------------------------------------------------------- | |
151 | // wxDropSource implementation | |
152 | // ---------------------------------------------------------------------------- | |
153 | ||
154 | // ctors | |
155 | ||
156 | // common part of all ctors | |
157 | void wxDropSource::Init() | |
158 | { | |
2d93e133 VZ |
159 | m_pIDropSource = new wxIDropSource(this); |
160 | m_pIDropSource->AddRef(); | |
269a5a34 VZ |
161 | } |
162 | ||
8e193f38 | 163 | wxDropSource::wxDropSource(wxWindow* WXUNUSED(win), |
2d93e133 VZ |
164 | const wxCursor &cursorCopy, |
165 | const wxCursor &cursorMove, | |
166 | const wxCursor &cursorStop) | |
167 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
269a5a34 | 168 | { |
2d93e133 | 169 | Init(); |
269a5a34 VZ |
170 | } |
171 | ||
8e193f38 VZ |
172 | wxDropSource::wxDropSource(wxDataObject& data, |
173 | wxWindow* WXUNUSED(win), | |
2d93e133 VZ |
174 | const wxCursor &cursorCopy, |
175 | const wxCursor &cursorMove, | |
176 | const wxCursor &cursorStop) | |
177 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
269a5a34 | 178 | { |
2d93e133 VZ |
179 | Init(); |
180 | SetData(data); | |
269a5a34 VZ |
181 | } |
182 | ||
269a5a34 VZ |
183 | wxDropSource::~wxDropSource() |
184 | { | |
2d93e133 | 185 | m_pIDropSource->Release(); |
269a5a34 VZ |
186 | } |
187 | ||
188 | // Name : DoDragDrop | |
189 | // Purpose : start drag and drop operation | |
46ccb510 | 190 | // Returns : wxDragResult - the code of performed operation |
2245b2b2 | 191 | // Params : [in] int flags: specifies if moving is allowe (or only copying) |
269a5a34 | 192 | // Notes : you must call SetData() before if you had used def ctor |
2245b2b2 | 193 | wxDragResult wxDropSource::DoDragDrop(int flags) |
269a5a34 | 194 | { |
9e2896e5 | 195 | wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") ); |
269a5a34 VZ |
196 | |
197 | DWORD dwEffect; | |
9e2896e5 | 198 | HRESULT hr = ::DoDragDrop(m_data->GetInterface(), |
3f480da3 | 199 | m_pIDropSource, |
2245b2b2 VZ |
200 | (flags & wxDrag_AllowMove) |
201 | ? DROPEFFECT_COPY | DROPEFFECT_MOVE | |
202 | : DROPEFFECT_COPY, | |
269a5a34 VZ |
203 | &dwEffect); |
204 | ||
205 | if ( hr == DRAGDROP_S_CANCEL ) { | |
46ccb510 | 206 | return wxDragCancel; |
269a5a34 VZ |
207 | } |
208 | else if ( hr == DRAGDROP_S_DROP ) { | |
209 | if ( dwEffect & DROPEFFECT_COPY ) { | |
46ccb510 | 210 | return wxDragCopy; |
269a5a34 VZ |
211 | } |
212 | else if ( dwEffect & DROPEFFECT_MOVE ) { | |
213 | // consistency check: normally, we shouldn't get "move" at all | |
2245b2b2 VZ |
214 | // here if we don't allow it, but in practice it does happen quite often |
215 | return (flags & wxDrag_AllowMove) ? wxDragMove : wxDragCopy; | |
269a5a34 VZ |
216 | } |
217 | else { | |
218 | // not copy or move | |
46ccb510 | 219 | return wxDragNone; |
269a5a34 VZ |
220 | } |
221 | } | |
222 | else { | |
223 | if ( FAILED(hr) ) { | |
161f4f73 | 224 | wxLogApiError(wxT("DoDragDrop"), hr); |
223d09f6 | 225 | wxLogError(wxT("Drag & drop operation failed.")); |
269a5a34 VZ |
226 | } |
227 | else { | |
2d93e133 VZ |
228 | wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."), |
229 | hr); | |
269a5a34 VZ |
230 | } |
231 | ||
46ccb510 | 232 | return wxDragError; |
269a5a34 VZ |
233 | } |
234 | } | |
235 | ||
236 | // Name : wxDropSource::GiveFeedback | |
237 | // Purpose : visually inform the user about d&d operation state | |
238 | // Returns : bool: true if we do all ourselves or false for default feedback | |
239 | // Params : [in] DragResult effect - what would happen if we dropped now | |
269a5a34 | 240 | // Notes : here we just leave this stuff for default implementation |
2d93e133 | 241 | bool wxDropSource::GiveFeedback(wxDragResult effect) |
269a5a34 | 242 | { |
2d93e133 VZ |
243 | const wxCursor& cursor = GetCursor(effect); |
244 | if ( cursor.Ok() ) | |
245 | { | |
246 | ::SetCursor((HCURSOR)cursor.GetHCURSOR()); | |
247 | ||
248 | return TRUE; | |
249 | } | |
250 | else | |
251 | { | |
252 | return FALSE; | |
253 | } | |
269a5a34 VZ |
254 | } |
255 | ||
46ccb510 | 256 | #endif //USE_DRAG_AND_DROP |