]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/ole/dropsrc.cpp
TRUE not true, FALSE not false
[wxWidgets.git] / src / msw / ole / dropsrc.cpp
... / ...
CommitLineData
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
60class wxIDropSource : public IDropSource
61{
62public:
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
71private:
72 DWORD m_grfInitKeyState; // button which started the d&d operation
73 wxDropSource *m_pDropSource; // pointer to C++ class we belong to
74
75 DECLARE_NO_COPY_CLASS(wxIDropSource)
76};
77
78// ============================================================================
79// Implementation
80// ============================================================================
81
82// ----------------------------------------------------------------------------
83// wxIDropSource implementation
84// ----------------------------------------------------------------------------
85BEGIN_IID_TABLE(wxIDropSource)
86 ADD_IID(Unknown)
87 ADD_IID(DropSource)
88END_IID_TABLE;
89
90IMPLEMENT_IUNKNOWN_METHODS(wxIDropSource)
91
92wxIDropSource::wxIDropSource(wxDropSource *pDropSource)
93{
94 wxASSERT( pDropSource != NULL );
95
96 m_pDropSource = pDropSource;
97 m_grfInitKeyState = 0;
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
110STDMETHODIMP 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
131// Returns : STDMETHODIMP
132// Params : [in] DWORD dwEffect - what would happen if we dropped now
133// Notes : default implementation is ok in more than 99% of cases
134STDMETHODIMP wxIDropSource::GiveFeedback(DWORD dwEffect)
135{
136 wxDragResult effect;
137 if ( dwEffect & DROPEFFECT_COPY )
138 effect = wxDragCopy;
139 else if ( dwEffect & DROPEFFECT_MOVE )
140 effect = wxDragMove;
141 else
142 effect = wxDragNone;
143
144 if ( m_pDropSource->GiveFeedback(effect) )
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
157void wxDropSource::Init()
158{
159 m_pIDropSource = new wxIDropSource(this);
160 m_pIDropSource->AddRef();
161}
162
163wxDropSource::wxDropSource(wxWindow* WXUNUSED(win),
164 const wxCursor &cursorCopy,
165 const wxCursor &cursorMove,
166 const wxCursor &cursorStop)
167 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
168{
169 Init();
170}
171
172wxDropSource::wxDropSource(wxDataObject& data,
173 wxWindow* WXUNUSED(win),
174 const wxCursor &cursorCopy,
175 const wxCursor &cursorMove,
176 const wxCursor &cursorStop)
177 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
178{
179 Init();
180 SetData(data);
181}
182
183wxDropSource::~wxDropSource()
184{
185 m_pIDropSource->Release();
186}
187
188// Name : DoDragDrop
189// Purpose : start drag and drop operation
190// Returns : wxDragResult - the code of performed operation
191// Params : [in] int flags: specifies if moving is allowe (or only copying)
192// Notes : you must call SetData() before if you had used def ctor
193wxDragResult wxDropSource::DoDragDrop(int flags)
194{
195 wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") );
196
197 DWORD dwEffect;
198 HRESULT hr = ::DoDragDrop(m_data->GetInterface(),
199 m_pIDropSource,
200 (flags & wxDrag_AllowMove)
201 ? DROPEFFECT_COPY | DROPEFFECT_MOVE
202 : DROPEFFECT_COPY,
203 &dwEffect);
204
205 if ( hr == DRAGDROP_S_CANCEL ) {
206 return wxDragCancel;
207 }
208 else if ( hr == DRAGDROP_S_DROP ) {
209 if ( dwEffect & DROPEFFECT_COPY ) {
210 return wxDragCopy;
211 }
212 else if ( dwEffect & DROPEFFECT_MOVE ) {
213 // consistency check: normally, we shouldn't get "move" at all
214 // here if we don't allow it, but in practice it does happen quite often
215 return (flags & wxDrag_AllowMove) ? wxDragMove : wxDragCopy;
216 }
217 else {
218 // not copy or move
219 return wxDragNone;
220 }
221 }
222 else {
223 if ( FAILED(hr) ) {
224 wxLogApiError(wxT("DoDragDrop"), hr);
225 wxLogError(wxT("Drag & drop operation failed."));
226 }
227 else {
228 wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."),
229 hr);
230 }
231
232 return wxDragError;
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
240// Notes : here we just leave this stuff for default implementation
241bool wxDropSource::GiveFeedback(wxDragResult effect)
242{
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 }
254}
255
256#endif //USE_DRAG_AND_DROP