Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / data / global.cxx
blob05ac79c4b6393ba4be44824663bc7db20a7a9cf4
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 <vcl/svapp.hxx>
21 #include "scitems.hxx"
22 #include <svx/algitem.hxx>
23 #include <editeng/brushitem.hxx>
24 #include <editeng/editobj.hxx>
25 #include <editeng/scripttypeitem.hxx>
26 #include <svl/srchitem.hxx>
27 #include <editeng/langitem.hxx>
28 #include <sfx2/docfile.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <sfx2/objsh.hxx>
31 #include <sfx2/viewfrm.hxx>
32 #include <sfx2/viewsh.hxx>
33 #include <svl/stritem.hxx>
34 #include <svl/zforlist.hxx>
35 #include <svl/zformat.hxx>
36 #include <vcl/image.hxx>
37 #include <vcl/virdev.hxx>
38 #include <sal/macros.h>
39 #include <tools/rcid.h>
40 #include <unotools/charclass.hxx>
41 #include <unotools/securityoptions.hxx>
42 #include <stdlib.h>
43 #include <time.h>
44 #include <ctype.h>
45 #include <numeric>
48 #include <i18nlangtag/mslangid.hxx>
49 #include <com/sun/star/lang/Locale.hpp>
50 #include <comphelper/processfactory.hxx>
51 #include <comphelper/string.hxx>
52 #include <unotools/calendarwrapper.hxx>
53 #include <unotools/collatorwrapper.hxx>
54 #include <com/sun/star/i18n/CollatorOptions.hpp>
55 #include <unotools/intlwrapper.hxx>
56 #include <unotools/syslocale.hxx>
57 #include <unotools/transliterationwrapper.hxx>
59 #include "global.hxx"
60 #include "scresid.hxx"
61 #include "autoform.hxx"
62 #include "document.hxx"
63 #include "patattr.hxx"
64 #include "addincol.hxx"
65 #include "adiasync.hxx"
66 #include "userlist.hxx"
67 #include "interpre.hxx"
68 #include "strload.hxx"
69 #include "docpool.hxx"
70 #include "unitconv.hxx"
71 #include "compiler.hxx"
72 #include "parclass.hxx"
73 #include "funcdesc.hxx"
74 #include "globstr.hrc"
75 #include "scfuncs.hrc"
76 #include "sc.hrc"
77 #include "scmod.hxx"
78 #include "appoptio.hxx"
79 #include "random.hxx"
80 #include "editutil.hxx"
82 // -----------------------------------------------------------------------
85 ScDocShellRef* ScGlobal::pDrawClipDocShellRef = NULL;
86 SvxSearchItem* ScGlobal::pSearchItem = NULL;
87 ScAutoFormat* ScGlobal::pAutoFormat = NULL;
88 FuncCollection* ScGlobal::pFuncCollection = NULL;
89 ScUnoAddInCollection* ScGlobal::pAddInCollection = NULL;
90 ScUserList* ScGlobal::pUserList = NULL;
91 OUString** ScGlobal::ppRscString = NULL;
92 LanguageType ScGlobal::eLnge = LANGUAGE_SYSTEM;
93 ::com::sun::star::lang::Locale* ScGlobal::pLocale = NULL;
94 SvtSysLocale* ScGlobal::pSysLocale = NULL;
95 const CharClass* ScGlobal::pCharClass = NULL;
96 const LocaleDataWrapper* ScGlobal::pLocaleData = NULL;
97 CalendarWrapper* ScGlobal::pCalendar = NULL;
98 CollatorWrapper* ScGlobal::pCollator = NULL;
99 CollatorWrapper* ScGlobal::pCaseCollator = NULL;
100 ::utl::TransliterationWrapper* ScGlobal::pTransliteration = NULL;
101 ::utl::TransliterationWrapper* ScGlobal::pCaseTransliteration = NULL;
102 ::com::sun::star::uno::Reference< ::com::sun::star::i18n::XOrdinalSuffix> ScGlobal::xOrdinalSuffix = NULL;
103 IntlWrapper* ScGlobal::pScIntlWrapper = NULL;
104 sal_Unicode ScGlobal::cListDelimiter = ',';
105 OUString* ScGlobal::pEmptyOUString = NULL;
106 OUString* ScGlobal::pStrClipDocName = NULL;
108 SvxBrushItem* ScGlobal::pEmptyBrushItem = NULL;
109 SvxBrushItem* ScGlobal::pButtonBrushItem = NULL;
110 SvxBrushItem* ScGlobal::pEmbeddedBrushItem = NULL;
111 SvxBrushItem* ScGlobal::pProtectedBrushItem = NULL;
113 ImageList* ScGlobal::pOutlineBitmaps = NULL;
115 ScFunctionList* ScGlobal::pStarCalcFunctionList = NULL;
116 ScFunctionMgr* ScGlobal::pStarCalcFunctionMgr = NULL;
118 ScUnitConverter* ScGlobal::pUnitConverter = NULL;
119 SvNumberFormatter* ScGlobal::pEnglishFormatter = NULL;
120 ScFieldEditEngine* ScGlobal::pFieldEditEngine = NULL;
122 double ScGlobal::nScreenPPTX = 96.0;
123 double ScGlobal::nScreenPPTY = 96.0;
125 sal_uInt16 ScGlobal::nDefFontHeight = 225;
126 sal_uInt16 ScGlobal::nStdRowHeight = 256;
128 long ScGlobal::nLastRowHeightExtra = 0;
129 long ScGlobal::nLastColWidthExtra = STD_EXTRA_WIDTH;
131 static sal_uInt16 nPPTZoom = 0; // ScreenZoom used to determine nScreenPPTX/Y
134 class SfxViewShell;
135 SfxViewShell* pScActiveViewShell = NULL; //! als Member !!!!!
136 sal_uInt16 nScClickMouseModifier = 0; //! dito
137 sal_uInt16 nScFillModeMouseModifier = 0; //! dito
139 // Hack: ScGlobal::GetUserList() muss InitAppOptions in der UI aufrufen,
140 // damit UserList aus Cfg geladen wird
142 void global_InitAppOptions();
144 //========================================================================
146 // statische Funktionen
148 //========================================================================
150 sal_Bool ScGlobal::HasAttrChanged( const SfxItemSet& rNewAttrs,
151 const SfxItemSet& rOldAttrs,
152 const sal_uInt16 nWhich )
154 sal_Bool bInvalidate = false;
155 const SfxItemState eNewState = rNewAttrs.GetItemState( nWhich );
156 const SfxItemState eOldState = rOldAttrs.GetItemState( nWhich );
158 //----------------------------------------------------------
160 if ( eNewState == eOldState )
162 // beide Items gesetzt
163 // PoolItems, d.h. Pointer-Vergleich zulaessig
164 if ( SFX_ITEM_SET == eOldState )
165 bInvalidate = (&rNewAttrs.Get( nWhich ) != &rOldAttrs.Get( nWhich ));
167 else
169 // ein Default-Item dabei
170 // PoolItems, d.h. Item-Vergleich noetig
172 const SfxPoolItem& rOldItem = ( SFX_ITEM_SET == eOldState )
173 ? rOldAttrs.Get( nWhich )
174 : rOldAttrs.GetPool()->GetDefaultItem( nWhich );
176 const SfxPoolItem& rNewItem = ( SFX_ITEM_SET == eNewState )
177 ? rNewAttrs.Get( nWhich )
178 : rNewAttrs.GetPool()->GetDefaultItem( nWhich );
180 bInvalidate = sal::static_int_cast<sal_Bool>(rNewItem != rOldItem);
183 return bInvalidate;
186 sal_uLong ScGlobal::GetStandardFormat( SvNumberFormatter& rFormatter,
187 sal_uLong nFormat, short nType )
189 const SvNumberformat* pFormat = rFormatter.GetEntry( nFormat );
190 if ( pFormat )
191 return rFormatter.GetStandardFormat( nFormat, nType, pFormat->GetLanguage() );
192 return rFormatter.GetStandardFormat( nType, eLnge );
195 sal_uInt16 ScGlobal::GetStandardRowHeight()
197 return nStdRowHeight;
200 sal_uLong ScGlobal::GetStandardFormat( double fNumber, SvNumberFormatter& rFormatter,
201 sal_uLong nFormat, short nType )
203 const SvNumberformat* pFormat = rFormatter.GetEntry( nFormat );
204 if ( pFormat )
205 return rFormatter.GetStandardFormat( fNumber, nFormat, nType,
206 pFormat->GetLanguage() );
207 return rFormatter.GetStandardFormat( nType, eLnge );
211 SvNumberFormatter* ScGlobal::GetEnglishFormatter()
213 if ( !pEnglishFormatter )
215 pEnglishFormatter = new SvNumberFormatter(
216 ::comphelper::getProcessComponentContext(), LANGUAGE_ENGLISH_US );
217 pEnglishFormatter->SetEvalDateFormat( NF_EVALDATEFORMAT_INTL_FORMAT );
219 return pEnglishFormatter;
223 //------------------------------------------------------------------------
225 sal_Bool ScGlobal::CheckWidthInvalidate( bool& bNumFormatChanged,
226 const SfxItemSet& rNewAttrs,
227 const SfxItemSet& rOldAttrs )
229 // Ueberpruefen, ob Attributaenderungen in rNewAttrs gegnueber
230 // rOldAttrs die Textbreite an einer Zelle ungueltig machen
232 bNumFormatChanged =
233 HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_VALUE_FORMAT );
234 return ( bNumFormatChanged
235 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_LANGUAGE_FORMAT )
236 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT )
237 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CJK_FONT )
238 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CTL_FONT )
239 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_HEIGHT )
240 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CJK_FONT_HEIGHT )
241 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CTL_FONT_HEIGHT )
242 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_WEIGHT )
243 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CJK_FONT_WEIGHT )
244 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CTL_FONT_WEIGHT )
245 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_POSTURE )
246 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CJK_FONT_POSTURE )
247 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_CTL_FONT_POSTURE )
248 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_UNDERLINE )
249 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_OVERLINE )
250 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_CROSSEDOUT )
251 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_CONTOUR )
252 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_FONT_SHADOWED )
253 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_STACKED )
254 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_ROTATE_VALUE )
255 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_ROTATE_MODE )
256 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_LINEBREAK )
257 || HasAttrChanged( rNewAttrs, rOldAttrs, ATTR_MARGIN )
261 const SvxSearchItem& ScGlobal::GetSearchItem()
263 if (!pSearchItem)
265 pSearchItem = new SvxSearchItem( SID_SEARCH_ITEM );
266 pSearchItem->SetAppFlag( SVX_SEARCHAPP_CALC );
268 return *pSearchItem;
271 void ScGlobal::SetSearchItem( const SvxSearchItem& rNew )
273 // Hier waere ein Zuweisungsoperator ganz nett:
274 delete pSearchItem;
275 pSearchItem = (SvxSearchItem*)rNew.Clone();
277 pSearchItem->SetWhich( SID_SEARCH_ITEM );
278 pSearchItem->SetAppFlag( SVX_SEARCHAPP_CALC );
281 void ScGlobal::ClearAutoFormat()
283 if (pAutoFormat!=NULL)
285 delete pAutoFormat;
286 pAutoFormat=NULL;
290 ScAutoFormat* ScGlobal::GetAutoFormat()
292 return pAutoFormat;
295 ScAutoFormat* ScGlobal::GetOrCreateAutoFormat()
297 if ( !pAutoFormat )
299 pAutoFormat = new ScAutoFormat;
300 pAutoFormat->Load();
303 return pAutoFormat;
306 FuncCollection* ScGlobal::GetFuncCollection()
308 if (!pFuncCollection)
309 pFuncCollection = new FuncCollection();
310 return pFuncCollection;
313 ScUnoAddInCollection* ScGlobal::GetAddInCollection()
315 if (!pAddInCollection)
316 pAddInCollection = new ScUnoAddInCollection();
317 return pAddInCollection;
320 ScUserList* ScGlobal::GetUserList()
322 // Hack: Cfg-Item an der App ggF. laden
324 global_InitAppOptions();
326 if (!pUserList)
327 pUserList = new ScUserList();
328 return pUserList;
331 void ScGlobal::SetUserList( const ScUserList* pNewList )
333 if ( pNewList )
335 if ( !pUserList )
336 pUserList = new ScUserList( *pNewList );
337 else
338 *pUserList = *pNewList;
340 else
342 delete pUserList;
343 pUserList = NULL;
347 const OUString& ScGlobal::GetRscString( sal_uInt16 nIndex )
349 assert( nIndex < SC_GLOBSTR_STR_COUNT);
350 if( !ppRscString[ nIndex ] )
352 OpCode eOp = ocNone;
353 // Map former globstr.src strings moved to compiler.src
354 switch (nIndex)
356 case STR_NULL_ERROR:
357 eOp = ocErrNull;
358 break;
359 case STR_DIV_ZERO:
360 eOp = ocErrDivZero;
361 break;
362 case STR_NO_VALUE:
363 eOp = ocErrValue;
364 break;
365 case STR_NOREF_STR:
366 eOp = ocErrRef;
367 break;
368 case STR_NO_NAME_REF:
369 eOp = ocErrName;
370 break;
371 case STR_NUM_ERROR:
372 eOp = ocErrNum;
373 break;
374 case STR_NV_STR:
375 eOp = ocErrNA;
376 break;
377 default:
378 ; // nothing
380 if (eOp != ocNone)
381 ppRscString[ nIndex ] = new OUString( ScCompiler::GetNativeSymbol( eOp));
382 else
383 ppRscString[ nIndex ] = new OUString( SC_STRLOAD( RID_GLOBSTR, nIndex ));
385 return *ppRscString[ nIndex ];
388 OUString ScGlobal::GetErrorString(sal_uInt16 nErrNumber)
390 OUString sResStr;
391 switch (nErrNumber)
393 case NOTAVAILABLE : nErrNumber = STR_NV_STR; break;
394 case errNoRef : nErrNumber = STR_NO_REF_TABLE; break;
395 case errNoName : nErrNumber = STR_NO_NAME_REF; break;
396 case errNoAddin : nErrNumber = STR_NO_ADDIN; break;
397 case errNoMacro : nErrNumber = STR_NO_MACRO; break;
398 case errDoubleRef :
399 case errNoValue : nErrNumber = STR_NO_VALUE; break;
400 case errNoCode : nErrNumber = STR_NULL_ERROR; break;
401 case errDivisionByZero : nErrNumber = STR_DIV_ZERO; break;
402 case errIllegalFPOperation : nErrNumber = STR_NUM_ERROR; break;
404 default : sResStr = GetRscString(STR_ERROR_STR) + OUString::number( nErrNumber );
405 nErrNumber = 0;
406 break;
408 if( nErrNumber )
409 sResStr = GetRscString( nErrNumber );
410 return sResStr;
413 OUString ScGlobal::GetLongErrorString(sal_uInt16 nErrNumber)
415 switch (nErrNumber)
417 case 0:
418 break;
419 case 1:
420 case errIllegalArgument:
421 nErrNumber = STR_LONG_ERR_ILL_ARG;
422 break;
423 case 2:
424 case 3:
425 case 4:
426 case 5:
427 case errIllegalFPOperation:
428 nErrNumber = STR_LONG_ERR_ILL_FPO;
429 break;
430 case errIllegalChar:
431 nErrNumber = STR_LONG_ERR_ILL_CHAR;
432 break;
433 case errIllegalParameter:
434 nErrNumber = STR_LONG_ERR_ILL_PAR;
435 break;
436 case errSeparator:
437 nErrNumber = STR_LONG_ERR_ILL_SEP;
438 break;
439 case errPair:
440 case errPairExpected:
441 nErrNumber = STR_LONG_ERR_PAIR;
442 break;
443 case errOperatorExpected:
444 nErrNumber = STR_LONG_ERR_OP_EXP;
445 break;
446 case errVariableExpected:
447 case errParameterExpected:
448 nErrNumber = STR_LONG_ERR_VAR_EXP;
449 break;
450 case errCodeOverflow:
451 nErrNumber = STR_LONG_ERR_CODE_OVF;
452 break;
453 case errStringOverflow:
454 nErrNumber = STR_LONG_ERR_STR_OVF;
455 break;
456 case errStackOverflow:
457 case errInterpOverflow:
458 nErrNumber = STR_LONG_ERR_STACK_OVF;
459 break;
460 case errIllegalJump:
461 case errUnknownState:
462 case errUnknownVariable:
463 case errUnknownOpCode:
464 case errUnknownStackVariable:
465 case errUnknownToken:
466 case errNoCode:
467 case errDoubleRef:
468 nErrNumber = STR_LONG_ERR_SYNTAX;
469 break;
470 case errCircularReference:
471 nErrNumber = STR_LONG_ERR_CIRC_REF;
472 break;
473 case errNoConvergence:
474 nErrNumber = STR_LONG_ERR_NO_CONV;
475 break;
476 case errNoRef:
477 nErrNumber = STR_LONG_ERR_NO_REF;
478 break;
479 case errNoName:
480 nErrNumber = STR_LONG_ERR_NO_NAME;
481 break;
482 case errNoAddin:
483 nErrNumber = STR_LONG_ERR_NO_ADDIN;
484 break;
485 case errNoMacro:
486 nErrNumber = STR_LONG_ERR_NO_MACRO;
487 break;
488 case errDivisionByZero:
489 nErrNumber = STR_LONG_ERR_DIV_ZERO;
490 break;
491 case errNestedArray:
492 nErrNumber = STR_ERR_LONG_NESTED_ARRAY;
493 break;
494 case errNoValue:
495 nErrNumber = STR_LONG_ERR_NO_VALUE;
496 break;
497 case NOTAVAILABLE:
498 nErrNumber = STR_LONG_ERR_NV;
499 break;
500 default:
501 nErrNumber = STR_ERROR_STR;
502 break;
504 OUString aRes( GetRscString( nErrNumber ) );
505 return aRes;
508 SvxBrushItem* ScGlobal::GetButtonBrushItem()
510 pButtonBrushItem->SetColor( Application::GetSettings().GetStyleSettings().GetFaceColor() );
511 return pButtonBrushItem;
514 const OUString& ScGlobal::GetEmptyOUString()
516 return *pEmptyOUString;
519 ImageList* ScGlobal::GetOutlineSymbols()
521 ImageList*& rpImageList = pOutlineBitmaps;
522 if( !rpImageList )
523 rpImageList = new ImageList( ScResId( RID_OUTLINEBITMAPS ) );
524 return rpImageList;
527 void ScGlobal::Init()
529 pEmptyOUString = new OUString;
531 // Die Default-Sprache fuer Zahlenformate (ScGlobal::eLnge)
532 // muss immer LANGUAGE_SYSTEM sein
533 //! Dann kann auch die Variable raus
534 eLnge = LANGUAGE_SYSTEM;
536 //! Wenn Sortierung etc. von der Sprache der installierten Offfice-Version
537 //! abhaengen sollen, hier "Application::GetSettings().GetUILanguage()"
538 pSysLocale = new SvtSysLocale;
539 pCharClass = pSysLocale->GetCharClassPtr();
540 pLocaleData = pSysLocale->GetLocaleDataPtr();
542 ppRscString = new OUString *[ SC_GLOBSTR_STR_COUNT ];
543 for( sal_uInt16 nC = 0 ; nC < SC_GLOBSTR_STR_COUNT ; nC++ ) ppRscString[ nC ] = NULL;
545 pEmptyBrushItem = new SvxBrushItem( Color( COL_TRANSPARENT ), ATTR_BACKGROUND );
546 pButtonBrushItem = new SvxBrushItem( Color(), ATTR_BACKGROUND );
547 pEmbeddedBrushItem = new SvxBrushItem( Color( COL_LIGHTCYAN ), ATTR_BACKGROUND );
548 pProtectedBrushItem = new SvxBrushItem( Color( COL_LIGHTGRAY ), ATTR_BACKGROUND );
550 UpdatePPT(NULL);
551 //ScCompiler::InitSymbolsNative();
552 // ScParameterClassification _after_ Compiler, needs function resources if
553 // arguments are to be merged in, which in turn need strings of function
554 // names from the compiler.
555 ScParameterClassification::Init();
556 srand( (unsigned) time( NULL ) ); // Random Seed Init fuer Interpreter
557 sc::rng::seed( time( NULL ) ); // seed for libc rand() replacement
559 InitAddIns();
561 pStrClipDocName = new OUString( ScResId( SCSTR_NONAME ) );
562 *pStrClipDocName += "1";
564 // ScDocumentPool::InitVersionMaps() ist schon vorher gerufen worden
567 void ScGlobal::UpdatePPT( OutputDevice* pDev )
569 sal_uInt16 nCurrentZoom = Application::GetSettings().GetStyleSettings().GetScreenZoom();
570 if ( nCurrentZoom != nPPTZoom )
572 // Screen PPT values must be updated when ScreenZoom has changed.
573 // If called from Window::DataChanged, the window is passed as pDev,
574 // to make sure LogicToPixel uses a device which already uses the new zoom.
575 // For the initial settings, NULL is passed and GetDefaultDevice used.
577 if ( !pDev )
578 pDev = Application::GetDefaultDevice();
579 Point aPix1000 = pDev->LogicToPixel( Point(1000,1000), MAP_TWIP );
580 nScreenPPTX = aPix1000.X() / 1000.0;
581 nScreenPPTY = aPix1000.Y() / 1000.0;
582 nPPTZoom = nCurrentZoom;
586 const OUString& ScGlobal::GetClipDocName()
588 return *pStrClipDocName;
591 void ScGlobal::SetClipDocName( const OUString& rNew )
593 *pStrClipDocName = rNew;
597 void ScGlobal::InitTextHeight(SfxItemPool* pPool)
599 if (!pPool)
601 OSL_FAIL("ScGlobal::InitTextHeight: No Pool");
602 return;
605 const ScPatternAttr* pPattern = (const ScPatternAttr*)&pPool->GetDefaultItem(ATTR_PATTERN);
606 if (!pPattern)
608 OSL_FAIL("ScGlobal::InitTextHeight: No default pattern");
609 return;
612 OutputDevice* pDefaultDev = Application::GetDefaultDevice();
613 VirtualDevice aVirtWindow( *pDefaultDev );
614 aVirtWindow.SetMapMode(MAP_PIXEL);
615 Font aDefFont;
616 pPattern->GetFont(aDefFont, SC_AUTOCOL_BLACK, &aVirtWindow); // font color doesn't matter here
617 aVirtWindow.SetFont(aDefFont);
618 sal_uInt16 nTest = static_cast<sal_uInt16>(
619 aVirtWindow.PixelToLogic(Size(0, aVirtWindow.GetTextHeight()), MAP_TWIP).Height());
621 if (nTest > nDefFontHeight)
622 nDefFontHeight = nTest;
624 const SvxMarginItem* pMargin = (const SvxMarginItem*)&pPattern->GetItem(ATTR_MARGIN);
626 nTest = static_cast<sal_uInt16>(
627 nDefFontHeight + pMargin->GetTopMargin() + pMargin->GetBottomMargin() - STD_ROWHEIGHT_DIFF);
629 if (nTest > nStdRowHeight)
630 nStdRowHeight = nTest;
633 void ScGlobal::Clear()
635 // asyncs _vor_ ExitExternalFunc zerstoeren!
636 for( ScAddInAsyncs::iterator it = theAddInAsyncTbl.begin(); it != theAddInAsyncTbl.end(); ++it )
638 delete *it;
640 theAddInAsyncTbl.clear();
641 ExitExternalFunc();
642 DELETEZ(pAutoFormat);
643 DELETEZ(pSearchItem);
644 DELETEZ(pFuncCollection);
645 DELETEZ(pAddInCollection);
646 DELETEZ(pUserList);
648 for( sal_uInt16 nC = 0 ; nC < SC_GLOBSTR_STR_COUNT ; nC++ )
649 if( ppRscString ) delete ppRscString[ nC ];
650 delete[] ppRscString;
651 ppRscString = NULL;
653 DELETEZ(pStarCalcFunctionList); // vor ResMgr zerstoeren!
654 DELETEZ(pStarCalcFunctionMgr);
655 ScParameterClassification::Exit();
656 ScCompiler::DeInit();
657 ScInterpreter::GlobalExit(); // statischen Stack loeschen
659 DELETEZ(pEmptyBrushItem);
660 DELETEZ(pButtonBrushItem);
661 DELETEZ(pEmbeddedBrushItem);
662 DELETEZ(pProtectedBrushItem);
663 DELETEZ(pOutlineBitmaps);
664 DELETEZ(pEnglishFormatter);
665 DELETEZ(pCaseTransliteration);
666 DELETEZ(pTransliteration);
667 DELETEZ(pCaseCollator);
668 DELETEZ(pCollator);
669 DELETEZ(pCalendar);
670 //! do NOT delete pCharClass since it is a pointer to the single SvtSysLocale instance
671 pCharClass = NULL;
672 //! do NOT delete pLocaleData since it is a pointer to the single SvtSysLocale instance
673 pLocaleData = NULL;
674 DELETEZ(pSysLocale);
675 DELETEZ(pLocale);
676 DELETEZ(pScIntlWrapper);
677 DELETEZ(pStrClipDocName);
679 DELETEZ(pUnitConverter);
680 DELETEZ(pFieldEditEngine);
682 ScDocumentPool::DeleteVersionMaps();
684 DELETEZ(pEmptyOUString);
687 //------------------------------------------------------------------------
689 rtl_TextEncoding ScGlobal::GetCharsetValue( const OUString& rCharSet )
691 // new TextEncoding values
692 if ( CharClass::isAsciiNumeric( rCharSet ) )
694 sal_Int32 nVal = rCharSet.toInt32();
695 if ( !nVal || nVal == RTL_TEXTENCODING_DONTKNOW )
696 return osl_getThreadTextEncoding();
697 return (rtl_TextEncoding) nVal;
699 // old CharSet values for compatibility
700 else if (rCharSet.equalsIgnoreAsciiCase("ANSI") ) return RTL_TEXTENCODING_MS_1252;
701 else if (rCharSet.equalsIgnoreAsciiCase("MAC") ) return RTL_TEXTENCODING_APPLE_ROMAN;
702 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC") ) return RTL_TEXTENCODING_IBM_850;
703 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC_437")) return RTL_TEXTENCODING_IBM_437;
704 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC_850")) return RTL_TEXTENCODING_IBM_850;
705 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC_860")) return RTL_TEXTENCODING_IBM_860;
706 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC_861")) return RTL_TEXTENCODING_IBM_861;
707 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC_863")) return RTL_TEXTENCODING_IBM_863;
708 else if (rCharSet.equalsIgnoreAsciiCase("IBMPC_865")) return RTL_TEXTENCODING_IBM_865;
709 else return osl_getThreadTextEncoding();
712 //------------------------------------------------------------------------
714 OUString ScGlobal::GetCharsetString( rtl_TextEncoding eVal )
716 const sal_Char* pChar;
717 switch ( eVal )
719 // old CharSet strings for compatibility
720 case RTL_TEXTENCODING_MS_1252: pChar = "ANSI"; break;
721 case RTL_TEXTENCODING_APPLE_ROMAN: pChar = "MAC"; break;
722 // IBMPC == IBMPC_850
723 case RTL_TEXTENCODING_IBM_437: pChar = "IBMPC_437"; break;
724 case RTL_TEXTENCODING_IBM_850: pChar = "IBMPC_850"; break;
725 case RTL_TEXTENCODING_IBM_860: pChar = "IBMPC_860"; break;
726 case RTL_TEXTENCODING_IBM_861: pChar = "IBMPC_861"; break;
727 case RTL_TEXTENCODING_IBM_863: pChar = "IBMPC_863"; break;
728 case RTL_TEXTENCODING_IBM_865: pChar = "IBMPC_865"; break;
729 case RTL_TEXTENCODING_DONTKNOW: pChar = "SYSTEM"; break;
730 // new string of TextEncoding value
731 default:
732 return OUString::number( eVal );
734 return OUString::createFromAscii(pChar);
737 //------------------------------------------------------------------------
739 bool ScGlobal::HasStarCalcFunctionList()
741 return ( pStarCalcFunctionList != NULL );
744 ScFunctionList* ScGlobal::GetStarCalcFunctionList()
746 if ( !pStarCalcFunctionList )
747 pStarCalcFunctionList = new ScFunctionList;
749 return pStarCalcFunctionList;
752 //------------------------------------------------------------------------
754 ScFunctionMgr* ScGlobal::GetStarCalcFunctionMgr()
756 if ( !pStarCalcFunctionMgr )
757 pStarCalcFunctionMgr = new ScFunctionMgr;
759 return pStarCalcFunctionMgr;
762 void ScGlobal::ResetFunctionList()
764 // FunctionMgr has pointers into FunctionList, must also be updated
766 DELETEZ( pStarCalcFunctionMgr );
767 DELETEZ( pStarCalcFunctionList );
770 //------------------------------------------------------------------------
772 ScUnitConverter* ScGlobal::GetUnitConverter()
774 if ( !pUnitConverter )
775 pUnitConverter = new ScUnitConverter;
777 return pUnitConverter;
781 //------------------------------------------------------------------------
783 const sal_Unicode* ScGlobal::UnicodeStrChr( const sal_Unicode* pStr,
784 sal_Unicode c )
786 if ( !pStr )
787 return NULL;
788 while ( *pStr )
790 if ( *pStr == c )
791 return pStr;
792 pStr++;
794 return NULL;
797 // ----------------------------------------------------------------------------
799 OUString ScGlobal::addToken(const OUString& rTokenList, const OUString& rToken,
800 sal_Unicode cSep, sal_Int32 nSepCount, bool bForceSep)
802 OUStringBuffer aBuf(rTokenList);
803 if( bForceSep || (!rToken.isEmpty() && !rTokenList.isEmpty()) )
804 comphelper::string::padToLength(aBuf, aBuf.getLength() + nSepCount, cSep);
805 aBuf.append(rToken);
806 return aBuf.makeStringAndClear();
809 bool ScGlobal::IsQuoted( const OUString& rString, sal_Unicode cQuote )
811 return (rString.getLength() >= 2) && (rString[0] == cQuote) && (rString[ rString.getLength() - 1 ] == cQuote);
814 void ScGlobal::AddQuotes( OUString& rString, sal_Unicode cQuote, bool bEscapeEmbedded )
816 if (bEscapeEmbedded)
818 sal_Unicode pQ[3];
819 pQ[0] = pQ[1] = cQuote;
820 pQ[2] = 0;
821 OUString aQuotes( pQ );
822 rString = rString.replaceAll( OUString(cQuote), aQuotes);
824 rString = OUString( cQuote ) + rString + OUString( cQuote );
827 void ScGlobal::EraseQuotes( OUString& rString, sal_Unicode cQuote, bool bUnescapeEmbedded )
829 if ( IsQuoted( rString, cQuote ) )
831 rString = rString.copy( 1, rString.getLength() - 2 );
832 if (bUnescapeEmbedded)
834 sal_Unicode pQ[3];
835 pQ[0] = pQ[1] = cQuote;
836 pQ[2] = 0;
837 OUString aQuotes( pQ );
838 rString = rString.replaceAll( aQuotes, OUString(cQuote));
843 sal_Int32 ScGlobal::FindUnquoted( const OUString& rString, sal_Unicode cChar)
845 const sal_Unicode cQuote = '\'';
846 const sal_Unicode* const pStart = rString.getStr();
847 const sal_Unicode* const pStop = pStart + rString.getLength();
848 const sal_Unicode* p = pStart;
849 bool bQuoted = false;
850 while (p < pStop)
852 if (*p == cChar && !bQuoted)
853 return sal::static_int_cast< sal_Int32 >( p - pStart );
854 else if (*p == cQuote)
856 if (!bQuoted)
857 bQuoted = true;
858 else if (p < pStop-1 && *(p+1) == cQuote)
859 ++p;
860 else
861 bQuoted = false;
863 ++p;
865 return -1;
868 const sal_Unicode* ScGlobal::FindUnquoted( const sal_Unicode* pString, sal_Unicode cChar, sal_Unicode cQuote )
870 const sal_Unicode* p = pString;
871 bool bQuoted = false;
872 while (*p)
874 if (*p == cChar && !bQuoted)
875 return p;
876 else if (*p == cQuote)
878 if (!bQuoted)
879 bQuoted = true;
880 else if (*(p+1) == cQuote)
881 ++p;
882 else
883 bQuoted = false;
885 ++p;
887 return NULL;
890 //------------------------------------------------------------------------
892 sal_Bool ScGlobal::EETextObjEqual( const EditTextObject* pObj1,
893 const EditTextObject* pObj2 )
895 if ( pObj1 == pObj2 ) // both empty or the same object
896 return sal_True;
898 if ( pObj1 && pObj2 )
900 // first test for equal text content
901 sal_Int32 nParCount = pObj1->GetParagraphCount();
902 if ( nParCount != pObj2->GetParagraphCount() )
903 return false;
904 for (sal_Int32 nPar=0; nPar<nParCount; nPar++)
905 if ( pObj1->GetText(nPar) != pObj2->GetText(nPar) )
906 return false;
908 SvMemoryStream aStream1;
909 SvMemoryStream aStream2;
910 pObj1->Store( aStream1 );
911 pObj2->Store( aStream2 );
912 sal_uLong nSize = aStream1.Tell();
913 if ( aStream2.Tell() == nSize )
914 if ( !memcmp( aStream1.GetData(), aStream2.GetData(), (sal_uInt16) nSize ) )
915 return sal_True;
918 return false;
921 void ScGlobal::OpenURL( const OUString& rURL, const OUString& rTarget )
923 // OpenURL wird immer ueber irgendwelche Umwege durch Mausklicks im GridWindow
924 // aufgerufen, darum stimmen pScActiveViewShell und nScClickMouseModifier.
925 //SvtSecurityOptions to access Libreoffice global security parameters
926 SvtSecurityOptions aSecOpt;
927 bool bCtrlClickHappened = (nScClickMouseModifier & KEY_MOD1);
928 bool bCtrlClickSecOption = aSecOpt.IsOptionSet( SvtSecurityOptions::E_CTRLCLICK_HYPERLINK );
929 if( bCtrlClickHappened && !( bCtrlClickSecOption ) )
931 //return since ctrl+click happened when the
932 //ctrl+click security option was disabled, link should not open
933 return;
935 else if( !( bCtrlClickHappened ) && bCtrlClickSecOption )
937 //ctrl+click did not happen; only click happened maybe with some
938 //other key combo. and security option is set, so return
939 return;
941 SfxStringItem aUrl( SID_FILE_NAME, rURL );
942 SfxStringItem aTarget( SID_TARGETNAME, rTarget );
943 aTarget.SetValue(OUString("_blank"));
944 SfxViewFrame* pFrame = NULL;
945 OUString aReferName;
946 if ( pScActiveViewShell )
948 pFrame = pScActiveViewShell->GetViewFrame();
949 SfxMedium* pMed = pFrame->GetObjectShell()->GetMedium();
950 if (pMed)
951 aReferName = pMed->GetName();
954 SfxFrameItem aFrm( SID_DOCFRAME, pFrame );
955 SfxStringItem aReferer( SID_REFERER, aReferName );
957 SfxBoolItem aNewView( SID_OPEN_NEW_VIEW, false );
958 SfxBoolItem aBrowsing( SID_BROWSE, sal_True );
960 // kein SID_SILENT mehr
962 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
963 if (pViewFrm)
964 pViewFrm->GetDispatcher()->Execute( SID_OPENDOC,
965 SFX_CALLMODE_ASYNCHRON | SFX_CALLMODE_RECORD,
966 &aUrl, &aTarget,
967 &aFrm, &aReferer,
968 &aNewView, &aBrowsing,
969 0L );
972 //------------------------------------------------------------------------
974 sal_Bool ScGlobal::IsSystemRTL()
976 return MsLangId::isRightToLeft( Application::GetSettings().GetLanguageTag().getLanguageType() );
979 sal_uInt8 ScGlobal::GetDefaultScriptType()
981 // Used when text contains only WEAK characters.
982 // Script type of office language is used then (same as GetEditDefaultLanguage,
983 // to get consistent behavior of text in simple cells and EditEngine,
984 // also same as GetAppLanguage() in Writer)
986 return (sal_uInt8) SvtLanguageOptions::GetScriptTypeOfLanguage( Application::GetSettings().GetLanguageTag().getLanguageType() );
989 LanguageType ScGlobal::GetEditDefaultLanguage()
991 // used for EditEngine::SetDefaultLanguage
993 return Application::GetSettings().GetLanguageTag().getLanguageType();
996 sal_uInt16 ScGlobal::GetScriptedWhichID( sal_uInt8 nScriptType, sal_uInt16 nWhich )
998 switch ( nScriptType )
1000 case SCRIPTTYPE_LATIN:
1001 case SCRIPTTYPE_ASIAN:
1002 case SCRIPTTYPE_COMPLEX:
1003 break; // take exact matches
1004 default: // prefer one, first COMPLEX, then ASIAN
1005 if ( nScriptType & SCRIPTTYPE_COMPLEX )
1006 nScriptType = SCRIPTTYPE_COMPLEX;
1007 else if ( nScriptType & SCRIPTTYPE_ASIAN )
1008 nScriptType = SCRIPTTYPE_ASIAN;
1010 switch ( nScriptType )
1012 case SCRIPTTYPE_COMPLEX:
1014 switch ( nWhich )
1016 case ATTR_FONT:
1017 case ATTR_CJK_FONT:
1018 nWhich = ATTR_CTL_FONT;
1019 break;
1020 case ATTR_FONT_HEIGHT:
1021 case ATTR_CJK_FONT_HEIGHT:
1022 nWhich = ATTR_CTL_FONT_HEIGHT;
1023 break;
1024 case ATTR_FONT_WEIGHT:
1025 case ATTR_CJK_FONT_WEIGHT:
1026 nWhich = ATTR_CTL_FONT_WEIGHT;
1027 break;
1028 case ATTR_FONT_POSTURE:
1029 case ATTR_CJK_FONT_POSTURE:
1030 nWhich = ATTR_CTL_FONT_POSTURE;
1031 break;
1034 break;
1035 case SCRIPTTYPE_ASIAN:
1037 switch ( nWhich )
1039 case ATTR_FONT:
1040 case ATTR_CTL_FONT:
1041 nWhich = ATTR_CJK_FONT;
1042 break;
1043 case ATTR_FONT_HEIGHT:
1044 case ATTR_CTL_FONT_HEIGHT:
1045 nWhich = ATTR_CJK_FONT_HEIGHT;
1046 break;
1047 case ATTR_FONT_WEIGHT:
1048 case ATTR_CTL_FONT_WEIGHT:
1049 nWhich = ATTR_CJK_FONT_WEIGHT;
1050 break;
1051 case ATTR_FONT_POSTURE:
1052 case ATTR_CTL_FONT_POSTURE:
1053 nWhich = ATTR_CJK_FONT_POSTURE;
1054 break;
1057 break;
1058 default:
1060 switch ( nWhich )
1062 case ATTR_CTL_FONT:
1063 case ATTR_CJK_FONT:
1064 nWhich = ATTR_FONT;
1065 break;
1066 case ATTR_CTL_FONT_HEIGHT:
1067 case ATTR_CJK_FONT_HEIGHT:
1068 nWhich = ATTR_FONT_HEIGHT;
1069 break;
1070 case ATTR_CTL_FONT_WEIGHT:
1071 case ATTR_CJK_FONT_WEIGHT:
1072 nWhich = ATTR_FONT_WEIGHT;
1073 break;
1074 case ATTR_CTL_FONT_POSTURE:
1075 case ATTR_CJK_FONT_POSTURE:
1076 nWhich = ATTR_FONT_POSTURE;
1077 break;
1081 return nWhich;
1084 //------------------------------------------------------------------------
1086 void ScGlobal::AddLanguage( SfxItemSet& rSet, SvNumberFormatter& rFormatter )
1088 OSL_ENSURE( rSet.GetItemState( ATTR_LANGUAGE_FORMAT, false ) == SFX_ITEM_DEFAULT,
1089 "ScGlobal::AddLanguage - language already added");
1091 const SfxPoolItem* pHardItem;
1092 if ( rSet.GetItemState( ATTR_VALUE_FORMAT, false, &pHardItem ) == SFX_ITEM_SET )
1094 const SvNumberformat* pHardFormat = rFormatter.GetEntry(
1095 ((const SfxUInt32Item*)pHardItem)->GetValue() );
1097 sal_uLong nParentFmt = 0; // pool default
1098 const SfxItemSet* pParent = rSet.GetParent();
1099 if ( pParent )
1100 nParentFmt = ((const SfxUInt32Item&)pParent->Get( ATTR_VALUE_FORMAT )).GetValue();
1101 const SvNumberformat* pParFormat = rFormatter.GetEntry( nParentFmt );
1103 if ( pHardFormat && pParFormat &&
1104 (pHardFormat->GetLanguage() != pParFormat->GetLanguage()) )
1105 rSet.Put( SvxLanguageItem( pHardFormat->GetLanguage(), ATTR_LANGUAGE_FORMAT ) );
1110 //--------------------------------------------------------------------
1114 //------------------------------------------------------------------------
1116 utl::TransliterationWrapper* ScGlobal::GetpTransliteration()
1118 if ( !pTransliteration )
1120 const LanguageType eOfficeLanguage = Application::GetSettings().GetLanguageTag().getLanguageType();
1121 pTransliteration = new ::utl::TransliterationWrapper(
1122 ::comphelper::getProcessComponentContext(), SC_TRANSLITERATION_IGNORECASE );
1123 pTransliteration->loadModuleIfNeeded( eOfficeLanguage );
1125 OSL_ENSURE(
1126 pTransliteration,
1127 "ScGlobal::GetpTransliteration() called before ScGlobal::Init()");
1128 return pTransliteration;
1131 const LocaleDataWrapper* ScGlobal::GetpLocaleData()
1133 OSL_ENSURE(
1134 pLocaleData,
1135 "ScGlobal::GetpLocaleData() called before ScGlobal::Init()");
1136 return pLocaleData;
1138 CalendarWrapper* ScGlobal::GetCalendar()
1140 if ( !pCalendar )
1142 pCalendar = new CalendarWrapper( ::comphelper::getProcessComponentContext() );
1143 pCalendar->loadDefaultCalendar( *GetLocale() );
1145 return pCalendar;
1147 CollatorWrapper* ScGlobal::GetCollator()
1149 if ( !pCollator )
1151 pCollator = new CollatorWrapper( ::comphelper::getProcessComponentContext() );
1152 pCollator->loadDefaultCollator( *GetLocale(), SC_COLLATOR_IGNORES );
1154 return pCollator;
1156 CollatorWrapper* ScGlobal::GetCaseCollator()
1158 if ( !pCaseCollator )
1160 pCaseCollator = new CollatorWrapper( ::comphelper::getProcessComponentContext() );
1161 pCaseCollator->loadDefaultCollator( *GetLocale(), 0 );
1163 return pCaseCollator;
1165 ::utl::TransliterationWrapper* ScGlobal::GetCaseTransliteration()
1167 if ( !pCaseTransliteration )
1169 const LanguageType eOfficeLanguage = Application::GetSettings().GetLanguageTag().getLanguageType();
1170 pCaseTransliteration = new ::utl::TransliterationWrapper(::comphelper::getProcessComponentContext(), SC_TRANSLITERATION_CASESENSE );
1171 pCaseTransliteration->loadModuleIfNeeded( eOfficeLanguage );
1173 return pCaseTransliteration;
1175 IntlWrapper* ScGlobal::GetScIntlWrapper()
1177 if ( !pScIntlWrapper )
1179 pScIntlWrapper = new IntlWrapper( LanguageTag( *GetLocale()) );
1181 return pScIntlWrapper;
1183 ::com::sun::star::lang::Locale* ScGlobal::GetLocale()
1185 if ( !pLocale )
1187 pLocale = new ::com::sun::star::lang::Locale( Application::GetSettings().GetLanguageTag().getLocale());
1189 return pLocale;
1192 ScFieldEditEngine& ScGlobal::GetStaticFieldEditEngine()
1194 if (!pFieldEditEngine)
1196 // Creating a ScFieldEditEngine with pDocument=NULL leads to document
1197 // specific fields not being resolvable! See
1198 // ScFieldEditEngine::CalcFieldValue(). pEnginePool=NULL lets
1199 // EditEngine internally create and delete a default pool.
1200 pFieldEditEngine = new ScFieldEditEngine( NULL, NULL);
1202 return *pFieldEditEngine;
1205 OUString ScGlobal::ReplaceOrAppend( const OUString& rString,
1206 const OUString& rPlaceholder, const OUString& rReplacement )
1208 if (rString.isEmpty())
1209 return rReplacement;
1210 sal_Int32 nFound = rString.indexOf( rPlaceholder);
1211 if (nFound < 0)
1213 if (rString[rString.getLength()-1] == ' ')
1214 return rString + rReplacement;
1215 return rString + " " + rReplacement;
1217 return rString.replaceFirst( rPlaceholder, rReplacement, &nFound);
1220 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */