xref: /aoo41x/main/sc/source/filter/inc/xehelper.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_XEHELPER_HXX
25 #define SC_XEHELPER_HXX
26 
27 #include "xladdress.hxx"
28 #include "xeroot.hxx"
29 #include "xestring.hxx"
30 
31 // Export progress bar ========================================================
32 
33 class ScfProgressBar;
34 
35 /** The main progress bar for the export filter.
36 
37     This class encapsulates creation and initialization of sub progress
38     segments. The Activate***Segment() functions activate a specific segement
39     of the main progress bar. The implementation of these functions contain the
40     calculation of the needed size of the segment. Following calls of the
41     Progress() function increase the currently activated sub segment.
42  */
43 class XclExpProgressBar : protected XclExpRoot
44 {
45 public:
46     explicit            XclExpProgressBar( const XclExpRoot& rRoot );
47     virtual             ~XclExpProgressBar();
48 
49     /** Initializes all segments and sub progress bars. */
50     void                Initialize();
51 
52     /** Increases the number of existing ROW records by 1. */
53     void                IncRowRecordCount();
54 
55     /** Activates the progress segment to create ROW records. */
56     void                ActivateCreateRowsSegment();
57     /** Activates the progress segment to finalize ROW records. */
58     void                ActivateFinalRowsSegment();
59 
60     /** Increases the currently activated (sub) progress bar by 1 step. */
61     void                Progress();
62 
63 private:
64     typedef ::std::auto_ptr< ScfProgressBar > ScfProgressBarPtr;
65 
66     ScfProgressBarPtr   mxProgress;         /// Progress bar implementation.
67     ScfProgressBar*     mpSubProgress;      /// Current sub progress bar.
68 
69     ScfProgressBar*     mpSubRowCreate;     /// Sub progress bar for creating table rows.
70     ScfInt32Vec         maSubSegRowCreate;  /// Segment ID's for all sheets in sub progress bar.
71 
72     ScfProgressBar*     mpSubRowFinal;      /// Sub progress bar for finalizing ROW records.
73     sal_Int32           mnSegRowFinal;      /// Progress segment for finalizing ROW records.
74 
75     sal_Size            mnRowCount;         /// Number of created ROW records.
76 };
77 
78 // Calc->Excel cell address/range conversion ==================================
79 
80 /** Provides functions to convert Calc cell addresses to Excel cell addresses. */
81 class XclExpAddressConverter : public XclAddressConverterBase
82 {
83 public:
84     explicit            XclExpAddressConverter( const XclExpRoot& rRoot );
85 
86     // cell address -----------------------------------------------------------
87 
88     /** Checks if the passed Calc cell address is valid.
89         @param rScPos  The Calc cell address to check.
90         @param bWarn  true = Sets the internal flag that produces a warning box
91             after loading/saving the file, if the cell address is not valid.
92         @return  true = Cell address in rScPos is valid. */
93     bool                CheckAddress( const ScAddress& rScPos, bool bWarn );
94 
95     /** Converts the passed Calc cell address to an Excel cell address.
96         @param rXclPos  (Out) The converted Excel cell address, if valid.
97         @param rScPos  The Calc cell address to convert.
98         @param bWarn  true = Sets the internal flag that produces a warning box
99             after loading/saving the file, if the cell address is not valid.
100         @return  true = Cell address returned in rXclPos is valid. */
101     bool                ConvertAddress( XclAddress& rXclPos,
102                             const ScAddress& rScPos, bool bWarn );
103 
104     /** Returns a valid cell address by moving it into allowed dimensions.
105         @param rScPos  The Calc cell address to convert.
106         @param bWarn  true = Sets the internal flag that produces a warning box
107             after loading/saving the file, if the cell address is invalid.
108         @return  The converted Excel cell address. */
109     XclAddress          CreateValidAddress( const ScAddress& rScPos, bool bWarn );
110 
111     // cell range -------------------------------------------------------------
112 
113     /** Checks if the passed cell range is valid (checks start and end position).
114         @param rScRange  The Calc cell range to check.
115         @param bWarn  true = Sets the internal flag that produces a warning box
116             after loading/saving the file, if the cell range is not valid.
117         @return  true = Cell range in rScRange is valid. */
118     bool                CheckRange( const ScRange& rScRange, bool bWarn );
119 
120     /** Checks and eventually crops the cell range to valid dimensions.
121         @descr  The start position of the range will not be modified.
122         @param rScRange  (In/out) The cell range to validate.
123         @param bWarn  true = Sets the internal flag that produces a warning box
124             after loading/saving the file, if the cell range contains invalid
125             cells. If the range is partly valid, this function sets the warning
126             flag, corrects the range and returns true.
127         @return  true = Cell range in rScRange is valid (original or cropped). */
128     bool                ValidateRange( ScRange& rScRange, bool bWarn );
129 
130     /** Converts the passed Calc cell range to an Excel cell range.
131         @param rXclRange  (Out) The converted Excel cell range, if valid.
132         @param rScRange  The Calc cell range to convert.
133         @param bWarn  true = Sets the internal flag that produces a warning box
134             after loading/saving the file, if the cell range contains invalid cells.
135         @return  true = Cell range returned in rXclRange is valid (original or cropped). */
136     bool                ConvertRange( XclRange& rXclRange, const ScRange& rScRange, bool bWarn );
137 
138 //UNUSED2008-05  /** Returns a valid cell range by moving it into allowed dimensions.
139 //UNUSED2008-05      @descr  The start and/or end position of the range may be modified.
140 //UNUSED2008-05      @param rScRange  The Calc cell range to convert.
141 //UNUSED2008-05      @param bWarn  true = Sets the internal flag that produces a warning box
142 //UNUSED2008-05          after loading/saving the file, if the cell range contains invalid cells.
143 //UNUSED2008-05      @return  The converted Excel cell range. */
144 //UNUSED2008-05  XclRange            CreateValidRange( const ScRange& rScRange, bool bWarn );
145 
146     // cell range list --------------------------------------------------------
147 
148 //UNUSED2008-05  /** Checks if the passed cell range list is valid.
149 //UNUSED2008-05      @param rScRanges  The Calc cell range list to check.
150 //UNUSED2008-05      @param bWarn  true = Sets the internal flag that produces a warning box
151 //UNUSED2008-05          after loading/saving the file, if the cell range list contains at
152 //UNUSED2008-05          least one invalid range.
153 //UNUSED2008-05      @return  true = Cell range list in rScRanges is completly valid. */
154 //UNUSED2008-05  bool                CheckRangeList( const ScRangeList& rScRanges, bool bWarn );
155 
156     /** Checks and eventually crops the cell ranges to valid dimensions.
157         @descr  The start position of the ranges will not be modified. Cell
158             ranges that fit partly into valid dimensions are cropped
159             accordingly. Cell ranges that do not fit at all, are removed from
160             the cell range list.
161         @param rScRanges  (In/out) The cell range list to check.
162         @param bWarn  true = Sets the internal flag that produces a warning box
163             after loading/saving the file, if at least one of the cell ranges
164             contains invalid cells. */
165     void                ValidateRangeList( ScRangeList& rScRanges, bool bWarn );
166 
167     /** Converts the passed Calc cell range list to an Excel cell range list.
168         @descr  The start position of the ranges will not be modified. Cell
169             ranges that fit partly into valid dimensions are cropped
170             accordingly. Cell ranges that do not fit at all, are not inserted
171             into the Excel cell range list.
172         @param rXclRanges  (Out) The converted Excel cell range list.
173         @param rScRanges  The Calc cell range list to convert.
174         @param bWarn  true = Sets the internal flag that produces a warning box
175             after loading/saving the file, if at least one of the cell ranges
176             contains invalid cells. */
177     void                ConvertRangeList( XclRangeList& rXclRanges,
178                             const ScRangeList& rScRanges, bool bWarn );
179 };
180 
181 // EditEngine->String conversion ==============================================
182 
183 class SvxURLField;
184 class XclExpHyperlink;
185 
186 /** Helper to create HLINK records during creation of formatted cell strings.
187 
188     In Excel it is not possible to have more than one hyperlink in a cell. This
189     helper detects multiple occurences of hyperlinks and fills a string which
190     is used to create a cell note containing all URLs. Only cells containing
191     one hyperlink are exported as hyperlink cells.
192  */
193 class XclExpHyperlinkHelper : protected XclExpRoot
194 {
195 public:
196     typedef ScfRef< XclExpHyperlink > XclExpHyperlinkRef;
197 
198     explicit            XclExpHyperlinkHelper( const XclExpRoot& rRoot, const ScAddress& rScPos );
199                         ~XclExpHyperlinkHelper();
200 
201     /** Processes the passed URL field (tries to create a HLINK record).
202         @return  The representation string of the URL field. */
203     String              ProcessUrlField( const SvxURLField& rUrlField );
204 
205     /** Returns true, if a single HLINK record has been created. */
206     bool                HasLinkRecord() const;
207     /** Returns the craeted single HLINk record, or an empty reference. */
208     XclExpHyperlinkRef  GetLinkRecord();
209 
210     /** Returns true, if multiple URLs have been processed. */
HasMultipleUrls() const211     inline bool         HasMultipleUrls() const { return mbMultipleUrls; }
212     /** Returns a string containing all processed URLs. */
GetUrlList()213     inline const String& GetUrlList() { return maUrlList; }
214 
215 private:
216     XclExpHyperlinkRef  mxLinkRec;          /// Created HLINK record.
217     ScAddress           maScPos;            /// Cell position to set at the HLINK record.
218     String              maUrlList;          /// List with all processed URLs.
219     bool                mbMultipleUrls;     /// true = Multiple URL fields processed.
220 };
221 
222 // ----------------------------------------------------------------------------
223 
224 class EditEngine;
225 class SdrTextObj;
226 class ScStringCell;
227 class ScEditCell;
228 class ScPatternAttr;
229 
230 /** This class provides methods to create an XclExpString.
231     @descr  The string can be created from an edit engine text object or
232     directly from a Calc edit cell. */
233 class XclExpStringHelper : ScfNoInstance
234 {
235 public:
236     /** Creates a new unformatted string from the passed string.
237         @descr  Creates a Unicode string or a byte string, depending on the
238                 current BIFF version contained in the passed XclExpRoot object.
239         @param rString  The source string.
240         @param nFlags  Modifiers for string export.
241         @param nMaxLen  The maximum number of characters to store in this string.
242         @return  The new string object (shared pointer). */
243     static XclExpStringRef CreateString(
244                             const XclExpRoot& rRoot,
245                             const String& rString,
246                             XclStrFlags nFlags = EXC_STR_DEFAULT,
247                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
248 
249     /** Creates a new unformatted string from the passed character.
250         @descr  Creates a Unicode string or a byte string, depending on the
251                 current BIFF version contained in the passed XclExpRoot object.
252         @param cChar  The source character. The NUL character is explicitly allowed.
253         @param nFlags  Modifiers for string export.
254         @param nMaxLen  The maximum number of characters to store in this string.
255         @return  The new string object (shared pointer). */
256     static XclExpStringRef CreateString(
257                             const XclExpRoot& rRoot,
258                             sal_Unicode cChar,
259                             XclStrFlags nFlags = EXC_STR_DEFAULT,
260                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
261 
262     /** Appends an unformatted string to an Excel string object.
263         @descr  Selects the correct Append() function depending on the current
264                 BIFF version contained in the passed XclExpRoot object.
265         @param rXclString  The Excel string object.
266         @param rString  The source string. */
267     static void         AppendString(
268                             XclExpString& rXclString,
269                             const XclExpRoot& rRoot,
270                             const String& rString );
271 
272     /** Appends a character to an Excel string object.
273         @descr  Selects the correct Append() function depending on the current
274                 BIFF version contained in the passed XclExpRoot object.
275         @param rXclString  The Excel string object.
276         @param rString  The source string. */
277     static void         AppendChar(
278                             XclExpString& rXclString,
279                             const XclExpRoot& rRoot,
280                             sal_Unicode cChar );
281 
282     /** Creates a new formatted string from a Calc string cell.
283         @descr  Creates a Unicode string or a byte string, depending on the
284                 current BIFF version contained in the passed XclExpRoot object.
285                 May create a formatted string object, if the cell text contains
286                 different script types.
287         @param rStringCell  The Calc string cell object.
288         @param pCellAttr  The set item containing the cell formatting.
289         @param nFlags  Modifiers for string export.
290         @param nMaxLen  The maximum number of characters to store in this string.
291         @return  The new string object (shared pointer). */
292     static XclExpStringRef CreateCellString(
293                             const XclExpRoot& rRoot,
294                             const ScStringCell& rStringCell,
295                             const ScPatternAttr* pCellAttr,
296                             XclStrFlags nFlags = EXC_STR_DEFAULT,
297                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
298 
299     /** Creates a new formatted string from a Calc edit cell.
300         @descr  Creates a Unicode string or a byte string, depending on the
301                 current BIFF version contained in the passed XclExpRoot object.
302         @param rEditCell  The Calc edit cell object.
303         @param pCellAttr  The set item containing the cell formatting.
304         @param rLinkHelper  Helper object for hyperlink conversion.
305         @param nFlags  Modifiers for string export.
306         @param nMaxLen  The maximum number of characters to store in this string.
307         @return  The new string object (shared pointer). */
308     static XclExpStringRef CreateCellString(
309                             const XclExpRoot& rRoot,
310                             const ScEditCell& rEditCell,
311                             const ScPatternAttr* pCellAttr,
312                             XclExpHyperlinkHelper& rLinkHelper,
313                             XclStrFlags nFlags = EXC_STR_DEFAULT,
314                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
315 
316     /** Creates a new formatted string from a drawing text box.
317         @descr  Creates a Unicode string or a byte string, depending on the
318                 current BIFF version contained in the passed XclExpRoot object.
319         @param rTextObj  The text box object.
320         @param nFlags  Modifiers for string export.
321         @param nMaxLen  The maximum number of characters to store in this string.
322         @return  The new string object (shared pointer). */
323     static XclExpStringRef CreateString(
324                             const XclExpRoot& rRoot,
325                             const SdrTextObj& rTextObj,
326                             XclStrFlags nFlags = EXC_STR_DEFAULT,
327                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
328 
329     /** Creates a new formatted string from a edit text string.
330         @param rEditObj  The edittext object.
331         @param nFlags  Modifiers for string export.
332         @param nMaxLen The maximum number of characters to store in this string.
333         @return  The new string object. */
334     static XclExpStringRef CreateString(
335                             const XclExpRoot& rRoot,
336                             const EditTextObject& rEditObj,
337                             XclStrFlags nFlags = EXC_STR_DEFAULT,
338                             sal_uInt16 nMaxLen = EXC_STR_MAXLEN );
339 
340     /** Returns the script type first text portion different to WEAK, or the system
341         default script type, if there is only weak script in the passed string. */
342     static sal_Int16    GetLeadingScriptType( const XclExpRoot& rRoot, const String& rString );
343 };
344 
345 // Header/footer conversion ===================================================
346 
347 class EditEngine;
348 
349 /** Converts edit engine text objects to an Excel header/footer string.
350     @descr  Header/footer content is divided into three parts: Left, center and
351     right portion. All formatting information will be encoded in the Excel string
352     using special character seuences. A control sequence starts with the ampersand
353     character.
354 
355     Supported control sequences:
356     &L                      start of left portion
357     &C                      start of center portion
358     &R                      start of right portion
359     &P                      current page number
360     &N                      page count
361     &D                      current date
362     &T                      current time
363     &A                      table name
364     &F                      file name without path
365     &Z                      file path without file name
366     &Z&F                    file path and name
367     &U                      underlining on/off
368     &E                      double underlining on/off
369     &S                      strikeout characters on/off
370     &X                      superscript on/off
371     &Y                      subscript on/off
372     &"fontname,fontstyle"   use font with name 'fontname' and style 'fontstyle'
373     &fontheight             set font height in points ('fontheight' is a decimal value)
374 
375     Known but unsupported control sequences:
376     &G                      picture
377  */
378 class XclExpHFConverter : protected XclExpRoot, ScfNoCopy
379 {
380 public:
381     explicit            XclExpHFConverter( const XclExpRoot& rRoot );
382 
383     /** Generates the header/footer string from the passed edit engine text objects. */
384     void                GenerateString(
385                             const EditTextObject* pLeftObj,
386                             const EditTextObject* pCenterObj,
387                             const EditTextObject* pRightObj );
388 
389     /** Returns the last generated header/footer string. */
GetHFString() const390     inline const String& GetHFString() const { return maHFString; }
391     /** Returns the total height of the last generated header/footer in twips. */
GetTotalHeight() const392     inline sal_Int32    GetTotalHeight() const { return mnTotalHeight; }
393 
394 private:
395     /** Converts the text object contents and stores it in the passed string. */
396     void                AppendPortion(
397                             const EditTextObject* pTextObj,
398                             sal_Unicode cPortionCode );
399 
400 private:
401     EditEngine&         mrEE;           /// The header/footer edit engine.
402     String              maHFString;     /// The last generated header/footer string.
403     sal_Int32           mnTotalHeight;  /// Total height of the last header/footer (twips).
404 };
405 
406 // URL conversion =============================================================
407 
408 /** This class contains static methods to encode a file URL.
409     @descr  Excel stores URLs in a format that contains special control characters,
410     i.e. for directory separators or volume names. */
411 class XclExpUrlHelper : ScfNoInstance
412 {
413 public:
414     /** Encodes and returns the URL passed in rAbsUrl to an Excel like URL.
415         @param pTableName  Optional pointer to a table name to be encoded in this URL. */
416     static String       EncodeUrl( const XclExpRoot& rRoot, const String& rAbsUrl, const String* pTableName = 0 );
417     /** Encodes and returns the passed DDE link to an Excel like DDE link. */
418     static String       EncodeDde( const String& rApplic, const String rTopic );
419 };
420 
421 // ----------------------------------------------------------------------------
422 class ScDocument;
423 class ScMatrix;
424 
425 /** Contains cached values in a 2-dimensional array. */
426 class XclExpCachedMatrix
427 {
428     void            GetDimensions( SCSIZE & nCols, SCSIZE & nRows ) const;
429 public:
430     /** Constructs and fills a new matrix.
431         @param rMatrix  The Calc value matrix. */
432     explicit        XclExpCachedMatrix( const ScMatrix& rMatrix );
433                    ~XclExpCachedMatrix();
434 
435     /** Returns the byte count of all contained data. */
436     sal_Size        GetSize() const;
437     /** Writes the complete matrix to stream. */
438     void            Save( XclExpStream& rStrm ) const;
439 
440 private:
441     const ScMatrix& mrMatrix;
442 };
443 
444 // ============================================================================
445 
446 #endif
447 
448