LCOV - code coverage report
Current view: top level - sc/source/ui/vba - excelvbahelper.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 160 186 86.0 %
Date: 2014-04-11 Functions: 23 26 88.5 %
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 "excelvbahelper.hxx"
      21             : 
      22             : #include <comphelper/processfactory.hxx>
      23             : #include <com/sun/star/sheet/XSheetCellRange.hpp>
      24             : #include <com/sun/star/sheet/GlobalSheetSettings.hpp>
      25             : 
      26             : #include "docuno.hxx"
      27             : #include "tabvwsh.hxx"
      28             : #include "transobj.hxx"
      29             : #include "scmod.hxx"
      30             : #include "cellsuno.hxx"
      31             : 
      32             : #include <com/sun/star/script/vba/VBAEventId.hpp>
      33             : #include <com/sun/star/script/vba/XVBACompatibility.hpp>
      34             : #include <com/sun/star/script/vba/XVBAEventProcessor.hpp>
      35             : #include <com/sun/star/script/vba/XVBAModuleInfo.hpp>
      36             : #include <com/sun/star/script/ModuleInfo.hpp>
      37             : #include <com/sun/star/script/ModuleType.hpp>
      38             : 
      39             : using namespace ::com::sun::star;
      40             : using namespace ::ooo::vba;
      41             : 
      42             : namespace ooo {
      43             : namespace vba {
      44             : namespace excel {
      45             : 
      46             : uno::Reference< sheet::XUnnamedDatabaseRanges >
      47          13 : GetUnnamedDataBaseRanges( ScDocShell* pShell ) throw ( uno::RuntimeException )
      48             : {
      49          13 :     uno::Reference< frame::XModel > xModel;
      50          13 :     if ( pShell )
      51          13 :         xModel.set( pShell->GetModel(), uno::UNO_QUERY_THROW );
      52          26 :     uno::Reference< beans::XPropertySet > xModelProps( xModel, uno::UNO_QUERY_THROW );
      53          13 :     uno::Reference< sheet::XUnnamedDatabaseRanges > xUnnamedDBRanges( xModelProps->getPropertyValue("UnnamedDatabaseRanges"), uno::UNO_QUERY_THROW );
      54          26 :     return xUnnamedDBRanges;
      55             : }
      56             : 
      57             : // returns the XDatabaseRange for the autofilter on sheet (nSheet)
      58             : // also populates sName with the name of range
      59             : uno::Reference< sheet::XDatabaseRange >
      60          12 : GetAutoFiltRange( ScDocShell* pShell, sal_Int16 nSheet ) throw ( uno::RuntimeException )
      61             : {
      62          12 :     uno::Reference< sheet::XUnnamedDatabaseRanges > xUnnamedDBRanges( GetUnnamedDataBaseRanges( pShell ), uno::UNO_QUERY_THROW );
      63          12 :     uno::Reference< sheet::XDatabaseRange > xDataBaseRange;
      64          12 :     if (xUnnamedDBRanges->hasByTable( nSheet ) )
      65             :     {
      66          11 :         uno::Reference< sheet::XDatabaseRange > xDBRange( xUnnamedDBRanges->getByTable( nSheet ) , uno::UNO_QUERY_THROW );
      67          11 :         sal_Bool bHasAuto = false;
      68          22 :         uno::Reference< beans::XPropertySet > xProps( xDBRange, uno::UNO_QUERY_THROW );
      69          11 :         xProps->getPropertyValue("AutoFilter") >>= bHasAuto;
      70          11 :         if ( bHasAuto )
      71             :         {
      72          11 :             xDataBaseRange=xDBRange;
      73          11 :         }
      74             :     }
      75          12 :     return xDataBaseRange;
      76             : }
      77             : 
      78        1677 : ScDocShell* GetDocShellFromRange( const uno::Reference< uno::XInterface >& xRange ) throw ( uno::RuntimeException )
      79             : {
      80        1677 :     ScCellRangesBase* pScCellRangesBase = ScCellRangesBase::getImplementation( xRange );
      81        1677 :     if ( !pScCellRangesBase )
      82             :     {
      83           0 :         throw uno::RuntimeException("Failed to access underlying doc shell uno range object", uno::Reference< uno::XInterface >() );
      84             :     }
      85        1677 :     return pScCellRangesBase->GetDocShell();
      86             : }
      87             : 
      88             : uno::Reference< XHelperInterface >
      89        1454 : getUnoSheetModuleObj( const uno::Reference< table::XCellRange >& xRange ) throw ( uno::RuntimeException )
      90             : {
      91        1454 :     uno::Reference< sheet::XSheetCellRange > xSheetRange( xRange, uno::UNO_QUERY_THROW );
      92        2908 :     uno::Reference< sheet::XSpreadsheet > xSheet( xSheetRange->getSpreadsheet(), uno::UNO_SET_THROW );
      93        2908 :     return getUnoSheetModuleObj( xSheet );
      94             : }
      95             : 
      96           2 : void implSetZoom( const uno::Reference< frame::XModel >& xModel, sal_Int16 nZoom, std::vector< SCTAB >& nTabs )
      97             : {
      98           2 :     ScTabViewShell* pViewSh = excel::getBestViewShell( xModel );
      99           2 :     Fraction aFract( nZoom, 100 );
     100           2 :     pViewSh->GetViewData()->SetZoom( aFract, aFract, nTabs );
     101           2 :     pViewSh->RefreshZoom();
     102           2 : }
     103             : 
     104           2 : const OUString REPLACE_CELLS_WARNING( "ReplaceCellsWarning");
     105             : 
     106             : class PasteCellsWarningReseter
     107             : {
     108             : private:
     109             :     bool bInitialWarningState;
     110          45 :     static uno::Reference< sheet::XGlobalSheetSettings > getGlobalSheetSettings() throw ( uno::RuntimeException )
     111             :     {
     112          45 :         static uno::Reference< sheet::XGlobalSheetSettings > xProps = sheet::GlobalSheetSettings::create( comphelper::getProcessComponentContext() );
     113          45 :         return xProps;
     114             :     }
     115             : 
     116          15 :     bool getReplaceCellsWarning() throw ( uno::RuntimeException )
     117             :     {
     118          15 :         sal_Bool res = getGlobalSheetSettings()->getReplaceCellsWarning();
     119          15 :         return ( res == sal_True );
     120             :     }
     121             : 
     122          30 :     void setReplaceCellsWarning( bool bState ) throw ( uno::RuntimeException )
     123             :     {
     124          30 :         getGlobalSheetSettings()->setReplaceCellsWarning( bState );
     125          30 :     }
     126             : public:
     127          15 :     PasteCellsWarningReseter() throw ( uno::RuntimeException )
     128             :     {
     129          15 :         bInitialWarningState = getReplaceCellsWarning();
     130          15 :         if ( bInitialWarningState )
     131          15 :             setReplaceCellsWarning( false );
     132          15 :     }
     133          15 :     ~PasteCellsWarningReseter()
     134             :     {
     135          15 :         if ( bInitialWarningState )
     136             :         {
     137             :             // don't allow dtor to throw
     138             :             try
     139             :             {
     140          15 :                 setReplaceCellsWarning( true );
     141             :             }
     142           0 :             catch ( uno::Exception& /*e*/ ){}
     143             :         }
     144          15 :     }
     145             : };
     146             : 
     147             : void
     148           3 : implnPaste( const uno::Reference< frame::XModel>& xModel )
     149             : {
     150           3 :     PasteCellsWarningReseter resetWarningBox;
     151           3 :     ScTabViewShell* pViewShell = getBestViewShell( xModel );
     152           3 :     if ( pViewShell )
     153             :     {
     154           3 :         pViewShell->PasteFromSystem();
     155           3 :         pViewShell->CellContentChanged();
     156           3 :     }
     157           3 : }
     158             : 
     159             : 
     160             : void
     161          11 : implnCopy( const uno::Reference< frame::XModel>& xModel )
     162             : {
     163          11 :     ScTabViewShell* pViewShell = getBestViewShell( xModel );
     164          11 :     if ( pViewShell )
     165             :     {
     166          11 :         pViewShell->CopyToClip(NULL,false,false,true);
     167             : 
     168             :         // mark the copied transfer object so it is used in ScVbaRange::Insert
     169          11 :         ScTransferObj* pClipObj = ScTransferObj::GetOwnClipboard( NULL );
     170          11 :         if (pClipObj)
     171           0 :             pClipObj->SetUseInApi( true );
     172             :     }
     173          11 : }
     174             : 
     175             : void
     176           0 : implnCut( const uno::Reference< frame::XModel>& xModel )
     177             : {
     178           0 :     ScTabViewShell* pViewShell =  getBestViewShell( xModel );
     179           0 :     if ( pViewShell )
     180             :     {
     181           0 :         pViewShell->CutToClip( NULL, true );
     182             : 
     183             :         // mark the copied transfer object so it is used in ScVbaRange::Insert
     184           0 :         ScTransferObj* pClipObj = ScTransferObj::GetOwnClipboard( NULL );
     185           0 :         if (pClipObj)
     186           0 :             pClipObj->SetUseInApi( true );
     187             :     }
     188           0 : }
     189             : 
     190          12 : void implnPasteSpecial( const uno::Reference< frame::XModel>& xModel, sal_uInt16 nFlags, sal_uInt16 nFunction, bool bSkipEmpty, bool bTranspose)
     191             : {
     192          12 :     PasteCellsWarningReseter resetWarningBox;
     193          12 :     InsCellCmd eMoveMode = INS_NONE;
     194             : 
     195          12 :     ScTabViewShell* pTabViewShell = getBestViewShell( xModel );
     196          12 :     if ( pTabViewShell )
     197             :     {
     198          12 :         ScViewData* pView = pTabViewShell->GetViewData();
     199          12 :         Window* pWin = ( pView != NULL ) ? pView->GetActiveWin() : NULL;
     200          12 :         if (pWin)
     201             :         {
     202          12 :             ScTransferObj* pOwnClip = ScTransferObj::GetOwnClipboard( pWin );
     203          12 :             ScDocument* pDoc = NULL;
     204          12 :             if ( pOwnClip )
     205           0 :                 pDoc = pOwnClip->GetDocument();
     206             :             pTabViewShell->PasteFromClip( nFlags, pDoc,
     207             :                 nFunction, bSkipEmpty, bTranspose, false,
     208          12 :                 eMoveMode, IDF_NONE, true );
     209          12 :             pTabViewShell->CellContentChanged();
     210             :         }
     211          12 :     }
     212             : 
     213          12 : }
     214             : 
     215             : ScDocShell*
     216         136 : getDocShell( const css::uno::Reference< css::frame::XModel>& xModel )
     217             : {
     218         136 :     uno::Reference< uno::XInterface > xIf( xModel, uno::UNO_QUERY_THROW );
     219         136 :     ScModelObj* pModel = dynamic_cast< ScModelObj* >( xIf.get() );
     220         136 :     ScDocShell* pDocShell = NULL;
     221         136 :     if ( pModel )
     222         136 :         pDocShell = (ScDocShell*)pModel->GetEmbeddedObject();
     223         136 :     return pDocShell;
     224             : 
     225             : }
     226             : 
     227             : ScTabViewShell*
     228         112 : getBestViewShell( const css::uno::Reference< css::frame::XModel>& xModel )
     229             : {
     230         112 :     ScDocShell* pDocShell = getDocShell( xModel );
     231         112 :     if ( pDocShell )
     232         112 :         return pDocShell->GetBestViewShell();
     233           0 :     return NULL;
     234             : }
     235             : 
     236             : ScTabViewShell*
     237          33 : getCurrentBestViewShell(  const uno::Reference< uno::XComponentContext >& xContext )
     238             : {
     239          33 :     uno::Reference< frame::XModel > xModel = getCurrentExcelDoc( xContext );
     240          33 :     return getBestViewShell( xModel );
     241             : }
     242             : 
     243             : SfxViewFrame*
     244          11 : getViewFrame( const uno::Reference< frame::XModel >& xModel )
     245             : {
     246          11 :     ScTabViewShell* pViewShell = getBestViewShell( xModel );
     247          11 :     if ( pViewShell )
     248          11 :         return pViewShell->GetViewFrame();
     249           0 :     return NULL;
     250             : }
     251             : 
     252             : uno::Reference< XHelperInterface >
     253        1677 : getUnoSheetModuleObj( const uno::Reference< sheet::XSpreadsheet >& xSheet ) throw ( uno::RuntimeException )
     254             : {
     255        1677 :     uno::Reference< beans::XPropertySet > xProps( xSheet, uno::UNO_QUERY_THROW );
     256        3354 :     OUString sCodeName;
     257        1677 :     xProps->getPropertyValue("CodeName") >>= sCodeName;
     258             :     // #TODO #FIXME ideally we should 'throw' here if we don't get a valid parent, but... it is possible
     259             :     // to create a module ( and use 'Option VBASupport 1' ) for a calc document, in this scenario there
     260             :     // are *NO* special document module objects ( of course being able to switch between vba/non vba mode at
     261             :     // the document in the future could fix this, especially IF the switching of the vba mode takes care to
     262             :     // create the special document module objects if they don't exist.
     263        3354 :     return getUnoDocModule( sCodeName, GetDocShellFromRange( xSheet ) );
     264             : }
     265             : 
     266             : uno::Reference< XHelperInterface >
     267          50 : getUnoSheetModuleObj( const uno::Reference< sheet::XSheetCellRangeContainer >& xRanges ) throw ( uno::RuntimeException )
     268             : {
     269          50 :     uno::Reference< container::XEnumerationAccess > xEnumAccess( xRanges, uno::UNO_QUERY_THROW );
     270         100 :     uno::Reference< container::XEnumeration > xEnum = xEnumAccess->createEnumeration();
     271         100 :     uno::Reference< table::XCellRange > xRange( xEnum->nextElement(), uno::UNO_QUERY_THROW );
     272         100 :     return getUnoSheetModuleObj( xRange );
     273             : }
     274             : 
     275             : uno::Reference< XHelperInterface >
     276           0 : getUnoSheetModuleObj( const uno::Reference< table::XCell >& xCell ) throw ( uno::RuntimeException )
     277             : {
     278           0 :     uno::Reference< sheet::XSheetCellRange > xSheetRange( xCell, uno::UNO_QUERY_THROW );
     279           0 :     uno::Reference< sheet::XSpreadsheet > xSheet( xSheetRange->getSpreadsheet(), uno::UNO_SET_THROW );
     280           0 :     return getUnoSheetModuleObj( xSheet );
     281             : }
     282             : 
     283             : uno::Reference< XHelperInterface >
     284           0 : getUnoSheetModuleObj( const uno::Reference< frame::XModel >& xModel, SCTAB nTab ) throw ( uno::RuntimeException )
     285             : {
     286           0 :     uno::Reference< sheet::XSpreadsheetDocument > xDoc( xModel, uno::UNO_QUERY_THROW );
     287           0 :     uno::Reference< container::XIndexAccess > xSheets( xDoc->getSheets(), uno::UNO_QUERY_THROW );
     288           0 :     uno::Reference< sheet::XSpreadsheet > xSheet( xSheets->getByIndex( nTab ), uno::UNO_QUERY_THROW );
     289           0 :     return getUnoSheetModuleObj( xSheet );
     290             : }
     291             : 
     292           1 : void setUpDocumentModules( const uno::Reference< sheet::XSpreadsheetDocument >& xDoc )
     293             : {
     294           1 :     uno::Reference< frame::XModel > xModel( xDoc, uno::UNO_QUERY );
     295           1 :     ScDocShell* pShell = excel::getDocShell( xModel );
     296           1 :     if ( pShell )
     297             :     {
     298           1 :         OUString aPrjName( "Standard" );
     299           1 :         pShell->GetBasicManager()->SetName( aPrjName );
     300             : 
     301             :         /*  Set library container to VBA compatibility mode. This will create
     302             :             the VBA Globals object and store it in the Basic manager of the
     303             :             document. */
     304           2 :         uno::Reference<script::XLibraryContainer> xLibContainer = pShell->GetBasicContainer();
     305           2 :         uno::Reference<script::vba::XVBACompatibility> xVBACompat( xLibContainer, uno::UNO_QUERY_THROW );
     306           1 :         xVBACompat->setVBACompatibilityMode( sal_True );
     307             : 
     308           1 :         if( xLibContainer.is() )
     309             :         {
     310           1 :             if( !xLibContainer->hasByName( aPrjName ) )
     311           0 :                 xLibContainer->createLibrary( aPrjName );
     312           1 :             uno::Any aLibAny = xLibContainer->getByName( aPrjName );
     313           2 :             uno::Reference< container::XNameContainer > xLib;
     314           1 :             aLibAny >>= xLib;
     315           1 :             if( xLib.is()  )
     316             :             {
     317           1 :                 uno::Reference< script::vba::XVBAModuleInfo > xVBAModuleInfo( xLib, uno::UNO_QUERY_THROW );
     318           2 :                 uno::Reference< lang::XMultiServiceFactory> xSF( pShell->GetModel(), uno::UNO_QUERY_THROW);
     319           2 :                 uno::Reference< container::XNameAccess > xVBACodeNamedObjectAccess( xSF->createInstance("ooo.vba.VBAObjectModuleObjectProvider"), uno::UNO_QUERY_THROW );
     320             :                 // set up the module info for the workbook and sheets in the nealy created
     321             :                 // spreadsheet
     322           1 :                 ScDocument* pDoc = pShell->GetDocument();
     323           2 :                 OUString sCodeName = pDoc->GetCodeName();
     324           1 :                 if ( sCodeName.isEmpty() )
     325             :                 {
     326           1 :                     sCodeName = "ThisWorkbook";
     327           1 :                     pDoc->SetCodeName( sCodeName );
     328             :                 }
     329             : 
     330           2 :                 std::vector< OUString > sDocModuleNames;
     331           1 :                 sDocModuleNames.push_back( sCodeName );
     332             : 
     333           2 :                 for ( SCTAB index = 0; index < pDoc->GetTableCount(); index++)
     334             :                 {
     335           1 :                     OUString aName;
     336           1 :                     pDoc->GetCodeName( index, aName );
     337           1 :                     sDocModuleNames.push_back( aName );
     338           1 :                 }
     339             : 
     340           1 :                 std::vector<OUString>::iterator it_end = sDocModuleNames.end();
     341             : 
     342           3 :                 for ( std::vector<OUString>::iterator it = sDocModuleNames.begin(); it != it_end; ++it )
     343             :                 {
     344           2 :                     script::ModuleInfo sModuleInfo;
     345             : 
     346           4 :                     uno::Any aName= xVBACodeNamedObjectAccess->getByName( *it );
     347           2 :                     sModuleInfo.ModuleObject.set( aName, uno::UNO_QUERY );
     348           2 :                     sModuleInfo.ModuleType = script::ModuleType::DOCUMENT;
     349           2 :                     xVBAModuleInfo->insertModuleInfo( *it, sModuleInfo );
     350           2 :                     if( xLib->hasByName( *it ) )
     351           0 :                         xLib->replaceByName( *it, uno::makeAny( OUString( "Option VBASupport 1\n") ) );
     352             :                     else
     353           2 :                         xLib->insertByName( *it, uno::makeAny( OUString( "Option VBASupport 1\n" ) ) );
     354           3 :                 }
     355           1 :             }
     356             :         }
     357             : 
     358             :         /*  Trigger the Workbook_Open event, event processor will register
     359             :             itself as listener for specific events. */
     360             :         try
     361             :         {
     362           1 :             uno::Reference< script::vba::XVBAEventProcessor > xVbaEvents( pShell->GetDocument()->GetVbaEventProcessor(), uno::UNO_SET_THROW );
     363           2 :             uno::Sequence< uno::Any > aArgs;
     364           2 :             xVbaEvents->processVbaEvent( script::vba::VBAEventId::WORKBOOK_OPEN, aArgs );
     365             :         }
     366           0 :         catch( uno::Exception& )
     367             :         {
     368           1 :         }
     369           1 :     }
     370           1 : }
     371             : 
     372             : SfxItemSet*
     373          72 : ScVbaCellRangeAccess::GetDataSet( ScCellRangesBase* pRangeObj )
     374             : {
     375          72 :     return pRangeObj ? pRangeObj->GetCurrentDataSet( true ) : 0;
     376             : }
     377             : 
     378             : } // namespace excel
     379             : } // namespace vba
     380           6 : } // namespace ooo
     381             : 
     382             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10