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