1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "vbawindows.hxx"
#include <boost/unordered_map.hpp>
#include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <cppuhelper/implbase3.hxx>
#include "vbawindow.hxx"
#include "vbaglobals.hxx"
using namespace ::com::sun::star;
using namespace ::ooo::vba;
typedef boost::unordered_map< OUString,
sal_Int32, OUStringHash,
::std::equal_to< OUString > > NameIndexHash;
static uno::Reference< XHelperInterface > lcl_createWorkbookHIParent( const uno::Reference< frame::XModel >& xModel, const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication )
{
return new ScVbaWorkbook( uno::Reference< XHelperInterface >( aApplication, uno::UNO_QUERY_THROW ), xContext, xModel );
}
uno::Any ComponentToWindow( const uno::Any& aSource, uno::Reference< uno::XComponentContext > & xContext, const uno::Any& aApplication )
{
uno::Reference< frame::XModel > xModel( aSource, uno::UNO_QUERY_THROW );
// !! TODO !! iterate over all controllers
uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
uno::Reference< excel::XWindow > xWin( new ScVbaWindow( lcl_createWorkbookHIParent( xModel, xContext, aApplication ), xContext, xModel, xController ) );
return uno::makeAny( xWin );
}
typedef std::vector < uno::Reference< sheet::XSpreadsheetDocument > > Components;
// #TODO more or less the same as class in workwindows ( code sharing needed )
class WindowComponentEnumImpl : public EnumerationHelper_BASE
{
protected:
uno::Reference< uno::XComponentContext > m_xContext;
Components m_components;
Components::const_iterator m_it;
public:
WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const Components& components ) throw ( uno::RuntimeException ) : m_xContext( xContext ), m_components( components )
{
m_it = m_components.begin();
}
WindowComponentEnumImpl( const uno::Reference< uno::XComponentContext >& xContext ) throw ( uno::RuntimeException ) : m_xContext( xContext )
{
uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(m_xContext);
uno::Reference< container::XEnumeration > mxComponents = xDesktop->getComponents()->createEnumeration();
while( mxComponents->hasMoreElements() )
{
uno::Reference< sheet::XSpreadsheetDocument > xNext( mxComponents->nextElement(), uno::UNO_QUERY );
if ( xNext.is() )
m_components.push_back( xNext );
}
m_it = m_components.begin();
}
// XEnumeration
virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
{
return m_it != m_components.end();
}
virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
{
if ( !hasMoreElements() )
{
throw container::NoSuchElementException();
}
return makeAny( *(m_it++) );
}
};
class WindowEnumImpl : public WindowComponentEnumImpl
{
uno::Any m_aApplication;
public:
WindowEnumImpl(const uno::Reference< uno::XComponentContext >& xContext, const Components& components, const uno::Any& aApplication ):WindowComponentEnumImpl( xContext, components ), m_aApplication( aApplication ){}
WindowEnumImpl( const uno::Reference< uno::XComponentContext >& xContext, const uno::Any& aApplication ): WindowComponentEnumImpl( xContext ), m_aApplication( aApplication ) {}
virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
{
return ComponentToWindow( WindowComponentEnumImpl::nextElement(), m_xContext, m_aApplication );
}
};
typedef ::cppu::WeakImplHelper3< container::XEnumerationAccess
, com::sun::star::container::XIndexAccess
, com::sun::star::container::XNameAccess
> WindowsAccessImpl_BASE;
class WindowsAccessImpl : public WindowsAccessImpl_BASE
{
uno::Reference< uno::XComponentContext > m_xContext;
Components m_windows;
NameIndexHash namesToIndices;
public:
WindowsAccessImpl( const uno::Reference< uno::XComponentContext >& xContext ):m_xContext( xContext )
{
uno::Reference< container::XEnumeration > xEnum = new WindowComponentEnumImpl( m_xContext );
sal_Int32 nIndex=0;
while( xEnum->hasMoreElements() )
{
uno::Reference< sheet::XSpreadsheetDocument > xNext( xEnum->nextElement(), uno::UNO_QUERY );
if ( xNext.is() )
{
m_windows.push_back( xNext );
uno::Reference< frame::XModel > xModel( xNext, uno::UNO_QUERY_THROW ); // that the spreadsheetdocument is a xmodel is a given
// !! TODO !! iterate over all controllers
uno::Reference< frame::XController > xController( xModel->getCurrentController(), uno::UNO_SET_THROW );
uno::Reference< XHelperInterface > xTemp; // temporary needed for g++ 3.3.5
ScVbaWindow window( xTemp, m_xContext, xModel, xController );
OUString sCaption;
window.getCaption() >>= sCaption;
namesToIndices[ sCaption ] = nIndex++;
}
}
}
//XEnumerationAccess
virtual uno::Reference< container::XEnumeration > SAL_CALL createEnumeration( ) throw (uno::RuntimeException)
{
return new WindowComponentEnumImpl( m_xContext, m_windows );
}
// XIndexAccess
virtual ::sal_Int32 SAL_CALL getCount( ) throw (uno::RuntimeException)
{
return m_windows.size();
}
virtual uno::Any SAL_CALL getByIndex( ::sal_Int32 Index ) throw ( lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException)
{
if ( Index < 0
|| static_cast< Components::size_type >( Index ) >= m_windows.size() )
throw lang::IndexOutOfBoundsException();
return makeAny( m_windows[ Index ] ); // returns xspreadsheetdoc
}
//XElementAccess
virtual uno::Type SAL_CALL getElementType( ) throw (uno::RuntimeException)
{
return sheet::XSpreadsheetDocument::static_type(0);
}
virtual ::sal_Bool SAL_CALL hasElements( ) throw (uno::RuntimeException)
{
return ( !m_windows.empty() );
}
//XNameAccess
virtual uno::Any SAL_CALL getByName( const OUString& aName ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
{
NameIndexHash::const_iterator it = namesToIndices.find( aName );
if ( it == namesToIndices.end() )
throw container::NoSuchElementException();
return makeAny( m_windows[ it->second ] );
}
virtual uno::Sequence< OUString > SAL_CALL getElementNames( ) throw (uno::RuntimeException)
{
uno::Sequence< OUString > names( namesToIndices.size() );
OUString* pString = names.getArray();
NameIndexHash::const_iterator it = namesToIndices.begin();
NameIndexHash::const_iterator it_end = namesToIndices.end();
for ( ; it != it_end; ++it, ++pString )
*pString = it->first;
return names;
}
virtual ::sal_Bool SAL_CALL hasByName( const OUString& aName ) throw (uno::RuntimeException)
{
NameIndexHash::const_iterator it = namesToIndices.find( aName );
return (it != namesToIndices.end());
}
};
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 ) ) )
{
}
uno::Reference< container::XEnumeration >
ScVbaWindows::createEnumeration() throw (uno::RuntimeException)
{
return new WindowEnumImpl( mxContext, Application() );
}
uno::Any
ScVbaWindows::createCollectionObject( const css::uno::Any& aSource )
{
return ComponentToWindow( aSource, mxContext, Application() );
}
uno::Type
ScVbaWindows::getElementType() throw (uno::RuntimeException)
{
return excel::XWindows::static_type(0);
}
void SAL_CALL
ScVbaWindows::Arrange( ::sal_Int32 /*ArrangeStyle*/, const uno::Any& /*ActiveWorkbook*/, const uno::Any& /*SyncHorizontal*/, const uno::Any& /*SyncVertical*/ ) throw (uno::RuntimeException)
{
//#TODO #FIXME see what can be done for an implementation here
}
OUString
ScVbaWindows::getServiceImplName()
{
return OUString("ScVbaWindows");
}
css::uno::Sequence<OUString>
ScVbaWindows::getServiceNames()
{
static uno::Sequence< OUString > sNames;
if ( sNames.getLength() == 0 )
{
sNames.realloc( 1 );
sNames[0] = "ooo.vba.excel.Windows";
}
return sNames;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|