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