4 * Copyright 2006 - 2007 Thomas Weidenmueller <w3seek@reactos.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 public CComCoClass
<CDropTarget
>,
25 public CComObjectRootEx
<CComMultiThreadModelNoCS
>,
29 CComPtr
<IDropTargetHelper
> DropTargetHelper
;
32 DROPTARGET_CALLBACKS Callbacks
;
37 FindSupportedFormat(IN IDataObject
*pDataObject
)
39 FORMATETC
*Current
, *Last
;
42 /* NOTE: we could use IDataObject::EnumFormatEtc(),
43 but this appears to be a lot easier! */
44 Last
= Formats
+ FormatsCount
;
45 for (Current
= Formats
;
49 hr
= pDataObject
->QueryGetData(Current
);
65 ZeroMemory(&Callbacks
, sizeof(Callbacks
));
68 virtual ~CDropTarget() { }
70 HRESULT
Initialize(IN HWND hwndTarget
,
71 IN DWORD nSupportedFormats
,
72 IN
const FORMATETC
*formats OPTIONAL
,
73 IN PVOID Context OPTIONAL
,
74 IN
const DROPTARGET_CALLBACKS
*Callbacks OPTIONAL
)
76 this->hwndTarget
= hwndTarget
;
77 FormatsCount
= nSupportedFormats
;
78 if (nSupportedFormats
!= 0)
80 Formats
= new FORMATETC
[nSupportedFormats
];
83 sizeof(formats
[0]) * nSupportedFormats
);
86 this->Context
= Context
;
87 if (Callbacks
!= NULL
)
89 CopyMemory(&this->Callbacks
,
94 HRESULT hr
= CoCreateInstance(CLSID_DragDropHelper
,
97 IID_PPV_ARG(IDropTargetHelper
, &DropTargetHelper
));
102 virtual HRESULT STDMETHODCALLTYPE
DragEnter(
103 IN IDataObject
*pDataObject
,
104 IN DWORD grfKeyState
,
106 IN OUT DWORD
*pdwEffect
)
108 const FORMATETC
*Format
;
111 if (pDataObject
== NULL
)
116 hr
= DropTargetHelper
->DragEnter(
124 Format
= FindSupportedFormat(
128 /* We found a format that we support! */
129 if (Callbacks
.OnDragEnter
!= NULL
)
131 hr
= Callbacks
.OnDragEnter(this,
143 /* Special return value by the callback routine,
144 doesn't want to allow dragging */
145 *pdwEffect
= DROPEFFECT_NONE
;
152 *pdwEffect
= DROPEFFECT_NONE
;
157 *pdwEffect
= DROPEFFECT_NONE
;
160 *pdwEffect
= DROPEFFECT_NONE
;
166 virtual HRESULT STDMETHODCALLTYPE
DragOver(
167 IN DWORD grfKeyState
,
169 IN OUT DWORD
*pdwEffect
)
173 hr
= DropTargetHelper
->DragOver(
181 if (Callbacks
.OnDragOver
!= NULL
)
183 hr
= Callbacks
.OnDragOver(this,
192 /* Special return value by the callback routine,
193 doesn't want to allow dropping here */
194 *pdwEffect
= DROPEFFECT_NONE
;
201 *pdwEffect
= DROPEFFECT_NONE
;
206 *pdwEffect
= DROPEFFECT_NONE
;
209 *pdwEffect
= DROPEFFECT_NONE
;
215 virtual HRESULT STDMETHODCALLTYPE
DragLeave()
219 hr
= DropTargetHelper
->DragLeave();
222 if (Callbacks
.OnDragLeave
!= NULL
)
224 hr
= Callbacks
.OnDragLeave(this,
232 virtual HRESULT STDMETHODCALLTYPE
Drop(
233 IN IDataObject
*pDataObject
,
234 IN DWORD grfKeyState
,
236 IN OUT DWORD
*pdwEffect
)
238 const FORMATETC
*Format
;
241 if (pDataObject
== NULL
)
244 hr
= DropTargetHelper
->Drop(
249 if (SUCCEEDED(hr
) && CanDrop
)
251 Format
= FindSupportedFormat(pDataObject
);
254 /* We found a format that we support! */
255 if (Callbacks
.OnDrop
!= NULL
)
257 hr
= Callbacks
.OnDrop(this,
269 /* Special return value by the callback routine,
270 doesn't want to allow dragging */
271 *pdwEffect
= DROPEFFECT_NONE
;
278 *pdwEffect
= DROPEFFECT_NONE
;
283 *pdwEffect
= DROPEFFECT_NONE
;
286 *pdwEffect
= DROPEFFECT_NONE
;
292 DECLARE_NOT_AGGREGATABLE(CDropTarget
)
294 DECLARE_PROTECT_FINAL_CONSTRUCT()
295 BEGIN_COM_MAP(CDropTarget
)
296 COM_INTERFACE_ENTRY_IID(IID_IDropTarget
, IDropTarget
)
301 CreateDropTarget(IN HWND hwndTarget
,
302 IN DWORD nSupportedFormats
,
303 IN
const FORMATETC
*Formats OPTIONAL
,
304 IN PVOID Context OPTIONAL
,
305 IN
const DROPTARGET_CALLBACKS
*Callbacks OPTIONAL
)
309 HRESULT hr
= ShellObjectCreatorInit
<CDropTarget
>(hwndTarget
, nSupportedFormats
, Formats
, Context
, Callbacks
, IID_IDropTarget
, &dt
);
310 if (FAILED_UNEXPECTEDLY(hr
))