2 * OLE 2 clipboard support
4 * Copyright 1999 Noel Borthwick <noel@macadamian.com>
7 * This file contains the implementation for the OLE Clipboard and its
8 * internal interfaces. The OLE clipboard interacts with an IDataObject
9 * interface via the OleSetClipboard, OleGetClipboard and
10 * OleIsCurrentClipboard API's. An internal IDataObject delegates
11 * to a client supplied IDataObject or the WIN32 clipboard API depending
12 * on whether OleSetClipboard has been invoked.
13 * Here are some operating scenarios:
15 * 1. OleSetClipboard called: In this case the internal IDataObject
16 * delegates to the client supplied IDataObject. Additionally OLE takes
17 * ownership of the Windows clipboard and any HGLOCBAL IDataObject
18 * items are placed on the Windows clipboard. This allows non OLE aware
19 * applications to access these. A local WinProc fields WM_RENDERFORMAT
20 * and WM_RENDERALLFORMATS messages in this case.
22 * 2. OleGetClipboard called without previous OleSetClipboard. Here the internal
23 * IDataObject functionality wraps around the WIN32 clipboard API.
25 * 3. OleGetClipboard called after previous OleSetClipboard. Here the internal
26 * IDataObject delegates to the source IDataObjects functionality directly,
27 * thereby bypassing the Windows clipboard.
29 * Implementation references : Inside OLE 2'nd edition by Kraig Brockschmidt
32 * - Support for pasting between different processes. OLE clipboard support
33 * currently works only for in process copy and paste. Since we internally
34 * store a pointer to the source's IDataObject and delegate to that, this
35 * will fail if the IDataObject client belongs to a different process.
36 * - IDataObject::GetDataHere is not implemented
37 * - OleFlushClipboard needs to additionally handle TYMED_IStorage media
38 * by copying the storage into global memory. Subsequently the default
39 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
40 * back to TYMED_IStorage.
41 * - OLE1 compatibility formats to be synthesized from OLE2 formats and put on
42 * clipboard in OleSetClipboard.
52 #include "debugtools.h"
55 #define HANDLE_ERROR(err) { hr = err; TRACE("(HRESULT=%lx)\n", (HRESULT)err); goto CLEANUP; }
57 /* For CoGetMalloc (MEMCTX_TASK is currently ignored) */
59 #define MEMCTX_TASK -1
62 DEFAULT_DEBUG_CHANNEL(ole)
64 /****************************************************************************
66 * DO NOT add any members before the VTables declaration!
71 * List all interface VTables here
73 ICOM_VTABLE(IDataObject)* lpvtbl1; /* IDataObject VTable */
76 * The hidden OLE clipboard window. This window is used as the bridge between the
77 * the OLE and windows clipboard API. (Windows creates one such window per process)
82 * Pointer to the source data object (via OleSetClipboard)
84 IDataObject* pIDataObjectSrc;
87 * The registered DataObject clipboard format
92 * The handle to our ourself
97 * Reference count of this object
102 typedef struct OLEClipbrd OLEClipbrd;
105 /****************************************************************************
106 * IEnumFORMATETC implementation
107 * DO NOT add any members before the VTables declaration!
111 /* IEnumFORMATETC VTable */
112 ICOM_VFIELD(IEnumFORMATETC);
114 /* IEnumFORMATETC fields */
115 UINT posFmt; /* current enumerator position */
116 UINT countFmt; /* number of EnumFORMATETC's in array */
117 LPFORMATETC pFmt; /* array of EnumFORMATETC's */
120 * Reference count of this object
125 * IUnknown implementation of the parent data object.
127 IUnknown* pUnkDataObj;
129 } IEnumFORMATETCImpl;
133 * The one and only OLEClipbrd object which is created by OLEClipbrd_Initialize()
135 static HGLOBAL hTheOleClipboard = 0;
136 static OLEClipbrd* theOleClipboard = NULL;
140 * Prototypes for the methods of the OLEClipboard class.
142 extern void OLEClipbrd_Initialize();
143 extern void OLEClipbrd_UnInitialize();
144 static OLEClipbrd* OLEClipbrd_Construct();
145 static void OLEClipbrd_Destroy(OLEClipbrd* ptrToDestroy);
146 static HWND OLEClipbrd_CreateWindow();
147 static void OLEClipbrd_DestroyWindow(HWND hwnd);
148 LRESULT CALLBACK OLEClipbrd_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
149 static HRESULT OLEClipbrd_RenderFormat( IDataObject *pIDataObject, LPFORMATETC pFormatetc );
150 static HGLOBAL OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc );
153 * Prototypes for the methods of the OLEClipboard class
154 * that implement IDataObject methods.
156 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryInterface(
160 static ULONG WINAPI OLEClipbrd_IDataObject_AddRef(
162 static ULONG WINAPI OLEClipbrd_IDataObject_Release(
164 static HRESULT WINAPI OLEClipbrd_IDataObject_GetData(
166 LPFORMATETC pformatetcIn,
168 static HRESULT WINAPI OLEClipbrd_IDataObject_GetDataHere(
170 LPFORMATETC pformatetc,
172 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryGetData(
174 LPFORMATETC pformatetc);
175 static HRESULT WINAPI OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
177 LPFORMATETC pformatectIn,
178 LPFORMATETC pformatetcOut);
179 static HRESULT WINAPI OLEClipbrd_IDataObject_SetData(
181 LPFORMATETC pformatetc,
184 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumFormatEtc(
187 IEnumFORMATETC** ppenumFormatEtc);
188 static HRESULT WINAPI OLEClipbrd_IDataObject_DAdvise(
190 FORMATETC* pformatetc,
192 IAdviseSink* pAdvSink,
193 DWORD* pdwConnection);
194 static HRESULT WINAPI OLEClipbrd_IDataObject_DUnadvise(
197 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumDAdvise(
199 IEnumSTATDATA** ppenumAdvise);
202 * Prototypes for the IEnumFORMATETC methods.
204 static LPENUMFORMATETC OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt, const FORMATETC afmt[],
205 LPUNKNOWN pUnkDataObj);
206 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface(LPENUMFORMATETC iface, REFIID riid,
208 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface);
209 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface);
210 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next(LPENUMFORMATETC iface, ULONG celt,
211 FORMATETC* rgelt, ULONG* pceltFethed);
212 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface, ULONG celt);
213 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface);
214 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone(LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum);
218 * Virtual function table for the OLEClipbrd's exposed IDataObject interface
220 static ICOM_VTABLE(IDataObject) OLEClipbrd_IDataObject_VTable =
222 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
223 OLEClipbrd_IDataObject_QueryInterface,
224 OLEClipbrd_IDataObject_AddRef,
225 OLEClipbrd_IDataObject_Release,
226 OLEClipbrd_IDataObject_GetData,
227 OLEClipbrd_IDataObject_GetDataHere,
228 OLEClipbrd_IDataObject_QueryGetData,
229 OLEClipbrd_IDataObject_GetCanonicalFormatEtc,
230 OLEClipbrd_IDataObject_SetData,
231 OLEClipbrd_IDataObject_EnumFormatEtc,
232 OLEClipbrd_IDataObject_DAdvise,
233 OLEClipbrd_IDataObject_DUnadvise,
234 OLEClipbrd_IDataObject_EnumDAdvise
238 * Virtual function table for IEnumFORMATETC interface
240 static struct ICOM_VTABLE(IEnumFORMATETC) efvt =
242 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
243 OLEClipbrd_IEnumFORMATETC_QueryInterface,
244 OLEClipbrd_IEnumFORMATETC_AddRef,
245 OLEClipbrd_IEnumFORMATETC_Release,
246 OLEClipbrd_IEnumFORMATETC_Next,
247 OLEClipbrd_IEnumFORMATETC_Skip,
248 OLEClipbrd_IEnumFORMATETC_Reset,
249 OLEClipbrd_IEnumFORMATETC_Clone
253 * Name of our registered OLE clipboard window class
255 CHAR OLEClipbrd_WNDCLASS[] = "CLIPBRDWNDCLASS";
258 * If we need to store state info we can store it here.
259 * For now we dont need this functionality.
261 typedef struct tagClipboardWindowInfo
263 } ClipboardWindowInfo;
266 /*---------------------------------------------------------------------*
267 * Win32 OLE clipboard API
268 *---------------------------------------------------------------------*/
270 /***********************************************************************
271 * OleSetClipboard [OLE32.127]
272 * Places a pointer to the specified data object onto the clipboard,
273 * making the data object accessible to the OleGetClipboard function.
277 * S_OK IDataObject pointer placed on the clipboard
278 * CLIPBRD_E_CANT_OPEN OpenClipboard failed
279 * CLIPBRD_E_CANT_EMPTY EmptyClipboard failed
280 * CLIPBRD_E_CANT_CLOSE CloseClipboard failed
281 * CLIPBRD_E_CANT_SET SetClipboard failed
284 HRESULT WINAPI OleSetClipboard(IDataObject* pDataObj)
287 IEnumFORMATETC* penumFormatetc = NULL;
289 BOOL bClipboardOpen = FALSE;
291 HGLOBAL hDataObject = 0;
292 OLEClipbrd **ppDataObject;
295 TRACE("(%p)\n", pDataObj);
298 * Make sure we have a clipboard object
300 OLEClipbrd_Initialize();
303 * If the Ole clipboard window hasn't been created yet, create it now.
305 if ( !theOleClipboard->hWndClipboard )
306 theOleClipboard->hWndClipboard = OLEClipbrd_CreateWindow();
308 if ( !theOleClipboard->hWndClipboard ) /* sanity check */
309 HANDLE_ERROR( E_FAIL );
312 * Open the Windows clipboard, associating it with our hidden window
314 if ( !(bClipboardOpen = OpenClipboard(theOleClipboard->hWndClipboard)) )
315 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
318 * Empty the current clipboard and make our window the clipboard owner
319 * NOTE: This will trigger a WM_DESTROYCLIPBOARD message
321 if ( !EmptyClipboard() )
322 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY );
325 * If we are already holding on to an IDataObject first release that.
327 if ( theOleClipboard->pIDataObjectSrc )
329 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
330 theOleClipboard->pIDataObjectSrc = NULL;
334 * AddRef the data object passed in and save its pointer.
335 * A NULL value indicates that the clipboard should be emptied.
337 theOleClipboard->pIDataObjectSrc = pDataObj;
340 IDataObject_AddRef(theOleClipboard->pIDataObjectSrc);
344 * Enumerate all HGLOBAL formats supported by the source and make
345 * those formats available using delayed rendering using SetClipboardData.
346 * Only global memory based data items may be made available to non-OLE
347 * applications via the standard Windows clipboard API. Data based on other
348 * mediums(non TYMED_HGLOBAL) can only be accessed via the Ole Clipboard API.
350 * TODO: Do we need to additionally handle TYMED_IStorage media by copying
351 * the storage into global memory?
355 if ( FAILED(hr = IDataObject_EnumFormatEtc( pDataObj,
362 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
364 if ( rgelt.tymed == TYMED_HGLOBAL )
367 TRACE("(cfFormat=%d:%s)\n", rgelt.cfFormat,
368 GetClipboardFormatNameA(rgelt.cfFormat, szFmtName, sizeof(szFmtName)-1)
371 SetClipboardData( rgelt.cfFormat, (HANDLE)NULL);
374 IEnumFORMATETC_Release(penumFormatetc);
378 * Windows additionally creates a new "DataObject" clipboard format
379 * and stores in on the clipboard. We could possibly store a pointer
380 * to our internal IDataObject interface on the clipboard. I'm not
381 * sure what the use of this is though.
382 * Enable the code below for this functionality.
385 theOleClipboard->cfDataObj = RegisterClipboardFormatA("DataObject");
386 hDataObject = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT,
387 sizeof(OLEClipbrd *));
389 HANDLE_ERROR( E_OUTOFMEMORY );
391 ppDataObject = (OLEClipbrd**)GlobalLock(hDataObject);
392 *ppDataObject = theOleClipboard;
393 GlobalUnlock(hDataObject);
395 if ( !SetClipboardData( theOleClipboard->cfDataObj, hDataObject ) )
396 HANDLE_ERROR( CLIPBRD_E_CANT_SET );
404 * Close Windows clipboard (It remains associated with our window)
406 if ( bClipboardOpen && !CloseClipboard() )
407 hr = CLIPBRD_E_CANT_CLOSE;
410 * Release the source IDataObject if something failed
414 if (theOleClipboard->pIDataObjectSrc)
416 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
417 theOleClipboard->pIDataObjectSrc = NULL;
425 /***********************************************************************
426 * OleGetClipboard32 [OLE32.105]
427 * Returns a pointer to our internal IDataObject which represents the conceptual
428 * state of the Windows clipboard. If the current clipboard already contains
429 * an IDataObject, our internal IDataObject will delegate to this object.
431 HRESULT WINAPI OleGetClipboard(IDataObject** ppDataObj)
437 * Make sure we have a clipboard object
439 OLEClipbrd_Initialize();
441 if (!theOleClipboard)
442 return E_OUTOFMEMORY;
444 /* Return a reference counted IDataObject */
445 hr = IDataObject_QueryInterface( (IDataObject*)&(theOleClipboard->lpvtbl1),
446 &IID_IDataObject, (void**)ppDataObj);
450 /***********************************************************************
451 * OleFlushClipboard [OLE2.76]
454 HRESULT WINAPI OleFlushClipboard16(void)
457 return OleFlushClipboard();
461 /******************************************************************************
462 * OleFlushClipboard [OLE32.103]
463 * Renders the data from the source IDataObject into the windows clipboard
465 * TODO: OleFlushClipboard needs to additionally handle TYMED_IStorage media
466 * by copying the storage into global memory. Subsequently the default
467 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
468 * back to TYMED_IStorage.
470 HRESULT WINAPI OleFlushClipboard()
472 IEnumFORMATETC* penumFormatetc = NULL;
475 BOOL bClipboardOpen = FALSE;
476 IDataObject* pIDataObjectSrc = NULL;
481 * Make sure we have a clipboard object
483 OLEClipbrd_Initialize();
486 * Already flushed or no source DataObject? Nothing to do.
488 if (!theOleClipboard->pIDataObjectSrc)
492 * Addref and save the source data object we are holding on to temporarily,
493 * since it will be released when we empty the clipboard.
495 pIDataObjectSrc = theOleClipboard->pIDataObjectSrc;
496 IDataObject_AddRef(pIDataObjectSrc);
499 * Open the Windows clipboard
501 if ( !(bClipboardOpen = OpenClipboard(theOleClipboard->hWndClipboard)) )
502 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
505 * Empty the current clipboard
507 if ( !EmptyClipboard() )
508 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY );
511 * Render all HGLOBAL formats supported by the source into
512 * the windows clipboard.
514 if ( FAILED( hr = IDataObject_EnumFormatEtc( pIDataObjectSrc,
521 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
523 if ( rgelt.tymed == TYMED_HGLOBAL )
526 TRACE("(cfFormat=%d:%s)\n", rgelt.cfFormat,
527 GetClipboardFormatNameA(rgelt.cfFormat, szFmtName, sizeof(szFmtName)-1)
531 * Render the clipboard data
533 if ( FAILED(OLEClipbrd_RenderFormat( pIDataObjectSrc, &rgelt )) )
538 IEnumFORMATETC_Release(penumFormatetc);
541 * Release the source data object we are holding on to
543 IDataObject_Release(pIDataObjectSrc);
548 * Close Windows clipboard (It remains associated with our window)
550 if ( bClipboardOpen && !CloseClipboard() )
551 hr = CLIPBRD_E_CANT_CLOSE;
557 /***********************************************************************
558 * OleIsCurrentClipboard32 [OLE32.110]
560 HRESULT WINAPI OleIsCurrentClipboard ( IDataObject *pDataObject)
564 * Make sure we have a clipboard object
566 OLEClipbrd_Initialize();
568 if (!theOleClipboard)
569 return E_OUTOFMEMORY;
571 return (pDataObject == theOleClipboard->pIDataObjectSrc) ? S_OK : S_FALSE;
575 /*---------------------------------------------------------------------*
576 * Internal implementation methods for the OLE clipboard
577 *---------------------------------------------------------------------*/
579 /***********************************************************************
580 * OLEClipbrd_Initialize()
581 * Initializes the OLE clipboard.
583 void OLEClipbrd_Initialize()
586 * Create the clipboard if necesary
588 if ( !theOleClipboard )
591 theOleClipboard = OLEClipbrd_Construct();
596 /***********************************************************************
597 * OLEClipbrd_UnInitialize()
598 * Un-Initializes the OLE clipboard
600 void OLEClipbrd_UnInitialize()
604 * Destroy the clipboard if no one holds a reference to us.
605 * Note that the clipboard was created with a reference count of 1.
607 if ( theOleClipboard && (theOleClipboard->ref <= 1) )
609 OLEClipbrd_Destroy( theOleClipboard );
613 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
618 /*********************************************************
619 * Construct the OLEClipbrd class.
621 static OLEClipbrd* OLEClipbrd_Construct()
623 OLEClipbrd* newObject = NULL;
624 HGLOBAL hNewObject = 0;
627 * Allocate space for the object. We use GlobalAlloc since we need
628 * an HGLOBAL to expose our DataObject as a registered clipboard type.
630 hNewObject = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT,
636 * Lock the handle for the entire lifetime of the clipboard.
638 newObject = GlobalLock(hNewObject);
641 * Initialize the virtual function table.
643 newObject->lpvtbl1 = &OLEClipbrd_IDataObject_VTable;
646 * Start with one reference count. The caller of this function
647 * must release the interface pointer when it is done.
651 newObject->hSelf = hNewObject;
654 * The Ole clipboard is a singleton - save the global handle and pointer
656 theOleClipboard = newObject;
657 hTheOleClipboard = hNewObject;
659 return theOleClipboard;
662 static void OLEClipbrd_Destroy(OLEClipbrd* ptrToDestroy)
670 * Destroy the Ole clipboard window
672 if ( ptrToDestroy->hWndClipboard )
673 OLEClipbrd_DestroyWindow(ptrToDestroy->hWndClipboard);
676 * Free the actual OLE Clipboard structure.
678 TRACE("() - Destroying clipboard data object.\n");
679 GlobalUnlock(ptrToDestroy->hSelf);
680 GlobalFree(ptrToDestroy->hSelf);
683 * The Ole clipboard is a singleton (ptrToDestroy == theOleClipboard)
685 theOleClipboard = NULL;
686 hTheOleClipboard = 0;
690 /***********************************************************************
691 * OLEClipbrd_CreateWindow()
692 * Create the clipboard window
694 static HWND OLEClipbrd_CreateWindow()
701 * Register the clipboard window class if necessary
704 if ( !( atom = GlobalFindAtomA(OLEClipbrd_WNDCLASS) ) ||
705 !( CLASS_FindClassByAtom(atom, 0) ) )
707 ZeroMemory( &wcex, sizeof(WNDCLASSEXA));
709 wcex.cbSize = sizeof(WNDCLASSEXA);
710 /* Windows creates this class with a style mask of 0
711 * We dont bother doing this since the FindClassByAtom code
712 * would have to be changed to deal with this idiosyncracy. */
713 wcex.style = CS_GLOBALCLASS;
714 wcex.lpfnWndProc = (WNDPROC)OLEClipbrd_WndProc;
716 wcex.lpszClassName = OLEClipbrd_WNDCLASS;
718 RegisterClassExA(&wcex);
722 * Create a hidden window to receive OLE clipboard messages
726 * If we need to store state info we can store it here.
727 * For now we dont need this functionality.
728 * ClipboardWindowInfo clipboardInfo;
729 * ZeroMemory( &trackerInfo, sizeof(ClipboardWindowInfo));
732 hwnd = CreateWindowA(OLEClipbrd_WNDCLASS,
734 WS_POPUP | WS_CLIPSIBLINGS | WS_OVERLAPPED,
735 CW_USEDEFAULT, CW_USEDEFAULT,
736 CW_USEDEFAULT, CW_USEDEFAULT,
740 0 /*(LPVOID)&clipboardInfo */);
745 /***********************************************************************
746 * OLEClipbrd_DestroyWindow(HWND)
747 * Destroy the clipboard window and unregister its class
749 static void OLEClipbrd_DestroyWindow(HWND hwnd)
752 * Destroy clipboard window and unregister its WNDCLASS
755 UnregisterClassA( OLEClipbrd_WNDCLASS, 0 );
758 /***********************************************************************
759 * OLEClipbrd_WndProc(HWND, unsigned, WORD, LONG)
760 * Processes messages sent to the OLE clipboard window.
761 * Note that we will intercept messages in our WndProc only when data
762 * has been placed in the clipboard via OleSetClipboard().
763 * i.e. Only when OLE owns the windows clipboard.
765 LRESULT CALLBACK OLEClipbrd_WndProc
766 (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
772 * We receive this message to allow us to handle delayed rendering of
773 * a specific clipboard format when an application requests data in
774 * that format by calling GetClipboardData.
775 * (Recall that in OleSetClipboard, we used SetClipboardData to
776 * make all HGLOBAL formats supported by the source IDataObject
777 * available using delayed rendering)
778 * On receiving this mesage we must actually render the data in the
779 * specified format and place it on the clipboard by calling the
780 * SetClipboardData function.
782 case WM_RENDERFORMAT:
786 ZeroMemory( &rgelt, sizeof(FORMATETC));
789 * Initialize FORMATETC to a Windows clipboard friendly format
791 rgelt.cfFormat = (UINT) wParam;
792 rgelt.dwAspect = DVASPECT_CONTENT;
794 rgelt.tymed = TYMED_HGLOBAL;
796 TRACE("(): WM_RENDERFORMAT(cfFormat=%d)\n", rgelt.cfFormat);
799 * Render the clipboard data.
800 * (We must have a source data object or we wouldn't be in this WndProc)
802 OLEClipbrd_RenderFormat( (IDataObject*)&(theOleClipboard->lpvtbl1), &rgelt );
808 * WM_RENDERALLFORMATS
809 * Sent before the clipboard owner window is destroyed.
810 * We should receive this message only when OleUninitialize is called
811 * while we have an IDataObject in the clipboard.
812 * For the content of the clipboard to remain available to other
813 * applications, we must render data in all the formats the source IDataObject
814 * is capable of generating, and place the data on the clipboard by calling
817 case WM_RENDERALLFORMATS:
819 IEnumFORMATETC* penumFormatetc = NULL;
822 TRACE("(): WM_RENDERALLFORMATS\n");
825 * Render all HGLOBAL formats supported by the source into
826 * the windows clipboard.
828 if ( FAILED( IDataObject_EnumFormatEtc( (IDataObject*)&(theOleClipboard->lpvtbl1),
829 DATADIR_GET, &penumFormatetc) ) )
831 WARN("(): WM_RENDERALLFORMATS failed to retrieve EnumFormatEtc!\n");
835 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
837 if ( rgelt.tymed == TYMED_HGLOBAL )
840 * Render the clipboard data.
842 if ( FAILED(OLEClipbrd_RenderFormat( (IDataObject*)&(theOleClipboard->lpvtbl1), &rgelt )) )
845 TRACE("(): WM_RENDERALLFORMATS(cfFormat=%d)\n", rgelt.cfFormat);
849 IEnumFORMATETC_Release(penumFormatetc);
855 * WM_DESTROYCLIPBOARD
856 * This is sent by EmptyClipboard before the clipboard is emptied.
857 * We should release any IDataObject we are holding onto when we receive
858 * this message, since it indicates that the OLE clipboard should be empty
859 * from this point on.
861 case WM_DESTROYCLIPBOARD:
863 TRACE("(): WM_DESTROYCLIPBOARD\n");
865 * Release the data object we are holding on to
867 if ( theOleClipboard->pIDataObjectSrc )
869 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
870 theOleClipboard->pIDataObjectSrc = NULL;
876 case WM_ASKCBFORMATNAME:
877 case WM_CHANGECBCHAIN:
878 case WM_DRAWCLIPBOARD:
879 case WM_SIZECLIPBOARD:
880 case WM_HSCROLLCLIPBOARD:
881 case WM_VSCROLLCLIPBOARD:
882 case WM_PAINTCLIPBOARD:
885 return DefWindowProcA(hWnd, message, wParam, lParam);
892 /***********************************************************************
893 * OLEClipbrd_RenderFormat(LPFORMATETC)
894 * Render the clipboard data. Note that this call will delegate to the
895 * source data object.
896 * Note: This function assumes it is passed an HGLOBAL format to render.
898 static HRESULT OLEClipbrd_RenderFormat(IDataObject *pIDataObject, LPFORMATETC pFormatetc)
904 if ( FAILED(hr = IDataObject_GetData(pIDataObject, pFormatetc, &medium)) )
906 WARN("() : IDataObject_GetData failed to render clipboard data! (%lx)\n", hr);
911 * Put a copy of the rendered data back on the clipboard
914 if ( !(hDup = OLEClipbrd_GlobalDupMem(medium.u.hGlobal)) )
915 HANDLE_ERROR( E_OUTOFMEMORY );
917 if ( !SetClipboardData( pFormatetc->cfFormat, hDup ) )
920 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
925 ReleaseStgMedium(&medium);
931 /***********************************************************************
932 * OLEClipbrd_GlobalDupMem( HGLOBAL )
933 * Helper method to duplicate an HGLOBAL chunk of memory
935 static HGLOBAL OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc )
938 PVOID pGlobalSrc, pGlobalDest;
944 cBytes = GlobalSize(hGlobalSrc);
948 hGlobalDest = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE,
953 pGlobalSrc = GlobalLock(hGlobalSrc);
954 pGlobalDest = GlobalLock(hGlobalDest);
955 if ( !pGlobalSrc || !pGlobalDest )
958 memcpy(pGlobalDest, pGlobalSrc, cBytes);
960 GlobalUnlock(hGlobalSrc);
961 GlobalUnlock(hGlobalDest);
967 /*---------------------------------------------------------------------*
968 * Implementation of the internal IDataObject interface exposed by
970 *---------------------------------------------------------------------*/
973 /************************************************************************
974 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
976 * See Windows documentation for more details on IUnknown methods.
978 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryInterface(
984 * Declare "This" pointer
986 ICOM_THIS(OLEClipbrd, iface);
987 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObject);
990 * Perform a sanity check on the parameters.
992 if ( (This==0) || (ppvObject==0) )
996 * Initialize the return parameter.
1001 * Compare the riid with the interface IDs implemented by this object.
1003 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
1007 else if (memcmp(&IID_IDataObject, riid, sizeof(IID_IDataObject)) == 0)
1009 *ppvObject = (IDataObject*)&(This->lpvtbl1);
1011 else /* We only support IUnknown and IDataObject */
1013 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
1014 return E_NOINTERFACE;
1018 * Query Interface always increases the reference count by one when it is
1021 IUnknown_AddRef((IUnknown*)*ppvObject);
1026 /************************************************************************
1027 * OLEClipbrd_IDataObject_AddRef (IUnknown)
1029 * See Windows documentation for more details on IUnknown methods.
1031 static ULONG WINAPI OLEClipbrd_IDataObject_AddRef(
1035 * Declare "This" pointer
1037 ICOM_THIS(OLEClipbrd, iface);
1039 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1046 /************************************************************************
1047 * OLEClipbrd_IDataObject_Release (IUnknown)
1049 * See Windows documentation for more details on IUnknown methods.
1051 static ULONG WINAPI OLEClipbrd_IDataObject_Release(
1055 * Declare "This" pointer
1057 ICOM_THIS(OLEClipbrd, iface);
1059 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1062 * Decrease the reference count on this object.
1067 * If the reference count goes down to 0, perform suicide.
1071 OLEClipbrd_Destroy(This);
1078 /************************************************************************
1079 * OLEClipbrd_IDataObject_GetData (IDataObject)
1081 * The OLE Clipboard's implementation of this method delegates to
1082 * a data source if there is one or wraps around the windows clipboard
1084 * See Windows documentation for more details on IDataObject methods.
1086 static HRESULT WINAPI OLEClipbrd_IDataObject_GetData(
1088 LPFORMATETC pformatetcIn,
1092 BOOL bClipboardOpen = FALSE;
1096 * Declare "This" pointer
1098 ICOM_THIS(OLEClipbrd, iface);
1100 if ( !pformatetcIn || !pformatetcIn || !pmedium )
1101 return E_INVALIDARG;
1103 TRACE("(%p, %p)\n", iface, pformatetcIn);
1106 * If we have a data source placed on the clipboard (via OleSetClipboard)
1107 * simply delegate to the source object's QueryGetData
1108 * NOTE: This code assumes that the IDataObject is in the same address space!
1109 * We will need to add marshalling support when Wine handles multiple processes.
1111 if ( This->pIDataObjectSrc )
1113 return IDataObject_GetData(This->pIDataObjectSrc, pformatetcIn, pmedium);
1116 if ( pformatetcIn->lindex != -1 )
1118 if ( (pformatetcIn->tymed & TYMED_HGLOBAL) != TYMED_HGLOBAL )
1121 if ( pformatetcIn->dwAspect != DVASPECT_CONTENT )
1122 return DV_E_DVASPECT;
1126 * Otherwise, get the data from the windows clipboard using GetClipboardData
1128 if ( !(bClipboardOpen = OpenClipboard(theOleClipboard->hWndClipboard)) )
1129 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
1131 hData = GetClipboardData(pformatetcIn->cfFormat);
1134 * Return the clipboard data in the storage medium structure
1136 pmedium->tymed = (hData == 0) ? TYMED_NULL : TYMED_HGLOBAL;
1137 pmedium->u.hGlobal = (HGLOBAL)hData;
1138 pmedium->pUnkForRelease = NULL;
1144 * Close Windows clipboard
1146 if ( bClipboardOpen && !CloseClipboard() )
1147 hr = CLIPBRD_E_CANT_CLOSE;
1151 return (hData == 0) ? DV_E_FORMATETC : S_OK;
1154 static HRESULT WINAPI OLEClipbrd_IDataObject_GetDataHere(
1156 LPFORMATETC pformatetc,
1163 /************************************************************************
1164 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
1166 * The OLE Clipboard's implementation of this method delegates to
1167 * a data source if there is one or wraps around the windows clipboard
1168 * function IsClipboardFormatAvailable() otherwise.
1170 * See Windows documentation for more details on IDataObject methods.
1172 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryGetData(
1174 LPFORMATETC pformatetc)
1177 * Declare "This" pointer
1179 ICOM_THIS(OLEClipbrd, iface);
1181 TRACE("(%p, %p)\n", iface, pformatetc);
1184 * If we have a data source placed on the clipboard (via OleSetClipboard)
1185 * simply delegate to the source object's QueryGetData
1187 if ( This->pIDataObjectSrc )
1189 return IDataObject_QueryGetData(This->pIDataObjectSrc, pformatetc);
1193 return E_INVALIDARG;
1195 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
1196 return DV_E_DVASPECT;
1198 if ( pformatetc->lindex != -1 )
1201 /* TODO: Handle TYMED_IStorage media which were put on the clipboard
1202 * by copying the storage into global memory. We must convert this
1203 * TYMED_HGLOBAL back to TYMED_IStorage.
1205 if ( pformatetc->tymed != TYMED_HGLOBAL )
1209 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
1211 return (IsClipboardFormatAvailable(pformatetc->cfFormat)) ? S_OK : DV_E_FORMATETC;
1214 /************************************************************************
1215 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
1217 * See Windows documentation for more details on IDataObject methods.
1219 static HRESULT WINAPI OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
1221 LPFORMATETC pformatectIn,
1222 LPFORMATETC pformatetcOut)
1224 TRACE("(%p, %p, %p)\n", iface, pformatectIn, pformatetcOut);
1226 if ( !pformatectIn || !pformatetcOut )
1227 return E_INVALIDARG;
1229 memcpy(pformatetcOut, pformatectIn, sizeof(FORMATETC));
1230 return DATA_S_SAMEFORMATETC;
1233 /************************************************************************
1234 * OLEClipbrd_IDataObject_SetData (IDataObject)
1236 * The OLE Clipboard's does not implement this method
1238 * See Windows documentation for more details on IDataObject methods.
1240 static HRESULT WINAPI OLEClipbrd_IDataObject_SetData(
1242 LPFORMATETC pformatetc,
1249 /************************************************************************
1250 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
1252 * See Windows documentation for more details on IDataObject methods.
1254 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumFormatEtc(
1257 IEnumFORMATETC** ppenumFormatEtc)
1260 FORMATETC *afmt = NULL;
1263 BOOL bClipboardOpen;
1266 * Declare "This" pointer
1268 ICOM_THIS(OLEClipbrd, iface);
1270 TRACE("(%p, %lx, %p)\n", iface, dwDirection, ppenumFormatEtc);
1273 * If we have a data source placed on the clipboard (via OleSetClipboard)
1274 * simply delegate to the source object's EnumFormatEtc
1276 if ( This->pIDataObjectSrc )
1278 return IDataObject_EnumFormatEtc(This->pIDataObjectSrc,
1279 dwDirection, ppenumFormatEtc);
1283 * Otherwise we must provide our own enumerator which wraps around the
1284 * Windows clipboard function EnumClipboardFormats
1286 if ( !ppenumFormatEtc )
1287 return E_INVALIDARG;
1289 if ( dwDirection != DATADIR_GET ) /* IDataObject_SetData not implemented */
1293 * Store all current clipboard formats in an array of FORMATETC's,
1294 * and create an IEnumFORMATETC enumerator from this list.
1296 cfmt = CountClipboardFormats();
1297 afmt = (FORMATETC *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1298 sizeof(FORMATETC) * cfmt);
1300 * Open the Windows clipboard, associating it with our hidden window
1302 if ( !(bClipboardOpen = OpenClipboard(This->hWndClipboard)) )
1303 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
1306 * Store all current clipboard formats in an array of FORMATETC's
1307 * TODO: Handle TYMED_IStorage media which were put on the clipboard
1308 * by copying the storage into global memory. We must convert this
1309 * TYMED_HGLOBAL back to TYMED_IStorage.
1311 for (i = 0, format = 0; i < cfmt; i++)
1313 format = EnumClipboardFormats(format);
1314 if (!format) /* Failed! */
1316 ERR("EnumClipboardFormats failed to return format!\n");
1317 HANDLE_ERROR( E_FAIL );
1320 /* Init the FORMATETC struct */
1321 afmt[i].cfFormat = format;
1323 afmt[i].dwAspect = DVASPECT_CONTENT;
1324 afmt[i].lindex = -1;
1325 afmt[i].tymed = TYMED_HGLOBAL;
1329 * Create an EnumFORMATETC enumerator and return an
1330 * EnumFORMATETC after bumping up its ref count
1332 *ppenumFormatEtc = OLEClipbrd_IEnumFORMATETC_Construct( cfmt, afmt, (LPUNKNOWN)iface);
1333 if (!(*ppenumFormatEtc))
1334 HANDLE_ERROR( E_OUTOFMEMORY );
1336 if (FAILED( hr = IEnumFORMATETC_AddRef(*ppenumFormatEtc)))
1343 * Free the array of FORMATETC's
1346 HeapFree(GetProcessHeap(), 0, afmt);
1349 * Close Windows clipboard
1351 if ( bClipboardOpen && !CloseClipboard() )
1352 hr = CLIPBRD_E_CANT_CLOSE;
1357 /************************************************************************
1358 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
1360 * The OLE Clipboard's does not implement this method
1362 * See Windows documentation for more details on IDataObject methods.
1364 static HRESULT WINAPI OLEClipbrd_IDataObject_DAdvise(
1366 FORMATETC* pformatetc,
1368 IAdviseSink* pAdvSink,
1369 DWORD* pdwConnection)
1374 /************************************************************************
1375 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
1377 * The OLE Clipboard's does not implement this method
1379 * See Windows documentation for more details on IDataObject methods.
1381 static HRESULT WINAPI OLEClipbrd_IDataObject_DUnadvise(
1388 /************************************************************************
1389 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1391 * The OLE Clipboard does not implement this method
1393 * See Windows documentation for more details on IDataObject methods.
1395 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumDAdvise(
1397 IEnumSTATDATA** ppenumAdvise)
1403 /*---------------------------------------------------------------------*
1404 * Implementation of the internal IEnumFORMATETC interface returned by
1405 * the OLE clipboard's IDataObject.
1406 *---------------------------------------------------------------------*/
1408 /************************************************************************
1409 * OLEClipbrd_IEnumFORMATETC_Construct (UINT, const FORMATETC, LPUNKNOWN)
1411 * Creates an IEnumFORMATETC enumerator from an array of FORMATETC
1412 * Structures. pUnkOuter is the outer unknown for reference counting only.
1413 * NOTE: this does not AddRef the interface.
1416 LPENUMFORMATETC OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt, const FORMATETC afmt[],
1417 LPUNKNOWN pUnkDataObj)
1419 IEnumFORMATETCImpl* ef;
1420 DWORD size=cfmt * sizeof(FORMATETC);
1423 ef = (IEnumFORMATETCImpl*)HeapAlloc(GetProcessHeap(),
1425 sizeof(IEnumFORMATETCImpl));
1430 ICOM_VTBL(ef) = &efvt;
1431 ef->pUnkDataObj = pUnkDataObj;
1434 ef->countFmt = cfmt;
1435 if (FAILED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
1437 ef->pFmt = (LPFORMATETC)IMalloc_Alloc(pIMalloc, size);
1438 IMalloc_Release(pIMalloc);
1441 memcpy(ef->pFmt, afmt, size);
1443 TRACE("(%p)->()\n",ef);
1444 return (LPENUMFORMATETC)ef;
1448 /************************************************************************
1449 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
1451 * See Windows documentation for more details on IUnknown methods.
1453 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
1454 (LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
1456 ICOM_THIS(IEnumFORMATETCImpl,iface);
1458 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
1461 * Since enumerators are seperate objects from the parent data object
1462 * we only need to support the IUnknown and IEnumFORMATETC interfaces
1467 if(IsEqualIID(riid, &IID_IUnknown))
1471 else if(IsEqualIID(riid, &IID_IEnumFORMATETC))
1473 *ppvObj = (IDataObject*)This;
1478 IEnumFORMATETC_AddRef((IEnumFORMATETC*)*ppvObj);
1479 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1483 TRACE("-- Interface: E_NOINTERFACE\n");
1484 return E_NOINTERFACE;
1487 /************************************************************************
1488 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
1490 * Since enumerating formats only makes sense when our data object is around,
1491 * we insure that it stays as long as we stay by calling our parents IUnknown
1492 * for AddRef and Release. But since we are not controlled by the lifetime of
1493 * the outer object, we still keep our own reference count in order to
1496 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface)
1498 ICOM_THIS(IEnumFORMATETCImpl,iface);
1499 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1501 if (This->pUnkDataObj)
1502 IUnknown_AddRef(This->pUnkDataObj);
1504 return ++(This->ref);
1507 /************************************************************************
1508 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
1510 * See Windows documentation for more details on IUnknown methods.
1512 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface)
1514 ICOM_THIS(IEnumFORMATETCImpl,iface);
1517 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1519 if (This->pUnkDataObj)
1520 IUnknown_Release(This->pUnkDataObj); /* Release parent data object */
1524 TRACE("() - destroying IEnumFORMATETC(%p)\n",This);
1525 if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
1527 IMalloc_Free(pIMalloc, This->pFmt);
1528 IMalloc_Release(pIMalloc);
1531 HeapFree(GetProcessHeap(),0,This);
1538 /************************************************************************
1539 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
1541 * Standard enumerator members for IEnumFORMATETC
1543 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
1544 (LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
1546 ICOM_THIS(IEnumFORMATETCImpl,iface);
1548 HRESULT hres = S_FALSE;
1550 TRACE("(%p)->(pos=%u)\n", This, This->posFmt);
1552 if (This->posFmt < This->countFmt)
1554 cfetch = This->countFmt - This->posFmt;
1561 memcpy(rgelt, &This->pFmt[This->posFmt], cfetch * sizeof(FORMATETC));
1562 This->posFmt += cfetch;
1571 *pceltFethed = cfetch;
1577 /************************************************************************
1578 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
1580 * Standard enumerator members for IEnumFORMATETC
1582 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface, ULONG celt)
1584 ICOM_THIS(IEnumFORMATETCImpl,iface);
1585 TRACE("(%p)->(num=%lu)\n", This, celt);
1587 This->posFmt += celt;
1588 if (This->posFmt > This->countFmt)
1590 This->posFmt = This->countFmt;
1596 /************************************************************************
1597 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
1599 * Standard enumerator members for IEnumFORMATETC
1601 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface)
1603 ICOM_THIS(IEnumFORMATETCImpl,iface);
1604 TRACE("(%p)->()\n", This);
1610 /************************************************************************
1611 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
1613 * Standard enumerator members for IEnumFORMATETC
1615 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
1616 (LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum)
1618 ICOM_THIS(IEnumFORMATETCImpl,iface);
1621 TRACE("(%p)->(ppenum=%p)\n", This, ppenum);
1624 return E_INVALIDARG;
1626 *ppenum = OLEClipbrd_IEnumFORMATETC_Construct(This->countFmt,
1630 if (FAILED( hr = IEnumFORMATETC_AddRef(*ppenum)))
1633 return (*ppenum) ? S_OK : E_OUTOFMEMORY;