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
* aDocument
, ScAddress::Details aAddressDetails
) :
23 mAddressDetails(aAddressDetails
)
26 void FormulaTemplate::setTemplate(OUString aTemplate
)
28 mTemplate
= aTemplate
;
31 void FormulaTemplate::setTemplate(const char* aTemplate
)
33 mTemplate
= OUString::createFromAscii(aTemplate
);
36 OUString
& FormulaTemplate::getTemplate()
41 void FormulaTemplate::applyRange(OUString aVariable
, ScRange aRange
)
43 OUString aString
= aRange
.Format(SCR_ABS
, mDocument
, mAddressDetails
);
44 mTemplate
= mTemplate
.replaceAll(aVariable
, aString
);
47 void FormulaTemplate::applyRangeList(OUString aVariable
, ScRangeList aRangeList
)
50 aRangeList
.Format(aString
, SCR_ABS
, mDocument
);
51 mTemplate
= mTemplate
.replaceAll(aVariable
, aString
);
54 void FormulaTemplate::applyAddress(OUString aVariable
, ScAddress aAddress
)
56 OUString aString
= aAddress
.Format(SCR_ABS
, mDocument
, mAddressDetails
);
57 mTemplate
= mTemplate
.replaceAll(aVariable
, aString
);
60 void FormulaTemplate::applyString(OUString aVariable
, OUString aValue
)
62 mTemplate
= mTemplate
.replaceAll(aVariable
, aValue
);
65 void FormulaTemplate::applyNumber(OUString aVariable
, sal_Int32 aValue
)
67 mTemplate
= mTemplate
.replaceAll(aVariable
, OUString::number(aValue
));
70 AddressWalker::AddressWalker(ScAddress aInitialAddress
, bool aTrackRange
) :
71 mCurrentAddress(aInitialAddress
),
72 mMinimumAddress(aInitialAddress
),
73 mMaximumAddress(aInitialAddress
),
74 mTrackRange(aTrackRange
)
76 mAddressStack
.push_back(mCurrentAddress
);
79 void AddressWalker::resetColumn()
81 mCurrentAddress
.SetCol(mAddressStack
.back().Col());
84 void AddressWalker::resetRow()
86 mCurrentAddress
.SetRow(mAddressStack
.back().Row());
89 void AddressWalker::reset()
91 mCurrentAddress
= mAddressStack
.back();
94 ScAddress
AddressWalker::current(SCCOL aRelCol
, SCROW aRelRow
, SCTAB aRelTab
)
97 mCurrentAddress
.Col() + aRelCol
,
98 mCurrentAddress
.Row() + aRelRow
,
99 mCurrentAddress
.Tab() + aRelTab
);
102 void AddressWalker::nextColumn()
104 mCurrentAddress
.IncCol();
108 if(mMaximumAddress
.Col() < mCurrentAddress
.Col())
109 mMaximumAddress
.SetCol(mCurrentAddress
.Col());
113 void AddressWalker::nextRow()
115 mCurrentAddress
.IncRow();
118 if(mMaximumAddress
.Row() < mCurrentAddress
.Row())
119 mMaximumAddress
.SetRow(mCurrentAddress
.Row());
123 void AddressWalker::push(SCCOL aRelativeCol
, SCROW aRelativeRow
, SCTAB aRelativeTab
)
125 mCurrentAddress
= current(aRelativeCol
, aRelativeRow
, aRelativeTab
);
126 mAddressStack
.push_back(mCurrentAddress
);
129 void AddressWalker::pop()
131 mCurrentAddress
= mAddressStack
.back();
132 mAddressStack
.pop_back();
135 AddressWalkerWriter::AddressWalkerWriter(ScAddress aInitialAddress
, ScDocShell
* pDocShell
, ScDocument
* pDocument
,
136 formula::FormulaGrammar::Grammar eGrammar
) :
137 AddressWalker(aInitialAddress
, true),
138 mpDocShell(pDocShell
),
139 mpDocument(pDocument
),
143 void AddressWalkerWriter::writeFormula(OUString aFormula
)
145 mpDocShell
->GetDocFunc().SetFormulaCell(mCurrentAddress
,
146 new ScFormulaCell(mpDocument
, mCurrentAddress
, aFormula
, meGrammar
), true);
149 void AddressWalkerWriter::writeString(OUString aString
)
151 mpDocShell
->GetDocFunc().SetStringCell(mCurrentAddress
, aString
, true);
154 void AddressWalkerWriter::writeString(const char* aCharArray
)
156 writeString(OUString::createFromAscii(aCharArray
));
159 void AddressWalkerWriter::writeBoldString(OUString aString
)
161 ScFieldEditEngine
& rEngine
= mpDocument
->GetEditEngine();
162 rEngine
.SetText(aString
);
163 SfxItemSet aItemSet
= rEngine
.GetEmptyItemSet();
164 SvxWeightItem
aWeight(WEIGHT_BOLD
, EE_CHAR_WEIGHT
);
165 aItemSet
.Put(aWeight
);
166 rEngine
.QuickSetAttribs(aItemSet
, ESelection(0, 0, 0, aString
.getLength()) );
167 boost::scoped_ptr
<EditTextObject
> pEditText(rEngine
.CreateTextObject());
168 mpDocShell
->GetDocFunc().SetEditCell(mCurrentAddress
, *pEditText
, true);
171 void AddressWalkerWriter::writeValue(double aValue
)
173 mpDocShell
->GetDocFunc().SetValueCell(mCurrentAddress
, aValue
, true);
178 DataCellIterator::DataCellIterator(ScRange aInputRange
, bool aByColumn
) :
179 mInputRange(aInputRange
),
183 mCol
= aInputRange
.aStart
.Col();
185 mRow
= aInputRange
.aStart
.Row();
188 DataCellIterator::~DataCellIterator()
191 bool DataCellIterator::hasNext()
194 return mCol
<= mInputRange
.aEnd
.Col();
196 return mRow
<= mInputRange
.aEnd
.Row();
199 void DataCellIterator::next()
207 ScAddress
DataCellIterator::get()
209 return getRelative(0);
212 ScAddress
DataCellIterator::getRelative(int aDelta
)
216 SCCOL aNewColumn
= mCol
+ aDelta
;
217 if(aNewColumn
< mInputRange
.aStart
.Col() || aNewColumn
> mInputRange
.aEnd
.Col())
220 aResult
.SetInvalid();
223 return ScAddress(aNewColumn
, mInputRange
.aStart
.Row(), mInputRange
.aStart
.Tab());
227 SCROW aNewRow
= mRow
+ aDelta
;
228 if(aNewRow
< mInputRange
.aStart
.Row() || aNewRow
> mInputRange
.aEnd
.Row())
231 aResult
.SetInvalid();
234 return ScAddress(mInputRange
.aStart
.Col(), aNewRow
, mInputRange
.aStart
.Tab());
240 DataRangeIterator::DataRangeIterator(ScRange aInputRange
) :
241 mInputRange(aInputRange
),
245 DataRangeIterator::~DataRangeIterator()
248 sal_Int32
DataRangeIterator::index()
253 // DataRangeByColumnIterator
255 DataRangeByColumnIterator::DataRangeByColumnIterator(ScRange aInputRange
) :
256 DataRangeIterator(aInputRange
),
257 mCol(aInputRange
.aStart
.Col())
260 bool DataRangeByColumnIterator::hasNext()
262 return mCol
<= mInputRange
.aEnd
.Col();
265 void DataRangeByColumnIterator::next()
271 ScRange
DataRangeByColumnIterator::get()
274 ScAddress(mCol
, mInputRange
.aStart
.Row(), mInputRange
.aStart
.Tab()),
275 ScAddress(mCol
, mInputRange
.aEnd
.Row(), mInputRange
.aEnd
.Tab())
279 void DataRangeByColumnIterator::reset()
281 mCol
= mInputRange
.aStart
.Col();
284 DataCellIterator
DataRangeByColumnIterator::iterateCells()
286 return DataCellIterator(get(), false);
289 // DataRangeByRowIterator
291 DataRangeByRowIterator::DataRangeByRowIterator(ScRange aInputRange
) :
292 DataRangeIterator(aInputRange
),
293 mRow(aInputRange
.aStart
.Row())
296 bool DataRangeByRowIterator::hasNext()
298 return mRow
<= mInputRange
.aEnd
.Row();
301 void DataRangeByRowIterator::next()
307 ScRange
DataRangeByRowIterator::get()
310 ScAddress(mInputRange
.aStart
.Col(), mRow
, mInputRange
.aStart
.Tab()),
311 ScAddress(mInputRange
.aEnd
.Col(), mRow
, mInputRange
.aEnd
.Tab())
315 void DataRangeByRowIterator::reset()
317 mRow
= mInputRange
.aStart
.Row();
320 DataCellIterator
DataRangeByRowIterator::iterateCells()
322 return DataCellIterator(get(), true);
325 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */