fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / inc / xetable.hxx
blob0c566624334d91b90ca818ac4017159071b66a7c
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 .
20 #ifndef INCLUDED_SC_SOURCE_FILTER_INC_XETABLE_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XETABLE_HXX
23 #include "xltable.hxx"
25 #include <vector>
26 #include <tools/mempool.hxx>
27 #include "xladdress.hxx"
28 #include "xerecord.hxx"
29 #include "xestring.hxx"
30 #include "xeformula.hxx"
31 #include "xestyle.hxx"
32 #include "xeextlst.hxx"
34 #include <map>
35 #include <memory>
36 #include <unordered_map>
37 #include <unordered_set>
39 /* ============================================================================
40 Export of cell tables including row and column description.
41 - Managing all used and formatted cells in a sheet.
42 - Row and column properties, i.e. width/height, visibility.
43 - Find default row formatting and default column formatting.
44 - Merged cell ranges.
45 ============================================================================ */
47 // Helper records for cell records
49 /** Represents a STRING record that contains the result of a string formula. */
50 class XclExpStringRec : public XclExpRecord
52 public:
53 explicit XclExpStringRec( const XclExpRoot& rRoot, const OUString& rResult );
55 private:
56 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
58 private:
59 XclExpStringRef mxResult;
62 // Additional records for special formula ranges ==============================
64 /** Base record for additional range formula records (i.e. ARRAY, SHRFMLA). */
65 class XclExpRangeFmlaBase : public XclExpRecord
67 public:
68 /** Returns true, if the passed cell position is equal to own base position. */
69 bool IsBasePos( sal_uInt16 nXclCol, sal_uInt32 nXclRow ) const;
71 /** Derived classes create the token array for a corresponding FORMULA cell record. */
72 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const = 0;
73 /** Derived classes return true, if the own formula contains volatile functions. */
74 virtual bool IsVolatile() const = 0;
76 protected:
77 /** Constructs the record with a single cell. */
78 explicit XclExpRangeFmlaBase(
79 sal_uInt16 nRecId, sal_uInt32 nRecSize, const ScAddress& rScPos );
80 /** Constructs the record with a cell range. */
81 explicit XclExpRangeFmlaBase(
82 sal_uInt16 nRecId, sal_uInt32 nRecSize, const ScRange& rScRange );
84 /** Extends the cell range to include the passed cell address. */
85 void Extend( const ScAddress& rScPos );
87 /** Writes the range address covered by this record. */
88 void WriteRangeAddress( XclExpStream& rStrm ) const;
90 protected:
91 XclRange maXclRange; /// Range described by this record.
92 XclAddress maBaseXclPos; /// Address of base cell (first FORMULA record).
95 typedef std::shared_ptr< XclExpRangeFmlaBase > XclExpRangeFmlaRef;
97 // Array formulas =============================================================
99 class ScTokenArray;
101 /** Represents an ARRAY record that contains the token array of a matrix formula.
103 An ARRAY record is stored following the first FORMULA record that is part
104 of a matrix formula. All FORMULA records of a matrix formula contain a
105 reference to the ARRAY record, while the ARRAY record contains the formula
106 token array used by all formulas.
108 class XclExpArray : public XclExpRangeFmlaBase
110 public:
111 explicit XclExpArray( XclTokenArrayRef xTokArr, const ScRange& rScRange );
113 /** Creates and returns the token array for a corresponding FORMULA cell record. */
114 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const SAL_OVERRIDE;
115 /** Returns true, if the array formula contains volatile functions. */
116 virtual bool IsVolatile() const SAL_OVERRIDE;
118 private:
119 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
121 private:
122 XclTokenArrayRef mxTokArr; /// The token array of a matrix formula.
125 typedef std::shared_ptr< XclExpArray > XclExpArrayRef;
127 /** Caches all ARRAY records. */
128 class XclExpArrayBuffer : protected XclExpRoot
130 public:
131 explicit XclExpArrayBuffer( const XclExpRoot& rRoot );
133 /** Inserts a new ARRAY record into the buffer and returns it. */
134 XclExpArrayRef CreateArray( const ScTokenArray& rScTokArr, const ScRange& rScRange );
135 /** Tries to find an ARRAY record that corresponds to an ocMatRef token. */
136 XclExpArrayRef FindArray( const ScTokenArray& rScTokArr, const ScAddress& rBasePos ) const;
138 private:
139 typedef ::std::map< ScAddress, XclExpArrayRef > XclExpArrayMap;
140 XclExpArrayMap maRecMap; /// Map containing the ARRAY records.
143 // Shared formulas ============================================================
145 /** Represents a SHRFMLA record that contains the token array of a shared formula.
147 A SHRFMLA record is stored following the first FORMULA record that is part
148 of a shared formula. All FORMULA records of a shared formula contain a
149 reference to the SHRFMLA record, while the SHRFMLA record contains the
150 formula token array used by all formulas.
152 class XclExpShrfmla : public XclExpRangeFmlaBase
154 public:
155 /** Creates a SHRFMLA record that consists of the passed cell address only. */
156 explicit XclExpShrfmla( XclTokenArrayRef xTokArr, const ScAddress& rScPos );
158 /** Extends the cell range to include the passed cell address. */
159 void ExtendRange( const ScAddress& rScPos );
161 /** Creates and returns the token array for a corresponding FORMULA cell record. */
162 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const SAL_OVERRIDE;
163 /** Returns true, if the shared formula contains volatile functions. */
164 virtual bool IsVolatile() const SAL_OVERRIDE;
166 private:
167 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
169 private:
170 XclTokenArrayRef mxTokArr; /// The token array of a shared formula.
171 sal_uInt8 mnUsedCount; /// Number of FORMULA records referring to this record.
174 typedef std::shared_ptr< XclExpShrfmla > XclExpShrfmlaRef;
176 /** Caches all SHRFMLA records and provides functions to update their ranges. */
177 class XclExpShrfmlaBuffer : protected XclExpRoot
179 public:
180 explicit XclExpShrfmlaBuffer( const XclExpRoot& rRoot );
182 /** Tries to create a new or to update an existing SHRFMLA record.
183 @return An empty reference, if the passed token array does not contain
184 a shared formula. If the token array is a shared formula, this
185 function updates its cell range to include the passed cell position,
186 if there is a SHRFMLA record for the passed token array; otherwise
187 this function creates and returns a new SHRFMLA record. */
188 XclExpShrfmlaRef CreateOrExtendShrfmla( const ScFormulaCell& rScCell, const ScAddress& rScPos );
190 private:
192 * Check for presence of token that's not allowed in Excel's shared
193 * formula. Refer to the "SharedParsedFormula" section of [MS-XLS] spec
194 * for more info.
196 bool IsValidTokenArray( const ScTokenArray& rArray ) const;
198 typedef std::unordered_map<const ScTokenArray*, XclExpShrfmlaRef> TokensType;
199 typedef std::unordered_set<const ScTokenArray*> BadTokenArraysType;
201 TokensType maRecMap; /// Map containing the SHRFMLA records.
202 BadTokenArraysType maBadTokens; /// shared tokens we should *not* export as SHRFMLA
205 // Multiple operations ========================================================
207 struct XclMultipleOpRefs;
209 /** Represents a TABLEOP record for a multiple operations range. */
210 class XclExpTableop : public XclExpRangeFmlaBase
212 public:
213 explicit XclExpTableop( const ScAddress& rScPos,
214 const XclMultipleOpRefs& rRefs, sal_uInt8 nScMode );
216 /** Returns true, if the cell range has been extended to the passed position.
217 @descr All references passed in rRefs must fit the ranges passed in the constructor. */
218 bool TryExtend( const ScAddress& rScPos, const XclMultipleOpRefs& rRefs );
220 /** Finalizes the record. Tests on valid cell range and reference addresses. */
221 void Finalize();
223 /** Creates and returns the token array for a corresponding FORMULA cell record. */
224 virtual XclTokenArrayRef CreateCellTokenArray( const XclExpRoot& rRoot ) const SAL_OVERRIDE;
225 /** Returns true, if the multiple operations range is volatile. */
226 virtual bool IsVolatile() const SAL_OVERRIDE;
227 /** Writes the record if it is valid. */
228 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
230 private:
231 /** Returns true, if the passed cell position can be appended to this record. */
232 bool IsAppendable( sal_uInt16 nXclCol, sal_uInt16 nXclRow ) const;
234 /** Writes the contents of the TABLEOP record. */
235 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
237 private:
238 sal_uInt16 mnLastAppXclCol;/// Column index of last appended cell.
239 sal_uInt16 mnColInpXclCol; /// Column index of column input cell.
240 sal_uInt32 mnColInpXclRow; /// Row index of column input cell.
241 sal_uInt16 mnRowInpXclCol; /// Column index of row input cell.
242 sal_uInt32 mnRowInpXclRow; /// Row index of row input cell.
243 sal_uInt8 mnScMode; /// Type of the multiple operation (Calc constant).
244 bool mbValid; /// true = Contains valid references.
247 typedef std::shared_ptr< XclExpTableop > XclExpTableopRef;
249 /** Contains all created TABLEOP records and supports creating or updating them. */
250 class XclExpTableopBuffer : protected XclExpRoot
252 public:
253 explicit XclExpTableopBuffer( const XclExpRoot& rRoot );
255 /** Tries to update an existing or to create a new TABLEOP record.
256 @return Reference to the TABLEOP record for this cell (existing or new),
257 or an empty reference, if rScTokArr does not contain a multiple
258 operations formula. */
259 XclExpTableopRef CreateOrExtendTableop(
260 const ScTokenArray& rScTokArr, const ScAddress& rScPos );
262 /** Finalizes all contained TABLEOP records. */
263 void Finalize();
265 private:
266 /** Tries to create a new TABLEOP record, if rRefs contains valid references. */
267 XclExpTableopRef TryCreate( const ScAddress& rScPos, const XclMultipleOpRefs& rRefs );
269 private:
270 typedef XclExpRecordList< XclExpTableop > XclExpTableopList;
271 XclExpTableopList maTableopList; /// List of all TABLEOP records.
274 // Cell records
276 /** The base class of all cell records. */
277 class XclExpCellBase : public XclExpRecord
279 public:
280 /** Returns the (first) address of the cell(s). */
281 inline const XclAddress& GetXclPos() const { return maXclPos; }
282 /** Returns the (first) Excel column index of the cell(s). */
283 inline sal_uInt16 GetXclCol() const { return maXclPos.mnCol; }
284 /** Returns the Excel row index of the cell. */
285 inline sal_uInt32 GetXclRow() const { return maXclPos.mnRow; }
287 /** Derived classes return the column index of the last contained cell. */
288 virtual sal_uInt16 GetLastXclCol() const = 0;
289 /** Derived classes return the XF identifier of the first contained cell. */
290 virtual sal_uInt32 GetFirstXFId() const = 0;
291 /** Derived classes return true, if this record does not contain at least one valid cell. */
292 virtual bool IsEmpty() const = 0;
293 /** Derived classes return whether the cell contains multi-line text. */
294 virtual bool IsMultiLineText() const;
296 /** Derived classes try to merge the contents of the passed cell to own data. */
297 virtual bool TryMerge( const XclExpCellBase& rCell );
298 /** Derived classes convert the XF identifier(s) into the Excel XF index(es).
299 @param rXFIndexes The converted XF index(es) are inserted here. */
300 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) = 0;
301 /** Derived classes for blank cells insert the Excel XF index(es) into the passed vector. */
302 virtual void GetBlankXFIndexes( ScfUInt16Vec& rXFIndexes ) const;
303 /** Derived classes for blank cells remove unused Excel XF index(es). */
304 virtual void RemoveUnusedBlankCells( const ScfUInt16Vec& rXFIndexes );
306 protected:
307 explicit XclExpCellBase(
308 sal_uInt16 nRecId, sal_Size nContSize, const XclAddress& rXclPos );
310 /** Sets this record to a new column position. */
311 inline void SetXclCol( sal_uInt16 nXclCol ) { maXclPos.mnCol = nXclCol; }
312 /** Sets this record to a new row position. */
313 inline void SetXclRow( sal_uInt32 nXclRow ) { maXclPos.mnRow = nXclRow; }
315 private:
316 XclAddress maXclPos; /// Address of the cell.
319 typedef std::shared_ptr< XclExpCellBase > XclExpCellRef;
321 // Single cell records ========================================================
323 /** Base class for all cell records not supporting multiple contents. */
324 class XclExpSingleCellBase : public XclExpCellBase
326 public:
327 /** Returns the last column, which is equal to the first column for single cells. */
328 virtual sal_uInt16 GetLastXclCol() const SAL_OVERRIDE;
329 /** Return the XF identifier of the cell. */
330 virtual sal_uInt32 GetFirstXFId() const SAL_OVERRIDE;
331 /** Returns true, if this record does not contain at least one valid cell. */
332 virtual bool IsEmpty() const SAL_OVERRIDE;
333 /** Converts the XF identifier into the Excel XF index. */
334 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) SAL_OVERRIDE;
335 /** Writes cell address, XF index, and calls WriteContents() for each cell. */
336 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
338 protected:
339 explicit XclExpSingleCellBase( sal_uInt16 nRecId, sal_Size nContSize,
340 const XclAddress& rXclPos, sal_uInt32 nXFId );
342 explicit XclExpSingleCellBase( const XclExpRoot& rRoot,
343 sal_uInt16 nRecId, sal_Size nContSize, const XclAddress& rXclPos,
344 const ScPatternAttr* pPattern, sal_Int16 nScript, sal_uInt32 nForcedXFId );
346 inline void SetContSize( sal_Size nContSize ) { mnContSize = nContSize; }
347 inline sal_Size GetContSize() const { return mnContSize; }
349 inline void SetXFId( sal_uInt32 nXFId ) { maXFId.mnXFId = nXFId; }
350 inline sal_uInt32 GetXFId() const { return maXFId.mnXFId; }
352 private:
353 /** Writes cell address, XF index, and calls WriteContents() for each cell. */
354 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
355 /** Derived classes write the contents of the specified cell (without XF index). */
356 virtual void WriteContents( XclExpStream& rStrm ) = 0;
358 private:
359 XclExpXFId maXFId; /// The XF identifier of the cell formatting.
360 sal_Size mnContSize; /// The size of the cell contents.
363 /** Represents a NUMBER record that describes a cell with a double value. */
364 class XclExpNumberCell : public XclExpSingleCellBase
366 DECL_FIXEDMEMPOOL_NEWDEL( XclExpNumberCell )
368 public:
369 explicit XclExpNumberCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
370 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
371 double fValue );
373 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
374 private:
375 virtual void WriteContents( XclExpStream& rStrm ) SAL_OVERRIDE;
377 private:
378 double mfValue; /// The cell value.
381 /** Represents a BOOLERR record that describes a cell with a Boolean value. */
382 class XclExpBooleanCell : public XclExpSingleCellBase
384 DECL_FIXEDMEMPOOL_NEWDEL( XclExpBooleanCell )
386 public:
387 explicit XclExpBooleanCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
388 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
389 bool bValue );
391 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
392 private:
393 virtual void WriteContents( XclExpStream& rStrm ) SAL_OVERRIDE;
395 private:
396 bool mbValue; /// The cell value.
399 class XclExpHyperlinkHelper;
400 class EditTextObject;
402 /** Represents a text cell record.
404 May contain a BIFF2-BIFF7 LABEL record for a simple string, or a BIFF2-BIFF7
405 RSTRING record for a formatted string, or a BIFF8 LABELSST string for any
406 string (simply stores a reference to the Shared String Table).
408 class XclExpLabelCell : public XclExpSingleCellBase
410 DECL_FIXEDMEMPOOL_NEWDEL( XclExpLabelCell )
412 public:
413 /** Constructs the record from an unformatted Calc string cell. */
414 explicit XclExpLabelCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
415 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
416 const OUString& rStr );
418 /** Constructs the record from a formatted Calc edit cell. */
419 explicit XclExpLabelCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
420 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
421 const EditTextObject* pEditText, XclExpHyperlinkHelper& rHlinkHelper );
423 /** Returns true if the cell contains multi-line text. */
424 virtual bool IsMultiLineText() const SAL_OVERRIDE;
426 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
427 private:
428 /** Initializes the record contents. Called from constructors. */
429 void Init( const XclExpRoot& rRoot,
430 const ScPatternAttr* pPattern, XclExpStringRef xText );
432 virtual void WriteContents( XclExpStream& rStrm ) SAL_OVERRIDE;
434 private:
435 XclExpStringRef mxText; /// The cell text.
436 sal_uInt32 mnSstIndex; /// Index into Shared String Table (only used for BIFF8).
437 bool mbLineBreak; /// True = cell has automatic linebreaks enabled.
440 class ScFormulaCell;
442 /** Represents a FORMULA record that describes a cell with a formula. */
443 class XclExpFormulaCell : public XclExpSingleCellBase
445 DECL_FIXEDMEMPOOL_NEWDEL( XclExpFormulaCell )
447 public:
448 explicit XclExpFormulaCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
449 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
450 const ScFormulaCell& rScFmlaCell,
451 XclExpArrayBuffer& rArrayBfr,
452 XclExpShrfmlaBuffer& rShrfmlaBfr,
453 XclExpTableopBuffer& rTableopBfr );
455 /** Writes the FORMULA record and additional records related to the formula. */
456 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
457 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
459 private:
460 virtual void WriteContents( XclExpStream& rStrm ) SAL_OVERRIDE;
462 private:
463 ScFormulaCell& mrScFmlaCell; /// The Calc formula cell.
464 XclTokenArrayRef mxTokArr; /// The token array of the formula.
465 XclExpRangeFmlaRef mxAddRec; /// Additional record for matrix/shared formulas.
466 XclExpRecordRef mxStringRec; /// STRING record for string result.
469 // Multiple cell records ======================================================
471 struct XclExpMultiXFId : public XclExpXFId
473 sal_uInt16 mnCount; /// Number of XF identifiers.
475 inline explicit XclExpMultiXFId( sal_uInt32 nXFId, sal_uInt16 nCount = 1 ) :
476 XclExpXFId( nXFId ), mnCount( nCount ) {}
479 /** Base class for all cell records supporting multiple contents. */
480 class XclExpMultiCellBase : public XclExpCellBase
482 public:
483 /** Returns the column index of the last cell this record describes. */
484 virtual sal_uInt16 GetLastXclCol() const SAL_OVERRIDE;
485 /** Return the XF identifier of the first contained cell. */
486 virtual sal_uInt32 GetFirstXFId() const SAL_OVERRIDE;
487 /** Returns true, if this record does not contain at least one valid cell. */
488 virtual bool IsEmpty() const SAL_OVERRIDE;
490 /** Convert all XF identifiers into the Excel XF indexes. */
491 virtual void ConvertXFIndexes( const XclExpRoot& rRoot ) SAL_OVERRIDE;
492 /** Writes the record, calls WriteContents() for each contained cell.
493 @descr May write several records, if unused XF indexes are contained. */
494 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
495 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
497 protected:
498 explicit XclExpMultiCellBase( sal_uInt16 nRecId, sal_uInt16 nMulRecId,
499 sal_Size nContSize, const XclAddress& rXclPos );
501 /** Sets the size of the remaining contents of one cell (without the XF index). */
502 inline void SetContSize( sal_Size nContSize ) { mnContSize = nContSize; }
503 /** Returns the size of the remaining contents of one cell (without the XF index). */
504 inline sal_Size GetContSize() const { return mnContSize; }
506 /** Returns the number of cells this record represents. */
507 sal_uInt16 GetCellCount() const;
509 /** Appends the passed XF identifier nCount times to the list of XF identifiers. */
510 void AppendXFId( const XclExpMultiXFId& rXFId );
511 /** Appends the passed cell format nCount times to the list of XF identifiers. */
512 void AppendXFId( const XclExpRoot& rRoot,
513 const ScPatternAttr* pPattern, sal_uInt16 nScript,
514 sal_uInt32 nForcedXFId, sal_uInt16 nCount = 1 );
516 /** Tries to merge the XF ID list of the passed cell with the own list. */
517 bool TryMergeXFIds( const XclExpMultiCellBase& rCell );
518 /** Inserts the Excel XF index(es) into the passed vector. */
519 void GetXFIndexes( ScfUInt16Vec& rXFIndexes ) const;
521 /** Removes unused Excel XF index(es).
522 @param rXFIndexes Specifies which XF indexes are used. */
523 void RemoveUnusedXFIndexes( const ScfUInt16Vec& rXFIndexes );
525 private:
526 /** Derived classes write the remaining contents of the specified cell (without XF index).
527 @param nRelCol Relative column index (starts with 0 for first cell of this record). */
528 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) = 0;
529 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) = 0;
531 private:
532 typedef ::std::vector< XclExpMultiXFId > XclExpMultiXFIdDeq;
534 sal_uInt16 mnMulRecId; /// Record ID for multiple record variant.
535 sal_Size mnContSize; /// Data size of contents for one cell
536 XclExpMultiXFIdDeq maXFIds; /// The XF identifiers of the cell formatting.
539 /** Represents a BLANK or MULBLANK record that describes empty but formatted cells. */
540 class XclExpBlankCell : public XclExpMultiCellBase
542 DECL_FIXEDMEMPOOL_NEWDEL( XclExpBlankCell )
544 public:
545 explicit XclExpBlankCell( const XclAddress& rXclPos, const XclExpMultiXFId& rXFId );
547 explicit XclExpBlankCell( const XclExpRoot& rRoot,
548 const XclAddress& rXclPos, sal_uInt16 nLastXclCol,
549 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId );
551 /** Tries to merge the contents of the passed cell to own data. */
552 virtual bool TryMerge( const XclExpCellBase& rCell ) SAL_OVERRIDE;
553 /** Inserts the Excel XF index(es) into the passed vector. */
554 virtual void GetBlankXFIndexes( ScfUInt16Vec& rXFIndexes ) const SAL_OVERRIDE;
555 /** Tries to remove unused Excel XF index(es). */
556 virtual void RemoveUnusedBlankCells( const ScfUInt16Vec& rXFIndexes ) SAL_OVERRIDE;
558 private:
559 /** Writes the remaining contents of the specified cell (without XF index). */
560 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) SAL_OVERRIDE;
561 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) SAL_OVERRIDE;
564 /** Represents an RK or MULRK record that describes cells with a compressed double values. */
565 class XclExpRkCell : public XclExpMultiCellBase
567 DECL_FIXEDMEMPOOL_NEWDEL( XclExpRkCell )
569 public:
570 explicit XclExpRkCell( const XclExpRoot& rRoot, const XclAddress& rXclPos,
571 const ScPatternAttr* pPattern, sal_uInt32 nForcedXFId,
572 sal_Int32 nRkValue );
574 /** Tries to merge the contents of the passed cell to own data. */
575 virtual bool TryMerge( const XclExpCellBase& rCell ) SAL_OVERRIDE;
577 private:
578 /** Writes the remaining contents of the specified cell (without XF index). */
579 virtual void WriteContents( XclExpStream& rStrm, sal_uInt16 nRelCol ) SAL_OVERRIDE;
580 virtual void WriteXmlContents( XclExpXmlStream& rStrm, const XclAddress& rAddress, sal_uInt32 nXFId, sal_uInt16 nRelCol ) SAL_OVERRIDE;
582 private:
583 ScfInt32Vec maRkValues; /// The cell values.
586 // Rows and Columns
588 class ScOutlineArray;
590 /** Base class for buffers containing row or column outline data. */
591 class XclExpOutlineBuffer
593 public:
594 /** Returns true, if a collapsed group ends at the last processed position. */
595 inline bool IsCollapsed() const { return mbCurrCollapse; }
596 /** Returns the highest level of an open group at the last processed position. */
597 inline sal_uInt8 GetLevel() const { return ::std::min( mnCurrLevel, EXC_OUTLINE_MAX ); }
599 protected:
600 /** Constructs the outline buffer.
601 @param bRows true = Process row ouline array; false = Process column outline array. */
602 explicit XclExpOutlineBuffer( const XclExpRoot& rRoot, bool bRows );
604 /** Updates the current state by processing the settings at the passed Calc position. */
605 void UpdateColRow( SCCOLROW nScPos );
607 private:
608 /** Data about an outline level. */
609 struct XclExpLevelInfo
611 SCCOLROW mnScEndPos; /// The end position of a group in a level.
612 bool mbHidden; /// true = Group in this level is hidden.
613 inline explicit XclExpLevelInfo() : mnScEndPos( 0 ), mbHidden( false ) {}
615 typedef ::std::vector< XclExpLevelInfo > XclExpLevelInfoVec;
617 const ScOutlineArray* mpScOLArray; /// Pointer to Calc outline array.
618 XclExpLevelInfoVec maLevelInfos; /// Info for current row and all levels.
619 sal_uInt8 mnCurrLevel; /// Highest level of an open group for current position.
620 bool mbCurrCollapse; /// true = Collapsed group ends at current position.
623 /** The outline buffer for column outlines. */
624 class XclExpColOutlineBuffer : public XclExpOutlineBuffer
626 public:
627 inline explicit XclExpColOutlineBuffer( const XclExpRoot& rRoot ) :
628 XclExpOutlineBuffer( rRoot, false ) {}
630 /** Updates the current state by processing the settings of the passed Calc column. */
631 inline void Update( SCCOL nScCol )
632 { UpdateColRow( static_cast< SCCOLROW >( nScCol ) ); }
635 /** The outline buffer for row outlines. */
636 class XclExpRowOutlineBuffer : public XclExpOutlineBuffer
638 public:
639 inline explicit XclExpRowOutlineBuffer( const XclExpRoot& rRoot ) :
640 XclExpOutlineBuffer( rRoot, true ) {}
642 /** Updates the current state by processing the settings of the passed Calc row. */
643 inline void Update( SCROW nScRow )
644 { UpdateColRow( static_cast< SCCOLROW >( nScRow ) ); }
647 /** Represents a GUTS record containing the level count of row and column outlines. */
648 class XclExpGuts : public XclExpRecord
650 public:
651 explicit XclExpGuts( const XclExpRoot& rRoot );
653 private:
654 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
656 private:
657 sal_uInt16 mnColLevels; /// Number of visible column outline levels.
658 sal_uInt16 mnColWidth; /// Width of column outline area (pixels).
659 sal_uInt16 mnRowLevels; /// Number of visible row outline levels.
660 sal_uInt16 mnRowWidth; /// Width of row outline area (pixels).
663 /** Represents a DIMENSIONS record containing the used area of a sheet. */
664 class XclExpDimensions : public XclExpRecord
666 public:
667 explicit XclExpDimensions( const XclExpRoot& rRoot );
669 /** Sets the used area to the record. */
670 void SetDimensions(
671 sal_uInt16 nFirstUsedXclCol, sal_uInt32 nFirstUsedXclRow,
672 sal_uInt16 nFirstFreeXclCol, sal_uInt32 nFirstFreeXclRow );
674 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
675 private:
676 /** Writes the contents of the DIMENSIONS record. */
677 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
679 private:
680 sal_uInt32 mnFirstUsedXclRow; /// First used row.
681 sal_uInt32 mnFirstFreeXclRow; /// First unused row after used area.
682 sal_uInt16 mnFirstUsedXclCol; /// First used column.
683 sal_uInt16 mnFirstFreeXclCol; /// First free column after used area.
686 /** Represents the DEFCOLWIDTH record containing the default column width of a sheet.
688 Excel stores the default column width in entire character widths of the '0'
689 character using the application default font (i.e. the default width is 10,
690 if the '0' character fits 10 times into a cell in a column with default
691 width.
693 The IsDefWidth() function returns true, if the passed width (measured in
694 1/256 of the width of the '0' character) could be converted exactly to the
695 default width. If the passed width is rounded up or down to get the default
696 width, the function returns false.
698 class XclExpDefcolwidth : public XclExpUInt16Record, protected XclExpRoot
700 public:
701 explicit XclExpDefcolwidth( const XclExpRoot& rRoot );
703 /** Returns true, if the own default width exactly matches the passed width. */
704 bool IsDefWidth( sal_uInt16 nXclColWidth ) const;
706 /** Sets the passed column width (in 1/256 character width) as default width. */
707 void SetDefWidth( sal_uInt16 nXclColWidth );
710 /** Contains the column settings for a range of columns.
712 After construction the record contains a temporary XF identifier returned
713 from the XF buffer. After creating the entire Excel document in memory, the
714 ConvertXFIndexes() function converts it into the real Excel XF index.
716 class XclExpColinfo : public XclExpRecord, protected XclExpRoot
718 public:
719 /** Constructs the record with the settings in the Calc document. */
720 explicit XclExpColinfo( const XclExpRoot& rRoot,
721 SCCOL nScCol, SCROW nLastScRow,
722 XclExpColOutlineBuffer& rOutlineBfr );
724 /** Converts the XF identifier into the Excel XF index, returns the latter. */
725 sal_uInt16 ConvertXFIndexes();
727 /** Tries to merge this record with the passed record.
728 @descr Possible, if passed record directly follows this record and has equal contents.
729 @return true = This record is equal to passed record and has been updated. */
730 bool TryMerge( const XclExpColinfo& rColInfo );
732 /** Returns the Excel width of the column(s). */
733 inline sal_uInt16 GetColWidth() const { return mnWidth; }
734 /** Returns the final Excel XF index of the column(s). */
735 inline sal_uInt16 GetXFIndex() const { return maXFId.mnXFIndex; }
736 /** Returns the number of columns represented by this record. */
737 inline sal_uInt16 GetColCount() const { return mnLastXclCol - mnFirstXclCol + 1; }
739 /** Returns true, if the column has default format and width. */
740 bool IsDefault( const XclExpDefcolwidth& rDefColWidth ) const;
742 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
744 private:
745 /** Writes the contents of this COLINFO record. */
746 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
748 private:
749 XclExpXFId maXFId; /// The XF identifier for column default format.
750 sal_uInt16 mnWidth; /// Excel width of the column.
751 sal_uInt16 mnScWidth; /// Calc width of the column.
752 sal_uInt16 mnFlags; /// Additional column flags.
753 sal_uInt16 mnFirstXclCol; /// Index to first column.
754 sal_uInt16 mnLastXclCol; /// Index to last column.
757 /** Contains COLINFO records for all columns of a Calc sheet.
759 On construction one COLINFO record per column is created. After creating
760 the entire Excel document in memory, the ConvertXFIndexes() function converts
761 all temporary XF identifiers into real Excel XF indexes and merges all equal
762 COLINFO records together.
764 class XclExpColinfoBuffer : public XclExpRecordBase, protected XclExpRoot
766 public:
767 explicit XclExpColinfoBuffer( const XclExpRoot& rRoot );
769 /** Initializes the buffer: finds settings and formatting of all columns.
770 @param nLastScRow Last row used to find default formatting. */
771 void Initialize( SCROW nLastScRow );
772 /** Converts the XF identifiers into the Excel XF indexes and merges equal columns.
773 @param rXFIndexes Returns the final XF indexes of all columns. */
774 void Finalize( ScfUInt16Vec& rXFIndexes );
776 /** Writes all COLINFO records of this buffer. */
777 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
778 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
780 private:
781 typedef XclExpRecordList< XclExpColinfo > XclExpColinfoList;
782 typedef XclExpColinfoList::RecordRefType XclExpColinfoRef;
784 XclExpColinfoList maColInfos; /// List of COLINFO records.
785 XclExpDefcolwidth maDefcolwidth; /// The DEFCOLWIDTH record.
786 XclExpColOutlineBuffer maOutlineBfr; /// Buffer for column outline groups.
789 class XclExpRow;
791 /** Contains all possible default row settings. */
792 struct XclExpDefaultRowData
794 sal_uInt16 mnFlags; /// Default flags for unspecified rows.
795 sal_uInt16 mnHeight; /// Default height for unspecified rows.
797 explicit XclExpDefaultRowData();
798 explicit XclExpDefaultRowData( const XclExpRow& rRow );
800 /** Returns true, if rows are hidden by default. */
801 inline bool IsHidden() const { return ::get_flag( mnFlags, EXC_DEFROW_HIDDEN ); }
802 /** Returns true, if the rows have a manually set height by default. */
803 inline bool IsUnsynced() const { return ::get_flag( mnFlags, EXC_DEFROW_UNSYNCED ); }
806 /** Represents a DEFROWHEIGHT record containing default format for unused rows. */
807 class XclExpDefrowheight : public XclExpRecord
809 public:
810 explicit XclExpDefrowheight();
812 /** Sets the passed default data as current record contents. */
813 void SetDefaultData( const XclExpDefaultRowData& rDefData );
814 XclExpDefaultRowData& GetDefaultData() { return maDefData; }
815 private:
816 /** Writes the contents of the record. */
817 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
819 private:
820 XclExpDefaultRowData maDefData; /// Record data.
823 /** Represents a ROW record and additionally contains all cells records of a row.
825 This class contains all cell records of a row in a spreadsheet. There are 2
826 cell records in Excel that support storing a range of cells in one record
827 (MULBLANK for multiple blank cells, and MULRK for multiple RK values). The
828 insertion functions try to merge a new inserted cell with existing
829 neighbors, if this is supported by the current type of cell record.
831 The Finalize() function converts the XF identifiers of all cell records to
832 the final Excel XF indexes. Then a default
834 class XclExpRow : public XclExpRecord, protected XclExpRoot
836 public:
837 /** Constructs the ROW record and converts the Calc row settings.
838 @param bAlwaysEmpty true = This row will not be filled with blank cells
839 in the Finalize() function. */
840 explicit XclExpRow( const XclExpRoot& rRoot, sal_uInt32 nXclRow,
841 XclExpRowOutlineBuffer& rOutlineBfr, bool bAlwaysEmpty );
843 /** Returns the excel row index of this ROW record. */
844 inline sal_uInt32 GetXclRow() const { return mnXclRow; }
845 /** Returns the height of the row in twips. */
846 inline sal_uInt16 GetHeight() const { return mnHeight; }
847 /** Returns true, if this row does not contain at least one valid cell. */
848 inline bool IsEmpty() const { return maCellList.IsEmpty(); }
849 /** Returns true, if this row is hidden. */
850 inline bool IsHidden() const { return ::get_flag( mnFlags, EXC_ROW_HIDDEN ); }
851 /** Returns true, if this row contains a manually set height. */
852 inline bool IsUnsynced() const { return ::get_flag( mnFlags, EXC_ROW_UNSYNCED ); }
853 /** Returns true, if this row is enabled (will be exported). */
854 inline bool IsEnabled() const { return mbEnabled; }
856 /** Appends the passed cell object to this row. */
857 void AppendCell( XclExpCellRef xCell, bool bIsMergedBase );
859 /** Converts all XF identifiers into the Excel XF indexes. */
860 void Finalize( const ScfUInt16Vec& rColXFIndexes,
861 bool bUpdateProgress );
863 /** Returns the column index of the first used cell in this row.
864 @descr This function can only be called after Finalize(). */
865 sal_uInt16 GetFirstUsedXclCol() const;
866 /** Returns the column index of the first unused cell following all used cells in this row.
867 @descr This function can only be called after Finalize(). */
868 sal_uInt16 GetFirstFreeXclCol() const;
870 /** Returns true, if this row may be omitted by using the DEFROWHEIGHT record.
871 @descr A row may be omitted, if it does not contain any cell or
872 explicit default cell formatting, and is not part of an outline.
873 This function can only be called after Finalize(). */
874 bool IsDefaultable() const;
875 /** Disables this row, if it is defaultable and has the passed default format.
876 @descr Disabled rows will not be saved.
877 This function can only be called after Finalize(). */
878 void DisableIfDefault( const XclExpDefaultRowData& rDefRowData );
880 /** Writes all cell records of this row. */
881 void WriteCellList( XclExpStream& rStrm );
883 /** Writes the ROW record if the row is not disabled (see DisableIfDefault() function). */
884 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
885 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
887 inline sal_uInt32 GetXclRowRpt() const { return mnXclRowRpt; }
888 inline void SetXclRowRpt( sal_uInt32 nRpt ){ mnXclRowRpt = nRpt; }
889 private:
890 /** Initializes the record data. Called from constructors. */
891 void Init( sal_uInt16 nXclRow, XclExpRowOutlineBuffer* pOutlineBfr );
892 /** Inserts a cell at the specified list position, tries to merge with neighbors. */
893 void InsertCell( XclExpCellRef xCell, size_t nPos, bool bIsMergedBase );
895 /** Writes the contents of the ROW record. */
896 virtual void WriteBody( XclExpStream& rStrm ) SAL_OVERRIDE;
898 private:
899 typedef XclExpRecordList< XclExpCellBase > XclExpCellList;
901 XclExpCellList maCellList; /// List of cell records for this row.
902 sal_uInt32 mnXclRow; /// Excel row index of this row.
903 sal_uInt16 mnHeight; /// Row height in twips.
904 sal_uInt16 mnFlags; /// Flags for the ROW record.
905 sal_uInt16 mnXFIndex; /// Default row formatting.
906 sal_uInt16 mnOutlineLevel; /// Outline Level (for OOXML)
907 sal_uInt32 mnXclRowRpt;
908 sal_uInt32 mnCurrentRow;
909 bool mbAlwaysEmpty; /// true = Do not add blank cells in Finalize().
910 bool mbEnabled; /// true = Write this ROW record.
913 /** Collects all rows which contain all cells of a sheet.
915 This row buffer automatically creates ROW records when cells are inserted
916 with the AppendCell() function. It is possible to force creation of more
917 ROW records with the CreateRows() function. In both cases, all preceding
918 missing ROW records are inserted too.
920 class XclExpRowBuffer : public XclExpRecordBase, protected XclExpRoot
922 public:
923 explicit XclExpRowBuffer( const XclExpRoot& rRoot );
925 /** Appends the passed cell object to the row that the cell specifies. */
926 void AppendCell( XclExpCellRef xCell, bool bIsMergedBase );
927 /** Forces insertion of all ROW records before the passed row. */
928 void CreateRows( SCROW nFirstFreeScRow );
930 /** Converts all XF identifiers into the Excel XF indexes and calculates default formats.
931 @param rDefRowData (out-param) The default row format is returned here.
932 @param rColXFIndexes The column default XF indexes. */
933 void Finalize( XclExpDefaultRowData& rDefRowData, const ScfUInt16Vec& rColXFIndexes );
935 /** Writes the DIMENSIONS record, all ROW records and all cell records. */
936 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
937 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
939 XclExpDimensions& GetDimensions() { return maDimensions;}
941 private:
942 /** Returns access to the specified ROW record. Inserts preceding missing ROW records.
943 @param bRowAlwaysEmpty true = Created rows will not be filled with blank cells
944 in the XclExpRow::Finalize() function. */
945 XclExpRow& GetOrCreateRow( sal_uInt32 nXclRow, bool bRowAlwaysEmpty );
947 private:
948 typedef std::shared_ptr<XclExpRow> RowRef;
949 typedef ::std::map<sal_uInt32, RowRef> RowMap;
951 RowMap maRowMap;
952 XclExpRowOutlineBuffer maOutlineBfr; /// Buffer for row outline groups.
953 XclExpDimensions maDimensions; /// DIMENSIONS record for used area.
956 // Cell Table
958 class XclExpNote;
959 class XclExpMergedcells;
960 class XclExpHyperlink;
961 class XclExpDval;
963 /** This class contains the cell contents and more of an entire sheet.
965 The cell table includes the settings and default formatting of all columns,
966 the settings and default formatting of all used rows, and the contents of
967 all cells of one sheet in a spreadsheet document.
969 The constructor does all the work creating the cell table. It reads the
970 Calc sheet and converts all columns, rows, and cells to Excel record data.
971 Additioanlly, hyperlink records, note records, additional records for
972 formula cells, data validation records, and outline records are created.
974 The Finalize() function does even more work. It calculates default column
975 settings and removes column records that are equal to this default. The
976 same happens with rows: A default format is calculated for each row, and
977 all blank cells in this row that have the same format are removed. Then,
978 the most used row settings are calculated, and all empty rows that have the
979 same settings are removed too.
981 Records that are not stored inside the cell table area in an Excel file
982 (i.e. DEFROWHEIGHT record, NOTE records, MERGEDCELLS record, HLINK records,
983 DVAL and DV records for data validation) can be accessed with the function
984 CreateRecord(). It returns the reference to the respective record (or
985 record list) which can be inserted into a record list.
987 class XclExpCellTable : public XclExpRecordBase, protected XclExpRoot
989 public:
990 explicit XclExpCellTable( const XclExpRoot& rRoot );
992 /** Converts all XF identifiers into the Excel XF indexes and calculates default formats. */
993 void Finalize();
995 /** Returns the reference to an internal record specified by the passed record id.
996 @param nRecId The record identifier that specifies which record is
997 returned. Possible values are: EXC_ID_DEFROWHEIGHT, EXC_ID_NOTE,
998 EXC_ID_MERGEDCELLS, EXC_ID_HLINK, EXC_ID_DVAL. */
999 XclExpRecordRef CreateRecord( sal_uInt16 nRecId ) const;
1000 /** Saves the entire cell table. */
1001 virtual void Save( XclExpStream& rStrm ) SAL_OVERRIDE;
1002 virtual void SaveXml( XclExpXmlStream& rStrm ) SAL_OVERRIDE;
1004 private:
1005 typedef XclExpRecordList< XclExpNote > XclExpNoteList;
1006 typedef XclExpRecordList< XclExpHyperlink > XclExpHyperlinkList;
1008 typedef std::shared_ptr< XclExpDefrowheight > XclExpDefrowhRef;
1009 typedef std::shared_ptr< XclExpNoteList > XclExpNoteListRef;
1010 typedef std::shared_ptr< XclExpMergedcells > XclExpMergedcellsRef;
1011 typedef std::shared_ptr< XclExpHyperlinkList > XclExpHyperlinkRef;
1012 typedef std::shared_ptr< XclExpDval > XclExpDvalRef;
1013 typedef std::shared_ptr< XclExtLst > XclExtLstRef;
1015 XclExpColinfoBuffer maColInfoBfr; /// Buffer for column formatting.
1016 XclExpRowBuffer maRowBfr; /// Rows and cell records.
1017 XclExpArrayBuffer maArrayBfr; /// Buffer for ARRAY records.
1018 XclExpShrfmlaBuffer maShrfmlaBfr; /// Buffer for SHRFMLA records.
1019 XclExpTableopBuffer maTableopBfr; /// Buffer for TABLEOP records.
1020 XclExpDefrowhRef mxDefrowheight; /// DEFROWHEIGHT record for default row format.
1021 XclExpRecordRef mxGuts; /// GUTS record for outline areas.
1022 XclExpNoteListRef mxNoteList; /// List of NOTE records.
1023 XclExpMergedcellsRef mxMergedcells; /// MERGEDCELLS record for merged cell ranges.
1024 XclExpHyperlinkRef mxHyperlinkList; /// List of HLINK records.
1025 XclExpDvalRef mxDval; /// Data validation with DVAL and DV records.
1026 XclExtLstRef mxExtLst;
1029 #endif
1031 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */