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 #include "vbastyles.hxx" 24cdf0e10cSrcweir #include "vbastyle.hxx" 25cdf0e10cSrcweir #include <ooo/vba/excel/XRange.hpp> 26cdf0e10cSrcweir 27cdf0e10cSrcweir using namespace ::ooo::vba; 28cdf0e10cSrcweir using namespace ::com::sun::star; 29cdf0e10cSrcweir 30cdf0e10cSrcweir static rtl::OUString SDEFAULTCELLSTYLENAME( RTL_CONSTASCII_USTRINGPARAM("Default") ); 31cdf0e10cSrcweir css::uno::Any 32cdf0e10cSrcweir lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel ) 33cdf0e10cSrcweir { 34cdf0e10cSrcweir uno::Reference< beans::XPropertySet > xStyleProps( aObject, uno::UNO_QUERY_THROW ); 35cdf0e10cSrcweir uno::Reference< excel::XStyle > xStyle( new ScVbaStyle( xParent, xContext, xStyleProps, xModel ) ); 36cdf0e10cSrcweir return uno::makeAny( xStyle ); 37cdf0e10cSrcweir } 38cdf0e10cSrcweir 39cdf0e10cSrcweir 40cdf0e10cSrcweir ScVbaStyles::ScVbaStyles( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< css::uno::XComponentContext > & xContext, const uno::Reference< frame::XModel >& xModel ) throw ( script::BasicErrorException ) : ScVbaStyles_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( ScVbaStyle::getStylesNameContainer( xModel ), uno::UNO_QUERY_THROW ) ), mxModel( xModel ), mxParent( xParent ) 41cdf0e10cSrcweir { 42cdf0e10cSrcweir try 43cdf0e10cSrcweir { 44cdf0e10cSrcweir mxMSF.set( mxModel, uno::UNO_QUERY_THROW ); 45cdf0e10cSrcweir mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW ); 46cdf0e10cSrcweir } 47cdf0e10cSrcweir catch (uno::Exception& ) 48cdf0e10cSrcweir { 49cdf0e10cSrcweir DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 50cdf0e10cSrcweir } 51cdf0e10cSrcweir } 52cdf0e10cSrcweir 53cdf0e10cSrcweir uno::Sequence< rtl::OUString > 54cdf0e10cSrcweir ScVbaStyles::getStyleNames() throw ( uno::RuntimeException ) 55cdf0e10cSrcweir { 56cdf0e10cSrcweir return mxNameContainerCellStyles->getElementNames(); 57cdf0e10cSrcweir } 58cdf0e10cSrcweir 59cdf0e10cSrcweir 60cdf0e10cSrcweir uno::Any 61cdf0e10cSrcweir ScVbaStyles::createCollectionObject(const uno::Any& aObject) 62cdf0e10cSrcweir { 63cdf0e10cSrcweir return lcl_createAPIStyleToVBAObject( aObject, mxParent, mxContext, mxModel ); 64cdf0e10cSrcweir } 65cdf0e10cSrcweir 66cdf0e10cSrcweir uno::Type SAL_CALL 67cdf0e10cSrcweir ScVbaStyles::getElementType() throw (uno::RuntimeException) 68cdf0e10cSrcweir { 69cdf0e10cSrcweir return excel::XStyle::static_type(0); 70cdf0e10cSrcweir } 71cdf0e10cSrcweir 72cdf0e10cSrcweir class EnumWrapper : public EnumerationHelper_BASE 73cdf0e10cSrcweir { 74cdf0e10cSrcweir uno::Reference<container::XIndexAccess > m_xIndexAccess; 75cdf0e10cSrcweir uno::Reference<XHelperInterface > m_xParent; 76cdf0e10cSrcweir uno::Reference<uno::XComponentContext > m_xContext; 77cdf0e10cSrcweir uno::Reference<frame::XModel > m_xModel; 78cdf0e10cSrcweir 79cdf0e10cSrcweir sal_Int32 nIndex; 80cdf0e10cSrcweir public: 81cdf0e10cSrcweir EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<XHelperInterface >& xParent, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<frame::XModel >& xModel ) : m_xIndexAccess( xIndexAccess ), m_xParent( xParent ), m_xContext( xContext ), m_xModel( xModel ), nIndex( 0 ) {} 82cdf0e10cSrcweir virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException) 83cdf0e10cSrcweir { 84cdf0e10cSrcweir return ( nIndex < m_xIndexAccess->getCount() ); 85cdf0e10cSrcweir } 86cdf0e10cSrcweir virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException) 87cdf0e10cSrcweir { 88cdf0e10cSrcweir if ( nIndex < m_xIndexAccess->getCount() ) 89cdf0e10cSrcweir return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel ); 90cdf0e10cSrcweir throw container::NoSuchElementException(); 91cdf0e10cSrcweir } 92cdf0e10cSrcweir }; 93cdf0e10cSrcweir 94cdf0e10cSrcweir uno::Reference< container::XEnumeration > SAL_CALL 95cdf0e10cSrcweir ScVbaStyles::createEnumeration() throw (uno::RuntimeException) 96cdf0e10cSrcweir { 97cdf0e10cSrcweir return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel ); 98cdf0e10cSrcweir } 99cdf0e10cSrcweir 100cdf0e10cSrcweir uno::Reference< excel::XStyle > SAL_CALL 101cdf0e10cSrcweir ScVbaStyles::Add( const ::rtl::OUString& _sName, const uno::Any& _aBasedOn ) throw (script::BasicErrorException, uno::RuntimeException) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir uno::Reference< excel::XStyle > aRet; 104cdf0e10cSrcweir try 105cdf0e10cSrcweir { 106cdf0e10cSrcweir rtl::OUString sParentCellStyleName( RTL_CONSTASCII_USTRINGPARAM("Default")); 107cdf0e10cSrcweir if ( _aBasedOn.hasValue() ) 108cdf0e10cSrcweir { 109cdf0e10cSrcweir uno::Reference< excel::XRange > oRange; 110cdf0e10cSrcweir if ( _aBasedOn >>= oRange) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW ); 113cdf0e10cSrcweir if ( oStyle.is() ) 114cdf0e10cSrcweir { 115cdf0e10cSrcweir sParentCellStyleName = oStyle->getName(); 116cdf0e10cSrcweir } 117cdf0e10cSrcweir else 118cdf0e10cSrcweir { 119cdf0e10cSrcweir DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString() ); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir } 122cdf0e10cSrcweir else 123cdf0e10cSrcweir { 124cdf0e10cSrcweir DebugHelper::exception(SbERR_BAD_ARGUMENT, rtl::OUString()); 125cdf0e10cSrcweir } 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir uno::Reference< style::XStyle > xStyle( mxMSF->createInstance( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.style.CellStyle"))), uno::UNO_QUERY_THROW ); 129cdf0e10cSrcweir 130cdf0e10cSrcweir if (!mxNameContainerCellStyles->hasByName(_sName)) 131cdf0e10cSrcweir { 132cdf0e10cSrcweir mxNameContainerCellStyles->insertByName(_sName, uno::makeAny( xStyle) ); 133cdf0e10cSrcweir } 134cdf0e10cSrcweir if (!sParentCellStyleName.equals(SDEFAULTCELLSTYLENAME)) 135cdf0e10cSrcweir { 136cdf0e10cSrcweir xStyle->setParentStyle( sParentCellStyleName ); 137cdf0e10cSrcweir } 138cdf0e10cSrcweir aRet.set( Item( uno::makeAny( _sName ), uno::Any() ), uno::UNO_QUERY_THROW ); 139cdf0e10cSrcweir } 140cdf0e10cSrcweir catch (uno::Exception& ) 141cdf0e10cSrcweir { 142cdf0e10cSrcweir DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 143cdf0e10cSrcweir } 144cdf0e10cSrcweir return aRet; 145cdf0e10cSrcweir } 146cdf0e10cSrcweir 147cdf0e10cSrcweir void 148cdf0e10cSrcweir ScVbaStyles::Delete(const rtl::OUString _sStyleName) throw ( script::BasicErrorException ) 149cdf0e10cSrcweir { 150cdf0e10cSrcweir try 151cdf0e10cSrcweir { 152cdf0e10cSrcweir if (mxNameContainerCellStyles->hasByName( _sStyleName ) ) 153cdf0e10cSrcweir mxNameContainerCellStyles->removeByName( _sStyleName ); 154cdf0e10cSrcweir } 155cdf0e10cSrcweir catch (uno::Exception& ) 156cdf0e10cSrcweir { 157cdf0e10cSrcweir DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString()); 158cdf0e10cSrcweir } 159cdf0e10cSrcweir } 160cdf0e10cSrcweir 161cdf0e10cSrcweir rtl::OUString& 162cdf0e10cSrcweir ScVbaStyles::getServiceImplName() 163cdf0e10cSrcweir { 164cdf0e10cSrcweir static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaStyles") ); 165cdf0e10cSrcweir return sImplName; 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir uno::Sequence< rtl::OUString > 169cdf0e10cSrcweir ScVbaStyles::getServiceNames() 170cdf0e10cSrcweir { 171cdf0e10cSrcweir static uno::Sequence< rtl::OUString > aServiceNames; 172cdf0e10cSrcweir if ( aServiceNames.getLength() == 0 ) 173cdf0e10cSrcweir { 174cdf0e10cSrcweir aServiceNames.realloc( 1 ); 175cdf0e10cSrcweir aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.XStyles" ) ); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir return aServiceNames; 178cdf0e10cSrcweir } 179