Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / data / documen6.cxx
blobb9b8d5f4dbe9984db64165003d84e9844e186202
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 "scitems.hxx"
21 #include <editeng/scripttypeitem.hxx>
23 #include <com/sun/star/i18n/BreakIterator.hpp>
24 #include <com/sun/star/i18n/ScriptType.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <comphelper/processfactory.hxx>
28 #include "document.hxx"
29 #include "cellform.hxx"
30 #include "patattr.hxx"
31 #include "scrdata.hxx"
32 #include "poolhelp.hxx"
33 #include "attrib.hxx"
34 #include "globalnames.hxx"
35 #include "columnspanset.hxx"
36 #include "table.hxx"
38 using namespace com::sun::star;
41 // this file is compiled with exceptions enabled
42 // put functions here that need exceptions!
45 // -----------------------------------------------------------------------
47 const uno::Reference< i18n::XBreakIterator >& ScDocument::GetBreakIterator()
49 if ( !pScriptTypeData )
50 pScriptTypeData = new ScScriptTypeData;
51 if ( !pScriptTypeData->xBreakIter.is() )
53 pScriptTypeData->xBreakIter = i18n::BreakIterator::create( comphelper::getProcessComponentContext() );
55 return pScriptTypeData->xBreakIter;
58 bool ScDocument::HasStringWeakCharacters( const OUString& rString )
60 if (!rString.isEmpty())
62 uno::Reference<i18n::XBreakIterator> xBreakIter = GetBreakIterator();
63 if ( xBreakIter.is() )
65 sal_Int32 nLen = rString.getLength();
67 sal_Int32 nPos = 0;
70 sal_Int16 nType = xBreakIter->getScriptType( rString, nPos );
71 if ( nType == i18n::ScriptType::WEAK )
72 return true; // found
74 nPos = xBreakIter->endOfScript( rString, nPos, nType );
76 while ( nPos >= 0 && nPos < nLen );
80 return false; // none found
83 sal_uInt8 ScDocument::GetStringScriptType( const OUString& rString )
86 sal_uInt8 nRet = 0;
87 if (!rString.isEmpty())
89 uno::Reference<i18n::XBreakIterator> xBreakIter = GetBreakIterator();
90 if ( xBreakIter.is() )
92 sal_Int32 nLen = rString.getLength();
94 sal_Int32 nPos = 0;
97 sal_Int16 nType = xBreakIter->getScriptType( rString, nPos );
98 switch ( nType )
100 case i18n::ScriptType::LATIN:
101 nRet |= SCRIPTTYPE_LATIN;
102 break;
103 case i18n::ScriptType::ASIAN:
104 nRet |= SCRIPTTYPE_ASIAN;
105 break;
106 case i18n::ScriptType::COMPLEX:
107 nRet |= SCRIPTTYPE_COMPLEX;
108 break;
109 // WEAK is ignored
111 nPos = xBreakIter->endOfScript( rString, nPos, nType );
113 while ( nPos >= 0 && nPos < nLen );
116 return nRet;
119 sal_uInt8 ScDocument::GetCellScriptType( const ScAddress& rPos, sal_uLong nNumberFormat )
121 sal_uInt8 nStored = GetScriptType(rPos);
122 if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
123 return nStored; // use stored value
125 Color* pColor;
126 OUString aStr = ScCellFormat::GetString(*this, rPos, nNumberFormat, &pColor, *xPoolHelper->GetFormTable());
128 sal_uInt8 nRet = GetStringScriptType( aStr );
130 SetScriptType(rPos, nRet); // store for later calls
132 return nRet;
135 sal_uInt8 ScDocument::GetScriptType( SCCOL nCol, SCROW nRow, SCTAB nTab )
137 // if script type is set, don't have to get number formats
139 ScAddress aPos(nCol, nRow, nTab);
140 sal_uInt8 nStored = GetScriptType(aPos);
141 if ( nStored != SC_SCRIPTTYPE_UNKNOWN ) // stored value valid?
142 return nStored; // use stored value
144 // include number formats from conditional formatting
146 const ScPatternAttr* pPattern = GetPattern( nCol, nRow, nTab );
147 if (!pPattern) return 0;
148 const SfxItemSet* pCondSet = NULL;
149 if ( !((const ScCondFormatItem&)pPattern->GetItem(ATTR_CONDITIONAL)).GetCondFormatData().empty() )
150 pCondSet = GetCondResult( nCol, nRow, nTab );
152 sal_uLong nFormat = pPattern->GetNumberFormat( xPoolHelper->GetFormTable(), pCondSet );
154 return GetCellScriptType(aPos, nFormat);
157 namespace {
159 class ScriptTypeAggregator : public sc::ColumnSpanSet::Action
161 ScDocument& mrDoc;
162 sc::ColumnBlockPosition maBlockPos;
163 sal_uInt8 mnScriptType;
165 public:
166 ScriptTypeAggregator(ScDocument& rDoc) : mrDoc(rDoc), mnScriptType(0) {}
168 virtual void startColumn(SCTAB nTab, SCCOL nCol)
170 mrDoc.InitColumnBlockPosition(maBlockPos, nTab, nCol);
173 virtual void execute(const ScAddress& rPos, SCROW nLength, bool bVal)
175 if (!bVal)
176 return;
178 mnScriptType |= mrDoc.GetRangeScriptType(maBlockPos, rPos, nLength);
181 sal_uInt8 getScriptType() const { return mnScriptType; }
186 sal_uInt8 ScDocument::GetRangeScriptType(
187 sc::ColumnBlockPosition& rBlockPos, const ScAddress& rPos, SCROW nLength )
189 if (!TableExists(rPos.Tab()))
190 return 0;
192 return maTabs[rPos.Tab()]->GetRangeScriptType(rBlockPos, rPos.Col(), rPos.Row(), rPos.Row()+nLength-1);
195 sal_uInt8 ScDocument::GetRangeScriptType( const ScRangeList& rRanges )
197 sc::ColumnSpanSet aSet(false);
198 for (size_t i = 0, n = rRanges.size(); i < n; ++i)
200 const ScRange& rRange = *rRanges[i];
201 SCTAB nTab = rRange.aStart.Tab();
202 SCROW nRow1 = rRange.aStart.Row();
203 SCROW nRow2 = rRange.aEnd.Row();
204 for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
205 aSet.set(nTab, nCol, nRow1, nRow2, true);
208 ScriptTypeAggregator aAction(*this);
209 aSet.executeAction(aAction);
210 return aAction.getScriptType();
213 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */