LCOV - code coverage report
Current view: top level - svx/source/form - fmcontrollayout.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 19 119 16.0 %
Date: 2014-11-03 Functions: 5 10 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "fmcontrollayout.hxx"
      22             : #include "fmprop.hrc"
      23             : 
      24             : #include <com/sun/star/form/FormComponentType.hpp>
      25             : #include <com/sun/star/awt/VisualEffect.hpp>
      26             : #include <com/sun/star/i18n/ScriptType.hpp>
      27             : #include <com/sun/star/lang/Locale.hpp>
      28             : #include <com/sun/star/awt/FontDescriptor.hpp>
      29             : #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
      30             : #include <com/sun/star/lang/XServiceInfo.hpp>
      31             : #include <com/sun/star/container/XChild.hpp>
      32             : 
      33             : #include <comphelper/processfactory.hxx>
      34             : #include <i18nlangtag/mslangid.hxx>
      35             : #include <i18nlangtag/languagetag.hxx>
      36             : #include <unotools/syslocale.hxx>
      37             : 
      38             : #include <toolkit/helper/vclunohelper.hxx>
      39             : #include <tools/debug.hxx>
      40             : #include <tools/diagnose_ex.h>
      41             : #include <vcl/outdev.hxx>
      42             : 
      43             : 
      44             : namespace svxform
      45             : {
      46             : 
      47             : 
      48             :     using namespace ::utl;
      49             :     using ::com::sun::star::uno::Reference;
      50             :     using ::com::sun::star::uno::XInterface;
      51             :     using ::com::sun::star::uno::UNO_QUERY;
      52             :     using ::com::sun::star::uno::UNO_QUERY_THROW;
      53             :     using ::com::sun::star::uno::UNO_SET_THROW;
      54             :     using ::com::sun::star::uno::Exception;
      55             :     using ::com::sun::star::uno::RuntimeException;
      56             :     using ::com::sun::star::uno::Any;
      57             :     using ::com::sun::star::uno::makeAny;
      58             :     using ::com::sun::star::uno::Sequence;
      59             :     using ::com::sun::star::uno::Type;
      60             :     using ::com::sun::star::beans::XPropertySet;
      61             :     using ::com::sun::star::beans::XPropertySetInfo;
      62             :     using ::com::sun::star::lang::Locale;
      63             :     using ::com::sun::star::awt::FontDescriptor;
      64             :     using ::com::sun::star::style::XStyleFamiliesSupplier;
      65             :     using ::com::sun::star::lang::XServiceInfo;
      66             :     using ::com::sun::star::container::XNameAccess;
      67             :     using ::com::sun::star::container::XChild;
      68             : 
      69             :     namespace FormComponentType = ::com::sun::star::form::FormComponentType;
      70             :     namespace VisualEffect = ::com::sun::star::awt::VisualEffect;
      71             :     namespace ScriptType = ::com::sun::star::i18n::ScriptType;
      72             : 
      73             : 
      74             :     namespace
      75             :     {
      76             : 
      77             :         template< class INTERFACE_TYPE >
      78           0 :         Reference< INTERFACE_TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
      79             :         {
      80           0 :             Reference< INTERFACE_TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
      81           0 :             if ( xTypedNode.is() )
      82           0 :                 return xTypedNode;
      83             :             else
      84             :             {
      85           0 :                 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
      86           0 :                 if ( xChild.is() )
      87           0 :                     return getTypedModelNode< INTERFACE_TYPE >( xChild->getParent() );
      88             :                 else
      89           0 :                     return NULL;
      90           0 :             }
      91             :         }
      92             : 
      93             : 
      94           0 :         static bool lcl_getDocumentDefaultStyleAndFamily( const Reference< XInterface >& _rxDocument, OUString& _rFamilyName, OUString& _rStyleName )
      95             :         {
      96           0 :             bool bSuccess = true;
      97           0 :             Reference< XServiceInfo > xDocumentSI( _rxDocument, UNO_QUERY );
      98           0 :             if ( xDocumentSI.is() )
      99             :             {
     100           0 :                 if (  xDocumentSI->supportsService("com.sun.star.text.TextDocument")
     101           0 :                    || xDocumentSI->supportsService("com.sun.star.text.WebDocument")
     102             :                    )
     103             :                 {
     104           0 :                     _rFamilyName = "ParagraphStyles";
     105           0 :                     _rStyleName = "Standard";
     106             :                 }
     107           0 :                 else if ( xDocumentSI->supportsService("com.sun.star.sheet.SpreadsheetDocument") )
     108             :                 {
     109           0 :                     _rFamilyName = "CellStyles";
     110           0 :                     _rStyleName = "Default";
     111             :                 }
     112           0 :                 else if (  xDocumentSI->supportsService("com.sun.star.drawing.DrawingDocument")
     113           0 :                         || xDocumentSI->supportsService("com.sun.star.presentation.PresentationDocument")
     114             :                         )
     115             :                 {
     116           0 :                     _rFamilyName = "graphics";
     117           0 :                     _rStyleName = "standard";
     118             :                 }
     119             :                 else
     120           0 :                     bSuccess = false;
     121             :             }
     122           0 :             return bSuccess;
     123             :         }
     124             : 
     125             : 
     126           0 :         static void lcl_initializeControlFont( const Reference< XPropertySet >& _rxModel )
     127             :         {
     128             :             try
     129             :             {
     130           0 :                 Reference< XPropertySet > xStyle( ControlLayouter::getDefaultDocumentTextStyle( _rxModel ), UNO_SET_THROW );
     131           0 :                 Reference< XPropertySetInfo > xStylePSI( xStyle->getPropertySetInfo(), UNO_SET_THROW );
     132             : 
     133             :                 // determine the script type associated with the system locale
     134           0 :                 const SvtSysLocale aSysLocale;
     135           0 :                 const LocaleDataWrapper& rSysLocaleData = aSysLocale.GetLocaleData();
     136           0 :                 const sal_Int16 eSysLocaleScriptType = MsLangId::getScriptType( rSysLocaleData.getLanguageTag().getLanguageType() );
     137             : 
     138             :                 // depending on this script type, use the right property from the document's style which controls the
     139             :                 // default locale for document content
     140           0 :                 const sal_Char* pCharLocalePropertyName = "CharLocale";
     141           0 :                 switch ( eSysLocaleScriptType )
     142             :                 {
     143             :                 case ScriptType::LATIN:
     144             :                     // already defaulted above
     145           0 :                     break;
     146             :                 case ScriptType::ASIAN:
     147           0 :                     pCharLocalePropertyName = "CharLocaleAsian";
     148           0 :                     break;
     149             :                 case ScriptType::COMPLEX:
     150           0 :                     pCharLocalePropertyName = "CharLocaleComplex";
     151           0 :                     break;
     152             :                 default:
     153             :                     OSL_FAIL( "lcl_initializeControlFont: unexpected script type for system locale!" );
     154           0 :                     break;
     155             :                 }
     156             : 
     157           0 :                 OUString sCharLocalePropertyName = OUString::createFromAscii( pCharLocalePropertyName );
     158           0 :                 Locale aDocumentCharLocale;
     159           0 :                 if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
     160             :                 {
     161           0 :                     OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
     162             :                 }
     163             :                 // fall back to CharLocale property at the style
     164           0 :                 if ( aDocumentCharLocale.Language.isEmpty() )
     165             :                 {
     166           0 :                     sCharLocalePropertyName = "CharLocale";
     167           0 :                     if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
     168             :                     {
     169           0 :                         OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
     170             :                     }
     171             :                 }
     172             :                 // fall back to the system locale
     173           0 :                 if ( aDocumentCharLocale.Language.isEmpty() )
     174             :                 {
     175           0 :                     aDocumentCharLocale = rSysLocaleData.getLanguageTag().getLocale();
     176             :                 }
     177             : 
     178             :                 // retrieve a default font for this locale, and set it at the control
     179           0 :                 vcl::Font aFont = OutputDevice::GetDefaultFont( DEFAULTFONT_SANS, LanguageTag::convertToLanguageType( aDocumentCharLocale ), DEFAULTFONT_FLAGS_ONLYONE );
     180           0 :                 FontDescriptor aFontDesc = VCLUnoHelper::CreateFontDescriptor( aFont );
     181           0 :                 _rxModel->setPropertyValue(
     182             :                     OUString( "FontDescriptor" ),
     183             :                     makeAny( aFontDesc )
     184           0 :                 );
     185             :             }
     186           0 :             catch( const Exception& )
     187             :             {
     188             :                 DBG_UNHANDLED_EXCEPTION();
     189             :             }
     190           0 :         }
     191             :     }
     192             : 
     193             : 
     194             :     //= ControlLayouter
     195             : 
     196             : 
     197           0 :     Reference< XPropertySet > ControlLayouter::getDefaultDocumentTextStyle( const Reference< XPropertySet >& _rxModel )
     198             :     {
     199             :         // the style family collection
     200           0 :         Reference< XStyleFamiliesSupplier > xSuppStyleFamilies( getTypedModelNode< XStyleFamiliesSupplier >( _rxModel.get() ), UNO_SET_THROW );
     201           0 :         Reference< XNameAccess > xStyleFamilies( xSuppStyleFamilies->getStyleFamilies(), UNO_SET_THROW );
     202             : 
     203             :         // the names of the family, and the style - depends on the document type we live in
     204           0 :         OUString sFamilyName, sStyleName;
     205           0 :         if ( !lcl_getDocumentDefaultStyleAndFamily( xSuppStyleFamilies.get(), sFamilyName, sStyleName ) )
     206           0 :             throw RuntimeException("unknown document type!");
     207             : 
     208             :         // the concrete style
     209           0 :         Reference< XNameAccess > xStyleFamily( xStyleFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
     210           0 :         return Reference< XPropertySet >( xStyleFamily->getByName( sStyleName ), UNO_QUERY_THROW );
     211             :     }
     212             : 
     213             : 
     214           0 :     void ControlLayouter::initializeControlLayout( const Reference< XPropertySet >& _rxControlModel, DocumentType _eDocType )
     215             :     {
     216             :         DBG_ASSERT( _rxControlModel.is(), "ControlLayouter::initializeControlLayout: invalid model!" );
     217           0 :         if ( !_rxControlModel.is() )
     218           0 :             return;
     219             : 
     220             :         try
     221             :         {
     222           0 :             Reference< XPropertySetInfo > xPSI( _rxControlModel->getPropertySetInfo(), UNO_SET_THROW );
     223             : 
     224             :             // the control type
     225           0 :             sal_Int16 nClassId = FormComponentType::CONTROL;
     226           0 :             _rxControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
     227             : 
     228             :             // the document type
     229           0 :             if ( _eDocType == eUnknownDocumentType )
     230           0 :                 _eDocType = DocumentClassification::classifyHostDocument( _rxControlModel.get() );
     231             : 
     232             :             // let's see what the configuration says about the visual effect
     233           0 :             OConfigurationNode  aConfig = getLayoutSettings( _eDocType );
     234           0 :             Any aVisualEffect = aConfig.getNodeValue( OUString( "VisualEffect" ) );
     235           0 :             if ( aVisualEffect.hasValue() )
     236             :             {
     237           0 :                 OUString sVisualEffect;
     238           0 :                 OSL_VERIFY( aVisualEffect >>= sVisualEffect );
     239             : 
     240           0 :                 sal_Int16 nVisualEffect = VisualEffect::NONE;
     241           0 :                 if ( sVisualEffect == "flat" )
     242           0 :                     nVisualEffect = VisualEffect::FLAT;
     243           0 :                 else if ( sVisualEffect == "3D" )
     244           0 :                     nVisualEffect = VisualEffect::LOOK3D;
     245             : 
     246           0 :                 if ( xPSI->hasPropertyByName( FM_PROP_BORDER ) )
     247             :                 {
     248           0 :                     if  (  ( nClassId != FormComponentType::COMMANDBUTTON )
     249           0 :                         && ( nClassId != FormComponentType::RADIOBUTTON )
     250           0 :                         && ( nClassId != FormComponentType::CHECKBOX    )
     251           0 :                         && ( nClassId != FormComponentType::GROUPBOX )
     252           0 :                         && ( nClassId != FormComponentType::FIXEDTEXT )
     253           0 :                         && ( nClassId != FormComponentType::SCROLLBAR )
     254           0 :                         && ( nClassId != FormComponentType::SPINBUTTON )
     255             :                         )
     256             :                     {
     257           0 :                         _rxControlModel->setPropertyValue( FM_PROP_BORDER, makeAny( nVisualEffect ) );
     258           0 :                         if  (   ( nVisualEffect == VisualEffect::FLAT )
     259           0 :                             &&  ( xPSI->hasPropertyByName( FM_PROP_BORDERCOLOR ) )
     260             :                             )
     261             :                             // light gray flat border
     262           0 :                             _rxControlModel->setPropertyValue( FM_PROP_BORDERCOLOR, makeAny( (sal_Int32)0x00C0C0C0 ) );
     263             :                     }
     264             :                 }
     265           0 :                 if ( xPSI->hasPropertyByName( FM_PROP_VISUALEFFECT ) )
     266           0 :                     _rxControlModel->setPropertyValue( FM_PROP_VISUALEFFECT, makeAny( nVisualEffect ) );
     267             :             }
     268             : 
     269             :             // the font (only if we use the document's ref devices for rendering control text, otherwise, the
     270             :             // default font of VCL controls is assumed to be fine)
     271           0 :             if  (   useDocumentReferenceDevice( _eDocType )
     272           0 :                 &&  xPSI->hasPropertyByName( FM_PROP_FONT )
     273             :                 )
     274           0 :                 lcl_initializeControlFont( _rxControlModel );
     275             :         }
     276           0 :         catch( const Exception& )
     277             :         {
     278             :             OSL_FAIL( "ControlLayouter::initializeControlLayout: caught an exception!" );
     279             :         }
     280             :     }
     281             : 
     282             : 
     283         504 :     ::utl::OConfigurationNode ControlLayouter::getLayoutSettings( DocumentType _eDocType )
     284             :     {
     285         504 :         OUString sConfigName = "/org.openoffice.Office.Common/Forms/ControlLayout/";
     286         504 :         sConfigName += DocumentClassification::getModuleIdentifierForDocumentType( _eDocType );
     287             :         return OConfigurationTreeRoot::createWithComponentContext(
     288             :             ::comphelper::getProcessComponentContext(),    // TODO
     289         504 :             sConfigName );
     290             :     }
     291             : 
     292             : 
     293         148 :     bool ControlLayouter::useDynamicBorderColor( DocumentType _eDocType )
     294             :     {
     295         148 :         OConfigurationNode aConfig = getLayoutSettings( _eDocType );
     296         296 :         Any aDynamicBorderColor = aConfig.getNodeValue( OUString( "DynamicBorderColors" ) );
     297         148 :         bool bDynamicBorderColor = false;
     298         148 :         OSL_VERIFY( aDynamicBorderColor >>= bDynamicBorderColor );
     299         296 :         return bDynamicBorderColor;
     300             :     }
     301             : 
     302             : 
     303         358 :     bool ControlLayouter::useDocumentReferenceDevice( DocumentType _eDocType )
     304             :     {
     305         358 :         if ( _eDocType == eUnknownDocumentType )
     306           2 :             return false;
     307         356 :         OConfigurationNode aConfig = getLayoutSettings( _eDocType );
     308         712 :         Any aUseRefDevice = aConfig.getNodeValue( OUString( "UseDocumentTextMetrics" ) );
     309         356 :         bool bUseRefDevice = false;
     310         356 :         OSL_VERIFY( aUseRefDevice >>= bUseRefDevice );
     311         712 :         return bUseRefDevice;
     312             :     }
     313             : 
     314             : 
     315         651 : } // namespace svxform
     316             : 
     317             : 
     318             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10