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>
27 using namespace ::ooo::vba
;
28 using namespace ::com::sun::star
;
31 lcl_createAPIStyleToVBAObject( const css::uno::Any
& aObject
, const uno::Reference
< XHelperInterface
>& xParent
, const uno::Reference
< uno::XComponentContext
>& xContext
, const uno::Reference
< frame::XModel
>& xModel
)
33 uno::Reference
< beans::XPropertySet
> xStyleProps( aObject
, uno::UNO_QUERY_THROW
);
34 uno::Reference
< excel::XStyle
> xStyle( new ScVbaStyle( xParent
, xContext
, xStyleProps
, xModel
) );
35 return uno::makeAny( xStyle
);
38 ScVbaStyles::ScVbaStyles( const uno::Reference
< XHelperInterface
>& xParent
,
39 const uno::Reference
< css::uno::XComponentContext
> & xContext
,
40 const uno::Reference
< frame::XModel
>& xModel
)
41 : ScVbaStyles_BASE( xParent
,
43 uno::Reference
< container::XIndexAccess
>( ScVbaStyle::getStylesNameContainer( xModel
), uno::UNO_QUERY_THROW
) ),
48 mxMSF
.set( mxModel
, uno::UNO_QUERY_THROW
);
49 mxNameContainerCellStyles
.set( m_xNameAccess
, uno::UNO_QUERY_THROW
);
51 catch (uno::Exception
& )
53 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
57 uno::Sequence
< OUString
>
58 ScVbaStyles::getStyleNames()
60 return mxNameContainerCellStyles
->getElementNames();
64 ScVbaStyles::createCollectionObject(const uno::Any
& aObject
)
66 return lcl_createAPIStyleToVBAObject( aObject
, mxParent
, mxContext
, mxModel
);
70 ScVbaStyles::getElementType()
72 return cppu::UnoType
<excel::XStyle
>::get();
77 class EnumWrapper
: public EnumerationHelper_BASE
79 uno::Reference
<container::XIndexAccess
> m_xIndexAccess
;
80 uno::Reference
<XHelperInterface
> m_xParent
;
81 uno::Reference
<uno::XComponentContext
> m_xContext
;
82 uno::Reference
<frame::XModel
> m_xModel
;
86 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 ) {}
87 virtual sal_Bool SAL_CALL
hasMoreElements( ) override
89 return ( nIndex
< m_xIndexAccess
->getCount() );
91 virtual uno::Any SAL_CALL
nextElement( ) override
95 if ( nIndex
< m_xIndexAccess
->getCount() )
96 return lcl_createAPIStyleToVBAObject( m_xIndexAccess
->getByIndex( nIndex
++ ), m_xParent
, m_xContext
, m_xModel
);
98 catch (const container::NoSuchElementException
&)
102 catch (const lang::WrappedTargetException
&)
106 catch (const uno::RuntimeException
&)
110 catch (const uno::Exception
& e
)
112 css::uno::Any
a(cppu::getCaughtException());
113 throw css::lang::WrappedTargetException(
114 "wrapped Exception " + e
.Message
,
115 css::uno::Reference
<css::uno::XInterface
>(), a
);
117 throw container::NoSuchElementException();
123 uno::Reference
< container::XEnumeration
> SAL_CALL
124 ScVbaStyles::createEnumeration()
126 return new EnumWrapper( m_xIndexAccess
, mxParent
, mxContext
, mxModel
);
129 uno::Reference
< excel::XStyle
> SAL_CALL
130 ScVbaStyles::Add( const OUString
& _sName
, const uno::Any
& _aBasedOn
)
132 uno::Reference
< excel::XStyle
> aRet
;
135 OUString
sParentCellStyleName("Default");
136 if ( _aBasedOn
.hasValue() )
138 uno::Reference
< excel::XRange
> oRange
;
139 if ( _aBasedOn
>>= oRange
)
141 uno::Reference
< excel::XStyle
> oStyle( oRange
->getStyle(), uno::UNO_QUERY_THROW
);
142 sParentCellStyleName
= oStyle
->getName();
146 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT
, {});
150 uno::Reference
< style::XStyle
> xStyle( mxMSF
->createInstance("com.sun.star.style.CellStyle"), uno::UNO_QUERY_THROW
);
152 if (!mxNameContainerCellStyles
->hasByName(_sName
))
154 mxNameContainerCellStyles
->insertByName(_sName
, uno::makeAny( xStyle
) );
156 if (sParentCellStyleName
!= "Default")
158 xStyle
->setParentStyle( sParentCellStyleName
);
160 aRet
.set( Item( uno::makeAny( _sName
), uno::Any() ), uno::UNO_QUERY_THROW
);
162 catch (const uno::Exception
&)
164 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
170 ScVbaStyles::Delete(const OUString
& _sStyleName
)
174 if (mxNameContainerCellStyles
->hasByName( _sStyleName
) )
175 mxNameContainerCellStyles
->removeByName( _sStyleName
);
177 catch (const uno::Exception
&)
179 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED
, {});
184 ScVbaStyles::getServiceImplName()
186 return "ScVbaStyles";
189 uno::Sequence
< OUString
>
190 ScVbaStyles::getServiceNames()
192 static uno::Sequence
< OUString
> const aServiceNames
194 "ooo.vba.excel.XStyles"
196 return aServiceNames
;
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */