1 // IDataObjectImpl.h: interface for the CIDataObjectImpl class.
2 /**************************************************************************
3 THIS CODE AND INFORMATION IS PROVIDED 'AS IS' WITHOUT WARRANTY OF
4 ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
5 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
7 Author: Leon Finker 1/2001
8 **************************************************************************/
9 #ifndef __DRAGDROPIMPL_H__
10 #define __DRAGDROPIMPL_H__
11 //#include <ShlDisp.h>
15 ///////////////////////////////////////////////////////////////////////////////////////////////
16 class CDragSourceNotify
: IDropSourceNotify
23 CDragSourceNotify(void)
28 ~CDragSourceNotify(void)
32 HRESULT STDMETHODCALLTYPE
QueryInterface(REFIID riid
, void** ppvObject
)
39 if (IsEqualIID(riid
, IID_IUnknown
))
41 *ppvObject
= static_cast<IUnknown
*>(this);
43 else if (IsEqualIID(riid
, IID_IDropSourceNotify
))
45 *ppvObject
= static_cast<IDropSourceNotify
*>(this);
57 ULONG STDMETHODCALLTYPE
AddRef(void)
59 return InterlockedIncrement(&refCount
);
62 ULONG STDMETHODCALLTYPE
Release(void)
64 ULONG ret
= InterlockedDecrement(&refCount
);
71 HRESULT STDMETHODCALLTYPE
DragEnterTarget(HWND
/*hWndTarget*/)
76 HRESULT STDMETHODCALLTYPE
DragLeaveTarget(void)
84 ///////////////////////////////////////////////////////////////////////////////////////////////
85 class CEnumFormatEtc
: public IEnumFORMATETC
89 CSimpleArray
<FORMATETC
> m_pFmtEtc
;
93 CEnumFormatEtc(const CSimpleArray
<FORMATETC
>& ArrFE
);
94 CEnumFormatEtc(const CSimpleArray
<FORMATETC
*>& ArrFE
);
96 STDMETHOD(QueryInterface
)(REFIID
, void FAR
* FAR
*);
97 STDMETHOD_(ULONG
, AddRef
)(void);
98 STDMETHOD_(ULONG
, Release
)(void);
100 //IEnumFORMATETC members
101 STDMETHOD(Next
)(ULONG
, LPFORMATETC
, ULONG FAR
*);
102 STDMETHOD(Skip
)(ULONG
);
103 STDMETHOD(Reset
)(void);
104 STDMETHOD(Clone
)(IEnumFORMATETC FAR
* FAR
*);
107 ///////////////////////////////////////////////////////////////////////////////////////////////
108 class CIDropSource
: public IDropSource
113 IDataObject
* m_pIDataObj
;
114 CDragSourceNotify
* pDragSourceNotify
;
117 CIDropSource():m_cRefCount(0),m_bDropped(false),m_pIDataObj(NULL
)
119 pDragSourceNotify
= new CDragSourceNotify();
120 pDragSourceNotify
->AddRef();
126 m_pIDataObj
->Release();
129 if (pDragSourceNotify
)
131 pDragSourceNotify
->Release();
132 pDragSourceNotify
= NULL
;
136 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(
137 /* [in] */ REFIID riid
,
138 /* [iid_is][out] */ void __RPC_FAR
*__RPC_FAR
*ppvObject
);
139 virtual ULONG STDMETHODCALLTYPE
AddRef( void);
140 virtual ULONG STDMETHODCALLTYPE
Release( void);
142 virtual HRESULT STDMETHODCALLTYPE
QueryContinueDrag(
143 /* [in] */ BOOL fEscapePressed
,
144 /* [in] */ DWORD grfKeyState
);
146 virtual HRESULT STDMETHODCALLTYPE
GiveFeedback(
147 /* [in] */ DWORD dwEffect
);
150 ///////////////////////////////////////////////////////////////////////////////////////////////
151 class CIDataObject
: public IDataObject
//,public IAsyncOperation
153 CIDropSource
* m_pDropSource
;
155 CSimpleArray
<FORMATETC
*> m_ArrFormatEtc
;
156 CSimpleArray
<STGMEDIUM
*> m_StgMedium
;
158 CIDataObject(CIDropSource
* pDropSource
);
160 void CopyMedium(STGMEDIUM
* pMedDest
, STGMEDIUM
* pMedSrc
, FORMATETC
* pFmtSrc
);
162 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(
163 /* [in] */ REFIID riid
,
164 /* [iid_is][out] */ void __RPC_FAR
*__RPC_FAR
*ppvObject
);
165 virtual ULONG STDMETHODCALLTYPE
AddRef( void);
166 virtual ULONG STDMETHODCALLTYPE
Release( void);
169 virtual /* [local] */ HRESULT STDMETHODCALLTYPE
GetData(
170 /* [unique][in] */ FORMATETC __RPC_FAR
*pformatetcIn
,
171 /* [out] */ STGMEDIUM __RPC_FAR
*pmedium
);
173 virtual /* [local] */ HRESULT STDMETHODCALLTYPE
GetDataHere(
174 /* [unique][in] */ FORMATETC __RPC_FAR
*pformatetc
,
175 /* [out][in] */ STGMEDIUM __RPC_FAR
*pmedium
);
177 virtual HRESULT STDMETHODCALLTYPE
QueryGetData(
178 /* [unique][in] */ FORMATETC __RPC_FAR
*pformatetc
);
180 virtual HRESULT STDMETHODCALLTYPE
GetCanonicalFormatEtc(
181 /* [unique][in] */ FORMATETC __RPC_FAR
*pformatectIn
,
182 /* [out] */ FORMATETC __RPC_FAR
*pformatetcOut
);
184 virtual /* [local] */ HRESULT STDMETHODCALLTYPE
SetData(
185 /* [unique][in] */ FORMATETC __RPC_FAR
*pformatetc
,
186 /* [unique][in] */ STGMEDIUM __RPC_FAR
*pmedium
,
187 /* [in] */ BOOL fRelease
);
189 virtual HRESULT STDMETHODCALLTYPE
EnumFormatEtc(
190 /* [in] */ DWORD dwDirection
,
191 /* [out] */ IEnumFORMATETC __RPC_FAR
*__RPC_FAR
*ppenumFormatEtc
);
193 virtual HRESULT STDMETHODCALLTYPE
DAdvise(
194 /* [in] */ FORMATETC __RPC_FAR
*pformatetc
,
195 /* [in] */ DWORD advf
,
196 /* [unique][in] */ IAdviseSink __RPC_FAR
*pAdvSink
,
197 /* [out] */ DWORD __RPC_FAR
*pdwConnection
);
199 virtual HRESULT STDMETHODCALLTYPE
DUnadvise(
200 /* [in] */ DWORD dwConnection
);
202 virtual HRESULT STDMETHODCALLTYPE
EnumDAdvise(
203 /* [out] */ IEnumSTATDATA __RPC_FAR
*__RPC_FAR
*ppenumAdvise
);
206 //virtual HRESULT STDMETHODCALLTYPE SetAsyncMode(
207 // /* [in] */ BOOL fDoOpAsync)
212 //virtual HRESULT STDMETHODCALLTYPE GetAsyncMode(
213 // /* [out] */ BOOL __RPC_FAR *pfIsOpAsync)
218 //virtual HRESULT STDMETHODCALLTYPE StartOperation(
219 // /* [optional][unique][in] */ IBindCtx __RPC_FAR *pbcReserved)
224 //virtual HRESULT STDMETHODCALLTYPE InOperation(
225 // /* [out] */ BOOL __RPC_FAR *pfInAsyncOp)
230 //virtual HRESULT STDMETHODCALLTYPE EndOperation(
231 // /* [in] */ HRESULT hResult,
232 // /* [unique][in] */ IBindCtx __RPC_FAR *pbcReserved,
233 // /* [in] */ DWORD dwEffects)
240 HRESULT
SetDropDescription(DROPIMAGETYPE image
, LPCTSTR format
, LPCTSTR insert
);
243 ///////////////////////////////////////////////////////////////////////////////////////////////
244 class CIDropTarget
: public IDropTarget
248 IDropTargetHelper
*m_pDropTargetHelper
;
249 CSimpleArray
<FORMATETC
> m_formatetc
;
250 FORMATETC
* m_pSupportedFrmt
;
253 IDataObject
* m_pIDataObject
;
256 CIDropTarget(HWND m_hTargetWnd
);
257 virtual ~CIDropTarget();
258 void AddSuportedFormat(FORMATETC
& ftetc
) { m_formatetc
.Add(ftetc
); }
260 //return values: true - release the medium. false - don't release the medium
261 virtual bool OnDrop(FORMATETC
* pFmtEtc
, STGMEDIUM
& medium
,DWORD
*pdwEffect
, POINTL pt
) = 0;
263 virtual HRESULT STDMETHODCALLTYPE
QueryInterface(
264 /* [in] */ REFIID riid
,
265 /* [iid_is][out] */ void __RPC_FAR
*__RPC_FAR
*ppvObject
);
266 virtual ULONG STDMETHODCALLTYPE
AddRef( void) { ATLTRACE("CIDropTarget::AddRef\n"); return ++m_cRefCount
; }
267 virtual ULONG STDMETHODCALLTYPE
Release( void);
269 bool QueryDrop(DWORD grfKeyState
, LPDWORD pdwEffect
);
270 virtual HRESULT STDMETHODCALLTYPE
DragEnter(
271 /* [unique][in] */ IDataObject __RPC_FAR
*pDataObj
,
272 /* [in] */ DWORD grfKeyState
,
273 /* [in] */ POINTL pt
,
274 /* [out][in] */ DWORD __RPC_FAR
*pdwEffect
);
275 virtual HRESULT STDMETHODCALLTYPE
DragOver(
276 /* [in] */ DWORD grfKeyState
,
277 /* [in] */ POINTL pt
,
278 /* [out][in] */ DWORD __RPC_FAR
*pdwEffect
);
279 virtual HRESULT STDMETHODCALLTYPE
DragLeave( void);
280 virtual HRESULT STDMETHODCALLTYPE
Drop(
281 /* [unique][in] */ IDataObject __RPC_FAR
*pDataObj
,
282 /* [in] */ DWORD grfKeyState
,
283 /* [in] */ POINTL pt
,
284 /* [out][in] */ DWORD __RPC_FAR
*pdwEffect
);
287 HRESULT
SetDropDescription(DROPIMAGETYPE image
, LPCTSTR format
, LPCTSTR insert
);
290 class CDragSourceHelper
292 IDragSourceHelper2
* pDragSourceHelper2
;
293 IDragSourceHelper
* pDragSourceHelper
;
298 pDragSourceHelper
= NULL
;
299 pDragSourceHelper2
= NULL
;
300 if(FAILED(CoCreateInstance(CLSID_DragDropHelper
,
302 CLSCTX_INPROC_SERVER
,
303 IID_IDragSourceHelper2
,
304 (void**)&pDragSourceHelper2
)))
306 pDragSourceHelper2
= NULL
;
307 if(FAILED(CoCreateInstance(CLSID_DragDropHelper
,
309 CLSCTX_INPROC_SERVER
,
310 IID_IDragSourceHelper
,
311 (void**)&pDragSourceHelper
)))
312 pDragSourceHelper
= NULL
;
315 virtual ~CDragSourceHelper()
317 if( pDragSourceHelper2
!= NULL
)
319 pDragSourceHelper2
->Release();
320 pDragSourceHelper2
=NULL
;
322 if( pDragSourceHelper
!= NULL
)
324 pDragSourceHelper
->Release();
325 pDragSourceHelper
=NULL
;
330 HRESULT
InitializeFromBitmap(HBITMAP hBitmap
,
331 POINT
& pt
, // cursor position in client coords of the window
332 RECT
& rc
, // selected item's bounding rect
333 IDataObject
* pDataObject
,
334 BOOL allowDropDescription
=TRUE
,
335 COLORREF crColorKey
=GetSysColor(COLOR_WINDOW
)// color of the window used for transparent effect.
338 if((pDragSourceHelper
== NULL
)&&(pDragSourceHelper2
== NULL
))
341 if ((allowDropDescription
)&&(pDragSourceHelper2
))
342 pDragSourceHelper2
->SetFlags(DSH_ALLOWDROPDESCRIPTIONTEXT
);
346 GetObject(hBitmap
, sizeof(bm
), &bm
);
347 di
.sizeDragImage
.cx
= bm
.bmWidth
;
348 di
.sizeDragImage
.cy
= bm
.bmHeight
;
349 di
.hbmpDragImage
= hBitmap
;
350 di
.crColorKey
= crColorKey
;
351 di
.ptOffset
.x
= pt
.x
- rc
.left
;
352 di
.ptOffset
.y
= pt
.y
- rc
.top
;
353 if (pDragSourceHelper2
)
354 return pDragSourceHelper2
->InitializeFromBitmap(&di
, pDataObject
);
355 if (pDragSourceHelper
== NULL
)
357 return pDragSourceHelper
->InitializeFromBitmap(&di
, pDataObject
);
359 HRESULT
InitializeFromWindow(HWND hwnd
, POINT
& pt
, IDataObject
* pDataObject
, BOOL allowDropDescription
=TRUE
)
361 if((pDragSourceHelper
== NULL
)&&(pDragSourceHelper2
== NULL
))
363 if ((allowDropDescription
)&&(pDragSourceHelper2
))
364 pDragSourceHelper2
->SetFlags(DSH_ALLOWDROPDESCRIPTIONTEXT
);
365 if (pDragSourceHelper2
)
366 return pDragSourceHelper2
->InitializeFromWindow(hwnd
, &pt
, pDataObject
);
367 if (pDragSourceHelper
== NULL
)
369 return pDragSourceHelper
->InitializeFromWindow(hwnd
, &pt
, pDataObject
);
372 #endif //__DRAGDROPIMPL_H__