1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 #include "vbaformatcondition.hxx"
28 #include "vbaformatconditions.hxx"
29 #include <ooo/vba/excel/XlFormatConditionType.hpp>
30 
31 using namespace ::ooo::vba;
32 using namespace ::com::sun::star;
33 
34 ScVbaFormatConditions*
35 lcl_getScVbaFormatConditionsPtr( const uno::Reference< excel::XFormatConditions >& xFormatConditions ) throw ( script::BasicErrorException )
36 {
37 	ScVbaFormatConditions* pFormatConditions = static_cast< ScVbaFormatConditions* >( xFormatConditions.get() );
38 	if ( !pFormatConditions )
39 		DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
40 	return pFormatConditions;
41 }
42 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 )
43 {
44         mxSheetConditionalEntries = lcl_getScVbaFormatConditionsPtr( moFormatConditions )->getSheetConditionalEntries();
45 
46         mxSheetConditionalEntry = _xSheetConditionalEntry;
47         msStyleName = mxStyle->getName();
48 }
49 
50 
51 void SAL_CALL
52 ScVbaFormatCondition::Delete(  ) throw (script::BasicErrorException, uno::RuntimeException)
53 {
54 	ScVbaFormatConditions* pFormatConditions = lcl_getScVbaFormatConditionsPtr( moFormatConditions );
55 	pFormatConditions->removeFormatCondition(msStyleName, sal_True);
56         notifyRange();
57 }
58 
59 void SAL_CALL
60 ScVbaFormatCondition::Modify( ::sal_Int32 _nType, const uno::Any& _aOperator, const uno::Any& _aFormula1, const uno::Any& _aFormula2 ) throw (script::BasicErrorException, uno::RuntimeException)
61 {
62 	try
63 	{
64 		ScVbaFormatConditions* pFormatConditions = lcl_getScVbaFormatConditionsPtr( moFormatConditions );
65 		pFormatConditions->removeFormatCondition(msStyleName, sal_False);
66 		pFormatConditions->Add(_nType, _aOperator, _aFormula1, _aFormula2, mxStyle);
67 	}
68 	catch (uno::Exception& )
69 	{
70 		DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
71 	}
72 }
73 
74 uno::Reference< excel::XInterior > SAL_CALL
75 ScVbaFormatCondition::Interior(  ) throw (script::BasicErrorException, uno::RuntimeException)
76 {
77 	return mxStyle->Interior();
78 }
79 
80 uno::Reference< excel::XFont > SAL_CALL
81 ScVbaFormatCondition::Font(  ) throw (script::BasicErrorException, uno::RuntimeException)
82 {
83 	return mxStyle->Font();
84 }
85 uno::Any SAL_CALL
86 ScVbaFormatCondition::Borders( const uno::Any& Index ) throw (script::BasicErrorException, uno::RuntimeException)
87 { return mxStyle->Borders( Index );
88 }
89 
90 sheet::ConditionOperator
91 ScVbaFormatCondition::retrieveAPIType(sal_Int32 _nVBAType, const uno::Reference< sheet::XSheetCondition >& _xSheetCondition ) throw ( script::BasicErrorException )
92 {
93 	sheet::ConditionOperator aAPIType = sheet::ConditionOperator_NONE;
94 	switch (_nVBAType)
95 	{
96 		case excel::XlFormatConditionType::xlExpression:
97 			aAPIType = sheet::ConditionOperator_FORMULA;
98 			break;
99 		case excel::XlFormatConditionType::xlCellValue:
100 			if ( _xSheetCondition.is() && (_xSheetCondition->getOperator() == sheet::ConditionOperator_FORMULA ) )
101 				aAPIType = sheet::ConditionOperator_NONE;
102 			break;
103 		default:
104 			DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
105 	}
106 	return aAPIType;
107 }
108 
109 void
110 ScVbaFormatCondition::setFormula1( const uno::Any& _aFormula1) throw ( script::BasicErrorException )
111 {
112 	// getA1Formula *SHOULD* detect whether the formula is r1c1 or A1 syntax
113 	// and if R1C1 convert to A1
114 	ScVbaFormatCondition_BASE::setFormula1( uno::makeAny( lcl_getScVbaFormatConditionsPtr( moFormatConditions )->getA1Formula(_aFormula1) ) );
115 }
116 
117 void
118 ScVbaFormatCondition::setFormula2( const uno::Any& _aFormula2) throw ( script::BasicErrorException )
119 {
120 	ScVbaFormatCondition_BASE::setFormula1( uno::makeAny( lcl_getScVbaFormatConditionsPtr( moFormatConditions )->getA1Formula(_aFormula2)) );
121 }
122 
123 ::sal_Int32 SAL_CALL
124 ScVbaFormatCondition::Type(  ) throw ( script::BasicErrorException, uno::RuntimeException )
125 {
126 	sal_Int32 nReturnType = 0;
127 	if ( mxSheetCondition->getOperator() == sheet::ConditionOperator_FORMULA)
128 		nReturnType = excel::XlFormatConditionType::xlExpression;
129 	else
130 		nReturnType = excel::XlFormatConditionType::xlCellValue;
131 	return nReturnType;
132 }
133 
134 
135 ::sal_Int32
136 ScVbaFormatCondition::Operator( sal_Bool bVal ) throw (script::BasicErrorException )
137 {
138 	return ScVbaFormatCondition_BASE::Operator( bVal );
139 }
140 ::sal_Int32 SAL_CALL
141 ScVbaFormatCondition::Operator(  ) throw (script::BasicErrorException, uno::RuntimeException)
142 {
143 	return ScVbaFormatCondition_BASE::Operator( sal_True );
144 }
145 
146 void
147 ScVbaFormatCondition::notifyRange() throw ( script::BasicErrorException )
148 {
149 	try
150 	{
151 		mxParentRangePropertySet->setPropertyValue( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ConditionalFormat") ), uno::makeAny( mxSheetConditionalEntries) );
152     }
153 	catch (uno::Exception& )
154 	{
155         DebugHelper::exception(SbERR_METHOD_FAILED, rtl::OUString() );
156     }
157 }
158 
159 rtl::OUString&
160 ScVbaFormatCondition::getServiceImplName()
161 {
162 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaFormatCondition") );
163 	return sImplName;
164 }
165 
166 uno::Sequence< rtl::OUString >
167 ScVbaFormatCondition::getServiceNames()
168 {
169 	static uno::Sequence< rtl::OUString > aServiceNames;
170 	if ( aServiceNames.getLength() == 0 )
171 	{
172 		aServiceNames.realloc( 1 );
173 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.FormatCondition" ) );
174 	}
175 	return aServiceNames;
176 }
177