Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / form / fmcontrollayout.cxx
blob2d61c9c91a4487ba78fa2b398563dcb276f3bac1
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.hrc"
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/syslocale.hxx>
38 #include <toolkit/helper/vclunohelper.hxx>
39 #include <tools/debug.hxx>
40 #include <tools/diagnose_ex.h>
41 #include <vcl/outdev.hxx>
44 namespace svxform
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;
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
77 template< class INTERFACE_TYPE >
78 Reference< INTERFACE_TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
80 Reference< INTERFACE_TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
81 if ( xTypedNode.is() )
82 return xTypedNode;
83 else
85 Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
86 if ( xChild.is() )
87 return getTypedModelNode< INTERFACE_TYPE >( xChild->getParent() );
88 else
89 return NULL;
94 static bool lcl_getDocumentDefaultStyleAndFamily( const Reference< XInterface >& _rxDocument, OUString& _rFamilyName, OUString& _rStyleName ) SAL_THROW(( Exception ))
96 bool bSuccess = true;
97 Reference< XServiceInfo > xDocumentSI( _rxDocument, UNO_QUERY );
98 if ( xDocumentSI.is() )
100 if ( xDocumentSI->supportsService("com.sun.star.text.TextDocument")
101 || xDocumentSI->supportsService("com.sun.star.text.WebDocument")
104 _rFamilyName = "ParagraphStyles";
105 _rStyleName = "Standard";
107 else if ( xDocumentSI->supportsService("com.sun.star.sheet.SpreadsheetDocument") )
109 _rFamilyName = "CellStyles";
110 _rStyleName = "Default";
112 else if ( xDocumentSI->supportsService("com.sun.star.drawing.DrawingDocument")
113 || xDocumentSI->supportsService("com.sun.star.presentation.PresentationDocument")
116 _rFamilyName = "graphics";
117 _rStyleName = "standard";
119 else
120 bSuccess = false;
122 return bSuccess;
126 static void lcl_initializeControlFont( const Reference< XPropertySet >& _rxModel )
130 Reference< XPropertySet > xStyle( ControlLayouter::getDefaultDocumentTextStyle( _rxModel ), UNO_SET_THROW );
131 Reference< XPropertySetInfo > xStylePSI( xStyle->getPropertySetInfo(), UNO_SET_THROW );
133 // determine the script type associated with the system locale
134 const SvtSysLocale aSysLocale;
135 const LocaleDataWrapper& rSysLocaleData = aSysLocale.GetLocaleData();
136 const sal_Int16 eSysLocaleScriptType = MsLangId::getScriptType( rSysLocaleData.getLanguageTag().getLanguageType() );
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 const sal_Char* pCharLocalePropertyName = "CharLocale";
141 switch ( eSysLocaleScriptType )
143 case ScriptType::LATIN:
144 // already defaulted above
145 break;
146 case ScriptType::ASIAN:
147 pCharLocalePropertyName = "CharLocaleAsian";
148 break;
149 case ScriptType::COMPLEX:
150 pCharLocalePropertyName = "CharLocaleComplex";
151 break;
152 default:
153 OSL_FAIL( "lcl_initializeControlFont: unexpected script type for system locale!" );
154 break;
157 OUString sCharLocalePropertyName = OUString::createFromAscii( pCharLocalePropertyName );
158 Locale aDocumentCharLocale;
159 if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
161 OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
163 // fall back to CharLocale property at the style
164 if ( aDocumentCharLocale.Language.isEmpty() )
166 sCharLocalePropertyName = "CharLocale";
167 if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
169 OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
172 // fall back to the system locale
173 if ( aDocumentCharLocale.Language.isEmpty() )
175 aDocumentCharLocale = rSysLocaleData.getLanguageTag().getLocale();
178 // retrieve a default font for this locale, and set it at the control
179 Font aFont = OutputDevice::GetDefaultFont( DEFAULTFONT_SANS, LanguageTag::convertToLanguageType( aDocumentCharLocale ), DEFAULTFONT_FLAGS_ONLYONE );
180 FontDescriptor aFontDesc = VCLUnoHelper::CreateFontDescriptor( aFont );
181 _rxModel->setPropertyValue(
182 OUString( "FontDescriptor" ),
183 makeAny( aFontDesc )
186 catch( const Exception& )
188 DBG_UNHANDLED_EXCEPTION();
194 //= ControlLayouter
197 Reference< XPropertySet > ControlLayouter::getDefaultDocumentTextStyle( const Reference< XPropertySet >& _rxModel )
199 // the style family collection
200 Reference< XStyleFamiliesSupplier > xSuppStyleFamilies( getTypedModelNode< XStyleFamiliesSupplier >( _rxModel.get() ), UNO_SET_THROW );
201 Reference< XNameAccess > xStyleFamilies( xSuppStyleFamilies->getStyleFamilies(), UNO_SET_THROW );
203 // the names of the family, and the style - depends on the document type we live in
204 OUString sFamilyName, sStyleName;
205 if ( !lcl_getDocumentDefaultStyleAndFamily( xSuppStyleFamilies.get(), sFamilyName, sStyleName ) )
206 throw RuntimeException("unknown document type!", NULL );
208 // the concrete style
209 Reference< XNameAccess > xStyleFamily( xStyleFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
210 return Reference< XPropertySet >( xStyleFamily->getByName( sStyleName ), UNO_QUERY_THROW );
214 void ControlLayouter::initializeControlLayout( const Reference< XPropertySet >& _rxControlModel, DocumentType _eDocType )
216 DBG_ASSERT( _rxControlModel.is(), "ControlLayouter::initializeControlLayout: invalid model!" );
217 if ( !_rxControlModel.is() )
218 return;
222 Reference< XPropertySetInfo > xPSI( _rxControlModel->getPropertySetInfo(), UNO_SET_THROW );
224 // the control type
225 sal_Int16 nClassId = FormComponentType::CONTROL;
226 _rxControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
228 // the document type
229 if ( _eDocType == eUnknownDocumentType )
230 _eDocType = DocumentClassification::classifyHostDocument( _rxControlModel.get() );
232 // let's see what the configuration says about the visual effect
233 OConfigurationNode aConfig = getLayoutSettings( _eDocType );
234 Any aVisualEffect = aConfig.getNodeValue( OUString( "VisualEffect" ) );
235 if ( aVisualEffect.hasValue() )
237 OUString sVisualEffect;
238 OSL_VERIFY( aVisualEffect >>= sVisualEffect );
240 sal_Int16 nVisualEffect = VisualEffect::NONE;
241 if ( sVisualEffect == "flat" )
242 nVisualEffect = VisualEffect::FLAT;
243 else if ( sVisualEffect == "3D" )
244 nVisualEffect = VisualEffect::LOOK3D;
246 if ( xPSI->hasPropertyByName( FM_PROP_BORDER ) )
248 if ( ( nClassId != FormComponentType::COMMANDBUTTON )
249 && ( nClassId != FormComponentType::RADIOBUTTON )
250 && ( nClassId != FormComponentType::CHECKBOX )
251 && ( nClassId != FormComponentType::GROUPBOX )
252 && ( nClassId != FormComponentType::FIXEDTEXT )
253 && ( nClassId != FormComponentType::SCROLLBAR )
254 && ( nClassId != FormComponentType::SPINBUTTON )
257 _rxControlModel->setPropertyValue( FM_PROP_BORDER, makeAny( nVisualEffect ) );
258 if ( ( nVisualEffect == VisualEffect::FLAT )
259 && ( xPSI->hasPropertyByName( FM_PROP_BORDERCOLOR ) )
261 // light gray flat border
262 _rxControlModel->setPropertyValue( FM_PROP_BORDERCOLOR, makeAny( (sal_Int32)0x00C0C0C0 ) );
265 if ( xPSI->hasPropertyByName( FM_PROP_VISUALEFFECT ) )
266 _rxControlModel->setPropertyValue( FM_PROP_VISUALEFFECT, makeAny( nVisualEffect ) );
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 if ( useDocumentReferenceDevice( _eDocType )
272 && xPSI->hasPropertyByName( FM_PROP_FONT )
274 lcl_initializeControlFont( _rxControlModel );
276 catch( const Exception& )
278 OSL_FAIL( "ControlLayouter::initializeControlLayout: caught an exception!" );
283 ::utl::OConfigurationNode ControlLayouter::getLayoutSettings( DocumentType _eDocType )
285 OUString sConfigName = "/org.openoffice.Office.Common/Forms/ControlLayout/";
286 sConfigName += DocumentClassification::getModuleIdentifierForDocumentType( _eDocType );
287 return OConfigurationTreeRoot::createWithComponentContext(
288 ::comphelper::getProcessComponentContext(), // TODO
289 sConfigName );
293 bool ControlLayouter::useDynamicBorderColor( DocumentType _eDocType )
295 OConfigurationNode aConfig = getLayoutSettings( _eDocType );
296 Any aDynamicBorderColor = aConfig.getNodeValue( OUString( "DynamicBorderColors" ) );
297 bool bDynamicBorderColor = false;
298 OSL_VERIFY( aDynamicBorderColor >>= bDynamicBorderColor );
299 return bDynamicBorderColor;
303 bool ControlLayouter::useDocumentReferenceDevice( DocumentType _eDocType )
305 if ( _eDocType == eUnknownDocumentType )
306 return false;
307 OConfigurationNode aConfig = getLayoutSettings( _eDocType );
308 Any aUseRefDevice = aConfig.getNodeValue( OUString( "UseDocumentTextMetrics" ) );
309 bool bUseRefDevice = false;
310 OSL_VERIFY( aUseRefDevice >>= bUseRefDevice );
311 return bUseRefDevice;
315 } // namespace svxform
318 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */