Coverage Report

Created: 2026-06-30 11:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/vcl/source/gdi/formpdfexport.cxx
Line
Count
Source
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 <memory>
22
#include <vcl/formpdfexport.hxx>
23
#include <comphelper/diagnose_ex.hxx>
24
#include <tools/lineend.hxx>
25
#include <unordered_map>
26
#include <sal/log.hxx>
27
28
#include <com/sun/star/container/XIndexAccess.hpp>
29
#include <com/sun/star/form/XForm.hpp>
30
#include <com/sun/star/container/XChild.hpp>
31
#include <com/sun/star/lang/XServiceInfo.hpp>
32
#include <com/sun/star/beans/XPropertySet.hpp>
33
#include <com/sun/star/form/FormComponentType.hpp>
34
#include <com/sun/star/awt/TextAlign.hpp>
35
#include <com/sun/star/awt/XControl.hpp>
36
#include <com/sun/star/style/VerticalAlignment.hpp>
37
#include <com/sun/star/form/FormButtonType.hpp>
38
#include <com/sun/star/form/FormSubmitMethod.hpp>
39
40
#include <toolkit/helper/vclunohelper.hxx>
41
#include <vcl/pdfextoutdevdata.hxx>
42
#include <vcl/unohelp.hxx>
43
44
#include <algorithm>
45
#include <iterator>
46
47
48
static vcl::Font CreateFont( const css::awt::FontDescriptor& rDescr )
49
0
{
50
0
    vcl::Font aFont;
51
0
    if ( !rDescr.Name.isEmpty() )
52
0
        aFont.SetFamilyName( rDescr.Name );
53
0
    if ( !rDescr.StyleName.isEmpty() )
54
0
        aFont.SetStyleName( rDescr.StyleName );
55
0
    if ( rDescr.Height )
56
0
        aFont.SetFontSize( Size( rDescr.Width, rDescr.Height ) );
57
0
    if ( static_cast<FontFamily>(rDescr.Family) != FAMILY_DONTKNOW )
58
0
        aFont.SetFamily( static_cast<FontFamily>(rDescr.Family) );
59
0
    if ( static_cast<rtl_TextEncoding>(rDescr.CharSet) != RTL_TEXTENCODING_DONTKNOW )
60
0
        aFont.SetCharSet( static_cast<rtl_TextEncoding>(rDescr.CharSet) );
61
0
    if ( static_cast<FontPitch>(rDescr.Pitch) != PITCH_DONTKNOW )
62
0
        aFont.SetPitch( static_cast<FontPitch>(rDescr.Pitch) );
63
0
    if ( rDescr.CharacterWidth )
64
0
        aFont.SetWidthType(vcl::unohelper::ConvertFontWidth(rDescr.CharacterWidth));
65
0
    if ( rDescr.Weight )
66
0
        aFont.SetWeight(vcl::unohelper::ConvertFontWeight(rDescr.Weight));
67
0
    if ( rDescr.Slant != css::awt::FontSlant_DONTKNOW )
68
0
        aFont.SetItalic(vcl::unohelper::ConvertFontSlant(rDescr.Slant));
69
0
    if ( static_cast<FontLineStyle>(rDescr.Underline) != LINESTYLE_DONTKNOW )
70
0
        aFont.SetUnderline( static_cast<FontLineStyle>(rDescr.Underline) );
71
0
    if ( static_cast<FontStrikeout>(rDescr.Strikeout) != STRIKEOUT_DONTKNOW )
72
0
        aFont.SetStrikeout( static_cast<FontStrikeout>(rDescr.Strikeout) );
73
74
    // Not DONTKNOW
75
0
    aFont.SetOrientation( Degree10(static_cast<sal_Int16>(rDescr.Orientation * 10)) );
76
0
    aFont.SetKerning( static_cast<FontKerning>(rDescr.Kerning) );
77
0
    aFont.SetWordLineMode( rDescr.WordLineMode );
78
79
0
    return aFont;
80
0
}
81
82
namespace toolkitform
83
{
84
85
86
    using namespace ::com::sun::star;
87
    using namespace ::com::sun::star::uno;
88
    using namespace ::com::sun::star::awt;
89
    using namespace ::com::sun::star::style;
90
    using namespace ::com::sun::star::beans;
91
    using namespace ::com::sun::star::form;
92
    using namespace ::com::sun::star::lang;
93
    using namespace ::com::sun::star::container;
94
95
    constexpr OUString FM_PROP_NAME = u"Name"_ustr;
96
97
    namespace
98
    {
99
100
        /** determines the FormComponentType of a form control
101
        */
102
        sal_Int16 classifyFormControl( const Reference< XPropertySet >& _rxModel )
103
0
        {
104
0
            static constexpr OUString FM_PROP_CLASSID = u"ClassId"_ustr;
105
0
            sal_Int16 nControlType = FormComponentType::CONTROL;
106
107
0
            Reference< XPropertySetInfo > xPSI;
108
0
            if ( _rxModel.is() )
109
0
                xPSI = _rxModel->getPropertySetInfo();
110
0
            if ( xPSI.is() && xPSI->hasPropertyByName( FM_PROP_CLASSID ) )
111
0
            {
112
0
                if( ! (_rxModel->getPropertyValue( FM_PROP_CLASSID ) >>= nControlType) ) {
113
0
                    SAL_WARN("toolkit.helper", "classifyFormControl: unable to get property " << FM_PROP_CLASSID);
114
0
                }
115
0
            }
116
117
0
            return nControlType;
118
0
        }
119
120
121
        /** (default-)creates a PDF widget according to a given FormComponentType
122
        */
123
        std::unique_ptr<vcl::pdf::PDFWriter::AnyWidget> createDefaultWidget( sal_Int16 _nFormComponentType )
124
0
        {
125
0
            switch ( _nFormComponentType )
126
0
            {
127
0
            case FormComponentType::COMMANDBUTTON:
128
0
                return std::make_unique<vcl::pdf::PDFWriter::PushButtonWidget>();
129
0
            case FormComponentType::CHECKBOX:
130
0
                return std::make_unique<vcl::pdf::PDFWriter::CheckBoxWidget>();
131
0
            case FormComponentType::RADIOBUTTON:
132
0
                return std::make_unique<vcl::pdf::PDFWriter::RadioButtonWidget>();
133
0
            case FormComponentType::LISTBOX:
134
0
                return std::make_unique<vcl::pdf::PDFWriter::ListBoxWidget>();
135
0
            case FormComponentType::COMBOBOX:
136
0
                return std::make_unique<vcl::pdf::PDFWriter::ComboBoxWidget>();
137
138
0
            case FormComponentType::TEXTFIELD:
139
0
            case FormComponentType::FILECONTROL:
140
0
            case FormComponentType::DATEFIELD:
141
0
            case FormComponentType::TIMEFIELD:
142
0
            case FormComponentType::NUMERICFIELD:
143
0
            case FormComponentType::CURRENCYFIELD:
144
0
            case FormComponentType::PATTERNFIELD:
145
0
                return std::make_unique<vcl::pdf::PDFWriter::EditWidget>();
146
0
            }
147
0
            return nullptr;
148
0
        }
149
150
151
        /** determines a unique number for the radio group which the given radio
152
            button model belongs to
153
154
            The number is guaranteed to be
155
            <ul><li>unique within the document in which the button lives</li>
156
                <li>the same for subsequent calls with other radio button models,
157
                    which live in the same document, and belong to the same group</li>
158
            </ul>
159
160
            @precond
161
                the model must be part of the form component hierarchy in a document
162
        */
163
        sal_Int32 determineRadioGroupId( const Reference< XPropertySet >& _rxRadioModel )
164
0
        {
165
0
            OSL_ENSURE( classifyFormControl( _rxRadioModel ) == FormComponentType::RADIOBUTTON,
166
0
                "determineRadioGroupId: this *is* no radio button model!" );
167
            // The fact that radio button groups need to be unique within the complete
168
            // host document makes it somewhat difficult ...
169
            // Problem is that two form radio buttons belong to the same group if
170
            // - they have the same parent
171
            // - AND they have the same name or group name
172
            // This implies that we need some knowledge about (potentially) *all* radio button
173
            // groups in the document.
174
175
            // get the root-level container
176
0
            Reference< XChild > xChild( _rxRadioModel, UNO_QUERY );
177
0
            Reference< XForm > xParentForm( xChild.is() ? xChild->getParent() : Reference< XInterface >(), UNO_QUERY );
178
0
            OSL_ENSURE( xParentForm.is(), "determineRadioGroupId: no parent form -> group id!" );
179
0
            if ( !xParentForm.is() )
180
0
                return -1;
181
182
0
            while ( xParentForm.is() )
183
0
            {
184
0
                xChild = xParentForm.get();
185
0
                xParentForm.set(xChild->getParent(), css::uno::UNO_QUERY);
186
0
            }
187
0
            Reference< XIndexAccess > xRoot( xChild->getParent(), UNO_QUERY );
188
0
            OSL_ENSURE( xRoot.is(), "determineRadioGroupId: unable to determine the root of the form component hierarchy!" );
189
0
            if ( !xRoot.is() )
190
0
                return -1;
191
192
            // count the leafs in the hierarchy, until we encounter radio button
193
0
            ::std::vector< Reference< XIndexAccess > > aAncestors;
194
0
            ::std::vector< sal_Int32 >                 aPath;
195
196
0
            Reference< XInterface > xNormalizedLookup( _rxRadioModel, UNO_QUERY );
197
0
            Reference< XIndexAccess > xCurrentContainer( xRoot );
198
0
            sal_Int32 nStartWithChild = 0;
199
0
            sal_Int32 nGroupsEncountered = 0;
200
0
            do
201
0
            {
202
0
                std::unordered_map<OUString,sal_Int32> GroupNameMap;
203
0
                std::unordered_map<OUString,sal_Int32> SharedNameMap;
204
0
                sal_Int32 nCount = xCurrentContainer->getCount();
205
0
                sal_Int32 i;
206
0
                for ( i = nStartWithChild; i < nCount; ++i )
207
0
                {
208
0
                    Reference< XInterface > xElement( xCurrentContainer->getByIndex( i ), UNO_QUERY );
209
0
                    if ( !xElement.is() )
210
0
                    {
211
0
                        OSL_FAIL( "determineRadioGroupId: very suspicious!" );
212
0
                        continue;
213
0
                    }
214
215
0
                    Reference< XIndexAccess > xNewContainer( xElement, UNO_QUERY );
216
0
                    if ( xNewContainer.is() )
217
0
                    {
218
                        // step down the hierarchy
219
0
                        aAncestors.push_back( xCurrentContainer );
220
0
                        xCurrentContainer = std::move(xNewContainer);
221
0
                        aPath.push_back( i );
222
0
                        nStartWithChild = 0;
223
0
                        break;
224
                            // out of the inner loop, but continue with the outer loop
225
0
                    }
226
227
0
                    if ( xElement.get() == xNormalizedLookup.get() )
228
0
                    {
229
                        // Our radio button is in this container.
230
                        // Now take the time to ID this container's groups and return the button's groupId
231
0
                        for ( i = 0; i < nCount; ++i )
232
0
                        {
233
0
                            try
234
0
                            {
235
0
                                xElement.set( xCurrentContainer->getByIndex( i ), UNO_QUERY_THROW );
236
0
                                Reference< XServiceInfo > xModelSI( xElement, UNO_QUERY_THROW );
237
0
                                if ( xModelSI->supportsService(u"com.sun.star.awt.UnoControlRadioButtonModel"_ustr) )
238
0
                                {
239
0
                                    Reference< XPropertySet >  aProps( xElement, UNO_QUERY_THROW );
240
241
0
                                    OUString sGroupName;
242
0
                                    aProps->getPropertyValue(u"GroupName"_ustr) >>= sGroupName;
243
0
                                    if ( !sGroupName.isEmpty() )
244
0
                                    {
245
                                        // map: unique key is the group name, so attempts to add a different ID value
246
                                        // for an existing group are ignored - keeping the first ID - perfect for this scenario.
247
0
                                        GroupNameMap.emplace( sGroupName, nGroupsEncountered + i );
248
249
0
                                        if ( xElement.get() == xNormalizedLookup.get() )
250
0
                                            return GroupNameMap[sGroupName];
251
0
                                    }
252
0
                                    else
253
0
                                    {
254
                                        // Old implementation didn't have a GroupName, just identical Control names.
255
0
                                        aProps->getPropertyValue( FM_PROP_NAME ) >>= sGroupName;
256
0
                                        SharedNameMap.emplace( sGroupName, nGroupsEncountered + i );
257
258
0
                                        if ( xElement.get() == xNormalizedLookup.get() )
259
0
                                            return SharedNameMap[sGroupName];
260
0
                                    }
261
262
0
                                }
263
0
                            }
264
0
                            catch( uno::Exception& )
265
0
                            {
266
0
                                DBG_UNHANDLED_EXCEPTION("toolkit");
267
0
                            }
268
0
                        }
269
0
                        SAL_WARN("toolkit.helper","determineRadioGroupId: did not find the radios element's group!" );
270
0
                    }
271
0
                }
272
273
                // we encounter this container the first time. In particular, we did not just step up
274
0
                if ( nStartWithChild == 0 )
275
0
                {
276
                    // Our control wasn't in this container, so consider every item to be a possible unique group.
277
                    // This is way too much: Not all of the elements in the current container will form groups.
278
                    // But anyway, this number is sufficient for our purpose, since sequential group ids are not required.
279
                    // Ultimately, the container contains *at most* this many groups.
280
0
                    nGroupsEncountered += nCount;
281
0
                }
282
283
0
                if (  i >= nCount )
284
0
                {
285
                    // the loop terminated because there were no more elements
286
                    // -> step up, if possible
287
0
                    if ( aAncestors.empty() )
288
0
                        break;
289
290
0
                    xCurrentContainer = aAncestors.back(); aAncestors.pop_back();
291
0
                    nStartWithChild = aPath.back() + 1; aPath.pop_back();
292
0
                }
293
0
            }
294
0
            while ( true );
295
0
            return -1;
296
0
        }
297
298
299
        /** copies a StringItemList to a PDF widget's list
300
        */
301
        void getStringItemVector( const Reference< XPropertySet >& _rxModel, ::std::vector< OUString >& _rVector )
302
0
        {
303
0
            Sequence< OUString > aListEntries;
304
0
            if( ! (_rxModel->getPropertyValue( u"StringItemList"_ustr ) >>= aListEntries) ) {
305
0
                SAL_WARN("toolkit.helper", "getStringItemVector: unable to get property StringItemList");
306
0
            }
307
0
            _rVector.insert( _rVector.end(), std::cbegin(aListEntries), std::cend(aListEntries) );
308
0
        }
309
    }
310
311
312
    /** creates a PDF compatible control descriptor for the given control
313
    */
314
    std::unique_ptr<vcl::pdf::PDFWriter::AnyWidget> describePDFControl( const Reference< XControl >& _rxControl,
315
        vcl::PDFExtOutDevData& i_pdfExportData )
316
0
    {
317
0
        std::unique_ptr<vcl::pdf::PDFWriter::AnyWidget> Descriptor;
318
0
        OSL_ENSURE( _rxControl.is(), "describePDFControl: invalid (NULL) control!" );
319
0
        if ( !_rxControl.is() )
320
0
            return Descriptor;
321
322
0
        try
323
0
        {
324
0
            Reference< XPropertySet > xModelProps( _rxControl->getModel(), UNO_QUERY );
325
0
            sal_Int16 nControlType = classifyFormControl( xModelProps );
326
0
            Descriptor = createDefaultWidget( nControlType );
327
0
            if (!Descriptor)
328
                // no PDF widget available for this
329
0
                return Descriptor;
330
331
0
            Reference< XPropertySetInfo > xPSI( xModelProps->getPropertySetInfo() );
332
0
            Reference< XServiceInfo > xSI( xModelProps, UNO_QUERY );
333
0
            OSL_ENSURE( xSI.is(), "describePDFControl: no service info!" );
334
                // if we survived classifyFormControl, then it's a real form control, and they all have
335
                // service infos
336
337
338
            // set the common widget properties
339
340
341
            // Name, Description, Text
342
0
            if( ! (xModelProps->getPropertyValue( FM_PROP_NAME ) >>= Descriptor->Name) ) {
343
0
                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_NAME);
344
0
            }
345
0
            if( ! (xModelProps->getPropertyValue( u"HelpText"_ustr ) >>= Descriptor->Description) ) {
346
0
                SAL_INFO("toolkit.helper", "describePDFControl: unable to get property HelpText");
347
0
            }
348
0
            Any aText;
349
0
            static constexpr OUString FM_PROP_TEXT = u"Text"_ustr;
350
0
            static constexpr OUString FM_PROP_LABEL = u"Label"_ustr;
351
0
            static constexpr OUString FM_PROP_VALUE = u"Value"_ustr;
352
0
            if ( xPSI->hasPropertyByName( FM_PROP_TEXT ) )
353
0
                aText = xModelProps->getPropertyValue( FM_PROP_TEXT );
354
0
            else if ( xPSI->hasPropertyByName( FM_PROP_LABEL ) )
355
0
                aText = xModelProps->getPropertyValue( FM_PROP_LABEL );
356
0
            else if ( xPSI->hasPropertyByName( FM_PROP_VALUE ) )
357
0
            {
358
0
                double aValue;
359
0
                if (xModelProps->getPropertyValue( FM_PROP_VALUE ) >>= aValue)
360
0
                    aText <<= OUString::number(aValue);
361
0
            }
362
363
0
            if ( aText.hasValue() ) {
364
0
                if( ! (aText >>= Descriptor->Text) ) {
365
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to assign aText to Descriptor->Text");
366
0
                }
367
0
            }
368
369
370
            // readonly
371
0
            static constexpr OUString FM_PROP_READONLY = u"ReadOnly"_ustr;
372
0
            if ( xPSI->hasPropertyByName( FM_PROP_READONLY ) )
373
0
                if( ! (xModelProps->getPropertyValue( FM_PROP_READONLY ) >>= Descriptor->ReadOnly) )
374
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_READONLY);
375
376
377
            // border
378
0
            {
379
0
                static constexpr OUString FM_PROP_BORDER = u"Border"_ustr;
380
0
                if ( xPSI->hasPropertyByName( FM_PROP_BORDER ) )
381
0
                {
382
0
                    sal_Int16 nBorderType = 0;
383
0
                    if( ! (xModelProps->getPropertyValue( FM_PROP_BORDER ) >>= nBorderType) )
384
0
                        SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_BORDER);
385
0
                    Descriptor->Border = ( nBorderType != 0 );
386
387
0
                    OUString sBorderColorPropertyName( u"BorderColor"_ustr );
388
0
                    if ( xPSI->hasPropertyByName( sBorderColorPropertyName ) )
389
0
                    {
390
0
                        Color nBorderColor = COL_TRANSPARENT;
391
0
                        if ( xModelProps->getPropertyValue( sBorderColorPropertyName ) >>= nBorderColor )
392
0
                            Descriptor->BorderColor = nBorderColor;
393
0
                        else
394
0
                            Descriptor->BorderColor = COL_BLACK;
395
0
                    }
396
0
                }
397
0
            }
398
399
400
            // background color
401
0
            static constexpr OUString FM_PROP_BACKGROUNDCOLOR = u"BackgroundColor"_ustr;
402
0
            if ( xPSI->hasPropertyByName( FM_PROP_BACKGROUNDCOLOR ) )
403
0
            {
404
0
                Color nBackColor = COL_TRANSPARENT;
405
0
                xModelProps->getPropertyValue( FM_PROP_BACKGROUNDCOLOR ) >>= nBackColor;
406
0
                Descriptor->Background = true;
407
0
                Descriptor->BackgroundColor = nBackColor;
408
0
            }
409
410
411
            // text color
412
0
            static constexpr OUString FM_PROP_TEXTCOLOR = u"TextColor"_ustr;
413
0
            if ( xPSI->hasPropertyByName( FM_PROP_TEXTCOLOR ) )
414
0
            {
415
0
                Color nTextColor = COL_TRANSPARENT;
416
0
                xModelProps->getPropertyValue( FM_PROP_TEXTCOLOR ) >>= nTextColor;
417
0
                Descriptor->TextColor = nTextColor;
418
0
            }
419
420
421
            // text style
422
0
            Descriptor->TextStyle = DrawTextFlags::NONE;
423
424
            // multi line and word break
425
            // The MultiLine property of the control is mapped to both the "MULTILINE" and
426
            // "WORDBREAK" style flags
427
0
            static constexpr OUString FM_PROP_MULTILINE = u"MultiLine"_ustr;
428
0
            if ( xPSI->hasPropertyByName( FM_PROP_MULTILINE ) )
429
0
            {
430
0
                bool bMultiLine = false;
431
0
                if( ! (xModelProps->getPropertyValue( FM_PROP_MULTILINE ) >>= bMultiLine) )
432
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_MULTILINE);
433
0
                if ( bMultiLine )
434
0
                    Descriptor->TextStyle |= DrawTextFlags::MultiLine | DrawTextFlags::WordBreak;
435
0
            }
436
437
            // horizontal alignment
438
0
            static constexpr OUString FM_PROP_ALIGN = u"Align"_ustr;
439
0
            if ( xPSI->hasPropertyByName( FM_PROP_ALIGN ) )
440
0
            {
441
0
                sal_Int16 nAlign = awt::TextAlign::LEFT;
442
0
                xModelProps->getPropertyValue( FM_PROP_ALIGN ) >>= nAlign;
443
                // TODO: when the property is VOID - are there situations/UIs where this
444
                // means something else than LEFT?
445
0
                switch ( nAlign )
446
0
                {
447
0
                case awt::TextAlign::LEFT:  Descriptor->TextStyle |= DrawTextFlags::Left; break;
448
0
                case awt::TextAlign::CENTER:  Descriptor->TextStyle |= DrawTextFlags::Center; break;
449
0
                case awt::TextAlign::RIGHT:  Descriptor->TextStyle |= DrawTextFlags::Right; break;
450
0
                default:
451
0
                    OSL_FAIL( "describePDFControl: invalid text align!" );
452
0
                }
453
0
            }
