LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbawindows.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 51 96 53.1 %
Date: 2014-11-03 Functions: 16 32 50.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             : #include "vbawindows.hxx"
      20             : 
      21             : #include <boost/unordered_map.hpp>
      22             : 
      23             : #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
      24             : #include <com/sun/star/frame/Desktop.hpp>
      25             : #include <cppuhelper/implbase3.hxx>
      26             : 
      27             : #include "vbawindow.hxx"
      28             : #include "vbaglobals.hxx"
      29             : 
      30             : using namespace ::com::sun::star;
      31             : using namespace ::ooo::vba;
      32             : 
      33             : typedef  boost::unordered_map< OUString,
      34             : sal_Int32, OUStringHash,
      35             : ::std::equal_to< OUString > > NameIndexHash;
      36             : 
      37          42 : static uno::Reference< XHelperInterface > lcl_createWorkbookHIParent( const uno::Reference< frame::XModel >& xModel, const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication )
      38             : {
      39          42 :     return new ScVbaWorkbook( uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext,  xModel );
      40             : }
      41             : 
      42          42 : uno::Any ComponentToWindow( const uno::Any& aSource, uno::Reference< uno::XComponentContext > & xContext, const uno::Any& aApplication )
      43             : {
      44          42 :     uno::Reference< frame::XModel > xModel( aSource, uno::UNO_QUERY_THROW );
      45             :     // !! TODO !! iterate over all controllers
      46          84 :     uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
      47          84 :     uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext, xModel, xController ) );
      48          84 :     return uno::makeAny( xWin );
      49             : }
      50             : 
      51             : typedef std::vector < uno::Reference< sheet::XSpreadsheetDocument > > Components;
      52             : // #TODO more or less the same as class in workwindows ( code sharing needed )
      53         100 : class WindowComponentEnumImpl : public EnumerationHelper_BASE
      54             : {
      55             : protected:
      56             :     uno::Reference< uno::XComponentContext > m_xContext;
      57             :     Components m_components;
      58             :     Components::const_iterator m_it;
      59             : 
      60             : public:
      61           0 :     WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Components& components ) throw ( uno::RuntimeException ) :  m_xContext( xContext ), m_components( components )
      62             :     {
      63           0 :         m_it = m_components.begin();
      64           0 :     }
      65             : 
      66          50 :     WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException ) :  m_xContext( xContext )
      67             :     {
      68          50 :         uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(m_xContext);
      69         100 :         uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
      70         150 :         while( mxComponents->hasMoreElements() )
      71             :         {
      72          50 :             uno::Reference< sheet::XSpreadsheetDocument > xNext( mxComponents->nextElement(), uno::UNO_QUERY );
      73          50 :             if ( xNext.is() )
      74          50 :                 m_components.push_back( xNext );
      75          50 :         }
      76         100 :         m_it = m_components.begin();
      77          50 :     }
      78             :     // XEnumeration
      79         150 :     virtual sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
      80             :     {
      81         150 :         return m_it != m_components.end();
      82             :     }
      83             : 
      84          50 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
      85             :     {
      86          50 :         if ( !hasMoreElements() )
      87             :         {
      88           0 :             throw container::NoSuchElementException();
      89             :         }
      90          50 :         return makeAny( *(m_it++) );
      91             :     }
      92             : };
      93             : 
      94           0 : class WindowEnumImpl : public  WindowComponentEnumImpl
      95             : {
      96             :     uno::Any m_aApplication;
      97             : public:
      98           0 :     WindowEnumImpl( const uno::Reference< uno::XComponentContext >& xContext,  const uno::Any& aApplication ): WindowComponentEnumImpl( xContext ), m_aApplication( aApplication ) {}
      99           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
     100             :     {
     101           0 :         return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext, m_aApplication );
     102             :     }
     103             : };
     104             : 
     105             : typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
     106             :     , com::sun::star::container::XIndexAccess
     107             :     , com::sun::star::container::XNameAccess
     108             :     > WindowsAccessImpl_BASE;
     109             : 
     110         100 : class WindowsAccessImpl : public WindowsAccessImpl_BASE
     111             : {
     112             :     uno::Reference< uno::XComponentContext > m_xContext;
     113             :     Components m_windows;
     114             :     NameIndexHash namesToIndices;
     115             : public:
     116          50 :     WindowsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext ):m_xContext( xContext )
     117             :     {
     118          50 :         uno::Reference< container::XEnumeration > xEnum = new WindowComponentEnumImpl( m_xContext );
     119          50 :         sal_Int32 nIndex=0;
     120         150 :         while( xEnum->hasMoreElements() )
     121             :         {
     122          50 :             uno::Reference< sheet::XSpreadsheetDocument > xNext( xEnum->nextElement(), uno::UNO_QUERY );
     123          50 :             if ( xNext.is() )
     124             :             {
     125          50 :                 m_windows.push_back( xNext );
     126          50 :                 uno::Reference< frame::XModel > xModel( xNext, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
     127             :                 // !! TODO !! iterate over all controllers
     128         100 :                 uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
     129         100 :                 uno::Reference< XHelperInterface > xTemp;  // temporary needed for g++ 3.3.5
     130         100 :                 ScVbaWindow window( xTemp, m_xContext, xModel, xController );
     131         100 :                 OUString sCaption;
     132          50 :                 window.getCaption() >>= sCaption;
     133         100 :                 namesToIndices[ sCaption ] = nIndex++;
     134             :             }
     135         100 :         }
     136             : 
     137          50 :     }
     138             : 
     139             :     //XEnumerationAccess
     140           0 :     virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     141             :     {
     142           0 :         return new WindowComponentEnumImpl( m_xContext, m_windows );
     143             :     }
     144             :     // XIndexAccess
     145           8 :     virtual ::sal_Int32 SAL_CALL getCount(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     146             :     {
     147           8 :         return m_windows.size();
     148             :     }
     149          42 :     virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
     150             :     {
     151          42 :         if ( Index < 0
     152          42 :             || static_cast< Components::size_type >( Index ) >= m_windows.size() )
     153           0 :             throw lang::IndexOutOfBoundsException();
     154          42 :         return makeAny( m_windows[ Index ] ); // returns xspreadsheetdoc
     155             :     }
     156             : 
     157             :     //XElementAccess
     158           0 :     virtual uno::Type SAL_CALL getElementType(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     159             :     {
     160           0 :         return cppu::UnoType<sheet::XSpreadsheetDocument>::get();
     161             :     }
     162             : 
     163           0 :     virtual sal_Bool SAL_CALL hasElements(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     164             :     {
     165           0 :         return ( !m_windows.empty() );
     166             :     }
     167             : 
     168             :     //XNameAccess
     169           0 :     virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
     170             :     {
     171           0 :         NameIndexHash::const_iterator it = namesToIndices.find( aName );
     172           0 :         if ( it == namesToIndices.end() )
     173           0 :             throw container::NoSuchElementException();
     174           0 :         return makeAny( m_windows[ it->second ] );
     175             : 
     176             :     }
     177             : 
     178           0 :     virtual uno::Sequence< OUString > SAL_CALL getElementNames(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     179             :     {
     180           0 :         uno::Sequence< OUString > names( namesToIndices.size() );
     181           0 :         OUString* pString = names.getArray();
     182           0 :         NameIndexHash::const_iterator it = namesToIndices.begin();
     183           0 :         NameIndexHash::const_iterator it_end = namesToIndices.end();
     184           0 :         for ( ; it != it_end; ++it, ++pString )
     185           0 :             *pString = it->first;
     186           0 :         return names;
     187             :     }
     188             : 
     189           0 :     virtual sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
     190             :     {
     191           0 :         NameIndexHash::const_iterator it = namesToIndices.find( aName );
     192           0 :         return (it != namesToIndices.end());
     193             :     }
     194             : 
     195             : };
     196             : 
     197          50 : ScVbaWindows::ScVbaWindows( const uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext ) : ScVbaWindows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess > ( new WindowsAccessImpl( xContext ) ) )
     198             : {
     199          50 : }
     200             : uno::Reference< container::XEnumeration >
     201           0 : ScVbaWindows::createEnumeration() throw (uno::RuntimeException)
     202             : {
     203           0 :     return new WindowEnumImpl( mxContext, Application() );
     204             : }
     205             : 
     206             : uno::Any
     207          42 : ScVbaWindows::createCollectionObject( const css::uno::Any& aSource )
     208             : {
     209          42 :     return ComponentToWindow( aSource,  mxContext, Application() );
     210             : }
     211             : 
     212             : uno::Type
     213           0 : ScVbaWindows::getElementType() throw (uno::RuntimeException)
     214             : {
     215           0 :     return cppu::UnoType<excel::XWindows>::get();
     216             : }
     217             : 
     218             : void SAL_CALL
     219           0 : ScVbaWindows::Arrange( ::sal_Int32 /*ArrangeStyle*/, const uno::Any& /*ActiveWorkbook*/, const uno::Any& /*SyncHorizontal*/, const uno::Any& /*SyncVertical*/ ) throw (uno::RuntimeException, std::exception)
     220             : {
     221             :     //#TODO #FIXME see what can be done for an implementation here
     222           0 : }
     223             : 
     224             : OUString
     225           0 : ScVbaWindows::getServiceImplName()
     226             : {
     227           0 :     return OUString("ScVbaWindows");
     228             : }
     229             : 
     230             : css::uno::Sequence<OUString>
     231           0 : ScVbaWindows::getServiceNames()
     232             : {
     233           0 :     static uno::Sequence< OUString > sNames;
     234           0 :     if ( sNames.getLength() == 0 )
     235             :     {
     236           0 :         sNames.realloc( 1 );
     237           0 :         sNames[0] = "ooo.vba.excel.Windows";
     238             :     }
     239           0 :     return sNames;
     240          12 : }
     241             : 
     242             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10