1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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>
37 #include <unotools/localedatawrapper.hxx>
39 #include <toolkit/helper/vclunohelper.hxx>
40 #include <tools/debug.hxx>
41 #include <tools/diagnose_ex.h>
42 #include <vcl/outdev.hxx>
49 using namespace ::utl
;
50 using ::com::sun::star::uno::Reference
;
51 using ::com::sun::star::uno::XInterface
;
52 using ::com::sun::star::uno::UNO_QUERY
;
53 using ::com::sun::star::uno::UNO_QUERY_THROW
;
54 using ::com::sun::star::uno::UNO_SET_THROW
;
55 using ::com::sun::star::uno::Exception
;
56 using ::com::sun::star::uno::RuntimeException
;
57 using ::com::sun::star::uno::Any
;
58 using ::com::sun::star::uno::makeAny
;
59 using ::com::sun::star::beans::XPropertySet
;
60 using ::com::sun::star::beans::XPropertySetInfo
;
61 using ::com::sun::star::lang::Locale
;
62 using ::com::sun::star::awt::FontDescriptor
;
63 using ::com::sun::star::style::XStyleFamiliesSupplier
;
64 using ::com::sun::star::lang::XServiceInfo
;
65 using ::com::sun::star::container::XNameAccess
;
66 using ::com::sun::star::container::XChild
;
68 namespace FormComponentType
= ::com::sun::star::form::FormComponentType
;
69 namespace VisualEffect
= ::com::sun::star::awt::VisualEffect
;
70 namespace ScriptType
= ::com::sun::star::i18n::ScriptType
;
75 ::utl::OConfigurationNode
getLayoutSettings( DocumentType _eDocType
)
77 OUString sConfigName
= "/org.openoffice.Office.Common/Forms/ControlLayout/" +
78 DocumentClassification::getModuleIdentifierForDocumentType( _eDocType
);
79 return OConfigurationTreeRoot::createWithComponentContext(
80 ::comphelper::getProcessComponentContext(), // TODO
84 template< class INTERFACE_TYPE
>
85 Reference
< INTERFACE_TYPE
> getTypedModelNode( const Reference
< XInterface
>& _rxModelNode
)
87 Reference
< INTERFACE_TYPE
> xTypedNode( _rxModelNode
, UNO_QUERY
);
88 if ( xTypedNode
.is() )
92 Reference
< XChild
> xChild( _rxModelNode
, UNO_QUERY
);
94 return getTypedModelNode
< INTERFACE_TYPE
>( xChild
->getParent() );
101 bool lcl_getDocumentDefaultStyleAndFamily( const Reference
< XInterface
>& _rxDocument
, OUString
& _rFamilyName
, OUString
& _rStyleName
)
103 bool bSuccess
= true;
104 Reference
< XServiceInfo
> xDocumentSI( _rxDocument
, UNO_QUERY
);
105 if ( xDocumentSI
.is() )
107 if ( xDocumentSI
->supportsService("com.sun.star.text.TextDocument")
108 || xDocumentSI
->supportsService("com.sun.star.text.WebDocument")
111 _rFamilyName
= "ParagraphStyles";
112 _rStyleName
= "Standard";
114 else if ( xDocumentSI
->supportsService("com.sun.star.sheet.SpreadsheetDocument") )
116 _rFamilyName
= "CellStyles";
117 _rStyleName
= "Default";
119 else if ( xDocumentSI
->supportsService("com.sun.star.drawing.DrawingDocument")
120 || xDocumentSI
->supportsService("com.sun.star.presentation.PresentationDocument")
123 _rFamilyName
= "graphics";
124 _rStyleName
= "standard";
133 void lcl_initializeControlFont( const Reference
< XPropertySet
>& _rxModel
)
137 Reference
< XPropertySet
> xStyle( ControlLayouter::getDefaultDocumentTextStyle( _rxModel
), UNO_SET_THROW
);
138 Reference
< XPropertySetInfo
> xStylePSI( xStyle
->getPropertySetInfo(), UNO_SET_THROW
);
140 // determine the script type associated with the system locale
141 const SvtSysLocale aSysLocale
;
142 const LocaleDataWrapper
& rSysLocaleData
= aSysLocale
.GetLocaleData();
143 const sal_Int16 eSysLocaleScriptType
= MsLangId::getScriptType( rSysLocaleData
.getLanguageTag().getLanguageType() );
145 // depending on this script type, use the right property from the document's style which controls the
146 // default locale for document content
147 const sal_Char
* pCharLocalePropertyName
= "CharLocale";
148 switch ( eSysLocaleScriptType
)
150 case ScriptType::LATIN
:
151 // already defaulted above
153 case ScriptType::ASIAN
:
154 pCharLocalePropertyName
= "CharLocaleAsian";
156 case ScriptType::COMPLEX
:
157 pCharLocalePropertyName
= "CharLocaleComplex";
160 OSL_FAIL( "lcl_initializeControlFont: unexpected script type for system locale!" );
164 OUString sCharLocalePropertyName
= OUString::createFromAscii( pCharLocalePropertyName
);
165 Locale aDocumentCharLocale
;
166 if ( xStylePSI
->hasPropertyByName( sCharLocalePropertyName
) )
168 OSL_VERIFY( xStyle
->getPropertyValue( sCharLocalePropertyName
) >>= aDocumentCharLocale
);
170 // fall back to CharLocale property at the style
171 if ( aDocumentCharLocale
.Language
.isEmpty() )
173 sCharLocalePropertyName
= "CharLocale";
174 if ( xStylePSI
->hasPropertyByName( sCharLocalePropertyName
) )
176 OSL_VERIFY( xStyle
->getPropertyValue( sCharLocalePropertyName
) >>= aDocumentCharLocale
);
179 // fall back to the system locale
180 if ( aDocumentCharLocale
.Language
.isEmpty() )
182 aDocumentCharLocale
= rSysLocaleData
.getLanguageTag().getLocale();
185 // retrieve a default font for this locale, and set it at the control
186 vcl::Font aFont
= OutputDevice::GetDefaultFont( DefaultFontType::SANS
, LanguageTag::convertToLanguageType( aDocumentCharLocale
), GetDefaultFontFlags::OnlyOne
);
187 FontDescriptor aFontDesc
= VCLUnoHelper::CreateFontDescriptor( aFont
);
188 _rxModel
->setPropertyValue("FontDescriptor", makeAny( aFontDesc
)
191 catch( const Exception
& )
193 DBG_UNHANDLED_EXCEPTION("svx");
202 Reference
< XPropertySet
> ControlLayouter::getDefaultDocumentTextStyle( const Reference
< XPropertySet
>& _rxModel
)
204 // the style family collection
205 Reference
< XStyleFamiliesSupplier
> xSuppStyleFamilies( getTypedModelNode
< XStyleFamiliesSupplier
>( _rxModel
.get() ), UNO_SET_THROW
);
206 Reference
< XNameAccess
> xStyleFamilies( xSuppStyleFamilies
->getStyleFamilies(), UNO_SET_THROW
);
208 // the names of the family, and the style - depends on the document type we live in
209 OUString sFamilyName
, sStyleName
;
210 if ( !lcl_getDocumentDefaultStyleAndFamily( xSuppStyleFamilies
.get(), sFamilyName
, sStyleName
) )
211 throw RuntimeException("unknown document type!");
213 // the concrete style
214 Reference
< XNameAccess
> xStyleFamily( xStyleFamilies
->getByName( sFamilyName
), UNO_QUERY_THROW
);
215 return Reference
< XPropertySet
>( xStyleFamily
->getByName( sStyleName
), UNO_QUERY_THROW
);
219 void ControlLayouter::initializeControlLayout( const Reference
< XPropertySet
>& _rxControlModel
, DocumentType _eDocType
)
221 DBG_ASSERT( _rxControlModel
.is(), "ControlLayouter::initializeControlLayout: invalid model!" );
222 if ( !_rxControlModel
.is() )
227 Reference
< XPropertySetInfo
> xPSI( _rxControlModel
->getPropertySetInfo(), UNO_SET_THROW
);
230 sal_Int16 nClassId
= FormComponentType::CONTROL
;
231 _rxControlModel
->getPropertyValue( FM_PROP_CLASSID
) >>= nClassId
;
234 if ( _eDocType
== eUnknownDocumentType
)
235 _eDocType
= DocumentClassification::classifyHostDocument( _rxControlModel
.get() );
237 // let's see what the configuration says about the visual effect
238 OConfigurationNode aConfig
= getLayoutSettings( _eDocType
);
239 Any aVisualEffect
= aConfig
.getNodeValue( OUString( "VisualEffect" ) );
240 if ( aVisualEffect
.hasValue() )
242 OUString sVisualEffect
;
243 OSL_VERIFY( aVisualEffect
>>= sVisualEffect
);
245 sal_Int16 nVisualEffect
= VisualEffect::NONE
;
246 if ( sVisualEffect
== "flat" )
247 nVisualEffect
= VisualEffect::FLAT
;
248 else if ( sVisualEffect
== "3D" )
249 nVisualEffect
= VisualEffect::LOOK3D
;
251 if ( xPSI
->hasPropertyByName( FM_PROP_BORDER
) )
253 if ( ( nClassId
!= FormComponentType::COMMANDBUTTON
)
254 && ( nClassId
!= FormComponentType::RADIOBUTTON
)
255 && ( nClassId
!= FormComponentType::CHECKBOX
)
256 && ( nClassId
!= FormComponentType::GROUPBOX
)
257 && ( nClassId
!= FormComponentType::FIXEDTEXT
)
258 && ( nClassId
!= FormComponentType::SCROLLBAR
)
259 && ( nClassId
!= FormComponentType::SPINBUTTON
)
262 _rxControlModel
->setPropertyValue( FM_PROP_BORDER
, makeAny( nVisualEffect
) );
263 if ( ( nVisualEffect
== VisualEffect::FLAT
)
264 && ( xPSI
->hasPropertyByName( FM_PROP_BORDERCOLOR
) )
266 // light gray flat border
267 _rxControlModel
->setPropertyValue( FM_PROP_BORDERCOLOR
, makeAny( sal_Int32(0x00C0C0C0) ) );
270 if ( xPSI
->hasPropertyByName( FM_PROP_VISUALEFFECT
) )
271 _rxControlModel
->setPropertyValue( FM_PROP_VISUALEFFECT
, makeAny( nVisualEffect
) );
274 // the font (only if we use the document's ref devices for rendering control text, otherwise, the
275 // default font of VCL controls is assumed to be fine)
276 if ( useDocumentReferenceDevice( _eDocType
)
277 && xPSI
->hasPropertyByName( FM_PROP_FONT
)
279 lcl_initializeControlFont( _rxControlModel
);
281 catch( const Exception
& )
283 OSL_FAIL( "ControlLayouter::initializeControlLayout: caught an exception!" );
287 bool ControlLayouter::useDynamicBorderColor( DocumentType _eDocType
)
289 OConfigurationNode aConfig
= getLayoutSettings( _eDocType
);
290 Any aDynamicBorderColor
= aConfig
.getNodeValue( OUString( "DynamicBorderColors" ) );
291 bool bDynamicBorderColor
= false;
292 OSL_VERIFY( aDynamicBorderColor
>>= bDynamicBorderColor
);
293 return bDynamicBorderColor
;
297 bool ControlLayouter::useDocumentReferenceDevice( DocumentType _eDocType
)
299 if ( _eDocType
== eUnknownDocumentType
)
301 OConfigurationNode aConfig
= getLayoutSettings( _eDocType
);
302 Any aUseRefDevice
= aConfig
.getNodeValue( OUString( "UseDocumentTextMetrics" ) );
303 bool bUseRefDevice
= false;
304 OSL_VERIFY( aUseRefDevice
>>= bUseRefDevice
);
305 return bUseRefDevice
;
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */