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.
51 #define NONAMELESSUNION
59 #include "compobj_private.h"
60 #include "wine/unicode.h"
61 #include "wine/list.h"
62 #include "wine/debug.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
66 /****************************************************************************
67 * PresentationDataHeader
69 * This structure represents the header of the \002OlePresXXX stream in
70 * the OLE object storage.
72 typedef struct PresentationDataHeader
75 * - standard clipformat:
76 * DWORD length = 0xffffffff;
78 * - or custom clipformat:
80 * CHAR format_name[length]; (null-terminated)
82 DWORD tdSize
; /* This is actually a truncated DVTARGETDEVICE, if tdSize > sizeof(DWORD)
83 then there are tdSize - sizeof(DWORD) more bytes before dvAspect */
87 DWORD unknown7
; /* 0 */
88 DWORD dwObjectExtentX
;
89 DWORD dwObjectExtentY
;
91 } PresentationDataHeader
;
93 #define STREAM_NUMBER_NOT_SET -2
94 #define STREAM_NUMBER_CONTENTS -1 /* CONTENTS stream */
96 typedef struct DataCacheEntry
99 /* format of this entry */
107 /* stream number that the entry was loaded from.
108 This is used to defer loading until the data is actually needed. */
110 /* stream number that the entry will be saved to.
111 This may differ from above if cache entries have been Uncache()d for example. */
113 /* sink id set when object is running */
115 /* Advise sink flags */
119 /****************************************************************************
125 * List all interface here
127 IUnknown IUnknown_inner
;
128 IDataObject IDataObject_iface
;
129 IPersistStorage IPersistStorage_iface
;
130 IViewObject2 IViewObject2_iface
;
131 IOleCache2 IOleCache2_iface
;
132 IOleCacheControl IOleCacheControl_iface
;
134 /* The sink that is connected to a remote object.
135 The other interfaces are not available by QI'ing the sink and vice-versa */
136 IAdviseSink IAdviseSink_iface
;
139 * Reference count of this object
144 * IUnknown implementation of the outer object.
149 * The user of this object can setup ONE advise sink
150 * connection with the object. These parameters describe
154 DWORD sinkAdviseFlag
;
155 IAdviseSink
*sinkInterface
;
158 /* Is the clsid one of the CLSID_Picture classes */
161 IStorage
*presentationStorage
;
163 /* list of cache entries */
164 struct list cache_list
;
165 /* last id assigned to an entry */
169 /* running object set by OnRun */
170 IDataObject
*running_object
;
173 typedef struct DataCache DataCache
;
176 * Here, I define utility macros to help with the casting of the
178 * There is a version to accommodate all of the VTables implemented
182 static inline DataCache
*impl_from_IDataObject( IDataObject
*iface
)
184 return CONTAINING_RECORD(iface
, DataCache
, IDataObject_iface
);
187 static inline DataCache
*impl_from_IUnknown( IUnknown
*iface
)
189 return CONTAINING_RECORD(iface
, DataCache
, IUnknown_inner
);
192 static inline DataCache
*impl_from_IPersistStorage( IPersistStorage
*iface
)
194 return CONTAINING_RECORD(iface
, DataCache
, IPersistStorage_iface
);
197 static inline DataCache
*impl_from_IViewObject2( IViewObject2
*iface
)
199 return CONTAINING_RECORD(iface
, DataCache
, IViewObject2_iface
);
202 static inline DataCache
*impl_from_IOleCache2( IOleCache2
*iface
)
204 return CONTAINING_RECORD(iface
, DataCache
, IOleCache2_iface
);
207 static inline DataCache
*impl_from_IOleCacheControl( IOleCacheControl
*iface
)
209 return CONTAINING_RECORD(iface
, DataCache
, IOleCacheControl_iface
);
212 static inline DataCache
*impl_from_IAdviseSink( IAdviseSink
*iface
)
214 return CONTAINING_RECORD(iface
, DataCache
, IAdviseSink_iface
);
217 const char *debugstr_formatetc(const FORMATETC
*formatetc
)
219 return wine_dbg_sprintf("{ cfFormat = 0x%x, ptd = %p, dwAspect = %d, lindex = %d, tymed = %d }",
220 formatetc
->cfFormat
, formatetc
->ptd
, formatetc
->dwAspect
,
221 formatetc
->lindex
, formatetc
->tymed
);
224 /***********************************************************************
227 * Return the size of the bitmap info structure including color table.
229 static int bitmap_info_size( const BITMAPINFO
* info
, WORD coloruse
)
231 unsigned int colors
, size
, masks
= 0;
233 if (info
->bmiHeader
.biSize
== sizeof(BITMAPCOREHEADER
))
235 const BITMAPCOREHEADER
*core
= (const BITMAPCOREHEADER
*)info
;
236 colors
= (core
->bcBitCount
<= 8) ? 1 << core
->bcBitCount
: 0;
237 return sizeof(BITMAPCOREHEADER
) + colors
*
238 ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBTRIPLE
) : sizeof(WORD
));
240 else /* assume BITMAPINFOHEADER */
242 colors
= info
->bmiHeader
.biClrUsed
;
243 if (colors
> 256) /* buffer overflow otherwise */
245 if (!colors
&& (info
->bmiHeader
.biBitCount
<= 8))
246 colors
= 1 << info
->bmiHeader
.biBitCount
;
247 if (info
->bmiHeader
.biCompression
== BI_BITFIELDS
) masks
= 3;
248 size
= max( info
->bmiHeader
.biSize
, sizeof(BITMAPINFOHEADER
) + masks
* sizeof(DWORD
) );
249 return size
+ colors
* ((coloruse
== DIB_RGB_COLORS
) ? sizeof(RGBQUAD
) : sizeof(WORD
));
253 static void DataCacheEntry_Destroy(DataCache
*cache
, DataCacheEntry
*cache_entry
)
255 list_remove(&cache_entry
->entry
);
256 CoTaskMemFree(cache_entry
->fmtetc
.ptd
);
257 ReleaseStgMedium(&cache_entry
->stgmedium
);
258 if(cache_entry
->sink_id
)
259 IDataObject_DUnadvise(cache
->running_object
, cache_entry
->sink_id
);
261 HeapFree(GetProcessHeap(), 0, cache_entry
);
264 static void DataCache_Destroy(
265 DataCache
* ptrToDestroy
)
267 DataCacheEntry
*cache_entry
, *next_cache_entry
;
271 if (ptrToDestroy
->sinkInterface
!= NULL
)
273 IAdviseSink_Release(ptrToDestroy
->sinkInterface
);
274 ptrToDestroy
->sinkInterface
= NULL
;
277 LIST_FOR_EACH_ENTRY_SAFE(cache_entry
, next_cache_entry
, &ptrToDestroy
->cache_list
, DataCacheEntry
, entry
)
278 DataCacheEntry_Destroy(ptrToDestroy
, cache_entry
);
280 if (ptrToDestroy
->presentationStorage
!= NULL
)
282 IStorage_Release(ptrToDestroy
->presentationStorage
);
283 ptrToDestroy
->presentationStorage
= NULL
;
287 * Free the datacache pointer.
289 HeapFree(GetProcessHeap(), 0, ptrToDestroy
);
292 static DataCacheEntry
*DataCache_GetEntryForFormatEtc(DataCache
*This
, const FORMATETC
*formatetc
)
294 DataCacheEntry
*cache_entry
;
295 FORMATETC fmt
= *formatetc
;
297 if (fmt
.cfFormat
== CF_BITMAP
)
299 fmt
.cfFormat
= CF_DIB
;
300 fmt
.tymed
= TYMED_HGLOBAL
;
303 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
305 /* FIXME: also compare DVTARGETDEVICEs */
306 if ((fmt
.cfFormat
== cache_entry
->fmtetc
.cfFormat
) &&
307 (fmt
.dwAspect
== cache_entry
->fmtetc
.dwAspect
) &&
308 (fmt
.lindex
== cache_entry
->fmtetc
.lindex
) &&
309 ((fmt
.tymed
== cache_entry
->fmtetc
.tymed
) || !cache_entry
->fmtetc
.cfFormat
)) /* tymed is ignored for view caching */
315 /* Returns the cache entry associated with a static CLSID.
316 This will be first in the list with connection id == 1 */
317 static HRESULT
get_static_entry( DataCache
*cache
, DataCacheEntry
**cache_entry
)
319 DataCacheEntry
*entry
;
320 struct list
*head
= list_head( &cache
->cache_list
);
327 entry
= LIST_ENTRY( head
, DataCacheEntry
, entry
);
330 *cache_entry
= entry
;
338 /* checks that the clipformat and tymed are valid and returns an error if they
339 * aren't and CACHE_S_NOTSUPPORTED if they are valid, but can't be rendered by
341 static HRESULT
check_valid_formatetc( const FORMATETC
*fmt
)
343 /* DVASPECT_ICON must be CF_METAFILEPICT */
344 if (fmt
->dwAspect
== DVASPECT_ICON
&& fmt
->cfFormat
!= CF_METAFILEPICT
)
345 return DV_E_FORMATETC
;
347 if (!fmt
->cfFormat
||
348 (fmt
->cfFormat
== CF_METAFILEPICT
&& fmt
->tymed
== TYMED_MFPICT
) ||
349 (fmt
->cfFormat
== CF_BITMAP
&& fmt
->tymed
== TYMED_GDI
) ||
350 (fmt
->cfFormat
== CF_DIB
&& fmt
->tymed
== TYMED_HGLOBAL
) ||
351 (fmt
->cfFormat
== CF_ENHMETAFILE
&& fmt
->tymed
== TYMED_ENHMF
))
353 else if (fmt
->tymed
== TYMED_HGLOBAL
)
354 return CACHE_S_FORMATETC_NOTSUPPORTED
;
357 WARN("invalid clipformat/tymed combination: %d/%d\n", fmt
->cfFormat
, fmt
->tymed
);
362 static BOOL
init_cache_entry(DataCacheEntry
*entry
, const FORMATETC
*fmt
, DWORD advf
,
367 hr
= copy_formatetc(&entry
->fmtetc
, fmt
);
368 if (FAILED(hr
)) return FALSE
;
370 entry
->stgmedium
.tymed
= TYMED_NULL
;
371 entry
->stgmedium
.pUnkForRelease
= NULL
;
374 entry
->load_stream_num
= STREAM_NUMBER_NOT_SET
;
375 entry
->save_stream_num
= STREAM_NUMBER_NOT_SET
;
377 entry
->advise_flags
= advf
;
382 static HRESULT
DataCache_CreateEntry(DataCache
*This
, const FORMATETC
*formatetc
, DWORD advf
,
383 BOOL automatic
, DataCacheEntry
**cache_entry
)
386 DWORD id
= automatic
? 1 : This
->last_cache_id
;
387 DataCacheEntry
*entry
;
389 hr
= check_valid_formatetc( formatetc
);
392 if (hr
== CACHE_S_FORMATETC_NOTSUPPORTED
)
393 TRACE("creating unsupported format %d\n", formatetc
->cfFormat
);
395 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(*entry
));
397 return E_OUTOFMEMORY
;
399 if (!init_cache_entry(entry
, formatetc
, advf
, id
))
403 list_add_head(&This
->cache_list
, &entry
->entry
);
406 list_add_tail(&This
->cache_list
, &entry
->entry
);
407 This
->last_cache_id
++;
410 if (cache_entry
) *cache_entry
= entry
;
414 HeapFree(GetProcessHeap(), 0, entry
);
415 return E_OUTOFMEMORY
;
418 /************************************************************************
419 * DataCache_FireOnViewChange
421 * This method will fire an OnViewChange notification to the advise
422 * sink registered with the datacache.
424 * See IAdviseSink::OnViewChange for more details.
426 static void DataCache_FireOnViewChange(
431 TRACE("(%p, %x, %d)\n", this, aspect
, lindex
);
434 * The sink supplies a filter when it registers
435 * we make sure we only send the notifications when that
438 if ((this->sinkAspects
& aspect
) != 0)
440 if (this->sinkInterface
!= NULL
)
442 IAdviseSink_OnViewChange(this->sinkInterface
,
447 * Some sinks want to be unregistered automatically when
448 * the first notification goes out.
450 if ( (this->sinkAdviseFlag
& ADVF_ONLYONCE
) != 0)
452 IAdviseSink_Release(this->sinkInterface
);
454 this->sinkInterface
= NULL
;
455 this->sinkAspects
= 0;
456 this->sinkAdviseFlag
= 0;
462 static HRESULT
read_clipformat(IStream
*stream
, CLIPFORMAT
*clipformat
)
470 hr
= IStream_Read(stream
, &length
, sizeof(length
), &read
);
471 if (hr
!= S_OK
|| read
!= sizeof(length
))
472 return DV_E_CLIPFORMAT
;
474 /* No clipboard format present */
480 hr
= IStream_Read(stream
, &cf
, sizeof(cf
), &read
);
481 if (hr
!= S_OK
|| read
!= sizeof(cf
))
482 return DV_E_CLIPFORMAT
;
487 char *format_name
= HeapAlloc(GetProcessHeap(), 0, length
);
489 return E_OUTOFMEMORY
;
490 hr
= IStream_Read(stream
, format_name
, length
, &read
);
491 if (hr
!= S_OK
|| read
!= length
|| format_name
[length
- 1] != '\0')
493 HeapFree(GetProcessHeap(), 0, format_name
);
494 return DV_E_CLIPFORMAT
;
496 *clipformat
= RegisterClipboardFormatA(format_name
);
497 HeapFree(GetProcessHeap(), 0, format_name
);
502 static HRESULT
write_clipformat(IStream
*stream
, CLIPFORMAT clipformat
)
506 char format_name
[256];
510 else if (clipformat
< 0xc000)
514 length
= GetClipboardFormatNameA(clipformat
, format_name
, sizeof(format_name
));
515 /* If there is a clipboard format name, we need to include its terminating \0 */
516 if (length
) length
++;
518 hr
= IStream_Write(stream
, &length
, sizeof(length
), NULL
);
519 if (FAILED(hr
) || clipformat
== 0)
522 if (clipformat
< 0xc000)
524 DWORD cf
= clipformat
;
525 hr
= IStream_Write(stream
, &cf
, sizeof(cf
), NULL
);
529 hr
= IStream_Write(stream
, format_name
, length
, NULL
);
534 static const WCHAR CONTENTS
[] = {'C','O','N','T','E','N','T','S',0};
536 static HRESULT
open_pres_stream( IStorage
*stg
, int stream_number
, IStream
**stm
)
538 WCHAR pres
[] = {2,'O','l','e','P','r','e','s',
539 '0' + (stream_number
/ 100) % 10,
540 '0' + (stream_number
/ 10) % 10,
541 '0' + stream_number
% 10, 0};
542 const WCHAR
*name
= pres
;
544 if (stream_number
== STREAM_NUMBER_NOT_SET
) return E_FAIL
;
545 if (stream_number
== STREAM_NUMBER_CONTENTS
) name
= CONTENTS
;
547 return IStorage_OpenStream( stg
, name
, NULL
, STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
550 static HRESULT
load_mf_pict( DataCacheEntry
*cache_entry
, IStream
*stm
)
554 ULARGE_INTEGER current_pos
;
556 METAFILEPICT
*mfpict
;
558 PresentationDataHeader header
;
559 CLIPFORMAT clipformat
;
560 static const LARGE_INTEGER offset_zero
;
563 if (cache_entry
->load_stream_num
== STREAM_NUMBER_CONTENTS
)
565 FIXME( "Unimplemented for CONTENTS stream\n" );
569 hr
= IStream_Stat( stm
, &stat
, STATFLAG_NONAME
);
570 if (FAILED( hr
)) return hr
;
572 hr
= read_clipformat( stm
, &clipformat
);
573 if (FAILED( hr
)) return hr
;
575 hr
= IStream_Read( stm
, &header
, sizeof(header
), &read
);
576 if (hr
!= S_OK
|| read
!= sizeof(header
)) return E_FAIL
;
578 hr
= IStream_Seek( stm
, offset_zero
, STREAM_SEEK_CUR
, ¤t_pos
);
579 if (FAILED( hr
)) return hr
;
581 stat
.cbSize
.QuadPart
-= current_pos
.QuadPart
;
583 hmfpict
= GlobalAlloc( GMEM_MOVEABLE
, sizeof(METAFILEPICT
) );
584 if (!hmfpict
) return E_OUTOFMEMORY
;
585 mfpict
= GlobalLock( hmfpict
);
587 bits
= HeapAlloc( GetProcessHeap(), 0, stat
.cbSize
.u
.LowPart
);
590 GlobalFree( hmfpict
);
591 return E_OUTOFMEMORY
;
594 hr
= IStream_Read( stm
, bits
, stat
.cbSize
.u
.LowPart
, &read
);
595 if (hr
!= S_OK
|| read
!= stat
.cbSize
.u
.LowPart
) hr
= E_FAIL
;
599 /* FIXME: get this from the stream */
600 mfpict
->mm
= MM_ANISOTROPIC
;
601 mfpict
->xExt
= header
.dwObjectExtentX
;
602 mfpict
->yExt
= header
.dwObjectExtentY
;
603 mfpict
->hMF
= SetMetaFileBitsEx( stat
.cbSize
.u
.LowPart
, bits
);
608 GlobalUnlock( hmfpict
);
611 cache_entry
->stgmedium
.tymed
= TYMED_MFPICT
;
612 cache_entry
->stgmedium
.u
.hMetaFilePict
= hmfpict
;
615 GlobalFree( hmfpict
);
617 HeapFree( GetProcessHeap(), 0, bits
);
622 static HRESULT
load_dib( DataCacheEntry
*cache_entry
, IStream
*stm
)
628 ULONG read
, info_size
, bi_size
;
629 BITMAPFILEHEADER file
;
630 BITMAPINFOHEADER
*info
;
632 if (cache_entry
->load_stream_num
!= STREAM_NUMBER_CONTENTS
)
634 FIXME( "Unimplemented for presentation stream\n" );
638 hr
= IStream_Stat( stm
, &stat
, STATFLAG_NONAME
);
639 if (FAILED( hr
)) return hr
;
641 if (stat
.cbSize
.QuadPart
< sizeof(file
) + sizeof(DWORD
)) return E_FAIL
;
642 hr
= IStream_Read( stm
, &file
, sizeof(file
), &read
);
643 if (hr
!= S_OK
|| read
!= sizeof(file
)) return E_FAIL
;
644 stat
.cbSize
.QuadPart
-= sizeof(file
);
646 hglobal
= GlobalAlloc( GMEM_MOVEABLE
, stat
.cbSize
.u
.LowPart
);
647 if (!hglobal
) return E_OUTOFMEMORY
;
648 dib
= GlobalLock( hglobal
);
650 hr
= IStream_Read( stm
, dib
, sizeof(DWORD
), &read
);
651 if (hr
!= S_OK
|| read
!= sizeof(DWORD
)) goto fail
;
652 bi_size
= *(DWORD
*)dib
;
653 if (stat
.cbSize
.QuadPart
< bi_size
) goto fail
;
655 hr
= IStream_Read( stm
, (char *)dib
+ sizeof(DWORD
), bi_size
- sizeof(DWORD
), &read
);
656 if (hr
!= S_OK
|| read
!= bi_size
- sizeof(DWORD
)) goto fail
;
658 info_size
= bitmap_info_size( dib
, DIB_RGB_COLORS
);
659 if (stat
.cbSize
.QuadPart
< info_size
) goto fail
;
660 if (info_size
> bi_size
)
662 hr
= IStream_Read( stm
, (char *)dib
+ bi_size
, info_size
- bi_size
, &read
);
663 if (hr
!= S_OK
|| read
!= info_size
- bi_size
) goto fail
;
665 stat
.cbSize
.QuadPart
-= info_size
;
671 skip
.QuadPart
= file
.bfOffBits
- sizeof(file
) - info_size
;
672 if (stat
.cbSize
.QuadPart
< skip
.QuadPart
) goto fail
;
673 hr
= IStream_Seek( stm
, skip
, STREAM_SEEK_CUR
, NULL
);
674 if (hr
!= S_OK
) goto fail
;
675 stat
.cbSize
.QuadPart
-= skip
.QuadPart
;
678 hr
= IStream_Read( stm
, (char *)dib
+ info_size
, stat
.cbSize
.u
.LowPart
, &read
);
679 if (hr
!= S_OK
|| read
!= stat
.cbSize
.QuadPart
) goto fail
;
681 if (bi_size
>= sizeof(*info
))
683 info
= (BITMAPINFOHEADER
*)dib
;
684 if (info
->biXPelsPerMeter
== 0 || info
->biYPelsPerMeter
== 0)
686 HDC hdc
= GetDC( 0 );
687 info
->biXPelsPerMeter
= MulDiv( GetDeviceCaps( hdc
, LOGPIXELSX
), 10000, 254 );
688 info
->biYPelsPerMeter
= MulDiv( GetDeviceCaps( hdc
, LOGPIXELSY
), 10000, 254 );
693 GlobalUnlock( hglobal
);
695 cache_entry
->stgmedium
.tymed
= TYMED_HGLOBAL
;
696 cache_entry
->stgmedium
.u
.hGlobal
= hglobal
;
701 GlobalUnlock( hglobal
);
702 GlobalFree( hglobal
);
707 /************************************************************************
708 * DataCacheEntry_LoadData
710 * This method will read information for the requested presentation
711 * into the given structure.
714 * This - The entry to load the data from.
717 * This method returns a metafile handle if it is successful.
718 * it will return 0 if not.
720 static HRESULT
DataCacheEntry_LoadData(DataCacheEntry
*cache_entry
, IStorage
*stg
)
725 if (!stg
) return OLE_E_BLANK
;
726 hr
= open_pres_stream( stg
, cache_entry
->load_stream_num
, &stm
);
727 if (FAILED(hr
)) return hr
;
729 switch (cache_entry
->fmtetc
.cfFormat
)
731 case CF_METAFILEPICT
:
732 hr
= load_mf_pict( cache_entry
, stm
);
736 hr
= load_dib( cache_entry
, stm
);
740 FIXME( "Unimplemented clip format %x\n", cache_entry
->fmtetc
.cfFormat
);
744 IStream_Release( stm
);
748 static void init_stream_header(DataCacheEntry
*entry
, PresentationDataHeader
*header
)
750 if (entry
->fmtetc
.ptd
)
751 FIXME("ptd not serialized\n");
752 header
->tdSize
= sizeof(header
->tdSize
);
753 header
->dvAspect
= entry
->fmtetc
.dwAspect
;
754 header
->lindex
= entry
->fmtetc
.lindex
;
755 header
->advf
= entry
->advise_flags
;
756 header
->unknown7
= 0;
757 header
->dwObjectExtentX
= 0;
758 header
->dwObjectExtentY
= 0;
762 static HRESULT
save_dib(DataCacheEntry
*entry
, BOOL contents
, IStream
*stream
)
766 BITMAPINFO
*bmi
= NULL
;
768 if (entry
->stgmedium
.tymed
!= TYMED_NULL
)
770 data_size
= GlobalSize(entry
->stgmedium
.u
.hGlobal
);
771 bmi
= GlobalLock(entry
->stgmedium
.u
.hGlobal
);
776 PresentationDataHeader header
;
778 init_stream_header(entry
, &header
);
779 hr
= write_clipformat(stream
, entry
->fmtetc
.cfFormat
);
780 if (FAILED(hr
)) goto end
;
783 header
.dwSize
= data_size
;
784 /* Size in units of 0.01mm (ie. MM_HIMETRIC) */
785 if (bmi
->bmiHeader
.biXPelsPerMeter
!= 0 && bmi
->bmiHeader
.biYPelsPerMeter
!= 0)
787 header
.dwObjectExtentX
= MulDiv(bmi
->bmiHeader
.biWidth
, 100000, bmi
->bmiHeader
.biXPelsPerMeter
);
788 header
.dwObjectExtentY
= MulDiv(bmi
->bmiHeader
.biHeight
, 100000, bmi
->bmiHeader
.biYPelsPerMeter
);
793 header
.dwObjectExtentX
= MulDiv(bmi
->bmiHeader
.biWidth
, 2540, GetDeviceCaps(hdc
, LOGPIXELSX
));
794 header
.dwObjectExtentY
= MulDiv(bmi
->bmiHeader
.biHeight
, 2540, GetDeviceCaps(hdc
, LOGPIXELSY
));
798 hr
= IStream_Write(stream
, &header
, sizeof(PresentationDataHeader
), NULL
);
799 if (hr
== S_OK
&& data_size
)
800 hr
= IStream_Write(stream
, bmi
, data_size
, NULL
);
804 BITMAPFILEHEADER bmp_fhdr
;
806 bmp_fhdr
.bfType
= 0x4d42;
807 bmp_fhdr
.bfSize
= data_size
+ sizeof(BITMAPFILEHEADER
);
808 bmp_fhdr
.bfReserved1
= bmp_fhdr
.bfReserved2
= 0;
809 bmp_fhdr
.bfOffBits
= bitmap_info_size(bmi
, DIB_RGB_COLORS
) + sizeof(BITMAPFILEHEADER
);
810 hr
= IStream_Write(stream
, &bmp_fhdr
, sizeof(BITMAPFILEHEADER
), NULL
);
812 hr
= IStream_Write(stream
, bmi
, data_size
, NULL
);
816 if (bmi
) GlobalUnlock(entry
->stgmedium
.u
.hGlobal
);
820 #include <pshpack2.h>
821 struct meta_placeable
825 WORD bounding_box
[4];
832 static HRESULT
save_mfpict(DataCacheEntry
*entry
, BOOL contents
, IStream
*stream
)
837 METAFILEPICT
*mfpict
= NULL
;
841 PresentationDataHeader header
;
843 init_stream_header(entry
, &header
);
844 hr
= write_clipformat(stream
, entry
->fmtetc
.cfFormat
);
845 if (FAILED(hr
)) return hr
;
846 if (entry
->stgmedium
.tymed
!= TYMED_NULL
)
848 mfpict
= GlobalLock(entry
->stgmedium
.u
.hMetaFilePict
);
850 return DV_E_STGMEDIUM
;
851 data_size
= GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
852 header
.dwObjectExtentX
= mfpict
->xExt
;
853 header
.dwObjectExtentY
= mfpict
->yExt
;
854 header
.dwSize
= data_size
;
855 data
= HeapAlloc(GetProcessHeap(), 0, header
.dwSize
);
858 GlobalUnlock(entry
->stgmedium
.u
.hMetaFilePict
);
859 return E_OUTOFMEMORY
;
861 GetMetaFileBitsEx(mfpict
->hMF
, header
.dwSize
, data
);
862 GlobalUnlock(entry
->stgmedium
.u
.hMetaFilePict
);
864 hr
= IStream_Write(stream
, &header
, sizeof(PresentationDataHeader
), NULL
);
865 if (hr
== S_OK
&& data_size
)
866 hr
= IStream_Write(stream
, data
, data_size
, NULL
);
867 HeapFree(GetProcessHeap(), 0, data
);
869 else if (entry
->stgmedium
.tymed
!= TYMED_NULL
)
871 struct meta_placeable meta_place_rec
;
874 mfpict
= GlobalLock(entry
->stgmedium
.u
.hMetaFilePict
);
876 return DV_E_STGMEDIUM
;
877 data_size
= GetMetaFileBitsEx(mfpict
->hMF
, 0, NULL
);
878 data
= HeapAlloc(GetProcessHeap(), 0, data_size
);
881 GlobalUnlock(entry
->stgmedium
.u
.hMetaFilePict
);
882 return E_OUTOFMEMORY
;
884 GetMetaFileBitsEx(mfpict
->hMF
, data_size
, data
);
886 /* units are in 1/8th of a point (1 point is 1/72th of an inch) */
887 meta_place_rec
.key
= 0x9ac6cdd7;
888 meta_place_rec
.hwmf
= 0;
889 meta_place_rec
.inch
= 576;
890 meta_place_rec
.bounding_box
[0] = 0;
891 meta_place_rec
.bounding_box
[1] = 0;
892 meta_place_rec
.bounding_box
[2] = 0;
893 meta_place_rec
.bounding_box
[3] = 0;
894 meta_place_rec
.checksum
= 0;
895 meta_place_rec
.reserved
= 0;
897 /* These values are rounded down so MulDiv won't do the right thing */
898 meta_place_rec
.bounding_box
[2] = (LONGLONG
)mfpict
->xExt
* meta_place_rec
.inch
/ 2540;
899 meta_place_rec
.bounding_box
[3] = (LONGLONG
)mfpict
->yExt
* meta_place_rec
.inch
/ 2540;
900 GlobalUnlock(entry
->stgmedium
.u
.hMetaFilePict
);
902 for (check
= (WORD
*)&meta_place_rec
; check
!= (WORD
*)&meta_place_rec
.checksum
; check
++)
903 meta_place_rec
.checksum
^= *check
;
904 hr
= IStream_Write(stream
, &meta_place_rec
, sizeof(struct meta_placeable
), NULL
);
905 if (hr
== S_OK
&& data_size
)
906 hr
= IStream_Write(stream
, data
, data_size
, NULL
);
907 HeapFree(GetProcessHeap(), 0, data
);
913 static HRESULT
save_emf(DataCacheEntry
*entry
, BOOL contents
, IStream
*stream
)
921 PresentationDataHeader header
;
922 METAFILEPICT
*mfpict
;
925 init_stream_header(entry
, &header
);
926 hr
= write_clipformat(stream
, entry
->fmtetc
.cfFormat
);
932 data_size
= GetWinMetaFileBits(entry
->stgmedium
.u
.hEnhMetaFile
, 0, NULL
, MM_ANISOTROPIC
, hdc
);
933 header
.dwSize
= data_size
;
934 data
= HeapAlloc(GetProcessHeap(), 0, header
.dwSize
);
938 return E_OUTOFMEMORY
;
940 GetWinMetaFileBits(entry
->stgmedium
.u
.hEnhMetaFile
, header
.dwSize
, data
, MM_ANISOTROPIC
, hdc
);
942 mfpict
= (METAFILEPICT
*)data
;
943 header
.dwObjectExtentX
= mfpict
->xExt
;
944 header
.dwObjectExtentY
= mfpict
->yExt
;
945 hr
= IStream_Write(stream
, &header
, sizeof(PresentationDataHeader
), NULL
);
946 if (hr
== S_OK
&& data_size
)
947 hr
= IStream_Write(stream
, data
, data_size
, NULL
);
948 HeapFree(GetProcessHeap(), 0, data
);
950 else if (entry
->stgmedium
.tymed
!= TYMED_NULL
)
952 data_size
= GetEnhMetaFileBits(entry
->stgmedium
.u
.hEnhMetaFile
, 0, NULL
);
953 data
= HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD
) + sizeof(ENHMETAHEADER
) + data_size
);
954 if (!data
) return E_OUTOFMEMORY
;
955 *((DWORD
*)data
) = sizeof(ENHMETAHEADER
);
956 GetEnhMetaFileBits(entry
->stgmedium
.u
.hEnhMetaFile
, data_size
, data
+ sizeof(DWORD
) + sizeof(ENHMETAHEADER
));
957 memcpy(data
+ sizeof(DWORD
), data
+ sizeof(DWORD
) + sizeof(ENHMETAHEADER
), sizeof(ENHMETAHEADER
));
958 data_size
+= sizeof(DWORD
) + sizeof(ENHMETAHEADER
);
959 hr
= IStream_Write(stream
, data
, data_size
, NULL
);
960 HeapFree(GetProcessHeap(), 0, data
);
966 static HRESULT
save_view_cache(DataCacheEntry
*entry
, IStream
*stream
)
969 PresentationDataHeader header
;
971 init_stream_header(entry
, &header
);
972 hr
= write_clipformat(stream
, entry
->fmtetc
.cfFormat
);
974 hr
= IStream_Write(stream
, &header
, FIELD_OFFSET(PresentationDataHeader
, unknown7
), NULL
);
979 static HRESULT
create_stream(DataCacheEntry
*cache_entry
, IStorage
*storage
,
980 BOOL contents
, IStream
**stream
)
982 WCHAR pres
[] = {2,'O','l','e','P','r','e','s',
983 '0' + (cache_entry
->save_stream_num
/ 100) % 10,
984 '0' + (cache_entry
->save_stream_num
/ 10) % 10,
985 '0' + cache_entry
->save_stream_num
% 10, 0};
993 return IStorage_CreateStream(storage
, name
,
994 STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
| STGM_CREATE
,
998 static HRESULT
DataCacheEntry_Save(DataCacheEntry
*cache_entry
, IStorage
*storage
,
1003 BOOL contents
= (cache_entry
->id
== 1);
1005 TRACE("stream_number = %d, fmtetc = %s\n", cache_entry
->save_stream_num
, debugstr_formatetc(&cache_entry
->fmtetc
));
1007 hr
= create_stream(cache_entry
, storage
, contents
, &stream
);
1011 switch (cache_entry
->fmtetc
.cfFormat
)
1014 hr
= save_dib(cache_entry
, contents
, stream
);
1016 case CF_METAFILEPICT
:
1017 hr
= save_mfpict(cache_entry
, contents
, stream
);
1019 case CF_ENHMETAFILE
:
1020 hr
= save_emf(cache_entry
, contents
, stream
);
1023 hr
= save_view_cache(cache_entry
, stream
);
1026 FIXME("got unsupported clipboard format %x\n", cache_entry
->fmtetc
.cfFormat
);
1029 IStream_Release(stream
);
1033 /* helper for copying STGMEDIUM of type bitmap, MF, EMF or HGLOBAL.
1034 * does no checking of whether src_stgm has a supported tymed, so this should be
1035 * done in the caller */
1036 static HRESULT
copy_stg_medium(CLIPFORMAT cf
, STGMEDIUM
*dest_stgm
,
1037 const STGMEDIUM
*src_stgm
)
1039 if (src_stgm
->tymed
== TYMED_MFPICT
)
1041 const METAFILEPICT
*src_mfpict
= GlobalLock(src_stgm
->u
.hMetaFilePict
);
1042 METAFILEPICT
*dest_mfpict
;
1045 return DV_E_STGMEDIUM
;
1046 dest_stgm
->u
.hMetaFilePict
= GlobalAlloc(GMEM_MOVEABLE
, sizeof(METAFILEPICT
));
1047 dest_mfpict
= GlobalLock(dest_stgm
->u
.hMetaFilePict
);
1050 GlobalUnlock(src_stgm
->u
.hMetaFilePict
);
1051 return E_OUTOFMEMORY
;
1053 *dest_mfpict
= *src_mfpict
;
1054 dest_mfpict
->hMF
= CopyMetaFileW(src_mfpict
->hMF
, NULL
);
1055 GlobalUnlock(src_stgm
->u
.hMetaFilePict
);
1056 GlobalUnlock(dest_stgm
->u
.hMetaFilePict
);
1058 else if (src_stgm
->tymed
!= TYMED_NULL
)
1060 dest_stgm
->u
.hGlobal
= OleDuplicateData(src_stgm
->u
.hGlobal
, cf
,
1062 if (!dest_stgm
->u
.hGlobal
)
1063 return E_OUTOFMEMORY
;
1065 dest_stgm
->tymed
= src_stgm
->tymed
;
1066 dest_stgm
->pUnkForRelease
= src_stgm
->pUnkForRelease
;
1067 if (dest_stgm
->pUnkForRelease
)
1068 IUnknown_AddRef(dest_stgm
->pUnkForRelease
);
1072 static HRESULT
synthesize_dib( HBITMAP bm
, STGMEDIUM
*med
)
1074 HDC hdc
= GetDC( 0 );
1075 BITMAPINFOHEADER header
;
1077 HRESULT hr
= E_FAIL
;
1080 memset( &header
, 0, sizeof(header
) );
1081 header
.biSize
= sizeof(header
);
1082 if (!GetDIBits( hdc
, bm
, 0, 0, NULL
, (BITMAPINFO
*)&header
, DIB_RGB_COLORS
)) goto done
;
1084 header_size
= bitmap_info_size( (BITMAPINFO
*)&header
, DIB_RGB_COLORS
);
1085 if (!(med
->u
.hGlobal
= GlobalAlloc( GMEM_MOVEABLE
, header_size
+ header
.biSizeImage
))) goto done
;
1086 bmi
= GlobalLock( med
->u
.hGlobal
);
1087 memset( bmi
, 0, header_size
);
1088 memcpy( bmi
, &header
, header
.biSize
);
1089 GetDIBits( hdc
, bm
, 0, abs(header
.biHeight
), (char *)bmi
+ header_size
, bmi
, DIB_RGB_COLORS
);
1090 GlobalUnlock( med
->u
.hGlobal
);
1091 med
->tymed
= TYMED_HGLOBAL
;
1092 med
->pUnkForRelease
= NULL
;
1096 ReleaseDC( 0, hdc
);
1100 static HRESULT
synthesize_bitmap( HGLOBAL dib
, STGMEDIUM
*med
)
1102 HRESULT hr
= E_FAIL
;
1104 HDC hdc
= GetDC( 0 );
1106 if ((bmi
= GlobalLock( dib
)))
1108 /* FIXME: validate data size */
1109 med
->u
.hBitmap
= CreateDIBitmap( hdc
, &bmi
->bmiHeader
, CBM_INIT
,
1110 (char *)bmi
+ bitmap_info_size( bmi
, DIB_RGB_COLORS
),
1111 bmi
, DIB_RGB_COLORS
);
1112 GlobalUnlock( dib
);
1113 med
->tymed
= TYMED_GDI
;
1114 med
->pUnkForRelease
= NULL
;
1117 ReleaseDC( 0, hdc
);
1121 static HRESULT
synthesize_emf( HMETAFILEPICT data
, STGMEDIUM
*med
)
1124 HRESULT hr
= E_FAIL
;
1128 if (!(pict
= GlobalLock( data
))) return hr
;
1130 size
= GetMetaFileBitsEx( pict
->hMF
, 0, NULL
);
1131 if ((bits
= HeapAlloc( GetProcessHeap(), 0, size
)))
1133 GetMetaFileBitsEx( pict
->hMF
, size
, bits
);
1134 med
->u
.hEnhMetaFile
= SetWinMetaFileBits( size
, bits
, NULL
, pict
);
1135 HeapFree( GetProcessHeap(), 0, bits
);
1136 med
->tymed
= TYMED_ENHMF
;
1137 med
->pUnkForRelease
= NULL
;
1141 GlobalUnlock( data
);
1145 static HRESULT
DataCacheEntry_SetData(DataCacheEntry
*cache_entry
,
1146 const FORMATETC
*formatetc
,
1147 STGMEDIUM
*stgmedium
,
1153 if ((!cache_entry
->fmtetc
.cfFormat
&& !formatetc
->cfFormat
) ||
1154 (cache_entry
->fmtetc
.tymed
== TYMED_NULL
&& formatetc
->tymed
== TYMED_NULL
) ||
1155 stgmedium
->tymed
== TYMED_NULL
)
1157 WARN("invalid formatetc\n");
1158 return DV_E_FORMATETC
;
1161 cache_entry
->dirty
= TRUE
;
1162 ReleaseStgMedium(&cache_entry
->stgmedium
);
1164 if (formatetc
->cfFormat
== CF_BITMAP
)
1166 hr
= synthesize_dib( stgmedium
->u
.hBitmap
, ©
);
1167 if (FAILED(hr
)) return hr
;
1168 if (fRelease
) ReleaseStgMedium(stgmedium
);
1172 else if (formatetc
->cfFormat
== CF_METAFILEPICT
&& cache_entry
->fmtetc
.cfFormat
== CF_ENHMETAFILE
)
1174 hr
= synthesize_emf( stgmedium
->u
.hMetaFilePict
, ©
);
1175 if (FAILED(hr
)) return hr
;
1176 if (fRelease
) ReleaseStgMedium(stgmedium
);
1183 cache_entry
->stgmedium
= *stgmedium
;
1187 return copy_stg_medium(cache_entry
->fmtetc
.cfFormat
, &cache_entry
->stgmedium
, stgmedium
);
1190 static HRESULT
DataCacheEntry_GetData(DataCacheEntry
*cache_entry
, IStorage
*stg
, FORMATETC
*fmt
, STGMEDIUM
*stgmedium
)
1192 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
&& cache_entry
->load_stream_num
!= STREAM_NUMBER_NOT_SET
)
1194 HRESULT hr
= DataCacheEntry_LoadData(cache_entry
, stg
);
1198 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
1201 if (fmt
->cfFormat
== CF_BITMAP
)
1202 return synthesize_bitmap( cache_entry
->stgmedium
.u
.hGlobal
, stgmedium
);
1204 return copy_stg_medium(cache_entry
->fmtetc
.cfFormat
, stgmedium
, &cache_entry
->stgmedium
);
1207 static inline HRESULT
DataCacheEntry_DiscardData(DataCacheEntry
*cache_entry
)
1209 ReleaseStgMedium(&cache_entry
->stgmedium
);
1213 static inline DWORD
tymed_from_cf( DWORD cf
)
1217 case CF_BITMAP
: return TYMED_GDI
;
1218 case CF_METAFILEPICT
: return TYMED_MFPICT
;
1219 case CF_ENHMETAFILE
: return TYMED_ENHMF
;
1221 default: return TYMED_HGLOBAL
;
1225 /****************************************************************
1226 * create_automatic_entry
1228 * Creates an appropriate cache entry for one of the CLSID_Picture_
1229 * classes. The connection id of the entry is one. Any pre-existing
1230 * automatic entry is re-assigned a new connection id, and moved to
1231 * the end of the list.
1233 static HRESULT
create_automatic_entry(DataCache
*cache
, const CLSID
*clsid
)
1235 static const struct data
1241 { &CLSID_Picture_Dib
, { CF_DIB
, 0, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
} },
1242 { &CLSID_Picture_Metafile
, { CF_METAFILEPICT
, 0, DVASPECT_CONTENT
, -1, TYMED_MFPICT
} },
1243 { &CLSID_Picture_EnhMetafile
, { CF_ENHMETAFILE
, 0, DVASPECT_CONTENT
, -1, TYMED_ENHMF
} },
1246 const struct data
*ptr
= data
;
1248 DataCacheEntry
*entry
;
1250 if (IsEqualCLSID( &cache
->clsid
, clsid
)) return S_OK
;
1252 /* move and reassign any pre-existing automatic entry */
1253 if ((head
= list_head( &cache
->cache_list
)))
1255 entry
= LIST_ENTRY( head
, DataCacheEntry
, entry
);
1258 list_remove( &entry
->entry
);
1259 entry
->id
= cache
->last_cache_id
++;
1260 list_add_tail( &cache
->cache_list
, &entry
->entry
);
1266 if (IsEqualCLSID( clsid
, ptr
->clsid
))
1268 cache
->clsid_static
= TRUE
;
1269 return DataCache_CreateEntry( cache
, &ptr
->fmt
, 0, TRUE
, NULL
);
1273 cache
->clsid_static
= FALSE
;
1277 /*********************************************************
1278 * Method implementation for the non delegating IUnknown
1279 * part of the DataCache class.
1282 /************************************************************************
1283 * DataCache_NDIUnknown_QueryInterface (IUnknown)
1285 * This version of QueryInterface will not delegate its implementation
1286 * to the outer unknown.
1288 static HRESULT WINAPI
DataCache_NDIUnknown_QueryInterface(
1293 DataCache
*this = impl_from_IUnknown(iface
);
1296 return E_INVALIDARG
;
1300 if (IsEqualIID(&IID_IUnknown
, riid
))
1302 if (this->outer_unk
== iface
) /* non-aggregated, return IUnknown from IOleCache2 */
1303 *ppvObject
= &this->IOleCache2_iface
;
1307 else if (IsEqualIID(&IID_IDataObject
, riid
))
1309 *ppvObject
= &this->IDataObject_iface
;
1311 else if ( IsEqualIID(&IID_IPersistStorage
, riid
) ||
1312 IsEqualIID(&IID_IPersist
, riid
) )
1314 *ppvObject
= &this->IPersistStorage_iface
;
1316 else if ( IsEqualIID(&IID_IViewObject
, riid
) ||
1317 IsEqualIID(&IID_IViewObject2
, riid
) )
1319 *ppvObject
= &this->IViewObject2_iface
;
1321 else if ( IsEqualIID(&IID_IOleCache
, riid
) ||
1322 IsEqualIID(&IID_IOleCache2
, riid
) )
1324 *ppvObject
= &this->IOleCache2_iface
;
1326 else if ( IsEqualIID(&IID_IOleCacheControl
, riid
) )
1328 *ppvObject
= &this->IOleCacheControl_iface
;
1331 if ((*ppvObject
)==0)
1333 WARN( "() : asking for unsupported interface %s\n", debugstr_guid(riid
));
1334 return E_NOINTERFACE
;
1337 IUnknown_AddRef((IUnknown
*)*ppvObject
);
1342 /************************************************************************
1343 * DataCache_NDIUnknown_AddRef (IUnknown)
1345 * This version of QueryInterface will not delegate its implementation
1346 * to the outer unknown.
1348 static ULONG WINAPI
DataCache_NDIUnknown_AddRef(
1351 DataCache
*this = impl_from_IUnknown(iface
);
1352 return InterlockedIncrement(&this->ref
);
1355 /************************************************************************
1356 * DataCache_NDIUnknown_Release (IUnknown)
1358 * This version of QueryInterface will not delegate its implementation
1359 * to the outer unknown.
1361 static ULONG WINAPI
DataCache_NDIUnknown_Release(
1364 DataCache
*this = impl_from_IUnknown(iface
);
1367 ref
= InterlockedDecrement(&this->ref
);
1369 if (ref
== 0) DataCache_Destroy(this);
1374 /*********************************************************
1375 * Method implementation for the IDataObject
1376 * part of the DataCache class.
1379 /************************************************************************
1380 * DataCache_IDataObject_QueryInterface (IUnknown)
1382 static HRESULT WINAPI
DataCache_IDataObject_QueryInterface(
1387 DataCache
*this = impl_from_IDataObject(iface
);
1389 return IUnknown_QueryInterface(this->outer_unk
, riid
, ppvObject
);
1392 /************************************************************************
1393 * DataCache_IDataObject_AddRef (IUnknown)
1395 static ULONG WINAPI
DataCache_IDataObject_AddRef(
1398 DataCache
*this = impl_from_IDataObject(iface
);
1400 return IUnknown_AddRef(this->outer_unk
);
1403 /************************************************************************
1404 * DataCache_IDataObject_Release (IUnknown)
1406 static ULONG WINAPI
DataCache_IDataObject_Release(
1409 DataCache
*this = impl_from_IDataObject(iface
);
1411 return IUnknown_Release(this->outer_unk
);
1414 /************************************************************************
1417 * Get Data from a source dataobject using format pformatetcIn->cfFormat
1419 static HRESULT WINAPI
DataCache_GetData(
1421 LPFORMATETC pformatetcIn
,
1424 DataCache
*This
= impl_from_IDataObject(iface
);
1425 DataCacheEntry
*cache_entry
;
1427 TRACE("(%p, %s, %p)\n", iface
, debugstr_formatetc(pformatetcIn
), pmedium
);
1429 memset(pmedium
, 0, sizeof(*pmedium
));
1431 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetcIn
);
1435 return DataCacheEntry_GetData(cache_entry
, This
->presentationStorage
, pformatetcIn
, pmedium
);
1438 static HRESULT WINAPI
DataCache_GetDataHere(
1440 LPFORMATETC pformatetc
,
1447 static HRESULT WINAPI
DataCache_QueryGetData( IDataObject
*iface
, FORMATETC
*fmt
)
1449 DataCache
*This
= impl_from_IDataObject( iface
);
1450 DataCacheEntry
*cache_entry
;
1452 TRACE( "(%p)->(%s)\n", iface
, debugstr_formatetc( fmt
) );
1453 cache_entry
= DataCache_GetEntryForFormatEtc( This
, fmt
);
1455 return cache_entry
? S_OK
: S_FALSE
;
1458 /************************************************************************
1459 * DataCache_EnumFormatEtc (IDataObject)
1461 * The data cache doesn't implement this method.
1463 static HRESULT WINAPI
DataCache_GetCanonicalFormatEtc(
1465 LPFORMATETC pformatectIn
,
1466 LPFORMATETC pformatetcOut
)
1472 /************************************************************************
1473 * DataCache_IDataObject_SetData (IDataObject)
1475 * This method is delegated to the IOleCache2 implementation.
1477 static HRESULT WINAPI
DataCache_IDataObject_SetData(
1479 LPFORMATETC pformatetc
,
1483 IOleCache2
* oleCache
= NULL
;
1486 TRACE("(%p, %p, %p, %d)\n", iface
, pformatetc
, pmedium
, fRelease
);
1488 hres
= IDataObject_QueryInterface(iface
, &IID_IOleCache2
, (void**)&oleCache
);
1491 return E_UNEXPECTED
;
1493 hres
= IOleCache2_SetData(oleCache
, pformatetc
, pmedium
, fRelease
);
1495 IOleCache2_Release(oleCache
);
1500 /************************************************************************
1501 * DataCache_EnumFormatEtc (IDataObject)
1503 * The data cache doesn't implement this method.
1505 static HRESULT WINAPI
DataCache_EnumFormatEtc(
1508 IEnumFORMATETC
** ppenumFormatEtc
)
1514 /************************************************************************
1515 * DataCache_DAdvise (IDataObject)
1517 * The data cache doesn't support connections.
1519 static HRESULT WINAPI
DataCache_DAdvise(
1521 FORMATETC
* pformatetc
,
1523 IAdviseSink
* pAdvSink
,
1524 DWORD
* pdwConnection
)
1527 return OLE_E_ADVISENOTSUPPORTED
;
1530 /************************************************************************
1531 * DataCache_DUnadvise (IDataObject)
1533 * The data cache doesn't support connections.
1535 static HRESULT WINAPI
DataCache_DUnadvise(
1540 return OLE_E_NOCONNECTION
;
1543 /************************************************************************
1544 * DataCache_EnumDAdvise (IDataObject)
1546 * The data cache doesn't support connections.
1548 static HRESULT WINAPI
DataCache_EnumDAdvise(
1550 IEnumSTATDATA
** ppenumAdvise
)
1553 return OLE_E_ADVISENOTSUPPORTED
;
1556 /*********************************************************
1557 * Method implementation for the IDataObject
1558 * part of the DataCache class.
1561 /************************************************************************
1562 * DataCache_IPersistStorage_QueryInterface (IUnknown)
1564 static HRESULT WINAPI
DataCache_IPersistStorage_QueryInterface(
1565 IPersistStorage
* iface
,
1569 DataCache
*this = impl_from_IPersistStorage(iface
);
1571 return IUnknown_QueryInterface(this->outer_unk
, riid
, ppvObject
);
1574 /************************************************************************
1575 * DataCache_IPersistStorage_AddRef (IUnknown)
1577 static ULONG WINAPI
DataCache_IPersistStorage_AddRef(
1578 IPersistStorage
* iface
)
1580 DataCache
*this = impl_from_IPersistStorage(iface
);
1582 return IUnknown_AddRef(this->outer_unk
);
1585 /************************************************************************
1586 * DataCache_IPersistStorage_Release (IUnknown)
1588 static ULONG WINAPI
DataCache_IPersistStorage_Release(
1589 IPersistStorage
* iface
)
1591 DataCache
*this = impl_from_IPersistStorage(iface
);
1593 return IUnknown_Release(this->outer_unk
);
1596 /************************************************************************
1597 * DataCache_GetClassID (IPersistStorage)
1600 static HRESULT WINAPI
DataCache_GetClassID(IPersistStorage
*iface
, CLSID
*clsid
)
1602 DataCache
*This
= impl_from_IPersistStorage( iface
);
1604 TRACE( "(%p, %p) returning %s\n", iface
, clsid
, debugstr_guid(&This
->clsid
) );
1605 *clsid
= This
->clsid
;
1610 /************************************************************************
1611 * DataCache_IsDirty (IPersistStorage)
1613 static HRESULT WINAPI
DataCache_IsDirty(
1614 IPersistStorage
* iface
)
1616 DataCache
*This
= impl_from_IPersistStorage(iface
);
1617 DataCacheEntry
*cache_entry
;
1619 TRACE("(%p)\n", iface
);
1624 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1625 if (cache_entry
->dirty
)
1631 /************************************************************************
1632 * DataCache_InitNew (IPersistStorage)
1634 * The data cache implementation of IPersistStorage_InitNew simply stores
1635 * the storage pointer.
1637 static HRESULT WINAPI
DataCache_InitNew(
1638 IPersistStorage
* iface
,
1641 DataCache
*This
= impl_from_IPersistStorage(iface
);
1645 TRACE("(%p, %p)\n", iface
, pStg
);
1647 if (This
->presentationStorage
!= NULL
)
1648 return CO_E_ALREADYINITIALIZED
;
1650 This
->presentationStorage
= pStg
;
1652 IStorage_AddRef(This
->presentationStorage
);
1654 ReadClassStg( pStg
, &clsid
);
1655 hr
= create_automatic_entry( This
, &clsid
);
1658 IStorage_Release( pStg
);
1659 This
->presentationStorage
= NULL
;
1662 This
->clsid
= clsid
;
1668 static HRESULT
add_cache_entry( DataCache
*This
, const FORMATETC
*fmt
, DWORD advf
, int stream_number
)
1670 DataCacheEntry
*cache_entry
;
1673 TRACE( "loading entry with formatetc: %s\n", debugstr_formatetc( fmt
) );
1675 cache_entry
= DataCache_GetEntryForFormatEtc( This
, fmt
);
1677 hr
= DataCache_CreateEntry( This
, fmt
, advf
, FALSE
, &cache_entry
);
1678 if (SUCCEEDED( hr
))
1680 DataCacheEntry_DiscardData( cache_entry
);
1681 cache_entry
->load_stream_num
= stream_number
;
1682 cache_entry
->save_stream_num
= stream_number
;
1683 cache_entry
->dirty
= FALSE
;
1688 static HRESULT
parse_pres_streams( DataCache
*cache
, IStorage
*stg
)
1692 PresentationDataHeader header
;
1694 CLIPFORMAT clipformat
;
1696 int stream_number
= 0;
1700 hr
= open_pres_stream( stg
, stream_number
, &stm
);
1701 if (FAILED(hr
)) break;
1703 hr
= read_clipformat( stm
, &clipformat
);
1705 if (hr
== S_OK
) hr
= IStream_Read( stm
, &header
, sizeof(header
), &actual_read
);
1707 if (hr
== S_OK
&& actual_read
== sizeof(header
))
1709 fmtetc
.cfFormat
= clipformat
;
1710 fmtetc
.ptd
= NULL
; /* FIXME */
1711 fmtetc
.dwAspect
= header
.dvAspect
;
1712 fmtetc
.lindex
= header
.lindex
;
1713 fmtetc
.tymed
= tymed_from_cf( clipformat
);
1715 add_cache_entry( cache
, &fmtetc
, header
.advf
, stream_number
);
1717 IStream_Release( stm
);
1719 } while (hr
== S_OK
);
1724 static HRESULT
parse_contents_stream( DataCache
*cache
, IStorage
*stg
)
1728 DataCacheEntry
*cache_entry
;
1730 hr
= open_pres_stream( stg
, STREAM_NUMBER_CONTENTS
, &stm
);
1731 if (FAILED( hr
)) return hr
;
1733 hr
= get_static_entry( cache
, &cache_entry
);
1736 cache_entry
->load_stream_num
= STREAM_NUMBER_CONTENTS
;
1737 cache_entry
->save_stream_num
= STREAM_NUMBER_CONTENTS
;
1738 cache_entry
->dirty
= FALSE
;
1741 IStream_Release( stm
);
1745 /************************************************************************
1746 * DataCache_Load (IPersistStorage)
1748 * The data cache implementation of IPersistStorage_Load doesn't
1749 * actually load anything. Instead, it holds on to the storage pointer
1750 * and it will load the presentation information when the
1751 * IDataObject_GetData or IViewObject2_Draw methods are called.
1753 static HRESULT WINAPI
DataCache_Load( IPersistStorage
*iface
, IStorage
*stg
)
1755 DataCache
*This
= impl_from_IPersistStorage(iface
);
1758 DataCacheEntry
*entry
, *cursor2
;
1760 TRACE("(%p, %p)\n", iface
, stg
);
1762 IPersistStorage_HandsOffStorage( iface
);
1764 LIST_FOR_EACH_ENTRY_SAFE( entry
, cursor2
, &This
->cache_list
, DataCacheEntry
, entry
)
1765 DataCacheEntry_Destroy( This
, entry
);
1767 ReadClassStg( stg
, &clsid
);
1768 hr
= create_automatic_entry( This
, &clsid
);
1769 if (FAILED( hr
)) return hr
;
1771 This
->clsid
= clsid
;
1773 if (This
->clsid_static
)
1775 hr
= parse_contents_stream( This
, stg
);
1776 if (FAILED(hr
)) hr
= parse_pres_streams( This
, stg
);
1779 hr
= parse_pres_streams( This
, stg
);
1781 if (SUCCEEDED( hr
))
1783 This
->dirty
= FALSE
;
1784 This
->presentationStorage
= stg
;
1785 IStorage_AddRef( This
->presentationStorage
);
1791 /************************************************************************
1792 * DataCache_Save (IPersistStorage)
1794 * Until we actually connect to a running object and retrieve new
1795 * information to it, we never have to save anything. However, it is
1796 * our responsibility to copy the information when saving to a new
1799 static HRESULT WINAPI
DataCache_Save(IPersistStorage
* iface
, IStorage
*stg
, BOOL same_as_load
)
1801 DataCache
*This
= impl_from_IPersistStorage(iface
);
1802 DataCacheEntry
*cache_entry
;
1804 int stream_number
= 0;
1806 TRACE("(%p, %p, %d)\n", iface
, stg
, same_as_load
);
1808 /* assign stream numbers to the cache entries */
1809 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1811 if (cache_entry
->save_stream_num
!= stream_number
)
1813 cache_entry
->dirty
= TRUE
; /* needs to be written out again */
1814 cache_entry
->save_stream_num
= stream_number
;
1819 /* write out the cache entries */
1820 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1822 if (!same_as_load
|| cache_entry
->dirty
)
1824 hr
= DataCacheEntry_Save(cache_entry
, stg
, same_as_load
);
1828 if (same_as_load
) cache_entry
->dirty
= FALSE
;
1832 if (same_as_load
) This
->dirty
= FALSE
;
1836 /************************************************************************
1837 * DataCache_SaveCompleted (IPersistStorage)
1839 * This method is called to tell the cache to release the storage
1840 * pointer it's currently holding.
1842 static HRESULT WINAPI
DataCache_SaveCompleted(
1843 IPersistStorage
* iface
,
1846 TRACE("(%p, %p)\n", iface
, pStgNew
);
1850 IPersistStorage_HandsOffStorage(iface
);
1852 DataCache_Load(iface
, pStgNew
);
1858 /************************************************************************
1859 * DataCache_HandsOffStorage (IPersistStorage)
1861 * This method is called to tell the cache to release the storage
1862 * pointer it's currently holding.
1864 static HRESULT WINAPI
DataCache_HandsOffStorage(
1865 IPersistStorage
* iface
)
1867 DataCache
*this = impl_from_IPersistStorage(iface
);
1869 TRACE("(%p)\n", iface
);
1871 if (this->presentationStorage
!= NULL
)
1873 IStorage_Release(this->presentationStorage
);
1874 this->presentationStorage
= NULL
;
1880 /*********************************************************
1881 * Method implementation for the IViewObject2
1882 * part of the DataCache class.
1885 /************************************************************************
1886 * DataCache_IViewObject2_QueryInterface (IUnknown)
1888 static HRESULT WINAPI
DataCache_IViewObject2_QueryInterface(
1889 IViewObject2
* iface
,
1893 DataCache
*this = impl_from_IViewObject2(iface
);
1895 return IUnknown_QueryInterface(this->outer_unk
, riid
, ppvObject
);
1898 /************************************************************************
1899 * DataCache_IViewObject2_AddRef (IUnknown)
1901 static ULONG WINAPI
DataCache_IViewObject2_AddRef(
1902 IViewObject2
* iface
)
1904 DataCache
*this = impl_from_IViewObject2(iface
);
1906 return IUnknown_AddRef(this->outer_unk
);
1909 /************************************************************************
1910 * DataCache_IViewObject2_Release (IUnknown)
1912 static ULONG WINAPI
DataCache_IViewObject2_Release(
1913 IViewObject2
* iface
)
1915 DataCache
*this = impl_from_IViewObject2(iface
);
1917 return IUnknown_Release(this->outer_unk
);
1920 /************************************************************************
1921 * DataCache_Draw (IViewObject2)
1923 * This method will draw the cached representation of the object
1924 * to the given device context.
1926 static HRESULT WINAPI
DataCache_Draw(
1927 IViewObject2
* iface
,
1931 DVTARGETDEVICE
* ptd
,
1934 LPCRECTL lprcBounds
,
1935 LPCRECTL lprcWBounds
,
1936 BOOL (CALLBACK
*pfnContinue
)(ULONG_PTR dwContinue
),
1937 ULONG_PTR dwContinue
)
1939 DataCache
*This
= impl_from_IViewObject2(iface
);
1941 DataCacheEntry
*cache_entry
;
1943 TRACE("(%p, %x, %d, %p, %p, %p, %p, %p, %p, %lx)\n",
1955 if (lprcBounds
==NULL
)
1956 return E_INVALIDARG
;
1958 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
1960 /* FIXME: compare ptd too */
1961 if ((cache_entry
->fmtetc
.dwAspect
!= dwDrawAspect
) ||
1962 (cache_entry
->fmtetc
.lindex
!= lindex
))
1965 /* if the data hasn't been loaded yet, do it now */
1966 if ((cache_entry
->stgmedium
.tymed
== TYMED_NULL
) && (cache_entry
->load_stream_num
!= STREAM_NUMBER_NOT_SET
))
1968 hres
= DataCacheEntry_LoadData(cache_entry
, This
->presentationStorage
);
1974 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
1977 if (pfnContinue
&& !pfnContinue(dwContinue
)) return E_ABORT
;
1979 switch (cache_entry
->fmtetc
.cfFormat
)
1981 case CF_METAFILEPICT
:
1984 * We have to be careful not to modify the state of the
1989 SIZE oldViewportExt
;
1990 POINT oldViewportOrg
;
1991 METAFILEPICT
*mfpict
;
1993 if ((cache_entry
->stgmedium
.tymed
!= TYMED_MFPICT
) ||
1994 !((mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
))))
1997 prevMapMode
= SetMapMode(hdcDraw
, mfpict
->mm
);
1999 SetWindowExtEx(hdcDraw
,
2004 SetViewportExtEx(hdcDraw
,
2005 lprcBounds
->right
- lprcBounds
->left
,
2006 lprcBounds
->bottom
- lprcBounds
->top
,
2009 SetViewportOrgEx(hdcDraw
,
2014 PlayMetaFile(hdcDraw
, mfpict
->hMF
);
2016 SetWindowExtEx(hdcDraw
,
2021 SetViewportExtEx(hdcDraw
,
2026 SetViewportOrgEx(hdcDraw
,
2031 SetMapMode(hdcDraw
, prevMapMode
);
2033 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
2042 if ((cache_entry
->stgmedium
.tymed
!= TYMED_HGLOBAL
) ||
2043 !((info
= GlobalLock( cache_entry
->stgmedium
.u
.hGlobal
))))
2046 bits
= (BYTE
*) info
+ bitmap_info_size( info
, DIB_RGB_COLORS
);
2047 StretchDIBits( hdcDraw
, lprcBounds
->left
, lprcBounds
->top
,
2048 lprcBounds
->right
- lprcBounds
->left
, lprcBounds
->bottom
- lprcBounds
->top
,
2049 0, 0, info
->bmiHeader
.biWidth
, info
->bmiHeader
.biHeight
,
2050 bits
, info
, DIB_RGB_COLORS
, SRCCOPY
);
2052 GlobalUnlock( cache_entry
->stgmedium
.u
.hGlobal
);
2058 WARN("no data could be found to be drawn\n");
2063 static HRESULT WINAPI
DataCache_GetColorSet(
2064 IViewObject2
* iface
,
2068 DVTARGETDEVICE
* ptd
,
2069 HDC hicTargetDevice
,
2070 LOGPALETTE
** ppColorSet
)
2076 static HRESULT WINAPI
DataCache_Freeze(
2077 IViewObject2
* iface
,
2087 static HRESULT WINAPI
DataCache_Unfreeze(
2088 IViewObject2
* iface
,
2095 /************************************************************************
2096 * DataCache_SetAdvise (IViewObject2)
2098 * This sets-up an advisory sink with the data cache. When the object's
2099 * view changes, this sink is called.
2101 static HRESULT WINAPI
DataCache_SetAdvise(
2102 IViewObject2
* iface
,
2105 IAdviseSink
* pAdvSink
)
2107 DataCache
*this = impl_from_IViewObject2(iface
);
2109 TRACE("(%p, %x, %x, %p)\n", iface
, aspects
, advf
, pAdvSink
);
2112 * A call to this function removes the previous sink
2114 if (this->sinkInterface
!= NULL
)
2116 IAdviseSink_Release(this->sinkInterface
);
2117 this->sinkInterface
= NULL
;
2118 this->sinkAspects
= 0;
2119 this->sinkAdviseFlag
= 0;
2123 * Now, setup the new one.
2127 this->sinkInterface
= pAdvSink
;
2128 this->sinkAspects
= aspects
;
2129 this->sinkAdviseFlag
= advf
;
2131 IAdviseSink_AddRef(this->sinkInterface
);
2135 * When the ADVF_PRIMEFIRST flag is set, we have to advise the
2138 if (advf
& ADVF_PRIMEFIRST
)
2140 DataCache_FireOnViewChange(this, aspects
, -1);
2146 /************************************************************************
2147 * DataCache_GetAdvise (IViewObject2)
2149 * This method queries the current state of the advise sink
2150 * installed on the data cache.
2152 static HRESULT WINAPI
DataCache_GetAdvise(
2153 IViewObject2
* iface
,
2156 IAdviseSink
** ppAdvSink
)
2158 DataCache
*this = impl_from_IViewObject2(iface
);
2160 TRACE("(%p, %p, %p, %p)\n", iface
, pAspects
, pAdvf
, ppAdvSink
);
2163 * Just copy all the requested values.
2166 *pAspects
= this->sinkAspects
;
2169 *pAdvf
= this->sinkAdviseFlag
;
2171 if (ppAdvSink
!=NULL
)
2173 if (this->sinkInterface
!= NULL
)
2174 IAdviseSink_QueryInterface(this->sinkInterface
,
2177 else *ppAdvSink
= NULL
;
2183 /************************************************************************
2184 * DataCache_GetExtent (IViewObject2)
2186 * This method retrieves the "natural" size of this cached object.
2188 static HRESULT WINAPI
DataCache_GetExtent(
2189 IViewObject2
* iface
,
2192 DVTARGETDEVICE
* ptd
,
2195 DataCache
*This
= impl_from_IViewObject2(iface
);
2196 HRESULT hres
= E_FAIL
;
2197 DataCacheEntry
*cache_entry
;
2199 TRACE("(%p, %x, %d, %p, %p)\n",
2200 iface
, dwDrawAspect
, lindex
, ptd
, lpsizel
);
2209 FIXME("Unimplemented flag lindex = %d\n", lindex
);
2212 * Right now, we support only the callback from
2213 * the default handler.
2216 FIXME("Unimplemented ptd = %p\n", ptd
);
2218 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2220 /* FIXME: compare ptd too */
2221 if ((cache_entry
->fmtetc
.dwAspect
!= dwDrawAspect
) ||
2222 (cache_entry
->fmtetc
.lindex
!= lindex
))
2225 /* if the data hasn't been loaded yet, do it now */
2226 if ((cache_entry
->stgmedium
.tymed
== TYMED_NULL
) && (cache_entry
->load_stream_num
!= STREAM_NUMBER_NOT_SET
))
2228 hres
= DataCacheEntry_LoadData(cache_entry
, This
->presentationStorage
);
2234 if (cache_entry
->stgmedium
.tymed
== TYMED_NULL
)
2238 switch (cache_entry
->fmtetc
.cfFormat
)
2240 case CF_METAFILEPICT
:
2242 METAFILEPICT
*mfpict
;
2244 if ((cache_entry
->stgmedium
.tymed
!= TYMED_MFPICT
) ||
2245 !((mfpict
= GlobalLock(cache_entry
->stgmedium
.u
.hMetaFilePict
))))
2248 lpsizel
->cx
= mfpict
->xExt
;
2249 lpsizel
->cy
= mfpict
->yExt
;
2251 GlobalUnlock(cache_entry
->stgmedium
.u
.hMetaFilePict
);
2257 BITMAPINFOHEADER
*info
;
2258 LONG x_pels_m
, y_pels_m
;
2261 if ((cache_entry
->stgmedium
.tymed
!= TYMED_HGLOBAL
) ||
2262 !((info
= GlobalLock( cache_entry
->stgmedium
.u
.hGlobal
))))
2265 x_pels_m
= info
->biXPelsPerMeter
;
2266 y_pels_m
= info
->biYPelsPerMeter
;
2268 /* Size in units of 0.01mm (ie. MM_HIMETRIC) */
2269 if (x_pels_m
!= 0 && y_pels_m
!= 0)
2271 lpsizel
->cx
= info
->biWidth
* 100000 / x_pels_m
;
2272 lpsizel
->cy
= info
->biHeight
* 100000 / y_pels_m
;
2276 HDC hdc
= GetDC( 0 );
2277 lpsizel
->cx
= info
->biWidth
* 2540 / GetDeviceCaps( hdc
, LOGPIXELSX
);
2278 lpsizel
->cy
= info
->biHeight
* 2540 / GetDeviceCaps( hdc
, LOGPIXELSY
);
2280 ReleaseDC( 0, hdc
);
2283 GlobalUnlock( cache_entry
->stgmedium
.u
.hGlobal
);
2290 WARN("no data could be found to get the extents from\n");
2293 * This method returns OLE_E_BLANK when it fails.
2299 /*********************************************************
2300 * Method implementation for the IOleCache2
2301 * part of the DataCache class.
2304 /************************************************************************
2305 * DataCache_IOleCache2_QueryInterface (IUnknown)
2307 static HRESULT WINAPI
DataCache_IOleCache2_QueryInterface(
2312 DataCache
*this = impl_from_IOleCache2(iface
);
2314 return IUnknown_QueryInterface(this->outer_unk
, riid
, ppvObject
);
2317 /************************************************************************
2318 * DataCache_IOleCache2_AddRef (IUnknown)
2320 static ULONG WINAPI
DataCache_IOleCache2_AddRef(
2323 DataCache
*this = impl_from_IOleCache2(iface
);
2325 return IUnknown_AddRef(this->outer_unk
);
2328 /************************************************************************
2329 * DataCache_IOleCache2_Release (IUnknown)
2331 static ULONG WINAPI
DataCache_IOleCache2_Release(
2334 DataCache
*this = impl_from_IOleCache2(iface
);
2336 return IUnknown_Release(this->outer_unk
);
2339 /*****************************************************************************
2342 * Set up the sink connection to the running object.
2344 static HRESULT
setup_sink(DataCache
*This
, DataCacheEntry
*cache_entry
)
2346 HRESULT hr
= S_FALSE
;
2349 /* Clear the ADVFCACHE_* bits. Native also sets the two highest bits for some reason. */
2350 flags
= cache_entry
->advise_flags
& ~(ADVFCACHE_NOHANDLER
| ADVFCACHE_FORCEBUILTIN
| ADVFCACHE_ONSAVE
);
2352 if(This
->running_object
)
2353 if(!(flags
& ADVF_NODATA
))
2354 hr
= IDataObject_DAdvise(This
->running_object
, &cache_entry
->fmtetc
, flags
,
2355 &This
->IAdviseSink_iface
, &cache_entry
->sink_id
);
2359 static HRESULT WINAPI
DataCache_Cache(
2361 FORMATETC
* pformatetc
,
2363 DWORD
* pdwConnection
)
2365 DataCache
*This
= impl_from_IOleCache2(iface
);
2366 DataCacheEntry
*cache_entry
;
2370 TRACE("(%p, 0x%x, %p)\n", pformatetc
, advf
, pdwConnection
);
2372 if (!pformatetc
|| !pdwConnection
)
2373 return E_INVALIDARG
;
2375 TRACE("pformatetc = %s\n", debugstr_formatetc(pformatetc
));
2377 fmt_cpy
= *pformatetc
; /* No need for a deep copy */
2378 if (fmt_cpy
.cfFormat
== CF_BITMAP
&& fmt_cpy
.tymed
== TYMED_GDI
)
2380 fmt_cpy
.cfFormat
= CF_DIB
;
2381 fmt_cpy
.tymed
= TYMED_HGLOBAL
;
2384 /* View caching DVASPECT_ICON gets converted to CF_METAFILEPICT */
2385 if (fmt_cpy
.dwAspect
== DVASPECT_ICON
&& fmt_cpy
.cfFormat
== 0)
2387 fmt_cpy
.cfFormat
= CF_METAFILEPICT
;
2388 fmt_cpy
.tymed
= TYMED_MFPICT
;
2393 cache_entry
= DataCache_GetEntryForFormatEtc(This
, &fmt_cpy
);
2396 TRACE("found an existing cache entry\n");
2397 *pdwConnection
= cache_entry
->id
;
2398 return CACHE_S_SAMECACHE
;
2401 if (This
->clsid_static
&& fmt_cpy
.dwAspect
!= DVASPECT_ICON
) return DV_E_FORMATETC
;
2403 hr
= DataCache_CreateEntry(This
, &fmt_cpy
, advf
, FALSE
, &cache_entry
);
2407 *pdwConnection
= cache_entry
->id
;
2408 setup_sink(This
, cache_entry
);
2414 static HRESULT WINAPI
DataCache_Uncache(
2418 DataCache
*This
= impl_from_IOleCache2(iface
);
2419 DataCacheEntry
*cache_entry
;
2421 TRACE("(%d)\n", dwConnection
);
2423 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2424 if (cache_entry
->id
== dwConnection
)
2426 DataCacheEntry_Destroy(This
, cache_entry
);
2430 WARN("no connection found for %d\n", dwConnection
);
2432 return OLE_E_NOCONNECTION
;
2435 static HRESULT WINAPI
DataCache_EnumCache(IOleCache2
*iface
,
2436 IEnumSTATDATA
**enum_stat
)
2438 DataCache
*This
= impl_from_IOleCache2( iface
);
2439 DataCacheEntry
*cache_entry
;
2440 int i
= 0, count
= 0;
2444 TRACE( "(%p, %p)\n", This
, enum_stat
);
2446 LIST_FOR_EACH_ENTRY( cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2449 if (cache_entry
->fmtetc
.cfFormat
== CF_DIB
)
2453 data
= CoTaskMemAlloc( count
* sizeof(*data
) );
2454 if (!data
) return E_OUTOFMEMORY
;
2456 LIST_FOR_EACH_ENTRY( cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2458 if (i
== count
) goto fail
;
2459 hr
= copy_formatetc( &data
[i
].formatetc
, &cache_entry
->fmtetc
);
2460 if (FAILED(hr
)) goto fail
;
2461 data
[i
].advf
= cache_entry
->advise_flags
;
2462 data
[i
].pAdvSink
= NULL
;
2463 data
[i
].dwConnection
= cache_entry
->id
;
2466 if (cache_entry
->fmtetc
.cfFormat
== CF_DIB
)
2468 if (i
== count
) goto fail
;
2469 hr
= copy_formatetc( &data
[i
].formatetc
, &cache_entry
->fmtetc
);
2470 if (FAILED(hr
)) goto fail
;
2471 data
[i
].formatetc
.cfFormat
= CF_BITMAP
;
2472 data
[i
].formatetc
.tymed
= TYMED_GDI
;
2473 data
[i
].advf
= cache_entry
->advise_flags
;
2474 data
[i
].pAdvSink
= NULL
;
2475 data
[i
].dwConnection
= cache_entry
->id
;
2480 hr
= EnumSTATDATA_Construct( NULL
, 0, i
, data
, FALSE
, enum_stat
);
2481 if (SUCCEEDED(hr
)) return hr
;
2484 while (i
--) CoTaskMemFree( data
[i
].formatetc
.ptd
);
2485 CoTaskMemFree( data
);
2489 static HRESULT WINAPI
DataCache_InitCache( IOleCache2
*iface
, IDataObject
*data
)
2491 TRACE( "(%p %p)\n", iface
, data
);
2492 return IOleCache2_UpdateCache( iface
, data
, UPDFCACHE_ALLBUTNODATACACHE
, NULL
);
2495 static HRESULT WINAPI
DataCache_IOleCache2_SetData(
2497 FORMATETC
* pformatetc
,
2501 DataCache
*This
= impl_from_IOleCache2(iface
);
2502 DataCacheEntry
*cache_entry
;
2505 TRACE("(%p, %p, %s)\n", pformatetc
, pmedium
, fRelease
? "TRUE" : "FALSE");
2506 TRACE("formatetc = %s\n", debugstr_formatetc(pformatetc
));
2508 cache_entry
= DataCache_GetEntryForFormatEtc(This
, pformatetc
);
2511 hr
= DataCacheEntry_SetData(cache_entry
, pformatetc
, pmedium
, fRelease
);
2514 DataCache_FireOnViewChange(This
, cache_entry
->fmtetc
.dwAspect
,
2515 cache_entry
->fmtetc
.lindex
);
2519 WARN("cache entry not found\n");
2524 static BOOL
entry_updatable( DataCacheEntry
*entry
, DWORD mode
)
2526 BOOL is_blank
= entry
->stgmedium
.tymed
== TYMED_NULL
;
2528 if ((mode
& UPDFCACHE_ONLYIFBLANK
) && !is_blank
) return FALSE
;
2530 if ((mode
& UPDFCACHE_NODATACACHE
) && (entry
->advise_flags
& ADVF_NODATA
)) return TRUE
;
2531 if ((mode
& UPDFCACHE_ONSAVECACHE
) && (entry
->advise_flags
& ADVFCACHE_ONSAVE
)) return TRUE
;
2532 if ((mode
& UPDFCACHE_ONSTOPCACHE
) && (entry
->advise_flags
& ADVF_DATAONSTOP
)) return TRUE
;
2533 if ((mode
& UPDFCACHE_NORMALCACHE
) && (entry
->advise_flags
== 0)) return TRUE
;
2534 if ((mode
& UPDFCACHE_IFBLANK
) && (is_blank
&& !(entry
->advise_flags
& ADVF_NODATA
))) return TRUE
;
2539 static HRESULT WINAPI
DataCache_UpdateCache( IOleCache2
*iface
, IDataObject
*data
,
2540 DWORD mode
, void *reserved
)
2542 DataCache
*This
= impl_from_IOleCache2(iface
);
2543 DataCacheEntry
*cache_entry
;
2546 CLIPFORMAT view_list
[] = { CF_METAFILEPICT
, CF_ENHMETAFILE
, CF_DIB
, CF_BITMAP
};
2549 BOOL done_one
= FALSE
;
2551 TRACE( "(%p %p %08x %p)\n", iface
, data
, mode
, reserved
);
2553 LIST_FOR_EACH_ENTRY( cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2557 if (!entry_updatable( cache_entry
, mode
))
2563 fmt
= cache_entry
->fmtetc
;
2567 hr
= IDataObject_GetData( data
, &fmt
, &med
);
2568 if (hr
!= S_OK
&& fmt
.cfFormat
== CF_DIB
)
2570 fmt
.cfFormat
= CF_BITMAP
;
2571 fmt
.tymed
= TYMED_GDI
;
2572 hr
= IDataObject_GetData( data
, &fmt
, &med
);
2574 if (hr
!= S_OK
&& fmt
.cfFormat
== CF_ENHMETAFILE
)
2576 fmt
.cfFormat
= CF_METAFILEPICT
;
2577 fmt
.tymed
= TYMED_MFPICT
;
2578 hr
= IDataObject_GetData( data
, &fmt
, &med
);
2582 hr
= DataCacheEntry_SetData( cache_entry
, &fmt
, &med
, TRUE
);
2583 if (hr
!= S_OK
) ReleaseStgMedium( &med
);
2584 else done_one
= TRUE
;
2589 for (i
= 0; i
< sizeof(view_list
) / sizeof(view_list
[0]); i
++)
2591 fmt
.cfFormat
= view_list
[i
];
2592 fmt
.tymed
= tymed_from_cf( fmt
.cfFormat
);
2593 hr
= IDataObject_QueryGetData( data
, &fmt
);
2596 hr
= IDataObject_GetData( data
, &fmt
, &med
);
2599 if (fmt
.cfFormat
== CF_BITMAP
)
2601 cache_entry
->fmtetc
.cfFormat
= CF_DIB
;
2602 cache_entry
->fmtetc
.tymed
= TYMED_HGLOBAL
;
2606 cache_entry
->fmtetc
.cfFormat
= fmt
.cfFormat
;
2607 cache_entry
->fmtetc
.tymed
= fmt
.tymed
;
2609 hr
= DataCacheEntry_SetData( cache_entry
, &fmt
, &med
, TRUE
);
2610 if (hr
!= S_OK
) ReleaseStgMedium( &med
);
2611 else done_one
= TRUE
;
2619 return (!slots
|| done_one
) ? S_OK
: CACHE_E_NOCACHE_UPDATED
;
2622 static HRESULT WINAPI
DataCache_DiscardCache(
2624 DWORD dwDiscardOptions
)
2626 DataCache
*This
= impl_from_IOleCache2(iface
);
2627 DataCacheEntry
*cache_entry
;
2630 TRACE("(%d)\n", dwDiscardOptions
);
2632 if (dwDiscardOptions
== DISCARDCACHE_SAVEIFDIRTY
)
2633 hr
= DataCache_Save(&This
->IPersistStorage_iface
, This
->presentationStorage
, TRUE
);
2635 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2637 hr
= DataCacheEntry_DiscardData(cache_entry
);
2646 /*********************************************************
2647 * Method implementation for the IOleCacheControl
2648 * part of the DataCache class.
2651 /************************************************************************
2652 * DataCache_IOleCacheControl_QueryInterface (IUnknown)
2654 static HRESULT WINAPI
DataCache_IOleCacheControl_QueryInterface(
2655 IOleCacheControl
* iface
,
2659 DataCache
*this = impl_from_IOleCacheControl(iface
);
2661 return IUnknown_QueryInterface(this->outer_unk
, riid
, ppvObject
);
2664 /************************************************************************
2665 * DataCache_IOleCacheControl_AddRef (IUnknown)
2667 static ULONG WINAPI
DataCache_IOleCacheControl_AddRef(
2668 IOleCacheControl
* iface
)
2670 DataCache
*this = impl_from_IOleCacheControl(iface
);
2672 return IUnknown_AddRef(this->outer_unk
);
2675 /************************************************************************
2676 * DataCache_IOleCacheControl_Release (IUnknown)
2678 static ULONG WINAPI
DataCache_IOleCacheControl_Release(
2679 IOleCacheControl
* iface
)
2681 DataCache
*this = impl_from_IOleCacheControl(iface
);
2683 return IUnknown_Release(this->outer_unk
);
2686 /************************************************************************
2687 * DataCache_OnRun (IOleCacheControl)
2689 static HRESULT WINAPI
DataCache_OnRun(IOleCacheControl
* iface
, IDataObject
*data_obj
)
2691 DataCache
*This
= impl_from_IOleCacheControl(iface
);
2692 DataCacheEntry
*cache_entry
;
2694 TRACE("(%p)->(%p)\n", iface
, data_obj
);
2696 if(This
->running_object
) return S_OK
;
2698 /* No reference is taken on the data object */
2699 This
->running_object
= data_obj
;
2701 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2703 setup_sink(This
, cache_entry
);
2709 /************************************************************************
2710 * DataCache_OnStop (IOleCacheControl)
2712 static HRESULT WINAPI
DataCache_OnStop(IOleCacheControl
* iface
)
2714 DataCache
*This
= impl_from_IOleCacheControl(iface
);
2715 DataCacheEntry
*cache_entry
;
2717 TRACE("(%p)\n", iface
);
2719 if(!This
->running_object
) return S_OK
;
2721 LIST_FOR_EACH_ENTRY(cache_entry
, &This
->cache_list
, DataCacheEntry
, entry
)
2723 if(cache_entry
->sink_id
)
2725 IDataObject_DUnadvise(This
->running_object
, cache_entry
->sink_id
);
2726 cache_entry
->sink_id
= 0;
2730 /* No ref taken in OnRun, so no Release call here */
2731 This
->running_object
= NULL
;
2735 /************************************************************************
2736 * IAdviseSink methods.
2737 * This behaves as an internal object to the data cache. QI'ing its ptr doesn't
2738 * give access to the cache's other interfaces. We don't maintain a ref count,
2739 * the object exists as long as the cache is around.
2741 static HRESULT WINAPI
DataCache_IAdviseSink_QueryInterface(IAdviseSink
*iface
, REFIID iid
, void **obj
)
2744 if (IsEqualIID(&IID_IUnknown
, iid
) ||
2745 IsEqualIID(&IID_IAdviseSink
, iid
))
2752 IAdviseSink_AddRef(iface
);
2755 return E_NOINTERFACE
;
2758 static ULONG WINAPI
DataCache_IAdviseSink_AddRef(IAdviseSink
*iface
)
2763 static ULONG WINAPI
DataCache_IAdviseSink_Release(IAdviseSink
*iface
)
2768 static void WINAPI
DataCache_OnDataChange(IAdviseSink
*iface
, FORMATETC
*fmt
, STGMEDIUM
*med
)
2770 DataCache
*This
= impl_from_IAdviseSink(iface
);
2771 TRACE("(%p)->(%s, %p)\n", This
, debugstr_formatetc(fmt
), med
);
2772 IOleCache2_SetData(&This
->IOleCache2_iface
, fmt
, med
, FALSE
);
2775 static void WINAPI
DataCache_OnViewChange(IAdviseSink
*iface
, DWORD aspect
, LONG index
)
2780 static void WINAPI
DataCache_OnRename(IAdviseSink
*iface
, IMoniker
*mk
)
2785 static void WINAPI
DataCache_OnSave(IAdviseSink
*iface
)
2790 static void WINAPI
DataCache_OnClose(IAdviseSink
*iface
)
2796 * Virtual function tables for the DataCache class.
2798 static const IUnknownVtbl DataCache_NDIUnknown_VTable
=
2800 DataCache_NDIUnknown_QueryInterface
,
2801 DataCache_NDIUnknown_AddRef
,
2802 DataCache_NDIUnknown_Release
2805 static const IDataObjectVtbl DataCache_IDataObject_VTable
=
2807 DataCache_IDataObject_QueryInterface
,
2808 DataCache_IDataObject_AddRef
,
2809 DataCache_IDataObject_Release
,
2811 DataCache_GetDataHere
,
2812 DataCache_QueryGetData
,
2813 DataCache_GetCanonicalFormatEtc
,
2814 DataCache_IDataObject_SetData
,
2815 DataCache_EnumFormatEtc
,
2817 DataCache_DUnadvise
,
2818 DataCache_EnumDAdvise
2821 static const IPersistStorageVtbl DataCache_IPersistStorage_VTable
=
2823 DataCache_IPersistStorage_QueryInterface
,
2824 DataCache_IPersistStorage_AddRef
,
2825 DataCache_IPersistStorage_Release
,
2826 DataCache_GetClassID
,
2831 DataCache_SaveCompleted
,
2832 DataCache_HandsOffStorage
2835 static const IViewObject2Vtbl DataCache_IViewObject2_VTable
=
2837 DataCache_IViewObject2_QueryInterface
,
2838 DataCache_IViewObject2_AddRef
,
2839 DataCache_IViewObject2_Release
,
2841 DataCache_GetColorSet
,
2844 DataCache_SetAdvise
,
2845 DataCache_GetAdvise
,
2849 static const IOleCache2Vtbl DataCache_IOleCache2_VTable
=
2851 DataCache_IOleCache2_QueryInterface
,
2852 DataCache_IOleCache2_AddRef
,
2853 DataCache_IOleCache2_Release
,
2856 DataCache_EnumCache
,
2857 DataCache_InitCache
,
2858 DataCache_IOleCache2_SetData
,
2859 DataCache_UpdateCache
,
2860 DataCache_DiscardCache
2863 static const IOleCacheControlVtbl DataCache_IOleCacheControl_VTable
=
2865 DataCache_IOleCacheControl_QueryInterface
,
2866 DataCache_IOleCacheControl_AddRef
,
2867 DataCache_IOleCacheControl_Release
,
2872 static const IAdviseSinkVtbl DataCache_IAdviseSink_VTable
=
2874 DataCache_IAdviseSink_QueryInterface
,
2875 DataCache_IAdviseSink_AddRef
,
2876 DataCache_IAdviseSink_Release
,
2877 DataCache_OnDataChange
,
2878 DataCache_OnViewChange
,
2884 /*********************************************************
2885 * Method implementation for DataCache class.
2887 static DataCache
* DataCache_Construct(
2889 LPUNKNOWN pUnkOuter
)
2891 DataCache
* newObject
= 0;
2894 * Allocate space for the object.
2896 newObject
= HeapAlloc(GetProcessHeap(), 0, sizeof(DataCache
));
2902 * Initialize the virtual function table.
2904 newObject
->IDataObject_iface
.lpVtbl
= &DataCache_IDataObject_VTable
;
2905 newObject
->IUnknown_inner
.lpVtbl
= &DataCache_NDIUnknown_VTable
;
2906 newObject
->IPersistStorage_iface
.lpVtbl
= &DataCache_IPersistStorage_VTable
;
2907 newObject
->IViewObject2_iface
.lpVtbl
= &DataCache_IViewObject2_VTable
;
2908 newObject
->IOleCache2_iface
.lpVtbl
= &DataCache_IOleCache2_VTable
;
2909 newObject
->IOleCacheControl_iface
.lpVtbl
= &DataCache_IOleCacheControl_VTable
;
2910 newObject
->IAdviseSink_iface
.lpVtbl
= &DataCache_IAdviseSink_VTable
;
2911 newObject
->outer_unk
= pUnkOuter
? pUnkOuter
: &newObject
->IUnknown_inner
;
2915 * Initialize the other members of the structure.
2917 newObject
->sinkAspects
= 0;
2918 newObject
->sinkAdviseFlag
= 0;
2919 newObject
->sinkInterface
= 0;
2920 newObject
->clsid
= CLSID_NULL
;
2921 newObject
->clsid_static
= FALSE
;
2922 newObject
->presentationStorage
= NULL
;
2923 list_init(&newObject
->cache_list
);
2924 newObject
->last_cache_id
= 2;
2925 newObject
->dirty
= FALSE
;
2926 newObject
->running_object
= NULL
;
2928 create_automatic_entry( newObject
, clsid
);
2929 newObject
->clsid
= *clsid
;
2934 /******************************************************************************
2935 * CreateDataCache [OLE32.@]
2937 * Creates a data cache to allow an object to render one or more of its views,
2938 * whether running or not.
2941 * pUnkOuter [I] Outer unknown for the object.
2943 * riid [I] IID of interface to return.
2944 * ppvObj [O] Address where the data cache object will be stored on return.
2948 * Failure: HRESULT code.
2951 * The following interfaces are supported by the returned data cache object:
2952 * IOleCache, IOleCache2, IOleCacheControl, IPersistStorage, IDataObject,
2953 * IViewObject and IViewObject2.
2955 HRESULT WINAPI
CreateDataCache(
2956 LPUNKNOWN pUnkOuter
,
2961 DataCache
* newCache
= NULL
;
2964 TRACE("(%s, %p, %s, %p)\n", debugstr_guid(rclsid
), pUnkOuter
, debugstr_guid(riid
), ppvObj
);
2975 * If this cache is constructed for aggregation, make sure
2976 * the caller is requesting the IUnknown interface.
2977 * This is necessary because it's the only time the non-delegating
2978 * IUnknown pointer can be returned to the outside.
2980 if ( pUnkOuter
&& !IsEqualIID(&IID_IUnknown
, riid
) )
2981 return E_INVALIDARG
;
2984 * Try to construct a new instance of the class.
2986 newCache
= DataCache_Construct(rclsid
,
2990 return E_OUTOFMEMORY
;
2992 hr
= IUnknown_QueryInterface(&newCache
->IUnknown_inner
, riid
, ppvObj
);
2993 IUnknown_Release(&newCache
->IUnknown_inner
);