Added support for WODM_BREAKLOOP message.
[wine.git] / dlls / ole32 / clipboard.c
blob372f9bffeb2de824801c6e31b1a81c216f26ca45
1 /*
2 * OLE 2 clipboard support
4 * Copyright 1999 Noel Borthwick <noel@macadamian.com>
6 * NOTES:
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
31 * TODO:
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.
46 #include <assert.h>
47 #include "winuser.h"
48 #include "winbase.h"
49 #include "winerror.h"
50 #include "ole2.h"
51 #include "class.h"
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) */
58 #ifndef MEMCTX_TASK
59 #define MEMCTX_TASK -1
60 #endif
62 DEFAULT_DEBUG_CHANNEL(ole)
64 /****************************************************************************
65 * OLEClipbrd
66 * DO NOT add any members before the VTables declaration!
68 struct OLEClipbrd
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)
79 HWND hWndClipboard;
82 * Pointer to the source data object (via OleSetClipboard)
84 IDataObject* pIDataObjectSrc;
87 * The registered DataObject clipboard format
89 UINT cfDataObj;
92 * The handle to our ourself
94 UINT hSelf;
97 * Reference count of this object
99 ULONG ref;
102 typedef struct OLEClipbrd OLEClipbrd;
105 /****************************************************************************
106 * IEnumFORMATETC implementation
107 * DO NOT add any members before the VTables declaration!
109 typedef struct
111 /* IEnumFORMATETC VTable */
112 ICOM_VFIELD(IEnumFORMATETC);
114 /* IEnumFORMATETC fields */
115 UINT posFmt; /* current enumerator position */
116 UINT countFmt; /* number of EnumFORMATETC's in array */
117 LPFORMATETC pFmt; /* array of EnumFORMATETC's */
120 * Reference count of this object
122 DWORD ref;
125 * IUnknown implementation of the parent data object.
127 IUnknown* pUnkDataObj;
129 } IEnumFORMATETCImpl;
133 * The one and only OLEClipbrd object which is created by OLEClipbrd_Initialize()
135 static HGLOBAL hTheOleClipboard = 0;
136 static OLEClipbrd* theOleClipboard = NULL;
140 * Prototypes for the methods of the OLEClipboard class.
142 extern void OLEClipbrd_Initialize();
143 extern void OLEClipbrd_UnInitialize();
144 static OLEClipbrd* OLEClipbrd_Construct();
145 static void OLEClipbrd_Destroy(OLEClipbrd* ptrToDestroy);
146 static HWND OLEClipbrd_CreateWindow();
147 static void OLEClipbrd_DestroyWindow(HWND hwnd);
148 LRESULT CALLBACK OLEClipbrd_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
149 static HRESULT OLEClipbrd_RenderFormat( IDataObject *pIDataObject, LPFORMATETC pFormatetc );
150 static HGLOBAL OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc );
153 * Prototypes for the methods of the OLEClipboard class
154 * that implement IDataObject methods.
156 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryInterface(
157 IDataObject* iface,
158 REFIID riid,
159 void** ppvObject);
160 static ULONG WINAPI OLEClipbrd_IDataObject_AddRef(
161 IDataObject* iface);
162 static ULONG WINAPI OLEClipbrd_IDataObject_Release(
163 IDataObject* iface);
164 static HRESULT WINAPI OLEClipbrd_IDataObject_GetData(
165 IDataObject* iface,
166 LPFORMATETC pformatetcIn,
167 STGMEDIUM* pmedium);
168 static HRESULT WINAPI OLEClipbrd_IDataObject_GetDataHere(
169 IDataObject* iface,
170 LPFORMATETC pformatetc,
171 STGMEDIUM* pmedium);
172 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryGetData(
173 IDataObject* iface,
174 LPFORMATETC pformatetc);
175 static HRESULT WINAPI OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
176 IDataObject* iface,
177 LPFORMATETC pformatectIn,
178 LPFORMATETC pformatetcOut);
179 static HRESULT WINAPI OLEClipbrd_IDataObject_SetData(
180 IDataObject* iface,
181 LPFORMATETC pformatetc,
182 STGMEDIUM* pmedium,
183 BOOL fRelease);
184 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumFormatEtc(
185 IDataObject* iface,
186 DWORD dwDirection,
187 IEnumFORMATETC** ppenumFormatEtc);
188 static HRESULT WINAPI OLEClipbrd_IDataObject_DAdvise(
189 IDataObject* iface,
190 FORMATETC* pformatetc,
191 DWORD advf,
192 IAdviseSink* pAdvSink,
193 DWORD* pdwConnection);
194 static HRESULT WINAPI OLEClipbrd_IDataObject_DUnadvise(
195 IDataObject* iface,
196 DWORD dwConnection);
197 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumDAdvise(
198 IDataObject* iface,
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,
207 LPVOID* ppvObj);
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.
275 * RETURNS:
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)
286 HRESULT hr = S_OK;
287 IEnumFORMATETC* penumFormatetc = NULL;
288 FORMATETC rgelt;
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;
338 if ( 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?
353 if ( pDataObj )
355 if ( FAILED(hr = IDataObject_EnumFormatEtc( pDataObj,
356 DATADIR_GET,
357 &penumFormatetc )))
359 HANDLE_ERROR( hr );
362 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
364 if ( rgelt.tymed == TYMED_HGLOBAL )
366 CHAR szFmtName[80];
367 TRACE("(cfFormat=%d:%s)\n", rgelt.cfFormat,
368 GetClipboardFormatNameA(rgelt.cfFormat, szFmtName, sizeof(szFmtName)-1)
369 ? szFmtName : "");
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 *));
388 if (hDataObject==0)
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 );
399 hr = S_OK;
401 CLEANUP:
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
412 if ( FAILED(hr) )
414 if (theOleClipboard->pIDataObjectSrc)
416 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
417 theOleClipboard->pIDataObjectSrc = NULL;
421 return hr;
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)
433 HRESULT hr = S_OK;
434 TRACE("()\n");
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);
447 return hr;
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;
473 FORMATETC rgelt;
474 HRESULT hr = S_OK;
475 BOOL bClipboardOpen = FALSE;
476 IDataObject* pIDataObjectSrc = NULL;
478 TRACE("()\n");
481 * Make sure we have a clipboard object
483 OLEClipbrd_Initialize();
486 * Already flushed or no source DataObject? Nothing to do.
488 if (!theOleClipboard->pIDataObjectSrc)
489 return S_OK;
492 * Addref and save the source data object we are holding on to temporarily,
493 * since it will be released when we empty the clipboard.
495 pIDataObjectSrc = theOleClipboard->pIDataObjectSrc;
496 IDataObject_AddRef(pIDataObjectSrc);
499 * Open the Windows clipboard
501 if ( !(bClipboardOpen = OpenClipboard(theOleClipboard->hWndClipboard)) )
502 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
505 * Empty the current clipboard
507 if ( !EmptyClipboard() )
508 HANDLE_ERROR( CLIPBRD_E_CANT_EMPTY );
511 * Render all HGLOBAL formats supported by the source into
512 * the windows clipboard.
514 if ( FAILED( hr = IDataObject_EnumFormatEtc( pIDataObjectSrc,
515 DATADIR_GET,
516 &penumFormatetc) ))
518 HANDLE_ERROR( hr );
521 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
523 if ( rgelt.tymed == TYMED_HGLOBAL )
525 CHAR szFmtName[80];
526 TRACE("(cfFormat=%d:%s)\n", rgelt.cfFormat,
527 GetClipboardFormatNameA(rgelt.cfFormat, szFmtName, sizeof(szFmtName)-1)
528 ? szFmtName : "");
531 * Render the clipboard data
533 if ( FAILED(OLEClipbrd_RenderFormat( pIDataObjectSrc, &rgelt )) )
534 continue;
538 IEnumFORMATETC_Release(penumFormatetc);
541 * Release the source data object we are holding on to
543 IDataObject_Release(pIDataObjectSrc);
545 CLEANUP:
548 * Close Windows clipboard (It remains associated with our window)
550 if ( bClipboardOpen && !CloseClipboard() )
551 hr = CLIPBRD_E_CANT_CLOSE;
553 return hr;
557 /***********************************************************************
558 * OleIsCurrentClipboard32 [OLE32.110]
560 HRESULT WINAPI OleIsCurrentClipboard ( IDataObject *pDataObject)
562 TRACE("()\n");
564 * Make sure we have a clipboard object
566 OLEClipbrd_Initialize();
568 if (!theOleClipboard)
569 return E_OUTOFMEMORY;
571 return (pDataObject == theOleClipboard->pIDataObjectSrc) ? S_OK : S_FALSE;
575 /*---------------------------------------------------------------------*
576 * Internal implementation methods for the OLE clipboard
577 *---------------------------------------------------------------------*/
579 /***********************************************************************
580 * OLEClipbrd_Initialize()
581 * Initializes the OLE clipboard.
583 void OLEClipbrd_Initialize()
586 * Create the clipboard if necesary
588 if ( !theOleClipboard )
590 TRACE("()\n");
591 theOleClipboard = OLEClipbrd_Construct();
596 /***********************************************************************
597 * OLEClipbrd_UnInitialize()
598 * Un-Initializes the OLE clipboard
600 void OLEClipbrd_UnInitialize()
602 TRACE("()\n");
604 * Destroy the clipboard if no one holds a reference to us.
605 * Note that the clipboard was created with a reference count of 1.
607 if ( theOleClipboard && (theOleClipboard->ref <= 1) )
609 OLEClipbrd_Destroy( theOleClipboard );
611 else
613 WARN( "() : OLEClipbrd_UnInitialize called while client holds an IDataObject reference!\n");
618 /*********************************************************
619 * Construct the OLEClipbrd class.
621 static OLEClipbrd* OLEClipbrd_Construct()
623 OLEClipbrd* newObject = NULL;
624 HGLOBAL hNewObject = 0;
627 * Allocate space for the object. We use GlobalAlloc since we need
628 * an HGLOBAL to expose our DataObject as a registered clipboard type.
630 hNewObject = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE|GMEM_ZEROINIT,
631 sizeof(OLEClipbrd));
632 if (hNewObject==0)
633 return NULL;
636 * Lock the handle for the entire lifetime of the clipboard.
638 newObject = GlobalLock(hNewObject);
641 * Initialize the virtual function table.
643 newObject->lpvtbl1 = &OLEClipbrd_IDataObject_VTable;
646 * Start with one reference count. The caller of this function
647 * must release the interface pointer when it is done.
649 newObject->ref = 1;
651 newObject->hSelf = hNewObject;
654 * The Ole clipboard is a singleton - save the global handle and pointer
656 theOleClipboard = newObject;
657 hTheOleClipboard = hNewObject;
659 return theOleClipboard;
662 static void OLEClipbrd_Destroy(OLEClipbrd* ptrToDestroy)
664 TRACE("()\n");
666 if ( !ptrToDestroy )
667 return;
670 * Destroy the Ole clipboard window
672 if ( ptrToDestroy->hWndClipboard )
673 OLEClipbrd_DestroyWindow(ptrToDestroy->hWndClipboard);
676 * Free the actual OLE Clipboard structure.
678 TRACE("() - Destroying clipboard data object.\n");
679 GlobalUnlock(ptrToDestroy->hSelf);
680 GlobalFree(ptrToDestroy->hSelf);
683 * The Ole clipboard is a singleton (ptrToDestroy == theOleClipboard)
685 theOleClipboard = NULL;
686 hTheOleClipboard = 0;
690 /***********************************************************************
691 * OLEClipbrd_CreateWindow()
692 * Create the clipboard window
694 static HWND OLEClipbrd_CreateWindow()
696 HWND hwnd = 0;
697 WNDCLASSEXA wcex;
698 ATOM atom;
701 * Register the clipboard window class if necessary
704 if ( !( atom = GlobalFindAtomA(OLEClipbrd_WNDCLASS) ) ||
705 !( CLASS_FindClassByAtom(atom, 0) ) )
707 ZeroMemory( &wcex, sizeof(WNDCLASSEXA));
709 wcex.cbSize = sizeof(WNDCLASSEXA);
710 /* Windows creates this class with a style mask of 0
711 * We dont bother doing this since the FindClassByAtom code
712 * would have to be changed to deal with this idiosyncracy. */
713 wcex.style = CS_GLOBALCLASS;
714 wcex.lpfnWndProc = (WNDPROC)OLEClipbrd_WndProc;
715 wcex.hInstance = 0;
716 wcex.lpszClassName = OLEClipbrd_WNDCLASS;
718 RegisterClassExA(&wcex);
722 * Create a hidden window to receive OLE clipboard messages
726 * If we need to store state info we can store it here.
727 * For now we dont need this functionality.
728 * ClipboardWindowInfo clipboardInfo;
729 * ZeroMemory( &trackerInfo, sizeof(ClipboardWindowInfo));
732 hwnd = CreateWindowA(OLEClipbrd_WNDCLASS,
733 "ClipboardWindow",
734 WS_POPUP | WS_CLIPSIBLINGS | WS_OVERLAPPED,
735 CW_USEDEFAULT, CW_USEDEFAULT,
736 CW_USEDEFAULT, CW_USEDEFAULT,
740 0 /*(LPVOID)&clipboardInfo */);
742 return hwnd;
745 /***********************************************************************
746 * OLEClipbrd_DestroyWindow(HWND)
747 * Destroy the clipboard window and unregister its class
749 static void OLEClipbrd_DestroyWindow(HWND hwnd)
752 * Destroy clipboard window and unregister its WNDCLASS
754 DestroyWindow(hwnd);
755 UnregisterClassA( OLEClipbrd_WNDCLASS, 0 );
758 /***********************************************************************
759 * OLEClipbrd_WndProc(HWND, unsigned, WORD, LONG)
760 * Processes messages sent to the OLE clipboard window.
761 * Note that we will intercept messages in our WndProc only when data
762 * has been placed in the clipboard via OleSetClipboard().
763 * i.e. Only when OLE owns the windows clipboard.
765 LRESULT CALLBACK OLEClipbrd_WndProc
766 (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
768 switch (message)
771 * WM_RENDERFORMAT
772 * We receive this message to allow us to handle delayed rendering of
773 * a specific clipboard format when an application requests data in
774 * that format by calling GetClipboardData.
775 * (Recall that in OleSetClipboard, we used SetClipboardData to
776 * make all HGLOBAL formats supported by the source IDataObject
777 * available using delayed rendering)
778 * On receiving this mesage we must actually render the data in the
779 * specified format and place it on the clipboard by calling the
780 * SetClipboardData function.
782 case WM_RENDERFORMAT:
784 FORMATETC rgelt;
786 ZeroMemory( &rgelt, sizeof(FORMATETC));
789 * Initialize FORMATETC to a Windows clipboard friendly format
791 rgelt.cfFormat = (UINT) wParam;
792 rgelt.dwAspect = DVASPECT_CONTENT;
793 rgelt.lindex = -1;
794 rgelt.tymed = TYMED_HGLOBAL;
796 TRACE("(): WM_RENDERFORMAT(cfFormat=%d)\n", rgelt.cfFormat);
799 * Render the clipboard data.
800 * (We must have a source data object or we wouldn't be in this WndProc)
802 OLEClipbrd_RenderFormat( (IDataObject*)&(theOleClipboard->lpvtbl1), &rgelt );
804 break;
808 * WM_RENDERALLFORMATS
809 * Sent before the clipboard owner window is destroyed.
810 * We should receive this message only when OleUninitialize is called
811 * while we have an IDataObject in the clipboard.
812 * For the content of the clipboard to remain available to other
813 * applications, we must render data in all the formats the source IDataObject
814 * is capable of generating, and place the data on the clipboard by calling
815 * SetClipboardData.
817 case WM_RENDERALLFORMATS:
819 IEnumFORMATETC* penumFormatetc = NULL;
820 FORMATETC rgelt;
822 TRACE("(): WM_RENDERALLFORMATS\n");
825 * Render all HGLOBAL formats supported by the source into
826 * the windows clipboard.
828 if ( FAILED( IDataObject_EnumFormatEtc( (IDataObject*)&(theOleClipboard->lpvtbl1),
829 DATADIR_GET, &penumFormatetc) ) )
831 WARN("(): WM_RENDERALLFORMATS failed to retrieve EnumFormatEtc!\n");
832 return 0;
835 while ( S_OK == IEnumFORMATETC_Next(penumFormatetc, 1, &rgelt, NULL) )
837 if ( rgelt.tymed == TYMED_HGLOBAL )
840 * Render the clipboard data.
842 if ( FAILED(OLEClipbrd_RenderFormat( (IDataObject*)&(theOleClipboard->lpvtbl1), &rgelt )) )
843 continue;
845 TRACE("(): WM_RENDERALLFORMATS(cfFormat=%d)\n", rgelt.cfFormat);
849 IEnumFORMATETC_Release(penumFormatetc);
851 break;
855 * WM_DESTROYCLIPBOARD
856 * This is sent by EmptyClipboard before the clipboard is emptied.
857 * We should release any IDataObject we are holding onto when we receive
858 * this message, since it indicates that the OLE clipboard should be empty
859 * from this point on.
861 case WM_DESTROYCLIPBOARD:
863 TRACE("(): WM_DESTROYCLIPBOARD\n");
865 * Release the data object we are holding on to
867 if ( theOleClipboard->pIDataObjectSrc )
869 IDataObject_Release(theOleClipboard->pIDataObjectSrc);
870 theOleClipboard->pIDataObjectSrc = NULL;
872 break;
876 case WM_ASKCBFORMATNAME:
877 case WM_CHANGECBCHAIN:
878 case WM_DRAWCLIPBOARD:
879 case WM_SIZECLIPBOARD:
880 case WM_HSCROLLCLIPBOARD:
881 case WM_VSCROLLCLIPBOARD:
882 case WM_PAINTCLIPBOARD:
884 default:
885 return DefWindowProcA(hWnd, message, wParam, lParam);
888 return 0;
892 /***********************************************************************
893 * OLEClipbrd_RenderFormat(LPFORMATETC)
894 * Render the clipboard data. Note that this call will delegate to the
895 * source data object.
896 * Note: This function assumes it is passed an HGLOBAL format to render.
898 static HRESULT OLEClipbrd_RenderFormat(IDataObject *pIDataObject, LPFORMATETC pFormatetc)
900 STGMEDIUM medium;
901 HGLOBAL hDup;
902 HRESULT hr = S_OK;
904 if ( FAILED(hr = IDataObject_GetData(pIDataObject, pFormatetc, &medium)) )
906 WARN("() : IDataObject_GetData failed to render clipboard data! (%lx)\n", hr);
907 return hr;
911 * Put a copy of the rendered data back on the clipboard
914 if ( !(hDup = OLEClipbrd_GlobalDupMem(medium.u.hGlobal)) )
915 HANDLE_ERROR( E_OUTOFMEMORY );
917 if ( !SetClipboardData( pFormatetc->cfFormat, hDup ) )
919 GlobalFree(hDup);
920 WARN("() : Failed to set rendered clipboard data into clipboard!\n");
923 CLEANUP:
925 ReleaseStgMedium(&medium);
927 return hr;
931 /***********************************************************************
932 * OLEClipbrd_GlobalDupMem( HGLOBAL )
933 * Helper method to duplicate an HGLOBAL chunk of memory
935 static HGLOBAL OLEClipbrd_GlobalDupMem( HGLOBAL hGlobalSrc )
937 HGLOBAL hGlobalDest;
938 PVOID pGlobalSrc, pGlobalDest;
939 DWORD cBytes;
941 if ( !hGlobalSrc )
942 return 0;
944 cBytes = GlobalSize(hGlobalSrc);
945 if ( 0 == cBytes )
946 return 0;
948 hGlobalDest = GlobalAlloc( GMEM_DDESHARE|GMEM_MOVEABLE,
949 cBytes );
950 if ( !hGlobalDest )
951 return 0;
953 pGlobalSrc = GlobalLock(hGlobalSrc);
954 pGlobalDest = GlobalLock(hGlobalDest);
955 if ( !pGlobalSrc || !pGlobalDest )
956 return 0;
958 memcpy(pGlobalDest, pGlobalSrc, cBytes);
960 GlobalUnlock(hGlobalSrc);
961 GlobalUnlock(hGlobalDest);
963 return hGlobalDest;
967 /*---------------------------------------------------------------------*
968 * Implementation of the internal IDataObject interface exposed by
969 * the OLE clipboard.
970 *---------------------------------------------------------------------*/
973 /************************************************************************
974 * OLEClipbrd_IDataObject_QueryInterface (IUnknown)
976 * See Windows documentation for more details on IUnknown methods.
978 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryInterface(
979 IDataObject* iface,
980 REFIID riid,
981 void** ppvObject)
984 * Declare "This" pointer
986 ICOM_THIS(OLEClipbrd, iface);
987 char xriid[50];
989 WINE_StringFromCLSID((LPCLSID)riid,xriid);
990 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObject);
993 * Perform a sanity check on the parameters.
995 if ( (This==0) || (ppvObject==0) )
996 return E_INVALIDARG;
999 * Initialize the return parameter.
1001 *ppvObject = 0;
1004 * Compare the riid with the interface IDs implemented by this object.
1006 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
1008 *ppvObject = iface;
1010 else if (memcmp(&IID_IDataObject, riid, sizeof(IID_IDataObject)) == 0)
1012 *ppvObject = (IDataObject*)&(This->lpvtbl1);
1014 else /* We only support IUnknown and IDataObject */
1016 char clsid[50];
1018 WINE_StringFromCLSID((LPCLSID)riid,clsid);
1019 WARN( "() : asking for unsupported interface %s\n", clsid);
1021 return E_NOINTERFACE;
1025 * Query Interface always increases the reference count by one when it is
1026 * successful.
1028 IUnknown_AddRef((IUnknown*)*ppvObject);
1030 return S_OK;
1033 /************************************************************************
1034 * OLEClipbrd_IDataObject_AddRef (IUnknown)
1036 * See Windows documentation for more details on IUnknown methods.
1038 static ULONG WINAPI OLEClipbrd_IDataObject_AddRef(
1039 IDataObject* iface)
1042 * Declare "This" pointer
1044 ICOM_THIS(OLEClipbrd, iface);
1046 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1048 This->ref++;
1050 return This->ref;
1053 /************************************************************************
1054 * OLEClipbrd_IDataObject_Release (IUnknown)
1056 * See Windows documentation for more details on IUnknown methods.
1058 static ULONG WINAPI OLEClipbrd_IDataObject_Release(
1059 IDataObject* iface)
1062 * Declare "This" pointer
1064 ICOM_THIS(OLEClipbrd, iface);
1066 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1069 * Decrease the reference count on this object.
1071 This->ref--;
1074 * If the reference count goes down to 0, perform suicide.
1076 if (This->ref==0)
1078 OLEClipbrd_Destroy(This);
1081 return This->ref;
1085 /************************************************************************
1086 * OLEClipbrd_IDataObject_GetData (IDataObject)
1088 * The OLE Clipboard's implementation of this method delegates to
1089 * a data source if there is one or wraps around the windows clipboard
1091 * See Windows documentation for more details on IDataObject methods.
1093 static HRESULT WINAPI OLEClipbrd_IDataObject_GetData(
1094 IDataObject* iface,
1095 LPFORMATETC pformatetcIn,
1096 STGMEDIUM* pmedium)
1098 HANDLE hData = 0;
1099 BOOL bClipboardOpen = FALSE;
1100 HRESULT hr = S_OK;
1103 * Declare "This" pointer
1105 ICOM_THIS(OLEClipbrd, iface);
1107 if ( !pformatetcIn || !pformatetcIn || !pmedium )
1108 return E_INVALIDARG;
1110 TRACE("(%p, %p)\n", iface, pformatetcIn);
1113 * If we have a data source placed on the clipboard (via OleSetClipboard)
1114 * simply delegate to the source object's QueryGetData
1115 * NOTE: This code assumes that the IDataObject is in the same address space!
1116 * We will need to add marshalling support when Wine handles multiple processes.
1118 if ( This->pIDataObjectSrc )
1120 return IDataObject_GetData(This->pIDataObjectSrc, pformatetcIn, pmedium);
1123 if ( pformatetcIn->lindex != -1 )
1124 return DV_E_LINDEX;
1125 if ( (pformatetcIn->tymed & TYMED_HGLOBAL) != TYMED_HGLOBAL )
1126 return DV_E_TYMED;
1128 if ( pformatetcIn->dwAspect != DVASPECT_CONTENT )
1129 return DV_E_DVASPECT;
1133 * Otherwise, get the data from the windows clipboard using GetClipboardData
1135 if ( !(bClipboardOpen = OpenClipboard(theOleClipboard->hWndClipboard)) )
1136 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
1138 hData = GetClipboardData(pformatetcIn->cfFormat);
1141 * Return the clipboard data in the storage medium structure
1143 pmedium->tymed = (hData == 0) ? TYMED_NULL : TYMED_HGLOBAL;
1144 pmedium->u.hGlobal = (HGLOBAL)hData;
1145 pmedium->pUnkForRelease = NULL;
1147 hr = S_OK;
1149 CLEANUP:
1151 * Close Windows clipboard
1153 if ( bClipboardOpen && !CloseClipboard() )
1154 hr = CLIPBRD_E_CANT_CLOSE;
1156 if ( FAILED(hr) )
1157 return hr;
1158 return (hData == 0) ? DV_E_FORMATETC : S_OK;
1161 static HRESULT WINAPI OLEClipbrd_IDataObject_GetDataHere(
1162 IDataObject* iface,
1163 LPFORMATETC pformatetc,
1164 STGMEDIUM* pmedium)
1166 FIXME(": Stub\n");
1167 return E_NOTIMPL;
1170 /************************************************************************
1171 * OLEClipbrd_IDataObject_QueryGetData (IDataObject)
1173 * The OLE Clipboard's implementation of this method delegates to
1174 * a data source if there is one or wraps around the windows clipboard
1175 * function IsClipboardFormatAvailable() otherwise.
1177 * See Windows documentation for more details on IDataObject methods.
1179 static HRESULT WINAPI OLEClipbrd_IDataObject_QueryGetData(
1180 IDataObject* iface,
1181 LPFORMATETC pformatetc)
1184 * Declare "This" pointer
1186 ICOM_THIS(OLEClipbrd, iface);
1188 TRACE("(%p, %p)\n", iface, pformatetc);
1191 * If we have a data source placed on the clipboard (via OleSetClipboard)
1192 * simply delegate to the source object's QueryGetData
1194 if ( This->pIDataObjectSrc )
1196 return IDataObject_QueryGetData(This->pIDataObjectSrc, pformatetc);
1199 if (!pformatetc)
1200 return E_INVALIDARG;
1202 if ( pformatetc->dwAspect != DVASPECT_CONTENT )
1203 return DV_E_DVASPECT;
1205 if ( pformatetc->lindex != -1 )
1206 return DV_E_LINDEX;
1208 /* TODO: Handle TYMED_IStorage media which were put on the clipboard
1209 * by copying the storage into global memory. We must convert this
1210 * TYMED_HGLOBAL back to TYMED_IStorage.
1212 if ( pformatetc->tymed != TYMED_HGLOBAL )
1213 return DV_E_TYMED;
1216 * Delegate to the Windows clipboard function IsClipboardFormatAvailable
1218 return (IsClipboardFormatAvailable(pformatetc->cfFormat)) ? S_OK : DV_E_FORMATETC;
1221 /************************************************************************
1222 * OLEClipbrd_IDataObject_GetCanonicalFormatEtc (IDataObject)
1224 * See Windows documentation for more details on IDataObject methods.
1226 static HRESULT WINAPI OLEClipbrd_IDataObject_GetCanonicalFormatEtc(
1227 IDataObject* iface,
1228 LPFORMATETC pformatectIn,
1229 LPFORMATETC pformatetcOut)
1231 TRACE("(%p, %p, %p)\n", iface, pformatectIn, pformatetcOut);
1233 if ( !pformatectIn || !pformatetcOut )
1234 return E_INVALIDARG;
1236 memcpy(pformatetcOut, pformatectIn, sizeof(FORMATETC));
1237 return DATA_S_SAMEFORMATETC;
1240 /************************************************************************
1241 * OLEClipbrd_IDataObject_SetData (IDataObject)
1243 * The OLE Clipboard's does not implement this method
1245 * See Windows documentation for more details on IDataObject methods.
1247 static HRESULT WINAPI OLEClipbrd_IDataObject_SetData(
1248 IDataObject* iface,
1249 LPFORMATETC pformatetc,
1250 STGMEDIUM* pmedium,
1251 BOOL fRelease)
1253 return E_NOTIMPL;
1256 /************************************************************************
1257 * OLEClipbrd_IDataObject_EnumFormatEtc (IDataObject)
1259 * See Windows documentation for more details on IDataObject methods.
1261 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumFormatEtc(
1262 IDataObject* iface,
1263 DWORD dwDirection,
1264 IEnumFORMATETC** ppenumFormatEtc)
1266 HRESULT hr = S_OK;
1267 FORMATETC *afmt = NULL;
1268 int cfmt, i;
1269 UINT format;
1270 BOOL bClipboardOpen;
1273 * Declare "This" pointer
1275 ICOM_THIS(OLEClipbrd, iface);
1277 TRACE("(%p, %lx, %p)\n", iface, dwDirection, ppenumFormatEtc);
1280 * If we have a data source placed on the clipboard (via OleSetClipboard)
1281 * simply delegate to the source object's EnumFormatEtc
1283 if ( This->pIDataObjectSrc )
1285 return IDataObject_EnumFormatEtc(This->pIDataObjectSrc,
1286 dwDirection, ppenumFormatEtc);
1290 * Otherwise we must provide our own enumerator which wraps around the
1291 * Windows clipboard function EnumClipboardFormats
1293 if ( !ppenumFormatEtc )
1294 return E_INVALIDARG;
1296 if ( dwDirection != DATADIR_GET ) /* IDataObject_SetData not implemented */
1297 return E_NOTIMPL;
1300 * Store all current clipboard formats in an array of FORMATETC's,
1301 * and create an IEnumFORMATETC enumerator from this list.
1303 cfmt = CountClipboardFormats();
1304 afmt = (FORMATETC *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1305 sizeof(FORMATETC) * cfmt);
1307 * Open the Windows clipboard, associating it with our hidden window
1309 if ( !(bClipboardOpen = OpenClipboard(This->hWndClipboard)) )
1310 HANDLE_ERROR( CLIPBRD_E_CANT_OPEN );
1313 * Store all current clipboard formats in an array of FORMATETC's
1314 * TODO: Handle TYMED_IStorage media which were put on the clipboard
1315 * by copying the storage into global memory. We must convert this
1316 * TYMED_HGLOBAL back to TYMED_IStorage.
1318 for (i = 0, format = 0; i < cfmt; i++)
1320 format = EnumClipboardFormats(format);
1321 if (!format) /* Failed! */
1323 ERR("EnumClipboardFormats failed to return format!\n");
1324 HANDLE_ERROR( E_FAIL );
1327 /* Init the FORMATETC struct */
1328 afmt[i].cfFormat = format;
1329 afmt[i].ptd = NULL;
1330 afmt[i].dwAspect = DVASPECT_CONTENT;
1331 afmt[i].lindex = -1;
1332 afmt[i].tymed = TYMED_HGLOBAL;
1336 * Create an EnumFORMATETC enumerator and return an
1337 * EnumFORMATETC after bumping up its ref count
1339 *ppenumFormatEtc = OLEClipbrd_IEnumFORMATETC_Construct( cfmt, afmt, (LPUNKNOWN)iface);
1340 if (!(*ppenumFormatEtc))
1341 HANDLE_ERROR( E_OUTOFMEMORY );
1343 if (FAILED( hr = IEnumFORMATETC_AddRef(*ppenumFormatEtc)))
1344 HANDLE_ERROR( hr );
1346 hr = S_OK;
1348 CLEANUP:
1350 * Free the array of FORMATETC's
1352 if (afmt)
1353 HeapFree(GetProcessHeap(), 0, afmt);
1356 * Close Windows clipboard
1358 if ( bClipboardOpen && !CloseClipboard() )
1359 hr = CLIPBRD_E_CANT_CLOSE;
1361 return hr;
1364 /************************************************************************
1365 * OLEClipbrd_IDataObject_DAdvise (IDataObject)
1367 * The OLE Clipboard's does not implement this method
1369 * See Windows documentation for more details on IDataObject methods.
1371 static HRESULT WINAPI OLEClipbrd_IDataObject_DAdvise(
1372 IDataObject* iface,
1373 FORMATETC* pformatetc,
1374 DWORD advf,
1375 IAdviseSink* pAdvSink,
1376 DWORD* pdwConnection)
1378 return E_NOTIMPL;
1381 /************************************************************************
1382 * OLEClipbrd_IDataObject_DUnadvise (IDataObject)
1384 * The OLE Clipboard's does not implement this method
1386 * See Windows documentation for more details on IDataObject methods.
1388 static HRESULT WINAPI OLEClipbrd_IDataObject_DUnadvise(
1389 IDataObject* iface,
1390 DWORD dwConnection)
1392 return E_NOTIMPL;
1395 /************************************************************************
1396 * OLEClipbrd_IDataObject_EnumDAdvise (IDataObject)
1398 * The OLE Clipboard does not implement this method
1400 * See Windows documentation for more details on IDataObject methods.
1402 static HRESULT WINAPI OLEClipbrd_IDataObject_EnumDAdvise(
1403 IDataObject* iface,
1404 IEnumSTATDATA** ppenumAdvise)
1406 return E_NOTIMPL;
1410 /*---------------------------------------------------------------------*
1411 * Implementation of the internal IEnumFORMATETC interface returned by
1412 * the OLE clipboard's IDataObject.
1413 *---------------------------------------------------------------------*/
1415 /************************************************************************
1416 * OLEClipbrd_IEnumFORMATETC_Construct (UINT, const FORMATETC, LPUNKNOWN)
1418 * Creates an IEnumFORMATETC enumerator from an array of FORMATETC
1419 * Structures. pUnkOuter is the outer unknown for reference counting only.
1420 * NOTE: this does not AddRef the interface.
1423 LPENUMFORMATETC OLEClipbrd_IEnumFORMATETC_Construct(UINT cfmt, const FORMATETC afmt[],
1424 LPUNKNOWN pUnkDataObj)
1426 IEnumFORMATETCImpl* ef;
1427 DWORD size=cfmt * sizeof(FORMATETC);
1428 LPMALLOC pIMalloc;
1430 ef = (IEnumFORMATETCImpl*)HeapAlloc(GetProcessHeap(),
1431 HEAP_ZERO_MEMORY,
1432 sizeof(IEnumFORMATETCImpl));
1433 if (!ef)
1434 return NULL;
1436 ef->ref = 0;
1437 ICOM_VTBL(ef) = &efvt;
1438 ef->pUnkDataObj = pUnkDataObj;
1440 ef->posFmt = 0;
1441 ef->countFmt = cfmt;
1442 if (FAILED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
1443 return NULL;
1444 ef->pFmt = (LPFORMATETC)IMalloc_Alloc(pIMalloc, size);
1445 IMalloc_Release(pIMalloc);
1447 if (ef->pFmt)
1448 memcpy(ef->pFmt, afmt, size);
1450 TRACE("(%p)->()\n",ef);
1451 return (LPENUMFORMATETC)ef;
1455 /************************************************************************
1456 * OLEClipbrd_IEnumFORMATETC_QueryInterface (IUnknown)
1458 * See Windows documentation for more details on IUnknown methods.
1460 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_QueryInterface
1461 (LPENUMFORMATETC iface, REFIID riid, LPVOID* ppvObj)
1463 ICOM_THIS(IEnumFORMATETCImpl,iface);
1464 char xriid[50];
1466 WINE_StringFromCLSID((LPCLSID)riid,xriid);
1467 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
1470 * Since enumerators are seperate objects from the parent data object
1471 * we only need to support the IUnknown and IEnumFORMATETC interfaces
1474 *ppvObj = NULL;
1476 if(IsEqualIID(riid, &IID_IUnknown))
1478 *ppvObj = This;
1480 else if(IsEqualIID(riid, &IID_IEnumFORMATETC))
1482 *ppvObj = (IDataObject*)This;
1485 if(*ppvObj)
1487 IEnumFORMATETC_AddRef((IEnumFORMATETC*)*ppvObj);
1488 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
1489 return S_OK;
1492 TRACE("-- Interface: E_NOINTERFACE\n");
1493 return E_NOINTERFACE;
1496 /************************************************************************
1497 * OLEClipbrd_IEnumFORMATETC_AddRef (IUnknown)
1499 * Since enumerating formats only makes sense when our data object is around,
1500 * we insure that it stays as long as we stay by calling our parents IUnknown
1501 * for AddRef and Release. But since we are not controlled by the lifetime of
1502 * the outer object, we still keep our own reference count in order to
1503 * free ourselves.
1505 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_AddRef(LPENUMFORMATETC iface)
1507 ICOM_THIS(IEnumFORMATETCImpl,iface);
1508 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1510 if (This->pUnkDataObj)
1511 IUnknown_AddRef(This->pUnkDataObj);
1513 return ++(This->ref);
1516 /************************************************************************
1517 * OLEClipbrd_IEnumFORMATETC_Release (IUnknown)
1519 * See Windows documentation for more details on IUnknown methods.
1521 static ULONG WINAPI OLEClipbrd_IEnumFORMATETC_Release(LPENUMFORMATETC iface)
1523 ICOM_THIS(IEnumFORMATETCImpl,iface);
1524 LPMALLOC pIMalloc;
1526 TRACE("(%p)->(count=%lu)\n",This, This->ref);
1528 if (This->pUnkDataObj)
1529 IUnknown_Release(This->pUnkDataObj); /* Release parent data object */
1531 if (!--(This->ref))
1533 TRACE("() - destroying IEnumFORMATETC(%p)\n",This);
1534 if (SUCCEEDED(CoGetMalloc(MEMCTX_TASK, &pIMalloc)))
1536 IMalloc_Free(pIMalloc, This->pFmt);
1537 IMalloc_Release(pIMalloc);
1540 HeapFree(GetProcessHeap(),0,This);
1541 return 0;
1544 return This->ref;
1547 /************************************************************************
1548 * OLEClipbrd_IEnumFORMATETC_Next (IEnumFORMATETC)
1550 * Standard enumerator members for IEnumFORMATETC
1552 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Next
1553 (LPENUMFORMATETC iface, ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
1555 ICOM_THIS(IEnumFORMATETCImpl,iface);
1556 UINT cfetch;
1557 HRESULT hres = S_FALSE;
1559 TRACE("(%p)->(pos=%u)\n", This, This->posFmt);
1561 if (This->posFmt < This->countFmt)
1563 cfetch = This->countFmt - This->posFmt;
1564 if (cfetch >= celt)
1566 cfetch = celt;
1567 hres = S_OK;
1570 memcpy(rgelt, &This->pFmt[This->posFmt], cfetch * sizeof(FORMATETC));
1571 This->posFmt += cfetch;
1573 else
1575 cfetch = 0;
1578 if (pceltFethed)
1580 *pceltFethed = cfetch;
1583 return hres;
1586 /************************************************************************
1587 * OLEClipbrd_IEnumFORMATETC_Skip (IEnumFORMATETC)
1589 * Standard enumerator members for IEnumFORMATETC
1591 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Skip(LPENUMFORMATETC iface, ULONG celt)
1593 ICOM_THIS(IEnumFORMATETCImpl,iface);
1594 TRACE("(%p)->(num=%lu)\n", This, celt);
1596 This->posFmt += celt;
1597 if (This->posFmt > This->countFmt)
1599 This->posFmt = This->countFmt;
1600 return S_FALSE;
1602 return S_OK;
1605 /************************************************************************
1606 * OLEClipbrd_IEnumFORMATETC_Reset (IEnumFORMATETC)
1608 * Standard enumerator members for IEnumFORMATETC
1610 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Reset(LPENUMFORMATETC iface)
1612 ICOM_THIS(IEnumFORMATETCImpl,iface);
1613 TRACE("(%p)->()\n", This);
1615 This->posFmt = 0;
1616 return S_OK;
1619 /************************************************************************
1620 * OLEClipbrd_IEnumFORMATETC_Clone (IEnumFORMATETC)
1622 * Standard enumerator members for IEnumFORMATETC
1624 static HRESULT WINAPI OLEClipbrd_IEnumFORMATETC_Clone
1625 (LPENUMFORMATETC iface, LPENUMFORMATETC* ppenum)
1627 ICOM_THIS(IEnumFORMATETCImpl,iface);
1628 HRESULT hr = S_OK;
1630 TRACE("(%p)->(ppenum=%p)\n", This, ppenum);
1632 if ( !ppenum )
1633 return E_INVALIDARG;
1635 *ppenum = OLEClipbrd_IEnumFORMATETC_Construct(This->countFmt,
1636 This->pFmt,
1637 This->pUnkDataObj);
1639 if (FAILED( hr = IEnumFORMATETC_AddRef(*ppenum)))
1640 return ( hr );
1642 return (*ppenum) ? S_OK : E_OUTOFMEMORY;