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/.
10 #include "clipcontext.hxx"
11 #include "document.hxx"
12 #include "mtvelements.hxx"
14 #include <scitems.hxx>
15 #include <tokenarray.hxx>
16 #include <editutil.hxx>
17 #include <clipparam.hxx>
19 #include <svl/intitem.hxx>
23 ClipContextBase::ClipContextBase(ScDocument
& rDoc
) :
24 mpSet(new ColumnBlockPositionSet(rDoc
)) {}
26 ClipContextBase::~ClipContextBase() {}
28 ColumnBlockPosition
* ClipContextBase::getBlockPosition(SCTAB nTab
, SCCOL nCol
)
30 return mpSet
->getBlockPosition(nTab
, nCol
);
33 CopyFromClipContext::CopyFromClipContext(ScDocument
& rDoc
,
34 ScDocument
* pRefUndoDoc
, ScDocument
* pClipDoc
, InsertDeleteFlags nInsertFlag
,
35 bool bAsLink
, bool bSkipAttrForEmptyCells
) :
36 ClipContextBase(rDoc
),
37 mnDestCol1(-1), mnDestCol2(-1),
38 mnDestRow1(-1), mnDestRow2(-1),
39 mnTabStart(-1), mnTabEnd(-1),
41 mpRefUndoDoc(pRefUndoDoc
), mpClipDoc(pClipDoc
),
42 mnInsertFlag(nInsertFlag
), mnDeleteFlag(IDF_NONE
),
43 mpCondFormatList(NULL
),
44 mbAsLink(bAsLink
), mbSkipAttrForEmptyCells(bSkipAttrForEmptyCells
),
45 mbCloneNotes (mnInsertFlag
& (IDF_NOTE
|IDF_ADDNOTES
)),
46 mbTableProtected(false)
50 CopyFromClipContext::~CopyFromClipContext()
54 void CopyFromClipContext::setTabRange(SCTAB nStart
, SCTAB nEnd
)
60 SCTAB
CopyFromClipContext::getTabStart() const
65 SCTAB
CopyFromClipContext::getTabEnd() const
70 void CopyFromClipContext::setDestRange( SCCOL nCol1
, SCROW nRow1
, SCCOL nCol2
, SCROW nRow2
)
78 CopyFromClipContext::Range
CopyFromClipContext::getDestRange() const
81 aRet
.mnCol1
= mnDestCol1
;
82 aRet
.mnCol2
= mnDestCol2
;
83 aRet
.mnRow1
= mnDestRow1
;
84 aRet
.mnRow2
= mnDestRow2
;
88 ScDocument
* CopyFromClipContext::getUndoDoc()
93 ScDocument
* CopyFromClipContext::getClipDoc()
98 InsertDeleteFlags
CopyFromClipContext::getInsertFlag() const
103 void CopyFromClipContext::setDeleteFlag( InsertDeleteFlags nFlag
)
105 mnDeleteFlag
= nFlag
;
108 InsertDeleteFlags
CopyFromClipContext::getDeleteFlag() const
113 void CopyFromClipContext::setSingleCellColumnSize( size_t nSize
)
115 maSingleCells
.resize(nSize
);
116 maSingleCellAttrs
.resize(nSize
);
117 maSinglePatterns
.resize(nSize
, NULL
);
118 maSingleNotes
.resize(nSize
, NULL
);
121 ScCellValue
& CopyFromClipContext::getSingleCell( size_t nColOffset
)
123 assert(nColOffset
< maSingleCells
.size());
124 return maSingleCells
[nColOffset
];
127 sc::CellTextAttr
& CopyFromClipContext::getSingleCellAttr( size_t nColOffset
)
129 assert(nColOffset
< maSingleCellAttrs
.size());
130 return maSingleCellAttrs
[nColOffset
];
133 void CopyFromClipContext::setSingleCell( const ScAddress
& rSrcPos
, const ScColumn
& rSrcCol
)
135 SCCOL nColOffset
= rSrcPos
.Col() - mpClipDoc
->GetClipParam().getWholeRange().aStart
.Col();
136 ScCellValue
& rSrcCell
= getSingleCell(nColOffset
);
138 const sc::CellTextAttr
* pAttr
= rSrcCol
.GetCellTextAttr(rSrcPos
.Row());
142 sc::CellTextAttr
& rAttr
= getSingleCellAttr(nColOffset
);
148 ScSingleRefData aRef
;
149 aRef
.InitAddress(rSrcPos
);
150 aRef
.SetFlag3D(true);
153 aArr
.AddSingleReference(aRef
);
154 rSrcCell
.set(new ScFormulaCell(mpClipDoc
, rSrcPos
, aArr
));
158 rSrcCell
.assign(*mpClipDoc
, rSrcPos
);
160 // Check the paste flag to see whether we want to paste this cell. If the
161 // flag says we don't want to paste this cell, we'll return with true.
162 InsertDeleteFlags nFlags
= getInsertFlag();
163 bool bNumeric
= (nFlags
& IDF_VALUE
) != IDF_NONE
;
164 bool bDateTime
= (nFlags
& IDF_DATETIME
) != IDF_NONE
;
165 bool bString
= (nFlags
& IDF_STRING
) != IDF_NONE
;
166 bool bBoolean
= (nFlags
& IDF_SPECIAL_BOOLEAN
) != IDF_NONE
;
167 bool bFormula
= (nFlags
& IDF_FORMULA
) != IDF_NONE
;
169 switch (rSrcCell
.meType
)
173 bool bPaste
= isDateCell(rSrcCol
, rSrcPos
.Row()) ? bDateTime
: bNumeric
;
179 case CELLTYPE_STRING
:
187 case CELLTYPE_FORMULA
:
191 // Check if this formula cell is a boolean cell, and if so, go ahead and paste it.
192 ScTokenArray
* pCode
= rSrcCell
.mpFormula
->GetCode();
193 if (pCode
&& pCode
->GetLen() == 1)
195 const formula::FormulaToken
* p
= pCode
->First();
196 if (p
->GetOpCode() == ocTrue
|| p
->GetOpCode() == ocFalse
)
197 // This is a boolean formula. Good.
206 sal_uInt16 nErr
= rSrcCell
.mpFormula
->GetErrCode();
209 // error codes are cloned with values
211 // Error code is treated as numeric value. Don't paste it.
214 else if (rSrcCell
.mpFormula
->IsValue())
216 bool bPaste
= isDateCell(rSrcCol
, rSrcPos
.Row()) ? bDateTime
: bNumeric
;
224 // Turn this into a numeric cell.
225 rSrcCell
.set(rSrcCell
.mpFormula
->GetValue());
229 svl::SharedString aStr
= rSrcCell
.mpFormula
->GetString();
232 // do not clone empty string
237 // Turn this into a string or edit cell.
238 if (rSrcCell
.mpFormula
->IsMultilineResult())
240 // TODO : Add shared string support to the edit engine to
241 // make this process simpler.
242 ScFieldEditEngine
& rEngine
= mrDestDoc
.GetEditEngine();
243 rEngine
.SetText(rSrcCell
.mpFormula
->GetString().getString());
244 boost::scoped_ptr
<EditTextObject
> pObj(rEngine
.CreateTextObject());
245 pObj
->NormalizeString(mrDestDoc
.GetSharedStringPool());
249 rSrcCell
.set(rSrcCell
.mpFormula
->GetString());
252 // We don't want to paste this.
258 // There is nothing to paste.
263 const ScPatternAttr
* CopyFromClipContext::getSingleCellPattern( size_t nColOffset
) const
265 assert(nColOffset
< maSinglePatterns
.size());
266 return maSinglePatterns
[nColOffset
];
269 void CopyFromClipContext::setSingleCellPattern( size_t nColOffset
, const ScPatternAttr
* pAttr
)
271 assert(nColOffset
< maSinglePatterns
.size());
272 maSinglePatterns
[nColOffset
] = pAttr
;
275 const ScPostIt
* CopyFromClipContext::getSingleCellNote( size_t nColOffset
) const
277 assert(nColOffset
< maSingleNotes
.size());
278 return maSingleNotes
[nColOffset
];
281 void CopyFromClipContext::setSingleCellNote( size_t nColOffset
, const ScPostIt
* pNote
)
283 assert(nColOffset
< maSingleNotes
.size());
284 maSingleNotes
[nColOffset
] = pNote
;
287 void CopyFromClipContext::setCondFormatList( ScConditionalFormatList
* pCondFormatList
)
289 mpCondFormatList
= pCondFormatList
;
292 ScConditionalFormatList
* CopyFromClipContext::getCondFormatList()
294 return mpCondFormatList
;
297 void CopyFromClipContext::setTableProtected( bool b
)
299 mbTableProtected
= b
;
302 bool CopyFromClipContext::isTableProtected() const
304 return mbTableProtected
;
307 bool CopyFromClipContext::isAsLink() const
312 bool CopyFromClipContext::isSkipAttrForEmptyCells() const
314 return mbSkipAttrForEmptyCells
;
317 bool CopyFromClipContext::isCloneNotes() const
322 bool CopyFromClipContext::isDateCell( const ScColumn
& rCol
, SCROW nRow
) const
324 sal_uLong nNumIndex
= static_cast<const SfxUInt32Item
*>(rCol
.GetAttr(nRow
, ATTR_VALUE_FORMAT
))->GetValue();
325 short nType
= mpClipDoc
->GetFormatTable()->GetType(nNumIndex
);
326 return (nType
== css::util::NumberFormat::DATE
) || (nType
== css::util::NumberFormat::TIME
) || (nType
== css::util::NumberFormat::DATETIME
);
329 CopyToClipContext::CopyToClipContext(
330 ScDocument
& rDoc
, bool bKeepScenarioFlags
, bool bCloneNotes
) :
331 ClipContextBase(rDoc
), mbKeepScenarioFlags(bKeepScenarioFlags
), mbCloneNotes(bCloneNotes
) {}
333 CopyToClipContext::~CopyToClipContext() {}
335 bool CopyToClipContext::isKeepScenarioFlags() const
337 return mbKeepScenarioFlags
;
340 bool CopyToClipContext::isCloneNotes() const
345 CopyToDocContext::CopyToDocContext(ScDocument
& rDoc
) :
346 ClipContextBase(rDoc
), mbStartListening(true) {}
348 CopyToDocContext::~CopyToDocContext() {}
350 void CopyToDocContext::setStartListening( bool b
)
352 mbStartListening
= b
;
355 bool CopyToDocContext::isStartListening() const
357 return mbStartListening
;
360 MixDocContext::MixDocContext(ScDocument
& rDoc
) : ClipContextBase(rDoc
) {}
361 MixDocContext::~MixDocContext() {}
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */