1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #include <refupdatecontext.hxx>
11 #include <algorithm>
12 #include <clipparam.hxx>
13 #include <mtvelements.hxx>
14 
15 namespace sc {
16 
setUpdatedName(SCTAB nTab,sal_uInt16 nIndex)17 void UpdatedRangeNames::setUpdatedName(SCTAB nTab, sal_uInt16 nIndex)
18 {
19     // Map anything <-1 to global names. Unless we really want to come up with
20     // some classification there...
21     if (nTab < -1)
22         nTab = -1;
23 
24     UpdatedNamesType::iterator it = maUpdatedNames.find(nTab);
25     if (it == maUpdatedNames.end())
26     {
27         // Insert a new container for this sheet index.
28         NameIndicesType aIndices;
29         std::pair<UpdatedNamesType::iterator,bool> r =
30             maUpdatedNames.emplace( nTab, aIndices);
31 
32         if (!r.second)
33             // Insertion failed for whatever reason.
34             return;
35 
36         it = r.first;
37     }
38 
39     NameIndicesType& rIndices = it->second;
40     rIndices.insert(nIndex);
41 }
42 
isNameUpdated(SCTAB nTab,sal_uInt16 nIndex) const43 bool UpdatedRangeNames::isNameUpdated(SCTAB nTab, sal_uInt16 nIndex) const
44 {
45     UpdatedNamesType::const_iterator it = maUpdatedNames.find(nTab);
46     if (it == maUpdatedNames.end())
47         return false;
48 
49     const NameIndicesType& rIndices = it->second;
50     return rIndices.count(nIndex) > 0;
51 }
52 
getUpdatedNames(SCTAB nTab) const53 UpdatedRangeNames::NameIndicesType UpdatedRangeNames::getUpdatedNames(SCTAB nTab) const
54 {
55     UpdatedNamesType::const_iterator it = maUpdatedNames.find(nTab);
56     if (it == maUpdatedNames.end())
57         return NameIndicesType();
58     return it->second;
59 }
60 
isEmpty(SCTAB nTab) const61 bool UpdatedRangeNames::isEmpty(SCTAB nTab) const
62 {
63     UpdatedNamesType::const_iterator it = maUpdatedNames.find(nTab);
64     return it == maUpdatedNames.end();
65 }
66 
RefUpdateContext(ScDocument & rDoc,ScDocument * pClipdoc)67 RefUpdateContext::RefUpdateContext(ScDocument& rDoc, ScDocument* pClipdoc)
68     : mrDoc(rDoc)
69     , meMode(URM_INSDEL)
70     , mbTransposed(pClipdoc != nullptr && pClipdoc->GetClipParam().isTransposed())
71     , mnColDelta(0)
72     , mnRowDelta(0)
73     , mnTabDelta(0)
74     , mpBlockPos(nullptr)
75 {
76     assert((pClipdoc == nullptr || pClipdoc->IsClipboard()) && "only nullptr or clipdoc allowed");
77 }
78 
isInserted() const79 bool RefUpdateContext::isInserted() const
80 {
81     return (meMode == URM_INSDEL) && (mnColDelta > 0 || mnRowDelta > 0 || mnTabDelta > 0);
82 }
83 
isDeleted() const84 bool RefUpdateContext::isDeleted() const
85 {
86     return (meMode == URM_INSDEL) && (mnColDelta < 0 || mnRowDelta < 0 || mnTabDelta < 0);
87 }
88 
setBlockPositionReference(ColumnBlockPositionSet * blockPos)89 void RefUpdateContext::setBlockPositionReference( ColumnBlockPositionSet* blockPos )
90 {
91     mpBlockPos = blockPos;
92 }
93 
getBlockPosition(SCTAB nTab,SCCOL nCol)94 ColumnBlockPosition* RefUpdateContext::getBlockPosition(SCTAB nTab, SCCOL nCol)
95 {
96     return mpBlockPos ? mpBlockPos->getBlockPosition(nTab, nCol) : nullptr;
97 }
98 
RefUpdateResult()99 RefUpdateResult::RefUpdateResult() : mbValueChanged(false), mbReferenceModified(false), mbNameModified(false) {}
100 
RefUpdateInsertTabContext(ScDocument & rDoc,SCTAB nInsertPos,SCTAB nSheets)101 RefUpdateInsertTabContext::RefUpdateInsertTabContext(ScDocument& rDoc, SCTAB nInsertPos, SCTAB nSheets) :
102     mrDoc(rDoc), mnInsertPos(nInsertPos), mnSheets(nSheets) {}
103 
RefUpdateDeleteTabContext(ScDocument & rDoc,SCTAB nDeletePos,SCTAB nSheets)104 RefUpdateDeleteTabContext::RefUpdateDeleteTabContext(ScDocument& rDoc, SCTAB nDeletePos, SCTAB nSheets) :
105     mrDoc(rDoc), mnDeletePos(nDeletePos), mnSheets(nSheets) {}
106 
RefUpdateMoveTabContext(ScDocument & rDoc,SCTAB nOldPos,SCTAB nNewPos)107 RefUpdateMoveTabContext::RefUpdateMoveTabContext(ScDocument& rDoc, SCTAB nOldPos, SCTAB nNewPos) :
108     mrDoc(rDoc), mnOldPos(nOldPos), mnNewPos(nNewPos) {}
109 
getNewTab(SCTAB nOldTab) const110 SCTAB RefUpdateMoveTabContext::getNewTab(SCTAB nOldTab) const
111 {
112     // Sheets below the lower bound or above the upper bound will not change.
113     SCTAB nLowerBound = std::min(mnOldPos, mnNewPos);
114     SCTAB nUpperBound = std::max(mnOldPos, mnNewPos);
115 
116     if (nOldTab < nLowerBound || nUpperBound < nOldTab)
117         // Outside the boundary. Nothing to adjust.
118         return nOldTab;
119 
120     if (nOldTab == mnOldPos)
121         return mnNewPos;
122 
123     // It's somewhere in between.
124     if (mnOldPos < mnNewPos)
125     {
126         // Moving a sheet to the right. The rest of the sheets shifts to the left.
127         return nOldTab - 1;
128     }
129 
130     // Moving a sheet to the left. The rest of the sheets shifts to the right.
131     return nOldTab + 1;
132 }
133 
SetFormulaDirtyContext()134 SetFormulaDirtyContext::SetFormulaDirtyContext() :
135     mnTabDeletedStart(-1), mnTabDeletedEnd(-1), mbClearTabDeletedFlag(false) {}
136 
137 }
138 
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
140