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