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 typedef struct PresentationDataHeader
74 * - standard clipformat:
75 * DWORD length = 0xffffffff;
77 * - or custom clipformat:
79 * CHAR format_name[length]; (null-terminated)
81 DWORD unknown3
; /* 4, possibly TYMED_ISTREAM */
85 DWORD unknown7
; /* 0 */
86 DWORD dwObjectExtentX
;
87 DWORD dwObjectExtentY
;
89 } PresentationDataHeader
;
91 typedef struct DataCacheEntry
94 /* format of this entry */
96 /* the clipboard format of the data */
101 * This storage pointer is set through a call to
102 * IPersistStorage_Load. This is where the visual
103 * representation of the object is stored.
110 /* stream number (-1 if not set ) */
111 unsigned short stream_number
;
114 /****************************************************************************
120 * List all interface VTables here
122 const IDataObjectVtbl
* lpVtbl
;
123 const IUnknownVtbl
* lpvtblNDIUnknown
;
124 const IPersistStorageVtbl
* lpvtblIPersistStorage
;
125 const IViewObject2Vtbl
* lpvtblIViewObject
;
126 const IOleCache2Vtbl
* lpvtblIOleCache2
;
127 const IOleCacheControlVtbl
* lpvtblIOleCacheControl
;
130 * Reference count of this object
135 * IUnknown implementation of the outer object.
137 IUnknown
* outerUnknown
;
140 * The user of this object can setup ONE advise sink
141 * connection with the object. These parameters describe
145 DWORD sinkAdviseFlag
;
146 IAdviseSink
* sinkInterface
;
147 IStorage
*presentationStorage
;
149 /* list of cache entries */
150 struct list cache_list
;
151 /* last id assigned to an entry */
157 typedef struct DataCache DataCache
;
160 * Here, I define utility macros to help with the casting of the
162 * There is a version to accommodate all of the VTables implemented
166 static inline DataCache
*impl_from_IDataObject( IDataObject
*iface
)
168 return (DataCache
*)((char*)iface
- FIELD_OFFSET(DataCache
, lpVtbl
));
171 static inline DataCache
*impl_from_NDIUnknown( IUnknown
*iface
)
173 return (DataCache
*)((char*)iface
- FIELD_OFFSET(DataCache
, lpvtblNDIUnknown
));
176 static inline DataCache
*impl_from_IPersistStorage( IPersistStorage
*iface
)
178 return (DataCache
*)((char*)iface
- FIELD_OFFSET(DataCache
, lpvtblIPersistStorage
));
181 static inline DataCache
*impl_from_IViewObject2( IViewObject2
*iface
)
183 return (DataCache
*)((char*)iface
- FIELD_OFFSET(DataCache
, lpvtblIViewObject
));
186 static inline DataCache
*impl_from_IOleCache2( IOleCache2
*iface
)
188 return (DataCache
*)((char*)iface
- FIELD_OFFSET(DataCache
, lpvtblIOleCache2
));
191 static inline DataCache
*impl_from_IOleCacheControl( IOleCacheControl
*iface
)
193 return (DataCache
*)((char*)iface
- FIELD_OFFSET(DataCache
, lpvtblIOleCacheControl
));
196 static const char * debugstr_formatetc(const FORMATETC
*formatetc
)
198 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
199 formatetc
->cfFormat
, formatetc
->ptd
, formatetc
->dwAspect
,
200 formatetc
->lindex
, formatetc
->tymed
);
204 * Prototypes for the methods of the DataCache class.
206 static DataCache
* DataCache_Construct(REFCLSID clsid
,
207 LPUNKNOWN pUnkOuter
);
208 static HRESULT
DataCacheEntry_OpenPresStream(DataCacheEntry
*This
,
211 static void DataCacheEntry_Destroy(DataCacheEntry
*This
)
213 list_remove(&This
->entry
);
215 IStorage_Release(This
->storage
);
216 HeapFree(GetProcessHeap(), 0, This
->fmtetc
.ptd
);
217 ReleaseStgMedium(&This
->stgmedium
);
218 HeapFree(GetProcessHeap(), 0, This
);
221 static void DataCache_Destroy(
222 DataCache
* ptrToDestroy
)
224 DataCacheEntry
*cache_entry
, *next_cache_entry
;
228 if (ptrToDestroy
->sinkInterface
!= NULL
)
230 IAdviseSink_Release(ptrToDestroy
->sinkInterface
);
231 ptrToDestroy
->sinkInterface
= NULL
;
234 LIST_FOR_EACH_ENTRY_SAFE(cache_entry
, next_cache_entry
, &ptrToDestroy
->cache_list
, DataCacheEntry
, entry
)
235 DataCacheEntry_Destroy(cache_entry
);
238 * Free the datacache pointer.
240 HeapFree(GetProcessHeap(), 0, ptrToDestroy
);
243 static DataCacheEntry
*DataCache_GetEntryForFormatEtc(DataCache
*This
, const FORMATETC
*formatetc
)
245 DataCacheEntry
*cache_entry
;
246 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
248 /* FIXME: also compare DVTARGETDEVICEs */
249 if ((!cache_entry
->fmtetc
.cfFormat
|| !formatetc
->cfFormat
|| (formatetc
->cfFormat
== cache_entry
->fmtetc
.cfFormat
)) &&
250 (formatetc
->dwAspect
== cache_entry
->fmtetc
.dwAspect
) &&
251 (formatetc
->lindex
== cache_entry
->fmtetc
.lindex
) &&
252 (!cache_entry
->fmtetc
.tymed
|| !formatetc
->tymed
|| (formatetc
->tymed
== cache_entry
->fmtetc
.tymed
)))
258 /* checks that the clipformat and tymed are valid and returns an error if they
259 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
261 static HRESULT
check_valid_clipformat_and_tymed(CLIPFORMAT cfFormat
, DWORD tymed
)
263 if (!cfFormat
|| !tymed
||
264 (cfFormat
== CF_METAFILEPICT
&& tymed
== TYMED_MFPICT
) ||
265 (cfFormat
== CF_BITMAP
&& tymed
== TYMED_GDI
) ||
266 (cfFormat
== CF_DIB
&& tymed
== TYMED_HGLOBAL
) ||
267 (cfFormat
== CF_ENHMETAFILE
&& tymed
== TYMED_ENHMF
))
269 else if (tymed
== TYMED_HGLOBAL
)
270 return CACHE_S_FORMATETC_NOTSUPPORTED
;
273 WARN("invalid clipformat/tymed combination: %d/%d\n", cfFormat
, tymed
);
278 static HRESULT
DataCache_CreateEntry(DataCache
*This
, const FORMATETC
*formatetc
, DataCacheEntry
**cache_entry
)
282 hr
= check_valid_clipformat_and_tymed(formatetc
->cfFormat
, formatetc
->tymed
);
285 if (hr
== CACHE_S_FORMATETC_NOTSUPPORTED
)
286 TRACE("creating unsupported format %d\n", formatetc
->cfFormat
);
288 *cache_entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(**cache_entry
));
290 return E_OUTOFMEMORY
;
292 (*cache_entry
)->fmtetc
= *formatetc
;
295 (*cache_entry
)->fmtetc
.ptd
= HeapAlloc(GetProcessHeap(), 0, formatetc
->ptd
->tdSize
);
296 memcpy((*cache_entry
)->fmtetc
.ptd
, formatetc
->ptd
, formatetc
->ptd
->tdSize
);
298 (*cache_entry
)->stgmedium
.tymed
= TYMED_NULL
;
299 (*cache_entry
)->stgmedium
.pUnkForRelease
= NULL
;
300 (*cache_entry
)->storage
= NULL
;
301 (*cache_entry
)->id
= This
->last_cache_id
++;
302 (*cache_entry
)->dirty
= TRUE
;
303 (*cache_entry
)->stream_number
= -1;
304 list_add_tail(&This
->cache_list
, &(*cache_entry
)->entry
);
308 /************************************************************************
309 * DataCache_FireOnViewChange
311 * This method will fire an OnViewChange notification to the advise
312 * sink registered with the datacache.
314 * See IAdviseSink::OnViewChange for more details.
316 static void DataCache_FireOnViewChange(
321 TRACE("(%p, %x, %d)\n", this, aspect
, lindex
);
324 * The sink supplies a filter when it registers
325 * we make sure we only send the notifications when that
328 if ((this->sinkAspects
& aspect
) != 0)
330 if (this->sinkInterface
!= NULL
)
332 IAdviseSink_OnViewChange(this->sinkInterface
,
337 * Some sinks want to be unregistered automatically when
338 * the first notification goes out.
340 if ( (this->sinkAdviseFlag
& ADVF_ONLYONCE
) != 0)
342 IAdviseSink_Release(this->sinkInterface
);
344 this->sinkInterface
= NULL
;
345 this->sinkAspects
= 0;
346 this->sinkAdviseFlag
= 0;
352 /* Helper for DataCacheEntry_OpenPresStream */
353 static BOOL
DataCache_IsPresentationStream(const STATSTG
*elem
)
355 /* The presentation streams have names of the form "\002OlePresXXX",
356 * where XXX goes from 000 to 999. */
357 static const WCHAR OlePres
[] = { 2,'O','l','e','P','r','e','s' };
359 LPCWSTR name
= elem
->pwcsName
;
361 return (elem
->type
== STGTY_STREAM
)
362 && (strlenW(name
) == 11)
363 && (strncmpW(name
, OlePres
, 8) == 0)
364 && (name
[8] >= '0') && (name
[8] <= '9')
365 && (name
[9] >= '0') && (name
[9] <= '9')
366 && (name
[10] >= '0') && (name
[10] <= '9');
369 static HRESULT
read_clipformat(IStream
*stream
, CLIPFORMAT
*clipformat
)
377 hr
= IStream_Read(stream
, &length
, sizeof(length
), &read
);
378 if (hr
!= S_OK
|| read
!= sizeof(length
))
379 return DV_E_CLIPFORMAT
;
383 hr
= IStream_Read(stream
, &cf
, sizeof(cf
), 0);
384 if (hr
!= S_OK
|| read
!= sizeof(cf
))
385 return DV_E_CLIPFORMAT
;
390 char *format_name
= HeapAlloc(GetProcessHeap(), 0, length
);
392 return E_OUTOFMEMORY
;
393 hr
= IStream_Read(stream
, format_name
, length
, &read
);
394 if (hr
!= S_OK
|| read
!= length
|| format_name
[length
- 1] != '\0')
396 HeapFree(GetProcessHeap(), 0, format_name
);
397 return DV_E_CLIPFORMAT
;
399 *clipformat
= RegisterClipboardFormatA(format_name
);
400 HeapFree(GetProcessHeap(), 0, format_name
);
405 static HRESULT
write_clipformat(IStream
*stream
, CLIPFORMAT clipformat
)
410 if (clipformat
< 0xc000)
413 length
= GetClipboardFormatNameA(clipformat
, NULL
, 0);
414 hr
= IStream_Write(stream
, &length
, sizeof(length
), NULL
);
417 if (clipformat
< 0xc000)
419 DWORD cf
= clipformat
;
420 hr
= IStream_Write(stream
, &cf
, sizeof(cf
), NULL
);
424 char *format_name
= HeapAlloc(GetProcessHeap(), 0, length
);
426 return E_OUTOFMEMORY
;
427 GetClipboardFormatNameA(clipformat
, format_name
, length
);
428 hr
= IStream_Write(stream
, format_name
, length
, NULL
);
429 HeapFree(GetProcessHeap(), 0, format_name
);
434 /************************************************************************
435 * DataCacheEntry_OpenPresStream
437 * This method will find the stream for the given presentation. It makes
438 * no attempt at fallback.
441 * this - Pointer to the DataCache object
442 * drawAspect - The aspect of the object that we wish to draw.
443 * pStm - A returned stream. It points to the beginning of the
444 * - presentation data, including the header.
447 * S_OK The requested stream has been opened.
448 * OLE_E_BLANK The requested stream could not be found.
449 * Quite a few others I'm too lazy to map correctly.
452 * Algorithm: Scan the elements of the presentation storage, looking
453 * for presentation streams. For each presentation stream,
454 * load the header and check to see if the aspect matches.
456 * If a fallback is desired, just opening the first presentation stream
459 static HRESULT
DataCacheEntry_OpenPresStream(
460 DataCacheEntry
*This
,
467 if (!ppStm
) return E_POINTER
;
469 hr
= IStorage_EnumElements(This
->storage
, 0, NULL
, 0, &pEnum
);
470 if (FAILED(hr
)) return hr
;
472 while ((hr
= IEnumSTATSTG_Next(pEnum
, 1, &elem
, NULL
)) == S_OK
)
474 if (DataCache_IsPresentationStream(&elem
))
478 hr
= IStorage_OpenStream(This
->storage
, elem
.pwcsName
,
479 NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0,
483 PresentationDataHeader header
;
485 CLIPFORMAT clipformat
;
487 hr
= read_clipformat(pStm
, &clipformat
);
490 hr
= IStream_Read(pStm
, &header
, sizeof(header
), &actual_read
);
492 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
493 if (hr
== S_OK
&& actual_read
== sizeof(header
)
494 && header
.dvAspect
== This
->fmtetc
.dwAspect
)
496 /* Rewind the stream before returning it. */
497 LARGE_INTEGER offset
;
498 offset
.u
.LowPart
= 0;
499 offset
.u
.HighPart
= 0;
500 IStream_Seek(pStm
, offset
, STREAM_SEEK_SET
, NULL
);
504 CoTaskMemFree(elem
.pwcsName
);
505 IEnumSTATSTG_Release(pEnum
);
510 IStream_Release(pStm
);
514 CoTaskMemFree(elem
.pwcsName
);
517 IEnumSTATSTG_Release(pEnum
);
519 return (hr
== S_FALSE
? OLE_E_BLANK
: hr
);
522 /************************************************************************
523 * DataCacheEntry_LoadData
525 * This method will read information for the requested presentation
526 * into the given structure.
529 * This - The entry to load the data from.
532 * This method returns a metafile handle if it is successful.
533 * it will return 0 if not.
535 static HRESULT
DataCacheEntry_LoadData(DataCacheEntry
*This
)
537 IStream
* presStream
= NULL
;
539 ULARGE_INTEGER current_pos
;
542 METAFILEPICT
*mfpict
;
544 PresentationDataHeader header
;
545 CLIPFORMAT clipformat
;
546 static const LARGE_INTEGER offset_zero
;
549 * Open the presentation stream.
551 hres
= DataCacheEntry_OpenPresStream(
559 * Get the size of the stream.
561 hres
= IStream_Stat(presStream
,
569 hres
= read_clipformat(presStream
, &clipformat
);
572 IStream_Release(presStream
);
579 sizeof(PresentationDataHeader
),
583 IStream_Release(presStream
);
587 hres
= IStream_Seek(presStream
, offset_zero
, STREAM_SEEK_CUR
, ¤t_pos
);
589 streamInfo
.cbSize
.QuadPart
-= current_pos
.QuadPart
;
591 hmfpict
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(METAFILEPICT
));
594 IStream_Release(presStream
);
595 return E_OUTOFMEMORY
;
597 mfpict
= GlobalLock(hmfpict
);
600 * Allocate a buffer for the metafile bits.
602 metafileBits
= HeapAlloc(GetProcessHeap(),
604 streamInfo
.cbSize
.u
.LowPart
);
607 * Read the metafile bits.
612 streamInfo
.cbSize
.u
.LowPart
,
616 * Create a metafile with those bits.
620 /* FIXME: get this from the stream */
621 mfpict
->mm
= MM_ANISOTROPIC
;
622 mfpict
->xExt
= header
.dwObjectExtentX
;
623 mfpict
->yExt
= header
.dwObjectExtentY
;
624 mfpict
->hMF
= SetMetaFileBitsEx(streamInfo
.cbSize
.u
.LowPart
, metafileBits
);
629 GlobalUnlock(hmfpict
);
632 This
->data_cf
= This
->fmtetc
.cfFormat
;
633 This
->stgmedium
.tymed
= TYMED_MFPICT
;
634 This
->stgmedium
.u
.hMetaFilePict
= hmfpict
;
642 HeapFree(GetProcessHeap(), 0, metafileBits
);
643 IStream_Release(presStream
);
648 static HRESULT
DataCacheEntry_CreateStream(DataCacheEntry
*This
,
649 IStorage
*storage
, IStream
**stream
)
652 WCHAR wszName
[] = {2,'O','l','e','P','r','e','s',
653 '0' + (This
->stream_number
/ 100) % 10,
654 '0' + (This
->stream_number
/ 10) % 10,
655 '0' + This
->stream_number
% 10, 0};
657 /* FIXME: cache the created stream in This? */
658 hr
= IStorage_CreateStream(storage
, wszName
,
659 STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
| STGM_CREATE
,
664 static HRESULT
DataCacheEntry_Save(DataCacheEntry
*This
, IStorage
*storage
,
667 PresentationDataHeader header
;
669 IStream
*pres_stream
;
672 TRACE("stream_number = %d, fmtetc = %s\n", This
->stream_number
, debugstr_formatetc(&This
->fmtetc
));
674 hr
= DataCacheEntry_CreateStream(This
, storage
, &pres_stream
);
678 hr
= write_clipformat(pres_stream
, This
->data_cf
);
682 if (This
->fmtetc
.ptd
)
683 FIXME("ptd not serialized\n");
685 header
.dvAspect
= This
->fmtetc
.dwAspect
;
686 header
.lindex
= This
->fmtetc
.lindex
;
687 header
.tymed
= This
->stgmedium
.tymed
;
689 header
.dwObjectExtentX
= 0;
690 header
.dwObjectExtentY
= 0;
694 switch (This
->data_cf
)
696 case CF_METAFILEPICT
:
698 if (This
->stgmedium
.tymed
!= TYMED_NULL
)
700 const METAFILEPICT
*mfpict
= GlobalLock(This
->stgmedium
.u
.hMetaFilePict
);
703 IStream_Release(pres_stream
);
704 return DV_E_STGMEDIUM
;
706 header
.dwObjectExtentX
= mfpict
->xExt
;
707 header
.dwObjectExtentY
= mfpict
->yExt
;
708 header
.dwSize
= GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
709 GlobalUnlock(This
->stgmedium
.u
.hMetaFilePict
);
720 hr
= IStream_Write(pres_stream
, &header
, sizeof(PresentationDataHeader
),
724 IStream_Release(pres_stream
);
729 switch (This
->data_cf
)
731 case CF_METAFILEPICT
:
733 if (This
->stgmedium
.tymed
!= TYMED_NULL
)
735 const METAFILEPICT
*mfpict
= GlobalLock(This
->stgmedium
.u
.hMetaFilePict
);
738 IStream_Release(pres_stream
);
739 return DV_E_STGMEDIUM
;
741 data
= HeapAlloc(GetProcessHeap(), 0, header
.dwSize
);
742 GetMetaFileBitsEx(mfpict
->hMF
, header
.dwSize
, data
);
743 GlobalUnlock(This
->stgmedium
.u
.hMetaFilePict
);
752 hr
= IStream_Write(pres_stream
, data
, header
.dwSize
, NULL
);
754 IStream_Release(pres_stream
);
758 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
759 * does no checking of whether src_stgm has a supported tymed, so this should be
760 * done in the caller */
761 static HRESULT
copy_stg_medium(CLIPFORMAT cf
, STGMEDIUM
*dest_stgm
,
762 const STGMEDIUM
*src_stgm
)
764 if (src_stgm
->tymed
== TYMED_MFPICT
)
766 const METAFILEPICT
*src_mfpict
= GlobalLock(src_stgm
->u
.hMetaFilePict
);
767 METAFILEPICT
*dest_mfpict
;
770 return DV_E_STGMEDIUM
;
771 dest_stgm
->u
.hMetaFilePict
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(METAFILEPICT
));
772 dest_mfpict
= GlobalLock(dest_stgm
->u
.hMetaFilePict
);
775 GlobalUnlock(src_stgm
->u
.hMetaFilePict
);
776 return E_OUTOFMEMORY
;
778 *dest_mfpict
= *src_mfpict
;
779 dest_mfpict
->hMF
= CopyMetaFileW(src_mfpict
->hMF
, NULL
);
780 GlobalUnlock(src_stgm
->u
.hMetaFilePict
);
781 GlobalUnlock(dest_stgm
->u
.hMetaFilePict
);
783 else if (src_stgm
->tymed
!= TYMED_NULL
)
785 dest_stgm
->u
.hGlobal
= OleDuplicateData(src_stgm
->u
.hGlobal
, cf
,
787 if (!dest_stgm
->u
.hGlobal
)
788 return E_OUTOFMEMORY
;
790 dest_stgm
->tymed
= src_stgm
->tymed
;
791 dest_stgm
->pUnkForRelease
= src_stgm
->pUnkForRelease
;
792 if (dest_stgm
->pUnkForRelease
)
793 IUnknown_AddRef(dest_stgm
->pUnkForRelease
);
797 static HRESULT
DataCacheEntry_SetData(DataCacheEntry
*This
,
798 const FORMATETC
*formatetc
,
799 const STGMEDIUM
*stgmedium
,
802 if ((!This
->fmtetc
.cfFormat
&& !formatetc
->cfFormat
) ||
803 (This
->fmtetc
.tymed
== TYMED_NULL
&& formatetc
->tymed
== TYMED_NULL
) ||
804 stgmedium
->tymed
== TYMED_NULL
)
806 WARN("invalid formatetc\n");
807 return DV_E_FORMATETC
;
811 ReleaseStgMedium(&This
->stgmedium
);
812 This
->data_cf
= This
->fmtetc
.cfFormat
? This
->fmtetc
.cfFormat
: formatetc
->cfFormat
;
815 This
->stgmedium
= *stgmedium
;
819 return copy_stg_medium(This
->data_cf
,
820 &This
->stgmedium
, stgmedium
);
823 static HRESULT
DataCacheEntry_GetData(DataCacheEntry
*This
,
824 STGMEDIUM
*stgmedium
)
826 if (stgmedium
->tymed
== TYMED_NULL
&& This
->storage
)
828 HRESULT hr
= DataCacheEntry_LoadData(This
);
832 if (stgmedium
->tymed
== TYMED_NULL
)
834 return copy_stg_medium(This
->data_cf
, stgmedium
, &This
->stgmedium
);
837 static inline HRESULT
DataCacheEntry_DiscardData(DataCacheEntry
*This
)
839 ReleaseStgMedium(&This
->stgmedium
);
840 This
->data_cf
= This
->fmtetc
.cfFormat
;
844 static inline void DataCacheEntry_HandsOffStorage(DataCacheEntry
*This
)
848 IStorage_Release(This
->storage
);
849 This
->storage
= NULL
;
853 /*********************************************************
854 * Method implementation for the non delegating IUnknown
855 * part of the DataCache class.
858 /************************************************************************
859 * DataCache_NDIUnknown_QueryInterface (IUnknown)
861 * See Windows documentation for more details on IUnknown methods.
863 * This version of QueryInterface will not delegate it's implementation
864 * to the outer unknown.
866 static HRESULT WINAPI
DataCache_NDIUnknown_QueryInterface(
871 DataCache
*this = impl_from_NDIUnknown(iface
);
874 * Perform a sanity check on the parameters.
876 if ( (this==0) || (ppvObject
==0) )
880 * Initialize the return parameter.
885 * Compare the riid with the interface IDs implemented by this object.
887 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
891 else if (memcmp(&IID_IDataObject
, riid
, sizeof(IID_IDataObject
)) == 0)
893 *ppvObject
= (IDataObject
*)&(this->lpVtbl
);
895 else if ( (memcmp(&IID_IPersistStorage
, riid
, sizeof(IID_IPersistStorage
)) == 0) ||
896 (memcmp(&IID_IPersist
, riid
, sizeof(IID_IPersist
)) == 0) )
898 *ppvObject
= (IPersistStorage
*)&(this->lpvtblIPersistStorage
);
900 else if ( (memcmp(&IID_IViewObject
, riid
, sizeof(IID_IViewObject
)) == 0) ||
901 (memcmp(&IID_IViewObject2
, riid
, sizeof(IID_IViewObject2
)) == 0) )
903 *ppvObject
= (IViewObject2
*)&(this->lpvtblIViewObject
);
905 else if ( (memcmp(&IID_IOleCache
, riid
, sizeof(IID_IOleCache
)) == 0) ||
906 (memcmp(&IID_IOleCache2
, riid
, sizeof(IID_IOleCache2
)) == 0) )
908 *ppvObject
= (IOleCache2
*)&(this->lpvtblIOleCache2
);
910 else if (memcmp(&IID_IOleCacheControl
, riid
, sizeof(IID_IOleCacheControl
)) == 0)
912 *ppvObject
= (IOleCacheControl
*)&(this->lpvtblIOleCacheControl
);
916 * Check that we obtained an interface.
920 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid
));
921 return E_NOINTERFACE
;
925 * Query Interface always increases the reference count by one when it is
928 IUnknown_AddRef((IUnknown
*)*ppvObject
);
933 /************************************************************************
934 * DataCache_NDIUnknown_AddRef (IUnknown)
936 * See Windows documentation for more details on IUnknown methods.
938 * This version of QueryInterface will not delegate it's implementation
939 * to the outer unknown.
941 static ULONG WINAPI
DataCache_NDIUnknown_AddRef(
944 DataCache
*this = impl_from_NDIUnknown(iface
);
945 return InterlockedIncrement(&this->ref
);
948 /************************************************************************
949 * DataCache_NDIUnknown_Release (IUnknown)
951 * See Windows documentation for more details on IUnknown methods.
953 * This version of QueryInterface will not delegate it's implementation
954 * to the outer unknown.
956 static ULONG WINAPI
DataCache_NDIUnknown_Release(
959 DataCache
*this = impl_from_NDIUnknown(iface
);
963 * Decrease the reference count on this object.
965 ref
= InterlockedDecrement(&this->ref
);
968 * If the reference count goes down to 0, perform suicide.
970 if (ref
== 0) DataCache_Destroy(this);
975 /*********************************************************
976 * Method implementation for the IDataObject
977 * part of the DataCache class.
980 /************************************************************************
981 * DataCache_IDataObject_QueryInterface (IUnknown)
983 * See Windows documentation for more details on IUnknown methods.
985 static HRESULT WINAPI
DataCache_IDataObject_QueryInterface(
990 DataCache
*this = impl_from_IDataObject(iface
);
992 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
995 /************************************************************************
996 * DataCache_IDataObject_AddRef (IUnknown)
998 * See Windows documentation for more details on IUnknown methods.
1000 static ULONG WINAPI
DataCache_IDataObject_AddRef(
1003 DataCache
*this = impl_from_IDataObject(iface
);
1005 return IUnknown_AddRef(this->outerUnknown
);
1008 /************************************************************************
1009 * DataCache_IDataObject_Release (IUnknown)
1011 * See Windows documentation for more details on IUnknown methods.
1013 static ULONG WINAPI
DataCache_IDataObject_Release(
1016 DataCache
*this = impl_from_IDataObject(iface
);
1018 return IUnknown_Release(this->outerUnknown
);
1021 /************************************************************************
1024 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1025 * See Windows documentation for more details on GetData.
1027 static HRESULT WINAPI
DataCache_GetData(
1029 LPFORMATETC pformatetcIn
,
1032 DataCache
*This
= impl_from_IDataObject(iface
);
1033 DataCacheEntry
*cache_entry
;
1035 memset(pmedium
, 0, sizeof(*pmedium
));
1037 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetcIn
);
1041 return DataCacheEntry_GetData(cache_entry
, pmedium
);
1044 static HRESULT WINAPI
DataCache_GetDataHere(
1046 LPFORMATETC pformatetc
,
1053 static HRESULT WINAPI
DataCache_QueryGetData(
1055 LPFORMATETC pformatetc
)
1061 /************************************************************************
1062 * DataCache_EnumFormatEtc (IDataObject)
1064 * The data cache doesn't implement this method.
1066 * See Windows documentation for more details on IDataObject methods.
1068 static HRESULT WINAPI
DataCache_GetCanonicalFormatEtc(
1070 LPFORMATETC pformatectIn
,
1071 LPFORMATETC pformatetcOut
)
1077 /************************************************************************
1078 * DataCache_IDataObject_SetData (IDataObject)
1080 * This method is delegated to the IOleCache2 implementation.
1082 * See Windows documentation for more details on IDataObject methods.
1084 static HRESULT WINAPI
DataCache_IDataObject_SetData(
1086 LPFORMATETC pformatetc
,
1090 IOleCache2
* oleCache
= NULL
;
1093 TRACE("(%p, %p, %p, %d)\n", iface
, pformatetc
, pmedium
, fRelease
);
1095 hres
= IDataObject_QueryInterface(iface
, &IID_IOleCache2
, (void**)&oleCache
);
1098 return E_UNEXPECTED
;
1100 hres
= IOleCache2_SetData(oleCache
, pformatetc
, pmedium
, fRelease
);
1102 IOleCache2_Release(oleCache
);
1107 /************************************************************************
1108 * DataCache_EnumFormatEtc (IDataObject)
1110 * The data cache doesn't implement this method.
1112 * See Windows documentation for more details on IDataObject methods.
1114 static HRESULT WINAPI
DataCache_EnumFormatEtc(
1117 IEnumFORMATETC
** ppenumFormatEtc
)
1123 /************************************************************************
1124 * DataCache_DAdvise (IDataObject)
1126 * The data cache doesn't support connections.
1128 * See Windows documentation for more details on IDataObject methods.
1130 static HRESULT WINAPI
DataCache_DAdvise(
1132 FORMATETC
* pformatetc
,
1134 IAdviseSink
* pAdvSink
,
1135 DWORD
* pdwConnection
)
1138 return OLE_E_ADVISENOTSUPPORTED
;
1141 /************************************************************************
1142 * DataCache_DUnadvise (IDataObject)
1144 * The data cache doesn't support connections.
1146 * See Windows documentation for more details on IDataObject methods.
1148 static HRESULT WINAPI
DataCache_DUnadvise(
1153 return OLE_E_NOCONNECTION
;
1156 /************************************************************************
1157 * DataCache_EnumDAdvise (IDataObject)
1159 * The data cache doesn't support connections.
1161 * See Windows documentation for more details on IDataObject methods.
1163 static HRESULT WINAPI
DataCache_EnumDAdvise(
1165 IEnumSTATDATA
** ppenumAdvise
)
1168 return OLE_E_ADVISENOTSUPPORTED
;
1171 /*********************************************************
1172 * Method implementation for the IDataObject
1173 * part of the DataCache class.
1176 /************************************************************************
1177 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1179 * See Windows documentation for more details on IUnknown methods.
1181 static HRESULT WINAPI
DataCache_IPersistStorage_QueryInterface(
1182 IPersistStorage
* iface
,
1186 DataCache
*this = impl_from_IPersistStorage(iface
);
1188 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
1191 /************************************************************************
1192 * DataCache_IPersistStorage_AddRef (IUnknown)
1194 * See Windows documentation for more details on IUnknown methods.
1196 static ULONG WINAPI
DataCache_IPersistStorage_AddRef(
1197 IPersistStorage
* iface
)
1199 DataCache
*this = impl_from_IPersistStorage(iface
);
1201 return IUnknown_AddRef(this->outerUnknown
);
1204 /************************************************************************
1205 * DataCache_IPersistStorage_Release (IUnknown)
1207 * See Windows documentation for more details on IUnknown methods.
1209 static ULONG WINAPI
DataCache_IPersistStorage_Release(
1210 IPersistStorage
* iface
)
1212 DataCache
*this = impl_from_IPersistStorage(iface
);
1214 return IUnknown_Release(this->outerUnknown
);
1217 /************************************************************************
1218 * DataCache_GetClassID (IPersistStorage)
1220 * The data cache doesn't implement this method.
1222 * See Windows documentation for more details on IPersistStorage methods.
1224 static HRESULT WINAPI
DataCache_GetClassID(
1225 IPersistStorage
* iface
,
1228 DataCache
*This
= impl_from_IPersistStorage(iface
);
1229 DataCacheEntry
*cache_entry
;
1231 TRACE("(%p, %p)\n", iface
, pClassID
);
1233 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1235 if (cache_entry
->storage
!= NULL
)
1238 HRESULT hr
= IStorage_Stat(cache_entry
->storage
, &statstg
, STATFLAG_NONAME
);
1241 memcpy(pClassID
, &statstg
.clsid
, sizeof(*pClassID
));
1247 memcpy(pClassID
, &CLSID_NULL
, sizeof(*pClassID
));
1252 /************************************************************************
1253 * DataCache_IsDirty (IPersistStorage)
1255 * See Windows documentation for more details on IPersistStorage methods.
1257 static HRESULT WINAPI
DataCache_IsDirty(
1258 IPersistStorage
* iface
)
1260 DataCache
*This
= impl_from_IPersistStorage(iface
);
1261 DataCacheEntry
*cache_entry
;
1263 TRACE("(%p)\n", iface
);
1268 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1269 if (cache_entry
->dirty
)
1275 /************************************************************************
1276 * DataCache_InitNew (IPersistStorage)
1278 * The data cache implementation of IPersistStorage_InitNew simply stores
1279 * the storage pointer.
1281 * See Windows documentation for more details on IPersistStorage methods.
1283 static HRESULT WINAPI
DataCache_InitNew(
1284 IPersistStorage
* iface
,
1287 DataCache
*This
= impl_from_IPersistStorage(iface
);
1289 TRACE("(%p, %p)\n", iface
, pStg
);
1291 if (This
->presentationStorage
!= NULL
)
1292 IStorage_Release(This
->presentationStorage
);
1294 This
->presentationStorage
= pStg
;
1296 IStorage_AddRef(This
->presentationStorage
);
1302 /************************************************************************
1303 * DataCache_Load (IPersistStorage)
1305 * The data cache implementation of IPersistStorage_Load doesn't
1306 * actually load anything. Instead, it holds on to the storage pointer
1307 * and it will load the presentation information when the
1308 * IDataObject_GetData or IViewObject2_Draw methods are called.
1310 * See Windows documentation for more details on IPersistStorage methods.
1312 static HRESULT WINAPI
DataCache_Load(
1313 IPersistStorage
* iface
,
1316 DataCache
*This
= impl_from_IPersistStorage(iface
);
1318 IEnumSTATSTG
*pEnum
;
1321 TRACE("(%p, %p)\n", iface
, pStg
);
1323 if (This
->presentationStorage
!= NULL
)
1324 IStorage_Release(This
->presentationStorage
);
1326 This
->presentationStorage
= pStg
;
1328 hr
= IStorage_EnumElements(pStg
, 0, NULL
, 0, &pEnum
);
1329 if (FAILED(hr
)) return hr
;
1331 while ((hr
= IEnumSTATSTG_Next(pEnum
, 1, &elem
, NULL
)) == S_OK
)
1333 if (DataCache_IsPresentationStream(&elem
))
1337 hr
= IStorage_OpenStream(This
->presentationStorage
, elem
.pwcsName
,
1338 NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0,
1342 PresentationDataHeader header
;
1344 CLIPFORMAT clipformat
;
1346 hr
= read_clipformat(pStm
, &clipformat
);
1349 hr
= IStream_Read(pStm
, &header
, sizeof(header
),
1352 /* can't use SUCCEEDED(hr): S_FALSE counts as an error */
1353 if (hr
== S_OK
&& actual_read
== sizeof(header
))
1355 DataCacheEntry
*cache_entry
;
1358 fmtetc
.cfFormat
= clipformat
;
1359 fmtetc
.ptd
= NULL
; /* FIXME */
1360 fmtetc
.dwAspect
= header
.dvAspect
;
1361 fmtetc
.lindex
= header
.lindex
;
1362 fmtetc
.tymed
= header
.tymed
;
1364 TRACE("loading entry with formatetc: %s\n", debugstr_formatetc(&fmtetc
));
1366 cache_entry
= DataCache_GetEntryForFormatEtc(This
, &fmtetc
);
1368 hr
= DataCache_CreateEntry(This
, &fmtetc
, &cache_entry
);
1371 DataCacheEntry_DiscardData(cache_entry
);
1372 if (cache_entry
->storage
) IStorage_Release(cache_entry
->storage
);
1373 cache_entry
->storage
= pStg
;
1374 IStorage_AddRef(pStg
);
1375 cache_entry
->dirty
= FALSE
;
1379 IStream_Release(pStm
);
1383 CoTaskMemFree(elem
.pwcsName
);
1386 This
->dirty
= FALSE
;
1388 IEnumSTATSTG_Release(pEnum
);
1390 IStorage_AddRef(This
->presentationStorage
);
1394 /************************************************************************
1395 * DataCache_Save (IPersistStorage)
1397 * Until we actually connect to a running object and retrieve new
1398 * information to it, we never have to save anything. However, it is
1399 * our responsibility to copy the information when saving to a new
1402 * See Windows documentation for more details on IPersistStorage methods.
1404 static HRESULT WINAPI
DataCache_Save(
1405 IPersistStorage
* iface
,
1409 DataCache
*This
= impl_from_IPersistStorage(iface
);
1410 DataCacheEntry
*cache_entry
;
1413 unsigned short stream_number
= 0;
1415 TRACE("(%p, %p, %d)\n", iface
, pStg
, fSameAsLoad
);
1417 dirty
= This
->dirty
;
1420 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1422 dirty
= cache_entry
->dirty
;
1428 /* this is a shortcut if nothing changed */
1429 if (!dirty
&& !fSameAsLoad
&& This
->presentationStorage
)
1431 return IStorage_CopyTo(This
->presentationStorage
, 0, NULL
, NULL
, pStg
);
1434 /* assign stream numbers to the cache entries */
1435 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1437 if (cache_entry
->stream_number
!= stream_number
)
1439 cache_entry
->dirty
= TRUE
; /* needs to be written out again */
1440 cache_entry
->stream_number
= stream_number
;
1445 /* write out the cache entries */
1446 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1448 if (!fSameAsLoad
|| cache_entry
->dirty
)
1450 hr
= DataCacheEntry_Save(cache_entry
, pStg
, fSameAsLoad
);
1454 cache_entry
->dirty
= FALSE
;
1458 This
->dirty
= FALSE
;
1462 /************************************************************************
1463 * DataCache_SaveCompleted (IPersistStorage)
1465 * This method is called to tell the cache to release the storage
1466 * pointer it's currently holding.
1468 * See Windows documentation for more details on IPersistStorage methods.
1470 static HRESULT WINAPI
DataCache_SaveCompleted(
1471 IPersistStorage
* iface
,
1474 TRACE("(%p, %p)\n", iface
, pStgNew
);
1479 * First, make sure we get our hands off any storage we have.
1482 IPersistStorage_HandsOffStorage(iface
);
1485 * Then, attach to the new storage.
1488 DataCache_Load(iface
, pStgNew
);
1494 /************************************************************************
1495 * DataCache_HandsOffStorage (IPersistStorage)
1497 * This method is called to tell the cache to release the storage
1498 * pointer it's currently holding.
1500 * See Windows documentation for more details on IPersistStorage methods.
1502 static HRESULT WINAPI
DataCache_HandsOffStorage(
1503 IPersistStorage
* iface
)
1505 DataCache
*this = impl_from_IPersistStorage(iface
);
1506 DataCacheEntry
*cache_entry
;
1508 TRACE("(%p)\n", iface
);
1510 if (this->presentationStorage
!= NULL
)
1512 IStorage_Release(this->presentationStorage
);
1513 this->presentationStorage
= NULL
;
1516 LIST_FOR_EACH_ENTRY(cache_entry
, &this->cache_list
, DataCacheEntry
, entry
)
1517 DataCacheEntry_HandsOffStorage(cache_entry
);
1522 /*********************************************************
1523 * Method implementation for the IViewObject2
1524 * part of the DataCache class.
1527 /************************************************************************
1528 * DataCache_IViewObject2_QueryInterface (IUnknown)
1530 * See Windows documentation for more details on IUnknown methods.
1532 static HRESULT WINAPI
DataCache_IViewObject2_QueryInterface(
1533 IViewObject2
* iface
,
1537 DataCache
*this = impl_from_IViewObject2(iface
);
1539 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
1542 /************************************************************************
1543 * DataCache_IViewObject2_AddRef (IUnknown)
1545 * See Windows documentation for more details on IUnknown methods.
1547 static ULONG WINAPI
DataCache_IViewObject2_AddRef(
1548 IViewObject2
* iface
)
1550 DataCache
*this = impl_from_IViewObject2(iface
);
1552 return IUnknown_AddRef(this->outerUnknown
);
1555 /************************************************************************
1556 * DataCache_IViewObject2_Release (IUnknown)
1558 * See Windows documentation for more details on IUnknown methods.
1560 static ULONG WINAPI
DataCache_IViewObject2_Release(
1561 IViewObject2
* iface
)
1563 DataCache
*this = impl_from_IViewObject2(iface
);
1565 return IUnknown_Release(this->outerUnknown
);
1568 /************************************************************************
1569 * DataCache_Draw (IViewObject2)
1571 * This method will draw the cached representation of the object
1572 * to the given device context.
1574 * See Windows documentation for more details on IViewObject2 methods.
1576 static HRESULT WINAPI
DataCache_Draw(
1577 IViewObject2
* iface
,
1581 DVTARGETDEVICE
* ptd
,
1584 LPCRECTL lprcBounds
,
1585 LPCRECTL lprcWBounds
,
1586 BOOL (CALLBACK
*pfnContinue
)(ULONG_PTR dwContinue
),
1587 ULONG_PTR dwContinue
)
1589 DataCache
*This
= impl_from_IViewObject2(iface
);
1591 DataCacheEntry
*cache_entry
;
1593 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1608 if (lprcBounds
==NULL
)
1609 return E_INVALIDARG
;
1611 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1613 /* FIXME: compare ptd too */
1614 if ((cache_entry
->fmtetc
.dwAspect
!= dwDrawAspect
) ||
1615 (cache_entry
->fmtetc
.lindex
!= lindex
))
1618 /* if the data hasn't been loaded yet, do it now */
1619 if ((cache_entry
->stgmedium
.tymed
== TYMED_NULL
) && cache_entry
->storage
)
1621 hres
= DataCacheEntry_LoadData(cache_entry
);
1627 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
1630 switch (cache_entry
->data_cf
)
1632 case CF_METAFILEPICT
:
1635 * We have to be careful not to modify the state of the
1640 SIZE oldViewportExt
;
1641 POINT oldViewportOrg
;
1642 METAFILEPICT
*mfpict
;
1644 if ((cache_entry
->stgmedium
.tymed
!= TYMED_MFPICT
) ||
1645 !((mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
))))
1648 prevMapMode
= SetMapMode(hdcDraw
, mfpict
->mm
);
1650 SetWindowExtEx(hdcDraw
,
1655 SetViewportExtEx(hdcDraw
,
1656 lprcBounds
->right
- lprcBounds
->left
,
1657 lprcBounds
->bottom
- lprcBounds
->top
,
1660 SetViewportOrgEx(hdcDraw
,
1665 PlayMetaFile(hdcDraw
, mfpict
->hMF
);
1667 SetWindowExtEx(hdcDraw
,
1672 SetViewportExtEx(hdcDraw
,
1677 SetViewportOrgEx(hdcDraw
,
1682 SetMapMode(hdcDraw
, prevMapMode
);
1684 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
1691 WARN("no data could be found to be drawn\n");
1696 static HRESULT WINAPI
DataCache_GetColorSet(
1697 IViewObject2
* iface
,
1701 DVTARGETDEVICE
* ptd
,
1702 HDC hicTargetDevice
,
1703 LOGPALETTE
** ppColorSet
)
1709 static HRESULT WINAPI
DataCache_Freeze(
1710 IViewObject2
* iface
,
1720 static HRESULT WINAPI
DataCache_Unfreeze(
1721 IViewObject2
* iface
,
1728 /************************************************************************
1729 * DataCache_SetAdvise (IViewObject2)
1731 * This sets-up an advisory sink with the data cache. When the object's
1732 * view changes, this sink is called.
1734 * See Windows documentation for more details on IViewObject2 methods.
1736 static HRESULT WINAPI
DataCache_SetAdvise(
1737 IViewObject2
* iface
,
1740 IAdviseSink
* pAdvSink
)
1742 DataCache
*this = impl_from_IViewObject2(iface
);
1744 TRACE("(%p, %x, %x, %p)\n", iface
, aspects
, advf
, pAdvSink
);
1747 * A call to this function removes the previous sink
1749 if (this->sinkInterface
!= NULL
)
1751 IAdviseSink_Release(this->sinkInterface
);
1752 this->sinkInterface
= NULL
;
1753 this->sinkAspects
= 0;
1754 this->sinkAdviseFlag
= 0;
1758 * Now, setup the new one.
1762 this->sinkInterface
= pAdvSink
;
1763 this->sinkAspects
= aspects
;
1764 this->sinkAdviseFlag
= advf
;
1766 IAdviseSink_AddRef(this->sinkInterface
);
1770 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
1773 if (advf
& ADVF_PRIMEFIRST
)
1775 DataCache_FireOnViewChange(this, aspects
, -1);
1781 /************************************************************************
1782 * DataCache_GetAdvise (IViewObject2)
1784 * This method queries the current state of the advise sink
1785 * installed on the data cache.
1787 * See Windows documentation for more details on IViewObject2 methods.
1789 static HRESULT WINAPI
DataCache_GetAdvise(
1790 IViewObject2
* iface
,
1793 IAdviseSink
** ppAdvSink
)
1795 DataCache
*this = impl_from_IViewObject2(iface
);
1797 TRACE("(%p, %p, %p, %p)\n", iface
, pAspects
, pAdvf
, ppAdvSink
);
1800 * Just copy all the requested values.
1803 *pAspects
= this->sinkAspects
;
1806 *pAdvf
= this->sinkAdviseFlag
;
1808 if (ppAdvSink
!=NULL
)
1810 if (this->sinkInterface
!= NULL
)
1811 IAdviseSink_QueryInterface(this->sinkInterface
,
1814 else *ppAdvSink
= NULL
;
1820 /************************************************************************
1821 * DataCache_GetExtent (IViewObject2)
1823 * This method retrieves the "natural" size of this cached object.
1825 * See Windows documentation for more details on IViewObject2 methods.
1827 static HRESULT WINAPI
DataCache_GetExtent(
1828 IViewObject2
* iface
,
1831 DVTARGETDEVICE
* ptd
,
1834 DataCache
*This
= impl_from_IViewObject2(iface
);
1835 HRESULT hres
= E_FAIL
;
1836 DataCacheEntry
*cache_entry
;
1838 TRACE("(%p, %x, %d, %p, %p)\n",
1839 iface
, dwDrawAspect
, lindex
, ptd
, lpsizel
);
1848 * Initialize the out parameter.
1854 * This flag should be set to -1.
1857 FIXME("Unimplemented flag lindex = %d\n", lindex
);
1860 * Right now, we support only the callback from
1861 * the default handler.
1864 FIXME("Unimplemented ptd = %p\n", ptd
);
1866 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1868 /* FIXME: compare ptd too */
1869 if ((cache_entry
->fmtetc
.dwAspect
!= dwDrawAspect
) ||
1870 (cache_entry
->fmtetc
.lindex
!= lindex
))
1873 /* if the data hasn't been loaded yet, do it now */
1874 if ((cache_entry
->stgmedium
.tymed
== TYMED_NULL
) && cache_entry
->storage
)
1876 hres
= DataCacheEntry_LoadData(cache_entry
);
1882 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
1886 switch (cache_entry
->data_cf
)
1888 case CF_METAFILEPICT
:
1890 METAFILEPICT
*mfpict
;
1892 if ((cache_entry
->stgmedium
.tymed
!= TYMED_MFPICT
) ||
1893 !((mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
))))
1896 lpsizel
->cx
= mfpict
->xExt
;
1897 lpsizel
->cy
= mfpict
->yExt
;
1899 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
1906 WARN("no data could be found to get the extents from\n");
1909 * This method returns OLE_E_BLANK when it fails.
1915 /*********************************************************
1916 * Method implementation for the IOleCache2
1917 * part of the DataCache class.
1920 /************************************************************************
1921 * DataCache_IOleCache2_QueryInterface (IUnknown)
1923 * See Windows documentation for more details on IUnknown methods.
1925 static HRESULT WINAPI
DataCache_IOleCache2_QueryInterface(
1930 DataCache
*this = impl_from_IOleCache2(iface
);
1932 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
1935 /************************************************************************
1936 * DataCache_IOleCache2_AddRef (IUnknown)
1938 * See Windows documentation for more details on IUnknown methods.
1940 static ULONG WINAPI
DataCache_IOleCache2_AddRef(
1943 DataCache
*this = impl_from_IOleCache2(iface
);
1945 return IUnknown_AddRef(this->outerUnknown
);
1948 /************************************************************************
1949 * DataCache_IOleCache2_Release (IUnknown)
1951 * See Windows documentation for more details on IUnknown methods.
1953 static ULONG WINAPI
DataCache_IOleCache2_Release(
1956 DataCache
*this = impl_from_IOleCache2(iface
);
1958 return IUnknown_Release(this->outerUnknown
);
1961 static HRESULT WINAPI
DataCache_Cache(
1963 FORMATETC
* pformatetc
,
1965 DWORD
* pdwConnection
)
1967 DataCache
*This
= impl_from_IOleCache2(iface
);
1968 DataCacheEntry
*cache_entry
;
1971 TRACE("(%p, 0x%x, %p)\n", pformatetc
, advf
, pdwConnection
);
1972 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc
));
1976 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetc
);
1979 TRACE("found an existing cache entry\n");
1980 *pdwConnection
= cache_entry
->id
;
1981 return CACHE_S_SAMECACHE
;
1984 hr
= DataCache_CreateEntry(This
, pformatetc
, &cache_entry
);
1987 *pdwConnection
= cache_entry
->id
;
1992 static HRESULT WINAPI
DataCache_Uncache(
1996 DataCache
*This
= impl_from_IOleCache2(iface
);
1997 DataCacheEntry
*cache_entry
;
1999 TRACE("(%d)\n", dwConnection
);
2001 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2002 if (cache_entry
->id
== dwConnection
)
2004 DataCacheEntry_Destroy(cache_entry
);
2008 WARN("no connection found for %d\n", dwConnection
);
2010 return OLE_E_NOCONNECTION
;
2013 static HRESULT WINAPI
DataCache_EnumCache(
2015 IEnumSTATDATA
** ppenumSTATDATA
)
2021 static HRESULT WINAPI
DataCache_InitCache(
2023 IDataObject
* pDataObject
)
2029 static HRESULT WINAPI
DataCache_IOleCache2_SetData(
2031 FORMATETC
* pformatetc
,
2035 DataCache
*This
= impl_from_IOleCache2(iface
);
2036 DataCacheEntry
*cache_entry
;
2039 TRACE("(%p, %p, %s)\n", pformatetc
, pmedium
, fRelease
? "TRUE" : "FALSE");
2040 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc
));
2042 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetc
);
2045 hr
= DataCacheEntry_SetData(cache_entry
, pformatetc
, pmedium
, fRelease
);
2048 DataCache_FireOnViewChange(This
, cache_entry
->fmtetc
.dwAspect
,
2049 cache_entry
->fmtetc
.lindex
);
2053 WARN("cache entry not found\n");
2058 static HRESULT WINAPI
DataCache_UpdateCache(
2060 LPDATAOBJECT pDataObject
,
2064 FIXME("(%p, 0x%x, %p): stub\n", pDataObject
, grfUpdf
, pReserved
);
2068 static HRESULT WINAPI
DataCache_DiscardCache(
2070 DWORD dwDiscardOptions
)
2072 DataCache
*This
= impl_from_IOleCache2(iface
);
2073 DataCacheEntry
*cache_entry
;
2076 TRACE("(%d)\n", dwDiscardOptions
);
2078 if (dwDiscardOptions
== DISCARDCACHE_SAVEIFDIRTY
)
2079 hr
= DataCache_Save((IPersistStorage
*)&This
->lpvtblIPersistStorage
,
2080 This
->presentationStorage
, TRUE
);
2082 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2084 hr
= DataCacheEntry_DiscardData(cache_entry
);
2093 /*********************************************************
2094 * Method implementation for the IOleCacheControl
2095 * part of the DataCache class.
2098 /************************************************************************
2099 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2101 * See Windows documentation for more details on IUnknown methods.
2103 static HRESULT WINAPI
DataCache_IOleCacheControl_QueryInterface(
2104 IOleCacheControl
* iface
,
2108 DataCache
*this = impl_from_IOleCacheControl(iface
);
2110 return IUnknown_QueryInterface(this->outerUnknown
, riid
, ppvObject
);
2113 /************************************************************************
2114 * DataCache_IOleCacheControl_AddRef (IUnknown)
2116 * See Windows documentation for more details on IUnknown methods.
2118 static ULONG WINAPI
DataCache_IOleCacheControl_AddRef(
2119 IOleCacheControl
* iface
)
2121 DataCache
*this = impl_from_IOleCacheControl(iface
);
2123 return IUnknown_AddRef(this->outerUnknown
);
2126 /************************************************************************
2127 * DataCache_IOleCacheControl_Release (IUnknown)
2129 * See Windows documentation for more details on IUnknown methods.
2131 static ULONG WINAPI
DataCache_IOleCacheControl_Release(
2132 IOleCacheControl
* iface
)
2134 DataCache
*this = impl_from_IOleCacheControl(iface
);
2136 return IUnknown_Release(this->outerUnknown
);
2139 static HRESULT WINAPI
DataCache_OnRun(
2140 IOleCacheControl
* iface
,
2141 LPDATAOBJECT pDataObject
)
2147 static HRESULT WINAPI
DataCache_OnStop(
2148 IOleCacheControl
* iface
)
2155 * Virtual function tables for the DataCache class.
2157 static const IUnknownVtbl DataCache_NDIUnknown_VTable
=
2159 DataCache_NDIUnknown_QueryInterface
,
2160 DataCache_NDIUnknown_AddRef
,
2161 DataCache_NDIUnknown_Release
2164 static const IDataObjectVtbl DataCache_IDataObject_VTable
=
2166 DataCache_IDataObject_QueryInterface
,
2167 DataCache_IDataObject_AddRef
,
2168 DataCache_IDataObject_Release
,
2170 DataCache_GetDataHere
,
2171 DataCache_QueryGetData
,
2172 DataCache_GetCanonicalFormatEtc
,
2173 DataCache_IDataObject_SetData
,
2174 DataCache_EnumFormatEtc
,
2176 DataCache_DUnadvise
,
2177 DataCache_EnumDAdvise
2180 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable
=
2182 DataCache_IPersistStorage_QueryInterface
,
2183 DataCache_IPersistStorage_AddRef
,
2184 DataCache_IPersistStorage_Release
,
2185 DataCache_GetClassID
,
2190 DataCache_SaveCompleted
,
2191 DataCache_HandsOffStorage
2194 static const IViewObject2Vtbl DataCache_IViewObject2_VTable
=
2196 DataCache_IViewObject2_QueryInterface
,
2197 DataCache_IViewObject2_AddRef
,
2198 DataCache_IViewObject2_Release
,
2200 DataCache_GetColorSet
,
2203 DataCache_SetAdvise
,
2204 DataCache_GetAdvise
,
2208 static const IOleCache2Vtbl DataCache_IOleCache2_VTable
=
2210 DataCache_IOleCache2_QueryInterface
,
2211 DataCache_IOleCache2_AddRef
,
2212 DataCache_IOleCache2_Release
,
2215 DataCache_EnumCache
,
2216 DataCache_InitCache
,
2217 DataCache_IOleCache2_SetData
,
2218 DataCache_UpdateCache
,
2219 DataCache_DiscardCache
2222 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable
=
2224 DataCache_IOleCacheControl_QueryInterface
,
2225 DataCache_IOleCacheControl_AddRef
,
2226 DataCache_IOleCacheControl_Release
,
2231 /******************************************************************************
2232 * CreateDataCache [OLE32.@]
2234 * Creates a data cache to allow an object to render one or more of its views,
2235 * whether running or not.
2238 * pUnkOuter [I] Outer unknown for the object.
2240 * riid [I] IID of interface to return.
2241 * ppvObj [O] Address where the data cache object will be stored on return.
2245 * Failure: HRESULT code.
2248 * The following interfaces are supported by the returned data cache object:
2249 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorae, IDataObject,
2250 * IViewObject and IViewObject2.
2252 HRESULT WINAPI
CreateDataCache(
2253 LPUNKNOWN pUnkOuter
,
2258 DataCache
* newCache
= NULL
;
2261 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid
), pUnkOuter
, debugstr_guid(riid
), ppvObj
);
2272 * If this cache is constructed for aggregation, make sure
2273 * the caller is requesting the IUnknown interface.
2274 * This is necessary because it's the only time the non-delegating
2275 * IUnknown pointer can be returned to the outside.
2277 if ( (pUnkOuter
!=NULL
) &&
2278 (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) != 0) )
2279 return CLASS_E_NOAGGREGATION
;
2282 * Try to construct a new instance of the class.
2284 newCache
= DataCache_Construct(rclsid
,
2288 return E_OUTOFMEMORY
;
2291 * Make sure it supports the interface required by the caller.
2293 hr
= IUnknown_QueryInterface((IUnknown
*)&(newCache
->lpvtblNDIUnknown
), riid
, ppvObj
);
2296 * Release the reference obtained in the constructor. If
2297 * the QueryInterface was unsuccessful, it will free the class.
2299 IUnknown_Release((IUnknown
*)&(newCache
->lpvtblNDIUnknown
));
2304 /*********************************************************
2305 * Method implementation for DataCache class.
2307 static DataCache
* DataCache_Construct(
2309 LPUNKNOWN pUnkOuter
)
2311 DataCache
* newObject
= 0;
2314 * Allocate space for the object.
2316 newObject
= HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache
));
2322 * Initialize the virtual function table.
2324 newObject
->lpVtbl
= &DataCache_IDataObject_VTable
;
2325 newObject
->lpvtblNDIUnknown
= &DataCache_NDIUnknown_VTable
;
2326 newObject
->lpvtblIPersistStorage
= &DataCache_IPersistStorage_VTable
;
2327 newObject
->lpvtblIViewObject
= &DataCache_IViewObject2_VTable
;
2328 newObject
->lpvtblIOleCache2
= &DataCache_IOleCache2_VTable
;
2329 newObject
->lpvtblIOleCacheControl
= &DataCache_IOleCacheControl_VTable
;
2332 * Start with one reference count. The caller of this function
2333 * must release the interface pointer when it is done.
2338 * Initialize the outer unknown
2339 * We don't keep a reference on the outer unknown since, the way
2340 * aggregation works, our lifetime is at least as large as its
2343 if (pUnkOuter
==NULL
)
2344 pUnkOuter
= (IUnknown
*)&(newObject
->lpvtblNDIUnknown
);
2346 newObject
->outerUnknown
= pUnkOuter
;
2349 * Initialize the other members of the structure.
2351 newObject
->sinkAspects
= 0;
2352 newObject
->sinkAdviseFlag
= 0;
2353 newObject
->sinkInterface
= 0;
2354 newObject
->presentationStorage
= NULL
;
2355 list_init(&newObject
->cache_list
);
2356 newObject
->last_cache_id
= 1;
2357 newObject
->dirty
= FALSE
;