Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "vbavalidation.hxx"
21 : #include "vbaformatcondition.hxx"
22 : #include <com/sun/star/sheet/XSheetCondition.hpp>
23 : #include <com/sun/star/sheet/ValidationType.hpp>
24 : #include <com/sun/star/sheet/ValidationAlertStyle.hpp>
25 : #include <com/sun/star/beans/XPropertySet.hpp>
26 : #include <ooo/vba/excel/XlDVType.hpp>
27 : #include <ooo/vba/excel/XlFormatConditionOperator.hpp>
28 : #include <ooo/vba/excel/XlDVAlertStyle.hpp>
29 :
30 : #include "unonames.hxx"
31 : #include "rangelst.hxx"
32 : #include "excelvbahelper.hxx"
33 : #include "vbarange.hxx"
34 :
35 : using namespace ::ooo::vba;
36 : using namespace ::com::sun::star;
37 :
38 0 : const static OUString VALIDATION( SC_UNONAME_VALIDAT );
39 0 : const static OUString IGNOREBLANK( SC_UNONAME_IGNOREBL );
40 0 : const static OUString SHOWINPUT( SC_UNONAME_SHOWINP );
41 0 : const static OUString SHOWERROR( SC_UNONAME_SHOWERR );
42 0 : const static OUString ERRORTITLE( SC_UNONAME_ERRTITLE );
43 0 : const static OUString INPUTTITLE( SC_UNONAME_INPTITLE );
44 0 : const static OUString INPUTMESS( SC_UNONAME_INPMESS );
45 0 : const static OUString ERRORMESS( SC_UNONAME_ERRMESS );
46 0 : const static OUString STYPE( SC_UNONAME_TYPE );
47 0 : const static OUString SHOWLIST( SC_UNONAME_SHOWLIST );
48 0 : const static OUString ALERTSTYLE( SC_UNONAME_ERRALSTY );
49 :
50 : static void
51 0 : lcl_setValidationProps( const uno::Reference< table::XCellRange >& xRange, const uno::Reference< beans::XPropertySet >& xProps )
52 : {
53 0 : uno::Reference< beans::XPropertySet > xRangeProps( xRange, uno::UNO_QUERY_THROW );
54 0 : xRangeProps->setPropertyValue( VALIDATION , uno::makeAny( xProps ) );
55 0 : }
56 :
57 : static uno::Reference< beans::XPropertySet >
58 0 : lcl_getValidationProps( const uno::Reference< table::XCellRange >& xRange )
59 : {
60 0 : uno::Reference< beans::XPropertySet > xProps( xRange, uno::UNO_QUERY_THROW );
61 0 : uno::Reference< beans::XPropertySet > xValProps;
62 0 : xValProps.set( xProps->getPropertyValue( VALIDATION ), uno::UNO_QUERY_THROW );
63 0 : return xValProps;
64 : }
65 :
66 : sal_Bool SAL_CALL
67 0 : ScVbaValidation::getIgnoreBlank() throw (uno::RuntimeException, std::exception)
68 : {
69 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
70 0 : sal_Bool bBlank = false;
71 0 : xProps->getPropertyValue( IGNOREBLANK ) >>= bBlank;
72 0 : return bBlank;
73 : }
74 :
75 : void SAL_CALL
76 0 : ScVbaValidation::setIgnoreBlank( sal_Bool _ignoreblank ) throw (uno::RuntimeException, std::exception)
77 : {
78 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
79 0 : xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _ignoreblank ) );
80 0 : lcl_setValidationProps( m_xRange, xProps );
81 0 : }
82 :
83 : sal_Bool SAL_CALL
84 0 : ScVbaValidation::getInCellDropdown() throw (uno::RuntimeException, std::exception)
85 : {
86 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
87 0 : sal_Int32 nShowList = 0;
88 0 : xProps->getPropertyValue( SHOWLIST ) >>= nShowList;
89 0 : return ( nShowList ? sal_True : false );
90 : }
91 :
92 : void SAL_CALL
93 0 : ScVbaValidation::setInCellDropdown( sal_Bool _incelldropdown ) throw (uno::RuntimeException, std::exception)
94 : {
95 0 : sal_Int32 nDropDown = 0;
96 0 : if ( _incelldropdown )
97 0 : nDropDown = 1;
98 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
99 0 : xProps->setPropertyValue( SHOWLIST, uno::makeAny( nDropDown ) );
100 0 : lcl_setValidationProps( m_xRange, xProps );
101 0 : }
102 :
103 : sal_Bool SAL_CALL
104 0 : ScVbaValidation::getShowInput() throw (uno::RuntimeException, std::exception)
105 : {
106 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
107 0 : sal_Bool bShowInput = false;
108 0 : xProps->getPropertyValue( SHOWINPUT ) >>= bShowInput;
109 0 : return bShowInput;
110 : }
111 :
112 : void SAL_CALL
113 0 : ScVbaValidation:: setShowInput( sal_Bool _showinput ) throw (uno::RuntimeException, std::exception)
114 : {
115 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
116 0 : xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _showinput ) );
117 0 : lcl_setValidationProps( m_xRange, xProps );
118 0 : }
119 :
120 : sal_Bool SAL_CALL
121 0 : ScVbaValidation::getShowError() throw (uno::RuntimeException, std::exception)
122 : {
123 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
124 0 : sal_Bool bShowError = false;
125 0 : xProps->getPropertyValue( SHOWERROR ) >>= bShowError;
126 0 : return bShowError;
127 : }
128 :
129 : void SAL_CALL
130 0 : ScVbaValidation::setShowError( sal_Bool _showerror ) throw (uno::RuntimeException, std::exception)
131 : {
132 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
133 0 : xProps->setPropertyValue( SHOWERROR, uno::makeAny( _showerror ) );
134 0 : lcl_setValidationProps( m_xRange, xProps );
135 0 : }
136 :
137 : OUString SAL_CALL
138 0 : ScVbaValidation::getErrorTitle() throw (uno::RuntimeException, std::exception)
139 : {
140 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
141 0 : OUString sErrorTitle;
142 0 : xProps->getPropertyValue( ERRORTITLE ) >>= sErrorTitle;
143 0 : return sErrorTitle;
144 : }
145 :
146 : void
147 0 : ScVbaValidation::setErrorTitle( const OUString& _errormessage ) throw (uno::RuntimeException, std::exception)
148 : {
149 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
150 0 : xProps->setPropertyValue( ERRORTITLE, uno::makeAny( _errormessage ) );
151 0 : lcl_setValidationProps( m_xRange, xProps );
152 0 : }
153 :
154 : OUString SAL_CALL
155 0 : ScVbaValidation::getInputMessage() throw (uno::RuntimeException, std::exception)
156 : {
157 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
158 0 : OUString sMsg;
159 0 : xProps->getPropertyValue( INPUTMESS ) >>= sMsg;
160 0 : return sMsg;
161 : }
162 :
163 : void SAL_CALL
164 0 : ScVbaValidation::setInputMessage( const OUString& _inputmessage ) throw (uno::RuntimeException, std::exception)
165 : {
166 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
167 0 : xProps->setPropertyValue( INPUTMESS, uno::makeAny( _inputmessage ) );
168 0 : lcl_setValidationProps( m_xRange, xProps );
169 0 : }
170 :
171 : OUString SAL_CALL
172 0 : ScVbaValidation::getInputTitle() throw (uno::RuntimeException, std::exception)
173 : {
174 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
175 0 : OUString sString;
176 0 : xProps->getPropertyValue( INPUTTITLE ) >>= sString;
177 0 : return sString;
178 : }
179 :
180 : void SAL_CALL
181 0 : ScVbaValidation::setInputTitle( const OUString& _inputtitle ) throw (uno::RuntimeException, std::exception)
182 : {
183 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
184 0 : xProps->setPropertyValue( INPUTTITLE, uno::makeAny( _inputtitle ) );
185 0 : lcl_setValidationProps( m_xRange, xProps );
186 0 : }
187 :
188 : OUString SAL_CALL
189 0 : ScVbaValidation::getErrorMessage() throw (uno::RuntimeException, std::exception)
190 : {
191 0 : uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
192 0 : OUString sString;
193 0 : xProps->getPropertyValue( ERRORMESS ) >>= sString;
194 0 : return sString;
195 : }
196 :
197 : void SAL_CALL
198 0 : ScVbaValidation::setErrorMessage( const OUString& _errormessage ) throw (uno::RuntimeException, std::exception)
199 : {
200 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
201 0 : xProps->setPropertyValue( ERRORMESS, uno::makeAny( _errormessage ) );
202 0 : lcl_setValidationProps( m_xRange, xProps );
203 0 : }
204 :
205 :
206 : void SAL_CALL
207 0 : ScVbaValidation::Delete( ) throw (uno::RuntimeException, std::exception)
208 : {
209 0 : OUString sBlank;
210 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
211 0 : uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
212 0 : xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( sal_True ) );
213 0 : xProps->setPropertyValue( SHOWINPUT, uno::makeAny( sal_True ) );
214 0 : xProps->setPropertyValue( SHOWERROR, uno::makeAny( sal_True ) );
215 0 : xProps->setPropertyValue( ERRORTITLE, uno::makeAny( sBlank ) );
216 0 : xProps->setPropertyValue( INPUTMESS, uno::makeAny( sBlank) );
217 0 : xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( sheet::ValidationAlertStyle_STOP) );
218 0 : xProps->setPropertyValue( STYPE, uno::makeAny( sheet::ValidationType_ANY ) );
219 0 : xCond->setFormula1( sBlank );
220 0 : xCond->setFormula2( sBlank );
221 0 : xCond->setOperator( sheet::ConditionOperator_NONE );
222 :
223 0 : lcl_setValidationProps( m_xRange, xProps );
224 0 : }
225 :
226 : // Fix the defect that validatation cannot work when the input should be limited between a lower bound and an upper bound
227 : void SAL_CALL
228 0 : 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, std::exception)
229 : {
230 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
231 0 : uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
232 :
233 0 : sheet::ValidationType nValType = sheet::ValidationType_ANY;
234 0 : xProps->getPropertyValue( STYPE ) >>= nValType;
235 0 : if ( nValType != sheet::ValidationType_ANY )
236 0 : throw uno::RuntimeException("validation object already exists", uno::Reference< uno::XInterface >() );
237 0 : sal_Int32 nType = -1;
238 0 : if ( !Type.hasValue() || !( Type >>= nType ) )
239 0 : throw uno::RuntimeException("missing required param", uno::Reference< uno::XInterface >() );
240 :
241 0 : Delete(); // set up defaults
242 0 : OUString sFormula1;
243 0 : Formula1 >>= sFormula1;
244 0 : OUString sFormula2;
245 0 : Formula2 >>= sFormula2;
246 0 : switch ( nType )
247 : {
248 : case excel::XlDVType::xlValidateList:
249 : {
250 : // for validate list
251 : // at least formula1 is required
252 0 : if ( !Formula1.hasValue() )
253 0 : throw uno::RuntimeException("missing param", uno::Reference< uno::XInterface >() );
254 0 : nValType = sheet::ValidationType_LIST;
255 0 : xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
256 : // #TODO validate required params
257 : // #TODO need to correct the ';' delimited formula on get/set
258 0 : break;
259 : }
260 : case excel::XlDVType::xlValidateWholeNumber:
261 0 : nValType = sheet::ValidationType_WHOLE;
262 0 : xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
263 0 : break;
264 : default:
265 0 : throw uno::RuntimeException("unsupported operation...", uno::Reference< uno::XInterface >() );
266 : }
267 :
268 0 : sheet::ValidationAlertStyle eStyle = sheet::ValidationAlertStyle_STOP;
269 0 : sal_Int32 nVbaAlertStyle = excel::XlDVAlertStyle::xlValidAlertStop;
270 0 : if ( AlertStyle.hasValue() && ( AlertStyle >>= nVbaAlertStyle ) )
271 : {
272 0 : switch( nVbaAlertStyle )
273 : {
274 : case excel::XlDVAlertStyle::xlValidAlertStop:
275 : // yes I know it's already defaulted but safer to assume
276 : // someone propbably could change the code above
277 0 : eStyle = sheet::ValidationAlertStyle_STOP;
278 0 : break;
279 : case excel::XlDVAlertStyle::xlValidAlertWarning:
280 0 : eStyle = sheet::ValidationAlertStyle_WARNING;
281 0 : break;
282 : case excel::XlDVAlertStyle::xlValidAlertInformation:
283 0 : eStyle = sheet::ValidationAlertStyle_INFO;
284 0 : break;
285 : default:
286 0 : throw uno::RuntimeException("bad param...", uno::Reference< uno::XInterface >() );
287 :
288 : }
289 : }
290 :
291 0 : xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( eStyle ) );
292 :
293 : // i#108860: fix the defect that validation cannot work when the input
294 : // should be limited between a lower bound and an upper bound
295 0 : if ( Operator.hasValue() )
296 : {
297 0 : css::sheet::ConditionOperator conOperator = ScVbaFormatCondition::retrieveAPIOperator( Operator );
298 0 : xCond->setOperator( conOperator );
299 : }
300 :
301 0 : if ( !sFormula1.isEmpty() )
302 0 : xCond->setFormula1( sFormula1 );
303 0 : if ( !sFormula2.isEmpty() )
304 0 : xCond->setFormula2( sFormula2 );
305 :
306 0 : lcl_setValidationProps( m_xRange, xProps );
307 0 : }
308 :
309 : OUString SAL_CALL
310 0 : ScVbaValidation::getFormula1() throw (uno::RuntimeException, std::exception)
311 : {
312 0 : uno::Reference< sheet::XSheetCondition > xCond( lcl_getValidationProps( m_xRange ), uno::UNO_QUERY_THROW );
313 0 : OUString sString = xCond->getFormula1();
314 :
315 0 : sal_uInt16 nFlags = 0;
316 0 : ScRangeList aCellRanges;
317 0 : formula::FormulaGrammar::AddressConvention eConv = formula::FormulaGrammar::CONV_XL_A1;
318 :
319 0 : ScDocShell* pDocSh = excel::GetDocShellFromRange( m_xRange );
320 : // in calc validation formula is either a range or formula
321 : // that results in range.
322 : // In VBA both formula and address can have a leading '='
323 : // in result of getFormula1, however it *seems* that a named range or
324 : // real formula has to (or is expected to) have the '='
325 0 : if ( pDocSh && !ScVbaRange::getCellRangesForAddress( nFlags, sString, pDocSh, aCellRanges, eConv ) )
326 0 : sString = "=" + sString;
327 0 : return sString;
328 : }
329 :
330 : OUString SAL_CALL
331 0 : ScVbaValidation::getFormula2() throw (uno::RuntimeException, std::exception)
332 : {
333 0 : uno::Reference< sheet::XSheetCondition > xCond( lcl_getValidationProps( m_xRange ), uno::UNO_QUERY_THROW );
334 0 : return xCond->getFormula2();
335 : }
336 :
337 : sal_Int32 SAL_CALL
338 0 : ScVbaValidation::getType() throw (uno::RuntimeException, std::exception)
339 : {
340 0 : uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
341 0 : sheet::ValidationType nValType = sheet::ValidationType_ANY;
342 0 : xProps->getPropertyValue( STYPE ) >>= nValType;
343 0 : sal_Int32 nExcelType = excel::XlDVType::xlValidateList; // pick a default
344 0 : if ( xProps.is() )
345 : {
346 0 : switch ( nValType )
347 : {
348 : case sheet::ValidationType_LIST:
349 0 : nExcelType = excel::XlDVType::xlValidateList;
350 0 : break;
351 : case sheet::ValidationType_ANY: // not ANY not really a great match for anything I fear:-(
352 0 : nExcelType = excel::XlDVType::xlValidateInputOnly;
353 0 : break;
354 : case sheet::ValidationType_CUSTOM:
355 0 : nExcelType = excel::XlDVType::xlValidateCustom;
356 0 : break;
357 : case sheet::ValidationType_WHOLE:
358 0 : nExcelType = excel::XlDVType::xlValidateWholeNumber;
359 0 : break;
360 : case sheet::ValidationType_DECIMAL:
361 0 : nExcelType = excel::XlDVType::xlValidateDecimal;
362 0 : break;
363 : case sheet::ValidationType_DATE:
364 0 : nExcelType = excel::XlDVType::xlValidateDate;
365 0 : break;
366 : case sheet::ValidationType_TIME:
367 0 : nExcelType = excel::XlDVType::xlValidateTime;
368 0 : break;
369 : case sheet::ValidationType_TEXT_LEN:
370 0 : nExcelType = excel::XlDVType::xlValidateTextLength;
371 0 : break;
372 : case sheet::ValidationType_MAKE_FIXED_SIZE:
373 : default:
374 0 : break;
375 : };
376 : }
377 0 : return nExcelType;
378 : }
379 :
380 : OUString
381 0 : ScVbaValidation::getServiceImplName()
382 : {
383 0 : return OUString("ScVbaValidation");
384 : }
385 :
386 : uno::Sequence< OUString >
387 0 : ScVbaValidation::getServiceNames()
388 : {
389 0 : static uno::Sequence< OUString > aServiceNames;
390 0 : if ( aServiceNames.getLength() == 0 )
391 : {
392 0 : aServiceNames.realloc( 1 );
393 0 : aServiceNames[ 0 ] = "ooo.vba.excel.Validation";
394 : }
395 0 : return aServiceNames;
396 0 : }
397 :
398 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|