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 <i18npool/mslangid.hxx>
35 : #include <i18npool/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 : /** === begin UNO using === **/
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::uno::Sequence;
60 : using ::com::sun::star::uno::Type;
61 : using ::com::sun::star::beans::XPropertySet;
62 : using ::com::sun::star::beans::XPropertySetInfo;
63 : using ::com::sun::star::lang::Locale;
64 : using ::com::sun::star::awt::FontDescriptor;
65 : using ::com::sun::star::style::XStyleFamiliesSupplier;
66 : using ::com::sun::star::lang::XServiceInfo;
67 : using ::com::sun::star::container::XNameAccess;
68 : using ::com::sun::star::container::XChild;
69 : /** === end UNO using === **/
70 : namespace FormComponentType = ::com::sun::star::form::FormComponentType;
71 : namespace VisualEffect = ::com::sun::star::awt::VisualEffect;
72 : namespace ScriptType = ::com::sun::star::i18n::ScriptType;
73 :
74 : //--------------------------------------------------------------------
75 : namespace
76 : {
77 : //....................................................................
78 : template< class INTERFACE_TYPE >
79 0 : Reference< INTERFACE_TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
80 : {
81 0 : Reference< INTERFACE_TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
82 0 : if ( xTypedNode.is() )
83 0 : return xTypedNode;
84 : else
85 : {
86 0 : Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
87 0 : if ( xChild.is() )
88 0 : return getTypedModelNode< INTERFACE_TYPE >( xChild->getParent() );
89 : else
90 0 : return NULL;
91 : }
92 : }
93 :
94 : //....................................................................
95 0 : static bool lcl_getDocumentDefaultStyleAndFamily( const Reference< XInterface >& _rxDocument, ::rtl::OUString& _rFamilyName, ::rtl::OUString& _rStyleName ) SAL_THROW(( Exception ))
96 : {
97 0 : bool bSuccess = true;
98 0 : Reference< XServiceInfo > xDocumentSI( _rxDocument, UNO_QUERY );
99 0 : if ( xDocumentSI.is() )
100 : {
101 0 : if ( xDocumentSI->supportsService( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.TextDocument" ) ) )
102 0 : || xDocumentSI->supportsService( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.text.WebDocument" ) ) )
103 : )
104 : {
105 0 : _rFamilyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ParagraphStyles" ) );
106 0 : _rStyleName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Standard" ) );
107 : }
108 0 : else if ( xDocumentSI->supportsService( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.sheet.SpreadsheetDocument" ) ) ) )
109 : {
110 0 : _rFamilyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CellStyles" ) );
111 0 : _rStyleName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Default" ) );
112 : }
113 0 : else if ( xDocumentSI->supportsService( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.drawing.DrawingDocument" ) ) )
114 0 : || xDocumentSI->supportsService( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PresentationDocument" ) ) )
115 : )
116 : {
117 0 : _rFamilyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "graphics" ) );
118 0 : _rStyleName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "standard" ) );
119 : }
120 : else
121 0 : bSuccess = false;
122 : }
123 0 : return bSuccess;
124 : }
125 :
126 : //....................................................................
127 0 : static void lcl_initializeControlFont( const Reference< XPropertySet >& _rxModel )
128 : {
129 : try
130 : {
131 0 : Reference< XPropertySet > xStyle( ControlLayouter::getDefaultDocumentTextStyle( _rxModel ), UNO_SET_THROW );
132 0 : Reference< XPropertySetInfo > xStylePSI( xStyle->getPropertySetInfo(), UNO_SET_THROW );
133 :
134 : // determine the script type associated with the system locale
135 0 : const SvtSysLocale aSysLocale;
136 0 : const LocaleDataWrapper& rSysLocaleData = aSysLocale.GetLocaleData();
137 0 : const sal_Int16 eSysLocaleScriptType = MsLangId::getScriptType( rSysLocaleData.getLanguageTag().getLanguageType() );
138 :
139 : // depending on this script type, use the right property from the document's style which controls the
140 : // default locale for document content
141 0 : const sal_Char* pCharLocalePropertyName = "CharLocale";
142 0 : switch ( eSysLocaleScriptType )
143 : {
144 : case ScriptType::LATIN:
145 : // already defaulted above
146 0 : break;
147 : case ScriptType::ASIAN:
148 0 : pCharLocalePropertyName = "CharLocaleAsian";
149 0 : break;
150 : case ScriptType::COMPLEX:
151 0 : pCharLocalePropertyName = "CharLocaleComplex";
152 0 : break;
153 : default:
154 : OSL_FAIL( "lcl_initializeControlFont: unexpected script type for system locale!" );
155 0 : break;
156 : }
157 :
158 0 : ::rtl::OUString sCharLocalePropertyName = ::rtl::OUString::createFromAscii( pCharLocalePropertyName );
159 0 : Locale aDocumentCharLocale;
160 0 : if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
161 : {
162 0 : OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
163 : }
164 : // fall back to CharLocale property at the style
165 0 : if ( aDocumentCharLocale.Language.isEmpty() )
166 : {
167 0 : sCharLocalePropertyName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "CharLocale" ) );
168 0 : if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
169 : {
170 0 : OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
171 : }
172 : }
173 : // fall back to the system locale
174 0 : if ( aDocumentCharLocale.Language.isEmpty() )
175 : {
176 0 : aDocumentCharLocale = rSysLocaleData.getLanguageTag().getLocale();
177 : }
178 :
179 : // retrieve a default font for this locale, and set it at the control
180 0 : Font aFont = OutputDevice::GetDefaultFont( DEFAULTFONT_SANS, LanguageTag( aDocumentCharLocale ).getLanguageType(), DEFAULTFONT_FLAGS_ONLYONE );
181 0 : FontDescriptor aFontDesc = VCLUnoHelper::CreateFontDescriptor( aFont );
182 0 : _rxModel->setPropertyValue(
183 : ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "FontDescriptor" ) ),
184 : makeAny( aFontDesc )
185 0 : );
186 : }
187 0 : catch( const Exception& )
188 : {
189 : DBG_UNHANDLED_EXCEPTION();
190 : }
191 0 : }
192 : }
193 :
194 : //====================================================================
195 : //= ControlLayouter
196 : //====================================================================
197 : //--------------------------------------------------------------------
198 0 : Reference< XPropertySet > ControlLayouter::getDefaultDocumentTextStyle( const Reference< XPropertySet >& _rxModel )
199 : {
200 : // the style family collection
201 0 : Reference< XStyleFamiliesSupplier > xSuppStyleFamilies( getTypedModelNode< XStyleFamiliesSupplier >( _rxModel.get() ), UNO_SET_THROW );
202 0 : Reference< XNameAccess > xStyleFamilies( xSuppStyleFamilies->getStyleFamilies(), UNO_SET_THROW );
203 :
204 : // the names of the family, and the style - depends on the document type we live in
205 0 : ::rtl::OUString sFamilyName, sStyleName;
206 0 : if ( !lcl_getDocumentDefaultStyleAndFamily( xSuppStyleFamilies.get(), sFamilyName, sStyleName ) )
207 0 : throw RuntimeException( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "unknown document type!" ) ), NULL );
208 :
209 : // the concrete style
210 0 : Reference< XNameAccess > xStyleFamily( xStyleFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
211 0 : return Reference< XPropertySet >( xStyleFamily->getByName( sStyleName ), UNO_QUERY_THROW );
212 : }
213 :
214 : //--------------------------------------------------------------------
215 0 : void ControlLayouter::initializeControlLayout( const Reference< XPropertySet >& _rxControlModel, DocumentType _eDocType )
216 : {
217 : DBG_ASSERT( _rxControlModel.is(), "ControlLayouter::initializeControlLayout: invalid model!" );
218 0 : if ( !_rxControlModel.is() )
219 0 : return;
220 :
221 : try
222 : {
223 0 : Reference< XPropertySetInfo > xPSI( _rxControlModel->getPropertySetInfo(), UNO_SET_THROW );
224 :
225 : // the control type
226 0 : sal_Int16 nClassId = FormComponentType::CONTROL;
227 0 : _rxControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
228 :
229 : // the document type
230 0 : if ( _eDocType == eUnknownDocumentType )
231 0 : _eDocType = DocumentClassification::classifyHostDocument( _rxControlModel.get() );
232 :
233 : // let's see what the configuration says about the visual effect
234 0 : OConfigurationNode aConfig = getLayoutSettings( _eDocType );
235 0 : Any aVisualEffect = aConfig.getNodeValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "VisualEffect" ) ) );
236 0 : if ( aVisualEffect.hasValue() )
237 : {
238 0 : ::rtl::OUString sVisualEffect;
239 0 : OSL_VERIFY( aVisualEffect >>= sVisualEffect );
240 :
241 0 : sal_Int16 nVisualEffect = VisualEffect::NONE;
242 0 : if ( sVisualEffect == "flat" )
243 0 : nVisualEffect = VisualEffect::FLAT;
244 0 : else if ( sVisualEffect == "3D" )
245 0 : nVisualEffect = VisualEffect::LOOK3D;
246 :
247 0 : if ( xPSI->hasPropertyByName( FM_PROP_BORDER ) )
248 : {
249 0 : if ( ( nClassId != FormComponentType::COMMANDBUTTON )
250 : && ( nClassId != FormComponentType::RADIOBUTTON )
251 : && ( nClassId != FormComponentType::CHECKBOX )
252 : && ( nClassId != FormComponentType::GROUPBOX )
253 : && ( nClassId != FormComponentType::FIXEDTEXT )
254 : && ( nClassId != FormComponentType::SCROLLBAR )
255 : && ( nClassId != FormComponentType::SPINBUTTON )
256 : )
257 : {
258 0 : _rxControlModel->setPropertyValue( FM_PROP_BORDER, makeAny( nVisualEffect ) );
259 0 : if ( ( nVisualEffect == VisualEffect::FLAT )
260 0 : && ( xPSI->hasPropertyByName( FM_PROP_BORDERCOLOR ) )
261 : )
262 : // light gray flat border
263 0 : _rxControlModel->setPropertyValue( FM_PROP_BORDERCOLOR, makeAny( (sal_Int32)0x00C0C0C0 ) );
264 : }
265 : }
266 0 : if ( xPSI->hasPropertyByName( FM_PROP_VISUALEFFECT ) )
267 0 : _rxControlModel->setPropertyValue( FM_PROP_VISUALEFFECT, makeAny( nVisualEffect ) );
268 : }
269 :
270 : // the font (only if we use the document's ref devices for rendering control text, otherwise, the
271 : // default font of VCL controls is assumed to be fine)
272 0 : if ( useDocumentReferenceDevice( _eDocType )
273 0 : && xPSI->hasPropertyByName( FM_PROP_FONT )
274 : )
275 0 : lcl_initializeControlFont( _rxControlModel );
276 : }
277 0 : catch( const Exception& )
278 : {
279 : OSL_FAIL( "ControlLayouter::initializeControlLayout: caught an exception!" );
280 : }
281 : }
282 :
283 : //--------------------------------------------------------------------
284 2 : ::utl::OConfigurationNode ControlLayouter::getLayoutSettings( DocumentType _eDocType )
285 : {
286 2 : ::rtl::OUString sConfigName = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "/org.openoffice.Office.Common/Forms/ControlLayout/" ) );
287 2 : sConfigName += DocumentClassification::getModuleIdentifierForDocumentType( _eDocType );
288 : return OConfigurationTreeRoot::createWithServiceFactory(
289 : ::comphelper::getProcessServiceFactory(), // TODO
290 2 : sConfigName );
291 : }
292 :
293 : //--------------------------------------------------------------------
294 0 : bool ControlLayouter::useDynamicBorderColor( DocumentType _eDocType )
295 : {
296 0 : OConfigurationNode aConfig = getLayoutSettings( _eDocType );
297 0 : Any aDynamicBorderColor = aConfig.getNodeValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "DynamicBorderColors" ) ) );
298 0 : bool bDynamicBorderColor = false;
299 0 : OSL_VERIFY( aDynamicBorderColor >>= bDynamicBorderColor );
300 0 : return bDynamicBorderColor;
301 : }
302 :
303 : //--------------------------------------------------------------------
304 2 : bool ControlLayouter::useDocumentReferenceDevice( DocumentType _eDocType )
305 : {
306 2 : if ( _eDocType == eUnknownDocumentType )
307 0 : return false;
308 2 : OConfigurationNode aConfig = getLayoutSettings( _eDocType );
309 2 : Any aUseRefDevice = aConfig.getNodeValue( ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "UseDocumentTextMetrics" ) ) );
310 2 : bool bUseRefDevice = false;
311 2 : OSL_VERIFY( aUseRefDevice >>= bUseRefDevice );
312 2 : return bUseRefDevice;
313 : }
314 :
315 : //........................................................................
316 : } // namespace svxform
317 : //........................................................................
318 :
319 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|