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