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

Generated by: LCOV version 1.10