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 "xladdress.hxx"
26 #include "xestring.hxx"
28 // Export progress bar ========================================================
32 /** The main progress bar for the export filter.
34 This class encapsulates creation and initialization of sub progress
35 segments. The Activate***Segment() functions activate a specific segement
36 of the main progress bar. The implementation of these functions contain the
37 calculation of the needed size of the segment. Following calls of the
38 Progress() function increase the currently activated sub segment.
40 class XclExpProgressBar
: protected XclExpRoot
43 explicit XclExpProgressBar( const XclExpRoot
& rRoot
);
44 virtual ~XclExpProgressBar();
46 /** Initializes all segments and sub progress bars. */
49 /** Increases the number of existing ROW records by 1. */
50 void IncRowRecordCount();
52 /** Activates the progress segment to create ROW records. */
53 void ActivateCreateRowsSegment();
54 /** Activates the progress segment to finalize ROW records. */
55 void ActivateFinalRowsSegment();
57 /** Increases the currently activated (sub) progress bar by 1 step. */
61 typedef std::unique_ptr
< ScfProgressBar
> ScfProgressBarPtr
;
63 ScfProgressBarPtr mxProgress
; /// Progress bar implementation.
64 ScfProgressBar
* mpSubProgress
; /// Current sub progress bar.
66 ScfProgressBar
* mpSubRowCreate
; /// Sub progress bar for creating table rows.
67 ScfInt32Vec maSubSegRowCreate
; /// Segment ID's for all sheets in sub progress bar.
69 ScfProgressBar
* mpSubRowFinal
; /// Sub progress bar for finalizing ROW records.
70 sal_Int32 mnSegRowFinal
; /// Progress segment for finalizing ROW records.
72 sal_Size mnRowCount
; /// Number of created ROW records.
75 // Calc->Excel cell address/range conversion ==================================
77 /** Provides functions to convert Calc cell addresses to Excel cell addresses. */
78 class XclExpAddressConverter
: public XclAddressConverterBase
81 explicit XclExpAddressConverter( const XclExpRoot
& rRoot
);
83 // cell address -----------------------------------------------------------
85 /** Checks if the passed Calc cell address is valid.
86 @param rScPos The Calc cell address to check.
87 @param bWarn true = Sets the internal flag that produces a warning box
88 after loading/saving the file, if the cell address is not valid.
89 @return true = Cell address in rScPos is valid. */
90 bool CheckAddress( const ScAddress
& rScPos
, bool bWarn
);
92 /** Converts the passed Calc cell address to an Excel cell address.
93 @param rXclPos (Out) The converted Excel cell address, if valid.
94 @param rScPos The Calc cell address to convert.
95 @param bWarn true = Sets the internal flag that produces a warning box
96 after loading/saving the file, if the cell address is not valid.
97 @return true = Cell address returned in rXclPos is valid. */
98 bool ConvertAddress( XclAddress
& rXclPos
,
99 const ScAddress
& rScPos
, bool bWarn
);
101 /** Returns a valid cell address by moving it into allowed dimensions.
102 @param rScPos The Calc cell address to convert.
103 @param bWarn true = Sets the internal flag that produces a warning box
104 after loading/saving the file, if the cell address is invalid.
105 @return The converted Excel cell address. */
106 XclAddress
CreateValidAddress( const ScAddress
& rScPos
, bool bWarn
);
108 // cell range -------------------------------------------------------------
110 /** Checks if the passed cell range is valid (checks start and end position).
111 @param rScRange The Calc cell range to check.
112 @param bWarn true = Sets the internal flag that produces a warning box
113 after loading/saving the file, if the cell range is not valid.
114 @return true = Cell range in rScRange is valid. */
115 bool CheckRange( const ScRange
& rScRange
, bool bWarn
);
117 /** Checks and eventually crops the cell range to valid dimensions.
118 @descr The start position of the range will not be modified.
119 @param rScRange (In/out) The cell range to validate.
120 @param bWarn true = Sets the internal flag that produces a warning box
121 after loading/saving the file, if the cell range contains invalid
122 cells. If the range is partly valid, this function sets the warning
123 flag, corrects the range and returns true.
124 @return true = Cell range in rScRange is valid (original or cropped). */
125 bool ValidateRange( ScRange
& rScRange
, bool bWarn
);
127 /** Converts the passed Calc cell range to an Excel cell range.
128 @param rXclRange (Out) The converted Excel cell range, if valid.
129 @param rScRange The Calc cell range to convert.
130 @param bWarn true = Sets the internal flag that produces a warning box
131 after loading/saving the file, if the cell range contains invalid cells.
132 @return true = Cell range returned in rXclRange is valid (original or cropped). */
133 bool ConvertRange( XclRange
& rXclRange
, const ScRange
& rScRange
, bool bWarn
);
135 // cell range list --------------------------------------------------------
137 /** Checks and eventually crops the cell ranges to valid dimensions.
138 @descr The start position of the ranges will not be modified. Cell
139 ranges that fit partly into valid dimensions are cropped
140 accordingly. Cell ranges that do not fit at all, are removed from
142 @param rScRanges (In/out) The cell range list to check.
143 @param bWarn true = Sets the internal flag that produces a warning box
144 after loading/saving the file, if at least one of the cell ranges
145 contains invalid cells. */
146 void ValidateRangeList( ScRangeList
& rScRanges
, bool bWarn
);
148 /** Converts the passed Calc cell range list to an Excel cell range list.
149 @descr The start position of the ranges will not be modified. Cell
150 ranges that fit partly into valid dimensions are cropped
151 accordingly. Cell ranges that do not fit at all, are not inserted
152 into the Excel cell range list.
153 @param rXclRanges (Out) The converted Excel cell range list.
154 @param rScRanges The Calc cell range list to convert.
155 @param bWarn true = Sets the internal flag that produces a warning box
156 after loading/saving the file, if at least one of the cell ranges
157 contains invalid cells. */
158 void ConvertRangeList( XclRangeList
& rXclRanges
,
159 const ScRangeList
& rScRanges
, bool bWarn
);
162 // EditEngine->String conversion ==============================================
165 class XclExpHyperlink
;
167 /** Helper to create HLINK records during creation of formatted cell strings.
169 In Excel it is not possible to have more than one hyperlink in a cell. This
170 helper detects multiple occurrences of hyperlinks and fills a string which
171 is used to create a cell note containing all URLs. Only cells containing
172 one hyperlink are exported as hyperlink cells.
174 class XclExpHyperlinkHelper
: protected XclExpRoot
177 typedef std::shared_ptr
< XclExpHyperlink
> XclExpHyperlinkRef
;
179 explicit XclExpHyperlinkHelper( const XclExpRoot
& rRoot
, const ScAddress
& rScPos
);
180 virtual ~XclExpHyperlinkHelper();
182 /** Processes the passed URL field (tries to create a HLINK record).
183 @return The representation string of the URL field. */
184 OUString
ProcessUrlField( const SvxURLField
& rUrlField
);
186 /** Returns true, if a single HLINK record has been created. */
187 bool HasLinkRecord() const;
188 /** Returns the created single HLINk record, or an empty reference. */
189 XclExpHyperlinkRef
GetLinkRecord();
191 /** Returns true, if multiple URLs have been processed. */
192 inline bool HasMultipleUrls() const { return mbMultipleUrls
; }
193 /** Returns a string containing all processed URLs. */
194 inline const OUString
& GetUrlList() { return maUrlList
; }
197 XclExpHyperlinkRef mxLinkRec
; /// Created HLINK record.
198 ScAddress maScPos
; /// Cell position to set at the HLINK record.
199 OUString maUrlList
; /// List with all processed URLs.
200 bool mbMultipleUrls
; /// true = Multiple URL fields processed.
204 class EditTextObject
;
208 /** This class provides methods to create an XclExpString.
209 @descr The string can be created from an edit engine text object or
210 directly from a Calc edit cell. */
211 class XclExpStringHelper
214 /** removes copy constructor */
215 XclExpStringHelper(const XclExpStringHelper
&) = delete;
216 /** remove copy-assignment operator */
217 const XclExpStringHelper
& operator=(const XclExpStringHelper
&) = delete;
218 /** We don't want anybody to instantiate this class, since it is just a
219 collection of static methods */
220 XclExpStringHelper() = delete;
222 /** Creates a new unformatted string from the passed string.
223 @descr Creates a Unicode string or a byte string, depending on the
224 current BIFF version contained in the passed XclExpRoot object.
225 @param rString The source string.
226 @param nFlags Modifiers for string export.
227 @param nMaxLen The maximum number of characters to store in this string.
228 @return The new string object (shared pointer). */
229 static XclExpStringRef
CreateString(
230 const XclExpRoot
& rRoot
,
231 const OUString
& rString
,
232 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
233 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
235 /** Creates a new unformatted string from the passed character.
236 @descr Creates a Unicode string or a byte string, depending on the
237 current BIFF version contained in the passed XclExpRoot object.
238 @param cChar The source character. The NUL character is explicitly allowed.
239 @param nFlags Modifiers for string export.
240 @param nMaxLen The maximum number of characters to store in this string.
241 @return The new string object (shared pointer). */
242 static XclExpStringRef
CreateString(
243 const XclExpRoot
& rRoot
,
245 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
246 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
248 /** Appends an unformatted string to an Excel string object.
249 @descr Selects the correct Append() function depending on the current
250 BIFF version contained in the passed XclExpRoot object.
251 @param rXclString The Excel string object.
252 @param rString The source string. */
253 static void AppendString(
254 XclExpString
& rXclString
,
255 const XclExpRoot
& rRoot
,
256 const OUString
& rString
);
258 /** Appends a character to an Excel string object.
259 @descr Selects the correct Append() function depending on the current
260 BIFF version contained in the passed XclExpRoot object.
261 @param rXclString The Excel string object.
262 @param rString The source string. */
263 static void AppendChar(
264 XclExpString
& rXclString
,
265 const XclExpRoot
& rRoot
,
268 /** Creates a new formatted string from a Calc string cell.
269 @descr Creates a Unicode string or a byte string, depending on the
270 current BIFF version contained in the passed XclExpRoot object.
271 May create a formatted string object, if the cell text contains
272 different script types.
273 @param rStringCell The Calc string cell object.
274 @param pCellAttr The set item containing the cell formatting.
275 @param nFlags Modifiers for string export.
276 @param nMaxLen The maximum number of characters to store in this string.
277 @return The new string object (shared pointer). */
278 static XclExpStringRef
CreateCellString(
279 const XclExpRoot
& rRoot
,
280 const OUString
& rString
,
281 const ScPatternAttr
* pCellAttr
,
282 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
283 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
285 /** Creates a new formatted string from a Calc edit cell.
286 @descr Creates a Unicode string or a byte string, depending on the
287 current BIFF version contained in the passed XclExpRoot object.
288 @param rEditCell The Calc edit cell object.
289 @param pCellAttr The set item containing the cell formatting.
290 @param rLinkHelper Helper object for hyperlink conversion.
291 @param nFlags Modifiers for string export.
292 @param nMaxLen The maximum number of characters to store in this string.
293 @return The new string object (shared pointer). */
294 static XclExpStringRef
CreateCellString(
295 const XclExpRoot
& rRoot
,
296 const EditTextObject
& rEditText
,
297 const ScPatternAttr
* pCellAttr
,
298 XclExpHyperlinkHelper
& rLinkHelper
,
299 XclStrFlags nFlags
= EXC_STR_DEFAULT
,
300 sal_uInt16 nMaxLen
= EXC_STR_MAXLEN
);
302 /** Creates a new formatted string from a drawing text box.
303 @descr Creates a Unicode string or a byte string, depending on the
304 current BIFF version contained in the passed XclExpRoot object.
305 @param rTextObj The text box object.
306 @param nFlags Modifiers for string export.
307 @return The new string object (shared pointer). */
308 static XclExpStringRef
CreateString(
309 const XclExpRoot
& rRoot
,
310 const SdrTextObj
& rTextObj
,
311 XclStrFlags nFlags
= EXC_STR_DEFAULT
);
313 /** Creates a new formatted string from a edit text string.
314 @param rEditObj The edittext object.
315 @param nFlags Modifiers for string export.
316 @return The new string object. */
317 static XclExpStringRef
CreateString(
318 const XclExpRoot
& rRoot
,
319 const EditTextObject
& rEditObj
,
320 XclStrFlags nFlags
= EXC_STR_DEFAULT
);
322 /** Returns the script type first text portion different to WEAK, or the system
323 default script type, if there is only weak script in the passed string. */
324 static sal_Int16
GetLeadingScriptType( const XclExpRoot
& rRoot
, const OUString
& rString
);
327 // Header/footer conversion ===================================================
331 /** Converts edit engine text objects to an Excel header/footer string.
332 @descr Header/footer content is divided into three parts: Left, center and
333 right portion. All formatting information will be encoded in the Excel string
334 using special character seuences. A control sequence starts with the ampersand
337 Supported control sequences:
338 &L start of left portion
339 &C start of center portion
340 &R start of right portion
341 &P current page number
346 &F file name without path
347 &Z file path without file name
348 &Z&F file path and name
349 &U underlining on/off
350 &E double underlining on/off
351 &S strikeout characters on/off
352 &X superscript on/off
354 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
355 &fontheight set font height in points ('fontheight' is a decimal value)
357 Known but unsupported control sequences:
360 class XclExpHFConverter
: protected XclExpRoot
363 /** delete copy constructor */
364 XclExpHFConverter(const XclExpHFConverter
&) = delete;
365 /** delete copy-assignment operator */
366 const XclExpHFConverter
& operator=(const XclExpHFConverter
&) = delete;
368 explicit XclExpHFConverter( const XclExpRoot
& rRoot
);
370 /** Generates the header/footer string from the passed edit engine text objects. */
372 const EditTextObject
* pLeftObj
,
373 const EditTextObject
* pCenterObj
,
374 const EditTextObject
* pRightObj
);
376 /** Returns the last generated header/footer string. */
377 inline const OUString
& GetHFString() const { return maHFString
; }
378 /** Returns the total height of the last generated header/footer in twips. */
379 inline sal_Int32
GetTotalHeight() const { return mnTotalHeight
; }
382 /** Converts the text object contents and stores it in the passed string. */
384 const EditTextObject
* pTextObj
,
385 sal_Unicode cPortionCode
);
388 EditEngine
& mrEE
; /// The header/footer edit engine.
389 OUString maHFString
; /// The last generated header/footer string.
390 sal_Int32 mnTotalHeight
; /// Total height of the last header/footer (twips).
393 // URL conversion =============================================================
395 /** This class contains static methods to encode a file URL.
396 @descr Excel stores URLs in a format that contains special control characters,
397 i.e. for directory separators or volume names. */
398 class XclExpUrlHelper
401 /** delete copy constructor */
402 XclExpUrlHelper(const XclExpUrlHelper
&) = delete;
403 /** delete copy-assignment operator */
404 const XclExpUrlHelper
& operator=(const XclExpUrlHelper
&) = delete;
405 /** We don't want anybody to instantiate this class, since it is just a
406 collection of static methods. */
407 XclExpUrlHelper() = delete;
409 /** Encodes and returns the URL passed in rAbsUrl to an Excel like URL.
410 @param pTableName Optional pointer to a table name to be encoded in this URL. */
411 static OUString
EncodeUrl( const XclExpRoot
& rRoot
, const OUString
& rAbsUrl
, const OUString
* pTableName
= nullptr );
412 /** Encodes and returns the passed DDE link to an Excel like DDE link. */
413 static OUString
EncodeDde( const OUString
& rApplic
, const OUString
& rTopic
);
418 /** Contains cached values in a 2-dimensional array. */
419 class XclExpCachedMatrix
421 void GetDimensions( SCSIZE
& nCols
, SCSIZE
& nRows
) const;
423 /** Constructs and fills a new matrix.
424 @param rMatrix The Calc value matrix. */
425 explicit XclExpCachedMatrix( const ScMatrix
& rMatrix
);
426 ~XclExpCachedMatrix();
428 /** Returns the byte count of all contained data. */
429 sal_Size
GetSize() const;
430 /** Writes the complete matrix to stream. */
431 void Save( XclExpStream
& rStrm
) const;
434 const ScMatrix
& mrMatrix
;
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */