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