LCOV - code coverage report
Current view: top level - oox/source/ole - vbacontrol.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 259 378 68.5 %
Date: 2014-04-11 Functions: 35 43 81.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10