fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / sc / source / filter / excel / xltools.cxx
blob4dde77c93c87880ab07fdb9c1bf4ea4bc555e3c8
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 #include <algorithm>
21 #include <math.h>
22 #include <sal/mathconf.h>
23 #include <unotools/fontcvt.hxx>
24 #include <sfx2/objsh.hxx>
25 #include <sal/macros.h>
26 #include <editeng/editstat.hxx>
27 #include <filter/msfilter/msvbahelper.hxx>
28 #include "xestream.hxx"
29 #include "document.hxx"
30 #include "docuno.hxx"
31 #include "editutil.hxx"
32 #include <formula/errorcodes.hxx>
33 #include "globstr.hrc"
34 #include "xlstyle.hxx"
35 #include "xlname.hxx"
36 #include "xistream.hxx"
37 #include "xiroot.hxx"
38 #include "xltools.hxx"
40 // GUID import/export
42 XclGuid::XclGuid()
44 ::std::fill( mpnData, STATIC_ARRAY_END( mpnData ), 0 );
47 XclGuid::XclGuid(
48 sal_uInt32 nData1, sal_uInt16 nData2, sal_uInt16 nData3,
49 sal_uInt8 nData41, sal_uInt8 nData42, sal_uInt8 nData43, sal_uInt8 nData44,
50 sal_uInt8 nData45, sal_uInt8 nData46, sal_uInt8 nData47, sal_uInt8 nData48 )
52 // convert to little endian -> makes streaming easy
53 UInt32ToSVBT32( nData1, mpnData );
54 ShortToSVBT16( nData2, mpnData + 4 );
55 ShortToSVBT16( nData3, mpnData + 6 );
56 mpnData[ 8 ] = nData41;
57 mpnData[ 9 ] = nData42;
58 mpnData[ 10 ] = nData43;
59 mpnData[ 11 ] = nData44;
60 mpnData[ 12 ] = nData45;
61 mpnData[ 13 ] = nData46;
62 mpnData[ 14 ] = nData47;
63 mpnData[ 15 ] = nData48;
66 bool operator==( const XclGuid& rCmp1, const XclGuid& rCmp2 )
68 return ::std::equal( rCmp1.mpnData, STATIC_ARRAY_END( rCmp1.mpnData ), rCmp2.mpnData );
71 bool operator<( const XclGuid& rCmp1, const XclGuid& rCmp2 )
73 return ::std::lexicographical_compare(
74 rCmp1.mpnData, STATIC_ARRAY_END( rCmp1.mpnData ),
75 rCmp2.mpnData, STATIC_ARRAY_END( rCmp2.mpnData ) );
78 XclImpStream& operator>>( XclImpStream& rStrm, XclGuid& rGuid )
80 rStrm.Read( rGuid.mpnData, 16 ); // mpnData always in little endian
81 return rStrm;
84 XclExpStream& operator<<( XclExpStream& rStrm, const XclGuid& rGuid )
86 rStrm.Write( rGuid.mpnData, 16 ); // mpnData already in little endian
87 return rStrm;
90 // Excel Tools
92 // GUID's
93 const XclGuid XclTools::maGuidStdLink(
94 0x79EAC9D0, 0xBAF9, 0x11CE, 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B );
96 const XclGuid XclTools::maGuidUrlMoniker(
97 0x79EAC9E0, 0xBAF9, 0x11CE, 0x8C, 0x82, 0x00, 0xAA, 0x00, 0x4B, 0xA9, 0x0B );
99 const XclGuid XclTools::maGuidFileMoniker(
100 0x00000303, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46 );
102 // numeric conversion
104 double XclTools::GetDoubleFromRK( sal_Int32 nRKValue )
106 union
108 double fVal;
109 sal_math_Double smD;
111 fVal = 0.0;
113 if( ::get_flag( nRKValue, EXC_RK_INTFLAG ) )
115 sal_Int32 nTemp = nRKValue >> 2;
116 ::set_flag< sal_Int32 >( nTemp, 0xE0000000, nRKValue < 0 );
117 fVal = nTemp;
119 else
121 smD.w32_parts.msw = nRKValue & EXC_RK_VALUEMASK;
124 if( ::get_flag( nRKValue, EXC_RK_100FLAG ) )
125 fVal /= 100.0;
127 return fVal;
130 bool XclTools::GetRKFromDouble( sal_Int32& rnRKValue, double fValue )
132 double fFrac, fInt;
134 // integer
135 fFrac = modf( fValue, &fInt );
136 if( (fFrac == 0.0) && (fInt >= -536870912.0) && (fInt <= 536870911.0) ) // 2^29
138 rnRKValue
139 = static_cast<sal_Int32>(
140 static_cast<sal_uInt32>(static_cast<sal_Int32>(fInt)) << 2)
141 | EXC_RK_INT;
142 return true;
145 // integer/100
146 fFrac = modf( fValue * 100.0, &fInt );
147 if( (fFrac == 0.0) && (fInt >= -536870912.0) && (fInt <= 536870911.0) )
149 rnRKValue
150 = static_cast<sal_Int32>(
151 static_cast<sal_uInt32>(static_cast<sal_Int32>(fInt)) << 2)
152 | EXC_RK_INT100;
153 return true;
156 // double
157 return false;
160 sal_Int32 XclTools::GetScRotation( sal_uInt16 nXclRot, sal_Int32 nRotForStacked )
162 if( nXclRot == EXC_ROT_STACKED )
163 return nRotForStacked;
164 OSL_ENSURE( nXclRot <= 180, "XclTools::GetScRotation - illegal rotation angle" );
165 return static_cast< sal_Int32 >( (nXclRot <= 180) ? (100 * ((nXclRot > 90) ? (450 - nXclRot) : nXclRot)) : 0 );
168 sal_uInt8 XclTools::GetXclRotation( sal_Int32 nScRot )
170 sal_Int32 nXclRot = nScRot / 100;
171 if( (0 <= nXclRot) && (nXclRot <= 90) )
172 return static_cast< sal_uInt8 >( nXclRot );
173 if( nXclRot < 180 )
174 return static_cast< sal_uInt8 >( 270 - nXclRot );
175 if( nXclRot < 270 )
176 return static_cast< sal_uInt8 >( nXclRot - 180 );
177 if( nXclRot < 360 )
178 return static_cast< sal_uInt8 >( 450 - nXclRot );
179 return 0;
182 sal_uInt8 XclTools::GetXclRotFromOrient( sal_uInt8 nXclOrient )
184 switch( nXclOrient )
186 case EXC_ORIENT_NONE: return EXC_ROT_NONE;
187 case EXC_ORIENT_STACKED: return EXC_ROT_STACKED;
188 case EXC_ORIENT_90CCW: return EXC_ROT_90CCW;
189 case EXC_ORIENT_90CW: return EXC_ROT_90CW;
190 default: OSL_FAIL( "XclTools::GetXclRotFromOrient - unknown text orientation" );
192 return EXC_ROT_NONE;
195 sal_uInt8 XclTools::GetXclOrientFromRot( sal_uInt16 nXclRot )
197 if( nXclRot == EXC_ROT_STACKED )
198 return EXC_ORIENT_STACKED;
199 OSL_ENSURE( nXclRot <= 180, "XclTools::GetXclOrientFromRot - unknown text rotation" );
200 if( (45 < nXclRot) && (nXclRot <= 90) )
201 return EXC_ORIENT_90CCW;
202 if( (135 < nXclRot) && (nXclRot <= 180) )
203 return EXC_ORIENT_90CW;
204 return EXC_ORIENT_NONE;
207 sal_uInt8 XclTools::GetXclErrorCode( sal_uInt16 nScError )
209 using namespace ScErrorCodes;
210 switch( nScError )
212 case errIllegalArgument: return EXC_ERR_VALUE;
213 case errIllegalFPOperation: return EXC_ERR_NUM; // maybe DIV/0 or NUM...
214 case errDivisionByZero: return EXC_ERR_DIV0;
215 case errIllegalParameter: return EXC_ERR_VALUE;
216 case errPairExpected: return EXC_ERR_VALUE;
217 case errOperatorExpected: return EXC_ERR_VALUE;
218 case errVariableExpected: return EXC_ERR_VALUE;
219 case errParameterExpected: return EXC_ERR_VALUE;
220 case errNoValue: return EXC_ERR_VALUE;
221 case errCircularReference: return EXC_ERR_VALUE;
222 case errNoCode: return EXC_ERR_NULL;
223 case errNoRef: return EXC_ERR_REF;
224 case errNoName: return EXC_ERR_NAME;
225 case errNoAddin: return EXC_ERR_NAME;
226 case errNoMacro: return EXC_ERR_NAME;
227 case NOTAVAILABLE: return EXC_ERR_NA;
229 return EXC_ERR_NA;
232 sal_uInt16 XclTools::GetScErrorCode( sal_uInt8 nXclError )
234 using namespace ScErrorCodes;
235 switch( nXclError )
237 case EXC_ERR_NULL: return errNoCode;
238 case EXC_ERR_DIV0: return errDivisionByZero;
239 case EXC_ERR_VALUE: return errNoValue;
240 case EXC_ERR_REF: return errNoRef;
241 case EXC_ERR_NAME: return errNoName;
242 case EXC_ERR_NUM: return errIllegalFPOperation;
243 case EXC_ERR_NA: return NOTAVAILABLE;
244 default: OSL_FAIL( "XclTools::GetScErrorCode - unknown error code" );
246 return NOTAVAILABLE;
249 double XclTools::ErrorToDouble( sal_uInt8 nXclError )
251 union
253 double fVal;
254 sal_math_Double smD;
256 ::rtl::math::setNan( &fVal );
257 smD.nan_parts.fraction_lo = GetScErrorCode( nXclError );
258 return fVal;
261 XclBoolError XclTools::ErrorToEnum( double& rfDblValue, bool bErrOrBool, sal_uInt8 nValue )
263 XclBoolError eType;
264 if( bErrOrBool )
266 // error value
267 switch( nValue )
269 case EXC_ERR_NULL: eType = xlErrNull; break;
270 case EXC_ERR_DIV0: eType = xlErrDiv0; break;
271 case EXC_ERR_VALUE: eType = xlErrValue; break;
272 case EXC_ERR_REF: eType = xlErrRef; break;
273 case EXC_ERR_NAME: eType = xlErrName; break;
274 case EXC_ERR_NUM: eType = xlErrNum; break;
275 case EXC_ERR_NA: eType = xlErrNA; break;
276 default: eType = xlErrUnknown;
278 rfDblValue = 0.0;
280 else
282 // Boolean value
283 eType = nValue ? xlErrTrue : xlErrFalse;
284 rfDblValue = nValue ? 1.0 : 0.0;
286 return eType;
289 sal_uInt16 XclTools::GetTwipsFromInch( double fInches )
291 return static_cast< sal_uInt16 >(
292 ::std::min( ::std::max( (fInches * EXC_TWIPS_PER_INCH + 0.5), 0.0 ), 65535.0 ) );
295 sal_uInt16 XclTools::GetTwipsFromHmm( sal_Int32 nHmm )
297 return GetTwipsFromInch( static_cast< double >( nHmm ) / 1000.0 / CM_PER_INCH );
300 double XclTools::GetInchFromTwips( sal_Int32 nTwips )
302 return static_cast< double >( nTwips ) / EXC_TWIPS_PER_INCH;
305 double XclTools::GetInchFromHmm( sal_Int32 nHmm )
307 return GetInchFromTwips( GetTwipsFromHmm( nHmm ) );
310 sal_Int32 XclTools::GetHmmFromInch( double fInches )
312 return static_cast< sal_Int32 >( fInches * CM_PER_INCH * 1000 );
315 sal_Int32 XclTools::GetHmmFromTwips( sal_Int32 nTwips )
317 return GetHmmFromInch( GetInchFromTwips( nTwips ) );
320 sal_uInt16 XclTools::GetScColumnWidth( sal_uInt16 nXclWidth, long nScCharWidth )
322 double fScWidth = static_cast< double >( nXclWidth ) / 256.0 * nScCharWidth + 0.5;
323 return limit_cast< sal_uInt16 >( fScWidth );
326 sal_uInt16 XclTools::GetXclColumnWidth( sal_uInt16 nScWidth, long nScCharWidth )
328 double fXclWidth = static_cast< double >( nScWidth ) * 256.0 / nScCharWidth + 0.5;
329 return limit_cast< sal_uInt16 >( fXclWidth );
332 double XclTools::GetXclDefColWidthCorrection( long nXclDefFontHeight )
334 return 40960.0 / ::std::max( nXclDefFontHeight - 15L, 60L ) + 50.0;
337 // formatting
339 Color XclTools::GetPatternColor( const Color& rPattColor, const Color& rBackColor, sal_uInt16 nXclPattern )
341 // 0x00 == 0% transparence (full rPattColor)
342 // 0x80 == 100% transparence (full rBackColor)
343 static const sal_uInt8 pnRatioTable[] =
345 0x80, 0x00, 0x40, 0x20, 0x60, 0x40, 0x40, 0x40, // 00 - 07
346 0x40, 0x40, 0x20, 0x60, 0x60, 0x60, 0x60, 0x48, // 08 - 15
347 0x50, 0x70, 0x78 // 16 - 18
349 return (nXclPattern < SAL_N_ELEMENTS( pnRatioTable )) ?
350 ScfTools::GetMixedColor( rPattColor, rBackColor, pnRatioTable[ nXclPattern ] ) : rPattColor;
353 // text encoding
355 namespace {
357 const struct XclCodePageEntry
359 sal_uInt16 mnCodePage;
360 rtl_TextEncoding meTextEnc;
362 pCodePageTable[] =
364 { 437, RTL_TEXTENCODING_IBM_437 }, // OEM US
365 // { 720, RTL_TEXTENCODING_IBM_720 }, // OEM Arabic
366 { 737, RTL_TEXTENCODING_IBM_737 }, // OEM Greek
367 { 775, RTL_TEXTENCODING_IBM_775 }, // OEM Baltic
368 { 850, RTL_TEXTENCODING_IBM_850 }, // OEM Latin I
369 { 852, RTL_TEXTENCODING_IBM_852 }, // OEM Latin II (Central European)
370 { 855, RTL_TEXTENCODING_IBM_855 }, // OEM Cyrillic
371 { 857, RTL_TEXTENCODING_IBM_857 }, // OEM Turkish
372 // { 858, RTL_TEXTENCODING_IBM_858 }, // OEM Multilingual Latin I with Euro
373 { 860, RTL_TEXTENCODING_IBM_860 }, // OEM Portuguese
374 { 861, RTL_TEXTENCODING_IBM_861 }, // OEM Icelandic
375 { 862, RTL_TEXTENCODING_IBM_862 }, // OEM Hebrew
376 { 863, RTL_TEXTENCODING_IBM_863 }, // OEM Canadian (French)
377 { 864, RTL_TEXTENCODING_IBM_864 }, // OEM Arabic
378 { 865, RTL_TEXTENCODING_IBM_865 }, // OEM Nordic
379 { 866, RTL_TEXTENCODING_IBM_866 }, // OEM Cyrillic (Russian)
380 { 869, RTL_TEXTENCODING_IBM_869 }, // OEM Greek (Modern)
381 { 874, RTL_TEXTENCODING_MS_874 }, // MS Windows Thai
382 { 932, RTL_TEXTENCODING_MS_932 }, // MS Windows Japanese Shift-JIS
383 { 936, RTL_TEXTENCODING_MS_936 }, // MS Windows Chinese Simplified GBK
384 { 949, RTL_TEXTENCODING_MS_949 }, // MS Windows Korean (Wansung)
385 { 950, RTL_TEXTENCODING_MS_950 }, // MS Windows Chinese Traditional BIG5
386 { 1200, RTL_TEXTENCODING_DONTKNOW }, // Unicode (BIFF8) - return *_DONTKNOW to preserve old code page
387 { 1250, RTL_TEXTENCODING_MS_1250 }, // MS Windows Latin II (Central European)
388 { 1251, RTL_TEXTENCODING_MS_1251 }, // MS Windows Cyrillic
389 { 1252, RTL_TEXTENCODING_MS_1252 }, // MS Windows Latin I (BIFF4-BIFF8)
390 { 1253, RTL_TEXTENCODING_MS_1253 }, // MS Windows Greek
391 { 1254, RTL_TEXTENCODING_MS_1254 }, // MS Windows Turkish
392 { 1255, RTL_TEXTENCODING_MS_1255 }, // MS Windows Hebrew
393 { 1256, RTL_TEXTENCODING_MS_1256 }, // MS Windows Arabic
394 { 1257, RTL_TEXTENCODING_MS_1257 }, // MS Windows Baltic
395 { 1258, RTL_TEXTENCODING_MS_1258 }, // MS Windows Vietnamese
396 { 1361, RTL_TEXTENCODING_MS_1361 }, // MS Windows Korean (Johab)
397 { 10000, RTL_TEXTENCODING_APPLE_ROMAN }, // Apple Roman
398 { 32768, RTL_TEXTENCODING_APPLE_ROMAN }, // Apple Roman
399 { 32769, RTL_TEXTENCODING_MS_1252 } // MS Windows Latin I (BIFF2-BIFF3)
401 const XclCodePageEntry* const pCodePageTableEnd = STATIC_ARRAY_END( pCodePageTable );
403 struct XclCodePageEntry_CPPred
405 inline explicit XclCodePageEntry_CPPred( sal_uInt16 nCodePage ) : mnCodePage( nCodePage ) {}
406 inline bool operator()( const XclCodePageEntry& rEntry ) const { return rEntry.mnCodePage == mnCodePage; }
407 sal_uInt16 mnCodePage;
410 struct XclCodePageEntry_TEPred
412 inline explicit XclCodePageEntry_TEPred( rtl_TextEncoding eTextEnc ) : meTextEnc( eTextEnc ) {}
413 inline bool operator()( const XclCodePageEntry& rEntry ) const { return rEntry.meTextEnc == meTextEnc; }
414 rtl_TextEncoding meTextEnc;
417 } // namespace
419 rtl_TextEncoding XclTools::GetTextEncoding( sal_uInt16 nCodePage )
421 const XclCodePageEntry* pEntry = ::std::find_if( pCodePageTable, pCodePageTableEnd, XclCodePageEntry_CPPred( nCodePage ) );
422 if( pEntry == pCodePageTableEnd )
424 OSL_TRACE( "XclTools::GetTextEncoding - unknown code page: 0x%04hX (%d)", nCodePage, nCodePage );
425 return RTL_TEXTENCODING_DONTKNOW;
427 return pEntry->meTextEnc;
430 sal_uInt16 XclTools::GetXclCodePage( rtl_TextEncoding eTextEnc )
432 if( eTextEnc == RTL_TEXTENCODING_UNICODE )
433 return 1200; // for BIFF8
435 const XclCodePageEntry* pEntry = ::std::find_if( pCodePageTable, pCodePageTableEnd, XclCodePageEntry_TEPred( eTextEnc ) );
436 if( pEntry == pCodePageTableEnd )
438 OSL_TRACE( "XclTools::GetXclCodePage - unsupported text encoding: %d", eTextEnc );
439 return 1252;
441 return pEntry->mnCodePage;
444 OUString XclTools::GetXclFontName( const OUString& rFontName )
446 // substitute with MS fonts
447 OUString aNewName = GetSubsFontName(rFontName, SubsFontFlags::ONLYONE | SubsFontFlags::MS);
448 return aNewName.isEmpty() ? rFontName : aNewName;
451 // built-in defined names
452 const OUString XclTools::maDefNamePrefix( "Excel_BuiltIn_" );
454 const OUString XclTools::maDefNamePrefixXml ( "_xlnm." );
456 static const sal_Char* const ppcDefNames[] =
458 "Consolidate_Area",
459 "Auto_Open",
460 "Auto_Close",
461 "Extract",
462 "Database",
463 "Criteria",
464 "Print_Area",
465 "Print_Titles",
466 "Recorder",
467 "Data_Form",
468 "Auto_Activate",
469 "Auto_Deactivate",
470 "Sheet_Title",
471 "_FilterDatabase"
474 OUString XclTools::GetXclBuiltInDefName( sal_Unicode cBuiltIn )
476 OSL_ENSURE( SAL_N_ELEMENTS( ppcDefNames ) == EXC_BUILTIN_UNKNOWN,
477 "XclTools::GetXclBuiltInDefName - built-in defined name list modified" );
479 if( cBuiltIn < SAL_N_ELEMENTS( ppcDefNames ) )
480 return OUString::createFromAscii(ppcDefNames[cBuiltIn]);
481 else
482 return OUString::number(cBuiltIn);
485 OUString XclTools::GetBuiltInDefName( sal_Unicode cBuiltIn )
487 OUStringBuffer aBuf(maDefNamePrefix);
488 aBuf.append(GetXclBuiltInDefName(cBuiltIn));
489 return aBuf.makeStringAndClear();
492 OUString XclTools::GetBuiltInDefNameXml( sal_Unicode cBuiltIn )
494 OUStringBuffer aBuf(maDefNamePrefixXml);
495 aBuf.append(GetXclBuiltInDefName(cBuiltIn));
496 return aBuf.makeStringAndClear();
499 sal_Unicode XclTools::GetBuiltInDefNameIndex( const OUString& rDefName )
501 sal_Int32 nPrefixLen = maDefNamePrefix.getLength();
502 if( rDefName.startsWithIgnoreAsciiCase( maDefNamePrefix ) )
504 for( sal_Unicode cBuiltIn = 0; cBuiltIn < EXC_BUILTIN_UNKNOWN; ++cBuiltIn )
506 OUString aBuiltInName(GetXclBuiltInDefName(cBuiltIn));
507 sal_Int32 nBuiltInLen = aBuiltInName.getLength();
508 if( rDefName.matchIgnoreAsciiCase( aBuiltInName, nPrefixLen ) )
510 // name can be followed by underline or space character
511 sal_Int32 nNextCharPos = nPrefixLen + nBuiltInLen;
512 sal_Unicode cNextChar = (rDefName.getLength() > nNextCharPos) ? rDefName[nNextCharPos] : '\0';
513 if( (cNextChar == '\0') || (cNextChar == ' ') || (cNextChar == '_') )
514 return cBuiltIn;
518 return EXC_BUILTIN_UNKNOWN;
521 // built-in style names
523 const OUString XclTools::maStyleNamePrefix1( "Excel_BuiltIn_" );
524 const OUString XclTools::maStyleNamePrefix2( "Excel Built-in " );
526 static const sal_Char* const ppcStyleNames[] =
528 "", // "Normal" not used directly, but localized "Default"
529 "RowLevel_", // outline level will be appended
530 "ColumnLevel_", // outline level will be appended
531 "Comma",
532 "Currency",
533 "Percent",
534 "Comma_0",
535 "Currency_0",
536 "Hyperlink",
537 "Followed_Hyperlink"
540 OUString XclTools::GetBuiltInStyleName( sal_uInt8 nStyleId, const OUString& rName, sal_uInt8 nLevel )
542 OUString aStyleName;
544 if( nStyleId == EXC_STYLE_NORMAL ) // "Normal" becomes "Default" style
546 aStyleName = ScGlobal::GetRscString( STR_STYLENAME_STANDARD );
548 else
550 OUStringBuffer aBuf(maStyleNamePrefix1);
551 if( nStyleId < SAL_N_ELEMENTS( ppcStyleNames ) )
552 aBuf.appendAscii(ppcStyleNames[nStyleId]);
553 else if (!rName.isEmpty())
554 aBuf.append(rName);
555 else
556 aBuf.append(static_cast<sal_Int32>(nStyleId));
558 if( (nStyleId == EXC_STYLE_ROWLEVEL) || (nStyleId == EXC_STYLE_COLLEVEL) )
559 aBuf.append(static_cast<sal_Int32>(nLevel+1));
561 aStyleName = aBuf.makeStringAndClear();
564 return aStyleName;
567 bool XclTools::IsBuiltInStyleName( const OUString& rStyleName, sal_uInt8* pnStyleId, sal_Int32* pnNextChar )
569 // "Default" becomes "Normal"
570 if (rStyleName.equals(ScGlobal::GetRscString(STR_STYLENAME_STANDARD)))
572 if( pnStyleId ) *pnStyleId = EXC_STYLE_NORMAL;
573 if( pnNextChar ) *pnNextChar = rStyleName.getLength();
574 return true;
577 // try the other built-in styles
578 sal_uInt8 nFoundId = 0;
579 sal_Int32 nNextChar = 0;
581 sal_Int32 nPrefixLen = 0;
582 if( rStyleName.startsWithIgnoreAsciiCase( maStyleNamePrefix1 ) )
583 nPrefixLen = maStyleNamePrefix1.getLength();
584 else if( rStyleName.startsWithIgnoreAsciiCase( maStyleNamePrefix2 ) )
585 nPrefixLen = maStyleNamePrefix2.getLength();
586 if( nPrefixLen > 0 )
588 for( sal_uInt8 nId = 0; nId < SAL_N_ELEMENTS( ppcStyleNames ); ++nId )
590 if( nId != EXC_STYLE_NORMAL )
592 OUString aShortName = OUString::createFromAscii(ppcStyleNames[nId]);
593 if( rStyleName.matchIgnoreAsciiCase( aShortName, nPrefixLen ) &&
594 (nNextChar < nPrefixLen + aShortName.getLength()))
596 nFoundId = nId;
597 nNextChar = nPrefixLen + aShortName.getLength();
603 if( nNextChar > 0 )
605 if( pnStyleId ) *pnStyleId = nFoundId;
606 if( pnNextChar ) *pnNextChar = nNextChar;
607 return true;
610 if( pnStyleId ) *pnStyleId = EXC_STYLE_USERDEF;
611 if( pnNextChar ) *pnNextChar = 0;
612 return nPrefixLen > 0; // also return true for unknown built-in styles
615 bool XclTools::GetBuiltInStyleId( sal_uInt8& rnStyleId, sal_uInt8& rnLevel, const OUString& rStyleName )
617 sal_uInt8 nStyleId;
618 sal_Int32 nNextChar;
619 if( IsBuiltInStyleName( rStyleName, &nStyleId, &nNextChar ) && (nStyleId != EXC_STYLE_USERDEF) )
621 if( (nStyleId == EXC_STYLE_ROWLEVEL) || (nStyleId == EXC_STYLE_COLLEVEL) )
623 OUString aLevel = rStyleName.copy(nNextChar);
624 sal_Int32 nLevel = aLevel.toInt32();
625 if (OUString::number(nLevel) == aLevel && nLevel > 0 && nLevel <= EXC_STYLE_LEVELCOUNT)
627 rnStyleId = nStyleId;
628 rnLevel = static_cast< sal_uInt8 >( nLevel - 1 );
629 return true;
632 else if( rStyleName.getLength() == nNextChar )
634 rnStyleId = nStyleId;
635 rnLevel = EXC_STYLE_NOLEVEL;
636 return true;
639 rnStyleId = EXC_STYLE_USERDEF;
640 rnLevel = EXC_STYLE_NOLEVEL;
641 return false;
644 // conditional formatting style names
646 const OUString XclTools::maCFStyleNamePrefix1( "Excel_CondFormat_" );
647 const OUString XclTools::maCFStyleNamePrefix2( "ConditionalStyle_" );
649 OUString XclTools::GetCondFormatStyleName( SCTAB nScTab, sal_Int32 nFormat, sal_uInt16 nCondition )
651 OUStringBuffer aBuf(maCFStyleNamePrefix1);
652 aBuf.append(static_cast<sal_Int32>(nScTab+1));
653 aBuf.append('_');
654 aBuf.append(static_cast<sal_Int32>(nFormat+1));
655 aBuf.append('_');
656 aBuf.append(static_cast<sal_Int32>(nCondition+1));
657 return aBuf.makeStringAndClear();
660 bool XclTools::IsCondFormatStyleName( const OUString& rStyleName )
662 if( rStyleName.startsWithIgnoreAsciiCase( maCFStyleNamePrefix1 ) )
663 return true;
665 if( rStyleName.startsWithIgnoreAsciiCase( maCFStyleNamePrefix2 ) )
666 return true;
668 return false;
671 // stream handling
673 void XclTools::SkipSubStream( XclImpStream& rStrm )
675 bool bLoop = true;
676 while( bLoop && rStrm.StartNextRecord() )
678 sal_uInt16 nRecId = rStrm.GetRecId();
679 bLoop = nRecId != EXC_ID_EOF;
680 if( (nRecId == EXC_ID2_BOF) || (nRecId == EXC_ID3_BOF) || (nRecId == EXC_ID4_BOF) || (nRecId == EXC_ID5_BOF) )
681 SkipSubStream( rStrm );
685 // Basic macro names
687 const OUString XclTools::maSbMacroPrefix( "vnd.sun.star.script:" );
688 const OUString XclTools::maSbMacroSuffix( "?language=Basic&location=document" );
690 OUString XclTools::GetSbMacroUrl( const OUString& rMacroName, SfxObjectShell* pDocShell )
692 OSL_ENSURE( !rMacroName.isEmpty(), "XclTools::GetSbMacroUrl - macro name is empty" );
693 ::ooo::vba::MacroResolvedInfo aMacroInfo = ::ooo::vba::resolveVBAMacro( pDocShell, rMacroName, false );
694 if( aMacroInfo.mbFound )
695 return ::ooo::vba::makeMacroURL( aMacroInfo.msResolvedMacro );
696 return OUString();
699 OUString XclTools::GetXclMacroName( const OUString& rSbMacroUrl )
701 sal_Int32 nSbMacroUrlLen = rSbMacroUrl.getLength();
702 sal_Int32 nMacroNameLen = nSbMacroUrlLen - maSbMacroPrefix.getLength() - maSbMacroSuffix.getLength();
703 if( (nMacroNameLen > 0) && rSbMacroUrl.startsWithIgnoreAsciiCase( maSbMacroPrefix ) &&
704 rSbMacroUrl.endsWithIgnoreAsciiCase( maSbMacroSuffix ) )
706 sal_Int32 nPrjDot = rSbMacroUrl.indexOf( '.', maSbMacroPrefix.getLength() ) + 1;
707 return rSbMacroUrl.copy( nPrjDot, nSbMacroUrlLen - nPrjDot - maSbMacroSuffix.getLength() );
709 return OUString();
712 // read/write colors
714 XclImpStream& operator>>( XclImpStream& rStrm, Color& rColor )
716 sal_uInt8 nR = rStrm.ReaduInt8();
717 sal_uInt8 nG = rStrm.ReaduInt8();
718 sal_uInt8 nB = rStrm.ReaduInt8();
719 rStrm.Ignore( 1 );//nD
720 rColor.SetColor( RGB_COLORDATA( nR, nG, nB ) );
721 return rStrm;
724 XclExpStream& operator<<( XclExpStream& rStrm, const Color& rColor )
726 return rStrm << rColor.GetRed() << rColor.GetGreen() << rColor.GetBlue() << sal_uInt8( 0 );
729 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */