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 strorage.
71 * Most fields are still unknown.
73 typedef struct PresentationDataHeader
75 DWORD unknown1; /* -1 */
77 DWORD unknown3; /* 4, possibly TYMED_ISTREAM */
81 DWORD unknown7; /* 0 */
82 DWORD dwObjectExtentX;
83 DWORD dwObjectExtentY;
85 } PresentationDataHeader;
87 typedef struct DataCacheEntry
90 /* format of this entry */
92 /* the clipboard format of the data */
97 * This storage pointer is set through a call to
98 * IPersistStorage_Load. This is where the visual
99 * representation of the object is stored.
106 /* stream number (-1 if not set ) */
107 unsigned short stream_number;
110 /****************************************************************************
116 * List all interface VTables here
118 const IDataObjectVtbl* lpVtbl;
119 const IUnknownVtbl* lpvtblNDIUnknown;
120 const IPersistStorageVtbl* lpvtblIPersistStorage;
121 const IViewObject2Vtbl* lpvtblIViewObject;
122 const IOleCache2Vtbl* lpvtblIOleCache2;
123 const IOleCacheControlVtbl* lpvtblIOleCacheControl;
126 * Reference count of this object
131 * IUnknown implementation of the outer object.
133 IUnknown* outerUnknown;
136 * The user of this object can setup ONE advise sink
137 * connection with the object. These parameters describe
141 DWORD sinkAdviseFlag;
142 IAdviseSink* sinkInterface;
143 IStorage *presentationStorage;
145 /* list of cache entries */
146 struct list cache_list;
147 /* last id assigned to an entry */
153 typedef struct DataCache DataCache;
156 * Here, I define utility macros to help with the casting of the
158 * There is a version to accommodate all of the VTables implemented
162 static inline DataCache *impl_from_IDataObject( IDataObject *iface )
164 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpVtbl));
167 static inline DataCache *impl_from_NDIUnknown( IUnknown *iface )
169 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblNDIUnknown));
172 static inline DataCache *impl_from_IPersistStorage( IPersistStorage *iface )
174 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIPersistStorage));
177 static inline DataCache *impl_from_IViewObject2( IViewObject2 *iface )
179 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIViewObject));
182 static inline DataCache *impl_from_IOleCache2( IOleCache2 *iface )
184 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCache2));
187 static inline DataCache *impl_from_IOleCacheControl( IOleCacheControl *iface )
189 return (DataCache *)((char*)iface - FIELD_OFFSET(DataCache, lpvtblIOleCacheControl));
192 static void dump_FORMATETC(const FORMATETC *formatetc)
194 TRACE("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
195 formatetc->cfFormat, formatetc->ptd, formatetc->dwAspect,
196 formatetc->lindex, formatetc->tymed);
200 * Prototypes for the methods of the DataCache class.
202 static DataCache* DataCache_Construct(REFCLSID clsid,
203 LPUNKNOWN pUnkOuter);
204 static HRESULT DataCacheEntry_OpenPresStream(DataCacheEntry *This,
207 static void DataCacheEntry_Destroy(DataCacheEntry *This)
209 list_remove(&This->entry);
211 IStorage_Release(This->storage);
212 HeapFree(GetProcessHeap(), 0, This->fmtetc.ptd);
213 ReleaseStgMedium(&This->stgmedium);
214 HeapFree(GetProcessHeap(), 0, This);
217 static void DataCache_Destroy(
218 DataCache* ptrToDestroy)
220 DataCacheEntry *cache_entry, *next_cache_entry;
224 if (ptrToDestroy->sinkInterface != NULL)
226 IAdviseSink_Release(ptrToDestroy->sinkInterface);
227 ptrToDestroy->sinkInterface = NULL;
230 LIST_FOR_EACH_ENTRY_SAFE(cache_entry, next_cache_entry, &ptrToDestroy->cache_list, DataCacheEntry, entry)
231 DataCacheEntry_Destroy(cache_entry);
234 * Free the datacache pointer.
236 HeapFree(GetProcessHeap(), 0, ptrToDestroy);
239 static DataCacheEntry *DataCache_GetEntryForFormatEtc(DataCache *This, const FORMATETC *formatetc)
241 DataCacheEntry *cache_entry;
242 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
244 /* FIXME: also compare DVTARGETDEVICEs */
245 if ((!cache_entry->fmtetc.cfFormat || !formatetc->cfFormat || (formatetc->cfFormat == cache_entry->fmtetc.cfFormat)) &&
246 (formatetc->dwAspect == cache_entry->fmtetc.dwAspect) &&
247 (formatetc->lindex == cache_entry->fmtetc.lindex) &&
248 (!cache_entry->fmtetc.tymed || !formatetc->tymed || (formatetc->tymed == cache_entry->fmtetc.tymed)))
254 /* checks that the clipformat and tymed are valid and returns an error if they
255 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
257 static HRESULT check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat, DWORD tymed)
259 if (!cfFormat || !tymed ||
260 (cfFormat == CF_METAFILEPICT && tymed == TYMED_MFPICT) ||
261 (cfFormat == CF_BITMAP && tymed == TYMED_GDI) ||
262 (cfFormat == CF_DIB && tymed == TYMED_HGLOBAL) ||
263 (cfFormat == CF_ENHMETAFILE && tymed == TYMED_ENHMF))
265 else if (tymed == TYMED_HGLOBAL)
266 return CACHE_S_FORMATETC_NOTSUPPORTED;
269 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat, tymed);
274 static HRESULT DataCache_CreateEntry(DataCache *This, const FORMATETC *formatetc, DataCacheEntry **cache_entry)
278 hr = check_valid_clipformat_and_tymed(formatetc->cfFormat, formatetc->tymed);
281 if (hr == CACHE_S_FORMATETC_NOTSUPPORTED)
282 TRACE("creating unsupported format %d\n", formatetc->cfFormat);
284 *cache_entry = HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry));
286 return E_OUTOFMEMORY;
288 (*cache_entry)->fmtetc = *formatetc;
291 (*cache_entry)->fmtetc.ptd = HeapAlloc(GetProcessHeap(), 0, formatetc->ptd->tdSize);
292 memcpy((*cache_entry)->fmtetc.ptd, formatetc->ptd, formatetc->ptd->tdSize);
294 (*cache_entry)->stgmedium.tymed = TYMED_NULL;
295 (*cache_entry)->stgmedium.pUnkForRelease = NULL;
296 (*cache_entry)->storage = NULL;
297 (*cache_entry)->id = This->last_cache_id++;
298 (*cache_entry)->dirty = TRUE;
299 (*cache_entry)->stream_number = -1;
300 list_add_tail(&This->cache_list, &(*cache_entry)->entry);
304 /************************************************************************
305 * DataCache_FireOnViewChange
307 * This method will fire an OnViewChange notification to the advise
308 * sink registered with the datacache.
310 * See IAdviseSink::OnViewChange for more details.
312 static void DataCache_FireOnViewChange(
317 TRACE("(%p, %x, %d)\n", this, aspect, lindex);
320 * The sink supplies a filter when it registers
321 * we make sure we only send the notifications when that
324 if ((this->sinkAspects & aspect) != 0)
326 if (this->sinkInterface != NULL)
328 IAdviseSink_OnViewChange(this->sinkInterface,
333 * Some sinks want to be unregistered automatically when
334 * the first notification goes out.
336 if ( (this->sinkAdviseFlag & ADVF_ONLYONCE) != 0)
338 IAdviseSink_Release(this->sinkInterface);
340 this->sinkInterface = NULL;
341 this->sinkAspects = 0;
342 this->sinkAdviseFlag = 0;
348 /* Helper for DataCacheEntry_OpenPresStream */
349 static BOOL DataCache_IsPresentationStream(const STATSTG *elem)
351 /* The presentation streams have names of the form "\002OlePresXXX",
352 * where XXX goes from 000 to 999. */
353 static const WCHAR OlePres[] = { 2,'O','l','e','P','r','e','s' };
355 LPCWSTR name = elem->pwcsName;
357 return (elem->type == STGTY_STREAM)
358 && (elem->cbSize.u.LowPart >= sizeof(PresentationDataHeader))
359 && (strlenW(name) == 11)
360 && (strncmpW(name, OlePres, 8) == 0)
361 && (name[8] >= '0') && (name[8] <= '9')
362 && (name[9] >= '0') && (name[9] <= '9')
363 && (name[10] >= '0') && (name[10] <= '9');
366 /************************************************************************
367 * DataCacheEntry_OpenPresStream
369 * This method will find the stream for the given presentation. It makes
370 * no attempt at fallback.
373 * this - Pointer to the DataCache object
374 * drawAspect - The aspect of the object that we wish to draw.
375 * pStm - A returned stream. It points to the beginning of the
376 * - presentation data, including the header.
379 * S_OK The requested stream has been opened.
380 * OLE_E_BLANK The requested stream could not be found.
381 * Quite a few others I'm too lazy to map correctly.
384 * Algorithm: Scan the elements of the presentation storage, looking
385 * for presentation streams. For each presentation stream,
386 * load the header and check to see if the aspect matches.
388 * If a fallback is desired, just opening the first presentation stream
391 static HRESULT DataCacheEntry_OpenPresStream(
392 DataCacheEntry *This,
399 if (!ppStm) return E_POINTER;
401 hr = IStorage_EnumElements(This->storage, 0, NULL, 0, &pEnum);
402 if (FAILED(hr)) return hr;
404 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
406 if (DataCache_IsPresentationStream(&elem))
410 hr = IStorage_OpenStream(This->storage, elem.pwcsName,
411 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
415 PresentationDataHeader header;
418 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
420 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
421 if (hr == S_OK && actual_read == sizeof(header)
422 && header.dvAspect == This->fmtetc.dwAspect)
424 /* Rewind the stream before returning it. */
425 LARGE_INTEGER offset;
426 offset.u.LowPart = 0;
427 offset.u.HighPart = 0;
428 IStream_Seek(pStm, offset, STREAM_SEEK_SET, NULL);
432 CoTaskMemFree(elem.pwcsName);
433 IEnumSTATSTG_Release(pEnum);
438 IStream_Release(pStm);
442 CoTaskMemFree(elem.pwcsName);
445 IEnumSTATSTG_Release(pEnum);
447 return (hr == S_FALSE ? OLE_E_BLANK : hr);
450 /************************************************************************
451 * DataCacheEntry_LoadData
453 * This method will read information for the requested presentation
454 * into the given structure.
457 * This - The entry to load the data from.
460 * This method returns a metafile handle if it is successful.
461 * it will return 0 if not.
463 static HRESULT DataCacheEntry_LoadData(DataCacheEntry *This)
465 IStream* presStream = NULL;
469 METAFILEPICT *mfpict;
471 PresentationDataHeader header;
474 * Open the presentation stream.
476 hres = DataCacheEntry_OpenPresStream(
484 * Get the size of the stream.
486 hres = IStream_Stat(presStream,
497 sizeof(PresentationDataHeader),
500 streamInfo.cbSize.QuadPart -= sizeof(PresentationDataHeader);
502 hmfpict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
505 IStream_Release(presStream);
506 return E_OUTOFMEMORY;
508 mfpict = GlobalLock(hmfpict);
511 * Allocate a buffer for the metafile bits.
513 metafileBits = HeapAlloc(GetProcessHeap(),
515 streamInfo.cbSize.u.LowPart);
518 * Read the metafile bits.
523 streamInfo.cbSize.u.LowPart,
527 * Create a metafile with those bits.
531 /* FIXME: get this from the stream */
532 mfpict->mm = MM_ANISOTROPIC;
533 mfpict->xExt = header.dwObjectExtentX;
534 mfpict->yExt = header.dwObjectExtentY;
535 mfpict->hMF = SetMetaFileBitsEx(streamInfo.cbSize.u.LowPart, metafileBits);
540 GlobalUnlock(hmfpict);
543 This->data_cf = This->fmtetc.cfFormat;
544 This->stgmedium.tymed = TYMED_MFPICT;
545 This->stgmedium.u.hMetaFilePict = hmfpict;
553 HeapFree(GetProcessHeap(), 0, metafileBits);
554 IStream_Release(presStream);
559 static HRESULT DataCacheEntry_CreateStream(DataCacheEntry *This,
560 IStorage *storage, IStream **stream)
563 WCHAR wszName[] = {2,'O','l','e','P','r','e','s',
564 '0' + (This->stream_number / 100) % 10,
565 '0' + (This->stream_number / 10) % 10,
566 '0' + This->stream_number % 10, 0};
568 /* FIXME: cache the created stream in This? */
569 hr = IStorage_CreateStream(storage, wszName,
570 STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_CREATE,
575 static HRESULT DataCacheEntry_Save(DataCacheEntry *This, IStorage *storage,
578 PresentationDataHeader header;
580 IStream *pres_stream;
583 TRACE("stream_number = %d, fmtetc = ", This->stream_number); dump_FORMATETC(&This->fmtetc); TRACE("\n");
585 hr = DataCacheEntry_CreateStream(This, storage, &pres_stream);
589 /* custom clipformat */
590 if (This->data_cf >= 0xc000)
591 FIXME("custom clipboard format not serialized properly\n");
592 header.unknown1 = -1;
593 header.clipformat = This->data_cf;
594 if (This->fmtetc.ptd)
595 FIXME("ptd not serialized\n");
597 header.dvAspect = This->fmtetc.dwAspect;
598 header.lindex = This->fmtetc.lindex;
599 header.tymed = This->stgmedium.tymed;
601 header.dwObjectExtentX = 0;
602 header.dwObjectExtentY = 0;
606 switch (This->data_cf)
608 case CF_METAFILEPICT:
610 if (This->stgmedium.tymed != TYMED_NULL)
612 const METAFILEPICT *mfpict = GlobalLock(This->stgmedium.u.hMetaFilePict);
615 IStream_Release(pres_stream);
616 return DV_E_STGMEDIUM;
618 header.dwObjectExtentX = mfpict->xExt;
619 header.dwObjectExtentY = mfpict->yExt;
620 header.dwSize = GetMetaFileBitsEx(mfpict->hMF, 0, NULL);
621 GlobalUnlock(This->stgmedium.u.hMetaFilePict);
632 hr = IStream_Write(pres_stream, &header, sizeof(PresentationDataHeader),
636 IStream_Release(pres_stream);
641 switch (This->data_cf)
643 case CF_METAFILEPICT:
645 if (This->stgmedium.tymed != TYMED_NULL)
647 const METAFILEPICT *mfpict = GlobalLock(This->stgmedium.u.hMetaFilePict);
650 IStream_Release(pres_stream);
651 return DV_E_STGMEDIUM;
653 data = HeapAlloc(GetProcessHeap(), 0, header.dwSize);
654 GetMetaFileBitsEx(mfpict->hMF, header.dwSize, data);
655 GlobalUnlock(This->stgmedium.u.hMetaFilePict);
664 hr = IStream_Write(pres_stream, data, header.dwSize, NULL);
666 IStream_Release(pres_stream);
670 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
671 * does no checking of whether src_stgm has a supported tymed, so this should be
672 * done in the caller */
673 static HRESULT copy_stg_medium(CLIPFORMAT cf, STGMEDIUM *dest_stgm,
674 const STGMEDIUM *src_stgm)
676 if (src_stgm->tymed == TYMED_MFPICT)
678 const METAFILEPICT *src_mfpict = GlobalLock(src_stgm->u.hMetaFilePict);
679 METAFILEPICT *dest_mfpict;
682 return DV_E_STGMEDIUM;
683 dest_stgm->u.hMetaFilePict = GlobalAlloc(GMEM_MOVEABLE, sizeof(METAFILEPICT));
684 dest_mfpict = GlobalLock(dest_stgm->u.hMetaFilePict);
687 GlobalUnlock(src_stgm->u.hMetaFilePict);
688 return E_OUTOFMEMORY;
690 *dest_mfpict = *src_mfpict;
691 dest_mfpict->hMF = CopyMetaFileW(src_mfpict->hMF, NULL);
692 GlobalUnlock(src_stgm->u.hMetaFilePict);
693 GlobalUnlock(dest_stgm->u.hMetaFilePict);
695 else if (src_stgm->tymed != TYMED_NULL)
697 dest_stgm->u.hGlobal = OleDuplicateData(src_stgm->u.hGlobal, cf,
699 if (!dest_stgm->u.hGlobal)
700 return E_OUTOFMEMORY;
702 dest_stgm->tymed = src_stgm->tymed;
703 dest_stgm->pUnkForRelease = src_stgm->pUnkForRelease;
704 if (dest_stgm->pUnkForRelease)
705 IUnknown_AddRef(dest_stgm->pUnkForRelease);
709 static HRESULT DataCacheEntry_SetData(DataCacheEntry *This,
710 const FORMATETC *formatetc,
711 const STGMEDIUM *stgmedium,
714 if ((!This->fmtetc.cfFormat && !formatetc->cfFormat) ||
715 (This->fmtetc.tymed == TYMED_NULL && formatetc->tymed == TYMED_NULL) ||
716 stgmedium->tymed == TYMED_NULL)
718 WARN("invalid formatetc\n");
719 return DV_E_FORMATETC;
723 ReleaseStgMedium(&This->stgmedium);
724 This->data_cf = This->fmtetc.cfFormat ? This->fmtetc.cfFormat : formatetc->cfFormat;
727 This->stgmedium = *stgmedium;
731 return copy_stg_medium(This->data_cf,
732 &This->stgmedium, stgmedium);
735 /*********************************************************
736 * Method implementation for the non delegating IUnknown
737 * part of the DataCache class.
740 /************************************************************************
741 * DataCache_NDIUnknown_QueryInterface (IUnknown)
743 * See Windows documentation for more details on IUnknown methods.
745 * This version of QueryInterface will not delegate it's implementation
746 * to the outer unknown.
748 static HRESULT WINAPI DataCache_NDIUnknown_QueryInterface(
753 DataCache *this = impl_from_NDIUnknown(iface);
756 * Perform a sanity check on the parameters.
758 if ( (this==0) || (ppvObject==0) )
762 * Initialize the return parameter.
767 * Compare the riid with the interface IDs implemented by this object.
769 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
773 else if (memcmp(&IID_IDataObject, riid, sizeof(IID_IDataObject)) == 0)
775 *ppvObject = (IDataObject*)&(this->lpVtbl);
777 else if ( (memcmp(&IID_IPersistStorage, riid, sizeof(IID_IPersistStorage)) == 0) ||
778 (memcmp(&IID_IPersist, riid, sizeof(IID_IPersist)) == 0) )
780 *ppvObject = (IPersistStorage*)&(this->lpvtblIPersistStorage);
782 else if ( (memcmp(&IID_IViewObject, riid, sizeof(IID_IViewObject)) == 0) ||
783 (memcmp(&IID_IViewObject2, riid, sizeof(IID_IViewObject2)) == 0) )
785 *ppvObject = (IViewObject2*)&(this->lpvtblIViewObject);
787 else if ( (memcmp(&IID_IOleCache, riid, sizeof(IID_IOleCache)) == 0) ||
788 (memcmp(&IID_IOleCache2, riid, sizeof(IID_IOleCache2)) == 0) )
790 *ppvObject = (IOleCache2*)&(this->lpvtblIOleCache2);
792 else if (memcmp(&IID_IOleCacheControl, riid, sizeof(IID_IOleCacheControl)) == 0)
794 *ppvObject = (IOleCacheControl*)&(this->lpvtblIOleCacheControl);
798 * Check that we obtained an interface.
802 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid));
803 return E_NOINTERFACE;
807 * Query Interface always increases the reference count by one when it is
810 IUnknown_AddRef((IUnknown*)*ppvObject);
815 /************************************************************************
816 * DataCache_NDIUnknown_AddRef (IUnknown)
818 * See Windows documentation for more details on IUnknown methods.
820 * This version of QueryInterface will not delegate it's implementation
821 * to the outer unknown.
823 static ULONG WINAPI DataCache_NDIUnknown_AddRef(
826 DataCache *this = impl_from_NDIUnknown(iface);
827 return InterlockedIncrement(&this->ref);
830 /************************************************************************
831 * DataCache_NDIUnknown_Release (IUnknown)
833 * See Windows documentation for more details on IUnknown methods.
835 * This version of QueryInterface will not delegate it's implementation
836 * to the outer unknown.
838 static ULONG WINAPI DataCache_NDIUnknown_Release(
841 DataCache *this = impl_from_NDIUnknown(iface);
845 * Decrease the reference count on this object.
847 ref = InterlockedDecrement(&this->ref);
850 * If the reference count goes down to 0, perform suicide.
852 if (ref == 0) DataCache_Destroy(this);
857 /*********************************************************
858 * Method implementation for the IDataObject
859 * part of the DataCache class.
862 /************************************************************************
863 * DataCache_IDataObject_QueryInterface (IUnknown)
865 * See Windows documentation for more details on IUnknown methods.
867 static HRESULT WINAPI DataCache_IDataObject_QueryInterface(
872 DataCache *this = impl_from_IDataObject(iface);
874 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
877 /************************************************************************
878 * DataCache_IDataObject_AddRef (IUnknown)
880 * See Windows documentation for more details on IUnknown methods.
882 static ULONG WINAPI DataCache_IDataObject_AddRef(
885 DataCache *this = impl_from_IDataObject(iface);
887 return IUnknown_AddRef(this->outerUnknown);
890 /************************************************************************
891 * DataCache_IDataObject_Release (IUnknown)
893 * See Windows documentation for more details on IUnknown methods.
895 static ULONG WINAPI DataCache_IDataObject_Release(
898 DataCache *this = impl_from_IDataObject(iface);
900 return IUnknown_Release(this->outerUnknown);
903 /************************************************************************
906 * Get Data from a source dataobject using format pformatetcIn->cfFormat
907 * See Windows documentation for more details on GetData.
908 * TODO: Currently only CF_METAFILEPICT is implemented
910 static HRESULT WINAPI DataCache_GetData(
912 LPFORMATETC pformatetcIn,
916 HRESULT hrRet = E_UNEXPECTED;
917 IPersistStorage *pPersistStorage = 0;
918 IStorage *pStorage = 0;
919 IStream *pStream = 0;
920 OLECHAR name[]={ 2, 'O', 'l', 'e', 'P', 'r', 'e', 's', '0', '0', '0', 0};
921 HGLOBAL hGlobalMF = 0;
923 PresentationDataHeader pdh;
924 METAFILEPICT *mfPict;
925 HMETAFILE hMetaFile = 0;
927 if (pformatetcIn->cfFormat == CF_METAFILEPICT)
929 /* Get the Persist Storage */
931 hr = IDataObject_QueryInterface(iface, &IID_IPersistStorage, (void**)&pPersistStorage);
936 /* Create a doc file to copy the doc to a storage */
938 hr = StgCreateDocfile(NULL, STGM_CREATE | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, 0, &pStorage);
943 /* Save it to storage */
945 hr = OleSave(pPersistStorage, pStorage, FALSE);
950 /* Open the Presentation data srteam */
952 hr = IStorage_OpenStream(pStorage, name, 0, STGM_CREATE|STGM_SHARE_EXCLUSIVE|STGM_READWRITE, 0, &pStream);
957 /* Read the presentation header */
959 hr = IStream_Read(pStream, &pdh, sizeof(PresentationDataHeader), NULL);
964 mfBits = HeapAlloc(GetProcessHeap(), 0, pdh.dwSize);
966 /* Read the Metafile bits */
968 hr = IStream_Read(pStream, mfBits, pdh.dwSize, NULL);
973 /* Create the metafile and place it in the STGMEDIUM structure */
975 hMetaFile = SetMetaFileBitsEx(pdh.dwSize, mfBits);
977 hGlobalMF = GlobalAlloc(GMEM_SHARE|GMEM_MOVEABLE, sizeof(METAFILEPICT));
978 mfPict = (METAFILEPICT *)GlobalLock(hGlobalMF);
979 mfPict->hMF = hMetaFile;
981 GlobalUnlock(hGlobalMF);
983 pmedium->u.hGlobal = hGlobalMF;
984 pmedium->tymed = TYMED_MFPICT;
989 HeapFree(GetProcessHeap(), 0, mfBits);
992 IStream_Release(pStream);
995 IStorage_Release(pStorage);
998 IPersistStorage_Release(pPersistStorage);
1003 /* TODO: Other formats are not implemented */
1008 static HRESULT WINAPI DataCache_GetDataHere(
1010 LPFORMATETC pformatetc,
1017 static HRESULT WINAPI DataCache_QueryGetData(
1019 LPFORMATETC pformatetc)
1025 /************************************************************************
1026 * DataCache_EnumFormatEtc (IDataObject)
1028 * The data cache doesn't implement this method.
1030 * See Windows documentation for more details on IDataObject methods.
1032 static HRESULT WINAPI DataCache_GetCanonicalFormatEtc(
1034 LPFORMATETC pformatectIn,
1035 LPFORMATETC pformatetcOut)
1041 /************************************************************************
1042 * DataCache_IDataObject_SetData (IDataObject)
1044 * This method is delegated to the IOleCache2 implementation.
1046 * See Windows documentation for more details on IDataObject methods.
1048 static HRESULT WINAPI DataCache_IDataObject_SetData(
1050 LPFORMATETC pformatetc,
1054 IOleCache2* oleCache = NULL;
1057 TRACE("(%p, %p, %p, %d)\n", iface, pformatetc, pmedium, fRelease);
1059 hres = IDataObject_QueryInterface(iface, &IID_IOleCache2, (void**)&oleCache);
1062 return E_UNEXPECTED;
1064 hres = IOleCache2_SetData(oleCache, pformatetc, pmedium, fRelease);
1066 IOleCache2_Release(oleCache);
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_EnumFormatEtc(
1081 IEnumFORMATETC** ppenumFormatEtc)
1087 /************************************************************************
1088 * DataCache_DAdvise (IDataObject)
1090 * The data cache doesn't support connections.
1092 * See Windows documentation for more details on IDataObject methods.
1094 static HRESULT WINAPI DataCache_DAdvise(
1096 FORMATETC* pformatetc,
1098 IAdviseSink* pAdvSink,
1099 DWORD* pdwConnection)
1102 return OLE_E_ADVISENOTSUPPORTED;
1105 /************************************************************************
1106 * DataCache_DUnadvise (IDataObject)
1108 * The data cache doesn't support connections.
1110 * See Windows documentation for more details on IDataObject methods.
1112 static HRESULT WINAPI DataCache_DUnadvise(
1117 return OLE_E_NOCONNECTION;
1120 /************************************************************************
1121 * DataCache_EnumDAdvise (IDataObject)
1123 * The data cache doesn't support connections.
1125 * See Windows documentation for more details on IDataObject methods.
1127 static HRESULT WINAPI DataCache_EnumDAdvise(
1129 IEnumSTATDATA** ppenumAdvise)
1132 return OLE_E_ADVISENOTSUPPORTED;
1135 /*********************************************************
1136 * Method implementation for the IDataObject
1137 * part of the DataCache class.
1140 /************************************************************************
1141 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1143 * See Windows documentation for more details on IUnknown methods.
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 * See Windows documentation for more details on IUnknown methods.
1160 static ULONG WINAPI DataCache_IPersistStorage_AddRef(
1161 IPersistStorage* iface)
1163 DataCache *this = impl_from_IPersistStorage(iface);
1165 return IUnknown_AddRef(this->outerUnknown);
1168 /************************************************************************
1169 * DataCache_IPersistStorage_Release (IUnknown)
1171 * See Windows documentation for more details on IUnknown methods.
1173 static ULONG WINAPI DataCache_IPersistStorage_Release(
1174 IPersistStorage* iface)
1176 DataCache *this = impl_from_IPersistStorage(iface);
1178 return IUnknown_Release(this->outerUnknown);
1181 /************************************************************************
1182 * DataCache_GetClassID (IPersistStorage)
1184 * The data cache doesn't implement this method.
1186 * See Windows documentation for more details on IPersistStorage methods.
1188 static HRESULT WINAPI DataCache_GetClassID(
1189 IPersistStorage* iface,
1192 DataCache *This = impl_from_IPersistStorage(iface);
1193 DataCacheEntry *cache_entry;
1195 TRACE("(%p, %p)\n", iface, pClassID);
1197 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1199 if (cache_entry->storage != NULL)
1202 HRESULT hr = IStorage_Stat(cache_entry->storage, &statstg, STATFLAG_NONAME);
1205 memcpy(pClassID, &statstg.clsid, sizeof(*pClassID));
1211 memcpy(pClassID, &CLSID_NULL, sizeof(*pClassID));
1216 /************************************************************************
1217 * DataCache_IsDirty (IPersistStorage)
1219 * See Windows documentation for more details on IPersistStorage methods.
1221 static HRESULT WINAPI DataCache_IsDirty(
1222 IPersistStorage* iface)
1224 DataCache *This = impl_from_IPersistStorage(iface);
1225 DataCacheEntry *cache_entry;
1227 TRACE("(%p)\n", iface);
1232 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1233 if (cache_entry->dirty)
1239 /************************************************************************
1240 * DataCache_InitNew (IPersistStorage)
1242 * The data cache implementation of IPersistStorage_InitNew simply stores
1243 * the storage pointer.
1245 * See Windows documentation for more details on IPersistStorage methods.
1247 static HRESULT WINAPI DataCache_InitNew(
1248 IPersistStorage* iface,
1251 DataCache *This = impl_from_IPersistStorage(iface);
1253 TRACE("(%p, %p)\n", iface, pStg);
1255 if (This->presentationStorage != NULL)
1256 IStorage_Release(This->presentationStorage);
1258 This->presentationStorage = pStg;
1260 IStorage_AddRef(This->presentationStorage);
1266 /************************************************************************
1267 * DataCache_Load (IPersistStorage)
1269 * The data cache implementation of IPersistStorage_Load doesn't
1270 * actually load anything. Instead, it holds on to the storage pointer
1271 * and it will load the presentation information when the
1272 * IDataObject_GetData or IViewObject2_Draw methods are called.
1274 * See Windows documentation for more details on IPersistStorage methods.
1276 static HRESULT WINAPI DataCache_Load(
1277 IPersistStorage* iface,
1280 DataCache *This = impl_from_IPersistStorage(iface);
1282 IEnumSTATSTG *pEnum;
1285 TRACE("(%p, %p)\n", iface, pStg);
1287 if (This->presentationStorage != NULL)
1288 IStorage_Release(This->presentationStorage);
1290 This->presentationStorage = pStg;
1292 hr = IStorage_EnumElements(pStg, 0, NULL, 0, &pEnum);
1293 if (FAILED(hr)) return hr;
1295 while ((hr = IEnumSTATSTG_Next(pEnum, 1, &elem, NULL)) == S_OK)
1297 if (DataCache_IsPresentationStream(&elem))
1301 hr = IStorage_OpenStream(This->presentationStorage, elem.pwcsName,
1302 NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0,
1306 PresentationDataHeader header;
1309 hr = IStream_Read(pStm, &header, sizeof(header), &actual_read);
1311 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1312 if (hr == S_OK && actual_read == sizeof(header))
1314 DataCacheEntry *cache_entry;
1317 fmtetc.cfFormat = header.clipformat;
1318 fmtetc.ptd = NULL; /* FIXME */
1319 fmtetc.dwAspect = header.dvAspect;
1320 fmtetc.lindex = header.lindex;
1321 fmtetc.tymed = header.tymed;
1323 TRACE("loading entry with formatetc: "); dump_FORMATETC(&fmtetc); TRACE("\n");
1325 cache_entry = DataCache_GetEntryForFormatEtc(This, &fmtetc);
1327 hr = DataCache_CreateEntry(This, &fmtetc, &cache_entry);
1330 ReleaseStgMedium(&cache_entry->stgmedium);
1331 if (cache_entry->storage) IStorage_Release(cache_entry->storage);
1332 cache_entry->storage = pStg;
1333 IStorage_AddRef(pStg);
1334 cache_entry->dirty = FALSE;
1338 IStream_Release(pStm);
1342 CoTaskMemFree(elem.pwcsName);
1345 This->dirty = FALSE;
1347 IEnumSTATSTG_Release(pEnum);
1349 IStorage_AddRef(This->presentationStorage);
1353 /************************************************************************
1354 * DataCache_Save (IPersistStorage)
1356 * Until we actually connect to a running object and retrieve new
1357 * information to it, we never have to save anything. However, it is
1358 * our responsibility to copy the information when saving to a new
1361 * See Windows documentation for more details on IPersistStorage methods.
1363 static HRESULT WINAPI DataCache_Save(
1364 IPersistStorage* iface,
1368 DataCache *This = impl_from_IPersistStorage(iface);
1369 DataCacheEntry *cache_entry;
1372 unsigned short stream_number = 0;
1374 TRACE("(%p, %p, %d)\n", iface, pStg, fSameAsLoad);
1376 dirty = This->dirty;
1379 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1381 dirty = cache_entry->dirty;
1387 /* this is a shortcut if nothing changed */
1388 if (!dirty && !fSameAsLoad && This->presentationStorage)
1390 return IStorage_CopyTo(This->presentationStorage, 0, NULL, NULL, pStg);
1393 /* assign stream numbers to the cache entries */
1394 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1396 if (cache_entry->stream_number != stream_number)
1398 cache_entry->dirty = TRUE; /* needs to be written out again */
1399 cache_entry->stream_number = stream_number;
1404 /* write out the cache entries */
1405 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1407 if (!fSameAsLoad || cache_entry->dirty)
1409 hr = DataCacheEntry_Save(cache_entry, pStg, fSameAsLoad);
1413 cache_entry->dirty = FALSE;
1417 This->dirty = FALSE;
1421 /************************************************************************
1422 * DataCache_SaveCompleted (IPersistStorage)
1424 * This method is called to tell the cache to release the storage
1425 * pointer it's currently holding.
1427 * See Windows documentation for more details on IPersistStorage methods.
1429 static HRESULT WINAPI DataCache_SaveCompleted(
1430 IPersistStorage* iface,
1433 TRACE("(%p, %p)\n", iface, pStgNew);
1438 * First, make sure we get our hands off any storage we have.
1441 IPersistStorage_HandsOffStorage(iface);
1444 * Then, attach to the new storage.
1447 DataCache_Load(iface, pStgNew);
1453 /************************************************************************
1454 * DataCache_HandsOffStorage (IPersistStorage)
1456 * This method is called to tell the cache to release the storage
1457 * pointer it's currently holding.
1459 * See Windows documentation for more details on IPersistStorage methods.
1461 static HRESULT WINAPI DataCache_HandsOffStorage(
1462 IPersistStorage* iface)
1464 DataCache *this = impl_from_IPersistStorage(iface);
1466 TRACE("(%p)\n", iface);
1468 if (this->presentationStorage != NULL)
1470 IStorage_Release(this->presentationStorage);
1471 this->presentationStorage = NULL;
1477 /*********************************************************
1478 * Method implementation for the IViewObject2
1479 * part of the DataCache class.
1482 /************************************************************************
1483 * DataCache_IViewObject2_QueryInterface (IUnknown)
1485 * See Windows documentation for more details on IUnknown methods.
1487 static HRESULT WINAPI DataCache_IViewObject2_QueryInterface(
1488 IViewObject2* iface,
1492 DataCache *this = impl_from_IViewObject2(iface);
1494 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1497 /************************************************************************
1498 * DataCache_IViewObject2_AddRef (IUnknown)
1500 * See Windows documentation for more details on IUnknown methods.
1502 static ULONG WINAPI DataCache_IViewObject2_AddRef(
1503 IViewObject2* iface)
1505 DataCache *this = impl_from_IViewObject2(iface);
1507 return IUnknown_AddRef(this->outerUnknown);
1510 /************************************************************************
1511 * DataCache_IViewObject2_Release (IUnknown)
1513 * See Windows documentation for more details on IUnknown methods.
1515 static ULONG WINAPI DataCache_IViewObject2_Release(
1516 IViewObject2* iface)
1518 DataCache *this = impl_from_IViewObject2(iface);
1520 return IUnknown_Release(this->outerUnknown);
1523 /************************************************************************
1524 * DataCache_Draw (IViewObject2)
1526 * This method will draw the cached representation of the object
1527 * to the given device context.
1529 * See Windows documentation for more details on IViewObject2 methods.
1531 static HRESULT WINAPI DataCache_Draw(
1532 IViewObject2* iface,
1536 DVTARGETDEVICE* ptd,
1539 LPCRECTL lprcBounds,
1540 LPCRECTL lprcWBounds,
1541 BOOL (CALLBACK *pfnContinue)(ULONG_PTR dwContinue),
1542 ULONG_PTR dwContinue)
1544 DataCache *This = impl_from_IViewObject2(iface);
1546 DataCacheEntry *cache_entry;
1548 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1563 if (lprcBounds==NULL)
1564 return E_INVALIDARG;
1566 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1568 /* FIXME: compare ptd too */
1569 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1570 (cache_entry->fmtetc.lindex != lindex))
1573 /* if the data hasn't been loaded yet, do it now */
1574 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1576 hres = DataCacheEntry_LoadData(cache_entry);
1582 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1585 switch (cache_entry->data_cf)
1587 case CF_METAFILEPICT:
1590 * We have to be careful not to modify the state of the
1595 SIZE oldViewportExt;
1596 POINT oldViewportOrg;
1597 METAFILEPICT *mfpict;
1599 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1600 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1603 prevMapMode = SetMapMode(hdcDraw, mfpict->mm);
1605 SetWindowExtEx(hdcDraw,
1610 SetViewportExtEx(hdcDraw,
1611 lprcBounds->right - lprcBounds->left,
1612 lprcBounds->bottom - lprcBounds->top,
1615 SetViewportOrgEx(hdcDraw,
1620 PlayMetaFile(hdcDraw, mfpict->hMF);
1622 SetWindowExtEx(hdcDraw,
1627 SetViewportExtEx(hdcDraw,
1632 SetViewportOrgEx(hdcDraw,
1637 SetMapMode(hdcDraw, prevMapMode);
1639 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1646 WARN("no data could be found to be drawn\n");
1651 static HRESULT WINAPI DataCache_GetColorSet(
1652 IViewObject2* iface,
1656 DVTARGETDEVICE* ptd,
1657 HDC hicTargetDevice,
1658 LOGPALETTE** ppColorSet)
1664 static HRESULT WINAPI DataCache_Freeze(
1665 IViewObject2* iface,
1675 static HRESULT WINAPI DataCache_Unfreeze(
1676 IViewObject2* iface,
1683 /************************************************************************
1684 * DataCache_SetAdvise (IViewObject2)
1686 * This sets-up an advisory sink with the data cache. When the object's
1687 * view changes, this sink is called.
1689 * See Windows documentation for more details on IViewObject2 methods.
1691 static HRESULT WINAPI DataCache_SetAdvise(
1692 IViewObject2* iface,
1695 IAdviseSink* pAdvSink)
1697 DataCache *this = impl_from_IViewObject2(iface);
1699 TRACE("(%p, %x, %x, %p)\n", iface, aspects, advf, pAdvSink);
1702 * A call to this function removes the previous sink
1704 if (this->sinkInterface != NULL)
1706 IAdviseSink_Release(this->sinkInterface);
1707 this->sinkInterface = NULL;
1708 this->sinkAspects = 0;
1709 this->sinkAdviseFlag = 0;
1713 * Now, setup the new one.
1717 this->sinkInterface = pAdvSink;
1718 this->sinkAspects = aspects;
1719 this->sinkAdviseFlag = advf;
1721 IAdviseSink_AddRef(this->sinkInterface);
1725 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1728 if (advf & ADVF_PRIMEFIRST)
1730 DataCache_FireOnViewChange(this, aspects, -1);
1736 /************************************************************************
1737 * DataCache_GetAdvise (IViewObject2)
1739 * This method queries the current state of the advise sink
1740 * installed on the data cache.
1742 * See Windows documentation for more details on IViewObject2 methods.
1744 static HRESULT WINAPI DataCache_GetAdvise(
1745 IViewObject2* iface,
1748 IAdviseSink** ppAdvSink)
1750 DataCache *this = impl_from_IViewObject2(iface);
1752 TRACE("(%p, %p, %p, %p)\n", iface, pAspects, pAdvf, ppAdvSink);
1755 * Just copy all the requested values.
1758 *pAspects = this->sinkAspects;
1761 *pAdvf = this->sinkAdviseFlag;
1763 if (ppAdvSink!=NULL)
1765 if (this->sinkInterface != NULL)
1766 IAdviseSink_QueryInterface(this->sinkInterface,
1769 else *ppAdvSink = NULL;
1775 /************************************************************************
1776 * DataCache_GetExtent (IViewObject2)
1778 * This method retrieves the "natural" size of this cached object.
1780 * See Windows documentation for more details on IViewObject2 methods.
1782 static HRESULT WINAPI DataCache_GetExtent(
1783 IViewObject2* iface,
1786 DVTARGETDEVICE* ptd,
1789 DataCache *This = impl_from_IViewObject2(iface);
1790 HRESULT hres = E_FAIL;
1791 DataCacheEntry *cache_entry;
1793 TRACE("(%p, %x, %d, %p, %p)\n",
1794 iface, dwDrawAspect, lindex, ptd, lpsizel);
1803 * Initialize the out parameter.
1809 * This flag should be set to -1.
1812 FIXME("Unimplemented flag lindex = %d\n", lindex);
1815 * Right now, we support only the callback from
1816 * the default handler.
1819 FIXME("Unimplemented ptd = %p\n", ptd);
1821 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1823 /* FIXME: compare ptd too */
1824 if ((cache_entry->fmtetc.dwAspect != dwDrawAspect) ||
1825 (cache_entry->fmtetc.lindex != lindex))
1828 /* if the data hasn't been loaded yet, do it now */
1829 if ((cache_entry->stgmedium.tymed == TYMED_NULL) && cache_entry->storage)
1831 hres = DataCacheEntry_LoadData(cache_entry);
1837 if (cache_entry->stgmedium.tymed == TYMED_NULL)
1841 switch (cache_entry->data_cf)
1843 case CF_METAFILEPICT:
1845 METAFILEPICT *mfpict;
1847 if ((cache_entry->stgmedium.tymed != TYMED_MFPICT) ||
1848 !((mfpict = GlobalLock(cache_entry->stgmedium.u.hMetaFilePict))))
1851 lpsizel->cx = mfpict->xExt;
1852 lpsizel->cy = mfpict->yExt;
1854 GlobalUnlock(cache_entry->stgmedium.u.hMetaFilePict);
1861 WARN("no data could be found to get the extents from\n");
1864 * This method returns OLE_E_BLANK when it fails.
1870 /*********************************************************
1871 * Method implementation for the IOleCache2
1872 * part of the DataCache class.
1875 /************************************************************************
1876 * DataCache_IOleCache2_QueryInterface (IUnknown)
1878 * See Windows documentation for more details on IUnknown methods.
1880 static HRESULT WINAPI DataCache_IOleCache2_QueryInterface(
1885 DataCache *this = impl_from_IOleCache2(iface);
1887 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
1890 /************************************************************************
1891 * DataCache_IOleCache2_AddRef (IUnknown)
1893 * See Windows documentation for more details on IUnknown methods.
1895 static ULONG WINAPI DataCache_IOleCache2_AddRef(
1898 DataCache *this = impl_from_IOleCache2(iface);
1900 return IUnknown_AddRef(this->outerUnknown);
1903 /************************************************************************
1904 * DataCache_IOleCache2_Release (IUnknown)
1906 * See Windows documentation for more details on IUnknown methods.
1908 static ULONG WINAPI DataCache_IOleCache2_Release(
1911 DataCache *this = impl_from_IOleCache2(iface);
1913 return IUnknown_Release(this->outerUnknown);
1916 static HRESULT WINAPI DataCache_Cache(
1918 FORMATETC* pformatetc,
1920 DWORD* pdwConnection)
1922 DataCache *This = impl_from_IOleCache2(iface);
1923 DataCacheEntry *cache_entry;
1926 TRACE("(%p, 0x%x, %p)\n", pformatetc, advf, pdwConnection);
1927 TRACE("pformatetc = "); dump_FORMATETC(pformatetc); TRACE("\n");
1931 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
1934 TRACE("found an existing cache entry\n");
1935 *pdwConnection = cache_entry->id;
1936 return CACHE_S_SAMECACHE;
1939 hr = DataCache_CreateEntry(This, pformatetc, &cache_entry);
1942 *pdwConnection = cache_entry->id;
1947 static HRESULT WINAPI DataCache_Uncache(
1951 DataCache *This = impl_from_IOleCache2(iface);
1952 DataCacheEntry *cache_entry;
1954 TRACE("(%d)\n", dwConnection);
1956 LIST_FOR_EACH_ENTRY(cache_entry, &This->cache_list, DataCacheEntry, entry)
1957 if (cache_entry->id == dwConnection)
1959 DataCacheEntry_Destroy(cache_entry);
1963 WARN("no connection found for %d\n", dwConnection);
1965 return OLE_E_NOCONNECTION;
1968 static HRESULT WINAPI DataCache_EnumCache(
1970 IEnumSTATDATA** ppenumSTATDATA)
1976 static HRESULT WINAPI DataCache_InitCache(
1978 IDataObject* pDataObject)
1984 static HRESULT WINAPI DataCache_IOleCache2_SetData(
1986 FORMATETC* pformatetc,
1990 DataCache *This = impl_from_IOleCache2(iface);
1991 DataCacheEntry *cache_entry;
1994 TRACE("(%p, %p, %s)\n", pformatetc, pmedium, fRelease ? "TRUE" : "FALSE");
1995 TRACE("formatetc = "); dump_FORMATETC(pformatetc); TRACE("\n");
1997 cache_entry = DataCache_GetEntryForFormatEtc(This, pformatetc);
2000 hr = DataCacheEntry_SetData(cache_entry, pformatetc, pmedium, fRelease);
2003 DataCache_FireOnViewChange(This, cache_entry->fmtetc.dwAspect,
2004 cache_entry->fmtetc.lindex);
2008 WARN("cache entry not found\n");
2013 static HRESULT WINAPI DataCache_UpdateCache(
2015 LPDATAOBJECT pDataObject,
2023 static HRESULT WINAPI DataCache_DiscardCache(
2025 DWORD dwDiscardOptions)
2032 /*********************************************************
2033 * Method implementation for the IOleCacheControl
2034 * part of the DataCache class.
2037 /************************************************************************
2038 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2040 * See Windows documentation for more details on IUnknown methods.
2042 static HRESULT WINAPI DataCache_IOleCacheControl_QueryInterface(
2043 IOleCacheControl* iface,
2047 DataCache *this = impl_from_IOleCacheControl(iface);
2049 return IUnknown_QueryInterface(this->outerUnknown, riid, ppvObject);
2052 /************************************************************************
2053 * DataCache_IOleCacheControl_AddRef (IUnknown)
2055 * See Windows documentation for more details on IUnknown methods.
2057 static ULONG WINAPI DataCache_IOleCacheControl_AddRef(
2058 IOleCacheControl* iface)
2060 DataCache *this = impl_from_IOleCacheControl(iface);
2062 return IUnknown_AddRef(this->outerUnknown);
2065 /************************************************************************
2066 * DataCache_IOleCacheControl_Release (IUnknown)
2068 * See Windows documentation for more details on IUnknown methods.
2070 static ULONG WINAPI DataCache_IOleCacheControl_Release(
2071 IOleCacheControl* iface)
2073 DataCache *this = impl_from_IOleCacheControl(iface);
2075 return IUnknown_Release(this->outerUnknown);
2078 static HRESULT WINAPI DataCache_OnRun(
2079 IOleCacheControl* iface,
2080 LPDATAOBJECT pDataObject)
2086 static HRESULT WINAPI DataCache_OnStop(
2087 IOleCacheControl* iface)
2094 * Virtual function tables for the DataCache class.
2096 static const IUnknownVtbl DataCache_NDIUnknown_VTable =
2098 DataCache_NDIUnknown_QueryInterface,
2099 DataCache_NDIUnknown_AddRef,
2100 DataCache_NDIUnknown_Release
2103 static const IDataObjectVtbl DataCache_IDataObject_VTable =
2105 DataCache_IDataObject_QueryInterface,
2106 DataCache_IDataObject_AddRef,
2107 DataCache_IDataObject_Release,
2109 DataCache_GetDataHere,
2110 DataCache_QueryGetData,
2111 DataCache_GetCanonicalFormatEtc,
2112 DataCache_IDataObject_SetData,
2113 DataCache_EnumFormatEtc,
2115 DataCache_DUnadvise,
2116 DataCache_EnumDAdvise
2119 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable =
2121 DataCache_IPersistStorage_QueryInterface,
2122 DataCache_IPersistStorage_AddRef,
2123 DataCache_IPersistStorage_Release,
2124 DataCache_GetClassID,
2129 DataCache_SaveCompleted,
2130 DataCache_HandsOffStorage
2133 static const IViewObject2Vtbl DataCache_IViewObject2_VTable =
2135 DataCache_IViewObject2_QueryInterface,
2136 DataCache_IViewObject2_AddRef,
2137 DataCache_IViewObject2_Release,
2139 DataCache_GetColorSet,
2142 DataCache_SetAdvise,
2143 DataCache_GetAdvise,
2147 static const IOleCache2Vtbl DataCache_IOleCache2_VTable =
2149 DataCache_IOleCache2_QueryInterface,
2150 DataCache_IOleCache2_AddRef,
2151 DataCache_IOleCache2_Release,
2154 DataCache_EnumCache,
2155 DataCache_InitCache,
2156 DataCache_IOleCache2_SetData,
2157 DataCache_UpdateCache,
2158 DataCache_DiscardCache
2161 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable =
2163 DataCache_IOleCacheControl_QueryInterface,
2164 DataCache_IOleCacheControl_AddRef,
2165 DataCache_IOleCacheControl_Release,
2170 /******************************************************************************
2171 * CreateDataCache [OLE32.@]
2173 * Creates a data cache to allow an object to render one or more of its views,
2174 * whether running or not.
2177 * pUnkOuter [I] Outer unknown for the object.
2179 * riid [I] IID of interface to return.
2180 * ppvObj [O] Address where the data cache object will be stored on return.
2184 * Failure: HRESULT code.
2187 * The following interfaces are supported by the returned data cache object:
2188 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorae, IDataObject,
2189 * IViewObject and IViewObject2.
2191 HRESULT WINAPI CreateDataCache(
2192 LPUNKNOWN pUnkOuter,
2197 DataCache* newCache = NULL;
2200 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid), pUnkOuter, debugstr_guid(riid), ppvObj);
2211 * If this cache is constructed for aggregation, make sure
2212 * the caller is requesting the IUnknown interface.
2213 * This is necessary because it's the only time the non-delegating
2214 * IUnknown pointer can be returned to the outside.
2216 if ( (pUnkOuter!=NULL) &&
2217 (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) != 0) )
2218 return CLASS_E_NOAGGREGATION;
2221 * Try to construct a new instance of the class.
2223 newCache = DataCache_Construct(rclsid,
2227 return E_OUTOFMEMORY;
2230 * Make sure it supports the interface required by the caller.
2232 hr = IUnknown_QueryInterface((IUnknown*)&(newCache->lpvtblNDIUnknown), riid, ppvObj);
2235 * Release the reference obtained in the constructor. If
2236 * the QueryInterface was unsuccessful, it will free the class.
2238 IUnknown_Release((IUnknown*)&(newCache->lpvtblNDIUnknown));
2243 /*********************************************************
2244 * Method implementation for DataCache class.
2246 static DataCache* DataCache_Construct(
2248 LPUNKNOWN pUnkOuter)
2250 DataCache* newObject = 0;
2253 * Allocate space for the object.
2255 newObject = HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache));
2261 * Initialize the virtual function table.
2263 newObject->lpVtbl = &DataCache_IDataObject_VTable;
2264 newObject->lpvtblNDIUnknown = &DataCache_NDIUnknown_VTable;
2265 newObject->lpvtblIPersistStorage = &DataCache_IPersistStorage_VTable;
2266 newObject->lpvtblIViewObject = &DataCache_IViewObject2_VTable;
2267 newObject->lpvtblIOleCache2 = &DataCache_IOleCache2_VTable;
2268 newObject->lpvtblIOleCacheControl = &DataCache_IOleCacheControl_VTable;
2271 * Start with one reference count. The caller of this function
2272 * must release the interface pointer when it is done.
2277 * Initialize the outer unknown
2278 * We don't keep a reference on the outer unknown since, the way
2279 * aggregation works, our lifetime is at least as large as its
2282 if (pUnkOuter==NULL)
2283 pUnkOuter = (IUnknown*)&(newObject->lpvtblNDIUnknown);
2285 newObject->outerUnknown = pUnkOuter;
2288 * Initialize the other members of the structure.
2290 newObject->sinkAspects = 0;
2291 newObject->sinkAdviseFlag = 0;
2292 newObject->sinkInterface = 0;
2293 newObject->presentationStorage = NULL;
2294 list_init(&newObject->cache_list);
2295 newObject->last_cache_id = 1;
2296 newObject->dirty = FALSE;