xref: /aoo4110/main/sc/source/filter/inc/xltools.hxx (revision b1cdbd2c)
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_XLTOOLS_HXX
25 #define SC_XLTOOLS_HXX
26 
27 #include "address.hxx"
28 #include "ftools.hxx"
29 
30 class SfxObjectShell;
31 
32 // BIFF versions ==============================================================
33 
34 #define DBG_ERROR_BIFF()        DBG_ERRORFILE( "Unknown BIFF type!" )
35 #define DBG_ASSERT_BIFF( c )    DBG_ASSERT( c, "Unknown BIFF type!" )
36 
37 // Enumerations ===============================================================
38 
39 /** An enumeration for all Excel error codes and the values true and false. */
40 enum XclBoolError
41 {
42     xlErrNull,                  /// The error code #NULL!
43     xlErrDiv0,                  /// The error code #DIV/0!
44     xlErrValue,                 /// The error code #VALUE!
45     xlErrRef,                   /// The error code #REF!
46     xlErrName,                  /// The error code #NAME?
47     xlErrNum,                   /// The error code #NUM!
48     xlErrNA,                    /// The error code #N/A!
49     xlErrTrue,                  /// The Boolean value true.
50     xlErrFalse,                 /// The Boolean value false.
51     xlErrUnknown                /// For unknown codes and values.
52 };
53 
54 // GUID import/export =========================================================
55 
56 class XclImpStream;
57 class XclExpStream;
58 
59 /** This struct stores a GUID (class ID) and supports reading, writing and comparison. */
60 struct XclGuid
61 {
62     sal_uInt8           mpnData[ 16 ];  /// Stores GUID always in little endian.
63 
64     explicit            XclGuid();
65     explicit            XclGuid(
66                             sal_uInt32 nData1,
67                             sal_uInt16 nData2, sal_uInt16 nData3,
68                             sal_uInt8 nData41, sal_uInt8 nData42,
69                             sal_uInt8 nData43, sal_uInt8 nData44,
70                             sal_uInt8 nData45, sal_uInt8 nData46,
71                             sal_uInt8 nData47, sal_uInt8 nData48 );
72 };
73 
74 bool operator==( const XclGuid& rCmp1, const XclGuid& rCmp2 );
operator !=(const XclGuid & rCmp1,const XclGuid & rCmp2)75 inline bool operator!=( const XclGuid& rCmp1, const XclGuid& rCmp2 ) { return !(rCmp1 == rCmp2); }
76 bool operator<( const XclGuid& rCmp1, const XclGuid& rCmp2 );
77 
78 XclImpStream& operator>>( XclImpStream& rStrm, XclGuid& rGuid );
79 XclExpStream& operator<<( XclExpStream& rStrm, const XclGuid& rGuid );
80 
81 // Excel Tools ================================================================
82 
83 class SvStream;
84 class ScDocument;
85 
86 /** This class contains static helper methods for the Excel import and export filters. */
87 class XclTools : ScfNoInstance
88 {
89 public:
90     // GUID's -----------------------------------------------------------------
91 
92     static const XclGuid maGuidStdLink;     /// GUID of StdLink (HLINK record).
93     static const XclGuid maGuidUrlMoniker;  /// GUID of URL moniker (HLINK record).
94     static const XclGuid maGuidFileMoniker; /// GUID of file moniker (HLINK record).
95 
96     // numeric conversion -----------------------------------------------------
97 
98     /** Calculates the double value from an RK value (encoded integer or double). */
99     static double       GetDoubleFromRK( sal_Int32 nRKValue );
100     /** Calculates an RK value (encoded integer or double) from a double value.
101         @param rnRKValue  Returns the calculated RK value.
102         @param fValue  The double value.
103         @return  true = An RK value could be created. */
104     static bool         GetRKFromDouble( sal_Int32& rnRKValue, double fValue );
105 
106     /** Calculates an angle (in 1/100 of degrees) from an Excel angle value.
107         @param nRotForStacked  This value will be returned, if nXclRot contains 'stacked'. */
108     static sal_Int32    GetScRotation( sal_uInt16 nXclRot, sal_Int32 nRotForStacked );
109     /** Calculates the Excel angle value from an angle in 1/100 of degrees. */
110     static sal_uInt8    GetXclRotation( sal_Int32 nScRot );
111 
112     /** Calculates BIFF8 rotation angle from BIFF2-BIFF5 text orientation. */
113     static sal_uInt8    GetXclRotFromOrient( sal_uInt8 nXclOrient );
114     /** Calculates BIFF2-BIFF5 text orientation from BIFF8 rotation angle. */
115     static sal_uInt8    GetXclOrientFromRot( sal_uInt16 nXclRot );
116 
117     /** Converts a Calc error code to an Excel error code. */
118     static sal_uInt8    GetXclErrorCode( sal_uInt16 nScError );
119     /** Converts an Excel error code to a Calc error code. */
120     static sal_uInt16       GetScErrorCode( sal_uInt8 nXclError );
121 
122     /** Converts the passed BIFF error to a double containing the respective Calc error code. */
123     static double       ErrorToDouble( sal_uInt8 nXclError );
124     /** Gets a translated error code or Boolean value from Excel error codes.
125         @param rfDblValue  Returns 0.0 for error codes or the value of a Boolean (0.0 or 1.0).
126         @param bErrorOrBool  false = nError is a Boolean value; true = is an error value.
127         @param nValue  The error code or Boolean value. */
128     static XclBoolError ErrorToEnum( double& rfDblValue, sal_uInt8 bErrOrBool, sal_uInt8 nValue );
129 
130     /** Returns the length in twips calculated from a length in inches. */
131     static sal_uInt16   GetTwipsFromInch( double fInches );
132     /** Returns the length in twips calculated from a length in 1/100 mm. */
133     static sal_uInt16   GetTwipsFromHmm( sal_Int32 nHmm );
134 
135     /** Returns the length in inches calculated from a length in twips. */
136     static double       GetInchFromTwips( sal_Int32 nTwips );
137     /** Returns the length in inches calculated from a length in 1/100 mm. */
138     static double       GetInchFromHmm( sal_Int32 nHmm );
139 
140     /** Returns the length in 1/100 mm calculated from a length in inches. */
141     static sal_Int32    GetHmmFromInch( double fInches );
142     /** Returns the length in 1/100 mm calculated from a length in twips. */
143     static sal_Int32    GetHmmFromTwips( sal_Int32 nTwips );
144 
145     /** Returns the Calc column width (twips) for the passed Excel width.
146         @param nScCharWidth  Width of the '0' character in Calc (twips). */
147     static sal_uInt16       GetScColumnWidth( sal_uInt16 nXclWidth, long nScCharWidth );
148     /** Returns the Excel column width for the passed Calc width (twips).
149         @param nScCharWidth  Width of the '0' character in Calc (twips). */
150     static sal_uInt16   GetXclColumnWidth( sal_uInt16 nScWidth, long nScCharWidth );
151 
152     /** Returns a correction value to convert column widths from/to default column widths.
153         @param nXclDefFontHeight  Excel height of application default font. */
154     static double       GetXclDefColWidthCorrection( long nXclDefFontHeight );
155 
156     // formatting -------------------------------------------------------------
157 
158     /** Returns the best fitting color for an Excel pattern area format. */
159     static Color        GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt16 nXclPattern );
160 
161     // text encoding ----------------------------------------------------------
162 
163     /** Returns a text encoding from an Excel code page.
164         @return  The corresponding text encoding or RTL_TEXTENCODING_DONTKNOW. */
165     static rtl_TextEncoding GetTextEncoding( sal_uInt16 nCodePage );
166 
167     /** Returns an Excel code page from a text encoding. */
168     static sal_uInt16   GetXclCodePage( rtl_TextEncoding eTextEnc );
169 
170     // font names -------------------------------------------------------------
171 
172     /** Returns the matching Excel font name for a passed Calc font name. */
173     static String       GetXclFontName( const String& rFontName );
174 
175     // built-in defined names -------------------------------------------------
176 
177     /** Returns the raw English UI representation of a built-in defined name used in NAME records.
178         @param cBuiltIn  Excel index of the built-in name. */
179     static String       GetXclBuiltInDefName( sal_Unicode cBuiltIn );
180     /** Returns the Calc UI representation of a built-in defined name used in NAME records.
181         @descr  Adds a prefix to the representation returned by GetXclBuiltInDefName().
182         @param cBuiltIn  Excel index of the built-in name. */
183     static String       GetBuiltInDefName( sal_Unicode cBuiltIn );
184     /** Returns the Excel built-in name index of the passed defined name from Calc.
185         @descr  Ignores any characters following a valid representation of a built-in name.
186         @param pcBuiltIn  (out-param) If not 0, the index of the built-in name will be returned here.
187         @return  true = passed string is a built-in name; false = user-defined name. */
188     static sal_Unicode  GetBuiltInDefNameIndex( const String& rDefName );
189 
190     // built-in style names ---------------------------------------------------
191 
192     /** Returns the specified built-in cell style name.
193         @param nStyleId  The identifier of the built-in style.
194         @param rName  Default name for unknown styles.
195         @param nLevel  The zero-based outline level for RowLevel and ColLevel styles.
196         @return  The style name or an empty string, if the parameters are not valid. */
197     static String       GetBuiltInStyleName( sal_uInt8 nStyleId, const String& rName, sal_uInt8 nLevel );
198     /** Returns the passed style name with a special built-in prefix. */
199     static String       GetBuiltInStyleName( const String& rStyleName );
200     /** Returns true, if the passed string is a name of an Excel built-in style.
201         @param pnStyleId  If not 0, the found style identifier will be returned here.
202         @param pnNextChar  If not 0, the index of the char after the evaluated substring will be returned here. */
203     static bool         IsBuiltInStyleName( const String& rStyleName, sal_uInt8* pnStyleId = 0, xub_StrLen* pnNextChar = 0 );
204     /** Returns the Excel built-in style identifier of a passed style name.
205         @param rnStyleId  The style identifier is returned here.
206         @param rnLevel  The zero-based outline level for RowLevel and ColLevel styles is returned here.
207         @param rStyleName  The style name to examine.
208         @return  true = passed string is a built-in style name, false = user style. */
209     static bool         GetBuiltInStyleId(
210                             sal_uInt8& rnStyleId, sal_uInt8& rnLevel,
211                             const String& rStyleName );
212 
213     // conditional formatting style names -------------------------------------
214 
215     /** Returns the style name for a single condition of a conditional formatting.
216         @param nScTab  The current Calc sheet index.
217         @param nFormat  The zero-based index of the conditional formatting.
218         @param nCondition  The zero-based index of the condition.
219         @return  A style sheet name in the form "Excel_CondFormat_<sheet>_<format>_<condition>". */
220     static String       GetCondFormatStyleName( SCTAB nScTab, sal_Int32 nFormat, sal_uInt16 nCondition );
221     /** Returns true, if the passed string is a name of a conditional format style created by Excel import.
222         @param pnNextChar  If not 0, the index of the char after the evaluated substring will be returned here. */
223     static bool         IsCondFormatStyleName( const String& rStyleName, xub_StrLen* pnNextChar = 0 );
224 
225     // stream handling --------------------------------------------------------
226 
227     /** Skips a substream (BOF/EOF record block). Includes all embedded substreams. */
228     static void         SkipSubStream( XclImpStream& rStrm );
229 
230     // Basic macro names ------------------------------------------------------
231 
232     /** Returns the full StarBasic macro URL from an Excel macro name. */
233     static ::rtl::OUString GetSbMacroUrl( const String& rMacroName, SfxObjectShell* pDocShell = 0 );
234     /** Returns the full StarBasic macro URL from an Excel module and macro name. */
235     static ::rtl::OUString GetSbMacroUrl( const String& rModuleName, const String& rMacroName, SfxObjectShell* pDocShell = 0 );
236     /** Returns the Excel macro name from a full StarBasic macro URL. */
237     static String       GetXclMacroName( const ::rtl::OUString& rSbMacroUrl );
238 
239 // ------------------------------------------------------------------------
240 private:
241     static const String maDefNamePrefix;            /// Prefix for built-in defined names.
242     static const String maStyleNamePrefix1;         /// Prefix for built-in cell style names.
243     static const String maStyleNamePrefix2;         /// Prefix for built-in cell style names from OOX filter.
244     static const String maCFStyleNamePrefix1;       /// Prefix for cond. formatting style names.
245     static const String maCFStyleNamePrefix2;       /// Prefix for cond. formatting style names from OOX filter.
246     static const ::rtl::OUString maSbMacroPrefix;   /// Prefix for StarBasic macros.
247     static const ::rtl::OUString maSbMacroSuffix;   /// Suffix for StarBasic macros.
248 };
249 
250 // read/write colors ----------------------------------------------------------
251 
252 /** Reads a color from the passed stream.
253     @descr  The color has the format (all values 8-bit): Red, Green, Blue, 0. */
254 XclImpStream& operator>>( XclImpStream& rStrm, Color& rColor );
255 
256 /** Reads a color to the passed stream.
257     @descr  The color has the format (all values 8-bit): Red, Green, Blue, 0. */
258 XclExpStream& operator<<( XclExpStream& rStrm, const Color& rColor );
259 
260 // ============================================================================
261 
262 #endif
263 
264