2 * OLE 2 clipboard support
4 * Copyright 1999 Noel Borthwick <noel@macadamian.com>
5 * Copyright 2000 Abey George <abey@macadamian.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * This file contains the implementation for the OLE Clipboard and its
23 * internal interfaces. The OLE clipboard interacts with an IDataObject
24 * interface via the OleSetClipboard, OleGetClipboard and
25 * OleIsCurrentClipboard API's. An internal IDataObject delegates
26 * to a client supplied IDataObject or the WIN32 clipboard API depending
27 * on whether OleSetClipboard has been invoked.
28 * Here are some operating scenarios:
30 * 1. OleSetClipboard called: In this case the internal IDataObject
31 * delegates to the client supplied IDataObject. Additionally OLE takes
32 * ownership of the Windows clipboard and any HGLOCBAL IDataObject
33 * items are placed on the Windows clipboard. This allows non OLE aware
34 * applications to access these. A local WinProc fields WM_RENDERFORMAT
35 * and WM_RENDERALLFORMATS messages in this case.
37 * 2. OleGetClipboard called without previous OleSetClipboard. Here the internal
38 * IDataObject functionality wraps around the WIN32 clipboard API.
40 * 3. OleGetClipboard called after previous OleSetClipboard. Here the internal
41 * IDataObject delegates to the source IDataObjects functionality directly,
42 * thereby bypassing the Windows clipboard.
44 * Implementation references : Inside OLE 2'nd edition by Kraig Brockschmidt
47 * - Support for pasting between different processes. OLE clipboard support
48 * currently works only for in process copy and paste. Since we internally
49 * store a pointer to the source's IDataObject and delegate to that, this
50 * will fail if the IDataObject client belongs to a different process.
51 * - IDataObject::GetDataHere is not implemented
52 * - OleFlushClipboard needs to additionally handle TYMED_IStorage media
53 * by copying the storage into global memory. Subsequently the default
54 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
55 * back to TYMED_IStorage.
56 * - OLE1 compatibility formats to be synthesized from OLE2 formats and put on
57 * clipboard in OleSetClipboard.
66 #define NONAMELESSUNION
67 #define NONAMELESSSTRUCT
76 #include "wine/debug.h"
79 #include "storage32.h"
81 #include "compobj_private.h"
83 WINE_DEFAULT_DEBUG_CHANNEL(ole);
85 #define HANDLE_ERROR(err) do { hr = err; TRACE("(HRESULT=%x)\n", (HRESULT)err); goto CLEANUP; } while (0)
87 /* Structure of 'Ole Private Data' clipboard format */
91 DWORD first_use; /* Has this cf been added to the list already */
93 } ole_priv_data_entry;
98 DWORD size; /* in bytes of the entire structure */
100 DWORD count; /* no. of format entries */
102 ole_priv_data_entry entries[1]; /* array of size count */
103 /* then follows any DVTARGETDEVICE structures referenced in the FORMATETCs */
106 /*****************************************************************************
107 * create_empty_priv_data
109 * Create an empty data structure. The only thing that really matters
110 * here is setting count and size members. This is used by the enumerator as a
111 * convenience when there's an empty list.
113 static HRESULT create_empty_priv_data(ole_priv_data **data)
118 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*ptr));
119 if(!ptr) return E_OUTOFMEMORY;
120 ptr->size = sizeof(*ptr);
127 /****************************************************************************
130 typedef struct ole_clipbrd
132 const IDataObjectVtbl* lpvtbl; /* Exposed IDataObject vtable */
136 HWND hWndClipboard; /* Hidden clipboard window */
137 IDataObject *pIDataObjectSrc; /* Source object passed to OleSetClipboard */
138 ole_priv_data *cached_enum; /* Cached result from the enumeration of src data object */
141 static inline ole_clipbrd *impl_from_IDataObject(IDataObject *iface)
143 return (ole_clipbrd*)((char*)iface - FIELD_OFFSET(ole_clipbrd, lpvtbl));
146 typedef struct PresentationDataHeader
149 DWORD dwObjectExtentX;
150 DWORD dwObjectExtentY;
152 } PresentationDataHeader;
155 * The one and only ole_clipbrd object which is created by OLEClipbrd_Initialize()
157 static ole_clipbrd* theOleClipboard;
161 * Name of our registered OLE clipboard window class
163 static const CHAR OLEClipbrd_WNDCLASS[] = "CLIPBRDWNDCLASS";
165 static UINT dataobject_clipboard_format;
166 static UINT ole_priv_data_clipboard_format;
167 static UINT embed_source_clipboard_format;
169 /*---------------------------------------------------------------------*
170 * Implementation of the internal IEnumFORMATETC interface returned by
171 * the OLE clipboard's IDataObject.
172 *---------------------------------------------------------------------*/
174 typedef struct enum_fmtetc
176 const IEnumFORMATETCVtbl *lpVtbl;
179 UINT pos; /* current enumerator position */
183 static inline enum_fmtetc *impl_from_IEnumFORMATETC(IEnumFORMATETC *iface)
185 return (enum_fmtetc*)((char*)iface - FIELD_OFFSET(enum_fmtetc, lpVtbl));
188 /************************************************************************
189 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
191 * See Windows documentation for more details on IUnknown methods.
193 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
194 (LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
196 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
198 TRACE("(%p)->(IID: %s, %p)\n", This, debugstr_guid(riid), ppvObj);
202 if(IsEqualIID(riid, &IID_IUnknown) ||
203 IsEqualIID(riid, &IID_IEnumFORMATETC))
210 IEnumFORMATETC_AddRef((IEnumFORMATETC*)*ppvObj);
211 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
215 TRACE("-- Interface: E_NOINTERFACE\n");
216 return E_NOINTERFACE;
219 /************************************************************************
220 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
223 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface)
225 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
226 TRACE("(%p)->(count=%u)\n",This, This->ref);
228 return InterlockedIncrement(&This->ref);
231 /************************************************************************
232 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
234 * See Windows documentation for more details on IUnknown methods.
236 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface)
238 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
241 TRACE("(%p)->(count=%u)\n",This, This->ref);
243 ref = InterlockedDecrement(&This->ref);
246 TRACE("() - destroying IEnumFORMATETC(%p)\n",This);
247 HeapFree(GetProcessHeap(), 0, This->data);
248 HeapFree(GetProcessHeap(), 0, This);
253 /************************************************************************
254 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
256 * Standard enumerator members for IEnumFORMATETC
258 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
259 (LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
261 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
263 HRESULT hres = S_FALSE;
265 TRACE("(%p)->(pos=%u)\n", This, This->pos);
267 if (This->pos < This->data->count)
269 cfetch = This->data->count - This->pos;
276 for(i = 0; i < cfetch; i++)
278 rgelt[i] = This->data->entries[This->pos++].fmtetc;
281 DVTARGETDEVICE *target = (DVTARGETDEVICE *)((char *)This->data + (DWORD)rgelt[i].ptd);
282 rgelt[i].ptd = CoTaskMemAlloc(target->tdSize);
283 if(!rgelt[i].ptd) return E_OUTOFMEMORY;
284 memcpy(rgelt[i].ptd, target, target->tdSize);
295 *pceltFethed = cfetch;
301 /************************************************************************
302 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
304 * Standard enumerator members for IEnumFORMATETC
306 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface, ULONG celt)
308 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
309 TRACE("(%p)->(num=%u)\n", This, celt);
312 if (This->pos > This->data->count)
314 This->pos = This->data->count;
320 /************************************************************************
321 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
323 * Standard enumerator members for IEnumFORMATETC
325 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface)
327 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
328 TRACE("(%p)->()\n", This);
334 static HRESULT enum_fmtetc_construct(ole_priv_data *data, UINT pos, IEnumFORMATETC **obj);
336 /************************************************************************
337 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
339 * Standard enumerator members for IEnumFORMATETC
341 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
342 (LPENUMFORMATETC iface, LPENUMFORMATETC* obj)
344 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
345 ole_priv_data *new_data;
347 TRACE("(%p)->(%p)\n", This, obj);
349 if ( !obj ) return E_INVALIDARG;
352 new_data = HeapAlloc(GetProcessHeap(), 0, This->data->size);
353 if(!new_data) return E_OUTOFMEMORY;
355 return enum_fmtetc_construct(new_data, This->pos, obj);
358 static const IEnumFORMATETCVtbl efvt =
360 OLEClipbrd_IEnumFORMATETC_QueryInterface,
361 OLEClipbrd_IEnumFORMATETC_AddRef,
362 OLEClipbrd_IEnumFORMATETC_Release,
363 OLEClipbrd_IEnumFORMATETC_Next,
364 OLEClipbrd_IEnumFORMATETC_Skip,
365 OLEClipbrd_IEnumFORMATETC_Reset,
366 OLEClipbrd_IEnumFORMATETC_Clone
369 /************************************************************************
370 * enum_fmtetc_construct
372 * Creates an IEnumFORMATETC enumerator from ole_priv_data which it then owns.
374 static HRESULT enum_fmtetc_construct(ole_priv_data *data, UINT pos, IEnumFORMATETC **obj)
379 ef = HeapAlloc(GetProcessHeap(), 0, sizeof(*ef));
380 if (!ef) return E_OUTOFMEMORY;
387 TRACE("(%p)->()\n", ef);
388 *obj = (IEnumFORMATETC *)ef;
392 /***********************************************************************
395 * Helper method to duplicate an HGLOBAL chunk of memory
397 static HRESULT dup_global_mem( HGLOBAL src, HGLOBAL *dst )
399 void *src_ptr, *dst_ptr;
403 if ( !src ) return S_FALSE;
405 size = GlobalSize(src);
407 *dst = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE, size );
408 if ( !*dst ) return E_OUTOFMEMORY;
410 src_ptr = GlobalLock(src);
411 dst_ptr = GlobalLock(*dst);
413 memcpy(dst_ptr, src_ptr, size);
421 /************************************************************
422 * render_embed_source_hack
424 * This is clearly a hack and has no place in the clipboard code.
427 static HRESULT render_embed_source_hack(IDataObject *data, LPFORMATETC fmt)
430 HGLOBAL hStorage = 0;
432 ILockBytes *ptrILockBytes;
434 memset(&std, 0, sizeof(STGMEDIUM));
435 std.tymed = fmt->tymed = TYMED_ISTORAGE;
437 hStorage = GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE, 0);
438 if (hStorage == NULL) return E_OUTOFMEMORY;
439 hr = CreateILockBytesOnHGlobal(hStorage, FALSE, &ptrILockBytes);
440 hr = StgCreateDocfileOnILockBytes(ptrILockBytes, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &std.u.pstg);
441 ILockBytes_Release(ptrILockBytes);
443 if (FAILED(hr = IDataObject_GetDataHere(theOleClipboard->pIDataObjectSrc, fmt, &std)))
445 WARN("() : IDataObject_GetDataHere failed to render clipboard data! (%x)\n", hr);
446 GlobalFree(hStorage);
450 if (1) /* check whether the presentation data is already -not- present */
454 METAFILEPICT *mfp = 0;
456 fmt2.cfFormat = CF_METAFILEPICT;
458 fmt2.dwAspect = DVASPECT_CONTENT;
460 fmt2.tymed = TYMED_MFPICT;
462 memset(&std2, 0, sizeof(STGMEDIUM));
463 std2.tymed = TYMED_MFPICT;
465 /* Get the metafile picture out of it */
467 if (SUCCEEDED(hr = IDataObject_GetData(theOleClipboard->pIDataObjectSrc, &fmt2, &std2)))
469 mfp = GlobalLock(std2.u.hGlobal);
474 OLECHAR name[]={ 2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
475 IStream *pStream = 0;
477 PresentationDataHeader pdh;
481 CHAR strOleTypeName[51];
482 BYTE OlePresStreamHeader [] =
484 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00,
485 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
486 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
487 0x00, 0x00, 0x00, 0x00
490 nSize = GetMetaFileBitsEx(mfp->hMF, 0, NULL);
492 memset(&pdh, 0, sizeof(PresentationDataHeader));
493 memcpy(&pdh, OlePresStreamHeader, sizeof(OlePresStreamHeader));
495 pdh.dwObjectExtentX = mfp->xExt;
496 pdh.dwObjectExtentY = mfp->yExt;
499 hr = IStorage_CreateStream(std.u.pstg, name, STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, 0, &pStream);
501 hr = IStream_Write(pStream, &pdh, sizeof(PresentationDataHeader), NULL);
503 mfBits = HeapAlloc(GetProcessHeap(), 0, nSize);
504 nSize = GetMetaFileBitsEx(mfp->hMF, nSize, mfBits);
506 hr = IStream_Write(pStream, mfBits, nSize, NULL);
508 IStream_Release(pStream);
510 HeapFree(GetProcessHeap(), 0, mfBits);
512 GlobalUnlock(std2.u.hGlobal);
513 ReleaseStgMedium(&std2);
515 ReadClassStg(std.u.pstg, &clsID);
516 ProgIDFromCLSID(&clsID, &strProgID);
518 WideCharToMultiByte( CP_ACP, 0, strProgID, -1, strOleTypeName, sizeof(strOleTypeName), NULL, NULL );
519 OLECONVERT_CreateOleStream(std.u.pstg);
520 OLECONVERT_CreateCompObjStream(std.u.pstg, strOleTypeName);
524 if ( !SetClipboardData( fmt->cfFormat, hStorage ) )
526 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
527 GlobalFree(hStorage);
528 hr = CLIPBRD_E_CANT_SET;
531 ReleaseStgMedium(&std);
535 /************************************************************************
536 * find_format_in_list
538 * Returns the first entry that matches the provided clipboard format.
540 static inline ole_priv_data_entry *find_format_in_list(ole_priv_data_entry *entries, DWORD num, UINT cf)
543 for(i = 0; i < num; i++)
544 if(entries[i].fmtetc.cfFormat == cf)
550 /***********************************************************************
553 * Render the clipboard data. Note that this call will delegate to the
554 * source data object.
555 * Note: This function assumes it is passed an HGLOBAL format to render.
557 static HRESULT render_format(IDataObject *data, LPFORMATETC fmt)
560 HGLOBAL clip_data = NULL;
563 /* Embed source hack */
564 if(fmt->cfFormat == embed_source_clipboard_format)
566 return render_embed_source_hack(data, fmt);
569 if (FAILED(hr = IDataObject_GetData(data, fmt, &std)))
571 WARN("() : IDataObject_GetData failed to render clipboard data! (%x)\n", hr);
575 if(std.tymed != TYMED_HGLOBAL)
577 FIXME("got tymed %x\n", std.tymed);
582 hr = dup_global_mem(std.u.hGlobal, &clip_data);
583 if(FAILED(hr)) goto end;
585 if ( !SetClipboardData( fmt->cfFormat, clip_data ) )
587 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
588 GlobalFree(clip_data);
589 hr = CLIPBRD_E_CANT_SET;
593 ReleaseStgMedium(&std);
598 /***********************************************************************
601 static LRESULT CALLBACK clipbrd_wndproc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
603 ole_clipbrd *clipbrd = theOleClipboard;
607 case WM_RENDERFORMAT:
610 ole_priv_data_entry *entry;
612 TRACE("(): WM_RENDERFORMAT(cfFormat=%x)\n", cf);
613 entry = find_format_in_list(clipbrd->cached_enum->entries, clipbrd->cached_enum->count, cf);
616 render_format(clipbrd->pIDataObjectSrc, &entry->fmtetc);
621 case WM_RENDERALLFORMATS:
624 ole_priv_data_entry *entries = clipbrd->cached_enum->entries;
626 TRACE("(): WM_RENDERALLFORMATS\n");
628 for(i = 0; i < clipbrd->cached_enum->count; i++)
630 if(entries[i].first_use)
631 render_format(clipbrd->pIDataObjectSrc, &entries[i].fmtetc);
636 case WM_DESTROYCLIPBOARD:
638 TRACE("(): WM_DESTROYCLIPBOARD\n");
640 if ( clipbrd->pIDataObjectSrc )
642 IDataObject_Release(clipbrd->pIDataObjectSrc);
643 clipbrd->pIDataObjectSrc = NULL;
644 HeapFree(GetProcessHeap(), 0, clipbrd->cached_enum);
645 clipbrd->cached_enum = NULL;
651 return DefWindowProcA(hwnd, message, wparam, lparam);
658 /*---------------------------------------------------------------------*
659 * Implementation of the internal IDataObject interface exposed by
661 *---------------------------------------------------------------------*/
664 /************************************************************************
665 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
667 * See Windows documentation for more details on IUnknown methods.
669 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryInterface(
674 ole_clipbrd *This = impl_from_IDataObject(iface);
675 TRACE("(%p)->(IID:%s, %p)\n", This, debugstr_guid(riid), ppvObject);
677 if ( (This==0) || (ppvObject==0) )
682 if (IsEqualIID(&IID_IUnknown, riid) ||
683 IsEqualIID(&IID_IDataObject, riid))
689 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
690 return E_NOINTERFACE;
693 IUnknown_AddRef((IUnknown*)*ppvObject);
698 /************************************************************************
699 * OLEClipbrd_IDataObject_AddRef (IUnknown)
701 * See Windows documentation for more details on IUnknown methods.
703 static ULONG WINAPI OLEClipbrd_IDataObject_AddRef(
706 ole_clipbrd *This = impl_from_IDataObject(iface);
708 TRACE("(%p)->(count=%u)\n",This, This->ref);
710 return InterlockedIncrement(&This->ref);
713 /***********************************************************************
714 * OLEClipbrd_DestroyWindow(HWND)
715 * Destroy the clipboard window and unregister its class
717 static void OLEClipbrd_DestroyWindow(HWND hwnd)
720 UnregisterClassA( OLEClipbrd_WNDCLASS, 0 );
723 static void OLEClipbrd_Destroy(ole_clipbrd* This)
729 theOleClipboard = NULL;
731 if ( This->hWndClipboard )
732 OLEClipbrd_DestroyWindow(This->hWndClipboard);
734 HeapFree(GetProcessHeap(), 0, This);
737 /************************************************************************
738 * OLEClipbrd_IDataObject_Release (IUnknown)
740 * See Windows documentation for more details on IUnknown methods.
742 static ULONG WINAPI OLEClipbrd_IDataObject_Release(
745 ole_clipbrd *This = impl_from_IDataObject(iface);
748 TRACE("(%p)->(count=%u)\n",This, This->ref);
750 ref = InterlockedDecrement(&This->ref);
754 OLEClipbrd_Destroy(This);
761 /************************************************************************
762 * OLEClipbrd_IDataObject_GetData (IDataObject)
764 * The OLE Clipboard's implementation of this method delegates to
765 * a data source if there is one or wraps around the windows clipboard
767 * See Windows documentation for more details on IDataObject methods.
769 static HRESULT WINAPI OLEClipbrd_IDataObject_GetData(
771 LPFORMATETC pformatetcIn,
776 ole_clipbrd *This = impl_from_IDataObject(iface);
778 TRACE("(%p,%p,%p)\n", iface, pformatetcIn, pmedium);
780 if ( !pformatetcIn || !pmedium )
784 * If we have a data source placed on the clipboard (via OleSetClipboard)
785 * simply delegate to the source object's QueryGetData
786 * NOTE: This code assumes that the IDataObject is in the same address space!
787 * We will need to add marshalling support when Wine handles multiple processes.
789 if ( This->pIDataObjectSrc )
791 return IDataObject_GetData(This->pIDataObjectSrc, pformatetcIn, pmedium);
794 if ( pformatetcIn->lindex != -1 )
795 return DV_E_FORMATETC;
797 if ( (pformatetcIn->tymed & TYMED_HGLOBAL) != TYMED_HGLOBAL )
800 if ( pformatetcIn->dwAspect != DVASPECT_CONTENT )
801 return DV_E_DVASPECT;
805 * Otherwise, get the data from the windows clipboard using GetClipboardData
807 if ( !OpenClipboard(theOleClipboard->hWndClipboard)) return CLIPBRD_E_CANT_OPEN;
809 hData = GetClipboardData(pformatetcIn->cfFormat);
811 /* Must make a copy of global handle returned by GetClipboardData; it
812 * is not valid after we call CloseClipboard
813 * Application is responsible for freeing the memory (Forte Agent does this)
815 src = GlobalLock(hData);
821 size = GlobalSize(hData);
822 hDest = GlobalAlloc(GHND, size);
823 dest = GlobalLock(hDest);
824 memcpy(dest, src, size);
831 * Return the clipboard data in the storage medium structure
833 pmedium->tymed = (hData == 0) ? TYMED_NULL : TYMED_HGLOBAL;
834 pmedium->u.hGlobal = hData;
835 pmedium->pUnkForRelease = NULL;
837 if ( !CloseClipboard() ) return CLIPBRD_E_CANT_CLOSE;
839 return (hData == 0) ? DV_E_FORMATETC : S_OK;
842 static HRESULT WINAPI OLEClipbrd_IDataObject_GetDataHere(
844 LPFORMATETC pformatetc,
851 /************************************************************************
852 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
854 * The OLE Clipboard's implementation of this method delegates to
855 * a data source if there is one or wraps around the windows clipboard
856 * function IsClipboardFormatAvailable() otherwise.
858 * See Windows documentation for more details on IDataObject methods.
860 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryGetData(
862 LPFORMATETC pformatetc)
864 TRACE("(%p, %p)\n", iface, pformatetc);
869 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
870 return DV_E_FORMATETC;
872 if ( pformatetc->lindex != -1 )
873 return DV_E_FORMATETC;
876 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
878 return (IsClipboardFormatAvailable(pformatetc->cfFormat)) ? S_OK : DV_E_CLIPFORMAT;
881 /************************************************************************
882 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
884 * See Windows documentation for more details on IDataObject methods.
886 static HRESULT WINAPI OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
888 LPFORMATETC pformatectIn,
889 LPFORMATETC pformatetcOut)
891 TRACE("(%p, %p, %p)\n", iface, pformatectIn, pformatetcOut);
893 if ( !pformatectIn || !pformatetcOut )
896 *pformatetcOut = *pformatectIn;
897 return DATA_S_SAMEFORMATETC;
900 /************************************************************************
901 * OLEClipbrd_IDataObject_SetData (IDataObject)
903 * The OLE Clipboard's does not implement this method
905 * See Windows documentation for more details on IDataObject methods.
907 static HRESULT WINAPI OLEClipbrd_IDataObject_SetData(
909 LPFORMATETC pformatetc,
917 /************************************************************************
918 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
920 * See Windows documentation for more details on IDataObject methods.
922 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumFormatEtc(
925 IEnumFORMATETC** enum_fmt)
928 ole_clipbrd *This = impl_from_IDataObject(iface);
930 ole_priv_data *data = NULL;
932 TRACE("(%p, %x, %p)\n", iface, dwDirection, enum_fmt);
936 if ( dwDirection != DATADIR_GET ) return E_NOTIMPL;
937 if ( !OpenClipboard(This->hWndClipboard) ) return CLIPBRD_E_CANT_OPEN;
939 handle = GetClipboardData( ole_priv_data_clipboard_format );
942 ole_priv_data *src = GlobalLock(handle);
945 /* FIXME: sanity check on size */
946 data = HeapAlloc(GetProcessHeap(), 0, src->size);
949 GlobalUnlock(handle);
953 memcpy(data, src, src->size);
954 GlobalUnlock(handle);
958 if(!data) hr = create_empty_priv_data(&data);
959 if(FAILED(hr)) goto end;
961 hr = enum_fmtetc_construct( data, 0, enum_fmt );
964 if ( !CloseClipboard() ) hr = CLIPBRD_E_CANT_CLOSE;
968 /************************************************************************
969 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
971 * The OLE Clipboard's does not implement this method
973 * See Windows documentation for more details on IDataObject methods.
975 static HRESULT WINAPI OLEClipbrd_IDataObject_DAdvise(
977 FORMATETC* pformatetc,
979 IAdviseSink* pAdvSink,
980 DWORD* pdwConnection)
986 /************************************************************************
987 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
989 * The OLE Clipboard's does not implement this method
991 * See Windows documentation for more details on IDataObject methods.
993 static HRESULT WINAPI OLEClipbrd_IDataObject_DUnadvise(
1001 /************************************************************************
1002 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1004 * The OLE Clipboard does not implement this method
1006 * See Windows documentation for more details on IDataObject methods.
1008 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumDAdvise(
1010 IEnumSTATDATA** ppenumAdvise)
1016 static const IDataObjectVtbl OLEClipbrd_IDataObject_VTable =
1018 OLEClipbrd_IDataObject_QueryInterface,
1019 OLEClipbrd_IDataObject_AddRef,
1020 OLEClipbrd_IDataObject_Release,
1021 OLEClipbrd_IDataObject_GetData,
1022 OLEClipbrd_IDataObject_GetDataHere,
1023 OLEClipbrd_IDataObject_QueryGetData,
1024 OLEClipbrd_IDataObject_GetCanonicalFormatEtc,
1025 OLEClipbrd_IDataObject_SetData,
1026 OLEClipbrd_IDataObject_EnumFormatEtc,
1027 OLEClipbrd_IDataObject_DAdvise,
1028 OLEClipbrd_IDataObject_DUnadvise,
1029 OLEClipbrd_IDataObject_EnumDAdvise
1032 /*---------------------------------------------------------------------*
1033 * Internal implementation methods for the OLE clipboard
1034 *---------------------------------------------------------------------*/
1036 /*********************************************************
1037 * Construct the OLEClipbrd class.
1039 static ole_clipbrd* OLEClipbrd_Construct(void)
1043 This = HeapAlloc( GetProcessHeap(), 0, sizeof(*This) );
1044 if (!This) return NULL;
1046 This->lpvtbl = &OLEClipbrd_IDataObject_VTable;
1049 This->hWndClipboard = NULL;
1050 This->pIDataObjectSrc = NULL;
1051 This->cached_enum = NULL;
1053 theOleClipboard = This;
1057 static void register_clipboard_formats(void)
1059 static const WCHAR DataObjectW[] = { 'D','a','t','a','O','b','j','e','c','t',0 };
1060 static const WCHAR OlePrivateDataW[] = { 'O','l','e',' ','P','r','i','v','a','t','e',' ','D','a','t','a',0 };
1061 static const WCHAR EmbedSourceW[] = { 'E','m','b','e','d',' ','S','o','u','r','c','e',0 };
1063 if(!dataobject_clipboard_format)
1064 dataobject_clipboard_format = RegisterClipboardFormatW(DataObjectW);
1065 if(!ole_priv_data_clipboard_format)
1066 ole_priv_data_clipboard_format = RegisterClipboardFormatW(OlePrivateDataW);
1067 if(!embed_source_clipboard_format)
1068 embed_source_clipboard_format = RegisterClipboardFormatW(EmbedSourceW);
1071 /***********************************************************************
1072 * OLEClipbrd_Initialize()
1073 * Initializes the OLE clipboard.
1075 void OLEClipbrd_Initialize(void)
1077 register_clipboard_formats();
1079 if ( !theOleClipboard )
1082 theOleClipboard = OLEClipbrd_Construct();
1087 /***********************************************************************
1088 * OLEClipbrd_UnInitialize()
1089 * Un-Initializes the OLE clipboard
1091 void OLEClipbrd_UnInitialize(void)
1095 * Destroy the clipboard if no one holds a reference to us.
1096 * Note that the clipboard was created with a reference count of 1.
1098 if ( theOleClipboard && (theOleClipboard->ref <= 1) )
1100 OLEClipbrd_Destroy( theOleClipboard );
1104 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
1108 /***********************************************************************
1109 * OLEClipbrd_CreateWindow()
1110 * Create the clipboard window
1112 static HWND OLEClipbrd_CreateWindow(void)
1118 * Register the clipboard window class if necessary
1120 ZeroMemory( &wcex, sizeof(WNDCLASSEXA));
1122 wcex.cbSize = sizeof(WNDCLASSEXA);
1123 /* Windows creates this class with a style mask of 0
1124 * We don't bother doing this since the FindClassByAtom code
1125 * would have to be changed to deal with this idiosyncrasy. */
1126 wcex.style = CS_GLOBALCLASS;
1127 wcex.lpfnWndProc = clipbrd_wndproc;
1129 wcex.lpszClassName = OLEClipbrd_WNDCLASS;
1131 RegisterClassExA(&wcex);
1134 * Create a hidden window to receive OLE clipboard messages
1138 * If we need to store state info we can store it here.
1139 * For now we don't need this functionality.
1140 * ClipboardWindowInfo clipboardInfo;
1141 * ZeroMemory( &trackerInfo, sizeof(ClipboardWindowInfo));
1144 hwnd = CreateWindowA(OLEClipbrd_WNDCLASS,
1146 WS_POPUP | WS_CLIPSIBLINGS | WS_OVERLAPPED,
1147 CW_USEDEFAULT, CW_USEDEFAULT,
1148 CW_USEDEFAULT, CW_USEDEFAULT,
1152 0 /*(LPVOID)&clipboardInfo */);
1157 /*********************************************************************
1158 * set_clipboard_formats
1160 * Enumerate all HGLOBAL formats supported by the source and make
1161 * those formats available using delayed rendering using SetClipboardData.
1163 * TODO: We need to additionally handle TYMED_IStorage and
1164 * TYMED_IStream data by copying into global memory.
1166 static HRESULT set_clipboard_formats(ole_clipbrd *clipbrd, IDataObject *data)
1170 IEnumFORMATETC *enum_fmt;
1171 HGLOBAL priv_data_handle;
1172 DWORD target_offset;
1173 ole_priv_data *priv_data;
1174 DWORD count = 0, needed = sizeof(*priv_data), idx;
1176 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
1177 if(FAILED(hr)) return hr;
1179 while(IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL) == S_OK)
1182 needed += sizeof(priv_data->entries[0]);
1185 needed += fmt.ptd->tdSize;
1186 CoTaskMemFree(fmt.ptd);
1190 /* Windows pads the list with two empty ole_priv_data_entries, one
1191 * after the entries array and one after the target device data.
1192 * Allocating with zero init to zero these pads. */
1194 needed += sizeof(priv_data->entries[0]); /* initialisation of needed includes one of these. */
1195 priv_data_handle = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT, needed);
1196 priv_data = GlobalLock(priv_data_handle);
1198 priv_data->unk1 = 0;
1199 priv_data->size = needed;
1200 priv_data->unk2 = 1;
1201 priv_data->count = count;
1202 priv_data->unk3[0] = 0;
1203 priv_data->unk3[1] = 0;
1205 IEnumFORMATETC_Reset(enum_fmt);
1208 target_offset = FIELD_OFFSET(ole_priv_data, entries[count + 1]); /* count entries + one pad. */
1210 while(IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL) == S_OK)
1212 if (fmt.tymed == TYMED_HGLOBAL)
1215 TRACE("(cfFormat=%d:%s)\n", fmt.cfFormat,
1216 GetClipboardFormatNameA(fmt.cfFormat, fmt_name, sizeof(fmt_name)-1) ? fmt_name : "");
1218 SetClipboardData(fmt.cfFormat, NULL);
1221 priv_data->entries[idx].fmtetc = fmt;
1224 memcpy((char*)priv_data + target_offset, fmt.ptd, fmt.ptd->tdSize);
1225 priv_data->entries[idx].fmtetc.ptd = (DVTARGETDEVICE*)target_offset;
1226 target_offset += fmt.ptd->tdSize;
1227 CoTaskMemFree(fmt.ptd);
1230 priv_data->entries[idx].first_use = !find_format_in_list(priv_data->entries, idx, fmt.cfFormat);
1231 priv_data->entries[idx].unk[0] = 0;
1232 priv_data->entries[idx].unk[1] = 0;
1237 IEnumFORMATETC_Release(enum_fmt);
1239 /* Cache the list and fixup any target device offsets to ptrs */
1240 clipbrd->cached_enum = HeapAlloc(GetProcessHeap(), 0, needed);
1241 memcpy(clipbrd->cached_enum, priv_data, needed);
1242 for(idx = 0; idx < clipbrd->cached_enum->count; idx++)
1243 if(clipbrd->cached_enum->entries[idx].fmtetc.ptd)
1244 clipbrd->cached_enum->entries[idx].fmtetc.ptd =
1245 (DVTARGETDEVICE *)((char*)clipbrd->cached_enum + (DWORD)clipbrd->cached_enum->entries[idx].fmtetc.ptd);
1247 GlobalUnlock(priv_data_handle);
1248 SetClipboardData(ole_priv_data_clipboard_format, priv_data_handle);
1253 /*********************************************************************
1254 * set_dataobject_format
1256 * Windows creates a 'DataObject' clipboard format that contains the
1257 * clipboard window's HWND or NULL if the Ole clipboard has been flushed.
1259 static HRESULT set_dataobject_format(HWND hwnd)
1261 HGLOBAL h = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, sizeof(hwnd));
1264 if(!h) return E_OUTOFMEMORY;
1266 data = GlobalLock(h);
1270 if(!SetClipboardData(dataobject_clipboard_format, h))
1273 return CLIPBRD_E_CANT_SET;
1279 /*---------------------------------------------------------------------*
1280 * Win32 OLE clipboard API
1281 *---------------------------------------------------------------------*/
1283 /***********************************************************************
1284 * OleSetClipboard [OLE32.@]
1285 * Places a pointer to the specified data object onto the clipboard,
1286 * making the data object accessible to the OleGetClipboard function.
1290 * S_OK IDataObject pointer placed on the clipboard
1291 * CLIPBRD_E_CANT_OPEN OpenClipboard failed
1292 * CLIPBRD_E_CANT_EMPTY EmptyClipboard failed
1293 * CLIPBRD_E_CANT_CLOSE CloseClipboard failed
1294 * CLIPBRD_E_CANT_SET SetClipboard failed
1297 HRESULT WINAPI OleSetClipboard(IDataObject* pDataObj)
1300 struct oletls *info = COM_CurrentInfo();
1302 TRACE("(%p)\n", pDataObj);
1305 WARN("Could not allocate tls\n");
1307 if(!info->ole_inits)
1308 return CO_E_NOTINITIALIZED;
1310 OLEClipbrd_Initialize();
1313 * If the Ole clipboard window hasn't been created yet, create it now.
1315 if ( !theOleClipboard->hWndClipboard )
1316 theOleClipboard->hWndClipboard = OLEClipbrd_CreateWindow();
1318 if ( !theOleClipboard->hWndClipboard ) return E_FAIL;
1320 if ( !OpenClipboard(theOleClipboard->hWndClipboard) ) return CLIPBRD_E_CANT_OPEN;
1323 * Empty the current clipboard and make our window the clipboard owner
1324 * NOTE: This will trigger a WM_DESTROYCLIPBOARD message
1326 if ( !EmptyClipboard() )
1328 hr = CLIPBRD_E_CANT_EMPTY;
1333 * If we are already holding on to an IDataObject first release that.
1335 if ( theOleClipboard->pIDataObjectSrc )
1337 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
1338 theOleClipboard->pIDataObjectSrc = NULL;
1339 HeapFree(GetProcessHeap(), 0, theOleClipboard->cached_enum);
1340 theOleClipboard->cached_enum = NULL;
1343 /* A NULL value indicates that the clipboard should be emptied. */
1344 theOleClipboard->pIDataObjectSrc = pDataObj;
1347 IDataObject_AddRef(theOleClipboard->pIDataObjectSrc);
1348 hr = set_clipboard_formats(theOleClipboard, pDataObj);
1349 if(FAILED(hr)) goto end;
1352 hr = set_dataobject_format(theOleClipboard->hWndClipboard);
1356 if ( !CloseClipboard() ) hr = CLIPBRD_E_CANT_CLOSE;
1360 if (theOleClipboard->pIDataObjectSrc)
1362 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
1363 theOleClipboard->pIDataObjectSrc = NULL;
1364 HeapFree(GetProcessHeap(), 0, theOleClipboard->cached_enum);
1365 theOleClipboard->cached_enum = NULL;
1373 /***********************************************************************
1374 * OleGetClipboard [OLE32.@]
1375 * Returns a pointer to our internal IDataObject which represents the conceptual
1376 * state of the Windows clipboard. If the current clipboard already contains
1377 * an IDataObject, our internal IDataObject will delegate to this object.
1379 HRESULT WINAPI OleGetClipboard(IDataObject** ppDataObj)
1385 * Make sure we have a clipboard object
1387 OLEClipbrd_Initialize();
1389 if (!theOleClipboard)
1390 return E_OUTOFMEMORY;
1392 /* Return a reference counted IDataObject */
1393 hr = IDataObject_QueryInterface( (IDataObject*)&(theOleClipboard->lpvtbl),
1394 &IID_IDataObject, (void**)ppDataObj);
1398 /******************************************************************************
1399 * OleFlushClipboard [OLE32.@]
1400 * Renders the data from the source IDataObject into the windows clipboard
1402 * TODO: OleFlushClipboard needs to additionally handle TYMED_IStorage media
1403 * by copying the storage into global memory. Subsequently the default
1404 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
1405 * back to TYMED_IStorage.
1407 HRESULT WINAPI OleFlushClipboard(void)
1409 IEnumFORMATETC* penumFormatetc = NULL;
1415 OLEClipbrd_Initialize();
1418 * Already flushed or no source DataObject? Nothing to do.
1420 if (!theOleClipboard->pIDataObjectSrc)
1423 if (!OpenClipboard(theOleClipboard->hWndClipboard))
1424 return CLIPBRD_E_CANT_OPEN;
1427 * Render all HGLOBAL formats supported by the source into
1428 * the windows clipboard.
1430 if ( FAILED( hr = IDataObject_EnumFormatEtc( theOleClipboard->pIDataObjectSrc,
1436 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
1438 if ( rgelt.tymed == TYMED_HGLOBAL )
1441 TRACE("(cfFormat=%d:%s)\n", rgelt.cfFormat,
1442 GetClipboardFormatNameA(rgelt.cfFormat, szFmtName, sizeof(szFmtName)-1)
1445 if ( FAILED(render_format( theOleClipboard->pIDataObjectSrc, &rgelt )) )
1450 IEnumFORMATETC_Release(penumFormatetc);
1452 hr = set_dataobject_format(NULL);
1454 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
1455 theOleClipboard->pIDataObjectSrc = NULL;
1456 HeapFree(GetProcessHeap(), 0, theOleClipboard->cached_enum);
1457 theOleClipboard->cached_enum = NULL;
1461 if ( !CloseClipboard() ) hr = CLIPBRD_E_CANT_CLOSE;
1467 /***********************************************************************
1468 * OleIsCurrentClipboard [OLE32.@]
1470 HRESULT WINAPI OleIsCurrentClipboard(IDataObject *pDataObject)
1474 * Make sure we have a clipboard object
1476 OLEClipbrd_Initialize();
1478 if (!theOleClipboard)
1479 return E_OUTOFMEMORY;
1481 if (pDataObject == NULL)
1484 return (pDataObject == theOleClipboard->pIDataObjectSrc) ? S_OK : S_FALSE;