update dev300-m58
[ooovba.git] / canvas / source / directx / dx_winstuff.hxx
blobdc9e47422e42a79f3d52577f62d0bbb010a546b1
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 // Needed for #?$&/@ gdiplus header
106 #ifndef max
107 #define max(a,b) (((a) > (b)) ? (a) : (b))
108 #define __WORKAROUND_MAX_DEFINED__
109 #endif
111 #ifndef min
112 #define min(a,b) (((a) < (b)) ? (a) : (b))
113 #define __WORKAROUND_MIN_DEFINED__
114 #endif
116 #include <gdiplus.h>
118 #ifdef __WORKAROUND_MAX_DEFINED__
119 #undef max
120 #endif
122 #ifdef __WORKAROUND_MIN_DEFINED__
123 #undef min
124 #endif
126 namespace dxcanvas
128 // some shared pointer typedefs to Gdiplus objects
129 typedef ::boost::shared_ptr< Gdiplus::Graphics > GraphicsSharedPtr;
130 typedef ::boost::shared_ptr< Gdiplus::GraphicsPath > GraphicsPathSharedPtr;
131 typedef ::boost::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr;
132 typedef ::boost::shared_ptr< Gdiplus::CachedBitmap > CachedBitmapSharedPtr;
133 typedef ::boost::shared_ptr< Gdiplus::Font > FontSharedPtr;
134 typedef ::boost::shared_ptr< Gdiplus::Brush > BrushSharedPtr;
135 typedef ::boost::shared_ptr< Gdiplus::TextureBrush > TextureBrushSharedPtr;
137 /** COM object RAII wrapper
139 This template wraps a Windows COM object, transparently
140 handling lifetime issues the C++ way (i.e. releasing the
141 reference when the object is destroyed)
143 template< typename T > class COMReference
145 public:
146 typedef T Wrappee;
148 COMReference() :
149 mp( NULL )
153 /** Create from raw pointer
155 @attention This constructor assumes the interface is
156 already acquired (unless p is NULL), no additional AddRef
157 is called here.
159 This caters e.g. for all DirectX factory methods, which
160 return the created interfaces pre-acquired, into a raw
161 pointer. Simply pass the pointer to this class, but don't
162 call Release manually on it!
164 @example IDirectDrawSurface* pSurface;
165 pDD->CreateSurface(&aSurfaceDesc, &pSurface, NULL);
166 mpSurface = COMReference< IDirectDrawSurface >(pSurface);
169 explicit COMReference( T* p ) :
170 mp( p )
174 COMReference( const COMReference& rNew ) :
175 mp( NULL )
177 if( rNew.mp == NULL )
178 return;
180 rNew.mp->AddRef(); // do that _before_ assigning the
181 // pointer. Just in case...
182 mp = rNew.mp;
185 COMReference& operator=( const COMReference& rRHS )
187 COMReference aTmp(rRHS);
188 ::std::swap( mp, aTmp.mp );
190 return *this;
193 ~COMReference()
195 reset();
198 int reset()
200 int refcount = 0;
201 if( mp )
202 refcount = mp->Release();
204 mp = NULL;
205 return refcount;
208 bool is() const { return mp != NULL; }
209 T* get() const { return mp; }
210 T* operator->() const { return mp; }
211 T& operator*() const { return *mp; }
213 private:
214 T* mp;
217 // get_pointer() enables boost::mem_fn to recognize COMReference
218 template<class T> inline T * get_pointer(COMReference<T> const& p)
220 return p.get();
224 #if defined _MSC_VER
225 #pragma warning(pop)
226 #endif
228 #undef DELETE
229 #undef BOOL
230 #undef INT32
231 #undef UINT32
232 #undef PolyPolygon
234 #endif /* _DXCANVAS_WINSTUFF_HXX */