1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <boost/scoped_ptr.hpp>
13 #include <editeng/editobj.hxx>
14 #include <editeng/wghtitem.hxx>
15 #include <editeng/eeitem.hxx>
17 #include "editutil.hxx"
19 #include "TableFillingAndNavigationTools.hxx"
21 FormulaTemplate::FormulaTemplate(ScDocument
* pDoc
) : mpDoc(pDoc
) {}
23 void FormulaTemplate::setTemplate(const OUString
& aTemplate
)
25 mTemplate
= aTemplate
;
28 void FormulaTemplate::setTemplate(const char* aTemplate
)
30 mTemplate
= OUString::createFromAscii(aTemplate
);
33 const OUString
& FormulaTemplate::getTemplate()
35 RangeReplacementMap::iterator itRange
;
36 for (itRange
= mRangeReplacementMap
.begin(); itRange
!= mRangeReplacementMap
.end(); ++itRange
)
38 applyRange(itRange
->first
, itRange
->second
);
40 AddressReplacementMap::iterator itAddress
;
41 for (itAddress
= mAddressReplacementMap
.begin(); itAddress
!= mAddressReplacementMap
.end(); ++itAddress
)
43 applyAddress(itAddress
->first
, itAddress
->second
);
48 void FormulaTemplate::autoReplaceRange(const OUString
& aVariable
, const ScRange
& rRange
)
50 mRangeReplacementMap
.insert ( std::pair
<OUString
, ScRange
>(aVariable
, rRange
) );
53 void FormulaTemplate::autoReplaceAddress(const OUString
& aVariable
, ScAddress aAddress
)
55 mAddressReplacementMap
.insert ( std::pair
<OUString
, ScAddress
>(aVariable
, aAddress
) );
58 void FormulaTemplate::applyRange(const OUString
& aVariable
, const ScRange
& aRange
, bool b3D
)
60 sal_uInt16 nFlag
= b3D
? SCR_ABS_3D
: SCR_ABS
;
61 OUString aString
= aRange
.Format(nFlag
, mpDoc
, mpDoc
->GetAddressConvention());
62 mTemplate
= mTemplate
.replaceAll(aVariable
, aString
);
65 void FormulaTemplate::applyRangeList(const OUString
& aVariable
, const ScRangeList
& aRangeList
, bool b3D
)
67 sal_uInt16 nFlag
= b3D
? SCR_ABS_3D
: SCR_ABS
;
69 aRangeList
.Format(aString
, nFlag
, mpDoc
, mpDoc
->GetAddressConvention());
70 mTemplate
= mTemplate
.replaceAll(aVariable
, aString
);
73 void FormulaTemplate::applyAddress(const OUString
& aVariable
, const ScAddress
& aAddress
, bool b3D
)
75 sal_uInt16 nFlag
= b3D
? SCA_ABS_3D
: SCA_ABS
;
76 OUString aString
= aAddress
.Format(nFlag
, mpDoc
, mpDoc
->GetAddressConvention());
77 mTemplate
= mTemplate
.replaceAll(aVariable
, aString
);
80 void FormulaTemplate::applyString(const OUString
& aVariable
, const OUString
& aValue
)
82 mTemplate
= mTemplate
.replaceAll(aVariable
, aValue
);
85 void FormulaTemplate::applyNumber(const OUString
& aVariable
, sal_Int32 aValue
)
87 mTemplate
= mTemplate
.replaceAll(aVariable
, OUString::number(aValue
));
90 AddressWalker::AddressWalker(ScAddress aInitialAddress
, bool aTrackRange
) :
91 mCurrentAddress(aInitialAddress
),
92 mMinimumAddress(aInitialAddress
),
93 mMaximumAddress(aInitialAddress
),
94 mTrackRange(aTrackRange
)
96 mAddressStack
.push_back(mCurrentAddress
);
99 void AddressWalker::resetColumn()
101 mCurrentAddress
.SetCol(mAddressStack
.back().Col());
104 void AddressWalker::resetRow()
106 mCurrentAddress
.SetRow(mAddressStack
.back().Row());
109 void AddressWalker::reset()
111 mCurrentAddress
= mAddressStack
.back();
114 void AddressWalker::newLine()
120 ScAddress
AddressWalker::current(SCCOL aRelCol
, SCROW aRelRow
, SCTAB aRelTab
)
123 mCurrentAddress
.Col() + aRelCol
,
124 mCurrentAddress
.Row() + aRelRow
,
125 mCurrentAddress
.Tab() + aRelTab
);
128 void AddressWalker::nextColumn()
130 mCurrentAddress
.IncCol();
134 if(mMaximumAddress
.Col() < mCurrentAddress
.Col())
135 mMaximumAddress
.SetCol(mCurrentAddress
.Col());
139 void AddressWalker::nextRow()
141 mCurrentAddress
.IncRow();
144 if(mMaximumAddress
.Row() < mCurrentAddress
.Row())
145 mMaximumAddress
.SetRow(mCurrentAddress
.Row());
149 void AddressWalker::push(SCCOL aRelativeCol
, SCROW aRelativeRow
, SCTAB aRelativeTab
)
151 mCurrentAddress
= current(aRelativeCol
, aRelativeRow
, aRelativeTab
);
152 mAddressStack
.push_back(mCurrentAddress
);
155 AddressWalkerWriter::AddressWalkerWriter(ScAddress aInitialAddress
, ScDocShell
* pDocShell
, ScDocument
* pDocument
,
156 formula::FormulaGrammar::Grammar eGrammar
) :
157 AddressWalker(aInitialAddress
, true),
158 mpDocShell(pDocShell
),
159 mpDocument(pDocument
),
163 void AddressWalkerWriter::writeFormula(const OUString
& aFormula
)
165 mpDocShell
->GetDocFunc().SetFormulaCell(mCurrentAddress
,
166 new ScFormulaCell(mpDocument
, mCurrentAddress
, aFormula
, meGrammar
), true);
169 void AddressWalkerWriter::writeMatrixFormula(const OUString
& aFormula
)
172 aRange
.aStart
= mCurrentAddress
;
173 aRange
.aEnd
= mCurrentAddress
;
174 mpDocShell
->GetDocFunc().EnterMatrix(aRange
, NULL
, NULL
, aFormula
, false, false, OUString(), meGrammar
);
177 void AddressWalkerWriter::writeString(const OUString
& aString
)
179 mpDocShell
->GetDocFunc().SetStringCell(mCurrentAddress
, aString
, true);
182 void AddressWalkerWriter::writeString(const char* aCharArray
)
184 writeString(OUString::createFromAscii(aCharArray
));
187 void AddressWalkerWriter::writeBoldString(const OUString
& aString
)
189 ScFieldEditEngine
& rEngine
= mpDocument
->GetEditEngine();
190 rEngine
.SetText(aString
);
191 SfxItemSet aItemSet
= rEngine
.GetEmptyItemSet();
192 SvxWeightItem
aWeight(WEIGHT_BOLD
, EE_CHAR_WEIGHT
);
193 aItemSet
.Put(aWeight
);
194 rEngine
.QuickSetAttribs(aItemSet
, ESelection(0, 0, 0, aString
.getLength()) );
195 boost::scoped_ptr
<EditTextObject
> pEditText(rEngine
.CreateTextObject());
196 mpDocShell
->GetDocFunc().SetEditCell(mCurrentAddress
, *pEditText
, true);
199 void AddressWalkerWriter::writeValue(double aValue
)
201 mpDocShell
->GetDocFunc().SetValueCell(mCurrentAddress
, aValue
, true);
206 DataCellIterator::DataCellIterator(ScRange aInputRange
, bool aByColumn
)
207 : mInputRange(aInputRange
)
208 , mByColumn(aByColumn
)
213 mCol
= aInputRange
.aStart
.Col();
215 mRow
= aInputRange
.aStart
.Row();
218 DataCellIterator::~DataCellIterator()
221 bool DataCellIterator::hasNext()
224 return mCol
<= mInputRange
.aEnd
.Col();
226 return mRow
<= mInputRange
.aEnd
.Row();
229 void DataCellIterator::next()
237 ScAddress
DataCellIterator::get()
239 return getRelative(0);
242 ScAddress
DataCellIterator::getRelative(int aDelta
)
246 SCCOL aNewColumn
= mCol
+ aDelta
;
247 if(aNewColumn
< mInputRange
.aStart
.Col() || aNewColumn
> mInputRange
.aEnd
.Col())
250 aResult
.SetInvalid();
253 return ScAddress(aNewColumn
, mInputRange
.aStart
.Row(), mInputRange
.aStart
.Tab());
257 SCROW aNewRow
= mRow
+ aDelta
;
258 if(aNewRow
< mInputRange
.aStart
.Row() || aNewRow
> mInputRange
.aEnd
.Row())
261 aResult
.SetInvalid();
264 return ScAddress(mInputRange
.aStart
.Col(), aNewRow
, mInputRange
.aStart
.Tab());
270 DataRangeIterator::DataRangeIterator(ScRange aInputRange
) :
271 mInputRange(aInputRange
),
275 DataRangeIterator::~DataRangeIterator()
278 sal_Int32
DataRangeIterator::index()
283 // DataRangeByColumnIterator
285 DataRangeByColumnIterator::DataRangeByColumnIterator(ScRange aInputRange
) :
286 DataRangeIterator(aInputRange
),
287 mCol(aInputRange
.aStart
.Col())
290 bool DataRangeByColumnIterator::hasNext()
292 return mCol
<= mInputRange
.aEnd
.Col();
295 void DataRangeByColumnIterator::next()
301 ScRange
DataRangeByColumnIterator::get()
304 ScAddress(mCol
, mInputRange
.aStart
.Row(), mInputRange
.aStart
.Tab()),
305 ScAddress(mCol
, mInputRange
.aEnd
.Row(), mInputRange
.aEnd
.Tab())
309 void DataRangeByColumnIterator::reset()
311 mCol
= mInputRange
.aStart
.Col();
314 DataCellIterator
DataRangeByColumnIterator::iterateCells()
316 return DataCellIterator(get(), false);
319 // DataRangeByRowIterator
321 DataRangeByRowIterator::DataRangeByRowIterator(ScRange aInputRange
) :
322 DataRangeIterator(aInputRange
),
323 mRow(aInputRange
.aStart
.Row())
326 bool DataRangeByRowIterator::hasNext()
328 return mRow
<= mInputRange
.aEnd
.Row();
331 void DataRangeByRowIterator::next()
337 ScRange
DataRangeByRowIterator::get()
340 ScAddress(mInputRange
.aStart
.Col(), mRow
, mInputRange
.aStart
.Tab()),
341 ScAddress(mInputRange
.aEnd
.Col(), mRow
, mInputRange
.aEnd
.Tab())
345 void DataRangeByRowIterator::reset()
347 mRow
= mInputRange
.aStart
.Row();
350 DataCellIterator
DataRangeByRowIterator::iterateCells()
352 return DataCellIterator(get(), true);
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */