nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / ui / vba / vbaformatconditions.cxx
bloba4970f58aac725bf445a5050930ee781cb0a9609
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <ooo/vba/excel/XRange.hpp>
21 #include <com/sun/star/sheet/XSheetConditionalEntry.hpp>
22 #include <basic/sberrors.hxx>
23 #include <comphelper/sequence.hxx>
24 #include <cppuhelper/exc_hlp.hxx>
25 #include <vector>
26 #include <unonames.hxx>
27 #include "vbaformatconditions.hxx"
28 #include "vbaformatcondition.hxx"
29 #include "vbastyles.hxx"
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
34 void SAL_CALL
35 ScVbaFormatConditions::Delete( )
37 try
39 ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
40 if ( !pStyles )
41 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {} );
42 sal_Int32 nCount = mxSheetConditionalEntries->getCount();
43 for (sal_Int32 i = nCount - 1; i >= 0; i--)
45 uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
46 pStyles->Delete(xSheetConditionalEntry->getStyleName());
47 mxSheetConditionalEntries->removeByIndex(i);
49 notifyRange();
51 catch (uno::Exception& )
53 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
57 uno::Type SAL_CALL
58 ScVbaFormatConditions::getElementType()
60 return cppu::UnoType<excel::XFormatCondition>::get();
63 static uno::Any xSheetConditionToFormatCondition( const uno::Reference< XHelperInterface >& xRangeParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xFormatConditions, const uno::Reference< beans::XPropertySet >& xRangeProps, const uno::Any& aObject )
65 uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry;
66 aObject >>= xSheetConditionalEntry;
68 uno::Reference< excel::XStyle > xStyle( xStyles->Item( uno::makeAny( xSheetConditionalEntry->getStyleName() ), uno::Any() ), uno::UNO_QUERY_THROW );
69 uno::Reference< excel::XFormatCondition > xCondition = new ScVbaFormatCondition( xRangeParent, xContext, xSheetConditionalEntry, xStyle, xFormatConditions, xRangeProps );
70 return uno::makeAny( xCondition );
73 uno::Any
74 ScVbaFormatConditions::createCollectionObject(const uno::Any& aObject )
76 return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, mxStyles, this, mxParentRangePropertySet, aObject );
79 namespace {
81 class EnumWrapper : public EnumerationHelper_BASE
83 uno::Reference<container::XIndexAccess > m_xIndexAccess;
84 uno::Reference<excel::XRange > m_xParentRange;
85 uno::Reference<uno::XComponentContext > m_xContext;
86 uno::Reference<excel::XStyles > m_xStyles;
87 uno::Reference<excel::XFormatConditions > m_xParentCollection;
88 uno::Reference<beans::XPropertySet > m_xProps;
90 sal_Int32 nIndex;
91 public:
92 EnumWrapper( const uno::Reference< container::XIndexAccess >& xIndexAccess, const uno::Reference<excel::XRange >& xRange, const uno::Reference<uno::XComponentContext >& xContext, const uno::Reference<excel::XStyles >& xStyles, const uno::Reference< excel::XFormatConditions >& xCollection, const uno::Reference<beans::XPropertySet >& xProps ) : m_xIndexAccess( xIndexAccess ), m_xParentRange( xRange ), m_xContext( xContext ), m_xStyles( xStyles ), m_xParentCollection( xCollection ), m_xProps( xProps ), nIndex( 0 ) {}
93 virtual sal_Bool SAL_CALL hasMoreElements( ) override
95 return ( nIndex < m_xIndexAccess->getCount() );
98 virtual uno::Any SAL_CALL nextElement( ) override
102 if ( nIndex < m_xIndexAccess->getCount() )
103 return xSheetConditionToFormatCondition( uno::Reference< XHelperInterface >( m_xParentRange, uno::UNO_QUERY_THROW ), m_xContext, m_xStyles, m_xParentCollection, m_xProps, m_xIndexAccess->getByIndex( nIndex++ ) );
105 catch (const container::NoSuchElementException&)
107 throw;
109 catch (const lang::WrappedTargetException&)
111 throw;
113 catch (const uno::RuntimeException&)
115 throw;
117 catch (const uno::Exception& e)
119 css::uno::Any a(cppu::getCaughtException());
120 throw css::lang::WrappedTargetException(
121 "wrapped Exception " + e.Message,
122 css::uno::Reference<css::uno::XInterface>(), a);
124 throw container::NoSuchElementException();
130 uno::Reference< excel::XFormatCondition > SAL_CALL
131 ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 )
133 return Add( _nType, _aOperator, _aFormula1, _aFormula2, uno::Reference< excel::XStyle >() );
136 uno::Reference< excel::XFormatCondition >
137 ScVbaFormatConditions::Add( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2, const css::uno::Reference< excel::XStyle >& _xStyle )
139 // #TODO
140 // #FIXME
141 // This method will NOT handle r1c1 formulas [*]and only assumes that
142 // the formulas are _xlA1 based ( need to hook into calc work this should
143 // address this )
144 // [*] reason: getA1Formula method below is just a hook and just
145 // returns what it gets ( e.g. doesn't convert anything )
146 uno::Reference< excel::XStyle > xStyle( _xStyle );
147 uno::Reference< excel::XFormatCondition > xFormatCondition;
150 OUString sStyleName;
151 if ( !xStyle.is() )
153 sStyleName = getStyleName();
154 xStyle = mxStyles->Add(sStyleName, uno::Any() );
156 else
158 sStyleName = xStyle->getName();
161 std::vector< beans::PropertyValue > aPropertyValueVector;
162 sheet::ConditionOperator aType = ScVbaFormatCondition::retrieveAPIType(_nType, uno::Reference< sheet::XSheetCondition >() );
163 uno::Any aValue;
165 if ( aType == sheet::ConditionOperator_FORMULA)
166 aValue <<= sheet::ConditionOperator_FORMULA;
167 else
168 aValue <<= ScVbaFormatCondition::retrieveAPIOperator(_aOperator);
170 beans::PropertyValue aProperty( "Operator", 0, aValue, beans::PropertyState_DIRECT_VALUE );
171 aPropertyValueVector.push_back( aProperty );
173 if ( _aFormula1.hasValue() )
175 beans::PropertyValue aProp( "Formula1", 0, uno::makeAny( getA1Formula( _aFormula1 ) ), beans::PropertyState_DIRECT_VALUE );
176 aPropertyValueVector.push_back( aProp );
178 if ( _aFormula2.hasValue() )
180 beans::PropertyValue aProp( "Formula2", 0, uno::makeAny( getA1Formula( _aFormula2 ) ), beans::PropertyState_DIRECT_VALUE );
181 aPropertyValueVector.push_back( aProp );
183 aProperty.Name = "StyleName";
184 aProperty.Value <<= sStyleName;
186 mxSheetConditionalEntries->addNew(comphelper::containerToSequence(aPropertyValueVector));
187 for (sal_Int32 i = mxSheetConditionalEntries->getCount()-1; i >= 0; i--)
189 uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
190 if (xSheetConditionalEntry->getStyleName() == sStyleName)
192 xFormatCondition = new ScVbaFormatCondition(uno::Reference< XHelperInterface >( mxRangeParent, uno::UNO_QUERY_THROW ), mxContext, xSheetConditionalEntry, xStyle, this, mxParentRangePropertySet);
193 notifyRange();
194 return xFormatCondition;
198 catch (uno::Exception& )
201 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {} );
202 return xFormatCondition;
205 uno::Reference< container::XEnumeration > SAL_CALL
206 ScVbaFormatConditions::createEnumeration()
208 return new EnumWrapper( m_xIndexAccess, mxRangeParent, mxContext, mxStyles, this, mxParentRangePropertySet );
211 void
212 ScVbaFormatConditions::notifyRange()
216 mxParentRangePropertySet->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny( mxSheetConditionalEntries ));
218 catch (uno::Exception& )
220 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
224 OUString
225 ScVbaFormatConditions::getA1Formula(const css::uno::Any& _aFormula)
227 // #TODO, #FIXME hook-in proper formula conversion detection & logic
228 OUString sFormula;
229 if ( !( _aFormula >>= sFormula ) )
230 DebugHelper::basicexception(ERRCODE_BASIC_BAD_PARAMETER, {} );
231 return sFormula;
234 OUString
235 ScVbaFormatConditions::getStyleName()
237 ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
238 if ( !pStyles )
239 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {} );
240 uno::Sequence< OUString > sCellStyleNames = pStyles->getStyleNames();
241 return ContainerUtilities::getUniqueName(sCellStyleNames, "Excel_CondFormat", "_");
244 void
245 ScVbaFormatConditions::removeFormatCondition( const OUString& _sStyleName, bool _bRemoveStyle)
249 sal_Int32 nElems = mxSheetConditionalEntries->getCount();
250 for (sal_Int32 i = 0; i < nElems; i++)
252 uno::Reference< sheet::XSheetConditionalEntry > xSheetConditionalEntry( mxSheetConditionalEntries->getByIndex(i), uno::UNO_QUERY_THROW );
253 if (_sStyleName == xSheetConditionalEntry->getStyleName())
255 mxSheetConditionalEntries->removeByIndex(i);
256 if (_bRemoveStyle)
258 ScVbaStyles* pStyles = static_cast< ScVbaStyles* >( mxStyles.get() );
259 if ( !pStyles )
260 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
261 pStyles->Delete( _sStyleName );
263 return;
267 catch (const uno::Exception&)
269 DebugHelper::basicexception(ERRCODE_BASIC_METHOD_FAILED, {});
273 OUString
274 ScVbaFormatConditions::getServiceImplName()
276 return "ScVbaFormatConditions";
279 uno::Sequence< OUString >
280 ScVbaFormatConditions::getServiceNames()
282 static uno::Sequence< OUString > const aServiceNames
284 "ooo.vba.excel.FormatConditions"
286 return aServiceNames;
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */