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 SC_XIHELPER_HXX
21 #define SC_XIHELPER_HXX
23 #include <editeng/editdata.hxx>
24 #include <boost/noncopyable.hpp>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/ptr_container/ptr_vector.hpp>
27 #include "scmatrix.hxx"
28 #include "xladdress.hxx"
30 #include "xistring.hxx"
34 // Excel->Calc cell address/range conversion ==================================
36 /** Provides functions to convert Excel cell addresses to Calc cell addresses. */
37 class XclImpAddressConverter
: public XclAddressConverterBase
40 explicit XclImpAddressConverter( const XclImpRoot
& rRoot
);
42 // cell address -----------------------------------------------------------
44 /** Checks if the passed Excel cell address is valid.
45 @param rXclPos The Excel cell address to check.
46 @param bWarn true = Sets the internal flag that produces a warning box
47 after loading/saving the file, if the cell address is not valid.
48 @return true = Cell address in rXclPos is valid. */
49 bool CheckAddress( const XclAddress
& rXclPos
, bool bWarn
);
51 /** Converts the passed Excel cell address to a Calc cell address.
52 @param rScPos (Out) The converted Calc cell address, if valid.
53 @param rXclPos The Excel cell address to convert.
54 @param bWarn true = Sets the internal flag that produces a warning box
55 after loading/saving the file, if the cell address is invalid.
56 @return true = Cell address returned in rScPos is valid. */
57 bool ConvertAddress( ScAddress
& rScPos
,
58 const XclAddress
& rXclPos
, SCTAB nScTab
, bool bWarn
);
60 /** Returns a valid cell address by moving it into allowed dimensions.
61 @param rXclPos The Excel cell address to convert.
62 @param bWarn true = Sets the internal flag that produces a warning box
63 after loading/saving the file, if the cell address is invalid.
64 @return The converted Calc cell address. */
65 ScAddress
CreateValidAddress( const XclAddress
& rXclPos
,
66 SCTAB nScTab
, bool bWarn
);
68 // cell range -------------------------------------------------------------
70 /** Converts the passed Excel cell range to a Calc cell range.
71 @param rScRange (Out) The converted Calc cell range, if valid.
72 @param rXclRange The Excel cell range to convert.
73 @param bWarn true = Sets the internal flag that produces a warning box
74 after loading/saving the file, if the cell range contains invalid cells.
75 @return true = Cell range returned in rScRange is valid (original or cropped). */
76 bool ConvertRange( ScRange
& rScRange
, const XclRange
& rXclRange
,
77 SCTAB nScTab1
, SCTAB nScTab2
, bool bWarn
);
79 // cell range list --------------------------------------------------------
81 /** Converts the passed Excel cell range list to a Calc cell range list.
82 @descr The start position of the ranges will not be modified. Cell
83 ranges that fit partly into valid dimensions are cropped
84 accordingly. Cell ranges that do not fit at all, are not inserted
85 into the Calc cell range list.
86 @param rScRanges (Out) The converted Calc cell range list.
87 @param rXclRanges The Excel cell range list to convert.
88 @param bWarn true = Sets the internal flag that produces a warning box
89 after loading/saving the file, if at least one of the cell ranges
90 contains invalid cells. */
91 void ConvertRangeList( ScRangeList
& rScRanges
,
92 const XclRangeList
& rXclRanges
, SCTAB nScTab
, bool bWarn
);
95 // String->EditEngine conversion ==============================================
99 /** This class provides methods to convert an XclImpString.
100 @The string can be converted to an edit engine text object or directly
101 to a Calc edit cell. */
102 class XclImpStringHelper
: boost::noncopyable
105 /** Returns a new edit engine text object.
106 @param nXFIndex Index to XF for first text portion (for escapement). */
107 static EditTextObject
* CreateTextObject(
108 const XclImpRoot
& rRoot
,
109 const XclImpString
& rString
);
111 static void SetToDocument(
112 ScDocument
& rDoc
, const ScAddress
& rPos
, const XclImpRoot
& rRoot
,
113 const XclImpString
& rString
, sal_uInt16 nXFIndex
= 0 );
116 /** We don't want anybody to instantiate this class, since it is just a
117 collection of static methods. To enforce this, the default constructor
119 XclImpStringHelper();
122 // Header/footer conversion ===================================================
125 class EditTextObject
;
130 /** Converts an Excel header/footer string into three edit engine text objects.
131 @descr Header/footer content is divided into three parts: Left, center and
132 right portion. All formatting information is encoded in the Excel string
133 using special character seuences. A control sequence starts with the ampersand
136 Supported control sequences:
137 &L start of left portion
138 &C start of center portion
139 &R start of right portion
140 &P current page number
145 &F file name without path (see also &Z&F)
146 &Z file path without file name (converted to full file name, see also &Z&F)
147 &Z&F file path and name
148 &U underlining on/off
149 &E double underlining on/off
150 &S strikeout characters on/off
151 &X superscript on/off
153 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
154 &fontheight set font height in points ('fontheight' is a decimal value)
156 Known but unsupported control sequences:
159 class XclImpHFConverter
: protected XclImpRoot
, private boost::noncopyable
162 explicit XclImpHFConverter( const XclImpRoot
& rRoot
);
163 ~XclImpHFConverter();
165 /** Parses the passed string and creates three new edit engine text objects. */
166 void ParseString( const String
& rHFString
);
168 /** Creates a ScPageHFItem and inserts it into the passed item set. */
169 void FillToItemSet( SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
) const;
170 /** Returns the total height of the converted header or footer in twips. */
171 sal_Int32
GetTotalHeight() const;
174 typedef ::std::auto_ptr
< XclFontData
> XclFontDataPtr
;
176 /** Enumerates the supported header/footer portions. */
177 enum XclImpHFPortion
{ EXC_HF_LEFT
, EXC_HF_CENTER
, EXC_HF_RIGHT
, EXC_HF_PORTION_COUNT
};
179 /** Contains all information about a header/footer portion. */
180 struct XclImpHFPortionInfo
182 typedef boost::shared_ptr
< EditTextObject
> EditTextObjectRef
;
183 EditTextObjectRef mxObj
; /// Edit engine text object.
184 ESelection maSel
; /// Edit engine selection.
185 sal_Int32 mnHeight
; /// Height of previous lines in twips.
186 sal_uInt16 mnMaxLineHt
; /// Maximum font height for the current text line.
187 explicit XclImpHFPortionInfo();
189 typedef ::std::vector
< XclImpHFPortionInfo
> XclImpHFPortionInfoVec
;
192 /** Returns the current edit engine text object. */
193 inline XclImpHFPortionInfo
& GetCurrInfo() { return maInfos
[ meCurrObj
]; }
194 /** Returns the current edit engine text object. */
195 inline XclImpHFPortionInfo::EditTextObjectRef
& GetCurrObj() { return GetCurrInfo().mxObj
; }
196 /** Returns the current selection. */
197 inline ESelection
& GetCurrSel() { return GetCurrInfo().maSel
; }
199 /** Returns the maximum line height of the specified portion. */
200 sal_uInt16
GetMaxLineHeight( XclImpHFPortion ePortion
) const;
201 /** Returns the current maximum line height. */
202 sal_uInt16
GetCurrMaxLineHeight() const;
204 /** Updates the maximum line height of the specified portion, using the current font size. */
205 void UpdateMaxLineHeight( XclImpHFPortion ePortion
);
206 /** Updates the current maximum line height, using the current font size. */
207 void UpdateCurrMaxLineHeight();
209 /** Sets the font attributes at the current selection.
210 @descr After that, the start position of the current selection object is
211 adjusted to the end of the selection. */
213 /** Resets font data to application default font. */
214 void ResetFontData();
216 /** Inserts maCurrText into edit engine and adjusts the current selection object.
217 @descr The text shall not contain a newline character.
218 The text will be cleared after insertion. */
220 /** Inserts the passed text field and adjusts the current selection object. */
221 void InsertField( const SvxFieldItem
& rFieldItem
);
222 /** Inserts a line break and adjusts the current selection object. */
223 void InsertLineBreak();
225 /** Creates the edit engine text object of current portion from edit engine. */
226 void CreateCurrObject();
227 /** Changes current header/footer portion to eNew.
228 @descr Creates text object of current portion and reinitializes edit engine. */
229 void SetNewPortion( XclImpHFPortion eNew
);
232 EditEngine
& mrEE
; /// The header/footer edit engine.
233 XclImpHFPortionInfoVec maInfos
; /// Edit engine text objects for all portions.
234 String maCurrText
; /// Current text to insert into edit engine.
235 XclFontDataPtr mxFontData
; /// Font data of current text.
236 XclImpHFPortion meCurrObj
; /// The current portion.
239 // URL conversion =============================================================
241 /** This class contains static methods to decode an URL stored in an Excel file.
242 @descr Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
243 This sheet name will be extracted automatically. */
244 class XclImpUrlHelper
: boost::noncopyable
247 /** Decodes an encoded external document URL with optional sheet name.
248 @param rUrl Returns the decoded file name incl. path.
249 @param rTabName Returns the decoded sheet name.
250 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
251 @param rEncodedUrl An encoded URL from Excel. */
252 static void DecodeUrl(
256 const XclImpRoot
& rRoot
,
257 const String
& rEncodedUrl
);
259 /** Decodes an encoded external document URL without sheet name.
260 @param rUrl Returns the decoded file name incl. path.
261 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
262 @param rEncodedUrl An encoded URL from Excel. */
263 static void DecodeUrl(
266 const XclImpRoot
& rRoot
,
267 const String
& rEncodedUrl
);
269 static void DecodeUrl(
272 const XclImpRoot
& rRoot
,
273 const OUString
& rEncodedUrl
);
275 /** Decodes the passed URL to OLE or DDE link components.
276 @descr For DDE links: Decodes to application name and topic.
277 For OLE object links: Decodes to class name and document URL.
278 @return true = decoding was successful, returned strings are valid (not empty). */
279 static bool DecodeLink( String
& rApplic
, String
& rTopic
, const String rEncUrl
);
282 /** We don't want anybody to instantiate this class, since it is just a
283 collection of static methods. To enforce this, the default constructor
288 // Cached values ==============================================================
292 /** This class stores one cached value of a cached value list (used for instance in
293 CRN, EXTERNNAME, tArray). */
294 class XclImpCachedValue
: boost::noncopyable
297 /** Creates a cached value and reads contents from stream and stores it with its array address. */
298 explicit XclImpCachedValue( XclImpStream
& rStrm
);
299 virtual ~XclImpCachedValue();
301 /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
302 inline sal_uInt8
GetType() const { return mnType
; }
303 /** Returns the cached string value, if this value is a string, else an empty string. */
304 inline const String
& GetString() const { return mxStr
.get() ? *mxStr
: EMPTY_STRING
; }
305 /** Returns the cached number, if this value has number type, else 0.0. */
306 inline double GetValue() const { return mfValue
; }
307 /** Returns the cached Boolean value, if this value has Boolean type, else false. */
308 inline bool GetBool() const { return (mnType
== EXC_CACHEDVAL_BOOL
) && (mnBoolErr
!= 0); }
309 /** Returns the cached Calc error code, if this value has Error type, else 0. */
310 inline sal_uInt8
GetXclError() const { return (mnType
== EXC_CACHEDVAL_ERROR
) ? mnBoolErr
: EXC_ERR_NA
; }
311 /** Returns the cached Calc error code, if this value has Error type, else 0. */
312 sal_uInt16
GetScError() const;
315 typedef ::std::auto_ptr
< String
> StringPtr
;
316 typedef ::std::auto_ptr
< const ScTokenArray
> ScTokenArrayPtr
;
318 StringPtr mxStr
; /// Cached value is a string.
319 double mfValue
; /// Cached value is a double.
320 ScTokenArrayPtr mxTokArr
; /// Cached value is a formula or error code or Boolean.
321 sal_uInt8 mnBoolErr
; /// Boolean value or Excel error code.
322 sal_uInt8 mnType
; /// The type of the cached value (EXC_CACHEDVAL_*).
325 // ----------------------------------------------------------------------------
327 /** Contains cached values in a 2-dimensional array. */
328 class XclImpCachedMatrix
331 explicit XclImpCachedMatrix( XclImpStream
& rStrm
);
332 ~XclImpCachedMatrix();
334 /** Creates a new ScMatrix object and fills it with the contained values. */
335 ScMatrixRef
CreateScMatrix() const;
338 typedef boost::ptr_vector
< XclImpCachedValue
> XclImpValueList
;
340 XclImpValueList maValueList
; /// List of cached cell values.
341 SCSIZE mnScCols
; /// Number of cached columns.
342 SCSIZE mnScRows
; /// Number of cached rows.
345 // ============================================================================
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */