Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / core / data / documentstreamaccess.cxx
blobbf3efe33f4a640af627f0897da878485df55945f
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/.
8 */
10 #include "documentstreamaccess.hxx"
11 #include "document.hxx"
12 #include "table.hxx"
13 #include "column.hxx"
14 #include "mtvelements.hxx"
16 #include "svl/sharedstringpool.hxx"
18 namespace sc {
20 struct DocumentStreamAccessImpl
22 ScDocument& mrDoc;
23 ColumnBlockPositionSet maBlockPosSet;
25 DocumentStreamAccessImpl( ScDocument& rDoc ) :
26 mrDoc(rDoc),
27 maBlockPosSet(rDoc)
31 DocumentStreamAccess::DocumentStreamAccess( ScDocument& rDoc ) :
32 mpImpl(new DocumentStreamAccessImpl(rDoc)) {}
34 void DocumentStreamAccess::setNumericCell( const ScAddress& rPos, double fVal )
36 ScTable* pTab = mpImpl->mrDoc.FetchTable(rPos.Tab());
37 if (!pTab)
38 return;
40 ColumnBlockPosition* pBlockPos =
41 mpImpl->maBlockPosSet.getBlockPosition(rPos.Tab(), rPos.Col());
43 if (!pBlockPos)
44 return;
46 // Set the numeric value.
47 CellStoreType& rCells = pTab->aCol[rPos.Col()].maCells;
48 pBlockPos->miCellPos = rCells.set(pBlockPos->miCellPos, rPos.Row(), fVal);
50 // Be sure to set the corresponding text attribute to the default value.
51 CellTextAttrStoreType& rAttrs = pTab->aCol[rPos.Col()].maCellTextAttrs;
52 pBlockPos->miCellTextAttrPos = rAttrs.set(pBlockPos->miCellTextAttrPos, rPos.Row(), CellTextAttr());
55 void DocumentStreamAccess::setStringCell( const ScAddress& rPos, const OUString& rStr )
57 ScTable* pTab = mpImpl->mrDoc.FetchTable(rPos.Tab());
58 if (!pTab)
59 return;
61 ColumnBlockPosition* pBlockPos =
62 mpImpl->maBlockPosSet.getBlockPosition(rPos.Tab(), rPos.Col());
64 if (!pBlockPos)
65 return;
67 svl::SharedString aSS = mpImpl->mrDoc.GetSharedStringPool().intern(rStr);
68 if (!aSS.getData())
69 return;
71 // Set the string.
72 CellStoreType& rCells = pTab->aCol[rPos.Col()].maCells;
73 pBlockPos->miCellPos = rCells.set(pBlockPos->miCellPos, rPos.Row(), aSS);
75 // Be sure to set the corresponding text attribute to the default value.
76 CellTextAttrStoreType& rAttrs = pTab->aCol[rPos.Col()].maCellTextAttrs;
77 pBlockPos->miCellTextAttrPos = rAttrs.set(pBlockPos->miCellTextAttrPos, rPos.Row(), CellTextAttr());
80 void DocumentStreamAccess::reset()
82 mpImpl->maBlockPosSet.clear();
85 void DocumentStreamAccess::shiftRangeUp( const ScRange& rRange )
87 ScTable* pTab = mpImpl->mrDoc.FetchTable(rRange.aStart.Tab());
88 if (!pTab)
89 return;
91 SCROW nTopRow = rRange.aStart.Row();
92 SCROW nLastRow = rRange.aEnd.Row();
94 for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
96 ColumnBlockPosition* pBlockPos =
97 mpImpl->maBlockPosSet.getBlockPosition(rRange.aStart.Tab(), nCol);
99 if (!pBlockPos)
100 return;
102 CellStoreType& rCells = pTab->aCol[nCol].maCells;
103 rCells.erase(nTopRow, nTopRow); // Erase the top, and shift the rest up.
104 pBlockPos->miCellPos = rCells.insert_empty(nLastRow, 1);
106 // Do the same for the text attribute storage.
107 CellTextAttrStoreType& rAttrs = pTab->aCol[nCol].maCellTextAttrs;
108 rAttrs.erase(nTopRow, nTopRow);
109 pBlockPos->miCellTextAttrPos = rAttrs.insert_empty(nLastRow, 1);
113 void DocumentStreamAccess::shiftRangeDown( const ScRange& rRange )
115 ScTable* pTab = mpImpl->mrDoc.FetchTable(rRange.aStart.Tab());
116 if (!pTab)
117 return;
119 SCROW nTopRow = rRange.aStart.Row();
120 SCROW nLastRow = rRange.aEnd.Row();
122 for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
124 ColumnBlockPosition* pBlockPos =
125 mpImpl->maBlockPosSet.getBlockPosition(rRange.aStart.Tab(), nCol);
127 if (!pBlockPos)
128 return;
130 CellStoreType& rCells = pTab->aCol[nCol].maCells;
131 rCells.erase(nLastRow, nLastRow); // Erase the bottom.
132 pBlockPos->miCellPos = rCells.insert_empty(nTopRow, 1); // insert at the top and shift everything down.
134 // Do the same for the text attribute storage.
135 CellTextAttrStoreType& rAttrs = pTab->aCol[nCol].maCellTextAttrs;
136 rAttrs.erase(nLastRow, nLastRow);
137 pBlockPos->miCellTextAttrPos = rAttrs.insert_empty(nTopRow, 1);
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */