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