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