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