LibreOffice Module sc (master) 1
vbastyles.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#include "vbastyles.hxx"
20#include "vbastyle.hxx"
21#include <basic/sberrors.hxx>
23#include <ooo/vba/excel/XRange.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25#include <com/sun/star/lang/XMultiServiceFactory.hpp>
26#include <utility>
27
28using namespace ::ooo::vba;
29using namespace ::com::sun::star;
30
31static css::uno::Any
32lcl_createAPIStyleToVBAObject( const css::uno::Any& aObject, const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< frame::XModel >& xModel )
33{
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 );
37}
38
39ScVbaStyles::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,
43 xContext,
44 uno::Reference< container::XIndexAccess >( ScVbaStyle::getStylesNameContainer( xModel ), uno::UNO_QUERY_THROW ) ),
46{
47 try
48 {
49 mxMSF.set( mxModel, uno::UNO_QUERY_THROW );
50 mxNameContainerCellStyles.set( m_xNameAccess, uno::UNO_QUERY_THROW );
51 }
52 catch (uno::Exception& )
53 {
54 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
55 }
56}
57
58uno::Sequence< OUString >
60{
61 return mxNameContainerCellStyles->getElementNames();
62}
63
66{
68}
69
70uno::Type SAL_CALL
72{
74}
75
76namespace {
77
78class EnumWrapper : public EnumerationHelper_BASE
79{
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;
84
85 sal_Int32 nIndex;
86public:
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
89 {
90 return ( nIndex < m_xIndexAccess->getCount() );
91 }
92 virtual uno::Any SAL_CALL nextElement( ) override
93 {
94 try
95 {
96 if ( nIndex < m_xIndexAccess->getCount() )
97 return lcl_createAPIStyleToVBAObject( m_xIndexAccess->getByIndex( nIndex++ ), m_xParent, m_xContext, m_xModel );
98 }
99 catch (const container::NoSuchElementException&)
100 {
101 throw;
102 }
103 catch (const lang::WrappedTargetException&)
104 {
105 throw;
106 }
107 catch (const uno::RuntimeException&)
108 {
109 throw;
110 }
111 catch (const uno::Exception& e)
112 {
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);
117 }
118 throw container::NoSuchElementException();
119 }
120};
121
122}
123
124uno::Reference< container::XEnumeration > SAL_CALL
126{
127 return new EnumWrapper( m_xIndexAccess, mxParent, mxContext, mxModel );
128}
129
130uno::Reference< excel::XStyle > SAL_CALL
131ScVbaStyles::Add( const OUString& _sName, const uno::Any& _aBasedOn )
132{
133 uno::Reference< excel::XStyle > aRet;
134 try
135 {
136 OUString sParentCellStyleName("Default");
137 if ( _aBasedOn.hasValue() )
138 {
139 uno::Reference< excel::XRange > oRange;
140 if ( _aBasedOn >>= oRange)
141 {
142 uno::Reference< excel::XStyle > oStyle( oRange->getStyle(), uno::UNO_QUERY_THROW );
143 sParentCellStyleName = oStyle->getName();
144 }
145 else
146 {
147 DebugHelper::basicexception(ERRCODE_BASIC_BAD_ARGUMENT, {});
148 }
149 }
150
151 uno::Reference< style::XStyle > xStyle( mxMSF->createInstance("com.sun.star.style.CellStyle"), uno::UNO_QUERY_THROW );
152
153 if (!mxNameContainerCellStyles->hasByName(_sName))
154 {
155 mxNameContainerCellStyles->insertByName(_sName, uno::Any( xStyle) );
156 }
157 if (sParentCellStyleName != "Default")
158 {
159 xStyle->setParentStyle( sParentCellStyleName );
160 }
161 aRet.set( Item( uno::Any( _sName ), uno::Any() ), uno::UNO_QUERY_THROW );
162 }
163 catch (const uno::Exception&)
164 {
165 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
166 }
167 return aRet;
168}
169
170void
171ScVbaStyles::Delete(const OUString& _sStyleName)
172{
173 try
174 {
175 if (mxNameContainerCellStyles->hasByName( _sStyleName ) )
176 mxNameContainerCellStyles->removeByName( _sStyleName );
177 }
178 catch (const uno::Exception&)
179 {
180 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
181 }
182}
183
184OUString
186{
187 return "ScVbaStyles";
188}
189
190uno::Sequence< OUString >
192{
193 static uno::Sequence< OUString > const aServiceNames
194 {
195 "ooo.vba.excel.XStyles"
196 };
197 return aServiceNames;
198}
199
200/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XComponentContext > m_xContext
css::uno::Reference< css::frame::XModel2 > mxModel
css::uno::Reference< css::uno::XComponentContext > mxContext
css::uno::WeakReference< ov::XHelperInterface > mxParent
css::uno::Reference< css::container::XNameAccess > m_xNameAccess
virtual css::uno::Any SAL_CALL Item(const css::uno::Any &Index1, const css::uno::Any &) override
css::uno::Reference< css::container::XIndexAccess > m_xIndexAccess
virtual css::uno::Sequence< OUString > getServiceNames() override
Definition: vbastyles.cxx:191
css::uno::Sequence< OUString > getStyleNames()
Definition: vbastyles.cxx:59
css::uno::Reference< css::lang::XMultiServiceFactory > mxMSF
Definition: vbastyles.hxx:30
virtual css::uno::Reference< ov::excel::XStyle > SAL_CALL Add(const OUString &Name, const css::uno::Any &BasedOn) override
Definition: vbastyles.cxx:131
virtual css::uno::Reference< css::container::XEnumeration > SAL_CALL createEnumeration() override
Definition: vbastyles.cxx:125
css::uno::Reference< css::frame::XModel > mxModel
Definition: vbastyles.hxx:29
void Delete(const OUString &_sStyleName)
Definition: vbastyles.cxx:171
css::uno::Reference< css::container::XNameContainer > mxNameContainerCellStyles
Definition: vbastyles.hxx:31
virtual OUString getServiceImplName() override
Definition: vbastyles.cxx:185
ScVbaStyles(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const css::uno::Reference< css::frame::XModel > &xModel)
Definition: vbastyles.cxx:39
virtual css::uno::Any createCollectionObject(const css::uno::Any &) override
Definition: vbastyles.cxx:65
virtual css::uno::Type SAL_CALL getElementType() override
Definition: vbastyles.cxx:71
css::uno::Type const & get()
Reference< frame::XModel > m_xModel
Sequence< OUString > aServiceNames
sal_Int32 nIndex
uno_Any a
Any SAL_CALL getCaughtException()
Reference
#define ERRCODE_BASIC_METHOD_FAILED
#define ERRCODE_BASIC_BAD_ARGUMENT
bool hasValue()
Reference< XModel > xModel
unsigned char sal_Bool
::cppu::WeakImplHelper< css::container::XEnumeration > EnumerationHelper_BASE
static css::uno::Any lcl_createAPIStyleToVBAObject(const css::uno::Any &aObject, const uno::Reference< XHelperInterface > &xParent, const uno::Reference< uno::XComponentContext > &xContext, const uno::Reference< frame::XModel > &xModel)
Definition: vbastyles.cxx:32