use insert function instead of for loop
[LibreOffice.git] / sc / source / filter / excel / exctools.cxx
blob6e9d9177785f4b5023db773f7efce02b7a2c0db8
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 <osl/diagnose.h>
21 #include <document.hxx>
22 #include <attrib.hxx>
23 #include <scextopt.hxx>
24 #include <olinetab.hxx>
26 #include <root.hxx>
27 #include <excimp8.hxx>
28 #include <namebuff.hxx>
29 #include <otlnbuff.hxx>
30 #include <formel.hxx>
31 #include <xilink.hxx>
33 #include <memory>
34 #include <vector>
36 RootData::RootData()
38 eDateiTyp = BiffX;
39 pFmlaConverter = nullptr;
41 pTabId = nullptr;
42 pUserBViewList = nullptr;
44 pIR = nullptr;
45 pER = nullptr;
46 pColRowBuff = nullptr;
49 RootData::~RootData()
51 pExtSheetBuff.reset();
52 pShrfmlaBuff.reset();
53 pExtNameBuff.reset();
54 pAutoFilterBuffer.reset();
57 XclImpOutlineBuffer::XclImpOutlineBuffer( SCSIZE nNewSize ) :
58 maLevels(0, nNewSize, 0),
59 mpOutlineArray(nullptr),
60 mnEndPos(nNewSize),
61 mnMaxLevel(0),
62 mbButtonAfter(true)
66 XclImpOutlineBuffer::~XclImpOutlineBuffer()
70 void XclImpOutlineBuffer::SetLevel( SCSIZE nIndex, sal_uInt8 nVal, bool bCollapsed )
72 maLevels.insert_back(nIndex, nIndex+1, nVal);
73 if (nVal > mnMaxLevel)
74 mnMaxLevel = nVal;
75 if (bCollapsed)
76 maCollapsedPosSet.insert(nIndex);
79 void XclImpOutlineBuffer::SetOutlineArray( ScOutlineArray* pOArray )
81 mpOutlineArray = pOArray;
84 void XclImpOutlineBuffer::MakeScOutline()
86 if (!mpOutlineArray)
87 return;
89 ::std::vector<SCSIZE> aOutlineStack;
90 aOutlineStack.reserve(mnMaxLevel);
91 for (const auto& [nPos, nLevel] : maLevels)
93 if (nPos >= mnEndPos)
95 // Don't go beyond the max allowed position.
96 OSL_ENSURE(aOutlineStack.empty(), "XclImpOutlineBuffer::MakeScOutline: outline stack not empty but expected to be.");
97 break;
99 sal_uInt8 nCurLevel = static_cast<sal_uInt8>(aOutlineStack.size());
100 if (nLevel > nCurLevel)
102 for (sal_uInt8 i = 0; i < nLevel - nCurLevel; ++i)
103 aOutlineStack.push_back(nPos);
105 else
107 OSL_ENSURE(nLevel <= nCurLevel, "XclImpOutlineBuffer::MakeScOutline: unexpected level!");
108 for (sal_uInt8 i = 0; i < nCurLevel - nLevel; ++i)
110 if (aOutlineStack.empty())
112 // Something is wrong.
113 return;
115 SCSIZE nFirstPos = aOutlineStack.back();
116 aOutlineStack.pop_back();
117 bool bCollapsed = false;
118 if (mbButtonAfter)
119 bCollapsed = maCollapsedPosSet.count(nPos) > 0;
120 else if (nFirstPos > 0)
121 bCollapsed = maCollapsedPosSet.count(nFirstPos-1) > 0;
123 bool bDummy;
124 mpOutlineArray->Insert(nFirstPos, nPos-1, bDummy, bCollapsed);
130 void XclImpOutlineBuffer::SetLevelRange( SCSIZE nF, SCSIZE nL, sal_uInt8 nVal, bool bCollapsed )
132 if (nF > nL)
133 // invalid range
134 return;
136 maLevels.insert_back(nF, nL+1, nVal);
138 if (bCollapsed)
139 maCollapsedPosSet.insert(nF);
142 void XclImpOutlineBuffer::SetButtonMode( bool bRightOrUnder )
144 mbButtonAfter = bRightOrUnder;
147 ExcScenarioCell::ExcScenarioCell( const sal_uInt16 nC, const sal_uInt16 nR )
148 : nCol( nC ), nRow( nR )
152 ExcScenario::ExcScenario( XclImpStream& rIn, const RootData& rR )
153 : nTab( rR.pIR->GetCurrScTab() )
155 sal_uInt16 nCref;
156 sal_uInt8 nName, nComment;
158 nCref = rIn.ReaduInt16();
159 nProtected = rIn.ReaduInt8();
160 rIn.Ignore( 1 ); // Hide
161 nName = rIn.ReaduInt8();
162 nComment = rIn.ReaduInt8();
163 rIn.Ignore( 1 ); // instead of nUser!
165 if( nName )
166 aName = rIn.ReadUniString( nName );
167 else
169 aName = "Scenery";
170 rIn.Ignore( 1 );
173 rIn.ReadUniString(); // username
175 if( nComment )
176 aComment = rIn.ReadUniString();
178 sal_uInt16 n = nCref;
179 sal_uInt16 nC, nR;
180 aEntries.reserve(n);
181 while( n )
183 nR = rIn.ReaduInt16();
184 nC = rIn.ReaduInt16();
186 aEntries.emplace_back( nC, nR );
188 n--;
191 for (auto& rEntry : aEntries)
192 rEntry.SetValue(rIn.ReadUniString());
195 void ExcScenario::Apply( const XclImpRoot& rRoot, const bool bLast )
197 ScDocument& r = rRoot.GetDoc();
198 OUString aSzenName( aName );
199 sal_uInt16 nNewTab = nTab + 1;
201 if( !r.InsertTab( nNewTab, aSzenName ) )
202 return;
204 r.SetScenario( nNewTab, true );
205 // do not show scenario frames
206 const ScScenarioFlags nFlags = ScScenarioFlags::CopyAll
207 | (nProtected ? ScScenarioFlags::Protected : ScScenarioFlags::NONE);
208 /* | ScScenarioFlags::ShowFrame*/
209 r.SetScenarioData( nNewTab, aComment, COL_LIGHTGRAY, nFlags);
211 for (const auto& rEntry : aEntries)
213 sal_uInt16 nCol = rEntry.nCol;
214 sal_uInt16 nRow = rEntry.nRow;
215 OUString aVal = rEntry.GetValue();
217 r.ApplyFlagsTab( nCol, nRow, nCol, nRow, nNewTab, ScMF::Scenario );
219 r.SetString( nCol, nRow, nNewTab, aVal );
222 if( bLast )
223 r.SetActiveScenario( nNewTab, true );
225 // modify what the Active tab is set to if the new
226 // scenario tab occurs before the active tab.
227 ScExtDocSettings& rDocSett = rRoot.GetExtDocOptions().GetDocSettings();
228 if( (static_cast< SCCOL >( nTab ) < rDocSett.mnDisplTab) && (rDocSett.mnDisplTab < MAXTAB) )
229 ++rDocSett.mnDisplTab;
230 rRoot.GetTabInfo().InsertScTab( nNewTab );
233 void ExcScenarioList::Apply( const XclImpRoot& rRoot )
235 sal_uInt16 n = static_cast<sal_uInt16>(aEntries.size());
237 std::vector< std::unique_ptr<ExcScenario> >::reverse_iterator iter;
238 for (iter = aEntries.rbegin(); iter != aEntries.rend(); ++iter)
240 n--;
241 (*iter)->Apply(rRoot, n == nLastScenario);
245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */