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