Version 4.3.0.0.beta1, tag libreoffice-4.3.0.0.beta1
[LibreOffice.git] / svx / source / table / tablertfexporter.cxx
blob167c6654b5ba0589ff87ece471bdd5c94f6bd62b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <vector>
23 #include <com/sun/star/table/XTable.hpp>
24 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <tools/stream.hxx>
27 #include <svtools/rtfkeywd.hxx>
28 #include <svtools/rtfout.hxx>
30 #include <editeng/eeitem.hxx>
31 #include <svx/sdtaitm.hxx>
32 #include <editeng/wghtitem.hxx>
33 #include <editeng/postitem.hxx>
34 #include <editeng/udlnitem.hxx>
36 #include "cell.hxx"
37 #include "celltypes.hxx"
38 #include "svx/svdotable.hxx"
39 #include "svx/svdoutl.hxx"
40 #include "editeng/editeng.hxx"
41 #include "editeng/outlobj.hxx"
44 using namespace ::com::sun::star::uno;
45 using namespace ::com::sun::star::table;
46 using namespace ::com::sun::star::container;
47 using namespace ::com::sun::star::beans;
49 namespace sdr { namespace table {
51 class SdrTableRtfExporter
53 public:
54 SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
55 sal_uLong Write();
56 void WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart );
57 void WriteCell( sal_Int32 nCol, sal_Int32 nRow );
59 private:
60 SvStream& mrStrm;
61 SdrTableObj& mrObj;
62 Reference< XTable > mxTable;
63 const OUString msSize;
66 void SdrTableObj::ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
68 SdrTableRtfExporter aEx( rStrm, rObj );
69 aEx.Write();
72 SdrTableRtfExporter::SdrTableRtfExporter( SvStream& rStrm, SdrTableObj& rObj )
73 : mrStrm( rStrm )
74 , mrObj( rObj )
75 , mxTable( rObj.getTable() )
76 , msSize( "Size" )
80 long HundMMToTwips( long nIn )
82 long nRet = OutputDevice::LogicToLogic( nIn, MAP_100TH_MM, MAP_TWIP );
83 return nRet;
86 sal_uLong SdrTableRtfExporter::Write()
88 mrStrm.WriteChar( '{' ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_RTF );
89 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ANSI ).WriteCharPtr( SAL_NEWLINE_STRING );
91 Reference< XTableColumns > xColumns( mxTable->getColumns() );
92 const sal_Int32 nColCount = xColumns->getCount();
94 std::vector< sal_Int32 > aColumnStart;
95 aColumnStart.reserve( nColCount );
97 // determine right offset of cells
98 sal_Int32 nPos = 0;
99 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
101 Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
102 sal_Int32 nWidth = 0;
103 xSet->getPropertyValue( msSize ) >>= nWidth;
104 nPos += HundMMToTwips( nWidth );
105 aColumnStart.push_back( nPos );
107 catch( Exception& e )
109 (void)e;
110 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
113 // export rows
114 Reference< XTableRows > xRows( mxTable->getRows() );
115 const sal_Int32 nRowCount = xRows->getCount();
117 for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
119 Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
120 WriteRow( xRowSet, nRow, aColumnStart );
122 catch( Exception& e )
124 (void)e;
125 OSL_FAIL("SdrTableRtfExporter::Write(), exception caught!");
128 mrStrm.WriteChar( '}' ).WriteCharPtr( SAL_NEWLINE_STRING );
129 return mrStrm.GetError();
132 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
134 sal_Int32 nRowHeight = 0;
135 xRowSet->getPropertyValue( msSize ) >>= nRowHeight;
137 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TROWD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRGAPH ).WriteCharPtr( "30" ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRLEFT ).WriteCharPtr( "-30" );
138 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_TRRH ).WriteCharPtr( OString::number(nRowHeight).getStr() );
140 const sal_Int32 nColCount = mxTable->getColumnCount();
141 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
143 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
145 if( !xCell.is() )
146 continue;
148 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELLX ).WriteCharPtr( OString::number(aColumnStart[nCol]).getStr() );
149 if ( (nCol & 0x0F) == 0x0F )
150 mrStrm.WriteCharPtr( SAL_NEWLINE_STRING ); // Zeilen nicht zu lang werden lassen
152 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL ).WriteCharPtr( SAL_NEWLINE_STRING );
154 sal_uLong nStrmPos = mrStrm.Tell();
155 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
157 WriteCell( nCol, nRow );
158 if ( mrStrm.Tell() - nStrmPos > 255 )
160 mrStrm.WriteCharPtr( SAL_NEWLINE_STRING );
161 nStrmPos = mrStrm.Tell();
164 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_ROW ).WriteCharPtr( SAL_NEWLINE_STRING );
168 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
170 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
172 if( !xCell.is() || xCell->isMerged() )
174 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
175 return ;
178 OUString aContent;
180 OutlinerParaObject* pParaObj = xCell->GetEditOutlinerParaObject();
181 bool bOwnParaObj = pParaObj != 0;
183 if( pParaObj == 0 )
184 pParaObj = xCell->GetOutlinerParaObject();
186 if(pParaObj)
188 // handle outliner attributes
189 SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
190 rOutliner.SetText(*pParaObj);
192 aContent = rOutliner.GetEditEngine().GetText( LINEEND_LF );
194 rOutliner.Clear();
196 if( bOwnParaObj )
197 delete pParaObj;
200 bool bResetPar, bResetAttr;
201 bResetPar = bResetAttr = false;
203 SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
205 const SfxItemSet& rCellSet = xCell->GetItemSet();
207 const SvxWeightItem& rWeightItem = (const SvxWeightItem&) rCellSet.Get( EE_CHAR_WEIGHT );
208 const SvxPostureItem& rPostureItem = (const SvxPostureItem&) rCellSet.Get( EE_CHAR_ITALIC );
209 const SvxUnderlineItem& rUnderlineItem = (const SvxUnderlineItem&) rCellSet.Get( EE_CHAR_UNDERLINE );
211 const sal_Char* pChar;
213 switch( eHAdj )
215 case SDRTEXTHORZADJUST_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
216 case SDRTEXTHORZADJUST_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
217 case SDRTEXTHORZADJUST_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
218 case SDRTEXTHORZADJUST_LEFT:
219 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
221 mrStrm.WriteCharPtr( pChar );
223 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
224 { // bold
225 bResetAttr = true;
226 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_B );
228 if ( rPostureItem.GetPosture() != ITALIC_NONE )
229 { // italic
230 bResetAttr = true;
231 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_I );
233 if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
234 { // underline
235 bResetAttr = true;
236 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_UL );
239 mrStrm.WriteChar( ' ' );
240 RTFOutFuncs::Out_String( mrStrm, aContent );
241 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_CELL );
243 if ( bResetPar )
244 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PARD ).WriteCharPtr( OOO_STRING_SVTOOLS_RTF_INTBL );
245 if ( bResetAttr )
246 mrStrm.WriteCharPtr( OOO_STRING_SVTOOLS_RTF_PLAIN );
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */