Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / dtrans / source / win32 / workbench / XTDo.cxx
blobf8da707d92e38b62c84f767e57909149f8b1c05f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/diagnose.h>
22 #include "../DTransHelper.hxx"
24 #include "XTDo.hxx"
26 #if defined _MSC_VER
27 #pragma warning(push,1)
28 #endif
29 #include <windows.h>
30 #include <ole2.h>
31 #if defined _MSC_VER
32 #pragma warning(pop)
33 #endif
34 #include <memory>
36 using namespace ::std;
38 // OTWrapperDataObject
41 in the constructor we enumerate all formats offered by the transferable
42 and convert the formats into formatetc structures
43 if the transferable supports text in different charsets we use either
44 the charset equal to the charset of the current thread or an arbitrary
45 charset supported by the transferable and the system
46 if the transferable supports only unicodetext we offer in addition to
47 this text in the charset of the current thread
48 in order to allow the consumer of the clipboard to query for the charset
49 of the text in the clipboard we offer a CF_LOCALE
51 CXTDataObject::CXTDataObject( ) :
52 m_nRefCnt( 0 )
57 // IUnknown->QueryInterface
59 STDMETHODIMP CXTDataObject::QueryInterface( REFIID iid, LPVOID* ppvObject )
61 OSL_ASSERT( NULL != ppvObject );
63 if ( NULL == ppvObject )
64 return E_INVALIDARG;
66 HRESULT hr = E_NOINTERFACE;
68 *ppvObject = NULL;
70 if ( ( __uuidof( IUnknown ) == iid ) || ( __uuidof( IDataObject ) == iid ) )
72 *ppvObject = static_cast< IUnknown* >( this );
73 ( (LPUNKNOWN)*ppvObject )->AddRef( );
74 hr = S_OK;
77 return hr;
80 // IUnknown->AddRef
82 STDMETHODIMP_(ULONG) CXTDataObject::AddRef( )
84 return static_cast< ULONG >( InterlockedIncrement( &m_nRefCnt ) );
87 // IUnknown->Release
89 STDMETHODIMP_(ULONG) CXTDataObject::Release( )
91 // we need a helper variable because it's
92 // not allowed to access a member variable
93 // after an object is destroyed
94 ULONG nRefCnt = static_cast< ULONG >( InterlockedDecrement( &m_nRefCnt ) );
96 if ( 0 == nRefCnt )
98 delete this;
101 return nRefCnt;
104 /*------------------------------------------------------------------------
106 IDataObject->GetData
107 we deliver data only into global memory
109 algo:
110 1. convert the given formatect struct into a valid dataflavor
111 2. if the transferable directly supports the requested format
112 2.1. if text data requested add a trailing '\0' in order to prevent
113 problems (windows needs '\0' terminated strings
114 2.2. we expect unicode data as Sequence< sal_Unicode > and all other
115 text and raw data as Sequence< sal_Int8 >
117 ------------------------------------------------------------------------*/
119 STDMETHODIMP CXTDataObject::GetData( LPFORMATETC pFormatetc, LPSTGMEDIUM pmedium )
121 if ( ( NULL == pFormatetc ) || ( NULL == pmedium ) )
122 return E_INVALIDARG;
124 HRESULT hr = E_FAIL;
126 if ( CF_TEXT == pFormatetc->cfFormat )
128 CHGlobalHelper hGlobHlp( TRUE );
130 char pBuff[] = "Test OleClipboard";
131 hGlobHlp.Write( pBuff, sizeof( pBuff ), NULL );
133 pmedium->tymed = TYMED_HGLOBAL;
134 pmedium->hGlobal = hGlobHlp.GetHGlobal( );
135 pmedium->pUnkForRelease = NULL;
137 hr = S_OK;
140 return hr;
143 // IDataObject->EnumFormatEtc
145 STDMETHODIMP CXTDataObject::EnumFormatEtc( DWORD dwDirection, IEnumFORMATETC** ppenumFormatetc )
147 if ( ( NULL == ppenumFormatetc ) || ( DATADIR_SET == dwDirection ) )
148 return E_INVALIDARG;
150 *ppenumFormatetc = NULL;
152 HRESULT hr = E_FAIL;
154 if ( DATADIR_GET == dwDirection )
156 *ppenumFormatetc = new CEnumFormatEtc( this );
157 static_cast< LPUNKNOWN >( *ppenumFormatetc )->AddRef( );
158 hr = S_OK;
161 return hr;
164 // IDataObject->QueryGetData
166 STDMETHODIMP CXTDataObject::QueryGetData( LPFORMATETC pFormatetc )
168 return E_NOTIMPL;
171 // IDataObject->GetDataHere
173 STDMETHODIMP CXTDataObject::GetDataHere( LPFORMATETC, LPSTGMEDIUM )
175 return E_NOTIMPL;
178 // IDataObject->GetCanonicalFormatEtc
180 STDMETHODIMP CXTDataObject::GetCanonicalFormatEtc( LPFORMATETC, LPFORMATETC )
182 return E_NOTIMPL;
185 // IDataObject->SetData
187 STDMETHODIMP CXTDataObject::SetData( LPFORMATETC, LPSTGMEDIUM, BOOL )
189 return E_NOTIMPL;
192 // IDataObject->DAdvise
194 STDMETHODIMP CXTDataObject::DAdvise( LPFORMATETC, DWORD, LPADVISESINK, DWORD * )
196 return E_NOTIMPL;
199 // IDataObject->DUnadvise
201 STDMETHODIMP CXTDataObject::DUnadvise( DWORD )
203 return E_NOTIMPL;
206 // IDataObject->EnumDAdvise
208 STDMETHODIMP CXTDataObject::EnumDAdvise( LPENUMSTATDATA * )
210 return E_NOTIMPL;
213 CXTDataObject::operator IDataObject*( )
215 return static_cast< IDataObject* >( this );
218 CEnumFormatEtc::CEnumFormatEtc( LPUNKNOWN pUnkDataObj ) :
219 m_nRefCnt( 0 ),
220 m_pUnkDataObj( pUnkDataObj ),
221 m_nCurrPos( 0 )
225 // IUnknown->QueryInterface
227 STDMETHODIMP CEnumFormatEtc::QueryInterface( REFIID iid, LPVOID* ppvObject )
229 if ( NULL == ppvObject )
230 return E_INVALIDARG;
232 HRESULT hr = E_NOINTERFACE;
234 *ppvObject = NULL;
236 if ( ( __uuidof( IUnknown ) == iid ) || ( __uuidof( IEnumFORMATETC ) == iid ) )
238 *ppvObject = static_cast< IUnknown* >( this );
239 static_cast< LPUNKNOWN >( *ppvObject )->AddRef( );
240 hr = S_OK;
243 return hr;
246 // IUnknown->AddRef
248 STDMETHODIMP_(ULONG) CEnumFormatEtc::AddRef( )
250 // keep the dataobject alive
251 m_pUnkDataObj->AddRef( );
252 return InterlockedIncrement( &m_nRefCnt );
255 // IUnknown->Release
257 STDMETHODIMP_(ULONG) CEnumFormatEtc::Release( )
259 // release the outer dataobject
260 m_pUnkDataObj->Release( );
262 // we need a helper variable because it's
263 // not allowed to access a member variable
264 // after an object is destroyed
265 ULONG nRefCnt = InterlockedDecrement( &m_nRefCnt );
266 if ( 0 == nRefCnt )
267 delete this;
269 return nRefCnt;
272 // IEnumFORMATETC->Next
274 STDMETHODIMP CEnumFormatEtc::Next( ULONG celt, LPFORMATETC rgelt, ULONG* pceltFetched )
276 if ( ( 0 != celt ) && ( NULL == rgelt ) )
277 return E_INVALIDARG;
279 ULONG ulFetched = 0;
280 ULONG ulToFetch = celt;
281 HRESULT hr = S_FALSE;
283 while( m_nCurrPos < 1 )
285 rgelt->cfFormat = CF_TEXT;
286 rgelt->ptd = NULL;
287 rgelt->dwAspect = DVASPECT_CONTENT;
288 rgelt->lindex = -1;
289 rgelt->tymed = TYMED_HGLOBAL;
291 ++m_nCurrPos;
292 ++rgelt;
293 --ulToFetch;
294 ++ulFetched;
297 if ( ulFetched == celt )
298 hr = S_OK;
300 if ( NULL != pceltFetched )
302 *pceltFetched = ulFetched;
305 return hr;
308 // IEnumFORMATETC->Skip
310 STDMETHODIMP CEnumFormatEtc::Skip( ULONG celt )
312 HRESULT hr = S_FALSE;
315 if ( ( m_nCurrPos + celt ) < m_nClipFormats )
317 m_nCurrPos += celt;
318 hr = S_OK;
322 return hr;
325 // IEnumFORMATETC->Reset
327 STDMETHODIMP CEnumFormatEtc::Reset( )
329 m_nCurrPos = 0;
330 return S_OK;
333 // IEnumFORMATETC->Clone
335 STDMETHODIMP CEnumFormatEtc::Clone( IEnumFORMATETC** ppenum )
337 OSL_ASSERT( NULL != ppenum );
339 if ( NULL == ppenum )
340 return E_INVALIDARG;
342 HRESULT hr = E_FAIL;
344 *ppenum = NULL;
346 CEnumFormatEtc* pCEnumFEtc = new CEnumFormatEtc( m_pUnkDataObj );
347 if ( NULL != pCEnumFEtc )
349 pCEnumFEtc->m_nCurrPos = m_nCurrPos;
350 *ppenum = static_cast< IEnumFORMATETC* >( pCEnumFEtc );
351 static_cast< LPUNKNOWN >( *ppenum )->AddRef( );
352 hr = NOERROR;
355 return hr;
358 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */