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;
129 /* The sink that is connected to a remote object.
130 The other interfaces are not available by QI'ing the sink and vice-versa */
131 const IAdviseSinkVtbl* lpvtblIAdviseSink;
134 * Reference count of this object
139 * IUnknown implementation of the outer object.
141 IUnknown* outerUnknown;
144 * The user of this object can setup ONE advise sink
145 * connection with the object. These parameters describe
149 DWORD sinkAdviseFlag;
150 IAdviseSink* sinkInterface;
151 IStorage *presentationStorage;
153 /* list of cache entries */
154 struct list cache_list;
155 /* last id assigned to an entry */
161 typedef struct DataCache DataCache;
164 * Here, I define utility macros to help with the casting of the
166 * There is a version to accommodate all of the VTables implemented
170 static inline DataCache *impl_from_IDataObject( IDataObject *iface )
172 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpVtbl));
175 static inline DataCache *impl_from_NDIUnknown( IUnknown *iface )
177 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblNDIUnknown));
180 static inline DataCache *impl_from_IPersistStorage( IPersistStorage *iface )
182 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIPersistStorage));
185 static inline DataCache *impl_from_IViewObject2( IViewObject2 *iface )
187 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIViewObject));
190 static inline DataCache *impl_from_IOleCache2( IOleCache2 *iface )
192 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCache2));
195 static inline DataCache *impl_from_IOleCacheControl( IOleCacheControl *iface )
197 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCacheControl));
200 static inline DataCache *impl_from_IAdviseSink( IAdviseSink *iface )
202 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIAdviseSink));
205 static const char * debugstr_formatetc(const FORMATETC *formatetc)
207 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
208 formatetc->cfFormat, formatetc->ptd, formatetc->dwAspect,
209 formatetc->lindex, formatetc->tymed);
213 * Prototypes for the methods of the DataCache class.
215 static DataCache* DataCache_Construct(REFCLSID clsid,
216 LPUNKNOWN pUnkOuter);
217 static HRESULT DataCacheEntry_OpenPresStream(DataCacheEntry *cache_entry,
220 static void DataCacheEntry_Destroy(DataCacheEntry *cache_entry)
222 list_remove(&cache_entry->entry);
223 if (cache_entry->storage)
224 IStorage_Release(cache_entry->storage);
225 HeapFree(GetProcessHeap(), 0, cache_entry->fmtetc.ptd);
226 ReleaseStgMedium(&cache_entry->stgmedium);
227 HeapFree(GetProcessHeap(), 0, cache_entry);
230 static void DataCache_Destroy(
231 DataCache* ptrToDestroy)
233 DataCacheEntry *cache_entry, *next_cache_entry;
237 if (ptrToDestroy->sinkInterface != NULL)
239 IAdviseSink_Release(ptrToDestroy->sinkInterface);
240 ptrToDestroy->sinkInterface = NULL;
243 LIST_FOR_EACH_ENTRY_SAFE(cache_entry, next_cache_entry, &ptrToDestroy->cache_list, DataCacheEntry, entry)
244 DataCacheEntry_Destroy(cache_entry);
246 if (ptrToDestroy->presentationStorage != NULL)
248 IStorage_Release(ptrToDestroy->presentationStorage);
249 ptrToDestroy->presentationStorage = NULL;
253 * Free the datacache pointer.
255 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
258 static DataCacheEntry *DataCache_GetEntryForFormatEtc(DataCache *This, const FORMATETC *formatetc)
260 DataCacheEntry *cache_entry;
261 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
263 /* FIXME: also compare DVTARGETDEVICEs */
264 if ((!cache_entry->fmtetc.cfFormat || !formatetc->cfFormat || (formatetc->cfFormat == cache_entry->fmtetc.cfFormat)) &&
265 (formatetc->dwAspect == cache_entry->fmtetc.dwAspect) &&
266 (formatetc->lindex == cache_entry->fmtetc.lindex) &&
267 (!cache_entry->fmtetc.tymed || !formatetc->tymed || (formatetc->tymed == cache_entry->fmtetc.tymed)))
273 /* checks that the clipformat and tymed are valid and returns an error if they
274 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
276 static HRESULT check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat, DWORD tymed)
278 if (!cfFormat || !tymed ||
279 (cfFormat == CF_METAFILEPICT && tymed == TYMED_MFPICT) ||
280 (cfFormat == CF_BITMAP && tymed == TYMED_GDI) ||
281 (cfFormat == CF_DIB && tymed == TYMED_HGLOBAL) ||
282 (cfFormat == CF_ENHMETAFILE && tymed == TYMED_ENHMF))
284 else if (tymed == TYMED_HGLOBAL)
285 return CACHE_S_FORMATETC_NOTSUPPORTED;
288 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat, tymed);
293 static HRESULT DataCache_CreateEntry(DataCache *This, const FORMATETC *formatetc, DataCacheEntry **cache_entry)
297 hr = check_valid_clipformat_and_tymed(formatetc->cfFormat, formatetc->tymed);
300 if (hr == CACHE_S_FORMATETC_NOTSUPPORTED)
301 TRACE("creating unsupported format %d\n", formatetc->cfFormat);
303 *cache_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry));
305 return E_OUTOFMEMORY;
307 (*cache_entry)->fmtetc = *formatetc;
310 (*cache_entry)->fmtetc.ptd = HeapAlloc(GetProcessHeap(), 0, formatetc->ptd->tdSize);
311 memcpy((*cache_entry)->fmtetc.ptd, formatetc->ptd, formatetc->ptd->tdSize);
313 (*cache_entry)->data_cf = 0;
314 (*cache_entry)->stgmedium.tymed = TYMED_NULL;
315 (*cache_entry)->stgmedium.pUnkForRelease = NULL;
316 (*cache_entry)->storage = NULL;
317 (*cache_entry)->id = This->last_cache_id++;
318 (*cache_entry)->dirty = TRUE;
319 (*cache_entry)->stream_number = -1;
320 list_add_tail(&This->cache_list, &(*cache_entry)->entry);
324 /************************************************************************
325 * DataCache_FireOnViewChange
327 * This method will fire an OnViewChange notification to the advise
328 * sink registered with the datacache.
330 * See IAdviseSink::OnViewChange for more details.
332 static void DataCache_FireOnViewChange(
337 TRACE("(%p, %x, %d)\n", this, aspect, lindex);
340 * The sink supplies a filter when it registers
341 * we make sure we only send the notifications when that
344 if ((this->sinkAspects & aspect) != 0)
346 if (this->sinkInterface != NULL)
348 IAdviseSink_OnViewChange(this->sinkInterface,
353 * Some sinks want to be unregistered automatically when
354 * the first notification goes out.
356 if ( (this->sinkAdviseFlag & ADVF_ONLYONCE) != 0)
358 IAdviseSink_Release(this->sinkInterface);
360 this->sinkInterface = NULL;
361 this->sinkAspects = 0;
362 this->sinkAdviseFlag = 0;
368 /* Helper for DataCacheEntry_OpenPresStream */
369 static BOOL DataCache_IsPresentationStream(const STATSTG *elem)
371 /* The presentation streams have names of the form "\002OlePresXXX",
372 * where XXX goes from 000 to 999. */
373 static const WCHAR OlePres[] = { 2,'O','l','e','P','r','e','s' };
375 LPCWSTR name = elem->pwcsName;
377 return (elem->type == STGTY_STREAM)
378 && (strlenW(name) == 11)
379 && (strncmpW(name, OlePres, 8) == 0)
380 && (name[8] >= '0') && (name[8] <= '9')
381 && (name[9] >= '0') && (name[9] <= '9')
382 && (name[10] >= '0') && (name[10] <= '9');
385 static HRESULT read_clipformat(IStream *stream, CLIPFORMAT *clipformat)
393 hr = IStream_Read(stream, &length, sizeof(length), &read);
394 if (hr != S_OK || read != sizeof(length))
395 return DV_E_CLIPFORMAT;
399 hr = IStream_Read(stream, &cf, sizeof(cf), 0);
400 if (hr != S_OK || read != sizeof(cf))
401 return DV_E_CLIPFORMAT;
406 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
408 return E_OUTOFMEMORY;
409 hr = IStream_Read(stream, format_name, length, &read);
410 if (hr != S_OK || read != length || format_name[length - 1] != '\0')
412 HeapFree(GetProcessHeap(), 0, format_name);
413 return DV_E_CLIPFORMAT;
415 *clipformat = RegisterClipboardFormatA(format_name);
416 HeapFree(GetProcessHeap(), 0, format_name);
421 static HRESULT write_clipformat(IStream *stream, CLIPFORMAT clipformat)
426 if (clipformat < 0xc000)
429 length = GetClipboardFormatNameA(clipformat, NULL, 0);
430 hr = IStream_Write(stream, &length, sizeof(length), NULL);
433 if (clipformat < 0xc000)
435 DWORD cf = clipformat;
436 hr = IStream_Write(stream, &cf, sizeof(cf), NULL);
440 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
442 return E_OUTOFMEMORY;
443 GetClipboardFormatNameA(clipformat, format_name, length);
444 hr = IStream_Write(stream, format_name, length, NULL);
445 HeapFree(GetProcessHeap(), 0, format_name);
450 /************************************************************************
451 * DataCacheEntry_OpenPresStream
453 * This method will find the stream for the given presentation. It makes
454 * no attempt at fallback.
457 * this - Pointer to the DataCache object
458 * drawAspect - The aspect of the object that we wish to draw.
459 * pStm - A returned stream. It points to the beginning of the
460 * - presentation data, including the header.
463 * S_OK The requested stream has been opened.
464 * OLE_E_BLANK The requested stream could not be found.
465 * Quite a few others I'm too lazy to map correctly.
468 * Algorithm: Scan the elements of the presentation storage, looking
469 * for presentation streams. For each presentation stream,
470 * load the header and check to see if the aspect matches.
472 * If a fallback is desired, just opening the first presentation stream
475 static HRESULT DataCacheEntry_OpenPresStream(DataCacheEntry *cache_entry, IStream **ppStm)
481 if (!ppStm) return E_POINTER;
483 hr = IStorage_EnumElements(cache_entry->storage, 0, NULL, 0, &pEnum);
484 if (FAILED(hr)) return hr;
486 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
488 if (DataCache_IsPresentationStream(&elem))
492 hr = IStorage_OpenStream(cache_entry->storage, elem.pwcsName,
493 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
497 PresentationDataHeader header;
499 CLIPFORMAT clipformat;
501 hr = read_clipformat(pStm, &clipformat);
504 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
506 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
507 if (hr == S_OK && actual_read == sizeof(header)
508 && header.dvAspect == cache_entry->fmtetc.dwAspect)
510 /* Rewind the stream before returning it. */
511 LARGE_INTEGER offset;
512 offset.u.LowPart = 0;
513 offset.u.HighPart = 0;
514 IStream_Seek(pStm, offset, STREAM_SEEK_SET, NULL);
518 CoTaskMemFree(elem.pwcsName);
519 IEnumSTATSTG_Release(pEnum);
524 IStream_Release(pStm);
528 CoTaskMemFree(elem.pwcsName);
531 IEnumSTATSTG_Release(pEnum);
533 return (hr == S_FALSE ? OLE_E_BLANK : hr);
536 /************************************************************************
537 * DataCacheEntry_LoadData
539 * This method will read information for the requested presentation
540 * into the given structure.
543 * This - The entry to load the data from.
546 * This method returns a metafile handle if it is successful.
547 * it will return 0 if not.
549 static HRESULT DataCacheEntry_LoadData(DataCacheEntry *cache_entry)
551 IStream* presStream = NULL;
553 ULARGE_INTEGER current_pos;
556 METAFILEPICT *mfpict;
558 PresentationDataHeader header;
559 CLIPFORMAT clipformat;
560 static const LARGE_INTEGER offset_zero;
563 * Open the presentation stream.
565 hres = DataCacheEntry_OpenPresStream(cache_entry, &presStream);
571 * Get the size of the stream.
573 hres = IStream_Stat(presStream,
581 hres = read_clipformat(presStream, &clipformat);
584 IStream_Release(presStream);
591 sizeof(PresentationDataHeader),
595 IStream_Release(presStream);
599 hres = IStream_Seek(presStream, offset_zero, STREAM_SEEK_CUR, ¤t_pos);
601 streamInfo.cbSize.QuadPart -= current_pos.QuadPart;
603 hmfpict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
606 IStream_Release(presStream);
607 return E_OUTOFMEMORY;
609 mfpict = GlobalLock(hmfpict);
612 * Allocate a buffer for the metafile bits.
614 metafileBits = HeapAlloc(GetProcessHeap(),
616 streamInfo.cbSize.u.LowPart);
619 * Read the metafile bits.
624 streamInfo.cbSize.u.LowPart,
628 * Create a metafile with those bits.
632 /* FIXME: get this from the stream */
633 mfpict->mm = MM_ANISOTROPIC;
634 mfpict->xExt = header.dwObjectExtentX;
635 mfpict->yExt = header.dwObjectExtentY;
636 mfpict->hMF = SetMetaFileBitsEx(streamInfo.cbSize.u.LowPart, metafileBits);
641 GlobalUnlock(hmfpict);
644 cache_entry->data_cf = cache_entry->fmtetc.cfFormat;
645 cache_entry->stgmedium.tymed = TYMED_MFPICT;
646 cache_entry->stgmedium.u.hMetaFilePict = hmfpict;
654 HeapFree(GetProcessHeap(), 0, metafileBits);
655 IStream_Release(presStream);
660 static HRESULT DataCacheEntry_CreateStream(DataCacheEntry *cache_entry,
661 IStorage *storage, IStream **stream)
663 WCHAR wszName[] = {2,'O','l','e','P','r','e','s',
664 '0' + (cache_entry->stream_number / 100) % 10,
665 '0' + (cache_entry->stream_number / 10) % 10,
666 '0' + cache_entry->stream_number % 10, 0};
668 /* FIXME: cache the created stream in This? */
669 return IStorage_CreateStream(storage, wszName,
670 STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
674 static HRESULT DataCacheEntry_Save(DataCacheEntry *cache_entry, IStorage *storage,
677 PresentationDataHeader header;
679 IStream *pres_stream;
682 TRACE("stream_number = %d, fmtetc = %s\n", cache_entry->stream_number, debugstr_formatetc(&cache_entry->fmtetc));
684 hr = DataCacheEntry_CreateStream(cache_entry, storage, &pres_stream);
688 hr = write_clipformat(pres_stream, cache_entry->data_cf);
692 if (cache_entry->fmtetc.ptd)
693 FIXME("ptd not serialized\n");
695 header.dvAspect = cache_entry->fmtetc.dwAspect;
696 header.lindex = cache_entry->fmtetc.lindex;
697 header.tymed = cache_entry->stgmedium.tymed;
699 header.dwObjectExtentX = 0;
700 header.dwObjectExtentY = 0;
704 switch (cache_entry->data_cf)
706 case CF_METAFILEPICT:
708 if (cache_entry->stgmedium.tymed != TYMED_NULL)
710 const METAFILEPICT *mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict);
713 IStream_Release(pres_stream);
714 return DV_E_STGMEDIUM;
716 header.dwObjectExtentX = mfpict->xExt;
717 header.dwObjectExtentY = mfpict->yExt;
718 header.dwSize = GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
719 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
730 hr = IStream_Write(pres_stream, &header, sizeof(PresentationDataHeader),
734 IStream_Release(pres_stream);
739 switch (cache_entry->data_cf)
741 case CF_METAFILEPICT:
743 if (cache_entry->stgmedium.tymed != TYMED_NULL)
745 const METAFILEPICT *mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict);
748 IStream_Release(pres_stream);
749 return DV_E_STGMEDIUM;
751 data = HeapAlloc(GetProcessHeap(), 0, header.dwSize);
752 GetMetaFileBitsEx(mfpict->hMF, header.dwSize, data);
753 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
762 hr = IStream_Write(pres_stream, data, header.dwSize, NULL);
763 HeapFree(GetProcessHeap(), 0, data);
765 IStream_Release(pres_stream);
769 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
770 * does no checking of whether src_stgm has a supported tymed, so this should be
771 * done in the caller */
772 static HRESULT copy_stg_medium(CLIPFORMAT cf, STGMEDIUM *dest_stgm,
773 const STGMEDIUM *src_stgm)
775 if (src_stgm->tymed == TYMED_MFPICT)
777 const METAFILEPICT *src_mfpict = GlobalLock(src_stgm->u.hMetaFilePict);
778 METAFILEPICT *dest_mfpict;
781 return DV_E_STGMEDIUM;
782 dest_stgm->u.hMetaFilePict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
783 dest_mfpict = GlobalLock(dest_stgm->u.hMetaFilePict);
786 GlobalUnlock(src_stgm->u.hMetaFilePict);
787 return E_OUTOFMEMORY;
789 *dest_mfpict = *src_mfpict;
790 dest_mfpict->hMF = CopyMetaFileW(src_mfpict->hMF, NULL);
791 GlobalUnlock(src_stgm->u.hMetaFilePict);
792 GlobalUnlock(dest_stgm->u.hMetaFilePict);
794 else if (src_stgm->tymed != TYMED_NULL)
796 dest_stgm->u.hGlobal = OleDuplicateData(src_stgm->u.hGlobal, cf,
798 if (!dest_stgm->u.hGlobal)
799 return E_OUTOFMEMORY;
801 dest_stgm->tymed = src_stgm->tymed;
802 dest_stgm->pUnkForRelease = src_stgm->pUnkForRelease;
803 if (dest_stgm->pUnkForRelease)
804 IUnknown_AddRef(dest_stgm->pUnkForRelease);
808 static HRESULT DataCacheEntry_SetData(DataCacheEntry *cache_entry,
809 const FORMATETC *formatetc,
810 const STGMEDIUM *stgmedium,
813 if ((!cache_entry->fmtetc.cfFormat && !formatetc->cfFormat) ||
814 (cache_entry->fmtetc.tymed == TYMED_NULL && formatetc->tymed == TYMED_NULL) ||
815 stgmedium->tymed == TYMED_NULL)
817 WARN("invalid formatetc\n");
818 return DV_E_FORMATETC;
821 cache_entry->dirty = TRUE;
822 ReleaseStgMedium(&cache_entry->stgmedium);
823 cache_entry->data_cf = cache_entry->fmtetc.cfFormat ? cache_entry->fmtetc.cfFormat : formatetc->cfFormat;
826 cache_entry->stgmedium = *stgmedium;
830 return copy_stg_medium(cache_entry->data_cf,
831 &cache_entry->stgmedium, stgmedium);
834 static HRESULT DataCacheEntry_GetData(DataCacheEntry *cache_entry, STGMEDIUM *stgmedium)
836 if (stgmedium->tymed == TYMED_NULL && cache_entry->storage)
838 HRESULT hr = DataCacheEntry_LoadData(cache_entry);
842 if (cache_entry->stgmedium.tymed == TYMED_NULL)
844 return copy_stg_medium(cache_entry->data_cf, stgmedium, &cache_entry->stgmedium);
847 static inline HRESULT DataCacheEntry_DiscardData(DataCacheEntry *cache_entry)
849 ReleaseStgMedium(&cache_entry->stgmedium);
850 cache_entry->data_cf = cache_entry->fmtetc.cfFormat;
854 static inline void DataCacheEntry_HandsOffStorage(DataCacheEntry *cache_entry)
856 if (cache_entry->storage)
858 IStorage_Release(cache_entry->storage);
859 cache_entry->storage = NULL;
863 /*********************************************************
864 * Method implementation for the non delegating IUnknown
865 * part of the DataCache class.
868 /************************************************************************
869 * DataCache_NDIUnknown_QueryInterface (IUnknown)
871 * See Windows documentation for more details on IUnknown methods.
873 * This version of QueryInterface will not delegate it's implementation
874 * to the outer unknown.
876 static HRESULT WINAPI DataCache_NDIUnknown_QueryInterface(
881 DataCache *this = impl_from_NDIUnknown(iface);
884 * Perform a sanity check on the parameters.
890 * Initialize the return parameter.
895 * Compare the riid with the interface IDs implemented by this object.
897 if (IsEqualIID(&IID_IUnknown, riid))
901 else if (IsEqualIID(&IID_IDataObject, riid))
903 *ppvObject = &this->lpVtbl;
905 else if ( IsEqualIID(&IID_IPersistStorage, riid) ||
906 IsEqualIID(&IID_IPersist, riid) )
908 *ppvObject = &this->lpvtblIPersistStorage;
910 else if ( IsEqualIID(&IID_IViewObject, riid) ||
911 IsEqualIID(&IID_IViewObject2, riid) )
913 *ppvObject = &this->lpvtblIViewObject;
915 else if ( IsEqualIID(&IID_IOleCache, riid) ||
916 IsEqualIID(&IID_IOleCache2, riid) )
918 *ppvObject = &this->lpvtblIOleCache2;
920 else if ( IsEqualIID(&IID_IOleCacheControl, riid) )
922 *ppvObject = &this->lpvtblIOleCacheControl;
926 * Check that we obtained an interface.
930 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
931 return E_NOINTERFACE;
935 * Query Interface always increases the reference count by one when it is
938 IUnknown_AddRef((IUnknown*)*ppvObject);
943 /************************************************************************
944 * DataCache_NDIUnknown_AddRef (IUnknown)
946 * See Windows documentation for more details on IUnknown methods.
948 * This version of QueryInterface will not delegate it's implementation
949 * to the outer unknown.
951 static ULONG WINAPI DataCache_NDIUnknown_AddRef(
954 DataCache *this = impl_from_NDIUnknown(iface);
955 return InterlockedIncrement(&this->ref);
958 /************************************************************************
959 * DataCache_NDIUnknown_Release (IUnknown)
961 * See Windows documentation for more details on IUnknown methods.
963 * This version of QueryInterface will not delegate it's implementation
964 * to the outer unknown.
966 static ULONG WINAPI DataCache_NDIUnknown_Release(
969 DataCache *this = impl_from_NDIUnknown(iface);
973 * Decrease the reference count on this object.
975 ref = InterlockedDecrement(&this->ref);
978 * If the reference count goes down to 0, perform suicide.
980 if (ref == 0) DataCache_Destroy(this);
985 /*********************************************************
986 * Method implementation for the IDataObject
987 * part of the DataCache class.
990 /************************************************************************
991 * DataCache_IDataObject_QueryInterface (IUnknown)
993 * See Windows documentation for more details on IUnknown methods.
995 static HRESULT WINAPI DataCache_IDataObject_QueryInterface(
1000 DataCache *this = impl_from_IDataObject(iface);
1002 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1005 /************************************************************************
1006 * DataCache_IDataObject_AddRef (IUnknown)
1008 * See Windows documentation for more details on IUnknown methods.
1010 static ULONG WINAPI DataCache_IDataObject_AddRef(
1013 DataCache *this = impl_from_IDataObject(iface);
1015 return IUnknown_AddRef(this->outerUnknown);
1018 /************************************************************************
1019 * DataCache_IDataObject_Release (IUnknown)
1021 * See Windows documentation for more details on IUnknown methods.
1023 static ULONG WINAPI DataCache_IDataObject_Release(
1026 DataCache *this = impl_from_IDataObject(iface);
1028 return IUnknown_Release(this->outerUnknown);
1031 /************************************************************************
1034 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1035 * See Windows documentation for more details on GetData.
1037 static HRESULT WINAPI DataCache_GetData(
1039 LPFORMATETC pformatetcIn,
1042 DataCache *This = impl_from_IDataObject(iface);
1043 DataCacheEntry *cache_entry;
1045 memset(pmedium, 0, sizeof(*pmedium));
1047 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetcIn);
1051 return DataCacheEntry_GetData(cache_entry, pmedium);
1054 static HRESULT WINAPI DataCache_GetDataHere(
1056 LPFORMATETC pformatetc,
1063 static HRESULT WINAPI DataCache_QueryGetData(
1065 LPFORMATETC pformatetc)
1071 /************************************************************************
1072 * DataCache_EnumFormatEtc (IDataObject)
1074 * The data cache doesn't implement this method.
1076 * See Windows documentation for more details on IDataObject methods.
1078 static HRESULT WINAPI DataCache_GetCanonicalFormatEtc(
1080 LPFORMATETC pformatectIn,
1081 LPFORMATETC pformatetcOut)
1087 /************************************************************************
1088 * DataCache_IDataObject_SetData (IDataObject)
1090 * This method is delegated to the IOleCache2 implementation.
1092 * See Windows documentation for more details on IDataObject methods.
1094 static HRESULT WINAPI DataCache_IDataObject_SetData(
1096 LPFORMATETC pformatetc,
1100 IOleCache2* oleCache = NULL;
1103 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1105 hres = IDataObject_QueryInterface(iface, &IID_IOleCache2, (void**)&oleCache);
1108 return E_UNEXPECTED;
1110 hres = IOleCache2_SetData(oleCache, pformatetc, pmedium, fRelease);
1112 IOleCache2_Release(oleCache);
1117 /************************************************************************
1118 * DataCache_EnumFormatEtc (IDataObject)
1120 * The data cache doesn't implement this method.
1122 * See Windows documentation for more details on IDataObject methods.
1124 static HRESULT WINAPI DataCache_EnumFormatEtc(
1127 IEnumFORMATETC** ppenumFormatEtc)
1133 /************************************************************************
1134 * DataCache_DAdvise (IDataObject)
1136 * The data cache doesn't support connections.
1138 * See Windows documentation for more details on IDataObject methods.
1140 static HRESULT WINAPI DataCache_DAdvise(
1142 FORMATETC* pformatetc,
1144 IAdviseSink* pAdvSink,
1145 DWORD* pdwConnection)
1148 return OLE_E_ADVISENOTSUPPORTED;
1151 /************************************************************************
1152 * DataCache_DUnadvise (IDataObject)
1154 * The data cache doesn't support connections.
1156 * See Windows documentation for more details on IDataObject methods.
1158 static HRESULT WINAPI DataCache_DUnadvise(
1163 return OLE_E_NOCONNECTION;
1166 /************************************************************************
1167 * DataCache_EnumDAdvise (IDataObject)
1169 * The data cache doesn't support connections.
1171 * See Windows documentation for more details on IDataObject methods.
1173 static HRESULT WINAPI DataCache_EnumDAdvise(
1175 IEnumSTATDATA** ppenumAdvise)
1178 return OLE_E_ADVISENOTSUPPORTED;
1181 /*********************************************************
1182 * Method implementation for the IDataObject
1183 * part of the DataCache class.
1186 /************************************************************************
1187 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1189 * See Windows documentation for more details on IUnknown methods.
1191 static HRESULT WINAPI DataCache_IPersistStorage_QueryInterface(
1192 IPersistStorage* iface,
1196 DataCache *this = impl_from_IPersistStorage(iface);
1198 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1201 /************************************************************************
1202 * DataCache_IPersistStorage_AddRef (IUnknown)
1204 * See Windows documentation for more details on IUnknown methods.
1206 static ULONG WINAPI DataCache_IPersistStorage_AddRef(
1207 IPersistStorage* iface)
1209 DataCache *this = impl_from_IPersistStorage(iface);
1211 return IUnknown_AddRef(this->outerUnknown);
1214 /************************************************************************
1215 * DataCache_IPersistStorage_Release (IUnknown)
1217 * See Windows documentation for more details on IUnknown methods.
1219 static ULONG WINAPI DataCache_IPersistStorage_Release(
1220 IPersistStorage* iface)
1222 DataCache *this = impl_from_IPersistStorage(iface);
1224 return IUnknown_Release(this->outerUnknown);
1227 /************************************************************************
1228 * DataCache_GetClassID (IPersistStorage)
1230 * The data cache doesn't implement this method.
1232 * See Windows documentation for more details on IPersistStorage methods.
1234 static HRESULT WINAPI DataCache_GetClassID(
1235 IPersistStorage* iface,
1238 DataCache *This = impl_from_IPersistStorage(iface);
1239 DataCacheEntry *cache_entry;
1241 TRACE("(%p, %p)\n", iface, pClassID);
1243 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1245 if (cache_entry->storage != NULL)
1248 HRESULT hr = IStorage_Stat(cache_entry->storage, &statstg, STATFLAG_NONAME);
1251 *pClassID = statstg.clsid;
1257 *pClassID = CLSID_NULL;
1262 /************************************************************************
1263 * DataCache_IsDirty (IPersistStorage)
1265 * See Windows documentation for more details on IPersistStorage methods.
1267 static HRESULT WINAPI DataCache_IsDirty(
1268 IPersistStorage* iface)
1270 DataCache *This = impl_from_IPersistStorage(iface);
1271 DataCacheEntry *cache_entry;
1273 TRACE("(%p)\n", iface);
1278 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1279 if (cache_entry->dirty)
1285 /************************************************************************
1286 * DataCache_InitNew (IPersistStorage)
1288 * The data cache implementation of IPersistStorage_InitNew simply stores
1289 * the storage pointer.
1291 * See Windows documentation for more details on IPersistStorage methods.
1293 static HRESULT WINAPI DataCache_InitNew(
1294 IPersistStorage* iface,
1297 DataCache *This = impl_from_IPersistStorage(iface);
1299 TRACE("(%p, %p)\n", iface, pStg);
1301 if (This->presentationStorage != NULL)
1302 IStorage_Release(This->presentationStorage);
1304 This->presentationStorage = pStg;
1306 IStorage_AddRef(This->presentationStorage);
1312 /************************************************************************
1313 * DataCache_Load (IPersistStorage)
1315 * The data cache implementation of IPersistStorage_Load doesn't
1316 * actually load anything. Instead, it holds on to the storage pointer
1317 * and it will load the presentation information when the
1318 * IDataObject_GetData or IViewObject2_Draw methods are called.
1320 * See Windows documentation for more details on IPersistStorage methods.
1322 static HRESULT WINAPI DataCache_Load(
1323 IPersistStorage* iface,
1326 DataCache *This = impl_from_IPersistStorage(iface);
1328 IEnumSTATSTG *pEnum;
1331 TRACE("(%p, %p)\n", iface, pStg);
1333 if (This->presentationStorage != NULL)
1334 IStorage_Release(This->presentationStorage);
1336 This->presentationStorage = pStg;
1338 hr = IStorage_EnumElements(pStg, 0, NULL, 0, &pEnum);
1339 if (FAILED(hr)) return hr;
1341 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
1343 if (DataCache_IsPresentationStream(&elem))
1347 hr = IStorage_OpenStream(This->presentationStorage, elem.pwcsName,
1348 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
1352 PresentationDataHeader header;
1354 CLIPFORMAT clipformat;
1356 hr = read_clipformat(pStm, &clipformat);
1359 hr = IStream_Read(pStm, &header, sizeof(header),
1362 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1363 if (hr == S_OK && actual_read == sizeof(header))
1365 DataCacheEntry *cache_entry;
1368 fmtetc.cfFormat = clipformat;
1369 fmtetc.ptd = NULL; /* FIXME */
1370 fmtetc.dwAspect = header.dvAspect;
1371 fmtetc.lindex = header.lindex;
1372 fmtetc.tymed = header.tymed;
1374 TRACE("loading entry with formatetc: %s\n", debugstr_formatetc(&fmtetc));
1376 cache_entry = DataCache_GetEntryForFormatEtc(This, &fmtetc);
1378 hr = DataCache_CreateEntry(This, &fmtetc, &cache_entry);
1381 DataCacheEntry_DiscardData(cache_entry);
1382 if (cache_entry->storage) IStorage_Release(cache_entry->storage);
1383 cache_entry->storage = pStg;
1384 IStorage_AddRef(pStg);
1385 cache_entry->dirty = FALSE;
1389 IStream_Release(pStm);
1393 CoTaskMemFree(elem.pwcsName);
1396 This->dirty = FALSE;
1398 IEnumSTATSTG_Release(pEnum);
1400 IStorage_AddRef(This->presentationStorage);
1404 /************************************************************************
1405 * DataCache_Save (IPersistStorage)
1407 * Until we actually connect to a running object and retrieve new
1408 * information to it, we never have to save anything. However, it is
1409 * our responsibility to copy the information when saving to a new
1412 * See Windows documentation for more details on IPersistStorage methods.
1414 static HRESULT WINAPI DataCache_Save(
1415 IPersistStorage* iface,
1419 DataCache *This = impl_from_IPersistStorage(iface);
1420 DataCacheEntry *cache_entry;
1423 unsigned short stream_number = 0;
1425 TRACE("(%p, %p, %d)\n", iface, pStg, fSameAsLoad);
1427 dirty = This->dirty;
1430 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1432 dirty = cache_entry->dirty;
1438 /* this is a shortcut if nothing changed */
1439 if (!dirty && !fSameAsLoad && This->presentationStorage)
1441 return IStorage_CopyTo(This->presentationStorage, 0, NULL, NULL, pStg);
1444 /* assign stream numbers to the cache entries */
1445 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1447 if (cache_entry->stream_number != stream_number)
1449 cache_entry->dirty = TRUE; /* needs to be written out again */
1450 cache_entry->stream_number = stream_number;
1455 /* write out the cache entries */
1456 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1458 if (!fSameAsLoad || cache_entry->dirty)
1460 hr = DataCacheEntry_Save(cache_entry, pStg, fSameAsLoad);
1464 cache_entry->dirty = FALSE;
1468 This->dirty = FALSE;
1472 /************************************************************************
1473 * DataCache_SaveCompleted (IPersistStorage)
1475 * This method is called to tell the cache to release the storage
1476 * pointer it's currently holding.
1478 * See Windows documentation for more details on IPersistStorage methods.
1480 static HRESULT WINAPI DataCache_SaveCompleted(
1481 IPersistStorage* iface,
1484 TRACE("(%p, %p)\n", iface, pStgNew);
1489 * First, make sure we get our hands off any storage we have.
1492 IPersistStorage_HandsOffStorage(iface);
1495 * Then, attach to the new storage.
1498 DataCache_Load(iface, pStgNew);
1504 /************************************************************************
1505 * DataCache_HandsOffStorage (IPersistStorage)
1507 * This method is called to tell the cache to release the storage
1508 * pointer it's currently holding.
1510 * See Windows documentation for more details on IPersistStorage methods.
1512 static HRESULT WINAPI DataCache_HandsOffStorage(
1513 IPersistStorage* iface)
1515 DataCache *this = impl_from_IPersistStorage(iface);
1516 DataCacheEntry *cache_entry;
1518 TRACE("(%p)\n", iface);
1520 if (this->presentationStorage != NULL)
1522 IStorage_Release(this->presentationStorage);
1523 this->presentationStorage = NULL;
1526 LIST_FOR_EACH_ENTRY(cache_entry, &this->cache_list, DataCacheEntry, entry)
1527 DataCacheEntry_HandsOffStorage(cache_entry);
1532 /*********************************************************
1533 * Method implementation for the IViewObject2
1534 * part of the DataCache class.
1537 /************************************************************************
1538 * DataCache_IViewObject2_QueryInterface (IUnknown)
1540 * See Windows documentation for more details on IUnknown methods.
1542 static HRESULT WINAPI DataCache_IViewObject2_QueryInterface(
1543 IViewObject2* iface,
1547 DataCache *this = impl_from_IViewObject2(iface);
1549 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1552 /************************************************************************
1553 * DataCache_IViewObject2_AddRef (IUnknown)
1555 * See Windows documentation for more details on IUnknown methods.
1557 static ULONG WINAPI DataCache_IViewObject2_AddRef(
1558 IViewObject2* iface)
1560 DataCache *this = impl_from_IViewObject2(iface);
1562 return IUnknown_AddRef(this->outerUnknown);
1565 /************************************************************************
1566 * DataCache_IViewObject2_Release (IUnknown)
1568 * See Windows documentation for more details on IUnknown methods.
1570 static ULONG WINAPI DataCache_IViewObject2_Release(
1571 IViewObject2* iface)
1573 DataCache *this = impl_from_IViewObject2(iface);
1575 return IUnknown_Release(this->outerUnknown);
1578 /************************************************************************
1579 * DataCache_Draw (IViewObject2)
1581 * This method will draw the cached representation of the object
1582 * to the given device context.
1584 * See Windows documentation for more details on IViewObject2 methods.
1586 static HRESULT WINAPI DataCache_Draw(
1587 IViewObject2* iface,
1591 DVTARGETDEVICE* ptd,
1594 LPCRECTL lprcBounds,
1595 LPCRECTL lprcWBounds,
1596 BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue),
1597 ULONG_PTR dwContinue)
1599 DataCache *This = impl_from_IViewObject2(iface);
1601 DataCacheEntry *cache_entry;
1603 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1618 if (lprcBounds==NULL)
1619 return E_INVALIDARG;
1621 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1623 /* FIXME: compare ptd too */
1624 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1625 (cache_entry->fmtetc.lindex != lindex))
1628 /* if the data hasn't been loaded yet, do it now */
1629 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1631 hres = DataCacheEntry_LoadData(cache_entry);
1637 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1640 if (pfnContinue && !pfnContinue(dwContinue)) return E_ABORT;
1642 switch (cache_entry->data_cf)
1644 case CF_METAFILEPICT:
1647 * We have to be careful not to modify the state of the
1652 SIZE oldViewportExt;
1653 POINT oldViewportOrg;
1654 METAFILEPICT *mfpict;
1656 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1657 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1660 prevMapMode = SetMapMode(hdcDraw, mfpict->mm);
1662 SetWindowExtEx(hdcDraw,
1667 SetViewportExtEx(hdcDraw,
1668 lprcBounds->right - lprcBounds->left,
1669 lprcBounds->bottom - lprcBounds->top,
1672 SetViewportOrgEx(hdcDraw,
1677 PlayMetaFile(hdcDraw, mfpict->hMF);
1679 SetWindowExtEx(hdcDraw,
1684 SetViewportExtEx(hdcDraw,
1689 SetViewportOrgEx(hdcDraw,
1694 SetMapMode(hdcDraw, prevMapMode);
1696 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1703 WARN("no data could be found to be drawn\n");
1708 static HRESULT WINAPI DataCache_GetColorSet(
1709 IViewObject2* iface,
1713 DVTARGETDEVICE* ptd,
1714 HDC hicTargetDevice,
1715 LOGPALETTE** ppColorSet)
1721 static HRESULT WINAPI DataCache_Freeze(
1722 IViewObject2* iface,
1732 static HRESULT WINAPI DataCache_Unfreeze(
1733 IViewObject2* iface,
1740 /************************************************************************
1741 * DataCache_SetAdvise (IViewObject2)
1743 * This sets-up an advisory sink with the data cache. When the object's
1744 * view changes, this sink is called.
1746 * See Windows documentation for more details on IViewObject2 methods.
1748 static HRESULT WINAPI DataCache_SetAdvise(
1749 IViewObject2* iface,
1752 IAdviseSink* pAdvSink)
1754 DataCache *this = impl_from_IViewObject2(iface);
1756 TRACE("(%p, %x, %x, %p)\n", iface, aspects, advf, pAdvSink);
1759 * A call to this function removes the previous sink
1761 if (this->sinkInterface != NULL)
1763 IAdviseSink_Release(this->sinkInterface);
1764 this->sinkInterface = NULL;
1765 this->sinkAspects = 0;
1766 this->sinkAdviseFlag = 0;
1770 * Now, setup the new one.
1774 this->sinkInterface = pAdvSink;
1775 this->sinkAspects = aspects;
1776 this->sinkAdviseFlag = advf;
1778 IAdviseSink_AddRef(this->sinkInterface);
1782 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1785 if (advf & ADVF_PRIMEFIRST)
1787 DataCache_FireOnViewChange(this, aspects, -1);
1793 /************************************************************************
1794 * DataCache_GetAdvise (IViewObject2)
1796 * This method queries the current state of the advise sink
1797 * installed on the data cache.
1799 * See Windows documentation for more details on IViewObject2 methods.
1801 static HRESULT WINAPI DataCache_GetAdvise(
1802 IViewObject2* iface,
1805 IAdviseSink** ppAdvSink)
1807 DataCache *this = impl_from_IViewObject2(iface);
1809 TRACE("(%p, %p, %p, %p)\n", iface, pAspects, pAdvf, ppAdvSink);
1812 * Just copy all the requested values.
1815 *pAspects = this->sinkAspects;
1818 *pAdvf = this->sinkAdviseFlag;
1820 if (ppAdvSink!=NULL)
1822 if (this->sinkInterface != NULL)
1823 IAdviseSink_QueryInterface(this->sinkInterface,
1826 else *ppAdvSink = NULL;
1832 /************************************************************************
1833 * DataCache_GetExtent (IViewObject2)
1835 * This method retrieves the "natural" size of this cached object.
1837 * See Windows documentation for more details on IViewObject2 methods.
1839 static HRESULT WINAPI DataCache_GetExtent(
1840 IViewObject2* iface,
1843 DVTARGETDEVICE* ptd,
1846 DataCache *This = impl_from_IViewObject2(iface);
1847 HRESULT hres = E_FAIL;
1848 DataCacheEntry *cache_entry;
1850 TRACE("(%p, %x, %d, %p, %p)\n",
1851 iface, dwDrawAspect, lindex, ptd, lpsizel);
1860 * Initialize the out parameter.
1866 * This flag should be set to -1.
1869 FIXME("Unimplemented flag lindex = %d\n", lindex);
1872 * Right now, we support only the callback from
1873 * the default handler.
1876 FIXME("Unimplemented ptd = %p\n", ptd);
1878 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1880 /* FIXME: compare ptd too */
1881 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1882 (cache_entry->fmtetc.lindex != lindex))
1885 /* if the data hasn't been loaded yet, do it now */
1886 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1888 hres = DataCacheEntry_LoadData(cache_entry);
1894 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1898 switch (cache_entry->data_cf)
1900 case CF_METAFILEPICT:
1902 METAFILEPICT *mfpict;
1904 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1905 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1908 lpsizel->cx = mfpict->xExt;
1909 lpsizel->cy = mfpict->yExt;
1911 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1918 WARN("no data could be found to get the extents from\n");
1921 * This method returns OLE_E_BLANK when it fails.
1927 /*********************************************************
1928 * Method implementation for the IOleCache2
1929 * part of the DataCache class.
1932 /************************************************************************
1933 * DataCache_IOleCache2_QueryInterface (IUnknown)
1935 * See Windows documentation for more details on IUnknown methods.
1937 static HRESULT WINAPI DataCache_IOleCache2_QueryInterface(
1942 DataCache *this = impl_from_IOleCache2(iface);
1944 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1947 /************************************************************************
1948 * DataCache_IOleCache2_AddRef (IUnknown)
1950 * See Windows documentation for more details on IUnknown methods.
1952 static ULONG WINAPI DataCache_IOleCache2_AddRef(
1955 DataCache *this = impl_from_IOleCache2(iface);
1957 return IUnknown_AddRef(this->outerUnknown);
1960 /************************************************************************
1961 * DataCache_IOleCache2_Release (IUnknown)
1963 * See Windows documentation for more details on IUnknown methods.
1965 static ULONG WINAPI DataCache_IOleCache2_Release(
1968 DataCache *this = impl_from_IOleCache2(iface);
1970 return IUnknown_Release(this->outerUnknown);
1973 static HRESULT WINAPI DataCache_Cache(
1975 FORMATETC* pformatetc,
1977 DWORD* pdwConnection)
1979 DataCache *This = impl_from_IOleCache2(iface);
1980 DataCacheEntry *cache_entry;
1983 TRACE("(%p, 0x%x, %p)\n", pformatetc, advf, pdwConnection);
1985 if (!pformatetc || !pdwConnection)
1986 return E_INVALIDARG;
1988 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc));
1992 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1995 TRACE("found an existing cache entry\n");
1996 *pdwConnection = cache_entry->id;
1997 return CACHE_S_SAMECACHE;
2000 hr = DataCache_CreateEntry(This, pformatetc, &cache_entry);
2003 *pdwConnection = cache_entry->id;
2008 static HRESULT WINAPI DataCache_Uncache(
2012 DataCache *This = impl_from_IOleCache2(iface);
2013 DataCacheEntry *cache_entry;
2015 TRACE("(%d)\n", dwConnection);
2017 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2018 if (cache_entry->id == dwConnection)
2020 DataCacheEntry_Destroy(cache_entry);
2024 WARN("no connection found for %d\n", dwConnection);
2026 return OLE_E_NOCONNECTION;
2029 static HRESULT WINAPI DataCache_EnumCache(
2031 IEnumSTATDATA** ppenumSTATDATA)
2037 static HRESULT WINAPI DataCache_InitCache(
2039 IDataObject* pDataObject)
2045 static HRESULT WINAPI DataCache_IOleCache2_SetData(
2047 FORMATETC* pformatetc,
2051 DataCache *This = impl_from_IOleCache2(iface);
2052 DataCacheEntry *cache_entry;
2055 TRACE("(%p, %p, %s)\n", pformatetc, pmedium, fRelease ? "TRUE" : "FALSE");
2056 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc));
2058 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
2061 hr = DataCacheEntry_SetData(cache_entry, pformatetc, pmedium, fRelease);
2064 DataCache_FireOnViewChange(This, cache_entry->fmtetc.dwAspect,
2065 cache_entry->fmtetc.lindex);
2069 WARN("cache entry not found\n");
2074 static HRESULT WINAPI DataCache_UpdateCache(
2076 LPDATAOBJECT pDataObject,
2080 FIXME("(%p, 0x%x, %p): stub\n", pDataObject, grfUpdf, pReserved);
2084 static HRESULT WINAPI DataCache_DiscardCache(
2086 DWORD dwDiscardOptions)
2088 DataCache *This = impl_from_IOleCache2(iface);
2089 DataCacheEntry *cache_entry;
2092 TRACE("(%d)\n", dwDiscardOptions);
2094 if (dwDiscardOptions == DISCARDCACHE_SAVEIFDIRTY)
2095 hr = DataCache_Save((IPersistStorage *)&This->lpvtblIPersistStorage,
2096 This->presentationStorage, TRUE);
2098 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2100 hr = DataCacheEntry_DiscardData(cache_entry);
2109 /*********************************************************
2110 * Method implementation for the IOleCacheControl
2111 * part of the DataCache class.
2114 /************************************************************************
2115 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2117 * See Windows documentation for more details on IUnknown methods.
2119 static HRESULT WINAPI DataCache_IOleCacheControl_QueryInterface(
2120 IOleCacheControl* iface,
2124 DataCache *this = impl_from_IOleCacheControl(iface);
2126 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
2129 /************************************************************************
2130 * DataCache_IOleCacheControl_AddRef (IUnknown)
2132 * See Windows documentation for more details on IUnknown methods.
2134 static ULONG WINAPI DataCache_IOleCacheControl_AddRef(
2135 IOleCacheControl* iface)
2137 DataCache *this = impl_from_IOleCacheControl(iface);
2139 return IUnknown_AddRef(this->outerUnknown);
2142 /************************************************************************
2143 * DataCache_IOleCacheControl_Release (IUnknown)
2145 * See Windows documentation for more details on IUnknown methods.
2147 static ULONG WINAPI DataCache_IOleCacheControl_Release(
2148 IOleCacheControl* iface)
2150 DataCache *this = impl_from_IOleCacheControl(iface);
2152 return IUnknown_Release(this->outerUnknown);
2155 static HRESULT WINAPI DataCache_OnRun(
2156 IOleCacheControl* iface,
2157 LPDATAOBJECT pDataObject)
2163 static HRESULT WINAPI DataCache_OnStop(
2164 IOleCacheControl* iface)
2170 /************************************************************************
2171 * IAdviseSink methods.
2172 * This behaves as an internal object to the data cache. QI'ing its ptr doesn't
2173 * give access to the cache's other interfaces. We don't maintain a ref count,
2174 * the object exists as long as the cache is around.
2176 static HRESULT WINAPI DataCache_IAdviseSink_QueryInterface(IAdviseSink *iface, REFIID iid, void **obj)
2179 if (IsEqualIID(&IID_IUnknown, iid) ||
2180 IsEqualIID(&IID_IAdviseSink, iid))
2187 IAdviseSink_AddRef(iface);
2190 return E_NOINTERFACE;
2193 static ULONG WINAPI DataCache_IAdviseSink_AddRef(IAdviseSink *iface)
2198 static ULONG WINAPI DataCache_IAdviseSink_Release(IAdviseSink *iface)
2203 static void WINAPI DataCache_OnDataChange(IAdviseSink *iface, FORMATETC *fmt, STGMEDIUM *med)
2208 static void WINAPI DataCache_OnViewChange(IAdviseSink *iface, DWORD aspect, LONG index)
2213 static void WINAPI DataCache_OnRename(IAdviseSink *iface, IMoniker *mk)
2218 static void WINAPI DataCache_OnSave(IAdviseSink *iface)
2223 static void WINAPI DataCache_OnClose(IAdviseSink *iface)
2229 * Virtual function tables for the DataCache class.
2231 static const IUnknownVtbl DataCache_NDIUnknown_VTable =
2233 DataCache_NDIUnknown_QueryInterface,
2234 DataCache_NDIUnknown_AddRef,
2235 DataCache_NDIUnknown_Release
2238 static const IDataObjectVtbl DataCache_IDataObject_VTable =
2240 DataCache_IDataObject_QueryInterface,
2241 DataCache_IDataObject_AddRef,
2242 DataCache_IDataObject_Release,
2244 DataCache_GetDataHere,
2245 DataCache_QueryGetData,
2246 DataCache_GetCanonicalFormatEtc,
2247 DataCache_IDataObject_SetData,
2248 DataCache_EnumFormatEtc,
2250 DataCache_DUnadvise,
2251 DataCache_EnumDAdvise
2254 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable =
2256 DataCache_IPersistStorage_QueryInterface,
2257 DataCache_IPersistStorage_AddRef,
2258 DataCache_IPersistStorage_Release,
2259 DataCache_GetClassID,
2264 DataCache_SaveCompleted,
2265 DataCache_HandsOffStorage
2268 static const IViewObject2Vtbl DataCache_IViewObject2_VTable =
2270 DataCache_IViewObject2_QueryInterface,
2271 DataCache_IViewObject2_AddRef,
2272 DataCache_IViewObject2_Release,
2274 DataCache_GetColorSet,
2277 DataCache_SetAdvise,
2278 DataCache_GetAdvise,
2282 static const IOleCache2Vtbl DataCache_IOleCache2_VTable =
2284 DataCache_IOleCache2_QueryInterface,
2285 DataCache_IOleCache2_AddRef,
2286 DataCache_IOleCache2_Release,
2289 DataCache_EnumCache,
2290 DataCache_InitCache,
2291 DataCache_IOleCache2_SetData,
2292 DataCache_UpdateCache,
2293 DataCache_DiscardCache
2296 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable =
2298 DataCache_IOleCacheControl_QueryInterface,
2299 DataCache_IOleCacheControl_AddRef,
2300 DataCache_IOleCacheControl_Release,
2305 static const IAdviseSinkVtbl DataCache_IAdviseSink_VTable =
2307 DataCache_IAdviseSink_QueryInterface,
2308 DataCache_IAdviseSink_AddRef,
2309 DataCache_IAdviseSink_Release,
2310 DataCache_OnDataChange,
2311 DataCache_OnViewChange,
2318 /******************************************************************************
2319 * CreateDataCache [OLE32.@]
2321 * Creates a data cache to allow an object to render one or more of its views,
2322 * whether running or not.
2325 * pUnkOuter [I] Outer unknown for the object.
2327 * riid [I] IID of interface to return.
2328 * ppvObj [O] Address where the data cache object will be stored on return.
2332 * Failure: HRESULT code.
2335 * The following interfaces are supported by the returned data cache object:
2336 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorage, IDataObject,
2337 * IViewObject and IViewObject2.
2339 HRESULT WINAPI CreateDataCache(
2340 LPUNKNOWN pUnkOuter,
2345 DataCache* newCache = NULL;
2348 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid), pUnkOuter, debugstr_guid(riid), ppvObj);
2359 * If this cache is constructed for aggregation, make sure
2360 * the caller is requesting the IUnknown interface.
2361 * This is necessary because it's the only time the non-delegating
2362 * IUnknown pointer can be returned to the outside.
2364 if ( pUnkOuter && !IsEqualIID(&IID_IUnknown, riid) )
2365 return CLASS_E_NOAGGREGATION;
2368 * Try to construct a new instance of the class.
2370 newCache = DataCache_Construct(rclsid,
2374 return E_OUTOFMEMORY;
2377 * Make sure it supports the interface required by the caller.
2379 hr = IUnknown_QueryInterface((IUnknown*)&(newCache->lpvtblNDIUnknown), riid, ppvObj);
2382 * Release the reference obtained in the constructor. If
2383 * the QueryInterface was unsuccessful, it will free the class.
2385 IUnknown_Release((IUnknown*)&(newCache->lpvtblNDIUnknown));
2390 /*********************************************************
2391 * Method implementation for DataCache class.
2393 static DataCache* DataCache_Construct(
2395 LPUNKNOWN pUnkOuter)
2397 DataCache* newObject = 0;
2400 * Allocate space for the object.
2402 newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache));
2408 * Initialize the virtual function table.
2410 newObject->lpVtbl = &DataCache_IDataObject_VTable;
2411 newObject->lpvtblNDIUnknown = &DataCache_NDIUnknown_VTable;
2412 newObject->lpvtblIPersistStorage = &DataCache_IPersistStorage_VTable;
2413 newObject->lpvtblIViewObject = &DataCache_IViewObject2_VTable;
2414 newObject->lpvtblIOleCache2 = &DataCache_IOleCache2_VTable;
2415 newObject->lpvtblIOleCacheControl = &DataCache_IOleCacheControl_VTable;
2416 newObject->lpvtblIAdviseSink = &DataCache_IAdviseSink_VTable;
2419 * Start with one reference count. The caller of this function
2420 * must release the interface pointer when it is done.
2425 * Initialize the outer unknown
2426 * We don't keep a reference on the outer unknown since, the way
2427 * aggregation works, our lifetime is at least as large as its
2430 if (pUnkOuter==NULL)
2431 pUnkOuter = (IUnknown*)&(newObject->lpvtblNDIUnknown);
2433 newObject->outerUnknown = pUnkOuter;
2436 * Initialize the other members of the structure.
2438 newObject->sinkAspects = 0;
2439 newObject->sinkAdviseFlag = 0;
2440 newObject->sinkInterface = 0;
2441 newObject->presentationStorage = NULL;
2442 list_init(&newObject->cache_list);
2443 newObject->last_cache_id = 1;
2444 newObject->dirty = FALSE;