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
24 #include <rangelst.hxx>
25 #include "xladdress.hxx"
27 #include "xlstring.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 segment
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() override
;
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 std::size_t 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() override
;
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 created single HLINk record, or an empty reference. */
190 XclExpHyperlinkRef
GetLinkRecord() const;
192 /** Returns true, if multiple URLs have been processed. */
193 bool HasMultipleUrls() const { return mbMultipleUrls
; }
194 /** Returns a string containing all processed URLs. */
195 const OUString
& GetUrlList() const { return maUrlList
; }
198 XclExpHyperlinkRef mxLinkRec
; /// Created HLINK record.
199 ScAddress
const 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
215 /** removes copy constructor */
216 XclExpStringHelper(const XclExpStringHelper
&) = delete;
217 /** remove copy-assignment operator */
218 const XclExpStringHelper
& operator=(const XclExpStringHelper
&) = delete;
219 /** We don't want anybody to instantiate this class, since it is just a
220 collection of static methods */
221 XclExpStringHelper() = delete;
223 /** Creates a new unformatted string from the passed string.
224 @descr Creates a Unicode string or a byte string, depending on the
225 current BIFF version contained in the passed XclExpRoot object.
226 @param rString The source string.
227 @param nFlags Modifiers for string export.
228 @param nMaxLen The maximum number of characters to store in this string.
229 @return The new string object (shared pointer). */
230 static XclExpStringRef
CreateString(
231 const XclExpRoot
& rRoot
,
232 const OUString
& rString
,
233 XclStrFlags nFlags
= XclStrFlags::NONE
,
234 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
236 /** Creates a new unformatted string from the passed character.
237 @descr Creates a Unicode string or a byte string, depending on the
238 current BIFF version contained in the passed XclExpRoot object.
239 @param cChar The source character. The NUL character is explicitly allowed.
240 @param nFlags Modifiers for string export.
241 @param nMaxLen The maximum number of characters to store in this string.
242 @return The new string object (shared pointer). */
243 static XclExpStringRef
CreateString(
244 const XclExpRoot
& rRoot
,
246 XclStrFlags nFlags
= XclStrFlags::NONE
,
247 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
249 /** Appends an unformatted string to an Excel string object.
250 @descr Selects the correct Append() function depending on the current
251 BIFF version contained in the passed XclExpRoot object.
252 @param rXclString The Excel string object.
253 @param rString The source string. */
254 static void AppendString(
255 XclExpString
& rXclString
,
256 const XclExpRoot
& rRoot
,
257 const OUString
& rString
);
259 /** Appends a character to an Excel string object.
260 @descr Selects the correct Append() function depending on the current
261 BIFF version contained in the passed XclExpRoot object.
262 @param rXclString The Excel string object.
263 @param rString The source string. */
264 static void AppendChar(
265 XclExpString
& rXclString
,
266 const XclExpRoot
& rRoot
,
269 /** Creates a new formatted string from a Calc string cell.
270 @descr Creates a Unicode string or a byte string, depending on the
271 current BIFF version contained in the passed XclExpRoot object.
272 May create a formatted string object, if the cell text contains
273 different script types.
274 @param rStringCell The Calc string cell object.
275 @param pCellAttr The set item containing the cell formatting.
276 @param nFlags Modifiers for string export.
277 @param nMaxLen The maximum number of characters to store in this string.
278 @return The new string object (shared pointer). */
279 static XclExpStringRef
CreateCellString(
280 const XclExpRoot
& rRoot
,
281 const OUString
& rString
,
282 const ScPatternAttr
* pCellAttr
,
283 XclStrFlags nFlags
= XclStrFlags::NONE
,
284 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
286 /** Creates a new formatted string from a Calc edit cell.
287 @descr Creates a Unicode string or a byte string, depending on the
288 current BIFF version contained in the passed XclExpRoot object.
289 @param rEditCell The Calc edit cell object.
290 @param pCellAttr The set item containing the cell formatting.
291 @param rLinkHelper Helper object for hyperlink conversion.
292 @param nFlags Modifiers for string export.
293 @param nMaxLen The maximum number of characters to store in this string.
294 @return The new string object (shared pointer). */
295 static XclExpStringRef
CreateCellString(
296 const XclExpRoot
& rRoot
,
297 const EditTextObject
& rEditText
,
298 const ScPatternAttr
* pCellAttr
,
299 XclExpHyperlinkHelper
& rLinkHelper
,
300 XclStrFlags nFlags
= XclStrFlags::NONE
,
301 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
303 /** Creates a new formatted string from a drawing text box.
304 @descr Creates a Unicode string or a byte string, depending on the
305 current BIFF version contained in the passed XclExpRoot object.
306 @param rTextObj The text box object.
307 @param nFlags Modifiers for string export.
308 @return The new string object (shared pointer). */
309 static XclExpStringRef
CreateString(
310 const XclExpRoot
& rRoot
,
311 const SdrTextObj
& rTextObj
,
312 XclStrFlags nFlags
= XclStrFlags::NONE
);
314 /** Creates a new formatted string from an edit text string.
315 @param rEditObj The edittext object.
316 @param nFlags Modifiers for string export.
317 @return The new string object. */
318 static XclExpStringRef
CreateString(
319 const XclExpRoot
& rRoot
,
320 const EditTextObject
& rEditObj
,
321 XclStrFlags nFlags
= XclStrFlags::NONE
);
323 /** Returns the script type first text portion different to WEAK, or the system
324 default script type, if there is only weak script in the passed string. */
325 static sal_Int16
GetLeadingScriptType( const XclExpRoot
& rRoot
, const OUString
& rString
);
328 // Header/footer conversion ===================================================
330 /** Converts edit engine text objects to an Excel header/footer string.
331 @descr Header/footer content is divided into three parts: Left, center and
332 right portion. All formatting information will be encoded in the Excel string
333 using special character sequences. A control sequence starts with the ampersand
336 Supported control sequences:
337 &L start of left portion
338 &C start of center portion
339 &R start of right portion
340 &P current page number
345 &F file name without path
346 &Z file path without file name
347 &Z&F file path and name
348 &U underlining on/off
349 &E double underlining on/off
350 &S strikeout characters on/off
351 &X superscript on/off
353 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
354 &fontheight set font height in points ('fontheight' is a decimal value)
356 Known but unsupported control sequences:
359 class XclExpHFConverter
: protected XclExpRoot
362 /** delete copy constructor */
363 XclExpHFConverter(const XclExpHFConverter
&) = delete;
364 /** delete copy-assignment operator */
365 const XclExpHFConverter
& operator=(const XclExpHFConverter
&) = delete;
367 explicit XclExpHFConverter( const XclExpRoot
& rRoot
);
369 /** Generates the header/footer string from the passed edit engine text objects. */
371 const EditTextObject
* pLeftObj
,
372 const EditTextObject
* pCenterObj
,
373 const EditTextObject
* pRightObj
);
375 /** Returns the last generated header/footer string. */
376 const OUString
& GetHFString() const { return maHFString
; }
377 /** Returns the total height of the last generated header/footer in twips. */
378 sal_Int32
GetTotalHeight() const { return mnTotalHeight
; }
381 /** Converts the text object contents and stores it in the passed string. */
383 const EditTextObject
* pTextObj
,
384 sal_Unicode cPortionCode
);
387 EditEngine
& mrEE
; /// The header/footer edit engine.
388 OUString maHFString
; /// The last generated header/footer string.
389 sal_Int32 mnTotalHeight
; /// Total height of the last header/footer (twips).
392 // URL conversion =============================================================
394 /** This class contains static methods to encode a file URL.
395 @descr Excel stores URLs in a format that contains special control characters,
396 i.e. for directory separators or volume names. */
397 class XclExpUrlHelper
400 /** delete copy constructor */
401 XclExpUrlHelper(const XclExpUrlHelper
&) = delete;
402 /** delete copy-assignment operator */
403 const XclExpUrlHelper
& operator=(const XclExpUrlHelper
&) = delete;
404 /** We don't want anybody to instantiate this class, since it is just a
405 collection of static methods. */
406 XclExpUrlHelper() = delete;
408 /** Encodes and returns the URL passed in rAbsUrl to an Excel like URL.
409 @param pTableName Optional pointer to a table name to be encoded in this URL. */
410 static OUString
EncodeUrl( const XclExpRoot
& rRoot
, const OUString
& rAbsUrl
, const OUString
* pTableName
= nullptr );
411 /** Encodes and returns the passed DDE link to an Excel like DDE link. */
412 static OUString
EncodeDde( const OUString
& rApplic
, const OUString
& rTopic
);
417 /** Contains cached values in a 2-dimensional array. */
418 class XclExpCachedMatrix
420 void GetDimensions( SCSIZE
& nCols
, SCSIZE
& nRows
) const;
422 /** Constructs and fills a new matrix.
423 @param rMatrix The Calc value matrix. */
424 explicit XclExpCachedMatrix( const ScMatrix
& rMatrix
);
425 ~XclExpCachedMatrix();
427 /** Returns the byte count of all contained data. */
428 std::size_t GetSize() const;
429 /** Writes the complete matrix to stream. */
430 void Save( XclExpStream
& rStrm
) const;
433 const ScMatrix
& mrMatrix
;
438 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */