xref: /aoo41x/main/sc/source/ui/vba/vbavalidation.cxx (revision b3f79822)
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 "vbavalidation.hxx"
24 #include <com/sun/star/sheet/XSheetCondition.hpp>
25 #include <com/sun/star/sheet/ValidationType.hpp>
26 #include <com/sun/star/sheet/ValidationAlertStyle.hpp>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <ooo/vba/excel/XlDVType.hpp>
29 #include <ooo/vba/excel/XlFormatConditionOperator.hpp>
30 #include <ooo/vba/excel/XlDVAlertStyle.hpp>
31 
32 #include "unonames.hxx"
33 
34 using namespace ::ooo::vba;
35 using namespace ::com::sun::star;
36 
37 const static rtl::OUString VALIDATION( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_VALIDAT ) );
38 const static rtl::OUString IGNOREBLANK( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_IGNOREBL ) );
39 const static rtl::OUString SHOWINPUT( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWINP  ) );
40 const static rtl::OUString SHOWERROR( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWERR  ) );
41 const static rtl::OUString ERRORTITLE( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_ERRTITLE  ) );
42 const static rtl::OUString INPUTTITLE( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_INPTITLE  ) );
43 const static rtl::OUString INPUTMESS( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_INPMESS  ) );
44 const static rtl::OUString ERRORMESS( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_ERRMESS  ) );
45 const static rtl::OUString STYPE( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_TYPE  ) );
46 const static rtl::OUString SHOWLIST( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_SHOWLIST  ) );
47 const static rtl::OUString ALERTSTYLE( RTL_CONSTASCII_USTRINGPARAM(  SC_UNONAME_ERRALSTY  ) );
48 
49 void
50 lcl_setValidationProps( const uno::Reference< table::XCellRange >& xRange, const uno::Reference< beans::XPropertySet >& xProps )
51 {
52 	uno::Reference< beans::XPropertySet > xRangeProps( xRange, uno::UNO_QUERY_THROW );
53 	xRangeProps->setPropertyValue( VALIDATION , uno::makeAny( xProps ) );
54 }
55 
56 uno::Reference< beans::XPropertySet >
57 lcl_getValidationProps( const uno::Reference< table::XCellRange >& xRange )
58 {
59 	uno::Reference< beans::XPropertySet > xProps( xRange, uno::UNO_QUERY_THROW );
60 	uno::Reference< beans::XPropertySet > xValProps;
61 	xValProps.set( xProps->getPropertyValue( VALIDATION ), uno::UNO_QUERY_THROW );
62 	return xValProps;
63 }
64 
65 ::sal_Bool SAL_CALL
66 ScVbaValidation::getIgnoreBlank() throw (uno::RuntimeException)
67 {
68 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
69 	sal_Bool bBlank = sal_False;
70 	xProps->getPropertyValue( IGNOREBLANK )  >>= bBlank;
71 	return bBlank;
72 }
73 
74 void SAL_CALL
75 ScVbaValidation::setIgnoreBlank( ::sal_Bool _ignoreblank ) throw (uno::RuntimeException)
76 {
77 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
78 	xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _ignoreblank ) );
79 	lcl_setValidationProps( m_xRange, xProps );
80 }
81 
82 ::sal_Bool SAL_CALL
83 ScVbaValidation::getInCellDropdown() throw (uno::RuntimeException)
84 {
85 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
86 	sal_Int32 nShowList = 0;
87 	xProps->getPropertyValue( SHOWLIST )  >>= nShowList;
88 	return ( nShowList ? sal_True : sal_False );
89 }
90 
91 void SAL_CALL
92 ScVbaValidation::setInCellDropdown( ::sal_Bool  _incelldropdown  ) throw (uno::RuntimeException)
93 {
94 	sal_Int32 nDropDown = sal_False;
95 	if ( _incelldropdown )
96 		nDropDown = 1;
97 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
98 	xProps->setPropertyValue( SHOWLIST, uno::makeAny( nDropDown ) );
99 	lcl_setValidationProps( m_xRange, xProps );
100 }
101 
102 ::sal_Bool SAL_CALL
103 ScVbaValidation::getShowInput() throw (uno::RuntimeException)
104 {
105 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
106 	sal_Bool bShowInput = sal_False;
107 	xProps->getPropertyValue( SHOWINPUT )  >>= bShowInput;
108 	return bShowInput;
109 }
110 
111 void SAL_CALL
112 ScVbaValidation:: setShowInput( ::sal_Bool _showinput ) throw (uno::RuntimeException)
113 {
114 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
115 	xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _showinput ) );
116 	lcl_setValidationProps( m_xRange, xProps );
117 }
118 
119 ::sal_Bool SAL_CALL
120 ScVbaValidation::getShowError() throw (uno::RuntimeException)
121 {
122 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
123 	sal_Bool bShowError = sal_False;
124 	xProps->getPropertyValue( SHOWERROR )  >>= bShowError;
125 	return bShowError;
126 }
127 
128 void SAL_CALL
129 ScVbaValidation::setShowError( ::sal_Bool _showerror ) throw (uno::RuntimeException)
130 {
131 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
132 	xProps->setPropertyValue( SHOWERROR, uno::makeAny( _showerror ) );
133 	lcl_setValidationProps( m_xRange, xProps );
134 }
135 
136 ::rtl::OUString SAL_CALL
137 ScVbaValidation::getErrorTitle() throw (uno::RuntimeException)
138 {
139 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
140 	rtl::OUString sErrorTitle;
141 	xProps->getPropertyValue( ERRORTITLE )  >>= sErrorTitle;
142 	return sErrorTitle;
143 }
144 
145 void
146 ScVbaValidation::setErrorTitle( const rtl::OUString& _errormessage ) throw (uno::RuntimeException)
147 {
148 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
149 	xProps->setPropertyValue( ERRORTITLE, uno::makeAny( _errormessage ) );
150 	lcl_setValidationProps( m_xRange, xProps );
151 }
152 
153 ::rtl::OUString SAL_CALL
154 ScVbaValidation::getInputMessage() throw (uno::RuntimeException)
155 {
156 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
157 	rtl::OUString sMsg;
158 	xProps->getPropertyValue( INPUTMESS )  >>= sMsg;
159 	return sMsg;
160 }
161 
162 void SAL_CALL
163 ScVbaValidation::setInputMessage( const ::rtl::OUString& _inputmessage ) throw (uno::RuntimeException)
164 {
165 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
166 	xProps->setPropertyValue( INPUTMESS, uno::makeAny( _inputmessage ) );
167 	lcl_setValidationProps( m_xRange, xProps );
168 }
169 
170 ::rtl::OUString SAL_CALL
171 ScVbaValidation::getInputTitle() throw (uno::RuntimeException)
172 {
173 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
174 	rtl::OUString sString;
175 	xProps->getPropertyValue( INPUTTITLE )  >>= sString;
176 	return sString;
177 }
178 
179 void SAL_CALL
180 ScVbaValidation::setInputTitle( const ::rtl::OUString& _inputtitle ) throw (uno::RuntimeException)
181 {
182 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
183 	xProps->setPropertyValue( INPUTTITLE, uno::makeAny( _inputtitle ) );
184 	lcl_setValidationProps( m_xRange, xProps );
185 }
186 
187 ::rtl::OUString SAL_CALL
188 ScVbaValidation::getErrorMessage() throw (uno::RuntimeException)
189 {
190 	uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
191 	rtl::OUString sString;
192 	xProps->getPropertyValue( ERRORMESS )  >>= sString;
193 	return sString;
194 }
195 
196 void SAL_CALL
197 ScVbaValidation::setErrorMessage( const ::rtl::OUString& _errormessage ) throw (uno::RuntimeException)
198 {
199 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
200 	xProps->setPropertyValue( ERRORMESS, uno::makeAny( _errormessage ) );
201 	lcl_setValidationProps( m_xRange, xProps );
202 }
203 
204 
205 void SAL_CALL
206 ScVbaValidation::Delete(  ) throw (uno::RuntimeException)
207 {
208 	rtl::OUString sBlank;
209 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
210 	uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
211 	xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( sal_True ) );
212 	xProps->setPropertyValue( SHOWINPUT, uno::makeAny( sal_True ) );
213 	xProps->setPropertyValue( SHOWERROR, uno::makeAny( sal_True ) );
214 	xProps->setPropertyValue( ERRORTITLE, uno::makeAny( sBlank ) );
215 	xProps->setPropertyValue( INPUTMESS, uno::makeAny( sBlank) );
216 	xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( sheet::ValidationAlertStyle_STOP) );
217 	xProps->setPropertyValue( STYPE, uno::makeAny( sheet::ValidationType_ANY ) );
218 	xCond->setFormula1( sBlank );
219 	xCond->setFormula2( sBlank );
220 	xCond->setOperator( sheet::ConditionOperator_NONE );
221 
222 	lcl_setValidationProps( m_xRange, xProps );
223 }
224 void SAL_CALL
225 ScVbaValidation::Add( const uno::Any& Type, const uno::Any& AlertStyle, const uno::Any& /*Operator*/, const uno::Any& Formula1, const uno::Any& Formula2 ) throw (uno::RuntimeException)
226 {
227 	uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
228 	uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
229 
230 	sheet::ValidationType nValType = sheet::ValidationType_ANY;
231 	xProps->getPropertyValue( STYPE )  >>= nValType;
232 	if ( nValType  != sheet::ValidationType_ANY  )
233 		throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "validation object already exists" ) ), uno::Reference< uno::XInterface >() );
234 	sal_Int32 nType = -1;
235 	if ( !Type.hasValue()  || !( Type >>= nType ) )
236 		throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "missing required param" ) ), uno::Reference< uno::XInterface >() );
237 
238 	Delete(); // set up defaults
239 	rtl::OUString sFormula1;
240 	Formula1 >>= sFormula1;
241 	rtl::OUString sFormula2;
242 	Formula2 >>= sFormula2;
243 	switch ( nType )
244 	{
245 		case excel::XlDVType::xlValidateList:
246 			{
247 				// for validate list
248 				// at least formula1 is required
249 				if ( !Formula1.hasValue() )
250 					throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "missing param" ) ), uno::Reference< uno::XInterface >() );
251 				nValType = sheet::ValidationType_LIST;
252 				xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
253 				// #TODO validate required params
254 				// #TODO need to correct the ';' delimited formula on get/set
255 				break;
256 			}
257 		case excel::XlDVType::xlValidateWholeNumber:
258 			nValType = sheet::ValidationType_WHOLE;
259 			xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
260 			break;
261 		default:
262 			throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "unsupported operation..." ) ), uno::Reference< uno::XInterface >() );
263 	}
264 
265 	sheet::ValidationAlertStyle eStyle = sheet::ValidationAlertStyle_STOP;
266 	sal_Int32 nVbaAlertStyle = excel::XlDVAlertStyle::xlValidAlertStop;
267 	if ( AlertStyle.hasValue() && ( AlertStyle >>= nVbaAlertStyle ) )
268 	{
269 		switch( nVbaAlertStyle )
270 		{
271 			case excel::XlDVAlertStyle::xlValidAlertStop:
272 				// yes I know it's already defaulted but safer to assume
273 				// someone propbably could change the code above
274 				eStyle = sheet::ValidationAlertStyle_STOP;
275 				break;
276 			case excel::XlDVAlertStyle::xlValidAlertWarning:
277 				eStyle = sheet::ValidationAlertStyle_WARNING;
278 				break;
279 			case excel::XlDVAlertStyle::xlValidAlertInformation:
280 				eStyle = sheet::ValidationAlertStyle_INFO;
281 				break;
282 			default:
283 			throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "bad param..." ) ), uno::Reference< uno::XInterface >() );
284 
285 		}
286 	}
287 
288 	xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( eStyle ) );
289 
290 	if ( sFormula1.getLength() )
291 		xCond->setFormula1( sFormula1 );
292 	if ( sFormula2.getLength() )
293 		xCond->setFormula2( sFormula2 );
294 
295 	lcl_setValidationProps( m_xRange, xProps );
296 }
297 
298 ::rtl::OUString SAL_CALL
299 ScVbaValidation::getFormula1() throw (uno::RuntimeException)
300 {
301 	uno::Reference< sheet::XSheetCondition > xCond( lcl_getValidationProps( m_xRange ), uno::UNO_QUERY_THROW );
302 	return xCond->getFormula1();
303 }
304 
305 ::rtl::OUString SAL_CALL
306 ScVbaValidation::getFormula2() throw (uno::RuntimeException)
307 {
308 		uno::Reference< sheet::XSheetCondition > xCond( lcl_getValidationProps( m_xRange ), uno::UNO_QUERY_THROW );
309 	return xCond->getFormula2();
310 }
311 
312 rtl::OUString&
313 ScVbaValidation::getServiceImplName()
314 {
315 	static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaValidation") );
316 	return sImplName;
317 }
318 
319 uno::Sequence< rtl::OUString >
320 ScVbaValidation::getServiceNames()
321 {
322 	static uno::Sequence< rtl::OUString > aServiceNames;
323 	if ( aServiceNames.getLength() == 0 )
324 	{
325 		aServiceNames.realloc( 1 );
326 		aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Validation" ) );
327 	}
328 	return aServiceNames;
329 }
330