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