Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / directx / dx_winstuff.hxx
blob7cf2ba870d51c49edf924152c4050143bdab3fc0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef INCLUDED_CANVAS_SOURCE_DIRECTX_DX_WINSTUFF_HXX
21 #define INCLUDED_CANVAS_SOURCE_DIRECTX_DX_WINSTUFF_HXX
23 #include <algorithm>
25 #include <boost/shared_ptr.hpp>
27 #include <basegfx/numeric/ftools.hxx>
29 #ifdef _WINDOWS_
30 #error someone else included <windows.h>
31 #endif
33 // Enabling Direct3D Debug Information Further more, with registry key
34 // \\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Direct3D\D3D9Debugging\\EnableCreationStack
35 // set to 1, sets a backtrace each time an object is created to the
36 // following global variable: LPCWSTR CreationCallStack
37 #if OSL_DEBUG_LEVEL > 0
38 # define D3D_DEBUG_INFO
39 #endif
41 #if defined _MSC_VER
42 #pragma warning(push,1)
43 #endif
46 #define ULONG win32ULONG
47 #define GradientStyle_RECT win32GradientStyle_RECT
48 #define Polygon win32Polygon
49 #undef WB_LEFT
50 #undef WB_RIGHT
52 #define WIN32_LEAN_AND_MEAN
53 #include <windows.h>
55 #include <d3d9.h>
57 typedef IDirect3DSurface9 surface_type;
60 #undef DrawText
62 #ifdef __MINGW32__
63 using ::std::max;
64 using ::std::min;
65 #endif
67 #include <gdiplus.h>
69 #ifdef min
70 # undef min
71 #endif
72 #ifdef max
73 # undef max
74 #endif
76 namespace dxcanvas
78 // some shared pointer typedefs to Gdiplus objects
79 typedef ::boost::shared_ptr< Gdiplus::Graphics > GraphicsSharedPtr;
80 typedef ::boost::shared_ptr< Gdiplus::GraphicsPath > GraphicsPathSharedPtr;
81 typedef ::boost::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr;
82 typedef ::boost::shared_ptr< Gdiplus::CachedBitmap > CachedBitmapSharedPtr;
83 typedef ::boost::shared_ptr< Gdiplus::Font > FontSharedPtr;
84 typedef ::boost::shared_ptr< Gdiplus::Brush > BrushSharedPtr;
85 typedef ::boost::shared_ptr< Gdiplus::TextureBrush > TextureBrushSharedPtr;
87 /** COM object RAII wrapper
89 This template wraps a Windows COM object, transparently
90 handling lifetime issues the C++ way (i.e. releasing the
91 reference when the object is destroyed)
93 template< typename T > class COMReference
95 public:
96 typedef T Wrappee;
98 COMReference() :
99 mp( NULL )
103 /** Create from raw pointer
105 @attention This constructor assumes the interface is
106 already acquired (unless p is NULL), no additional AddRef
107 is called here.
109 This caters e.g. for all DirectX factory methods, which
110 return the created interfaces pre-acquired, into a raw
111 pointer. Simply pass the pointer to this class, but don't
112 call Release manually on it!
114 @example IDirectDrawSurface* pSurface;
115 pDD->CreateSurface(&aSurfaceDesc, &pSurface, NULL);
116 mpSurface = COMReference< IDirectDrawSurface >(pSurface);
119 explicit COMReference( T* p ) :
120 mp( p )
124 COMReference( const COMReference& rNew ) :
125 mp( NULL )
127 if( rNew.mp == NULL )
128 return;
130 rNew.mp->AddRef(); // do that _before_ assigning the
131 // pointer. Just in case...
132 mp = rNew.mp;
135 COMReference& operator=( const COMReference& rRHS )
137 COMReference aTmp(rRHS);
138 ::std::swap( mp, aTmp.mp );
140 return *this;
143 ~COMReference()
145 reset();
148 int reset()
150 int refcount = 0;
151 if( mp )
152 refcount = mp->Release();
154 mp = NULL;
155 return refcount;
158 bool is() const { return mp != NULL; }
159 T* get() const { return mp; }
160 T* operator->() const { return mp; }
161 T& operator*() const { return *mp; }
163 private:
164 T* mp;
167 // get_pointer() enables boost::mem_fn to recognize COMReference
168 template<class T> inline T * get_pointer(COMReference<T> const& p)
170 return p.get();
174 #if defined _MSC_VER
175 #pragma warning(pop)
176 #endif
178 #undef DELETE
179 #undef GradientStyle_RECT
180 #undef Polygon
182 #endif // INCLUDED_CANVAS_SOURCE_DIRECTX_DX_WINSTUFF_HXX
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */