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