xref: /trunk/main/sc/source/core/data/clipparam.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 // INCLUDE ---------------------------------------------------------------
32 
33 #include "clipparam.hxx"
34 
35 using ::std::vector;
36 
37 ScClipParam::ScClipParam() :
38     meDirection(Unspecified),
39     mbCutMode(false),
40     mnSourceDocID(0)
41 {
42 }
43 
44 ScClipParam::ScClipParam(const ScRange& rRange, bool bCutMode) :
45     meDirection(Unspecified),
46     mbCutMode(bCutMode),
47     mnSourceDocID(0)
48 {
49     maRanges.Append(rRange);
50 }
51 
52 ScClipParam::ScClipParam(const ScClipParam& r) :
53     maRanges(r.maRanges),
54     meDirection(r.meDirection),
55     mbCutMode(r.mbCutMode),
56     mnSourceDocID(r.mnSourceDocID),
57     maProtectedChartRangesVector(r.maProtectedChartRangesVector)
58 {
59 }
60 
61 bool ScClipParam::isMultiRange() const
62 {
63     return maRanges.Count() > 1;
64 }
65 
66 SCCOL ScClipParam::getPasteColSize()
67 {
68     if (!maRanges.Count())
69         return 0;
70 
71     switch (meDirection)
72     {
73         case ScClipParam::Column:
74         {
75             SCCOL nColSize = 0;
76             for (ScRangePtr p = maRanges.First(); p; p = maRanges.Next())
77                 nColSize += p->aEnd.Col() - p->aStart.Col() + 1;
78             return nColSize;
79         }
80         case ScClipParam::Row:
81         {
82             // We assume that all ranges have identical column size.
83             const ScRange& rRange = *maRanges.First();
84             return rRange.aEnd.Col() - rRange.aStart.Col() + 1;
85         }
86         case ScClipParam::Unspecified:
87         default:
88             ;
89     }
90     return 0;
91 }
92 
93 SCROW ScClipParam::getPasteRowSize()
94 {
95     if (!maRanges.Count())
96         return 0;
97 
98     switch (meDirection)
99     {
100         case ScClipParam::Column:
101         {
102             // We assume that all ranges have identical row size.
103             const ScRange& rRange = *maRanges.First();
104             return rRange.aEnd.Row() - rRange.aStart.Row() + 1;
105         }
106         case ScClipParam::Row:
107         {
108             SCROW nRowSize = 0;
109             for (ScRangePtr p = maRanges.First(); p; p = maRanges.Next())
110                 nRowSize += p->aEnd.Row() - p->aStart.Row() + 1;
111             return nRowSize;
112         }
113         case ScClipParam::Unspecified:
114         default:
115             ;
116     }
117     return 0;
118 }
119 
120 ScRange ScClipParam::getWholeRange() const
121 {
122     ScRange aWhole;
123     bool bFirst = true;
124     ScRangeList aRanges = maRanges;
125     for (ScRange* p = aRanges.First(); p; p = aRanges.Next())
126     {
127         if (bFirst)
128         {
129             aWhole = *p;
130             bFirst = false;
131             continue;
132         }
133 
134         if (aWhole.aStart.Col() > p->aStart.Col())
135             aWhole.aStart.SetCol(p->aStart.Col());
136 
137         if (aWhole.aStart.Row() > p->aStart.Row())
138             aWhole.aStart.SetRow(p->aStart.Row());
139 
140         if (aWhole.aEnd.Col() < p->aEnd.Col())
141             aWhole.aEnd.SetCol(p->aEnd.Col());
142 
143         if (aWhole.aEnd.Row() < p->aEnd.Row())
144             aWhole.aEnd.SetRow(p->aEnd.Row());
145     }
146     return aWhole;
147 }
148 
149 void ScClipParam::transpose()
150 {
151     switch (meDirection)
152     {
153         case Column:
154             meDirection = ScClipParam::Row;
155         break;
156         case Row:
157             meDirection = ScClipParam::Column;
158         break;
159         case Unspecified:
160         default:
161             ;
162     }
163 
164     ScRangeList aNewRanges;
165     if (maRanges.Count())
166     {
167         ScRange* p = maRanges.First();
168         SCCOL nColOrigin = p->aStart.Col();
169         SCROW nRowOrigin = p->aStart.Row();
170         for (; p; p = maRanges.Next())
171         {
172             SCCOL nColDelta = p->aStart.Col() - nColOrigin;
173             SCROW nRowDelta = p->aStart.Row() - nRowOrigin;
174             SCCOL nCol1 = 0;
175             SCCOL nCol2 = static_cast<SCCOL>(p->aEnd.Row() - p->aStart.Row());
176             SCROW nRow1 = 0;
177             SCROW nRow2 = static_cast<SCROW>(p->aEnd.Col() - p->aStart.Col());
178             nCol1 += static_cast<SCCOL>(nRowDelta);
179             nCol2 += static_cast<SCCOL>(nRowDelta);
180             nRow1 += static_cast<SCROW>(nColDelta);
181             nRow2 += static_cast<SCROW>(nColDelta);
182             ScRange aNew(nCol1, nRow1, p->aStart.Tab(), nCol2, nRow2, p->aStart.Tab());
183             aNewRanges.Append(aNew);
184         }
185     }
186     maRanges = aNewRanges;
187 }
188 
189 // ============================================================================
190 
191 ScClipRangeNameData::ScClipRangeNameData() :
192     mbReplace(false)
193 {
194 }
195 
196 ScClipRangeNameData::~ScClipRangeNameData()
197 {
198 }
199 
200 void ScClipRangeNameData::insert(sal_uInt16 nOldIndex, sal_uInt16 nNewIndex)
201 {
202     maRangeMap.insert(
203         ScRangeData::IndexMap::value_type(nOldIndex, nNewIndex));
204 }
205