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

Generated by: LCOV version 1.10