1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/dropsrc.cpp
3 // Purpose: implementation of wxIDropSource and wxDropSource
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
27 #include "wx/window.h"
32 #if wxUSE_OLE && wxUSE_DRAG_AND_DROP
37 #include "wx/msw/private.h"
39 // for some compilers, the entire ole2.h must be included, not only oleauto.h
40 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
46 #include "wx/msw/ole/oleutils.h"
48 // ----------------------------------------------------------------------------
49 // wxIDropSource implementation of IDropSource interface
50 // ----------------------------------------------------------------------------
52 class wxIDropSource
: public IDropSource
55 wxIDropSource(wxDropSource
*pDropSource
);
56 virtual ~wxIDropSource() { }
58 DECLARE_IUNKNOWN_METHODS
;
61 STDMETHODIMP
QueryContinueDrag(BOOL fEscapePressed
, DWORD grfKeyState
);
62 STDMETHODIMP
GiveFeedback(DWORD dwEffect
);
65 DWORD m_grfInitKeyState
; // button which started the d&d operation
66 wxDropSource
*m_pDropSource
; // pointer to C++ class we belong to
68 DECLARE_NO_COPY_CLASS(wxIDropSource
)
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // wxIDropSource implementation
77 // ----------------------------------------------------------------------------
78 BEGIN_IID_TABLE(wxIDropSource
)
83 IMPLEMENT_IUNKNOWN_METHODS(wxIDropSource
)
85 wxIDropSource::wxIDropSource(wxDropSource
*pDropSource
)
87 wxASSERT( pDropSource
!= NULL
);
89 m_pDropSource
= pDropSource
;
90 m_grfInitKeyState
= 0;
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
,
106 if ( fEscapePressed
)
107 return DRAGDROP_S_CANCEL
;
109 // initialize ourself with the drag begin button
110 if ( m_grfInitKeyState
== 0 ) {
111 m_grfInitKeyState
= grfKeyState
& (MK_LBUTTON
| MK_RBUTTON
| MK_MBUTTON
);
114 if ( !(grfKeyState
& m_grfInitKeyState
) ) {
115 // button which started d&d released, go!
116 return DRAGDROP_S_DROP
;
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
)
130 if ( dwEffect
& DROPEFFECT_COPY
)
132 else if ( dwEffect
& DROPEFFECT_MOVE
)
137 if ( m_pDropSource
->GiveFeedback(effect
) )
140 return DRAGDROP_S_USEDEFAULTCURSORS
;
143 // ----------------------------------------------------------------------------
144 // wxDropSource implementation
145 // ----------------------------------------------------------------------------
149 // common part of all ctors
150 void wxDropSource::Init()
152 m_pIDropSource
= new wxIDropSource(this);
153 m_pIDropSource
->AddRef();
156 wxDropSource::wxDropSource(wxWindow
* WXUNUSED(win
),
157 const wxCursor
&cursorCopy
,
158 const wxCursor
&cursorMove
,
159 const wxCursor
&cursorStop
)
160 : wxDropSourceBase(cursorCopy
, cursorMove
, cursorStop
)
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
)
176 wxDropSource::~wxDropSource()
178 m_pIDropSource
->Release();
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
)
188 wxCHECK_MSG( m_data
!= NULL
, wxDragNone
, wxT("No data in wxDropSource!") );
191 HRESULT hr
= ::DoDragDrop(m_data
->GetInterface(),
193 (flags
& wxDrag_AllowMove
)
194 ? DROPEFFECT_COPY
| DROPEFFECT_MOVE
198 if ( hr
== DRAGDROP_S_CANCEL
) {
201 else if ( hr
== DRAGDROP_S_DROP
) {
202 if ( dwEffect
& DROPEFFECT_COPY
) {
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
;
217 wxLogApiError(wxT("DoDragDrop"), hr
);
218 wxLogError(wxT("Drag & drop operation failed."));
221 wxLogDebug(wxT("Unexpected success return code %08lx from DoDragDrop."),
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
)
236 const wxCursor
& cursor
= GetCursor(effect
);
239 ::SetCursor((HCURSOR
)cursor
.GetHCURSOR());
249 #endif //USE_DRAG_AND_DROP