LCOV - code coverage report
Current view: top level - sc/source/ui/vba - vbavalidation.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 120 212 56.6 %
Date: 2014-04-11 Functions: 16 27 59.3 %
Legend: Lines: hit not hit

          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           2 : const static OUString VALIDATION( SC_UNONAME_VALIDAT );
      39           2 : const static OUString IGNOREBLANK( SC_UNONAME_IGNOREBL );
      40           2 : const static OUString SHOWINPUT( SC_UNONAME_SHOWINP  );
      41           2 : const static OUString SHOWERROR( SC_UNONAME_SHOWERR  );
      42           2 : const static OUString ERRORTITLE(  SC_UNONAME_ERRTITLE  );
      43           2 : const static OUString INPUTTITLE(  SC_UNONAME_INPTITLE  );
      44           2 : const static OUString INPUTMESS(  SC_UNONAME_INPMESS  );
      45           2 : const static OUString ERRORMESS(  SC_UNONAME_ERRMESS  );
      46           2 : const static OUString STYPE(  SC_UNONAME_TYPE  );
      47           2 : const static OUString SHOWLIST(  SC_UNONAME_SHOWLIST  );
      48           2 : const static OUString ALERTSTYLE(  SC_UNONAME_ERRALSTY  );
      49             : 
      50             : static void
      51          10 : lcl_setValidationProps( const uno::Reference< table::XCellRange >& xRange, const uno::Reference< beans::XPropertySet >& xProps )
      52             : {
      53          10 :     uno::Reference< beans::XPropertySet > xRangeProps( xRange, uno::UNO_QUERY_THROW );
      54          10 :     xRangeProps->setPropertyValue( VALIDATION , uno::makeAny( xProps ) );
      55          10 : }
      56             : 
      57             : static uno::Reference< beans::XPropertySet >
      58          20 : lcl_getValidationProps( const uno::Reference< table::XCellRange >& xRange )
      59             : {
      60          20 :     uno::Reference< beans::XPropertySet > xProps( xRange, uno::UNO_QUERY_THROW );
      61          20 :     uno::Reference< beans::XPropertySet > xValProps;
      62          20 :     xValProps.set( xProps->getPropertyValue( VALIDATION ), uno::UNO_QUERY_THROW );
      63          20 :     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           1 : ScVbaValidation:: setShowInput( sal_Bool _showinput ) throw (uno::RuntimeException, std::exception)
     114             : {
     115           1 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps(m_xRange) );
     116           1 :     xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( _showinput ) );
     117           1 :     lcl_setValidationProps( m_xRange, xProps );
     118           1 : }
     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           1 : ScVbaValidation::setShowError( sal_Bool _showerror ) throw (uno::RuntimeException, std::exception)
     131             : {
     132           1 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     133           1 :     xProps->setPropertyValue( SHOWERROR, uno::makeAny( _showerror ) );
     134           1 :     lcl_setValidationProps( m_xRange, xProps );
     135           1 : }
     136             : 
     137             : OUString SAL_CALL
     138           2 : ScVbaValidation::getErrorTitle() throw (uno::RuntimeException, std::exception)
     139             : {
     140           2 :     uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
     141           2 :     OUString sErrorTitle;
     142           2 :     xProps->getPropertyValue( ERRORTITLE )  >>= sErrorTitle;
     143           2 :     return sErrorTitle;
     144             : }
     145             : 
     146             : void
     147           1 : ScVbaValidation::setErrorTitle( const OUString& _errormessage ) throw (uno::RuntimeException, std::exception)
     148             : {
     149           1 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     150           1 :     xProps->setPropertyValue( ERRORTITLE, uno::makeAny( _errormessage ) );
     151           1 :     lcl_setValidationProps( m_xRange, xProps );
     152           1 : }
     153             : 
     154             : OUString SAL_CALL
     155           2 : ScVbaValidation::getInputMessage() throw (uno::RuntimeException, std::exception)
     156             : {
     157           2 :     uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
     158           2 :     OUString sMsg;
     159           2 :     xProps->getPropertyValue( INPUTMESS )  >>= sMsg;
     160           2 :     return sMsg;
     161             : }
     162             : 
     163             : void SAL_CALL
     164           1 : ScVbaValidation::setInputMessage( const OUString& _inputmessage ) throw (uno::RuntimeException, std::exception)
     165             : {
     166           1 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     167           1 :     xProps->setPropertyValue( INPUTMESS, uno::makeAny( _inputmessage ) );
     168           1 :     lcl_setValidationProps( m_xRange, xProps );
     169           1 : }
     170             : 
     171             : OUString SAL_CALL
     172           2 : ScVbaValidation::getInputTitle() throw (uno::RuntimeException, std::exception)
     173             : {
     174           2 :     uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
     175           2 :     OUString sString;
     176           2 :     xProps->getPropertyValue( INPUTTITLE )  >>= sString;
     177           2 :     return sString;
     178             : }
     179             : 
     180             : void SAL_CALL
     181           1 : ScVbaValidation::setInputTitle( const OUString& _inputtitle ) throw (uno::RuntimeException, std::exception)
     182             : {
     183           1 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     184           1 :     xProps->setPropertyValue( INPUTTITLE, uno::makeAny( _inputtitle ) );
     185           1 :     lcl_setValidationProps( m_xRange, xProps );
     186           1 : }
     187             : 
     188             : OUString SAL_CALL
     189           4 : ScVbaValidation::getErrorMessage() throw (uno::RuntimeException, std::exception)
     190             : {
     191           4 :     uno::Reference< beans::XPropertySet > xProps = lcl_getValidationProps( m_xRange );
     192           4 :     OUString sString;
     193           4 :     xProps->getPropertyValue( ERRORMESS )  >>= sString;
     194           4 :     return sString;
     195             : }
     196             : 
     197             : void SAL_CALL
     198           2 : ScVbaValidation::setErrorMessage( const OUString& _errormessage ) throw (uno::RuntimeException, std::exception)
     199             : {
     200           2 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     201           2 :     xProps->setPropertyValue( ERRORMESS, uno::makeAny( _errormessage ) );
     202           2 :     lcl_setValidationProps( m_xRange, xProps );
     203           2 : }
     204             : 
     205             : 
     206             : void SAL_CALL
     207           2 : ScVbaValidation::Delete(  ) throw (uno::RuntimeException, std::exception)
     208             : {
     209           2 :     OUString sBlank;
     210           4 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     211           4 :     uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
     212           2 :     xProps->setPropertyValue( IGNOREBLANK, uno::makeAny( sal_True ) );
     213           2 :     xProps->setPropertyValue( SHOWINPUT, uno::makeAny( sal_True ) );
     214           2 :     xProps->setPropertyValue( SHOWERROR, uno::makeAny( sal_True ) );
     215           2 :     xProps->setPropertyValue( ERRORTITLE, uno::makeAny( sBlank ) );
     216           2 :     xProps->setPropertyValue( INPUTMESS, uno::makeAny( sBlank) );
     217           2 :     xProps->setPropertyValue( ALERTSTYLE, uno::makeAny( sheet::ValidationAlertStyle_STOP) );
     218           2 :     xProps->setPropertyValue( STYPE, uno::makeAny( sheet::ValidationType_ANY ) );
     219           2 :     xCond->setFormula1( sBlank );
     220           2 :     xCond->setFormula2( sBlank );
     221           2 :     xCond->setOperator( sheet::ConditionOperator_NONE );
     222             : 
     223           4 :     lcl_setValidationProps( m_xRange, xProps );
     224           2 : }
     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           1 : 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           1 :     uno::Reference< beans::XPropertySet > xProps( lcl_getValidationProps( m_xRange ) );
     231           2 :     uno::Reference< sheet::XSheetCondition > xCond( xProps, uno::UNO_QUERY_THROW );
     232             : 
     233           1 :     sheet::ValidationType nValType = sheet::ValidationType_ANY;
     234           1 :     xProps->getPropertyValue( STYPE )  >>= nValType;
     235           1 :     if ( nValType  != sheet::ValidationType_ANY  )
     236           0 :         throw uno::RuntimeException("validation object already exists", uno::Reference< uno::XInterface >() );
     237           1 :     sal_Int32 nType = -1;
     238           1 :     if ( !Type.hasValue()  || !( Type >>= nType ) )
     239           0 :         throw uno::RuntimeException("missing required param", uno::Reference< uno::XInterface >() );
     240             : 
     241           1 :     Delete(); // set up defaults
     242           2 :     OUString sFormula1;
     243           1 :     Formula1 >>= sFormula1;
     244           2 :     OUString sFormula2;
     245           1 :     Formula2 >>= sFormula2;
     246           1 :     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           1 :             nValType = sheet::ValidationType_WHOLE;
     262           1 :             xProps->setPropertyValue( STYPE, uno::makeAny(nValType ));
     263           1 :             break;
     264             :         default:
     265           0 :             throw uno::RuntimeException("unsupported operation...", uno::Reference< uno::XInterface >() );
     266             :     }
     267             : 
     268           1 :     sheet::ValidationAlertStyle eStyle = sheet::ValidationAlertStyle_STOP;
     269           1 :     sal_Int32 nVbaAlertStyle = excel::XlDVAlertStyle::xlValidAlertStop;
     270           1 :     if ( AlertStyle.hasValue() && ( AlertStyle >>= nVbaAlertStyle ) )
     271             :     {
     272           1 :         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           1 :                 eStyle = sheet::ValidationAlertStyle_STOP;
     278           1 :                 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           1 :     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           1 :     if ( Operator.hasValue() )
     296             :     {
     297           1 :         css::sheet::ConditionOperator conOperator = ScVbaFormatCondition::retrieveAPIOperator( Operator );
     298           1 :         xCond->setOperator( conOperator );
     299             :     }
     300             : 
     301           1 :     if ( !sFormula1.isEmpty() )
     302           1 :         xCond->setFormula1( sFormula1 );
     303           1 :     if ( !sFormula2.isEmpty() )
     304           1 :         xCond->setFormula2( sFormula2 );
     305             : 
     306           2 :     lcl_setValidationProps( m_xRange, xProps );
     307           1 : }
     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           6 : }
     397             : 
     398             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10