1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: vbavalidation.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include "vbavalidation.hxx"
31 #include <com/sun/star/sheet/XSheetCondition.hpp>
32 #include <com/sun/star/sheet/ValidationType.hpp>
33 #include <com/sun/star/sheet/ValidationAlertStyle.hpp>
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <ooo/vba/excel/XlDVType.hpp>
36 #include <ooo/vba/excel/XlFormatConditionOperator.hpp>
37 #include <ooo/vba/excel/XlDVAlertStyle.hpp>
39 #include "unonames.hxx"
41 using namespace ::ooo::vba
;
42 using namespace ::com::sun::star
;
44 const static rtl::OUString
VALIDATION( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_VALIDAT
) );
45 const static rtl::OUString
IGNOREBLANK( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_IGNOREBL
) );
46 const static rtl::OUString
SHOWINPUT( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWINP
) );
47 const static rtl::OUString
SHOWERROR( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWERR
) );
48 const static rtl::OUString
ERRORTITLE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_ERRTITLE
) );
49 const static rtl::OUString
INPUTTITLE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_INPTITLE
) );
50 const static rtl::OUString
INPUTMESS( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_INPMESS
) );
51 const static rtl::OUString
ERRORMESS( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_ERRMESS
) );
52 const static rtl::OUString
STYPE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_TYPE
) );
53 const static rtl::OUString
SHOWLIST( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_SHOWLIST
) );
54 const static rtl::OUString
ALERTSTYLE( RTL_CONSTASCII_USTRINGPARAM( SC_UNONAME_ERRALSTY
) );
57 lcl_setValidationProps( const uno::Reference
< table::XCellRange
>& xRange
, const uno::Reference
< beans::XPropertySet
>& xProps
)
59 uno::Reference
< beans::XPropertySet
> xRangeProps( xRange
, uno::UNO_QUERY_THROW
);
60 xRangeProps
->setPropertyValue( VALIDATION
, uno::makeAny( xProps
) );
63 uno::Reference
< beans::XPropertySet
>
64 lcl_getValidationProps( const uno::Reference
< table::XCellRange
>& xRange
)
66 uno::Reference
< beans::XPropertySet
> xProps( xRange
, uno::UNO_QUERY_THROW
);
67 uno::Reference
< beans::XPropertySet
> xValProps
;
68 xValProps
.set( xProps
->getPropertyValue( VALIDATION
), uno::UNO_QUERY_THROW
);
73 ScVbaValidation::getIgnoreBlank() throw (uno::RuntimeException
)
75 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
76 sal_Bool bBlank
= sal_False
;
77 xProps
->getPropertyValue( IGNOREBLANK
) >>= bBlank
;
82 ScVbaValidation::setIgnoreBlank( ::sal_Bool _ignoreblank
) throw (uno::RuntimeException
)
84 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
85 xProps
->setPropertyValue( IGNOREBLANK
, uno::makeAny( _ignoreblank
) );
86 lcl_setValidationProps( m_xRange
, xProps
);
90 ScVbaValidation::getInCellDropdown() throw (uno::RuntimeException
)
92 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
93 sal_Int32 nShowList
= 0;
94 xProps
->getPropertyValue( SHOWLIST
) >>= nShowList
;
95 return ( nShowList
? sal_True
: sal_False
);
99 ScVbaValidation::setInCellDropdown( ::sal_Bool _incelldropdown
) throw (uno::RuntimeException
)
101 sal_Int32 nDropDown
= sal_False
;
102 if ( _incelldropdown
)
104 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps(m_xRange
) );
105 xProps
->setPropertyValue( SHOWLIST
, uno::makeAny( nDropDown
) );
106 lcl_setValidationProps( m_xRange
, xProps
);
110 ScVbaValidation::getShowInput() throw (uno::RuntimeException
)
112 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
113 sal_Bool bShowInput
= sal_False
;
114 xProps
->getPropertyValue( SHOWINPUT
) >>= bShowInput
;
119 ScVbaValidation:: setShowInput( ::sal_Bool _showinput
) throw (uno::RuntimeException
)
121 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps(m_xRange
) );
122 xProps
->setPropertyValue( IGNOREBLANK
, uno::makeAny( _showinput
) );
123 lcl_setValidationProps( m_xRange
, xProps
);
127 ScVbaValidation::getShowError() throw (uno::RuntimeException
)
129 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
130 sal_Bool bShowError
= sal_False
;
131 xProps
->getPropertyValue( SHOWERROR
) >>= bShowError
;
136 ScVbaValidation::setShowError( ::sal_Bool _showerror
) throw (uno::RuntimeException
)
138 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
139 xProps
->setPropertyValue( SHOWERROR
, uno::makeAny( _showerror
) );
140 lcl_setValidationProps( m_xRange
, xProps
);
143 ::rtl::OUString SAL_CALL
144 ScVbaValidation::getErrorTitle() throw (uno::RuntimeException
)
146 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
147 rtl::OUString sErrorTitle
;
148 xProps
->getPropertyValue( ERRORTITLE
) >>= sErrorTitle
;
153 ScVbaValidation::setErrorTitle( const rtl::OUString
& _errormessage
) throw (uno::RuntimeException
)
155 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
156 xProps
->setPropertyValue( ERRORTITLE
, uno::makeAny( _errormessage
) );
157 lcl_setValidationProps( m_xRange
, xProps
);
160 ::rtl::OUString SAL_CALL
161 ScVbaValidation::getInputMessage() throw (uno::RuntimeException
)
163 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
165 xProps
->getPropertyValue( INPUTMESS
) >>= sMsg
;
170 ScVbaValidation::setInputMessage( const ::rtl::OUString
& _inputmessage
) throw (uno::RuntimeException
)
172 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
173 xProps
->setPropertyValue( INPUTMESS
, uno::makeAny( _inputmessage
) );
174 lcl_setValidationProps( m_xRange
, xProps
);
177 ::rtl::OUString SAL_CALL
178 ScVbaValidation::getInputTitle() throw (uno::RuntimeException
)
180 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
181 rtl::OUString sString
;
182 xProps
->getPropertyValue( INPUTTITLE
) >>= sString
;
187 ScVbaValidation::setInputTitle( const ::rtl::OUString
& _inputtitle
) throw (uno::RuntimeException
)
189 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
190 xProps
->setPropertyValue( INPUTTITLE
, uno::makeAny( _inputtitle
) );
191 lcl_setValidationProps( m_xRange
, xProps
);
194 ::rtl::OUString SAL_CALL
195 ScVbaValidation::getErrorMessage() throw (uno::RuntimeException
)
197 uno::Reference
< beans::XPropertySet
> xProps
= lcl_getValidationProps( m_xRange
);
198 rtl::OUString sString
;
199 xProps
->getPropertyValue( ERRORMESS
) >>= sString
;
204 ScVbaValidation::setErrorMessage( const ::rtl::OUString
& _errormessage
) throw (uno::RuntimeException
)
206 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
207 xProps
->setPropertyValue( ERRORMESS
, uno::makeAny( _errormessage
) );
208 lcl_setValidationProps( m_xRange
, xProps
);
213 ScVbaValidation::Delete( ) throw (uno::RuntimeException
)
215 rtl::OUString sBlank
;
216 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
217 uno::Reference
< sheet::XSheetCondition
> xCond( xProps
, uno::UNO_QUERY_THROW
);
218 xProps
->setPropertyValue( IGNOREBLANK
, uno::makeAny( sal_True
) );
219 xProps
->setPropertyValue( SHOWINPUT
, uno::makeAny( sal_True
) );
220 xProps
->setPropertyValue( SHOWERROR
, uno::makeAny( sal_True
) );
221 xProps
->setPropertyValue( ERRORTITLE
, uno::makeAny( sBlank
) );
222 xProps
->setPropertyValue( INPUTMESS
, uno::makeAny( sBlank
) );
223 xProps
->setPropertyValue( ALERTSTYLE
, uno::makeAny( sheet::ValidationAlertStyle_STOP
) );
224 xProps
->setPropertyValue( STYPE
, uno::makeAny( sheet::ValidationType_ANY
) );
225 xCond
->setFormula1( sBlank
);
226 xCond
->setFormula2( sBlank
);
227 xCond
->setOperator( sheet::ConditionOperator_NONE
);
229 lcl_setValidationProps( m_xRange
, xProps
);
232 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
)
234 uno::Reference
< beans::XPropertySet
> xProps( lcl_getValidationProps( m_xRange
) );
235 uno::Reference
< sheet::XSheetCondition
> xCond( xProps
, uno::UNO_QUERY_THROW
);
237 sheet::ValidationType nValType
= sheet::ValidationType_ANY
;
238 xProps
->getPropertyValue( STYPE
) >>= nValType
;
239 if ( nValType
!= sheet::ValidationType_ANY
)
240 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "validation object already exists" ) ), uno::Reference
< uno::XInterface
>() );
241 sal_Int32 nType
= -1;
242 if ( !Type
.hasValue() || !( Type
>>= nType
) )
243 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "missing required param" ) ), uno::Reference
< uno::XInterface
>() );
245 Delete(); // set up defaults
246 rtl::OUString sFormula1
;
247 Formula1
>>= sFormula1
;
248 rtl::OUString sFormula2
;
249 Formula2
>>= sFormula2
;
252 case excel::XlDVType::xlValidateList
:
255 // at least formula1 is required
256 if ( !Formula1
.hasValue() )
257 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "missing param" ) ), uno::Reference
< uno::XInterface
>() );
258 nValType
= sheet::ValidationType_LIST
;
259 xProps
->setPropertyValue( STYPE
, uno::makeAny(nValType
));
260 // #TODO validate required params
261 // #TODO need to correct the ';' delimited formula on get/set
264 case excel::XlDVType::xlValidateWholeNumber
:
265 nValType
= sheet::ValidationType_WHOLE
;
266 xProps
->setPropertyValue( STYPE
, uno::makeAny(nValType
));
269 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "unsupported operation..." ) ), uno::Reference
< uno::XInterface
>() );
272 sheet::ValidationAlertStyle eStyle
= sheet::ValidationAlertStyle_STOP
;
273 sal_Int32 nVbaAlertStyle
= excel::XlDVAlertStyle::xlValidAlertStop
;
274 if ( AlertStyle
.hasValue() && ( AlertStyle
>>= nVbaAlertStyle
) )
276 switch( nVbaAlertStyle
)
278 case excel::XlDVAlertStyle::xlValidAlertStop
:
279 // yes I know it's already defaulted but safer to assume
280 // someone propbably could change the code above
281 eStyle
= sheet::ValidationAlertStyle_STOP
;
283 case excel::XlDVAlertStyle::xlValidAlertWarning
:
284 eStyle
= sheet::ValidationAlertStyle_WARNING
;
286 case excel::XlDVAlertStyle::xlValidAlertInformation
:
287 eStyle
= sheet::ValidationAlertStyle_INFO
;
290 throw uno::RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "bad param..." ) ), uno::Reference
< uno::XInterface
>() );
295 xProps
->setPropertyValue( ALERTSTYLE
, uno::makeAny( eStyle
) );
297 if ( sFormula1
.getLength() )
298 xCond
->setFormula1( sFormula1
);
299 if ( sFormula2
.getLength() )
300 xCond
->setFormula2( sFormula2
);
302 lcl_setValidationProps( m_xRange
, xProps
);
305 ::rtl::OUString SAL_CALL
306 ScVbaValidation::getFormula1() throw (uno::RuntimeException
)
308 uno::Reference
< sheet::XSheetCondition
> xCond( lcl_getValidationProps( m_xRange
), uno::UNO_QUERY_THROW
);
309 return xCond
->getFormula1();
312 ::rtl::OUString SAL_CALL
313 ScVbaValidation::getFormula2() throw (uno::RuntimeException
)
315 uno::Reference
< sheet::XSheetCondition
> xCond( lcl_getValidationProps( m_xRange
), uno::UNO_QUERY_THROW
);
316 return xCond
->getFormula2();
320 ScVbaValidation::getServiceImplName()
322 static rtl::OUString
sImplName( RTL_CONSTASCII_USTRINGPARAM("ScVbaValidation") );
326 uno::Sequence
< rtl::OUString
>
327 ScVbaValidation::getServiceNames()
329 static uno::Sequence
< rtl::OUString
> aServiceNames
;
330 if ( aServiceNames
.getLength() == 0 )
332 aServiceNames
.realloc( 1 );
333 aServiceNames
[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.excel.Validation" ) );
335 return aServiceNames
;