]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/dropsrc.cpp
Must strip menu codes from labels before measuring (fixes bug 1400552)
[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
269a5a34
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#if defined(__BORLANDC__)
24 #pragma hdrstop
25#endif
7dee726c
RS
26#ifndef WX_PRECOMP
27#include "wx/window.h"
28#endif
269a5a34 29
3f480da3 30#include "wx/setup.h"
269a5a34 31
21709999 32#if wxUSE_OLE && wxUSE_DRAG_AND_DROP
269a5a34 33
3f480da3 34#include "wx/log.h"
9e2896e5 35#include "wx/dnd.h"
269a5a34 36
4676948b 37#include "wx/msw/private.h"
9e2896e5 38
a7b4607f 39// for some compilers, the entire ole2.h must be included, not only oleauto.h
4676948b 40#if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
9e2896e5 41 #include <ole2.h>
7dee726c 42#endif
17b74d79 43
17b74d79
JS
44#include <oleauto.h>
45
3f480da3 46#include "wx/msw/ole/oleutils.h"
17b74d79 47
269a5a34
VZ
48// ----------------------------------------------------------------------------
49// wxIDropSource implementation of IDropSource interface
50// ----------------------------------------------------------------------------
51
52class wxIDropSource : public IDropSource
53{
54public:
55 wxIDropSource(wxDropSource *pDropSource);
cc11ded1 56 virtual ~wxIDropSource() { }
269a5a34
VZ
57
58 DECLARE_IUNKNOWN_METHODS;
59
60 // IDropSource
61 STDMETHODIMP QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState);
62 STDMETHODIMP GiveFeedback(DWORD dwEffect);
63
64private:
65 DWORD m_grfInitKeyState; // button which started the d&d operation
66 wxDropSource *m_pDropSource; // pointer to C++ class we belong to
22f3361e 67
cc11ded1 68 DECLARE_NO_COPY_CLASS(wxIDropSource)
269a5a34
VZ
69};
70
71// ============================================================================
72// Implementation
73// ============================================================================
74
75// ----------------------------------------------------------------------------
76// wxIDropSource implementation
77// ----------------------------------------------------------------------------
78BEGIN_IID_TABLE(wxIDropSource)
79 ADD_IID(Unknown)
80 ADD_IID(DropSource)
81END_IID_TABLE;
82
83IMPLEMENT_IUNKNOWN_METHODS(wxIDropSource)
84
85wxIDropSource::wxIDropSource(wxDropSource *pDropSource)
86{
87 wxASSERT( pDropSource != NULL );
88
89 m_pDropSource = pDropSource;
90 m_grfInitKeyState = 0;
269a5a34
VZ
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
103STDMETHODIMP 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
3f480da3 124// Returns : STDMETHODIMP
269a5a34
VZ
125// Params : [in] DWORD dwEffect - what would happen if we dropped now
126// Notes : default implementation is ok in more than 99% of cases
127STDMETHODIMP wxIDropSource::GiveFeedback(DWORD dwEffect)
128{
46ccb510 129 wxDragResult effect;
269a5a34 130 if ( dwEffect & DROPEFFECT_COPY )
46ccb510 131 effect = wxDragCopy;
269a5a34 132 else if ( dwEffect & DROPEFFECT_MOVE )
46ccb510 133 effect = wxDragMove;
269a5a34 134 else
46ccb510 135 effect = wxDragNone;
269a5a34 136
2d93e133 137 if ( m_pDropSource->GiveFeedback(effect) )
269a5a34
VZ
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
150void wxDropSource::Init()
151{
2d93e133
VZ
152 m_pIDropSource = new wxIDropSource(this);
153 m_pIDropSource->AddRef();
269a5a34
VZ
154}
155
8e193f38 156wxDropSource::wxDropSource(wxWindow* WXUNUSED(win),
2d93e133
VZ
157 const wxCursor &cursorCopy,
158 const wxCursor &cursorMove,
159 const wxCursor &cursorStop)
160 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
269a5a34 161{
2d93e133 162 Init();
269a5a34
VZ
163}
164
8e193f38
VZ
165wxDropSource::wxDropSource(wxDataObject& data,
166 wxWindow* WXUNUSED(win),
2d93e133
VZ
167 const wxCursor &cursorCopy,
168 const wxCursor &cursorMove,
169 const wxCursor &cursorStop)
170 : wxDropSourceBase(cursorCopy, cursorMove, cursorStop)
269a5a34 171{
2d93e133
VZ
172 Init();
173 SetData(data);
269a5a34
VZ
174}
175
269a5a34
VZ
176wxDropSource::~wxDropSource()
177{
2d93e133 178 m_pIDropSource->Release();
269a5a34
VZ
179}
180
181// Name : DoDragDrop
182// Purpose : start drag and drop operation
46ccb510 183// Returns : wxDragResult - the code of performed operation
2245b2b2 184// Params : [in] int flags: specifies if moving is allowe (or only copying)
269a5a34 185// Notes : you must call SetData() before if you had used def ctor
2245b2b2 186wxDragResult wxDropSource::DoDragDrop(int flags)
269a5a34 187{
9e2896e5 188 wxCHECK_MSG( m_data != NULL, wxDragNone, wxT("No data in wxDropSource!") );
269a5a34
VZ
189
190 DWORD dwEffect;
9e2896e5 191 HRESULT hr = ::DoDragDrop(m_data->GetInterface(),
3f480da3 192 m_pIDropSource,
2245b2b2
VZ
193 (flags & wxDrag_AllowMove)
194 ? DROPEFFECT_COPY | DROPEFFECT_MOVE
195 : DROPEFFECT_COPY,
269a5a34
VZ
196 &dwEffect);
197
198 if ( hr == DRAGDROP_S_CANCEL ) {
46ccb510 199 return wxDragCancel;
269a5a34
VZ
200 }
201 else if ( hr == DRAGDROP_S_DROP ) {
202 if ( dwEffect & DROPEFFECT_COPY ) {
46ccb510 203 return wxDragCopy;
269a5a34
VZ
204 }
205 else if ( dwEffect & DROPEFFECT_MOVE ) {
206 // consistency check: normally, we shouldn't get "move" at all
2245b2b2
VZ
207 // here if we don't allow it, but in practice it does happen quite often
208 return (flags & wxDrag_AllowMove) ? wxDragMove : wxDragCopy;
269a5a34
VZ
209 }
210 else {
211 // not copy or move
46ccb510 212 return wxDragNone;
269a5a34
VZ
213 }
214 }
215 else {
216 if ( FAILED(hr) ) {
161f4f73 217 wxLogApiError(wxT("DoDragDrop"), hr);
223d09f6 218 wxLogError(wxT("Drag & drop operation failed."));
269a5a34
VZ
219 }
220 else {
2d93e133
VZ
221 wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."),
222 hr);
269a5a34
VZ
223 }
224
46ccb510 225 return wxDragError;
269a5a34
VZ
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
269a5a34 233// Notes : here we just leave this stuff for default implementation
2d93e133 234bool wxDropSource::GiveFeedback(wxDragResult effect)
269a5a34 235{
2d93e133
VZ
236 const wxCursor& cursor = GetCursor(effect);
237 if ( cursor.Ok() )
238 {
239 ::SetCursor((HCURSOR)cursor.GetHCURSOR());
240
0a0e6a5b 241 return true;
2d93e133
VZ
242 }
243 else
244 {
0a0e6a5b 245 return false;
2d93e133 246 }
269a5a34
VZ
247}
248
46ccb510 249#endif //USE_DRAG_AND_DROP