1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_canvas.hxx"
30 
31 #include <ctype.h> // don't ask. msdev breaks otherwise...
32 #include <vcl/window.hxx>
33 #include <vcl/canvastools.hxx>
34 #include <canvas/debug.hxx>
35 #include <canvas/verbosetrace.hxx>
36 #include <canvas/canvastools.hxx>
37 #include <tools/diagnose_ex.h>
38 
39 #include <osl/mutex.hxx>
40 #include <cppuhelper/compbase1.hxx>
41 
42 #include <com/sun/star/lang/NoSupportException.hpp>
43 #include <toolkit/helper/vclunohelper.hxx>
44 #include <basegfx/tools/canvastools.hxx>
45 #include "dx_linepolypolygon.hxx"
46 #include "dx_spritecanvas.hxx"
47 #include "dx_canvasbitmap.hxx"
48 #include "dx_devicehelper.hxx"
49 
50 
51 #undef WB_LEFT
52 #undef WB_RIGHT
53 #include "dx_winstuff.hxx"
54 
55 
56 #include <vcl/sysdata.hxx>
57 
58 using namespace ::com::sun::star;
59 
60 namespace dxcanvas
61 {
62     DeviceHelper::DeviceHelper() :
63         mpDevice( NULL ),
64         mnHDC(0)
65     {
66     }
67 
68     void DeviceHelper::init( HDC                        hdc,
69                              rendering::XGraphicDevice& rDevice )
70     {
71         mnHDC    = hdc;
72         mpDevice = &rDevice;
73     }
74 
75     void DeviceHelper::disposing()
76     {
77         // release all references
78         mnHDC = 0;
79         mpDevice = NULL;
80     }
81 
82     geometry::RealSize2D DeviceHelper::getPhysicalResolution()
83     {
84         if( !mpDevice )
85             return ::canvas::tools::createInfiniteSize2D(); // we're disposed
86 
87 		HDC hDC = getHDC();
88 		ENSURE_OR_THROW( hDC,
89                           "DeviceHelper::getPhysicalResolution(): cannot retrieve HDC from window" );
90 
91 		const int nHorzRes( GetDeviceCaps( hDC,
92                                            LOGPIXELSX ) );
93 		const int nVertRes( GetDeviceCaps( hDC,
94                                            LOGPIXELSY ) );
95 
96         return geometry::RealSize2D( nHorzRes*25.4,
97                                      nVertRes*25.4 );
98     }
99 
100     geometry::RealSize2D DeviceHelper::getPhysicalSize()
101     {
102         if( !mpDevice )
103             return ::canvas::tools::createInfiniteSize2D(); // we're disposed
104 
105 		HDC hDC=getHDC();
106 		ENSURE_OR_THROW( hDC,
107                           "DeviceHelper::getPhysicalSize(): cannot retrieve HDC from window" );
108 
109         const int nHorzSize( GetDeviceCaps( hDC,
110                                             HORZSIZE ) );
111         const int nVertSize( GetDeviceCaps( hDC,
112                                             VERTSIZE ) );
113 
114         return geometry::RealSize2D( nHorzSize,
115                                      nVertSize );
116     }
117 
118     uno::Reference< rendering::XLinePolyPolygon2D > DeviceHelper::createCompatibleLinePolyPolygon(
119         const uno::Reference< rendering::XGraphicDevice >& 				/*rDevice*/,
120         const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >&	points )
121     {
122         if( !mpDevice )
123             return uno::Reference< rendering::XLinePolyPolygon2D >(); // we're disposed
124 
125         return uno::Reference< rendering::XLinePolyPolygon2D >(
126             new LinePolyPolygon(
127                 ::basegfx::unotools::polyPolygonFromPoint2DSequenceSequence( points ) ) );
128     }
129 
130     uno::Reference< rendering::XBezierPolyPolygon2D > DeviceHelper::createCompatibleBezierPolyPolygon(
131         const uno::Reference< rendering::XGraphicDevice >& 						/*rDevice*/,
132         const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >&	points )
133     {
134         if( !mpDevice )
135             return uno::Reference< rendering::XBezierPolyPolygon2D >(); // we're disposed
136 
137         return uno::Reference< rendering::XBezierPolyPolygon2D >(
138             new LinePolyPolygon(
139                 ::basegfx::unotools::polyPolygonFromBezier2DSequenceSequence( points ) ) );
140     }
141 
142     uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleBitmap(
143         const uno::Reference< rendering::XGraphicDevice >& 	/*rDevice*/,
144         const geometry::IntegerSize2D& 						size )
145     {
146         if( !mpDevice )
147             return uno::Reference< rendering::XBitmap >(); // we're disposed
148 
149 		DXBitmapSharedPtr pBitmap(
150 			new DXBitmap(
151 				::basegfx::unotools::b2ISizeFromIntegerSize2D(size),
152 				false));
153 
154 		// create a 24bit RGB system memory surface
155         return uno::Reference< rendering::XBitmap >(new CanvasBitmap(pBitmap,mpDevice));
156     }
157 
158     uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileBitmap(
159         const uno::Reference< rendering::XGraphicDevice >& 	/*rDevice*/,
160         const geometry::IntegerSize2D& 						/*size*/ )
161     {
162         return uno::Reference< rendering::XVolatileBitmap >();
163     }
164 
165     uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleAlphaBitmap(
166         const uno::Reference< rendering::XGraphicDevice >& 	/*rDevice*/,
167         const geometry::IntegerSize2D& 						size )
168     {
169         if( !mpDevice )
170             return uno::Reference< rendering::XBitmap >(); // we're disposed
171 
172 		DXBitmapSharedPtr pBitmap(
173 			new DXBitmap(
174 				::basegfx::unotools::b2ISizeFromIntegerSize2D(size),
175 				true));
176 
177 		// create a 32bit ARGB system memory surface
178         return uno::Reference< rendering::XBitmap >(new CanvasBitmap(pBitmap,mpDevice));
179     }
180 
181     uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileAlphaBitmap(
182         const uno::Reference< rendering::XGraphicDevice >& 	/*rDevice*/,
183         const geometry::IntegerSize2D& 						/*size*/ )
184     {
185         return uno::Reference< rendering::XVolatileBitmap >();
186     }
187 
188     sal_Bool DeviceHelper::hasFullScreenMode()
189     {
190         return false;
191     }
192 
193     sal_Bool DeviceHelper::enterFullScreenMode( sal_Bool /*bEnter*/ )
194     {
195         return false;
196     }
197 
198     uno::Any DeviceHelper::isAccelerated() const
199     {
200         return ::com::sun::star::uno::makeAny(false);
201     }
202 
203 	uno::Any DeviceHelper::getDeviceHandle() const
204     {
205         HDC hdc( getHDC() );
206         if( hdc )
207             return uno::makeAny( reinterpret_cast< sal_Int64 >(hdc) );
208         else
209             return uno::Any();
210     }
211 
212     uno::Any DeviceHelper::getSurfaceHandle() const
213     {
214 		// TODO(F1): expose DirectDraw object
215         //return mpBackBuffer->getBitmap().get();
216 		return uno::Any();
217     }
218 
219     namespace
220     {
221         struct DeviceColorSpace: public rtl::StaticWithInit<uno::Reference<rendering::XColorSpace>,
222                                                             DeviceColorSpace>
223         {
224             uno::Reference<rendering::XColorSpace> operator()()
225             {
226                 return vcl::unotools::createStandardColorSpace();
227             }
228         };
229     }
230 
231     uno::Reference<rendering::XColorSpace> DeviceHelper::getColorSpace() const
232     {
233         // always the same
234         return DeviceColorSpace::get();
235     }
236 }
237