LibreOffice Module sc (master) 1
clipcontext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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 */
9
10#include <memory>
11#include <clipcontext.hxx>
12#include <document.hxx>
13#include <mtvelements.hxx>
14#include <column.hxx>
15#include <scitems.hxx>
16#include <tokenarray.hxx>
17#include <editutil.hxx>
18#include <clipparam.hxx>
19
20#include <svl/intitem.hxx>
21#include <svl/numformat.hxx>
23#include <refdata.hxx>
24
25namespace sc {
26
28 mpSet(new ColumnBlockPositionSet(rDoc)) {}
29
31
33{
34 return mpSet->getBlockPosition(nTab, nCol);
35}
36
38 ScDocument* pRefUndoDoc, ScDocument* pClipDoc, InsertDeleteFlags nInsertFlag,
39 bool bAsLink, bool bSkipEmptyCells) :
40 ClipContextBase(rDoc),
41 mnDestCol1(-1), mnDestCol2(-1),
42 mnDestRow1(-1), mnDestRow2(-1),
43 mnTabStart(-1), mnTabEnd(-1),
44 mrDestDoc(rDoc),
45 mpRefUndoDoc(pRefUndoDoc), mpClipDoc(pClipDoc),
46 mnInsertFlag(nInsertFlag), mnDeleteFlag(InsertDeleteFlags::NONE),
47 mpCondFormatList(nullptr),
48 mbAsLink(bAsLink), mbSkipEmptyCells(bSkipEmptyCells),
49 mbTableProtected(false)
50{
51}
52
54{
55}
56
58{
59 mnTabStart = nStart;
60 mnTabEnd = nEnd;
61}
62
64{
65 return mnTabStart;
66}
67
69{
70 return mnTabEnd;
71}
72
73void CopyFromClipContext::setDestRange( SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2 )
74{
75 mnDestCol1 = nCol1;
76 mnDestRow1 = nRow1;
77 mnDestCol2 = nCol2;
78 mnDestRow2 = nRow2;
79}
80
82{
83 Range aRet;
84 aRet.mnCol1 = mnDestCol1;
85 aRet.mnCol2 = mnDestCol2;
86 aRet.mnRow1 = mnDestRow1;
87 aRet.mnRow2 = mnDestRow2;
88 return aRet;
89}
90
92{
93 return mpRefUndoDoc;
94}
95
97{
98 return mpClipDoc;
99}
100
102{
103 return mnInsertFlag;
104}
105
107{
108 mnDeleteFlag = nFlag;
109}
110
112{
113 return mnDeleteFlag;
114}
115
117{
118 maSingleCells.resize(nSize);
119 maSingleCellAttrs.resize(nSize);
120 maSinglePatterns.resize(nSize, nullptr);
121 maSingleNotes.resize(nSize, nullptr);
122 maSingleSparkline.resize(nSize);
123}
124
126{
127 assert(nColOffset < maSingleCells.size());
128 return maSingleCells[nColOffset];
129}
130
132{
133 assert(nColOffset < maSingleCellAttrs.size());
134 return maSingleCellAttrs[nColOffset];
135}
136
137void CopyFromClipContext::setSingleCell( const ScAddress& rSrcPos, const ScColumn& rSrcCol )
138{
139 SCCOL nColOffset = rSrcPos.Col() - mpClipDoc->GetClipParam().getWholeRange().aStart.Col();
140 ScCellValue& rSrcCell = getSingleCell(nColOffset);
141
142 const sc::CellTextAttr* pAttr = rSrcCol.GetCellTextAttr(rSrcPos.Row());
143
144 if (pAttr)
145 {
146 sc::CellTextAttr& rAttr = getSingleCellAttr(nColOffset);
147 rAttr = *pAttr;
148 }
149
150 if (mbAsLink)
151 {
152 ScSingleRefData aRef;
153 aRef.InitAddress(rSrcPos);
154 aRef.SetFlag3D(true);
155
157 aArr.AddSingleReference(aRef);
158 rSrcCell.set(new ScFormulaCell(*mpClipDoc, rSrcPos, aArr));
159 return;
160 }
161
162 rSrcCell.assign(*mpClipDoc, rSrcPos);
163
164 // Check the paste flag to see whether we want to paste this cell. If the
165 // flag says we don't want to paste this cell, we'll return with true.
167 bool bNumeric = (nFlags & InsertDeleteFlags::VALUE) != InsertDeleteFlags::NONE;
168 bool bDateTime = (nFlags & InsertDeleteFlags::DATETIME) != InsertDeleteFlags::NONE;
169 bool bString = (nFlags & InsertDeleteFlags::STRING) != InsertDeleteFlags::NONE;
171 bool bFormula = (nFlags & InsertDeleteFlags::FORMULA) != InsertDeleteFlags::NONE;
172
173 switch (rSrcCell.getType())
174 {
175 case CELLTYPE_VALUE:
176 {
177 bool bPaste = isDateCell(rSrcCol, rSrcPos.Row()) ? bDateTime : bNumeric;
178 if (!bPaste)
179 // Don't paste this.
180 rSrcCell.clear();
181 }
182 break;
183 case CELLTYPE_STRING:
184 case CELLTYPE_EDIT:
185 {
186 if (!bString)
187 // Skip pasting.
188 rSrcCell.clear();
189 }
190 break;
191 case CELLTYPE_FORMULA:
192 {
193 if (bBoolean)
194 {
195 // Check if this formula cell is a boolean cell, and if so, go ahead and paste it.
196 const ScTokenArray* pCode = rSrcCell.getFormula()->GetCode();
197 if (pCode && pCode->GetLen() == 1)
198 {
199 const formula::FormulaToken* p = pCode->FirstToken();
200 if (p->GetOpCode() == ocTrue || p->GetOpCode() == ocFalse)
201 // This is a boolean formula. Good.
202 break;
203 }
204 }
205
206 if (bFormula)
207 // Good.
208 break;
209
210 FormulaError nErr = rSrcCell.getFormula()->GetErrCode();
211 if (nErr != FormulaError::NONE)
212 {
213 // error codes are cloned with values
214 if (!bNumeric)
215 // Error code is treated as numeric value. Don't paste it.
216 rSrcCell.clear();
217 else
218 {
219 // Turn this into a formula cell with just the error code.
220 ScFormulaCell* pErrCell = new ScFormulaCell(*mpClipDoc, rSrcPos);
221 pErrCell->SetErrCode(nErr);
222 rSrcCell.set(pErrCell);
223 }
224 }
225 else if (rSrcCell.getFormula()->IsEmptyDisplayedAsString())
226 {
227 // Empty stays empty and doesn't become 0.
228 rSrcCell.clear();
229 }
230 else if (rSrcCell.getFormula()->IsValue())
231 {
232 bool bPaste = isDateCell(rSrcCol, rSrcPos.Row()) ? bDateTime : bNumeric;
233 if (!bPaste)
234 {
235 // Don't paste this.
236 rSrcCell.clear();
237 break;
238 }
239
240 // Turn this into a numeric cell.
241 rSrcCell.set(rSrcCell.getFormula()->GetValue());
242 }
243 else if (bString)
244 {
246 if (aStr.isEmpty())
247 {
248 // do not clone empty string
249 rSrcCell.clear();
250 break;
251 }
252
253 // Turn this into a string or edit cell.
254 if (rSrcCell.getFormula()->IsMultilineResult())
255 {
256 // TODO : Add shared string support to the edit engine to
257 // make this process simpler.
259 rEngine.SetTextCurrentDefaults(rSrcCell.getFormula()->GetString().getString());
260 std::unique_ptr<EditTextObject> pObj(rEngine.CreateTextObject());
261 pObj->NormalizeString(mrDestDoc.GetSharedStringPool());
262 rSrcCell.set(*pObj);
263 }
264 else
265 rSrcCell.set(rSrcCell.getFormula()->GetString());
266 }
267 else
268 // We don't want to paste this.
269 rSrcCell.clear();
270 }
271 break;
272 case CELLTYPE_NONE:
273 default:
274 // There is nothing to paste.
275 rSrcCell.clear();
276 }
277}
278
280{
281 assert(nColOffset < maSinglePatterns.size());
282 return maSinglePatterns[nColOffset];
283}
284
285void CopyFromClipContext::setSingleCellPattern( size_t nColOffset, const ScPatternAttr* pAttr )
286{
287 assert(nColOffset < maSinglePatterns.size());
288 maSinglePatterns[nColOffset] = pAttr;
289}
290
291const ScPostIt* CopyFromClipContext::getSingleCellNote( size_t nColOffset ) const
292{
293 assert(nColOffset < maSingleNotes.size());
294 return maSingleNotes[nColOffset];
295}
296
297void CopyFromClipContext::setSingleCellNote( size_t nColOffset, const ScPostIt* pNote )
298{
299 assert(nColOffset < maSingleNotes.size());
300 maSingleNotes[nColOffset] = pNote;
301}
302
303std::shared_ptr<sc::Sparkline> const& CopyFromClipContext::getSingleSparkline(size_t nColOffset) const
304{
305 assert(nColOffset < maSingleSparkline.size());
306 return maSingleSparkline[nColOffset];
307}
308
309void CopyFromClipContext::setSingleSparkline(size_t nColOffset, std::shared_ptr<sc::Sparkline> const& pSparkline)
310{
311 assert(nColOffset < maSingleSparkline.size());
312 maSingleSparkline[nColOffset] = pSparkline;
313}
314
316{
317 mpCondFormatList = pCondFormatList;
318}
319
321{
322 return mpCondFormatList;
323}
324
326{
328}
329
331{
332 return mbTableProtected;
333}
334
336{
337 return mbAsLink;
338}
339
341{
342 return mbSkipEmptyCells;
343}
344
346{
348}
349
351{
353}
354
355bool CopyFromClipContext::isDateCell( const ScColumn& rCol, SCROW nRow ) const
356{
357 sal_uLong nNumIndex = rCol.GetAttr(nRow, ATTR_VALUE_FORMAT).GetValue();
359 return (nType == SvNumFormatType::DATE) || (nType == SvNumFormatType::TIME) || (nType == SvNumFormatType::DATETIME);
360}
361
363 ScDocument& rDoc, bool bKeepScenarioFlags) :
364 ClipContextBase(rDoc), mbKeepScenarioFlags(bKeepScenarioFlags) {}
365
367
369{
370 return mbKeepScenarioFlags;
371}
372
374 ClipContextBase(rDoc), mbStartListening(true) {}
375
377
379{
381}
382
384{
385 return mbStartListening;
386}
387
390
391}
392
393/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< EditTextObject > CreateTextObject()
SCROW Row() const
Definition: address.hxx:274
SCCOL Col() const
Definition: address.hxx:279
const SfxPoolItem & GetAttr(SCROW nRow, sal_uInt16 nWhich) const
Definition: column.hxx:957
const sc::CellTextAttr * GetCellTextAttr(SCROW nRow) const
Definition: column.cxx:692
ScClipParam & GetClipParam()
Definition: document.cxx:2601
SC_DLLPUBLIC ScFieldEditEngine & GetEditEngine()
Definition: documen2.cxx:483
SC_DLLPUBLIC svl::SharedStringPool & GetSharedStringPool()
Definition: documen2.cxx:592
SC_DLLPUBLIC SvNumberFormatter * GetFormatTable() const
Definition: documen2.cxx:467
void SetTextCurrentDefaults(const EditTextObject &rTextObject)
SetText and apply defaults already set.
Definition: editutil.cxx:616
void SetErrCode(FormulaError n)
bool IsEmptyDisplayedAsString()
double GetValue()
const svl::SharedString & GetString()
FormulaError GetErrCode()
ScTokenArray * GetCode()
bool IsMultilineResult()
Determines whether or not the result string contains more than one paragraph.
Additional class containing cell annotation data.
Definition: postit.hxx:58
ScAddress aStart
Definition: address.hxx:497
SvNumFormatType GetType(sal_uInt32 nFIndex) const
sal_uInt16 GetLen() const
FormulaToken * FirstToken() const
ColumnBlockPosition * getBlockPosition(SCTAB nTab, SCCOL nCol)
Definition: clipcontext.cxx:32
ClipContextBase()=delete
std::unique_ptr< ColumnBlockPositionSet > mpSet
Definition: clipcontext.hxx:33
virtual ~ClipContextBase()
Definition: clipcontext.cxx:30
void setSingleCellPattern(size_t nColOffset, const ScPatternAttr *pAttr)
void setSingleCellNote(size_t nColOffset, const ScPostIt *pNote)
void setDeleteFlag(InsertDeleteFlags nFlag)
void setSingleCellColumnSize(size_t nSize)
Set the column size of a "single cell" row, which is used when copying a single row of cells in a cli...
std::vector< const ScPatternAttr * > maSinglePatterns
Definition: clipcontext.hxx:62
ScDocument * mpRefUndoDoc
Definition: clipcontext.hxx:55
std::vector< sc::CellTextAttr > maSingleCellAttrs
Definition: clipcontext.hxx:61
void setSingleSparkline(size_t nColOffset, std::shared_ptr< sc::Sparkline > const &pSparkline)
void setDestRange(SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2)
Definition: clipcontext.cxx:73
virtual ~CopyFromClipContext() override
Definition: clipcontext.cxx:53
std::vector< std::shared_ptr< sc::Sparkline > > maSingleSparkline
Definition: clipcontext.hxx:64
sc::CellTextAttr & getSingleCellAttr(size_t nColOffset)
bool isCloneSparklines() const
InsertDeleteFlags getDeleteFlag() const
ScConditionalFormatList * getCondFormatList()
bool isSkipEmptyCells() const
Get the flag that indicates whether the "skip empty cells" paste option is selected.
ScCellValue & getSingleCell(size_t nColOffset)
InsertDeleteFlags getInsertFlag() const
void setCondFormatList(ScConditionalFormatList *pCondFormatList)
InsertDeleteFlags mnDeleteFlag
Definition: clipcontext.hxx:58
std::shared_ptr< sc::Sparkline > const & getSingleSparkline(size_t nColOffset) const
InsertDeleteFlags mnInsertFlag
Definition: clipcontext.hxx:57
void setTabRange(SCTAB nStart, SCTAB nEnd)
Definition: clipcontext.cxx:57
std::vector< const ScPostIt * > maSingleNotes
Definition: clipcontext.hxx:63
const ScPatternAttr * getSingleCellPattern(size_t nColOffset) const
bool isDateCell(const ScColumn &rCol, SCROW nRow) const
void setSingleCell(const ScAddress &rSrcPos, const ScColumn &rSrcCol)
ScConditionalFormatList * mpCondFormatList
Definition: clipcontext.hxx:66
void setTableProtected(bool b)
bool isTableProtected() const
SCTAB getTabStart() const
Definition: clipcontext.cxx:63
Range getDestRange() const
Definition: clipcontext.cxx:81
const ScPostIt * getSingleCellNote(size_t nColOffset) const
ScDocument * getUndoDoc()
Definition: clipcontext.cxx:91
SCTAB getTabEnd() const
Definition: clipcontext.cxx:68
ScDocument * getClipDoc()
Definition: clipcontext.cxx:96
std::vector< ScCellValue > maSingleCells
Definition: clipcontext.hxx:60
bool isKeepScenarioFlags() const
virtual ~CopyToClipContext() override
CopyToClipContext(ScDocument &rDoc, bool bKeepScenarioFlags)
virtual ~CopyToDocContext() override
bool isStartListening() const
CopyToDocContext(ScDocument &rDoc)
void setStartListening(bool b)
MixDocContext(ScDocument &rDoc)
virtual ~MixDocContext() override
const OUString & getString() const
FormulaError
@ CELLTYPE_EDIT
Definition: global.hxx:276
@ CELLTYPE_STRING
Definition: global.hxx:274
@ CELLTYPE_FORMULA
Definition: global.hxx:275
@ CELLTYPE_NONE
Definition: global.hxx:272
@ CELLTYPE_VALUE
Definition: global.hxx:273
InsertDeleteFlags
Definition: global.hxx:149
@ SPARKLINES
Sheet / outlining (grouping) information.
@ NOTE
Strings (and string results if InsertDeleteFlags::FORMULA is not set).
@ SPECIAL_BOOLEAN
Internal use only (copy from clip): do not delete existing cell contents when pasting notes.
@ STRING
Dates, times, datetime values.
@ ADDNOTES
Internal use only (undo etc.): do not copy/delete caption objects of cell notes.
@ DATETIME
Numeric values (and numeric results if InsertDeleteFlags::FORMULA is not set).
@ FORMULA
Cell notes.
void * p
aStr
NONE
CAUTION! The following defines must be in the same namespace as the respective type.
const SvxPageUsage aArr[]
ocFalse
ocTrue
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
constexpr TypedWhichId< SfxUInt32Item > ATTR_VALUE_FORMAT(146)
sal_uIntPtr sal_uLong
Store arbitrary cell value of any kind.
Definition: cellvalue.hxx:32
void assign(const ScDocument &rDoc, const ScAddress &rPos)
Take cell value from specified position in specified document.
Definition: cellvalue.cxx:359
void set(double fValue)
Definition: cellvalue.cxx:329
void clear() noexcept
Definition: cellvalue.cxx:311
CellType getType() const
Definition: cellvalue.cxx:296
ScFormulaCell * getFormula() const
Definition: cellvalue.hxx:59
ScRange getWholeRange() const
Return a single range that encompasses all individual ranges.
Definition: clipparam.cxx:109
Single reference (one address) into the sheet.
Definition: refdata.hxx:30
void InitAddress(const ScAddress &rAdr)
InitAddress: InitFlags and set address.
Definition: refdata.cxx:27
void SetFlag3D(bool bVal)
Definition: refdata.hxx:89
Store position data for column array storage.
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int16 SCCOL
Definition: types.hxx:21
sal_Int32 SCROW
Definition: types.hxx:17
SvNumFormatType