LCOV - code coverage report
Current view: top level - vbahelper/source/msforms - vbalistbox.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 64 144 44.4 %
Date: 2014-11-03 Functions: 12 23 52.2 %
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 "vbalistbox.hxx"
      21             : #include "vbanewfont.hxx"
      22             : #include <comphelper/anytostring.hxx>
      23             : #include <com/sun/star/script/ArrayWrapper.hpp>
      24             : #include <com/sun/star/form/validation/XValidatableFormComponent.hpp>
      25             : #include <ooo/vba/msforms/fmMultiSelect.hpp>
      26             : 
      27             : using namespace com::sun::star;
      28             : using namespace ooo::vba;
      29             : 
      30          46 : ScVbaListBox::ScVbaListBox( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< css::uno::XInterface >& xControl, const uno::Reference< frame::XModel >& xModel, AbstractGeometryAttributes* pGeomHelper )
      31             :     : ListBoxImpl_BASE(xParent, xContext, xControl, xModel, pGeomHelper)
      32          46 :     , m_nIndex(0)
      33             : {
      34          46 :     mpListHelper.reset( new ListControlHelper( m_xProps ) );
      35          46 : }
      36             : 
      37             : // Attributes
      38             : void SAL_CALL
      39           0 : ScVbaListBox::setListIndex( const uno::Any& _value ) throw (uno::RuntimeException, std::exception)
      40             : {
      41           0 :     sal_Int32 nIndex = 0;
      42           0 :     _value >>= nIndex;
      43           0 :     uno::Reference< XPropValue > xPropVal( Selected( nIndex ), uno::UNO_QUERY_THROW );
      44           0 :     xPropVal->setValue( uno::makeAny( sal_True ) );
      45           0 : }
      46             : 
      47             : uno::Any SAL_CALL
      48           0 : ScVbaListBox::getListIndex() throw (uno::RuntimeException, std::exception)
      49             : {
      50           0 :     uno::Sequence< sal_Int16 > sSelection;
      51           0 :     m_xProps->getPropertyValue( "SelectedItems" ) >>= sSelection;
      52           0 :     if ( sSelection.getLength() == 0 )
      53           0 :         return uno::Any( sal_Int32( -1 ) );
      54           0 :     return uno::Any( sSelection[ 0 ] );
      55             : }
      56             : 
      57             : uno::Any SAL_CALL
      58           4 : ScVbaListBox::getValue() throw (uno::RuntimeException, std::exception)
      59             : {
      60           4 :     uno::Sequence< sal_Int16 > sSelection;
      61           8 :     uno::Sequence< OUString > sItems;
      62           4 :     m_xProps->getPropertyValue( "SelectedItems" ) >>= sSelection;
      63           4 :     m_xProps->getPropertyValue( "StringItemList" ) >>= sItems;
      64           4 :     if( getMultiSelect() )
      65           0 :         throw uno::RuntimeException( "Attribute use invalid." );
      66           4 :     uno::Any aRet;
      67           4 :     if ( sSelection.getLength() )
      68           4 :         aRet = uno::makeAny( sItems[ sSelection[ 0 ] ] );
      69           8 :     return aRet;
      70             : }
      71             : 
      72             : void SAL_CALL
      73           4 : ScVbaListBox::setValue( const uno::Any& _value ) throw (uno::RuntimeException, std::exception)
      74             : {
      75           4 :     if( getMultiSelect() )
      76             :     {
      77           0 :         throw uno::RuntimeException( "Attribute use invalid." );
      78             :     }
      79           4 :     OUString sValue = getAnyAsString( _value );
      80           8 :     uno::Sequence< OUString > sList;
      81           4 :     m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
      82           4 :     sal_Int16 nLength = static_cast<sal_Int16>( sList.getLength() );
      83           4 :     sal_Int16 nValue = -1;
      84           4 :     sal_Int16 i = 0;
      85          12 :     for( i = 0; i < nLength; i++ )
      86             :     {
      87          12 :         if( sList[i].equals( sValue ) )
      88             :         {
      89           4 :             nValue = i;
      90           4 :             break;
      91             :         }
      92             :     }
      93           4 :     if( nValue == -1 )
      94           0 :         throw uno::RuntimeException( "Attribute use invalid." );
      95             : 
      96           8 :     uno::Sequence< sal_Int16 > nSelectedIndices(1);
      97           8 :     uno::Sequence< sal_Int16 > nOldSelectedIndices;
      98           4 :     m_xProps->getPropertyValue( "SelectedItems" ) >>= nOldSelectedIndices;
      99           4 :     nSelectedIndices[ 0 ] = nValue;
     100           4 :     m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nSelectedIndices ) );
     101           4 :     if ( nSelectedIndices != nOldSelectedIndices )
     102           8 :         fireClickEvent();
     103           4 : }
     104             : 
     105             : OUString SAL_CALL
     106           4 : ScVbaListBox::getText() throw (uno::RuntimeException, std::exception)
     107             : {
     108           4 :     OUString result;
     109           4 :     getValue() >>= result;
     110           4 :     return result;
     111             : }
     112             : 
     113             : void SAL_CALL
     114           4 : ScVbaListBox::setText( const OUString& _text ) throw (uno::RuntimeException, std::exception)
     115             : {
     116           4 :     setValue( uno::makeAny( _text ) ); // seems the same
     117           4 : }
     118             : 
     119             : sal_Int32 SAL_CALL
     120          16 : ScVbaListBox::getMultiSelect() throw (css::uno::RuntimeException, std::exception)
     121             : {
     122          16 :     bool bMultiSelect = false;
     123          16 :     m_xProps->getPropertyValue( "MultiSelection" ) >>= bMultiSelect;
     124             : 
     125          16 :     return bMultiSelect ? msforms::fmMultiSelect::fmMultiSelectMulti : msforms::fmMultiSelect::fmMultiSelectSingle;
     126             : }
     127             : 
     128             : void SAL_CALL
     129           8 : ScVbaListBox::setMultiSelect( sal_Int32 _multiselect ) throw (css::uno::RuntimeException, std::exception)
     130             : {
     131           8 :     bool bBoolVal = false;
     132           8 :     switch ( _multiselect )
     133             :     {
     134             :         case  msforms::fmMultiSelect::fmMultiSelectMulti:
     135             :         case  msforms::fmMultiSelect::fmMultiSelectExtended:
     136           4 :             bBoolVal = true;
     137           4 :             break;
     138             :         case msforms::fmMultiSelect::fmMultiSelectSingle:
     139           4 :             bBoolVal = false;
     140           4 :             break;
     141             :         default:
     142           0 :             throw lang::IllegalArgumentException();
     143             :             break;
     144             :     }
     145           8 :     m_xProps->setPropertyValue( "MultiSelection" , uno::makeAny( bBoolVal ) );
     146           8 : }
     147             : 
     148             : 
     149             : css::uno::Any SAL_CALL
     150           0 : ScVbaListBox::Selected( sal_Int32 index ) throw (css::uno::RuntimeException, std::exception)
     151             : {
     152           0 :     uno::Sequence< OUString > sList;
     153           0 :     m_xProps->getPropertyValue( "StringItemList" ) >>= sList;
     154           0 :     sal_Int16 nLength = static_cast< sal_Int16 >( sList.getLength() );
     155             :     // no choice but to do a horror cast as internally
     156             :     // the indices are but sal_Int16
     157           0 :     sal_Int16 nIndex = static_cast< sal_Int16 >( index );
     158           0 :     if( nIndex < 0 || nIndex >= nLength )
     159           0 :         throw uno::RuntimeException( "Error Number." );
     160           0 :     m_nIndex = nIndex;
     161           0 :     return uno::makeAny( uno::Reference< XPropValue > ( new ScVbaPropValue( this ) ) );
     162             : }
     163             : 
     164             : // Methods
     165             : void SAL_CALL
     166          12 : ScVbaListBox::AddItem( const uno::Any& pvargItem, const uno::Any& pvargIndex ) throw (uno::RuntimeException, std::exception)
     167             : {
     168          12 :     mpListHelper->AddItem( pvargItem, pvargIndex );
     169          12 :         }
     170             : 
     171             : void SAL_CALL
     172           0 : ScVbaListBox::removeItem( const uno::Any& index ) throw (uno::RuntimeException, std::exception)
     173             : {
     174           0 :     mpListHelper->removeItem( index );
     175           0 : }
     176             : 
     177             : void SAL_CALL
     178           4 : ScVbaListBox::Clear(  ) throw (uno::RuntimeException, std::exception)
     179             : {
     180           4 :     mpListHelper->Clear();
     181           4 : }
     182             : 
     183             : // this is called when something like the following vba code is used
     184             : // to set the selected state of particular entries in the Listbox
     185             : // ListBox1.Selected( 3 ) = false
     186             : //PropListener
     187             : void
     188           0 : ScVbaListBox::setValueEvent( const uno::Any& value )
     189             : {
     190           0 :     bool bValue = false;
     191           0 :     if( !(value >>= bValue) )
     192           0 :         throw uno::RuntimeException( "Invalid type. need boolean." );
     193           0 :     uno::Sequence< sal_Int16 > nList;
     194           0 :     m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
     195           0 :     sal_Int16 nLength = static_cast<sal_Int16>( nList.getLength() );
     196           0 :     sal_Int16 nIndex = m_nIndex;
     197           0 :     for( sal_Int16 i = 0; i < nLength; i++ )
     198             :     {
     199           0 :         if( nList[i] == nIndex )
     200             :         {
     201           0 :             if( bValue )
     202           0 :                 return;
     203             :             else
     204             :             {
     205           0 :                 for( ; i < nLength - 1; i++ )
     206             :                 {
     207           0 :                     nList[i] = nList[i + 1];
     208             :                 }
     209           0 :                 nList.realloc( nLength - 1 );
     210             :                 //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
     211           0 :                 fireClickEvent();
     212           0 :         m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
     213           0 :                 return;
     214             :             }
     215             :         }
     216             :     }
     217           0 :     if( bValue )
     218             :     {
     219           0 :         if( getMultiSelect() )
     220             :         {
     221           0 :             nList.realloc( nLength + 1 );
     222           0 :             nList[nLength] = nIndex;
     223             :         }
     224             :         else
     225             :         {
     226           0 :             nList.realloc( 1 );
     227           0 :             nList[0] = nIndex;
     228             :         }
     229             :         //m_xProps->setPropertyValue( sSourceName, uno::makeAny( nList ) );
     230           0 :         fireClickEvent();
     231           0 :         m_xProps->setPropertyValue( "SelectedItems", uno::makeAny( nList ) );
     232           0 :     }
     233             : }
     234             : 
     235             : // this is called when something like the following vba code is used
     236             : // to determine the selected state of particular entries in the Listbox
     237             : // msgbox ListBox1.Selected( 3 )
     238             : 
     239             : css::uno::Any
     240           0 : ScVbaListBox::getValueEvent()
     241             : {
     242           0 :     uno::Sequence< sal_Int16 > nList;
     243           0 :     m_xProps->getPropertyValue( "SelectedItems" ) >>= nList;
     244           0 :     sal_Int32 nLength = nList.getLength();
     245           0 :     sal_Int32 nIndex = m_nIndex;
     246             : 
     247           0 :     for( sal_Int32 i = 0; i < nLength; i++ )
     248             :     {
     249           0 :         if( nList[i] == nIndex )
     250           0 :             return uno::makeAny( sal_True );
     251             :     }
     252             : 
     253           0 :     return uno::makeAny( sal_False );
     254             : }
     255             : 
     256             : void SAL_CALL
     257           0 : ScVbaListBox::setRowSource( const OUString& _rowsource ) throw (uno::RuntimeException, std::exception)
     258             : {
     259           0 :     ScVbaControl::setRowSource( _rowsource );
     260           0 :     mpListHelper->setRowSource( _rowsource );
     261           0 : }
     262             : 
     263             : sal_Int32 SAL_CALL
     264           6 : ScVbaListBox::getListCount() throw (uno::RuntimeException, std::exception)
     265             : {
     266           6 :     return mpListHelper->getListCount();
     267             : }
     268             : 
     269             : uno::Any SAL_CALL
     270           0 : ScVbaListBox::List( const ::uno::Any& pvargIndex, const uno::Any& pvarColumn ) throw (uno::RuntimeException, std::exception)
     271             : {
     272           0 :     return mpListHelper->List( pvargIndex, pvarColumn );
     273             : }
     274             : 
     275           0 : uno::Reference< msforms::XNewFont > SAL_CALL ScVbaListBox::getFont() throw (uno::RuntimeException, std::exception)
     276             : {
     277           0 :     return new VbaNewFont( m_xProps );
     278             : }
     279             : 
     280             : OUString
     281           0 : ScVbaListBox::getServiceImplName()
     282             : {
     283           0 :     return OUString("ScVbaListBox");
     284             : }
     285             : 
     286             : uno::Sequence< OUString >
     287           0 : ScVbaListBox::getServiceNames()
     288             : {
     289           0 :     static uno::Sequence< OUString > aServiceNames;
     290           0 :     if ( aServiceNames.getLength() == 0 )
     291             :     {
     292           0 :         aServiceNames.realloc( 1 );
     293           0 :         aServiceNames[ 0 ] = "ooo.vba.msforms.ScVbaListBox";
     294             :     }
     295           0 :     return aServiceNames;
     296           6 : }
     297             : 
     298             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10