LibreOffice Module svx (master)  1
fmcontrollayout.cxx
Go to the documentation of this file.
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.hxx>
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 
34 #include <i18nlangtag/mslangid.hxx>
36 #include <unotools/confignode.hxx>
37 #include <unotools/syslocale.hxx>
39 
41 #include <tools/debug.hxx>
42 #include <tools/diagnose_ex.h>
43 #include <vcl/outdev.hxx>
44 
45 
46 namespace svxform
47 {
48 
49 
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;
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;
67 
68  namespace FormComponentType = ::com::sun::star::form::FormComponentType;
69  namespace VisualEffect = ::com::sun::star::awt::VisualEffect;
70  namespace ScriptType = ::com::sun::star::i18n::ScriptType;
71 
72 
73  namespace
74  {
75  ::utl::OConfigurationNode getLayoutSettings( DocumentType _eDocType )
76  {
77  OUString sConfigName = "/org.openoffice.Office.Common/Forms/ControlLayout/" +
79  return OConfigurationTreeRoot::createWithComponentContext(
81  sConfigName );
82  }
83 
84  template< class INTERFACE_TYPE >
85  Reference< INTERFACE_TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode )
86  {
87  Reference< INTERFACE_TYPE > xTypedNode( _rxModelNode, UNO_QUERY );
88  if ( xTypedNode.is() )
89  return xTypedNode;
90  else
91  {
92  Reference< XChild > xChild( _rxModelNode, UNO_QUERY );
93  if ( xChild.is() )
94  return getTypedModelNode< INTERFACE_TYPE >( xChild->getParent() );
95  else
96  return nullptr;
97  }
98  }
99 
100 
101  bool lcl_getDocumentDefaultStyleAndFamily( const Reference< XInterface >& _rxDocument, OUString& _rFamilyName, OUString& _rStyleName )
102  {
103  bool bSuccess = true;
104  Reference< XServiceInfo > xDocumentSI( _rxDocument, UNO_QUERY );
105  if ( xDocumentSI.is() )
106  {
107  if ( xDocumentSI->supportsService("com.sun.star.text.TextDocument")
108  || xDocumentSI->supportsService("com.sun.star.text.WebDocument")
109  )
110  {
111  _rFamilyName = "ParagraphStyles";
112  _rStyleName = "Standard";
113  }
114  else if ( xDocumentSI->supportsService("com.sun.star.sheet.SpreadsheetDocument") )
115  {
116  _rFamilyName = "CellStyles";
117  _rStyleName = "Default";
118  }
119  else if ( xDocumentSI->supportsService("com.sun.star.drawing.DrawingDocument")
120  || xDocumentSI->supportsService("com.sun.star.presentation.PresentationDocument")
121  )
122  {
123  _rFamilyName = "graphics";
124  _rStyleName = "standard";
125  }
126  else
127  bSuccess = false;
128  }
129  return bSuccess;
130  }
131 
132 
133  void lcl_initializeControlFont( const Reference< XPropertySet >& _rxModel )
134  {
135  try
136  {
137  Reference< XPropertySet > xStyle( ControlLayouter::getDefaultDocumentTextStyle( _rxModel ), UNO_SET_THROW );
138  Reference< XPropertySetInfo > xStylePSI( xStyle->getPropertySetInfo(), UNO_SET_THROW );
139 
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() );
144 
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 char* pCharLocalePropertyName = "CharLocale";
148  switch ( eSysLocaleScriptType )
149  {
150  case ScriptType::LATIN:
151  // already defaulted above
152  break;
153  case ScriptType::ASIAN:
154  pCharLocalePropertyName = "CharLocaleAsian";
155  break;
156  case ScriptType::COMPLEX:
157  pCharLocalePropertyName = "CharLocaleComplex";
158  break;
159  default:
160  OSL_FAIL( "lcl_initializeControlFont: unexpected script type for system locale!" );
161  break;
162  }
163 
164  OUString sCharLocalePropertyName = OUString::createFromAscii( pCharLocalePropertyName );
165  Locale aDocumentCharLocale;
166  if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
167  {
168  OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
169  }
170  // fall back to CharLocale property at the style
171  if ( aDocumentCharLocale.Language.isEmpty() )
172  {
173  sCharLocalePropertyName = "CharLocale";
174  if ( xStylePSI->hasPropertyByName( sCharLocalePropertyName ) )
175  {
176  OSL_VERIFY( xStyle->getPropertyValue( sCharLocalePropertyName ) >>= aDocumentCharLocale );
177  }
178  }
179  // fall back to the system locale
180  if ( aDocumentCharLocale.Language.isEmpty() )
181  {
182  aDocumentCharLocale = rSysLocaleData.getLanguageTag().getLocale();
183  }
184 
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", Any( aFontDesc )
189  );
190  }
191  catch( const Exception& )
192  {
194  }
195  }
196  }
197 
198 
199  //= ControlLayouter
200 
201 
203  {
204  // the style family collection
205  Reference< XStyleFamiliesSupplier > xSuppStyleFamilies( getTypedModelNode< XStyleFamiliesSupplier >( _rxModel ), UNO_SET_THROW );
206  Reference< XNameAccess > xStyleFamilies( xSuppStyleFamilies->getStyleFamilies(), UNO_SET_THROW );
207 
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, sFamilyName, sStyleName ) )
211  throw RuntimeException("unknown document type!");
212 
213  // the concrete style
214  Reference< XNameAccess > xStyleFamily( xStyleFamilies->getByName( sFamilyName ), UNO_QUERY_THROW );
215  return Reference< XPropertySet >( xStyleFamily->getByName( sStyleName ), UNO_QUERY_THROW );
216  }
217 
218 
220  {
221  DBG_ASSERT( _rxControlModel.is(), "ControlLayouter::initializeControlLayout: invalid model!" );
222  if ( !_rxControlModel.is() )
223  return;
224 
225  try
226  {
227  Reference< XPropertySetInfo > xPSI( _rxControlModel->getPropertySetInfo(), UNO_SET_THROW );
228 
229  // the control type
230  sal_Int16 nClassId = FormComponentType::CONTROL;
231  _rxControlModel->getPropertyValue( FM_PROP_CLASSID ) >>= nClassId;
232 
233  // the document type
234  if ( _eDocType == eUnknownDocumentType )
235  _eDocType = DocumentClassification::classifyHostDocument( _rxControlModel );
236 
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() )
241  {
242  OUString sVisualEffect;
243  OSL_VERIFY( aVisualEffect >>= sVisualEffect );
244 
245  sal_Int16 nVisualEffect = VisualEffect::NONE;
246  if ( sVisualEffect == "flat" )
247  nVisualEffect = VisualEffect::FLAT;
248  else if ( sVisualEffect == "3D" )
249  nVisualEffect = VisualEffect::LOOK3D;
250 
251  if ( xPSI->hasPropertyByName( FM_PROP_BORDER ) )
252  {
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 )
260  )
261  {
262  _rxControlModel->setPropertyValue( FM_PROP_BORDER, Any( nVisualEffect ) );
263  if ( ( nVisualEffect == VisualEffect::FLAT )
264  && ( xPSI->hasPropertyByName( FM_PROP_BORDERCOLOR ) )
265  )
266  // light gray flat border
267  _rxControlModel->setPropertyValue( FM_PROP_BORDERCOLOR, Any( sal_Int32(0x00C0C0C0) ) );
268  }
269  }
270  if ( xPSI->hasPropertyByName( FM_PROP_VISUALEFFECT ) )
271  _rxControlModel->setPropertyValue( FM_PROP_VISUALEFFECT, Any( nVisualEffect ) );
272  }
273 
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 )
278  )
279  lcl_initializeControlFont( _rxControlModel );
280  }
281  catch( const Exception& )
282  {
283  TOOLS_WARN_EXCEPTION( "svx", "ControlLayouter::initializeControlLayout" );
284  }
285  }
286 
288  {
289  OConfigurationNode aConfig = getLayoutSettings( _eDocType );
290  Any aDynamicBorderColor = aConfig.getNodeValue( OUString( "DynamicBorderColors" ) );
291  bool bDynamicBorderColor = false;
292  OSL_VERIFY( aDynamicBorderColor >>= bDynamicBorderColor );
293  return bDynamicBorderColor;
294  }
295 
296 
298  {
299  if ( _eDocType == eUnknownDocumentType )
300  return false;
301  OConfigurationNode aConfig = getLayoutSettings( _eDocType );
302  Any aUseRefDevice = aConfig.getNodeValue( OUString( "UseDocumentTextMetrics" ) );
303  bool bUseRefDevice = false;
304  OSL_VERIFY( aUseRefDevice >>= bUseRefDevice );
305  return bUseRefDevice;
306  }
307 
308 
309 }
310 
311 
312 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static vcl::Font GetDefaultFont(DefaultFontType nType, LanguageType eLang, GetDefaultFontFlags nFlags, const OutputDevice *pOutDev=nullptr)
constexpr OUStringLiteral FM_PROP_FONT
Definition: fmprop.hxx:94
void initializeControlLayout(const css::uno::Reference< css::beans::XPropertySet > &_rxControlModel, DocumentType _eDocType)
initializes the layout of a newly created form control (model)
LanguageType getLanguageType(bool bResolveSystem=true) const
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
static DocumentType classifyHostDocument(const css::uno::Reference< css::uno::XInterface > &_rxFormComponent)
constexpr OUStringLiteral FM_PROP_VISUALEFFECT
Definition: fmprop.hxx:138
constexpr OUStringLiteral FM_PROP_CLASSID
Definition: fmprop.hxx:32
const css::lang::Locale & getLocale(bool bResolveSystem=true) const
DocumentType
#define DBG_UNHANDLED_EXCEPTION(...)
#define TOOLS_WARN_EXCEPTION(area, stream)
#define DBG_ASSERT(sCon, aError)
class SAL_NO_VTABLE XPropertySet
Definition: xmlexchg.hxx:28
css::uno::Reference< css::beans::XPropertySet > getDefaultDocumentTextStyle(const css::uno::Reference< css::beans::XPropertySet > &_rxModel)
gets the "default" style in a document which can be used if some default text format is needed ...
constexpr OUStringLiteral FM_PROP_BORDER
Definition: fmprop.hxx:97
static sal_Int16 getScriptType(LanguageType nLang)
const LanguageTag & getLanguageTag() const
Reference< XComponentContext > getProcessComponentContext()
class FmSearchEngine - Impl class for FmSearchDialog
constexpr OUStringLiteral FM_PROP_BORDERCOLOR
Definition: fmprop.hxx:139
bool useDocumentReferenceDevice(DocumentType _eDocType)
determines whether for the given document type, form controls should use the document's reference dev...
ScriptType
static css::awt::FontDescriptor CreateFontDescriptor(const vcl::Font &rFont)
bool useDynamicBorderColor(DocumentType _eDocType)
determines whether for the given document type, dynamic control border coloring is enabled ...
static OUString getModuleIdentifierForDocumentType(DocumentType _eType)