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.
51 #include "debugtools.h"
54 #define HANDLE_ERROR(err) { hr = err; TRACE("(HRESULT=%lx)\n", (HRESULT)err); goto CLEANUP; }
56 /* For CoGetMalloc (MEMCTX_TASK is currently ignored) */
58 #define MEMCTX_TASK -1
61 DEFAULT_DEBUG_CHANNEL(ole
)
63 /****************************************************************************
65 * DO NOT add any members before the VTables declaration!
70 * List all interface VTables here
72 ICOM_VTABLE(IDataObject
)* lpvtbl1
; /* IDataObject VTable */
75 * The hidden OLE clipboard window. This window is used as the bridge between the
76 * the OLE and windows clipboard API. (Windows creates one such window per process)
81 * Pointer to the source data object (via OleSetClipboard)
83 IDataObject
* pIDataObjectSrc
;
86 * The registered DataObject clipboard format
91 * The handle to our ourself
96 * Reference count of this object
101 typedef struct OLEClipbrd OLEClipbrd
;
104 /****************************************************************************
105 * IEnumFORMATETC implementation
106 * DO NOT add any members before the VTables declaration!
110 /* IEnumFORMATETC VTable */
111 ICOM_VTABLE(IEnumFORMATETC
)* lpvtbl
;
113 /* IEnumFORMATETC fields */
114 UINT posFmt
; /* current enumerator position */
115 UINT countFmt
; /* number of EnumFORMATETC's in array */
116 LPFORMATETC pFmt
; /* array of EnumFORMATETC's */
119 * Reference count of this object
124 * IUnknown implementation of the parent data object.
126 IUnknown
* pUnkDataObj
;
128 } IEnumFORMATETCImpl
;
132 * The one and only OLEClipbrd object which is created by OLEClipbrd_Initialize()
134 static HGLOBAL hTheOleClipboard
= 0;
135 static OLEClipbrd
* theOleClipboard
= NULL
;
139 * Prototypes for the methods of the OLEClipboard class.
141 extern void OLEClipbrd_Initialize();
142 extern void OLEClipbrd_UnInitialize();
143 static OLEClipbrd
* OLEClipbrd_Construct();
144 static void OLEClipbrd_Destroy(OLEClipbrd
* ptrToDestroy
);
145 static HWND
OLEClipbrd_CreateWindow();
146 static void OLEClipbrd_DestroyWindow(HWND hwnd
);
147 LRESULT CALLBACK
OLEClipbrd_WndProc(HWND hWnd
, UINT message
, WPARAM wParam
, LPARAM lParam
);
148 static HRESULT
OLEClipbrd_RenderFormat(LPFORMATETC pFormatetc
);
149 static HGLOBAL
OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc
);
152 * Prototypes for the methods of the OLEClipboard class
153 * that implement IDataObject methods.
155 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryInterface(
159 static ULONG WINAPI
OLEClipbrd_IDataObject_AddRef(
161 static ULONG WINAPI
OLEClipbrd_IDataObject_Release(
163 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetData(
165 LPFORMATETC pformatetcIn
,
167 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetDataHere(
169 LPFORMATETC pformatetc
,
171 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryGetData(
173 LPFORMATETC pformatetc
);
174 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
176 LPFORMATETC pformatectIn
,
177 LPFORMATETC pformatetcOut
);
178 static HRESULT WINAPI
OLEClipbrd_IDataObject_SetData(
180 LPFORMATETC pformatetc
,
183 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumFormatEtc(
186 IEnumFORMATETC
** ppenumFormatEtc
);
187 static HRESULT WINAPI
OLEClipbrd_IDataObject_DAdvise(
189 FORMATETC
* pformatetc
,
191 IAdviseSink
* pAdvSink
,
192 DWORD
* pdwConnection
);
193 static HRESULT WINAPI
OLEClipbrd_IDataObject_DUnadvise(
196 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumDAdvise(
198 IEnumSTATDATA
** ppenumAdvise
);
201 * Prototypes for the IEnumFORMATETC methods.
203 static LPENUMFORMATETC
OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt
, const FORMATETC afmt
[],
204 LPUNKNOWN pUnkDataObj
);
205 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_QueryInterface(LPENUMFORMATETC iface
, REFIID riid
,
207 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface
);
208 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface
);
209 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Next(LPENUMFORMATETC iface
, ULONG celt
,
210 FORMATETC
* rgelt
, ULONG
* pceltFethed
);
211 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface
, ULONG celt
);
212 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface
);
213 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Clone(LPENUMFORMATETC iface
, LPENUMFORMATETC
* ppenum
);
217 * Virtual function table for the OLEClipbrd's exposed IDataObject interface
219 static ICOM_VTABLE(IDataObject
) OLEClipbrd_IDataObject_VTable
=
221 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
222 OLEClipbrd_IDataObject_QueryInterface
,
223 OLEClipbrd_IDataObject_AddRef
,
224 OLEClipbrd_IDataObject_Release
,
225 OLEClipbrd_IDataObject_GetData
,
226 OLEClipbrd_IDataObject_GetDataHere
,
227 OLEClipbrd_IDataObject_QueryGetData
,
228 OLEClipbrd_IDataObject_GetCanonicalFormatEtc
,
229 OLEClipbrd_IDataObject_SetData
,
230 OLEClipbrd_IDataObject_EnumFormatEtc
,
231 OLEClipbrd_IDataObject_DAdvise
,
232 OLEClipbrd_IDataObject_DUnadvise
,
233 OLEClipbrd_IDataObject_EnumDAdvise
237 * Virtual function table for IEnumFORMATETC interface
239 static struct ICOM_VTABLE(IEnumFORMATETC
) efvt
=
241 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
242 OLEClipbrd_IEnumFORMATETC_QueryInterface
,
243 OLEClipbrd_IEnumFORMATETC_AddRef
,
244 OLEClipbrd_IEnumFORMATETC_Release
,
245 OLEClipbrd_IEnumFORMATETC_Next
,
246 OLEClipbrd_IEnumFORMATETC_Skip
,
247 OLEClipbrd_IEnumFORMATETC_Reset
,
248 OLEClipbrd_IEnumFORMATETC_Clone
252 * Name of our registered OLE clipboard window class
254 CHAR OLEClipbrd_WNDCLASS
[] = "CLIPBRDWNDCLASS";
257 * If we need to store state info we can store it here.
258 * For now we dont need this functionality.
260 typedef struct tagClipboardWindowInfo
262 } ClipboardWindowInfo;
265 /*---------------------------------------------------------------------*
266 * Win32 OLE clipboard API
267 *---------------------------------------------------------------------*/
269 /***********************************************************************
270 * OleSetClipboard [OLE32.127]
271 * Places a pointer to the specified data object onto the clipboard,
272 * making the data object accessible to the OleGetClipboard function.
276 * S_OK IDataObject pointer placed on the clipboard
277 * CLIPBRD_E_CANT_OPEN OpenClipboard failed
278 * CLIPBRD_E_CANT_EMPTY EmptyClipboard failed
279 * CLIPBRD_E_CANT_CLOSE CloseClipboard failed
280 * CLIPBRD_E_CANT_SET SetClipboard failed
283 HRESULT WINAPI
OleSetClipboard(IDataObject
* pDataObj
)
286 IEnumFORMATETC
* penumFormatetc
= NULL
;
288 BOOL bClipboardOpen
= FALSE
;
290 HGLOBAL hDataObject = 0;
291 OLEClipbrd **ppDataObject;
294 TRACE("(%p)\n", pDataObj
);
297 * Make sure we have a clipboard object
299 OLEClipbrd_Initialize();
302 * If the Ole clipboard window hasn't been created yet, create it now.
304 if ( !theOleClipboard
->hWndClipboard
)
305 theOleClipboard
->hWndClipboard
= OLEClipbrd_CreateWindow();
307 if ( !theOleClipboard
->hWndClipboard
) /* sanity check */
308 HANDLE_ERROR( E_FAIL
);
311 * Open the Windows clipboard, associating it with our hidden window
313 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
314 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
317 * Empty the current clipboard and make our window the clipboard owner
318 * NOTE: This will trigger a WM_DESTROYCLIPBOARD message
320 if ( !EmptyClipboard() )
321 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY
);
324 * If we are already holding on to an IDataObject first release that.
326 if ( theOleClipboard
->pIDataObjectSrc
)
328 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
329 theOleClipboard
->pIDataObjectSrc
= NULL
;
333 * AddRef the data object passed in and save its pointer.
334 * A NULL value indicates that the clipboard should be emptied.
336 theOleClipboard
->pIDataObjectSrc
= pDataObj
;
339 IDataObject_AddRef(theOleClipboard
->pIDataObjectSrc
);
343 * Enumerate all HGLOBAL formats supported by the source and make
344 * those formats available using delayed rendering using SetClipboardData.
345 * Only global memory based data items may be made available to non-OLE
346 * applications via the standard Windows clipboard API. Data based on other
347 * mediums(non TYMED_HGLOBAL) can only be accessed via the Ole Clipboard API.
349 * TODO: Do we need to additionally handle TYMED_IStorage media by copying
350 * the storage into global memory?
354 if ( FAILED(hr
= IDataObject_EnumFormatEtc( pDataObj
,
361 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
363 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
366 TRACE("(cfFormat=%d:%s)\n", rgelt
.cfFormat
,
367 GetClipboardFormatNameA(rgelt
.cfFormat
, szFmtName
, sizeof(szFmtName
)-1)
370 SetClipboardData( rgelt
.cfFormat
, (HANDLE
)NULL
);
373 IEnumFORMATETC_Release(penumFormatetc
);
377 * Windows additionally creates a new "DataObject" clipboard format
378 * and stores in on the clipboard. We could possibly store a pointer
379 * to our internal IDataObject interface on the clipboard. I'm not
380 * sure what the use of this is though.
381 * Enable the code below for this functionality.
384 theOleClipboard->cfDataObj = RegisterClipboardFormatA("DataObject");
385 hDataObject = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT,
386 sizeof(OLEClipbrd *));
388 HANDLE_ERROR( E_OUTOFMEMORY );
390 ppDataObject = (OLEClipbrd**)GlobalLock(hDataObject);
391 *ppDataObject = theOleClipboard;
392 GlobalUnlock(hDataObject);
394 if ( !SetClipboardData( theOleClipboard->cfDataObj, hDataObject ) )
395 HANDLE_ERROR( CLIPBRD_E_CANT_SET );
403 * Close Windows clipboard (It remains associated with our window)
405 if ( bClipboardOpen
&& !CloseClipboard() )
406 hr
= CLIPBRD_E_CANT_CLOSE
;
409 * Release the source IDataObject if something failed
413 if (theOleClipboard
->pIDataObjectSrc
)
415 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
416 theOleClipboard
->pIDataObjectSrc
= NULL
;
424 /***********************************************************************
425 * OleGetClipboard32 [OLE32.105]
426 * Returns a pointer to our internal IDataObject which represents the conceptual
427 * state of the Windows clipboard. If the current clipboard already contains
428 * an IDataObject, our internal IDataObject will delegate to this object.
430 HRESULT WINAPI
OleGetClipboard(IDataObject
** ppDataObj
)
436 * Make sure we have a clipboard object
438 OLEClipbrd_Initialize();
440 if (!theOleClipboard
)
441 return E_OUTOFMEMORY
;
443 /* Return a reference counted IDataObject */
444 hr
= IDataObject_QueryInterface( (IDataObject
*)&(theOleClipboard
->lpvtbl1
),
445 &IID_IDataObject
, (void**)ppDataObj
);
449 /***********************************************************************
450 * OleFlushClipboard [OLE2.76]
453 HRESULT WINAPI
OleFlushClipboard16(void)
456 return OleFlushClipboard();
460 /******************************************************************************
461 * OleFlushClipboard [OLE32.103]
462 * Renders the data from the source IDataObject into the windows clipboard
464 * TODO: OleFlushClipboard needs to additionally handle TYMED_IStorage media
465 * by copying the storage into global memory. Subsequently the default
466 * data object exposed through OleGetClipboard must convert this TYMED_HGLOBAL
467 * back to TYMED_IStorage.
469 HRESULT WINAPI
OleFlushClipboard()
471 IEnumFORMATETC
* penumFormatetc
= NULL
;
474 BOOL bClipboardOpen
= FALSE
;
479 * Make sure we have a clipboard object
481 OLEClipbrd_Initialize();
484 * Already flushed or no source DataObject? Nothing to do.
486 if (!theOleClipboard
->pIDataObjectSrc
)
490 * Open the Windows clipboard
492 if ( !(bClipboardOpen
= OpenClipboard(theOleClipboard
->hWndClipboard
)) )
493 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
496 * Empty the current clipboard
498 if ( !EmptyClipboard() )
499 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY
);
502 * Render all HGLOBAL formats supported by the source into
503 * the windows clipboard.
505 if ( FAILED( hr
= IDataObject_EnumFormatEtc( (IDataObject
*)&(theOleClipboard
->lpvtbl1
),
512 while ( S_OK
== IEnumFORMATETC_Next(penumFormatetc
, 1, &rgelt
, NULL
) )
514 if ( rgelt
.tymed
== TYMED_HGLOBAL
)
517 TRACE("(cfFormat=%d:%s)\n", rgelt
.cfFormat
,
518 GetClipboardFormatNameA(rgelt
.cfFormat
, szFmtName
, sizeof(szFmtName
)-1)
522 * Render the clipboard data
524 if ( FAILED(OLEClipbrd_RenderFormat( &rgelt
)) )
529 IEnumFORMATETC_Release(penumFormatetc
);
532 * Release the data object we are holding on to
534 if ( theOleClipboard
->pIDataObjectSrc
)
536 IDataObject_Release(theOleClipboard
->pIDataObjectSrc
);
537 theOleClipboard
->pIDataObjectSrc
= NULL
;
543 * Close Windows clipboard (It remains associated with our window)
545 if ( bClipboardOpen
&& !CloseClipboard() )
546 hr
= CLIPBRD_E_CANT_CLOSE
;
552 /***********************************************************************
553 * OleIsCurrentClipboard32 [OLE32.110]
555 HRESULT WINAPI
OleIsCurrentClipboard ( IDataObject
*pDataObject
)
559 * Make sure we have a clipboard object
561 OLEClipbrd_Initialize();
563 if (!theOleClipboard
)
564 return E_OUTOFMEMORY
;
566 return (pDataObject
== theOleClipboard
->pIDataObjectSrc
) ? S_OK
: S_FALSE
;
570 /*---------------------------------------------------------------------*
571 * Internal implementation methods for the OLE clipboard
572 *---------------------------------------------------------------------*/
574 /***********************************************************************
575 * OLEClipbrd_Initialize()
576 * Initializes the OLE clipboard.
578 void OLEClipbrd_Initialize()
581 * Create the clipboard if necesary
583 if ( !theOleClipboard
)
586 theOleClipboard
= OLEClipbrd_Construct();
591 /***********************************************************************
592 * OLEClipbrd_UnInitialize()
593 * Un-Initializes the OLE clipboard
595 void OLEClipbrd_UnInitialize()
599 * Destroy the clipboard if no one holds a reference to us.
600 * Note that the clipboard was created with a reference count of 1.
602 if ( theOleClipboard
&& (theOleClipboard
->ref
<= 1) )
604 OLEClipbrd_Destroy( theOleClipboard
);
608 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
613 /*********************************************************
614 * Construct the OLEClipbrd class.
616 static OLEClipbrd
* OLEClipbrd_Construct()
618 OLEClipbrd
* newObject
= NULL
;
619 HGLOBAL hNewObject
= 0;
622 * Allocate space for the object. We use GlobalAlloc since we need
623 * an HGLOBAL to expose our DataObject as a registered clipboard type.
625 hNewObject
= GlobalAlloc( GMEM_DDESHARE
|GMEM_MOVEABLE
|GMEM_ZEROINIT
,
631 * Lock the handle for the entire lifetime of the clipboard.
633 newObject
= GlobalLock(hNewObject
);
636 * Initialize the virtual function table.
638 newObject
->lpvtbl1
= &OLEClipbrd_IDataObject_VTable
;
641 * Start with one reference count. The caller of this function
642 * must release the interface pointer when it is done.
646 newObject
->hSelf
= hNewObject
;
649 * The Ole clipboard is a singleton - save the global handle and pointer
651 theOleClipboard
= newObject
;
652 hTheOleClipboard
= hNewObject
;
654 return theOleClipboard
;
657 static void OLEClipbrd_Destroy(OLEClipbrd
* ptrToDestroy
)
665 * Destroy the Ole clipboard window
667 if ( ptrToDestroy
->hWndClipboard
)
668 OLEClipbrd_DestroyWindow(ptrToDestroy
->hWndClipboard
);
671 * Free the actual OLE Clipboard structure.
673 TRACE("() - Destroying clipboard data object.\n");
674 GlobalUnlock(ptrToDestroy
->hSelf
);
675 GlobalFree(ptrToDestroy
->hSelf
);
678 * The Ole clipboard is a singleton (ptrToDestroy == theOleClipboard)
680 theOleClipboard
= NULL
;
681 hTheOleClipboard
= 0;
685 /***********************************************************************
686 * OLEClipbrd_CreateWindow()
687 * Create the clipboard window
689 static HWND
OLEClipbrd_CreateWindow()
696 * Register the clipboard window class if necessary
699 if ( !( atom
= GlobalFindAtomA(OLEClipbrd_WNDCLASS
) ) ||
700 !( CLASS_FindClassByAtom(atom
, 0) ) )
702 ZeroMemory( &wcex
, sizeof(WNDCLASSEXA
));
704 wcex
.cbSize
= sizeof(WNDCLASSEXA
);
705 /* Windows creates this class with a style mask of 0
706 * We dont bother doing this since the FindClassByAtom code
707 * would have to be changed to deal with this idiosyncracy. */
708 wcex
.style
= CS_GLOBALCLASS
;
709 wcex
.lpfnWndProc
= (WNDPROC
)OLEClipbrd_WndProc
;
711 wcex
.lpszClassName
= OLEClipbrd_WNDCLASS
;
713 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( &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( &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(LPFORMATETC pFormatetc
)
899 if ( FAILED(hr
= IDataObject_GetData((IDataObject
*)&(theOleClipboard
->lpvtbl1
),
900 pFormatetc
, &medium
)) )
902 WARN("() : IDataObject_GetData failed to render clipboard data! (%lx)\n", hr
);
907 * Put a copy of the rendered data back on the clipboard
910 if ( !(hDup
= OLEClipbrd_GlobalDupMem(medium
.u
.hGlobal
)) )
911 HANDLE_ERROR( E_OUTOFMEMORY
);
913 if ( !SetClipboardData( pFormatetc
->cfFormat
, hDup
) )
916 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
921 ReleaseStgMedium(&medium
);
927 /***********************************************************************
928 * OLEClipbrd_GlobalDupMem( HGLOBAL )
929 * Helper method to duplicate an HGLOBAL chunk of memory
931 static HGLOBAL
OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc
)
934 PVOID pGlobalSrc
, pGlobalDest
;
940 cBytes
= GlobalSize(hGlobalSrc
);
944 hGlobalDest
= GlobalAlloc( GMEM_DDESHARE
|GMEM_MOVEABLE
,
949 pGlobalSrc
= GlobalLock(hGlobalSrc
);
950 pGlobalDest
= GlobalLock(hGlobalDest
);
951 if ( !pGlobalSrc
|| !pGlobalDest
)
954 memcpy(pGlobalDest
, pGlobalSrc
, cBytes
);
956 GlobalUnlock(hGlobalSrc
);
957 GlobalUnlock(hGlobalDest
);
963 /*---------------------------------------------------------------------*
964 * Implementation of the internal IDataObject interface exposed by
966 *---------------------------------------------------------------------*/
969 /************************************************************************
970 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
972 * See Windows documentation for more details on IUnknown methods.
974 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryInterface(
980 * Declare "This" pointer
982 ICOM_THIS(OLEClipbrd
, iface
);
985 WINE_StringFromCLSID((LPCLSID
)riid
,xriid
);
986 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,xriid
,ppvObject
);
989 * Perform a sanity check on the parameters.
991 if ( (This
==0) || (ppvObject
==0) )
995 * Initialize the return parameter.
1000 * Compare the riid with the interface IDs implemented by this object.
1002 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
1006 else if (memcmp(&IID_IDataObject
, riid
, sizeof(IID_IDataObject
)) == 0)
1008 *ppvObject
= (IDataObject
*)&(This
->lpvtbl1
);
1010 else /* We only support IUnknown and IDataObject */
1014 WINE_StringFromCLSID((LPCLSID
)riid
,clsid
);
1015 WARN( "() : asking for unsupported interface %s\n", clsid
);
1017 return E_NOINTERFACE
;
1021 * Query Interface always increases the reference count by one when it is
1024 IUnknown_AddRef((IUnknown
*)*ppvObject
);
1029 /************************************************************************
1030 * OLEClipbrd_IDataObject_AddRef (IUnknown)
1032 * See Windows documentation for more details on IUnknown methods.
1034 static ULONG WINAPI
OLEClipbrd_IDataObject_AddRef(
1038 * Declare "This" pointer
1040 ICOM_THIS(OLEClipbrd
, iface
);
1042 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1049 /************************************************************************
1050 * OLEClipbrd_IDataObject_Release (IUnknown)
1052 * See Windows documentation for more details on IUnknown methods.
1054 static ULONG WINAPI
OLEClipbrd_IDataObject_Release(
1058 * Declare "This" pointer
1060 ICOM_THIS(OLEClipbrd
, iface
);
1062 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1065 * Decrease the reference count on this object.
1070 * If the reference count goes down to 0, perform suicide.
1074 OLEClipbrd_Destroy(This
);
1081 /************************************************************************
1082 * OLEClipbrd_IDataObject_GetData (IDataObject)
1084 * The OLE Clipboard's implementation of this method delegates to
1085 * a data source if there is one or wraps around the windows clipboard
1087 * See Windows documentation for more details on IDataObject methods.
1089 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetData(
1091 LPFORMATETC pformatetcIn
,
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, delegate to the Windows clipboard function GetClipboardData
1128 hData
= GetClipboardData(pformatetcIn
->cfFormat
);
1131 * Return the clipboard data in the storage medium structure
1133 pmedium
->tymed
= (hData
== 0) ? TYMED_NULL
: TYMED_HGLOBAL
;
1134 pmedium
->u
.hGlobal
= (HGLOBAL
)hData
;
1135 pmedium
->pUnkForRelease
= NULL
;
1137 return (hData
== 0) ? DV_E_FORMATETC
: S_OK
;
1140 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetDataHere(
1142 LPFORMATETC pformatetc
,
1149 /************************************************************************
1150 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
1152 * The OLE Clipboard's implementation of this method delegates to
1153 * a data source if there is one or wraps around the windows clipboard
1154 * function IsClipboardFormatAvailable() otherwise.
1156 * See Windows documentation for more details on IDataObject methods.
1158 static HRESULT WINAPI
OLEClipbrd_IDataObject_QueryGetData(
1160 LPFORMATETC pformatetc
)
1163 * Declare "This" pointer
1165 ICOM_THIS(OLEClipbrd
, iface
);
1167 TRACE("(%p, %p)\n", iface
, pformatetc
);
1170 * If we have a data source placed on the clipboard (via OleSetClipboard)
1171 * simply delegate to the source object's QueryGetData
1173 if ( This
->pIDataObjectSrc
)
1175 return IDataObject_QueryGetData(This
->pIDataObjectSrc
, pformatetc
);
1179 return E_INVALIDARG
;
1181 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
1182 return DV_E_DVASPECT;
1184 if ( pformatetc
->lindex
!= -1 )
1187 /* TODO: Handle TYMED_IStorage media which were put on the clipboard
1188 * by copying the storage into global memory. We must convert this
1189 * TYMED_HGLOBAL back to TYMED_IStorage.
1191 if ( pformatetc
->tymed
!= TYMED_HGLOBAL
)
1195 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
1197 return (IsClipboardFormatAvailable(pformatetc
->cfFormat
)) ? S_OK
: DV_E_FORMATETC
;
1200 /************************************************************************
1201 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
1203 * See Windows documentation for more details on IDataObject methods.
1205 static HRESULT WINAPI
OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
1207 LPFORMATETC pformatectIn
,
1208 LPFORMATETC pformatetcOut
)
1210 TRACE("(%p, %p, %p)\n", iface
, pformatectIn
, pformatetcOut
);
1212 if ( !pformatectIn
|| !pformatetcOut
)
1213 return E_INVALIDARG
;
1215 memcpy(pformatetcOut
, pformatectIn
, sizeof(FORMATETC
));
1216 return DATA_S_SAMEFORMATETC
;
1219 /************************************************************************
1220 * OLEClipbrd_IDataObject_SetData (IDataObject)
1222 * The OLE Clipboard's does not implement this method
1224 * See Windows documentation for more details on IDataObject methods.
1226 static HRESULT WINAPI
OLEClipbrd_IDataObject_SetData(
1228 LPFORMATETC pformatetc
,
1235 /************************************************************************
1236 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
1238 * See Windows documentation for more details on IDataObject methods.
1240 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumFormatEtc(
1243 IEnumFORMATETC
** ppenumFormatEtc
)
1246 FORMATETC
*afmt
= NULL
;
1249 BOOL bClipboardOpen
;
1252 * Declare "This" pointer
1254 ICOM_THIS(OLEClipbrd
, iface
);
1256 TRACE("(%p, %lx, %p)\n", iface
, dwDirection
, ppenumFormatEtc
);
1259 * If we have a data source placed on the clipboard (via OleSetClipboard)
1260 * simply delegate to the source object's EnumFormatEtc
1262 if ( This
->pIDataObjectSrc
)
1264 return IDataObject_EnumFormatEtc(This
->pIDataObjectSrc
,
1265 dwDirection
, ppenumFormatEtc
);
1269 * Otherwise we must provide our own enumerator which wraps around the
1270 * Windows clipboard function EnumClipboardFormats
1272 if ( !ppenumFormatEtc
)
1273 return E_INVALIDARG
;
1275 if ( dwDirection
!= DATADIR_GET
) /* IDataObject_SetData not implemented */
1279 * Store all current clipboard formats in an array of FORMATETC's,
1280 * and create an IEnumFORMATETC enumerator from this list.
1282 cfmt
= CountClipboardFormats();
1283 afmt
= (FORMATETC
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
1284 sizeof(FORMATETC
) * cfmt
);
1286 * Open the Windows clipboard, associating it with our hidden window
1288 if ( !(bClipboardOpen
= OpenClipboard(This
->hWndClipboard
)) )
1289 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN
);
1292 * Store all current clipboard formats in an array of FORMATETC's
1293 * TODO: Handle TYMED_IStorage media which were put on the clipboard
1294 * by copying the storage into global memory. We must convert this
1295 * TYMED_HGLOBAL back to TYMED_IStorage.
1297 for (i
= 0, format
= 0; i
< cfmt
; i
++)
1299 format
= EnumClipboardFormats(format
);
1300 if (!format
) /* Failed! */
1302 ERR("EnumClipboardFormats failed to return format!\n");
1303 HANDLE_ERROR( E_FAIL
);
1306 /* Init the FORMATETC struct */
1307 afmt
[i
].cfFormat
= format
;
1309 afmt
[i
].dwAspect
= DVASPECT_CONTENT
;
1310 afmt
[i
].lindex
= -1;
1311 afmt
[i
].tymed
= TYMED_HGLOBAL
;
1315 * Create an EnumFORMATETC enumerator and return an
1316 * EnumFORMATETC after bumping up its ref count
1318 *ppenumFormatEtc
= OLEClipbrd_IEnumFORMATETC_Construct( cfmt
, afmt
, (LPUNKNOWN
)iface
);
1319 if (!(*ppenumFormatEtc
))
1320 HANDLE_ERROR( E_OUTOFMEMORY
);
1322 if (FAILED( hr
= IEnumFORMATETC_AddRef(*ppenumFormatEtc
)))
1329 * Free the array of FORMATETC's
1332 HeapFree(GetProcessHeap(), 0, afmt
);
1335 * Close Windows clipboard
1337 if ( bClipboardOpen
&& !CloseClipboard() )
1338 hr
= CLIPBRD_E_CANT_CLOSE
;
1343 /************************************************************************
1344 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
1346 * The OLE Clipboard's does not implement this method
1348 * See Windows documentation for more details on IDataObject methods.
1350 static HRESULT WINAPI
OLEClipbrd_IDataObject_DAdvise(
1352 FORMATETC
* pformatetc
,
1354 IAdviseSink
* pAdvSink
,
1355 DWORD
* pdwConnection
)
1360 /************************************************************************
1361 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
1363 * The OLE Clipboard's does not implement this method
1365 * See Windows documentation for more details on IDataObject methods.
1367 static HRESULT WINAPI
OLEClipbrd_IDataObject_DUnadvise(
1374 /************************************************************************
1375 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1377 * The OLE Clipboard does not implement this method
1379 * See Windows documentation for more details on IDataObject methods.
1381 static HRESULT WINAPI
OLEClipbrd_IDataObject_EnumDAdvise(
1383 IEnumSTATDATA
** ppenumAdvise
)
1389 /*---------------------------------------------------------------------*
1390 * Implementation of the internal IEnumFORMATETC interface returned by
1391 * the OLE clipboard's IDataObject.
1392 *---------------------------------------------------------------------*/
1394 /************************************************************************
1395 * OLEClipbrd_IEnumFORMATETC_Construct (UINT, const FORMATETC, LPUNKNOWN)
1397 * Creates an IEnumFORMATETC enumerator from an array of FORMATETC
1398 * Structures. pUnkOuter is the outer unknown for reference counting only.
1399 * NOTE: this does not AddRef the interface.
1402 LPENUMFORMATETC
OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt
, const FORMATETC afmt
[],
1403 LPUNKNOWN pUnkDataObj
)
1405 IEnumFORMATETCImpl
* ef
;
1406 DWORD size
=cfmt
* sizeof(FORMATETC
);
1409 ef
= (IEnumFORMATETCImpl
*)HeapAlloc(GetProcessHeap(),
1411 sizeof(IEnumFORMATETCImpl
));
1417 ef
->pUnkDataObj
= pUnkDataObj
;
1420 ef
->countFmt
= cfmt
;
1421 if (FAILED(CoGetMalloc(MEMCTX_TASK
, &pIMalloc
)))
1423 ef
->pFmt
= (LPFORMATETC
)IMalloc_Alloc(pIMalloc
, size
);
1424 IMalloc_Release(pIMalloc
);
1427 memcpy(ef
->pFmt
, afmt
, size
);
1429 TRACE("(%p)->()\n",ef
);
1430 return (LPENUMFORMATETC
)ef
;
1434 /************************************************************************
1435 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
1437 * See Windows documentation for more details on IUnknown methods.
1439 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
1440 (LPENUMFORMATETC iface
, REFIID riid
, LPVOID
* ppvObj
)
1442 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1445 WINE_StringFromCLSID((LPCLSID
)riid
,xriid
);
1446 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,xriid
,ppvObj
);
1449 * Since enumerators are seperate objects from the parent data object
1450 * we only need to support the IUnknown and IEnumFORMATETC interfaces
1455 if(IsEqualIID(riid
, &IID_IUnknown
))
1459 else if(IsEqualIID(riid
, &IID_IEnumFORMATETC
))
1461 *ppvObj
= (IDataObject
*)This
;
1466 IEnumFORMATETC_AddRef((IEnumFORMATETC
*)*ppvObj
);
1467 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
1471 TRACE("-- Interface: E_NOINTERFACE\n");
1472 return E_NOINTERFACE
;
1475 /************************************************************************
1476 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
1478 * Since enumerating formats only makes sense when our data object is around,
1479 * we insure that it stays as long as we stay by calling our parents IUnknown
1480 * for AddRef and Release. But since we are not controlled by the lifetime of
1481 * the outer object, we still keep our own reference count in order to
1484 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface
)
1486 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1487 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1489 if (This
->pUnkDataObj
)
1490 IUnknown_AddRef(This
->pUnkDataObj
);
1492 return ++(This
->ref
);
1495 /************************************************************************
1496 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
1498 * See Windows documentation for more details on IUnknown methods.
1500 static ULONG WINAPI
OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface
)
1502 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1505 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
1507 if (This
->pUnkDataObj
)
1508 IUnknown_Release(This
->pUnkDataObj
); /* Release parent data object */
1512 TRACE("() - destroying IEnumFORMATETC(%p)\n",This
);
1513 if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK
, &pIMalloc
)))
1515 IMalloc_Free(pIMalloc
, This
->pFmt
);
1516 IMalloc_Release(pIMalloc
);
1519 HeapFree(GetProcessHeap(),0,This
);
1526 /************************************************************************
1527 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
1529 * Standard enumerator members for IEnumFORMATETC
1531 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
1532 (LPENUMFORMATETC iface
, ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFethed
)
1534 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1536 HRESULT hres
= S_FALSE
;
1538 TRACE("(%p)->(pos=%u)\n", This
, This
->posFmt
);
1540 if (This
->posFmt
< This
->countFmt
)
1542 cfetch
= This
->countFmt
- This
->posFmt
;
1549 memcpy(rgelt
, &This
->pFmt
[This
->posFmt
], cfetch
* sizeof(FORMATETC
));
1550 This
->posFmt
+= cfetch
;
1559 *pceltFethed
= cfetch
;
1565 /************************************************************************
1566 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
1568 * Standard enumerator members for IEnumFORMATETC
1570 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface
, ULONG celt
)
1572 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1573 TRACE("(%p)->(num=%lu)\n", This
, celt
);
1575 This
->posFmt
+= celt
;
1576 if (This
->posFmt
> This
->countFmt
)
1578 This
->posFmt
= This
->countFmt
;
1584 /************************************************************************
1585 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
1587 * Standard enumerator members for IEnumFORMATETC
1589 static HRESULT WINAPI
OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface
)
1591 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1592 TRACE("(%p)->()\n", This
);
1598 /************************************************************************
1599 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
1601 * Standard enumerator members for IEnumFORMATETC
1603 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
1604 (LPENUMFORMATETC iface
, LPENUMFORMATETC
* ppenum
)
1606 ICOM_THIS(IEnumFORMATETCImpl
,iface
);
1609 TRACE("(%p)->(ppenum=%p)\n", This
, ppenum
);
1612 return E_INVALIDARG
;
1614 *ppenum
= OLEClipbrd_IEnumFORMATETC_Construct(This
->countFmt
,
1618 if (FAILED( hr
= IEnumFORMATETC_AddRef(*ppenum
)))
1621 return (*ppenum
) ? S_OK
: E_OUTOFMEMORY
;