Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / sc / source / filter / inc / xihelper.hxx
bloba0ff9bc8db1a529bcb63294f56429dc8f199c913
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 "xistring.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 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 = 0 );
128 // Header/footer conversion ===================================================
130 class EditEngine;
131 class EditTextObject;
132 class SfxItemSet;
133 class SvxFieldItem;
134 struct XclFontData;
136 /** Converts an Excel header/footer string into three edit engine text objects.
137 @descr Header/footer content is divided into three parts: Left, center and
138 right portion. All formatting information is encoded in the Excel string
139 using special character seuences. A control sequence starts with the ampersand
140 character.
142 Supported control sequences:
143 &L start of left portion
144 &C start of center portion
145 &R start of right portion
146 &P current page number
147 &N page count
148 &D current date
149 &T current time
150 &A table name
151 &F file name without path (see also &Z&F)
152 &Z file path without file name (converted to full file name, see also &Z&F)
153 &Z&F file path and name
154 &U underlining on/off
155 &E double underlining on/off
156 &S strikeout characters on/off
157 &X superscript on/off
158 &Y subscript on/off
159 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
160 &fontheight set font height in points ('fontheight' is a decimal value)
162 Known but unsupported control sequences:
163 &G picture
165 class XclImpHFConverter : protected XclImpRoot
167 public:
168 /** delete copy constructor */
169 XclImpHFConverter(const XclImpHFConverter&) = delete;
170 /** delete copy-assignment operator */
171 const XclImpHFConverter& operator=(const XclImpHFConverter&) = delete;
173 explicit XclImpHFConverter( const XclImpRoot& rRoot );
174 virtual ~XclImpHFConverter();
176 /** Parses the passed string and creates three new edit engine text objects. */
177 void ParseString( const OUString& rHFString );
179 /** Creates a ScPageHFItem and inserts it into the passed item set. */
180 void FillToItemSet( SfxItemSet& rItemSet, sal_uInt16 nWhichId ) const;
181 /** Returns the total height of the converted header or footer in twips. */
182 sal_Int32 GetTotalHeight() const;
184 private: // types
185 typedef ::std::unique_ptr< XclFontData > XclFontDataPtr;
187 /** Enumerates the supported header/footer portions. */
188 enum XclImpHFPortion { EXC_HF_LEFT, EXC_HF_CENTER, EXC_HF_RIGHT, EXC_HF_PORTION_COUNT };
190 /** Contains all information about a header/footer portion. */
191 struct XclImpHFPortionInfo
193 typedef std::shared_ptr< EditTextObject > EditTextObjectRef;
194 EditTextObjectRef mxObj; /// Edit engine text object.
195 ESelection maSel; /// Edit engine selection.
196 sal_Int32 mnHeight; /// Height of previous lines in twips.
197 sal_uInt16 mnMaxLineHt; /// Maximum font height for the current text line.
198 explicit XclImpHFPortionInfo();
200 typedef ::std::vector< XclImpHFPortionInfo > XclImpHFPortionInfoVec;
202 private:
203 /** Returns the current edit engine text object. */
204 inline XclImpHFPortionInfo& GetCurrInfo() { return maInfos[ meCurrObj ]; }
205 /** Returns the current edit engine text object. */
206 inline XclImpHFPortionInfo::EditTextObjectRef& GetCurrObj() { return GetCurrInfo().mxObj; }
207 /** Returns the current selection. */
208 inline ESelection& GetCurrSel() { return GetCurrInfo().maSel; }
210 /** Returns the maximum line height of the specified portion. */
211 sal_uInt16 GetMaxLineHeight( XclImpHFPortion ePortion ) const;
212 /** Returns the current maximum line height. */
213 sal_uInt16 GetCurrMaxLineHeight() const;
215 /** Updates the maximum line height of the specified portion, using the current font size. */
216 void UpdateMaxLineHeight( XclImpHFPortion ePortion );
217 /** Updates the current maximum line height, using the current font size. */
218 void UpdateCurrMaxLineHeight();
220 /** Sets the font attributes at the current selection.
221 @descr After that, the start position of the current selection object is
222 adjusted to the end of the selection. */
223 void SetAttribs();
224 /** Resets font data to application default font. */
225 void ResetFontData();
227 /** Inserts maCurrText into edit engine and adjusts the current selection object.
228 @descr The text shall not contain a newline character.
229 The text will be cleared after insertion. */
230 void InsertText();
231 /** Inserts the passed text field and adjusts the current selection object. */
232 void InsertField( const SvxFieldItem& rFieldItem );
233 /** Inserts a line break and adjusts the current selection object. */
234 void InsertLineBreak();
236 /** Creates the edit engine text object of current portion from edit engine. */
237 void CreateCurrObject();
238 /** Changes current header/footer portion to eNew.
239 @descr Creates text object of current portion and reinitializes edit engine. */
240 void SetNewPortion( XclImpHFPortion eNew );
242 private:
243 EditEngine& mrEE; /// The header/footer edit engine.
244 XclImpHFPortionInfoVec maInfos; /// Edit engine text objects for all portions.
245 OUString maCurrText; /// Current text to insert into edit engine.
246 XclFontDataPtr mxFontData; /// Font data of current text.
247 XclImpHFPortion meCurrObj; /// The current portion.
250 // URL conversion =============================================================
252 /** This class contains static methods to decode an URL stored in an Excel file.
253 @descr Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
254 This sheet name will be extracted automatically. */
255 class XclImpUrlHelper
257 public:
258 /** delete copy constructor */
259 XclImpUrlHelper(const XclImpUrlHelper&) = delete;
260 /** delete copy-assignment operator */
261 const XclImpUrlHelper& operator=(const XclImpUrlHelper&) = delete;
262 /** We don't want anybody to instantiate this class, since it is just a
263 collection of static methods. */
264 XclImpUrlHelper() = delete;
266 /** Decodes an encoded external document URL with optional sheet name.
267 @param rUrl Returns the decoded file name incl. path.
268 @param rTabName Returns the decoded sheet name.
269 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
270 @param rEncodedUrl An encoded URL from Excel. */
271 static void DecodeUrl(
272 OUString& rUrl,
273 OUString& rTabName,
274 bool& rbSameWb,
275 const XclImpRoot& rRoot,
276 const OUString& rEncodedUrl );
278 /** Decodes an encoded external document URL without sheet name.
279 @param rUrl Returns the decoded file name incl. path.
280 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
281 @param rEncodedUrl An encoded URL from Excel. */
283 static void DecodeUrl(
284 OUString& rUrl,
285 bool& rbSameWb,
286 const XclImpRoot& rRoot,
287 const OUString& rEncodedUrl );
289 /** Decodes the passed URL to OLE or DDE link components.
290 @descr For DDE links: Decodes to application name and topic.
291 For OLE object links: Decodes to class name and document URL.
292 @return true = decoding was successful, returned strings are valid (not empty). */
293 static bool DecodeLink( OUString& rApplic, OUString& rTopic, const OUString& rEncUrl );
296 // Cached values ==============================================================
298 class ScTokenArray;
300 /** This class stores one cached value of a cached value list (used for instance in
301 CRN, EXTERNNAME, tArray). */
302 class XclImpCachedValue
304 public:
305 /** delete copy constructor */
306 XclImpCachedValue(const XclImpCachedValue&) = delete;
307 /** delete copy-assignment operator */
308 const XclImpCachedValue& operator=(const XclImpCachedValue&) = delete;
309 /** Creates a cached value and reads contents from stream and stores it with its array address. */
310 explicit XclImpCachedValue( XclImpStream& rStrm );
311 virtual ~XclImpCachedValue();
313 /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
314 inline sal_uInt8 GetType() const { return mnType; }
315 /** Returns the cached string value, if this value is a string, else an empty string. */
316 const OUString& GetString() const { return maStr;}
317 /** Returns the cached number, if this value has number type, else 0.0. */
318 inline double GetValue() const { return mfValue; }
319 /** Returns the cached Boolean value, if this value has Boolean type, else false. */
320 inline bool GetBool() const { return (mnType == EXC_CACHEDVAL_BOOL) && (mnBoolErr != 0); }
321 /** Returns the cached Calc error code, if this value has Error type, else 0. */
322 inline sal_uInt8 GetXclError() const { return (mnType == EXC_CACHEDVAL_ERROR) ? mnBoolErr : EXC_ERR_NA; }
323 /** Returns the cached Calc error code, if this value has Error type, else 0. */
324 sal_uInt16 GetScError() const;
326 protected:
327 typedef ::std::unique_ptr< const ScTokenArray > ScTokenArrayPtr;
329 OUString maStr; /// Cached value is a string.
330 double mfValue; /// Cached value is a double.
331 ScTokenArrayPtr mxTokArr; /// Cached value is a formula or error code or Boolean.
332 sal_uInt8 mnBoolErr; /// Boolean value or Excel error code.
333 sal_uInt8 mnType; /// The type of the cached value (EXC_CACHEDVAL_*).
336 /** Contains cached values in a 2-dimensional array. */
337 class XclImpCachedMatrix
339 public:
340 explicit XclImpCachedMatrix( XclImpStream& rStrm );
341 ~XclImpCachedMatrix();
343 /** Creates a new ScFullMatrix object and fills it with the contained values. */
344 ScMatrixRef CreateScMatrix( svl::SharedStringPool& rPool ) const;
346 private:
347 typedef std::vector< std::unique_ptr<XclImpCachedValue> > XclImpValueList;
349 XclImpValueList maValueList; /// List of cached cell values.
350 SCSIZE mnScCols; /// Number of cached columns.
351 SCSIZE mnScRows; /// Number of cached rows.
354 #endif
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */