1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 #ifndef _DTRANSHELPER_HXX_
30 #define _DTRANSHELPER_HXX_
31 
32 //------------------------------------------------------------------------
33 // includes
34 //------------------------------------------------------------------------
35 
36 #if defined _MSC_VER
37 #pragma warning(push,1)
38 #endif
39 #include <windows.h>
40 #if defined _MSC_VER
41 #pragma warning(pop)
42 #endif
43 #include "..\misc\WinClip.hxx"
44 
45 //------------------------------------------------------------------------
46 // defines
47 //------------------------------------------------------------------------
48 
49 #define AUTO_INIT                 TRUE
50 #define NO_AUTO_INIT              FALSE
51 #define MEM_DESTROY_ON_RELEASE    TRUE
52 #define NO_MEM_DESTROY_ON_RELEASE FALSE
53 
54 //------------------------------------------------------------------------
55 // deklarations
56 //------------------------------------------------------------------------
57 
58 
59 //-------------------------------------------------------------------------
60 // a helper class to manage a global memory area, the clients can write
61 // into the global memory area and extract the handle to the global mem
62 // note: not thread-safe
63 //-------------------------------------------------------------------------
64 
65 class CStgTransferHelper
66 {
67 public:
68 	// will be thrown in case of failures
69 	class CStgTransferException
70 	{
71 	public:
72 		HRESULT m_hr;
73 		CStgTransferException( HRESULT hr ) : m_hr( hr ) {};
74 	};
75 
76 public:
77 	CStgTransferHelper(
78 		sal_Bool bAutoInit = sal_False,
79 		HGLOBAL  hGlob = NULL,
80 		sal_Bool bDelStgOnRelease = sal_False );
81 
82 	~CStgTransferHelper( );
83 
84 	void SAL_CALL write( const void* lpData, ULONG cb, ULONG* cbWritten = NULL );
85 	void SAL_CALL read( LPVOID pv, ULONG cb, ULONG* pcbRead = NULL );
86 
87 	HGLOBAL SAL_CALL getHGlobal( ) const;
88 	void    SAL_CALL getIStream( LPSTREAM* ppStream );
89 
90 	void SAL_CALL init(
91 		SIZE_T newSize,
92 		sal_uInt32 uiFlags = GHND,
93 		sal_Bool bDelStgOnRelease = sal_False );
94 
95 	void SAL_CALL init(
96 		HGLOBAL hGlob,
97 		sal_Bool bDelStgOnRelease = sal_False );
98 
99 	// returns the size of the managed memory
100 	sal_uInt32 SAL_CALL memSize( CLIPFORMAT cf = CF_INVALID ) const;
101 
102 	// free the global memory and necessary
103 	// release the internal stream pointer
104 	void SAL_CALL cleanup( );
105 
106 private:
107 	LPSTREAM m_lpStream;
108 	sal_Bool m_bDelStgOnRelease;
109 
110 private:
111 	CStgTransferHelper( const CStgTransferHelper& );
112 	CStgTransferHelper& operator=( const CStgTransferHelper& );
113 };
114 
115 //-------------------------------------------------------------------------
116 // something like an auto-pointer - allows access to the memory belonging
117 // to a HGLOBAL and automatically unlocks a global memory at destruction
118 // time
119 //-------------------------------------------------------------------------
120 
121 class CRawHGlobalPtr
122 {
123 public:
124 
125 	//---------------------------------------------
126 	// ctor
127 	//---------------------------------------------
128 
129 	CRawHGlobalPtr( HGLOBAL hGlob ) :
130 		m_hGlob( hGlob ),
131 		m_bIsLocked( FALSE ),
132 		m_pGlobMem( NULL )
133 	{
134 	}
135 
136 
137 	//---------------------------------------------
138 	// ctor
139 	//---------------------------------------------
140 
141 	CRawHGlobalPtr( const CStgTransferHelper& theHGlobalHelper ) :
142 		m_hGlob( theHGlobalHelper.getHGlobal( ) ),
143 		m_bIsLocked( FALSE ),
144 		m_pGlobMem( NULL )
145 	{
146 	}
147 
148 	//---------------------------------------------
149 	// dtor
150 	//---------------------------------------------
151 
152 	~CRawHGlobalPtr( )
153 	{
154 		if ( m_bIsLocked )
155 			GlobalUnlock( m_hGlob );
156 	}
157 
158 	//---------------------------------------------
159 	// lock the global memory (initializes a
160 	// pointer to this memory)
161 	//---------------------------------------------
162 
163 	BOOL Lock( )
164 	{
165 		if ( !m_bIsLocked && ( NULL != m_hGlob ) )
166 		{
167 			m_pGlobMem = GlobalLock( m_hGlob );
168 			m_bIsLocked = ( NULL != m_pGlobMem );
169 		}
170 
171 		return m_bIsLocked;
172 	}
173 
174 	//---------------------------------------------
175 	// unlock the global memory (invalidates the
176 	// pointer to this memory)
177 	//---------------------------------------------
178 
179 	BOOL Unlock( )
180 	{
181 		GlobalUnlock( m_hGlob );
182 		m_bIsLocked = FALSE;
183 		m_pGlobMem = NULL;
184 
185 		return ( NO_ERROR == GetLastError( ) );
186 	}
187 
188 	//---------------------------------------------
189 	// locks the global memory and returns a
190 	// pointer to this memory
191 	//---------------------------------------------
192 
193 	LPVOID GetMemPtr( )
194 	{
195 		Lock( );
196 		return m_pGlobMem;
197 	}
198 
199 	//---------------------------------------------
200 	// size of mem we point to
201 	//---------------------------------------------
202 
203 	int MemSize( ) const
204 	{
205 		return GlobalSize( m_hGlob );
206 	}
207 
208 private:
209 	HGLOBAL m_hGlob;
210 	BOOL    m_bIsLocked;
211 	LPVOID  m_pGlobMem;
212 };
213 
214 #endif
215