xref: /aoo41x/main/sc/source/filter/inc/xihelper.hxx (revision 38d50f7b)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SC_XIHELPER_HXX
25 #define SC_XIHELPER_HXX
26 
27 #include <editeng/editdata.hxx>
28 #include "scmatrix.hxx"
29 #include "xladdress.hxx"
30 #include "xiroot.hxx"
31 #include "xistring.hxx"
32 
33 // Excel->Calc cell address/range conversion ==================================
34 
35 /** Provides functions to convert Excel cell addresses to Calc cell addresses. */
36 class XclImpAddressConverter : public XclAddressConverterBase
37 {
38 public:
39     explicit            XclImpAddressConverter( const XclImpRoot& rRoot );
40 
41     // cell address -----------------------------------------------------------
42 
43     /** Checks if the passed Excel cell address is valid.
44         @param rXclPos  The Excel cell address to check.
45         @param bWarn  true = Sets the internal flag that produces a warning box
46             after loading/saving the file, if the cell address is not valid.
47         @return  true = Cell address in rXclPos is valid. */
48     bool                CheckAddress( const XclAddress& rXclPos, bool bWarn );
49 
50     /** Converts the passed Excel cell address to a Calc cell address.
51         @param rScPos  (Out) The converted Calc cell address, if valid.
52         @param rXclPos  The Excel cell address to convert.
53         @param bWarn  true = Sets the internal flag that produces a warning box
54             after loading/saving the file, if the cell address is invalid.
55         @return  true = Cell address returned in rScPos is valid. */
56     bool                ConvertAddress( ScAddress& rScPos,
57                             const XclAddress& rXclPos, SCTAB nScTab, bool bWarn );
58 
59     /** Returns a valid cell address by moving it into allowed dimensions.
60         @param rXclPos  The Excel cell address to convert.
61         @param bWarn  true = Sets the internal flag that produces a warning box
62             after loading/saving the file, if the cell address is invalid.
63         @return  The converted Calc cell address. */
64     ScAddress           CreateValidAddress( const XclAddress& rXclPos,
65                             SCTAB nScTab, bool bWarn );
66 
67     // cell range -------------------------------------------------------------
68 
69     /** Checks if the passed cell range is valid (checks start and end position).
70         @param rXclRange  The Excel cell range to check.
71         @param bWarn  true = Sets the internal flag that produces a warning box
72             after loading/saving the file, if the cell range is not valid.
73         @return  true = Cell range in rXclRange is valid. */
74     bool                CheckRange( const XclRange& rXclRange, bool bWarn );
75 
76     /** Converts the passed Excel cell range to a Calc cell range.
77         @param rScRange  (Out) The converted Calc cell range, if valid.
78         @param rXclRange  The Excel cell range to convert.
79         @param bWarn  true = Sets the internal flag that produces a warning box
80             after loading/saving the file, if the cell range contains invalid cells.
81         @return  true = Cell range returned in rScRange is valid (original or cropped). */
82     bool                ConvertRange( ScRange& rScRange, const XclRange& rXclRange,
83                             SCTAB nScTab1, SCTAB nScTab2, bool bWarn );
84 
85 //UNUSED2009-05 /** Returns a valid cell range by moving it into allowed dimensions.
86 //UNUSED2009-05     @descr  The start and/or end position of the range may be modified.
87 //UNUSED2009-05     @param rXclRange  The Excel cell range to convert.
88 //UNUSED2009-05     @param bWarn  true = Sets the internal flag that produces a warning box
89 //UNUSED2009-05         after loading/saving the file, if the cell range contains invalid cells.
90 //UNUSED2009-05     @return  The converted Calc cell range. */
91 //UNUSED2009-05 ScRange             CreateValidRange( const XclRange& rXclRange,
92 //UNUSED2009-05                         SCTAB nScTab1, SCTAB nScTab2, bool bWarn );
93 
94     // cell range list --------------------------------------------------------
95 
96 //UNUSED2009-05 /** Checks if the passed cell range list is valid.
97 //UNUSED2009-05     @param rXclRanges  The Excel cell range list to check.
98 //UNUSED2009-05     @param bWarn  true = Sets the internal flag that produces a warning box
99 //UNUSED2009-05         after loading/saving the file, if the cell range list contains at
100 //UNUSED2009-05         least one invalid range.
101 //UNUSED2009-05     @return  true = Cell range list in rScRanges is completly valid. */
102 //UNUSED2009-05 bool                CheckRangeList( const XclRangeList& rXclRanges, bool bWarn );
103 
104     /** Converts the passed Excel cell range list to a Calc cell range list.
105         @descr  The start position of the ranges will not be modified. Cell
106             ranges that fit partly into valid dimensions are cropped
107             accordingly. Cell ranges that do not fit at all, are not inserted
108             into the Calc cell range list.
109         @param rScRanges  (Out) The converted Calc cell range list.
110         @param rXclRanges  The Excel cell range list to convert.
111         @param bWarn  true = Sets the internal flag that produces a warning box
112             after loading/saving the file, if at least one of the cell ranges
113             contains invalid cells. */
114     void                ConvertRangeList( ScRangeList& rScRanges,
115                             const XclRangeList& rXclRanges, SCTAB nScTab, bool bWarn );
116 };
117 
118 // String->EditEngine conversion ==============================================
119 
120 class ScBaseCell;
121 class EditTextObject;
122 
123 /** This class provides methods to convert an XclImpString.
124     @The string can be converted to an edit engine text object or directly
125     to a Calc edit cell. */
126 class XclImpStringHelper : ScfNoInstance
127 {
128 public:
129     /** Returns a new edit engine text object.
130         @param nXFIndex  Index to XF for first text portion (for escapement). */
131     static EditTextObject* CreateTextObject(
132                             const XclImpRoot& rRoot,
133                             const XclImpString& rString );
134 
135 //UNUSED2009-05 /** Returns a new edit engine text object for a cell note.
136 //UNUSED2009-05     @param nXFIndex  Index to XF for first text portion (for escapement). */
137 //UNUSED2009-05 static EditTextObject* CreateNoteObject(
138 //UNUSED2009-05                         const XclImpRoot& rRoot,
139 //UNUSED2009-05                         const XclImpString& rString );
140 
141     /** Creates a new text cell or edit cell for a Calc document.
142         @param nXFIndex  Index to XF for first text portion (for escapement). */
143     static ScBaseCell*  CreateCell(
144                             const XclImpRoot& rRoot,
145                             const XclImpString& rString,
146                             sal_uInt16 nXFIndex = 0 );
147 };
148 
149 // Header/footer conversion ===================================================
150 
151 class EditEngine;
152 class EditTextObject;
153 class SfxItemSet;
154 class SvxFieldItem;
155 struct XclFontData;
156 
157 /** Converts an Excel header/footer string into three edit engine text objects.
158     @descr  Header/footer content is divided into three parts: Left, center and
159     right portion. All formatting information is encoded in the Excel string
160     using special character seuences. A control sequence starts with the ampersand
161     character.
162 
163     Supported control sequences:
164     &L                      start of left portion
165     &C                      start of center portion
166     &R                      start of right portion
167     &P                      current page number
168     &N                      page count
169     &D                      current date
170     &T                      current time
171     &A                      table name
172     &F                      file name without path (see also &Z&F)
173     &Z                      file path without file name (converted to full file name, see also &Z&F)
174     &Z&F                    file path and name
175     &U                      underlining on/off
176     &E                      double underlining on/off
177     &S                      strikeout characters on/off
178     &X                      superscript on/off
179     &Y                      subscript on/off
180     &"fontname,fontstyle"   use font with name 'fontname' and style 'fontstyle'
181     &fontheight             set font height in points ('fontheight' is a decimal value)
182 
183     Known but unsupported control sequences:
184     &G                      picture
185  */
186 class XclImpHFConverter : protected XclImpRoot, ScfNoCopy
187 {
188 public:
189     explicit            XclImpHFConverter( const XclImpRoot& rRoot );
190                         ~XclImpHFConverter();
191 
192     /** Parses the passed string and creates three new edit engine text objects. */
193     void                ParseString( const String& rHFString );
194 
195     /** Creates a ScPageHFItem and inserts it into the passed item set. */
196     void                FillToItemSet( SfxItemSet& rItemSet, sal_uInt16 nWhichId ) const;
197     /** Returns the total height of the converted header or footer in twips. */
198     sal_Int32           GetTotalHeight() const;
199 
200 private:    // types
201     typedef ::std::auto_ptr< XclFontData > XclFontDataPtr;
202 
203     /** Enumerates the supported header/footer portions. */
204     enum XclImpHFPortion { EXC_HF_LEFT, EXC_HF_CENTER, EXC_HF_RIGHT, EXC_HF_PORTION_COUNT };
205 
206     /** Contains all information about a header/footer portion. */
207     struct XclImpHFPortionInfo
208     {
209         typedef ScfRef< EditTextObject > EditTextObjectRef;
210         EditTextObjectRef   mxObj;          /// Edit engine text object.
211         ESelection          maSel;          /// Edit engine selection.
212         sal_Int32           mnHeight;       /// Height of previous lines in twips.
213         sal_uInt16          mnMaxLineHt;    /// Maximum font height for the current text line.
214         explicit            XclImpHFPortionInfo();
215     };
216     typedef ::std::vector< XclImpHFPortionInfo > XclImpHFPortionInfoVec;
217 
218 private:
219     /** Returns the current edit engine text object. */
GetCurrInfo()220     inline XclImpHFPortionInfo& GetCurrInfo() { return maInfos[ meCurrObj ]; }
221     /** Returns the current edit engine text object. */
GetCurrObj()222     inline XclImpHFPortionInfo::EditTextObjectRef& GetCurrObj() { return GetCurrInfo().mxObj; }
223     /** Returns the current selection. */
GetCurrSel()224     inline ESelection&  GetCurrSel() { return GetCurrInfo().maSel; }
225 
226     /** Returns the maximum line height of the specified portion. */
227     sal_uInt16          GetMaxLineHeight( XclImpHFPortion ePortion ) const;
228     /** Returns the current maximum line height. */
229     sal_uInt16          GetCurrMaxLineHeight() const;
230 
231     /** Updates the maximum line height of the specified portion, using the current font size. */
232     void                UpdateMaxLineHeight( XclImpHFPortion ePortion );
233     /** Updates the current maximum line height, using the current font size. */
234     void                UpdateCurrMaxLineHeight();
235 
236     /** Sets the font attributes at the current selection.
237         @descr  After that, the start position of the current selection object is
238         adjusted to the end of the selection. */
239     void                SetAttribs();
240     /** Resets font data to application default font. */
241     void                ResetFontData();
242 
243     /** Inserts maCurrText into edit engine and adjusts the current selection object.
244         @descr  The text shall not contain a newline character.
245         The text will be cleared after insertion. */
246     void                InsertText();
247     /** Inserts the passed text field and adjusts the current selection object. */
248     void                InsertField( const SvxFieldItem& rFieldItem );
249     /** Inserts a line break and adjusts the current selection object. */
250     void                InsertLineBreak();
251 
252     /** Creates the edit engine text object of current portion from edit engine. */
253     void                CreateCurrObject();
254     /** Changes current header/footer portion to eNew.
255         @descr  Creates text object of current portion and reinitializes edit engine. */
256     void                SetNewPortion( XclImpHFPortion eNew );
257 
258 private:
259     EditEngine&         mrEE;               /// The header/footer edit engine.
260     XclImpHFPortionInfoVec maInfos;         /// Edit engine text objects for all portions.
261     String              maCurrText;         /// Current text to insert into edit engine.
262     XclFontDataPtr      mxFontData;         /// Font data of current text.
263     XclImpHFPortion     meCurrObj;          /// The current portion.
264 };
265 
266 // URL conversion =============================================================
267 
268 /** This class contains static methods to decode an URL stored in an Excel file.
269     @descr  Excel URLs can contain a sheet name, for instance: path\[test.xls]Sheet1
270     This sheet name will be extracted automatically. */
271 class XclImpUrlHelper : ScfNoInstance
272 {
273 public:
274     /** Decodes an encoded external document URL with optional sheet name.
275         @param rUrl  Returns the decoded file name incl. path.
276         @param rTabName  Returns the decoded sheet name.
277         @param rbSameWb  Returns true, if the URL is a reference to the own workbook.
278         @param rEncodedUrl   An encoded URL from Excel. */
279     static void         DecodeUrl(
280                             String& rUrl,
281                             String& rTabName,
282                             bool& rbSameWb,
283                             const XclImpRoot& rRoot,
284                             const String& rEncodedUrl );
285 
286     /** Decodes an encoded external document URL without sheet name.
287         @param rUrl  Returns the decoded file name incl. path.
288         @param rbSameWb  Returns true, if the URL is a reference to the own workbook.
289         @param rEncodedUrl   An encoded URL from Excel. */
290     static void         DecodeUrl(
291                             String& rUrl,
292                             bool& rbSameWb,
293                             const XclImpRoot& rRoot,
294                             const String& rEncodedUrl );
295 
296     /** Decodes the passed URL to OLE or DDE link components.
297         @descr  For DDE links: Decodes to application name and topic.
298         For OLE object links: Decodes to class name and document URL.
299         @return  true = decoding was successful, returned strings are valid (not empty). */
300     static bool         DecodeLink( String& rApplic, String& rTopic, const String rEncUrl );
301 };
302 
303 // Cached values ==============================================================
304 
305 class ScTokenArray;
306 
307 /** This class stores one cached value of a cached value list (used for instance in
308     CRN, EXTERNNAME, tArray). */
309 class XclImpCachedValue : ScfNoCopy
310 {
311 public:
312     /** Creates a cached value and reads contents from stream and stores it with its array address. */
313     explicit            XclImpCachedValue( XclImpStream& rStrm );
314     virtual             ~XclImpCachedValue();
315 
316     /** Returns the type of the cached value (EXC_CACHEDVAL_*). */
GetType() const317     inline sal_uInt8    GetType() const     { return mnType; }
318     /** Returns the cached string value, if this value is a string, else an empty string. */
GetString() const319     inline const String& GetString() const  { return mxStr.get() ? *mxStr : EMPTY_STRING; }
320     /** Returns the cached number, if this value has number type, else 0.0. */
GetValue() const321     inline double       GetValue() const    { return mfValue; }
322     /** Returns the cached Boolean value, if this value has Boolean type, else false. */
GetBool() const323     inline bool         GetBool() const     { return (mnType == EXC_CACHEDVAL_BOOL) && (mnBoolErr != 0); }
324     /** Returns the cached Calc error code, if this value has Error type, else 0. */
GetXclError() const325     inline sal_uInt8    GetXclError() const { return (mnType == EXC_CACHEDVAL_ERROR) ? mnBoolErr : EXC_ERR_NA; }
326     /** Returns the cached Calc error code, if this value has Error type, else 0. */
327     sal_uInt16              GetScError() const;
328     /** Returns the token array if this is a Boolean value or error value, else 0. */
GetBoolErrFmla() const329     inline const ScTokenArray* GetBoolErrFmla() const { return mxTokArr.get(); }
330 
331 protected:
332     typedef ::std::auto_ptr< String >               StringPtr;
333     typedef ::std::auto_ptr< const ScTokenArray >   ScTokenArrayPtr;
334 
335     StringPtr           mxStr;      /// Cached value is a string.
336     double              mfValue;    /// Cached value is a double.
337     ScTokenArrayPtr     mxTokArr;   /// Cached value is a formula or error code or Boolean.
338     sal_uInt8           mnBoolErr;  /// Boolean value or Excel error code.
339     sal_uInt8           mnType;     /// The type of the cached value (EXC_CACHEDVAL_*).
340 };
341 
342 // ----------------------------------------------------------------------------
343 
344 /** Contains cached values in a 2-dimensional array. */
345 class XclImpCachedMatrix
346 {
347 public:
348     explicit            XclImpCachedMatrix( XclImpStream& rStrm );
349                         ~XclImpCachedMatrix();
350 
351     /** Creates a new ScMatrix object and fills it with the contained values. */
352     ScMatrixRef         CreateScMatrix() const;
353 
354 private:
355     typedef ScfDelList< XclImpCachedValue > XclImpValueList;
356 
357     XclImpValueList     maValueList;    /// List of cached cell values.
358     SCSIZE              mnScCols;       /// Number of cached columns.
359     SCSIZE              mnScRows;       /// Number of cached rows.
360 };
361 
362 // ============================================================================
363 
364 #endif
365 
366