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