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_VTABLE(IEnumFORMATETC
)* lpvtbl
;
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(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
;
480 * Make sure we have a clipboard object
482 OLEClipbrd_Initialize();
485 * Already flushed or no source DataObject? Nothing to do.
487 if (!theOleClipboard
->pIDataObjectSrc
)
491 * Open the Windows clipboard
493 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
494 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
497 * Empty the current clipboard
499 if ( !EmptyClipboard() )
500 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY
);
503 * Render all HGLOBAL formats supported by the source into
504 * the windows clipboard.
506 if ( FAILED( hr
= IDataObject_EnumFormatEtc( (IDataObject
*)&(theOleClipboard
->lpvtbl1
),
513 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
515 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
518 TRACE("(cfFormat=%d:%s)\n", rgelt
.cfFormat
,
519 GetClipboardFormatNameA(rgelt
.cfFormat
, szFmtName
, sizeof(szFmtName
)-1)
523 * Render the clipboard data
525 if ( FAILED(OLEClipbrd_RenderFormat( &rgelt
)) )
530 IEnumFORMATETC_Release(penumFormatetc
);
533 * Release the data object we are holding on to
535 if ( theOleClipboard
->pIDataObjectSrc
)
537 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
538 theOleClipboard
->pIDataObjectSrc
= NULL
;
544 * Close Windows clipboard (It remains associated with our window)
546 if ( bClipboardOpen
&& !CloseClipboard() )
547 hr
= CLIPBRD_E_CANT_CLOSE
;
553 /***********************************************************************
554 * OleIsCurrentClipboard32 [OLE32.110]
556 HRESULT WINAPI
OleIsCurrentClipboard ( IDataObject
*pDataObject
)
560 * Make sure we have a clipboard object
562 OLEClipbrd_Initialize();
564 if (!theOleClipboard
)
565 return E_OUTOFMEMORY
;
567 return (pDataObject
== theOleClipboard
->pIDataObjectSrc
) ? S_OK
: S_FALSE
;
571 /*---------------------------------------------------------------------*
572 * Internal implementation methods for the OLE clipboard
573 *---------------------------------------------------------------------*/
575 /***********************************************************************
576 * OLEClipbrd_Initialize()
577 * Initializes the OLE clipboard.
579 void OLEClipbrd_Initialize()
582 * Create the clipboard if necesary
584 if ( !theOleClipboard
)
587 theOleClipboard
= OLEClipbrd_Construct();
592 /***********************************************************************
593 * OLEClipbrd_UnInitialize()
594 * Un-Initializes the OLE clipboard
596 void OLEClipbrd_UnInitialize()
600 * Destroy the clipboard if no one holds a reference to us.
601 * Note that the clipboard was created with a reference count of 1.
603 if ( theOleClipboard
&& (theOleClipboard
->ref
<= 1) )
605 OLEClipbrd_Destroy( theOleClipboard
);
609 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
614 /*********************************************************
615 * Construct the OLEClipbrd class.
617 static OLEClipbrd
* OLEClipbrd_Construct()
619 OLEClipbrd
* newObject
= NULL
;
620 HGLOBAL hNewObject
= 0;
623 * Allocate space for the object. We use GlobalAlloc since we need
624 * an HGLOBAL to expose our DataObject as a registered clipboard type.
626 hNewObject
= GlobalAlloc( GMEM_DDESHARE
|GMEM_MOVEABLE
|GMEM_ZEROINIT
,
632 * Lock the handle for the entire lifetime of the clipboard.
634 newObject
= GlobalLock(hNewObject
);
637 * Initialize the virtual function table.
639 newObject
->lpvtbl1
= &OLEClipbrd_IDataObject_VTable
;
642 * Start with one reference count. The caller of this function
643 * must release the interface pointer when it is done.
647 newObject
->hSelf
= hNewObject
;
650 * The Ole clipboard is a singleton - save the global handle and pointer
652 theOleClipboard
= newObject
;
653 hTheOleClipboard
= hNewObject
;
655 return theOleClipboard
;
658 static void OLEClipbrd_Destroy(OLEClipbrd
* ptrToDestroy
)
666 * Destroy the Ole clipboard window
668 if ( ptrToDestroy
->hWndClipboard
)
669 OLEClipbrd_DestroyWindow(ptrToDestroy
->hWndClipboard
);
672 * Free the actual OLE Clipboard structure.
674 TRACE("() - Destroying clipboard data object.\n");
675 GlobalUnlock(ptrToDestroy
->hSelf
);
676 GlobalFree(ptrToDestroy
->hSelf
);
679 * The Ole clipboard is a singleton (ptrToDestroy == theOleClipboard)
681 theOleClipboard
= NULL
;
682 hTheOleClipboard
= 0;
686 /***********************************************************************
687 * OLEClipbrd_CreateWindow()
688 * Create the clipboard window
690 static HWND
OLEClipbrd_CreateWindow()
697 * Register the clipboard window class if necessary
700 if ( !( atom
= GlobalFindAtomA(OLEClipbrd_WNDCLASS
) ) ||
701 !( CLASS_FindClassByAtom(atom
, 0) ) )
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
);
718 * Create a hidden window to receive OLE clipboard messages
722 * If we need to store state info we can store it here.
723 * For now we dont need this functionality.
724 * ClipboardWindowInfo clipboardInfo;
725 * ZeroMemory( &trackerInfo, sizeof(ClipboardWindowInfo));
728 hwnd
= CreateWindowA(OLEClipbrd_WNDCLASS
,
730 WS_POPUP
| WS_CLIPSIBLINGS
| WS_OVERLAPPED
,
731 CW_USEDEFAULT
, CW_USEDEFAULT
,
732 CW_USEDEFAULT
, CW_USEDEFAULT
,
736 0 /*(LPVOID)&clipboardInfo */);
741 /***********************************************************************
742 * OLEClipbrd_DestroyWindow(HWND)
743 * Destroy the clipboard window and unregister its class
745 static void OLEClipbrd_DestroyWindow(HWND hwnd
)
748 * Destroy clipboard window and unregister its WNDCLASS
751 UnregisterClassA( OLEClipbrd_WNDCLASS
, 0 );
754 /***********************************************************************
755 * OLEClipbrd_WndProc(HWND, unsigned, WORD, LONG)
756 * Processes messages sent to the OLE clipboard window.
757 * Note that we will intercept messages in our WndProc only when data
758 * has been placed in the clipboard via OleSetClipboard().
759 * i.e. Only when OLE owns the windows clipboard.
761 LRESULT CALLBACK OLEClipbrd_WndProc
762 (HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
)
768 * We receive this message to allow us to handle delayed rendering of
769 * a specific clipboard format when an application requests data in
770 * that format by calling GetClipboardData.
771 * (Recall that in OleSetClipboard, we used SetClipboardData to
772 * make all HGLOBAL formats supported by the source IDataObject
773 * available using delayed rendering)
774 * On receiving this mesage we must actually render the data in the
775 * specified format and place it on the clipboard by calling the
776 * SetClipboardData function.
778 case WM_RENDERFORMAT
:
782 ZeroMemory( &rgelt
, sizeof(FORMATETC
));
785 * Initialize FORMATETC to a Windows clipboard friendly format
787 rgelt
.cfFormat
= (UINT
) wParam
;
788 rgelt
.dwAspect
= DVASPECT_CONTENT
;
790 rgelt
.tymed
= TYMED_HGLOBAL
;
792 TRACE("(): WM_RENDERFORMAT(cfFormat=%d)\n", rgelt
.cfFormat
);
795 * Render the clipboard data.
796 * (We must have a source data object or we wouldn't be in this WndProc)
798 OLEClipbrd_RenderFormat( &rgelt
);
804 * WM_RENDERALLFORMATS
805 * Sent before the clipboard owner window is destroyed.
806 * We should receive this message only when OleUninitialize is called
807 * while we have an IDataObject in the clipboard.
808 * For the content of the clipboard to remain available to other
809 * applications, we must render data in all the formats the source IDataObject
810 * is capable of generating, and place the data on the clipboard by calling
813 case WM_RENDERALLFORMATS
:
815 IEnumFORMATETC
* penumFormatetc
= NULL
;
818 TRACE("(): WM_RENDERALLFORMATS\n");
821 * Render all HGLOBAL formats supported by the source into
822 * the windows clipboard.
824 if ( FAILED( IDataObject_EnumFormatEtc( (IDataObject
*)&(theOleClipboard
->lpvtbl1
),
825 DATADIR_GET
, &penumFormatetc
) ) )
827 WARN("(): WM_RENDERALLFORMATS failed to retrieve EnumFormatEtc!\n");
831 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
833 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
836 * Render the clipboard data.
838 if ( FAILED(OLEClipbrd_RenderFormat( &rgelt
)) )
841 TRACE("(): WM_RENDERALLFORMATS(cfFormat=%d)\n", rgelt
.cfFormat
);
845 IEnumFORMATETC_Release(penumFormatetc
);
851 * WM_DESTROYCLIPBOARD
852 * This is sent by EmptyClipboard before the clipboard is emptied.
853 * We should release any IDataObject we are holding onto when we receive
854 * this message, since it indicates that the OLE clipboard should be empty
855 * from this point on.
857 case WM_DESTROYCLIPBOARD
:
859 TRACE("(): WM_DESTROYCLIPBOARD\n");
861 * Release the data object we are holding on to
863 if ( theOleClipboard
->pIDataObjectSrc
)
865 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
866 theOleClipboard
->pIDataObjectSrc
= NULL
;
872 case WM_ASKCBFORMATNAME:
873 case WM_CHANGECBCHAIN:
874 case WM_DRAWCLIPBOARD:
875 case WM_SIZECLIPBOARD:
876 case WM_HSCROLLCLIPBOARD:
877 case WM_VSCROLLCLIPBOARD:
878 case WM_PAINTCLIPBOARD:
881 return DefWindowProcA(hWnd
, message
, wParam
, lParam
);
888 /***********************************************************************
889 * OLEClipbrd_RenderFormat(LPFORMATETC)
890 * Render the clipboard data. Note that this call will delegate to the
891 * source data object.
892 * Note: This function assumes it is passed an HGLOBAL format to render.
894 static HRESULT
OLEClipbrd_RenderFormat(LPFORMATETC pFormatetc
)
900 if ( FAILED(hr
= IDataObject_GetData((IDataObject
*)&(theOleClipboard
->lpvtbl1
),
901 pFormatetc
, &medium
)) )
903 WARN("() : IDataObject_GetData failed to render clipboard data! (%lx)\n", hr
);
908 * Put a copy of the rendered data back on the clipboard
911 if ( !(hDup
= OLEClipbrd_GlobalDupMem(medium
.u
.hGlobal
)) )
912 HANDLE_ERROR( E_OUTOFMEMORY
);
914 if ( !SetClipboardData( pFormatetc
->cfFormat
, hDup
) )
917 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
922 ReleaseStgMedium(&medium
);
928 /***********************************************************************
929 * OLEClipbrd_GlobalDupMem( HGLOBAL )
930 * Helper method to duplicate an HGLOBAL chunk of memory
932 static HGLOBAL
OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc
)
935 PVOID pGlobalSrc
, pGlobalDest
;
941 cBytes
= GlobalSize(hGlobalSrc
);
945 hGlobalDest
= GlobalAlloc( GMEM_DDESHARE
|GMEM_MOVEABLE
,
950 pGlobalSrc
= GlobalLock(hGlobalSrc
);
951 pGlobalDest
= GlobalLock(hGlobalDest
);
952 if ( !pGlobalSrc
|| !pGlobalDest
)
955 memcpy(pGlobalDest
, pGlobalSrc
, cBytes
);
957 GlobalUnlock(hGlobalSrc
);
958 GlobalUnlock(hGlobalDest
);
964 /*---------------------------------------------------------------------*
965 * Implementation of the internal IDataObject interface exposed by
967 *---------------------------------------------------------------------*/
970 /************************************************************************
971 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
973 * See Windows documentation for more details on IUnknown methods.
975 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryInterface(
981 * Declare "This" pointer
983 ICOM_THIS(OLEClipbrd
, iface
);
986 WINE_StringFromCLSID((LPCLSID
)riid
,xriid
);
987 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,xriid
,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 */
1015 WINE_StringFromCLSID((LPCLSID
)riid
,clsid
);
1016 WARN( "() : asking for unsupported interface %s\n", clsid
);
1018 return E_NOINTERFACE
;
1022 * Query Interface always increases the reference count by one when it is
1025 IUnknown_AddRef((IUnknown
*)*ppvObject
);
1030 /************************************************************************
1031 * OLEClipbrd_IDataObject_AddRef (IUnknown)
1033 * See Windows documentation for more details on IUnknown methods.
1035 static ULONG WINAPI
OLEClipbrd_IDataObject_AddRef(
1039 * Declare "This" pointer
1041 ICOM_THIS(OLEClipbrd
, iface
);
1043 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1050 /************************************************************************
1051 * OLEClipbrd_IDataObject_Release (IUnknown)
1053 * See Windows documentation for more details on IUnknown methods.
1055 static ULONG WINAPI
OLEClipbrd_IDataObject_Release(
1059 * Declare "This" pointer
1061 ICOM_THIS(OLEClipbrd
, iface
);
1063 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1066 * Decrease the reference count on this object.
1071 * If the reference count goes down to 0, perform suicide.
1075 OLEClipbrd_Destroy(This
);
1082 /************************************************************************
1083 * OLEClipbrd_IDataObject_GetData (IDataObject)
1085 * The OLE Clipboard's implementation of this method delegates to
1086 * a data source if there is one or wraps around the windows clipboard
1088 * See Windows documentation for more details on IDataObject methods.
1090 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetData(
1092 LPFORMATETC pformatetcIn
,
1096 BOOL bClipboardOpen
= FALSE
;
1100 * Declare "This" pointer
1102 ICOM_THIS(OLEClipbrd
, iface
);
1104 if ( !pformatetcIn
|| !pformatetcIn
|| !pmedium
)
1105 return E_INVALIDARG
;
1107 TRACE("(%p, %p)\n", iface
, pformatetcIn
);
1110 * If we have a data source placed on the clipboard (via OleSetClipboard)
1111 * simply delegate to the source object's QueryGetData
1112 * NOTE: This code assumes that the IDataObject is in the same address space!
1113 * We will need to add marshalling support when Wine handles multiple processes.
1115 if ( This
->pIDataObjectSrc
)
1117 return IDataObject_GetData(This
->pIDataObjectSrc
, pformatetcIn
, pmedium
);
1120 if ( pformatetcIn
->lindex
!= -1 )
1122 if ( (pformatetcIn
->tymed
& TYMED_HGLOBAL
) != TYMED_HGLOBAL
)
1125 if ( pformatetcIn->dwAspect != DVASPECT_CONTENT )
1126 return DV_E_DVASPECT;
1130 * Otherwise, get the data from the windows clipboard using GetClipboardData
1132 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
1133 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
1135 hData
= GetClipboardData(pformatetcIn
->cfFormat
);
1138 * Return the clipboard data in the storage medium structure
1140 pmedium
->tymed
= (hData
== 0) ? TYMED_NULL
: TYMED_HGLOBAL
;
1141 pmedium
->u
.hGlobal
= (HGLOBAL
)hData
;
1142 pmedium
->pUnkForRelease
= NULL
;
1148 * Close Windows clipboard
1150 if ( bClipboardOpen
&& !CloseClipboard() )
1151 hr
= CLIPBRD_E_CANT_CLOSE
;
1155 return (hData
== 0) ? DV_E_FORMATETC
: S_OK
;
1158 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetDataHere(
1160 LPFORMATETC pformatetc
,
1167 /************************************************************************
1168 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
1170 * The OLE Clipboard's implementation of this method delegates to
1171 * a data source if there is one or wraps around the windows clipboard
1172 * function IsClipboardFormatAvailable() otherwise.
1174 * See Windows documentation for more details on IDataObject methods.
1176 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryGetData(
1178 LPFORMATETC pformatetc
)
1181 * Declare "This" pointer
1183 ICOM_THIS(OLEClipbrd
, iface
);
1185 TRACE("(%p, %p)\n", iface
, pformatetc
);
1188 * If we have a data source placed on the clipboard (via OleSetClipboard)
1189 * simply delegate to the source object's QueryGetData
1191 if ( This
->pIDataObjectSrc
)
1193 return IDataObject_QueryGetData(This
->pIDataObjectSrc
, pformatetc
);
1197 return E_INVALIDARG
;
1199 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
1200 return DV_E_DVASPECT;
1202 if ( pformatetc
->lindex
!= -1 )
1205 /* TODO: Handle TYMED_IStorage media which were put on the clipboard
1206 * by copying the storage into global memory. We must convert this
1207 * TYMED_HGLOBAL back to TYMED_IStorage.
1209 if ( pformatetc
->tymed
!= TYMED_HGLOBAL
)
1213 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
1215 return (IsClipboardFormatAvailable(pformatetc
->cfFormat
)) ? S_OK
: DV_E_FORMATETC
;
1218 /************************************************************************
1219 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
1221 * See Windows documentation for more details on IDataObject methods.
1223 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
1225 LPFORMATETC pformatectIn
,
1226 LPFORMATETC pformatetcOut
)
1228 TRACE("(%p, %p, %p)\n", iface
, pformatectIn
, pformatetcOut
);
1230 if ( !pformatectIn
|| !pformatetcOut
)
1231 return E_INVALIDARG
;
1233 memcpy(pformatetcOut
, pformatectIn
, sizeof(FORMATETC
));
1234 return DATA_S_SAMEFORMATETC
;
1237 /************************************************************************
1238 * OLEClipbrd_IDataObject_SetData (IDataObject)
1240 * The OLE Clipboard's does not implement this method
1242 * See Windows documentation for more details on IDataObject methods.
1244 static HRESULT WINAPI
OLEClipbrd_IDataObject_SetData(
1246 LPFORMATETC pformatetc
,
1253 /************************************************************************
1254 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
1256 * See Windows documentation for more details on IDataObject methods.
1258 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumFormatEtc(
1261 IEnumFORMATETC
** ppenumFormatEtc
)
1264 FORMATETC
*afmt
= NULL
;
1267 BOOL bClipboardOpen
;
1270 * Declare "This" pointer
1272 ICOM_THIS(OLEClipbrd
, iface
);
1274 TRACE("(%p, %lx, %p)\n", iface
, dwDirection
, ppenumFormatEtc
);
1277 * If we have a data source placed on the clipboard (via OleSetClipboard)
1278 * simply delegate to the source object's EnumFormatEtc
1280 if ( This
->pIDataObjectSrc
)
1282 return IDataObject_EnumFormatEtc(This
->pIDataObjectSrc
,
1283 dwDirection
, ppenumFormatEtc
);
1287 * Otherwise we must provide our own enumerator which wraps around the
1288 * Windows clipboard function EnumClipboardFormats
1290 if ( !ppenumFormatEtc
)
1291 return E_INVALIDARG
;
1293 if ( dwDirection
!= DATADIR_GET
) /* IDataObject_SetData not implemented */
1297 * Store all current clipboard formats in an array of FORMATETC's,
1298 * and create an IEnumFORMATETC enumerator from this list.
1300 cfmt
= CountClipboardFormats();
1301 afmt
= (FORMATETC
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1302 sizeof(FORMATETC
) * cfmt
);
1304 * Open the Windows clipboard, associating it with our hidden window
1306 if ( !(bClipboardOpen
= OpenClipboard(This
->hWndClipboard
)) )
1307 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
1310 * Store all current clipboard formats in an array of FORMATETC's
1311 * TODO: Handle TYMED_IStorage media which were put on the clipboard
1312 * by copying the storage into global memory. We must convert this
1313 * TYMED_HGLOBAL back to TYMED_IStorage.
1315 for (i
= 0, format
= 0; i
< cfmt
; i
++)
1317 format
= EnumClipboardFormats(format
);
1318 if (!format
) /* Failed! */
1320 ERR("EnumClipboardFormats failed to return format!\n");
1321 HANDLE_ERROR( E_FAIL
);
1324 /* Init the FORMATETC struct */
1325 afmt
[i
].cfFormat
= format
;
1327 afmt
[i
].dwAspect
= DVASPECT_CONTENT
;
1328 afmt
[i
].lindex
= -1;
1329 afmt
[i
].tymed
= TYMED_HGLOBAL
;
1333 * Create an EnumFORMATETC enumerator and return an
1334 * EnumFORMATETC after bumping up its ref count
1336 *ppenumFormatEtc
= OLEClipbrd_IEnumFORMATETC_Construct( cfmt
, afmt
, (LPUNKNOWN
)iface
);
1337 if (!(*ppenumFormatEtc
))
1338 HANDLE_ERROR( E_OUTOFMEMORY
);
1340 if (FAILED( hr
= IEnumFORMATETC_AddRef(*ppenumFormatEtc
)))
1347 * Free the array of FORMATETC's
1350 HeapFree(GetProcessHeap(), 0, afmt
);
1353 * Close Windows clipboard
1355 if ( bClipboardOpen
&& !CloseClipboard() )
1356 hr
= CLIPBRD_E_CANT_CLOSE
;
1361 /************************************************************************
1362 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
1364 * The OLE Clipboard's does not implement this method
1366 * See Windows documentation for more details on IDataObject methods.
1368 static HRESULT WINAPI
OLEClipbrd_IDataObject_DAdvise(
1370 FORMATETC
* pformatetc
,
1372 IAdviseSink
* pAdvSink
,
1373 DWORD
* pdwConnection
)
1378 /************************************************************************
1379 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
1381 * The OLE Clipboard's does not implement this method
1383 * See Windows documentation for more details on IDataObject methods.
1385 static HRESULT WINAPI
OLEClipbrd_IDataObject_DUnadvise(
1392 /************************************************************************
1393 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1395 * The OLE Clipboard does not implement this method
1397 * See Windows documentation for more details on IDataObject methods.
1399 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumDAdvise(
1401 IEnumSTATDATA
** ppenumAdvise
)
1407 /*---------------------------------------------------------------------*
1408 * Implementation of the internal IEnumFORMATETC interface returned by
1409 * the OLE clipboard's IDataObject.
1410 *---------------------------------------------------------------------*/
1412 /************************************************************************
1413 * OLEClipbrd_IEnumFORMATETC_Construct (UINT, const FORMATETC, LPUNKNOWN)
1415 * Creates an IEnumFORMATETC enumerator from an array of FORMATETC
1416 * Structures. pUnkOuter is the outer unknown for reference counting only.
1417 * NOTE: this does not AddRef the interface.
1420 LPENUMFORMATETC
OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt
, const FORMATETC afmt
[],
1421 LPUNKNOWN pUnkDataObj
)
1423 IEnumFORMATETCImpl
* ef
;
1424 DWORD size
=cfmt
* sizeof(FORMATETC
);
1427 ef
= (IEnumFORMATETCImpl
*)HeapAlloc(GetProcessHeap(),
1429 sizeof(IEnumFORMATETCImpl
));
1435 ef
->pUnkDataObj
= pUnkDataObj
;
1438 ef
->countFmt
= cfmt
;
1439 if (FAILED(CoGetMalloc(MEMCTX_TASK
, &pIMalloc
)))
1441 ef
->pFmt
= (LPFORMATETC
)IMalloc_Alloc(pIMalloc
, size
);
1442 IMalloc_Release(pIMalloc
);
1445 memcpy(ef
->pFmt
, afmt
, size
);
1447 TRACE("(%p)->()\n",ef
);
1448 return (LPENUMFORMATETC
)ef
;
1452 /************************************************************************
1453 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
1455 * See Windows documentation for more details on IUnknown methods.
1457 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
1458 (LPENUMFORMATETC iface
, REFIID riid
, LPVOID
* ppvObj
)
1460 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1463 WINE_StringFromCLSID((LPCLSID
)riid
,xriid
);
1464 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,xriid
,ppvObj
);
1467 * Since enumerators are seperate objects from the parent data object
1468 * we only need to support the IUnknown and IEnumFORMATETC interfaces
1473 if(IsEqualIID(riid
, &IID_IUnknown
))
1477 else if(IsEqualIID(riid
, &IID_IEnumFORMATETC
))
1479 *ppvObj
= (IDataObject
*)This
;
1484 IEnumFORMATETC_AddRef((IEnumFORMATETC
*)*ppvObj
);
1485 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
1489 TRACE("-- Interface: E_NOINTERFACE\n");
1490 return E_NOINTERFACE
;
1493 /************************************************************************
1494 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
1496 * Since enumerating formats only makes sense when our data object is around,
1497 * we insure that it stays as long as we stay by calling our parents IUnknown
1498 * for AddRef and Release. But since we are not controlled by the lifetime of
1499 * the outer object, we still keep our own reference count in order to
1502 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface
)
1504 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1505 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1507 if (This
->pUnkDataObj
)
1508 IUnknown_AddRef(This
->pUnkDataObj
);
1510 return ++(This
->ref
);
1513 /************************************************************************
1514 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
1516 * See Windows documentation for more details on IUnknown methods.
1518 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface
)
1520 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1523 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1525 if (This
->pUnkDataObj
)
1526 IUnknown_Release(This
->pUnkDataObj
); /* Release parent data object */
1530 TRACE("() - destroying IEnumFORMATETC(%p)\n",This
);
1531 if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK
, &pIMalloc
)))
1533 IMalloc_Free(pIMalloc
, This
->pFmt
);
1534 IMalloc_Release(pIMalloc
);
1537 HeapFree(GetProcessHeap(),0,This
);
1544 /************************************************************************
1545 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
1547 * Standard enumerator members for IEnumFORMATETC
1549 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
1550 (LPENUMFORMATETC iface
, ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFethed
)
1552 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1554 HRESULT hres
= S_FALSE
;
1556 TRACE("(%p)->(pos=%u)\n", This
, This
->posFmt
);
1558 if (This
->posFmt
< This
->countFmt
)
1560 cfetch
= This
->countFmt
- This
->posFmt
;
1567 memcpy(rgelt
, &This
->pFmt
[This
->posFmt
], cfetch
* sizeof(FORMATETC
));
1568 This
->posFmt
+= cfetch
;
1577 *pceltFethed
= cfetch
;
1583 /************************************************************************
1584 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
1586 * Standard enumerator members for IEnumFORMATETC
1588 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface
, ULONG celt
)
1590 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1591 TRACE("(%p)->(num=%lu)\n", This
, celt
);
1593 This
->posFmt
+= celt
;
1594 if (This
->posFmt
> This
->countFmt
)
1596 This
->posFmt
= This
->countFmt
;
1602 /************************************************************************
1603 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
1605 * Standard enumerator members for IEnumFORMATETC
1607 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface
)
1609 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1610 TRACE("(%p)->()\n", This
);
1616 /************************************************************************
1617 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
1619 * Standard enumerator members for IEnumFORMATETC
1621 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
1622 (LPENUMFORMATETC iface
, LPENUMFORMATETC
* ppenum
)
1624 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1627 TRACE("(%p)->(ppenum=%p)\n", This
, ppenum
);
1630 return E_INVALIDARG
;
1632 *ppenum
= OLEClipbrd_IEnumFORMATETC_Construct(This
->countFmt
,
1636 if (FAILED( hr
= IEnumFORMATETC_AddRef(*ppenum
)))
1639 return (*ppenum
) ? S_OK
: E_OUTOFMEMORY
;