454
455
            // vertical alignment
456
0
            {
457
0
                OUString sVertAlignPropertyName( u"VerticalAlign"_ustr );
458
0
                if ( xPSI->hasPropertyByName( sVertAlignPropertyName ) )
459
0
                {
460
0
                    VerticalAlignment nAlign = VerticalAlignment_MIDDLE;
461
0
                    xModelProps->getPropertyValue( sVertAlignPropertyName ) >>= nAlign;
462
0
                    switch ( nAlign )
463
0
                    {
464
0
                    case VerticalAlignment_TOP:  Descriptor->TextStyle |= DrawTextFlags::Top; break;
465
0
                    case VerticalAlignment_MIDDLE:  Descriptor->TextStyle |= DrawTextFlags::VCenter; break;
466
0
                    case VerticalAlignment_BOTTOM:  Descriptor->TextStyle |= DrawTextFlags::Bottom; break;
467
0
                    default:
468
0
                        OSL_FAIL( "describePDFControl: invalid vertical text align!" );
469
0
                    }
470
0
                }
471
0
            }
472
473
            // font
474
0
            static constexpr OUString FM_PROP_FONT = u"FontDescriptor"_ustr;
475
0
            if ( xPSI->hasPropertyByName( FM_PROP_FONT ) )
476
0
            {
477
0
                FontDescriptor aUNOFont;
478
0
                if( ! (xModelProps->getPropertyValue( FM_PROP_FONT ) >>= aUNOFont) )
479
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_FONT);
480
0
                Descriptor->TextFont = CreateFont( aUNOFont );
