Update ooo320-m1
[ooovba.git] / dtrans / source / win32 / workbench / XTDo.cxx
blobc7ca46d790d11a9e6502ca52b0ca0dd0973b2886
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XTDo.cxx,v $
10 * $Revision: 1.6 $
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 //------------------------------------------------------------------------
35 // includes
36 //------------------------------------------------------------------------
37 #include <osl/diagnose.h>
39 #include "..\DTransHelper.hxx"
41 #ifndef _TWRAPPERDATAOBJECT_HXX_
42 #include "XTDo.hxx"
43 #endif
45 #if defined _MSC_VER
46 #pragma warning(push,1)
47 #endif
48 #include <windows.h>
49 #include <ole2.h>
50 #if defined _MSC_VER
51 #pragma warning(pop)
52 #endif
53 #include <memory>
54 #include <tchar.h>
56 //------------------------------------------------------------------------
57 // namespace directives
58 //------------------------------------------------------------------------
60 using namespace ::std;
62 //============================================================================
63 // OTWrapperDataObject
64 //============================================================================
66 //------------------------------------------------------------------------
67 // ctor
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( ) :
81 m_nRefCnt( 0 )
86 //------------------------------------------------------------------------
87 // IUnknown->QueryInterface
88 //------------------------------------------------------------------------
90 STDMETHODIMP CXTDataObject::QueryInterface( REFIID iid, LPVOID* ppvObject )
92 OSL_ASSERT( NULL != ppvObject );
94 if ( NULL == ppvObject )
95 return E_INVALIDARG;
97 HRESULT hr = E_NOINTERFACE;
99 *ppvObject = NULL;
101 if ( ( __uuidof( IUnknown ) == iid ) || ( __uuidof( IDataObject ) == iid ) )
103 *ppvObject = static_cast< IUnknown* >( this );
104 ( (LPUNKNOWN)*ppvObject )->AddRef( );
105 hr = S_OK;
108 return hr;
111 //------------------------------------------------------------------------
112 // IUnknown->AddRef
113 //------------------------------------------------------------------------
115 STDMETHODIMP_(ULONG) CXTDataObject::AddRef( )
117 return static_cast< ULONG >( InterlockedIncrement( &m_nRefCnt ) );
120 //------------------------------------------------------------------------
121 // IUnknown->Release
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 ) );
131 if ( 0 == nRefCnt )
133 delete this;
136 return nRefCnt;
139 /*------------------------------------------------------------------------
141 IDataObject->GetData
142 we deliver data only into global memory
144 algo:
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 ) )
157 return E_INVALIDARG;
159 HRESULT hr = E_FAIL;
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;
172 hr = S_OK;
175 return hr;
178 //------------------------------------------------------------------------
179 // IDataObject->EnumFormatEtc
180 //------------------------------------------------------------------------
182 STDMETHODIMP CXTDataObject::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC** ppenumFormatetc )
184 if ( ( NULL == ppenumFormatetc ) || ( DATADIR_SET == dwDirection ) )
185 return E_INVALIDARG;
187 *ppenumFormatetc = NULL;
189 HRESULT hr = E_FAIL;
191 if ( DATADIR_GET == dwDirection )
193 *ppenumFormatetc = new CEnumFormatEtc( this );
194 static_cast< LPUNKNOWN >( *ppenumFormatetc )->AddRef( );
195 hr = S_OK;
198 return hr;
201 //------------------------------------------------------------------------
202 // IDataObject->QueryGetData
203 //------------------------------------------------------------------------
205 STDMETHODIMP CXTDataObject::QueryGetData( LPFORMATETC pFormatetc )
207 return E_NOTIMPL;
210 //------------------------------------------------------------------------
211 // IDataObject->GetDataHere
212 //------------------------------------------------------------------------
214 STDMETHODIMP CXTDataObject::GetDataHere( LPFORMATETC, LPSTGMEDIUM )
216 return E_NOTIMPL;
219 //------------------------------------------------------------------------
220 // IDataObject->GetCanonicalFormatEtc
221 //------------------------------------------------------------------------
223 STDMETHODIMP CXTDataObject::GetCanonicalFormatEtc( LPFORMATETC, LPFORMATETC )
225 return E_NOTIMPL;
228 //------------------------------------------------------------------------
229 // IDataObject->SetData
230 //------------------------------------------------------------------------
232 STDMETHODIMP CXTDataObject::SetData( LPFORMATETC, LPSTGMEDIUM, BOOL )
234 return E_NOTIMPL;
237 //------------------------------------------------------------------------
238 // IDataObject->DAdvise
239 //------------------------------------------------------------------------
241 STDMETHODIMP CXTDataObject::DAdvise( LPFORMATETC, DWORD, LPADVISESINK, DWORD * )
243 return E_NOTIMPL;
246 //------------------------------------------------------------------------
247 // IDataObject->DUnadvise
248 //------------------------------------------------------------------------
250 STDMETHODIMP CXTDataObject::DUnadvise( DWORD )
252 return E_NOTIMPL;
255 //------------------------------------------------------------------------
256 // IDataObject->EnumDAdvise
257 //------------------------------------------------------------------------
259 STDMETHODIMP CXTDataObject::EnumDAdvise( LPENUMSTATDATA * )
261 return E_NOTIMPL;
264 //------------------------------------------------------------------------
265 // for our convenience
266 //------------------------------------------------------------------------
268 CXTDataObject::operator IDataObject*( )
270 return static_cast< IDataObject* >( this );
274 //============================================================================
275 // CEnumFormatEtc
276 //============================================================================
278 //----------------------------------------------------------------------------
279 // ctor
280 //----------------------------------------------------------------------------
282 CEnumFormatEtc::CEnumFormatEtc( LPUNKNOWN pUnkDataObj ) :
283 m_nRefCnt( 0 ),
284 m_pUnkDataObj( pUnkDataObj ),
285 m_nCurrPos( 0 )
289 //----------------------------------------------------------------------------
290 // IUnknown->QueryInterface
291 //----------------------------------------------------------------------------
293 STDMETHODIMP CEnumFormatEtc::QueryInterface( REFIID iid, LPVOID* ppvObject )
295 if ( NULL == ppvObject )
296 return E_INVALIDARG;
298 HRESULT hr = E_NOINTERFACE;
300 *ppvObject = NULL;
302 if ( ( __uuidof( IUnknown ) == iid ) || ( __uuidof( IEnumFORMATETC ) == iid ) )
304 *ppvObject = static_cast< IUnknown* >( this );
305 static_cast< LPUNKNOWN >( *ppvObject )->AddRef( );
306 hr = S_OK;
309 return hr;
312 //----------------------------------------------------------------------------
313 // IUnknown->AddRef
314 //----------------------------------------------------------------------------
316 STDMETHODIMP_(ULONG) CEnumFormatEtc::AddRef( )
318 // keep the dataobject alive
319 m_pUnkDataObj->AddRef( );
320 return InterlockedIncrement( &m_nRefCnt );
323 //----------------------------------------------------------------------------
324 // IUnknown->Release
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 );
336 if ( 0 == nRefCnt )
337 delete this;
339 return nRefCnt;
342 //----------------------------------------------------------------------------
343 // IEnumFORMATETC->Next
344 //----------------------------------------------------------------------------
346 STDMETHODIMP CEnumFormatEtc::Next( ULONG celt, LPFORMATETC rgelt, ULONG* pceltFetched )
348 if ( ( 0 != celt ) && ( NULL == rgelt ) )
349 return E_INVALIDARG;
351 ULONG ulFetched = 0;
352 ULONG ulToFetch = celt;
353 HRESULT hr = S_FALSE;
355 while( m_nCurrPos < 1 )
357 rgelt->cfFormat = CF_TEXT;
358 rgelt->ptd = NULL;
359 rgelt->dwAspect = DVASPECT_CONTENT;
360 rgelt->lindex = -1;
361 rgelt->tymed = TYMED_HGLOBAL;
363 ++m_nCurrPos;
364 ++rgelt;
365 --ulToFetch;
366 ++ulFetched;
369 if ( ulFetched == celt )
370 hr = S_OK;
372 if ( NULL != pceltFetched )
374 *pceltFetched = ulFetched;
377 return hr;
380 //----------------------------------------------------------------------------
381 // IEnumFORMATETC->Skip
382 //----------------------------------------------------------------------------
384 STDMETHODIMP CEnumFormatEtc::Skip( ULONG celt )
386 HRESULT hr = S_FALSE;
389 if ( ( m_nCurrPos + celt ) < m_nClipFormats )
391 m_nCurrPos += celt;
392 hr = S_OK;
396 return hr;
399 //----------------------------------------------------------------------------
400 // IEnumFORMATETC->Reset
401 //----------------------------------------------------------------------------
403 STDMETHODIMP CEnumFormatEtc::Reset( )
405 m_nCurrPos = 0;
406 return S_OK;
409 //----------------------------------------------------------------------------
410 // IEnumFORMATETC->Clone
411 //----------------------------------------------------------------------------
413 STDMETHODIMP CEnumFormatEtc::Clone( IEnumFORMATETC** ppenum )
415 OSL_ASSERT( NULL != ppenum );
417 if ( NULL == ppenum )
418 return E_INVALIDARG;
420 HRESULT hr = E_FAIL;
422 *ppenum = NULL;
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( );
430 hr = NOERROR;
433 return hr;