bump product version to 4.1.6.2
[LibreOffice.git] / sc / source / ui / vba / vbastyles.cxx
blobdf854d17d5930794ad5fd8f50d7abbf0acf01399
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include "vbastyles.hxx"
20 #include "vbastyle.hxx"
21 #include <ooo/vba/excel/XRange.hpp>
23 using namespace ::ooo::vba;
24 using namespace ::com::sun::star;
26 static OUString SDEFAULTCELLSTYLENAME("Default");
27 static css::uno::Any
28 lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel )
30 uno::Reference< beans::XPropertySet > xStyleProps( aObject, uno::UNO_QUERY_THROW );
31 uno::Reference< excel::XStyle > xStyle( new ScVbaStyle( xParent, xContext, xStyleProps, xModel ) );
32 return uno::makeAny( xStyle );
36 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 )
38 try
40 mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
41 mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW );
43 catch (uno::Exception& )
45 DebugHelper::exception(SbERR_METHOD_FAILED, OUString());
49 uno::Sequence< OUString >
50 ScVbaStyles::getStyleNames() throw ( uno::RuntimeException )
52 return mxNameContainerCellStyles->getElementNames();
56 uno::Any
57 ScVbaStyles::createCollectionObject(const uno::Any& aObject)
59 return lcl_createAPIStyleToVBAObject( aObject, mxParent, mxContext, mxModel );
62 uno::Type SAL_CALL
63 ScVbaStyles::getElementType() throw (uno::RuntimeException)
65 return excel::XStyle::static_type(0);
68 class EnumWrapper : public EnumerationHelper_BASE
70 uno::Reference<container::XIndexAccess > m_xIndexAccess;
71 uno::Reference<XHelperInterface > m_xParent;
72 uno::Reference<uno::XComponentContext > m_xContext;
73 uno::Reference<frame::XModel > m_xModel;
75 sal_Int32 nIndex;
76 public:
77 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 ) {}
78 virtual ::sal_Bool SAL_CALL hasMoreElements( ) throw (uno::RuntimeException)
80 return ( nIndex < m_xIndexAccess->getCount() );
82 virtual uno::Any SAL_CALL nextElement( ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException)
84 if ( nIndex < m_xIndexAccess->getCount() )
85 return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel );
86 throw container::NoSuchElementException();
90 uno::Reference< container::XEnumeration > SAL_CALL
91 ScVbaStyles::createEnumeration() throw (uno::RuntimeException)
93 return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel );
96 uno::Reference< excel::XStyle > SAL_CALL
97 ScVbaStyles::Add( const OUString& _sName, const uno::Any& _aBasedOn ) throw (script::BasicErrorException, uno::RuntimeException)
99 uno::Reference< excel::XStyle > aRet;
102 OUString sParentCellStyleName("Default");
103 if ( _aBasedOn.hasValue() )
105 uno::Reference< excel::XRange > oRange;
106 if ( _aBasedOn >>= oRange)
108 uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW );
109 if ( oStyle.is() )
111 sParentCellStyleName = oStyle->getName();
113 else
115 DebugHelper::exception(SbERR_BAD_ARGUMENT, OUString() );
118 else
120 DebugHelper::exception(SbERR_BAD_ARGUMENT, OUString());
124 uno::Reference< style::XStyle > xStyle( mxMSF->createInstance( OUString("com.sun.star.style.CellStyle")), uno::UNO_QUERY_THROW );
126 if (!mxNameContainerCellStyles->hasByName(_sName))
128 mxNameContainerCellStyles->insertByName(_sName, uno::makeAny( xStyle) );
130 if (!sParentCellStyleName.equals(SDEFAULTCELLSTYLENAME))
132 xStyle->setParentStyle( sParentCellStyleName );
134 aRet.set( Item( uno::makeAny( _sName ), uno::Any() ), uno::UNO_QUERY_THROW );
136 catch (uno::Exception& )
138 DebugHelper::exception(SbERR_METHOD_FAILED, OUString());
140 return aRet;
143 void
144 ScVbaStyles::Delete(const OUString _sStyleName) throw ( script::BasicErrorException )
148 if (mxNameContainerCellStyles->hasByName( _sStyleName ) )
149 mxNameContainerCellStyles->removeByName( _sStyleName );
151 catch (uno::Exception& )
153 DebugHelper::exception(SbERR_METHOD_FAILED, OUString());
157 OUString
158 ScVbaStyles::getServiceImplName()
160 return OUString("ScVbaStyles");
163 uno::Sequence< OUString >
164 ScVbaStyles::getServiceNames()
166 static uno::Sequence< OUString > aServiceNames;
167 if ( aServiceNames.getLength() == 0 )
169 aServiceNames.realloc( 1 );
170 aServiceNames[ 0 ] = OUString("ooo.vba.excel.XStyles" );
172 return aServiceNames;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */