update dev300-m58
[ooovba.git] / svx / source / table / tablertfexporter.cxx
blob53c557d9727ed23bbe58d09b2dc45622787d3e43
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: tablertfexporter.cxx,v $
10 * $Revision: 1.3.212.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <vector>
36 #include <com/sun/star/table/XTable.hpp>
37 #include <com/sun/star/beans/XPropertySet.hpp>
39 #include <tools/stream.hxx>
40 #include <svtools/rtfkeywd.hxx>
41 #include <svtools/rtfout.hxx>
43 #include <svx/eeitem.hxx>
44 #include <svx/sdtaitm.hxx>
45 #include <svx/wghtitem.hxx>
46 #include <svx/postitem.hxx>
47 #include <svx/udlnitem.hxx>
49 #include "cell.hxx"
50 #include "celltypes.hxx"
51 #include "svx/svdotable.hxx"
52 #include "svx/svdoutl.hxx"
53 #include "svx/editeng.hxx"
54 #include "svx/outlobj.hxx"
56 //#include <tablertfexporter.hxx>
58 using ::rtl::OUString;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::table;
61 using namespace ::com::sun::star::container;
62 using namespace ::com::sun::star::beans;
64 namespace sdr { namespace table {
66 class SdrTableRtfExporter
68 public:
69 SdrTableRtfExporter( SvStream& rStrmP, SdrTableObj& rObj );
70 ULONG Write();
71 void WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart );
72 void WriteCell( sal_Int32 nCol, sal_Int32 nRow );
74 private:
75 SvStream& mrStrm;
76 SdrTableObj& mrObj;
77 Reference< XTable > mxTable;
78 const OUString msSize;
81 void SdrTableObj::ExportAsRTF( SvStream& rStrm, SdrTableObj& rObj )
83 SdrTableRtfExporter aEx( rStrm, rObj );
84 aEx.Write();
87 SdrTableRtfExporter::SdrTableRtfExporter( SvStream& rStrm, SdrTableObj& rObj )
88 : mrStrm( rStrm )
89 , mrObj( rObj )
90 , mxTable( rObj.getTable() )
91 , msSize( RTL_CONSTASCII_USTRINGPARAM("Size") )
95 long HundMMToTwips( long nIn )
97 long nRet = OutputDevice::LogicToLogic( nIn, MAP_100TH_MM, MAP_TWIP );
98 return nRet;
101 ULONG SdrTableRtfExporter::Write()
103 mrStrm << '{' << OOO_STRING_SVTOOLS_RTF_RTF;
104 mrStrm << OOO_STRING_SVTOOLS_RTF_ANSI << RTFOutFuncs::sNewLine;
106 Reference< XTableColumns > xColumns( mxTable->getColumns() );
107 const sal_Int32 nColCount = xColumns->getCount();
109 std::vector< sal_Int32 > aColumnStart;
110 aColumnStart.reserve( nColCount );
112 // determine right offset of cells
113 sal_Int32 nPos = 0;
114 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ ) try
116 Reference< XPropertySet > xSet( xColumns->getByIndex(nCol), UNO_QUERY_THROW );
117 sal_Int32 nWidth = 0;
118 xSet->getPropertyValue( msSize ) >>= nWidth;
119 nPos += HundMMToTwips( nWidth );
120 aColumnStart.push_back( nPos );
122 catch( Exception& e )
124 (void)e;
125 DBG_ERROR("SdrTableRtfExporter::Write(), exception caught!");
128 // export rows
129 Reference< XTableRows > xRows( mxTable->getRows() );
130 const sal_Int32 nRowCount = xRows->getCount();
132 for( sal_Int32 nRow = 0; nRow < nRowCount; nRow++ ) try
134 Reference< XPropertySet > xRowSet( xRows->getByIndex(nRow), UNO_QUERY_THROW );
135 WriteRow( xRowSet, nRow, aColumnStart );
137 catch( Exception& e )
139 (void)e;
140 DBG_ERROR("SdrTableRtfExporter::Write(), exception caught!");
143 mrStrm << '}' << RTFOutFuncs::sNewLine;
144 return mrStrm.GetError();
147 void SdrTableRtfExporter::WriteRow( const Reference< XPropertySet >& xRowSet, sal_Int32 nRow, const std::vector< sal_Int32 >& aColumnStart )
149 sal_Int32 nRowHeight = 0;
150 xRowSet->getPropertyValue( msSize ) >>= nRowHeight;
152 mrStrm << OOO_STRING_SVTOOLS_RTF_TROWD << OOO_STRING_SVTOOLS_RTF_TRGAPH << "30" << OOO_STRING_SVTOOLS_RTF_TRLEFT << "-30";
153 mrStrm << OOO_STRING_SVTOOLS_RTF_TRRH << ByteString::CreateFromInt32( nRowHeight ).GetBuffer();
155 const sal_Int32 nColCount = mxTable->getColumnCount();
156 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
158 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
160 if( !xCell.is() )
161 continue;
164 const sal_Bool bIsMerged = xCell->isMerged();
165 const sal_Int32 nRowSpan = xCell->getRowSpan();
166 const sal_Int32 nColSpan = xCell->getColumnSpan();
168 const sal_Char* pChar;
170 if( !bIsMerged && ((nRowSpan > 1) || (nColSpan > 1)) )
171 mrStrm << OOO_STRING_SVTOOLS_RTF_CLMGF; // The first cell in a range of table cells to be merged.
173 SdrTextVertAdjust eVAdj = xCell->GetTextVerticalAdjust();
174 switch( eVAdj )
176 case SVX_VER_JUSTIFY_TOP: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALT; break;
177 case SVX_VER_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALC; break;
178 case SVX_VER_JUSTIFY_BOTTOM: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break;
179 case SVX_VER_JUSTIFY_STANDARD: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break; //! Bottom
180 default: pChar = NULL; break;
182 if ( pChar )
183 mrStrm << pChar;
185 mrStrm << OOO_STRING_SVTOOLS_RTF_CELLX << ByteString::CreateFromInt32( aColumnStart[nCol] ).GetBuffer();
186 if ( (nCol & 0x0F) == 0x0F )
187 mrStrm << RTFOutFuncs::sNewLine; // Zeilen nicht zu lang werden lassen
189 mrStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_PLAIN << OOO_STRING_SVTOOLS_RTF_INTBL << RTFOutFuncs::sNewLine;
191 ULONG nStrmPos = mrStrm.Tell();
192 for( sal_Int32 nCol = 0; nCol < nColCount; nCol++ )
194 WriteCell( nCol, nRow );
195 if ( mrStrm.Tell() - nStrmPos > 255 )
197 mrStrm << RTFOutFuncs::sNewLine;
198 nStrmPos = mrStrm.Tell();
201 mrStrm << OOO_STRING_SVTOOLS_RTF_ROW << RTFOutFuncs::sNewLine;
205 void SdrTableRtfExporter::WriteCell( sal_Int32 nCol, sal_Int32 nRow )
207 CellRef xCell( dynamic_cast< Cell* >( mxTable->getCellByPosition( nCol, nRow ).get() ) );
209 if( !xCell.is() || xCell->isMerged() )
211 mrStrm << OOO_STRING_SVTOOLS_RTF_CELL;
212 return ;
215 String aContent;
217 OutlinerParaObject* pParaObj = xCell->GetEditOutlinerParaObject();
218 bool bOwnParaObj = pParaObj != 0;
220 if( pParaObj == 0 )
221 pParaObj = xCell->GetOutlinerParaObject();
223 if(pParaObj)
225 // handle outliner attributes
226 SdrOutliner& rOutliner = mrObj.ImpGetDrawOutliner();
227 rOutliner.SetText(*pParaObj);
229 aContent = rOutliner.GetEditEngine().GetText( LINEEND_LF );
231 rOutliner.Clear();
233 if( bOwnParaObj )
234 delete pParaObj;
237 bool bResetPar, bResetAttr;
238 bResetPar = bResetAttr = FALSE;
240 SdrTextHorzAdjust eHAdj = xCell->GetTextHorizontalAdjust();
242 const SfxItemSet& rCellSet = xCell->GetItemSet();
244 const SvxWeightItem& rWeightItem = (const SvxWeightItem&) rCellSet.Get( EE_CHAR_WEIGHT );
245 const SvxPostureItem& rPostureItem = (const SvxPostureItem&) rCellSet.Get( EE_CHAR_ITALIC );
246 const SvxUnderlineItem& rUnderlineItem = (const SvxUnderlineItem&) rCellSet.Get( EE_CHAR_UNDERLINE );
248 const sal_Char* pChar;
250 switch( eHAdj )
252 case SDRTEXTHORZADJUST_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
253 case SDRTEXTHORZADJUST_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
254 case SDRTEXTHORZADJUST_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
255 case SDRTEXTHORZADJUST_LEFT:
256 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
258 mrStrm << pChar;
260 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
261 { // bold
262 bResetAttr = true;
263 mrStrm << OOO_STRING_SVTOOLS_RTF_B;
265 if ( rPostureItem.GetPosture() != ITALIC_NONE )
266 { // italic
267 bResetAttr = true;
268 mrStrm << OOO_STRING_SVTOOLS_RTF_I;
270 if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
271 { // underline
272 bResetAttr = true;
273 mrStrm << OOO_STRING_SVTOOLS_RTF_UL;
276 mrStrm << ' ';
277 RTFOutFuncs::Out_String( mrStrm, aContent );
278 mrStrm << OOO_STRING_SVTOOLS_RTF_CELL;
280 if ( bResetPar )
281 mrStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_INTBL;
282 if ( bResetAttr )
283 mrStrm << OOO_STRING_SVTOOLS_RTF_PLAIN;