1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #include "vbaformatcondition.hxx"
24 #include "vbaformatconditions.hxx"
25 #include <ooo/vba/excel/XlFormatConditionType.hpp>
26 
27 using namespace ::ooo::vba;
28 using namespace ::com::sun::star;
29 
30 ScVbaFormatConditions*
lcl_getScVbaFormatConditionsPtr(const uno::Reference<excel::XFormatConditions> & xFormatConditions)31 lcl_getScVbaFormatConditionsPtr( const uno::Reference< excel::XFormatConditions >& xFormatConditions ) throw ( script::BasicErrorException )
32 {
33 	ScVbaFormatConditions* pFormatConditions = static_cast< ScVbaFormatConditions* >( xFormatConditions.get() );
34 	if ( !pFormatConditions )
35 		DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
36 	return pFormatConditions;
37 }
ScVbaFormatCondition(const uno::Reference<XHelperInterface> & xParent,const uno::Reference<uno::XComponentContext> & xContext,const uno::Reference<sheet::XSheetConditionalEntry> & _xSheetConditionalEntry,const uno::Reference<excel::XStyle> & _xStyle,const uno::Reference<excel::XFormatConditions> & _xFormatConditions,const uno::Reference<css::beans::XPropertySet> & _xPropertySet)38 ScVbaFormatCondition::ScVbaFormatCondition(  const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< sheet::XSheetConditionalEntry >& _xSheetConditionalEntry, const uno::Reference< excel::XStyle >& _xStyle, const uno::Reference< excel::XFormatConditions >& _xFormatConditions, const uno::Reference< css::beans::XPropertySet >& _xPropertySet ) throw ( css::uno::RuntimeException ) : ScVbaFormatCondition_BASE( xParent, xContext, uno::Reference< sheet::XSheetCondition >( _xSheetConditionalEntry, css::uno::UNO_QUERY_THROW ) ), moFormatConditions( _xFormatConditions ), mxStyle( _xStyle ), mxParentRangePropertySet( _xPropertySet )
39 {
40         mxSheetConditionalEntries = lcl_getScVbaFormatConditionsPtr( moFormatConditions )->getSheetConditionalEntries();
41 
42         mxSheetConditionalEntry = _xSheetConditionalEntry;
43         msStyleName = mxStyle->getName();
44 }
45 
46 
47 void SAL_CALL
Delete()48 ScVbaFormatCondition::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
49 {
50 	ScVbaFormatConditions* pFormatConditions = lcl_getScVbaFormatConditionsPtr( moFormatConditions );
51 	pFormatConditions->removeFormatCondition(msStyleName, sal_True);
52         notifyRange();
53 }
54 
55 void SAL_CALL
Modify(::sal_Int32 _nType,const uno::Any & _aOperator,const uno::Any & _aFormula1,const uno::Any & _aFormula2)56 ScVbaFormatCondition::Modify( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 ) throw (script::BasicErrorException, uno::RuntimeException)
57 {
58 	try
59 	{
60 		ScVbaFormatConditions* pFormatConditions = lcl_getScVbaFormatConditionsPtr( moFormatConditions );
61 		pFormatConditions->removeFormatCondition(msStyleName, sal_False);
62 		pFormatConditions->Add(_nType, _aOperator, _aFormula1, _aFormula2, mxStyle);
63 	}
64 	catch (uno::Exception& )
65 	{
66 		DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
67 	}
68 }
69 
70 uno::Reference< excel::XInterior > SAL_CALL
Interior()71 ScVbaFormatCondition::Interior(  ) throw (script::BasicErrorException, uno::RuntimeException)
72 {
73 	return mxStyle->Interior();
74 }
75 
76 uno::Reference< excel::XFont > SAL_CALL
Font()77 ScVbaFormatCondition::Font(  ) throw (script::BasicErrorException, uno::RuntimeException)
78 {
79 	return mxStyle->Font();
80 }
81 uno::Any SAL_CALL
Borders(const uno::Any & Index)82 ScVbaFormatCondition::Borders( const uno::Any& Index ) throw (script::BasicErrorException, uno::RuntimeException)
83 { return mxStyle->Borders( Index );
84 }
85 
86 sheet::ConditionOperator
retrieveAPIType(sal_Int32 _nVBAType,const uno::Reference<sheet::XSheetCondition> & _xSheetCondition)87 ScVbaFormatCondition::retrieveAPIType(sal_Int32 _nVBAType, const uno::Reference< sheet::XSheetCondition >& _xSheetCondition ) throw ( script::BasicErrorException )
88 {
89 	sheet::ConditionOperator aAPIType = sheet::ConditionOperator_NONE;
90 	switch (_nVBAType)
91 	{
92 		case excel::XlFormatConditionType::xlExpression:
93 			aAPIType = sheet::ConditionOperator_FORMULA;
94 			break;
95 		case excel::XlFormatConditionType::xlCellValue:
96 			if ( _xSheetCondition.is() && (_xSheetCondition->getOperator() == sheet::ConditionOperator_FORMULA ) )
97 				aAPIType = sheet::ConditionOperator_NONE;
98 			break;
99 		default:
100 			DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
101 	}
102 	return aAPIType;
103 }
104 
105 void
setFormula1(const uno::Any & _aFormula1)106 ScVbaFormatCondition::setFormula1( const uno::Any& _aFormula1) throw ( script::BasicErrorException )
107 {
108 	// getA1Formula *SHOULD* detect whether the formula is r1c1 or A1 syntax
109 	// and if R1C1 convert to A1
110 	ScVbaFormatCondition_BASE::setFormula1( uno::makeAny( lcl_getScVbaFormatConditionsPtr( moFormatConditions )->getA1Formula(_aFormula1) ) );
111 }
112 
113 void
setFormula2(const uno::Any & _aFormula2)114 ScVbaFormatCondition::setFormula2( const uno::Any& _aFormula2) throw ( script::BasicErrorException )
115 {
116 	ScVbaFormatCondition_BASE::setFormula1( uno::makeAny( lcl_getScVbaFormatConditionsPtr( moFormatConditions )->getA1Formula(_aFormula2)) );
117 }
118 
119 ::sal_Int32 SAL_CALL
Type()120 ScVbaFormatCondition::Type(  ) throw ( script::BasicErrorException, uno::RuntimeException )
121 {
122 	sal_Int32 nReturnType = 0;
123 	if ( mxSheetCondition->getOperator() == sheet::ConditionOperator_FORMULA)
124 		nReturnType = excel::XlFormatConditionType::xlExpression;
125 	else
126 		nReturnType = excel::XlFormatConditionType::xlCellValue;
127 	return nReturnType;
128 }
129 
130 
131 ::sal_Int32
Operator(sal_Bool bVal)132 ScVbaFormatCondition::Operator( sal_Bool bVal ) throw (script::BasicErrorException )
133 {
134 	return ScVbaFormatCondition_BASE::Operator( bVal );
135 }
136 ::sal_Int32 SAL_CALL
Operator()137 ScVbaFormatCondition::Operator(  ) throw (script::BasicErrorException, uno::RuntimeException)
138 {
139 	return ScVbaFormatCondition_BASE::Operator( sal_True );
140 }
141 
142 void
notifyRange()143 ScVbaFormatCondition::notifyRange() throw ( script::BasicErrorException )
144 {
145 	try
146 	{
147 		mxParentRangePropertySet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ConditionalFormat") ), uno::makeAny( mxSheetConditionalEntries) );
148     }
149 	catch (uno::Exception& )
150 	{
151         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
152     }
153 }
154 
155 rtl::OUString&
getServiceImplName()156 ScVbaFormatCondition::getServiceImplName()
157 {
158 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaFormatCondition") );
159 	return sImplName;
160 }
161 
162 uno::Sequence< rtl::OUString >
getServiceNames()163 ScVbaFormatCondition::getServiceNames()
164 {
165 	static uno::Sequence< rtl::OUString > aServiceNames;
166 	if ( aServiceNames.getLength() == 0 )
167 	{
168 		aServiceNames.realloc( 1 );
169 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.FormatCondition" ) );
170 	}
171 	return aServiceNames;
172 }
173