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.
53 #include "debugtools.h"
56 #define HANDLE_ERROR(err) { hr = err; TRACE("(HRESULT=%lx)\n", (HRESULT)err); goto CLEANUP; }
58 /* For CoGetMalloc (MEMCTX_TASK is currently ignored) */
60 #define MEMCTX_TASK -1
63 DEFAULT_DEBUG_CHANNEL(ole
)
65 /****************************************************************************
67 * DO NOT add any members before the VTables declaration!
72 * List all interface VTables here
74 ICOM_VTABLE(IDataObject
)* lpvtbl1
; /* IDataObject VTable */
77 * The hidden OLE clipboard window. This window is used as the bridge between the
78 * the OLE and windows clipboard API. (Windows creates one such window per process)
83 * Pointer to the source data object (via OleSetClipboard)
85 IDataObject
* pIDataObjectSrc
;
88 * The registered DataObject clipboard format
93 * The handle to our ourself
98 * Reference count of this object
103 typedef struct OLEClipbrd OLEClipbrd
;
106 /****************************************************************************
107 * IEnumFORMATETC implementation
108 * DO NOT add any members before the VTables declaration!
112 /* IEnumFORMATETC VTable */
113 ICOM_VFIELD(IEnumFORMATETC
);
115 /* IEnumFORMATETC fields */
116 UINT posFmt
; /* current enumerator position */
117 UINT countFmt
; /* number of EnumFORMATETC's in array */
118 LPFORMATETC pFmt
; /* array of EnumFORMATETC's */
121 * Reference count of this object
126 * IUnknown implementation of the parent data object.
128 IUnknown
* pUnkDataObj
;
130 } IEnumFORMATETCImpl
;
134 * The one and only OLEClipbrd object which is created by OLEClipbrd_Initialize()
136 static HGLOBAL hTheOleClipboard
= 0;
137 static OLEClipbrd
* theOleClipboard
= NULL
;
141 * Prototypes for the methods of the OLEClipboard class.
143 extern void OLEClipbrd_Initialize();
144 extern void OLEClipbrd_UnInitialize();
145 static OLEClipbrd
* OLEClipbrd_Construct();
146 static void OLEClipbrd_Destroy(OLEClipbrd
* ptrToDestroy
);
147 static HWND
OLEClipbrd_CreateWindow();
148 static void OLEClipbrd_DestroyWindow(HWND hwnd
);
149 LRESULT CALLBACK
OLEClipbrd_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
);
150 static HRESULT
OLEClipbrd_RenderFormat( IDataObject
*pIDataObject
, LPFORMATETC pFormatetc
);
151 static HGLOBAL
OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc
);
154 * Prototypes for the methods of the OLEClipboard class
155 * that implement IDataObject methods.
157 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryInterface(
161 static ULONG WINAPI
OLEClipbrd_IDataObject_AddRef(
163 static ULONG WINAPI
OLEClipbrd_IDataObject_Release(
165 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetData(
167 LPFORMATETC pformatetcIn
,
169 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetDataHere(
171 LPFORMATETC pformatetc
,
173 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryGetData(
175 LPFORMATETC pformatetc
);
176 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
178 LPFORMATETC pformatectIn
,
179 LPFORMATETC pformatetcOut
);
180 static HRESULT WINAPI
OLEClipbrd_IDataObject_SetData(
182 LPFORMATETC pformatetc
,
185 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumFormatEtc(
188 IEnumFORMATETC
** ppenumFormatEtc
);
189 static HRESULT WINAPI
OLEClipbrd_IDataObject_DAdvise(
191 FORMATETC
* pformatetc
,
193 IAdviseSink
* pAdvSink
,
194 DWORD
* pdwConnection
);
195 static HRESULT WINAPI
OLEClipbrd_IDataObject_DUnadvise(
198 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumDAdvise(
200 IEnumSTATDATA
** ppenumAdvise
);
203 * Prototypes for the IEnumFORMATETC methods.
205 static LPENUMFORMATETC
OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt
, const FORMATETC afmt
[],
206 LPUNKNOWN pUnkDataObj
);
207 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_QueryInterface(LPENUMFORMATETC iface
, REFIID riid
,
209 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface
);
210 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface
);
211 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Next(LPENUMFORMATETC iface
, ULONG celt
,
212 FORMATETC
* rgelt
, ULONG
* pceltFethed
);
213 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface
, ULONG celt
);
214 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface
);
215 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Clone(LPENUMFORMATETC iface
, LPENUMFORMATETC
* ppenum
);
219 * Virtual function table for the OLEClipbrd's exposed IDataObject interface
221 static ICOM_VTABLE(IDataObject
) OLEClipbrd_IDataObject_VTable
=
223 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
224 OLEClipbrd_IDataObject_QueryInterface
,
225 OLEClipbrd_IDataObject_AddRef
,
226 OLEClipbrd_IDataObject_Release
,
227 OLEClipbrd_IDataObject_GetData
,
228 OLEClipbrd_IDataObject_GetDataHere
,
229 OLEClipbrd_IDataObject_QueryGetData
,
230 OLEClipbrd_IDataObject_GetCanonicalFormatEtc
,
231 OLEClipbrd_IDataObject_SetData
,
232 OLEClipbrd_IDataObject_EnumFormatEtc
,
233 OLEClipbrd_IDataObject_DAdvise
,
234 OLEClipbrd_IDataObject_DUnadvise
,
235 OLEClipbrd_IDataObject_EnumDAdvise
239 * Virtual function table for IEnumFORMATETC interface
241 static struct ICOM_VTABLE(IEnumFORMATETC
) efvt
=
243 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
244 OLEClipbrd_IEnumFORMATETC_QueryInterface
,
245 OLEClipbrd_IEnumFORMATETC_AddRef
,
246 OLEClipbrd_IEnumFORMATETC_Release
,
247 OLEClipbrd_IEnumFORMATETC_Next
,
248 OLEClipbrd_IEnumFORMATETC_Skip
,
249 OLEClipbrd_IEnumFORMATETC_Reset
,
250 OLEClipbrd_IEnumFORMATETC_Clone
254 * Name of our registered OLE clipboard window class
256 CHAR OLEClipbrd_WNDCLASS
[] = "CLIPBRDWNDCLASS";
259 * If we need to store state info we can store it here.
260 * For now we dont need this functionality.
262 typedef struct tagClipboardWindowInfo
264 } ClipboardWindowInfo;
267 /*---------------------------------------------------------------------*
268 * Win32 OLE clipboard API
269 *---------------------------------------------------------------------*/
271 /***********************************************************************
272 * OleSetClipboard [OLE32.127]
273 * Places a pointer to the specified data object onto the clipboard,
274 * making the data object accessible to the OleGetClipboard function.
278 * S_OK IDataObject pointer placed on the clipboard
279 * CLIPBRD_E_CANT_OPEN OpenClipboard failed
280 * CLIPBRD_E_CANT_EMPTY EmptyClipboard failed
281 * CLIPBRD_E_CANT_CLOSE CloseClipboard failed
282 * CLIPBRD_E_CANT_SET SetClipboard failed
285 HRESULT WINAPI
OleSetClipboard(IDataObject
* pDataObj
)
288 IEnumFORMATETC
* penumFormatetc
= NULL
;
290 BOOL bClipboardOpen
= FALSE
;
292 HGLOBAL hDataObject = 0;
293 OLEClipbrd **ppDataObject;
296 TRACE("(%p)\n", pDataObj
);
299 * Make sure we have a clipboard object
301 OLEClipbrd_Initialize();
304 * If the Ole clipboard window hasn't been created yet, create it now.
306 if ( !theOleClipboard
->hWndClipboard
)
307 theOleClipboard
->hWndClipboard
= OLEClipbrd_CreateWindow();
309 if ( !theOleClipboard
->hWndClipboard
) /* sanity check */
310 HANDLE_ERROR( E_FAIL
);
313 * Open the Windows clipboard, associating it with our hidden window
315 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
316 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
319 * Empty the current clipboard and make our window the clipboard owner
320 * NOTE: This will trigger a WM_DESTROYCLIPBOARD message
322 if ( !EmptyClipboard() )
323 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY
);
326 * If we are already holding on to an IDataObject first release that.
328 if ( theOleClipboard
->pIDataObjectSrc
)
330 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
331 theOleClipboard
->pIDataObjectSrc
= NULL
;
335 * AddRef the data object passed in and save its pointer.
336 * A NULL value indicates that the clipboard should be emptied.
338 theOleClipboard
->pIDataObjectSrc
= pDataObj
;
341 IDataObject_AddRef(theOleClipboard
->pIDataObjectSrc
);
345 * Enumerate all HGLOBAL formats supported by the source and make
346 * those formats available using delayed rendering using SetClipboardData.
347 * Only global memory based data items may be made available to non-OLE
348 * applications via the standard Windows clipboard API. Data based on other
349 * mediums(non TYMED_HGLOBAL) can only be accessed via the Ole Clipboard API.
351 * TODO: Do we need to additionally handle TYMED_IStorage media by copying
352 * the storage into global memory?
356 if ( FAILED(hr
= IDataObject_EnumFormatEtc( pDataObj
,
363 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
365 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
368 TRACE("(cfFormat=%d:%s)\n", rgelt
.cfFormat
,
369 GetClipboardFormatNameA(rgelt
.cfFormat
, szFmtName
, sizeof(szFmtName
)-1)
372 SetClipboardData( rgelt
.cfFormat
, (HANDLE
)NULL
);
375 IEnumFORMATETC_Release(penumFormatetc
);
379 * Windows additionally creates a new "DataObject" clipboard format
380 * and stores in on the clipboard. We could possibly store a pointer
381 * to our internal IDataObject interface on the clipboard. I'm not
382 * sure what the use of this is though.
383 * Enable the code below for this functionality.
386 theOleClipboard->cfDataObj = RegisterClipboardFormatA("DataObject");
387 hDataObject = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT,
388 sizeof(OLEClipbrd *));
390 HANDLE_ERROR( E_OUTOFMEMORY );
392 ppDataObject = (OLEClipbrd**)GlobalLock(hDataObject);
393 *ppDataObject = theOleClipboard;
394 GlobalUnlock(hDataObject);
396 if ( !SetClipboardData( theOleClipboard->cfDataObj, hDataObject ) )
397 HANDLE_ERROR( CLIPBRD_E_CANT_SET );
405 * Close Windows clipboard (It remains associated with our window)
407 if ( bClipboardOpen
&& !CloseClipboard() )
408 hr
= CLIPBRD_E_CANT_CLOSE
;
411 * Release the source IDataObject if something failed
415 if (theOleClipboard
->pIDataObjectSrc
)
417 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
418 theOleClipboard
->pIDataObjectSrc
= NULL
;
426 /***********************************************************************
427 * OleGetClipboard32 [OLE32.105]
428 * Returns a pointer to our internal IDataObject which represents the conceptual
429 * state of the Windows clipboard. If the current clipboard already contains
430 * an IDataObject, our internal IDataObject will delegate to this object.
432 HRESULT WINAPI
OleGetClipboard(IDataObject
** ppDataObj
)
438 * Make sure we have a clipboard object
440 OLEClipbrd_Initialize();
442 if (!theOleClipboard
)
443 return E_OUTOFMEMORY
;
445 /* Return a reference counted IDataObject */
446 hr
= IDataObject_QueryInterface( (IDataObject
*)&(theOleClipboard
->lpvtbl1
),
447 &IID_IDataObject
, (void**)ppDataObj
);
451 /***********************************************************************
452 * OleFlushClipboard [OLE2.76]
455 HRESULT WINAPI
OleFlushClipboard16(void)
458 return OleFlushClipboard();
462 /******************************************************************************
463 * OleFlushClipboard [OLE32.103]
464 * Renders the data from the source IDataObject into the windows clipboard
466 * TODO: OleFlushClipboard needs to additionally handle TYMED_IStorage media
467 * by copying the storage into global memory. Subsequently the default
468 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
469 * back to TYMED_IStorage.
471 HRESULT WINAPI
OleFlushClipboard()
473 IEnumFORMATETC
* penumFormatetc
= NULL
;
476 BOOL bClipboardOpen
= FALSE
;
477 IDataObject
* pIDataObjectSrc
= NULL
;
482 * Make sure we have a clipboard object
484 OLEClipbrd_Initialize();
487 * Already flushed or no source DataObject? Nothing to do.
489 if (!theOleClipboard
->pIDataObjectSrc
)
493 * Addref and save the source data object we are holding on to temporarily,
494 * since it will be released when we empty the clipboard.
496 pIDataObjectSrc
= theOleClipboard
->pIDataObjectSrc
;
497 IDataObject_AddRef(pIDataObjectSrc
);
500 * Open the Windows clipboard
502 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
503 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
506 * Empty the current clipboard
508 if ( !EmptyClipboard() )
509 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY
);
512 * Render all HGLOBAL formats supported by the source into
513 * the windows clipboard.
515 if ( FAILED( hr
= IDataObject_EnumFormatEtc( pIDataObjectSrc
,
522 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
524 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
527 TRACE("(cfFormat=%d:%s)\n", rgelt
.cfFormat
,
528 GetClipboardFormatNameA(rgelt
.cfFormat
, szFmtName
, sizeof(szFmtName
)-1)
532 * Render the clipboard data
534 if ( FAILED(OLEClipbrd_RenderFormat( pIDataObjectSrc
, &rgelt
)) )
539 IEnumFORMATETC_Release(penumFormatetc
);
542 * Release the source data object we are holding on to
544 IDataObject_Release(pIDataObjectSrc
);
549 * Close Windows clipboard (It remains associated with our window)
551 if ( bClipboardOpen
&& !CloseClipboard() )
552 hr
= CLIPBRD_E_CANT_CLOSE
;
558 /***********************************************************************
559 * OleIsCurrentClipboard32 [OLE32.110]
561 HRESULT WINAPI
OleIsCurrentClipboard ( IDataObject
*pDataObject
)
565 * Make sure we have a clipboard object
567 OLEClipbrd_Initialize();
569 if (!theOleClipboard
)
570 return E_OUTOFMEMORY
;
572 return (pDataObject
== theOleClipboard
->pIDataObjectSrc
) ? S_OK
: S_FALSE
;
576 /*---------------------------------------------------------------------*
577 * Internal implementation methods for the OLE clipboard
578 *---------------------------------------------------------------------*/
580 /***********************************************************************
581 * OLEClipbrd_Initialize()
582 * Initializes the OLE clipboard.
584 void OLEClipbrd_Initialize()
587 * Create the clipboard if necesary
589 if ( !theOleClipboard
)
592 theOleClipboard
= OLEClipbrd_Construct();
597 /***********************************************************************
598 * OLEClipbrd_UnInitialize()
599 * Un-Initializes the OLE clipboard
601 void OLEClipbrd_UnInitialize()
605 * Destroy the clipboard if no one holds a reference to us.
606 * Note that the clipboard was created with a reference count of 1.
608 if ( theOleClipboard
&& (theOleClipboard
->ref
<= 1) )
610 OLEClipbrd_Destroy( theOleClipboard
);
614 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
619 /*********************************************************
620 * Construct the OLEClipbrd class.
622 static OLEClipbrd
* OLEClipbrd_Construct()
624 OLEClipbrd
* newObject
= NULL
;
625 HGLOBAL hNewObject
= 0;
628 * Allocate space for the object. We use GlobalAlloc since we need
629 * an HGLOBAL to expose our DataObject as a registered clipboard type.
631 hNewObject
= GlobalAlloc( GMEM_DDESHARE
|GMEM_MOVEABLE
|GMEM_ZEROINIT
,
637 * Lock the handle for the entire lifetime of the clipboard.
639 newObject
= GlobalLock(hNewObject
);
642 * Initialize the virtual function table.
644 newObject
->lpvtbl1
= &OLEClipbrd_IDataObject_VTable
;
647 * Start with one reference count. The caller of this function
648 * must release the interface pointer when it is done.
652 newObject
->hSelf
= hNewObject
;
655 * The Ole clipboard is a singleton - save the global handle and pointer
657 theOleClipboard
= newObject
;
658 hTheOleClipboard
= hNewObject
;
660 return theOleClipboard
;
663 static void OLEClipbrd_Destroy(OLEClipbrd
* ptrToDestroy
)
671 * Destroy the Ole clipboard window
673 if ( ptrToDestroy
->hWndClipboard
)
674 OLEClipbrd_DestroyWindow(ptrToDestroy
->hWndClipboard
);
677 * Free the actual OLE Clipboard structure.
679 TRACE("() - Destroying clipboard data object.\n");
680 GlobalUnlock(ptrToDestroy
->hSelf
);
681 GlobalFree(ptrToDestroy
->hSelf
);
684 * The Ole clipboard is a singleton (ptrToDestroy == theOleClipboard)
686 theOleClipboard
= NULL
;
687 hTheOleClipboard
= 0;
691 /***********************************************************************
692 * OLEClipbrd_CreateWindow()
693 * Create the clipboard window
695 static HWND
OLEClipbrd_CreateWindow()
701 * Register the clipboard window class if necessary
703 ZeroMemory( &wcex
, sizeof(WNDCLASSEXA
));
705 wcex
.cbSize
= sizeof(WNDCLASSEXA
);
706 /* Windows creates this class with a style mask of 0
707 * We dont bother doing this since the FindClassByAtom code
708 * would have to be changed to deal with this idiosyncracy. */
709 wcex
.style
= CS_GLOBALCLASS
;
710 wcex
.lpfnWndProc
= (WNDPROC
)OLEClipbrd_WndProc
;
712 wcex
.lpszClassName
= OLEClipbrd_WNDCLASS
;
714 RegisterClassExA(&wcex
);
717 * Create a hidden window to receive OLE clipboard messages
721 * If we need to store state info we can store it here.
722 * For now we dont need this functionality.
723 * ClipboardWindowInfo clipboardInfo;
724 * ZeroMemory( &trackerInfo, sizeof(ClipboardWindowInfo));
727 hwnd
= CreateWindowA(OLEClipbrd_WNDCLASS
,
729 WS_POPUP
| WS_CLIPSIBLINGS
| WS_OVERLAPPED
,
730 CW_USEDEFAULT
, CW_USEDEFAULT
,
731 CW_USEDEFAULT
, CW_USEDEFAULT
,
735 0 /*(LPVOID)&clipboardInfo */);
740 /***********************************************************************
741 * OLEClipbrd_DestroyWindow(HWND)
742 * Destroy the clipboard window and unregister its class
744 static void OLEClipbrd_DestroyWindow(HWND hwnd
)
747 * Destroy clipboard window and unregister its WNDCLASS
750 UnregisterClassA( OLEClipbrd_WNDCLASS
, 0 );
753 /***********************************************************************
754 * OLEClipbrd_WndProc(HWND, unsigned, WORD, LONG)
755 * Processes messages sent to the OLE clipboard window.
756 * Note that we will intercept messages in our WndProc only when data
757 * has been placed in the clipboard via OleSetClipboard().
758 * i.e. Only when OLE owns the windows clipboard.
760 LRESULT CALLBACK OLEClipbrd_WndProc
761 (HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
767 * We receive this message to allow us to handle delayed rendering of
768 * a specific clipboard format when an application requests data in
769 * that format by calling GetClipboardData.
770 * (Recall that in OleSetClipboard, we used SetClipboardData to
771 * make all HGLOBAL formats supported by the source IDataObject
772 * available using delayed rendering)
773 * On receiving this mesage we must actually render the data in the
774 * specified format and place it on the clipboard by calling the
775 * SetClipboardData function.
777 case WM_RENDERFORMAT
:
781 ZeroMemory( &rgelt
, sizeof(FORMATETC
));
784 * Initialize FORMATETC to a Windows clipboard friendly format
786 rgelt
.cfFormat
= (UINT
) wParam
;
787 rgelt
.dwAspect
= DVASPECT_CONTENT
;
789 rgelt
.tymed
= TYMED_HGLOBAL
;
791 TRACE("(): WM_RENDERFORMAT(cfFormat=%d)\n", rgelt
.cfFormat
);
794 * Render the clipboard data.
795 * (We must have a source data object or we wouldn't be in this WndProc)
797 OLEClipbrd_RenderFormat( (IDataObject
*)&(theOleClipboard
->lpvtbl1
), &rgelt
);
803 * WM_RENDERALLFORMATS
804 * Sent before the clipboard owner window is destroyed.
805 * We should receive this message only when OleUninitialize is called
806 * while we have an IDataObject in the clipboard.
807 * For the content of the clipboard to remain available to other
808 * applications, we must render data in all the formats the source IDataObject
809 * is capable of generating, and place the data on the clipboard by calling
812 case WM_RENDERALLFORMATS
:
814 IEnumFORMATETC
* penumFormatetc
= NULL
;
817 TRACE("(): WM_RENDERALLFORMATS\n");
820 * Render all HGLOBAL formats supported by the source into
821 * the windows clipboard.
823 if ( FAILED( IDataObject_EnumFormatEtc( (IDataObject
*)&(theOleClipboard
->lpvtbl1
),
824 DATADIR_GET
, &penumFormatetc
) ) )
826 WARN("(): WM_RENDERALLFORMATS failed to retrieve EnumFormatEtc!\n");
830 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
832 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
835 * Render the clipboard data.
837 if ( FAILED(OLEClipbrd_RenderFormat( (IDataObject
*)&(theOleClipboard
->lpvtbl1
), &rgelt
)) )
840 TRACE("(): WM_RENDERALLFORMATS(cfFormat=%d)\n", rgelt
.cfFormat
);
844 IEnumFORMATETC_Release(penumFormatetc
);
850 * WM_DESTROYCLIPBOARD
851 * This is sent by EmptyClipboard before the clipboard is emptied.
852 * We should release any IDataObject we are holding onto when we receive
853 * this message, since it indicates that the OLE clipboard should be empty
854 * from this point on.
856 case WM_DESTROYCLIPBOARD
:
858 TRACE("(): WM_DESTROYCLIPBOARD\n");
860 * Release the data object we are holding on to
862 if ( theOleClipboard
->pIDataObjectSrc
)
864 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
865 theOleClipboard
->pIDataObjectSrc
= NULL
;
871 case WM_ASKCBFORMATNAME:
872 case WM_CHANGECBCHAIN:
873 case WM_DRAWCLIPBOARD:
874 case WM_SIZECLIPBOARD:
875 case WM_HSCROLLCLIPBOARD:
876 case WM_VSCROLLCLIPBOARD:
877 case WM_PAINTCLIPBOARD:
880 return DefWindowProcA(hWnd
, message
, wParam
, lParam
);
887 /***********************************************************************
888 * OLEClipbrd_RenderFormat(LPFORMATETC)
889 * Render the clipboard data. Note that this call will delegate to the
890 * source data object.
891 * Note: This function assumes it is passed an HGLOBAL format to render.
893 static HRESULT
OLEClipbrd_RenderFormat(IDataObject
*pIDataObject
, LPFORMATETC pFormatetc
)
899 if ( FAILED(hr
= IDataObject_GetData(pIDataObject
, pFormatetc
, &medium
)) )
901 WARN("() : IDataObject_GetData failed to render clipboard data! (%lx)\n", hr
);
906 * Put a copy of the rendered data back on the clipboard
909 if ( !(hDup
= OLEClipbrd_GlobalDupMem(medium
.u
.hGlobal
)) )
910 HANDLE_ERROR( E_OUTOFMEMORY
);
912 if ( !SetClipboardData( pFormatetc
->cfFormat
, hDup
) )
915 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
920 ReleaseStgMedium(&medium
);
926 /***********************************************************************
927 * OLEClipbrd_GlobalDupMem( HGLOBAL )
928 * Helper method to duplicate an HGLOBAL chunk of memory
930 static HGLOBAL
OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc
)
933 PVOID pGlobalSrc
, pGlobalDest
;
939 cBytes
= GlobalSize(hGlobalSrc
);
943 hGlobalDest
= GlobalAlloc( GMEM_DDESHARE
|GMEM_MOVEABLE
,
948 pGlobalSrc
= GlobalLock(hGlobalSrc
);
949 pGlobalDest
= GlobalLock(hGlobalDest
);
950 if ( !pGlobalSrc
|| !pGlobalDest
)
953 memcpy(pGlobalDest
, pGlobalSrc
, cBytes
);
955 GlobalUnlock(hGlobalSrc
);
956 GlobalUnlock(hGlobalDest
);
962 /*---------------------------------------------------------------------*
963 * Implementation of the internal IDataObject interface exposed by
965 *---------------------------------------------------------------------*/
968 /************************************************************************
969 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
971 * See Windows documentation for more details on IUnknown methods.
973 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryInterface(
979 * Declare "This" pointer
981 ICOM_THIS(OLEClipbrd
, iface
);
982 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObject
);
985 * Perform a sanity check on the parameters.
987 if ( (This
==0) || (ppvObject
==0) )
991 * Initialize the return parameter.
996 * Compare the riid with the interface IDs implemented by this object.
998 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
1002 else if (memcmp(&IID_IDataObject
, riid
, sizeof(IID_IDataObject
)) == 0)
1004 *ppvObject
= (IDataObject
*)&(This
->lpvtbl1
);
1006 else /* We only support IUnknown and IDataObject */
1008 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid
));
1009 return E_NOINTERFACE
;
1013 * Query Interface always increases the reference count by one when it is
1016 IUnknown_AddRef((IUnknown
*)*ppvObject
);
1021 /************************************************************************
1022 * OLEClipbrd_IDataObject_AddRef (IUnknown)
1024 * See Windows documentation for more details on IUnknown methods.
1026 static ULONG WINAPI
OLEClipbrd_IDataObject_AddRef(
1030 * Declare "This" pointer
1032 ICOM_THIS(OLEClipbrd
, iface
);
1034 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1041 /************************************************************************
1042 * OLEClipbrd_IDataObject_Release (IUnknown)
1044 * See Windows documentation for more details on IUnknown methods.
1046 static ULONG WINAPI
OLEClipbrd_IDataObject_Release(
1050 * Declare "This" pointer
1052 ICOM_THIS(OLEClipbrd
, iface
);
1054 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1057 * Decrease the reference count on this object.
1062 * If the reference count goes down to 0, perform suicide.
1066 OLEClipbrd_Destroy(This
);
1073 /************************************************************************
1074 * OLEClipbrd_IDataObject_GetData (IDataObject)
1076 * The OLE Clipboard's implementation of this method delegates to
1077 * a data source if there is one or wraps around the windows clipboard
1079 * See Windows documentation for more details on IDataObject methods.
1081 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetData(
1083 LPFORMATETC pformatetcIn
,
1087 BOOL bClipboardOpen
= FALSE
;
1091 * Declare "This" pointer
1093 ICOM_THIS(OLEClipbrd
, iface
);
1095 TRACE("(%p,%p,%p)\n", iface
, pformatetcIn
, pmedium
);
1097 if ( !pformatetcIn
|| !pmedium
)
1098 return E_INVALIDARG
;
1101 * If we have a data source placed on the clipboard (via OleSetClipboard)
1102 * simply delegate to the source object's QueryGetData
1103 * NOTE: This code assumes that the IDataObject is in the same address space!
1104 * We will need to add marshalling support when Wine handles multiple processes.
1106 if ( This
->pIDataObjectSrc
)
1108 return IDataObject_GetData(This
->pIDataObjectSrc
, pformatetcIn
, pmedium
);
1111 if ( pformatetcIn
->lindex
!= -1 )
1113 if ( (pformatetcIn
->tymed
& TYMED_HGLOBAL
) != TYMED_HGLOBAL
)
1116 if ( pformatetcIn->dwAspect != DVASPECT_CONTENT )
1117 return DV_E_DVASPECT;
1121 * Otherwise, get the data from the windows clipboard using GetClipboardData
1123 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
1124 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
1126 hData
= GetClipboardData(pformatetcIn
->cfFormat
);
1129 * Return the clipboard data in the storage medium structure
1131 pmedium
->tymed
= (hData
== 0) ? TYMED_NULL
: TYMED_HGLOBAL
;
1132 pmedium
->u
.hGlobal
= (HGLOBAL
)hData
;
1133 pmedium
->pUnkForRelease
= NULL
;
1139 * Close Windows clipboard
1141 if ( bClipboardOpen
&& !CloseClipboard() )
1142 hr
= CLIPBRD_E_CANT_CLOSE
;
1146 return (hData
== 0) ? DV_E_FORMATETC
: S_OK
;
1149 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetDataHere(
1151 LPFORMATETC pformatetc
,
1158 /************************************************************************
1159 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
1161 * The OLE Clipboard's implementation of this method delegates to
1162 * a data source if there is one or wraps around the windows clipboard
1163 * function IsClipboardFormatAvailable() otherwise.
1165 * See Windows documentation for more details on IDataObject methods.
1167 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryGetData(
1169 LPFORMATETC pformatetc
)
1172 * Declare "This" pointer
1174 ICOM_THIS(OLEClipbrd
, iface
);
1176 TRACE("(%p, %p)\n", iface
, pformatetc
);
1179 * If we have a data source placed on the clipboard (via OleSetClipboard)
1180 * simply delegate to the source object's QueryGetData
1182 if ( This
->pIDataObjectSrc
)
1184 return IDataObject_QueryGetData(This
->pIDataObjectSrc
, pformatetc
);
1188 return E_INVALIDARG
;
1190 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
1191 return DV_E_DVASPECT;
1193 if ( pformatetc
->lindex
!= -1 )
1196 /* TODO: Handle TYMED_IStorage media which were put on the clipboard
1197 * by copying the storage into global memory. We must convert this
1198 * TYMED_HGLOBAL back to TYMED_IStorage.
1200 if ( pformatetc
->tymed
!= TYMED_HGLOBAL
)
1204 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
1206 return (IsClipboardFormatAvailable(pformatetc
->cfFormat
)) ? S_OK
: DV_E_FORMATETC
;
1209 /************************************************************************
1210 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
1212 * See Windows documentation for more details on IDataObject methods.
1214 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
1216 LPFORMATETC pformatectIn
,
1217 LPFORMATETC pformatetcOut
)
1219 TRACE("(%p, %p, %p)\n", iface
, pformatectIn
, pformatetcOut
);
1221 if ( !pformatectIn
|| !pformatetcOut
)
1222 return E_INVALIDARG
;
1224 memcpy(pformatetcOut
, pformatectIn
, sizeof(FORMATETC
));
1225 return DATA_S_SAMEFORMATETC
;
1228 /************************************************************************
1229 * OLEClipbrd_IDataObject_SetData (IDataObject)
1231 * The OLE Clipboard's does not implement this method
1233 * See Windows documentation for more details on IDataObject methods.
1235 static HRESULT WINAPI
OLEClipbrd_IDataObject_SetData(
1237 LPFORMATETC pformatetc
,
1245 /************************************************************************
1246 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
1248 * See Windows documentation for more details on IDataObject methods.
1250 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumFormatEtc(
1253 IEnumFORMATETC
** ppenumFormatEtc
)
1256 FORMATETC
*afmt
= NULL
;
1259 BOOL bClipboardOpen
;
1262 * Declare "This" pointer
1264 ICOM_THIS(OLEClipbrd
, iface
);
1266 TRACE("(%p, %lx, %p)\n", iface
, dwDirection
, ppenumFormatEtc
);
1269 * If we have a data source placed on the clipboard (via OleSetClipboard)
1270 * simply delegate to the source object's EnumFormatEtc
1272 if ( This
->pIDataObjectSrc
)
1274 return IDataObject_EnumFormatEtc(This
->pIDataObjectSrc
,
1275 dwDirection
, ppenumFormatEtc
);
1279 * Otherwise we must provide our own enumerator which wraps around the
1280 * Windows clipboard function EnumClipboardFormats
1282 if ( !ppenumFormatEtc
)
1283 return E_INVALIDARG
;
1285 if ( dwDirection
!= DATADIR_GET
) /* IDataObject_SetData not implemented */
1289 * Store all current clipboard formats in an array of FORMATETC's,
1290 * and create an IEnumFORMATETC enumerator from this list.
1292 cfmt
= CountClipboardFormats();
1293 afmt
= (FORMATETC
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1294 sizeof(FORMATETC
) * cfmt
);
1296 * Open the Windows clipboard, associating it with our hidden window
1298 if ( !(bClipboardOpen
= OpenClipboard(This
->hWndClipboard
)) )
1299 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
1302 * Store all current clipboard formats in an array of FORMATETC's
1303 * TODO: Handle TYMED_IStorage media which were put on the clipboard
1304 * by copying the storage into global memory. We must convert this
1305 * TYMED_HGLOBAL back to TYMED_IStorage.
1307 for (i
= 0, format
= 0; i
< cfmt
; i
++)
1309 format
= EnumClipboardFormats(format
);
1310 if (!format
) /* Failed! */
1312 ERR("EnumClipboardFormats failed to return format!\n");
1313 HANDLE_ERROR( E_FAIL
);
1316 /* Init the FORMATETC struct */
1317 afmt
[i
].cfFormat
= format
;
1319 afmt
[i
].dwAspect
= DVASPECT_CONTENT
;
1320 afmt
[i
].lindex
= -1;
1321 afmt
[i
].tymed
= TYMED_HGLOBAL
;
1325 * Create an EnumFORMATETC enumerator and return an
1326 * EnumFORMATETC after bumping up its ref count
1328 *ppenumFormatEtc
= OLEClipbrd_IEnumFORMATETC_Construct( cfmt
, afmt
, (LPUNKNOWN
)iface
);
1329 if (!(*ppenumFormatEtc
))
1330 HANDLE_ERROR( E_OUTOFMEMORY
);
1332 if (FAILED( hr
= IEnumFORMATETC_AddRef(*ppenumFormatEtc
)))
1339 * Free the array of FORMATETC's
1342 HeapFree(GetProcessHeap(), 0, afmt
);
1345 * Close Windows clipboard
1347 if ( bClipboardOpen
&& !CloseClipboard() )
1348 hr
= CLIPBRD_E_CANT_CLOSE
;
1353 /************************************************************************
1354 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
1356 * The OLE Clipboard's does not implement this method
1358 * See Windows documentation for more details on IDataObject methods.
1360 static HRESULT WINAPI
OLEClipbrd_IDataObject_DAdvise(
1362 FORMATETC
* pformatetc
,
1364 IAdviseSink
* pAdvSink
,
1365 DWORD
* pdwConnection
)
1371 /************************************************************************
1372 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
1374 * The OLE Clipboard's does not implement this method
1376 * See Windows documentation for more details on IDataObject methods.
1378 static HRESULT WINAPI
OLEClipbrd_IDataObject_DUnadvise(
1386 /************************************************************************
1387 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1389 * The OLE Clipboard does not implement this method
1391 * See Windows documentation for more details on IDataObject methods.
1393 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumDAdvise(
1395 IEnumSTATDATA
** ppenumAdvise
)
1402 /*---------------------------------------------------------------------*
1403 * Implementation of the internal IEnumFORMATETC interface returned by
1404 * the OLE clipboard's IDataObject.
1405 *---------------------------------------------------------------------*/
1407 /************************************************************************
1408 * OLEClipbrd_IEnumFORMATETC_Construct (UINT, const FORMATETC, LPUNKNOWN)
1410 * Creates an IEnumFORMATETC enumerator from an array of FORMATETC
1411 * Structures. pUnkOuter is the outer unknown for reference counting only.
1412 * NOTE: this does not AddRef the interface.
1415 LPENUMFORMATETC
OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt
, const FORMATETC afmt
[],
1416 LPUNKNOWN pUnkDataObj
)
1418 IEnumFORMATETCImpl
* ef
;
1419 DWORD size
=cfmt
* sizeof(FORMATETC
);
1422 ef
= (IEnumFORMATETCImpl
*)HeapAlloc(GetProcessHeap(),
1424 sizeof(IEnumFORMATETCImpl
));
1429 ICOM_VTBL(ef
) = &efvt
;
1430 ef
->pUnkDataObj
= pUnkDataObj
;
1433 ef
->countFmt
= cfmt
;
1434 if (FAILED(CoGetMalloc(MEMCTX_TASK
, &pIMalloc
)))
1436 ef
->pFmt
= (LPFORMATETC
)IMalloc_Alloc(pIMalloc
, size
);
1437 IMalloc_Release(pIMalloc
);
1440 memcpy(ef
->pFmt
, afmt
, size
);
1442 TRACE("(%p)->()\n",ef
);
1443 return (LPENUMFORMATETC
)ef
;
1447 /************************************************************************
1448 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
1450 * See Windows documentation for more details on IUnknown methods.
1452 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
1453 (LPENUMFORMATETC iface
, REFIID riid
, LPVOID
* ppvObj
)
1455 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1457 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
1460 * Since enumerators are seperate objects from the parent data object
1461 * we only need to support the IUnknown and IEnumFORMATETC interfaces
1466 if(IsEqualIID(riid
, &IID_IUnknown
))
1470 else if(IsEqualIID(riid
, &IID_IEnumFORMATETC
))
1472 *ppvObj
= (IDataObject
*)This
;
1477 IEnumFORMATETC_AddRef((IEnumFORMATETC
*)*ppvObj
);
1478 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
1482 TRACE("-- Interface: E_NOINTERFACE\n");
1483 return E_NOINTERFACE
;
1486 /************************************************************************
1487 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
1489 * Since enumerating formats only makes sense when our data object is around,
1490 * we insure that it stays as long as we stay by calling our parents IUnknown
1491 * for AddRef and Release. But since we are not controlled by the lifetime of
1492 * the outer object, we still keep our own reference count in order to
1495 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface
)
1497 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1498 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1500 if (This
->pUnkDataObj
)
1501 IUnknown_AddRef(This
->pUnkDataObj
);
1503 return ++(This
->ref
);
1506 /************************************************************************
1507 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
1509 * See Windows documentation for more details on IUnknown methods.
1511 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface
)
1513 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1516 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1518 if (This
->pUnkDataObj
)
1519 IUnknown_Release(This
->pUnkDataObj
); /* Release parent data object */
1523 TRACE("() - destroying IEnumFORMATETC(%p)\n",This
);
1524 if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK
, &pIMalloc
)))
1526 IMalloc_Free(pIMalloc
, This
->pFmt
);
1527 IMalloc_Release(pIMalloc
);
1530 HeapFree(GetProcessHeap(),0,This
);
1537 /************************************************************************
1538 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
1540 * Standard enumerator members for IEnumFORMATETC
1542 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
1543 (LPENUMFORMATETC iface
, ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFethed
)
1545 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1547 HRESULT hres
= S_FALSE
;
1549 TRACE("(%p)->(pos=%u)\n", This
, This
->posFmt
);
1551 if (This
->posFmt
< This
->countFmt
)
1553 cfetch
= This
->countFmt
- This
->posFmt
;
1560 memcpy(rgelt
, &This
->pFmt
[This
->posFmt
], cfetch
* sizeof(FORMATETC
));
1561 This
->posFmt
+= cfetch
;
1570 *pceltFethed
= cfetch
;
1576 /************************************************************************
1577 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
1579 * Standard enumerator members for IEnumFORMATETC
1581 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface
, ULONG celt
)
1583 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1584 TRACE("(%p)->(num=%lu)\n", This
, celt
);
1586 This
->posFmt
+= celt
;
1587 if (This
->posFmt
> This
->countFmt
)
1589 This
->posFmt
= This
->countFmt
;
1595 /************************************************************************
1596 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
1598 * Standard enumerator members for IEnumFORMATETC
1600 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface
)
1602 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1603 TRACE("(%p)->()\n", This
);
1609 /************************************************************************
1610 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
1612 * Standard enumerator members for IEnumFORMATETC
1614 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
1615 (LPENUMFORMATETC iface
, LPENUMFORMATETC
* ppenum
)
1617 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1620 TRACE("(%p)->(ppenum=%p)\n", This
, ppenum
);
1623 return E_INVALIDARG
;
1625 *ppenum
= OLEClipbrd_IEnumFORMATETC_Construct(This
->countFmt
,
1629 if (FAILED( hr
= IEnumFORMATETC_AddRef(*ppenum
)))
1632 return (*ppenum
) ? S_OK
: E_OUTOFMEMORY
;