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

Generated by: LCOV version 1.10