1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _DXCANVAS_WINSTUFF_HXX
25 #define _DXCANVAS_WINSTUFF_HXX
26 
27 #include <algorithm>
28 
29 #include <boost/shared_ptr.hpp>
30 
31 #include <basegfx/numeric/ftools.hxx>
32 
33 #ifdef _WINDOWS_
34 #error someone else included <windows.h>
35 #endif
36 
37 // Enabling Direct3D Debug Information Further more, with registry key
38 // \\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Direct3D\D3D9Debugging\\EnableCreationStack
39 // set to 1, sets a backtrace each time an object is created to the
40 // following global variable: LPCWSTR CreationCallStack
41 #if OSL_DEBUG_LEVEL > 0
42 # define D3D_DEBUG_INFO
43 #endif
44 
45 #ifndef DIRECTX_VERSION
46 #error please define for which directx version we should compile
47 #endif
48 
49 #if defined _MSC_VER
50 #pragma warning(push,1)
51 #endif
52 
53 
54 #define GradientStyle_RECT win32GradientStyle_RECT
55 #define Polygon win32Polygon
56 #define PolyPolygon win32PolyPolygon
57 #undef WB_LEFT
58 #undef WB_RIGHT
59 
60 #define WIN32_LEAN_AND_MEAN
61 #include <windows.h> // TODO(Q1): extract minimal set of required headers for gdiplus
62 
63 #if DIRECTX_VERSION < 0x0900
64 
65     #include <multimon.h>
66 
67 	// Be compatible with directdraw 3.0. Lets see how far this takes us
68 	#define DIRECTDRAW_VERSION 0x0300
69 	#include <ddraw.h>
70 
71 	// Be compatible with direct3d 5.0. Lets see how far this takes us
72 	#define DIRECT3D_VERSION 0x0500
73 	#define D3D_OVERLOADS
74 	#include <d3d.h>
75 
76 	typedef IDirectDrawSurface surface_type;
77 
78 #else
79 
80 	#include <d3d9.h>
81 	#include <d3dx9.h>
82 //	#include <dxerr9.h>		#i107614# removing include, it has been changed in the latest sdk fron August2009 from dxerr9.h into dxerr.h
83 
84 	typedef IDirect3DSurface9 surface_type;
85 
86 #endif
87 
88 #undef DrawText
89 
90 #ifdef __MINGW32__
91 using ::std::max;
92 using ::std::min;
93 #endif
94 
95 #include <gdiplus.h>
96 
97 #ifdef min
98 #   undef min
99 #endif
100 #ifdef max
101 #   undef max
102 #endif
103 
104 namespace dxcanvas
105 {
106     // some shared pointer typedefs to Gdiplus objects
107 	typedef ::boost::shared_ptr< Gdiplus::Graphics > 		GraphicsSharedPtr;
108     typedef ::boost::shared_ptr< Gdiplus::GraphicsPath > 	GraphicsPathSharedPtr;
109     typedef ::boost::shared_ptr< Gdiplus::Bitmap > 			BitmapSharedPtr;
110 	typedef ::boost::shared_ptr< Gdiplus::CachedBitmap > 	CachedBitmapSharedPtr;
111 	typedef ::boost::shared_ptr< Gdiplus::Font > 			FontSharedPtr;
112 	typedef ::boost::shared_ptr< Gdiplus::Brush > 			BrushSharedPtr;
113 	typedef ::boost::shared_ptr< Gdiplus::TextureBrush > 	TextureBrushSharedPtr;
114 
115     /** COM object RAII wrapper
116 
117 		This template wraps a Windows COM object, transparently
118 		handling lifetime issues the C++ way (i.e. releasing the
119 		reference when the object is destroyed)
120      */
121     template< typename T > class COMReference
122     {
123     public:
124         typedef T Wrappee;
125 
COMReference()126         COMReference() :
127             mp( NULL )
128         {
129         }
130 
131         /** Create from raw pointer
132 
133 			@attention This constructor assumes the interface is
134 			already acquired (unless p is NULL), no additional AddRef
135 			is called here.
136 
137             This caters e.g. for all DirectX factory methods, which
138             return the created interfaces pre-acquired, into a raw
139             pointer. Simply pass the pointer to this class, but don't
140             call Release manually on it!
141 
142             @example IDirectDrawSurface* pSurface;
143             pDD->CreateSurface(&aSurfaceDesc, &pSurface, NULL);
144             mpSurface = COMReference< IDirectDrawSurface >(pSurface);
145 
146          */
COMReference(T * p)147         explicit COMReference( T* p ) :
148             mp( p )
149         {
150         }
151 
COMReference(const COMReference & rNew)152         COMReference( const COMReference& rNew ) :
153             mp( NULL )
154         {
155             if( rNew.mp == NULL )
156                 return;
157 
158             rNew.mp->AddRef(); // do that _before_ assigning the
159                                // pointer. Just in case...
160             mp = rNew.mp;
161         }
162 
operator =(const COMReference & rRHS)163         COMReference& operator=( const COMReference& rRHS )
164         {
165             COMReference aTmp(rRHS);
166             ::std::swap( mp, aTmp.mp );
167 
168             return *this;
169         }
170 
~COMReference()171         ~COMReference()
172         {
173             reset();
174         }
175 
reset()176         int reset()
177         {
178 			int refcount = 0;
179             if( mp )
180                 refcount = mp->Release();
181 
182             mp = NULL;
183 			return refcount;
184         }
185 
is() const186         bool		is() const { return mp != NULL; }
get() const187         T*          get() const { return mp; }
operator ->() const188         T*          operator->() const { return mp; }
operator *() const189         T&          operator*() const { return *mp; }
190 
191     private:
192         T*	mp;
193     };
194 
195 	// get_pointer() enables boost::mem_fn to recognize COMReference
get_pointer(COMReference<T> const & p)196 	template<class T> inline T * get_pointer(COMReference<T> const& p)
197     {
198         return p.get();
199     }
200 }
201 
202 #if defined _MSC_VER
203 #pragma warning(pop)
204 #endif
205 
206 #undef DELETE
207 #undef PolyPolygon
208 
209 #endif /* _DXCANVAS_WINSTUFF_HXX */
210