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;
112 /* sink id set when object is running */
114 /* Advise sink flags */
118 /****************************************************************************
124 * List all interface here
126 IDataObject IDataObject_iface;
127 IUnknown IUnknown_iface;
128 IPersistStorage IPersistStorage_iface;
129 IViewObject2 IViewObject2_iface;
130 IOleCache2 IOleCache2_iface;
131 IOleCacheControl IOleCacheControl_iface;
133 /* The sink that is connected to a remote object.
134 The other interfaces are not available by QI'ing the sink and vice-versa */
135 IAdviseSink IAdviseSink_iface;
138 * Reference count of this object
143 * IUnknown implementation of the outer object.
145 IUnknown* outerUnknown;
148 * The user of this object can setup ONE advise sink
149 * connection with the object. These parameters describe
153 DWORD sinkAdviseFlag;
154 IAdviseSink* sinkInterface;
155 IStorage *presentationStorage;
157 /* list of cache entries */
158 struct list cache_list;
159 /* last id assigned to an entry */
163 /* running object set by OnRun */
164 IDataObject *running_object;
167 typedef struct DataCache DataCache;
170 * Here, I define utility macros to help with the casting of the
172 * There is a version to accommodate all of the VTables implemented
176 static inline DataCache *impl_from_IDataObject( IDataObject *iface )
178 return CONTAINING_RECORD(iface, DataCache, IDataObject_iface);
181 static inline DataCache *impl_from_IUnknown( IUnknown *iface )
183 return CONTAINING_RECORD(iface, DataCache, IUnknown_iface);
186 static inline DataCache *impl_from_IPersistStorage( IPersistStorage *iface )
188 return CONTAINING_RECORD(iface, DataCache, IPersistStorage_iface);
191 static inline DataCache *impl_from_IViewObject2( IViewObject2 *iface )
193 return CONTAINING_RECORD(iface, DataCache, IViewObject2_iface);
196 static inline DataCache *impl_from_IOleCache2( IOleCache2 *iface )
198 return CONTAINING_RECORD(iface, DataCache, IOleCache2_iface);
201 static inline DataCache *impl_from_IOleCacheControl( IOleCacheControl *iface )
203 return CONTAINING_RECORD(iface, DataCache, IOleCacheControl_iface);
206 static inline DataCache *impl_from_IAdviseSink( IAdviseSink *iface )
208 return CONTAINING_RECORD(iface, DataCache, IAdviseSink_iface);
211 static const char * debugstr_formatetc(const FORMATETC *formatetc)
213 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
214 formatetc->cfFormat, formatetc->ptd, formatetc->dwAspect,
215 formatetc->lindex, formatetc->tymed);
218 static void DataCacheEntry_Destroy(DataCache *cache, DataCacheEntry *cache_entry)
220 list_remove(&cache_entry->entry);
221 if (cache_entry->storage)
222 IStorage_Release(cache_entry->storage);
223 HeapFree(GetProcessHeap(), 0, cache_entry->fmtetc.ptd);
224 ReleaseStgMedium(&cache_entry->stgmedium);
225 if(cache_entry->sink_id)
226 IDataObject_DUnadvise(cache->running_object, cache_entry->sink_id);
228 HeapFree(GetProcessHeap(), 0, cache_entry);
231 static void DataCache_Destroy(
232 DataCache* ptrToDestroy)
234 DataCacheEntry *cache_entry, *next_cache_entry;
238 if (ptrToDestroy->sinkInterface != NULL)
240 IAdviseSink_Release(ptrToDestroy->sinkInterface);
241 ptrToDestroy->sinkInterface = NULL;
244 LIST_FOR_EACH_ENTRY_SAFE(cache_entry, next_cache_entry, &ptrToDestroy->cache_list, DataCacheEntry, entry)
245 DataCacheEntry_Destroy(ptrToDestroy, cache_entry);
247 if (ptrToDestroy->presentationStorage != NULL)
249 IStorage_Release(ptrToDestroy->presentationStorage);
250 ptrToDestroy->presentationStorage = NULL;
254 * Free the datacache pointer.
256 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
259 static DataCacheEntry *DataCache_GetEntryForFormatEtc(DataCache *This, const FORMATETC *formatetc)
261 DataCacheEntry *cache_entry;
262 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
264 /* FIXME: also compare DVTARGETDEVICEs */
265 if ((!cache_entry->fmtetc.cfFormat || !formatetc->cfFormat || (formatetc->cfFormat == cache_entry->fmtetc.cfFormat)) &&
266 (formatetc->dwAspect == cache_entry->fmtetc.dwAspect) &&
267 (formatetc->lindex == cache_entry->fmtetc.lindex) &&
268 (!cache_entry->fmtetc.tymed || !formatetc->tymed || (formatetc->tymed == cache_entry->fmtetc.tymed)))
274 /* checks that the clipformat and tymed are valid and returns an error if they
275 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
277 static HRESULT check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat, DWORD tymed)
279 if (!cfFormat || !tymed ||
280 (cfFormat == CF_METAFILEPICT && tymed == TYMED_MFPICT) ||
281 (cfFormat == CF_BITMAP && tymed == TYMED_GDI) ||
282 (cfFormat == CF_DIB && tymed == TYMED_HGLOBAL) ||
283 (cfFormat == CF_ENHMETAFILE && tymed == TYMED_ENHMF))
285 else if (tymed == TYMED_HGLOBAL)
286 return CACHE_S_FORMATETC_NOTSUPPORTED;
289 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat, tymed);
294 static HRESULT DataCache_CreateEntry(DataCache *This, const FORMATETC *formatetc, DataCacheEntry **cache_entry)
298 hr = check_valid_clipformat_and_tymed(formatetc->cfFormat, formatetc->tymed);
301 if (hr == CACHE_S_FORMATETC_NOTSUPPORTED)
302 TRACE("creating unsupported format %d\n", formatetc->cfFormat);
304 *cache_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry));
306 return E_OUTOFMEMORY;
308 (*cache_entry)->fmtetc = *formatetc;
311 (*cache_entry)->fmtetc.ptd = HeapAlloc(GetProcessHeap(), 0, formatetc->ptd->tdSize);
312 memcpy((*cache_entry)->fmtetc.ptd, formatetc->ptd, formatetc->ptd->tdSize);
314 (*cache_entry)->data_cf = 0;
315 (*cache_entry)->stgmedium.tymed = TYMED_NULL;
316 (*cache_entry)->stgmedium.pUnkForRelease = NULL;
317 (*cache_entry)->storage = NULL;
318 (*cache_entry)->id = This->last_cache_id++;
319 (*cache_entry)->dirty = TRUE;
320 (*cache_entry)->stream_number = -1;
321 (*cache_entry)->sink_id = 0;
322 (*cache_entry)->advise_flags = 0;
323 list_add_tail(&This->cache_list, &(*cache_entry)->entry);
327 /************************************************************************
328 * DataCache_FireOnViewChange
330 * This method will fire an OnViewChange notification to the advise
331 * sink registered with the datacache.
333 * See IAdviseSink::OnViewChange for more details.
335 static void DataCache_FireOnViewChange(
340 TRACE("(%p, %x, %d)\n", this, aspect, lindex);
343 * The sink supplies a filter when it registers
344 * we make sure we only send the notifications when that
347 if ((this->sinkAspects & aspect) != 0)
349 if (this->sinkInterface != NULL)
351 IAdviseSink_OnViewChange(this->sinkInterface,
356 * Some sinks want to be unregistered automatically when
357 * the first notification goes out.
359 if ( (this->sinkAdviseFlag & ADVF_ONLYONCE) != 0)
361 IAdviseSink_Release(this->sinkInterface);
363 this->sinkInterface = NULL;
364 this->sinkAspects = 0;
365 this->sinkAdviseFlag = 0;
371 /* Helper for DataCacheEntry_OpenPresStream */
372 static BOOL DataCache_IsPresentationStream(const STATSTG *elem)
374 /* The presentation streams have names of the form "\002OlePresXXX",
375 * where XXX goes from 000 to 999. */
376 static const WCHAR OlePres[] = { 2,'O','l','e','P','r','e','s' };
378 LPCWSTR name = elem->pwcsName;
380 return (elem->type == STGTY_STREAM)
381 && (strlenW(name) == 11)
382 && (strncmpW(name, OlePres, 8) == 0)
383 && (name[8] >= '0') && (name[8] <= '9')
384 && (name[9] >= '0') && (name[9] <= '9')
385 && (name[10] >= '0') && (name[10] <= '9');
388 static HRESULT read_clipformat(IStream *stream, CLIPFORMAT *clipformat)
396 hr = IStream_Read(stream, &length, sizeof(length), &read);
397 if (hr != S_OK || read != sizeof(length))
398 return DV_E_CLIPFORMAT;
402 hr = IStream_Read(stream, &cf, sizeof(cf), 0);
403 if (hr != S_OK || read != sizeof(cf))
404 return DV_E_CLIPFORMAT;
409 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
411 return E_OUTOFMEMORY;
412 hr = IStream_Read(stream, format_name, length, &read);
413 if (hr != S_OK || read != length || format_name[length - 1] != '\0')
415 HeapFree(GetProcessHeap(), 0, format_name);
416 return DV_E_CLIPFORMAT;
418 *clipformat = RegisterClipboardFormatA(format_name);
419 HeapFree(GetProcessHeap(), 0, format_name);
424 static HRESULT write_clipformat(IStream *stream, CLIPFORMAT clipformat)
429 if (clipformat < 0xc000)
432 length = GetClipboardFormatNameA(clipformat, NULL, 0);
433 hr = IStream_Write(stream, &length, sizeof(length), NULL);
436 if (clipformat < 0xc000)
438 DWORD cf = clipformat;
439 hr = IStream_Write(stream, &cf, sizeof(cf), NULL);
443 char *format_name = HeapAlloc(GetProcessHeap(), 0, length);
445 return E_OUTOFMEMORY;
446 GetClipboardFormatNameA(clipformat, format_name, length);
447 hr = IStream_Write(stream, format_name, length, NULL);
448 HeapFree(GetProcessHeap(), 0, format_name);
453 /************************************************************************
454 * DataCacheEntry_OpenPresStream
456 * This method will find the stream for the given presentation. It makes
457 * no attempt at fallback.
460 * this - Pointer to the DataCache object
461 * drawAspect - The aspect of the object that we wish to draw.
462 * pStm - A returned stream. It points to the beginning of the
463 * - presentation data, including the header.
466 * S_OK The requested stream has been opened.
467 * OLE_E_BLANK The requested stream could not be found.
468 * Quite a few others I'm too lazy to map correctly.
471 * Algorithm: Scan the elements of the presentation storage, looking
472 * for presentation streams. For each presentation stream,
473 * load the header and check to see if the aspect matches.
475 * If a fallback is desired, just opening the first presentation stream
478 static HRESULT DataCacheEntry_OpenPresStream(DataCacheEntry *cache_entry, IStream **ppStm)
484 if (!ppStm) return E_POINTER;
486 hr = IStorage_EnumElements(cache_entry->storage, 0, NULL, 0, &pEnum);
487 if (FAILED(hr)) return hr;
489 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
491 if (DataCache_IsPresentationStream(&elem))
495 hr = IStorage_OpenStream(cache_entry->storage, elem.pwcsName,
496 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
500 PresentationDataHeader header;
502 CLIPFORMAT clipformat;
504 hr = read_clipformat(pStm, &clipformat);
507 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
509 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
510 if (hr == S_OK && actual_read == sizeof(header)
511 && header.dvAspect == cache_entry->fmtetc.dwAspect)
513 /* Rewind the stream before returning it. */
514 LARGE_INTEGER offset;
515 offset.u.LowPart = 0;
516 offset.u.HighPart = 0;
517 IStream_Seek(pStm, offset, STREAM_SEEK_SET, NULL);
521 CoTaskMemFree(elem.pwcsName);
522 IEnumSTATSTG_Release(pEnum);
527 IStream_Release(pStm);
531 CoTaskMemFree(elem.pwcsName);
534 IEnumSTATSTG_Release(pEnum);
536 return (hr == S_FALSE ? OLE_E_BLANK : hr);
539 /************************************************************************
540 * DataCacheEntry_LoadData
542 * This method will read information for the requested presentation
543 * into the given structure.
546 * This - The entry to load the data from.
549 * This method returns a metafile handle if it is successful.
550 * it will return 0 if not.
552 static HRESULT DataCacheEntry_LoadData(DataCacheEntry *cache_entry)
554 IStream* presStream = NULL;
556 ULARGE_INTEGER current_pos;
559 METAFILEPICT *mfpict;
561 PresentationDataHeader header;
562 CLIPFORMAT clipformat;
563 static const LARGE_INTEGER offset_zero;
566 * Open the presentation stream.
568 hres = DataCacheEntry_OpenPresStream(cache_entry, &presStream);
574 * Get the size of the stream.
576 hres = IStream_Stat(presStream,
584 hres = read_clipformat(presStream, &clipformat);
587 IStream_Release(presStream);
594 sizeof(PresentationDataHeader),
598 IStream_Release(presStream);
602 hres = IStream_Seek(presStream, offset_zero, STREAM_SEEK_CUR, ¤t_pos);
604 streamInfo.cbSize.QuadPart -= current_pos.QuadPart;
606 hmfpict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
609 IStream_Release(presStream);
610 return E_OUTOFMEMORY;
612 mfpict = GlobalLock(hmfpict);
615 * Allocate a buffer for the metafile bits.
617 metafileBits = HeapAlloc(GetProcessHeap(),
619 streamInfo.cbSize.u.LowPart);
622 * Read the metafile bits.
627 streamInfo.cbSize.u.LowPart,
631 * Create a metafile with those bits.
635 /* FIXME: get this from the stream */
636 mfpict->mm = MM_ANISOTROPIC;
637 mfpict->xExt = header.dwObjectExtentX;
638 mfpict->yExt = header.dwObjectExtentY;
639 mfpict->hMF = SetMetaFileBitsEx(streamInfo.cbSize.u.LowPart, metafileBits);
644 GlobalUnlock(hmfpict);
647 cache_entry->data_cf = cache_entry->fmtetc.cfFormat;
648 cache_entry->stgmedium.tymed = TYMED_MFPICT;
649 cache_entry->stgmedium.u.hMetaFilePict = hmfpict;
657 HeapFree(GetProcessHeap(), 0, metafileBits);
658 IStream_Release(presStream);
663 static HRESULT DataCacheEntry_CreateStream(DataCacheEntry *cache_entry,
664 IStorage *storage, IStream **stream)
666 WCHAR wszName[] = {2,'O','l','e','P','r','e','s',
667 '0' + (cache_entry->stream_number / 100) % 10,
668 '0' + (cache_entry->stream_number / 10) % 10,
669 '0' + cache_entry->stream_number % 10, 0};
671 /* FIXME: cache the created stream in This? */
672 return IStorage_CreateStream(storage, wszName,
673 STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
677 static HRESULT DataCacheEntry_Save(DataCacheEntry *cache_entry, IStorage *storage,
680 PresentationDataHeader header;
682 IStream *pres_stream;
685 TRACE("stream_number = %d, fmtetc = %s\n", cache_entry->stream_number, debugstr_formatetc(&cache_entry->fmtetc));
687 hr = DataCacheEntry_CreateStream(cache_entry, storage, &pres_stream);
691 hr = write_clipformat(pres_stream, cache_entry->data_cf);
695 if (cache_entry->fmtetc.ptd)
696 FIXME("ptd not serialized\n");
698 header.dvAspect = cache_entry->fmtetc.dwAspect;
699 header.lindex = cache_entry->fmtetc.lindex;
700 header.tymed = cache_entry->stgmedium.tymed;
702 header.dwObjectExtentX = 0;
703 header.dwObjectExtentY = 0;
707 switch (cache_entry->data_cf)
709 case CF_METAFILEPICT:
711 if (cache_entry->stgmedium.tymed != TYMED_NULL)
713 const METAFILEPICT *mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict);
716 IStream_Release(pres_stream);
717 return DV_E_STGMEDIUM;
719 header.dwObjectExtentX = mfpict->xExt;
720 header.dwObjectExtentY = mfpict->yExt;
721 header.dwSize = GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
722 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
733 hr = IStream_Write(pres_stream, &header, sizeof(PresentationDataHeader),
737 IStream_Release(pres_stream);
742 switch (cache_entry->data_cf)
744 case CF_METAFILEPICT:
746 if (cache_entry->stgmedium.tymed != TYMED_NULL)
748 const METAFILEPICT *mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict);
751 IStream_Release(pres_stream);
752 return DV_E_STGMEDIUM;
754 data = HeapAlloc(GetProcessHeap(), 0, header.dwSize);
755 GetMetaFileBitsEx(mfpict->hMF, header.dwSize, data);
756 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
765 hr = IStream_Write(pres_stream, data, header.dwSize, NULL);
766 HeapFree(GetProcessHeap(), 0, data);
768 IStream_Release(pres_stream);
772 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
773 * does no checking of whether src_stgm has a supported tymed, so this should be
774 * done in the caller */
775 static HRESULT copy_stg_medium(CLIPFORMAT cf, STGMEDIUM *dest_stgm,
776 const STGMEDIUM *src_stgm)
778 if (src_stgm->tymed == TYMED_MFPICT)
780 const METAFILEPICT *src_mfpict = GlobalLock(src_stgm->u.hMetaFilePict);
781 METAFILEPICT *dest_mfpict;
784 return DV_E_STGMEDIUM;
785 dest_stgm->u.hMetaFilePict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
786 dest_mfpict = GlobalLock(dest_stgm->u.hMetaFilePict);
789 GlobalUnlock(src_stgm->u.hMetaFilePict);
790 return E_OUTOFMEMORY;
792 *dest_mfpict = *src_mfpict;
793 dest_mfpict->hMF = CopyMetaFileW(src_mfpict->hMF, NULL);
794 GlobalUnlock(src_stgm->u.hMetaFilePict);
795 GlobalUnlock(dest_stgm->u.hMetaFilePict);
797 else if (src_stgm->tymed != TYMED_NULL)
799 dest_stgm->u.hGlobal = OleDuplicateData(src_stgm->u.hGlobal, cf,
801 if (!dest_stgm->u.hGlobal)
802 return E_OUTOFMEMORY;
804 dest_stgm->tymed = src_stgm->tymed;
805 dest_stgm->pUnkForRelease = src_stgm->pUnkForRelease;
806 if (dest_stgm->pUnkForRelease)
807 IUnknown_AddRef(dest_stgm->pUnkForRelease);
811 static HRESULT DataCacheEntry_SetData(DataCacheEntry *cache_entry,
812 const FORMATETC *formatetc,
813 const STGMEDIUM *stgmedium,
816 if ((!cache_entry->fmtetc.cfFormat && !formatetc->cfFormat) ||
817 (cache_entry->fmtetc.tymed == TYMED_NULL && formatetc->tymed == TYMED_NULL) ||
818 stgmedium->tymed == TYMED_NULL)
820 WARN("invalid formatetc\n");
821 return DV_E_FORMATETC;
824 cache_entry->dirty = TRUE;
825 ReleaseStgMedium(&cache_entry->stgmedium);
826 cache_entry->data_cf = cache_entry->fmtetc.cfFormat ? cache_entry->fmtetc.cfFormat : formatetc->cfFormat;
829 cache_entry->stgmedium = *stgmedium;
833 return copy_stg_medium(cache_entry->data_cf,
834 &cache_entry->stgmedium, stgmedium);
837 static HRESULT DataCacheEntry_GetData(DataCacheEntry *cache_entry, STGMEDIUM *stgmedium)
839 if (stgmedium->tymed == TYMED_NULL && cache_entry->storage)
841 HRESULT hr = DataCacheEntry_LoadData(cache_entry);
845 if (cache_entry->stgmedium.tymed == TYMED_NULL)
847 return copy_stg_medium(cache_entry->data_cf, stgmedium, &cache_entry->stgmedium);
850 static inline HRESULT DataCacheEntry_DiscardData(DataCacheEntry *cache_entry)
852 ReleaseStgMedium(&cache_entry->stgmedium);
853 cache_entry->data_cf = cache_entry->fmtetc.cfFormat;
857 static inline void DataCacheEntry_HandsOffStorage(DataCacheEntry *cache_entry)
859 if (cache_entry->storage)
861 IStorage_Release(cache_entry->storage);
862 cache_entry->storage = NULL;
866 /*********************************************************
867 * Method implementation for the non delegating IUnknown
868 * part of the DataCache class.
871 /************************************************************************
872 * DataCache_NDIUnknown_QueryInterface (IUnknown)
874 * This version of QueryInterface will not delegate its implementation
875 * to the outer unknown.
877 static HRESULT WINAPI DataCache_NDIUnknown_QueryInterface(
882 DataCache *this = impl_from_IUnknown(iface);
889 if (IsEqualIID(&IID_IUnknown, riid))
893 else if (IsEqualIID(&IID_IDataObject, riid))
895 *ppvObject = &this->IDataObject_iface;
897 else if ( IsEqualIID(&IID_IPersistStorage, riid) ||
898 IsEqualIID(&IID_IPersist, riid) )
900 *ppvObject = &this->IPersistStorage_iface;
902 else if ( IsEqualIID(&IID_IViewObject, riid) ||
903 IsEqualIID(&IID_IViewObject2, riid) )
905 *ppvObject = &this->IViewObject2_iface;
907 else if ( IsEqualIID(&IID_IOleCache, riid) ||
908 IsEqualIID(&IID_IOleCache2, riid) )
910 *ppvObject = &this->IOleCache2_iface;
912 else if ( IsEqualIID(&IID_IOleCacheControl, riid) )
914 *ppvObject = &this->IOleCacheControl_iface;
919 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
920 return E_NOINTERFACE;
923 IUnknown_AddRef((IUnknown*)*ppvObject);
928 /************************************************************************
929 * DataCache_NDIUnknown_AddRef (IUnknown)
931 * This version of QueryInterface will not delegate its implementation
932 * to the outer unknown.
934 static ULONG WINAPI DataCache_NDIUnknown_AddRef(
937 DataCache *this = impl_from_IUnknown(iface);
938 return InterlockedIncrement(&this->ref);
941 /************************************************************************
942 * DataCache_NDIUnknown_Release (IUnknown)
944 * This version of QueryInterface will not delegate its implementation
945 * to the outer unknown.
947 static ULONG WINAPI DataCache_NDIUnknown_Release(
950 DataCache *this = impl_from_IUnknown(iface);
953 ref = InterlockedDecrement(&this->ref);
955 if (ref == 0) DataCache_Destroy(this);
960 /*********************************************************
961 * Method implementation for the IDataObject
962 * part of the DataCache class.
965 /************************************************************************
966 * DataCache_IDataObject_QueryInterface (IUnknown)
968 static HRESULT WINAPI DataCache_IDataObject_QueryInterface(
973 DataCache *this = impl_from_IDataObject(iface);
975 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
978 /************************************************************************
979 * DataCache_IDataObject_AddRef (IUnknown)
981 static ULONG WINAPI DataCache_IDataObject_AddRef(
984 DataCache *this = impl_from_IDataObject(iface);
986 return IUnknown_AddRef(this->outerUnknown);
989 /************************************************************************
990 * DataCache_IDataObject_Release (IUnknown)
992 static ULONG WINAPI DataCache_IDataObject_Release(
995 DataCache *this = impl_from_IDataObject(iface);
997 return IUnknown_Release(this->outerUnknown);
1000 /************************************************************************
1003 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1005 static HRESULT WINAPI DataCache_GetData(
1007 LPFORMATETC pformatetcIn,
1010 DataCache *This = impl_from_IDataObject(iface);
1011 DataCacheEntry *cache_entry;
1013 memset(pmedium, 0, sizeof(*pmedium));
1015 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetcIn);
1019 return DataCacheEntry_GetData(cache_entry, pmedium);
1022 static HRESULT WINAPI DataCache_GetDataHere(
1024 LPFORMATETC pformatetc,
1031 static HRESULT WINAPI DataCache_QueryGetData(
1033 LPFORMATETC pformatetc)
1039 /************************************************************************
1040 * DataCache_EnumFormatEtc (IDataObject)
1042 * The data cache doesn't implement this method.
1044 static HRESULT WINAPI DataCache_GetCanonicalFormatEtc(
1046 LPFORMATETC pformatectIn,
1047 LPFORMATETC pformatetcOut)
1053 /************************************************************************
1054 * DataCache_IDataObject_SetData (IDataObject)
1056 * This method is delegated to the IOleCache2 implementation.
1058 static HRESULT WINAPI DataCache_IDataObject_SetData(
1060 LPFORMATETC pformatetc,
1064 IOleCache2* oleCache = NULL;
1067 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1069 hres = IDataObject_QueryInterface(iface, &IID_IOleCache2, (void**)&oleCache);
1072 return E_UNEXPECTED;
1074 hres = IOleCache2_SetData(oleCache, pformatetc, pmedium, fRelease);
1076 IOleCache2_Release(oleCache);
1081 /************************************************************************
1082 * DataCache_EnumFormatEtc (IDataObject)
1084 * The data cache doesn't implement this method.
1086 static HRESULT WINAPI DataCache_EnumFormatEtc(
1089 IEnumFORMATETC** ppenumFormatEtc)
1095 /************************************************************************
1096 * DataCache_DAdvise (IDataObject)
1098 * The data cache doesn't support connections.
1100 static HRESULT WINAPI DataCache_DAdvise(
1102 FORMATETC* pformatetc,
1104 IAdviseSink* pAdvSink,
1105 DWORD* pdwConnection)
1108 return OLE_E_ADVISENOTSUPPORTED;
1111 /************************************************************************
1112 * DataCache_DUnadvise (IDataObject)
1114 * The data cache doesn't support connections.
1116 static HRESULT WINAPI DataCache_DUnadvise(
1121 return OLE_E_NOCONNECTION;
1124 /************************************************************************
1125 * DataCache_EnumDAdvise (IDataObject)
1127 * The data cache doesn't support connections.
1129 static HRESULT WINAPI DataCache_EnumDAdvise(
1131 IEnumSTATDATA** ppenumAdvise)
1134 return OLE_E_ADVISENOTSUPPORTED;
1137 /*********************************************************
1138 * Method implementation for the IDataObject
1139 * part of the DataCache class.
1142 /************************************************************************
1143 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1145 static HRESULT WINAPI DataCache_IPersistStorage_QueryInterface(
1146 IPersistStorage* iface,
1150 DataCache *this = impl_from_IPersistStorage(iface);
1152 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1155 /************************************************************************
1156 * DataCache_IPersistStorage_AddRef (IUnknown)
1158 static ULONG WINAPI DataCache_IPersistStorage_AddRef(
1159 IPersistStorage* iface)
1161 DataCache *this = impl_from_IPersistStorage(iface);
1163 return IUnknown_AddRef(this->outerUnknown);
1166 /************************************************************************
1167 * DataCache_IPersistStorage_Release (IUnknown)
1169 static ULONG WINAPI DataCache_IPersistStorage_Release(
1170 IPersistStorage* iface)
1172 DataCache *this = impl_from_IPersistStorage(iface);
1174 return IUnknown_Release(this->outerUnknown);
1177 /************************************************************************
1178 * DataCache_GetClassID (IPersistStorage)
1180 * The data cache doesn't implement this method.
1182 static HRESULT WINAPI DataCache_GetClassID(
1183 IPersistStorage* iface,
1186 DataCache *This = impl_from_IPersistStorage(iface);
1187 DataCacheEntry *cache_entry;
1189 TRACE("(%p, %p)\n", iface, pClassID);
1191 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1193 if (cache_entry->storage != NULL)
1196 HRESULT hr = IStorage_Stat(cache_entry->storage, &statstg, STATFLAG_NONAME);
1199 *pClassID = statstg.clsid;
1205 *pClassID = CLSID_NULL;
1210 /************************************************************************
1211 * DataCache_IsDirty (IPersistStorage)
1213 static HRESULT WINAPI DataCache_IsDirty(
1214 IPersistStorage* iface)
1216 DataCache *This = impl_from_IPersistStorage(iface);
1217 DataCacheEntry *cache_entry;
1219 TRACE("(%p)\n", iface);
1224 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1225 if (cache_entry->dirty)
1231 /************************************************************************
1232 * DataCache_InitNew (IPersistStorage)
1234 * The data cache implementation of IPersistStorage_InitNew simply stores
1235 * the storage pointer.
1237 static HRESULT WINAPI DataCache_InitNew(
1238 IPersistStorage* iface,
1241 DataCache *This = impl_from_IPersistStorage(iface);
1243 TRACE("(%p, %p)\n", iface, pStg);
1245 if (This->presentationStorage != NULL)
1246 IStorage_Release(This->presentationStorage);
1248 This->presentationStorage = pStg;
1250 IStorage_AddRef(This->presentationStorage);
1256 /************************************************************************
1257 * DataCache_Load (IPersistStorage)
1259 * The data cache implementation of IPersistStorage_Load doesn't
1260 * actually load anything. Instead, it holds on to the storage pointer
1261 * and it will load the presentation information when the
1262 * IDataObject_GetData or IViewObject2_Draw methods are called.
1264 static HRESULT WINAPI DataCache_Load(
1265 IPersistStorage* iface,
1268 DataCache *This = impl_from_IPersistStorage(iface);
1270 IEnumSTATSTG *pEnum;
1273 TRACE("(%p, %p)\n", iface, pStg);
1275 if (This->presentationStorage != NULL)
1276 IStorage_Release(This->presentationStorage);
1278 This->presentationStorage = pStg;
1280 hr = IStorage_EnumElements(pStg, 0, NULL, 0, &pEnum);
1281 if (FAILED(hr)) return hr;
1283 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
1285 if (DataCache_IsPresentationStream(&elem))
1289 hr = IStorage_OpenStream(This->presentationStorage, elem.pwcsName,
1290 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
1294 PresentationDataHeader header;
1296 CLIPFORMAT clipformat;
1298 hr = read_clipformat(pStm, &clipformat);
1301 hr = IStream_Read(pStm, &header, sizeof(header),
1304 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1305 if (hr == S_OK && actual_read == sizeof(header))
1307 DataCacheEntry *cache_entry;
1310 fmtetc.cfFormat = clipformat;
1311 fmtetc.ptd = NULL; /* FIXME */
1312 fmtetc.dwAspect = header.dvAspect;
1313 fmtetc.lindex = header.lindex;
1314 fmtetc.tymed = header.tymed;
1316 TRACE("loading entry with formatetc: %s\n", debugstr_formatetc(&fmtetc));
1318 cache_entry = DataCache_GetEntryForFormatEtc(This, &fmtetc);
1320 hr = DataCache_CreateEntry(This, &fmtetc, &cache_entry);
1323 DataCacheEntry_DiscardData(cache_entry);
1324 if (cache_entry->storage) IStorage_Release(cache_entry->storage);
1325 cache_entry->storage = pStg;
1326 IStorage_AddRef(pStg);
1327 cache_entry->dirty = FALSE;
1331 IStream_Release(pStm);
1335 CoTaskMemFree(elem.pwcsName);
1338 This->dirty = FALSE;
1340 IEnumSTATSTG_Release(pEnum);
1342 IStorage_AddRef(This->presentationStorage);
1346 /************************************************************************
1347 * DataCache_Save (IPersistStorage)
1349 * Until we actually connect to a running object and retrieve new
1350 * information to it, we never have to save anything. However, it is
1351 * our responsibility to copy the information when saving to a new
1354 static HRESULT WINAPI DataCache_Save(
1355 IPersistStorage* iface,
1359 DataCache *This = impl_from_IPersistStorage(iface);
1360 DataCacheEntry *cache_entry;
1363 unsigned short stream_number = 0;
1365 TRACE("(%p, %p, %d)\n", iface, pStg, fSameAsLoad);
1367 dirty = This->dirty;
1370 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1372 dirty = cache_entry->dirty;
1378 /* this is a shortcut if nothing changed */
1379 if (!dirty && !fSameAsLoad && This->presentationStorage)
1381 return IStorage_CopyTo(This->presentationStorage, 0, NULL, NULL, pStg);
1384 /* assign stream numbers to the cache entries */
1385 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1387 if (cache_entry->stream_number != stream_number)
1389 cache_entry->dirty = TRUE; /* needs to be written out again */
1390 cache_entry->stream_number = stream_number;
1395 /* write out the cache entries */
1396 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1398 if (!fSameAsLoad || cache_entry->dirty)
1400 hr = DataCacheEntry_Save(cache_entry, pStg, fSameAsLoad);
1404 cache_entry->dirty = FALSE;
1408 This->dirty = FALSE;
1412 /************************************************************************
1413 * DataCache_SaveCompleted (IPersistStorage)
1415 * This method is called to tell the cache to release the storage
1416 * pointer it's currently holding.
1418 static HRESULT WINAPI DataCache_SaveCompleted(
1419 IPersistStorage* iface,
1422 TRACE("(%p, %p)\n", iface, pStgNew);
1426 IPersistStorage_HandsOffStorage(iface);
1428 DataCache_Load(iface, pStgNew);
1434 /************************************************************************
1435 * DataCache_HandsOffStorage (IPersistStorage)
1437 * This method is called to tell the cache to release the storage
1438 * pointer it's currently holding.
1440 static HRESULT WINAPI DataCache_HandsOffStorage(
1441 IPersistStorage* iface)
1443 DataCache *this = impl_from_IPersistStorage(iface);
1444 DataCacheEntry *cache_entry;
1446 TRACE("(%p)\n", iface);
1448 if (this->presentationStorage != NULL)
1450 IStorage_Release(this->presentationStorage);
1451 this->presentationStorage = NULL;
1454 LIST_FOR_EACH_ENTRY(cache_entry, &this->cache_list, DataCacheEntry, entry)
1455 DataCacheEntry_HandsOffStorage(cache_entry);
1460 /*********************************************************
1461 * Method implementation for the IViewObject2
1462 * part of the DataCache class.
1465 /************************************************************************
1466 * DataCache_IViewObject2_QueryInterface (IUnknown)
1468 static HRESULT WINAPI DataCache_IViewObject2_QueryInterface(
1469 IViewObject2* iface,
1473 DataCache *this = impl_from_IViewObject2(iface);
1475 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1478 /************************************************************************
1479 * DataCache_IViewObject2_AddRef (IUnknown)
1481 static ULONG WINAPI DataCache_IViewObject2_AddRef(
1482 IViewObject2* iface)
1484 DataCache *this = impl_from_IViewObject2(iface);
1486 return IUnknown_AddRef(this->outerUnknown);
1489 /************************************************************************
1490 * DataCache_IViewObject2_Release (IUnknown)
1492 static ULONG WINAPI DataCache_IViewObject2_Release(
1493 IViewObject2* iface)
1495 DataCache *this = impl_from_IViewObject2(iface);
1497 return IUnknown_Release(this->outerUnknown);
1500 /************************************************************************
1501 * DataCache_Draw (IViewObject2)
1503 * This method will draw the cached representation of the object
1504 * to the given device context.
1506 static HRESULT WINAPI DataCache_Draw(
1507 IViewObject2* iface,
1511 DVTARGETDEVICE* ptd,
1514 LPCRECTL lprcBounds,
1515 LPCRECTL lprcWBounds,
1516 BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue),
1517 ULONG_PTR dwContinue)
1519 DataCache *This = impl_from_IViewObject2(iface);
1521 DataCacheEntry *cache_entry;
1523 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1535 if (lprcBounds==NULL)
1536 return E_INVALIDARG;
1538 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1540 /* FIXME: compare ptd too */
1541 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1542 (cache_entry->fmtetc.lindex != lindex))
1545 /* if the data hasn't been loaded yet, do it now */
1546 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1548 hres = DataCacheEntry_LoadData(cache_entry);
1554 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1557 if (pfnContinue && !pfnContinue(dwContinue)) return E_ABORT;
1559 switch (cache_entry->data_cf)
1561 case CF_METAFILEPICT:
1564 * We have to be careful not to modify the state of the
1569 SIZE oldViewportExt;
1570 POINT oldViewportOrg;
1571 METAFILEPICT *mfpict;
1573 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1574 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1577 prevMapMode = SetMapMode(hdcDraw, mfpict->mm);
1579 SetWindowExtEx(hdcDraw,
1584 SetViewportExtEx(hdcDraw,
1585 lprcBounds->right - lprcBounds->left,
1586 lprcBounds->bottom - lprcBounds->top,
1589 SetViewportOrgEx(hdcDraw,
1594 PlayMetaFile(hdcDraw, mfpict->hMF);
1596 SetWindowExtEx(hdcDraw,
1601 SetViewportExtEx(hdcDraw,
1606 SetViewportOrgEx(hdcDraw,
1611 SetMapMode(hdcDraw, prevMapMode);
1613 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1620 WARN("no data could be found to be drawn\n");
1625 static HRESULT WINAPI DataCache_GetColorSet(
1626 IViewObject2* iface,
1630 DVTARGETDEVICE* ptd,
1631 HDC hicTargetDevice,
1632 LOGPALETTE** ppColorSet)
1638 static HRESULT WINAPI DataCache_Freeze(
1639 IViewObject2* iface,
1649 static HRESULT WINAPI DataCache_Unfreeze(
1650 IViewObject2* iface,
1657 /************************************************************************
1658 * DataCache_SetAdvise (IViewObject2)
1660 * This sets-up an advisory sink with the data cache. When the object's
1661 * view changes, this sink is called.
1663 static HRESULT WINAPI DataCache_SetAdvise(
1664 IViewObject2* iface,
1667 IAdviseSink* pAdvSink)
1669 DataCache *this = impl_from_IViewObject2(iface);
1671 TRACE("(%p, %x, %x, %p)\n", iface, aspects, advf, pAdvSink);
1674 * A call to this function removes the previous sink
1676 if (this->sinkInterface != NULL)
1678 IAdviseSink_Release(this->sinkInterface);
1679 this->sinkInterface = NULL;
1680 this->sinkAspects = 0;
1681 this->sinkAdviseFlag = 0;
1685 * Now, setup the new one.
1689 this->sinkInterface = pAdvSink;
1690 this->sinkAspects = aspects;
1691 this->sinkAdviseFlag = advf;
1693 IAdviseSink_AddRef(this->sinkInterface);
1697 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1700 if (advf & ADVF_PRIMEFIRST)
1702 DataCache_FireOnViewChange(this, aspects, -1);
1708 /************************************************************************
1709 * DataCache_GetAdvise (IViewObject2)
1711 * This method queries the current state of the advise sink
1712 * installed on the data cache.
1714 static HRESULT WINAPI DataCache_GetAdvise(
1715 IViewObject2* iface,
1718 IAdviseSink** ppAdvSink)
1720 DataCache *this = impl_from_IViewObject2(iface);
1722 TRACE("(%p, %p, %p, %p)\n", iface, pAspects, pAdvf, ppAdvSink);
1725 * Just copy all the requested values.
1728 *pAspects = this->sinkAspects;
1731 *pAdvf = this->sinkAdviseFlag;
1733 if (ppAdvSink!=NULL)
1735 if (this->sinkInterface != NULL)
1736 IAdviseSink_QueryInterface(this->sinkInterface,
1739 else *ppAdvSink = NULL;
1745 /************************************************************************
1746 * DataCache_GetExtent (IViewObject2)
1748 * This method retrieves the "natural" size of this cached object.
1750 static HRESULT WINAPI DataCache_GetExtent(
1751 IViewObject2* iface,
1754 DVTARGETDEVICE* ptd,
1757 DataCache *This = impl_from_IViewObject2(iface);
1758 HRESULT hres = E_FAIL;
1759 DataCacheEntry *cache_entry;
1761 TRACE("(%p, %x, %d, %p, %p)\n",
1762 iface, dwDrawAspect, lindex, ptd, lpsizel);
1771 FIXME("Unimplemented flag lindex = %d\n", lindex);
1774 * Right now, we support only the callback from
1775 * the default handler.
1778 FIXME("Unimplemented ptd = %p\n", ptd);
1780 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1782 /* FIXME: compare ptd too */
1783 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1784 (cache_entry->fmtetc.lindex != lindex))
1787 /* if the data hasn't been loaded yet, do it now */
1788 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1790 hres = DataCacheEntry_LoadData(cache_entry);
1796 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1800 switch (cache_entry->data_cf)
1802 case CF_METAFILEPICT:
1804 METAFILEPICT *mfpict;
1806 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1807 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1810 lpsizel->cx = mfpict->xExt;
1811 lpsizel->cy = mfpict->yExt;
1813 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1820 WARN("no data could be found to get the extents from\n");
1823 * This method returns OLE_E_BLANK when it fails.
1829 /*********************************************************
1830 * Method implementation for the IOleCache2
1831 * part of the DataCache class.
1834 /************************************************************************
1835 * DataCache_IOleCache2_QueryInterface (IUnknown)
1837 static HRESULT WINAPI DataCache_IOleCache2_QueryInterface(
1842 DataCache *this = impl_from_IOleCache2(iface);
1844 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1847 /************************************************************************
1848 * DataCache_IOleCache2_AddRef (IUnknown)
1850 static ULONG WINAPI DataCache_IOleCache2_AddRef(
1853 DataCache *this = impl_from_IOleCache2(iface);
1855 return IUnknown_AddRef(this->outerUnknown);
1858 /************************************************************************
1859 * DataCache_IOleCache2_Release (IUnknown)
1861 static ULONG WINAPI DataCache_IOleCache2_Release(
1864 DataCache *this = impl_from_IOleCache2(iface);
1866 return IUnknown_Release(this->outerUnknown);
1869 /*****************************************************************************
1872 * Set up the sink connection to the running object.
1874 static HRESULT setup_sink(DataCache *This, DataCacheEntry *cache_entry)
1876 HRESULT hr = S_FALSE;
1879 /* Clear the ADVFCACHE_* bits. Native also sets the two highest bits for some reason. */
1880 flags = cache_entry->advise_flags & ~(ADVFCACHE_NOHANDLER | ADVFCACHE_FORCEBUILTIN | ADVFCACHE_ONSAVE);
1882 if(This->running_object)
1883 if(!(flags & ADVF_NODATA))
1884 hr = IDataObject_DAdvise(This->running_object, &cache_entry->fmtetc, flags,
1885 &This->IAdviseSink_iface, &cache_entry->sink_id);
1889 static HRESULT WINAPI DataCache_Cache(
1891 FORMATETC* pformatetc,
1893 DWORD* pdwConnection)
1895 DataCache *This = impl_from_IOleCache2(iface);
1896 DataCacheEntry *cache_entry;
1899 TRACE("(%p, 0x%x, %p)\n", pformatetc, advf, pdwConnection);
1901 if (!pformatetc || !pdwConnection)
1902 return E_INVALIDARG;
1904 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc));
1908 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1911 TRACE("found an existing cache entry\n");
1912 *pdwConnection = cache_entry->id;
1913 return CACHE_S_SAMECACHE;
1916 hr = DataCache_CreateEntry(This, pformatetc, &cache_entry);
1920 *pdwConnection = cache_entry->id;
1921 cache_entry->advise_flags = advf;
1922 setup_sink(This, cache_entry);
1928 static HRESULT WINAPI DataCache_Uncache(
1932 DataCache *This = impl_from_IOleCache2(iface);
1933 DataCacheEntry *cache_entry;
1935 TRACE("(%d)\n", dwConnection);
1937 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1938 if (cache_entry->id == dwConnection)
1940 DataCacheEntry_Destroy(This, cache_entry);
1944 WARN("no connection found for %d\n", dwConnection);
1946 return OLE_E_NOCONNECTION;
1949 static HRESULT WINAPI DataCache_EnumCache(
1951 IEnumSTATDATA** ppenumSTATDATA)
1957 static HRESULT WINAPI DataCache_InitCache(
1959 IDataObject* pDataObject)
1965 static HRESULT WINAPI DataCache_IOleCache2_SetData(
1967 FORMATETC* pformatetc,
1971 DataCache *This = impl_from_IOleCache2(iface);
1972 DataCacheEntry *cache_entry;
1975 TRACE("(%p, %p, %s)\n", pformatetc, pmedium, fRelease ? "TRUE" : "FALSE");
1976 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc));
1978 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1981 hr = DataCacheEntry_SetData(cache_entry, pformatetc, pmedium, fRelease);
1984 DataCache_FireOnViewChange(This, cache_entry->fmtetc.dwAspect,
1985 cache_entry->fmtetc.lindex);
1989 WARN("cache entry not found\n");
1994 static HRESULT WINAPI DataCache_UpdateCache(
1996 LPDATAOBJECT pDataObject,
2000 FIXME("(%p, 0x%x, %p): stub\n", pDataObject, grfUpdf, pReserved);
2004 static HRESULT WINAPI DataCache_DiscardCache(
2006 DWORD dwDiscardOptions)
2008 DataCache *This = impl_from_IOleCache2(iface);
2009 DataCacheEntry *cache_entry;
2012 TRACE("(%d)\n", dwDiscardOptions);
2014 if (dwDiscardOptions == DISCARDCACHE_SAVEIFDIRTY)
2015 hr = DataCache_Save(&This->IPersistStorage_iface, This->presentationStorage, TRUE);
2017 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2019 hr = DataCacheEntry_DiscardData(cache_entry);
2028 /*********************************************************
2029 * Method implementation for the IOleCacheControl
2030 * part of the DataCache class.
2033 /************************************************************************
2034 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2036 static HRESULT WINAPI DataCache_IOleCacheControl_QueryInterface(
2037 IOleCacheControl* iface,
2041 DataCache *this = impl_from_IOleCacheControl(iface);
2043 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
2046 /************************************************************************
2047 * DataCache_IOleCacheControl_AddRef (IUnknown)
2049 static ULONG WINAPI DataCache_IOleCacheControl_AddRef(
2050 IOleCacheControl* iface)
2052 DataCache *this = impl_from_IOleCacheControl(iface);
2054 return IUnknown_AddRef(this->outerUnknown);
2057 /************************************************************************
2058 * DataCache_IOleCacheControl_Release (IUnknown)
2060 static ULONG WINAPI DataCache_IOleCacheControl_Release(
2061 IOleCacheControl* iface)
2063 DataCache *this = impl_from_IOleCacheControl(iface);
2065 return IUnknown_Release(this->outerUnknown);
2068 /************************************************************************
2069 * DataCache_OnRun (IOleCacheControl)
2071 static HRESULT WINAPI DataCache_OnRun(IOleCacheControl* iface, IDataObject *data_obj)
2073 DataCache *This = impl_from_IOleCacheControl(iface);
2074 DataCacheEntry *cache_entry;
2076 TRACE("(%p)->(%p)\n", iface, data_obj);
2078 if(This->running_object) return S_OK;
2080 /* No reference is taken on the data object */
2081 This->running_object = data_obj;
2083 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2085 setup_sink(This, cache_entry);
2091 /************************************************************************
2092 * DataCache_OnStop (IOleCacheControl)
2094 static HRESULT WINAPI DataCache_OnStop(IOleCacheControl* iface)
2096 DataCache *This = impl_from_IOleCacheControl(iface);
2097 DataCacheEntry *cache_entry;
2099 TRACE("(%p)\n", iface);
2101 if(!This->running_object) return S_OK;
2103 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
2105 if(cache_entry->sink_id)
2107 IDataObject_DUnadvise(This->running_object, cache_entry->sink_id);
2108 cache_entry->sink_id = 0;
2112 /* No ref taken in OnRun, so no Release call here */
2113 This->running_object = NULL;
2117 /************************************************************************
2118 * IAdviseSink methods.
2119 * This behaves as an internal object to the data cache. QI'ing its ptr doesn't
2120 * give access to the cache's other interfaces. We don't maintain a ref count,
2121 * the object exists as long as the cache is around.
2123 static HRESULT WINAPI DataCache_IAdviseSink_QueryInterface(IAdviseSink *iface, REFIID iid, void **obj)
2126 if (IsEqualIID(&IID_IUnknown, iid) ||
2127 IsEqualIID(&IID_IAdviseSink, iid))
2134 IAdviseSink_AddRef(iface);
2137 return E_NOINTERFACE;
2140 static ULONG WINAPI DataCache_IAdviseSink_AddRef(IAdviseSink *iface)
2145 static ULONG WINAPI DataCache_IAdviseSink_Release(IAdviseSink *iface)
2150 static void WINAPI DataCache_OnDataChange(IAdviseSink *iface, FORMATETC *fmt, STGMEDIUM *med)
2152 DataCache *This = impl_from_IAdviseSink(iface);
2153 TRACE("(%p)->(%s, %p)\n", This, debugstr_formatetc(fmt), med);
2154 IOleCache2_SetData(&This->IOleCache2_iface, fmt, med, FALSE);
2157 static void WINAPI DataCache_OnViewChange(IAdviseSink *iface, DWORD aspect, LONG index)
2162 static void WINAPI DataCache_OnRename(IAdviseSink *iface, IMoniker *mk)
2167 static void WINAPI DataCache_OnSave(IAdviseSink *iface)
2172 static void WINAPI DataCache_OnClose(IAdviseSink *iface)
2178 * Virtual function tables for the DataCache class.
2180 static const IUnknownVtbl DataCache_NDIUnknown_VTable =
2182 DataCache_NDIUnknown_QueryInterface,
2183 DataCache_NDIUnknown_AddRef,
2184 DataCache_NDIUnknown_Release
2187 static const IDataObjectVtbl DataCache_IDataObject_VTable =
2189 DataCache_IDataObject_QueryInterface,
2190 DataCache_IDataObject_AddRef,
2191 DataCache_IDataObject_Release,
2193 DataCache_GetDataHere,
2194 DataCache_QueryGetData,
2195 DataCache_GetCanonicalFormatEtc,
2196 DataCache_IDataObject_SetData,
2197 DataCache_EnumFormatEtc,
2199 DataCache_DUnadvise,
2200 DataCache_EnumDAdvise
2203 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable =
2205 DataCache_IPersistStorage_QueryInterface,
2206 DataCache_IPersistStorage_AddRef,
2207 DataCache_IPersistStorage_Release,
2208 DataCache_GetClassID,
2213 DataCache_SaveCompleted,
2214 DataCache_HandsOffStorage
2217 static const IViewObject2Vtbl DataCache_IViewObject2_VTable =
2219 DataCache_IViewObject2_QueryInterface,
2220 DataCache_IViewObject2_AddRef,
2221 DataCache_IViewObject2_Release,
2223 DataCache_GetColorSet,
2226 DataCache_SetAdvise,
2227 DataCache_GetAdvise,
2231 static const IOleCache2Vtbl DataCache_IOleCache2_VTable =
2233 DataCache_IOleCache2_QueryInterface,
2234 DataCache_IOleCache2_AddRef,
2235 DataCache_IOleCache2_Release,
2238 DataCache_EnumCache,
2239 DataCache_InitCache,
2240 DataCache_IOleCache2_SetData,
2241 DataCache_UpdateCache,
2242 DataCache_DiscardCache
2245 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable =
2247 DataCache_IOleCacheControl_QueryInterface,
2248 DataCache_IOleCacheControl_AddRef,
2249 DataCache_IOleCacheControl_Release,
2254 static const IAdviseSinkVtbl DataCache_IAdviseSink_VTable =
2256 DataCache_IAdviseSink_QueryInterface,
2257 DataCache_IAdviseSink_AddRef,
2258 DataCache_IAdviseSink_Release,
2259 DataCache_OnDataChange,
2260 DataCache_OnViewChange,
2266 /*********************************************************
2267 * Method implementation for DataCache class.
2269 static DataCache* DataCache_Construct(
2271 LPUNKNOWN pUnkOuter)
2273 DataCache* newObject = 0;
2276 * Allocate space for the object.
2278 newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache));
2284 * Initialize the virtual function table.
2286 newObject->IDataObject_iface.lpVtbl = &DataCache_IDataObject_VTable;
2287 newObject->IUnknown_iface.lpVtbl = &DataCache_NDIUnknown_VTable;
2288 newObject->IPersistStorage_iface.lpVtbl = &DataCache_IPersistStorage_VTable;
2289 newObject->IViewObject2_iface.lpVtbl = &DataCache_IViewObject2_VTable;
2290 newObject->IOleCache2_iface.lpVtbl = &DataCache_IOleCache2_VTable;
2291 newObject->IOleCacheControl_iface.lpVtbl = &DataCache_IOleCacheControl_VTable;
2292 newObject->IAdviseSink_iface.lpVtbl = &DataCache_IAdviseSink_VTable;
2295 * Start with one reference count. The caller of this function
2296 * must release the interface pointer when it is done.
2301 * Initialize the outer unknown
2302 * We don't keep a reference on the outer unknown since, the way
2303 * aggregation works, our lifetime is at least as large as its
2306 if (pUnkOuter==NULL)
2307 pUnkOuter = &newObject->IUnknown_iface;
2309 newObject->outerUnknown = pUnkOuter;
2312 * Initialize the other members of the structure.
2314 newObject->sinkAspects = 0;
2315 newObject->sinkAdviseFlag = 0;
2316 newObject->sinkInterface = 0;
2317 newObject->presentationStorage = NULL;
2318 list_init(&newObject->cache_list);
2319 newObject->last_cache_id = 1;
2320 newObject->dirty = FALSE;
2321 newObject->running_object = NULL;
2326 /******************************************************************************
2327 * CreateDataCache [OLE32.@]
2329 * Creates a data cache to allow an object to render one or more of its views,
2330 * whether running or not.
2333 * pUnkOuter [I] Outer unknown for the object.
2335 * riid [I] IID of interface to return.
2336 * ppvObj [O] Address where the data cache object will be stored on return.
2340 * Failure: HRESULT code.
2343 * The following interfaces are supported by the returned data cache object:
2344 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorage, IDataObject,
2345 * IViewObject and IViewObject2.
2347 HRESULT WINAPI CreateDataCache(
2348 LPUNKNOWN pUnkOuter,
2353 DataCache* newCache = NULL;
2356 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid), pUnkOuter, debugstr_guid(riid), ppvObj);
2367 * If this cache is constructed for aggregation, make sure
2368 * the caller is requesting the IUnknown interface.
2369 * This is necessary because it's the only time the non-delegating
2370 * IUnknown pointer can be returned to the outside.
2372 if ( pUnkOuter && !IsEqualIID(&IID_IUnknown, riid) )
2373 return CLASS_E_NOAGGREGATION;
2376 * Try to construct a new instance of the class.
2378 newCache = DataCache_Construct(rclsid,
2382 return E_OUTOFMEMORY;
2385 * Make sure it supports the interface required by the caller.
2387 hr = IUnknown_QueryInterface(&newCache->IUnknown_iface, riid, ppvObj);
2390 * Release the reference obtained in the constructor. If
2391 * the QueryInterface was unsuccessful, it will free the class.
2393 IUnknown_Release(&newCache->IUnknown_iface);