4 * Copyright 1999 Francis Beaudet
5 * Copyright 2000 Abey George
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * The OLE2 data cache supports a whole whack of
23 * interfaces including:
24 * IDataObject, IPersistStorage, IViewObject2,
25 * IOleCache2 and IOleCacheControl.
27 * Most of the implementation details are taken from: Inside OLE
28 * second edition by Kraig Brockschmidt,
31 * - This implementation of the datacache will let your application
32 * load documents that have embedded OLE objects in them and it will
33 * also retrieve the metafile representation of those objects.
34 * - This implementation of the datacache will also allow your
35 * application to save new documents with OLE objects in them.
36 * - The main thing that it doesn't do is allow you to activate
37 * or modify the OLE objects in any way.
38 * - I haven't found any good documentation on the real usage of
39 * the streams created by the data cache. In particular, How to
40 * determine what the XXX stands for in the stream name
41 * "\002OlePresXXX". It appears to just be a counter.
42 * - Also, I don't know the real content of the presentation stream
43 * header. I was able to figure-out where the extent of the object
44 * was stored and the aspect, but that's about it.
50 #define NONAMELESSUNION
51 #define NONAMELESSSTRUCT
58 #include "wine/unicode.h"
60 #include "wine/list.h"
61 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(ole);
65 /****************************************************************************
66 * PresentationDataHeader
68 * This structure represents the header of the \002OlePresXXX stream in
69 * the OLE object storage.
71 typedef struct PresentationDataHeader
74 * - standard clipformat:
75 * DWORD length = 0xffffffff;
77 * - or custom clipformat:
79 * CHAR format_name[length]; (null-terminated)
81 DWORD unknown3; /* 4, possibly TYMED_ISTREAM */
85 DWORD unknown7; /* 0 */
86 DWORD dwObjectExtentX;
87 DWORD dwObjectExtentY;
89 } PresentationDataHeader;
91 typedef struct DataCacheEntry
94 /* format of this entry */
96 /* the clipboard format of the data */
101 * This storage pointer is set through a call to
102 * IPersistStorage_Load. This is where the visual
103 * representation of the object is stored.
110 /* stream number (-1 if not set ) */
111 unsigned short stream_number;
114 /****************************************************************************
120 * List all interface VTables here
122 const IDataObjectVtbl* lpVtbl;
123 const IUnknownVtbl* lpvtblNDIUnknown;
124 const IPersistStorageVtbl* lpvtblIPersistStorage;
125 const IViewObject2Vtbl* lpvtblIViewObject;
126 const IOleCache2Vtbl* lpvtblIOleCache2;
127 const IOleCacheControlVtbl* lpvtblIOleCacheControl;
130 * Reference count of this object
135 * IUnknown implementation of the outer object.
137 IUnknown* outerUnknown;
140 * The user of this object can setup ONE advise sink
141 * connection with the object. These parameters describe
145 DWORD sinkAdviseFlag;
146 IAdviseSink* sinkInterface;
147 IStorage *presentationStorage;
149 /* list of cache entries */
150 struct list cache_list;
151 /* last id assigned to an entry */
157 typedef struct DataCache DataCache;
160 * Here, I define utility macros to help with the casting of the
162 * There is a version to accommodate all of the VTables implemented
166 static inline DataCache *impl_from_IDataObject( IDataObject *iface )
168 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpVtbl));
171 static inline DataCache *impl_from_NDIUnknown( IUnknown *iface )
173 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblNDIUnknown));
176 static inline DataCache *impl_from_IPersistStorage( IPersistStorage *iface )
178 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIPersistStorage));
181 static inline DataCache *impl_from_IViewObject2( IViewObject2 *iface )
183 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIViewObject));
186 static inline DataCache *impl_from_IOleCache2( IOleCache2 *iface )
188 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCache2));
191 static inline DataCache *impl_from_IOleCacheControl( IOleCacheControl *iface )
193 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCacheControl));
196 static const char * debugstr_formatetc(const FORMATETC *formatetc)
198 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
199 formatetc->cfFormat, formatetc->ptd, formatetc->dwAspect,
200 formatetc->lindex, formatetc->tymed);
204 * Prototypes for the methods of the DataCache class.
206 static DataCache* DataCache_Construct(REFCLSID clsid,
207 LPUNKNOWN pUnkOuter);
208 static HRESULT DataCacheEntry_OpenPresStream(DataCacheEntry *This,
211 static void DataCacheEntry_Destroy(DataCacheEntry *This)
213 list_remove(&This->entry);
215 IStorage_Release(This->storage);
216 HeapFree(GetProcessHeap(), 0, This->fmtetc.ptd);
217 ReleaseStgMedium(&This->stgmedium);
218 HeapFree(GetProcessHeap(), 0, This);
221 static void DataCache_Destroy(
222 DataCache* ptrToDestroy)
224 DataCacheEntry *cache_entry, *next_cache_entry;
228 if (ptrToDestroy->sinkInterface != NULL)
230 IAdviseSink_Release(ptrToDestroy->sinkInterface);
231 ptrToDestroy->sinkInterface = NULL;
234 LIST_FOR_EACH_ENTRY_SAFE(cache_entry, next_cache_entry, &ptrToDestroy->cache_list, DataCacheEntry, entry)
235 DataCacheEntry_Destroy(cache_entry);
238 * Free the datacache pointer.
240 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
243 static DataCacheEntry *DataCache_GetEntryForFormatEtc(DataCache *This, const FORMATETC *formatetc)
245 DataCacheEntry *cache_entry;
246 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
248 /* FIXME: also compare DVTARGETDEVICEs */
249 if ((!cache_entry->fmtetc.cfFormat || !formatetc->cfFormat || (formatetc->cfFormat == cache_entry->fmtetc.cfFormat)) &&
250 (formatetc->dwAspect == cache_entry->fmtetc.dwAspect) &&
251 (formatetc->lindex == cache_entry->fmtetc.lindex) &&
252 (!cache_entry->fmtetc.tymed || !formatetc->tymed || (formatetc->tymed == cache_entry->fmtetc.tymed)))
258 /* checks that the clipformat and tymed are valid and returns an error if they
259 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
261 static HRESULT check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat, DWORD tymed)
263 if (!cfFormat || !tymed ||
264 (cfFormat == CF_METAFILEPICT && tymed == TYMED_MFPICT) ||
265 (cfFormat == CF_BITMAP && tymed == TYMED_GDI) ||
266 (cfFormat == CF_DIB && tymed == TYMED_HGLOBAL) ||
267 (cfFormat == CF_ENHMETAFILE && tymed == TYMED_ENHMF))
269 else if (tymed == TYMED_HGLOBAL)
270 return CACHE_S_FORMATETC_NOTSUPPORTED;
273 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat, tymed);
278 static HRESULT DataCache_CreateEntry(DataCache *This, const FORMATETC *formatetc, DataCacheEntry **cache_entry)
282 hr = check_valid_clipformat_and_tymed(formatetc->cfFormat, formatetc->tymed);
285 if (hr == CACHE_S_FORMATETC_NOTSUPPORTED)
286 TRACE("creating unsupported format %d\n", formatetc->cfFormat);
288 *cache_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry));
290 return E_OUTOFMEMORY;
292 (*cache_entry)->fmtetc = *formatetc;
295 (*cache_entry)->fmtetc.ptd = HeapAlloc(GetProcessHeap(), 0, formatetc->ptd->tdSize);
296 memcpy((*cache_entry)->fmtetc.ptd, formatetc->ptd, formatetc->ptd->tdSize);
298 (*cache_entry)->data_cf = 0;
299 (*cache_entry)->stgmedium.tymed = TYMED_NULL;
300 (*cache_entry)->stgmedium.pUnkForRelease = NULL;
301 (*cache_entry)->storage = NULL;
302 (*cache_entry)->id = This->last_cache_id++;
303 (*cache_entry)->dirty = TRUE;
304 (*cache_entry)->stream_number = -1;
305 list_add_tail(&This->cache_list, &(*cache_entry)->entry);
309 /************************************************************************
310 * DataCache_FireOnViewChange
312 * This method will fire an OnViewChange notification to the advise
313 * sink registered with the datacache.
315 * See IAdviseSink::OnViewChange for more details.
317 static void DataCache_FireOnViewChange(
322 TRACE("(%p, %x, %d)\n", this, aspect, lindex);
325 * The sink supplies a filter when it registers
326 * we make sure we only send the notifications when that
329 if ((this->sinkAspects & aspect) != 0)
331 if (this->sinkInterface != NULL)
333 IAdviseSink_OnViewChange(this->sinkInterface,
338 * Some sinks want to be unregistered automatically when
339 * the first notification goes out.
341 if ( (this->sinkAdviseFlag & ADVF_ONLYONCE) != 0)
343 IAdviseSink_Release(this->sinkInterface);
345 this->sinkInterface = NULL;
346 this->sinkAspects = 0;
347 this->sinkAdviseFlag = 0;
353 /* Helper for DataCacheEntry_OpenPresStream */
354 static BOOL DataCache_IsPresentationStream(const STATSTG *elem)
356 /* The presentation streams have names of the form "\002OlePresXXX",
357 * where XXX goes from 000 to 999. */
358 static const WCHAR OlePres[] = { 2,'O','l','e','P','r','e','s' };
360 LPCWSTR name = elem->pwcsName;
362 return (elem->type == STGTY_STREAM)
363 && (strlenW(name) == 11)
364 && (strncmpW(name, OlePres, 8) == 0)
365 && (name[8] >= '0') && (name[8] <= '9')
366 && (name[9] >= '0') && (name[9] <= '9')
367 && (name[10] >= '0') && (name[10] <= '9');
370 static HRESULT read_clipformat(IStream *stream, CLIPFORMAT *clipformat)
378 hr = IStream_Read(stream, &length, sizeof(length), &read);
379 if (hr != S_OK || read != sizeof(length))
380 return DV_E_CLIPFORMAT;
384 hr = IStream_Read(stream, &cf, sizeof(cf), 0);
385 if (hr != S_OK || read != sizeof(cf))
386 return DV_E_CLIPFORMAT;
391 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
393 return E_OUTOFMEMORY;
394 hr = IStream_Read(stream, format_name, length, &read);
395 if (hr != S_OK || read != length || format_name[length - 1] != '\0')
397 HeapFree(GetProcessHeap(), 0, format_name);
398 return DV_E_CLIPFORMAT;
400 *clipformat = RegisterClipboardFormatA(format_name);
401 HeapFree(GetProcessHeap(), 0, format_name);
406 static HRESULT write_clipformat(IStream *stream, CLIPFORMAT clipformat)
411 if (clipformat < 0xc000)
414 length = GetClipboardFormatNameA(clipformat, NULL, 0);
415 hr = IStream_Write(stream, &length, sizeof(length), NULL);
418 if (clipformat < 0xc000)
420 DWORD cf = clipformat;
421 hr = IStream_Write(stream, &cf, sizeof(cf), NULL);
425 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
427 return E_OUTOFMEMORY;
428 GetClipboardFormatNameA(clipformat, format_name, length);
429 hr = IStream_Write(stream, format_name, length, NULL);
430 HeapFree(GetProcessHeap(), 0, format_name);
435 /************************************************************************
436 * DataCacheEntry_OpenPresStream
438 * This method will find the stream for the given presentation. It makes
439 * no attempt at fallback.
442 * this - Pointer to the DataCache object
443 * drawAspect - The aspect of the object that we wish to draw.
444 * pStm - A returned stream. It points to the beginning of the
445 * - presentation data, including the header.
448 * S_OK The requested stream has been opened.
449 * OLE_E_BLANK The requested stream could not be found.
450 * Quite a few others I'm too lazy to map correctly.
453 * Algorithm: Scan the elements of the presentation storage, looking
454 * for presentation streams. For each presentation stream,
455 * load the header and check to see if the aspect matches.
457 * If a fallback is desired, just opening the first presentation stream
460 static HRESULT DataCacheEntry_OpenPresStream(
461 DataCacheEntry *This,
468 if (!ppStm) return E_POINTER;
470 hr = IStorage_EnumElements(This->storage, 0, NULL, 0, &pEnum);
471 if (FAILED(hr)) return hr;
473 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
475 if (DataCache_IsPresentationStream(&elem))
479 hr = IStorage_OpenStream(This->storage, elem.pwcsName,
480 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
484 PresentationDataHeader header;
486 CLIPFORMAT clipformat;
488 hr = read_clipformat(pStm, &clipformat);
491 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
493 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
494 if (hr == S_OK && actual_read == sizeof(header)
495 && header.dvAspect == This->fmtetc.dwAspect)
497 /* Rewind the stream before returning it. */
498 LARGE_INTEGER offset;
499 offset.u.LowPart = 0;
500 offset.u.HighPart = 0;
501 IStream_Seek(pStm, offset, STREAM_SEEK_SET, NULL);
505 CoTaskMemFree(elem.pwcsName);
506 IEnumSTATSTG_Release(pEnum);
511 IStream_Release(pStm);
515 CoTaskMemFree(elem.pwcsName);
518 IEnumSTATSTG_Release(pEnum);
520 return (hr == S_FALSE ? OLE_E_BLANK : hr);
523 /************************************************************************
524 * DataCacheEntry_LoadData
526 * This method will read information for the requested presentation
527 * into the given structure.
530 * This - The entry to load the data from.
533 * This method returns a metafile handle if it is successful.
534 * it will return 0 if not.
536 static HRESULT DataCacheEntry_LoadData(DataCacheEntry *This)
538 IStream* presStream = NULL;
540 ULARGE_INTEGER current_pos;
543 METAFILEPICT *mfpict;
545 PresentationDataHeader header;
546 CLIPFORMAT clipformat;
547 static const LARGE_INTEGER offset_zero;
550 * Open the presentation stream.
552 hres = DataCacheEntry_OpenPresStream(
560 * Get the size of the stream.
562 hres = IStream_Stat(presStream,
570 hres = read_clipformat(presStream, &clipformat);
573 IStream_Release(presStream);
580 sizeof(PresentationDataHeader),
584 IStream_Release(presStream);
588 hres = IStream_Seek(presStream, offset_zero, STREAM_SEEK_CUR, ¤t_pos);
590 streamInfo.cbSize.QuadPart -= current_pos.QuadPart;
592 hmfpict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
595 IStream_Release(presStream);
596 return E_OUTOFMEMORY;
598 mfpict = GlobalLock(hmfpict);
601 * Allocate a buffer for the metafile bits.
603 metafileBits = HeapAlloc(GetProcessHeap(),
605 streamInfo.cbSize.u.LowPart);
608 * Read the metafile bits.
613 streamInfo.cbSize.u.LowPart,
617 * Create a metafile with those bits.
621 /* FIXME: get this from the stream */
622 mfpict->mm = MM_ANISOTROPIC;
623 mfpict->xExt = header.dwObjectExtentX;
624 mfpict->yExt = header.dwObjectExtentY;
625 mfpict->hMF = SetMetaFileBitsEx(streamInfo.cbSize.u.LowPart, metafileBits);
630 GlobalUnlock(hmfpict);
633 This->data_cf = This->fmtetc.cfFormat;
634 This->stgmedium.tymed = TYMED_MFPICT;
635 This->stgmedium.u.hMetaFilePict = hmfpict;
643 HeapFree(GetProcessHeap(), 0, metafileBits);
644 IStream_Release(presStream);
649 static HRESULT DataCacheEntry_CreateStream(DataCacheEntry *This,
650 IStorage *storage, IStream **stream)
653 WCHAR wszName[] = {2,'O','l','e','P','r','e','s',
654 '0' + (This->stream_number / 100) % 10,
655 '0' + (This->stream_number / 10) % 10,
656 '0' + This->stream_number % 10, 0};
658 /* FIXME: cache the created stream in This? */
659 hr = IStorage_CreateStream(storage, wszName,
660 STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
665 static HRESULT DataCacheEntry_Save(DataCacheEntry *This, IStorage *storage,
668 PresentationDataHeader header;
670 IStream *pres_stream;
673 TRACE("stream_number = %d, fmtetc = %s\n", This->stream_number, debugstr_formatetc(&This->fmtetc));
675 hr = DataCacheEntry_CreateStream(This, storage, &pres_stream);
679 hr = write_clipformat(pres_stream, This->data_cf);
683 if (This->fmtetc.ptd)
684 FIXME("ptd not serialized\n");
686 header.dvAspect = This->fmtetc.dwAspect;
687 header.lindex = This->fmtetc.lindex;
688 header.tymed = This->stgmedium.tymed;
690 header.dwObjectExtentX = 0;
691 header.dwObjectExtentY = 0;
695 switch (This->data_cf)
697 case CF_METAFILEPICT:
699 if (This->stgmedium.tymed != TYMED_NULL)
701 const METAFILEPICT *mfpict = GlobalLock(This->stgmedium.u.hMetaFilePict);
704 IStream_Release(pres_stream);
705 return DV_E_STGMEDIUM;
707 header.dwObjectExtentX = mfpict->xExt;
708 header.dwObjectExtentY = mfpict->yExt;
709 header.dwSize = GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
710 GlobalUnlock(This->stgmedium.u.hMetaFilePict);
721 hr = IStream_Write(pres_stream, &header, sizeof(PresentationDataHeader),
725 IStream_Release(pres_stream);
730 switch (This->data_cf)
732 case CF_METAFILEPICT:
734 if (This->stgmedium.tymed != TYMED_NULL)
736 const METAFILEPICT *mfpict = GlobalLock(This->stgmedium.u.hMetaFilePict);
739 IStream_Release(pres_stream);
740 return DV_E_STGMEDIUM;
742 data = HeapAlloc(GetProcessHeap(), 0, header.dwSize);
743 GetMetaFileBitsEx(mfpict->hMF, header.dwSize, data);
744 GlobalUnlock(This->stgmedium.u.hMetaFilePict);
753 hr = IStream_Write(pres_stream, data, header.dwSize, NULL);
754 HeapFree(GetProcessHeap(), 0, data);
756 IStream_Release(pres_stream);
760 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
761 * does no checking of whether src_stgm has a supported tymed, so this should be
762 * done in the caller */
763 static HRESULT copy_stg_medium(CLIPFORMAT cf, STGMEDIUM *dest_stgm,
764 const STGMEDIUM *src_stgm)
766 if (src_stgm->tymed == TYMED_MFPICT)
768 const METAFILEPICT *src_mfpict = GlobalLock(src_stgm->u.hMetaFilePict);
769 METAFILEPICT *dest_mfpict;
772 return DV_E_STGMEDIUM;
773 dest_stgm->u.hMetaFilePict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
774 dest_mfpict = GlobalLock(dest_stgm->u.hMetaFilePict);
777 GlobalUnlock(src_stgm->u.hMetaFilePict);
778 return E_OUTOFMEMORY;
780 *dest_mfpict = *src_mfpict;
781 dest_mfpict->hMF = CopyMetaFileW(src_mfpict->hMF, NULL);
782 GlobalUnlock(src_stgm->u.hMetaFilePict);
783 GlobalUnlock(dest_stgm->u.hMetaFilePict);
785 else if (src_stgm->tymed != TYMED_NULL)
787 dest_stgm->u.hGlobal = OleDuplicateData(src_stgm->u.hGlobal, cf,
789 if (!dest_stgm->u.hGlobal)
790 return E_OUTOFMEMORY;
792 dest_stgm->tymed = src_stgm->tymed;
793 dest_stgm->pUnkForRelease = src_stgm->pUnkForRelease;
794 if (dest_stgm->pUnkForRelease)
795 IUnknown_AddRef(dest_stgm->pUnkForRelease);
799 static HRESULT DataCacheEntry_SetData(DataCacheEntry *This,
800 const FORMATETC *formatetc,
801 const STGMEDIUM *stgmedium,
804 if ((!This->fmtetc.cfFormat && !formatetc->cfFormat) ||
805 (This->fmtetc.tymed == TYMED_NULL && formatetc->tymed == TYMED_NULL) ||
806 stgmedium->tymed == TYMED_NULL)
808 WARN("invalid formatetc\n");
809 return DV_E_FORMATETC;
813 ReleaseStgMedium(&This->stgmedium);
814 This->data_cf = This->fmtetc.cfFormat ? This->fmtetc.cfFormat : formatetc->cfFormat;
817 This->stgmedium = *stgmedium;
821 return copy_stg_medium(This->data_cf,
822 &This->stgmedium, stgmedium);
825 static HRESULT DataCacheEntry_GetData(DataCacheEntry *This,
826 STGMEDIUM *stgmedium)
828 if (stgmedium->tymed == TYMED_NULL && This->storage)
830 HRESULT hr = DataCacheEntry_LoadData(This);
834 if (This->stgmedium.tymed == TYMED_NULL)
836 return copy_stg_medium(This->data_cf, stgmedium, &This->stgmedium);
839 static inline HRESULT DataCacheEntry_DiscardData(DataCacheEntry *This)
841 ReleaseStgMedium(&This->stgmedium);
842 This->data_cf = This->fmtetc.cfFormat;
846 static inline void DataCacheEntry_HandsOffStorage(DataCacheEntry *This)
850 IStorage_Release(This->storage);
851 This->storage = NULL;
855 /*********************************************************
856 * Method implementation for the non delegating IUnknown
857 * part of the DataCache class.
860 /************************************************************************
861 * DataCache_NDIUnknown_QueryInterface (IUnknown)
863 * See Windows documentation for more details on IUnknown methods.
865 * This version of QueryInterface will not delegate it's implementation
866 * to the outer unknown.
868 static HRESULT WINAPI DataCache_NDIUnknown_QueryInterface(
873 DataCache *this = impl_from_NDIUnknown(iface);
876 * Perform a sanity check on the parameters.
878 if ( (this==0) || (ppvObject==0) )
882 * Initialize the return parameter.
887 * Compare the riid with the interface IDs implemented by this object.
889 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
893 else if (memcmp(&IID_IDataObject, riid, sizeof(IID_IDataObject)) == 0)
895 *ppvObject = (IDataObject*)&(this->lpVtbl);
897 else if ( (memcmp(&IID_IPersistStorage, riid, sizeof(IID_IPersistStorage)) == 0) ||
898 (memcmp(&IID_IPersist, riid, sizeof(IID_IPersist)) == 0) )
900 *ppvObject = (IPersistStorage*)&(this->lpvtblIPersistStorage);
902 else if ( (memcmp(&IID_IViewObject, riid, sizeof(IID_IViewObject)) == 0) ||
903 (memcmp(&IID_IViewObject2, riid, sizeof(IID_IViewObject2)) == 0) )
905 *ppvObject = (IViewObject2*)&(this->lpvtblIViewObject);
907 else if ( (memcmp(&IID_IOleCache, riid, sizeof(IID_IOleCache)) == 0) ||
908 (memcmp(&IID_IOleCache2, riid, sizeof(IID_IOleCache2)) == 0) )
910 *ppvObject = (IOleCache2*)&(this->lpvtblIOleCache2);
912 else if (memcmp(&IID_IOleCacheControl, riid, sizeof(IID_IOleCacheControl)) == 0)
914 *ppvObject = (IOleCacheControl*)&(this->lpvtblIOleCacheControl);
918 * Check that we obtained an interface.
922 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
923 return E_NOINTERFACE;
927 * Query Interface always increases the reference count by one when it is
930 IUnknown_AddRef((IUnknown*)*ppvObject);
935 /************************************************************************
936 * DataCache_NDIUnknown_AddRef (IUnknown)
938 * See Windows documentation for more details on IUnknown methods.
940 * This version of QueryInterface will not delegate it's implementation
941 * to the outer unknown.
943 static ULONG WINAPI DataCache_NDIUnknown_AddRef(
946 DataCache *this = impl_from_NDIUnknown(iface);
947 return InterlockedIncrement(&this->ref);
950 /************************************************************************
951 * DataCache_NDIUnknown_Release (IUnknown)
953 * See Windows documentation for more details on IUnknown methods.
955 * This version of QueryInterface will not delegate it's implementation
956 * to the outer unknown.
958 static ULONG WINAPI DataCache_NDIUnknown_Release(
961 DataCache *this = impl_from_NDIUnknown(iface);
965 * Decrease the reference count on this object.
967 ref = InterlockedDecrement(&this->ref);
970 * If the reference count goes down to 0, perform suicide.
972 if (ref == 0) DataCache_Destroy(this);
977 /*********************************************************
978 * Method implementation for the IDataObject
979 * part of the DataCache class.
982 /************************************************************************
983 * DataCache_IDataObject_QueryInterface (IUnknown)
985 * See Windows documentation for more details on IUnknown methods.
987 static HRESULT WINAPI DataCache_IDataObject_QueryInterface(
992 DataCache *this = impl_from_IDataObject(iface);
994 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
997 /************************************************************************
998 * DataCache_IDataObject_AddRef (IUnknown)
1000 * See Windows documentation for more details on IUnknown methods.
1002 static ULONG WINAPI DataCache_IDataObject_AddRef(
1005 DataCache *this = impl_from_IDataObject(iface);
1007 return IUnknown_AddRef(this->outerUnknown);
1010 /************************************************************************
1011 * DataCache_IDataObject_Release (IUnknown)
1013 * See Windows documentation for more details on IUnknown methods.
1015 static ULONG WINAPI DataCache_IDataObject_Release(
1018 DataCache *this = impl_from_IDataObject(iface);
1020 return IUnknown_Release(this->outerUnknown);
1023 /************************************************************************
1026 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1027 * See Windows documentation for more details on GetData.
1029 static HRESULT WINAPI DataCache_GetData(
1031 LPFORMATETC pformatetcIn,
1034 DataCache *This = impl_from_IDataObject(iface);
1035 DataCacheEntry *cache_entry;
1037 memset(pmedium, 0, sizeof(*pmedium));
1039 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetcIn);
1043 return DataCacheEntry_GetData(cache_entry, pmedium);
1046 static HRESULT WINAPI DataCache_GetDataHere(
1048 LPFORMATETC pformatetc,
1055 static HRESULT WINAPI DataCache_QueryGetData(
1057 LPFORMATETC pformatetc)
1063 /************************************************************************
1064 * DataCache_EnumFormatEtc (IDataObject)
1066 * The data cache doesn't implement this method.
1068 * See Windows documentation for more details on IDataObject methods.
1070 static HRESULT WINAPI DataCache_GetCanonicalFormatEtc(
1072 LPFORMATETC pformatectIn,
1073 LPFORMATETC pformatetcOut)
1079 /************************************************************************
1080 * DataCache_IDataObject_SetData (IDataObject)
1082 * This method is delegated to the IOleCache2 implementation.
1084 * See Windows documentation for more details on IDataObject methods.
1086 static HRESULT WINAPI DataCache_IDataObject_SetData(
1088 LPFORMATETC pformatetc,
1092 IOleCache2* oleCache = NULL;
1095 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1097 hres = IDataObject_QueryInterface(iface, &IID_IOleCache2, (void**)&oleCache);
1100 return E_UNEXPECTED;
1102 hres = IOleCache2_SetData(oleCache, pformatetc, pmedium, fRelease);
1104 IOleCache2_Release(oleCache);
1109 /************************************************************************
1110 * DataCache_EnumFormatEtc (IDataObject)
1112 * The data cache doesn't implement this method.
1114 * See Windows documentation for more details on IDataObject methods.
1116 static HRESULT WINAPI DataCache_EnumFormatEtc(
1119 IEnumFORMATETC** ppenumFormatEtc)
1125 /************************************************************************
1126 * DataCache_DAdvise (IDataObject)
1128 * The data cache doesn't support connections.
1130 * See Windows documentation for more details on IDataObject methods.
1132 static HRESULT WINAPI DataCache_DAdvise(
1134 FORMATETC* pformatetc,
1136 IAdviseSink* pAdvSink,
1137 DWORD* pdwConnection)
1140 return OLE_E_ADVISENOTSUPPORTED;
1143 /************************************************************************
1144 * DataCache_DUnadvise (IDataObject)
1146 * The data cache doesn't support connections.
1148 * See Windows documentation for more details on IDataObject methods.
1150 static HRESULT WINAPI DataCache_DUnadvise(
1155 return OLE_E_NOCONNECTION;
1158 /************************************************************************
1159 * DataCache_EnumDAdvise (IDataObject)
1161 * The data cache doesn't support connections.
1163 * See Windows documentation for more details on IDataObject methods.
1165 static HRESULT WINAPI DataCache_EnumDAdvise(
1167 IEnumSTATDATA** ppenumAdvise)
1170 return OLE_E_ADVISENOTSUPPORTED;
1173 /*********************************************************
1174 * Method implementation for the IDataObject
1175 * part of the DataCache class.
1178 /************************************************************************
1179 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1181 * See Windows documentation for more details on IUnknown methods.
1183 static HRESULT WINAPI DataCache_IPersistStorage_QueryInterface(
1184 IPersistStorage* iface,
1188 DataCache *this = impl_from_IPersistStorage(iface);
1190 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1193 /************************************************************************
1194 * DataCache_IPersistStorage_AddRef (IUnknown)
1196 * See Windows documentation for more details on IUnknown methods.
1198 static ULONG WINAPI DataCache_IPersistStorage_AddRef(
1199 IPersistStorage* iface)
1201 DataCache *this = impl_from_IPersistStorage(iface);
1203 return IUnknown_AddRef(this->outerUnknown);
1206 /************************************************************************
1207 * DataCache_IPersistStorage_Release (IUnknown)
1209 * See Windows documentation for more details on IUnknown methods.
1211 static ULONG WINAPI DataCache_IPersistStorage_Release(
1212 IPersistStorage* iface)
1214 DataCache *this = impl_from_IPersistStorage(iface);
1216 return IUnknown_Release(this->outerUnknown);
1219 /************************************************************************
1220 * DataCache_GetClassID (IPersistStorage)
1222 * The data cache doesn't implement this method.
1224 * See Windows documentation for more details on IPersistStorage methods.
1226 static HRESULT WINAPI DataCache_GetClassID(
1227 IPersistStorage* iface,
1230 DataCache *This = impl_from_IPersistStorage(iface);
1231 DataCacheEntry *cache_entry;
1233 TRACE("(%p, %p)\n", iface, pClassID);
1235 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1237 if (cache_entry->storage != NULL)
1240 HRESULT hr = IStorage_Stat(cache_entry->storage, &statstg, STATFLAG_NONAME);
1243 *pClassID = statstg.clsid;
1249 *pClassID = CLSID_NULL;
1254 /************************************************************************
1255 * DataCache_IsDirty (IPersistStorage)
1257 * See Windows documentation for more details on IPersistStorage methods.
1259 static HRESULT WINAPI DataCache_IsDirty(
1260 IPersistStorage* iface)
1262 DataCache *This = impl_from_IPersistStorage(iface);
1263 DataCacheEntry *cache_entry;
1265 TRACE("(%p)\n", iface);
1270 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1271 if (cache_entry->dirty)
1277 /************************************************************************
1278 * DataCache_InitNew (IPersistStorage)
1280 * The data cache implementation of IPersistStorage_InitNew simply stores
1281 * the storage pointer.
1283 * See Windows documentation for more details on IPersistStorage methods.
1285 static HRESULT WINAPI DataCache_InitNew(
1286 IPersistStorage* iface,
1289 DataCache *This = impl_from_IPersistStorage(iface);
1291 TRACE("(%p, %p)\n", iface, pStg);
1293 if (This->presentationStorage != NULL)
1294 IStorage_Release(This->presentationStorage);
1296 This->presentationStorage = pStg;
1298 IStorage_AddRef(This->presentationStorage);
1304 /************************************************************************
1305 * DataCache_Load (IPersistStorage)
1307 * The data cache implementation of IPersistStorage_Load doesn't
1308 * actually load anything. Instead, it holds on to the storage pointer
1309 * and it will load the presentation information when the
1310 * IDataObject_GetData or IViewObject2_Draw methods are called.
1312 * See Windows documentation for more details on IPersistStorage methods.
1314 static HRESULT WINAPI DataCache_Load(
1315 IPersistStorage* iface,
1318 DataCache *This = impl_from_IPersistStorage(iface);
1320 IEnumSTATSTG *pEnum;
1323 TRACE("(%p, %p)\n", iface, pStg);
1325 if (This->presentationStorage != NULL)
1326 IStorage_Release(This->presentationStorage);
1328 This->presentationStorage = pStg;
1330 hr = IStorage_EnumElements(pStg, 0, NULL, 0, &pEnum);
1331 if (FAILED(hr)) return hr;
1333 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
1335 if (DataCache_IsPresentationStream(&elem))
1339 hr = IStorage_OpenStream(This->presentationStorage, elem.pwcsName,
1340 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
1344 PresentationDataHeader header;
1346 CLIPFORMAT clipformat;
1348 hr = read_clipformat(pStm, &clipformat);
1351 hr = IStream_Read(pStm, &header, sizeof(header),
1354 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1355 if (hr == S_OK && actual_read == sizeof(header))
1357 DataCacheEntry *cache_entry;
1360 fmtetc.cfFormat = clipformat;
1361 fmtetc.ptd = NULL; /* FIXME */
1362 fmtetc.dwAspect = header.dvAspect;
1363 fmtetc.lindex = header.lindex;
1364 fmtetc.tymed = header.tymed;
1366 TRACE("loading entry with formatetc: %s\n", debugstr_formatetc(&fmtetc));
1368 cache_entry = DataCache_GetEntryForFormatEtc(This, &fmtetc);
1370 hr = DataCache_CreateEntry(This, &fmtetc, &cache_entry);
1373 DataCacheEntry_DiscardData(cache_entry);
1374 if (cache_entry->storage) IStorage_Release(cache_entry->storage);
1375 cache_entry->storage = pStg;
1376 IStorage_AddRef(pStg);
1377 cache_entry->dirty = FALSE;
1381 IStream_Release(pStm);
1385 CoTaskMemFree(elem.pwcsName);
1388 This->dirty = FALSE;
1390 IEnumSTATSTG_Release(pEnum);
1392 IStorage_AddRef(This->presentationStorage);
1396 /************************************************************************
1397 * DataCache_Save (IPersistStorage)
1399 * Until we actually connect to a running object and retrieve new
1400 * information to it, we never have to save anything. However, it is
1401 * our responsibility to copy the information when saving to a new
1404 * See Windows documentation for more details on IPersistStorage methods.
1406 static HRESULT WINAPI DataCache_Save(
1407 IPersistStorage* iface,
1411 DataCache *This = impl_from_IPersistStorage(iface);
1412 DataCacheEntry *cache_entry;
1415 unsigned short stream_number = 0;
1417 TRACE("(%p, %p, %d)\n", iface, pStg, fSameAsLoad);
1419 dirty = This->dirty;
1422 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1424 dirty = cache_entry->dirty;
1430 /* this is a shortcut if nothing changed */
1431 if (!dirty && !fSameAsLoad && This->presentationStorage)
1433 return IStorage_CopyTo(This->presentationStorage, 0, NULL, NULL, pStg);
1436 /* assign stream numbers to the cache entries */
1437 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1439 if (cache_entry->stream_number != stream_number)
1441 cache_entry->dirty = TRUE; /* needs to be written out again */
1442 cache_entry->stream_number = stream_number;
1447 /* write out the cache entries */
1448 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1450 if (!fSameAsLoad || cache_entry->dirty)
1452 hr = DataCacheEntry_Save(cache_entry, pStg, fSameAsLoad);
1456 cache_entry->dirty = FALSE;
1460 This->dirty = FALSE;
1464 /************************************************************************
1465 * DataCache_SaveCompleted (IPersistStorage)
1467 * This method is called to tell the cache to release the storage
1468 * pointer it's currently holding.
1470 * See Windows documentation for more details on IPersistStorage methods.
1472 static HRESULT WINAPI DataCache_SaveCompleted(
1473 IPersistStorage* iface,
1476 TRACE("(%p, %p)\n", iface, pStgNew);
1481 * First, make sure we get our hands off any storage we have.
1484 IPersistStorage_HandsOffStorage(iface);
1487 * Then, attach to the new storage.
1490 DataCache_Load(iface, pStgNew);
1496 /************************************************************************
1497 * DataCache_HandsOffStorage (IPersistStorage)
1499 * This method is called to tell the cache to release the storage
1500 * pointer it's currently holding.
1502 * See Windows documentation for more details on IPersistStorage methods.
1504 static HRESULT WINAPI DataCache_HandsOffStorage(
1505 IPersistStorage* iface)
1507 DataCache *this = impl_from_IPersistStorage(iface);
1508 DataCacheEntry *cache_entry;
1510 TRACE("(%p)\n", iface);
1512 if (this->presentationStorage != NULL)
1514 IStorage_Release(this->presentationStorage);
1515 this->presentationStorage = NULL;
1518 LIST_FOR_EACH_ENTRY(cache_entry, &this->cache_list, DataCacheEntry, entry)
1519 DataCacheEntry_HandsOffStorage(cache_entry);
1524 /*********************************************************
1525 * Method implementation for the IViewObject2
1526 * part of the DataCache class.
1529 /************************************************************************
1530 * DataCache_IViewObject2_QueryInterface (IUnknown)
1532 * See Windows documentation for more details on IUnknown methods.
1534 static HRESULT WINAPI DataCache_IViewObject2_QueryInterface(
1535 IViewObject2* iface,
1539 DataCache *this = impl_from_IViewObject2(iface);
1541 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1544 /************************************************************************
1545 * DataCache_IViewObject2_AddRef (IUnknown)
1547 * See Windows documentation for more details on IUnknown methods.
1549 static ULONG WINAPI DataCache_IViewObject2_AddRef(
1550 IViewObject2* iface)
1552 DataCache *this = impl_from_IViewObject2(iface);
1554 return IUnknown_AddRef(this->outerUnknown);
1557 /************************************************************************
1558 * DataCache_IViewObject2_Release (IUnknown)
1560 * See Windows documentation for more details on IUnknown methods.
1562 static ULONG WINAPI DataCache_IViewObject2_Release(
1563 IViewObject2* iface)
1565 DataCache *this = impl_from_IViewObject2(iface);
1567 return IUnknown_Release(this->outerUnknown);
1570 /************************************************************************
1571 * DataCache_Draw (IViewObject2)
1573 * This method will draw the cached representation of the object
1574 * to the given device context.
1576 * See Windows documentation for more details on IViewObject2 methods.
1578 static HRESULT WINAPI DataCache_Draw(
1579 IViewObject2* iface,
1583 DVTARGETDEVICE* ptd,
1586 LPCRECTL lprcBounds,
1587 LPCRECTL lprcWBounds,
1588 BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue),
1589 ULONG_PTR dwContinue)
1591 DataCache *This = impl_from_IViewObject2(iface);
1593 DataCacheEntry *cache_entry;
1595 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1610 if (lprcBounds==NULL)
1611 return E_INVALIDARG;
1613 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1615 /* FIXME: compare ptd too */
1616 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1617 (cache_entry->fmtetc.lindex != lindex))
1620 /* if the data hasn't been loaded yet, do it now */
1621 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1623 hres = DataCacheEntry_LoadData(cache_entry);
1629 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1632 switch (cache_entry->data_cf)
1634 case CF_METAFILEPICT:
1637 * We have to be careful not to modify the state of the
1642 SIZE oldViewportExt;
1643 POINT oldViewportOrg;
1644 METAFILEPICT *mfpict;
1646 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1647 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1650 prevMapMode = SetMapMode(hdcDraw, mfpict->mm);
1652 SetWindowExtEx(hdcDraw,
1657 SetViewportExtEx(hdcDraw,
1658 lprcBounds->right - lprcBounds->left,
1659 lprcBounds->bottom - lprcBounds->top,
1662 SetViewportOrgEx(hdcDraw,
1667 PlayMetaFile(hdcDraw, mfpict->hMF);
1669 SetWindowExtEx(hdcDraw,
1674 SetViewportExtEx(hdcDraw,
1679 SetViewportOrgEx(hdcDraw,
1684 SetMapMode(hdcDraw, prevMapMode);
1686 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1693 WARN("no data could be found to be drawn\n");
1698 static HRESULT WINAPI DataCache_GetColorSet(
1699 IViewObject2* iface,
1703 DVTARGETDEVICE* ptd,
1704 HDC hicTargetDevice,
1705 LOGPALETTE** ppColorSet)
1711 static HRESULT WINAPI DataCache_Freeze(
1712 IViewObject2* iface,
1722 static HRESULT WINAPI DataCache_Unfreeze(
1723 IViewObject2* iface,
1730 /************************************************************************
1731 * DataCache_SetAdvise (IViewObject2)
1733 * This sets-up an advisory sink with the data cache. When the object's
1734 * view changes, this sink is called.
1736 * See Windows documentation for more details on IViewObject2 methods.
1738 static HRESULT WINAPI DataCache_SetAdvise(
1739 IViewObject2* iface,
1742 IAdviseSink* pAdvSink)
1744 DataCache *this = impl_from_IViewObject2(iface);
1746 TRACE("(%p, %x, %x, %p)\n", iface, aspects, advf, pAdvSink);
1749 * A call to this function removes the previous sink
1751 if (this->sinkInterface != NULL)
1753 IAdviseSink_Release(this->sinkInterface);
1754 this->sinkInterface = NULL;
1755 this->sinkAspects = 0;
1756 this->sinkAdviseFlag = 0;
1760 * Now, setup the new one.
1764 this->sinkInterface = pAdvSink;
1765 this->sinkAspects = aspects;
1766 this->sinkAdviseFlag = advf;
1768 IAdviseSink_AddRef(this->sinkInterface);
1772 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1775 if (advf & ADVF_PRIMEFIRST)
1777 DataCache_FireOnViewChange(this, aspects, -1);
1783 /************************************************************************
1784 * DataCache_GetAdvise (IViewObject2)
1786 * This method queries the current state of the advise sink
1787 * installed on the data cache.
1789 * See Windows documentation for more details on IViewObject2 methods.
1791 static HRESULT WINAPI DataCache_GetAdvise(
1792 IViewObject2* iface,
1795 IAdviseSink** ppAdvSink)
1797 DataCache *this = impl_from_IViewObject2(iface);
1799 TRACE("(%p, %p, %p, %p)\n", iface, pAspects, pAdvf, ppAdvSink);
1802 * Just copy all the requested values.
1805 *pAspects = this->sinkAspects;
1808 *pAdvf = this->sinkAdviseFlag;
1810 if (ppAdvSink!=NULL)
1812 if (this->sinkInterface != NULL)
1813 IAdviseSink_QueryInterface(this->sinkInterface,
1816 else *ppAdvSink = NULL;
1822 /************************************************************************
1823 * DataCache_GetExtent (IViewObject2)
1825 * This method retrieves the "natural" size of this cached object.
1827 * See Windows documentation for more details on IViewObject2 methods.
1829 static HRESULT WINAPI DataCache_GetExtent(
1830 IViewObject2* iface,
1833 DVTARGETDEVICE* ptd,
1836 DataCache *This = impl_from_IViewObject2(iface);
1837 HRESULT hres = E_FAIL;
1838 DataCacheEntry *cache_entry;
1840 TRACE("(%p, %x, %d, %p, %p)\n",
1841 iface, dwDrawAspect, lindex, ptd, lpsizel);
1850 * Initialize the out parameter.
1856 * This flag should be set to -1.
1859 FIXME("Unimplemented flag lindex = %d\n", lindex);
1862 * Right now, we support only the callback from
1863 * the default handler.
1866 FIXME("Unimplemented ptd = %p\n", ptd);
1868 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1870 /* FIXME: compare ptd too */
1871 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1872 (cache_entry->fmtetc.lindex != lindex))
1875 /* if the data hasn't been loaded yet, do it now */
1876 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1878 hres = DataCacheEntry_LoadData(cache_entry);
1884 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1888 switch (cache_entry->data_cf)
1890 case CF_METAFILEPICT:
1892 METAFILEPICT *mfpict;
1894 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1895 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1898 lpsizel->cx = mfpict->xExt;
1899 lpsizel->cy = mfpict->yExt;
1901 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1908 WARN("no data could be found to get the extents from\n");
1911 * This method returns OLE_E_BLANK when it fails.
1917 /*********************************************************
1918 * Method implementation for the IOleCache2
1919 * part of the DataCache class.
1922 /************************************************************************
1923 * DataCache_IOleCache2_QueryInterface (IUnknown)
1925 * See Windows documentation for more details on IUnknown methods.
1927 static HRESULT WINAPI DataCache_IOleCache2_QueryInterface(
1932 DataCache *this = impl_from_IOleCache2(iface);
1934 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1937 /************************************************************************
1938 * DataCache_IOleCache2_AddRef (IUnknown)
1940 * See Windows documentation for more details on IUnknown methods.
1942 static ULONG WINAPI DataCache_IOleCache2_AddRef(
1945 DataCache *this = impl_from_IOleCache2(iface);
1947 return IUnknown_AddRef(this->outerUnknown);
1950 /************************************************************************
1951 * DataCache_IOleCache2_Release (IUnknown)
1953 * See Windows documentation for more details on IUnknown methods.
1955 static ULONG WINAPI DataCache_IOleCache2_Release(
1958 DataCache *this = impl_from_IOleCache2(iface);
1960 return IUnknown_Release(this->outerUnknown);
1963 static HRESULT WINAPI DataCache_Cache(
1965 FORMATETC* pformatetc,
1967 DWORD* pdwConnection)
1969 DataCache *This = impl_from_IOleCache2(iface);
1970 DataCacheEntry *cache_entry;
1973 TRACE("(%p, 0x%x, %p)\n", pformatetc, advf, pdwConnection);
1975 if (!pformatetc || !pdwConnection)
1976 return E_INVALIDARG;
1978 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc));
1982 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1985 TRACE("found an existing cache entry\n");
1986 *pdwConnection = cache_entry->id;
1987 return CACHE_S_SAMECACHE;
1990 hr = DataCache_CreateEntry(This, pformatetc, &cache_entry);
1993 *pdwConnection = cache_entry->id;
1998 static HRESULT WINAPI DataCache_Uncache(
2002 DataCache *This = impl_from_IOleCache2(iface);
2003 DataCacheEntry *cache_entry;
2005 TRACE("(%d)\n", dwConnection);
2007 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2008 if (cache_entry->id == dwConnection)
2010 DataCacheEntry_Destroy(cache_entry);
2014 WARN("no connection found for %d\n", dwConnection);
2016 return OLE_E_NOCONNECTION;
2019 static HRESULT WINAPI DataCache_EnumCache(
2021 IEnumSTATDATA** ppenumSTATDATA)
2027 static HRESULT WINAPI DataCache_InitCache(
2029 IDataObject* pDataObject)
2035 static HRESULT WINAPI DataCache_IOleCache2_SetData(
2037 FORMATETC* pformatetc,
2041 DataCache *This = impl_from_IOleCache2(iface);
2042 DataCacheEntry *cache_entry;
2045 TRACE("(%p, %p, %s)\n", pformatetc, pmedium, fRelease ? "TRUE" : "FALSE");
2046 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc));
2048 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
2051 hr = DataCacheEntry_SetData(cache_entry, pformatetc, pmedium, fRelease);
2054 DataCache_FireOnViewChange(This, cache_entry->fmtetc.dwAspect,
2055 cache_entry->fmtetc.lindex);
2059 WARN("cache entry not found\n");
2064 static HRESULT WINAPI DataCache_UpdateCache(
2066 LPDATAOBJECT pDataObject,
2070 FIXME("(%p, 0x%x, %p): stub\n", pDataObject, grfUpdf, pReserved);
2074 static HRESULT WINAPI DataCache_DiscardCache(
2076 DWORD dwDiscardOptions)
2078 DataCache *This = impl_from_IOleCache2(iface);
2079 DataCacheEntry *cache_entry;
2082 TRACE("(%d)\n", dwDiscardOptions);
2084 if (dwDiscardOptions == DISCARDCACHE_SAVEIFDIRTY)
2085 hr = DataCache_Save((IPersistStorage *)&This->lpvtblIPersistStorage,
2086 This->presentationStorage, TRUE);
2088 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2090 hr = DataCacheEntry_DiscardData(cache_entry);
2099 /*********************************************************
2100 * Method implementation for the IOleCacheControl
2101 * part of the DataCache class.
2104 /************************************************************************
2105 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2107 * See Windows documentation for more details on IUnknown methods.
2109 static HRESULT WINAPI DataCache_IOleCacheControl_QueryInterface(
2110 IOleCacheControl* iface,
2114 DataCache *this = impl_from_IOleCacheControl(iface);
2116 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
2119 /************************************************************************
2120 * DataCache_IOleCacheControl_AddRef (IUnknown)
2122 * See Windows documentation for more details on IUnknown methods.
2124 static ULONG WINAPI DataCache_IOleCacheControl_AddRef(
2125 IOleCacheControl* iface)
2127 DataCache *this = impl_from_IOleCacheControl(iface);
2129 return IUnknown_AddRef(this->outerUnknown);
2132 /************************************************************************
2133 * DataCache_IOleCacheControl_Release (IUnknown)
2135 * See Windows documentation for more details on IUnknown methods.
2137 static ULONG WINAPI DataCache_IOleCacheControl_Release(
2138 IOleCacheControl* iface)
2140 DataCache *this = impl_from_IOleCacheControl(iface);
2142 return IUnknown_Release(this->outerUnknown);
2145 static HRESULT WINAPI DataCache_OnRun(
2146 IOleCacheControl* iface,
2147 LPDATAOBJECT pDataObject)
2153 static HRESULT WINAPI DataCache_OnStop(
2154 IOleCacheControl* iface)
2161 * Virtual function tables for the DataCache class.
2163 static const IUnknownVtbl DataCache_NDIUnknown_VTable =
2165 DataCache_NDIUnknown_QueryInterface,
2166 DataCache_NDIUnknown_AddRef,
2167 DataCache_NDIUnknown_Release
2170 static const IDataObjectVtbl DataCache_IDataObject_VTable =
2172 DataCache_IDataObject_QueryInterface,
2173 DataCache_IDataObject_AddRef,
2174 DataCache_IDataObject_Release,
2176 DataCache_GetDataHere,
2177 DataCache_QueryGetData,
2178 DataCache_GetCanonicalFormatEtc,
2179 DataCache_IDataObject_SetData,
2180 DataCache_EnumFormatEtc,
2182 DataCache_DUnadvise,
2183 DataCache_EnumDAdvise
2186 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable =
2188 DataCache_IPersistStorage_QueryInterface,
2189 DataCache_IPersistStorage_AddRef,
2190 DataCache_IPersistStorage_Release,
2191 DataCache_GetClassID,
2196 DataCache_SaveCompleted,
2197 DataCache_HandsOffStorage
2200 static const IViewObject2Vtbl DataCache_IViewObject2_VTable =
2202 DataCache_IViewObject2_QueryInterface,
2203 DataCache_IViewObject2_AddRef,
2204 DataCache_IViewObject2_Release,
2206 DataCache_GetColorSet,
2209 DataCache_SetAdvise,
2210 DataCache_GetAdvise,
2214 static const IOleCache2Vtbl DataCache_IOleCache2_VTable =
2216 DataCache_IOleCache2_QueryInterface,
2217 DataCache_IOleCache2_AddRef,
2218 DataCache_IOleCache2_Release,
2221 DataCache_EnumCache,
2222 DataCache_InitCache,
2223 DataCache_IOleCache2_SetData,
2224 DataCache_UpdateCache,
2225 DataCache_DiscardCache
2228 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable =
2230 DataCache_IOleCacheControl_QueryInterface,
2231 DataCache_IOleCacheControl_AddRef,
2232 DataCache_IOleCacheControl_Release,
2237 /******************************************************************************
2238 * CreateDataCache [OLE32.@]
2240 * Creates a data cache to allow an object to render one or more of its views,
2241 * whether running or not.
2244 * pUnkOuter [I] Outer unknown for the object.
2246 * riid [I] IID of interface to return.
2247 * ppvObj [O] Address where the data cache object will be stored on return.
2251 * Failure: HRESULT code.
2254 * The following interfaces are supported by the returned data cache object:
2255 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorage, IDataObject,
2256 * IViewObject and IViewObject2.
2258 HRESULT WINAPI CreateDataCache(
2259 LPUNKNOWN pUnkOuter,
2264 DataCache* newCache = NULL;
2267 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid), pUnkOuter, debugstr_guid(riid), ppvObj);
2278 * If this cache is constructed for aggregation, make sure
2279 * the caller is requesting the IUnknown interface.
2280 * This is necessary because it's the only time the non-delegating
2281 * IUnknown pointer can be returned to the outside.
2283 if ( (pUnkOuter!=NULL) &&
2284 (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) != 0) )
2285 return CLASS_E_NOAGGREGATION;
2288 * Try to construct a new instance of the class.
2290 newCache = DataCache_Construct(rclsid,
2294 return E_OUTOFMEMORY;
2297 * Make sure it supports the interface required by the caller.
2299 hr = IUnknown_QueryInterface((IUnknown*)&(newCache->lpvtblNDIUnknown), riid, ppvObj);
2302 * Release the reference obtained in the constructor. If
2303 * the QueryInterface was unsuccessful, it will free the class.
2305 IUnknown_Release((IUnknown*)&(newCache->lpvtblNDIUnknown));
2310 /*********************************************************
2311 * Method implementation for DataCache class.
2313 static DataCache* DataCache_Construct(
2315 LPUNKNOWN pUnkOuter)
2317 DataCache* newObject = 0;
2320 * Allocate space for the object.
2322 newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache));
2328 * Initialize the virtual function table.
2330 newObject->lpVtbl = &DataCache_IDataObject_VTable;
2331 newObject->lpvtblNDIUnknown = &DataCache_NDIUnknown_VTable;
2332 newObject->lpvtblIPersistStorage = &DataCache_IPersistStorage_VTable;
2333 newObject->lpvtblIViewObject = &DataCache_IViewObject2_VTable;
2334 newObject->lpvtblIOleCache2 = &DataCache_IOleCache2_VTable;
2335 newObject->lpvtblIOleCacheControl = &DataCache_IOleCacheControl_VTable;
2338 * Start with one reference count. The caller of this function
2339 * must release the interface pointer when it is done.
2344 * Initialize the outer unknown
2345 * We don't keep a reference on the outer unknown since, the way
2346 * aggregation works, our lifetime is at least as large as its
2349 if (pUnkOuter==NULL)
2350 pUnkOuter = (IUnknown*)&(newObject->lpvtblNDIUnknown);
2352 newObject->outerUnknown = pUnkOuter;
2355 * Initialize the other members of the structure.
2357 newObject->sinkAspects = 0;
2358 newObject->sinkAdviseFlag = 0;
2359 newObject->sinkInterface = 0;
2360 newObject->presentationStorage = NULL;
2361 list_init(&newObject->cache_list);
2362 newObject->last_cache_id = 1;
2363 newObject->dirty = FALSE;