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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_toolkit.hxx"
24
25 #include <tools/debug.hxx>
26 #include <tools/stream.hxx>
27 #include <vcl/bitmap.hxx>
28 #include <vcl/window.hxx>
29 #include <com/sun/star/util/MeasureUnit.hpp>
30 #include <com/sun/star/awt/XBitmap.hpp>
31 #include <com/sun/star/awt/XWindow.hpp>
32 #include <com/sun/star/awt/XDevice.hpp>
33 #include <com/sun/star/awt/XPointer.hpp>
34 #include <com/sun/star/awt/SimpleFontMetric.hpp>
35 #include <com/sun/star/awt/FontDescriptor.hpp>
36 #include <com/sun/star/awt/XControlContainer.hpp>
37 #include <com/sun/star/awt/FontWeight.hpp>
38 #include <com/sun/star/awt/FontWidth.hpp>
39 #include <com/sun/star/awt/KeyModifier.hpp>
40 #include <com/sun/star/awt/MouseButton.hpp>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/embed/EmbedMapUnits.hpp>
43 #include <com/sun/star/graphic/XGraphic.hpp>
44 #include <toolkit/helper/vclunohelper.hxx>
45 #include <toolkit/helper/convert.hxx>
46 #include <toolkit/awt/vclxbitmap.hxx>
47 #include <toolkit/awt/vclxregion.hxx>
48 #include <toolkit/awt/vclxwindow.hxx>
49 #include <toolkit/awt/vclxgraphics.hxx>
50 #include <toolkit/awt/vclxpointer.hxx>
51 #include <toolkit/awt/vclxfont.hxx>
52 #include <toolkit/controls/unocontrolcontainer.hxx>
53 #include <toolkit/controls/unocontrolcontainermodel.hxx>
54 #include <vcl/graph.hxx>
55 #include <comphelper/processfactory.hxx>
56 #include <com/sun/star/awt/Size.hpp>
57 #include <com/sun/star/awt/Point.hpp>
58 #include <vcl/dibtools.hxx>
59
60 using namespace ::com::sun::star;
61
62 // ----------------------------------------------------
63 // class VCLUnoHelper
64 // ----------------------------------------------------
65
CreateToolkit()66 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit> VCLUnoHelper::CreateToolkit()
67 {
68 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
69 ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > xI = xMSF->createInstance( ::rtl::OUString::createFromAscii( szServiceName2_Toolkit ) );
70
71 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit> xToolkit;
72 if ( xI.is() )
73 xToolkit = ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit>( xI, ::com::sun::star::uno::UNO_QUERY );
74
75 return xToolkit;
76 }
77
GetBitmap(const::com::sun::star::uno::Reference<::com::sun::star::awt::XBitmap> & rxBitmap)78 BitmapEx VCLUnoHelper::GetBitmap( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap>& rxBitmap )
79 {
80 BitmapEx aBmp;
81
82 ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic > xGraphic( rxBitmap, ::com::sun::star::uno::UNO_QUERY );
83 if( xGraphic.is() )
84 {
85 Graphic aGraphic( xGraphic );
86 aBmp = aGraphic.GetBitmapEx();
87 }
88 else if ( rxBitmap.is() )
89 {
90 VCLXBitmap* pVCLBitmap = VCLXBitmap::GetImplementation( rxBitmap );
91 if ( pVCLBitmap )
92 aBmp = pVCLBitmap->GetBitmap();
93 else
94 {
95 Bitmap aDIB, aMask;
96 {
97 ::com::sun::star::uno::Sequence<sal_Int8> aBytes = rxBitmap->getDIB();
98 SvMemoryStream aMem( (char*) aBytes.getArray(), aBytes.getLength(), STREAM_READ );
99 ReadDIB(aDIB, aMem, true);
100 }
101 {
102 ::com::sun::star::uno::Sequence<sal_Int8> aBytes = rxBitmap->getMaskDIB();
103 SvMemoryStream aMem( (char*) aBytes.getArray(), aBytes.getLength(), STREAM_READ );
104 ReadDIB(aMask, aMem, true);
105 }
106 aBmp = BitmapEx( aDIB, aMask );
107 }
108 }
109 return aBmp;
110 }
111
CreateBitmap(const BitmapEx & rBitmap)112 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap> VCLUnoHelper::CreateBitmap( const BitmapEx& rBitmap )
113 {
114 Graphic aGraphic( rBitmap );
115 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap> xBmp( aGraphic.GetXGraphic(), ::com::sun::star::uno::UNO_QUERY );
116 return xBmp;
117 }
118
GetWindow(const::com::sun::star::uno::Reference<::com::sun::star::awt::XWindow> & rxWindow)119 Window* VCLUnoHelper::GetWindow( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow>& rxWindow )
120 {
121 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( rxWindow );
122 return pVCLXWindow ? pVCLXWindow->GetWindow() : NULL;
123 }
124
GetWindow(const::com::sun::star::uno::Reference<::com::sun::star::awt::XWindow2> & rxWindow)125 Window* VCLUnoHelper::GetWindow( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow2>& rxWindow )
126 {
127 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( rxWindow );
128 return pVCLXWindow ? pVCLXWindow->GetWindow() : NULL;
129 }
130
GetWindow(const::com::sun::star::uno::Reference<::com::sun::star::awt::XWindowPeer> & rxWindow)131 Window* VCLUnoHelper::GetWindow( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer>& rxWindow )
132 {
133 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( rxWindow );
134 return pVCLXWindow ? pVCLXWindow->GetWindow() : NULL;
135 }
136
GetRegion(const::com::sun::star::uno::Reference<::com::sun::star::awt::XRegion> & rxRegion)137 Region VCLUnoHelper::GetRegion( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XRegion >& rxRegion )
138 {
139 Region aRegion;
140 VCLXRegion* pVCLRegion = VCLXRegion::GetImplementation( rxRegion );
141 if ( pVCLRegion )
142 aRegion = pVCLRegion->GetRegion();
143 else
144 {
145 ::com::sun::star::uno::Sequence< ::com::sun::star::awt::Rectangle > aRects = rxRegion->getRectangles();
146 sal_Int32 nRects = aRects.getLength();
147 for ( sal_Int32 n = 0; n < nRects; n++ )
148 aRegion.Union( VCLRectangle( aRects.getArray()[n] ) );
149 }
150 return aRegion;
151 }
152
GetInterface(Window * pWindow)153 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow> VCLUnoHelper::GetInterface( Window* pWindow )
154 {
155 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow > xWin;
156 if ( pWindow )
157 {
158 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xPeer = pWindow->GetComponentInterface();
159 xWin = xWin.query( xPeer );
160 }
161 return xWin;
162 }
163
CreatePointer()164 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPointer> VCLUnoHelper::CreatePointer()
165 {
166 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPointer> xPointer = new VCLXPointer;
167 return xPointer;
168 }
169
GetOutputDevice(const::com::sun::star::uno::Reference<::com::sun::star::awt::XDevice> & rxDevice)170 OutputDevice* VCLUnoHelper::GetOutputDevice( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice>& rxDevice )
171 {
172 OutputDevice* pOutDev = NULL;
173 VCLXDevice* pDev = VCLXDevice::GetImplementation( rxDevice );
174 if ( pDev )
175 pOutDev = pDev->GetOutputDevice();
176 return pOutDev;
177 }
178
GetOutputDevice(const::com::sun::star::uno::Reference<::com::sun::star::awt::XGraphics> & rxGraphics)179 OutputDevice* VCLUnoHelper::GetOutputDevice( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics>& rxGraphics )
180 {
181 OutputDevice* pOutDev = NULL;
182 VCLXGraphics* pGrf = VCLXGraphics::GetImplementation( rxGraphics );
183 if ( pGrf )
184 pOutDev = pGrf->GetOutputDevice();
185 return pOutDev;
186 }
187
CreatePolygon(const::com::sun::star::uno::Sequence<sal_Int32> & DataX,const::com::sun::star::uno::Sequence<sal_Int32> & DataY)188 Polygon VCLUnoHelper::CreatePolygon( const ::com::sun::star::uno::Sequence< sal_Int32 >& DataX, const ::com::sun::star::uno::Sequence< sal_Int32 >& DataY )
189 {
190 sal_uInt32 nLen = DataX.getLength();
191 const sal_Int32* pDataX = DataX.getConstArray();
192 const sal_Int32* pDataY = DataY.getConstArray();
193 Polygon aPoly( (sal_uInt16) nLen );
194 for ( sal_uInt16 n = 0; n < nLen; n++ )
195 {
196 Point aPnt;
197 aPnt.X() = pDataX[n];
198 aPnt.Y() = pDataY[n];
199 aPoly[n] = aPnt;
200 }
201 return aPoly;
202 }
203
CreateControlContainer(Window * pWindow)204 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlContainer> VCLUnoHelper::CreateControlContainer( Window* pWindow )
205 {
206 const uno::Reference< lang::XMultiServiceFactory > xFactory( ::comphelper::getProcessServiceFactory() );
207 UnoControlContainer* pContainer = new UnoControlContainer( xFactory, pWindow->GetComponentInterface( sal_True ) );
208 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlContainer > x = pContainer;
209
210 UnoControlModel* pContainerModel = new UnoControlContainerModel( xFactory );
211 pContainer->setModel( (::com::sun::star::awt::XControlModel*)pContainerModel );
212
213 return x;
214 }
215
ConvertFontWidth(FontWidth eWidth)216 float VCLUnoHelper::ConvertFontWidth( FontWidth eWidth )
217 {
218 if( eWidth == WIDTH_DONTKNOW )
219 return ::com::sun::star::awt::FontWidth::DONTKNOW;
220 else if( eWidth == WIDTH_ULTRA_CONDENSED )
221 return ::com::sun::star::awt::FontWidth::ULTRACONDENSED;
222 else if( eWidth == WIDTH_EXTRA_CONDENSED )
223 return ::com::sun::star::awt::FontWidth::EXTRACONDENSED;
224 else if( eWidth == WIDTH_CONDENSED )
225 return ::com::sun::star::awt::FontWidth::CONDENSED;
226 else if( eWidth == WIDTH_SEMI_CONDENSED )
227 return ::com::sun::star::awt::FontWidth::SEMICONDENSED;
228 else if( eWidth == WIDTH_NORMAL )
229 return ::com::sun::star::awt::FontWidth::NORMAL;
230 else if( eWidth == WIDTH_SEMI_EXPANDED )
231 return ::com::sun::star::awt::FontWidth::SEMIEXPANDED;
232 else if( eWidth == WIDTH_EXPANDED )
233 return ::com::sun::star::awt::FontWidth::EXPANDED;
234 else if( eWidth == WIDTH_EXTRA_EXPANDED )
235 return ::com::sun::star::awt::FontWidth::EXTRAEXPANDED;
236 else if( eWidth == WIDTH_ULTRA_EXPANDED )
237 return ::com::sun::star::awt::FontWidth::ULTRAEXPANDED;
238
239 DBG_ERROR( "Unknown FontWidth" );
240 return ::com::sun::star::awt::FontWidth::DONTKNOW;
241 }
242
ConvertFontWidth(float f)243 FontWidth VCLUnoHelper::ConvertFontWidth( float f )
244 {
245 if( f <= ::com::sun::star::awt::FontWidth::DONTKNOW )
246 return WIDTH_DONTKNOW;
247 else if( f <= ::com::sun::star::awt::FontWidth::ULTRACONDENSED )
248 return WIDTH_ULTRA_CONDENSED;
249 else if( f <= ::com::sun::star::awt::FontWidth::EXTRACONDENSED )
250 return WIDTH_EXTRA_CONDENSED;
251 else if( f <= ::com::sun::star::awt::FontWidth::CONDENSED )
252 return WIDTH_CONDENSED;
253 else if( f <= ::com::sun::star::awt::FontWidth::SEMICONDENSED )
254 return WIDTH_SEMI_CONDENSED;
255 else if( f <= ::com::sun::star::awt::FontWidth::NORMAL )
256 return WIDTH_NORMAL;
257 else if( f <= ::com::sun::star::awt::FontWidth::SEMIEXPANDED )
258 return WIDTH_SEMI_EXPANDED;
259 else if( f <= ::com::sun::star::awt::FontWidth::EXPANDED )
260 return WIDTH_EXPANDED;
261 else if( f <= ::com::sun::star::awt::FontWidth::EXTRAEXPANDED )
262 return WIDTH_EXTRA_EXPANDED;
263 else if( f <= ::com::sun::star::awt::FontWidth::ULTRAEXPANDED )
264 return WIDTH_ULTRA_EXPANDED;
265
266 DBG_ERROR( "Unknown FontWidth" );
267 return WIDTH_DONTKNOW;
268 }
269
ConvertFontWeight(FontWeight eWeight)270 float VCLUnoHelper::ConvertFontWeight( FontWeight eWeight )
271 {
272 if( eWeight == WEIGHT_DONTKNOW )
273 return ::com::sun::star::awt::FontWeight::DONTKNOW;
274 else if( eWeight == WEIGHT_THIN )
275 return ::com::sun::star::awt::FontWeight::THIN;
276 else if( eWeight == WEIGHT_ULTRALIGHT )
277 return ::com::sun::star::awt::FontWeight::ULTRALIGHT;
278 else if( eWeight == WEIGHT_LIGHT )
279 return ::com::sun::star::awt::FontWeight::LIGHT;
280 else if( eWeight == WEIGHT_SEMILIGHT )
281 return ::com::sun::star::awt::FontWeight::SEMILIGHT;
282 else if( ( eWeight == WEIGHT_NORMAL ) || ( eWeight == WEIGHT_MEDIUM ) )
283 return ::com::sun::star::awt::FontWeight::NORMAL;
284 else if( eWeight == WEIGHT_SEMIBOLD )
285 return ::com::sun::star::awt::FontWeight::SEMIBOLD;
286 else if( eWeight == WEIGHT_BOLD )
287 return ::com::sun::star::awt::FontWeight::BOLD;
288 else if( eWeight == WEIGHT_ULTRABOLD )
289 return ::com::sun::star::awt::FontWeight::ULTRABOLD;
290 else if( eWeight == WEIGHT_BLACK )
291 return ::com::sun::star::awt::FontWeight::BLACK;
292
293 DBG_ERROR( "Unknown FontWeigth" );
294 return ::com::sun::star::awt::FontWeight::DONTKNOW;
295 }
296
ConvertFontWeight(float f)297 FontWeight VCLUnoHelper::ConvertFontWeight( float f )
298 {
299 if( f <= ::com::sun::star::awt::FontWeight::DONTKNOW )
300 return WEIGHT_DONTKNOW;
301 else if( f <= ::com::sun::star::awt::FontWeight::THIN )
302 return WEIGHT_THIN;
303 else if( f <= ::com::sun::star::awt::FontWeight::ULTRALIGHT )
304 return WEIGHT_ULTRALIGHT;
305 else if( f <= ::com::sun::star::awt::FontWeight::LIGHT )
306 return WEIGHT_LIGHT;
307 else if( f <= ::com::sun::star::awt::FontWeight::SEMILIGHT )
308 return WEIGHT_SEMILIGHT;
309 else if( f <= ::com::sun::star::awt::FontWeight::NORMAL )
310 return WEIGHT_NORMAL;
311 else if( f <= ::com::sun::star::awt::FontWeight::SEMIBOLD )
312 return WEIGHT_SEMIBOLD;
313 else if( f <= ::com::sun::star::awt::FontWeight::BOLD )
314 return WEIGHT_BOLD;
315 else if( f <= ::com::sun::star::awt::FontWeight::ULTRABOLD )
316 return WEIGHT_ULTRABOLD;
317 else if( f <= ::com::sun::star::awt::FontWeight::BLACK )
318 return WEIGHT_BLACK;
319
320 DBG_ERROR( "Unknown FontWeigth" );
321 return WEIGHT_DONTKNOW;
322 }
323
324
CreateFontDescriptor(const Font & rFont)325 ::com::sun::star::awt::FontDescriptor VCLUnoHelper::CreateFontDescriptor( const Font& rFont )
326 {
327 ::com::sun::star::awt::FontDescriptor aFD;
328 aFD.Name = rFont.GetName();
329 aFD.StyleName = rFont.GetStyleName();
330 aFD.Height = (sal_Int16)rFont.GetSize().Height();
331 aFD.Width = (sal_Int16)rFont.GetSize().Width();
332 aFD.Family = sal::static_int_cast< sal_Int16 >(rFont.GetFamily());
333 aFD.CharSet = rFont.GetCharSet();
334 aFD.Pitch = sal::static_int_cast< sal_Int16 >(rFont.GetPitch());
335 aFD.CharacterWidth = VCLUnoHelper::ConvertFontWidth( rFont.GetWidthType() );
336 aFD.Weight= VCLUnoHelper::ConvertFontWeight( rFont.GetWeight() );
337 aFD.Slant = (::com::sun::star::awt::FontSlant)rFont.GetItalic();
338 aFD.Underline = sal::static_int_cast< sal_Int16 >(rFont.GetUnderline());
339 aFD.Strikeout = sal::static_int_cast< sal_Int16 >(rFont.GetStrikeout());
340 aFD.Orientation = rFont.GetOrientation();
341 aFD.Kerning = rFont.IsKerning();
342 aFD.WordLineMode = rFont.IsWordLineMode();
343 aFD.Type = 0; // ??? => Nur an Metric...
344 return aFD;
345 }
346
CreateFont(const::com::sun::star::awt::FontDescriptor & rDescr,const Font & rInitFont)347 Font VCLUnoHelper::CreateFont( const ::com::sun::star::awt::FontDescriptor& rDescr, const Font& rInitFont )
348 {
349 Font aFont( rInitFont );
350 if ( rDescr.Name.getLength() )
351 aFont.SetName( rDescr.Name );
352 if ( rDescr.StyleName.getLength() )
353 aFont.SetStyleName( rDescr.StyleName );
354 if ( rDescr.Height )
355 aFont.SetSize( Size( rDescr.Width, rDescr.Height ) );
356 if ( (FontFamily)rDescr.Family != FAMILY_DONTKNOW )
357 aFont.SetFamily( (FontFamily)rDescr.Family );
358 if ( (CharSet)rDescr.CharSet != RTL_TEXTENCODING_DONTKNOW )
359 aFont.SetCharSet( (CharSet)rDescr.CharSet );
360 if ( (FontPitch)rDescr.Pitch != PITCH_DONTKNOW )
361 aFont.SetPitch( (FontPitch)rDescr.Pitch );
362 if ( rDescr.CharacterWidth )
363 aFont.SetWidthType( VCLUnoHelper::ConvertFontWidth( rDescr.CharacterWidth ) );
364 if ( rDescr.Weight )
365 aFont.SetWeight( VCLUnoHelper::ConvertFontWeight( rDescr.Weight ) );
366 if ( (FontItalic)rDescr.Slant != ITALIC_DONTKNOW )
367 aFont.SetItalic( (FontItalic)rDescr.Slant );
368 if ( (FontUnderline)rDescr.Underline != UNDERLINE_DONTKNOW )
369 aFont.SetUnderline( (FontUnderline)rDescr.Underline );
370 if ( (FontStrikeout)rDescr.Strikeout != STRIKEOUT_DONTKNOW )
371 aFont.SetStrikeout( (FontStrikeout)rDescr.Strikeout );
372
373 // Kein DONTKNOW
374 aFont.SetOrientation( (short)rDescr.Orientation );
375 aFont.SetKerning( rDescr.Kerning );
376 aFont.SetWordLineMode( rDescr.WordLineMode );
377
378 return aFont;
379 }
380
CreateFont(const::com::sun::star::uno::Reference<::com::sun::star::awt::XFont> & rxFont)381 Font VCLUnoHelper::CreateFont( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont >& rxFont )
382 {
383 Font aFont;
384 VCLXFont* pVCLXFont = VCLXFont::GetImplementation( rxFont );
385 if ( pVCLXFont )
386 aFont = pVCLXFont->GetFont();
387 return aFont;
388 }
389
390
CreateFontMetric(const FontMetric & rFontMetric)391 ::com::sun::star::awt::SimpleFontMetric VCLUnoHelper::CreateFontMetric( const FontMetric& rFontMetric )
392 {
393 ::com::sun::star::awt::SimpleFontMetric aFM;
394 aFM.Ascent = (sal_Int16)rFontMetric.GetAscent();
395 aFM.Descent = (sal_Int16)rFontMetric.GetDescent();
396 aFM.Leading = (sal_Int16)rFontMetric.GetIntLeading();
397 aFM.Slant = (sal_Int16)rFontMetric.GetSlant();
398 aFM.FirstChar = 0x0020;
399 aFM.LastChar = 0xFFFD;
400 return aFM;
401 }
402
IsZero(::com::sun::star::awt::Rectangle rRect)403 sal_Bool VCLUnoHelper::IsZero( ::com::sun::star::awt::Rectangle rRect )
404 {
405 return ( !rRect.X && !rRect.Y && !rRect.Width && !rRect.Height );
406 }
407
UnoEmbed2VCLMapUnit(sal_Int32 nUnoEmbedMapUnit)408 MapUnit VCLUnoHelper::UnoEmbed2VCLMapUnit( sal_Int32 nUnoEmbedMapUnit )
409 {
410 switch( nUnoEmbedMapUnit )
411 {
412 case ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_MM:
413 return MAP_100TH_MM;
414 case ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_MM:
415 return MAP_10TH_MM;
416 case ::com::sun::star::embed::EmbedMapUnits::ONE_MM:
417 return MAP_MM;
418 case ::com::sun::star::embed::EmbedMapUnits::ONE_CM:
419 return MAP_CM;
420 case ::com::sun::star::embed::EmbedMapUnits::ONE_1000TH_INCH:
421 return MAP_1000TH_INCH;
422 case ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_INCH:
423 return MAP_100TH_INCH;
424 case ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_INCH:
425 return MAP_10TH_INCH;
426 case ::com::sun::star::embed::EmbedMapUnits::ONE_INCH:
427 return MAP_INCH;
428 case ::com::sun::star::embed::EmbedMapUnits::POINT:
429 return MAP_POINT;
430 case ::com::sun::star::embed::EmbedMapUnits::TWIP:
431 return MAP_TWIP;
432 case ::com::sun::star::embed::EmbedMapUnits::PIXEL:
433 return MAP_PIXEL;
434 }
435
436 OSL_ENSURE( sal_False, "Unexpected UNO map mode is provided!\n" );
437 return MAP_LASTENUMDUMMY;
438 }
439
VCL2UnoEmbedMapUnit(MapUnit nVCLMapUnit)440 sal_Int32 VCLUnoHelper::VCL2UnoEmbedMapUnit( MapUnit nVCLMapUnit )
441 {
442 switch( nVCLMapUnit )
443 {
444 case MAP_100TH_MM:
445 return ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_MM;
446 case MAP_10TH_MM:
447 return ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_MM;
448 case MAP_MM:
449 return ::com::sun::star::embed::EmbedMapUnits::ONE_MM;
450 case MAP_CM:
451 return ::com::sun::star::embed::EmbedMapUnits::ONE_CM;
452 case MAP_1000TH_INCH:
453 return ::com::sun::star::embed::EmbedMapUnits::ONE_1000TH_INCH;
454 case MAP_100TH_INCH:
455 return ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_INCH;
456 case MAP_10TH_INCH:
457 return ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_INCH;
458 case MAP_INCH:
459 return ::com::sun::star::embed::EmbedMapUnits::ONE_INCH;
460 case MAP_POINT:
461 return ::com::sun::star::embed::EmbedMapUnits::POINT;
462 case MAP_TWIP:
463 return ::com::sun::star::embed::EmbedMapUnits::TWIP;
464 case MAP_PIXEL:
465 return ::com::sun::star::embed::EmbedMapUnits::PIXEL;
466 default: ; // avoid compiler warning
467 }
468
469 OSL_ENSURE( sal_False, "Unexpected VCL map mode is provided!\n" );
470 return -1;
471 }
472
473 using namespace ::com::sun::star::util;
474
475 //====================================================================
476 //= file-local helpers
477 //====================================================================
478 namespace
479 {
480 enum UnitConversionDirection
481 {
482 FieldUnitToMeasurementUnit,
483 MeasurementUnitToFieldUnit
484 };
485
convertMeasurementUnit(sal_Int16 _nUnit,UnitConversionDirection eDirection,sal_Int16 & _rFieldToUNOValueFactor)486 sal_Int16 convertMeasurementUnit( sal_Int16 _nUnit, UnitConversionDirection eDirection, sal_Int16& _rFieldToUNOValueFactor )
487 {
488 static struct _unit_table
489 {
490 FieldUnit eFieldUnit;
491 sal_Int16 nMeasurementUnit;
492 sal_Int16 nFieldToMeasureFactor;
493 } aUnits[] = {
494 { FUNIT_NONE, -1 , -1},
495 { FUNIT_MM, MeasureUnit::MM, 1 }, // must precede MM_10TH
496 { FUNIT_MM, MeasureUnit::MM_10TH, 10 },
497 { FUNIT_100TH_MM, MeasureUnit::MM_100TH, 1 },
498 { FUNIT_CM, MeasureUnit::CM, 1 },
499 { FUNIT_M, MeasureUnit::M, 1 },
500 { FUNIT_KM, MeasureUnit::KM, 1 },
501 { FUNIT_TWIP, MeasureUnit::TWIP, 1 },
502 { FUNIT_POINT, MeasureUnit::POINT, 1 },
503 { FUNIT_PICA, MeasureUnit::PICA, 1 },
504 { FUNIT_INCH, MeasureUnit::INCH, 1 }, // must precede INCH_*TH
505 { FUNIT_INCH, MeasureUnit::INCH_10TH, 10 },
506 { FUNIT_INCH, MeasureUnit::INCH_100TH, 100 },
507 { FUNIT_INCH, MeasureUnit::INCH_1000TH, 1000 },
508 { FUNIT_FOOT, MeasureUnit::FOOT, 1 },
509 { FUNIT_MILE, MeasureUnit::MILE, 1 },
510 };
511 for ( size_t i = 0; i < sizeof( aUnits ) / sizeof( aUnits[0] ); ++i )
512 {
513 if ( eDirection == FieldUnitToMeasurementUnit )
514 {
515 if ( ( aUnits[ i ].eFieldUnit == (FieldUnit)_nUnit ) && ( aUnits[ i ].nFieldToMeasureFactor == _rFieldToUNOValueFactor ) )
516 return aUnits[ i ].nMeasurementUnit;
517 }
518 else
519 {
520 if ( aUnits[ i ].nMeasurementUnit == _nUnit )
521 {
522 _rFieldToUNOValueFactor = aUnits[ i ].nFieldToMeasureFactor;
523 return (sal_Int16)aUnits[ i ].eFieldUnit;
524 }
525 }
526 }
527 if ( eDirection == FieldUnitToMeasurementUnit )
528 return -1;
529
530 _rFieldToUNOValueFactor = 1;
531 return (sal_Int16)FUNIT_NONE;
532 }
533 }
534 //========================================================================
535 //= MeasurementUnitConversion
536 //========================================================================
537 //------------------------------------------------------------------------
ConvertToMeasurementUnit(FieldUnit _nFieldUnit,sal_Int16 _nUNOToFieldValueFactor)538 sal_Int16 VCLUnoHelper::ConvertToMeasurementUnit( FieldUnit _nFieldUnit, sal_Int16 _nUNOToFieldValueFactor )
539 {
540 return convertMeasurementUnit( (sal_Int16)_nFieldUnit, FieldUnitToMeasurementUnit, _nUNOToFieldValueFactor );
541 }
542
543 //------------------------------------------------------------------------
ConvertToFieldUnit(sal_Int16 _nMeasurementUnit,sal_Int16 & _rFieldToUNOValueFactor)544 FieldUnit VCLUnoHelper::ConvertToFieldUnit( sal_Int16 _nMeasurementUnit, sal_Int16& _rFieldToUNOValueFactor )
545 {
546 return (FieldUnit)convertMeasurementUnit( _nMeasurementUnit, MeasurementUnitToFieldUnit, _rFieldToUNOValueFactor );
547 }
548
549
ConvertToMapModeUnit(sal_Int16 _nMeasureUnit)550 MapUnit /* MapModeUnit */ VCLUnoHelper::ConvertToMapModeUnit(sal_Int16 /* com.sun.star.util.MeasureUnit.* */ _nMeasureUnit) throw (::com::sun::star::lang::IllegalArgumentException)
551 {
552 MapUnit eMode;
553 switch(_nMeasureUnit)
554 {
555 case com::sun::star::util::MeasureUnit::MM_100TH:
556 eMode = MAP_100TH_MM;
557 break;
558
559
560 case com::sun::star::util::MeasureUnit::MM_10TH:
561 eMode = MAP_10TH_MM;
562 break;
563
564 case com::sun::star::util::MeasureUnit::MM:
565 eMode = MAP_MM;
566 break;
567
568 case com::sun::star::util::MeasureUnit::CM:
569 eMode = MAP_CM;
570 break;
571
572 case com::sun::star::util::MeasureUnit::INCH_1000TH:
573 eMode = MAP_1000TH_INCH;
574 break;
575
576 case com::sun::star::util::MeasureUnit::INCH_100TH:
577 eMode = MAP_100TH_INCH;
578 break;
579
580 case com::sun::star::util::MeasureUnit::INCH_10TH:
581 eMode = MAP_10TH_INCH;
582 break;
583
584 case com::sun::star::util::MeasureUnit::INCH:
585 eMode = MAP_INCH;
586 break;
587
588 case com::sun::star::util::MeasureUnit::POINT:
589 eMode = MAP_POINT;
590 break;
591
592 case com::sun::star::util::MeasureUnit::TWIP:
593 eMode = MAP_TWIP;
594 break;
595
596 case com::sun::star::util::MeasureUnit::PIXEL:
597 eMode = MAP_PIXEL;
598 break;
599
600 /*
601 case com::sun::star::util::MeasureUnit::M:
602 break;
603 case com::sun::star::util::MeasureUnit::KM:
604 break;
605 case com::sun::star::util::MeasureUnit::PICA:
606 break;
607 case com::sun::star::util::MeasureUnit::FOOT:
608 break;
609 case com::sun::star::util::MeasureUnit::MILE:
610 break;
611 case com::sun::star::util::MeasureUnit::PERCENT:
612 break;
613 */
614 case com::sun::star::util::MeasureUnit::APPFONT:
615 eMode = MAP_APPFONT;
616 break;
617
618 case com::sun::star::util::MeasureUnit::SYSFONT:
619 eMode = MAP_SYSFONT;
620 break;
621
622 /*
623 case com::sun::star::util::MeasureUnit::RELATIVE:
624 eMode = MAP_RELATIVE;
625 break;
626 case com::sun::star::util::MeasureUnit::REALAPPFONT:
627 eMode = MAP_REALAPPFONT;
628 break;
629 */
630
631 default:
632 throw ::com::sun::star::lang::IllegalArgumentException(::rtl::OUString::createFromAscii("Unsupported measure unit."), NULL, 1 );
633 }
634 return eMode;
635 }
636
ConvertToMeasurementUnit(MapUnit _eMapModeUnit)637 sal_Int16 /* com.sun.star.util.MeasureUnit.* */ VCLUnoHelper::ConvertToMeasurementUnit(MapUnit /* MapModeUnit */ _eMapModeUnit) throw (::com::sun::star::lang::IllegalArgumentException)
638 {
639 sal_Int16 nMeasureUnit = 0;
640 switch (_eMapModeUnit)
641 {
642 case MAP_100TH_MM:
643 nMeasureUnit = com::sun::star::util::MeasureUnit::MM_100TH;
644 break;
645
646 case MAP_10TH_MM:
647 nMeasureUnit = com::sun::star::util::MeasureUnit::MM_10TH;
648 break;
649
650 case MAP_MM:
651 nMeasureUnit = com::sun::star::util::MeasureUnit::MM;
652 break;
653
654 case MAP_CM:
655 nMeasureUnit = com::sun::star::util::MeasureUnit::CM;
656 break;
657
658 case MAP_1000TH_INCH:
659 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH_1000TH;
660 break;
661
662 case MAP_100TH_INCH:
663 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH_100TH;
664 break;
665
666 case MAP_10TH_INCH:
667 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH_10TH;
668 break;
669
670 case MAP_INCH:
671 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH;
672 break;
673
674 case MAP_POINT:
675 nMeasureUnit = com::sun::star::util::MeasureUnit::POINT;
676 break;
677
678 case MAP_TWIP:
679 nMeasureUnit = com::sun::star::util::MeasureUnit::TWIP;
680 break;
681
682 case MAP_PIXEL:
683 nMeasureUnit = com::sun::star::util::MeasureUnit::PIXEL;
684 break;
685
686 case MAP_APPFONT:
687 nMeasureUnit = com::sun::star::util::MeasureUnit::APPFONT;
688 break;
689
690 case MAP_SYSFONT:
691 nMeasureUnit = com::sun::star::util::MeasureUnit::SYSFONT;
692 break;
693
694 /*
695 case MAP_RELATIVE:
696 break;
697
698 case MAP_REALAPPFONT:
699 break;
700 */
701 default:
702 throw ::com::sun::star::lang::IllegalArgumentException(::rtl::OUString::createFromAscii("Unsupported MapMode unit."), NULL, 1 );
703 }
704 return nMeasureUnit;
705 }
706
ConvertToVCLSize(com::sun::star::awt::Size const & _aSize)707 ::Size VCLUnoHelper::ConvertToVCLSize(com::sun::star::awt::Size const& _aSize)
708 {
709 ::Size aVCLSize(_aSize.Width, _aSize.Height);
710 return aVCLSize;
711 }
712
ConvertToAWTSize(::Size const & _aSize)713 com::sun::star::awt::Size VCLUnoHelper::ConvertToAWTSize(::Size /* VCLSize */ const& _aSize)
714 {
715 com::sun::star::awt::Size aAWTSize(_aSize.Width(), _aSize.Height());
716 return aAWTSize;
717 }
718
719
ConvertToVCLPoint(com::sun::star::awt::Point const & _aPoint)720 ::Point VCLUnoHelper::ConvertToVCLPoint(com::sun::star::awt::Point const& _aPoint)
721 {
722 ::Point aVCLPoint(_aPoint.X, _aPoint.Y);
723 return aVCLPoint;
724 }
725
ConvertToAWTPoint(::Point const & _aPoint)726 com::sun::star::awt::Point VCLUnoHelper::ConvertToAWTPoint(::Point /* VCLPoint */ const& _aPoint)
727 {
728 com::sun::star::awt::Point aAWTPoint(_aPoint.X(), _aPoint.Y());
729 return aAWTPoint;
730 }
731
ConvertToVCLRect(::com::sun::star::awt::Rectangle const & _rRect)732 ::Rectangle VCLUnoHelper::ConvertToVCLRect( ::com::sun::star::awt::Rectangle const & _rRect )
733 {
734 return ::Rectangle( _rRect.X, _rRect.Y, _rRect.X + _rRect.Width - 1, _rRect.Y + _rRect.Height - 1 );
735 }
736
ConvertToAWTRect(::Rectangle const & _rRect)737 ::com::sun::star::awt::Rectangle VCLUnoHelper::ConvertToAWTRect( ::Rectangle const & _rRect )
738 {
739 return ::com::sun::star::awt::Rectangle( _rRect.Left(), _rRect.Top(), _rRect.GetWidth(), _rRect.GetHeight() );
740 }
741
createMouseEvent(const::MouseEvent & _rVclEvent,const uno::Reference<uno::XInterface> & _rxContext)742 awt::MouseEvent VCLUnoHelper::createMouseEvent( const ::MouseEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext )
743 {
744 awt::MouseEvent aMouseEvent;
745 aMouseEvent.Source = _rxContext;
746
747 aMouseEvent.Modifiers = 0;
748 if ( _rVclEvent.IsShift() )
749 aMouseEvent.Modifiers |= ::com::sun::star::awt::KeyModifier::SHIFT;
750 if ( _rVclEvent.IsMod1() )
751 aMouseEvent.Modifiers |= ::com::sun::star::awt::KeyModifier::MOD1;
752 if ( _rVclEvent.IsMod2() )
753 aMouseEvent.Modifiers |= ::com::sun::star::awt::KeyModifier::MOD2;
754
755 aMouseEvent.Buttons = 0;
756 if ( _rVclEvent.IsLeft() )
757 aMouseEvent.Buttons |= ::com::sun::star::awt::MouseButton::LEFT;
758 if ( _rVclEvent.IsRight() )
759 aMouseEvent.Buttons |= ::com::sun::star::awt::MouseButton::RIGHT;
760 if ( _rVclEvent.IsMiddle() )
761 aMouseEvent.Buttons |= ::com::sun::star::awt::MouseButton::MIDDLE;
762
763 aMouseEvent.X = _rVclEvent.GetPosPixel().X();
764 aMouseEvent.Y = _rVclEvent.GetPosPixel().Y();
765 aMouseEvent.ClickCount = _rVclEvent.GetClicks();
766 aMouseEvent.PopupTrigger = sal_False;
767
768 return aMouseEvent;
769 }
770
createKeyEvent(const::KeyEvent & _rVclEvent,const uno::Reference<uno::XInterface> & _rxContext)771 awt::KeyEvent VCLUnoHelper::createKeyEvent( const ::KeyEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext )
772 {
773 awt::KeyEvent aKeyEvent;
774 aKeyEvent.Source = _rxContext;
775
776 aKeyEvent.Modifiers = 0;
777 if ( _rVclEvent.GetKeyCode().IsShift() )
778 aKeyEvent.Modifiers |= awt::KeyModifier::SHIFT;
779 if ( _rVclEvent.GetKeyCode().IsMod1() )
780 aKeyEvent.Modifiers |= awt::KeyModifier::MOD1;
781 if ( _rVclEvent.GetKeyCode().IsMod2() )
782 aKeyEvent.Modifiers |= awt::KeyModifier::MOD2;
783 if ( _rVclEvent.GetKeyCode().IsMod3() )
784 aKeyEvent.Modifiers |= awt::KeyModifier::MOD3;
785
786 aKeyEvent.KeyCode = _rVclEvent.GetKeyCode().GetCode();
787 aKeyEvent.KeyChar = _rVclEvent.GetCharCode();
788 aKeyEvent.KeyFunc = ::sal::static_int_cast< sal_Int16 >( _rVclEvent.GetKeyCode().GetFunction());
789
790 return aKeyEvent;
791 }
792