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