]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dropsrc.cpp
merged 2.4 branch into the trunk
[wxWidgets.git] / src / msw / ole / dropsrc.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/dropsrc.cpp
3 // Purpose: implementation of wxIDropSource and wxDropSource
4 // Author: Vadim Zeitlin
5 // Modified by:
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
30 #ifndef WX_PRECOMP
31 #include "wx/window.h"
32 #endif
33
34 #include "wx/setup.h"
35
36 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP
37
38 #include "wx/log.h"
39 #include "wx/dnd.h"
40
41 #include <windows.h>
42
43 #if wxUSE_NORLANDER_HEADERS
44 #include <ole2.h>
45 #endif
46
47 #ifndef __WIN32__
48 #include <ole2.h>
49 #include <olestd.h>
50 #endif
51
52 #include <oleauto.h>
53
54 #include "wx/msw/ole/oleutils.h"
55
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 }
97
98 // Name : wxIDropSource::QueryContinueDrag
99 // Purpose : decide if drag operation must be continued or not
100 // Returns : HRESULT: S_OK if we should continue
101 // DRAGDROP_S_DROP to drop right now
102 // DRAGDROP_S_CANCEL to cancel everything
103 // Params : [in] BOOL fEscapePressed <Esc> pressed since last call?
104 // [in] DWORD grfKeyState mask containing state of kbd keys
105 // Notes : as there is no reasonably simple portable way to implement this
106 // function, we currently don't give the possibility to override the
107 // default behaviour implemented here
108 STDMETHODIMP wxIDropSource::QueryContinueDrag(BOOL fEscapePressed,
109 DWORD grfKeyState)
110 {
111 if ( fEscapePressed )
112 return DRAGDROP_S_CANCEL;
113
114 // initialize ourself with the drag begin button
115 if ( m_grfInitKeyState == 0 ) {
116 m_grfInitKeyState = grfKeyState & (MK_LBUTTON | MK_RBUTTON | MK_MBUTTON);
117 }
118
119 if ( !(grfKeyState & m_grfInitKeyState) ) {
120 // button which started d&d released, go!
121 return DRAGDROP_S_DROP;
122 }
123
124 return S_OK;
125 }
126
127 // Name : wxIDropSource::GiveFeedback
128 // Purpose : give UI feedback according to current state of operation
129 // Returns : STDMETHODIMP
130 // Params : [in] DWORD dwEffect - what would happen if we dropped now
131 // Notes : default implementation is ok in more than 99% of cases
132 STDMETHODIMP wxIDropSource::GiveFeedback(DWORD dwEffect)
133 {
134 wxDragResult effect;
135 if ( dwEffect & DROPEFFECT_COPY )
136 effect = wxDragCopy;
137 else if ( dwEffect & DROPEFFECT_MOVE )
138 effect = wxDragMove;
139 else
140 effect = wxDragNone;
141
142 if ( m_pDropSource->GiveFeedback(effect) )
143 return S_OK;
144
145 return DRAGDROP_S_USEDEFAULTCURSORS;
146 }
147
148 // ----------------------------------------------------------------------------
149 // wxDropSource implementation
150 // ----------------------------------------------------------------------------
151
152 // ctors
153
154 // common part of all ctors
155 void wxDropSource::Init()
156 {
157 m_pIDropSource = new wxIDropSource(this);
158 m_pIDropSource->AddRef();
159 }
160
161 wxDropSource::wxDropSource(wxWindow* WXUNUSED(win),
162 const wxCursor &cursorCopy,
163 const wxCursor &cursorMove,
164 const wxCursor &cursorStop)
165 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
166 {
167 Init();
168 }
169
170 wxDropSource::wxDropSource(wxDataObject& data,
171 wxWindow* WXUNUSED(win),
172 const wxCursor &cursorCopy,
173 const wxCursor &cursorMove,
174 const wxCursor &cursorStop)
175 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
176 {
177 Init();
178 SetData(data);
179 }
180
181 wxDropSource::~wxDropSource()
182 {
183 m_pIDropSource->Release();
184 }
185
186 // Name : DoDragDrop
187 // Purpose : start drag and drop operation
188 // Returns : wxDragResult - the code of performed operation
189 // Params : [in] int flags: specifies if moving is allowe (or only copying)
190 // Notes : you must call SetData() before if you had used def ctor
191 wxDragResult wxDropSource::DoDragDrop(int flags)
192 {
193 wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") );
194
195 DWORD dwEffect;
196 HRESULT hr = ::DoDragDrop(m_data->GetInterface(),
197 m_pIDropSource,
198 (flags & wxDrag_AllowMove)
199 ? DROPEFFECT_COPY | DROPEFFECT_MOVE
200 : DROPEFFECT_COPY,
201 &dwEffect);
202
203 if ( hr == DRAGDROP_S_CANCEL ) {
204 return wxDragCancel;
205 }
206 else if ( hr == DRAGDROP_S_DROP ) {
207 if ( dwEffect & DROPEFFECT_COPY ) {
208 return wxDragCopy;
209 }
210 else if ( dwEffect & DROPEFFECT_MOVE ) {
211 // consistency check: normally, we shouldn't get "move" at all
212 // here if we don't allow it, but in practice it does happen quite often
213 return (flags & wxDrag_AllowMove) ? wxDragMove : wxDragCopy;
214 }
215 else {
216 // not copy or move
217 return wxDragNone;
218 }
219 }
220 else {
221 if ( FAILED(hr) ) {
222 wxLogApiError(wxT("DoDragDrop"), hr);
223 wxLogError(wxT("Drag & drop operation failed."));
224 }
225 else {
226 wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."),
227 hr);
228 }
229
230 return wxDragError;
231 }
232 }
233
234 // Name : wxDropSource::GiveFeedback
235 // Purpose : visually inform the user about d&d operation state
236 // Returns : bool: true if we do all ourselves or false for default feedback
237 // Params : [in] DragResult effect - what would happen if we dropped now
238 // Notes : here we just leave this stuff for default implementation
239 bool wxDropSource::GiveFeedback(wxDragResult effect)
240 {
241 const wxCursor& cursor = GetCursor(effect);
242 if ( cursor.Ok() )
243 {
244 ::SetCursor((HCURSOR)cursor.GetHCURSOR());
245
246 return TRUE;
247 }
248 else
249 {
250 return FALSE;
251 }
252 }
253
254 #endif //USE_DRAG_AND_DROP