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