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