LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/svx/source/table - tablertfimporter.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 174 0.6 %
Date: 2013-07-09 Functions: 2 19 10.5 %
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             : 
      21             : #include <vector>
      22             : #include <boost/shared_ptr.hpp>
      23             : 
      24             : #include <com/sun/star/table/XTable.hpp>
      25             : 
      26             : #include <tools/stream.hxx>
      27             : #include <svtools/rtftoken.h>
      28             : 
      29             : #include <editeng/eeitem.hxx>
      30             : #include <svx/svdetc.hxx>
      31             : #include <editeng/fhgtitem.hxx>
      32             : #include <editeng/outlobj.hxx>
      33             : 
      34             : #include "cell.hxx"
      35             : #include "celltypes.hxx"
      36             : #include "svx/svdotable.hxx"
      37             : #include "svx/svdoutl.hxx"
      38             : #include "editeng/editeng.hxx"
      39             : #include "editeng/editdata.hxx"
      40             : #include "svx/svdmodel.hxx"
      41             : #include "editeng/svxrtf.hxx"
      42             : 
      43             : using namespace ::com::sun::star::uno;
      44             : using namespace ::com::sun::star::table;
      45             : using namespace ::com::sun::star::container;
      46             : using namespace ::com::sun::star::beans;
      47             : 
      48             : namespace sdr { namespace table {
      49             : 
      50           0 : struct RTFCellDefault
      51             : {
      52             :     SfxItemSet          maItemSet;
      53             :     sal_Int32           mnCol;
      54             :     sal_uInt16              mnTwips;         // right border of the cell
      55             :     sal_Int32           mnColSpan;   // MergeCell if >1, merged cells if 0
      56             : 
      57           0 :     RTFCellDefault( SfxItemPool* pPool ) : maItemSet( *pPool ), mnCol(0), mnTwips(0 ), mnColSpan(1) {}
      58             : };
      59             : 
      60             : typedef std::vector< boost::shared_ptr< RTFCellDefault > > RTFCellDefaultVector;
      61             : 
      62           0 : struct RTFCellInfo
      63             : {
      64             :     SfxItemSet          maItemSet;
      65             :     sal_Int32           mnStartPara;
      66             :     sal_Int32           mnParaCount;
      67             :     sal_Int32           mnColSpan;
      68             : 
      69           0 :     RTFCellInfo( SfxItemPool& rPool ) : maItemSet(  rPool ), mnStartPara(0), mnParaCount(0), mnColSpan(0) {}
      70             : };
      71             : 
      72             : typedef boost::shared_ptr< RTFCellInfo > RTFCellInfoPtr;
      73             : typedef std::vector< RTFCellInfoPtr > RTFColumnVector;
      74             : 
      75             : typedef boost::shared_ptr< RTFColumnVector > RTFColumnVectorPtr;
      76             : 
      77             : typedef std::vector< RTFColumnVectorPtr > RTFRowVector;
      78             : 
      79             : class SdrTableRTFParser
      80             : {
      81             : public:
      82             :     SdrTableRTFParser( SdrTableObj& rTableObj );
      83             :     ~SdrTableRTFParser();
      84             : 
      85             :     void Read( SvStream& rStream );
      86             : 
      87             :     void ProcToken( ImportInfo* pInfo );
      88             : 
      89             :     void NextRow();
      90             :     void NextColumn();
      91             :     void NewCellRow();
      92             : 
      93             :     void InsertCell( ImportInfo* pInfo );
      94             : 
      95             :     void FillTable();
      96             : 
      97             :     DECL_LINK( RTFImportHdl, ImportInfo* );
      98             : 
      99             : private:
     100             :     SdrTableObj&    mrTableObj;
     101             :     SdrOutliner*    mpOutliner;
     102             :     SfxItemPool&    mrItemPool;
     103             : 
     104             :     RTFCellDefaultVector maDefaultList;
     105             :     RTFCellDefaultVector::iterator maDefaultIterator;
     106             : 
     107             :     int             mnLastToken;
     108             :     bool            mbNewDef;
     109             : 
     110             :     sal_Int32       mnStartPara;
     111             : 
     112             :     sal_Int32       mnColCnt;
     113             :     sal_Int32       mnRowCnt;
     114             :     sal_Int32       mnColMax;
     115             : 
     116             :     std::vector< sal_Int32 > maColumnEdges;
     117             : 
     118             :     RTFRowVector    maRows;
     119             : 
     120             :     RTFCellDefault* mpInsDefault;
     121             :     RTFCellDefault* mpActDefault;
     122             :     RTFCellDefault* mpDefMerge;
     123             : 
     124             :     Reference< XTable > mxTable;
     125             : };
     126             : 
     127           0 : SdrTableRTFParser::SdrTableRTFParser( SdrTableObj& rTableObj )
     128             : : mrTableObj( rTableObj )
     129           0 : , mpOutliner( SdrMakeOutliner( OUTLINERMODE_TEXTOBJECT, rTableObj.GetModel() ) )
     130           0 : , mrItemPool( rTableObj.GetModel()->GetItemPool() )
     131             : , mnLastToken( 0 )
     132             : , mbNewDef( false )
     133             : , mnStartPara( 0 )
     134             : , mnColCnt( 0 )
     135             : , mnRowCnt( 0 )
     136             : , mnColMax( 0 )
     137             : , mpActDefault( 0 )
     138             : , mpDefMerge( 0 )
     139           0 : , mxTable( rTableObj.getTable() )
     140             : {
     141           0 :     mpOutliner->SetUpdateMode(sal_True);
     142           0 :     mpOutliner->SetStyleSheet( 0, mrTableObj.GetStyleSheet() );
     143           0 :     mpInsDefault = new RTFCellDefault( &mrItemPool );
     144           0 : }
     145             : 
     146           0 : SdrTableRTFParser::~SdrTableRTFParser()
     147             : {
     148           0 :     delete mpOutliner;
     149           0 :     delete mpInsDefault;
     150           0 : }
     151             : 
     152           0 : void SdrTableRTFParser::Read( SvStream& rStream )
     153             : {
     154           0 :     EditEngine& rEdit = const_cast< EditEngine& >( mpOutliner->GetEditEngine() );
     155             : 
     156           0 :     Link aOldLink( rEdit.GetImportHdl() );
     157           0 :     rEdit.SetImportHdl( LINK( this, SdrTableRTFParser, RTFImportHdl ) );
     158           0 :     mpOutliner->Read( rStream, String(), EE_FORMAT_RTF );
     159           0 :     rEdit.SetImportHdl( aOldLink );
     160             : 
     161           0 :     FillTable();
     162           0 : }
     163             : 
     164           0 : IMPL_LINK( SdrTableRTFParser, RTFImportHdl, ImportInfo*, pInfo )
     165             : {
     166           0 :     switch ( pInfo->eState )
     167             :     {
     168             :         case RTFIMP_NEXTTOKEN:
     169           0 :             ProcToken( pInfo );
     170           0 :             break;
     171             :         case RTFIMP_UNKNOWNATTR:
     172           0 :             ProcToken( pInfo );
     173           0 :             break;
     174             :         case RTFIMP_START:
     175             :         {
     176           0 :             SvxRTFParser* pParser = (SvxRTFParser*) pInfo->pParser;
     177           0 :             pParser->SetAttrPool( &mrItemPool );
     178           0 :             RTFPardAttrMapIds& rMap = pParser->GetPardMap();
     179           0 :             rMap.nBox = SDRATTR_TABLE_BORDER;
     180             :         }
     181           0 :             break;
     182             :         case RTFIMP_END:
     183           0 :             if ( pInfo->aSelection.nEndPos )
     184             :             {
     185           0 :                 mpActDefault = NULL;
     186           0 :                 pInfo->nToken = RTF_PAR;
     187           0 :                 pInfo->aSelection.nEndPara++;
     188           0 :                 ProcToken( pInfo );
     189             :             }
     190           0 :             break;
     191             :         case RTFIMP_SETATTR:
     192           0 :             break;
     193             :         case RTFIMP_INSERTTEXT:
     194           0 :             break;
     195             :         case RTFIMP_INSERTPARA:
     196           0 :             break;
     197             :         default:
     198             :             SAL_WARN( "svx.table","unknown ImportInfo.eState");
     199             :     }
     200           0 :     return 0;
     201             : }
     202             : 
     203           0 : void SdrTableRTFParser::NextRow()
     204             : {
     205           0 :     ++mnRowCnt;
     206           0 : }
     207             : 
     208           0 : void SdrTableRTFParser::InsertCell( ImportInfo* pInfo )
     209             : {
     210           0 :     sal_Int32 nCol = mpActDefault->mnCol;
     211             : 
     212           0 :     RTFCellInfoPtr xCellInfo( new RTFCellInfo(mrItemPool) );
     213             : 
     214           0 :     xCellInfo->mnStartPara = mnStartPara;
     215           0 :     xCellInfo->mnParaCount = pInfo->aSelection.nEndPara - 1 - mnStartPara;
     216             : 
     217           0 :     if( !maRows.empty() )
     218             :     {
     219           0 :         RTFColumnVectorPtr xColumn( maRows.back() );
     220             : 
     221           0 :         if( xColumn->size() <= (size_t)nCol )
     222           0 :             xColumn->resize( nCol+1 );
     223             : 
     224           0 :         (*xColumn)[nCol] = xCellInfo;
     225             :     }
     226             : 
     227           0 :     mnStartPara = pInfo->aSelection.nEndPara - 1;
     228           0 : }
     229             : 
     230           0 : void SdrTableRTFParser::FillTable()
     231             : {
     232             :     try
     233             :     {
     234           0 :         sal_Int32 nColCount = mxTable->getColumnCount();
     235           0 :         Reference< XTableColumns > xCols( mxTable->getColumns(), UNO_QUERY_THROW );
     236             : 
     237           0 :         if( nColCount < mnColMax )
     238             :         {
     239           0 :             xCols->insertByIndex( nColCount, mnColMax - nColCount );
     240           0 :             nColCount = mxTable->getColumnCount();
     241             :         }
     242             : 
     243           0 :         const OUString sWidth("Width");
     244           0 :         sal_Int32 nCol, nLastEdge = 0;
     245           0 :         for( nCol = 0; nCol < nColCount; nCol++ )
     246             :         {
     247           0 :             Reference< XPropertySet > xSet( xCols->getByIndex( nCol ), UNO_QUERY_THROW );
     248           0 :             sal_Int32 nWidth = maColumnEdges[nCol] - nLastEdge;
     249             : 
     250           0 :             xSet->setPropertyValue( sWidth, Any( nWidth ) );
     251           0 :             nLastEdge += nWidth;
     252           0 :         }
     253             : 
     254           0 :         const sal_Int32 nRowCount = mxTable->getRowCount();
     255           0 :         if( nRowCount < mnRowCnt )
     256             :         {
     257           0 :             Reference< XTableRows > xRows( mxTable->getRows(), UNO_QUERY_THROW );
     258           0 :             xRows->insertByIndex( nRowCount, mnRowCnt - nRowCount );
     259             :         }
     260             : 
     261           0 :         for( sal_Int32 nRow = 0; nRow < (sal_Int32)maRows.size(); nRow++ )
     262             :         {
     263           0 :             RTFColumnVectorPtr xColumn( maRows[nRow] );
     264           0 :             for( nCol = 0; nCol < (sal_Int32)xColumn->size(); nCol++ )
     265             :             {
     266           0 :                 RTFCellInfoPtr xCellInfo( (*xColumn)[nCol] );
     267             : 
     268           0 :                 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
     269           0 :                 if( xCell.is() && xCellInfo.get() )
     270             :                 {
     271           0 :                     const SfxPoolItem *pPoolItem = 0;
     272           0 :                     if( xCellInfo->maItemSet.GetItemState(SDRATTR_TABLE_BORDER,sal_False,&pPoolItem)==SFX_ITEM_SET)
     273           0 :                         xCell->SetMergedItem( *pPoolItem );
     274             : 
     275           0 :                     OutlinerParaObject* pTextObject = mpOutliner->CreateParaObject( xCellInfo->mnStartPara, xCellInfo->mnParaCount );
     276           0 :                     if( pTextObject )
     277             :                     {
     278           0 :                         SdrOutliner& rOutliner=mrTableObj.ImpGetDrawOutliner();
     279           0 :                         rOutliner.SetUpdateMode(sal_True);
     280           0 :                         rOutliner.SetText( *pTextObject );
     281           0 :                         mrTableObj.NbcSetOutlinerParaObjectForText( rOutliner.CreateParaObject(), xCell.get() );
     282           0 :                         delete pTextObject;
     283             :                     }
     284             :                 }
     285           0 :             }
     286           0 :         }
     287             : 
     288           0 :         Rectangle aRect( mrTableObj.GetSnapRect() );
     289           0 :         aRect.Right() = aRect.Left() + nLastEdge;
     290           0 :         mrTableObj.NbcSetSnapRect( aRect );
     291             : 
     292             :     }
     293           0 :     catch( Exception& e )
     294             :     {
     295             :         (void)e;
     296             :         OSL_FAIL("sdr::table::SdrTableRTFParser::InsertCell(), exception caught!" );
     297             :     }
     298           0 : }
     299             : 
     300           0 : void SdrTableRTFParser::NewCellRow()
     301             : {
     302           0 :     if( mbNewDef )
     303             :     {
     304           0 :         mbNewDef = sal_False;
     305             : 
     306           0 :         maRows.push_back( RTFColumnVectorPtr( new RTFColumnVector() ) );
     307             :     }
     308           0 :     mpDefMerge = NULL;
     309           0 :     maDefaultIterator = maDefaultList.begin();
     310             : 
     311           0 :     NextColumn();
     312             : 
     313             :     DBG_ASSERT( mpActDefault, "NewCellRow: pActDefault==0" );
     314           0 : }
     315             : 
     316           0 : void SdrTableRTFParser::NextColumn()
     317             : {
     318           0 :     if( maDefaultIterator != maDefaultList.end() )
     319           0 :         mpActDefault = (*maDefaultIterator++).get();
     320             :     else
     321           0 :         mpActDefault = 0;
     322           0 : }
     323             : 
     324           0 : long TwipsToHundMM( long nIn )
     325             : {
     326           0 :     long nRet = OutputDevice::LogicToLogic( nIn, MAP_TWIP, MAP_100TH_MM );
     327           0 :     return nRet;
     328             : }
     329             : 
     330           0 : void SdrTableRTFParser::ProcToken( ImportInfo* pInfo )
     331             : {
     332           0 :     switch ( pInfo->nToken )
     333             :     {
     334             :         case RTF_TROWD:         // denotes table row defauls, before RTF_CELLX
     335             :         {
     336           0 :             mnColCnt = 0;
     337           0 :             maDefaultList.clear();
     338           0 :             mpDefMerge = NULL;
     339           0 :             mnLastToken = pInfo->nToken;
     340             :         }
     341           0 :         break;
     342             :         case RTF_CLMGF:         // The first cell of cells to be merged
     343             :         {
     344           0 :             mpDefMerge = mpInsDefault;
     345           0 :             mnLastToken = pInfo->nToken;
     346             :         }
     347           0 :         break;
     348             :         case RTF_CLMRG:         // A cell to be merged with the preceding cell
     349             :         {
     350           0 :             if ( !mpDefMerge )
     351           0 :                 mpDefMerge = maDefaultList.back().get();
     352             :             DBG_ASSERT( mpDefMerge, "RTF_CLMRG: pDefMerge==0" );
     353           0 :             if( mpDefMerge )
     354           0 :                 mpDefMerge->mnColSpan++;
     355           0 :             mpInsDefault->mnColSpan = 0;
     356           0 :             mnLastToken = pInfo->nToken;
     357             :         }
     358           0 :         break;
     359             :         case RTF_CELLX:         // closes cell default
     360             :         {
     361           0 :             mbNewDef = sal_True;
     362           0 :             mpInsDefault->mnCol = mnColCnt;
     363           0 :             maDefaultList.push_back( boost::shared_ptr< RTFCellDefault >( mpInsDefault ) );
     364             : 
     365           0 :             if( (sal_Int32)maColumnEdges.size() <= mnColCnt )
     366           0 :                 maColumnEdges.resize( mnColCnt + 1 );
     367             : 
     368           0 :             const sal_Int32 nSize = TwipsToHundMM( pInfo->nTokenValue );
     369           0 :             maColumnEdges[mnColCnt] = std::max( maColumnEdges[mnColCnt], nSize );
     370             : 
     371           0 :             mpInsDefault = new RTFCellDefault( &mrItemPool );
     372           0 :             if ( ++mnColCnt > mnColMax )
     373           0 :                 mnColMax = mnColCnt;
     374           0 :             mnLastToken = pInfo->nToken;
     375             :         }
     376           0 :         break;
     377             :         case RTF_INTBL:         // before the first RTF_CELL
     378             :         {
     379           0 :             if ( mnLastToken != RTF_INTBL && mnLastToken != RTF_CELL && mnLastToken != RTF_PAR )
     380             :             {
     381           0 :                 NewCellRow();
     382           0 :                 mnLastToken = pInfo->nToken;
     383             :             }
     384             :         }
     385           0 :         break;
     386             :         case RTF_CELL:          // denotes the end of a cell.
     387             :         {
     388             :             DBG_ASSERT( mpActDefault, "RTF_CELL: pActDefault==0" );
     389           0 :             if ( mbNewDef || !mpActDefault )
     390           0 :                 NewCellRow();
     391           0 :             if ( !mpActDefault )
     392           0 :                 mpActDefault = mpInsDefault;
     393           0 :             if ( mpActDefault->mnColSpan > 0 )
     394             :             {
     395           0 :                 InsertCell(pInfo);
     396             :             }
     397           0 :             NextColumn();
     398           0 :             mnLastToken = pInfo->nToken;
     399             :         }
     400           0 :         break;
     401             :         case RTF_ROW:           // means the end of a row
     402             :         {
     403           0 :             NextRow();
     404           0 :             mnLastToken = pInfo->nToken;
     405             :         }
     406           0 :         break;
     407             :         case RTF_PAR:           // Paragraph
     408           0 :             mnLastToken = pInfo->nToken;
     409           0 :             break;
     410             :         default:
     411             :         {   // do not set nLastToken
     412           0 :             switch ( pInfo->nToken & ~(0xff | RTF_TABLEDEF) )
     413             :             {
     414             :                 case RTF_SHADINGDEF:
     415             : //                  ((SvxRTFParser*)pInfo->pParser)->ReadBackgroundAttr(pInfo->nToken, mpInsDefault->maItemSet, sal_True );
     416           0 :                 break;
     417             :                 case RTF_BRDRDEF:
     418           0 :                     ((SvxRTFParser*)pInfo->pParser)->ReadBorderAttr(pInfo->nToken, mpInsDefault->maItemSet, sal_True );
     419           0 :                 break;
     420             :             }
     421             :         }
     422             :     }
     423           0 : }
     424             : 
     425           0 : void SdrTableObj::ImportAsRTF( SvStream& rStream, SdrTableObj& rObj )
     426             : {
     427           0 :     SdrTableRTFParser aParser( rObj );
     428           0 :     aParser.Read( rStream );
     429           0 : }
     430             : 
     431         216 : } }
     432             : 
     433             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10