1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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_XEHELPER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XEHELPER_HXX
23 #include <boost/noncopyable.hpp>
25 #include "xladdress.hxx"
27 #include "xestring.hxx"
29 // Export progress bar ========================================================
33 /** The main progress bar for the export filter.
35 This class encapsulates creation and initialization of sub progress
36 segments. The Activate***Segment() functions activate a specific segement
37 of the main progress bar. The implementation of these functions contain the
38 calculation of the needed size of the segment. Following calls of the
39 Progress() function increase the currently activated sub segment.
41 class XclExpProgressBar
: protected XclExpRoot
44 explicit XclExpProgressBar( const XclExpRoot
& rRoot
);
45 virtual ~XclExpProgressBar();
47 /** Initializes all segments and sub progress bars. */
50 /** Increases the number of existing ROW records by 1. */
51 void IncRowRecordCount();
53 /** Activates the progress segment to create ROW records. */
54 void ActivateCreateRowsSegment();
55 /** Activates the progress segment to finalize ROW records. */
56 void ActivateFinalRowsSegment();
58 /** Increases the currently activated (sub) progress bar by 1 step. */
62 typedef std::unique_ptr
< ScfProgressBar
> ScfProgressBarPtr
;
64 ScfProgressBarPtr mxProgress
; /// Progress bar implementation.
65 ScfProgressBar
* mpSubProgress
; /// Current sub progress bar.
67 ScfProgressBar
* mpSubRowCreate
; /// Sub progress bar for creating table rows.
68 ScfInt32Vec maSubSegRowCreate
; /// Segment ID's for all sheets in sub progress bar.
70 ScfProgressBar
* mpSubRowFinal
; /// Sub progress bar for finalizing ROW records.
71 sal_Int32 mnSegRowFinal
; /// Progress segment for finalizing ROW records.
73 sal_Size mnRowCount
; /// Number of created ROW records.
76 // Calc->Excel cell address/range conversion ==================================
78 /** Provides functions to convert Calc cell addresses to Excel cell addresses. */
79 class XclExpAddressConverter
: public XclAddressConverterBase
82 explicit XclExpAddressConverter( const XclExpRoot
& rRoot
);
84 // cell address -----------------------------------------------------------
86 /** Checks if the passed Calc cell address is valid.
87 @param rScPos The Calc cell address to check.
88 @param bWarn true = Sets the internal flag that produces a warning box
89 after loading/saving the file, if the cell address is not valid.
90 @return true = Cell address in rScPos is valid. */
91 bool CheckAddress( const ScAddress
& rScPos
, bool bWarn
);
93 /** Converts the passed Calc cell address to an Excel cell address.
94 @param rXclPos (Out) The converted Excel cell address, if valid.
95 @param rScPos The Calc cell address to convert.
96 @param bWarn true = Sets the internal flag that produces a warning box
97 after loading/saving the file, if the cell address is not valid.
98 @return true = Cell address returned in rXclPos is valid. */
99 bool ConvertAddress( XclAddress
& rXclPos
,
100 const ScAddress
& rScPos
, bool bWarn
);
102 /** Returns a valid cell address by moving it into allowed dimensions.
103 @param rScPos The Calc cell address to convert.
104 @param bWarn true = Sets the internal flag that produces a warning box
105 after loading/saving the file, if the cell address is invalid.
106 @return The converted Excel cell address. */
107 XclAddress
CreateValidAddress( const ScAddress
& rScPos
, bool bWarn
);
109 // cell range -------------------------------------------------------------
111 /** Checks if the passed cell range is valid (checks start and end position).
112 @param rScRange The Calc cell range to check.
113 @param bWarn true = Sets the internal flag that produces a warning box
114 after loading/saving the file, if the cell range is not valid.
115 @return true = Cell range in rScRange is valid. */
116 bool CheckRange( const ScRange
& rScRange
, bool bWarn
);
118 /** Checks and eventually crops the cell range to valid dimensions.
119 @descr The start position of the range will not be modified.
120 @param rScRange (In/out) The cell range to validate.
121 @param bWarn true = Sets the internal flag that produces a warning box
122 after loading/saving the file, if the cell range contains invalid
123 cells. If the range is partly valid, this function sets the warning
124 flag, corrects the range and returns true.
125 @return true = Cell range in rScRange is valid (original or cropped). */
126 bool ValidateRange( ScRange
& rScRange
, bool bWarn
);
128 /** Converts the passed Calc cell range to an Excel cell range.
129 @param rXclRange (Out) The converted Excel cell range, if valid.
130 @param rScRange The Calc cell range to convert.
131 @param bWarn true = Sets the internal flag that produces a warning box
132 after loading/saving the file, if the cell range contains invalid cells.
133 @return true = Cell range returned in rXclRange is valid (original or cropped). */
134 bool ConvertRange( XclRange
& rXclRange
, const ScRange
& rScRange
, bool bWarn
);
136 // cell range list --------------------------------------------------------
138 /** Checks and eventually crops the cell ranges to valid dimensions.
139 @descr The start position of the ranges will not be modified. Cell
140 ranges that fit partly into valid dimensions are cropped
141 accordingly. Cell ranges that do not fit at all, are removed from
143 @param rScRanges (In/out) The cell range list to check.
144 @param bWarn true = Sets the internal flag that produces a warning box
145 after loading/saving the file, if at least one of the cell ranges
146 contains invalid cells. */
147 void ValidateRangeList( ScRangeList
& rScRanges
, bool bWarn
);
149 /** Converts the passed Calc cell range list to an Excel cell range list.
150 @descr The start position of the ranges will not be modified. Cell
151 ranges that fit partly into valid dimensions are cropped
152 accordingly. Cell ranges that do not fit at all, are not inserted
153 into the Excel cell range list.
154 @param rXclRanges (Out) The converted Excel cell range list.
155 @param rScRanges The Calc cell range list to convert.
156 @param bWarn true = Sets the internal flag that produces a warning box
157 after loading/saving the file, if at least one of the cell ranges
158 contains invalid cells. */
159 void ConvertRangeList( XclRangeList
& rXclRanges
,
160 const ScRangeList
& rScRanges
, bool bWarn
);
163 // EditEngine->String conversion ==============================================
166 class XclExpHyperlink
;
168 /** Helper to create HLINK records during creation of formatted cell strings.
170 In Excel it is not possible to have more than one hyperlink in a cell. This
171 helper detects multiple occurrences of hyperlinks and fills a string which
172 is used to create a cell note containing all URLs. Only cells containing
173 one hyperlink are exported as hyperlink cells.
175 class XclExpHyperlinkHelper
: protected XclExpRoot
178 typedef std::shared_ptr
< XclExpHyperlink
> XclExpHyperlinkRef
;
180 explicit XclExpHyperlinkHelper( const XclExpRoot
& rRoot
, const ScAddress
& rScPos
);
181 virtual ~XclExpHyperlinkHelper();
183 /** Processes the passed URL field (tries to create a HLINK record).
184 @return The representation string of the URL field. */
185 OUString
ProcessUrlField( const SvxURLField
& rUrlField
);
187 /** Returns true, if a single HLINK record has been created. */
188 bool HasLinkRecord() const;
189 /** Returns the craeted single HLINk record, or an empty reference. */
190 XclExpHyperlinkRef
GetLinkRecord();
192 /** Returns true, if multiple URLs have been processed. */
193 inline bool HasMultipleUrls() const { return mbMultipleUrls
; }
194 /** Returns a string containing all processed URLs. */
195 inline const OUString
& GetUrlList() { return maUrlList
; }
198 XclExpHyperlinkRef mxLinkRec
; /// Created HLINK record.
199 ScAddress maScPos
; /// Cell position to set at the HLINK record.
200 OUString maUrlList
; /// List with all processed URLs.
201 bool mbMultipleUrls
; /// true = Multiple URL fields processed.
205 class EditTextObject
;
209 /** This class provides methods to create an XclExpString.
210 @descr The string can be created from an edit engine text object or
211 directly from a Calc edit cell. */
212 class XclExpStringHelper
: boost::noncopyable
215 /** Creates a new unformatted string from the passed string.
216 @descr Creates a Unicode string or a byte string, depending on the
217 current BIFF version contained in the passed XclExpRoot object.
218 @param rString The source string.
219 @param nFlags Modifiers for string export.
220 @param nMaxLen The maximum number of characters to store in this string.
221 @return The new string object (shared pointer). */
222 static XclExpStringRef
CreateString(
223 const XclExpRoot
& rRoot
,
224 const OUString
& rString
,
225 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
226 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
228 /** Creates a new unformatted string from the passed character.
229 @descr Creates a Unicode string or a byte string, depending on the
230 current BIFF version contained in the passed XclExpRoot object.
231 @param cChar The source character. The NUL character is explicitly allowed.
232 @param nFlags Modifiers for string export.
233 @param nMaxLen The maximum number of characters to store in this string.
234 @return The new string object (shared pointer). */
235 static XclExpStringRef
CreateString(
236 const XclExpRoot
& rRoot
,
238 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
239 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
241 /** Appends an unformatted string to an Excel string object.
242 @descr Selects the correct Append() function depending on the current
243 BIFF version contained in the passed XclExpRoot object.
244 @param rXclString The Excel string object.
245 @param rString The source string. */
246 static void AppendString(
247 XclExpString
& rXclString
,
248 const XclExpRoot
& rRoot
,
249 const OUString
& rString
);
251 /** Appends a character to an Excel string object.
252 @descr Selects the correct Append() function depending on the current
253 BIFF version contained in the passed XclExpRoot object.
254 @param rXclString The Excel string object.
255 @param rString The source string. */
256 static void AppendChar(
257 XclExpString
& rXclString
,
258 const XclExpRoot
& rRoot
,
261 /** Creates a new formatted string from a Calc string cell.
262 @descr Creates a Unicode string or a byte string, depending on the
263 current BIFF version contained in the passed XclExpRoot object.
264 May create a formatted string object, if the cell text contains
265 different script types.
266 @param rStringCell The Calc string cell object.
267 @param pCellAttr The set item containing the cell formatting.
268 @param nFlags Modifiers for string export.
269 @param nMaxLen The maximum number of characters to store in this string.
270 @return The new string object (shared pointer). */
271 static XclExpStringRef
CreateCellString(
272 const XclExpRoot
& rRoot
,
273 const OUString
& rString
,
274 const ScPatternAttr
* pCellAttr
,
275 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
276 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
278 /** Creates a new formatted string from a Calc edit cell.
279 @descr Creates a Unicode string or a byte string, depending on the
280 current BIFF version contained in the passed XclExpRoot object.
281 @param rEditCell The Calc edit cell object.
282 @param pCellAttr The set item containing the cell formatting.
283 @param rLinkHelper Helper object for hyperlink conversion.
284 @param nFlags Modifiers for string export.
285 @param nMaxLen The maximum number of characters to store in this string.
286 @return The new string object (shared pointer). */
287 static XclExpStringRef
CreateCellString(
288 const XclExpRoot
& rRoot
,
289 const EditTextObject
& rEditText
,
290 const ScPatternAttr
* pCellAttr
,
291 XclExpHyperlinkHelper
& rLinkHelper
,
292 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
293 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
295 /** Creates a new formatted string from a drawing text box.
296 @descr Creates a Unicode string or a byte string, depending on the
297 current BIFF version contained in the passed XclExpRoot object.
298 @param rTextObj The text box object.
299 @param nFlags Modifiers for string export.
300 @param nMaxLen The maximum number of characters to store in this string.
301 @return The new string object (shared pointer). */
302 static XclExpStringRef
CreateString(
303 const XclExpRoot
& rRoot
,
304 const SdrTextObj
& rTextObj
,
305 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
306 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
308 /** Creates a new formatted string from a edit text string.
309 @param rEditObj The edittext object.
310 @param nFlags Modifiers for string export.
311 @param nMaxLen The maximum number of characters to store in this string.
312 @return The new string object. */
313 static XclExpStringRef
CreateString(
314 const XclExpRoot
& rRoot
,
315 const EditTextObject
& rEditObj
,
316 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
317 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
319 /** Returns the script type first text portion different to WEAK, or the system
320 default script type, if there is only weak script in the passed string. */
321 static sal_Int16
GetLeadingScriptType( const XclExpRoot
& rRoot
, const OUString
& rString
);
324 /** We don't want anybody to instantiate this class, since it is just a
325 collection of static methods. To enforce this, the default constructor
327 XclExpStringHelper();
330 // Header/footer conversion ===================================================
334 /** Converts edit engine text objects to an Excel header/footer string.
335 @descr Header/footer content is divided into three parts: Left, center and
336 right portion. All formatting information will be encoded in the Excel string
337 using special character seuences. A control sequence starts with the ampersand
340 Supported control sequences:
341 &L start of left portion
342 &C start of center portion
343 &R start of right portion
344 &P current page number
349 &F file name without path
350 &Z file path without file name
351 &Z&F file path and name
352 &U underlining on/off
353 &E double underlining on/off
354 &S strikeout characters on/off
355 &X superscript on/off
357 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
358 &fontheight set font height in points ('fontheight' is a decimal value)
360 Known but unsupported control sequences:
363 class XclExpHFConverter
: protected XclExpRoot
, private boost::noncopyable
366 explicit XclExpHFConverter( const XclExpRoot
& rRoot
);
368 /** Generates the header/footer string from the passed edit engine text objects. */
370 const EditTextObject
* pLeftObj
,
371 const EditTextObject
* pCenterObj
,
372 const EditTextObject
* pRightObj
);
374 /** Returns the last generated header/footer string. */
375 inline const OUString
& GetHFString() const { return maHFString
; }
376 /** Returns the total height of the last generated header/footer in twips. */
377 inline sal_Int32
GetTotalHeight() const { return mnTotalHeight
; }
380 /** Converts the text object contents and stores it in the passed string. */
382 const EditTextObject
* pTextObj
,
383 sal_Unicode cPortionCode
);
386 EditEngine
& mrEE
; /// The header/footer edit engine.
387 OUString maHFString
; /// The last generated header/footer string.
388 sal_Int32 mnTotalHeight
; /// Total height of the last header/footer (twips).
391 // URL conversion =============================================================
393 /** This class contains static methods to encode a file URL.
394 @descr Excel stores URLs in a format that contains special control characters,
395 i.e. for directory separators or volume names. */
396 class XclExpUrlHelper
: boost::noncopyable
399 /** Encodes and returns the URL passed in rAbsUrl to an Excel like URL.
400 @param pTableName Optional pointer to a table name to be encoded in this URL. */
401 static OUString
EncodeUrl( const XclExpRoot
& rRoot
, const OUString
& rAbsUrl
, const OUString
* pTableName
= 0 );
402 /** Encodes and returns the passed DDE link to an Excel like DDE link. */
403 static OUString
EncodeDde( const OUString
& rApplic
, const OUString
& rTopic
);
406 /** We don't want anybody to instantiate this class, since it is just a
407 collection of static methods. To enforce this, the default constructor
414 /** Contains cached values in a 2-dimensional array. */
415 class XclExpCachedMatrix
417 void GetDimensions( SCSIZE
& nCols
, SCSIZE
& nRows
) const;
419 /** Constructs and fills a new matrix.
420 @param rMatrix The Calc value matrix. */
421 explicit XclExpCachedMatrix( const ScMatrix
& rMatrix
);
422 ~XclExpCachedMatrix();
424 /** Returns the byte count of all contained data. */
425 sal_Size
GetSize() const;
426 /** Writes the complete matrix to stream. */
427 void Save( XclExpStream
& rStrm
) const;
430 const ScMatrix
& mrMatrix
;
435 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */