]>
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 | |
74 | }; | |
75 | ||
76 | // ============================================================================ | |
77 | // Implementation | |
78 | // ============================================================================ | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // wxIDropSource implementation | |
82 | // ---------------------------------------------------------------------------- | |
83 | BEGIN_IID_TABLE(wxIDropSource) | |
84 | ADD_IID(Unknown) | |
85 | ADD_IID(DropSource) | |
86 | END_IID_TABLE; | |
87 | ||
88 | IMPLEMENT_IUNKNOWN_METHODS(wxIDropSource) | |
89 | ||
90 | wxIDropSource::wxIDropSource(wxDropSource *pDropSource) | |
91 | { | |
92 | wxASSERT( pDropSource != NULL ); | |
93 | ||
94 | m_pDropSource = pDropSource; | |
95 | m_grfInitKeyState = 0; | |
96 | m_cRef = 0; | |
97 | } | |
98 | ||
99 | // Name : wxIDropSource::QueryContinueDrag | |
100 | // Purpose : decide if drag operation must be continued or not | |
101 | // Returns : HRESULT: S_OK if we should continue | |
102 | // DRAGDROP_S_DROP to drop right now | |
103 | // DRAGDROP_S_CANCEL to cancel everything | |
104 | // Params : [in] BOOL fEscapePressed <Esc> pressed since last call? | |
105 | // [in] DWORD grfKeyState mask containing state of kbd keys | |
106 | // Notes : as there is no reasonably simple portable way to implement this | |
107 | // function, we currently don't give the possibility to override the | |
108 | // default behaviour implemented here | |
109 | STDMETHODIMP wxIDropSource::QueryContinueDrag(BOOL fEscapePressed, | |
110 | DWORD grfKeyState) | |
111 | { | |
112 | if ( fEscapePressed ) | |
113 | return DRAGDROP_S_CANCEL; | |
114 | ||
115 | // initialize ourself with the drag begin button | |
116 | if ( m_grfInitKeyState == 0 ) { | |
117 | m_grfInitKeyState = grfKeyState & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON); | |
118 | } | |
119 | ||
120 | if ( !(grfKeyState & m_grfInitKeyState) ) { | |
121 | // button which started d&d released, go! | |
122 | return DRAGDROP_S_DROP; | |
123 | } | |
124 | ||
125 | return S_OK; | |
126 | } | |
127 | ||
128 | // Name : wxIDropSource::GiveFeedback | |
129 | // Purpose : give UI feedback according to current state of operation | |
3f480da3 | 130 | // Returns : STDMETHODIMP |
269a5a34 VZ |
131 | // Params : [in] DWORD dwEffect - what would happen if we dropped now |
132 | // Notes : default implementation is ok in more than 99% of cases | |
133 | STDMETHODIMP wxIDropSource::GiveFeedback(DWORD dwEffect) | |
134 | { | |
46ccb510 | 135 | wxDragResult effect; |
269a5a34 | 136 | if ( dwEffect & DROPEFFECT_COPY ) |
46ccb510 | 137 | effect = wxDragCopy; |
269a5a34 | 138 | else if ( dwEffect & DROPEFFECT_MOVE ) |
46ccb510 | 139 | effect = wxDragMove; |
269a5a34 | 140 | else |
46ccb510 | 141 | effect = wxDragNone; |
269a5a34 | 142 | |
2d93e133 | 143 | if ( m_pDropSource->GiveFeedback(effect) ) |
269a5a34 VZ |
144 | return S_OK; |
145 | ||
146 | return DRAGDROP_S_USEDEFAULTCURSORS; | |
147 | } | |
148 | ||
149 | // ---------------------------------------------------------------------------- | |
150 | // wxDropSource implementation | |
151 | // ---------------------------------------------------------------------------- | |
152 | ||
153 | // ctors | |
154 | ||
155 | // common part of all ctors | |
156 | void wxDropSource::Init() | |
157 | { | |
2d93e133 VZ |
158 | m_pIDropSource = new wxIDropSource(this); |
159 | m_pIDropSource->AddRef(); | |
269a5a34 VZ |
160 | } |
161 | ||
8e193f38 | 162 | wxDropSource::wxDropSource(wxWindow* WXUNUSED(win), |
2d93e133 VZ |
163 | const wxCursor &cursorCopy, |
164 | const wxCursor &cursorMove, | |
165 | const wxCursor &cursorStop) | |
166 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
269a5a34 | 167 | { |
2d93e133 | 168 | Init(); |
269a5a34 VZ |
169 | } |
170 | ||
8e193f38 VZ |
171 | wxDropSource::wxDropSource(wxDataObject& data, |
172 | wxWindow* WXUNUSED(win), | |
2d93e133 VZ |
173 | const wxCursor &cursorCopy, |
174 | const wxCursor &cursorMove, | |
175 | const wxCursor &cursorStop) | |
176 | : wxDropSourceBase(cursorCopy, cursorMove, cursorStop) | |
269a5a34 | 177 | { |
2d93e133 VZ |
178 | Init(); |
179 | SetData(data); | |
269a5a34 VZ |
180 | } |
181 | ||
269a5a34 VZ |
182 | wxDropSource::~wxDropSource() |
183 | { | |
2d93e133 | 184 | m_pIDropSource->Release(); |
269a5a34 VZ |
185 | } |
186 | ||
187 | // Name : DoDragDrop | |
188 | // Purpose : start drag and drop operation | |
46ccb510 | 189 | // Returns : wxDragResult - the code of performed operation |
269a5a34 VZ |
190 | // Params : [in] bool bAllowMove: if false, only copy is allowed |
191 | // Notes : you must call SetData() before if you had used def ctor | |
46ccb510 | 192 | wxDragResult wxDropSource::DoDragDrop(bool bAllowMove) |
269a5a34 | 193 | { |
9e2896e5 | 194 | wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") ); |
269a5a34 VZ |
195 | |
196 | DWORD dwEffect; | |
9e2896e5 | 197 | HRESULT hr = ::DoDragDrop(m_data->GetInterface(), |
3f480da3 | 198 | m_pIDropSource, |
269a5a34 VZ |
199 | bAllowMove ? DROPEFFECT_COPY | DROPEFFECT_MOVE |
200 | : DROPEFFECT_COPY, | |
201 | &dwEffect); | |
202 | ||
203 | if ( hr == DRAGDROP_S_CANCEL ) { | |
46ccb510 | 204 | return wxDragCancel; |
269a5a34 VZ |
205 | } |
206 | else if ( hr == DRAGDROP_S_DROP ) { | |
207 | if ( dwEffect & DROPEFFECT_COPY ) { | |
46ccb510 | 208 | return wxDragCopy; |
269a5a34 VZ |
209 | } |
210 | else if ( dwEffect & DROPEFFECT_MOVE ) { | |
211 | // consistency check: normally, we shouldn't get "move" at all | |
212 | // here if !bAllowMove, but in practice it does happen quite often | |
213 | if ( bAllowMove ) | |
46ccb510 | 214 | return wxDragMove; |
269a5a34 | 215 | else |
46ccb510 | 216 | return wxDragCopy; |
269a5a34 VZ |
217 | } |
218 | else { | |
219 | // not copy or move | |
46ccb510 | 220 | return wxDragNone; |
269a5a34 VZ |
221 | } |
222 | } | |
223 | else { | |
224 | if ( FAILED(hr) ) { | |
161f4f73 | 225 | wxLogApiError(wxT("DoDragDrop"), hr); |
223d09f6 | 226 | wxLogError(wxT("Drag & drop operation failed.")); |
269a5a34 VZ |
227 | } |
228 | else { | |
2d93e133 VZ |
229 | wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."), |
230 | hr); | |
269a5a34 VZ |
231 | } |
232 | ||
46ccb510 | 233 | return wxDragError; |
269a5a34 VZ |
234 | } |
235 | } | |
236 | ||
237 | // Name : wxDropSource::GiveFeedback | |
238 | // Purpose : visually inform the user about d&d operation state | |
239 | // Returns : bool: true if we do all ourselves or false for default feedback | |
240 | // Params : [in] DragResult effect - what would happen if we dropped now | |
269a5a34 | 241 | // Notes : here we just leave this stuff for default implementation |
2d93e133 | 242 | bool wxDropSource::GiveFeedback(wxDragResult effect) |
269a5a34 | 243 | { |
2d93e133 VZ |
244 | const wxCursor& cursor = GetCursor(effect); |
245 | if ( cursor.Ok() ) | |
246 | { | |
247 | ::SetCursor((HCURSOR)cursor.GetHCURSOR()); | |
248 | ||
249 | return TRUE; | |
250 | } | |
251 | else | |
252 | { | |
253 | return FALSE; | |
254 | } | |
269a5a34 VZ |
255 | } |
256 | ||
46ccb510 | 257 | #endif //USE_DRAG_AND_DROP |