merge the formfield patch from ooo-build
[ooovba.git] / sc / source / filter / rtf / rtfexp.cxx
blobc430126d5f2e4ef446efc63706a4b31c64499b32
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: rtfexp.cxx,v $
10 * $Revision: 1.10.144.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_sc.hxx"
36 #include "scitems.hxx"
37 #include <svx/eeitem.hxx>
40 #include <svx/algitem.hxx>
41 #include <svx/wghtitem.hxx>
42 #include <svx/postitem.hxx>
43 #include <svx/udlnitem.hxx>
44 #include <svx/fontitem.hxx>
45 #include <svx/fhgtitem.hxx>
46 #include <svtools/style.hxx>
47 #include <svtools/rtfout.hxx>
48 #include <svtools/rtfkeywd.hxx>
50 #include "rtfexp.hxx"
51 #include "filter.hxx"
52 #include "document.hxx"
53 #include "patattr.hxx"
54 #include "attrib.hxx"
55 #include "cell.hxx"
56 #include "cellform.hxx"
57 #include "editutil.hxx"
58 #include "stlpool.hxx"
59 #include "ftools.hxx"
61 //------------------------------------------------------------------
63 FltError ScFormatFilterPluginImpl::ScExportRTF( SvStream& rStrm, ScDocument* pDoc,
64 const ScRange& rRange, const CharSet /*eNach*/ )
66 ScRTFExport aEx( rStrm, pDoc, rRange );
67 return aEx.Write();
71 ScRTFExport::ScRTFExport( SvStream& rStrmP, ScDocument* pDocP, const ScRange& rRangeP )
73 ScExportBase( rStrmP, pDocP, rRangeP ),
74 pCellX( new ULONG[ MAXCOL+2 ] )
79 ScRTFExport::~ScRTFExport()
81 delete [] pCellX;
85 ULONG ScRTFExport::Write()
87 rStrm << '{' << OOO_STRING_SVTOOLS_RTF_RTF;
88 rStrm << OOO_STRING_SVTOOLS_RTF_ANSI << sNewLine;
90 #if 0
91 // das ist noch nicht ausgegoren
93 SfxStyleSheetBasePool* pStylePool = pDoc->GetStyleSheetPool();
94 SfxStyleSheetBase* pStyleSheet = NULL;
95 DBG_ASSERT( pStylePool, "StylePool not found! :-(" );
96 pStylePool->SetSearchMask( SFX_STYLE_FAMILY_PARA, SFXSTYLEBIT_ALL );
97 pStyleSheet = pStylePool->Find( STRING_STANDARD, SFX_STYLE_FAMILY_PARA );
98 DBG_ASSERT( pStyleSheet, "ParaStyle not found! :-(" );
99 const SfxItemSet& rSetPara = pStyleSheet->GetItemSet();
101 // fonttbl
102 String aFontFamilyName(
103 ((const SvxFontItem&)(rSetPara.Get( ATTR_FONT ))).GetFamilyName() );
104 rStrm << OOO_STRING_SVTOOLS_RTF_DEFF << '0'
105 << '{' << OOO_STRING_SVTOOLS_RTF_FONTTBL
106 << '{' << OOO_STRING_SVTOOLS_RTF_F << '0' << OOO_STRING_SVTOOLS_RTF_FNIL << ' ' << aFontFamilyName.GetStr() << ";}"
107 << '}' << sNewLine;
109 // hier kaeme die colortbl
111 // stylesheet
112 UINT32 nFontHeight =
113 ((const SvxFontHeightItem&)(rSetPara.Get( ATTR_FONT_HEIGHT ))).GetHeight();
114 rStrm << '{' << OOO_STRING_SVTOOLS_RTF_STYLESHEET
115 << '{' << OOO_STRING_SVTOOLS_RTF_FS << String( UINT32(nFontHeight / TWIPS_PER_POINT) ).GetStr()
116 << ' ' << pStyleSheet->GetName().GetStr() << ";}"
117 << '}' << sNewLine;
119 #endif
121 // Daten
122 for ( SCTAB nTab = aRange.aStart.Tab(); nTab <= aRange.aEnd.Tab(); nTab++ )
124 if ( nTab > aRange.aStart.Tab() )
125 rStrm << OOO_STRING_SVTOOLS_RTF_PAR;
126 WriteTab( nTab );
129 rStrm << '}' << sNewLine;
130 return rStrm.GetError();
134 void ScRTFExport::WriteTab( SCTAB nTab )
136 rStrm << '{' << sNewLine;
137 if ( pDoc->HasTable( nTab ) )
139 memset( &pCellX[0], 0, (MAXCOL+2) * sizeof(ULONG) );
140 SCCOL nCol;
141 SCCOL nEndCol = aRange.aEnd.Col();
142 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
144 pCellX[nCol+1] = pCellX[nCol] + pDoc->GetColWidth( nCol, nTab );
147 SCROW nEndRow = aRange.aEnd.Row();
148 for ( SCROW nRow = aRange.aStart.Row(); nRow <= nEndRow; nRow++ )
150 WriteRow( nTab, nRow );
153 rStrm << '}' << sNewLine;
157 void ScRTFExport::WriteRow( SCTAB nTab, SCROW nRow )
159 rStrm << OOO_STRING_SVTOOLS_RTF_TROWD << OOO_STRING_SVTOOLS_RTF_TRGAPH << "30" << OOO_STRING_SVTOOLS_RTF_TRLEFT << "-30";
160 rStrm << OOO_STRING_SVTOOLS_RTF_TRRH << ByteString::CreateFromInt32( pDoc->GetRowHeight( nRow, nTab ) ).GetBuffer();
161 SCCOL nCol;
162 SCCOL nEndCol = aRange.aEnd.Col();
163 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
165 const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
166 const ScMergeAttr& rMergeAttr = (const ScMergeAttr&) pAttr->GetItem( ATTR_MERGE );
167 const SvxVerJustifyItem& rVerJustifyItem= (const SvxVerJustifyItem&)pAttr->GetItem( ATTR_VER_JUSTIFY );
169 const sal_Char* pChar;
171 if ( rMergeAttr.GetColMerge() != 0 )
172 rStrm << OOO_STRING_SVTOOLS_RTF_CLMGF;
173 else
175 const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
176 if ( rMergeFlagAttr.IsHorOverlapped() )
177 rStrm << OOO_STRING_SVTOOLS_RTF_CLMRG;
180 switch( rVerJustifyItem.GetValue() )
182 case SVX_VER_JUSTIFY_TOP: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALT; break;
183 case SVX_VER_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALC; break;
184 case SVX_VER_JUSTIFY_BOTTOM: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break;
185 case SVX_VER_JUSTIFY_STANDARD: pChar = OOO_STRING_SVTOOLS_RTF_CLVERTALB; break; //! Bottom
186 default: pChar = NULL; break;
188 if ( pChar )
189 rStrm << pChar;
191 rStrm << OOO_STRING_SVTOOLS_RTF_CELLX << ByteString::CreateFromInt32( pCellX[nCol+1] ).GetBuffer();
192 if ( (nCol & 0x0F) == 0x0F )
193 rStrm << sNewLine; // Zeilen nicht zu lang werden lassen
195 rStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_PLAIN << OOO_STRING_SVTOOLS_RTF_INTBL << sNewLine;
197 ULONG nStrmPos = rStrm.Tell();
198 for ( nCol = aRange.aStart.Col(); nCol <= nEndCol; nCol++ )
200 WriteCell( nTab, nRow, nCol );
201 if ( rStrm.Tell() - nStrmPos > 255 )
202 { // Zeilen nicht zu lang werden lassen
203 rStrm << sNewLine;
204 nStrmPos = rStrm.Tell();
207 rStrm << OOO_STRING_SVTOOLS_RTF_ROW << sNewLine;
211 void ScRTFExport::WriteCell( SCTAB nTab, SCROW nRow, SCCOL nCol )
213 const ScPatternAttr* pAttr = pDoc->GetPattern( nCol, nRow, nTab );
215 const ScMergeFlagAttr& rMergeFlagAttr = (const ScMergeFlagAttr&) pAttr->GetItem( ATTR_MERGE_FLAG );
216 if ( rMergeFlagAttr.IsHorOverlapped() )
218 rStrm << OOO_STRING_SVTOOLS_RTF_CELL;
219 return ;
222 ScBaseCell* pCell;
223 pDoc->GetCell( nCol, nRow, nTab, pCell );
224 BOOL bValueData;
225 String aContent;
226 if ( pCell )
228 switch ( pCell->GetCellType() )
230 case CELLTYPE_NOTE :
231 bValueData = FALSE;
232 break; // nix
233 case CELLTYPE_EDIT :
235 bValueData = FALSE;
236 EditEngine& rEngine = GetEditEngine();
237 const EditTextObject* pObj;
238 ((const ScEditCell*)pCell)->GetData( pObj );
239 if ( pObj )
241 rEngine.SetText( *pObj );
242 aContent = rEngine.GetText( LINEEND_LF ); // LineFeed zwischen Absaetzen!
245 break;
246 default:
248 bValueData = pCell->HasValueData();
249 ULONG nFormat = pAttr->GetNumberFormat( pFormatter );
250 Color* pColor;
251 ScCellFormat::GetString( pCell, nFormat, aContent, &pColor, *pFormatter );
255 else
256 bValueData = FALSE;
258 BOOL bResetPar, bResetAttr;
259 bResetPar = bResetAttr = FALSE;
261 const SvxHorJustifyItem& rHorJustifyItem = (const SvxHorJustifyItem&)pAttr->GetItem( ATTR_HOR_JUSTIFY );
262 const SvxWeightItem& rWeightItem = (const SvxWeightItem&) pAttr->GetItem( ATTR_FONT_WEIGHT );
263 const SvxPostureItem& rPostureItem = (const SvxPostureItem&) pAttr->GetItem( ATTR_FONT_POSTURE );
264 const SvxUnderlineItem& rUnderlineItem = (const SvxUnderlineItem&) pAttr->GetItem( ATTR_FONT_UNDERLINE );
266 const sal_Char* pChar;
268 switch( rHorJustifyItem.GetValue() )
270 case SVX_HOR_JUSTIFY_STANDARD:
271 pChar = (bValueData ? OOO_STRING_SVTOOLS_RTF_QR : OOO_STRING_SVTOOLS_RTF_QL);
272 break;
273 case SVX_HOR_JUSTIFY_CENTER: pChar = OOO_STRING_SVTOOLS_RTF_QC; break;
274 case SVX_HOR_JUSTIFY_BLOCK: pChar = OOO_STRING_SVTOOLS_RTF_QJ; break;
275 case SVX_HOR_JUSTIFY_RIGHT: pChar = OOO_STRING_SVTOOLS_RTF_QR; break;
276 case SVX_HOR_JUSTIFY_LEFT:
277 case SVX_HOR_JUSTIFY_REPEAT:
278 default: pChar = OOO_STRING_SVTOOLS_RTF_QL; break;
280 rStrm << pChar;
282 if ( rWeightItem.GetWeight() >= WEIGHT_BOLD )
283 { // bold
284 bResetAttr = TRUE;
285 rStrm << OOO_STRING_SVTOOLS_RTF_B;
287 if ( rPostureItem.GetPosture() != ITALIC_NONE )
288 { // italic
289 bResetAttr = TRUE;
290 rStrm << OOO_STRING_SVTOOLS_RTF_I;
292 if ( rUnderlineItem.GetLineStyle() != UNDERLINE_NONE )
293 { // underline
294 bResetAttr = TRUE;
295 rStrm << OOO_STRING_SVTOOLS_RTF_UL;
298 rStrm << ' ';
299 RTFOutFuncs::Out_String( rStrm, aContent );
300 rStrm << OOO_STRING_SVTOOLS_RTF_CELL;
302 if ( bResetPar )
303 rStrm << OOO_STRING_SVTOOLS_RTF_PARD << OOO_STRING_SVTOOLS_RTF_INTBL;
304 if ( bResetAttr )
305 rStrm << OOO_STRING_SVTOOLS_RTF_PLAIN;