481
0
            }
482
483
            // tab order
484
0
            OUString aTabIndexString( u"TabIndex"_ustr );
485
0
            if ( xPSI->hasPropertyByName( aTabIndexString ) )
486
0
            {
487
0
                sal_Int16 nIndex = -1;
488
0
                if( ! (xModelProps->getPropertyValue( aTabIndexString ) >>= nIndex) )
489
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << aTabIndexString);
490
0
                Descriptor->TabOrder = nIndex;
491
0
            }
492
493
494
            // special widget properties
495
496
            // edits
497
0
            if ( Descriptor->getType() == vcl::pdf::PDFWriter::Edit )
498
0
            {
499
0
                vcl::pdf::PDFWriter::EditWidget* pEditWidget = static_cast< vcl::pdf::PDFWriter::EditWidget* >( Descriptor.get() );
500
501
                // multiline (already flagged in the TextStyle)
502
0
                pEditWidget->MultiLine = bool( Descriptor->TextStyle & DrawTextFlags::MultiLine );
503
504
                // password input
505
0
                OUString sEchoCharPropName( u"EchoChar"_ustr );
506
0
                if ( xPSI->hasPropertyByName( sEchoCharPropName ) )
507
0
                {
508
0
                    sal_Int16 nEchoChar = 0;
509
0
                    if ( ( xModelProps->getPropertyValue( sEchoCharPropName ) >>= nEchoChar ) && ( nEchoChar != 0 ) )
510
0
                        pEditWidget->Password = true;
511
0
                }
512
513
                // file select
514
0
                if ( xSI->supportsService( u"com.sun.star.form.component.FileControl"_ustr ) )
515
0
                    pEditWidget->FileSelect = true;
516
517
                // maximum text length
518
0
                static constexpr OUString FM_PROP_MAXTEXTLEN = u"MaxTextLen"_ustr;
519
0
                if ( xPSI->hasPropertyByName( FM_PROP_MAXTEXTLEN ) )
520
0
                {
521
0
                    sal_Int16 nMaxTextLength = 0;
522
0
                    if( ! (xModelProps->getPropertyValue( FM_PROP_MAXTEXTLEN ) >>= nMaxTextLength) )
523
0
                        SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_MAXTEXTLEN);
524
0
                    if ( nMaxTextLength <= 0 )
525
                        // "-1" has a special meaning for database-bound controls
526
0
                        nMaxTextLength = 0;
527
0
                    pEditWidget->MaxLen = nMaxTextLength;
528
0
                }
529
530
0
                switch ( nControlType )
531
0
                {
532
0
                    case FormComponentType::CURRENCYFIELD:
533
0
                    case FormComponentType::NUMERICFIELD:
534
0
                    {
535
536
0
                        pEditWidget->Format = vcl::pdf::PDFWriter::Number;
537
538
0
                        static constexpr OUString FM_PROP_CURRENCYSYMBOL = u"CurrencySymbol"_ustr;
539
0
                        if ( xPSI->hasPropertyByName( FM_PROP_CURRENCYSYMBOL ) )
540
0
                        {
541
0
                            OUString sCurrencySymbol;
542
0
                            if( ! (xModelProps->getPropertyValue( FM_PROP_CURRENCYSYMBOL ) >>= sCurrencySymbol) )
543
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_CURRENCYSYMBOL);
544
0
                            pEditWidget->CurrencySymbol = sCurrencySymbol;
545
0
                        }
546
547
0
                        static constexpr OUString FM_PROP_DECIMALACCURACY = u"DecimalAccuracy"_ustr;
548
0
                        if ( xPSI->hasPropertyByName( FM_PROP_DECIMALACCURACY ) )
549
0
                        {
550
0
                            sal_Int32 nDecimalAccuracy = 0;
551
0
                            if( ! (xModelProps->getPropertyValue( FM_PROP_DECIMALACCURACY ) >>= nDecimalAccuracy) )
552
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_DECIMALACCURACY);
553
0
                            pEditWidget->DecimalAccuracy = nDecimalAccuracy;
554
0
                        }
555
556
0
                        static constexpr OUString FM_PROP_PREPENDCURRENCYSYMBOL = u"PrependCurrencySymbol"_ustr;
557
0
                        if ( xPSI->hasPropertyByName( FM_PROP_PREPENDCURRENCYSYMBOL ) )
558
0
                        {
559
0
                            bool bPrependCurrencySymbol = true;
560
0
                            if( ! (xModelProps->getPropertyValue( FM_PROP_PREPENDCURRENCYSYMBOL ) >>= bPrependCurrencySymbol) )
561
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_PREPENDCURRENCYSYMBOL);
562
0
                            pEditWidget->PrependCurrencySymbol = bPrependCurrencySymbol;
563
0
                        }
564
0
                    } break;
565
0
                    case FormComponentType::TIMEFIELD:
566
0
                    {
567
0
                        pEditWidget->Format = vcl::pdf::PDFWriter::Time;
568
569
0
                        static constexpr OUString FM_PROP_TIMEFORMAT = u"TimeFormat"_ustr;
570
0
                        if ( xPSI->hasPropertyByName( FM_PROP_TIMEFORMAT ) )
571
0
                        {
572
0
                            sal_Int32 nTimeFormat = 0;
573
0
                            if( ! (xModelProps->getPropertyValue( FM_PROP_TIMEFORMAT ) >>= nTimeFormat) )
574
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_TIMEFORMAT);
575
576
0
                            switch ( nTimeFormat )
577
0
                            {
578
0
                                case 0:
579
0
                                    pEditWidget->TimeFormat = "HH:MM"; //13:45
580
0
                                    break;
581
0
                                case 1:
582
0
                                    pEditWidget->TimeFormat = "HH:MM:ss"; //13:45:00
583
0
                                    break;
584
0
                                case 2:
585
0
                                    pEditWidget->TimeFormat = "h:MMtt"; //01:45 PM
586
0
                                    break;
587
0
                                case 3:
588
0
                                    pEditWidget->TimeFormat = "h:MM:sstt"; //01:45:00 PM
589
0
                                    break;
590
0
                            }
591
0
                        }
592
0
                    } break;
593
0
                    case FormComponentType::DATEFIELD:
594
0
                    {
595
0
                        pEditWidget->Format = vcl::pdf::PDFWriter::Date;
596
597
0
                        static constexpr OUString FM_PROP_DATEFORMAT = u"DateFormat"_ustr;
598
0
                        if ( xPSI->hasPropertyByName( FM_PROP_DATEFORMAT ) )
599
0
                        {
600
0
                            sal_Int32 nDateFormat = 0;
601
0
                            if( ! (xModelProps->getPropertyValue( FM_PROP_DATEFORMAT ) >>= nDateFormat) )
602
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_DATEFORMAT);
603
604
0
                            switch ( nDateFormat )
605
0
                            {
606
0
                                case 0:
607
0
                                case 1:
608
0
                                    pEditWidget->DateFormat = "mm/dd/yy"; // Standard (short)
609
0
                                    break;
610
0
                                case 2:
611
0
                                case 3:
612
0
                                    pEditWidget->DateFormat = "mm/dd/yyyy"; // Standard (long)
613
0
                                    break;
614
0
                                case 4:
615
0
                                    pEditWidget->DateFormat = "dd/mm/yy"; // DD/MM/YY
616
0
                                    break;
617
0
                                case 5:
618
0
                                    pEditWidget->DateFormat = "mm/dd/yy"; // MM/DD/YY
619
0
                                    break;
620
0
                                case 6:
621
0
                                    pEditWidget->DateFormat = "yy/mm/dd"; // YY/MM/DD
622
0
                                    break;
623
0
                                case 7:
624
0
                                    pEditWidget->DateFormat = "dd/mm/yyyy"; // DD/MM/YYYY
625
0
                                    break;
626
0
                                case 8:
627
0
                                    pEditWidget->DateFormat = "mm/dd/yyyy"; // MM/DD/YYYY
628
0
                                    break;
629
0
                                case 9:
630
0
                                    pEditWidget->DateFormat = "yyyy/mm/dd"; // YYYY/MM/DD
631
0
                                    break;
632
0
                                case 10:
633
0
                                    pEditWidget->DateFormat = "yy-mm-dd"; // YY-MM-DD
634
0
                                    break;
635
0
                                case 11:
636
0
                                    pEditWidget->DateFormat = "yyyy-mm-dd"; // YYYY-MM-DD
637
0
                                    break;
638
0
                            }
639
0
                        }
640
0
                    } break;
641
0
                }
642
0
            }
643
644
            // buttons
645
0
            if ( Descriptor->getType() == vcl::pdf::PDFWriter::PushButton )
646
0
            {
647
0
                vcl::pdf::PDFWriter::PushButtonWidget* pButtonWidget = static_cast< vcl::pdf::PDFWriter::PushButtonWidget* >( Descriptor.get() );
648
0
                FormButtonType eButtonType = FormButtonType_PUSH;
649
0
                if( ! (xModelProps->getPropertyValue(u"ButtonType"_ustr) >>= eButtonType) )
650
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property ButtonType");
651
0
                static constexpr OUString FM_PROP_TARGET_URL = u"TargetURL"_ustr;
652
0
                if ( eButtonType == FormButtonType_SUBMIT )
653
0
                {
654
                    // if a button is a submit button, then it uses the URL at its parent form
655
0
                    Reference< XChild > xChild( xModelProps, UNO_QUERY );
656
0
                    Reference < XPropertySet > xParentProps;
657
0
                    if ( xChild.is() )
658
0
                        xParentProps.set(xChild->getParent(), css::uno::UNO_QUERY);
659
0
                    if ( xParentProps.is() )
660
0
                    {
661
0
                        Reference< XServiceInfo > xParentSI( xParentProps, UNO_QUERY );
662
0
                        if ( xParentSI.is() && xParentSI->supportsService(u"com.sun.star.form.component.HTMLForm"_ustr) )
663
0
                        {
664
0
                            if( ! (xParentProps->getPropertyValue( FM_PROP_TARGET_URL ) >>= pButtonWidget->URL) )
665
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_TARGET_URL);
666
0
                            pButtonWidget->Submit = true;
667
0
                            FormSubmitMethod eMethod = FormSubmitMethod_POST;
668
0
                            if( ! (xParentProps->getPropertyValue(u"SubmitMethod"_ustr) >>= eMethod) )
669
0
                                SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_TARGET_URL);
670
0
                            pButtonWidget->SubmitGet = (eMethod == FormSubmitMethod_GET);
671
0
                        }
672
0
                    }
673
0
                }
674
0
                else if ( eButtonType == FormButtonType_URL )
675
0
                {
676
0
                    OUString sURL;
677
0
                    if( ! (xModelProps->getPropertyValue( FM_PROP_TARGET_URL ) >>= sURL) )
678
0
                        SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_TARGET_URL);
679
0
                    const bool bDocumentLocalTarget = sURL.startsWith("#");
680
0
                    if ( bDocumentLocalTarget )
681
0
                    {
682
                        // Register the destination for future handling ...
683
0
                        pButtonWidget->Dest = i_pdfExportData.RegisterDest();
684
685
                        // and put it into the bookmarks, to ensure the future handling really happens
686
0
                        ::std::vector< vcl::PDFExtOutDevBookmarkEntry >& rBookmarks( i_pdfExportData.GetBookmarks() );
687
0
                        vcl::PDFExtOutDevBookmarkEntry aBookmark;
688
0
                        aBookmark.nDestId = pButtonWidget->Dest;
689
0
                        aBookmark.aBookmark = sURL;
690
0
                        rBookmarks.push_back( aBookmark );
691
0
                    }
692
0
                    else
693
0
                        pButtonWidget->URL = sURL;
694
695
0
                    pButtonWidget->Submit = false;
696
0
                }
697
698
               // TODO:
699
                // In PDF files, buttons are either reset, url or submit buttons. So if we have a simple PUSH button
700
                // in a document, then this means that we do not export a SubmitToURL, which means that in PDF,
701
                // the button is used as reset button.
702
                // Is this desired? If no, we would have to reset Descriptor to NULL here, in case eButtonType
703
                // != FormButtonType_SUBMIT && != FormButtonType_RESET
704
705
                // the PDF exporter defaults the text style, if 0. To prevent this, we have to transfer the UNO
706
                // defaults to the PDF widget
707
0
                if ( pButtonWidget->TextStyle == DrawTextFlags::NONE )
708
0
                    pButtonWidget->TextStyle = DrawTextFlags::Center | DrawTextFlags::VCenter;
709
0
            }
710
711
712
            // check boxes
713
0
            static constexpr OUString FM_PROP_STATE = u"State"_ustr;
714
0
            if ( Descriptor->getType() == vcl::pdf::PDFWriter::CheckBox )
715
0
            {
716
0
                vcl::pdf::PDFWriter::CheckBoxWidget* pCheckBoxWidget = static_cast< vcl::pdf::PDFWriter::CheckBoxWidget* >( Descriptor.get() );
717
0
                sal_Int16 nState = 0;
718
0
                if( ! (xModelProps->getPropertyValue( FM_PROP_STATE ) >>= nState) )
719
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_STATE);
720
0
                pCheckBoxWidget->Checked = ( nState != 0 );
721
722
0
                try
723
0
                {
724
0
                    xModelProps->getPropertyValue( u"RefValue"_ustr ) >>= pCheckBoxWidget->OnValue;
725
0
                }
726
0
                catch(...)
727
0
                {
728
0
                }
729
730
0
                try
731
0
                {
732
0
                    xModelProps->getPropertyValue( u"SecondaryRefValue"_ustr ) >>= pCheckBoxWidget->OffValue;
733
0
                }
734
0
                catch(...)
735
0
                {
736
0
                }
737
0
            }
738
739
740
            // radio buttons
741
0
            if ( Descriptor->getType() == vcl::pdf::PDFWriter::RadioButton )
742
0
            {
743
0
                vcl::pdf::PDFWriter::RadioButtonWidget* pRadioWidget = static_cast< vcl::pdf::PDFWriter::RadioButtonWidget* >( Descriptor.get() );
744
0
                sal_Int16 nState = 0;
745
0
                if( ! (xModelProps->getPropertyValue( FM_PROP_STATE ) >>= nState) )
746
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property " << FM_PROP_STATE);
747
0
                pRadioWidget->Selected = ( nState != 0 );
748
0
                pRadioWidget->RadioGroup = determineRadioGroupId( xModelProps );
749
0
                try
750
0
                {
751
0
                    xModelProps->getPropertyValue( u"RefValue"_ustr ) >>= pRadioWidget->OnValue;
752
0
                }
753
0
                catch(...)
754
0
                {
755
0
                }
756
757
0
                try
758
0
                {
759
0
                    xModelProps->getPropertyValue( u"SecondaryRefValue"_ustr ) >>= pRadioWidget->OffValue;
760
0
                }
761
0
                catch(...)
762
0
                {
763
0
                }
764
0
            }
765
766
767
            // list boxes
768
0
            if ( Descriptor->getType() == vcl::pdf::PDFWriter::ListBox )
769
0
            {
770
0
                vcl::pdf::PDFWriter::ListBoxWidget* pListWidget = static_cast< vcl::pdf::PDFWriter::ListBoxWidget* >( Descriptor.get() );
771
772
                // drop down
773
0
                if( ! (xModelProps->getPropertyValue( u"Dropdown"_ustr ) >>= pListWidget->DropDown) )
774
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property Dropdown");
775
776
                // multi selection
777
0
                if( ! (xModelProps->getPropertyValue(u"MultiSelection"_ustr) >>= pListWidget->MultiSelect) )
778
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property MultiSelection");
779
780
                // entries
781
0
                getStringItemVector( xModelProps, pListWidget->Entries );
782
783
                // get selected items
784
0
                Sequence< sal_Int16 > aSelectIndices;
785
0
                if( ! (xModelProps->getPropertyValue(u"SelectedItems"_ustr) >>= aSelectIndices) )
786
0
                    SAL_WARN("toolkit.helper", "describePDFControl: unable to get property SelectedItems");
787
0
                if( aSelectIndices.hasElements() )
788
0
                {
789
0
                    pListWidget->SelectedEntries.resize( 0 );
790
0
                    auto nEntriesSize = static_cast<sal_Int16>(pListWidget->Entries.size());
791
0
                    std::copy_if(std::cbegin(aSelectIndices), std::cend(aSelectIndices), std::back_inserter(pListWidget->SelectedEntries),
792
0
                        [&nEntriesSize](const sal_Int16 nIndex) { return nIndex >= 0 && nIndex < nEntriesSize; });
793
0
                }
794
0
            }
795
796
797
            // combo boxes
798
0
            if ( Descriptor->getType() == vcl::pdf::PDFWriter::ComboBox )
799
0
            {
800
0
                vcl::pdf::PDFWriter::ComboBoxWidget* pComboWidget = static_cast< vcl::pdf::PDFWriter::ComboBoxWidget* >( Descriptor.get() );
801
802
                // entries
803
0
                getStringItemVector( xModelProps, pComboWidget->Entries );
804
0
            }
805
806
807
            // some post-processing
808
809
            // text line ends
810
            // some controls may (always or dependent on other settings) return UNIX line ends
811
0
            Descriptor->Text = convertLineEnd(Descriptor->Text, LINEEND_CRLF);
812
0
        }
813
0
        catch( const Exception& )
814
0
        {
815
0
            TOOLS_WARN_EXCEPTION( "toolkit", "describePDFControl" );
816
0
        }
817
0
        return Descriptor;
818
0
    }
819
820
821
} // namespace toolkitform
822
823
824
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */