merge the formfield patch from ooo-build
[ooovba.git] / canvas / source / directx / dx_winstuff.hxx
blobbb0cb2d74f914627e8a0e30cd5d3a53b4b1870f6
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: dx_winstuff.hxx,v $
10 * $Revision: 1.3 $
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 #ifndef _DXCANVAS_WINSTUFF_HXX
32 #define _DXCANVAS_WINSTUFF_HXX
34 #include <algorithm>
36 #include <boost/shared_ptr.hpp>
38 #include <basegfx/numeric/ftools.hxx>
40 #ifdef _WINDOWS_
41 #error someone else included <windows.h>
42 #endif
44 // Enabling Direct3D Debug Information Further more, with registry key
45 // \\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Direct3D\D3D9Debugging\\EnableCreationStack
46 // set to 1, sets a backtrace each time an object is created to the
47 // following global variable: LPCWSTR CreationCallStack
48 #if OSL_DEBUG_LEVEL > 0
49 # define D3D_DEBUG_INFO
50 #endif
52 #ifndef DIRECTX_VERSION
53 #error please define for which directx version we should compile
54 #endif
56 #if defined _MSC_VER
57 #pragma warning(push,1)
58 #endif
61 #define BOOL win32BOOL
62 #define INT32 win32INT32
63 #define UINT32 win32UINT32
64 #define GradientStyle_RECT win32GradientStyle_RECT
65 #define Polygon win32Polygon
66 #define PolyPolygon win32PolyPolygon
67 #undef WB_LEFT
68 #undef WB_RIGHT
70 #define WIN32_LEAN_AND_MEAN
71 #include <windows.h> // TODO(Q1): extract minimal set of required headers for gdiplus
73 #if DIRECTX_VERSION < 0x0900
75 #include <multimon.h>
77 // Be compatible with directdraw 3.0. Lets see how far this takes us
78 #define DIRECTDRAW_VERSION 0x0300
79 #include <ddraw.h>
81 // Be compatible with direct3d 5.0. Lets see how far this takes us
82 #define DIRECT3D_VERSION 0x0500
83 #define D3D_OVERLOADS
84 #include <d3d.h>
86 typedef IDirectDrawSurface surface_type;
88 #else
90 #include <dxsdkver.h>
91 #include <d3d9.h>
92 #include <d3dx9.h>
93 #if _DXSDK_BUILD_MAJOR < 1734 /* Earlier than the August 2009 DXSDK */
94 #include <dxerr9.h>
95 #else
96 #include <dxerr.h>
97 #endif
99 typedef IDirect3DSurface9 surface_type;
101 #endif
103 #undef DrawText
105 #ifdef __MINGW32__
106 using ::std::max;
107 using ::std::min;
108 #endif
110 #include <gdiplus.h>
112 #ifdef min
113 # undef min
114 #endif
115 #ifdef max
116 # undef max
117 #endif
119 namespace dxcanvas
121 // some shared pointer typedefs to Gdiplus objects
122 typedef ::boost::shared_ptr< Gdiplus::Graphics > GraphicsSharedPtr;
123 typedef ::boost::shared_ptr< Gdiplus::GraphicsPath > GraphicsPathSharedPtr;
124 typedef ::boost::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr;
125 typedef ::boost::shared_ptr< Gdiplus::CachedBitmap > CachedBitmapSharedPtr;
126 typedef ::boost::shared_ptr< Gdiplus::Font > FontSharedPtr;
127 typedef ::boost::shared_ptr< Gdiplus::Brush > BrushSharedPtr;
128 typedef ::boost::shared_ptr< Gdiplus::TextureBrush > TextureBrushSharedPtr;
130 /** COM object RAII wrapper
132 This template wraps a Windows COM object, transparently
133 handling lifetime issues the C++ way (i.e. releasing the
134 reference when the object is destroyed)
136 template< typename T > class COMReference
138 public:
139 typedef T Wrappee;
141 COMReference() :
142 mp( NULL )
146 /** Create from raw pointer
148 @attention This constructor assumes the interface is
149 already acquired (unless p is NULL), no additional AddRef
150 is called here.
152 This caters e.g. for all DirectX factory methods, which
153 return the created interfaces pre-acquired, into a raw
154 pointer. Simply pass the pointer to this class, but don't
155 call Release manually on it!
157 @example IDirectDrawSurface* pSurface;
158 pDD->CreateSurface(&aSurfaceDesc, &pSurface, NULL);
159 mpSurface = COMReference< IDirectDrawSurface >(pSurface);
162 explicit COMReference( T* p ) :
163 mp( p )
167 COMReference( const COMReference& rNew ) :
168 mp( NULL )
170 if( rNew.mp == NULL )
171 return;
173 rNew.mp->AddRef(); // do that _before_ assigning the
174 // pointer. Just in case...
175 mp = rNew.mp;
178 COMReference& operator=( const COMReference& rRHS )
180 COMReference aTmp(rRHS);
181 ::std::swap( mp, aTmp.mp );
183 return *this;
186 ~COMReference()
188 reset();
191 int reset()
193 int refcount = 0;
194 if( mp )
195 refcount = mp->Release();
197 mp = NULL;
198 return refcount;
201 bool is() const { return mp != NULL; }
202 T* get() const { return mp; }
203 T* operator->() const { return mp; }
204 T& operator*() const { return *mp; }
206 private:
207 T* mp;
210 // get_pointer() enables boost::mem_fn to recognize COMReference
211 template<class T> inline T * get_pointer(COMReference<T> const& p)
213 return p.get();
217 #if defined _MSC_VER
218 #pragma warning(pop)
219 #endif
221 #undef DELETE
222 #undef BOOL
223 #undef INT32
224 #undef UINT32
225 #undef PolyPolygon
227 #endif /* _DXCANVAS_WINSTUFF_HXX */