bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / form / fmcontrollayout.cxx
blob5acd501a4fda16d4bcc3ebac42f7cc91f40016b1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <fmcontrollayout.hxx>
22 #include <fmprop.hxx>
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>
33 #include <comphelper/processfactory.hxx>
34 #include <i18nlangtag/mslangid.hxx>
35 #include <i18nlangtag/languagetag.hxx>
36 #include <unotools/confignode.hxx>
37 #include <unotools/syslocale.hxx>
38 #include <unotools/localedatawrapper.hxx>
40 #include <toolkit/helper/vclunohelper.hxx>
41 #include <tools/debug.hxx>
42 #include <tools/diagnose_ex.h>
43 #include <vcl/outdev.hxx>
46 namespace svxform
50 using namespace ::utl;
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::XInterface;
53 using ::com::sun::star::uno::UNO_QUERY;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
55 using ::com::sun::star::uno::UNO_SET_THROW;
56 using ::com::sun::star::uno::Exception;
57 using ::com::sun::star::uno::RuntimeException;
58 using ::com::sun::star::uno::Any;
59 using ::com::sun::star::uno::makeAny;
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;
69 namespace FormComponentType = ::com::sun::star::form::FormComponentType;
70 namespace VisualEffect = ::com::sun::star::awt::VisualEffect;
71 namespace ScriptType = ::com::sun::star::i18n::ScriptType;
74 namespace
76 ::utl::OConfigurationNode getLayoutSettings( DocumentType _eDocType )
78 OUString sConfigName = "/org.openoffice.Office.Common/Forms/ControlLayout/" +
79 DocumentClassification::getModuleIdentifierForDocumentType( _eDocType );
80 return OConfigurationTreeRoot::createWithComponentContext(
81 ::comphelper::getProcessComponentContext(), // TODO
82 sConfigName );
85 template< class INTERFACE_TYPE >
86 Reference< INTERFACE_TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
88 Reference< INTERFACE_TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
89 if ( xTypedNode.is() )
90 return xTypedNode;
91 else
93 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
94 if ( xChild.is() )
95 return getTypedModelNode< INTERFACE_TYPE >( xChild->getParent() );
96 else
97 return nullptr;
102 bool lcl_getDocumentDefaultStyleAndFamily( const Reference< XInterface >& _rxDocument, OUString& _rFamilyName, OUString& _rStyleName )
104 bool bSuccess = true;
105 Reference< XServiceInfo > xDocumentSI( _rxDocument, UNO_QUERY );
106 if ( xDocumentSI.is() )
108 if ( xDocumentSI->supportsService("com.sun.star.text.TextDocument")
109 || xDocumentSI->supportsService("com.sun.star.text.WebDocument")
112 _rFamilyName = "ParagraphStyles";
113 _rStyleName = "Standard";
115 else if ( xDocumentSI->supportsService("com.sun.star.sheet.SpreadsheetDocument") )
117 _rFamilyName = "CellStyles";
118 _rStyleName = "Default";
120 else if ( xDocumentSI->supportsService("com.sun.star.drawing.DrawingDocument")
121 || xDocumentSI->supportsService("com.sun.star.presentation.PresentationDocument")
124 _rFamilyName = "graphics";
125 _rStyleName = "standard";
127 else
128 bSuccess = false;
130 return bSuccess;
134 void lcl_initializeControlFont( const Reference< XPropertySet >& _rxModel )
138 Reference< XPropertySet > xStyle( ControlLayouter::getDefaultDocumentTextStyle( _rxModel ), UNO_SET_THROW );
139 Reference< XPropertySetInfo > xStylePSI( xStyle->getPropertySetInfo(), UNO_SET_THROW );
141 // determine the script type associated with the system locale
142 const SvtSysLocale aSysLocale;
143 const LocaleDataWrapper& rSysLocaleData = aSysLocale.GetLocaleData();
144 const sal_Int16 eSysLocaleScriptType = MsLangId::getScriptType( rSysLocaleData.getLanguageTag().getLanguageType() );
146 // depending on this script type, use the right property from the document's style which controls the
147 // default locale for document content
148 const char* pCharLocalePropertyName = "CharLocale";
149 switch ( eSysLocaleScriptType )
151 case ScriptType::LATIN:
152 // already defaulted above
153 break;
154 case ScriptType::ASIAN:
155 pCharLocalePropertyName = "CharLocaleAsian";
156 break;
157 case ScriptType::COMPLEX:
158 pCharLocalePropertyName = "CharLocaleComplex";
159 break;
160 default:
161 OSL_FAIL( "lcl_initializeControlFont: unexpected script type for system locale!" );
162 break;
165 OUString sCharLocalePropertyName = OUString::createFromAscii( pCharLocalePropertyName );
166 Locale aDocumentCharLocale;
167 if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
169 OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
171 // fall back to CharLocale property at the style
172 if ( aDocumentCharLocale.Language.isEmpty() )
174 sCharLocalePropertyName = "CharLocale";
175 if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
177 OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
180 // fall back to the system locale
181 if ( aDocumentCharLocale.Language.isEmpty() )
183 aDocumentCharLocale = rSysLocaleData.getLanguageTag().getLocale();
186 // retrieve a default font for this locale, and set it at the control
187 vcl::Font aFont = OutputDevice::GetDefaultFont( DefaultFontType::SANS, LanguageTag::convertToLanguageType( aDocumentCharLocale ), GetDefaultFontFlags::OnlyOne );
188 FontDescriptor aFontDesc = VCLUnoHelper::CreateFontDescriptor( aFont );
189 _rxModel->setPropertyValue("FontDescriptor", makeAny( aFontDesc )
192 catch( const Exception& )
194 DBG_UNHANDLED_EXCEPTION("svx");
200 //= ControlLayouter
203 Reference< XPropertySet > ControlLayouter::getDefaultDocumentTextStyle( const Reference< XPropertySet >& _rxModel )
205 // the style family collection
206 Reference< XStyleFamiliesSupplier > xSuppStyleFamilies( getTypedModelNode< XStyleFamiliesSupplier >( _rxModel ), UNO_SET_THROW );
207 Reference< XNameAccess > xStyleFamilies( xSuppStyleFamilies->getStyleFamilies(), UNO_SET_THROW );
209 // the names of the family, and the style - depends on the document type we live in
210 OUString sFamilyName, sStyleName;
211 if ( !lcl_getDocumentDefaultStyleAndFamily( xSuppStyleFamilies, sFamilyName, sStyleName ) )
212 throw RuntimeException("unknown document type!");
214 // the concrete style
215 Reference< XNameAccess > xStyleFamily( xStyleFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
216 return Reference< XPropertySet >( xStyleFamily->getByName( sStyleName ), UNO_QUERY_THROW );
220 void ControlLayouter::initializeControlLayout( const Reference< XPropertySet >& _rxControlModel, DocumentType _eDocType )
222 DBG_ASSERT( _rxControlModel.is(), "ControlLayouter::initializeControlLayout: invalid model!" );
223 if ( !_rxControlModel.is() )
224 return;
228 Reference< XPropertySetInfo > xPSI( _rxControlModel->getPropertySetInfo(), UNO_SET_THROW );
230 // the control type
231 sal_Int16 nClassId = FormComponentType::CONTROL;
232 _rxControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
234 // the document type
235 if ( _eDocType == eUnknownDocumentType )
236 _eDocType = DocumentClassification::classifyHostDocument( _rxControlModel );
238 // let's see what the configuration says about the visual effect
239 OConfigurationNode aConfig = getLayoutSettings( _eDocType );
240 Any aVisualEffect = aConfig.getNodeValue( OUString( "VisualEffect" ) );
241 if ( aVisualEffect.hasValue() )
243 OUString sVisualEffect;
244 OSL_VERIFY( aVisualEffect >>= sVisualEffect );
246 sal_Int16 nVisualEffect = VisualEffect::NONE;
247 if ( sVisualEffect == "flat" )
248 nVisualEffect = VisualEffect::FLAT;
249 else if ( sVisualEffect == "3D" )
250 nVisualEffect = VisualEffect::LOOK3D;
252 if ( xPSI->hasPropertyByName( FM_PROP_BORDER ) )
254 if ( ( nClassId != FormComponentType::COMMANDBUTTON )
255 && ( nClassId != FormComponentType::RADIOBUTTON )
256 && ( nClassId != FormComponentType::CHECKBOX )
257 && ( nClassId != FormComponentType::GROUPBOX )
258 && ( nClassId != FormComponentType::FIXEDTEXT )
259 && ( nClassId != FormComponentType::SCROLLBAR )
260 && ( nClassId != FormComponentType::SPINBUTTON )
263 _rxControlModel->setPropertyValue( FM_PROP_BORDER, makeAny( nVisualEffect ) );
264 if ( ( nVisualEffect == VisualEffect::FLAT )
265 && ( xPSI->hasPropertyByName( FM_PROP_BORDERCOLOR ) )
267 // light gray flat border
268 _rxControlModel->setPropertyValue( FM_PROP_BORDERCOLOR, makeAny( sal_Int32(0x00C0C0C0) ) );
271 if ( xPSI->hasPropertyByName( FM_PROP_VISUALEFFECT ) )
272 _rxControlModel->setPropertyValue( FM_PROP_VISUALEFFECT, makeAny( nVisualEffect ) );
275 // the font (only if we use the document's ref devices for rendering control text, otherwise, the
276 // default font of VCL controls is assumed to be fine)
277 if ( useDocumentReferenceDevice( _eDocType )
278 && xPSI->hasPropertyByName( FM_PROP_FONT )
280 lcl_initializeControlFont( _rxControlModel );
282 catch( const Exception& )
284 TOOLS_WARN_EXCEPTION( "svx", "ControlLayouter::initializeControlLayout" );
288 bool ControlLayouter::useDynamicBorderColor( DocumentType _eDocType )
290 OConfigurationNode aConfig = getLayoutSettings( _eDocType );
291 Any aDynamicBorderColor = aConfig.getNodeValue( OUString( "DynamicBorderColors" ) );
292 bool bDynamicBorderColor = false;
293 OSL_VERIFY( aDynamicBorderColor >>= bDynamicBorderColor );
294 return bDynamicBorderColor;
298 bool ControlLayouter::useDocumentReferenceDevice( DocumentType _eDocType )
300 if ( _eDocType == eUnknownDocumentType )
301 return false;
302 OConfigurationNode aConfig = getLayoutSettings( _eDocType );
303 Any aUseRefDevice = aConfig.getNodeValue( OUString( "UseDocumentTextMetrics" ) );
304 bool bUseRefDevice = false;
305 OSL_VERIFY( aUseRefDevice >>= bUseRefDevice );
306 return bUseRefDevice;
313 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */