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