Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / sc / source / core / data / clipparam.cxx
blob271685d473b6ed22182eed43779b768c06b6b935
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 <clipparam.hxx>
23 ScClipParam::ScClipParam() :
24 meDirection(Unspecified),
25 mbCutMode(false),
26 mnSourceDocID(0)
30 ScClipParam::ScClipParam(const ScRange& rRange, bool bCutMode) :
31 meDirection(Unspecified),
32 mbCutMode(bCutMode),
33 mnSourceDocID(0)
35 maRanges.push_back(rRange);
38 bool ScClipParam::isMultiRange() const
40 return maRanges.size() > 1;
43 SCCOL ScClipParam::getPasteColSize()
45 if (maRanges.empty())
46 return 0;
48 switch (meDirection)
50 case ScClipParam::Column:
52 SCCOL nColSize = 0;
53 for ( size_t i = 0, nListSize = maRanges.size(); i < nListSize; ++i )
55 const ScRange& rRange = maRanges[ i ];
56 nColSize += rRange.aEnd.Col() - rRange.aStart.Col() + 1;
58 return nColSize;
60 case ScClipParam::Row:
62 // We assume that all ranges have identical column size.
63 const ScRange& rRange = maRanges.front();
64 return rRange.aEnd.Col() - rRange.aStart.Col() + 1;
66 case ScClipParam::Unspecified:
67 default:
70 return 0;
73 SCROW ScClipParam::getPasteRowSize(const ScDocument& rSrcDoc, bool bIncludeFiltered)
75 if (maRanges.empty())
76 return 0;
78 switch (meDirection)
80 case ScClipParam::Column:
82 // We assume that all ranges have identical row size.
83 const ScRange& rRange = maRanges.front();
84 return bIncludeFiltered
85 ? rRange.aEnd.Row() - rRange.aStart.Row() + 1
86 : rSrcDoc.CountNonFilteredRows(rRange.aStart.Row(), rRange.aEnd.Row(),
87 rRange.aStart.Tab());
89 case ScClipParam::Row:
91 SCROW nRowSize = 0;
92 for ( size_t i = 0, nListSize = maRanges.size(); i < nListSize; ++i )
94 const ScRange& rRange = maRanges[ i ];
95 nRowSize += bIncludeFiltered ? rRange.aEnd.Row() - rRange.aStart.Row() + 1
96 : rSrcDoc.CountNonFilteredRows(rRange.aStart.Row(),
97 rRange.aEnd.Row(),
98 rRange.aStart.Tab());
100 return nRowSize;
102 case ScClipParam::Unspecified:
103 default:
106 return 0;
109 ScRange ScClipParam::getWholeRange() const
111 return maRanges.Combine();
114 void ScClipParam::transpose(const ScDocument& rSrcDoc, bool bIncludeFiltered,
115 bool bIsMultiRangeRowFilteredTranspose)
117 mbTransposed = true;
119 switch (meDirection)
121 case Column:
122 meDirection = ScClipParam::Row;
123 break;
124 case Row:
125 meDirection = ScClipParam::Column;
126 break;
127 case Unspecified:
128 default:
132 ScRangeList aNewRanges;
133 if (!maRanges.empty())
135 const ScRange & rRange1 = maRanges.front();
136 SCCOL nColOrigin = rRange1.aStart.Col();
137 SCROW nRowOrigin = rRange1.aStart.Row();
138 SCROW nRowCount = 0;
139 for ( size_t i = 0, n = maRanges.size(); i < n; ++i )
141 const ScRange & rRange = maRanges[ i ];
142 SCCOL nColDelta = rRange.aStart.Col() - nColOrigin;
143 SCROW nRowDelta = rRange.aStart.Row() - nRowOrigin;
144 SCROW nNonFilteredRows = rSrcDoc.CountNonFilteredRows(
145 rRange.aStart.Row(), rRange.aEnd.Row(), rRange.aStart.Tab());
146 if (!bIsMultiRangeRowFilteredTranspose)
148 SCCOL nCol1 = 0;
149 SCCOL nCol2 = bIncludeFiltered
150 ? static_cast<SCCOL>(rRange.aEnd.Row() - rRange.aStart.Row())
151 : nNonFilteredRows - 1;
152 SCROW nRow1 = 0;
153 SCROW nRow2 = static_cast<SCROW>(rRange.aEnd.Col() - rRange.aStart.Col());
154 nCol1 += static_cast<SCCOL>(nRowDelta);
155 nCol2 += static_cast<SCCOL>(nRowDelta);
156 nRow1 += static_cast<SCROW>(nColDelta);
157 nRow2 += static_cast<SCROW>(nColDelta);
158 aNewRanges.push_back(ScRange(nColOrigin + nCol1, nRowOrigin + nRow1,
159 rRange.aStart.Tab(), nColOrigin + nCol2,
160 nRowOrigin + nRow2, rRange.aStart.Tab()));
162 else
163 nRowCount += nNonFilteredRows;
166 // Transpose of filtered multi range row selection is a special case since filtering
167 // and selection are in the same dimension (i.e. row), see ScDocument::TransposeClip()
168 if (bIsMultiRangeRowFilteredTranspose)
170 assert(!bIncludeFiltered && "bIsMultiRangeRowFilteredTranspose can only be true if bIncludeFiltered is false");
171 SCCOL nColDelta = rRange1.aStart.Col() - nColOrigin;
172 SCROW nRowDelta = rRange1.aStart.Row() - nRowOrigin;
173 SCCOL nCol1 = 0;
174 SCCOL nCol2 = nRowCount - 1;
175 SCROW nRow1 = 0;
176 SCROW nRow2 = static_cast<SCROW>(rRange1.aEnd.Col() - rRange1.aStart.Col());
177 nCol1 += static_cast<SCCOL>(nRowDelta);
178 nCol2 += static_cast<SCCOL>(nRowDelta);
179 nRow1 += static_cast<SCROW>(nColDelta);
180 nRow2 += static_cast<SCROW>(nColDelta);
181 aNewRanges.push_back(ScRange(nColOrigin + nCol1, nRowOrigin + nRow1,
182 rRange1.aStart.Tab(), nColOrigin + nCol2,
183 nRowOrigin + nRow2, rRange1.aStart.Tab()));
186 maRanges = std::move(aNewRanges);
189 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */