Coverage Report

Created: 2025-07-07 10:01

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