]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/dropsrc.cpp
fixed bug #976725: "RETURN closes dialog instead of combobox dropdown" (applied fix...
[wxWidgets.git] / src / msw / ole / dropsrc.cpp
CommitLineData
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>
65571936 9// Licence: wxWindows licence
269a5a34
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
269a5a34
VZ
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
4676948b 41#include "wx/msw/private.h"
9e2896e5 42
a7b4607f 43// for some compilers, the entire ole2.h must be included, not only oleauto.h
4676948b 44#if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
9e2896e5 45 #include <ole2.h>
7dee726c 46#endif
17b74d79 47
17b74d79
JS
48#include <oleauto.h>
49
3f480da3 50#include "wx/msw/ole/oleutils.h"
17b74d79 51
269a5a34
VZ
52// ----------------------------------------------------------------------------
53// wxIDropSource implementation of IDropSource interface
54// ----------------------------------------------------------------------------
55
56class wxIDropSource : public IDropSource
57{
58public:
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
67private:
68 DWORD m_grfInitKeyState; // button which started the d&d operation
69 wxDropSource *m_pDropSource; // pointer to C++ class we belong to
22f3361e
VZ
70
71 DECLARE_NO_COPY_CLASS(wxIDropSource)
269a5a34
VZ
72};
73
74// ============================================================================
75// Implementation
76// ============================================================================
77
78// ----------------------------------------------------------------------------
79// wxIDropSource implementation
80// ----------------------------------------------------------------------------
81BEGIN_IID_TABLE(wxIDropSource)
82 ADD_IID(Unknown)
83 ADD_IID(DropSource)
84END_IID_TABLE;
85
86IMPLEMENT_IUNKNOWN_METHODS(wxIDropSource)
87
88wxIDropSource::wxIDropSource(wxDropSource *pDropSource)
89{
90 wxASSERT( pDropSource != NULL );
91
92 m_pDropSource = pDropSource;
93 m_grfInitKeyState = 0;
269a5a34
VZ
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
106STDMETHODIMP 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
3f480da3 127// Returns : STDMETHODIMP
269a5a34
VZ
128// Params : [in] DWORD dwEffect - what would happen if we dropped now
129// Notes : default implementation is ok in more than 99% of cases
130STDMETHODIMP wxIDropSource::GiveFeedback(DWORD dwEffect)
131{
46ccb510 132 wxDragResult effect;
269a5a34 133 if ( dwEffect & DROPEFFECT_COPY )
46ccb510 134 effect = wxDragCopy;
269a5a34 135 else if ( dwEffect & DROPEFFECT_MOVE )
46ccb510 136 effect = wxDragMove;
269a5a34 137 else
46ccb510 138 effect = wxDragNone;
269a5a34 139
2d93e133 140 if ( m_pDropSource->GiveFeedback(effect) )
269a5a34
VZ
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
153void wxDropSource::Init()
154{
2d93e133
VZ
155 m_pIDropSource = new wxIDropSource(this);
156 m_pIDropSource->AddRef();
269a5a34
VZ
157}
158
8e193f38 159wxDropSource::wxDropSource(wxWindow* WXUNUSED(win),
2d93e133
VZ
160 const wxCursor &cursorCopy,
161 const wxCursor &cursorMove,
162 const wxCursor &cursorStop)
163 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
269a5a34 164{
2d93e133 165 Init();
269a5a34
VZ
166}
167
8e193f38
VZ
168wxDropSource::wxDropSource(wxDataObject& data,
169 wxWindow* WXUNUSED(win),
2d93e133
VZ
170 const wxCursor &cursorCopy,
171 const wxCursor &cursorMove,
172 const wxCursor &cursorStop)
173 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
269a5a34 174{
2d93e133
VZ
175 Init();
176 SetData(data);
269a5a34
VZ
177}
178
269a5a34
VZ
179wxDropSource::~wxDropSource()
180{
2d93e133 181 m_pIDropSource->Release();
269a5a34
VZ
182}
183
184// Name : DoDragDrop
185// Purpose : start drag and drop operation
46ccb510 186// Returns : wxDragResult - the code of performed operation
2245b2b2 187// Params : [in] int flags: specifies if moving is allowe (or only copying)
269a5a34 188// Notes : you must call SetData() before if you had used def ctor
2245b2b2 189wxDragResult wxDropSource::DoDragDrop(int flags)
269a5a34 190{
9e2896e5 191 wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") );
269a5a34
VZ
192
193 DWORD dwEffect;
9e2896e5 194 HRESULT hr = ::DoDragDrop(m_data->GetInterface(),
3f480da3 195 m_pIDropSource,
2245b2b2
VZ
196 (flags & wxDrag_AllowMove)
197 ? DROPEFFECT_COPY | DROPEFFECT_MOVE
198 : DROPEFFECT_COPY,
269a5a34
VZ
199 &dwEffect);
200
201 if ( hr == DRAGDROP_S_CANCEL ) {
46ccb510 202 return wxDragCancel;
269a5a34
VZ
203 }
204 else if ( hr == DRAGDROP_S_DROP ) {
205 if ( dwEffect & DROPEFFECT_COPY ) {
46ccb510 206 return wxDragCopy;
269a5a34
VZ
207 }
208 else if ( dwEffect & DROPEFFECT_MOVE ) {
209 // consistency check: normally, we shouldn't get "move" at all
2245b2b2
VZ
210 // here if we don't allow it, but in practice it does happen quite often
211 return (flags & wxDrag_AllowMove) ? wxDragMove : wxDragCopy;
269a5a34
VZ
212 }
213 else {
214 // not copy or move
46ccb510 215 return wxDragNone;
269a5a34
VZ
216 }
217 }
218 else {
219 if ( FAILED(hr) ) {
161f4f73 220 wxLogApiError(wxT("DoDragDrop"), hr);
223d09f6 221 wxLogError(wxT("Drag & drop operation failed."));
269a5a34
VZ
222 }
223 else {
2d93e133
VZ
224 wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."),
225 hr);
269a5a34
VZ
226 }
227
46ccb510 228 return wxDragError;
269a5a34
VZ
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
269a5a34 236// Notes : here we just leave this stuff for default implementation
2d93e133 237bool wxDropSource::GiveFeedback(wxDragResult effect)
269a5a34 238{
2d93e133
VZ
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 }
269a5a34
VZ
250}
251
46ccb510 252#endif //USE_DRAG_AND_DROP