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