Update ooo320-m1
[ooovba.git] / toolkit / source / helper / vclunohelper.cxx
blob426a6a16f199c87f788ebf8b9fb69c0fa7d0b3c9
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vclunohelper.cxx,v $
10 * $Revision: 1.19 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_toolkit.hxx"
34 #include <tools/debug.hxx>
35 #include <tools/stream.hxx>
36 #include <vcl/bitmap.hxx>
37 #include <vcl/window.hxx>
38 #include <com/sun/star/util/MeasureUnit.hpp>
39 #include <com/sun/star/awt/XBitmap.hpp>
40 #include <com/sun/star/awt/XWindow.hpp>
41 #include <com/sun/star/awt/XDevice.hpp>
42 #include <com/sun/star/awt/XPointer.hpp>
43 #include <com/sun/star/awt/SimpleFontMetric.hpp>
44 #include <com/sun/star/awt/FontDescriptor.hpp>
45 #include <com/sun/star/awt/XControlContainer.hpp>
46 #include <com/sun/star/awt/FontWeight.hpp>
47 #include <com/sun/star/awt/FontWidth.hpp>
48 #include <com/sun/star/awt/KeyModifier.hpp>
49 #include <com/sun/star/awt/MouseButton.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
51 #include <com/sun/star/embed/EmbedMapUnits.hpp>
53 #include <com/sun/star/graphic/XGraphic.hpp>
55 #include <toolkit/helper/vclunohelper.hxx>
56 #include <toolkit/helper/convert.hxx>
57 #include <toolkit/awt/vclxbitmap.hxx>
58 #include <toolkit/awt/vclxregion.hxx>
59 #include <toolkit/awt/vclxwindow.hxx>
60 #include <toolkit/awt/vclxgraphics.hxx>
61 #include <toolkit/awt/vclxpointer.hxx>
62 #include <toolkit/awt/vclxfont.hxx>
63 #include <toolkit/controls/unocontrolcontainer.hxx>
64 #include <toolkit/controls/unocontrolcontainermodel.hxx>
66 #include <vcl/graph.hxx>
67 #include <comphelper/processfactory.hxx>
69 #include <com/sun/star/awt/Size.hpp>
70 #include <com/sun/star/awt/Point.hpp>
72 using namespace ::com::sun::star;
74 // ----------------------------------------------------
75 // class VCLUnoHelper
76 // ----------------------------------------------------
78 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit> VCLUnoHelper::CreateToolkit()
80 ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory > xMSF = ::comphelper::getProcessServiceFactory();
81 ::com::sun::star::uno::Reference < ::com::sun::star::uno::XInterface > xI = xMSF->createInstance( ::rtl::OUString::createFromAscii( szServiceName2_Toolkit ) );
83 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit> xToolkit;
84 if ( xI.is() )
85 xToolkit = ::com::sun::star::uno::Reference< ::com::sun::star::awt::XToolkit>( xI, ::com::sun::star::uno::UNO_QUERY );
87 return xToolkit;
90 BitmapEx VCLUnoHelper::GetBitmap( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap>& rxBitmap )
92 BitmapEx aBmp;
94 ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic > xGraphic( rxBitmap, ::com::sun::star::uno::UNO_QUERY );
95 if( xGraphic.is() )
97 Graphic aGraphic( xGraphic );
98 aBmp = aGraphic.GetBitmapEx();
100 else if ( rxBitmap.is() )
102 VCLXBitmap* pVCLBitmap = VCLXBitmap::GetImplementation( rxBitmap );
103 if ( pVCLBitmap )
104 aBmp = pVCLBitmap->GetBitmap();
105 else
107 Bitmap aDIB, aMask;
109 ::com::sun::star::uno::Sequence<sal_Int8> aBytes = rxBitmap->getDIB();
110 SvMemoryStream aMem( (char*) aBytes.getArray(), aBytes.getLength(), STREAM_READ );
111 aMem >> aDIB;
114 ::com::sun::star::uno::Sequence<sal_Int8> aBytes = rxBitmap->getMaskDIB();
115 SvMemoryStream aMem( (char*) aBytes.getArray(), aBytes.getLength(), STREAM_READ );
116 aMem >> aMask;
118 aBmp = BitmapEx( aDIB, aMask );
121 return aBmp;
124 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap> VCLUnoHelper::CreateBitmap( const BitmapEx& rBitmap )
126 Graphic aGraphic( rBitmap );
127 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XBitmap> xBmp( aGraphic.GetXGraphic(), ::com::sun::star::uno::UNO_QUERY );
128 return xBmp;
131 Window* VCLUnoHelper::GetWindow( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow>& rxWindow )
133 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( rxWindow );
134 return pVCLXWindow ? pVCLXWindow->GetWindow() : NULL;
137 Window* VCLUnoHelper::GetWindow( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow2>& rxWindow )
139 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( rxWindow );
140 return pVCLXWindow ? pVCLXWindow->GetWindow() : NULL;
143 Window* VCLUnoHelper::GetWindow( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer>& rxWindow )
145 VCLXWindow* pVCLXWindow = VCLXWindow::GetImplementation( rxWindow );
146 return pVCLXWindow ? pVCLXWindow->GetWindow() : NULL;
149 Region VCLUnoHelper::GetRegion( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XRegion >& rxRegion )
151 Region aRegion;
152 VCLXRegion* pVCLRegion = VCLXRegion::GetImplementation( rxRegion );
153 if ( pVCLRegion )
154 aRegion = pVCLRegion->GetRegion();
155 else
157 ::com::sun::star::uno::Sequence< ::com::sun::star::awt::Rectangle > aRects = rxRegion->getRectangles();
158 sal_Int32 nRects = aRects.getLength();
159 for ( sal_Int32 n = 0; n < nRects; n++ )
160 aRegion.Union( VCLRectangle( aRects.getArray()[n] ) );
162 return aRegion;
165 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow> VCLUnoHelper::GetInterface( Window* pWindow )
167 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindow > xWin;
168 if ( pWindow )
170 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XWindowPeer> xPeer = pWindow->GetComponentInterface();
171 xWin = xWin.query( xPeer );
173 return xWin;
176 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPointer> VCLUnoHelper::CreatePointer()
178 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XPointer> xPointer = new VCLXPointer;
179 return xPointer;
182 OutputDevice* VCLUnoHelper::GetOutputDevice( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XDevice>& rxDevice )
184 OutputDevice* pOutDev = NULL;
185 VCLXDevice* pDev = VCLXDevice::GetImplementation( rxDevice );
186 if ( pDev )
187 pOutDev = pDev->GetOutputDevice();
188 return pOutDev;
191 OutputDevice* VCLUnoHelper::GetOutputDevice( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XGraphics>& rxGraphics )
193 OutputDevice* pOutDev = NULL;
194 VCLXGraphics* pGrf = VCLXGraphics::GetImplementation( rxGraphics );
195 if ( pGrf )
196 pOutDev = pGrf->GetOutputDevice();
197 return pOutDev;
200 Polygon VCLUnoHelper::CreatePolygon( const ::com::sun::star::uno::Sequence< sal_Int32 >& DataX, const ::com::sun::star::uno::Sequence< sal_Int32 >& DataY )
202 sal_uInt32 nLen = DataX.getLength();
203 const sal_Int32* pDataX = DataX.getConstArray();
204 const sal_Int32* pDataY = DataY.getConstArray();
205 Polygon aPoly( (sal_uInt16) nLen );
206 for ( sal_uInt16 n = 0; n < nLen; n++ )
208 Point aPnt;
209 aPnt.X() = pDataX[n];
210 aPnt.Y() = pDataY[n];
211 aPoly[n] = aPnt;
213 return aPoly;
216 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlContainer> VCLUnoHelper::CreateControlContainer( Window* pWindow )
218 UnoControlContainer* pContainer = new UnoControlContainer( pWindow->GetComponentInterface( sal_True ) );
219 ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControlContainer > x = pContainer;
221 UnoControlModel* pContainerModel = new UnoControlContainerModel;
222 pContainer->setModel( (::com::sun::star::awt::XControlModel*)pContainerModel );
224 return x;
227 float VCLUnoHelper::ConvertFontWidth( FontWidth eWidth )
229 if( eWidth == WIDTH_DONTKNOW )
230 return ::com::sun::star::awt::FontWidth::DONTKNOW;
231 else if( eWidth == WIDTH_ULTRA_CONDENSED )
232 return ::com::sun::star::awt::FontWidth::ULTRACONDENSED;
233 else if( eWidth == WIDTH_EXTRA_CONDENSED )
234 return ::com::sun::star::awt::FontWidth::EXTRACONDENSED;
235 else if( eWidth == WIDTH_CONDENSED )
236 return ::com::sun::star::awt::FontWidth::CONDENSED;
237 else if( eWidth == WIDTH_SEMI_CONDENSED )
238 return ::com::sun::star::awt::FontWidth::SEMICONDENSED;
239 else if( eWidth == WIDTH_NORMAL )
240 return ::com::sun::star::awt::FontWidth::NORMAL;
241 else if( eWidth == WIDTH_SEMI_EXPANDED )
242 return ::com::sun::star::awt::FontWidth::SEMIEXPANDED;
243 else if( eWidth == WIDTH_EXPANDED )
244 return ::com::sun::star::awt::FontWidth::EXPANDED;
245 else if( eWidth == WIDTH_EXTRA_EXPANDED )
246 return ::com::sun::star::awt::FontWidth::EXTRAEXPANDED;
247 else if( eWidth == WIDTH_ULTRA_EXPANDED )
248 return ::com::sun::star::awt::FontWidth::ULTRAEXPANDED;
250 DBG_ERROR( "Unknown FontWidth" );
251 return ::com::sun::star::awt::FontWidth::DONTKNOW;
254 FontWidth VCLUnoHelper::ConvertFontWidth( float f )
256 if( f <= ::com::sun::star::awt::FontWidth::DONTKNOW )
257 return WIDTH_DONTKNOW;
258 else if( f <= ::com::sun::star::awt::FontWidth::ULTRACONDENSED )
259 return WIDTH_ULTRA_CONDENSED;
260 else if( f <= ::com::sun::star::awt::FontWidth::EXTRACONDENSED )
261 return WIDTH_EXTRA_CONDENSED;
262 else if( f <= ::com::sun::star::awt::FontWidth::CONDENSED )
263 return WIDTH_CONDENSED;
264 else if( f <= ::com::sun::star::awt::FontWidth::SEMICONDENSED )
265 return WIDTH_SEMI_CONDENSED;
266 else if( f <= ::com::sun::star::awt::FontWidth::NORMAL )
267 return WIDTH_NORMAL;
268 else if( f <= ::com::sun::star::awt::FontWidth::SEMIEXPANDED )
269 return WIDTH_SEMI_EXPANDED;
270 else if( f <= ::com::sun::star::awt::FontWidth::EXPANDED )
271 return WIDTH_EXPANDED;
272 else if( f <= ::com::sun::star::awt::FontWidth::EXTRAEXPANDED )
273 return WIDTH_EXTRA_EXPANDED;
274 else if( f <= ::com::sun::star::awt::FontWidth::ULTRAEXPANDED )
275 return WIDTH_ULTRA_EXPANDED;
277 DBG_ERROR( "Unknown FontWidth" );
278 return WIDTH_DONTKNOW;
281 float VCLUnoHelper::ConvertFontWeight( FontWeight eWeight )
283 if( eWeight == WEIGHT_DONTKNOW )
284 return ::com::sun::star::awt::FontWeight::DONTKNOW;
285 else if( eWeight == WEIGHT_THIN )
286 return ::com::sun::star::awt::FontWeight::THIN;
287 else if( eWeight == WEIGHT_ULTRALIGHT )
288 return ::com::sun::star::awt::FontWeight::ULTRALIGHT;
289 else if( eWeight == WEIGHT_LIGHT )
290 return ::com::sun::star::awt::FontWeight::LIGHT;
291 else if( eWeight == WEIGHT_SEMILIGHT )
292 return ::com::sun::star::awt::FontWeight::SEMILIGHT;
293 else if( ( eWeight == WEIGHT_NORMAL ) || ( eWeight == WEIGHT_MEDIUM ) )
294 return ::com::sun::star::awt::FontWeight::NORMAL;
295 else if( eWeight == WEIGHT_SEMIBOLD )
296 return ::com::sun::star::awt::FontWeight::SEMIBOLD;
297 else if( eWeight == WEIGHT_BOLD )
298 return ::com::sun::star::awt::FontWeight::BOLD;
299 else if( eWeight == WEIGHT_ULTRABOLD )
300 return ::com::sun::star::awt::FontWeight::ULTRABOLD;
301 else if( eWeight == WEIGHT_BLACK )
302 return ::com::sun::star::awt::FontWeight::BLACK;
304 DBG_ERROR( "Unknown FontWeigth" );
305 return ::com::sun::star::awt::FontWeight::DONTKNOW;
308 FontWeight VCLUnoHelper::ConvertFontWeight( float f )
310 if( f <= ::com::sun::star::awt::FontWeight::DONTKNOW )
311 return WEIGHT_DONTKNOW;
312 else if( f <= ::com::sun::star::awt::FontWeight::THIN )
313 return WEIGHT_THIN;
314 else if( f <= ::com::sun::star::awt::FontWeight::ULTRALIGHT )
315 return WEIGHT_ULTRALIGHT;
316 else if( f <= ::com::sun::star::awt::FontWeight::LIGHT )
317 return WEIGHT_LIGHT;
318 else if( f <= ::com::sun::star::awt::FontWeight::SEMILIGHT )
319 return WEIGHT_SEMILIGHT;
320 else if( f <= ::com::sun::star::awt::FontWeight::NORMAL )
321 return WEIGHT_NORMAL;
322 else if( f <= ::com::sun::star::awt::FontWeight::SEMIBOLD )
323 return WEIGHT_SEMIBOLD;
324 else if( f <= ::com::sun::star::awt::FontWeight::BOLD )
325 return WEIGHT_BOLD;
326 else if( f <= ::com::sun::star::awt::FontWeight::ULTRABOLD )
327 return WEIGHT_ULTRABOLD;
328 else if( f <= ::com::sun::star::awt::FontWeight::BLACK )
329 return WEIGHT_BLACK;
331 DBG_ERROR( "Unknown FontWeigth" );
332 return WEIGHT_DONTKNOW;
336 ::com::sun::star::awt::FontDescriptor VCLUnoHelper::CreateFontDescriptor( const Font& rFont )
338 ::com::sun::star::awt::FontDescriptor aFD;
339 aFD.Name = rFont.GetName();
340 aFD.StyleName = rFont.GetStyleName();
341 aFD.Height = (sal_Int16)rFont.GetSize().Height();
342 aFD.Width = (sal_Int16)rFont.GetSize().Width();
343 aFD.Family = sal::static_int_cast< sal_Int16 >(rFont.GetFamily());
344 aFD.CharSet = rFont.GetCharSet();
345 aFD.Pitch = sal::static_int_cast< sal_Int16 >(rFont.GetPitch());
346 aFD.CharacterWidth = VCLUnoHelper::ConvertFontWidth( rFont.GetWidthType() );
347 aFD.Weight= VCLUnoHelper::ConvertFontWeight( rFont.GetWeight() );
348 aFD.Slant = (::com::sun::star::awt::FontSlant)rFont.GetItalic();
349 aFD.Underline = sal::static_int_cast< sal_Int16 >(rFont.GetUnderline());
350 aFD.Strikeout = sal::static_int_cast< sal_Int16 >(rFont.GetStrikeout());
351 aFD.Orientation = rFont.GetOrientation();
352 aFD.Kerning = rFont.IsKerning();
353 aFD.WordLineMode = rFont.IsWordLineMode();
354 aFD.Type = 0; // ??? => Nur an Metric...
355 return aFD;
358 Font VCLUnoHelper::CreateFont( const ::com::sun::star::awt::FontDescriptor& rDescr, const Font& rInitFont )
360 Font aFont( rInitFont );
361 if ( rDescr.Name.getLength() )
362 aFont.SetName( rDescr.Name );
363 if ( rDescr.StyleName.getLength() )
364 aFont.SetStyleName( rDescr.StyleName );
365 if ( rDescr.Height )
366 aFont.SetSize( Size( rDescr.Width, rDescr.Height ) );
367 if ( (FontFamily)rDescr.Family != FAMILY_DONTKNOW )
368 aFont.SetFamily( (FontFamily)rDescr.Family );
369 if ( (CharSet)rDescr.CharSet != RTL_TEXTENCODING_DONTKNOW )
370 aFont.SetCharSet( (CharSet)rDescr.CharSet );
371 if ( (FontPitch)rDescr.Pitch != PITCH_DONTKNOW )
372 aFont.SetPitch( (FontPitch)rDescr.Pitch );
373 if ( rDescr.CharacterWidth )
374 aFont.SetWidthType( VCLUnoHelper::ConvertFontWidth( rDescr.CharacterWidth ) );
375 if ( rDescr.Weight )
376 aFont.SetWeight( VCLUnoHelper::ConvertFontWeight( rDescr.Weight ) );
377 if ( (FontItalic)rDescr.Slant != ITALIC_DONTKNOW )
378 aFont.SetItalic( (FontItalic)rDescr.Slant );
379 if ( (FontUnderline)rDescr.Underline != UNDERLINE_DONTKNOW )
380 aFont.SetUnderline( (FontUnderline)rDescr.Underline );
381 if ( (FontStrikeout)rDescr.Strikeout != STRIKEOUT_DONTKNOW )
382 aFont.SetStrikeout( (FontStrikeout)rDescr.Strikeout );
384 // Kein DONTKNOW
385 aFont.SetOrientation( (short)rDescr.Orientation );
386 aFont.SetKerning( rDescr.Kerning );
387 aFont.SetWordLineMode( rDescr.WordLineMode );
389 return aFont;
392 Font VCLUnoHelper::CreateFont( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XFont >& rxFont )
394 Font aFont;
395 VCLXFont* pVCLXFont = VCLXFont::GetImplementation( rxFont );
396 if ( pVCLXFont )
397 aFont = pVCLXFont->GetFont();
398 return aFont;
402 ::com::sun::star::awt::SimpleFontMetric VCLUnoHelper::CreateFontMetric( const FontMetric& rFontMetric )
404 ::com::sun::star::awt::SimpleFontMetric aFM;
405 aFM.Ascent = (sal_Int16)rFontMetric.GetAscent();
406 aFM.Descent = (sal_Int16)rFontMetric.GetDescent();
407 aFM.Leading = (sal_Int16)rFontMetric.GetIntLeading();
408 aFM.Slant = (sal_Int16)rFontMetric.GetSlant();
409 aFM.FirstChar = 0x0020;
410 aFM.LastChar = 0xFFFD;
411 return aFM;
414 sal_Bool VCLUnoHelper::IsZero( ::com::sun::star::awt::Rectangle rRect )
416 return ( !rRect.X && !rRect.Y && !rRect.Width && !rRect.Height );
419 MapUnit VCLUnoHelper::UnoEmbed2VCLMapUnit( sal_Int32 nUnoEmbedMapUnit )
421 switch( nUnoEmbedMapUnit )
423 case ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_MM:
424 return MAP_100TH_MM;
425 case ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_MM:
426 return MAP_10TH_MM;
427 case ::com::sun::star::embed::EmbedMapUnits::ONE_MM:
428 return MAP_MM;
429 case ::com::sun::star::embed::EmbedMapUnits::ONE_CM:
430 return MAP_CM;
431 case ::com::sun::star::embed::EmbedMapUnits::ONE_1000TH_INCH:
432 return MAP_1000TH_INCH;
433 case ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_INCH:
434 return MAP_100TH_INCH;
435 case ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_INCH:
436 return MAP_10TH_INCH;
437 case ::com::sun::star::embed::EmbedMapUnits::ONE_INCH:
438 return MAP_INCH;
439 case ::com::sun::star::embed::EmbedMapUnits::POINT:
440 return MAP_POINT;
441 case ::com::sun::star::embed::EmbedMapUnits::TWIP:
442 return MAP_TWIP;
443 case ::com::sun::star::embed::EmbedMapUnits::PIXEL:
444 return MAP_PIXEL;
447 OSL_ENSURE( sal_False, "Unexpected UNO map mode is provided!\n" );
448 return MAP_LASTENUMDUMMY;
451 sal_Int32 VCLUnoHelper::VCL2UnoEmbedMapUnit( MapUnit nVCLMapUnit )
453 switch( nVCLMapUnit )
455 case MAP_100TH_MM:
456 return ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_MM;
457 case MAP_10TH_MM:
458 return ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_MM;
459 case MAP_MM:
460 return ::com::sun::star::embed::EmbedMapUnits::ONE_MM;
461 case MAP_CM:
462 return ::com::sun::star::embed::EmbedMapUnits::ONE_CM;
463 case MAP_1000TH_INCH:
464 return ::com::sun::star::embed::EmbedMapUnits::ONE_1000TH_INCH;
465 case MAP_100TH_INCH:
466 return ::com::sun::star::embed::EmbedMapUnits::ONE_100TH_INCH;
467 case MAP_10TH_INCH:
468 return ::com::sun::star::embed::EmbedMapUnits::ONE_10TH_INCH;
469 case MAP_INCH:
470 return ::com::sun::star::embed::EmbedMapUnits::ONE_INCH;
471 case MAP_POINT:
472 return ::com::sun::star::embed::EmbedMapUnits::POINT;
473 case MAP_TWIP:
474 return ::com::sun::star::embed::EmbedMapUnits::TWIP;
475 case MAP_PIXEL:
476 return ::com::sun::star::embed::EmbedMapUnits::PIXEL;
477 default: ; // avoid compiler warning
480 OSL_ENSURE( sal_False, "Unexpected VCL map mode is provided!\n" );
481 return -1;
484 using namespace ::com::sun::star::util;
486 //====================================================================
487 //= file-local helpers
488 //====================================================================
489 namespace
491 enum UnitConversionDirection
493 FieldUnitToMeasurementUnit,
494 MeasurementUnitToFieldUnit
497 sal_Int16 convertMeasurementUnit( sal_Int16 _nUnit, UnitConversionDirection eDirection, sal_Int16& _rFieldToUNOValueFactor )
499 static struct _unit_table
501 FieldUnit eFieldUnit;
502 sal_Int16 nMeasurementUnit;
503 sal_Int16 nFieldToMeasureFactor;
504 } aUnits[] = {
505 { FUNIT_NONE, -1 , -1},
506 { FUNIT_MM, MeasureUnit::MM, 1 }, // must precede MM_10TH
507 { FUNIT_MM, MeasureUnit::MM_10TH, 10 },
508 { FUNIT_100TH_MM, MeasureUnit::MM_100TH, 1 },
509 { FUNIT_CM, MeasureUnit::CM, 1 },
510 { FUNIT_M, MeasureUnit::M, 1 },
511 { FUNIT_KM, MeasureUnit::KM, 1 },
512 { FUNIT_TWIP, MeasureUnit::TWIP, 1 },
513 { FUNIT_POINT, MeasureUnit::POINT, 1 },
514 { FUNIT_PICA, MeasureUnit::PICA, 1 },
515 { FUNIT_INCH, MeasureUnit::INCH, 1 }, // must precede INCH_*TH
516 { FUNIT_INCH, MeasureUnit::INCH_10TH, 10 },
517 { FUNIT_INCH, MeasureUnit::INCH_100TH, 100 },
518 { FUNIT_INCH, MeasureUnit::INCH_1000TH, 1000 },
519 { FUNIT_FOOT, MeasureUnit::FOOT, 1 },
520 { FUNIT_MILE, MeasureUnit::MILE, 1 },
522 for ( size_t i = 0; i < sizeof( aUnits ) / sizeof( aUnits[0] ); ++i )
524 if ( eDirection == FieldUnitToMeasurementUnit )
526 if ( ( aUnits[ i ].eFieldUnit == (FieldUnit)_nUnit ) && ( aUnits[ i ].nFieldToMeasureFactor == _rFieldToUNOValueFactor ) )
527 return aUnits[ i ].nMeasurementUnit;
529 else
531 if ( aUnits[ i ].nMeasurementUnit == _nUnit )
533 _rFieldToUNOValueFactor = aUnits[ i ].nFieldToMeasureFactor;
534 return (sal_Int16)aUnits[ i ].eFieldUnit;
538 if ( eDirection == FieldUnitToMeasurementUnit )
539 return -1;
541 _rFieldToUNOValueFactor = 1;
542 return (sal_Int16)FUNIT_NONE;
545 //========================================================================
546 //= MeasurementUnitConversion
547 //========================================================================
548 //------------------------------------------------------------------------
549 sal_Int16 VCLUnoHelper::ConvertToMeasurementUnit( FieldUnit _nFieldUnit, sal_Int16 _nUNOToFieldValueFactor )
551 return convertMeasurementUnit( (sal_Int16)_nFieldUnit, FieldUnitToMeasurementUnit, _nUNOToFieldValueFactor );
554 //------------------------------------------------------------------------
555 FieldUnit VCLUnoHelper::ConvertToFieldUnit( sal_Int16 _nMeasurementUnit, sal_Int16& _rFieldToUNOValueFactor )
557 return (FieldUnit)convertMeasurementUnit( _nMeasurementUnit, MeasurementUnitToFieldUnit, _rFieldToUNOValueFactor );
561 MapUnit /* MapModeUnit */ VCLUnoHelper::ConvertToMapModeUnit(sal_Int16 /* com.sun.star.util.MeasureUnit.* */ _nMeasureUnit) throw (::com::sun::star::lang::IllegalArgumentException)
563 MapUnit eMode;
564 switch(_nMeasureUnit)
566 case com::sun::star::util::MeasureUnit::MM_100TH:
567 eMode = MAP_100TH_MM;
568 break;
571 case com::sun::star::util::MeasureUnit::MM_10TH:
572 eMode = MAP_10TH_MM;
573 break;
575 case com::sun::star::util::MeasureUnit::MM:
576 eMode = MAP_MM;
577 break;
579 case com::sun::star::util::MeasureUnit::CM:
580 eMode = MAP_CM;
581 break;
583 case com::sun::star::util::MeasureUnit::INCH_1000TH:
584 eMode = MAP_1000TH_INCH;
585 break;
587 case com::sun::star::util::MeasureUnit::INCH_100TH:
588 eMode = MAP_100TH_INCH;
589 break;
591 case com::sun::star::util::MeasureUnit::INCH_10TH:
592 eMode = MAP_10TH_INCH;
593 break;
595 case com::sun::star::util::MeasureUnit::INCH:
596 eMode = MAP_INCH;
597 break;
599 case com::sun::star::util::MeasureUnit::POINT:
600 eMode = MAP_POINT;
601 break;
603 case com::sun::star::util::MeasureUnit::TWIP:
604 eMode = MAP_TWIP;
605 break;
607 case com::sun::star::util::MeasureUnit::PIXEL:
608 eMode = MAP_PIXEL;
609 break;
612 case com::sun::star::util::MeasureUnit::M:
613 break;
614 case com::sun::star::util::MeasureUnit::KM:
615 break;
616 case com::sun::star::util::MeasureUnit::PICA:
617 break;
618 case com::sun::star::util::MeasureUnit::FOOT:
619 break;
620 case com::sun::star::util::MeasureUnit::MILE:
621 break;
622 case com::sun::star::util::MeasureUnit::PERCENT:
623 break;
625 case com::sun::star::util::MeasureUnit::APPFONT:
626 eMode = MAP_APPFONT;
627 break;
629 case com::sun::star::util::MeasureUnit::SYSFONT:
630 eMode = MAP_SYSFONT;
631 break;
634 case com::sun::star::util::MeasureUnit::RELATIVE:
635 eMode = MAP_RELATIVE;
636 break;
637 case com::sun::star::util::MeasureUnit::REALAPPFONT:
638 eMode = MAP_REALAPPFONT;
639 break;
642 default:
643 throw ::com::sun::star::lang::IllegalArgumentException(::rtl::OUString::createFromAscii("Unsupported measure unit."), NULL, 1 );
645 return eMode;
648 sal_Int16 /* com.sun.star.util.MeasureUnit.* */ VCLUnoHelper::ConvertToMeasurementUnit(MapUnit /* MapModeUnit */ _eMapModeUnit) throw (::com::sun::star::lang::IllegalArgumentException)
650 sal_Int16 nMeasureUnit = 0;
651 switch (_eMapModeUnit)
653 case MAP_100TH_MM:
654 nMeasureUnit = com::sun::star::util::MeasureUnit::MM_100TH;
655 break;
657 case MAP_10TH_MM:
658 nMeasureUnit = com::sun::star::util::MeasureUnit::MM_10TH;
659 break;
661 case MAP_MM:
662 nMeasureUnit = com::sun::star::util::MeasureUnit::MM;
663 break;
665 case MAP_CM:
666 nMeasureUnit = com::sun::star::util::MeasureUnit::CM;
667 break;
669 case MAP_1000TH_INCH:
670 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH_1000TH;
671 break;
673 case MAP_100TH_INCH:
674 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH_100TH;
675 break;
677 case MAP_10TH_INCH:
678 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH_10TH;
679 break;
681 case MAP_INCH:
682 nMeasureUnit = com::sun::star::util::MeasureUnit::INCH;
683 break;
685 case MAP_POINT:
686 nMeasureUnit = com::sun::star::util::MeasureUnit::POINT;
687 break;
689 case MAP_TWIP:
690 nMeasureUnit = com::sun::star::util::MeasureUnit::TWIP;
691 break;
693 case MAP_PIXEL:
694 nMeasureUnit = com::sun::star::util::MeasureUnit::PIXEL;
695 break;
697 case MAP_APPFONT:
698 nMeasureUnit = com::sun::star::util::MeasureUnit::APPFONT;
699 break;
701 case MAP_SYSFONT:
702 nMeasureUnit = com::sun::star::util::MeasureUnit::SYSFONT;
703 break;
706 case MAP_RELATIVE:
707 break;
709 case MAP_REALAPPFONT:
710 break;
712 default:
713 throw ::com::sun::star::lang::IllegalArgumentException(::rtl::OUString::createFromAscii("Unsupported MapMode unit."), NULL, 1 );
715 return nMeasureUnit;
718 ::Size VCLUnoHelper::ConvertToVCLSize(com::sun::star::awt::Size const& _aSize)
720 ::Size aVCLSize(_aSize.Width, _aSize.Height);
721 return aVCLSize;
724 com::sun::star::awt::Size VCLUnoHelper::ConvertToAWTSize(::Size /* VCLSize */ const& _aSize)
726 com::sun::star::awt::Size aAWTSize(_aSize.Width(), _aSize.Height());
727 return aAWTSize;
731 ::Point VCLUnoHelper::ConvertToVCLPoint(com::sun::star::awt::Point const& _aPoint)
733 ::Point aVCLPoint(_aPoint.X, _aPoint.Y);
734 return aVCLPoint;
737 com::sun::star::awt::Point VCLUnoHelper::ConvertToAWTPoint(::Point /* VCLPoint */ const& _aPoint)
739 com::sun::star::awt::Point aAWTPoint(_aPoint.X(), _aPoint.Y());
740 return aAWTPoint;
743 ::Rectangle VCLUnoHelper::ConvertToVCLRect( ::com::sun::star::awt::Rectangle const & _rRect )
745 return ::Rectangle( _rRect.X, _rRect.Y, _rRect.X + _rRect.Width - 1, _rRect.Y + _rRect.Height - 1 );
748 ::com::sun::star::awt::Rectangle VCLUnoHelper::ConvertToAWTRect( ::Rectangle const & _rRect )
750 return ::com::sun::star::awt::Rectangle( _rRect.Left(), _rRect.Top(), _rRect.GetWidth(), _rRect.GetHeight() );
753 awt::MouseEvent VCLUnoHelper::createMouseEvent( const ::MouseEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext )
755 awt::MouseEvent aMouseEvent;
756 aMouseEvent.Source = _rxContext;
758 aMouseEvent.Modifiers = 0;
759 if ( _rVclEvent.IsShift() )
760 aMouseEvent.Modifiers |= ::com::sun::star::awt::KeyModifier::SHIFT;
761 if ( _rVclEvent.IsMod1() )
762 aMouseEvent.Modifiers |= ::com::sun::star::awt::KeyModifier::MOD1;
763 if ( _rVclEvent.IsMod2() )
764 aMouseEvent.Modifiers |= ::com::sun::star::awt::KeyModifier::MOD2;
766 aMouseEvent.Buttons = 0;
767 if ( _rVclEvent.IsLeft() )
768 aMouseEvent.Buttons |= ::com::sun::star::awt::MouseButton::LEFT;
769 if ( _rVclEvent.IsRight() )
770 aMouseEvent.Buttons |= ::com::sun::star::awt::MouseButton::RIGHT;
771 if ( _rVclEvent.IsMiddle() )
772 aMouseEvent.Buttons |= ::com::sun::star::awt::MouseButton::MIDDLE;
774 aMouseEvent.X = _rVclEvent.GetPosPixel().X();
775 aMouseEvent.Y = _rVclEvent.GetPosPixel().Y();
776 aMouseEvent.ClickCount = _rVclEvent.GetClicks();
777 aMouseEvent.PopupTrigger = sal_False;
779 return aMouseEvent;
782 awt::KeyEvent VCLUnoHelper::createKeyEvent( const ::KeyEvent& _rVclEvent, const uno::Reference< uno::XInterface >& _rxContext )
784 awt::KeyEvent aKeyEvent;
785 aKeyEvent.Source = _rxContext;
787 aKeyEvent.Modifiers = 0;
788 if ( _rVclEvent.GetKeyCode().IsShift() )
789 aKeyEvent.Modifiers |= awt::KeyModifier::SHIFT;
790 if ( _rVclEvent.GetKeyCode().IsMod1() )
791 aKeyEvent.Modifiers |= awt::KeyModifier::MOD1;
792 if ( _rVclEvent.GetKeyCode().IsMod2() )
793 aKeyEvent.Modifiers |= awt::KeyModifier::MOD2;
794 if ( _rVclEvent.GetKeyCode().IsMod3() )
795 aKeyEvent.Modifiers |= awt::KeyModifier::MOD3;
797 aKeyEvent.KeyCode = _rVclEvent.GetKeyCode().GetCode();
798 aKeyEvent.KeyChar = _rVclEvent.GetCharCode();
799 aKeyEvent.KeyFunc = ::sal::static_int_cast< sal_Int16 >( _rVclEvent.GetKeyCode().GetFunction());
801 return aKeyEvent;