1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #include "vbasheetobjects.hxx" 29*cdf0e10cSrcweir #include <vector> 30*cdf0e10cSrcweir #include <rtl/math.hxx> 31*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp> 32*cdf0e10cSrcweir #include <com/sun/star/container/XIndexContainer.hpp> 33*cdf0e10cSrcweir #include <com/sun/star/container/XNamed.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/drawing/XControlShape.hpp> 35*cdf0e10cSrcweir #include <com/sun/star/drawing/XDrawPageSupplier.hpp> 36*cdf0e10cSrcweir #include <com/sun/star/drawing/XShapes.hpp> 37*cdf0e10cSrcweir #include <com/sun/star/form/FormComponentType.hpp> 38*cdf0e10cSrcweir #include <com/sun/star/form/XForm.hpp> 39*cdf0e10cSrcweir #include <com/sun/star/form/XFormComponent.hpp> 40*cdf0e10cSrcweir #include <com/sun/star/form/XFormsSupplier.hpp> 41*cdf0e10cSrcweir #include <oox/helper/helper.hxx> 42*cdf0e10cSrcweir #include "vbasheetobject.hxx" 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir using ::rtl::OUString; 45*cdf0e10cSrcweir using namespace ::com::sun::star; 46*cdf0e10cSrcweir using namespace ::ooo::vba; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir // ============================================================================ 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir namespace { 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir template< typename Type > 53*cdf0e10cSrcweir inline bool lclGetProperty( Type& orValue, const uno::Reference< beans::XPropertySet >& rxPropSet, const OUString& rPropName ) 54*cdf0e10cSrcweir { 55*cdf0e10cSrcweir try 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir return rxPropSet->getPropertyValue( rPropName ) >>= orValue; 58*cdf0e10cSrcweir } 59*cdf0e10cSrcweir catch( uno::Exception& ) 60*cdf0e10cSrcweir { 61*cdf0e10cSrcweir } 62*cdf0e10cSrcweir return false; 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir /** Rounds the passed value to a multiple of 0.75 and converts it to 1/100 mm. */ 66*cdf0e10cSrcweir inline double lclPointsToHmm( const uno::Any& rPoints ) throw (uno::RuntimeException) 67*cdf0e10cSrcweir { 68*cdf0e10cSrcweir return PointsToHmm( ::rtl::math::approxFloor( rPoints.get< double >() / 0.75 ) * 0.75 ); 69*cdf0e10cSrcweir } 70*cdf0e10cSrcweir 71*cdf0e10cSrcweir } // namespace 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir // ============================================================================ 74*cdf0e10cSrcweir // Base implementations 75*cdf0e10cSrcweir // ============================================================================ 76*cdf0e10cSrcweir 77*cdf0e10cSrcweir /** Container for a specific type of drawing object in a spreadsheet. 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir Derived classes provide all required functionality specific to the type of 80*cdf0e10cSrcweir shapes covered by the container. 81*cdf0e10cSrcweir */ 82*cdf0e10cSrcweir class ScVbaObjectContainer : public ::cppu::WeakImplHelper1< container::XIndexAccess > 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir public: 85*cdf0e10cSrcweir explicit ScVbaObjectContainer( 86*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 87*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 88*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 89*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet, 90*cdf0e10cSrcweir const uno::Type& rVbaType ) throw (uno::RuntimeException); 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir /** Returns the VBA helper interface of the VBA collection object. */ 93*cdf0e10cSrcweir inline const uno::Reference< XHelperInterface >& getParent() const { return mxParent; } 94*cdf0e10cSrcweir /** Returns the component context of the VBA collection object. */ 95*cdf0e10cSrcweir inline const uno::Reference< uno::XComponentContext >& getContext() const { return mxContext; } 96*cdf0e10cSrcweir /** Returns the VBA type information of the objects in this container. */ 97*cdf0e10cSrcweir inline const uno::Type& getVbaType() const { return maVbaType; } 98*cdf0e10cSrcweir 99*cdf0e10cSrcweir /** Collects all shapes supported by this instance and inserts them into 100*cdf0e10cSrcweir the internal shape vector. */ 101*cdf0e10cSrcweir void collectShapes() throw (uno::RuntimeException); 102*cdf0e10cSrcweir /** Creates and returns a new UNO shape. */ 103*cdf0e10cSrcweir uno::Reference< drawing::XShape > createShape( const awt::Point& rPos, const awt::Size& rSize ) throw (uno::RuntimeException); 104*cdf0e10cSrcweir /** Inserts the passed shape into the draw page and into this container, and returns its index in the draw page. */ 105*cdf0e10cSrcweir sal_Int32 insertShape( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException); 106*cdf0e10cSrcweir /** Creates and returns a new VBA implementation object for the passed shape. */ 107*cdf0e10cSrcweir ::rtl::Reference< ScVbaSheetObjectBase > createVbaObject( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException); 108*cdf0e10cSrcweir /** Creates and returns a new VBA implementation object for the passed shape in an Any. */ 109*cdf0e10cSrcweir uno::Any createCollectionObject( const uno::Any& rSource ) throw (uno::RuntimeException); 110*cdf0e10cSrcweir /** Returns the VBA implementation object with the specified name. */ 111*cdf0e10cSrcweir uno::Any getItemByStringIndex( const OUString& rIndex ) throw (uno::RuntimeException); 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir // XIndexAccess 114*cdf0e10cSrcweir virtual sal_Int32 SAL_CALL getCount() throw (uno::RuntimeException); 115*cdf0e10cSrcweir virtual uno::Any SAL_CALL getByIndex( sal_Int32 nIndex ) throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException); 116*cdf0e10cSrcweir 117*cdf0e10cSrcweir // XElementAccess 118*cdf0e10cSrcweir virtual uno::Type SAL_CALL getElementType() throw (uno::RuntimeException); 119*cdf0e10cSrcweir virtual sal_Bool SAL_CALL hasElements() throw (uno::RuntimeException); 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir protected: 122*cdf0e10cSrcweir /** Derived classes return true, if the passed shape is supported by the instance. */ 123*cdf0e10cSrcweir virtual bool implPickShape( const uno::Reference< drawing::XShape >& rxShape ) const = 0; 124*cdf0e10cSrcweir /** Derived classes create and return a new VBA implementation object for the passed shape. */ 125*cdf0e10cSrcweir virtual ScVbaSheetObjectBase* implCreateVbaObject( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException) = 0; 126*cdf0e10cSrcweir /** Derived classes return the service name of the UNO shape. */ 127*cdf0e10cSrcweir virtual OUString implGetShapeServiceName() const = 0; 128*cdf0e10cSrcweir 129*cdf0e10cSrcweir /** Returns the shape name via 'Name' property of the UNO shape. May be overwritten. */ 130*cdf0e10cSrcweir virtual OUString implGetShapeName( const uno::Reference< drawing::XShape >& rxShape ) const throw (uno::RuntimeException); 131*cdf0e10cSrcweir /** Is called when a new UNO shape has been created but not yet inserted into the drawing page. */ 132*cdf0e10cSrcweir virtual void implOnShapeCreated( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException); 133*cdf0e10cSrcweir /** Is called when a new UNO shape has been inserted into the drawing page. */ 134*cdf0e10cSrcweir virtual void implOnShapeInserted( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir protected: 137*cdf0e10cSrcweir uno::Reference< XHelperInterface > mxParent; 138*cdf0e10cSrcweir uno::Reference< uno::XComponentContext > mxContext; 139*cdf0e10cSrcweir uno::Reference< frame::XModel > mxModel; 140*cdf0e10cSrcweir uno::Reference< lang::XMultiServiceFactory > mxFactory; 141*cdf0e10cSrcweir uno::Reference< drawing::XShapes > mxShapes; 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir private: 144*cdf0e10cSrcweir typedef ::std::vector< uno::Reference< drawing::XShape > > ShapeVector; 145*cdf0e10cSrcweir const uno::Type maVbaType; 146*cdf0e10cSrcweir ShapeVector maShapes; 147*cdf0e10cSrcweir }; 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir ScVbaObjectContainer::ScVbaObjectContainer( 152*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 153*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 154*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 155*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet, 156*cdf0e10cSrcweir const uno::Type& rVbaType ) throw (uno::RuntimeException) : 157*cdf0e10cSrcweir mxParent( rxParent ), 158*cdf0e10cSrcweir mxContext( rxContext ), 159*cdf0e10cSrcweir mxModel( rxModel, uno::UNO_SET_THROW ), 160*cdf0e10cSrcweir mxFactory( rxModel, uno::UNO_QUERY_THROW ), 161*cdf0e10cSrcweir maVbaType( rVbaType ) 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir uno::Reference< drawing::XDrawPageSupplier > xDrawPageSupp( rxSheet, uno::UNO_QUERY_THROW ); 164*cdf0e10cSrcweir mxShapes.set( xDrawPageSupp->getDrawPage(), uno::UNO_QUERY_THROW ); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir void ScVbaObjectContainer::collectShapes() throw (uno::RuntimeException) 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir maShapes.clear(); 170*cdf0e10cSrcweir for( sal_Int32 nIndex = 0, nCount = mxShapes->getCount(); nIndex < nCount; ++nIndex ) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir uno::Reference< drawing::XShape > xShape( mxShapes->getByIndex( nIndex ), uno::UNO_QUERY_THROW ); 173*cdf0e10cSrcweir if( implPickShape( xShape ) ) 174*cdf0e10cSrcweir maShapes.push_back( xShape ); 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir } 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir uno::Reference< drawing::XShape > ScVbaObjectContainer::createShape( const awt::Point& rPos, const awt::Size& rSize ) throw (uno::RuntimeException) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir uno::Reference< drawing::XShape > xShape( mxFactory->createInstance( implGetShapeServiceName() ), uno::UNO_QUERY_THROW ); 181*cdf0e10cSrcweir xShape->setPosition( rPos ); 182*cdf0e10cSrcweir xShape->setSize( rSize ); 183*cdf0e10cSrcweir implOnShapeCreated( xShape ); 184*cdf0e10cSrcweir return xShape; 185*cdf0e10cSrcweir } 186*cdf0e10cSrcweir 187*cdf0e10cSrcweir sal_Int32 ScVbaObjectContainer::insertShape( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException) 188*cdf0e10cSrcweir { 189*cdf0e10cSrcweir mxShapes->add( rxShape ); 190*cdf0e10cSrcweir maShapes.push_back( rxShape ); 191*cdf0e10cSrcweir implOnShapeInserted( rxShape ); 192*cdf0e10cSrcweir return mxShapes->getCount() - 1; 193*cdf0e10cSrcweir } 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir ::rtl::Reference< ScVbaSheetObjectBase > ScVbaObjectContainer::createVbaObject( 196*cdf0e10cSrcweir const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir return implCreateVbaObject( rxShape ); 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir uno::Any ScVbaObjectContainer::createCollectionObject( const uno::Any& rSource ) throw (uno::RuntimeException) 202*cdf0e10cSrcweir { 203*cdf0e10cSrcweir uno::Reference< drawing::XShape > xShape( rSource, uno::UNO_QUERY_THROW ); 204*cdf0e10cSrcweir uno::Reference< excel::XSheetObject > xSheetObject( implCreateVbaObject( xShape ) ); 205*cdf0e10cSrcweir return uno::Any( xSheetObject ); 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir uno::Any ScVbaObjectContainer::getItemByStringIndex( const OUString& rIndex ) throw (uno::RuntimeException) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir for( ShapeVector::iterator aIt = maShapes.begin(), aEnd = maShapes.end(); aIt != aEnd; ++aIt ) 211*cdf0e10cSrcweir if( rIndex == implGetShapeName( *aIt ) ) 212*cdf0e10cSrcweir return createCollectionObject( uno::Any( *aIt ) ); 213*cdf0e10cSrcweir throw uno::RuntimeException(); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir // XIndexAccess 217*cdf0e10cSrcweir 218*cdf0e10cSrcweir sal_Int32 SAL_CALL ScVbaObjectContainer::getCount() throw (uno::RuntimeException) 219*cdf0e10cSrcweir { 220*cdf0e10cSrcweir return static_cast< sal_Int32 >( maShapes.size() ); 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir uno::Any SAL_CALL ScVbaObjectContainer::getByIndex( sal_Int32 nIndex ) 224*cdf0e10cSrcweir throw (lang::IndexOutOfBoundsException, lang::WrappedTargetException, uno::RuntimeException) 225*cdf0e10cSrcweir { 226*cdf0e10cSrcweir if( (0 <= nIndex) && (nIndex < getCount()) ) 227*cdf0e10cSrcweir return uno::Any( maShapes[ static_cast< size_t >( nIndex ) ] ); 228*cdf0e10cSrcweir throw lang::IndexOutOfBoundsException(); 229*cdf0e10cSrcweir } 230*cdf0e10cSrcweir 231*cdf0e10cSrcweir // XElementAccess 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir uno::Type SAL_CALL ScVbaObjectContainer::getElementType() throw (uno::RuntimeException) 234*cdf0e10cSrcweir { 235*cdf0e10cSrcweir return drawing::XShape::static_type( 0 ); 236*cdf0e10cSrcweir } 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir sal_Bool SAL_CALL ScVbaObjectContainer::hasElements() throw (uno::RuntimeException) 239*cdf0e10cSrcweir { 240*cdf0e10cSrcweir return !maShapes.empty(); 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir 243*cdf0e10cSrcweir // private 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir OUString ScVbaObjectContainer::implGetShapeName( const uno::Reference< drawing::XShape >& rxShape ) const throw (uno::RuntimeException) 246*cdf0e10cSrcweir { 247*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xPropSet( rxShape, uno::UNO_QUERY_THROW ); 248*cdf0e10cSrcweir return xPropSet->getPropertyValue( CREATE_OUSTRING( "Name" ) ).get< OUString >(); 249*cdf0e10cSrcweir } 250*cdf0e10cSrcweir 251*cdf0e10cSrcweir void ScVbaObjectContainer::implOnShapeCreated( const uno::Reference< drawing::XShape >& /*rxShape*/ ) throw (uno::RuntimeException) 252*cdf0e10cSrcweir { 253*cdf0e10cSrcweir } 254*cdf0e10cSrcweir 255*cdf0e10cSrcweir void ScVbaObjectContainer::implOnShapeInserted( const uno::Reference< drawing::XShape >& /*rxShape*/ ) throw (uno::RuntimeException) 256*cdf0e10cSrcweir { 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir // ============================================================================ 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir class ScVbaObjectEnumeration : public SimpleEnumerationBase 262*cdf0e10cSrcweir { 263*cdf0e10cSrcweir public: 264*cdf0e10cSrcweir explicit ScVbaObjectEnumeration( const ScVbaObjectContainerRef& rxContainer ); 265*cdf0e10cSrcweir virtual uno::Any createCollectionObject( const uno::Any& rSource ); 266*cdf0e10cSrcweir 267*cdf0e10cSrcweir private: 268*cdf0e10cSrcweir ScVbaObjectContainerRef mxContainer; 269*cdf0e10cSrcweir }; 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir ScVbaObjectEnumeration::ScVbaObjectEnumeration( const ScVbaObjectContainerRef& rxContainer ) : 274*cdf0e10cSrcweir SimpleEnumerationBase( rxContainer->getParent(), rxContainer->getContext(), rxContainer.get() ), 275*cdf0e10cSrcweir mxContainer( rxContainer ) 276*cdf0e10cSrcweir { 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir uno::Any ScVbaObjectEnumeration::createCollectionObject( const uno::Any& rSource ) 280*cdf0e10cSrcweir { 281*cdf0e10cSrcweir return mxContainer->createCollectionObject( rSource ); 282*cdf0e10cSrcweir } 283*cdf0e10cSrcweir 284*cdf0e10cSrcweir // ============================================================================ 285*cdf0e10cSrcweir 286*cdf0e10cSrcweir ScVbaSheetObjectsBase::ScVbaSheetObjectsBase( const ScVbaObjectContainerRef& rxContainer ) throw (css::uno::RuntimeException) : 287*cdf0e10cSrcweir ScVbaSheetObjects_BASE( rxContainer->getParent(), rxContainer->getContext(), rxContainer.get() ), 288*cdf0e10cSrcweir mxContainer( rxContainer ) 289*cdf0e10cSrcweir { 290*cdf0e10cSrcweir mxContainer->collectShapes(); 291*cdf0e10cSrcweir } 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir ScVbaSheetObjectsBase::~ScVbaSheetObjectsBase() 294*cdf0e10cSrcweir { 295*cdf0e10cSrcweir } 296*cdf0e10cSrcweir 297*cdf0e10cSrcweir void ScVbaSheetObjectsBase::collectShapes() throw (uno::RuntimeException) 298*cdf0e10cSrcweir { 299*cdf0e10cSrcweir mxContainer->collectShapes(); 300*cdf0e10cSrcweir } 301*cdf0e10cSrcweir 302*cdf0e10cSrcweir // XEnumerationAccess 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL ScVbaSheetObjectsBase::createEnumeration() throw (uno::RuntimeException) 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir return new ScVbaObjectEnumeration( mxContainer ); 307*cdf0e10cSrcweir } 308*cdf0e10cSrcweir 309*cdf0e10cSrcweir // XElementAccess 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir uno::Type SAL_CALL ScVbaSheetObjectsBase::getElementType() throw (uno::RuntimeException) 312*cdf0e10cSrcweir { 313*cdf0e10cSrcweir return mxContainer->getVbaType(); 314*cdf0e10cSrcweir } 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir // ScVbaCollectionBase 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir uno::Any ScVbaSheetObjectsBase::createCollectionObject( const uno::Any& rSource ) 319*cdf0e10cSrcweir { 320*cdf0e10cSrcweir return mxContainer->createCollectionObject( rSource ); 321*cdf0e10cSrcweir } 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir uno::Any ScVbaSheetObjectsBase::getItemByStringIndex( const OUString& rIndex ) throw (uno::RuntimeException) 324*cdf0e10cSrcweir { 325*cdf0e10cSrcweir return mxContainer->getItemByStringIndex( rIndex ); 326*cdf0e10cSrcweir } 327*cdf0e10cSrcweir 328*cdf0e10cSrcweir // ============================================================================ 329*cdf0e10cSrcweir // Graphic object containers supporting ooo.vba.excel.XGraphicObject 330*cdf0e10cSrcweir // ============================================================================ 331*cdf0e10cSrcweir 332*cdf0e10cSrcweir ScVbaGraphicObjectsBase::ScVbaGraphicObjectsBase( const ScVbaObjectContainerRef& rxContainer ) throw (uno::RuntimeException) : 333*cdf0e10cSrcweir ScVbaGraphicObjects_BASE( rxContainer ) 334*cdf0e10cSrcweir { 335*cdf0e10cSrcweir } 336*cdf0e10cSrcweir 337*cdf0e10cSrcweir // XGraphicObjects 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir uno::Any SAL_CALL ScVbaGraphicObjectsBase::Add( const uno::Any& rLeft, const uno::Any& rTop, const uno::Any& rWidth, const uno::Any& rHeight ) throw (uno::RuntimeException) 340*cdf0e10cSrcweir { 341*cdf0e10cSrcweir /* Extract double values from passed Anys (the lclPointsToHmm() helper 342*cdf0e10cSrcweir function will throw a RuntimeException on any error), and convert from 343*cdf0e10cSrcweir points to 1/100 mm. */ 344*cdf0e10cSrcweir awt::Point aPos( lclPointsToHmm( rLeft ), lclPointsToHmm( rTop ) ); 345*cdf0e10cSrcweir awt::Size aSize( lclPointsToHmm( rWidth ), lclPointsToHmm( rHeight ) ); 346*cdf0e10cSrcweir // TODO: translate coordinates for RTL sheets 347*cdf0e10cSrcweir if( (aPos.X < 0) || (aPos.Y < 0) || (aSize.Width <= 0) || (aSize.Height <= 0) ) 348*cdf0e10cSrcweir throw uno::RuntimeException(); 349*cdf0e10cSrcweir 350*cdf0e10cSrcweir // create the UNO shape 351*cdf0e10cSrcweir uno::Reference< drawing::XShape > xShape( mxContainer->createShape( aPos, aSize ), uno::UNO_SET_THROW ); 352*cdf0e10cSrcweir sal_Int32 nIndex = mxContainer->insertShape( xShape ); 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir // create and return the VBA object 355*cdf0e10cSrcweir ::rtl::Reference< ScVbaSheetObjectBase > xVbaObject = mxContainer->createVbaObject( xShape ); 356*cdf0e10cSrcweir xVbaObject->setDefaultProperties( nIndex ); 357*cdf0e10cSrcweir return uno::Any( uno::Reference< excel::XSheetObject >( xVbaObject.get() ) ); 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir // ============================================================================ 361*cdf0e10cSrcweir // Drawing controls 362*cdf0e10cSrcweir // ============================================================================ 363*cdf0e10cSrcweir 364*cdf0e10cSrcweir class ScVbaControlContainer : public ScVbaObjectContainer 365*cdf0e10cSrcweir { 366*cdf0e10cSrcweir public: 367*cdf0e10cSrcweir explicit ScVbaControlContainer( 368*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 369*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 370*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 371*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet, 372*cdf0e10cSrcweir const uno::Type& rVbaType, 373*cdf0e10cSrcweir const OUString& rModelServiceName, 374*cdf0e10cSrcweir sal_Int16 nComponentType ) throw (uno::RuntimeException); 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir protected: 377*cdf0e10cSrcweir uno::Reference< container::XIndexContainer > createForm() throw (uno::RuntimeException); 378*cdf0e10cSrcweir 379*cdf0e10cSrcweir virtual bool implPickShape( const uno::Reference< drawing::XShape >& rxShape ) const; 380*cdf0e10cSrcweir virtual OUString implGetShapeServiceName() const; 381*cdf0e10cSrcweir virtual bool implCheckProperties( const uno::Reference< beans::XPropertySet >& rxModelProps ) const; 382*cdf0e10cSrcweir virtual OUString implGetShapeName( const uno::Reference< drawing::XShape >& rxShape ) const throw (uno::RuntimeException); 383*cdf0e10cSrcweir virtual void implOnShapeCreated( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException); 384*cdf0e10cSrcweir 385*cdf0e10cSrcweir protected: 386*cdf0e10cSrcweir uno::Reference< container::XIndexContainer > mxFormIC; 387*cdf0e10cSrcweir OUString maModelServiceName; 388*cdf0e10cSrcweir sal_Int16 mnComponentType; 389*cdf0e10cSrcweir }; 390*cdf0e10cSrcweir 391*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 392*cdf0e10cSrcweir 393*cdf0e10cSrcweir ScVbaControlContainer::ScVbaControlContainer( 394*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 395*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 396*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 397*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet, 398*cdf0e10cSrcweir const uno::Type& rVbaType, 399*cdf0e10cSrcweir const OUString& rModelServiceName, 400*cdf0e10cSrcweir sal_Int16 nComponentType ) throw (uno::RuntimeException) : 401*cdf0e10cSrcweir ScVbaObjectContainer( rxParent, rxContext, rxModel, rxSheet, rVbaType ), 402*cdf0e10cSrcweir maModelServiceName( rModelServiceName ), 403*cdf0e10cSrcweir mnComponentType( nComponentType ) 404*cdf0e10cSrcweir { 405*cdf0e10cSrcweir } 406*cdf0e10cSrcweir 407*cdf0e10cSrcweir uno::Reference< container::XIndexContainer > ScVbaControlContainer::createForm() throw (uno::RuntimeException) 408*cdf0e10cSrcweir { 409*cdf0e10cSrcweir if( !mxFormIC.is() ) 410*cdf0e10cSrcweir { 411*cdf0e10cSrcweir uno::Reference< form::XFormsSupplier > xFormsSupp( mxShapes, uno::UNO_QUERY_THROW ); 412*cdf0e10cSrcweir uno::Reference< container::XNameContainer > xFormsNC( xFormsSupp->getForms(), uno::UNO_SET_THROW ); 413*cdf0e10cSrcweir OUString aFormName = CREATE_OUSTRING( "Standard" ); 414*cdf0e10cSrcweir if( xFormsNC->hasByName( aFormName ) ) 415*cdf0e10cSrcweir { 416*cdf0e10cSrcweir mxFormIC.set( xFormsNC->getByName( aFormName ), uno::UNO_QUERY_THROW ); 417*cdf0e10cSrcweir } 418*cdf0e10cSrcweir else 419*cdf0e10cSrcweir { 420*cdf0e10cSrcweir uno::Reference< form::XForm > xForm( mxFactory->createInstance( CREATE_OUSTRING( "com.sun.star.form.component.Form" ) ), uno::UNO_QUERY_THROW ); 421*cdf0e10cSrcweir xFormsNC->insertByName( aFormName, uno::Any( xForm ) ); 422*cdf0e10cSrcweir mxFormIC.set( xForm, uno::UNO_QUERY_THROW ); 423*cdf0e10cSrcweir } 424*cdf0e10cSrcweir } 425*cdf0e10cSrcweir return mxFormIC; 426*cdf0e10cSrcweir } 427*cdf0e10cSrcweir 428*cdf0e10cSrcweir bool ScVbaControlContainer::implPickShape( const uno::Reference< drawing::XShape >& rxShape ) const 429*cdf0e10cSrcweir { 430*cdf0e10cSrcweir try 431*cdf0e10cSrcweir { 432*cdf0e10cSrcweir uno::Reference< drawing::XControlShape > xControlShape( rxShape, uno::UNO_QUERY_THROW ); 433*cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xModelProps( xControlShape->getControl(), uno::UNO_QUERY_THROW ); 434*cdf0e10cSrcweir sal_Int16 nClassId = -1; 435*cdf0e10cSrcweir return lclGetProperty( nClassId, xModelProps, CREATE_OUSTRING( "ClassId" ) ) && 436*cdf0e10cSrcweir (nClassId == mnComponentType) && implCheckProperties( xModelProps ); 437*cdf0e10cSrcweir } 438*cdf0e10cSrcweir catch( uno::Exception& ) 439*cdf0e10cSrcweir { 440*cdf0e10cSrcweir } 441*cdf0e10cSrcweir return false; 442*cdf0e10cSrcweir } 443*cdf0e10cSrcweir 444*cdf0e10cSrcweir OUString ScVbaControlContainer::implGetShapeServiceName() const 445*cdf0e10cSrcweir { 446*cdf0e10cSrcweir return CREATE_OUSTRING( "com.sun.star.drawing.ControlShape" ); 447*cdf0e10cSrcweir } 448*cdf0e10cSrcweir 449*cdf0e10cSrcweir bool ScVbaControlContainer::implCheckProperties( const uno::Reference< beans::XPropertySet >& /*rxModelProps*/ ) const 450*cdf0e10cSrcweir { 451*cdf0e10cSrcweir return true; 452*cdf0e10cSrcweir } 453*cdf0e10cSrcweir 454*cdf0e10cSrcweir OUString ScVbaControlContainer::implGetShapeName( const uno::Reference< drawing::XShape >& rxShape ) const throw (uno::RuntimeException) 455*cdf0e10cSrcweir { 456*cdf0e10cSrcweir uno::Reference< drawing::XControlShape > xControlShape( rxShape, uno::UNO_QUERY_THROW ); 457*cdf0e10cSrcweir return uno::Reference< container::XNamed >( xControlShape->getControl(), uno::UNO_QUERY_THROW )->getName(); 458*cdf0e10cSrcweir } 459*cdf0e10cSrcweir 460*cdf0e10cSrcweir void ScVbaControlContainer::implOnShapeCreated( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException) 461*cdf0e10cSrcweir { 462*cdf0e10cSrcweir // passed shape must be a control shape 463*cdf0e10cSrcweir uno::Reference< drawing::XControlShape > xControlShape( rxShape, uno::UNO_QUERY_THROW ); 464*cdf0e10cSrcweir 465*cdf0e10cSrcweir // create the UNO control model 466*cdf0e10cSrcweir uno::Reference< form::XFormComponent > xFormComponent( mxFactory->createInstance( maModelServiceName ), uno::UNO_QUERY_THROW ); 467*cdf0e10cSrcweir uno::Reference< awt::XControlModel > xControlModel( xFormComponent, uno::UNO_QUERY_THROW ); 468*cdf0e10cSrcweir 469*cdf0e10cSrcweir // insert the control model into the form and the shape 470*cdf0e10cSrcweir createForm(); 471*cdf0e10cSrcweir mxFormIC->insertByIndex( mxFormIC->getCount(), uno::Any( xFormComponent ) ); 472*cdf0e10cSrcweir xControlShape->setControl( xControlModel ); 473*cdf0e10cSrcweir } 474*cdf0e10cSrcweir 475*cdf0e10cSrcweir // ============================================================================ 476*cdf0e10cSrcweir // Push button 477*cdf0e10cSrcweir // ============================================================================ 478*cdf0e10cSrcweir 479*cdf0e10cSrcweir class ScVbaButtonContainer : public ScVbaControlContainer 480*cdf0e10cSrcweir { 481*cdf0e10cSrcweir public: 482*cdf0e10cSrcweir explicit ScVbaButtonContainer( 483*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 484*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 485*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 486*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet ) throw (uno::RuntimeException); 487*cdf0e10cSrcweir 488*cdf0e10cSrcweir protected: 489*cdf0e10cSrcweir virtual ScVbaSheetObjectBase* implCreateVbaObject( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException); 490*cdf0e10cSrcweir virtual bool implCheckProperties( const uno::Reference< beans::XPropertySet >& rxModelProps ) const; 491*cdf0e10cSrcweir }; 492*cdf0e10cSrcweir 493*cdf0e10cSrcweir // ---------------------------------------------------------------------------- 494*cdf0e10cSrcweir 495*cdf0e10cSrcweir ScVbaButtonContainer::ScVbaButtonContainer( 496*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 497*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 498*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 499*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet ) throw (uno::RuntimeException) : 500*cdf0e10cSrcweir ScVbaControlContainer( 501*cdf0e10cSrcweir rxParent, rxContext, rxModel, rxSheet, 502*cdf0e10cSrcweir excel::XButton::static_type( 0 ), 503*cdf0e10cSrcweir CREATE_OUSTRING( "com.sun.star.form.component.CommandButton" ), 504*cdf0e10cSrcweir form::FormComponentType::COMMANDBUTTON ) 505*cdf0e10cSrcweir { 506*cdf0e10cSrcweir } 507*cdf0e10cSrcweir 508*cdf0e10cSrcweir ScVbaSheetObjectBase* ScVbaButtonContainer::implCreateVbaObject( const uno::Reference< drawing::XShape >& rxShape ) throw (uno::RuntimeException) 509*cdf0e10cSrcweir { 510*cdf0e10cSrcweir uno::Reference< drawing::XControlShape > xControlShape( rxShape, uno::UNO_QUERY_THROW ); 511*cdf0e10cSrcweir return new ScVbaButton( mxParent, mxContext, mxModel, createForm(), xControlShape ); 512*cdf0e10cSrcweir } 513*cdf0e10cSrcweir 514*cdf0e10cSrcweir bool ScVbaButtonContainer::implCheckProperties( const uno::Reference< beans::XPropertySet >& rxModelProps ) const 515*cdf0e10cSrcweir { 516*cdf0e10cSrcweir // do not insert toggle buttons into the 'Buttons' collection 517*cdf0e10cSrcweir bool bToggle = false; 518*cdf0e10cSrcweir return lclGetProperty( bToggle, rxModelProps, CREATE_OUSTRING( "Toggle" ) ) && !bToggle; 519*cdf0e10cSrcweir } 520*cdf0e10cSrcweir 521*cdf0e10cSrcweir // ============================================================================ 522*cdf0e10cSrcweir 523*cdf0e10cSrcweir ScVbaButtons::ScVbaButtons( 524*cdf0e10cSrcweir const uno::Reference< XHelperInterface >& rxParent, 525*cdf0e10cSrcweir const uno::Reference< uno::XComponentContext >& rxContext, 526*cdf0e10cSrcweir const uno::Reference< frame::XModel >& rxModel, 527*cdf0e10cSrcweir const uno::Reference< sheet::XSpreadsheet >& rxSheet ) throw (uno::RuntimeException) : 528*cdf0e10cSrcweir ScVbaGraphicObjectsBase( new ScVbaButtonContainer( rxParent, rxContext, rxModel, rxSheet ) ) 529*cdf0e10cSrcweir { 530*cdf0e10cSrcweir } 531*cdf0e10cSrcweir 532*cdf0e10cSrcweir VBAHELPER_IMPL_XHELPERINTERFACE( ScVbaButtons, "ooo.vba.excel.Buttons" ) 533*cdf0e10cSrcweir 534*cdf0e10cSrcweir // ============================================================================ 535