Version 7.5.1.1, tag libreoffice-7.5.1.1
[LibreOffice.git] / sc / source / filter / inc / xltools.hxx
blobf67d896af94973fcbff1588c3e553e74dbd97fd7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include <types.hxx>
23 #include "ftools.hxx"
25 #include <tools/long.hxx>
26 #include <tools/degree.hxx>
28 class SfxObjectShell;
29 enum class FormulaError : sal_uInt16;
31 // BIFF versions ==============================================================
33 #define DBG_ERROR_BIFF() OSL_FAIL( "Unknown BIFF type!" )
34 #define OSL_ENSURE_BIFF( c ) OSL_ENSURE( c, "Unknown BIFF type!" )
36 // Enumerations ===============================================================
38 /** An enumeration for all Excel error codes and the values true and false. */
39 enum XclBoolError
41 xlErrNull, /// The error code #NULL!
42 xlErrDiv0, /// The error code #DIV/0!
43 xlErrValue, /// The error code #VALUE!
44 xlErrRef, /// The error code #REF!
45 xlErrName, /// The error code #NAME?
46 xlErrNum, /// The error code #NUM!
47 xlErrNA, /// The error code #N/A!
48 xlErrTrue, /// The Boolean value true.
49 xlErrFalse, /// The Boolean value false.
50 xlErrUnknown /// For unknown codes and values.
53 // GUID import/export =========================================================
55 class XclImpStream;
56 class XclExpStream;
58 /** This struct stores a GUID (class ID) and supports reading, writing and comparison. */
59 struct XclGuid
61 sal_uInt8 mpnData[ 16 ]; /// Stores GUID always in little endian.
63 explicit XclGuid();
64 explicit XclGuid(
65 sal_uInt32 nData1,
66 sal_uInt16 nData2, sal_uInt16 nData3,
67 sal_uInt8 nData41, sal_uInt8 nData42,
68 sal_uInt8 nData43, sal_uInt8 nData44,
69 sal_uInt8 nData45, sal_uInt8 nData46,
70 sal_uInt8 nData47, sal_uInt8 nData48 );
73 bool operator==( const XclGuid& rCmp1, const XclGuid& rCmp2 );
75 XclImpStream& operator>>( XclImpStream& rStrm, XclGuid& rGuid );
76 XclExpStream& operator<<( XclExpStream& rStrm, const XclGuid& rGuid );
78 // Excel Tools ================================================================
80 /** This class contains static helper methods for the Excel import and export filters. */
81 class XclTools
83 public:
84 // noncopyable nonconstructable -------------------------------------------
86 XclTools(const XclTools&) = delete;
87 const XclTools& operator=(const XclTools&) = delete;
88 /** We don't want anybody to instantiate this class, since it is just a
89 collection of static items. */
90 XclTools() = delete;
93 // GUID's -----------------------------------------------------------------
95 static const XclGuid maGuidStdLink; /// GUID of StdLink (HLINK record).
96 static const XclGuid maGuidUrlMoniker; /// GUID of URL moniker (HLINK record).
97 static const XclGuid maGuidFileMoniker; /// GUID of file moniker (HLINK record).
99 // numeric conversion -----------------------------------------------------
101 /** Calculates the double value from an RK value (encoded integer or double). */
102 static double GetDoubleFromRK( sal_Int32 nRKValue );
103 /** Calculates an RK value (encoded integer or double) from a double value.
104 @param rnRKValue Returns the calculated RK value.
105 @param fValue The double value.
106 @return true = An RK value could be created. */
107 static bool GetRKFromDouble( sal_Int32& rnRKValue, double fValue );
109 /** Calculates an angle (in 1/100 of degrees) from an Excel angle value.
110 @param nRotForStacked This value will be returned, if nXclRot contains 'stacked'. */
111 static Degree100 GetScRotation( sal_uInt16 nXclRot, Degree100 nRotForStacked );
112 /** Calculates the Excel angle value from an angle in 1/100 of degrees. */
113 static sal_uInt8 GetXclRotation( Degree100 nScRot );
115 /** Calculates BIFF8 rotation angle from BIFF2-BIFF5 text orientation. */
116 static sal_uInt8 GetXclRotFromOrient( sal_uInt8 nXclOrient );
117 /** Calculates BIFF2-BIFF5 text orientation from BIFF8 rotation angle. */
118 static sal_uInt8 GetXclOrientFromRot( sal_uInt16 nXclRot );
120 /** Converts a Calc error code to an Excel error code. */
121 static sal_uInt8 GetXclErrorCode( FormulaError nScError );
122 /** Converts an Excel error code to a Calc error code. */
123 static FormulaError GetScErrorCode( sal_uInt8 nXclError );
125 /** Converts the passed BIFF error to a double containing the respective Calc error code. */
126 static double ErrorToDouble( sal_uInt8 nXclError );
127 /** Gets a translated error code or Boolean value from Excel error codes.
128 @param rfDblValue Returns 0.0 for error codes or the value of a Boolean (0.0 or 1.0).
129 @param bErrorOrBool false = nError is a Boolean value; true = is an error value.
130 @param nValue The error code or Boolean value. */
131 static XclBoolError ErrorToEnum( double& rfDblValue, bool bErrOrBool, sal_uInt8 nValue );
133 /** Returns the length in twips calculated from a length in inches. */
134 static sal_uInt16 GetTwipsFromInch( double fInches );
135 /** Returns the length in twips calculated from a length in 1/100 mm. */
136 static sal_uInt16 GetTwipsFromHmm( sal_Int32 nHmm );
138 /** Returns the length in inches calculated from a length in twips. */
139 static double GetInchFromTwips( sal_Int32 nTwips );
140 /** Returns the length in inches calculated from a length in 1/100 mm. */
141 static double GetInchFromHmm( sal_Int32 nHmm );
143 /** Returns the length in 1/100 mm calculated from a length in inches. */
144 static sal_Int32 GetHmmFromInch( double fInches );
145 /** Returns the length in 1/100 mm calculated from a length in twips. */
146 static sal_Int32 GetHmmFromTwips( sal_Int32 nTwips );
148 /** Returns the Calc column width (twips) for the passed Excel width.
149 * Excel Column width is stored as 1/256th of a character.
150 @param nScCharWidth Width of the '0' character in Calc (twips). */
151 static sal_uInt16 GetScColumnWidth( sal_uInt16 nXclWidth, tools::Long nScCharWidth );
152 /** Returns the Excel column width for the passed Calc width (twips).
153 @param nScCharWidth Width of the '0' character in Calc (twips). */
154 static sal_uInt16 GetXclColumnWidth( sal_uInt16 nScWidth, tools::Long nScCharWidth );
156 /** Returns a correction value to convert column widths from/to default column widths.
157 @param nXclDefFontHeight Excel height of application default font. */
158 static double GetXclDefColWidthCorrection( tools::Long nXclDefFontHeight );
160 // formatting -------------------------------------------------------------
162 /** Returns the best fitting color for an Excel pattern area format. */
163 static Color GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt16 nXclPattern );
165 // text encoding ----------------------------------------------------------
167 /** Returns a text encoding from an Excel code page.
168 @return The corresponding text encoding or RTL_TEXTENCODING_DONTKNOW. */
169 static rtl_TextEncoding GetTextEncoding( sal_uInt16 nCodePage );
171 /** Returns an Excel code page from a text encoding. */
172 static sal_uInt16 GetXclCodePage( rtl_TextEncoding eTextEnc );
174 // font names -------------------------------------------------------------
176 /** Returns the matching Excel font name for a passed Calc font name. */
177 static OUString GetXclFontName( const OUString& rFontName );
179 // built-in defined names -------------------------------------------------
181 /** Returns the raw English UI representation of a built-in defined name used in NAME records.
182 @param cBuiltIn Excel index of the built-in name. */
183 static OUString GetXclBuiltInDefName( sal_Unicode cBuiltIn );
184 /** Returns the Calc UI representation of a built-in defined name used in NAME records.
185 @descr Adds a prefix to the representation returned by GetXclBuiltInDefName().
186 @param cBuiltIn Excel index of the built-in name. */
187 static OUString GetBuiltInDefName( sal_Unicode cBuiltIn );
188 /** Returns the Excel built-in name with OOXML prefix
189 @descr Adds the "_xlnm." prefix to the representation returned by GetXclBuiltInDefName()
190 @param cBuiltIn Excel index of the built in name.*/
191 static OUString GetBuiltInDefNameXml( sal_Unicode cBuiltIn );
192 /** Returns the Excel built-in name index of the passed defined name from Calc.
193 @descr Ignores any characters following a valid representation of a built-in name.
194 @param rDefName raw English UI representation of a built-in defined name used in NAME records.
195 @return the index of the built-in name, or EXC_BUILTIN_UNKNOWN if it is not a built-in name. */
196 static sal_Unicode GetBuiltInDefNameIndex( const OUString& rDefName );
198 // built-in style names ---------------------------------------------------
200 /** Returns the specified built-in cell style name.
201 @param nStyleId The identifier of the built-in style.
202 @param rName Default name for unknown styles.
203 @param nLevel The zero-based outline level for RowLevel and ColLevel styles.
204 @return The style name or an empty string, if the parameters are not valid. */
205 static OUString GetBuiltInStyleName( sal_uInt8 nStyleId, std::u16string_view rName, sal_uInt8 nLevel );
207 /** Returns true, if the passed string is a name of an Excel built-in style.
208 @param pnStyleId If not 0, the found style identifier will be returned here.
209 @param pnNextChar If not 0, the index of the char after the evaluated substring will be returned here. */
210 static bool IsBuiltInStyleName( const OUString& rStyleName, sal_uInt8* pnStyleId = nullptr, sal_Int32* pnNextChar = nullptr );
211 /** Returns the Excel built-in style identifier of a passed style name.
212 @param rnStyleId The style identifier is returned here.
213 @param rnLevel The zero-based outline level for RowLevel and ColLevel styles is returned here.
214 @param rStyleName The style name to examine.
215 @return true = passed string is a built-in style name, false = user style. */
216 static bool GetBuiltInStyleId(
217 sal_uInt8& rnStyleId, sal_uInt8& rnLevel,
218 const OUString& rStyleName );
220 // conditional formatting style names -------------------------------------
222 /** Returns the style name for a single condition of a conditional formatting.
223 @param nScTab The current Calc sheet index.
224 @param nFormat The zero-based index of the conditional formatting.
225 @param nCondition The zero-based index of the condition.
226 @return A style sheet name in the form "Excel_CondFormat_<sheet>_<format>_<condition>". */
227 static OUString GetCondFormatStyleName( SCTAB nScTab, sal_Int32 nFormat, sal_uInt16 nCondition );
228 /** Returns true, if the passed string is a name of a conditional format style created by Excel import.
229 @param pnNextChar If not 0, the index of the char after the evaluated substring will be returned here. */
230 static bool IsCondFormatStyleName( const OUString& rStyleName );
232 // stream handling --------------------------------------------------------
234 /** Skips a substream (BOF/EOF record block). Includes all embedded substreams. */
235 static void SkipSubStream( XclImpStream& rStrm );
237 // Basic macro names ------------------------------------------------------
239 /** Returns the full StarBasic macro URL from an Excel macro name. */
240 static OUString GetSbMacroUrl( const OUString& rMacroName, SfxObjectShell* pDocShell );
241 /** Returns the Excel macro name from a full StarBasic macro URL. */
242 static OUString GetXclMacroName( const OUString& rSbMacroUrl );
246 // read/write colors ----------------------------------------------------------
248 /** Reads a color from the passed stream.
249 @descr The color has the format (all values 8-bit): Red, Green, Blue, 0. */
250 XclImpStream& operator>>( XclImpStream& rStrm, Color& rColor );
252 /** Reads a color to the passed stream.
253 @descr The color has the format (all values 8-bit): Red, Green, Blue, 0. */
254 XclExpStream& operator<<( XclExpStream& rStrm, const Color& rColor );
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */