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.
46 #define WIN32_NO_STATUS
53 #define NONAMELESSUNION
54 #define NONAMELESSSTRUCT
59 //#include "winuser.h"
60 //#include "winerror.h"
61 #include <wine/unicode.h>
63 #include <wine/list.h>
64 #include <wine/debug.h>
66 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
68 /****************************************************************************
69 * PresentationDataHeader
71 * This structure represents the header of the \002OlePresXXX stream in
72 * the OLE object storage.
74 typedef struct PresentationDataHeader
77 * - standard clipformat:
78 * DWORD length = 0xffffffff;
80 * - or custom clipformat:
82 * CHAR format_name[length]; (null-terminated)
84 DWORD unknown3
; /* 4, possibly TYMED_ISTREAM */
88 DWORD unknown7
; /* 0 */
89 DWORD dwObjectExtentX
;
90 DWORD dwObjectExtentY
;
92 } PresentationDataHeader
;
94 typedef struct DataCacheEntry
97 /* format of this entry */
99 /* the clipboard format of the data */
104 * This storage pointer is set through a call to
105 * IPersistStorage_Load. This is where the visual
106 * representation of the object is stored.
113 /* stream number (-1 if not set ) */
114 unsigned short stream_number
;
115 /* sink id set when object is running */
117 /* Advise sink flags */
121 /****************************************************************************
127 * List all interface here
129 IDataObject IDataObject_iface
;
130 IUnknown IUnknown_iface
;
131 IPersistStorage IPersistStorage_iface
;
132 IViewObject2 IViewObject2_iface
;
133 IOleCache2 IOleCache2_iface
;
134 IOleCacheControl IOleCacheControl_iface
;
136 /* The sink that is connected to a remote object.
137 The other interfaces are not available by QI'ing the sink and vice-versa */
138 IAdviseSink IAdviseSink_iface
;
141 * Reference count of this object
146 * IUnknown implementation of the outer object.
148 IUnknown
* outerUnknown
;
151 * The user of this object can setup ONE advise sink
152 * connection with the object. These parameters describe
156 DWORD sinkAdviseFlag
;
157 IAdviseSink
* sinkInterface
;
158 IStorage
*presentationStorage
;
160 /* list of cache entries */
161 struct list cache_list
;
162 /* last id assigned to an entry */
166 /* running object set by OnRun */
167 IDataObject
*running_object
;
170 typedef struct DataCache DataCache
;
173 * Here, I define utility macros to help with the casting of the
175 * There is a version to accommodate all of the VTables implemented
179 static inline DataCache
*impl_from_IDataObject( IDataObject
*iface
)
181 return CONTAINING_RECORD(iface
, DataCache
, IDataObject_iface
);
184 static inline DataCache
*impl_from_IUnknown( IUnknown
*iface
)
186 return CONTAINING_RECORD(iface
, DataCache
, IUnknown_iface
);
189 static inline DataCache
*impl_from_IPersistStorage( IPersistStorage
*iface
)
191 return CONTAINING_RECORD(iface
, DataCache
, IPersistStorage_iface
);
194 static inline DataCache
*impl_from_IViewObject2( IViewObject2
*iface
)
196 return CONTAINING_RECORD(iface
, DataCache
, IViewObject2_iface
);
199 static inline DataCache
*impl_from_IOleCache2( IOleCache2
*iface
)
201 return CONTAINING_RECORD(iface
, DataCache
, IOleCache2_iface
);
204 static inline DataCache
*impl_from_IOleCacheControl( IOleCacheControl
*iface
)
206 return CONTAINING_RECORD(iface
, DataCache
, IOleCacheControl_iface
);
209 static inline DataCache
*impl_from_IAdviseSink( IAdviseSink
*iface
)
211 return CONTAINING_RECORD(iface
, DataCache
, IAdviseSink_iface
);
214 static const char * debugstr_formatetc(const FORMATETC
*formatetc
)
216 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
217 formatetc
->cfFormat
, formatetc
->ptd
, formatetc
->dwAspect
,
218 formatetc
->lindex
, formatetc
->tymed
);
221 static void DataCacheEntry_Destroy(DataCache
*cache
, DataCacheEntry
*cache_entry
)
223 list_remove(&cache_entry
->entry
);
224 if (cache_entry
->storage
)
225 IStorage_Release(cache_entry
->storage
);
226 HeapFree(GetProcessHeap(), 0, cache_entry
->fmtetc
.ptd
);
227 ReleaseStgMedium(&cache_entry
->stgmedium
);
228 if(cache_entry
->sink_id
)
229 IDataObject_DUnadvise(cache
->running_object
, cache_entry
->sink_id
);
231 HeapFree(GetProcessHeap(), 0, cache_entry
);
234 static void DataCache_Destroy(
235 DataCache
* ptrToDestroy
)
237 DataCacheEntry
*cache_entry
, *next_cache_entry
;
241 if (ptrToDestroy
->sinkInterface
!= NULL
)
243 IAdviseSink_Release(ptrToDestroy
->sinkInterface
);
244 ptrToDestroy
->sinkInterface
= NULL
;
247 LIST_FOR_EACH_ENTRY_SAFE(cache_entry
, next_cache_entry
, &ptrToDestroy
->cache_list
, DataCacheEntry
, entry
)
248 DataCacheEntry_Destroy(ptrToDestroy
, cache_entry
);
250 if (ptrToDestroy
->presentationStorage
!= NULL
)
252 IStorage_Release(ptrToDestroy
->presentationStorage
);
253 ptrToDestroy
->presentationStorage
= NULL
;
257 * Free the datacache pointer.
259 HeapFree(GetProcessHeap(), 0, ptrToDestroy
);
262 static DataCacheEntry
*DataCache_GetEntryForFormatEtc(DataCache
*This
, const FORMATETC
*formatetc
)
264 DataCacheEntry
*cache_entry
;
265 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
267 /* FIXME: also compare DVTARGETDEVICEs */
268 if ((!cache_entry
->fmtetc
.cfFormat
|| !formatetc
->cfFormat
|| (formatetc
->cfFormat
== cache_entry
->fmtetc
.cfFormat
)) &&
269 (formatetc
->dwAspect
== cache_entry
->fmtetc
.dwAspect
) &&
270 (formatetc
->lindex
== cache_entry
->fmtetc
.lindex
) &&
271 (!cache_entry
->fmtetc
.tymed
|| !formatetc
->tymed
|| (formatetc
->tymed
== cache_entry
->fmtetc
.tymed
)))
277 /* checks that the clipformat and tymed are valid and returns an error if they
278 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
280 static HRESULT
check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat
, DWORD tymed
)
282 if (!cfFormat
|| !tymed
||
283 (cfFormat
== CF_METAFILEPICT
&& tymed
== TYMED_MFPICT
) ||
284 (cfFormat
== CF_BITMAP
&& tymed
== TYMED_GDI
) ||
285 (cfFormat
== CF_DIB
&& tymed
== TYMED_HGLOBAL
) ||
286 (cfFormat
== CF_ENHMETAFILE
&& tymed
== TYMED_ENHMF
))
288 else if (tymed
== TYMED_HGLOBAL
)
289 return CACHE_S_FORMATETC_NOTSUPPORTED
;
292 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat
, tymed
);
297 static HRESULT
DataCache_CreateEntry(DataCache
*This
, const FORMATETC
*formatetc
, DataCacheEntry
**cache_entry
)
301 hr
= check_valid_clipformat_and_tymed(formatetc
->cfFormat
, formatetc
->tymed
);
304 if (hr
== CACHE_S_FORMATETC_NOTSUPPORTED
)
305 TRACE("creating unsupported format %d\n", formatetc
->cfFormat
);
307 *cache_entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry
));
309 return E_OUTOFMEMORY
;
311 (*cache_entry
)->fmtetc
= *formatetc
;
314 (*cache_entry
)->fmtetc
.ptd
= HeapAlloc(GetProcessHeap(), 0, formatetc
->ptd
->tdSize
);
315 memcpy((*cache_entry
)->fmtetc
.ptd
, formatetc
->ptd
, formatetc
->ptd
->tdSize
);
317 (*cache_entry
)->data_cf
= 0;
318 (*cache_entry
)->stgmedium
.tymed
= TYMED_NULL
;
319 (*cache_entry
)->stgmedium
.pUnkForRelease
= NULL
;
320 (*cache_entry
)->storage
= NULL
;
321 (*cache_entry
)->id
= This
->last_cache_id
++;
322 (*cache_entry
)->dirty
= TRUE
;
323 (*cache_entry
)->stream_number
= -1;
324 (*cache_entry
)->sink_id
= 0;
325 (*cache_entry
)->advise_flags
= 0;
326 list_add_tail(&This
->cache_list
, &(*cache_entry
)->entry
);
330 /************************************************************************
331 * DataCache_FireOnViewChange
333 * This method will fire an OnViewChange notification to the advise
334 * sink registered with the datacache.
336 * See IAdviseSink::OnViewChange for more details.
338 static void DataCache_FireOnViewChange(
343 TRACE("(%p, %x, %d)\n", this, aspect
, lindex
);
346 * The sink supplies a filter when it registers
347 * we make sure we only send the notifications when that
350 if ((this->sinkAspects
& aspect
) != 0)
352 if (this->sinkInterface
!= NULL
)
354 IAdviseSink_OnViewChange(this->sinkInterface
,
359 * Some sinks want to be unregistered automatically when
360 * the first notification goes out.
362 if ( (this->sinkAdviseFlag
& ADVF_ONLYONCE
) != 0)
364 IAdviseSink_Release(this->sinkInterface
);
366 this->sinkInterface
= NULL
;
367 this->sinkAspects
= 0;
368 this->sinkAdviseFlag
= 0;
374 /* Helper for DataCacheEntry_OpenPresStream */
375 static BOOL
DataCache_IsPresentationStream(const STATSTG
*elem
)
377 /* The presentation streams have names of the form "\002OlePresXXX",
378 * where XXX goes from 000 to 999. */
379 static const WCHAR OlePres
[] = { 2,'O','l','e','P','r','e','s' };
381 LPCWSTR name
= elem
->pwcsName
;
383 return (elem
->type
== STGTY_STREAM
)
384 && (strlenW(name
) == 11)
385 && (strncmpW(name
, OlePres
, 8) == 0)
386 && (name
[8] >= '0') && (name
[8] <= '9')
387 && (name
[9] >= '0') && (name
[9] <= '9')
388 && (name
[10] >= '0') && (name
[10] <= '9');
391 static HRESULT
read_clipformat(IStream
*stream
, CLIPFORMAT
*clipformat
)
399 hr
= IStream_Read(stream
, &length
, sizeof(length
), &read
);
400 if (hr
!= S_OK
|| read
!= sizeof(length
))
401 return DV_E_CLIPFORMAT
;
405 hr
= IStream_Read(stream
, &cf
, sizeof(cf
), 0);
406 if (hr
!= S_OK
|| read
!= sizeof(cf
))
407 return DV_E_CLIPFORMAT
;
412 char *format_name
= HeapAlloc(GetProcessHeap(), 0, length
);
414 return E_OUTOFMEMORY
;
415 hr
= IStream_Read(stream
, format_name
, length
, &read
);
416 if (hr
!= S_OK
|| read
!= length
|| format_name
[length
- 1] != '\0')
418 HeapFree(GetProcessHeap(), 0, format_name
);
419 return DV_E_CLIPFORMAT
;
421 *clipformat
= RegisterClipboardFormatA(format_name
);
422 HeapFree(GetProcessHeap(), 0, format_name
);
427 static HRESULT
write_clipformat(IStream
*stream
, CLIPFORMAT clipformat
)
432 if (clipformat
< 0xc000)
435 length
= GetClipboardFormatNameA(clipformat
, NULL
, 0);
436 hr
= IStream_Write(stream
, &length
, sizeof(length
), NULL
);
439 if (clipformat
< 0xc000)
441 DWORD cf
= clipformat
;
442 hr
= IStream_Write(stream
, &cf
, sizeof(cf
), NULL
);
446 char *format_name
= HeapAlloc(GetProcessHeap(), 0, length
);
448 return E_OUTOFMEMORY
;
449 GetClipboardFormatNameA(clipformat
, format_name
, length
);
450 hr
= IStream_Write(stream
, format_name
, length
, NULL
);
451 HeapFree(GetProcessHeap(), 0, format_name
);
456 /************************************************************************
457 * DataCacheEntry_OpenPresStream
459 * This method will find the stream for the given presentation. It makes
460 * no attempt at fallback.
463 * this - Pointer to the DataCache object
464 * drawAspect - The aspect of the object that we wish to draw.
465 * pStm - A returned stream. It points to the beginning of the
466 * - presentation data, including the header.
469 * S_OK The requested stream has been opened.
470 * OLE_E_BLANK The requested stream could not be found.
471 * Quite a few others I'm too lazy to map correctly.
474 * Algorithm: Scan the elements of the presentation storage, looking
475 * for presentation streams. For each presentation stream,
476 * load the header and check to see if the aspect matches.
478 * If a fallback is desired, just opening the first presentation stream
481 static HRESULT
DataCacheEntry_OpenPresStream(DataCacheEntry
*cache_entry
, IStream
**ppStm
)
487 if (!ppStm
) return E_POINTER
;
489 hr
= IStorage_EnumElements(cache_entry
->storage
, 0, NULL
, 0, &pEnum
);
490 if (FAILED(hr
)) return hr
;
492 while ((hr
= IEnumSTATSTG_Next(pEnum
, 1, &elem
, NULL
)) == S_OK
)
494 if (DataCache_IsPresentationStream(&elem
))
498 hr
= IStorage_OpenStream(cache_entry
->storage
, elem
.pwcsName
,
499 NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0,
503 PresentationDataHeader header
;
505 CLIPFORMAT clipformat
;
507 hr
= read_clipformat(pStm
, &clipformat
);
510 hr
= IStream_Read(pStm
, &header
, sizeof(header
), &actual_read
);
512 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
513 if (hr
== S_OK
&& actual_read
== sizeof(header
)
514 && header
.dvAspect
== cache_entry
->fmtetc
.dwAspect
)
516 /* Rewind the stream before returning it. */
517 LARGE_INTEGER offset
;
518 offset
.u
.LowPart
= 0;
519 offset
.u
.HighPart
= 0;
520 IStream_Seek(pStm
, offset
, STREAM_SEEK_SET
, NULL
);
524 CoTaskMemFree(elem
.pwcsName
);
525 IEnumSTATSTG_Release(pEnum
);
530 IStream_Release(pStm
);
534 CoTaskMemFree(elem
.pwcsName
);
537 IEnumSTATSTG_Release(pEnum
);
539 return (hr
== S_FALSE
? OLE_E_BLANK
: hr
);
542 /************************************************************************
543 * DataCacheEntry_LoadData
545 * This method will read information for the requested presentation
546 * into the given structure.
549 * This - The entry to load the data from.
552 * This method returns a metafile handle if it is successful.
553 * it will return 0 if not.
555 static HRESULT
DataCacheEntry_LoadData(DataCacheEntry
*cache_entry
)
557 IStream
* presStream
= NULL
;
559 ULARGE_INTEGER current_pos
;
562 METAFILEPICT
*mfpict
;
564 PresentationDataHeader header
;
565 CLIPFORMAT clipformat
;
566 static const LARGE_INTEGER offset_zero
;
569 * Open the presentation stream.
571 hres
= DataCacheEntry_OpenPresStream(cache_entry
, &presStream
);
577 * Get the size of the stream.
579 hres
= IStream_Stat(presStream
,
587 hres
= read_clipformat(presStream
, &clipformat
);
590 IStream_Release(presStream
);
597 sizeof(PresentationDataHeader
),
601 IStream_Release(presStream
);
605 hres
= IStream_Seek(presStream
, offset_zero
, STREAM_SEEK_CUR
, ¤t_pos
);
607 streamInfo
.cbSize
.QuadPart
-= current_pos
.QuadPart
;
609 hmfpict
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(METAFILEPICT
));
612 IStream_Release(presStream
);
613 return E_OUTOFMEMORY
;
615 mfpict
= GlobalLock(hmfpict
);
618 * Allocate a buffer for the metafile bits.
620 metafileBits
= HeapAlloc(GetProcessHeap(),
622 streamInfo
.cbSize
.u
.LowPart
);
625 * Read the metafile bits.
630 streamInfo
.cbSize
.u
.LowPart
,
634 * Create a metafile with those bits.
638 /* FIXME: get this from the stream */
639 mfpict
->mm
= MM_ANISOTROPIC
;
640 mfpict
->xExt
= header
.dwObjectExtentX
;
641 mfpict
->yExt
= header
.dwObjectExtentY
;
642 mfpict
->hMF
= SetMetaFileBitsEx(streamInfo
.cbSize
.u
.LowPart
, metafileBits
);
647 GlobalUnlock(hmfpict
);
650 cache_entry
->data_cf
= cache_entry
->fmtetc
.cfFormat
;
651 cache_entry
->stgmedium
.tymed
= TYMED_MFPICT
;
652 cache_entry
->stgmedium
.u
.hMetaFilePict
= hmfpict
;
660 HeapFree(GetProcessHeap(), 0, metafileBits
);
661 IStream_Release(presStream
);
666 static HRESULT
DataCacheEntry_CreateStream(DataCacheEntry
*cache_entry
,
667 IStorage
*storage
, IStream
**stream
)
669 WCHAR wszName
[] = {2,'O','l','e','P','r','e','s',
670 '0' + (cache_entry
->stream_number
/ 100) % 10,
671 '0' + (cache_entry
->stream_number
/ 10) % 10,
672 '0' + cache_entry
->stream_number
% 10, 0};
674 /* FIXME: cache the created stream in This? */
675 return IStorage_CreateStream(storage
, wszName
,
676 STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
| STGM_CREATE
,
680 static HRESULT
DataCacheEntry_Save(DataCacheEntry
*cache_entry
, IStorage
*storage
,
683 PresentationDataHeader header
;
685 IStream
*pres_stream
;
688 TRACE("stream_number = %d, fmtetc = %s\n", cache_entry
->stream_number
, debugstr_formatetc(&cache_entry
->fmtetc
));
690 hr
= DataCacheEntry_CreateStream(cache_entry
, storage
, &pres_stream
);
694 hr
= write_clipformat(pres_stream
, cache_entry
->data_cf
);
698 if (cache_entry
->fmtetc
.ptd
)
699 FIXME("ptd not serialized\n");
701 header
.dvAspect
= cache_entry
->fmtetc
.dwAspect
;
702 header
.lindex
= cache_entry
->fmtetc
.lindex
;
703 header
.tymed
= cache_entry
->stgmedium
.tymed
;
705 header
.dwObjectExtentX
= 0;
706 header
.dwObjectExtentY
= 0;
710 switch (cache_entry
->data_cf
)
712 case CF_METAFILEPICT
:
714 if (cache_entry
->stgmedium
.tymed
!= TYMED_NULL
)
716 const METAFILEPICT
*mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
719 IStream_Release(pres_stream
);
720 return DV_E_STGMEDIUM
;
722 header
.dwObjectExtentX
= mfpict
->xExt
;
723 header
.dwObjectExtentY
= mfpict
->yExt
;
724 header
.dwSize
= GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
725 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
736 hr
= IStream_Write(pres_stream
, &header
, sizeof(PresentationDataHeader
),
740 IStream_Release(pres_stream
);
745 switch (cache_entry
->data_cf
)
747 case CF_METAFILEPICT
:
749 if (cache_entry
->stgmedium
.tymed
!= TYMED_NULL
)
751 const METAFILEPICT
*mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
754 IStream_Release(pres_stream
);
755 return DV_E_STGMEDIUM
;
757 data
= HeapAlloc(GetProcessHeap(), 0, header
.dwSize
);
758 GetMetaFileBitsEx(mfpict
->hMF
, header
.dwSize
, data
);
759 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
768 hr
= IStream_Write(pres_stream
, data
, header
.dwSize
, NULL
);
769 HeapFree(GetProcessHeap(), 0, data
);
771 IStream_Release(pres_stream
);
775 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
776 * does no checking of whether src_stgm has a supported tymed, so this should be
777 * done in the caller */
778 static HRESULT
copy_stg_medium(CLIPFORMAT cf
, STGMEDIUM
*dest_stgm
,
779 const STGMEDIUM
*src_stgm
)
781 if (src_stgm
->tymed
== TYMED_MFPICT
)
783 const METAFILEPICT
*src_mfpict
= GlobalLock(src_stgm
->u
.hMetaFilePict
);
784 METAFILEPICT
*dest_mfpict
;
787 return DV_E_STGMEDIUM
;
788 dest_stgm
->u
.hMetaFilePict
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(METAFILEPICT
));
789 dest_mfpict
= GlobalLock(dest_stgm
->u
.hMetaFilePict
);
792 GlobalUnlock(src_stgm
->u
.hMetaFilePict
);
793 return E_OUTOFMEMORY
;
795 *dest_mfpict
= *src_mfpict
;
796 dest_mfpict
->hMF
= CopyMetaFileW(src_mfpict
->hMF
, NULL
);
797 GlobalUnlock(src_stgm
->u
.hMetaFilePict
);
798 GlobalUnlock(dest_stgm
->u
.hMetaFilePict
);
800 else if (src_stgm
->tymed
!= TYMED_NULL
)
802 dest_stgm
->u
.hGlobal
= OleDuplicateData(src_stgm
->u
.hGlobal
, cf
,
804 if (!dest_stgm
->u
.hGlobal
)
805 return E_OUTOFMEMORY
;
807 dest_stgm
->tymed
= src_stgm
->tymed
;
808 dest_stgm
->pUnkForRelease
= src_stgm
->pUnkForRelease
;
809 if (dest_stgm
->pUnkForRelease
)
810 IUnknown_AddRef(dest_stgm
->pUnkForRelease
);
814 static HRESULT
DataCacheEntry_SetData(DataCacheEntry
*cache_entry
,
815 const FORMATETC
*formatetc
,
816 const STGMEDIUM
*stgmedium
,
819 if ((!cache_entry
->fmtetc
.cfFormat
&& !formatetc
->cfFormat
) ||
820 (cache_entry
->fmtetc
.tymed
== TYMED_NULL
&& formatetc
->tymed
== TYMED_NULL
) ||
821 stgmedium
->tymed
== TYMED_NULL
)
823 WARN("invalid formatetc\n");
824 return DV_E_FORMATETC
;
827 cache_entry
->dirty
= TRUE
;
828 ReleaseStgMedium(&cache_entry
->stgmedium
);
829 cache_entry
->data_cf
= cache_entry
->fmtetc
.cfFormat
? cache_entry
->fmtetc
.cfFormat
: formatetc
->cfFormat
;
832 cache_entry
->stgmedium
= *stgmedium
;
836 return copy_stg_medium(cache_entry
->data_cf
,
837 &cache_entry
->stgmedium
, stgmedium
);
840 static HRESULT
DataCacheEntry_GetData(DataCacheEntry
*cache_entry
, STGMEDIUM
*stgmedium
)
842 if (stgmedium
->tymed
== TYMED_NULL
&& cache_entry
->storage
)
844 HRESULT hr
= DataCacheEntry_LoadData(cache_entry
);
848 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
850 return copy_stg_medium(cache_entry
->data_cf
, stgmedium
, &cache_entry
->stgmedium
);
853 static inline HRESULT
DataCacheEntry_DiscardData(DataCacheEntry
*cache_entry
)
855 ReleaseStgMedium(&cache_entry
->stgmedium
);
856 cache_entry
->data_cf
= cache_entry
->fmtetc
.cfFormat
;
860 static inline void DataCacheEntry_HandsOffStorage(DataCacheEntry
*cache_entry
)
862 if (cache_entry
->storage
)
864 IStorage_Release(cache_entry
->storage
);
865 cache_entry
->storage
= NULL
;
869 /*********************************************************
870 * Method implementation for the non delegating IUnknown
871 * part of the DataCache class.
874 /************************************************************************
875 * DataCache_NDIUnknown_QueryInterface (IUnknown)
877 * This version of QueryInterface will not delegate its implementation
878 * to the outer unknown.
880 static HRESULT WINAPI
DataCache_NDIUnknown_QueryInterface(
885 DataCache
*this = impl_from_IUnknown(iface
);
892 if (IsEqualIID(&IID_IUnknown
, riid
))
896 else if (IsEqualIID(&IID_IDataObject
, riid
))
898 *ppvObject
= &this->IDataObject_iface
;
900 else if ( IsEqualIID(&IID_IPersistStorage
, riid
) ||
901 IsEqualIID(&IID_IPersist
, riid
) )
903 *ppvObject
= &this->IPersistStorage_iface
;
905 else if ( IsEqualIID(&IID_IViewObject
, riid
) ||
906 IsEqualIID(&IID_IViewObject2
, riid
) )
908 *ppvObject
= &this->IViewObject2_iface
;
910 else if ( IsEqualIID(&IID_IOleCache
, riid
) ||
911 IsEqualIID(&IID_IOleCache2
, riid
) )
913 *ppvObject
= &this->IOleCache2_iface
;
915 else if ( IsEqualIID(&IID_IOleCacheControl
, riid
) )
917 *ppvObject
= &this->IOleCacheControl_iface
;
922 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid
));
923 return E_NOINTERFACE
;
926 IUnknown_AddRef((IUnknown
*)*ppvObject
);
931 /************************************************************************
932 * DataCache_NDIUnknown_AddRef (IUnknown)
934 * This version of QueryInterface will not delegate its implementation
935 * to the outer unknown.
937 static ULONG WINAPI
DataCache_NDIUnknown_AddRef(
940 DataCache
*this = impl_from_IUnknown(iface
);
941 return InterlockedIncrement(&this->ref
);
944 /************************************************************************
945 * DataCache_NDIUnknown_Release (IUnknown)
947 * This version of QueryInterface will not delegate its implementation
948 * to the outer unknown.
950 static ULONG WINAPI
DataCache_NDIUnknown_Release(
953 DataCache
*this = impl_from_IUnknown(iface
);
956 ref
= InterlockedDecrement(&this->ref
);
958 if (ref
== 0) DataCache_Destroy(this);
963 /*********************************************************
964 * Method implementation for the IDataObject
965 * part of the DataCache class.
968 /************************************************************************
969 * DataCache_IDataObject_QueryInterface (IUnknown)
971 static HRESULT WINAPI
DataCache_IDataObject_QueryInterface(
976 DataCache
*this = impl_from_IDataObject(iface
);
978 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
981 /************************************************************************
982 * DataCache_IDataObject_AddRef (IUnknown)
984 static ULONG WINAPI
DataCache_IDataObject_AddRef(
987 DataCache
*this = impl_from_IDataObject(iface
);
989 return IUnknown_AddRef(this->outerUnknown
);
992 /************************************************************************
993 * DataCache_IDataObject_Release (IUnknown)
995 static ULONG WINAPI
DataCache_IDataObject_Release(
998 DataCache
*this = impl_from_IDataObject(iface
);
1000 return IUnknown_Release(this->outerUnknown
);
1003 /************************************************************************
1006 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1008 static HRESULT WINAPI
DataCache_GetData(
1010 LPFORMATETC pformatetcIn
,
1013 DataCache
*This
= impl_from_IDataObject(iface
);
1014 DataCacheEntry
*cache_entry
;
1016 memset(pmedium
, 0, sizeof(*pmedium
));
1018 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetcIn
);
1022 return DataCacheEntry_GetData(cache_entry
, pmedium
);
1025 static HRESULT WINAPI
DataCache_GetDataHere(
1027 LPFORMATETC pformatetc
,
1034 static HRESULT WINAPI
DataCache_QueryGetData(
1036 LPFORMATETC pformatetc
)
1042 /************************************************************************
1043 * DataCache_EnumFormatEtc (IDataObject)
1045 * The data cache doesn't implement this method.
1047 static HRESULT WINAPI
DataCache_GetCanonicalFormatEtc(
1049 LPFORMATETC pformatectIn
,
1050 LPFORMATETC pformatetcOut
)
1056 /************************************************************************
1057 * DataCache_IDataObject_SetData (IDataObject)
1059 * This method is delegated to the IOleCache2 implementation.
1061 static HRESULT WINAPI
DataCache_IDataObject_SetData(
1063 LPFORMATETC pformatetc
,
1067 IOleCache2
* oleCache
= NULL
;
1070 TRACE("(%p, %p, %p, %d)\n", iface
, pformatetc
, pmedium
, fRelease
);
1072 hres
= IDataObject_QueryInterface(iface
, &IID_IOleCache2
, (void**)&oleCache
);
1075 return E_UNEXPECTED
;
1077 hres
= IOleCache2_SetData(oleCache
, pformatetc
, pmedium
, fRelease
);
1079 IOleCache2_Release(oleCache
);
1084 /************************************************************************
1085 * DataCache_EnumFormatEtc (IDataObject)
1087 * The data cache doesn't implement this method.
1089 static HRESULT WINAPI
DataCache_EnumFormatEtc(
1092 IEnumFORMATETC
** ppenumFormatEtc
)
1098 /************************************************************************
1099 * DataCache_DAdvise (IDataObject)
1101 * The data cache doesn't support connections.
1103 static HRESULT WINAPI
DataCache_DAdvise(
1105 FORMATETC
* pformatetc
,
1107 IAdviseSink
* pAdvSink
,
1108 DWORD
* pdwConnection
)
1111 return OLE_E_ADVISENOTSUPPORTED
;
1114 /************************************************************************
1115 * DataCache_DUnadvise (IDataObject)
1117 * The data cache doesn't support connections.
1119 static HRESULT WINAPI
DataCache_DUnadvise(
1124 return OLE_E_NOCONNECTION
;
1127 /************************************************************************
1128 * DataCache_EnumDAdvise (IDataObject)
1130 * The data cache doesn't support connections.
1132 static HRESULT WINAPI
DataCache_EnumDAdvise(
1134 IEnumSTATDATA
** ppenumAdvise
)
1137 return OLE_E_ADVISENOTSUPPORTED
;
1140 /*********************************************************
1141 * Method implementation for the IDataObject
1142 * part of the DataCache class.
1145 /************************************************************************
1146 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1148 static HRESULT WINAPI
DataCache_IPersistStorage_QueryInterface(
1149 IPersistStorage
* iface
,
1153 DataCache
*this = impl_from_IPersistStorage(iface
);
1155 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
1158 /************************************************************************
1159 * DataCache_IPersistStorage_AddRef (IUnknown)
1161 static ULONG WINAPI
DataCache_IPersistStorage_AddRef(
1162 IPersistStorage
* iface
)
1164 DataCache
*this = impl_from_IPersistStorage(iface
);
1166 return IUnknown_AddRef(this->outerUnknown
);
1169 /************************************************************************
1170 * DataCache_IPersistStorage_Release (IUnknown)
1172 static ULONG WINAPI
DataCache_IPersistStorage_Release(
1173 IPersistStorage
* iface
)
1175 DataCache
*this = impl_from_IPersistStorage(iface
);
1177 return IUnknown_Release(this->outerUnknown
);
1180 /************************************************************************
1181 * DataCache_GetClassID (IPersistStorage)
1183 * The data cache doesn't implement this method.
1185 static HRESULT WINAPI
DataCache_GetClassID(
1186 IPersistStorage
* iface
,
1189 DataCache
*This
= impl_from_IPersistStorage(iface
);
1190 DataCacheEntry
*cache_entry
;
1192 TRACE("(%p, %p)\n", iface
, pClassID
);
1194 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1196 if (cache_entry
->storage
!= NULL
)
1199 HRESULT hr
= IStorage_Stat(cache_entry
->storage
, &statstg
, STATFLAG_NONAME
);
1202 *pClassID
= statstg
.clsid
;
1208 *pClassID
= CLSID_NULL
;
1213 /************************************************************************
1214 * DataCache_IsDirty (IPersistStorage)
1216 static HRESULT WINAPI
DataCache_IsDirty(
1217 IPersistStorage
* iface
)
1219 DataCache
*This
= impl_from_IPersistStorage(iface
);
1220 DataCacheEntry
*cache_entry
;
1222 TRACE("(%p)\n", iface
);
1227 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1228 if (cache_entry
->dirty
)
1234 /************************************************************************
1235 * DataCache_InitNew (IPersistStorage)
1237 * The data cache implementation of IPersistStorage_InitNew simply stores
1238 * the storage pointer.
1240 static HRESULT WINAPI
DataCache_InitNew(
1241 IPersistStorage
* iface
,
1244 DataCache
*This
= impl_from_IPersistStorage(iface
);
1246 TRACE("(%p, %p)\n", iface
, pStg
);
1248 if (This
->presentationStorage
!= NULL
)
1249 IStorage_Release(This
->presentationStorage
);
1251 This
->presentationStorage
= pStg
;
1253 IStorage_AddRef(This
->presentationStorage
);
1259 /************************************************************************
1260 * DataCache_Load (IPersistStorage)
1262 * The data cache implementation of IPersistStorage_Load doesn't
1263 * actually load anything. Instead, it holds on to the storage pointer
1264 * and it will load the presentation information when the
1265 * IDataObject_GetData or IViewObject2_Draw methods are called.
1267 static HRESULT WINAPI
DataCache_Load(
1268 IPersistStorage
* iface
,
1271 DataCache
*This
= impl_from_IPersistStorage(iface
);
1273 IEnumSTATSTG
*pEnum
;
1276 TRACE("(%p, %p)\n", iface
, pStg
);
1278 if (This
->presentationStorage
!= NULL
)
1279 IStorage_Release(This
->presentationStorage
);
1281 This
->presentationStorage
= pStg
;
1283 hr
= IStorage_EnumElements(pStg
, 0, NULL
, 0, &pEnum
);
1284 if (FAILED(hr
)) return hr
;
1286 while ((hr
= IEnumSTATSTG_Next(pEnum
, 1, &elem
, NULL
)) == S_OK
)
1288 if (DataCache_IsPresentationStream(&elem
))
1292 hr
= IStorage_OpenStream(This
->presentationStorage
, elem
.pwcsName
,
1293 NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0,
1297 PresentationDataHeader header
;
1299 CLIPFORMAT clipformat
;
1301 hr
= read_clipformat(pStm
, &clipformat
);
1304 hr
= IStream_Read(pStm
, &header
, sizeof(header
),
1307 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1308 if (hr
== S_OK
&& actual_read
== sizeof(header
))
1310 DataCacheEntry
*cache_entry
;
1313 fmtetc
.cfFormat
= clipformat
;
1314 fmtetc
.ptd
= NULL
; /* FIXME */
1315 fmtetc
.dwAspect
= header
.dvAspect
;
1316 fmtetc
.lindex
= header
.lindex
;
1317 fmtetc
.tymed
= header
.tymed
;
1319 TRACE("loading entry with formatetc: %s\n", debugstr_formatetc(&fmtetc
));
1321 cache_entry
= DataCache_GetEntryForFormatEtc(This
, &fmtetc
);
1323 hr
= DataCache_CreateEntry(This
, &fmtetc
, &cache_entry
);
1326 DataCacheEntry_DiscardData(cache_entry
);
1327 if (cache_entry
->storage
) IStorage_Release(cache_entry
->storage
);
1328 cache_entry
->storage
= pStg
;
1329 IStorage_AddRef(pStg
);
1330 cache_entry
->dirty
= FALSE
;
1334 IStream_Release(pStm
);
1338 CoTaskMemFree(elem
.pwcsName
);
1341 This
->dirty
= FALSE
;
1343 IEnumSTATSTG_Release(pEnum
);
1345 IStorage_AddRef(This
->presentationStorage
);
1349 /************************************************************************
1350 * DataCache_Save (IPersistStorage)
1352 * Until we actually connect to a running object and retrieve new
1353 * information to it, we never have to save anything. However, it is
1354 * our responsibility to copy the information when saving to a new
1357 static HRESULT WINAPI
DataCache_Save(
1358 IPersistStorage
* iface
,
1362 DataCache
*This
= impl_from_IPersistStorage(iface
);
1363 DataCacheEntry
*cache_entry
;
1366 unsigned short stream_number
= 0;
1368 TRACE("(%p, %p, %d)\n", iface
, pStg
, fSameAsLoad
);
1370 dirty
= This
->dirty
;
1373 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1375 dirty
= cache_entry
->dirty
;
1381 /* this is a shortcut if nothing changed */
1382 if (!dirty
&& !fSameAsLoad
&& This
->presentationStorage
)
1384 return IStorage_CopyTo(This
->presentationStorage
, 0, NULL
, NULL
, pStg
);
1387 /* assign stream numbers to the cache entries */
1388 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1390 if (cache_entry
->stream_number
!= stream_number
)
1392 cache_entry
->dirty
= TRUE
; /* needs to be written out again */
1393 cache_entry
->stream_number
= stream_number
;
1398 /* write out the cache entries */
1399 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1401 if (!fSameAsLoad
|| cache_entry
->dirty
)
1403 hr
= DataCacheEntry_Save(cache_entry
, pStg
, fSameAsLoad
);
1407 cache_entry
->dirty
= FALSE
;
1411 This
->dirty
= FALSE
;
1415 /************************************************************************
1416 * DataCache_SaveCompleted (IPersistStorage)
1418 * This method is called to tell the cache to release the storage
1419 * pointer it's currently holding.
1421 static HRESULT WINAPI
DataCache_SaveCompleted(
1422 IPersistStorage
* iface
,
1425 TRACE("(%p, %p)\n", iface
, pStgNew
);
1429 IPersistStorage_HandsOffStorage(iface
);
1431 DataCache_Load(iface
, pStgNew
);
1437 /************************************************************************
1438 * DataCache_HandsOffStorage (IPersistStorage)
1440 * This method is called to tell the cache to release the storage
1441 * pointer it's currently holding.
1443 static HRESULT WINAPI
DataCache_HandsOffStorage(
1444 IPersistStorage
* iface
)
1446 DataCache
*this = impl_from_IPersistStorage(iface
);
1447 DataCacheEntry
*cache_entry
;
1449 TRACE("(%p)\n", iface
);
1451 if (this->presentationStorage
!= NULL
)
1453 IStorage_Release(this->presentationStorage
);
1454 this->presentationStorage
= NULL
;
1457 LIST_FOR_EACH_ENTRY(cache_entry
, &this->cache_list
, DataCacheEntry
, entry
)
1458 DataCacheEntry_HandsOffStorage(cache_entry
);
1463 /*********************************************************
1464 * Method implementation for the IViewObject2
1465 * part of the DataCache class.
1468 /************************************************************************
1469 * DataCache_IViewObject2_QueryInterface (IUnknown)
1471 static HRESULT WINAPI
DataCache_IViewObject2_QueryInterface(
1472 IViewObject2
* iface
,
1476 DataCache
*this = impl_from_IViewObject2(iface
);
1478 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
1481 /************************************************************************
1482 * DataCache_IViewObject2_AddRef (IUnknown)
1484 static ULONG WINAPI
DataCache_IViewObject2_AddRef(
1485 IViewObject2
* iface
)
1487 DataCache
*this = impl_from_IViewObject2(iface
);
1489 return IUnknown_AddRef(this->outerUnknown
);
1492 /************************************************************************
1493 * DataCache_IViewObject2_Release (IUnknown)
1495 static ULONG WINAPI
DataCache_IViewObject2_Release(
1496 IViewObject2
* iface
)
1498 DataCache
*this = impl_from_IViewObject2(iface
);
1500 return IUnknown_Release(this->outerUnknown
);
1503 /************************************************************************
1504 * DataCache_Draw (IViewObject2)
1506 * This method will draw the cached representation of the object
1507 * to the given device context.
1509 static HRESULT WINAPI
DataCache_Draw(
1510 IViewObject2
* iface
,
1514 DVTARGETDEVICE
* ptd
,
1517 LPCRECTL lprcBounds
,
1518 LPCRECTL lprcWBounds
,
1519 BOOL (CALLBACK
*pfnContinue
)(ULONG_PTR dwContinue
),
1520 ULONG_PTR dwContinue
)
1522 DataCache
*This
= impl_from_IViewObject2(iface
);
1524 DataCacheEntry
*cache_entry
;
1526 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1538 if (lprcBounds
==NULL
)
1539 return E_INVALIDARG
;
1541 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1543 /* FIXME: compare ptd too */
1544 if ((cache_entry
->fmtetc
.dwAspect
!= dwDrawAspect
) ||
1545 (cache_entry
->fmtetc
.lindex
!= lindex
))
1548 /* if the data hasn't been loaded yet, do it now */
1549 if ((cache_entry
->stgmedium
.tymed
== TYMED_NULL
) && cache_entry
->storage
)
1551 hres
= DataCacheEntry_LoadData(cache_entry
);
1557 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
1560 if (pfnContinue
&& !pfnContinue(dwContinue
)) return E_ABORT
;
1562 switch (cache_entry
->data_cf
)
1564 case CF_METAFILEPICT
:
1567 * We have to be careful not to modify the state of the
1572 SIZE oldViewportExt
;
1573 POINT oldViewportOrg
;
1574 METAFILEPICT
*mfpict
;
1576 if ((cache_entry
->stgmedium
.tymed
!= TYMED_MFPICT
) ||
1577 !((mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
))))
1580 prevMapMode
= SetMapMode(hdcDraw
, mfpict
->mm
);
1582 SetWindowExtEx(hdcDraw
,
1587 SetViewportExtEx(hdcDraw
,
1588 lprcBounds
->right
- lprcBounds
->left
,
1589 lprcBounds
->bottom
- lprcBounds
->top
,
1592 SetViewportOrgEx(hdcDraw
,
1597 PlayMetaFile(hdcDraw
, mfpict
->hMF
);
1599 SetWindowExtEx(hdcDraw
,
1604 SetViewportExtEx(hdcDraw
,
1609 SetViewportOrgEx(hdcDraw
,
1614 SetMapMode(hdcDraw
, prevMapMode
);
1616 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
1623 WARN("no data could be found to be drawn\n");
1628 static HRESULT WINAPI
DataCache_GetColorSet(
1629 IViewObject2
* iface
,
1633 DVTARGETDEVICE
* ptd
,
1634 HDC hicTargetDevice
,
1635 LOGPALETTE
** ppColorSet
)
1641 static HRESULT WINAPI
DataCache_Freeze(
1642 IViewObject2
* iface
,
1652 static HRESULT WINAPI
DataCache_Unfreeze(
1653 IViewObject2
* iface
,
1660 /************************************************************************
1661 * DataCache_SetAdvise (IViewObject2)
1663 * This sets-up an advisory sink with the data cache. When the object's
1664 * view changes, this sink is called.
1666 static HRESULT WINAPI
DataCache_SetAdvise(
1667 IViewObject2
* iface
,
1670 IAdviseSink
* pAdvSink
)
1672 DataCache
*this = impl_from_IViewObject2(iface
);
1674 TRACE("(%p, %x, %x, %p)\n", iface
, aspects
, advf
, pAdvSink
);
1677 * A call to this function removes the previous sink
1679 if (this->sinkInterface
!= NULL
)
1681 IAdviseSink_Release(this->sinkInterface
);
1682 this->sinkInterface
= NULL
;
1683 this->sinkAspects
= 0;
1684 this->sinkAdviseFlag
= 0;
1688 * Now, setup the new one.
1692 this->sinkInterface
= pAdvSink
;
1693 this->sinkAspects
= aspects
;
1694 this->sinkAdviseFlag
= advf
;
1696 IAdviseSink_AddRef(this->sinkInterface
);
1700 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1703 if (advf
& ADVF_PRIMEFIRST
)
1705 DataCache_FireOnViewChange(this, aspects
, -1);
1711 /************************************************************************
1712 * DataCache_GetAdvise (IViewObject2)
1714 * This method queries the current state of the advise sink
1715 * installed on the data cache.
1717 static HRESULT WINAPI
DataCache_GetAdvise(
1718 IViewObject2
* iface
,
1721 IAdviseSink
** ppAdvSink
)
1723 DataCache
*this = impl_from_IViewObject2(iface
);
1725 TRACE("(%p, %p, %p, %p)\n", iface
, pAspects
, pAdvf
, ppAdvSink
);
1728 * Just copy all the requested values.
1731 *pAspects
= this->sinkAspects
;
1734 *pAdvf
= this->sinkAdviseFlag
;
1736 if (ppAdvSink
!=NULL
)
1738 if (this->sinkInterface
!= NULL
)
1739 IAdviseSink_QueryInterface(this->sinkInterface
,
1742 else *ppAdvSink
= NULL
;
1748 /************************************************************************
1749 * DataCache_GetExtent (IViewObject2)
1751 * This method retrieves the "natural" size of this cached object.
1753 static HRESULT WINAPI
DataCache_GetExtent(
1754 IViewObject2
* iface
,
1757 DVTARGETDEVICE
* ptd
,
1760 DataCache
*This
= impl_from_IViewObject2(iface
);
1761 HRESULT hres
= E_FAIL
;
1762 DataCacheEntry
*cache_entry
;
1764 TRACE("(%p, %x, %d, %p, %p)\n",
1765 iface
, dwDrawAspect
, lindex
, ptd
, lpsizel
);
1774 FIXME("Unimplemented flag lindex = %d\n", lindex
);
1777 * Right now, we support only the callback from
1778 * the default handler.
1781 FIXME("Unimplemented ptd = %p\n", ptd
);
1783 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1785 /* FIXME: compare ptd too */
1786 if ((cache_entry
->fmtetc
.dwAspect
!= dwDrawAspect
) ||
1787 (cache_entry
->fmtetc
.lindex
!= lindex
))
1790 /* if the data hasn't been loaded yet, do it now */
1791 if ((cache_entry
->stgmedium
.tymed
== TYMED_NULL
) && cache_entry
->storage
)
1793 hres
= DataCacheEntry_LoadData(cache_entry
);
1799 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
1803 switch (cache_entry
->data_cf
)
1805 case CF_METAFILEPICT
:
1807 METAFILEPICT
*mfpict
;
1809 if ((cache_entry
->stgmedium
.tymed
!= TYMED_MFPICT
) ||
1810 !((mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
))))
1813 lpsizel
->cx
= mfpict
->xExt
;
1814 lpsizel
->cy
= mfpict
->yExt
;
1816 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
1823 WARN("no data could be found to get the extents from\n");
1826 * This method returns OLE_E_BLANK when it fails.
1832 /*********************************************************
1833 * Method implementation for the IOleCache2
1834 * part of the DataCache class.
1837 /************************************************************************
1838 * DataCache_IOleCache2_QueryInterface (IUnknown)
1840 static HRESULT WINAPI
DataCache_IOleCache2_QueryInterface(
1845 DataCache
*this = impl_from_IOleCache2(iface
);
1847 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
1850 /************************************************************************
1851 * DataCache_IOleCache2_AddRef (IUnknown)
1853 static ULONG WINAPI
DataCache_IOleCache2_AddRef(
1856 DataCache
*this = impl_from_IOleCache2(iface
);
1858 return IUnknown_AddRef(this->outerUnknown
);
1861 /************************************************************************
1862 * DataCache_IOleCache2_Release (IUnknown)
1864 static ULONG WINAPI
DataCache_IOleCache2_Release(
1867 DataCache
*this = impl_from_IOleCache2(iface
);
1869 return IUnknown_Release(this->outerUnknown
);
1872 /*****************************************************************************
1875 * Set up the sink connection to the running object.
1877 static HRESULT
setup_sink(DataCache
*This
, DataCacheEntry
*cache_entry
)
1879 HRESULT hr
= S_FALSE
;
1882 /* Clear the ADVFCACHE_* bits. Native also sets the two highest bits for some reason. */
1883 flags
= cache_entry
->advise_flags
& ~(ADVFCACHE_NOHANDLER
| ADVFCACHE_FORCEBUILTIN
| ADVFCACHE_ONSAVE
);
1885 if(This
->running_object
)
1886 if(!(flags
& ADVF_NODATA
))
1887 hr
= IDataObject_DAdvise(This
->running_object
, &cache_entry
->fmtetc
, flags
,
1888 &This
->IAdviseSink_iface
, &cache_entry
->sink_id
);
1892 static HRESULT WINAPI
DataCache_Cache(
1894 FORMATETC
* pformatetc
,
1896 DWORD
* pdwConnection
)
1898 DataCache
*This
= impl_from_IOleCache2(iface
);
1899 DataCacheEntry
*cache_entry
;
1902 TRACE("(%p, 0x%x, %p)\n", pformatetc
, advf
, pdwConnection
);
1904 if (!pformatetc
|| !pdwConnection
)
1905 return E_INVALIDARG
;
1907 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc
));
1911 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetc
);
1914 TRACE("found an existing cache entry\n");
1915 *pdwConnection
= cache_entry
->id
;
1916 return CACHE_S_SAMECACHE
;
1919 hr
= DataCache_CreateEntry(This
, pformatetc
, &cache_entry
);
1923 *pdwConnection
= cache_entry
->id
;
1924 cache_entry
->advise_flags
= advf
;
1925 setup_sink(This
, cache_entry
);
1931 static HRESULT WINAPI
DataCache_Uncache(
1935 DataCache
*This
= impl_from_IOleCache2(iface
);
1936 DataCacheEntry
*cache_entry
;
1938 TRACE("(%d)\n", dwConnection
);
1940 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1941 if (cache_entry
->id
== dwConnection
)
1943 DataCacheEntry_Destroy(This
, cache_entry
);
1947 WARN("no connection found for %d\n", dwConnection
);
1949 return OLE_E_NOCONNECTION
;
1952 static HRESULT WINAPI
DataCache_EnumCache(
1954 IEnumSTATDATA
** ppenumSTATDATA
)
1960 static HRESULT WINAPI
DataCache_InitCache(
1962 IDataObject
* pDataObject
)
1968 static HRESULT WINAPI
DataCache_IOleCache2_SetData(
1970 FORMATETC
* pformatetc
,
1974 DataCache
*This
= impl_from_IOleCache2(iface
);
1975 DataCacheEntry
*cache_entry
;
1978 TRACE("(%p, %p, %s)\n", pformatetc
, pmedium
, fRelease
? "TRUE" : "FALSE");
1979 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc
));
1981 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetc
);
1984 hr
= DataCacheEntry_SetData(cache_entry
, pformatetc
, pmedium
, fRelease
);
1987 DataCache_FireOnViewChange(This
, cache_entry
->fmtetc
.dwAspect
,
1988 cache_entry
->fmtetc
.lindex
);
1992 WARN("cache entry not found\n");
1997 static HRESULT WINAPI
DataCache_UpdateCache(
1999 LPDATAOBJECT pDataObject
,
2003 FIXME("(%p, 0x%x, %p): stub\n", pDataObject
, grfUpdf
, pReserved
);
2007 static HRESULT WINAPI
DataCache_DiscardCache(
2009 DWORD dwDiscardOptions
)
2011 DataCache
*This
= impl_from_IOleCache2(iface
);
2012 DataCacheEntry
*cache_entry
;
2015 TRACE("(%d)\n", dwDiscardOptions
);
2017 if (dwDiscardOptions
== DISCARDCACHE_SAVEIFDIRTY
)
2018 hr
= DataCache_Save(&This
->IPersistStorage_iface
, This
->presentationStorage
, TRUE
);
2020 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2022 hr
= DataCacheEntry_DiscardData(cache_entry
);
2031 /*********************************************************
2032 * Method implementation for the IOleCacheControl
2033 * part of the DataCache class.
2036 /************************************************************************
2037 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2039 static HRESULT WINAPI
DataCache_IOleCacheControl_QueryInterface(
2040 IOleCacheControl
* iface
,
2044 DataCache
*this = impl_from_IOleCacheControl(iface
);
2046 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
2049 /************************************************************************
2050 * DataCache_IOleCacheControl_AddRef (IUnknown)
2052 static ULONG WINAPI
DataCache_IOleCacheControl_AddRef(
2053 IOleCacheControl
* iface
)
2055 DataCache
*this = impl_from_IOleCacheControl(iface
);
2057 return IUnknown_AddRef(this->outerUnknown
);
2060 /************************************************************************
2061 * DataCache_IOleCacheControl_Release (IUnknown)
2063 static ULONG WINAPI
DataCache_IOleCacheControl_Release(
2064 IOleCacheControl
* iface
)
2066 DataCache
*this = impl_from_IOleCacheControl(iface
);
2068 return IUnknown_Release(this->outerUnknown
);
2071 /************************************************************************
2072 * DataCache_OnRun (IOleCacheControl)
2074 static HRESULT WINAPI
DataCache_OnRun(IOleCacheControl
* iface
, IDataObject
*data_obj
)
2076 DataCache
*This
= impl_from_IOleCacheControl(iface
);
2077 DataCacheEntry
*cache_entry
;
2079 TRACE("(%p)->(%p)\n", iface
, data_obj
);
2081 if(This
->running_object
) return S_OK
;
2083 /* No reference is taken on the data object */
2084 This
->running_object
= data_obj
;
2086 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2088 setup_sink(This
, cache_entry
);
2094 /************************************************************************
2095 * DataCache_OnStop (IOleCacheControl)
2097 static HRESULT WINAPI
DataCache_OnStop(IOleCacheControl
* iface
)
2099 DataCache
*This
= impl_from_IOleCacheControl(iface
);
2100 DataCacheEntry
*cache_entry
;
2102 TRACE("(%p)\n", iface
);
2104 if(!This
->running_object
) return S_OK
;
2106 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2108 if(cache_entry
->sink_id
)
2110 IDataObject_DUnadvise(This
->running_object
, cache_entry
->sink_id
);
2111 cache_entry
->sink_id
= 0;
2115 /* No ref taken in OnRun, so no Release call here */
2116 This
->running_object
= NULL
;
2120 /************************************************************************
2121 * IAdviseSink methods.
2122 * This behaves as an internal object to the data cache. QI'ing its ptr doesn't
2123 * give access to the cache's other interfaces. We don't maintain a ref count,
2124 * the object exists as long as the cache is around.
2126 static HRESULT WINAPI
DataCache_IAdviseSink_QueryInterface(IAdviseSink
*iface
, REFIID iid
, void **obj
)
2129 if (IsEqualIID(&IID_IUnknown
, iid
) ||
2130 IsEqualIID(&IID_IAdviseSink
, iid
))
2137 IAdviseSink_AddRef(iface
);
2140 return E_NOINTERFACE
;
2143 static ULONG WINAPI
DataCache_IAdviseSink_AddRef(IAdviseSink
*iface
)
2148 static ULONG WINAPI
DataCache_IAdviseSink_Release(IAdviseSink
*iface
)
2153 static void WINAPI
DataCache_OnDataChange(IAdviseSink
*iface
, FORMATETC
*fmt
, STGMEDIUM
*med
)
2155 DataCache
*This
= impl_from_IAdviseSink(iface
);
2156 TRACE("(%p)->(%s, %p)\n", This
, debugstr_formatetc(fmt
), med
);
2157 IOleCache2_SetData(&This
->IOleCache2_iface
, fmt
, med
, FALSE
);
2160 static void WINAPI
DataCache_OnViewChange(IAdviseSink
*iface
, DWORD aspect
, LONG index
)
2165 static void WINAPI
DataCache_OnRename(IAdviseSink
*iface
, IMoniker
*mk
)
2170 static void WINAPI
DataCache_OnSave(IAdviseSink
*iface
)
2175 static void WINAPI
DataCache_OnClose(IAdviseSink
*iface
)
2181 * Virtual function tables for the DataCache class.
2183 static const IUnknownVtbl DataCache_NDIUnknown_VTable
=
2185 DataCache_NDIUnknown_QueryInterface
,
2186 DataCache_NDIUnknown_AddRef
,
2187 DataCache_NDIUnknown_Release
2190 static const IDataObjectVtbl DataCache_IDataObject_VTable
=
2192 DataCache_IDataObject_QueryInterface
,
2193 DataCache_IDataObject_AddRef
,
2194 DataCache_IDataObject_Release
,
2196 DataCache_GetDataHere
,
2197 DataCache_QueryGetData
,
2198 DataCache_GetCanonicalFormatEtc
,
2199 DataCache_IDataObject_SetData
,
2200 DataCache_EnumFormatEtc
,
2202 DataCache_DUnadvise
,
2203 DataCache_EnumDAdvise
2206 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable
=
2208 DataCache_IPersistStorage_QueryInterface
,
2209 DataCache_IPersistStorage_AddRef
,
2210 DataCache_IPersistStorage_Release
,
2211 DataCache_GetClassID
,
2216 DataCache_SaveCompleted
,
2217 DataCache_HandsOffStorage
2220 static const IViewObject2Vtbl DataCache_IViewObject2_VTable
=
2222 DataCache_IViewObject2_QueryInterface
,
2223 DataCache_IViewObject2_AddRef
,
2224 DataCache_IViewObject2_Release
,
2226 DataCache_GetColorSet
,
2229 DataCache_SetAdvise
,
2230 DataCache_GetAdvise
,
2234 static const IOleCache2Vtbl DataCache_IOleCache2_VTable
=
2236 DataCache_IOleCache2_QueryInterface
,
2237 DataCache_IOleCache2_AddRef
,
2238 DataCache_IOleCache2_Release
,
2241 DataCache_EnumCache
,
2242 DataCache_InitCache
,
2243 DataCache_IOleCache2_SetData
,
2244 DataCache_UpdateCache
,
2245 DataCache_DiscardCache
2248 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable
=
2250 DataCache_IOleCacheControl_QueryInterface
,
2251 DataCache_IOleCacheControl_AddRef
,
2252 DataCache_IOleCacheControl_Release
,
2257 static const IAdviseSinkVtbl DataCache_IAdviseSink_VTable
=
2259 DataCache_IAdviseSink_QueryInterface
,
2260 DataCache_IAdviseSink_AddRef
,
2261 DataCache_IAdviseSink_Release
,
2262 DataCache_OnDataChange
,
2263 DataCache_OnViewChange
,
2269 /*********************************************************
2270 * Method implementation for DataCache class.
2272 static DataCache
* DataCache_Construct(
2274 LPUNKNOWN pUnkOuter
)
2276 DataCache
* newObject
= 0;
2279 * Allocate space for the object.
2281 newObject
= HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache
));
2287 * Initialize the virtual function table.
2289 newObject
->IDataObject_iface
.lpVtbl
= &DataCache_IDataObject_VTable
;
2290 newObject
->IUnknown_iface
.lpVtbl
= &DataCache_NDIUnknown_VTable
;
2291 newObject
->IPersistStorage_iface
.lpVtbl
= &DataCache_IPersistStorage_VTable
;
2292 newObject
->IViewObject2_iface
.lpVtbl
= &DataCache_IViewObject2_VTable
;
2293 newObject
->IOleCache2_iface
.lpVtbl
= &DataCache_IOleCache2_VTable
;
2294 newObject
->IOleCacheControl_iface
.lpVtbl
= &DataCache_IOleCacheControl_VTable
;
2295 newObject
->IAdviseSink_iface
.lpVtbl
= &DataCache_IAdviseSink_VTable
;
2298 * Start with one reference count. The caller of this function
2299 * must release the interface pointer when it is done.
2304 * Initialize the outer unknown
2305 * We don't keep a reference on the outer unknown since, the way
2306 * aggregation works, our lifetime is at least as large as its
2309 if (pUnkOuter
==NULL
)
2310 pUnkOuter
= &newObject
->IUnknown_iface
;
2312 newObject
->outerUnknown
= pUnkOuter
;
2315 * Initialize the other members of the structure.
2317 newObject
->sinkAspects
= 0;
2318 newObject
->sinkAdviseFlag
= 0;
2319 newObject
->sinkInterface
= 0;
2320 newObject
->presentationStorage
= NULL
;
2321 list_init(&newObject
->cache_list
);
2322 newObject
->last_cache_id
= 1;
2323 newObject
->dirty
= FALSE
;
2324 newObject
->running_object
= NULL
;
2329 /******************************************************************************
2330 * CreateDataCache [OLE32.@]
2332 * Creates a data cache to allow an object to render one or more of its views,
2333 * whether running or not.
2336 * pUnkOuter [I] Outer unknown for the object.
2338 * riid [I] IID of interface to return.
2339 * ppvObj [O] Address where the data cache object will be stored on return.
2343 * Failure: HRESULT code.
2346 * The following interfaces are supported by the returned data cache object:
2347 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorage, IDataObject,
2348 * IViewObject and IViewObject2.
2350 HRESULT WINAPI
CreateDataCache(
2351 LPUNKNOWN pUnkOuter
,
2356 DataCache
* newCache
= NULL
;
2359 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid
), pUnkOuter
, debugstr_guid(riid
), ppvObj
);
2370 * If this cache is constructed for aggregation, make sure
2371 * the caller is requesting the IUnknown interface.
2372 * This is necessary because it's the only time the non-delegating
2373 * IUnknown pointer can be returned to the outside.
2375 if ( pUnkOuter
&& !IsEqualIID(&IID_IUnknown
, riid
) )
2376 return CLASS_E_NOAGGREGATION
;
2379 * Try to construct a new instance of the class.
2381 newCache
= DataCache_Construct(rclsid
,
2385 return E_OUTOFMEMORY
;
2388 * Make sure it supports the interface required by the caller.
2390 hr
= IUnknown_QueryInterface(&newCache
->IUnknown_iface
, riid
, ppvObj
);
2393 * Release the reference obtained in the constructor. If
2394 * the QueryInterface was unsuccessful, it will free the class.
2396 IUnknown_Release(&newCache
->IUnknown_iface
);