tdf#163962 Enable spell checking in editable sections in read-only mode
[LibreOffice.git] / vcl / win / dtrans / DTransHelper.cxx
blob176f9e205bccced9e16ba2da9eb0f2d2dae076dd
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 <rtl/ustring.h>
21 #include <osl/diagnose.h>
22 #include "DTransHelper.hxx"
23 #include <assert.h>
25 // implementation
27 CStgTransferHelper::CStgTransferHelper( bool bAutoInit,
28 HGLOBAL hGlob,
29 bool bDelStgOnRelease ) :
30 m_lpStream( nullptr ),
31 m_bDelStgOnRelease( bDelStgOnRelease )
33 if ( bAutoInit )
34 init( hGlob, m_bDelStgOnRelease );
37 // dtor
39 CStgTransferHelper::~CStgTransferHelper( )
41 if ( m_lpStream )
42 m_lpStream->Release( );
45 // TransferData into the
47 void CStgTransferHelper::write( const void* lpData, ULONG cb, ULONG* cbWritten )
49 HRESULT hr = E_FAIL;
51 if ( m_lpStream )
52 hr = m_lpStream->Write( lpData, cb, cbWritten );
54 if ( FAILED( hr ) )
55 throw CStgTransferException( hr );
57 #if OSL_DEBUG_LEVEL > 0
58 HGLOBAL hGlob;
59 hr = GetHGlobalFromStream( m_lpStream, &hGlob );
60 OSL_ASSERT( SUCCEEDED( hr ) );
62 /*DWORD dwSize =*/ GlobalSize( hGlob );
63 /*LPVOID lpdbgData =*/ GlobalLock( hGlob );
64 GlobalUnlock( hGlob );
65 #endif
68 // read
70 void CStgTransferHelper::read( LPVOID pv, ULONG cb, ULONG* pcbRead )
72 HRESULT hr = E_FAIL;
74 if ( m_lpStream )
75 hr = m_lpStream->Read( pv, cb , pcbRead );
77 if ( FAILED( hr ) )
78 throw CStgTransferException( hr );
81 // GetHGlobal
83 HGLOBAL CStgTransferHelper::getHGlobal( ) const
85 OSL_ASSERT( m_lpStream );
87 HGLOBAL hGlob = nullptr;
89 if ( m_lpStream )
91 HRESULT hr = GetHGlobalFromStream( m_lpStream, &hGlob );
92 if ( FAILED( hr ) )
93 hGlob = nullptr;
96 return hGlob;
99 // getIStream
101 void CStgTransferHelper::getIStream( LPSTREAM* ppStream )
103 assert(ppStream);
104 *ppStream = m_lpStream;
105 if ( *ppStream )
106 static_cast< LPUNKNOWN >( *ppStream )->AddRef( );
109 // Init
111 void CStgTransferHelper::init( SIZE_T newSize,
112 sal_uInt32 uiFlags,
113 bool bDelStgOnRelease )
115 cleanup( );
117 m_bDelStgOnRelease = bDelStgOnRelease;
119 HGLOBAL hGlob = GlobalAlloc( uiFlags, newSize );
120 if ( nullptr == hGlob )
121 throw CStgTransferException( STG_E_MEDIUMFULL );
123 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
124 if ( FAILED( hr ) )
126 GlobalFree( hGlob );
127 m_lpStream = nullptr;
128 throw CStgTransferException( hr );
131 #if OSL_DEBUG_LEVEL > 0
132 STATSTG statstg;
133 hr = m_lpStream->Stat( &statstg, STATFLAG_DEFAULT );
134 OSL_ASSERT( SUCCEEDED( hr ) );
135 #endif
138 // Init
140 void CStgTransferHelper::init( HGLOBAL hGlob,
141 bool bDelStgOnRelease )
143 cleanup( );
145 m_bDelStgOnRelease = bDelStgOnRelease;
147 HRESULT hr = CreateStreamOnHGlobal( hGlob, m_bDelStgOnRelease, &m_lpStream );
148 if ( FAILED( hr ) )
149 throw CStgTransferException( hr );
152 // free the global memory and invalidate the stream pointer
154 void CStgTransferHelper::cleanup( )
156 if ( m_lpStream && !m_bDelStgOnRelease )
158 HGLOBAL hGlob;
159 GetHGlobalFromStream( m_lpStream, &hGlob );
160 GlobalFree( hGlob );
163 if ( m_lpStream )
165 m_lpStream->Release( );
166 m_lpStream = nullptr;
170 // return the size of memory we point to
172 sal_uInt32 CStgTransferHelper::memSize( CLIPFORMAT cf ) const
174 DWORD dwSize = 0;
176 if ( nullptr != m_lpStream )
178 HGLOBAL hGlob;
179 GetHGlobalFromStream( m_lpStream, &hGlob );
181 if ( CF_TEXT == cf || RegisterClipboardFormatW( L"HTML Format" ) == cf )
183 char* pText = static_cast< char* >( GlobalLock( hGlob ) );
184 if ( pText )
186 dwSize = strlen(pText) + 1; // strlen + trailing '\0'
187 GlobalUnlock( hGlob );
190 else if ( CF_UNICODETEXT == cf )
192 sal_Unicode* pText = static_cast< sal_Unicode* >( GlobalLock( hGlob ) );
193 if ( pText )
195 dwSize = rtl_ustr_getLength( pText ) * sizeof( sal_Unicode );
196 GlobalUnlock( hGlob );
199 else
200 dwSize = GlobalSize( hGlob );
203 return dwSize;
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */