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 <basic/sberrors.hxx>
22 #include <cppuhelper/exc_hlp.hxx>
23 #include <ooo/vba/excel/XRange.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
28 using namespace ::ooo::vba
;
29 using namespace ::com::sun::star
;
32 lcl_createAPIStyleToVBAObject( const css::uno::Any
& aObject
, const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< frame::XModel
>& xModel
)
34 uno::Reference
< beans::XPropertySet
> xStyleProps( aObject
, uno::UNO_QUERY_THROW
);
35 uno::Reference
< excel::XStyle
> xStyle( new ScVbaStyle( xParent
, xContext
, xStyleProps
, xModel
) );
36 return uno::Any( xStyle
);
39 ScVbaStyles::ScVbaStyles( const uno::Reference
< XHelperInterface
>& xParent
,
40 const uno::Reference
< css::uno::XComponentContext
> & xContext
,
41 const uno::Reference
< frame::XModel
>& xModel
)
42 : ScVbaStyles_BASE( xParent
,
44 uno::Reference
< container::XIndexAccess
>( ScVbaStyle::getStylesNameContainer( xModel
), uno::UNO_QUERY_THROW
) ),
49 mxMSF
.set( mxModel
, uno::UNO_QUERY_THROW
);
50 mxNameContainerCellStyles
.set( m_xNameAccess
, uno::UNO_QUERY_THROW
);
52 catch (uno::Exception
& )
54 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
58 uno::Sequence
< OUString
>
59 ScVbaStyles::getStyleNames()
61 return mxNameContainerCellStyles
->getElementNames();
65 ScVbaStyles::createCollectionObject(const uno::Any
& aObject
)
67 return lcl_createAPIStyleToVBAObject( aObject
, mxParent
, mxContext
, mxModel
);
71 ScVbaStyles::getElementType()
73 return cppu::UnoType
<excel::XStyle
>::get();
78 class EnumWrapper
: public EnumerationHelper_BASE
80 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
81 uno::Reference
<XHelperInterface
> m_xParent
;
82 uno::Reference
<uno::XComponentContext
> m_xContext
;
83 uno::Reference
<frame::XModel
> m_xModel
;
87 EnumWrapper( uno::Reference
< container::XIndexAccess
> xIndexAccess
, uno::Reference
<XHelperInterface
> xParent
, uno::Reference
<uno::XComponentContext
> xContext
, uno::Reference
<frame::XModel
> xModel
) : m_xIndexAccess(std::move( xIndexAccess
)), m_xParent(std::move( xParent
)), m_xContext(std::move( xContext
)), m_xModel(std::move( xModel
)), nIndex( 0 ) {}
88 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
90 return ( nIndex
< m_xIndexAccess
->getCount() );
92 virtual uno::Any SAL_CALL
nextElement( ) override
96 if ( nIndex
< m_xIndexAccess
->getCount() )
97 return lcl_createAPIStyleToVBAObject( m_xIndexAccess
->getByIndex( nIndex
++ ), m_xParent
, m_xContext
, m_xModel
);
99 catch (const container::NoSuchElementException
&)
103 catch (const lang::WrappedTargetException
&)
107 catch (const uno::RuntimeException
&)
111 catch (const uno::Exception
& e
)
113 css::uno::Any
a(cppu::getCaughtException());
114 throw css::lang::WrappedTargetException(
115 "wrapped Exception " + e
.Message
,
116 css::uno::Reference
<css::uno::XInterface
>(), a
);
118 throw container::NoSuchElementException();
124 uno::Reference
< container::XEnumeration
> SAL_CALL
125 ScVbaStyles::createEnumeration()
127 return new EnumWrapper( m_xIndexAccess
, mxParent
, mxContext
, mxModel
);
130 uno::Reference
< excel::XStyle
> SAL_CALL
131 ScVbaStyles::Add( const OUString
& _sName
, const uno::Any
& _aBasedOn
)
133 uno::Reference
< excel::XStyle
> aRet
;
136 OUString
sParentCellStyleName(u
"Default"_ustr
);
137 if ( _aBasedOn
.hasValue() )
139 uno::Reference
< excel::XRange
> oRange
;
140 if ( _aBasedOn
>>= oRange
)
142 uno::Reference
< excel::XStyle
> oStyle( oRange
->getStyle(), uno::UNO_QUERY_THROW
);
143 sParentCellStyleName
= oStyle
->getName();
147 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT
, {});
151 uno::Reference
< style::XStyle
> xStyle( mxMSF
->createInstance(u
"com.sun.star.style.CellStyle"_ustr
), uno::UNO_QUERY_THROW
);
153 if (!mxNameContainerCellStyles
->hasByName(_sName
))
155 mxNameContainerCellStyles
->insertByName(_sName
, uno::Any( xStyle
) );
157 if (sParentCellStyleName
!= "Default")
159 xStyle
->setParentStyle( sParentCellStyleName
);
161 aRet
.set( Item( uno::Any( _sName
), uno::Any() ), uno::UNO_QUERY_THROW
);
163 catch (const uno::Exception
&)
165 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
171 ScVbaStyles::Delete(const OUString
& _sStyleName
)
175 if (mxNameContainerCellStyles
->hasByName( _sStyleName
) )
176 mxNameContainerCellStyles
->removeByName( _sStyleName
);
178 catch (const uno::Exception
&)
180 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
185 ScVbaStyles::getServiceImplName()
187 return u
"ScVbaStyles"_ustr
;
190 uno::Sequence
< OUString
>
191 ScVbaStyles::getServiceNames()
193 static uno::Sequence
< OUString
> const aServiceNames
195 u
"ooo.vba.excel.XStyles"_ustr
197 return aServiceNames
;
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */