1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: xihelper.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SC_XIHELPER_HXX
32 #define SC_XIHELPER_HXX
34 #include <svx/editdata.hxx>
35 #include "scmatrix.hxx"
36 #include "xladdress.hxx"
38 #include "xistring.hxx"
40 // Excel->Calc cell address/range conversion ==================================
42 /** Provides functions to convert Excel cell addresses to Calc cell addresses. */
43 class XclImpAddressConverter
: public XclAddressConverterBase
46 explicit XclImpAddressConverter( const XclImpRoot
& rRoot
);
48 // cell address -----------------------------------------------------------
50 /** Checks if the passed Excel cell address is valid.
51 @param rXclPos The Excel cell address to check.
52 @param bWarn true = Sets the internal flag that produces a warning box
53 after loading/saving the file, if the cell address is not valid.
54 @return true = Cell address in rXclPos is valid. */
55 bool CheckAddress( const XclAddress
& rXclPos
, bool bWarn
);
57 /** Converts the passed Excel cell address to a Calc cell address.
58 @param rScPos (Out) The converted Calc cell address, if valid.
59 @param rXclPos The Excel cell address to convert.
60 @param bWarn true = Sets the internal flag that produces a warning box
61 after loading/saving the file, if the cell address is invalid.
62 @return true = Cell address returned in rScPos is valid. */
63 bool ConvertAddress( ScAddress
& rScPos
,
64 const XclAddress
& rXclPos
, SCTAB nScTab
, bool bWarn
);
66 /** Returns a valid cell address by moving it into allowed dimensions.
67 @param rXclPos The Excel cell address to convert.
68 @param bWarn true = Sets the internal flag that produces a warning box
69 after loading/saving the file, if the cell address is invalid.
70 @return The converted Calc cell address. */
71 ScAddress
CreateValidAddress( const XclAddress
& rXclPos
,
72 SCTAB nScTab
, bool bWarn
);
74 // cell range -------------------------------------------------------------
76 /** Checks if the passed cell range is valid (checks start and end position).
77 @param rXclRange The Excel cell range to check.
78 @param bWarn true = Sets the internal flag that produces a warning box
79 after loading/saving the file, if the cell range is not valid.
80 @return true = Cell range in rXclRange is valid. */
81 bool CheckRange( const XclRange
& rXclRange
, bool bWarn
);
83 /** Converts the passed Excel cell range to a Calc cell range.
84 @param rScRange (Out) The converted Calc cell range, if valid.
85 @param rXclRange The Excel cell range to convert.
86 @param bWarn true = Sets the internal flag that produces a warning box
87 after loading/saving the file, if the cell range contains invalid cells.
88 @return true = Cell range returned in rScRange is valid (original or cropped). */
89 bool ConvertRange( ScRange
& rScRange
, const XclRange
& rXclRange
,
90 SCTAB nScTab1
, SCTAB nScTab2
, bool bWarn
);
92 //UNUSED2009-05 /** Returns a valid cell range by moving it into allowed dimensions.
93 //UNUSED2009-05 @descr The start and/or end position of the range may be modified.
94 //UNUSED2009-05 @param rXclRange The Excel cell range to convert.
95 //UNUSED2009-05 @param bWarn true = Sets the internal flag that produces a warning box
96 //UNUSED2009-05 after loading/saving the file, if the cell range contains invalid cells.
97 //UNUSED2009-05 @return The converted Calc cell range. */
98 //UNUSED2009-05 ScRange CreateValidRange( const XclRange& rXclRange,
99 //UNUSED2009-05 SCTAB nScTab1, SCTAB nScTab2, bool bWarn );
101 // cell range list --------------------------------------------------------
103 //UNUSED2009-05 /** Checks if the passed cell range list is valid.
104 //UNUSED2009-05 @param rXclRanges The Excel cell range list to check.
105 //UNUSED2009-05 @param bWarn true = Sets the internal flag that produces a warning box
106 //UNUSED2009-05 after loading/saving the file, if the cell range list contains at
107 //UNUSED2009-05 least one invalid range.
108 //UNUSED2009-05 @return true = Cell range list in rScRanges is completly valid. */
109 //UNUSED2009-05 bool CheckRangeList( const XclRangeList& rXclRanges, bool bWarn );
111 /** Converts the passed Excel cell range list to a Calc cell range list.
112 @descr The start position of the ranges will not be modified. Cell
113 ranges that fit partly into valid dimensions are cropped
114 accordingly. Cell ranges that do not fit at all, are not inserted
115 into the Calc cell range list.
116 @param rScRanges (Out) The converted Calc cell range list.
117 @param rXclRanges The Excel cell range list to convert.
118 @param bWarn true = Sets the internal flag that produces a warning box
119 after loading/saving the file, if at least one of the cell ranges
120 contains invalid cells. */
121 void ConvertRangeList( ScRangeList
& rScRanges
,
122 const XclRangeList
& rXclRanges
, SCTAB nScTab
, bool bWarn
);
125 // String->EditEngine conversion ==============================================
128 class EditTextObject
;
130 /** This class provides methods to convert an XclImpString.
131 @The string can be converted to an edit engine text object or directly
132 to a Calc edit cell. */
133 class XclImpStringHelper
: ScfNoInstance
136 /** Returns a new edit engine text object.
137 @param nXFIndex Index to XF for first text portion (for escapement). */
138 static EditTextObject
* CreateTextObject(
139 const XclImpRoot
& rRoot
,
140 const XclImpString
& rString
);
142 //UNUSED2009-05 /** Returns a new edit engine text object for a cell note.
143 //UNUSED2009-05 @param nXFIndex Index to XF for first text portion (for escapement). */
144 //UNUSED2009-05 static EditTextObject* CreateNoteObject(
145 //UNUSED2009-05 const XclImpRoot& rRoot,
146 //UNUSED2009-05 const XclImpString& rString );
148 /** Creates a new text cell or edit cell for a Calc document.
149 @param nXFIndex Index to XF for first text portion (for escapement). */
150 static ScBaseCell
* CreateCell(
151 const XclImpRoot
& rRoot
,
152 const XclImpString
& rString
,
153 sal_uInt16 nXFIndex
= 0 );
156 // Header/footer conversion ===================================================
159 class EditTextObject
;
164 /** Converts an Excel header/footer string into three edit engine text objects.
165 @descr Header/footer content is divided into three parts: Left, center and
166 right portion. All formatting information is encoded in the Excel string
167 using special character seuences. A control sequence starts with the ampersand
170 Supported control sequences:
171 &L start of left portion
172 &C start of center portion
173 &R start of right portion
174 &P current page number
179 &F file name without path (see also &Z&F)
180 &Z file path without file name (converted to full file name, see also &Z&F)
181 &Z&F file path and name
182 &U underlining on/off
183 &E double underlining on/off
184 &S strikeout characters on/off
185 &X superscript on/off
187 &"fontname,fontstyle" use font with name 'fontname' and style 'fontstyle'
188 &fontheight set font height in points ('fontheight' is a decimal value)
190 Known but unsupported control sequences:
193 class XclImpHFConverter
: protected XclImpRoot
, ScfNoCopy
196 explicit XclImpHFConverter( const XclImpRoot
& rRoot
);
197 ~XclImpHFConverter();
199 /** Parses the passed string and creates three new edit engine text objects. */
200 void ParseString( const String
& rHFString
);
202 /** Creates a ScPageHFItem and inserts it into the passed item set. */
203 void FillToItemSet( SfxItemSet
& rItemSet
, sal_uInt16 nWhichId
) const;
204 /** Returns the total height of the converted header or footer in twips. */
205 sal_Int32
GetTotalHeight() const;
208 typedef ::std::auto_ptr
< XclFontData
> XclFontDataPtr
;
210 /** Enumerates the supported header/footer portions. */
211 enum XclImpHFPortion
{ EXC_HF_LEFT
, EXC_HF_CENTER
, EXC_HF_RIGHT
, EXC_HF_PORTION_COUNT
};
213 /** Contains all information about a header/footer portion. */
214 struct XclImpHFPortionInfo
216 typedef ScfRef
< EditTextObject
> EditTextObjectRef
;
217 EditTextObjectRef mxObj
; /// Edit engine text object.
218 ESelection maSel
; /// Edit engine selection.
219 sal_Int32 mnHeight
; /// Height of previous lines in twips.
220 sal_uInt16 mnMaxLineHt
; /// Maximum font height for the current text line.
221 explicit XclImpHFPortionInfo();
223 typedef ::std::vector
< XclImpHFPortionInfo
> XclImpHFPortionInfoVec
;
226 /** Returns the current edit engine text object. */
227 inline XclImpHFPortionInfo
& GetCurrInfo() { return maInfos
[ meCurrObj
]; }
228 /** Returns the current edit engine text object. */
229 inline XclImpHFPortionInfo::EditTextObjectRef
& GetCurrObj() { return GetCurrInfo().mxObj
; }
230 /** Returns the current selection. */
231 inline ESelection
& GetCurrSel() { return GetCurrInfo().maSel
; }
233 /** Returns the maximum line height of the specified portion. */
234 sal_uInt16
GetMaxLineHeight( XclImpHFPortion ePortion
) const;
235 /** Returns the current maximum line height. */
236 sal_uInt16
GetCurrMaxLineHeight() const;
238 /** Updates the maximum line height of the specified portion, using the current font size. */
239 void UpdateMaxLineHeight( XclImpHFPortion ePortion
);
240 /** Updates the current maximum line height, using the current font size. */
241 void UpdateCurrMaxLineHeight();
243 /** Sets the font attributes at the current selection.
244 @descr After that, the start position of the current selection object is
245 adjusted to the end of the selection. */
247 /** Resets font data to application default font. */
248 void ResetFontData();
250 /** Inserts maCurrText into edit engine and adjusts the current selection object.
251 @descr The text shall not contain a newline character.
252 The text will be cleared after insertion. */
254 /** Inserts the passed text field and adjusts the current selection object. */
255 void InsertField( const SvxFieldItem
& rFieldItem
);
256 /** Inserts a line break and adjusts the current selection object. */
257 void InsertLineBreak();
259 /** Creates the edit engine text object of current portion from edit engine. */
260 void CreateCurrObject();
261 /** Changes current header/footer portion to eNew.
262 @descr Creates text object of current portion and reinitializes edit engine. */
263 void SetNewPortion( XclImpHFPortion eNew
);
266 EditEngine
& mrEE
; /// The header/footer edit engine.
267 XclImpHFPortionInfoVec maInfos
; /// Edit engine text objects for all portions.
268 String maCurrText
; /// Current text to insert into edit engine.
269 XclFontDataPtr mxFontData
; /// Font data of current text.
270 XclImpHFPortion meCurrObj
; /// The current portion.
273 // URL conversion =============================================================
275 /** This class contains static methods to decode an URL stored in an Excel file.
276 @descr Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
277 This sheet name will be extracted automatically. */
278 class XclImpUrlHelper
: ScfNoInstance
281 /** Decodes an encoded external document URL with optional sheet name.
282 @param rUrl Returns the decoded file name incl. path.
283 @param rTabName Returns the decoded sheet name.
284 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
285 @param rEncodedUrl An encoded URL from Excel. */
286 static void DecodeUrl(
290 const XclImpRoot
& rRoot
,
291 const String
& rEncodedUrl
);
293 /** Decodes an encoded external document URL without sheet name.
294 @param rUrl Returns the decoded file name incl. path.
295 @param rbSameWb Returns true, if the URL is a reference to the own workbook.
296 @param rEncodedUrl An encoded URL from Excel. */
297 static void DecodeUrl(
300 const XclImpRoot
& rRoot
,
301 const String
& rEncodedUrl
);
303 /** Decodes the passed URL to OLE or DDE link components.
304 @descr For DDE links: Decodes to application name and topic.
305 For OLE object links: Decodes to class name and document URL.
306 @return true = decoding was successful, returned strings are valid (not empty). */
307 static bool DecodeLink( String
& rApplic
, String
& rTopic
, const String rEncUrl
);
310 // Cached values ==============================================================
314 /** This class stores one cached value of a cached value list (used for instance in
315 CRN, EXTERNNAME, tArray). */
316 class XclImpCachedValue
: ScfNoCopy
319 /** Creates a cached value and reads contents from stream and stores it with its array address. */
320 explicit XclImpCachedValue( XclImpStream
& rStrm
);
321 virtual ~XclImpCachedValue();
323 /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
324 inline sal_uInt8
GetType() const { return mnType
; }
325 /** Returns the cached string value, if this value is a string, else an empty string. */
326 inline const String
& GetString() const { return mxStr
.get() ? *mxStr
: EMPTY_STRING
; }
327 /** Returns the cached number, if this value has number type, else 0.0. */
328 inline double GetValue() const { return mfValue
; }
329 /** Returns the cached Boolean value, if this value has Boolean type, else false. */
330 inline bool GetBool() const { return (mnType
== EXC_CACHEDVAL_BOOL
) && (mnBoolErr
!= 0); }
331 /** Returns the cached Calc error code, if this value has Error type, else 0. */
332 USHORT
GetError() const;
333 /** Returns the token array if this is a Boolean value or error value, else 0. */
334 inline const ScTokenArray
* GetBoolErrFmla() const { return mxTokArr
.get(); }
337 typedef ::std::auto_ptr
< String
> StringPtr
;
338 typedef ::std::auto_ptr
< const ScTokenArray
> ScTokenArrayPtr
;
340 StringPtr mxStr
; /// Cached value is a string.
341 double mfValue
; /// Cached value is a double.
342 ScTokenArrayPtr mxTokArr
; /// Cached value is a formula or error code or Boolean.
343 sal_uInt8 mnBoolErr
; /// Boolean value or Excel error code.
344 sal_uInt8 mnType
; /// The type of the cached value (EXC_CACHEDVAL_*).
347 // ----------------------------------------------------------------------------
349 /** Contains cached values in a 2-dimensional array. */
350 class XclImpCachedMatrix
353 explicit XclImpCachedMatrix( XclImpStream
& rStrm
);
354 ~XclImpCachedMatrix();
356 /** Creates a new ScMatrix object and fills it with the contained values. */
357 ScMatrixRef
CreateScMatrix() const;
360 typedef ScfDelList
< XclImpCachedValue
> XclImpValueList
;
362 XclImpValueList maValueList
; /// List of cached cell values.
363 SCSIZE mnScCols
; /// Number of cached columns.
364 SCSIZE mnScRows
; /// Number of cached rows.
367 // ============================================================================