LCOV - code coverage report
Current view: top level - sw/source/ui/vba - vbarows.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 1 181 0.6 %
Date: 2014-11-03 Functions: 2 29 6.9 %
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             : #include "vbarows.hxx"
      20             : #include "vbarow.hxx"
      21             : #include <com/sun/star/text/HoriOrientation.hpp>
      22             : #include <com/sun/star/table/XCellRange.hpp>
      23             : #include <ooo/vba/word/WdRowAlignment.hpp>
      24             : #include <ooo/vba/word/WdConstants.hpp>
      25             : #include <ooo/vba/word/WdRulerStyle.hpp>
      26             : #include "wordvbahelper.hxx"
      27             : #include "vbacolumns.hxx"
      28             : #include "vbatablehelper.hxx"
      29             : 
      30             : using namespace ::ooo::vba;
      31             : using namespace ::com::sun::star;
      32             : 
      33           0 : class RowsEnumWrapper : public EnumerationHelper_BASE
      34             : {
      35             :     uno::WeakReference< XHelperInterface > mxParent;
      36             :     uno::Reference< uno::XComponentContext > mxContext;
      37             :     uno::Reference< text::XTextTable > mxTextTable;
      38             :     uno::Reference< container::XIndexAccess > mxIndexAccess;
      39             :     sal_Int32 nIndex;
      40             : 
      41             : public:
      42           0 :     RowsEnumWrapper( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const uno::Reference< text::XTextTable >& xTextTable ) : mxParent( xParent ), mxContext( xContext ), mxTextTable( xTextTable ), nIndex( 0 )
      43             :     {
      44           0 :         mxIndexAccess.set( mxTextTable->getRows(), uno::UNO_QUERY );
      45           0 :     }
      46           0 :     virtual sal_Bool SAL_CALL hasMoreElements(  ) throw (uno::RuntimeException, std::exception) SAL_OVERRIDE
      47             :     {
      48           0 :         return ( nIndex < mxIndexAccess->getCount() );
      49             :     }
      50             : 
      51           0 :     virtual uno::Any SAL_CALL nextElement(  ) throw (container::NoSuchElementException, lang::WrappedTargetException, uno::RuntimeException, std::exception) SAL_OVERRIDE
      52             :     {
      53           0 :         if( nIndex < mxIndexAccess->getCount() )
      54             :         {
      55           0 :             return uno::makeAny( uno::Reference< word::XRow > ( new SwVbaRow( mxParent, mxContext, mxTextTable, nIndex++ ) ) );
      56             :         }
      57           0 :         throw container::NoSuchElementException();
      58             :     }
      59             : };
      60             : 
      61           0 : SwVbaRows::SwVbaRows( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextTable >& xTextTable, const uno::Reference< table::XTableRows >& xTableRows ) throw (uno::RuntimeException) : SwVbaRows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xTableRows, uno::UNO_QUERY_THROW ) ), mxTextTable( xTextTable ), mxTableRows( xTableRows )
      62             : {
      63           0 :     mnStartRowIndex = 0;
      64           0 :     mnEndRowIndex = m_xIndexAccess->getCount() - 1;
      65           0 : }
      66             : 
      67           0 : SwVbaRows::SwVbaRows( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext > & xContext, const uno::Reference< text::XTextTable >& xTextTable, const uno::Reference< table::XTableRows >& xTableRows, sal_Int32 nStarIndex, sal_Int32 nEndIndex ) throw (uno::RuntimeException) : SwVbaRows_BASE( xParent, xContext, uno::Reference< container::XIndexAccess >( xTableRows, uno::UNO_QUERY_THROW ) ), mxTextTable( xTextTable ), mxTableRows( xTableRows ), mnStartRowIndex( nStarIndex ), mnEndRowIndex( nEndIndex )
      68             : {
      69           0 :     if( mnEndRowIndex < mnStartRowIndex )
      70           0 :         throw uno::RuntimeException();
      71           0 : }
      72             : 
      73             : /**
      74             :  * get the alignment of the rows: SO format com.sun.star.text.HoriOrientation
      75             :  * is mapped to WdRowAlignment in Word
      76             :  * @return the alignment
      77             :  */
      78           0 : ::sal_Int32 SAL_CALL SwVbaRows::getAlignment() throw (uno::RuntimeException, std::exception)
      79             : {
      80           0 :     sal_Int16 nAlignment = text::HoriOrientation::LEFT;
      81           0 :     uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
      82           0 :     xTableProps->getPropertyValue("HoriOrient") >>= nAlignment;
      83           0 :     sal_Int32 nRet = 0;
      84           0 :     switch( nAlignment )
      85             :     {
      86             :         case text::HoriOrientation::CENTER:
      87             :         {
      88           0 :            nRet = word::WdRowAlignment::wdAlignRowCenter;
      89           0 :            break;
      90             :         }
      91             :         case text::HoriOrientation::RIGHT:
      92             :         {
      93           0 :             nRet = word::WdRowAlignment::wdAlignRowRight;
      94           0 :             break;
      95             :         }
      96             :         default:
      97             :         {
      98           0 :             nRet = word::WdRowAlignment::wdAlignRowLeft;
      99             :         }
     100             :     }
     101           0 :     return nRet;
     102             : }
     103             : 
     104           0 : void SAL_CALL SwVbaRows::setAlignment( ::sal_Int32 _alignment ) throw (uno::RuntimeException, std::exception)
     105             : {
     106           0 :     sal_Int16 nAlignment = text::HoriOrientation::LEFT;
     107           0 :     switch( _alignment )
     108             :     {
     109             :         case word::WdRowAlignment::wdAlignRowCenter:
     110             :         {
     111           0 :             nAlignment = text::HoriOrientation::CENTER;
     112           0 :             break;
     113             :         }
     114             :         case word::WdRowAlignment::wdAlignRowRight:
     115             :         {
     116           0 :             nAlignment = text::HoriOrientation::RIGHT;
     117           0 :             break;
     118             :         }
     119             :         default:
     120             :         {
     121           0 :             nAlignment = text::HoriOrientation::LEFT;
     122             :         }
     123             :     }
     124           0 :     uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
     125           0 :     xTableProps->setPropertyValue("HoriOrient", uno::makeAny( nAlignment ) );
     126           0 : }
     127             : 
     128           0 : uno::Any SAL_CALL SwVbaRows::getAllowBreakAcrossPages() throw (uno::RuntimeException, std::exception)
     129             : {
     130           0 :     bool bAllowBreak = false;
     131           0 :     uno::Reference< container::XIndexAccess > xRowsAccess( mxTableRows, uno::UNO_QUERY_THROW );
     132           0 :     for( sal_Int32 index = mnStartRowIndex; index <= mnEndRowIndex; ++index )
     133             :     {
     134           0 :         uno::Reference< beans::XPropertySet > xRowProps( xRowsAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
     135           0 :         bool bSplit = false;
     136           0 :         xRowProps->getPropertyValue("IsSplitAllowed") >>= bSplit;
     137           0 :         if( index == 0 )
     138             :         {
     139           0 :             bAllowBreak = bSplit;
     140             :         }
     141           0 :         if( bSplit != bAllowBreak )
     142             :         {
     143           0 :             sal_Int32 nRet = word::WdConstants::wdUndefined;
     144           0 :             return uno::makeAny( nRet );
     145             :         }
     146           0 :     }
     147           0 :     return uno::makeAny( bAllowBreak );
     148             : }
     149             : 
     150           0 : void SAL_CALL SwVbaRows::setAllowBreakAcrossPages( const uno::Any& _allowbreakacrosspages ) throw (uno::RuntimeException, std::exception)
     151             : {
     152           0 :     bool bAllowBreak = false;
     153           0 :     _allowbreakacrosspages >>= bAllowBreak;
     154           0 :     uno::Reference< container::XIndexAccess > xRowsAccess( mxTableRows, uno::UNO_QUERY_THROW );
     155           0 :     for( sal_Int32 index = mnStartRowIndex; index <= mnEndRowIndex; ++index )
     156             :     {
     157           0 :         uno::Reference< beans::XPropertySet > xRowProps( xRowsAccess->getByIndex( index ), uno::UNO_QUERY_THROW );
     158           0 :         xRowProps->setPropertyValue("IsSplitAllowed", uno::makeAny( bAllowBreak ) );
     159           0 :     }
     160           0 : }
     161             : 
     162           0 : float SAL_CALL SwVbaRows::getSpaceBetweenColumns() throw (uno::RuntimeException, std::exception)
     163             : {
     164             :     // just get the first spacing of the first cell
     165           0 :     uno::Reference< table::XCellRange > xCellRange( mxTextTable, uno::UNO_QUERY_THROW );
     166           0 :     uno::Reference< beans::XPropertySet > xCellProps( xCellRange->getCellByPosition( 0, mnStartRowIndex ), uno::UNO_QUERY_THROW );
     167           0 :     sal_Int32 nLeftBorderDistance = 0;
     168           0 :     sal_Int32 nRightBorderDistance = 0;
     169           0 :     xCellProps->getPropertyValue("LeftBorderDistance") >>= nLeftBorderDistance;
     170           0 :     xCellProps->getPropertyValue("RightBorderDistance") >>= nRightBorderDistance;
     171           0 :     return static_cast< float >( Millimeter::getInPoints( nLeftBorderDistance + nRightBorderDistance ) );
     172             : }
     173             : 
     174           0 : void SAL_CALL SwVbaRows::setSpaceBetweenColumns( float _spacebetweencolumns ) throw (uno::RuntimeException, std::exception)
     175             : {
     176           0 :     sal_Int32 nSpace = Millimeter::getInHundredthsOfOneMillimeter( _spacebetweencolumns ) / 2;
     177           0 :     uno::Reference< container::XIndexAccess > xColumnAccess( mxTextTable->getColumns(), uno::UNO_QUERY_THROW );
     178           0 :     uno::Reference< table::XCellRange > xCellRange( mxTextTable, uno::UNO_QUERY_THROW );
     179           0 :     SwVbaTableHelper aTableHelper( mxTextTable );
     180           0 :     for( sal_Int32 row = mnStartRowIndex; row <= mnEndRowIndex; ++row )
     181             :     {
     182           0 :         sal_Int32 nColumns = aTableHelper.getTabColumnsCount( row );
     183           0 :         for( sal_Int32 column = 0; column < nColumns; ++column )
     184             :         {
     185           0 :             uno::Reference< beans::XPropertySet > xCellProps( xCellRange->getCellByPosition( column, row ), uno::UNO_QUERY_THROW );
     186           0 :             xCellProps->setPropertyValue("LeftBorderDistance", uno::makeAny( nSpace ) );
     187           0 :             xCellProps->setPropertyValue("RightBorderDistance", uno::makeAny( nSpace ) );
     188           0 :         }
     189           0 :     }
     190           0 : }
     191             : 
     192           0 : void SAL_CALL SwVbaRows::Delete(  ) throw (uno::RuntimeException, std::exception)
     193             : {
     194           0 :     mxTableRows->removeByIndex( mnStartRowIndex, getCount() );
     195           0 : }
     196             : 
     197           0 : void SAL_CALL SwVbaRows::SetLeftIndent( float LeftIndent, ::sal_Int32 RulerStyle ) throw (uno::RuntimeException, std::exception)
     198             : {
     199           0 :     uno::Reference< word::XColumns > xColumns( new SwVbaColumns( getParent(), mxContext, mxTextTable, mxTextTable->getColumns() ) );
     200           0 :     sal_Int32 nIndent = (sal_Int32)( LeftIndent );
     201           0 :     switch( RulerStyle )
     202             :     {
     203             :         case word::WdRulerStyle::wdAdjustFirstColumn:
     204             :         {
     205           0 :             setIndentWithAdjustFirstColumn( xColumns, nIndent );
     206           0 :             break;
     207             :         }
     208             :         case word::WdRulerStyle::wdAdjustNone:
     209             :         {
     210           0 :             setIndentWithAdjustNone( nIndent );
     211           0 :             break;
     212             :         }
     213             :         case word::WdRulerStyle::wdAdjustProportional:
     214             :         {
     215           0 :             setIndentWithAdjustProportional( xColumns, nIndent );
     216           0 :             break;
     217             :         }
     218             :         case word::WdRulerStyle::wdAdjustSameWidth:
     219             :         {
     220           0 :             setIndentWithAdjustSameWidth( xColumns, nIndent );
     221           0 :             break;
     222             :         }
     223             :         default:
     224             :         {
     225           0 :             DebugHelper::runtimeexception(SbERR_BAD_ARGUMENT, OUString());
     226             :         }
     227           0 :     }
     228           0 : }
     229             : 
     230           0 : void SwVbaRows::setIndentWithAdjustNone( sal_Int32 indent ) throw (uno::RuntimeException)
     231             : {
     232           0 :     uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
     233           0 :     sal_Int32 nMargin = 0;
     234           0 :     xTableProps->getPropertyValue("LeftMargin") >>= nMargin;
     235           0 :     nMargin += indent;
     236           0 :     xTableProps->setPropertyValue("LeftMargin", uno::makeAny( nMargin ) );
     237           0 : }
     238             : 
     239           0 :  void SwVbaRows::setIndentWithAdjustFirstColumn( const uno::Reference< word::XColumns >& xColumns, sal_Int32 indent ) throw (uno::RuntimeException)
     240             :  {
     241           0 :     sal_Int32 nIndex = 1;
     242           0 :     uno::Reference< XCollection > xCol( xColumns, uno::UNO_QUERY_THROW );
     243           0 :     uno::Reference< word::XColumn > xColumn( xCol->Item( uno::makeAny( nIndex ), uno::Any() ), uno::UNO_QUERY_THROW );
     244           0 :     sal_Int32 nWidth = xColumn->getWidth();
     245           0 :     nWidth -= indent;
     246           0 :     xColumn->setWidth( nWidth );
     247           0 :     setIndentWithAdjustNone( indent );
     248           0 :  }
     249             : 
     250           0 :  void SwVbaRows::setIndentWithAdjustProportional(
     251             :     const uno::Reference< word::XColumns >& xColumns,
     252             :     sal_Int32 indent
     253             : ) throw (uno::RuntimeException)
     254             :  {
     255             :     // calculate the new width and get the proportion between old and new
     256           0 :     uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
     257           0 :     sal_Int32 nWidth = 0;
     258           0 :     xTableProps->getPropertyValue("Width") >>= nWidth;
     259           0 :     sal_Int32 nNewWidth = nWidth - indent;
     260           0 :     if ((nNewWidth <= 0) || (nWidth <= 0))
     261             :     {
     262             :         throw uno::RuntimeException(
     263             :             "Pb with width, in SwVbaRows::setIndentWithAdjustProportional "
     264             :             "(nNewWidth <= 0) || (nWidth <= 0)"
     265           0 :         );
     266             :     }
     267           0 :     double propFactor = (double)nNewWidth/(double)nWidth;
     268             : 
     269             :     // get all columns, calculate and set the new width of the columns
     270           0 :     uno::Reference< XCollection > xCol( xColumns, uno::UNO_QUERY_THROW );
     271           0 :     sal_Int32 nColCount = xCol->getCount();
     272           0 :     for( sal_Int32 i = 0; i < nColCount; i++ )
     273             :     {
     274           0 :         uno::Reference< word::XColumn > xColumn( xCol->Item( uno::makeAny( i ), uno::Any() ), uno::UNO_QUERY_THROW );
     275           0 :         sal_Int32 nColWidth = xColumn->getWidth();
     276           0 :         sal_Int32 nNewColWidth = ( sal_Int32 )( propFactor * nColWidth );
     277           0 :         xColumn->setWidth( nNewColWidth );
     278           0 :     }
     279             : 
     280             :     // set the width and position of the table
     281           0 :     setIndentWithAdjustNone( indent );
     282           0 :     xTableProps->setPropertyValue("Width", uno::makeAny( nNewWidth ) );
     283           0 :  }
     284             : 
     285           0 :  void SwVbaRows::setIndentWithAdjustSameWidth( const uno::Reference< word::XColumns >& xColumns, sal_Int32 indent ) throw (uno::RuntimeException)
     286             :  {
     287             :     // calculate the new width and get the width of all columns
     288           0 :     uno::Reference< beans::XPropertySet > xTableProps( mxTextTable, uno::UNO_QUERY_THROW );
     289           0 :     sal_Int32 nWidth = 0;
     290           0 :     xTableProps->getPropertyValue("Width") >>= nWidth;
     291           0 :     sal_Int32 nNewWidth = nWidth - indent;
     292             : 
     293             :     // get all columns, calculate and set the new width of the columns
     294           0 :     uno::Reference< XCollection > xCol( xColumns, uno::UNO_QUERY_THROW );
     295           0 :     sal_Int32 nColCount = xCol->getCount();
     296           0 :     sal_Int32 nNewColWidth = (sal_Int32)( double( nNewWidth )/nColCount );
     297           0 :     for( sal_Int32 i = 0; i < nColCount; i++ )
     298             :     {
     299           0 :         uno::Reference< word::XColumn > xColumn( xCol->Item( uno::makeAny( i ), uno::Any() ), uno::UNO_QUERY_THROW );
     300           0 :         xColumn->setWidth( nNewColWidth );
     301           0 :     }
     302             : 
     303             :     // set the width and position of the table
     304           0 :     setIndentWithAdjustNone( indent );
     305           0 :     xTableProps->setPropertyValue("Width", uno::makeAny( nNewWidth ) );
     306           0 :  }
     307             : 
     308           0 : void SAL_CALL SwVbaRows::Select(  ) throw (uno::RuntimeException, std::exception)
     309             : {
     310           0 :     SwVbaRow::SelectRow( getCurrentWordDoc(mxContext), mxTextTable, mnStartRowIndex, mnEndRowIndex );
     311           0 : }
     312             : 
     313           0 : ::sal_Int32 SAL_CALL SwVbaRows::getCount() throw (uno::RuntimeException)
     314             : {
     315           0 :     return ( mnEndRowIndex - mnStartRowIndex + 1 );
     316             : }
     317             : 
     318           0 : uno::Any SAL_CALL SwVbaRows::Item( const uno::Any& Index1, const uno::Any& /*not processed in this base class*/ ) throw (lang::IndexOutOfBoundsException, uno::RuntimeException)
     319             : {
     320           0 :     sal_Int32 nIndex = 0;
     321           0 :     if( ( Index1 >>= nIndex ) )
     322             :     {
     323           0 :         if( nIndex <= 0 || nIndex > getCount() )
     324             :         {
     325           0 :             throw  lang::IndexOutOfBoundsException("Index out of bounds" );
     326             :         }
     327           0 :         return uno::makeAny( uno::Reference< word::XRow >( new SwVbaRow( this, mxContext, mxTextTable, nIndex - 1 ) ) );
     328             :     }
     329           0 :     throw  uno::RuntimeException("Index out of bounds" );
     330             : }
     331             : 
     332             : // XEnumerationAccess
     333             : uno::Type
     334           0 : SwVbaRows::getElementType() throw (uno::RuntimeException)
     335             : {
     336           0 :     return cppu::UnoType<word::XRow>::get();
     337             : }
     338             : uno::Reference< container::XEnumeration >
     339           0 : SwVbaRows::createEnumeration() throw (uno::RuntimeException)
     340             : {
     341           0 :     return new RowsEnumWrapper( this, mxContext, mxTextTable );
     342             : }
     343             : 
     344             : uno::Any
     345           0 : SwVbaRows::createCollectionObject( const uno::Any& aSource )
     346             : {
     347           0 :     return aSource;
     348             : }
     349             : 
     350             : OUString
     351           0 : SwVbaRows::getServiceImplName()
     352             : {
     353           0 :     return OUString("SwVbaRows");
     354             : }
     355             : 
     356             : uno::Sequence<OUString>
     357           0 : SwVbaRows::getServiceNames()
     358             : {
     359           0 :     static uno::Sequence< OUString > sNames;
     360           0 :     if ( sNames.getLength() == 0 )
     361             :     {
     362           0 :         sNames.realloc( 1 );
     363           0 :         sNames[0] = "ooo.vba.word.Rows";
     364             :     }
     365           0 :     return sNames;
     366           6 : }
     367             : 
     368             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10