LCOV - code coverage report
Current view: top level - oox/source/ole - vbacontrol.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 378 0.0 %
Date: 2014-04-14 Functions: 0 43 0.0 %
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           0 : 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           0 : VbaControlNamesSet::VbaControlNamesSet() :
     103             :     maDummyBaseName( "DummyGroupSep" ),
     104           0 :     mnIndex( 0 )
     105             : {
     106           0 : }
     107             : 
     108           0 : void VbaControlNamesSet::insertName( const VbaFormControl& rControl )
     109             : {
     110           0 :     OUString aName = rControl.getControlName();
     111           0 :     if( !aName.isEmpty() )
     112           0 :         maCtrlNames.insert( aName );
     113           0 : }
     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           0 :     inline explicit     VbaControlNameInserter( VbaControlNamesSet& rCtrlNames ) : mrCtrlNames( rCtrlNames ) {}
     135           0 :     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           0 : 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           0 :     mnGroupId( 0 )
     173             : {
     174           0 : }
     175             : 
     176           0 : VbaSiteModel::~VbaSiteModel()
     177             : {
     178           0 : }
     179             : 
     180           0 : void VbaSiteModel::importProperty( sal_Int32 nPropId, const OUString& rValue )
     181             : {
     182           0 :     switch( nPropId )
     183             :     {
     184           0 :         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           0 : }
     189             : 
     190           0 : bool VbaSiteModel::importBinaryModel( BinaryInputStream& rInStrm )
     191             : {
     192           0 :     AxBinaryPropertyReader aReader( rInStrm );
     193           0 :     aReader.readStringProperty( maName );
     194           0 :     aReader.readStringProperty( maTag );
     195           0 :     aReader.readIntProperty< sal_Int32 >( mnId );
     196           0 :     aReader.readIntProperty< sal_Int32 >( mnHelpContextId );
     197           0 :     aReader.readIntProperty< sal_uInt32 >( mnFlags );
     198           0 :     aReader.readIntProperty< sal_uInt32 >( mnStreamLen );
     199           0 :     aReader.readIntProperty< sal_Int16 >( mnTabIndex );
     200           0 :     aReader.readIntProperty< sal_uInt16 >( mnClassIdOrCache );
     201           0 :     aReader.readPairProperty( maPos );
     202           0 :     aReader.readIntProperty< sal_uInt16 >( mnGroupId );
     203           0 :     aReader.skipUndefinedProperty();
     204           0 :     aReader.readStringProperty( maToolTip );
     205           0 :     aReader.skipStringProperty();   // license key
     206           0 :     aReader.readStringProperty( maControlSource );
     207           0 :     aReader.readStringProperty( maRowSource );
     208           0 :     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           0 : bool VbaSiteModel::isContainer() const
     218             : {
     219           0 :     return !getFlag( mnFlags, VBA_SITE_OSTREAM );
     220             : }
     221             : 
     222           0 : sal_uInt32 VbaSiteModel::getStreamLength() const
     223             : {
     224           0 :     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           0 : ControlModelRef VbaSiteModel::createControlModel( const AxClassTable& rClassTable ) const
     242             : {
     243           0 :     ControlModelRef xCtrlModel;
     244             : 
     245           0 :     sal_Int32 nTypeIndex = static_cast< sal_Int32 >( mnClassIdOrCache & VBA_SITE_INDEXMASK );
     246           0 :     if( !getFlag( mnClassIdOrCache, VBA_SITE_CLASSIDINDEX ) )
     247             :     {
     248           0 :         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           0 :             case VBA_SITE_TOGGLEBUTTON:     xCtrlModel.reset( new AxToggleButtonModel );    break;
     254           0 :             case VBA_SITE_CHECKBOX:         xCtrlModel.reset( new AxCheckBoxModel );        break;
     255           0 :             case VBA_SITE_OPTIONBUTTON:     xCtrlModel.reset( new AxOptionButtonModel );    break;
     256           0 :             case VBA_SITE_TEXTBOX:          xCtrlModel.reset( new AxTextBoxModel );         break;
     257           0 :             case VBA_SITE_LISTBOX:          xCtrlModel.reset( new AxListBoxModel );         break;
     258           0 :             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           0 :     if( xCtrlModel.get() )
     287             :     {
     288             :         // user form controls are AWT models
     289           0 :         xCtrlModel->setAwtModelMode();
     290             : 
     291             :         // check that container model matches container flag in site data
     292           0 :         bool bModelIsContainer = dynamic_cast< const AxContainerModelBase* >( xCtrlModel.get() ) != 0;
     293           0 :         bool bTypeMatch = bModelIsContainer == isContainer();
     294             :         OSL_ENSURE( bTypeMatch, "VbaSiteModel::createControlModel - container type does not match container flag" );
     295           0 :         if( !bTypeMatch )
     296           0 :             xCtrlModel.reset();
     297             :     }
     298           0 :     return xCtrlModel;
     299             : }
     300             : 
     301           0 : void VbaSiteModel::convertProperties( PropertyMap& rPropMap,
     302             :         const ControlConverter& rConv, ApiControlType eCtrlType, sal_Int32 nCtrlIndex ) const
     303             : {
     304           0 :     rPropMap.setProperty( PROP_Name, maName );
     305           0 :     rPropMap.setProperty( PROP_Tag, maTag );
     306             : 
     307           0 :     if( eCtrlType != API_CONTROL_DIALOG )
     308             :     {
     309           0 :         rPropMap.setProperty( PROP_HelpText, maToolTip );
     310           0 :         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           0 :         if( (0 <= nCtrlIndex) && (nCtrlIndex <= SAL_MAX_INT16) )
     313           0 :             rPropMap.setProperty( PROP_TabIndex, static_cast< sal_Int16 >( nCtrlIndex ) );
     314             :         // progress bar and group box support TabIndex, but not Tabstop...
     315           0 :         if( (eCtrlType != API_CONTROL_PROGRESSBAR) && (eCtrlType != API_CONTROL_GROUPBOX) && (eCtrlType != API_CONTROL_FRAME) && (eCtrlType != API_CONTROL_PAGE) )
     316           0 :             rPropMap.setProperty( PROP_Tabstop, getFlag( mnFlags, VBA_SITE_TABSTOP ) );
     317           0 :         rConv.convertPosition( rPropMap, maPos );
     318             :     }
     319           0 : }
     320             : 
     321             : 
     322             : 
     323           0 : VbaFormControl::VbaFormControl()
     324             : {
     325           0 : }
     326             : 
     327           0 : VbaFormControl::~VbaFormControl()
     328             : {
     329           0 : }
     330             : 
     331           0 : void VbaFormControl::importModelOrStorage( BinaryInputStream& rInStrm, StorageBase& rStrg, const AxClassTable& rClassTable )
     332             : {
     333           0 :     if( mxSiteModel.get() )
     334             :     {
     335           0 :         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           0 :         else if( !rInStrm.isEof() )
     343             :         {
     344           0 :             sal_Int64 nNextStrmPos = rInStrm.tell() + mxSiteModel->getStreamLength();
     345           0 :             importControlModel( rInStrm, rClassTable );
     346           0 :             rInStrm.seek( nNextStrmPos );
     347             :         }
     348             :     }
     349           0 : }
     350             : 
     351           0 : OUString VbaFormControl::getControlName() const
     352             : {
     353           0 :     return mxSiteModel.get() ? mxSiteModel->getName() : OUString();
     354             : }
     355             : 
     356           0 : void VbaFormControl::createAndConvert( sal_Int32 nCtrlIndex,
     357             :         const Reference< XNameContainer >& rxParentNC, const ControlConverter& rConv ) const
     358             : {
     359           0 :     if( rxParentNC.is() && mxSiteModel.get() && mxCtrlModel.get() ) try
     360             :     {
     361             :         // create the control model
     362           0 :         OUString aServiceName = mxCtrlModel->getServiceName();
     363           0 :         Reference< XMultiServiceFactory > xModelFactory( rxParentNC, UNO_QUERY_THROW );
     364           0 :         Reference< XControlModel > xCtrlModel( xModelFactory->createInstance( aServiceName ), UNO_QUERY_THROW );
     365             : 
     366             :         // convert all properties and embedded controls
     367           0 :         if( convertProperties( xCtrlModel, rConv, nCtrlIndex ) )
     368             :         {
     369             :             // insert into parent container
     370           0 :             const OUString& rCtrlName = mxSiteModel->getName();
     371             :             OSL_ENSURE( !rxParentNC->hasByName( rCtrlName ), "VbaFormControl::createAndConvert - multiple controls with equal name" );
     372           0 :             ContainerHelper::insertByName( rxParentNC, rCtrlName, Any( xCtrlModel ) );
     373           0 :         }
     374             :     }
     375           0 :     catch(const Exception& )
     376             :     {
     377             :     }
     378           0 : }
     379             : 
     380             : // protected ------------------------------------------------------------------
     381             : 
     382           0 : void VbaFormControl::importControlModel( BinaryInputStream& rInStrm, const AxClassTable& rClassTable )
     383             : {
     384           0 :     createControlModel( rClassTable );
     385           0 :     if( mxCtrlModel.get() )
     386           0 :         mxCtrlModel->importBinaryModel( rInStrm );
     387           0 : }
     388             : 
     389           0 : void VbaFormControl::importStorage( StorageBase& rStrg, const AxClassTable& rClassTable )
     390             : {
     391           0 :     createControlModel( rClassTable );
     392           0 :     AxContainerModelBase* pContainerModel = dynamic_cast< AxContainerModelBase* >( mxCtrlModel.get() );
     393             :     OSL_ENSURE( pContainerModel, "VbaFormControl::importStorage - missing container control model" );
     394           0 :     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           0 :         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           0 :         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           0 :             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           0 :             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           0 :                 ::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           0 :             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           0 :             finalizeEmbeddedControls();
     478           0 :         }
     479             :     }
     480           0 : }
     481             : 
     482           0 : bool VbaFormControl::convertProperties( const Reference< XControlModel >& rxCtrlModel,
     483             :         const ControlConverter& rConv, sal_Int32 nCtrlIndex ) const
     484             : {
     485           0 :     if( rxCtrlModel.is() && mxSiteModel.get() && mxCtrlModel.get() )
     486             :     {
     487           0 :         const OUString& rCtrlName = mxSiteModel->getName();
     488             :         OSL_ENSURE( !rCtrlName.isEmpty(), "VbaFormControl::convertProperties - control without name" );
     489           0 :         if( !rCtrlName.isEmpty() )
     490             :         {
     491             :             // convert all properties
     492           0 :             PropertyMap aPropMap;
     493           0 :             mxSiteModel->convertProperties( aPropMap, rConv, mxCtrlModel->getControlType(), nCtrlIndex );
     494           0 :             rConv.bindToSources( rxCtrlModel, mxSiteModel->getControlSource(), mxSiteModel->getRowSource() );
     495           0 :             mxCtrlModel->convertProperties( aPropMap, rConv );
     496           0 :             mxCtrlModel->convertSize( aPropMap, rConv );
     497           0 :             PropertySet aPropSet( rxCtrlModel );
     498           0 :             aPropSet.setProperties( aPropMap );
     499             : 
     500             :             // create and convert all embedded controls
     501           0 :             if( !maControls.empty() ) try
     502             :             {
     503           0 :                 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           0 :                     ::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           0 :             return true;
     515             :         }
     516             :     }
     517           0 :     return false;
     518             : }
     519             : 
     520             : // private --------------------------------------------------------------------
     521             : 
     522           0 : void VbaFormControl::createControlModel( const AxClassTable& rClassTable )
     523             : {
     524             :     // derived classes may have created their own control model
     525           0 :     if( !mxCtrlModel && mxSiteModel.get() )
     526           0 :         mxCtrlModel = mxSiteModel->createControlModel( rClassTable );
     527           0 : }
     528             : 
     529           0 : bool VbaFormControl::importSiteModel( BinaryInputStream& rInStrm )
     530             : {
     531           0 :     mxSiteModel.reset( new VbaSiteModel );
     532           0 :     return mxSiteModel->importBinaryModel( rInStrm );
     533             : }
     534             : 
     535           0 : bool VbaFormControl::importEmbeddedSiteModels( BinaryInputStream& rInStrm )
     536             : {
     537           0 :     sal_uInt64 nAnchorPos = rInStrm.tell();
     538             :     sal_uInt32 nSiteCount, nSiteDataSize;
     539           0 :     rInStrm >> nSiteCount >> nSiteDataSize;
     540           0 :     sal_Int64 nSiteEndPos = rInStrm.tell() + nSiteDataSize;
     541             : 
     542             :     // skip the site info structure
     543           0 :     sal_uInt32 nSiteIndex = 0;
     544           0 :     while( !rInStrm.isEof() && (nSiteIndex < nSiteCount) )
     545             :     {
     546           0 :         rInStrm.skip( 1 ); // site depth
     547           0 :         sal_uInt8 nTypeCount = rInStrm.readuInt8(); // 'type-or-count' byte
     548           0 :         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           0 :             rInStrm.skip( 1 );
     555           0 :             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           0 :     rInStrm.alignToBlock( 4, nAnchorPos );
     567             : 
     568             :     // import the site models for all embedded controls
     569           0 :     maControls.clear();
     570           0 :     bool bValid = !rInStrm.isEof();
     571           0 :     for( nSiteIndex = 0; bValid && (nSiteIndex < nSiteCount); ++nSiteIndex )
     572             :     {
     573           0 :         VbaFormControlRef xControl( new VbaFormControl );
     574           0 :         maControls.push_back( xControl );
     575           0 :         bValid = xControl->importSiteModel( rInStrm );
     576           0 :     }
     577             : 
     578           0 :     rInStrm.seek( nSiteEndPos );
     579           0 :     return bValid;
     580             : }
     581             : 
     582           0 : 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           0 :     ::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           0 :     VbaControlNamesSet aControlNames;
     601           0 :     VbaControlNameInserter aInserter( aControlNames );
     602           0 :     maControls.forEach( aInserter );
     603           0 :     for( VbaFormControlVector::iterator aIt = maControls.begin(), aEnd = maControls.end(); aIt != aEnd; ++aIt )
     604           0 :         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           0 :     VbaFormControlVectorVector aControlGroups;
     616             : 
     617             :     typedef RefMap< OUString, VbaFormControlVector > VbaFormControlVectorMap;
     618           0 :     VbaFormControlVectorMap aOptionGroups;
     619             : 
     620             :     typedef VbaFormControlVectorMap::mapped_type VbaFormControlVectorRef;
     621           0 :     bool bLastWasOptionButton = false;
     622           0 :     for( VbaFormControlVector::iterator aIt = maControls.begin(), aEnd = maControls.end(); aIt != aEnd; ++aIt )
     623             :     {
     624           0 :         VbaFormControlRef xControl = *aIt;
     625           0 :         const ControlModelBase* pCtrlModel = xControl->mxCtrlModel.get();
     626             : 
     627           0 :         if( const AxOptionButtonModel* pOptButtonModel = dynamic_cast< const AxOptionButtonModel* >( pCtrlModel ) )
     628             :         {
     629             :             // check if a new option group needs to be created
     630           0 :             const OUString& rGroupName = pOptButtonModel->getGroupName();
     631           0 :             VbaFormControlVectorRef& rxOptionGroup = aOptionGroups[ rGroupName ];
     632           0 :             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           0 :                 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           0 :                 rxOptionGroup.reset( new VbaFormControlVector );
     646           0 :                 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           0 :             rxOptionGroup->push_back( xControl );
     652           0 :             bLastWasOptionButton = true;
     653             :         }
     654             :         else
     655             :         {
     656             :             // open a new control group, if the last group is an option group
     657           0 :             if( bLastWasOptionButton || aControlGroups.empty() )
     658             :             {
     659           0 :                 VbaFormControlVectorRef xControlGroup( new VbaFormControlVector );
     660           0 :                 aControlGroups.push_back( xControlGroup );
     661             :             }
     662             :             // append the control to the last control group
     663           0 :             VbaFormControlVector& rLastGroup = *aControlGroups.back();
     664           0 :             rLastGroup.push_back( xControl );
     665           0 :             bLastWasOptionButton = false;
     666             : 
     667             :             // if control is a group box, move all its children to this control
     668           0 :             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           0 :     }
     682             : 
     683             :     // flatten the vector of vectors of form controls to a single vector
     684           0 :     maControls.clear();
     685           0 :     for( VbaFormControlVectorVector::iterator aIt = aControlGroups.begin(), aEnd = aControlGroups.end(); aIt != aEnd; ++aIt )
     686           0 :         maControls.insert( maControls.end(), (*aIt)->begin(), (*aIt)->end() );
     687           0 : }
     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           0 : bool VbaFormControl::compareByTabIndex( const VbaFormControlRef& rxLeft, const VbaFormControlRef& rxRight )
     718             : {
     719             :     // sort controls without model to the end
     720           0 :     sal_Int32 nLeftTabIndex = rxLeft->mxSiteModel.get() ? rxLeft->mxSiteModel->getTabIndex() : SAL_MAX_INT32;
     721           0 :     sal_Int32 nRightTabIndex = rxRight->mxSiteModel.get() ? rxRight->mxSiteModel->getTabIndex() : SAL_MAX_INT32;
     722           0 :     return nLeftTabIndex < nRightTabIndex;
     723             : }
     724             : 
     725             : 
     726             : 
     727             : namespace {
     728             : 
     729           0 : OUString lclGetQuotedString( const OUString& rCodeLine )
     730             : {
     731           0 :     OUStringBuffer aBuffer;
     732           0 :     sal_Int32 nLen = rCodeLine.getLength();
     733           0 :     if( (nLen > 0) && (rCodeLine[ 0 ] == '"') )
     734             :     {
     735           0 :         bool bExitLoop = false;
     736           0 :         for( sal_Int32 nIndex = 1; !bExitLoop && (nIndex < nLen); ++nIndex )
     737             :         {
     738           0 :             sal_Unicode cChar = rCodeLine[ nIndex ];
     739             :             // exit on closing quote char (but check on double quote chars)
     740           0 :             bExitLoop = (cChar == '"') && ((nIndex + 1 == nLen) || (rCodeLine[ nIndex + 1 ] != '"'));
     741           0 :             if( !bExitLoop )
     742             :             {
     743           0 :                 aBuffer.append( cChar );
     744             :                 // skip second quote char
     745           0 :                 if( cChar == '"' )
     746           0 :                     ++nIndex;
     747             :             }
     748             :         }
     749             :     }
     750           0 :     return aBuffer.makeStringAndClear();
     751             : }
     752             : 
     753           0 : bool lclEatWhitespace( OUString& rCodeLine )
     754             : {
     755           0 :     sal_Int32 nIndex = 0;
     756           0 :     while( (nIndex < rCodeLine.getLength()) && ((rCodeLine[ nIndex ] == ' ') || (rCodeLine[ nIndex ] == '\t')) )
     757           0 :         ++nIndex;
     758           0 :     if( nIndex > 0 )
     759             :     {
     760           0 :         rCodeLine = rCodeLine.copy( nIndex );
     761           0 :         return true;
     762             :     }
     763           0 :     return false;
     764             : }
     765             : 
     766           0 : bool lclEatKeyword( OUString& rCodeLine, const OUString& rKeyword )
     767             : {
     768           0 :     if( rCodeLine.matchIgnoreAsciiCase( rKeyword ) )
     769             :     {
     770           0 :         rCodeLine = rCodeLine.copy( rKeyword.getLength() );
     771             :         // success, if code line ends after keyword, or if whitespace follows
     772           0 :         return rCodeLine.isEmpty() || lclEatWhitespace( rCodeLine );
     773             :     }
     774           0 :     return false;
     775             : }
     776             : 
     777             : } // namespace
     778             : 
     779             : 
     780             : 
     781           0 : VbaUserForm::VbaUserForm( const Reference< XComponentContext >& rxContext,
     782             :         const Reference< XModel >& rxDocModel, const GraphicHelper& rGraphicHelper, bool bDefaultColorBgr ) :
     783             :     mxContext( rxContext ),
     784             :     mxDocModel( rxDocModel ),
     785           0 :     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           0 : }
     790             : 
     791           0 : 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           0 :     if( !mxContext.is() || !mxDocModel.is() || !rxDialogLib.is() )
     796           0 :         return;
     797             : 
     798             :     // check that the '03VBFrame' stream exists, this is required for forms
     799           0 :     BinaryXInputStream aInStrm( rVbaFormStrg.openInputStream( "\003VBFrame" ), true );
     800             :     OSL_ENSURE( !aInStrm.isEof(), "VbaUserForm::importForm - missing \\003VBFrame stream" );
     801           0 :     if( aInStrm.isEof() )
     802           0 :         return;
     803             : 
     804             :     // scan for the line 'Begin {GUID} <FormName>'
     805           0 :     TextInputStream aFrameTextStrm( mxContext, aInStrm, eTextEnc );
     806           0 :     const OUString aBegin = "Begin";
     807           0 :     OUString aLine;
     808           0 :     bool bBeginFound = false;
     809           0 :     while( !bBeginFound && !aFrameTextStrm.isEof() )
     810             :     {
     811           0 :         aLine = aFrameTextStrm.readLine().trim();
     812           0 :         bBeginFound = lclEatKeyword( aLine, aBegin );
     813             :     }
     814             :     // check for the specific GUID that represents VBA forms
     815           0 :     if( !bBeginFound || !lclEatKeyword( aLine, "{C62A69F0-16DC-11CE-9E98-00AA00574A4F}" ) )
     816           0 :         return;
     817             : 
     818             :     // remaining line is the form name
     819           0 :     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           0 :     if( aFormName.isEmpty() )
     823           0 :         aFormName = rModuleName;
     824           0 :     if( aFormName.isEmpty() )
     825           0 :         return;
     826           0 :     mxSiteModel.reset( new VbaSiteModel );
     827           0 :     mxSiteModel->importProperty( XML_Name, aFormName );
     828             : 
     829             :     // read the form properties (caption is contained in this '03VBFrame' stream, not in the 'f' stream)
     830           0 :     mxCtrlModel.reset( new AxUserFormModel );
     831           0 :     OUString aKey, aValue;
     832           0 :     bool bExitLoop = false;
     833           0 :     while( !bExitLoop && !aFrameTextStrm.isEof() )
     834             :     {
     835           0 :         aLine = aFrameTextStrm.readLine().trim();
     836           0 :         bExitLoop = aLine.equalsIgnoreAsciiCase( "End" );
     837           0 :         if( !bExitLoop && VbaHelper::extractKeyValue( aKey, aValue, aLine ) )
     838             :         {
     839           0 :             if( aKey.equalsIgnoreAsciiCase( "Caption" ) )
     840           0 :                 mxCtrlModel->importProperty( XML_Caption, lclGetQuotedString( aValue ) );
     841           0 :             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           0 :     importStorage( rVbaFormStrg, AxClassTable() );
     848             : 
     849             :     try
     850             :     {
     851             :         // create the dialog model
     852           0 :         OUString aServiceName = mxCtrlModel->getServiceName();
     853           0 :         Reference< XMultiServiceFactory > xFactory( mxContext->getServiceManager(), UNO_QUERY_THROW );
     854           0 :         Reference< XControlModel > xDialogModel( xFactory->createInstance( aServiceName ), UNO_QUERY_THROW );
     855           0 :         Reference< XNameContainer > xDialogNC( xDialogModel, UNO_QUERY_THROW );
     856             : 
     857             :         // convert properties and embedded controls
     858           0 :         if( convertProperties( xDialogModel, maConverter, 0 ) )
     859             :         {
     860             :             // export the dialog to XML and insert it into the dialog library
     861           0 :             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           0 :             ContainerHelper::insertByName( rxDialogLib, aFormName, Any( xDialogSource ) );
     864           0 :         }
     865             :     }
     866           0 :     catch(const Exception& )
     867             :     {
     868           0 :     }
     869             : }
     870             : 
     871             : 
     872             : 
     873             : } // namespace ole
     874           0 : } // namespace oox
     875             : 
     876             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10