1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XTDo.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_dtrans.hxx"
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
37 #include <osl/diagnose.h>
39 #include "..\DTransHelper.hxx"
41 #ifndef _TWRAPPERDATAOBJECT_HXX_
46 #pragma warning(push,1)
56 //------------------------------------------------------------------------
57 // namespace directives
58 //------------------------------------------------------------------------
60 using namespace ::std
;
62 //============================================================================
63 // OTWrapperDataObject
64 //============================================================================
66 //------------------------------------------------------------------------
68 //------------------------------------------------------------------------
70 in the constructor we enumerate all formats offered by the transferable
71 and convert the formats into formatetc structures
72 if the transferable supports text in different charsets we use either
73 the charset equal to the charset of the current thread or an arbitrary
74 charset supported by the transferable and the system
75 if the transferable supports only unicodetext we offer in addition to
76 this text in the charset of the current thread
77 in order to allow the consumer of the clipboard to query for the charset
78 of the text in the clipboard we offer a CF_LOCALE
80 CXTDataObject::CXTDataObject( ) :
86 //------------------------------------------------------------------------
87 // IUnknown->QueryInterface
88 //------------------------------------------------------------------------
90 STDMETHODIMP
CXTDataObject::QueryInterface( REFIID iid
, LPVOID
* ppvObject
)
92 OSL_ASSERT( NULL
!= ppvObject
);
94 if ( NULL
== ppvObject
)
97 HRESULT hr
= E_NOINTERFACE
;
101 if ( ( __uuidof( IUnknown
) == iid
) || ( __uuidof( IDataObject
) == iid
) )
103 *ppvObject
= static_cast< IUnknown
* >( this );
104 ( (LPUNKNOWN
)*ppvObject
)->AddRef( );
111 //------------------------------------------------------------------------
113 //------------------------------------------------------------------------
115 STDMETHODIMP_(ULONG
) CXTDataObject::AddRef( )
117 return static_cast< ULONG
>( InterlockedIncrement( &m_nRefCnt
) );
120 //------------------------------------------------------------------------
122 //------------------------------------------------------------------------
124 STDMETHODIMP_(ULONG
) CXTDataObject::Release( )
126 // we need a helper variable because it's
127 // not allowed to access a member variable
128 // after an object is destroyed
129 ULONG nRefCnt
= static_cast< ULONG
>( InterlockedDecrement( &m_nRefCnt
) );
139 /*------------------------------------------------------------------------
142 we deliver data only into global memory
145 1. convert the given formatect struct into a valid dataflavor
146 2. if the transferable directly supports the requested format
147 2.1. if text data requested add a trailing '\0' in order to prevent
148 problems (windows needs '\0' terminated strings
149 2.2. we expect unicode data as Sequence< sal_Unicode > and all other
150 text and raw data as Sequence< sal_Int8 >
152 ------------------------------------------------------------------------*/
154 STDMETHODIMP
CXTDataObject::GetData( LPFORMATETC pFormatetc
, LPSTGMEDIUM pmedium
)
156 if ( ( NULL
== pFormatetc
) || ( NULL
== pmedium
) )
160 char pBuff
[] = "Test OleClipboard";
162 if ( CF_TEXT
== pFormatetc
->cfFormat
)
164 CHGlobalHelper
hGlobHlp( TRUE
);
166 hGlobHlp
.Write( pBuff
, sizeof( pBuff
), NULL
);
168 pmedium
->tymed
= TYMED_HGLOBAL
;
169 pmedium
->hGlobal
= hGlobHlp
.GetHGlobal( );
170 pmedium
->pUnkForRelease
= NULL
;
178 //------------------------------------------------------------------------
179 // IDataObject->EnumFormatEtc
180 //------------------------------------------------------------------------
182 STDMETHODIMP
CXTDataObject::EnumFormatEtc( DWORD dwDirection
, IEnumFORMATETC
** ppenumFormatetc
)
184 if ( ( NULL
== ppenumFormatetc
) || ( DATADIR_SET
== dwDirection
) )
187 *ppenumFormatetc
= NULL
;
191 if ( DATADIR_GET
== dwDirection
)
193 *ppenumFormatetc
= new CEnumFormatEtc( this );
194 static_cast< LPUNKNOWN
>( *ppenumFormatetc
)->AddRef( );
201 //------------------------------------------------------------------------
202 // IDataObject->QueryGetData
203 //------------------------------------------------------------------------
205 STDMETHODIMP
CXTDataObject::QueryGetData( LPFORMATETC pFormatetc
)
210 //------------------------------------------------------------------------
211 // IDataObject->GetDataHere
212 //------------------------------------------------------------------------
214 STDMETHODIMP
CXTDataObject::GetDataHere( LPFORMATETC
, LPSTGMEDIUM
)
219 //------------------------------------------------------------------------
220 // IDataObject->GetCanonicalFormatEtc
221 //------------------------------------------------------------------------
223 STDMETHODIMP
CXTDataObject::GetCanonicalFormatEtc( LPFORMATETC
, LPFORMATETC
)
228 //------------------------------------------------------------------------
229 // IDataObject->SetData
230 //------------------------------------------------------------------------
232 STDMETHODIMP
CXTDataObject::SetData( LPFORMATETC
, LPSTGMEDIUM
, BOOL
)
237 //------------------------------------------------------------------------
238 // IDataObject->DAdvise
239 //------------------------------------------------------------------------
241 STDMETHODIMP
CXTDataObject::DAdvise( LPFORMATETC
, DWORD
, LPADVISESINK
, DWORD
* )
246 //------------------------------------------------------------------------
247 // IDataObject->DUnadvise
248 //------------------------------------------------------------------------
250 STDMETHODIMP
CXTDataObject::DUnadvise( DWORD
)
255 //------------------------------------------------------------------------
256 // IDataObject->EnumDAdvise
257 //------------------------------------------------------------------------
259 STDMETHODIMP
CXTDataObject::EnumDAdvise( LPENUMSTATDATA
* )
264 //------------------------------------------------------------------------
265 // for our convenience
266 //------------------------------------------------------------------------
268 CXTDataObject::operator IDataObject
*( )
270 return static_cast< IDataObject
* >( this );
274 //============================================================================
276 //============================================================================
278 //----------------------------------------------------------------------------
280 //----------------------------------------------------------------------------
282 CEnumFormatEtc::CEnumFormatEtc( LPUNKNOWN pUnkDataObj
) :
284 m_pUnkDataObj( pUnkDataObj
),
289 //----------------------------------------------------------------------------
290 // IUnknown->QueryInterface
291 //----------------------------------------------------------------------------
293 STDMETHODIMP
CEnumFormatEtc::QueryInterface( REFIID iid
, LPVOID
* ppvObject
)
295 if ( NULL
== ppvObject
)
298 HRESULT hr
= E_NOINTERFACE
;
302 if ( ( __uuidof( IUnknown
) == iid
) || ( __uuidof( IEnumFORMATETC
) == iid
) )
304 *ppvObject
= static_cast< IUnknown
* >( this );
305 static_cast< LPUNKNOWN
>( *ppvObject
)->AddRef( );
312 //----------------------------------------------------------------------------
314 //----------------------------------------------------------------------------
316 STDMETHODIMP_(ULONG
) CEnumFormatEtc::AddRef( )
318 // keep the dataobject alive
319 m_pUnkDataObj
->AddRef( );
320 return InterlockedIncrement( &m_nRefCnt
);
323 //----------------------------------------------------------------------------
325 //----------------------------------------------------------------------------
327 STDMETHODIMP_(ULONG
) CEnumFormatEtc::Release( )
329 // release the outer dataobject
330 m_pUnkDataObj
->Release( );
332 // we need a helper variable because it's
333 // not allowed to access a member variable
334 // after an object is destroyed
335 ULONG nRefCnt
= InterlockedDecrement( &m_nRefCnt
);
342 //----------------------------------------------------------------------------
343 // IEnumFORMATETC->Next
344 //----------------------------------------------------------------------------
346 STDMETHODIMP
CEnumFormatEtc::Next( ULONG celt
, LPFORMATETC rgelt
, ULONG
* pceltFetched
)
348 if ( ( 0 != celt
) && ( NULL
== rgelt
) )
352 ULONG ulToFetch
= celt
;
353 HRESULT hr
= S_FALSE
;
355 while( m_nCurrPos
< 1 )
357 rgelt
->cfFormat
= CF_TEXT
;
359 rgelt
->dwAspect
= DVASPECT_CONTENT
;
361 rgelt
->tymed
= TYMED_HGLOBAL
;
369 if ( ulFetched
== celt
)
372 if ( NULL
!= pceltFetched
)
374 *pceltFetched
= ulFetched
;
380 //----------------------------------------------------------------------------
381 // IEnumFORMATETC->Skip
382 //----------------------------------------------------------------------------
384 STDMETHODIMP
CEnumFormatEtc::Skip( ULONG celt
)
386 HRESULT hr
= S_FALSE
;
389 if ( ( m_nCurrPos + celt ) < m_nClipFormats )
399 //----------------------------------------------------------------------------
400 // IEnumFORMATETC->Reset
401 //----------------------------------------------------------------------------
403 STDMETHODIMP
CEnumFormatEtc::Reset( )
409 //----------------------------------------------------------------------------
410 // IEnumFORMATETC->Clone
411 //----------------------------------------------------------------------------
413 STDMETHODIMP
CEnumFormatEtc::Clone( IEnumFORMATETC
** ppenum
)
415 OSL_ASSERT( NULL
!= ppenum
);
417 if ( NULL
== ppenum
)
424 CEnumFormatEtc
* pCEnumFEtc
= new CEnumFormatEtc( m_pUnkDataObj
);
425 if ( NULL
!= pCEnumFEtc
)
427 pCEnumFEtc
->m_nCurrPos
= m_nCurrPos
;
428 *ppenum
= static_cast< IEnumFORMATETC
* >( pCEnumFEtc
);
429 static_cast< LPUNKNOWN
>( *ppenum
)->AddRef( );