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