Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sc / source / filter / inc / xihelper.hxx
blobae600e850317aee1c83b3d0fb4aafe06205ef763
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_XIHELPER_HXX
21 #define INCLUDED_SC_SOURCE_FILTER_INC_XIHELPER_HXX
23 #include <editeng/editdata.hxx>
24 #include <types.hxx>
25 #include "xladdress.hxx"
26 #include "xiroot.hxx"
27 #include "xltools.hxx"
28 #include <memory>
29 #include <vector>
31 class ScRangeList;
33 namespace svl {
35 class SharedStringPool;
39 // Excel->Calc cell address/range conversion ==================================
41 /** Provides functions to convert Excel cell addresses to Calc cell addresses. */
42 class XclImpAddressConverter : public XclAddressConverterBase
44 public:
45 explicit XclImpAddressConverter( const XclImpRoot& rRoot );
47 // cell address -----------------------------------------------------------
49 /** Checks if the passed Excel cell address is valid.
50 @param rXclPos The Excel cell address to check.
51 @param bWarn true = Sets the internal flag that produces a warning box
52 after loading/saving the file, if the cell address is not valid.
53 @return true = Cell address in rXclPos is valid. */
54 bool CheckAddress( const XclAddress& rXclPos, bool bWarn );
56 /** Converts the passed Excel cell address to a Calc cell address.
57 @param rScPos (Out) The converted Calc cell address, if valid.
58 @param rXclPos The Excel cell address to convert.
59 @param bWarn true = Sets the internal flag that produces a warning box
60 after loading/saving the file, if the cell address is invalid.
61 @return true = Cell address returned in rScPos is valid. */
62 bool ConvertAddress( ScAddress& rScPos,
63 const XclAddress& rXclPos, SCTAB nScTab, bool bWarn );
65 /** Returns a valid cell address by moving it into allowed dimensions.
66 @param rXclPos The Excel cell address to convert.
67 @param bWarn true = Sets the internal flag that produces a warning box
68 after loading/saving the file, if the cell address is invalid.
69 @return The converted Calc cell address. */
70 ScAddress CreateValidAddress( const XclAddress& rXclPos,
71 SCTAB nScTab, bool bWarn );
73 // cell range -------------------------------------------------------------
75 /** Converts the passed Excel cell range to a Calc cell range.
76 @param rScRange (Out) The converted Calc cell range, if valid.
77 @param rXclRange The Excel cell range to convert.
78 @param bWarn true = Sets the internal flag that produces a warning box
79 after loading/saving the file, if the cell range contains invalid cells.
80 @return true = Cell range returned in rScRange is valid (original or cropped). */
81 bool ConvertRange( ScRange& rScRange, const XclRange& rXclRange,
82 SCTAB nScTab1, SCTAB nScTab2, bool bWarn );
84 // cell range list --------------------------------------------------------
86 /** Converts the passed Excel cell range list to a Calc cell range list.
87 @descr The start position of the ranges will not be modified. Cell
88 ranges that fit partly into valid dimensions are cropped
89 accordingly. Cell ranges that do not fit at all, are not inserted
90 into the Calc cell range list.
91 @param rScRanges (Out) The converted Calc cell range list.
92 @param rXclRanges The Excel cell range list to convert.
93 @param bWarn true = Sets the internal flag that produces a warning box
94 after loading/saving the file, if at least one of the cell ranges
95 contains invalid cells. */
96 void ConvertRangeList( ScRangeList& rScRanges,
97 const XclRangeList& rXclRanges, SCTAB nScTab, bool bWarn );
100 // String->EditEngine conversion ==============================================
102 class EditTextObject;
104 /** This class provides methods to convert an XclImpString.
105 @The string can be converted to an edit engine text object or directly
106 to a Calc edit cell. */
107 class XclImpStringHelper
109 public:
110 /** delete copy constructor */
111 XclImpStringHelper(const XclImpStringHelper&) = delete;
112 /** delete copy-assignment operator */
113 const XclImpStringHelper& operator=(const XclImpStringHelper&) = delete;
114 /** We don't want anybody to instantiate this class, since it is just a
115 collection of static methods. */
116 XclImpStringHelper() = delete;
117 /** Returns a new edit engine text object.
118 @param nXFIndex Index to XF for first text portion (for escapement). */
119 static std::unique_ptr<EditTextObject> CreateTextObject(
120 const XclImpRoot& rRoot,
121 const XclImpString& rString );
123 static void SetToDocument(
124 ScDocumentImport& rDoc, const ScAddress& rPos, const XclImpRoot& rRoot,
125 const XclImpString& rString, sal_uInt16 nXFIndex );
128 // Header/footer conversion ===================================================
130 class EditEngine;
131 class SfxItemSet;
132 class SvxFieldItem;
133 struct XclFontData;
135 /** Converts an Excel header/footer string into three edit engine text objects.
136 @descr Header/footer content is divided into three parts: Left, center and
137 right portion. All formatting information is encoded in the Excel string
138 using special character sequences. A control sequence starts with the ampersand
139 character.
141 Supported control sequences:
142 &L start of left portion
143 &C start of center portion
144 &R start of right portion
145 &P current page number
146 &N page count
147 &D current date
148 &T current time
149 &A table name
150 &F file name without path (see also &Z&F)
151 &Z file path without file name (converted to full file name, see also &Z&F)
152 &Z&F file path and name
153 &U underlining on/off
154 &E double underlining on/off
155 &S strikeout characters on/off
156 &X superscript on/off
157 &Y subscript on/off
158 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
159 &fontheight set font height in points ('fontheight' is a decimal value)
161 Known but unsupported control sequences:
162 &G picture
164 class XclImpHFConverter : protected XclImpRoot
166 public:
167 /** delete copy constructor */
168 XclImpHFConverter(const XclImpHFConverter&) = delete;
169 /** delete copy-assignment operator */
170 const XclImpHFConverter& operator=(const XclImpHFConverter&) = delete;
172 explicit XclImpHFConverter( const XclImpRoot& rRoot );
173 virtual ~XclImpHFConverter() override;
175 /** Parses the passed string and creates three new edit engine text objects. */
176 void ParseString( const OUString& rHFString );
178 /** Creates a ScPageHFItem and inserts it into the passed item set. */
179 void FillToItemSet( SfxItemSet& rItemSet, sal_uInt16 nWhichId ) const;
180 /** Returns the total height of the converted header or footer in twips. */
181 sal_Int32 GetTotalHeight() const;
183 private: // types
184 typedef ::std::unique_ptr< XclFontData > XclFontDataPtr;
186 /** Enumerates the supported header/footer portions. */
187 enum XclImpHFPortion { EXC_HF_LEFT, EXC_HF_CENTER, EXC_HF_RIGHT, EXC_HF_PORTION_COUNT };
189 /** Contains all information about a header/footer portion. */
190 struct XclImpHFPortionInfo
192 typedef std::shared_ptr< EditTextObject > EditTextObjectRef;
193 EditTextObjectRef mxObj; /// Edit engine text object.
194 ESelection maSel; /// Edit engine selection.
195 sal_Int32 mnHeight; /// Height of previous lines in twips.
196 sal_uInt16 mnMaxLineHt; /// Maximum font height for the current text line.
197 explicit XclImpHFPortionInfo();
199 typedef ::std::vector< XclImpHFPortionInfo > XclImpHFPortionInfoVec;
201 private:
202 /** Returns the current edit engine text object. */
203 XclImpHFPortionInfo& GetCurrInfo() { return maInfos[ meCurrObj ]; }
204 /** Returns the current edit engine text object. */
205 XclImpHFPortionInfo::EditTextObjectRef& GetCurrObj() { return GetCurrInfo().mxObj; }
206 /** Returns the current selection. */
207 ESelection& GetCurrSel() { return GetCurrInfo().maSel; }
209 /** Returns the maximum line height of the specified portion. */
210 sal_uInt16 GetMaxLineHeight( XclImpHFPortion ePortion ) const;
212 /** Updates the maximum line height of the specified portion, using the current font size. */
213 void UpdateMaxLineHeight( XclImpHFPortion ePortion );
214 /** Updates the current maximum line height, using the current font size. */
215 void UpdateCurrMaxLineHeight();
217 /** Sets the font attributes at the current selection.
218 @descr After that, the start position of the current selection object is
219 adjusted to the end of the selection. */
220 void SetAttribs();
221 /** Resets font data to application default font. */
222 void ResetFontData();
224 /** Inserts maCurrText into edit engine and adjusts the current selection object.
225 @descr The text shall not contain a newline character.
226 The text will be cleared after insertion. */
227 void InsertText();
228 /** Inserts the passed text field and adjusts the current selection object. */
229 void InsertField( const SvxFieldItem& rFieldItem );
230 /** Inserts a line break and adjusts the current selection object. */
231 void InsertLineBreak();
233 /** Creates the edit engine text object of current portion from edit engine. */
234 void CreateCurrObject();
235 /** Changes current header/footer portion to eNew.
236 @descr Creates text object of current portion and reinitializes edit engine. */
237 void SetNewPortion( XclImpHFPortion eNew );
239 private:
240 EditEngine& mrEE; /// The header/footer edit engine.
241 XclImpHFPortionInfoVec maInfos; /// Edit engine text objects for all portions.
242 OUStringBuffer maCurrText; /// Current text to insert into edit engine.
243 XclFontDataPtr mxFontData; /// Font data of current text.
244 XclImpHFPortion meCurrObj; /// The current portion.
247 // URL conversion =============================================================
249 /** This class contains static methods to decode a URL stored in an Excel file.
250 @descr Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
251 This sheet name will be extracted automatically. */
252 class XclImpUrlHelper
254 public:
255 /** delete copy constructor */
256 XclImpUrlHelper(const XclImpUrlHelper&) = delete;
257 /** delete copy-assignment operator */
258 const XclImpUrlHelper& operator=(const XclImpUrlHelper&) = delete;
259 /** We don't want anybody to instantiate this class, since it is just a
260 collection of static methods. */
261 XclImpUrlHelper() = delete;
263 /** Decodes an encoded external document URL with optional sheet name.
264 @param rUrl Returns the decoded file name incl. path.
265 @param rTabName Returns the decoded sheet name.
266 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
267 @param rEncodedUrl An encoded URL from Excel. */
268 static void DecodeUrl(
269 OUString& rUrl,
270 OUString& rTabName,
271 bool& rbSameWb,
272 const XclImpRoot& rRoot,
273 const OUString& rEncodedUrl );
275 /** Decodes an encoded external document URL without sheet name.
276 @param rUrl Returns the decoded file name incl. path.
277 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
278 @param rEncodedUrl An encoded URL from Excel. */
280 static void DecodeUrl(
281 OUString& rUrl,
282 bool& rbSameWb,
283 const XclImpRoot& rRoot,
284 const OUString& rEncodedUrl );
286 /** Decodes the passed URL to OLE or DDE link components.
287 @descr For DDE links: Decodes to application name and topic.
288 For OLE object links: Decodes to class name and document URL.
289 @return true = decoding was successful, returned strings are valid (not empty). */
290 static bool DecodeLink( OUString& rApplic, OUString& rTopic, const OUString& rEncUrl );
293 // Cached values ==============================================================
295 class ScTokenArray;
297 /** This class stores one cached value of a cached value list (used for instance in
298 CRN, EXTERNNAME, tArray). */
299 class XclImpCachedValue
301 public:
302 /** delete copy constructor */
303 XclImpCachedValue(const XclImpCachedValue&) = delete;
304 /** delete copy-assignment operator */
305 const XclImpCachedValue& operator=(const XclImpCachedValue&) = delete;
306 /** Creates a cached value and reads contents from stream and stores it with its array address. */
307 explicit XclImpCachedValue( XclImpStream& rStrm );
308 virtual ~XclImpCachedValue();
310 /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
311 sal_uInt8 GetType() const { return mnType; }
312 /** Returns the cached string value, if this value is a string, else an empty string. */
313 const OUString& GetString() const { return maStr;}
314 /** Returns the cached number, if this value has number type, else 0.0. */
315 double GetValue() const { return mfValue; }
316 /** Returns the cached Boolean value, if this value has Boolean type, else false. */
317 bool GetBool() const { return (mnType == EXC_CACHEDVAL_BOOL) && (mnBoolErr != 0); }
318 /** Returns the cached Calc error code, if this value has Error type, else 0. */
319 sal_uInt8 GetXclError() const { return (mnType == EXC_CACHEDVAL_ERROR) ? mnBoolErr : EXC_ERR_NA; }
320 /** Returns the cached Calc error code, if this value has Error type, else 0. */
321 FormulaError GetScError() const;
323 protected:
324 typedef ::std::unique_ptr< const ScTokenArray > ScTokenArrayPtr;
326 OUString maStr; /// Cached value is a string.
327 double mfValue; /// Cached value is a double.
328 ScTokenArrayPtr mxTokArr; /// Cached value is a formula or error code or Boolean.
329 sal_uInt8 mnBoolErr; /// Boolean value or Excel error code.
330 sal_uInt8 mnType; /// The type of the cached value (EXC_CACHEDVAL_*).
333 /** Contains cached values in a 2-dimensional array. */
334 class XclImpCachedMatrix
336 public:
337 explicit XclImpCachedMatrix( XclImpStream& rStrm );
338 ~XclImpCachedMatrix();
340 /** Creates a new ScMatrix object and fills it with the contained values. */
341 ScMatrixRef CreateScMatrix( svl::SharedStringPool& rPool ) const;
343 private:
344 typedef std::vector< std::unique_ptr<XclImpCachedValue> > XclImpValueList;
346 XclImpValueList maValueList; /// List of cached cell values.
347 SCSIZE mnScCols; /// Number of cached columns.
348 SCSIZE mnScRows; /// Number of cached rows.
351 #endif
353 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */