Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/oox/source/ole/vbacontrol.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
#include <oox/ole/vbacontrol.hxx>
21
22
#include <algorithm>
23
#include <set>
24
#include <com/sun/star/awt/XControlModel.hpp>
25
#include <com/sun/star/beans/XPropertySet.hpp>
26
#include <com/sun/star/container/XNameContainer.hpp>
27
#include <com/sun/star/io/XInputStreamProvider.hpp>
28
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
29
#include <com/sun/star/uno/XComponentContext.hpp>
30
#include <osl/diagnose.h>
31
#include <rtl/ustrbuf.hxx>
32
#include <sal/log.hxx>
33
#include <tools/UnitConversion.hxx>
34
#include <xmlscript/xmldlg_imexp.hxx>
35
#include <oox/helper/attributelist.hxx>
36
#include <oox/helper/binaryinputstream.hxx>
37
#include <oox/helper/containerhelper.hxx>
38
#include <oox/helper/propertymap.hxx>
39
#include <oox/helper/propertyset.hxx>
40
#include <oox/helper/storagebase.hxx>
41
#include <oox/helper/textinputstream.hxx>
42
#include <oox/ole/vbahelper.hxx>
43
#include <oox/token/properties.hxx>
44
#include <oox/token/tokens.hxx>
45
#include <unordered_map>
46
47
namespace oox::ole {
48
49
using namespace ::com::sun::star::awt;
50
using namespace ::com::sun::star::container;
51
using namespace ::com::sun::star::frame;
52
using namespace ::com::sun::star::io;
53
using namespace ::com::sun::star::lang;
54
using namespace ::com::sun::star::uno;
55
56
namespace {
57
58
const sal_uInt16 VBA_SITE_CLASSIDINDEX          = 0x8000;
59
const sal_uInt16 VBA_SITE_INDEXMASK             = 0x7FFF;
60
const sal_uInt16 VBA_SITE_FORM                  = 7;
61
const sal_uInt16 VBA_SITE_IMAGE                 = 12;
62
const sal_uInt16 VBA_SITE_FRAME                 = 14;
63
const sal_uInt16 VBA_SITE_SPINBUTTON            = 16;
64
const sal_uInt16 VBA_SITE_COMMANDBUTTON         = 17;
65
const sal_uInt16 VBA_SITE_TABSTRIP              = 18;
66
const sal_uInt16 VBA_SITE_LABEL                 = 21;
67
const sal_uInt16 VBA_SITE_TEXTBOX               = 23;
68
const sal_uInt16 VBA_SITE_LISTBOX               = 24;
69
const sal_uInt16 VBA_SITE_COMBOBOX              = 25;
70
const sal_uInt16 VBA_SITE_CHECKBOX              = 26;
71
const sal_uInt16 VBA_SITE_OPTIONBUTTON          = 27;
72
const sal_uInt16 VBA_SITE_TOGGLEBUTTON          = 28;
73
const sal_uInt16 VBA_SITE_SCROLLBAR             = 47;
74
const sal_uInt16 VBA_SITE_MULTIPAGE             = 57;
75
const sal_uInt16 VBA_SITE_UNKNOWN               = 0x7FFF;
76
77
const sal_uInt32 VBA_SITE_TABSTOP               = 0x00000001;
78
const sal_uInt32 VBA_SITE_VISIBLE               = 0x00000002;
79
const sal_uInt32 VBA_SITE_OSTREAM               = 0x00000010;
80
const sal_uInt32 VBA_SITE_DEFFLAGS              = 0x00000033;
81
82
const sal_uInt8 VBA_SITEINFO_COUNT              = 0x80;
83
const sal_uInt8 VBA_SITEINFO_MASK               = 0x7F;
84
85
/** Collects names of all controls in a user form or container control. Allows
86
    to generate unused names for dummy controls separating option groups.
87
 */
88
class VbaControlNamesSet
89
{
90
public:
91
    explicit            VbaControlNamesSet();
92
93
    /** Inserts the name of the passed control. */
94
    void                insertName( const VbaFormControl& rControl );
95
    /** Returns a name that is not contained in this set. */
96
    OUString            generateDummyName();
97
98
private:
99
    ::std::set< OUString >
100
                        maCtrlNames;
101
    sal_Int32           mnIndex;
102
};
103
104
constexpr OUStringLiteral gaDummyBaseName( u"DummyGroupSep" );
105
106
VbaControlNamesSet::VbaControlNamesSet() :
107
0
    mnIndex( 0 )
108
0
{
109
0
}
110
111
void VbaControlNamesSet::insertName( const VbaFormControl& rControl )
112
0
{
113
0
    OUString aName = rControl.getControlName();
114
0
    if( !aName.isEmpty() )
115
0
        maCtrlNames.insert( aName );
116
0
}
117
118
OUString VbaControlNamesSet::generateDummyName()
119
0
{
120
0
    OUString aCtrlName;
121
0
    do
122
0
    {
123
0
        aCtrlName = gaDummyBaseName + OUString::number( ++mnIndex );
124
0
    }
125
0
    while( maCtrlNames.count( aCtrlName ) > 0 );
126
0
    maCtrlNames.insert( aCtrlName );
127
0
    return aCtrlName;
128
0
}
129
130
/** Functor that inserts the name of a control into a VbaControlNamesSet. */
131
struct VbaControlNameInserter
132
{
133
public:
134
    VbaControlNamesSet& mrCtrlNames;
135
0
    explicit     VbaControlNameInserter( VbaControlNamesSet& rCtrlNames ) : mrCtrlNames( rCtrlNames ) {}
136
0
    void         operator()( const VbaFormControl& rControl ) { mrCtrlNames.insertName( rControl ); }
137
};
138
139
/** A dummy invisible form control (fixed label without text) that is used to
140
    separate two groups of option buttons.
141
 */
142
class VbaDummyFormControl : public VbaFormControl
143
{
144
public:
145
    explicit            VbaDummyFormControl( const OUString& rName );
146
};
147
148
VbaDummyFormControl::VbaDummyFormControl( const OUString& rName )
149
0
{
150
0
    mxSiteModel = std::make_shared<VbaSiteModel>();
151
0
    mxSiteModel->importProperty( XML_Name, rName );
152
0
    mxSiteModel->importProperty( XML_VariousPropertyBits, OUString( '0' ) );
153
154
0
    mxCtrlModel = std::make_shared<AxLabelModel>();
155
0
    mxCtrlModel->setAwtModelMode();
156
0
    mxCtrlModel->importProperty( XML_Size, u"10;10"_ustr );
157
0
}
158
159
} // namespace
160
161
VbaSiteModel::VbaSiteModel() :
162
0
    maPos( 0, 0 ),
163
0
    mnId( 0 ),
164
0
    mnHelpContextId( 0 ),
165
0
    mnFlags( VBA_SITE_DEFFLAGS ),
166
0
    mnStreamLen( 0 ),
167
0
    mnTabIndex( -1 ),
168
0
    mnClassIdOrCache( VBA_SITE_UNKNOWN ),
169
0
    mnGroupId( 0 )
170
0
{
171
0
}
172
173
VbaSiteModel::~VbaSiteModel()
174
0
{
175
0
}
176
177
void VbaSiteModel::importProperty( sal_Int32 nPropId, const OUString& rValue )
178
0
{
179
0
    switch( nPropId )
180
0
    {
181
0
        case XML_Name:                  maName = rValue;                                            break;
182
0
        case XML_Tag:                   maTag = rValue;                                             break;
183
0
        case XML_VariousPropertyBits:   mnFlags = AttributeConversion::decodeUnsigned( rValue );    break;
184
0
    }
185
0
}
186
187
bool VbaSiteModel::importBinaryModel( BinaryInputStream& rInStrm )
188
0
{
189
0
    AxBinaryPropertyReader aReader( rInStrm );
190
0
    aReader.readStringProperty( maName );
191
0
    aReader.readStringProperty( maTag );
192
0
    aReader.readIntProperty< sal_Int32 >( mnId );
193
0
    aReader.readIntProperty< sal_Int32 >( mnHelpContextId );
194
0
    aReader.readIntProperty< sal_uInt32 >( mnFlags );
195
0
    aReader.readIntProperty< sal_uInt32 >( mnStreamLen );
196
0
    aReader.readIntProperty< sal_Int16 >( mnTabIndex );
197
0
    aReader.readIntProperty< sal_uInt16 >( mnClassIdOrCache );
198
0
    aReader.readPairProperty( maPos );
199
0
    aReader.readIntProperty< sal_uInt16 >( mnGroupId );
200
0
    aReader.skipUndefinedProperty();
201
0
    aReader.readStringProperty( maToolTip );
202
0
    aReader.skipStringProperty();   // license key
203
0
    aReader.readStringProperty( maControlSource );
204
0
    aReader.readStringProperty( maRowSource );
205
0
    return aReader.finalizeImport();
206
0
}
207
208
void VbaSiteModel::moveRelative( const AxPairData& rDistance )
209
0
{
210
0
    maPos.first += rDistance.first;
211
0
    maPos.second += rDistance.second;
212
0
}
213
214
bool VbaSiteModel::isContainer() const
215
0
{
216
0
    return !getFlag( mnFlags, VBA_SITE_OSTREAM );
217
0
}
218
219
sal_uInt32 VbaSiteModel::getStreamLength() const
220
0
{
221
0
    return isContainer() ? 0 : mnStreamLen;
222
0
}
223
224
OUString VbaSiteModel::getSubStorageName() const
225
0
{
226
0
    if( mnId >= 0 )
227
0
    {
228
0
        OUStringBuffer aBuffer( "i" );
229
0
        if( mnId < 10 )
230
0
            aBuffer.append( '0' );
231
0
        aBuffer.append( mnId );
232
0
        return aBuffer.makeStringAndClear();
233
0
    }
234
0
    return OUString();
235
0
}
236
237
ControlModelRef VbaSiteModel::createControlModel( const AxClassTable& rClassTable ) const
238
0
{
239
0
    ControlModelRef xCtrlModel;
240
241
0
    sal_Int32 nTypeIndex = static_cast< sal_Int32 >( mnClassIdOrCache & VBA_SITE_INDEXMASK );
242
0
    if( !getFlag( mnClassIdOrCache, VBA_SITE_CLASSIDINDEX ) )
243
0
    {
244
0
        switch( nTypeIndex )
245
0
        {
246
0
            case VBA_SITE_COMMANDBUTTON:    xCtrlModel= std::make_shared<AxCommandButtonModel>();   break;
247
0
            case VBA_SITE_LABEL:            xCtrlModel= std::make_shared<AxLabelModel>();           break;
248
0
            case VBA_SITE_IMAGE:            xCtrlModel= std::make_shared<AxImageModel>();           break;
249
0
            case VBA_SITE_TOGGLEBUTTON:     xCtrlModel= std::make_shared<AxToggleButtonModel>();    break;
250
0
            case VBA_SITE_CHECKBOX:         xCtrlModel= std::make_shared<AxCheckBoxModel>();        break;
251
0
            case VBA_SITE_OPTIONBUTTON:     xCtrlModel= std::make_shared<AxOptionButtonModel>();    break;
252
0
            case VBA_SITE_TEXTBOX:          xCtrlModel= std::make_shared<AxTextBoxModel>();         break;
253
0
            case VBA_SITE_LISTBOX:          xCtrlModel= std::make_shared<AxListBoxModel>();         break;
254
0
            case VBA_SITE_COMBOBOX:         xCtrlModel= std::make_shared<AxComboBoxModel>();        break;
255
0
            case VBA_SITE_SPINBUTTON:       xCtrlModel= std::make_shared<AxSpinButtonModel>();      break;
256
0
            case VBA_SITE_SCROLLBAR:        xCtrlModel= std::make_shared<AxScrollBarModel>();       break;
257
0
            case VBA_SITE_TABSTRIP:         xCtrlModel= std::make_shared<AxTabStripModel>();
258
0
            break;
259
0
            case VBA_SITE_FRAME:            xCtrlModel= std::make_shared<AxFrameModel>();           break;
260
0
            case VBA_SITE_MULTIPAGE:        xCtrlModel= std::make_shared<AxMultiPageModel>();
261
0
            break;
262
0
            case VBA_SITE_FORM:             xCtrlModel= std::make_shared<AxPageModel>();
263
0
            break;
264
0
            default:    OSL_FAIL( "VbaSiteModel::createControlModel - unknown type index" );
265
0
        }
266
0
    }
267
0
    else
268
0
    {
269
0
        const OUString* pGuid = ContainerHelper::getVectorElement( rClassTable, nTypeIndex );
270
0
        OSL_ENSURE( pGuid, "VbaSiteModel::createControlModel - invalid class table index" );
271
0
        if( pGuid )
272
0
        {
273
0
            if( *pGuid == COMCTL_GUID_SCROLLBAR_60 )
274
0
                xCtrlModel = std::make_shared<ComCtlScrollBarModel>( 6 );
275
0
            else if( *pGuid == COMCTL_GUID_PROGRESSBAR_50 )
276
0
                xCtrlModel = std::make_shared<ComCtlProgressBarModel>( 5 );
277
0
            else if( *pGuid == COMCTL_GUID_PROGRESSBAR_60 )
278
0
                xCtrlModel = std::make_shared<ComCtlProgressBarModel>( 6 );
279
0
        }
280
0
    }
281
282
0
    if( xCtrlModel )
283
0
    {
284
        // user form controls are AWT models
285
0
        xCtrlModel->setAwtModelMode();
286
287
        // check that container model matches container flag in site data
288
0
        bool bModelIsContainer = dynamic_cast< const AxContainerModelBase* >( xCtrlModel.get() ) != nullptr;
289
0
        bool bTypeMatch = bModelIsContainer == isContainer();
290
0
        OSL_ENSURE( bTypeMatch, "VbaSiteModel::createControlModel - container type does not match container flag" );
291
0
        if( !bTypeMatch )
292
0
            xCtrlModel.reset();
293
0
    }
294
0
    return xCtrlModel;
295
0
}
296
297
void VbaSiteModel::convertProperties( PropertyMap& rPropMap,
298
        const ControlConverter& rConv, ApiControlType eCtrlType, sal_Int32 nCtrlIndex ) const
299
0
{
300
0
    rPropMap.setProperty( PROP_Name, maName );
301
0
    rPropMap.setProperty( PROP_Tag, maTag );
302
303
0
    if( eCtrlType != API_CONTROL_DIALOG )
304
0
    {
305
0
        rPropMap.setProperty( PROP_HelpText, maToolTip );
306
0
        rPropMap.setProperty( PROP_EnableVisible, getFlag( mnFlags, VBA_SITE_VISIBLE ) );
307
        // we need to set the passed control index to make option button groups work
308
0
        if( (0 <= nCtrlIndex) && (nCtrlIndex <= SAL_MAX_INT16) )
309
0
            rPropMap.setProperty( PROP_TabIndex, static_cast< sal_Int16 >( nCtrlIndex ) );
310
        // progress bar and group box support TabIndex, but not Tabstop...
311
0
        if( (eCtrlType != API_CONTROL_PROGRESSBAR) && (eCtrlType != API_CONTROL_GROUPBOX) && (eCtrlType != API_CONTROL_FRAME) && (eCtrlType != API_CONTROL_PAGE) )
312
0
            rPropMap.setProperty( PROP_Tabstop, getFlag( mnFlags, VBA_SITE_TABSTOP ) );
313
0
        rConv.convertPosition( rPropMap, maPos );
314
0
    }
315
0
}
316
317
VbaFormControl::VbaFormControl()
318
0
{
319
0
}
320
321
VbaFormControl::~VbaFormControl()
322
0
{
323
0
}
324
325
void VbaFormControl::importModelOrStorage( BinaryInputStream& rInStrm, StorageBase& rStrg, const AxClassTable& rClassTable )
326
0
{
327
0
    if( !mxSiteModel )
328
0
        return;
329
330
0
    if( mxSiteModel->isContainer() )
331
0
    {
332
0
        StorageRef xSubStrg = rStrg.openSubStorage( mxSiteModel->getSubStorageName(), false );
333
0
        OSL_ENSURE( xSubStrg, "VbaFormControl::importModelOrStorage - cannot find storage for embedded control" );
334
0
        if( xSubStrg )
335
0
            importStorage( *xSubStrg, rClassTable );
336
0
    }
337
0
    else if( !rInStrm.isEof() )
338
0
    {
339
0
        sal_Int64 nNextStrmPos = rInStrm.tell() + mxSiteModel->getStreamLength();
340
0
        importControlModel( rInStrm, rClassTable );
341
0
        rInStrm.seek( nNextStrmPos );
342
0
    }
343
0
}
344
345
OUString VbaFormControl::getControlName() const
346
0
{
347
0
    return mxSiteModel ? mxSiteModel->getName() : OUString();
348
0
}
349
350
void VbaFormControl::createAndConvert( sal_Int32 nCtrlIndex,
351
        const Reference< XNameContainer >& rxParentNC, const ControlConverter& rConv ) const
352
0
{
353
0
    if( !(rxParentNC.is() && mxSiteModel && mxCtrlModel) )
354
0
        return;
355
356
0
    try
357
0
    {
358
        // create the control model
359
0
        OUString aServiceName = mxCtrlModel->getServiceName();
360
0
        Reference< XMultiServiceFactory > xModelFactory( rxParentNC, UNO_QUERY_THROW );
361
0
        Reference< XControlModel > xCtrlModel( xModelFactory->createInstance( aServiceName ), UNO_QUERY_THROW );
362
363
        // convert all properties and embedded controls
364
0
        if( convertProperties( xCtrlModel, rConv, nCtrlIndex ) )
365
0
        {
366
            // insert into parent container
367
0
            const OUString& rCtrlName = mxSiteModel->getName();
368
0
            OSL_ENSURE( !rxParentNC->hasByName( rCtrlName ), "VbaFormControl::createAndConvert - multiple controls with equal name" );
369
0
            ContainerHelper::insertByName( rxParentNC, rCtrlName, Any( xCtrlModel ) );
370
0
        }
371
0
    }
372
0
    catch(const Exception& )
373
0
    {
374
0
    }
375
0
}
376
377
// protected ------------------------------------------------------------------
378
379
void VbaFormControl::importControlModel( BinaryInputStream& rInStrm, const AxClassTable& rClassTable )
380
0
{
381
0
    createControlModel( rClassTable );
382
0
    if( mxCtrlModel )
383
0
        mxCtrlModel->importBinaryModel( rInStrm );
384
0
}
385
386
void VbaFormControl::importStorage( StorageBase& rStrg, const AxClassTable& rClassTable )
387
0
{
388
0
    createControlModel( rClassTable );
389
0
    AxContainerModelBase* pContainerModel = dynamic_cast< AxContainerModelBase* >( mxCtrlModel.get() );
390
0
    OSL_ENSURE( pContainerModel, "VbaFormControl::importStorage - missing container control model" );
391
0
    if( !pContainerModel )
392
0
        return;
393
394
    /*  Open the 'f' stream containing the model of this control and a list
395
        of site models for all child controls. */
396
0
    BinaryXInputStream aFStrm( rStrg.openInputStream( u"f"_ustr ), true );
397
0
    OSL_ENSURE( !aFStrm.isEof(), "VbaFormControl::importStorage - missing 'f' stream" );
398
399
    /*  Read the properties of this container control and the class table
400
        (into the maClassTable vector) containing a list of GUIDs for
401
        exotic embedded controls. */
402
0
    if( !(!aFStrm.isEof() && pContainerModel->importBinaryModel( aFStrm ) && pContainerModel->importClassTable( aFStrm, maClassTable )) )
403
0
        return;
404
405
    /*  Read the site models of all embedded controls (this fills the
406
        maControls vector). Ignore failure of importSiteModels() but
407
        try to import as much controls as possible. */
408
0
    importEmbeddedSiteModels( aFStrm );
409
    /*  Open the 'o' stream containing models of embedded simple
410
        controls. Stream may be empty or missing, if this control
411
        contains no controls or only container controls. */
412
0
    BinaryXInputStream aOStrm( rStrg.openInputStream( u"o"_ustr ), true );
413
414
    /*  Iterate over all embedded controls, import model from 'o'
415
        stream (for embedded simple controls) or from the substorage
416
        (for embedded container controls). */
417
0
    maControls.forEachMem( &VbaFormControl::importModelOrStorage,
418
0
        ::std::ref( aOStrm ), ::std::ref( rStrg ), ::std::cref( maClassTable ) );
419
420
    // Special handling for multi-page which has non-standard
421
    // containment and additionally needs to re-order Page children
422
0
    if ( pContainerModel->getControlType() == API_CONTROL_MULTIPAGE )
423
0
    {
424
0
        AxMultiPageModel* pMultiPage = dynamic_cast< AxMultiPageModel* >( pContainerModel );
425
0
        assert(pMultiPage);
426
0
        {
427
0
            BinaryXInputStream aXStrm( rStrg.openInputStream( u"x"_ustr ), true );
428
0
            pMultiPage->importPageAndMultiPageProperties( aXStrm, maControls.size() );
429
0
        }
430
0
        typedef std::unordered_map< sal_uInt32, std::shared_ptr< VbaFormControl > > IdToPageMap;
431
0
        IdToPageMap idToPage;
432
0
        AxArrayString sCaptions;
433
434
0
        for (auto const& control : maControls)
435
0
        {
436
0
            auto& elem = control->mxCtrlModel;
437
0
            if (!elem)
438
0
            {
439
0
                SAL_WARN("oox", "empty control model");
440
0
                continue;
441
0
            }
442
0
            if (elem->getControlType() == API_CONTROL_PAGE)
443
0
            {
444
0
                VbaSiteModelRef xPageSiteRef = control->mxSiteModel;
445
0
                if ( xPageSiteRef )
446
0
                    idToPage[ xPageSiteRef->getId() ] = control;
447
0
            }
448
0
            else if (elem->getControlType() == API_CONTROL_TABSTRIP)
449
0
            {
450
0
                AxTabStripModel* pTabStrip = static_cast<AxTabStripModel*>(elem.get());
451
0
                sCaptions = pTabStrip->maItems;
452
0
                pMultiPage->mnActiveTab = pTabStrip->mnListIndex;
453
0
                pMultiPage->mnTabStyle = pTabStrip->mnTabStyle;
454
0
            }
455
0
            else
456
0
            {
457
0
                SAL_WARN("oox", "unexpected control type " << elem->getControlType());
458
0
            }
459
0
        }
460
        // apply caption/titles to pages
461
462
0
        maControls.clear();
463
        // need to sort the controls according to the order of the ids
464
0
        if ( sCaptions.size() == idToPage.size() )
465
0
        {
466
0
            AxArrayString::iterator itCaption = sCaptions.begin();
467
0
            for ( const auto& rCtrlId : pMultiPage->mnIDs )
468
0
            {
469
0
                IdToPageMap::iterator iter = idToPage.find( rCtrlId );
470
0
                if ( iter != idToPage.end() )
471
0
                {
472
0
                    AxPageModel* pPage = static_cast<AxPageModel*> ( iter->second->mxCtrlModel.get() );
473
474
0
                    pPage->importProperty( XML_Caption, *itCaption );
475
0
                    maControls.push_back( iter->second );
476
0
                }
477
0
                ++itCaption;
478
0
            }
479
0
        }
480
0
    }
481
    /*  Reorder the controls (sorts all option buttons of an option
482
        group together), and move all children of all embedded frames
483
        (group boxes) to this control (UNO group boxes cannot contain
484
        other controls). */
485
0
    finalizeEmbeddedControls();
486
0
}
487
488
bool VbaFormControl::convertProperties( const Reference< XControlModel >& rxCtrlModel,
489
        const ControlConverter& rConv, sal_Int32 nCtrlIndex ) const
490
0
{
491
0
    if( rxCtrlModel.is() && mxSiteModel && mxCtrlModel )
492
0
    {
493
0
        const OUString& rCtrlName = mxSiteModel->getName();
494
0
        OSL_ENSURE( !rCtrlName.isEmpty(), "VbaFormControl::convertProperties - control without name" );
495
0
        if( !rCtrlName.isEmpty() )
496
0
        {
497
            // convert all properties
498
0
            PropertyMap aPropMap;
499
0
            mxSiteModel->convertProperties( aPropMap, rConv, mxCtrlModel->getControlType(), nCtrlIndex );
500
0
            rConv.bindToSources( rxCtrlModel, mxSiteModel->getControlSource(), mxSiteModel->getRowSource() );
501
0
            mxCtrlModel->convertProperties( aPropMap, rConv );
502
0
            mxCtrlModel->convertSize( aPropMap, rConv );
503
0
            PropertySet aPropSet( rxCtrlModel );
504
0
            aPropSet.setProperties( aPropMap );
505
506
            // create and convert all embedded controls
507
0
            if( !maControls.empty() ) try
508
0
            {
509
0
                Reference< XNameContainer > xCtrlModelNC( rxCtrlModel, UNO_QUERY_THROW );
510
                /*  Call conversion for all controls. Pass vector index as new
511
                    tab order to make option button groups work correctly. */
512
0
                maControls.forEachMemWithIndex( &VbaFormControl::createAndConvert,
513
0
                    ::std::cref( xCtrlModelNC ), ::std::cref( rConv ) );
514
0
            }
515
0
            catch(const Exception& )
516
0
            {
517
0
                OSL_FAIL( "VbaFormControl::convertProperties - cannot get control container interface" );
518
0
            }
519
520
0
            return true;
521
0
        }
522
0
    }
523
0
    return false;
524
0
}
525
526
// private --------------------------------------------------------------------
527
528
void VbaFormControl::createControlModel( const AxClassTable& rClassTable )
529
0
{
530
    // derived classes may have created their own control model
531
0
    if( !mxCtrlModel && mxSiteModel )
532
0
        mxCtrlModel = mxSiteModel->createControlModel( rClassTable );
533
0
}
534
535
bool VbaFormControl::importSiteModel( BinaryInputStream& rInStrm )
536
0
{
537
0
    mxSiteModel = std::make_shared<VbaSiteModel>();
538
0
    return mxSiteModel->importBinaryModel( rInStrm );
539
0
}
540
541
void VbaFormControl::importEmbeddedSiteModels( BinaryInputStream& rInStrm )
542
0
{
543
0
    sal_uInt64 nAnchorPos = rInStrm.tell();
544
0
    sal_uInt32 nSiteCount, nSiteDataSize;
545
0
    nSiteCount = rInStrm.readuInt32();
546
0
    nSiteDataSize = rInStrm.readuInt32();
547
0
    sal_Int64 nSiteEndPos = rInStrm.tell() + nSiteDataSize;
548
549
    // skip the site info structure
550
0
    sal_uInt32 nSiteIndex = 0;
551
0
    while( !rInStrm.isEof() && (nSiteIndex < nSiteCount) )
552
0
    {
553
0
        rInStrm.skip( 1 ); // site depth
554
0
        sal_uInt8 nTypeCount = rInStrm.readuInt8(); // 'type-or-count' byte
555
0
        if( getFlag( nTypeCount, VBA_SITEINFO_COUNT ) )
556
0
        {
557
            /*  Count flag is set: the 'type-or-count' byte contains the number
558
                of controls in the lower bits, the type specifier follows in
559
                the next byte. The type specifier should always be 1 according
560
                to the specification. */
561
0
            rInStrm.skip( 1 );
562
0
            nSiteIndex += (nTypeCount & VBA_SITEINFO_MASK);
563
0
        }
564
0
        else
565
0
        {
566
            /*  Count flag is not set: the 'type-or-count' byte contains the
567
                type specifier of *one* control in the lower bits (this type
568
                should be 1, see above). */
569
0
            ++nSiteIndex;
570
0
        }
571
0
    }
572
    // align the stream to 32bit, relative to start of entire site info
573
0
    rInStrm.alignToBlock( 4, nAnchorPos );
574
575
    // import the site models for all embedded controls
576
0
    maControls.clear();
577
0
    bool bValid = !rInStrm.isEof();
578
0
    for( nSiteIndex = 0; bValid && (nSiteIndex < nSiteCount); ++nSiteIndex )
579
0
    {
580
0
        VbaFormControlRef xControl = std::make_shared<VbaFormControl>();
581
0
        maControls.push_back( xControl );
582
0
        bValid = xControl->importSiteModel( rInStrm );
583
0
    }
584
585
0
    rInStrm.seek( nSiteEndPos );
586
0
}
587
588
void VbaFormControl::finalizeEmbeddedControls()
589
0
{
590
    /*  This function performs two tasks:
591
592
        1)  Reorder the controls appropriately (sort all option buttons of an
593
            option group together to make grouping work).
594
        2)  Move all children of all embedded frames (group boxes) to this
595
            control (UNO group boxes cannot contain other controls).
596
     */
597
598
    // first, sort all controls by original tab index
599
0
    ::std::sort( maControls.begin(), maControls.end(), &compareByTabIndex );
600
601
    /*  Collect the programmatical names of all embedded controls (needed to be
602
        able to set unused names to new dummy controls created below). Also
603
        collect the names of all children of embedded frames (group boxes).
604
        Luckily, names of controls must be unique in the entire form, not just
605
        in the current container. */
606
0
    VbaControlNamesSet aControlNames;
607
0
    VbaControlNameInserter aInserter( aControlNames );
608
0
    maControls.forEach( aInserter );
609
0
    for (auto const& control : maControls)
610
0
        if( control->mxCtrlModel && (control->mxCtrlModel->getControlType() == API_CONTROL_GROUPBOX) )
611
0
            control->maControls.forEach( aInserter );
612
613
    /*  Reprocess the sorted list and collect all option button controls that
614
        are part of the same option group (determined by group name). All
615
        controls will be stored in a vector of vectors, that collects every
616
        option button group in one vector element, and other controls between
617
        these option groups (or leading or trailing controls) in other vector
618
        elements. If an option button group follows another group, a dummy
619
        separator control has to be inserted. */
620
0
    typedef RefVector< VbaFormControlVector > VbaFormControlVectorVector;
621
0
    VbaFormControlVectorVector aControlGroups;
622
623
0
    typedef RefMap< OUString, VbaFormControlVector > VbaFormControlVectorMap;
624
0
    VbaFormControlVectorMap aOptionGroups;
625
626
0
    typedef VbaFormControlVectorMap::mapped_type VbaFormControlVectorRef;
627
0
    bool bLastWasOptionButton = false;
628
0
    for (auto const& control : maControls)
629
0
    {
630
0
        const ControlModelBase* pCtrlModel = control->mxCtrlModel.get();
631
632
0
        if( const AxOptionButtonModel* pOptButtonModel = dynamic_cast< const AxOptionButtonModel* >( pCtrlModel ) )
633
0
        {
634
            // check if a new option group needs to be created
635
0
            const OUString& rGroupName = pOptButtonModel->getGroupName();
636
0
            VbaFormControlVectorRef& rxOptionGroup = aOptionGroups[ rGroupName ];
637
0
            if( !rxOptionGroup )
638
0
            {
639
                /*  If last control was an option button too, we have two
640
                    option groups following each other, so a dummy separator
641
                    control is needed. */
642
0
                if( bLastWasOptionButton )
643
0
                {
644
0
                    VbaFormControlVectorRef xDummyGroup = std::make_shared<VbaFormControlVector>();
645
0
                    aControlGroups.push_back( xDummyGroup );
646
0
                    OUString aName = aControlNames.generateDummyName();
647
0
                    VbaFormControlRef xDummyControl = std::make_shared<VbaDummyFormControl>( aName );
648
0
                    xDummyGroup->push_back(std::move(xDummyControl));
649
0
                }
650
0
                rxOptionGroup = std::make_shared<VbaFormControlVector>();
651
0
                aControlGroups.push_back( rxOptionGroup );
652
0
            }
653
            /*  Append the option button to the control group (which is now
654
                referred by the vector aControlGroups and by the map
655
                aOptionGroups). */
656
0
            rxOptionGroup->push_back(control);
657
0
            bLastWasOptionButton = true;
658
0
        }
659
0
        else
660
0
        {
661
            // open a new control group, if the last group is an option group
662
0
            if( bLastWasOptionButton || aControlGroups.empty() )
663
0
            {
664
0
                aControlGroups.push_back(std::make_shared<VbaFormControlVector>());
665
0
            }
666
            // append the control to the last control group
667
0
            VbaFormControlVector& rLastGroup = *aControlGroups.back();
668
0
            rLastGroup.push_back(control);
669
0
            bLastWasOptionButton = false;
670
671
            // if control is a group box, move all its children to this control
672
0
            if( pCtrlModel && (pCtrlModel->getControlType() == API_CONTROL_GROUPBOX) )
673
0
            {
674
                /*  Move all embedded controls of the group box relative to the
675
                    position of the group box. */
676
0
                control->moveEmbeddedToAbsoluteParent();
677
                /*  Insert all children of the group box into the last control
678
                    group (following the group box). */
679
0
                rLastGroup.insert( rLastGroup.end(), control->maControls.begin(), control->maControls.end() );
680
0
                control->maControls.clear();
681
                // check if last control of the group box is an option button
682
0
                bLastWasOptionButton = dynamic_cast< const AxOptionButtonModel* >( rLastGroup.back()->mxCtrlModel.get() ) != nullptr;
683
0
            }
684
0
        }
685
0
    }
686
687
    // flatten the vector of vectors of form controls to a single vector
688
0
    maControls.clear();
689
0
    for (auto const& controlGroup : aControlGroups)
690
0
        maControls.insert( maControls.end(), controlGroup->begin(), controlGroup->end() );
691
0
}
692
693
void VbaFormControl::moveRelative( const AxPairData& rDistance )
694
0
{
695
0
    if( mxSiteModel )
696
0
        mxSiteModel->moveRelative( rDistance );
697
0
}
698
699
void VbaFormControl::moveEmbeddedToAbsoluteParent()
700
0
{
701
0
    if( !mxSiteModel || maControls.empty() )
702
0
        return;
703
704
    // distance to move is equal to position of this control in its parent
705
0
    AxPairData aDistance = mxSiteModel->getPosition();
706
707
    /*  For group boxes: add half of the font height to Y position (VBA
708
        positions relative to frame border line, not to 'top' of frame). */
709
0
    const AxFontDataModel* pFontModel = dynamic_cast< const AxFontDataModel* >( mxCtrlModel.get() );
710
0
    if( pFontModel && (pFontModel->getControlType() == API_CONTROL_GROUPBOX) )
711
0
    {
712
0
        sal_Int32 nFontHeight = convertPointToMm100(pFontModel->getFontHeight());
713
0
        aDistance.second += nFontHeight / 2;
714
0
    }
715
716
    // move the embedded controls
717
0
    maControls.forEachMem( &VbaFormControl::moveRelative, ::std::cref( aDistance ) );
718
0
}
719
720
bool VbaFormControl::compareByTabIndex( const VbaFormControlRef& rxLeft, const VbaFormControlRef& rxRight )
721
0
{
722
    // sort controls without model to the end
723
0
    sal_Int32 nLeftTabIndex = rxLeft->mxSiteModel ? rxLeft->mxSiteModel->getTabIndex() : SAL_MAX_INT32;
724
0
    sal_Int32 nRightTabIndex = rxRight->mxSiteModel ? rxRight->mxSiteModel->getTabIndex() : SAL_MAX_INT32;
725
0
    return nLeftTabIndex < nRightTabIndex;
726
0
}
727
728
namespace {
729
730
OUString lclGetQuotedString( std::u16string_view rCodeLine )
731
0
{
732
0
    OUStringBuffer aBuffer;
733
0
    size_t nLen = rCodeLine.size();
734
0
    if( (nLen > 0) && (rCodeLine[ 0 ] == '"') )
735
0
    {
736
0
        bool bExitLoop = false;
737
0
        for( size_t nIndex = 1; !bExitLoop && (nIndex < nLen); ++nIndex )
738
0
        {
739
0
            sal_Unicode cChar = rCodeLine[ nIndex ];
740
            // exit on closing quote char (but check on double quote chars)
741
0
            bExitLoop = (cChar == '"') && ((nIndex + 1 == nLen) || (rCodeLine[ nIndex + 1 ] != '"'));
742
0
            if( !bExitLoop )
743
0
            {
744
0
                aBuffer.append( cChar );
745
                // skip second quote char
746
0
                if( cChar == '"' )
747
0
                    ++nIndex;
748
0
            }
749
0
        }
750
0
    }
751
0
    return aBuffer.makeStringAndClear();
752
0
}
753
754
bool lclEatWhitespace( OUString& rCodeLine )
755
0
{
756
0
    sal_Int32 nIndex = 0;
757
0
    while( (nIndex < rCodeLine.getLength()) && ((rCodeLine[ nIndex ] == ' ') || (rCodeLine[ nIndex ] == '\t')) )
758
0
        ++nIndex;
759
0
    if( nIndex > 0 )
760
0
    {
761
0
        rCodeLine = rCodeLine.copy( nIndex );
762
0
        return true;
763
0
    }
764
0
    return false;
765
0
}
766
767
bool lclEatKeyword( OUString& rCodeLine, std::u16string_view rKeyword )
768
0
{
769
0
    if( rCodeLine.matchIgnoreAsciiCase( rKeyword ) )
770
0
    {
771
0
        rCodeLine = rCodeLine.copy( rKeyword.size() );
772
        // success, if code line ends after keyword, or if whitespace follows
773
0
        return rCodeLine.isEmpty() || lclEatWhitespace( rCodeLine );
774
0
    }
775
0
    return false;
776
0
}
777
778
} // namespace
779
780
VbaUserForm::VbaUserForm( const Reference< XComponentContext >& rxContext,
781
        const Reference< XModel >& rxDocModel, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr ) :
782
0
    mxContext( rxContext ),
783
0
    mxDocModel( rxDocModel ),
784
0
    maConverter( rxDocModel, rGraphicHelper, bDefaultColorBgr )
785
0
{
786
0
    OSL_ENSURE( mxContext.is(), "VbaUserForm::VbaUserForm - missing component context" );
787
0
    OSL_ENSURE( mxDocModel.is(), "VbaUserForm::VbaUserForm - missing document model" );
788
0
}
789
790
void VbaUserForm::importForm( const Reference< XNameContainer >& rxDialogLib,
791
                           StorageBase& rVbaFormStrg, const OUString& rModuleName, rtl_TextEncoding eTextEnc )
792
0
{
793
0
    OSL_ENSURE( rxDialogLib.is(), "VbaUserForm::importForm - missing dialog library" );
794
0
    if( !mxContext.is() || !mxDocModel.is() || !rxDialogLib.is() )
795
0
        return;
796
797
    // check that the '03VBFrame' stream exists, this is required for forms
798
0
    BinaryXInputStream aInStrm( rVbaFormStrg.openInputStream( u"\003VBFrame"_ustr ), true );
799
0
    OSL_ENSURE( !aInStrm.isEof(), "VbaUserForm::importForm - missing \\003VBFrame stream" );
800
0
    if( aInStrm.isEof() )
801
0
        return;
802
803
    // scan for the line 'Begin {GUID} <FormName>'
804
0
    TextInputStream aFrameTextStrm( mxContext, aInStrm, eTextEnc );
805
0
    static constexpr OUStringLiteral aBegin = u"Begin";
806
0
    OUString aLine;
807
0
    bool bBeginFound = false;
808
0
    while( !bBeginFound && !aFrameTextStrm.isEof() )
809
0
    {
810
0
        aLine = aFrameTextStrm.readLine().trim();
811
0
        bBeginFound = lclEatKeyword( aLine, aBegin );
812
0
    }
813
    // check for the specific GUID that represents VBA forms
814
0
    if( !bBeginFound || !lclEatKeyword( aLine, u"{C62A69F0-16DC-11CE-9E98-00AA00574A4F}" ) )
815
0
        return;
816
817
    // remaining line is the form name
818
0
    OUString aFormName = aLine.trim();
819
0
    OSL_ENSURE( !aFormName.isEmpty(), "VbaUserForm::importForm - missing form name" );
820
0
    OSL_ENSURE( rModuleName.equalsIgnoreAsciiCase( aFormName ), "VbaUserForm::importFrameStream - form and module name mismatch" );
821
0
    if( aFormName.isEmpty() )
822
0
        aFormName = rModuleName;
823
0
    if( aFormName.isEmpty() )
824
0
        return;
825
0
    mxSiteModel = std::make_shared<VbaSiteModel>();
826
0
    mxSiteModel->importProperty( XML_Name, aFormName );
827
828
    // read the form properties (caption is contained in this '03VBFrame' stream, not in the 'f' stream)
829
0
    mxCtrlModel = std::make_shared<AxUserFormModel>();
830
0
    OUString aKey, aValue;
831
0
    bool bExitLoop = false;
832
0
    while( !bExitLoop && !aFrameTextStrm.isEof() )
833
0
    {
834
0
        aLine = aFrameTextStrm.readLine().trim();
835
0
        bExitLoop = aLine.equalsIgnoreAsciiCase( "End" );
836
0
        if( !bExitLoop && VbaHelper::extractKeyValue( aKey, aValue, aLine ) )
837
0
        {
838
0
            if( aKey.equalsIgnoreAsciiCase( "Caption" ) )
839
0
                mxCtrlModel->importProperty( XML_Caption, lclGetQuotedString( aValue ) );
840
0
            else if( aKey.equalsIgnoreAsciiCase( "Tag" ) )
841
0
                mxSiteModel->importProperty( XML_Tag, lclGetQuotedString( aValue ) );
842
0
        }
843
0
    }
844
845
    // use generic container control functionality to import the embedded controls
846
0
    importStorage( rVbaFormStrg, AxClassTable() );
847
848
0
    try
849
0
    {
850
        // create the dialog model
851
0
        OUString aServiceName = mxCtrlModel->getServiceName();
852
0
        Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY_THROW );
853
0
        Reference< XControlModel > xDialogModel( xFactory->createInstance( aServiceName ), UNO_QUERY_THROW );
854
0
        Reference< XNameContainer > xDialogNC( xDialogModel, UNO_QUERY_THROW );
855
856
        // convert properties and embedded controls
857
0
        if( convertProperties( xDialogModel, maConverter, 0 ) )
858
0
        {
859
            // export the dialog to XML and insert it into the dialog library
860
0
            Reference< XInputStreamProvider > xDialogSource( ::xmlscript::exportDialogModel( xDialogNC, mxContext, mxDocModel ), UNO_SET_THROW );
861
0
            OSL_ENSURE( !rxDialogLib->hasByName( aFormName ), "VbaUserForm::importForm - multiple dialogs with equal name" );
862
0
            ContainerHelper::insertByName( rxDialogLib, aFormName, Any( xDialogSource ) );
863
0
        }
864
0
    }
865
0
    catch(const Exception& )
866
0
    {
867
0
    }
868
0
}
869
870
} // namespace oox
871
872
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */