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)
88 /****************************************************************************
93 const IDataObjectVtbl* lpvtbl; /* Exposed IDataObject vtable */
97 HWND hWndClipboard; /* Hidden clipboard window */
98 IDataObject* pIDataObjectSrc; /* Source object passed to OleSetClipboard */
101 typedef struct ole_clipbrd ole_clipbrd;
103 static inline ole_clipbrd *impl_from_IDataObject(IDataObject *iface)
105 return (ole_clipbrd*)((char*)iface - FIELD_OFFSET(ole_clipbrd, lpvtbl));
108 typedef struct PresentationDataHeader
111 DWORD dwObjectExtentX;
112 DWORD dwObjectExtentY;
114 } PresentationDataHeader;
117 * The one and only ole_clipbrd object which is created by OLEClipbrd_Initialize()
119 static ole_clipbrd* theOleClipboard;
123 * Name of our registered OLE clipboard window class
125 static const CHAR OLEClipbrd_WNDCLASS[] = "CLIPBRDWNDCLASS";
127 static UINT dataobject_clipboard_format;
128 static UINT ole_priv_data_clipboard_format;
129 static UINT embed_source_clipboard_format;
131 /* Structure of 'Ole Private Data' clipboard format */
135 DWORD first_use; /* Has this cf been added to the list already */
137 } ole_priv_data_entry;
142 DWORD size; /* in bytes of the entire structure */
144 DWORD count; /* no. of format entries */
146 ole_priv_data_entry entries[1]; /* array of size count */
147 /* then follows any DVTARGETDEVICE structures referenced in the FORMATETCs */
150 /* Create an empty data structure. The only thing that really matters
151 here is setting count and size members. This is used by the enumerator as a
152 convenience when there's an empty list. */
153 static HRESULT create_empty_priv_data(ole_priv_data **data)
158 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*ptr));
159 if(!ptr) return E_OUTOFMEMORY;
160 ptr->size = sizeof(*ptr);
166 /*---------------------------------------------------------------------*
167 * Implementation of the internal IEnumFORMATETC interface returned by
168 * the OLE clipboard's IDataObject.
169 *---------------------------------------------------------------------*/
171 typedef struct enum_fmtetc
173 const IEnumFORMATETCVtbl *lpVtbl;
176 UINT pos; /* current enumerator position */
180 static inline enum_fmtetc *impl_from_IEnumFORMATETC(IEnumFORMATETC *iface)
182 return (enum_fmtetc*)((char*)iface - FIELD_OFFSET(enum_fmtetc, lpVtbl));
185 /************************************************************************
186 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
188 * See Windows documentation for more details on IUnknown methods.
190 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
191 (LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
193 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
195 TRACE("(%p)->(IID: %s, %p)\n", This, debugstr_guid(riid), ppvObj);
199 if(IsEqualIID(riid, &IID_IUnknown) ||
200 IsEqualIID(riid, &IID_IEnumFORMATETC))
207 IEnumFORMATETC_AddRef((IEnumFORMATETC*)*ppvObj);
208 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
212 TRACE("-- Interface: E_NOINTERFACE\n");
213 return E_NOINTERFACE;
216 /************************************************************************
217 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
220 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface)
222 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
223 TRACE("(%p)->(count=%u)\n",This, This->ref);
225 return InterlockedIncrement(&This->ref);
228 /************************************************************************
229 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
231 * See Windows documentation for more details on IUnknown methods.
233 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface)
235 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
238 TRACE("(%p)->(count=%u)\n",This, This->ref);
240 ref = InterlockedDecrement(&This->ref);
243 TRACE("() - destroying IEnumFORMATETC(%p)\n",This);
244 HeapFree(GetProcessHeap(), 0, This->data);
245 HeapFree(GetProcessHeap(), 0, This);
250 /************************************************************************
251 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
253 * Standard enumerator members for IEnumFORMATETC
255 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
256 (LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
258 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
260 HRESULT hres = S_FALSE;
262 TRACE("(%p)->(pos=%u)\n", This, This->pos);
264 if (This->pos < This->data->count)
266 cfetch = This->data->count - This->pos;
273 for(i = 0; i < cfetch; i++)
275 rgelt[i] = This->data->entries[This->pos++].fmtetc;
278 DVTARGETDEVICE *target = (DVTARGETDEVICE *)((char *)This->data + (DWORD)rgelt[i].ptd);
279 rgelt[i].ptd = CoTaskMemAlloc(target->tdSize);
280 if(!rgelt[i].ptd) return E_OUTOFMEMORY;
281 memcpy(rgelt[i].ptd, target, target->tdSize);
292 *pceltFethed = cfetch;
298 /************************************************************************
299 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
301 * Standard enumerator members for IEnumFORMATETC
303 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface, ULONG celt)
305 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
306 TRACE("(%p)->(num=%u)\n", This, celt);
309 if (This->pos > This->data->count)
311 This->pos = This->data->count;
317 /************************************************************************
318 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
320 * Standard enumerator members for IEnumFORMATETC
322 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface)
324 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
325 TRACE("(%p)->()\n", This);
331 static HRESULT enum_fmtetc_construct(ole_priv_data *data, UINT pos, IEnumFORMATETC **obj);
333 /************************************************************************
334 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
336 * Standard enumerator members for IEnumFORMATETC
338 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
339 (LPENUMFORMATETC iface, LPENUMFORMATETC* obj)
341 enum_fmtetc *This = impl_from_IEnumFORMATETC(iface);
342 ole_priv_data *new_data;
344 TRACE("(%p)->(%p)\n", This, obj);
346 if ( !obj ) return E_INVALIDARG;
349 new_data = HeapAlloc(GetProcessHeap(), 0, This->data->size);
350 if(!new_data) return E_OUTOFMEMORY;
352 return enum_fmtetc_construct(new_data, This->pos, obj);
355 static const IEnumFORMATETCVtbl efvt =
357 OLEClipbrd_IEnumFORMATETC_QueryInterface,
358 OLEClipbrd_IEnumFORMATETC_AddRef,
359 OLEClipbrd_IEnumFORMATETC_Release,
360 OLEClipbrd_IEnumFORMATETC_Next,
361 OLEClipbrd_IEnumFORMATETC_Skip,
362 OLEClipbrd_IEnumFORMATETC_Reset,
363 OLEClipbrd_IEnumFORMATETC_Clone
366 /************************************************************************
367 * enum_fmtetc_construct
369 * Creates an IEnumFORMATETC enumerator from ole_priv_data which it then owns.
371 static HRESULT enum_fmtetc_construct(ole_priv_data *data, UINT pos, IEnumFORMATETC **obj)
376 ef = HeapAlloc(GetProcessHeap(), 0, sizeof(*ef));
377 if (!ef) return E_OUTOFMEMORY;
384 TRACE("(%p)->()\n", ef);
385 *obj = (IEnumFORMATETC *)ef;
389 /***********************************************************************
392 * Helper method to duplicate an HGLOBAL chunk of memory
394 static HRESULT dup_global_mem( HGLOBAL src, HGLOBAL *dst )
396 void *src_ptr, *dst_ptr;
400 if ( !src ) return S_FALSE;
402 size = GlobalSize(src);
404 *dst = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE, size );
405 if ( !*dst ) return E_OUTOFMEMORY;
407 src_ptr = GlobalLock(src);
408 dst_ptr = GlobalLock(*dst);
410 memcpy(dst_ptr, src_ptr, size);
418 /************************************************************
419 * render_embed_source_hack
421 * This is clearly a hack and has no place in the clipboard code.
424 static HRESULT render_embed_source_hack(IDataObject *data, LPFORMATETC fmt)
427 HGLOBAL hStorage = 0;
429 ILockBytes *ptrILockBytes;
431 memset(&std, 0, sizeof(STGMEDIUM));
432 std.tymed = fmt->tymed = TYMED_ISTORAGE;
434 hStorage = GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE, 0);
435 if (hStorage == NULL) return E_OUTOFMEMORY;
436 hr = CreateILockBytesOnHGlobal(hStorage, FALSE, &ptrILockBytes);
437 hr = StgCreateDocfileOnILockBytes(ptrILockBytes, STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &std.u.pstg);
438 ILockBytes_Release(ptrILockBytes);
440 if (FAILED(hr = IDataObject_GetDataHere(theOleClipboard->pIDataObjectSrc, fmt, &std)))
442 WARN("() : IDataObject_GetDataHere failed to render clipboard data! (%x)\n", hr);
443 GlobalFree(hStorage);
447 if (1) /* check whether the presentation data is already -not- present */
451 METAFILEPICT *mfp = 0;
453 fmt2.cfFormat = CF_METAFILEPICT;
455 fmt2.dwAspect = DVASPECT_CONTENT;
457 fmt2.tymed = TYMED_MFPICT;
459 memset(&std2, 0, sizeof(STGMEDIUM));
460 std2.tymed = TYMED_MFPICT;
462 /* Get the metafile picture out of it */
464 if (SUCCEEDED(hr = IDataObject_GetData(theOleClipboard->pIDataObjectSrc, &fmt2, &std2)))
466 mfp = GlobalLock(std2.u.hGlobal);
471 OLECHAR name[]={ 2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
472 IStream *pStream = 0;
474 PresentationDataHeader pdh;
478 CHAR strOleTypeName[51];
479 BYTE OlePresStreamHeader [] =
481 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x00, 0x00,
482 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
483 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
484 0x00, 0x00, 0x00, 0x00
487 nSize = GetMetaFileBitsEx(mfp->hMF, 0, NULL);
489 memset(&pdh, 0, sizeof(PresentationDataHeader));
490 memcpy(&pdh, OlePresStreamHeader, sizeof(OlePresStreamHeader));
492 pdh.dwObjectExtentX = mfp->xExt;
493 pdh.dwObjectExtentY = mfp->yExt;
496 hr = IStorage_CreateStream(std.u.pstg, name, STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, 0, &pStream);
498 hr = IStream_Write(pStream, &pdh, sizeof(PresentationDataHeader), NULL);
500 mfBits = HeapAlloc(GetProcessHeap(), 0, nSize);
501 nSize = GetMetaFileBitsEx(mfp->hMF, nSize, mfBits);
503 hr = IStream_Write(pStream, mfBits, nSize, NULL);
505 IStream_Release(pStream);
507 HeapFree(GetProcessHeap(), 0, mfBits);
509 GlobalUnlock(std2.u.hGlobal);
510 ReleaseStgMedium(&std2);
512 ReadClassStg(std.u.pstg, &clsID);
513 ProgIDFromCLSID(&clsID, &strProgID);
515 WideCharToMultiByte( CP_ACP, 0, strProgID, -1, strOleTypeName, sizeof(strOleTypeName), NULL, NULL );
516 OLECONVERT_CreateOleStream(std.u.pstg);
517 OLECONVERT_CreateCompObjStream(std.u.pstg, strOleTypeName);
521 if ( !SetClipboardData( fmt->cfFormat, hStorage ) )
523 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
524 GlobalFree(hStorage);
525 hr = CLIPBRD_E_CANT_SET;
528 ReleaseStgMedium(&std);
532 /***********************************************************************
535 * Render the clipboard data. Note that this call will delegate to the
536 * source data object.
537 * Note: This function assumes it is passed an HGLOBAL format to render.
539 static HRESULT render_format(IDataObject *data, LPFORMATETC fmt)
542 HGLOBAL clip_data = NULL;
545 /* Embed source hack */
546 if(fmt->cfFormat == embed_source_clipboard_format)
548 return render_embed_source_hack(data, fmt);
551 if (FAILED(hr = IDataObject_GetData(data, fmt, &std)))
553 WARN("() : IDataObject_GetData failed to render clipboard data! (%x)\n", hr);
557 if(std.tymed != TYMED_HGLOBAL)
559 FIXME("got tymed %x\n", std.tymed);
564 hr = dup_global_mem(std.u.hGlobal, &clip_data);
565 if(FAILED(hr)) goto end;
567 if ( !SetClipboardData( fmt->cfFormat, clip_data ) )
569 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
570 GlobalFree(clip_data);
571 hr = CLIPBRD_E_CANT_SET;
575 ReleaseStgMedium(&std);
580 /***********************************************************************
581 * OLEClipbrd_WndProc(HWND, unsigned, WORD, LONG)
582 * Processes messages sent to the OLE clipboard window.
583 * Note that we will intercept messages in our WndProc only when data
584 * has been placed in the clipboard via OleSetClipboard().
585 * i.e. Only when OLE owns the windows clipboard.
587 static LRESULT CALLBACK OLEClipbrd_WndProc
588 (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
594 * We receive this message to allow us to handle delayed rendering of
595 * a specific clipboard format when an application requests data in
596 * that format by calling GetClipboardData.
597 * (Recall that in OleSetClipboard, we used SetClipboardData to
598 * make all HGLOBAL formats supported by the source IDataObject
599 * available using delayed rendering)
600 * On receiving this message we must actually render the data in the
601 * specified format and place it on the clipboard by calling the
602 * SetClipboardData function.
604 case WM_RENDERFORMAT:
608 ZeroMemory( &rgelt, sizeof(FORMATETC));
611 * Initialize FORMATETC to a Windows clipboard friendly format
613 rgelt.cfFormat = (UINT) wParam;
614 rgelt.dwAspect = DVASPECT_CONTENT;
616 rgelt.tymed = TYMED_HGLOBAL;
618 TRACE("(): WM_RENDERFORMAT(cfFormat=%d)\n", rgelt.cfFormat);
621 * Render the clipboard data.
622 * (We must have a source data object or we wouldn't be in this WndProc)
624 render_format( (IDataObject*)&(theOleClipboard->lpvtbl), &rgelt );
630 * WM_RENDERALLFORMATS
631 * Sent before the clipboard owner window is destroyed.
632 * We should receive this message only when OleUninitialize is called
633 * while we have an IDataObject in the clipboard.
634 * For the content of the clipboard to remain available to other
635 * applications, we must render data in all the formats the source IDataObject
636 * is capable of generating, and place the data on the clipboard by calling
639 case WM_RENDERALLFORMATS:
641 IEnumFORMATETC* penumFormatetc = NULL;
644 TRACE("(): WM_RENDERALLFORMATS\n");
647 * Render all HGLOBAL formats supported by the source into
648 * the windows clipboard.
650 if ( FAILED( IDataObject_EnumFormatEtc( (IDataObject*)&(theOleClipboard->lpvtbl),
651 DATADIR_GET, &penumFormatetc) ) )
653 WARN("(): WM_RENDERALLFORMATS failed to retrieve EnumFormatEtc!\n");
657 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
659 if ( rgelt.tymed == TYMED_HGLOBAL )
662 * Render the clipboard data.
664 if ( FAILED(render_format( (IDataObject*)&(theOleClipboard->lpvtbl), &rgelt )) )
667 TRACE("(): WM_RENDERALLFORMATS(cfFormat=%d)\n", rgelt.cfFormat);
671 IEnumFORMATETC_Release(penumFormatetc);
677 * WM_DESTROYCLIPBOARD
678 * This is sent by EmptyClipboard before the clipboard is emptied.
679 * We should release any IDataObject we are holding onto when we receive
680 * this message, since it indicates that the OLE clipboard should be empty
681 * from this point on.
683 case WM_DESTROYCLIPBOARD:
685 TRACE("(): WM_DESTROYCLIPBOARD\n");
687 * Release the data object we are holding on to
689 if ( theOleClipboard->pIDataObjectSrc )
691 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
692 theOleClipboard->pIDataObjectSrc = NULL;
698 case WM_ASKCBFORMATNAME:
699 case WM_CHANGECBCHAIN:
700 case WM_DRAWCLIPBOARD:
701 case WM_SIZECLIPBOARD:
702 case WM_HSCROLLCLIPBOARD:
703 case WM_VSCROLLCLIPBOARD:
704 case WM_PAINTCLIPBOARD:
707 return DefWindowProcA(hWnd, message, wParam, lParam);
714 /*---------------------------------------------------------------------*
715 * Implementation of the internal IDataObject interface exposed by
717 *---------------------------------------------------------------------*/
720 /************************************************************************
721 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
723 * See Windows documentation for more details on IUnknown methods.
725 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryInterface(
730 ole_clipbrd *This = impl_from_IDataObject(iface);
731 TRACE("(%p)->(IID:%s, %p)\n", This, debugstr_guid(riid), ppvObject);
733 if ( (This==0) || (ppvObject==0) )
738 if (IsEqualIID(&IID_IUnknown, riid) ||
739 IsEqualIID(&IID_IDataObject, riid))
745 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
746 return E_NOINTERFACE;
749 IUnknown_AddRef((IUnknown*)*ppvObject);
754 /************************************************************************
755 * OLEClipbrd_IDataObject_AddRef (IUnknown)
757 * See Windows documentation for more details on IUnknown methods.
759 static ULONG WINAPI OLEClipbrd_IDataObject_AddRef(
762 ole_clipbrd *This = impl_from_IDataObject(iface);
764 TRACE("(%p)->(count=%u)\n",This, This->ref);
766 return InterlockedIncrement(&This->ref);
769 /***********************************************************************
770 * OLEClipbrd_DestroyWindow(HWND)
771 * Destroy the clipboard window and unregister its class
773 static void OLEClipbrd_DestroyWindow(HWND hwnd)
776 UnregisterClassA( OLEClipbrd_WNDCLASS, 0 );
779 static void OLEClipbrd_Destroy(ole_clipbrd* This)
785 theOleClipboard = NULL;
787 if ( This->hWndClipboard )
788 OLEClipbrd_DestroyWindow(This->hWndClipboard);
790 HeapFree(GetProcessHeap(), 0, This);
793 /************************************************************************
794 * OLEClipbrd_IDataObject_Release (IUnknown)
796 * See Windows documentation for more details on IUnknown methods.
798 static ULONG WINAPI OLEClipbrd_IDataObject_Release(
801 ole_clipbrd *This = impl_from_IDataObject(iface);
804 TRACE("(%p)->(count=%u)\n",This, This->ref);
806 ref = InterlockedDecrement(&This->ref);
810 OLEClipbrd_Destroy(This);
817 /************************************************************************
818 * OLEClipbrd_IDataObject_GetData (IDataObject)
820 * The OLE Clipboard's implementation of this method delegates to
821 * a data source if there is one or wraps around the windows clipboard
823 * See Windows documentation for more details on IDataObject methods.
825 static HRESULT WINAPI OLEClipbrd_IDataObject_GetData(
827 LPFORMATETC pformatetcIn,
832 ole_clipbrd *This = impl_from_IDataObject(iface);
834 TRACE("(%p,%p,%p)\n", iface, pformatetcIn, pmedium);
836 if ( !pformatetcIn || !pmedium )
840 * If we have a data source placed on the clipboard (via OleSetClipboard)
841 * simply delegate to the source object's QueryGetData
842 * NOTE: This code assumes that the IDataObject is in the same address space!
843 * We will need to add marshalling support when Wine handles multiple processes.
845 if ( This->pIDataObjectSrc )
847 return IDataObject_GetData(This->pIDataObjectSrc, pformatetcIn, pmedium);
850 if ( pformatetcIn->lindex != -1 )
851 return DV_E_FORMATETC;
853 if ( (pformatetcIn->tymed & TYMED_HGLOBAL) != TYMED_HGLOBAL )
856 if ( pformatetcIn->dwAspect != DVASPECT_CONTENT )
857 return DV_E_DVASPECT;
861 * Otherwise, get the data from the windows clipboard using GetClipboardData
863 if ( !OpenClipboard(theOleClipboard->hWndClipboard)) return CLIPBRD_E_CANT_OPEN;
865 hData = GetClipboardData(pformatetcIn->cfFormat);
867 /* Must make a copy of global handle returned by GetClipboardData; it
868 * is not valid after we call CloseClipboard
869 * Application is responsible for freeing the memory (Forte Agent does this)
871 src = GlobalLock(hData);
877 size = GlobalSize(hData);
878 hDest = GlobalAlloc(GHND, size);
879 dest = GlobalLock(hDest);
880 memcpy(dest, src, size);
887 * Return the clipboard data in the storage medium structure
889 pmedium->tymed = (hData == 0) ? TYMED_NULL : TYMED_HGLOBAL;
890 pmedium->u.hGlobal = hData;
891 pmedium->pUnkForRelease = NULL;
893 if ( !CloseClipboard() ) return CLIPBRD_E_CANT_CLOSE;
895 return (hData == 0) ? DV_E_FORMATETC : S_OK;
898 static HRESULT WINAPI OLEClipbrd_IDataObject_GetDataHere(
900 LPFORMATETC pformatetc,
907 /************************************************************************
908 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
910 * The OLE Clipboard's implementation of this method delegates to
911 * a data source if there is one or wraps around the windows clipboard
912 * function IsClipboardFormatAvailable() otherwise.
914 * See Windows documentation for more details on IDataObject methods.
916 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryGetData(
918 LPFORMATETC pformatetc)
920 TRACE("(%p, %p)\n", iface, pformatetc);
925 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
926 return DV_E_FORMATETC;
928 if ( pformatetc->lindex != -1 )
929 return DV_E_FORMATETC;
932 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
934 return (IsClipboardFormatAvailable(pformatetc->cfFormat)) ? S_OK : DV_E_CLIPFORMAT;
937 /************************************************************************
938 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
940 * See Windows documentation for more details on IDataObject methods.
942 static HRESULT WINAPI OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
944 LPFORMATETC pformatectIn,
945 LPFORMATETC pformatetcOut)
947 TRACE("(%p, %p, %p)\n", iface, pformatectIn, pformatetcOut);
949 if ( !pformatectIn || !pformatetcOut )
952 *pformatetcOut = *pformatectIn;
953 return DATA_S_SAMEFORMATETC;
956 /************************************************************************
957 * OLEClipbrd_IDataObject_SetData (IDataObject)
959 * The OLE Clipboard's does not implement this method
961 * See Windows documentation for more details on IDataObject methods.
963 static HRESULT WINAPI OLEClipbrd_IDataObject_SetData(
965 LPFORMATETC pformatetc,
973 /************************************************************************
974 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
976 * See Windows documentation for more details on IDataObject methods.
978 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumFormatEtc(
981 IEnumFORMATETC** enum_fmt)
984 ole_clipbrd *This = impl_from_IDataObject(iface);
986 ole_priv_data *data = NULL;
988 TRACE("(%p, %x, %p)\n", iface, dwDirection, enum_fmt);
992 if ( dwDirection != DATADIR_GET ) return E_NOTIMPL;
993 if ( !OpenClipboard(This->hWndClipboard) ) return CLIPBRD_E_CANT_OPEN;
995 handle = GetClipboardData( ole_priv_data_clipboard_format );
998 ole_priv_data *src = GlobalLock(handle);
1001 /* FIXME: sanity check on size */
1002 data = HeapAlloc(GetProcessHeap(), 0, src->size);
1005 GlobalUnlock(handle);
1009 memcpy(data, src, src->size);
1010 GlobalUnlock(handle);
1014 if(!data) hr = create_empty_priv_data(&data);
1015 if(FAILED(hr)) goto end;
1017 hr = enum_fmtetc_construct( data, 0, enum_fmt );
1020 if ( !CloseClipboard() ) hr = CLIPBRD_E_CANT_CLOSE;
1024 /************************************************************************
1025 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
1027 * The OLE Clipboard's does not implement this method
1029 * See Windows documentation for more details on IDataObject methods.
1031 static HRESULT WINAPI OLEClipbrd_IDataObject_DAdvise(
1033 FORMATETC* pformatetc,
1035 IAdviseSink* pAdvSink,
1036 DWORD* pdwConnection)
1042 /************************************************************************
1043 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
1045 * The OLE Clipboard's does not implement this method
1047 * See Windows documentation for more details on IDataObject methods.
1049 static HRESULT WINAPI OLEClipbrd_IDataObject_DUnadvise(
1057 /************************************************************************
1058 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1060 * The OLE Clipboard does not implement this method
1062 * See Windows documentation for more details on IDataObject methods.
1064 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumDAdvise(
1066 IEnumSTATDATA** ppenumAdvise)
1072 static const IDataObjectVtbl OLEClipbrd_IDataObject_VTable =
1074 OLEClipbrd_IDataObject_QueryInterface,
1075 OLEClipbrd_IDataObject_AddRef,
1076 OLEClipbrd_IDataObject_Release,
1077 OLEClipbrd_IDataObject_GetData,
1078 OLEClipbrd_IDataObject_GetDataHere,
1079 OLEClipbrd_IDataObject_QueryGetData,
1080 OLEClipbrd_IDataObject_GetCanonicalFormatEtc,
1081 OLEClipbrd_IDataObject_SetData,
1082 OLEClipbrd_IDataObject_EnumFormatEtc,
1083 OLEClipbrd_IDataObject_DAdvise,
1084 OLEClipbrd_IDataObject_DUnadvise,
1085 OLEClipbrd_IDataObject_EnumDAdvise
1088 /*---------------------------------------------------------------------*
1089 * Internal implementation methods for the OLE clipboard
1090 *---------------------------------------------------------------------*/
1092 /*********************************************************
1093 * Construct the OLEClipbrd class.
1095 static ole_clipbrd* OLEClipbrd_Construct(void)
1099 This = HeapAlloc( GetProcessHeap(), 0, sizeof(*This) );
1100 if (!This) return NULL;
1102 This->lpvtbl = &OLEClipbrd_IDataObject_VTable;
1105 This->hWndClipboard = NULL;
1106 This->pIDataObjectSrc = NULL;
1108 theOleClipboard = This;
1112 static void register_clipboard_formats(void)
1114 static const WCHAR DataObjectW[] = { 'D','a','t','a','O','b','j','e','c','t',0 };
1115 static const WCHAR OlePrivateDataW[] = { 'O','l','e',' ','P','r','i','v','a','t','e',' ','D','a','t','a',0 };
1116 static const WCHAR EmbedSourceW[] = { 'E','m','b','e','d',' ','S','o','u','r','c','e',0 };
1118 if(!dataobject_clipboard_format)
1119 dataobject_clipboard_format = RegisterClipboardFormatW(DataObjectW);
1120 if(!ole_priv_data_clipboard_format)
1121 ole_priv_data_clipboard_format = RegisterClipboardFormatW(OlePrivateDataW);
1122 if(!embed_source_clipboard_format)
1123 embed_source_clipboard_format = RegisterClipboardFormatW(EmbedSourceW);
1126 /***********************************************************************
1127 * OLEClipbrd_Initialize()
1128 * Initializes the OLE clipboard.
1130 void OLEClipbrd_Initialize(void)
1132 register_clipboard_formats();
1134 if ( !theOleClipboard )
1137 theOleClipboard = OLEClipbrd_Construct();
1142 /***********************************************************************
1143 * OLEClipbrd_UnInitialize()
1144 * Un-Initializes the OLE clipboard
1146 void OLEClipbrd_UnInitialize(void)
1150 * Destroy the clipboard if no one holds a reference to us.
1151 * Note that the clipboard was created with a reference count of 1.
1153 if ( theOleClipboard && (theOleClipboard->ref <= 1) )
1155 OLEClipbrd_Destroy( theOleClipboard );
1159 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
1163 /***********************************************************************
1164 * OLEClipbrd_CreateWindow()
1165 * Create the clipboard window
1167 static HWND OLEClipbrd_CreateWindow(void)
1173 * Register the clipboard window class if necessary
1175 ZeroMemory( &wcex, sizeof(WNDCLASSEXA));
1177 wcex.cbSize = sizeof(WNDCLASSEXA);
1178 /* Windows creates this class with a style mask of 0
1179 * We don't bother doing this since the FindClassByAtom code
1180 * would have to be changed to deal with this idiosyncrasy. */
1181 wcex.style = CS_GLOBALCLASS;
1182 wcex.lpfnWndProc = OLEClipbrd_WndProc;
1184 wcex.lpszClassName = OLEClipbrd_WNDCLASS;
1186 RegisterClassExA(&wcex);
1189 * Create a hidden window to receive OLE clipboard messages
1193 * If we need to store state info we can store it here.
1194 * For now we don't need this functionality.
1195 * ClipboardWindowInfo clipboardInfo;
1196 * ZeroMemory( &trackerInfo, sizeof(ClipboardWindowInfo));
1199 hwnd = CreateWindowA(OLEClipbrd_WNDCLASS,
1201 WS_POPUP | WS_CLIPSIBLINGS | WS_OVERLAPPED,
1202 CW_USEDEFAULT, CW_USEDEFAULT,
1203 CW_USEDEFAULT, CW_USEDEFAULT,
1207 0 /*(LPVOID)&clipboardInfo */);
1212 static inline BOOL is_format_in_list(ole_priv_data_entry *entries, DWORD num, UINT cf)
1215 for(i = 0; i < num; i++)
1216 if(entries[i].fmtetc.cfFormat == cf)
1222 /*********************************************************************
1223 * set_clipboard_formats
1225 * Enumerate all HGLOBAL formats supported by the source and make
1226 * those formats available using delayed rendering using SetClipboardData.
1228 * TODO: We need to additionally handle TYMED_IStorage and
1229 * TYMED_IStream data by copying into global memory.
1231 static HRESULT set_clipboard_formats(IDataObject *data)
1235 IEnumFORMATETC *enum_fmt;
1236 HGLOBAL priv_data_handle;
1237 DWORD target_offset;
1238 ole_priv_data *priv_data;
1239 DWORD count = 0, needed = sizeof(*priv_data), idx;
1241 hr = IDataObject_EnumFormatEtc(data, DATADIR_GET, &enum_fmt);
1242 if(FAILED(hr)) return hr;
1244 while(IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL) == S_OK)
1247 needed += sizeof(priv_data->entries[0]);
1250 needed += fmt.ptd->tdSize;
1251 CoTaskMemFree(fmt.ptd);
1255 /* Windows pads the list with two empty ole_priv_data_entries, one
1256 * after the entries array and one after the target device data.
1257 * Allocating with zero init to zero these pads. */
1259 needed += sizeof(priv_data->entries[0]); /* initialisation of needed includes one of these. */
1260 priv_data_handle = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT, needed);
1261 priv_data = GlobalLock(priv_data_handle);
1263 priv_data->unk1 = 0;
1264 priv_data->size = needed;
1265 priv_data->unk2 = 1;
1266 priv_data->count = count;
1267 priv_data->unk3[0] = 0;
1268 priv_data->unk3[1] = 0;
1270 IEnumFORMATETC_Reset(enum_fmt);
1273 target_offset = FIELD_OFFSET(ole_priv_data, entries[count + 1]); /* count entries + one pad. */
1275 while(IEnumFORMATETC_Next(enum_fmt, 1, &fmt, NULL) == S_OK)
1277 if (fmt.tymed == TYMED_HGLOBAL)
1280 TRACE("(cfFormat=%d:%s)\n", fmt.cfFormat,
1281 GetClipboardFormatNameA(fmt.cfFormat, fmt_name, sizeof(fmt_name)-1) ? fmt_name : "");
1283 SetClipboardData(fmt.cfFormat, NULL);
1286 priv_data->entries[idx].fmtetc = fmt;
1289 memcpy((char*)priv_data + target_offset, fmt.ptd, fmt.ptd->tdSize);
1290 priv_data->entries[idx].fmtetc.ptd = (DVTARGETDEVICE*)target_offset;
1291 target_offset += fmt.ptd->tdSize;
1292 CoTaskMemFree(fmt.ptd);
1295 priv_data->entries[idx].first_use = !is_format_in_list(priv_data->entries, idx, fmt.cfFormat);
1296 priv_data->entries[idx].unk[0] = 0;
1297 priv_data->entries[idx].unk[1] = 0;
1302 IEnumFORMATETC_Release(enum_fmt);
1304 GlobalUnlock(priv_data_handle);
1305 SetClipboardData(ole_priv_data_clipboard_format, priv_data_handle);
1310 /*********************************************************************
1311 * set_dataobject_format
1313 * Windows creates a 'DataObject' clipboard format that contains the
1314 * clipboard window's HWND or NULL if the Ole clipboard has been flushed.
1316 static HRESULT set_dataobject_format(HWND hwnd)
1318 HGLOBAL h = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, sizeof(hwnd));
1321 if(!h) return E_OUTOFMEMORY;
1323 data = GlobalLock(h);
1327 if(!SetClipboardData(dataobject_clipboard_format, h))
1330 return CLIPBRD_E_CANT_SET;
1336 /*---------------------------------------------------------------------*
1337 * Win32 OLE clipboard API
1338 *---------------------------------------------------------------------*/
1340 /***********************************************************************
1341 * OleSetClipboard [OLE32.@]
1342 * Places a pointer to the specified data object onto the clipboard,
1343 * making the data object accessible to the OleGetClipboard function.
1347 * S_OK IDataObject pointer placed on the clipboard
1348 * CLIPBRD_E_CANT_OPEN OpenClipboard failed
1349 * CLIPBRD_E_CANT_EMPTY EmptyClipboard failed
1350 * CLIPBRD_E_CANT_CLOSE CloseClipboard failed
1351 * CLIPBRD_E_CANT_SET SetClipboard failed
1354 HRESULT WINAPI OleSetClipboard(IDataObject* pDataObj)
1357 struct oletls *info = COM_CurrentInfo();
1359 TRACE("(%p)\n", pDataObj);
1362 WARN("Could not allocate tls\n");
1364 if(!info->ole_inits)
1365 return CO_E_NOTINITIALIZED;
1367 OLEClipbrd_Initialize();
1370 * If the Ole clipboard window hasn't been created yet, create it now.
1372 if ( !theOleClipboard->hWndClipboard )
1373 theOleClipboard->hWndClipboard = OLEClipbrd_CreateWindow();
1375 if ( !theOleClipboard->hWndClipboard ) return E_FAIL;
1377 if ( !OpenClipboard(theOleClipboard->hWndClipboard) ) return CLIPBRD_E_CANT_OPEN;
1380 * Empty the current clipboard and make our window the clipboard owner
1381 * NOTE: This will trigger a WM_DESTROYCLIPBOARD message
1383 if ( !EmptyClipboard() )
1385 hr = CLIPBRD_E_CANT_EMPTY;
1390 * If we are already holding on to an IDataObject first release that.
1392 if ( theOleClipboard->pIDataObjectSrc )
1394 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
1395 theOleClipboard->pIDataObjectSrc = NULL;
1398 /* A NULL value indicates that the clipboard should be emptied. */
1399 theOleClipboard->pIDataObjectSrc = pDataObj;
1402 IDataObject_AddRef(theOleClipboard->pIDataObjectSrc);
1403 hr = set_clipboard_formats(pDataObj);
1404 if(FAILED(hr)) goto end;
1407 hr = set_dataobject_format(theOleClipboard->hWndClipboard);
1411 if ( !CloseClipboard() ) hr = CLIPBRD_E_CANT_CLOSE;
1415 if (theOleClipboard->pIDataObjectSrc)
1417 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
1418 theOleClipboard->pIDataObjectSrc = NULL;
1426 /***********************************************************************
1427 * OleGetClipboard [OLE32.@]
1428 * Returns a pointer to our internal IDataObject which represents the conceptual
1429 * state of the Windows clipboard. If the current clipboard already contains
1430 * an IDataObject, our internal IDataObject will delegate to this object.
1432 HRESULT WINAPI OleGetClipboard(IDataObject** ppDataObj)
1438 * Make sure we have a clipboard object
1440 OLEClipbrd_Initialize();
1442 if (!theOleClipboard)
1443 return E_OUTOFMEMORY;
1445 /* Return a reference counted IDataObject */
1446 hr = IDataObject_QueryInterface( (IDataObject*)&(theOleClipboard->lpvtbl),
1447 &IID_IDataObject, (void**)ppDataObj);
1451 /******************************************************************************
1452 * OleFlushClipboard [OLE32.@]
1453 * Renders the data from the source IDataObject into the windows clipboard
1455 * TODO: OleFlushClipboard needs to additionally handle TYMED_IStorage media
1456 * by copying the storage into global memory. Subsequently the default
1457 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
1458 * back to TYMED_IStorage.
1460 HRESULT WINAPI OleFlushClipboard(void)
1462 IEnumFORMATETC* penumFormatetc = NULL;
1468 OLEClipbrd_Initialize();
1471 * Already flushed or no source DataObject? Nothing to do.
1473 if (!theOleClipboard->pIDataObjectSrc)
1476 if (!OpenClipboard(theOleClipboard->hWndClipboard))
1477 return CLIPBRD_E_CANT_OPEN;
1480 * Render all HGLOBAL formats supported by the source into
1481 * the windows clipboard.
1483 if ( FAILED( hr = IDataObject_EnumFormatEtc( theOleClipboard->pIDataObjectSrc,
1489 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
1491 if ( rgelt.tymed == TYMED_HGLOBAL )
1494 TRACE("(cfFormat=%d:%s)\n", rgelt.cfFormat,
1495 GetClipboardFormatNameA(rgelt.cfFormat, szFmtName, sizeof(szFmtName)-1)
1498 if ( FAILED(render_format( theOleClipboard->pIDataObjectSrc, &rgelt )) )
1503 IEnumFORMATETC_Release(penumFormatetc);
1505 hr = set_dataobject_format(NULL);
1507 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
1508 theOleClipboard->pIDataObjectSrc = NULL;
1512 if ( !CloseClipboard() ) hr = CLIPBRD_E_CANT_CLOSE;
1518 /***********************************************************************
1519 * OleIsCurrentClipboard [OLE32.@]
1521 HRESULT WINAPI OleIsCurrentClipboard(IDataObject *pDataObject)
1525 * Make sure we have a clipboard object
1527 OLEClipbrd_Initialize();
1529 if (!theOleClipboard)
1530 return E_OUTOFMEMORY;
1532 if (pDataObject == NULL)
1535 return (pDataObject == theOleClipboard->pIDataObjectSrc) ? S_OK : S_FALSE;