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