1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <cppuhelper/exc_hlp.hxx>
22 #include <ooo/vba/excel/XRange.hpp>
24 using namespace ::ooo::vba
;
25 using namespace ::com::sun::star
;
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
);
35 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
)
39 mxMSF
.set( mxModel
, uno::UNO_QUERY_THROW
);
40 mxNameContainerCellStyles
.set( m_xNameAccess
, uno::UNO_QUERY_THROW
);
42 catch (uno::Exception
& )
44 DebugHelper::basicexception(SbERR_METHOD_FAILED
, OUString());
48 uno::Sequence
< OUString
>
49 ScVbaStyles::getStyleNames() throw ( uno::RuntimeException
)
51 return mxNameContainerCellStyles
->getElementNames();
55 ScVbaStyles::createCollectionObject(const uno::Any
& aObject
)
57 return lcl_createAPIStyleToVBAObject( aObject
, mxParent
, mxContext
, mxModel
);
61 ScVbaStyles::getElementType() throw (uno::RuntimeException
)
63 return cppu::UnoType
<excel::XStyle
>::get();
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
;
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
, std::exception
) SAL_OVERRIDE
80 return ( nIndex
< m_xIndexAccess
->getCount() );
82 virtual uno::Any SAL_CALL
nextElement( ) throw (container::NoSuchElementException
, lang::WrappedTargetException
, uno::RuntimeException
, std::exception
) SAL_OVERRIDE
86 if ( nIndex
< m_xIndexAccess
->getCount() )
87 return lcl_createAPIStyleToVBAObject( m_xIndexAccess
->getByIndex( nIndex
++ ), m_xParent
, m_xContext
, m_xModel
);
89 catch (const container::NoSuchElementException
&)
93 catch (const lang::WrappedTargetException
&)
97 catch (const uno::RuntimeException
&)
101 catch (const uno::Exception
& e
)
103 css::uno::Any
a(cppu::getCaughtException());
104 throw css::lang::WrappedTargetException(
105 "wrapped Exception " + e
.Message
,
106 css::uno::Reference
<css::uno::XInterface
>(), a
);
108 throw container::NoSuchElementException();
114 uno::Reference
< container::XEnumeration
> SAL_CALL
115 ScVbaStyles::createEnumeration() throw (uno::RuntimeException
)
117 return new EnumWrapper( m_xIndexAccess
, mxParent
, mxContext
, mxModel
);
120 uno::Reference
< excel::XStyle
> SAL_CALL
121 ScVbaStyles::Add( const OUString
& _sName
, const uno::Any
& _aBasedOn
) throw (script::BasicErrorException
, uno::RuntimeException
, std::exception
)
123 uno::Reference
< excel::XStyle
> aRet
;
126 OUString
sParentCellStyleName("Default");
127 if ( _aBasedOn
.hasValue() )
129 uno::Reference
< excel::XRange
> oRange
;
130 if ( _aBasedOn
>>= oRange
)
132 uno::Reference
< excel::XStyle
> oStyle( oRange
->getStyle(), uno::UNO_QUERY_THROW
);
135 sParentCellStyleName
= oStyle
->getName();
139 DebugHelper::basicexception(SbERR_BAD_ARGUMENT
, OUString() );
144 DebugHelper::basicexception(SbERR_BAD_ARGUMENT
, OUString());
148 uno::Reference
< style::XStyle
> xStyle( mxMSF
->createInstance("com.sun.star.style.CellStyle"), uno::UNO_QUERY_THROW
);
150 if (!mxNameContainerCellStyles
->hasByName(_sName
))
152 mxNameContainerCellStyles
->insertByName(_sName
, uno::makeAny( xStyle
) );
154 if (sParentCellStyleName
!= "Default")
156 xStyle
->setParentStyle( sParentCellStyleName
);
158 aRet
.set( Item( uno::makeAny( _sName
), uno::Any() ), uno::UNO_QUERY_THROW
);
160 catch (const uno::Exception
&)
162 DebugHelper::basicexception(SbERR_METHOD_FAILED
, OUString());
168 ScVbaStyles::Delete(const OUString
& _sStyleName
) throw ( script::BasicErrorException
)
172 if (mxNameContainerCellStyles
->hasByName( _sStyleName
) )
173 mxNameContainerCellStyles
->removeByName( _sStyleName
);
175 catch (const uno::Exception
&)
177 DebugHelper::basicexception(SbERR_METHOD_FAILED
, OUString());
182 ScVbaStyles::getServiceImplName()
184 return OUString("ScVbaStyles");
187 uno::Sequence
< OUString
>
188 ScVbaStyles::getServiceNames()
190 static uno::Sequence
< OUString
> aServiceNames
;
191 if ( aServiceNames
.getLength() == 0 )
193 aServiceNames
.realloc( 1 );
194 aServiceNames
[ 0 ] = "ooo.vba.excel.XStyles";
196 return aServiceNames
;
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */