1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "vbacontrols.hxx"
32 #include <cppuhelper/implbase2.hxx>
33 #include <com/sun/star/awt/XControlContainer.hpp>
34 #include <ooo/vba//XControlProvider.hpp>
37 using namespace com::sun::star
;
38 using namespace ooo::vba
;
41 typedef ::cppu::WeakImplHelper2
< container::XNameAccess
, container::XIndexAccess
> ArrayWrapImpl
;
43 typedef std::hash_map
< rtl::OUString
, sal_Int32
, ::rtl::OUStringHash
,
44 ::std::equal_to
< ::rtl::OUString
> > ControlIndexMap
;
45 typedef std::vector
< uno::Reference
< awt::XControl
> > ControlVec
;
47 class ControlArrayWrapper
: public ArrayWrapImpl
49 uno::Reference
< awt::XControlContainer
> mxDialog
;
50 uno::Sequence
< ::rtl::OUString
> msNames
;
52 ControlIndexMap mIndices
;
54 rtl::OUString
getControlName( const uno::Reference
< awt::XControl
>& xCtrl
)
56 uno::Reference
< beans::XPropertySet
> xProp( xCtrl
->getModel(), uno::UNO_QUERY
);
58 xProp
->getPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Name" ) ) ) >>= sName
;
64 ControlArrayWrapper( const uno::Reference
< awt::XControl
>& xDialog
)
66 mxDialog
.set( xDialog
, uno::UNO_QUERY_THROW
);
67 uno::Sequence
< uno::Reference
< awt::XControl
> > sXControls
= mxDialog
->getControls();
69 msNames
.realloc( sXControls
.getLength() );
70 for ( sal_Int32 i
= 0; i
< sXControls
.getLength(); ++i
)
72 uno::Reference
< awt::XControl
> xCtrl
= sXControls
[ i
];
73 msNames
[ i
] = getControlName( xCtrl
);
74 mControls
.push_back( xCtrl
);
75 mIndices
[ msNames
[ i
] ] = i
;
80 virtual uno::Type SAL_CALL
getElementType( ) throw (uno::RuntimeException
)
82 return awt::XControl::static_type(0);
85 virtual ::sal_Bool SAL_CALL
hasElements( ) throw (uno::RuntimeException
)
87 return ( mControls
.size() > 0 );
91 virtual uno::Any SAL_CALL
getByName( const ::rtl::OUString
& aName
) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
93 if ( !hasByName( aName
) )
94 throw container::NoSuchElementException();
95 return getByIndex( mIndices
[ aName
] );
98 virtual uno::Sequence
< ::rtl::OUString
> SAL_CALL
getElementNames( ) throw (uno::RuntimeException
)
103 virtual ::sal_Bool SAL_CALL
hasByName( const ::rtl::OUString
& aName
) throw (css::uno::RuntimeException
)
105 ControlIndexMap::iterator it
= mIndices
.find( aName
);
106 return it
!= mIndices
.end();
110 virtual ::sal_Int32 SAL_CALL
getCount( ) throw (css::uno::RuntimeException
)
112 return mControls
.size();
115 virtual uno::Any SAL_CALL
getByIndex( ::sal_Int32 Index
) throw (lang::IndexOutOfBoundsException
, lang::WrappedTargetException
, uno::RuntimeException
)
117 if ( Index
< 0 || Index
>= static_cast< sal_Int32
>( mControls
.size() ) )
118 throw lang::IndexOutOfBoundsException();
119 return uno::makeAny( mControls
[ Index
] );
124 class ControlsEnumWrapper
: public EnumerationHelper_BASE
126 uno::Reference
<XHelperInterface
> m_xParent
;
127 uno::Reference
<uno::XComponentContext
> m_xContext
;
128 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
129 uno::Reference
<awt::XControl
> m_xDlg
;
134 ControlsEnumWrapper( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< container::XIndexAccess
>& xIndexAccess
, const uno::Reference
< awt::XControl
>& xDlg
) : m_xParent( xParent
), m_xContext( xContext
), m_xIndexAccess( xIndexAccess
), m_xDlg( xDlg
), nIndex( 0 ) {}
136 virtual ::sal_Bool SAL_CALL
hasMoreElements( ) throw (uno::RuntimeException
)
138 return ( nIndex
< m_xIndexAccess
->getCount() );
141 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
)
143 if ( nIndex
< m_xIndexAccess
->getCount() )
145 uno::Reference
< frame::XModel
> xModel
;
146 uno::Reference
< awt::XControl
> xControl
;
147 m_xIndexAccess
->getByIndex( nIndex
++ ) >>= xControl
;
149 uno::Reference
<lang::XMultiComponentFactory
> xServiceManager( m_xContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
150 uno::Reference
< XControlProvider
> xControlProvider( xServiceManager
->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.ControlProvider" ) ), m_xContext
), uno::UNO_QUERY_THROW
);
152 uno::Reference
< msforms::XControl
> xVBAControl( xControlProvider
->createUserformControl( xControl
, m_xDlg
, xModel
) );
153 return uno::makeAny( xVBAControl
);
155 throw container::NoSuchElementException();
161 uno::Reference
<container::XIndexAccess
>
162 lcl_controlsWrapper( const uno::Reference
< awt::XControl
>& xDlg
)
164 return new ControlArrayWrapper( xDlg
);
167 ScVbaControls::ScVbaControls( const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
,
168 const css::uno::Reference
< awt::XControl
>& xDialog
)
169 : ControlsImpl_BASE( xParent
, xContext
, lcl_controlsWrapper( xDialog
) )
171 mxDialog
.set( xDialog
, uno::UNO_QUERY_THROW
);
174 uno::Reference
< container::XEnumeration
>
175 ScVbaControls::createEnumeration() throw (uno::RuntimeException
)
177 uno::Reference
< container::XEnumeration
> xEnum( new ControlsEnumWrapper( mxParent
, mxContext
, m_xIndexAccess
, mxDialog
) );
179 throw uno::RuntimeException();
184 ScVbaControls::createCollectionObject( const css::uno::Any
& aSource
)
186 // Create control from awt::XControl
187 uno::Reference
< awt::XControl
> xControl
;
188 aSource
>>= xControl
;
189 uno::Reference
< frame::XModel
> xModel
;
190 uno::Reference
<lang::XMultiComponentFactory
> xServiceManager( mxContext
->getServiceManager(), uno::UNO_QUERY_THROW
);
191 uno::Reference
< XControlProvider
> xControlProvider( xServiceManager
->createInstanceWithContext( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ooo.vba.ControlProvider" ) ), mxContext
), uno::UNO_QUERY_THROW
);
193 uno::Reference
< msforms::XControl
> xVBAControl( xControlProvider
->createUserformControl( xControl
, mxDialog
, xModel
) );
195 return uno::makeAny( xVBAControl
);
199 ScVbaControls::Move( double cx
, double cy
) throw (uno::RuntimeException
)
201 uno::Reference
< container::XEnumeration
> xEnum( createEnumeration() );
202 while ( xEnum
->hasMoreElements() )
204 uno::Reference
< msforms::XControl
> xControl( xEnum
->nextElement(), uno::UNO_QUERY_THROW
);
205 xControl
->setLeft( xControl
->getLeft() + cx
);
206 xControl
->setTop( xControl
->getTop() + cy
);
211 ScVbaControls::getElementType() throw (uno::RuntimeException
)
213 return ooo::vba::msforms::XControl::static_type(0);
216 ScVbaControls::getServiceImplName()
218 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaControls") );
222 uno::Sequence
< rtl::OUString
>
223 ScVbaControls::getServiceNames()
225 static uno::Sequence
< rtl::OUString
> aServiceNames
;
226 if ( aServiceNames
.getLength() == 0 )
228 aServiceNames
.realloc( 1 );
229 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Controls" ) );
231 return aServiceNames
;