1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <cellvalue.hxx>
#include <document.hxx>
#include <column.hxx>
#include <formulacell.hxx>
#include <editeng/editobj.hxx>
#include <editeng/editstat.hxx>
#include <stringutil.hxx>
#include <editutil.hxx>
#include <tokenarray.hxx>
#include <formula/token.hxx>
#include <formula/errorcodes.hxx>
#include <svl/sharedstring.hxx>
namespace {
CellType adjustCellType( CellType eOrig )
{
switch (eOrig)
{
case CELLTYPE_EDIT:
return CELLTYPE_STRING;
default:
;
}
return eOrig;
}
template<typename T>
OUString getString( const T& rVal )
{
if (rVal.meType == CELLTYPE_STRING)
return rVal.mpString->getString();
if (rVal.meType == CELLTYPE_EDIT)
{
OUStringBuffer aRet;
sal_Int32 n = rVal.mpEditText->GetParagraphCount();
for (sal_Int32 i = 0; i < n; ++i)
{
if (i > 0)
aRet.append('\n');
aRet.append(rVal.mpEditText->GetText(i));
}
return aRet.makeStringAndClear();
}
return OUString();
}
bool equalsFormulaCells( const ScFormulaCell* p1, const ScFormulaCell* p2 )
{
const ScTokenArray* pCode1 = p1->GetCode();
const ScTokenArray* pCode2 = p2->GetCode();
if (pCode1->GetLen() != pCode2->GetLen())
return false;
if (pCode1->GetCodeError() != pCode2->GetCodeError())
return false;
sal_uInt16 n = pCode1->GetLen();
formula::FormulaToken** ppToken1 = pCode1->GetArray();
formula::FormulaToken** ppToken2 = pCode2->GetArray();
for (sal_uInt16 i = 0; i < n; ++i)
{
if (!ppToken1[i]->TextEqual(*(ppToken2[i])))
return false;
}
return true;
}
template<typename T>
bool equalsWithoutFormatImpl( const T& left, const T& right )
{
CellType eType1 = adjustCellType(left.meType);
CellType eType2 = adjustCellType(right.meType);
if (eType1 != eType2)
return false;
switch (eType1)
{
case CELLTYPE_NONE:
return true;
case CELLTYPE_VALUE:
return left.mfValue == right.mfValue;
case CELLTYPE_STRING:
{
OUString aStr1 = getString(left);
OUString aStr2 = getString(right);
return aStr1 == aStr2;
}
case CELLTYPE_FORMULA:
return equalsFormulaCells(left.mpFormula, right.mpFormula);
default:
;
}
return false;
}
void commitToColumn( const ScCellValue& rCell, ScColumn& rColumn, SCROW nRow )
{
switch (rCell.meType)
{
case CELLTYPE_STRING:
rColumn.SetRawString(nRow, *rCell.mpString);
break;
case CELLTYPE_EDIT:
rColumn.SetEditText(nRow, ScEditUtil::Clone(*rCell.mpEditText, rColumn.GetDoc()));
break;
case CELLTYPE_VALUE:
rColumn.SetValue(nRow, rCell.mfValue);
break;
case CELLTYPE_FORMULA:
{
ScAddress aDestPos(rColumn.GetCol(), nRow, rColumn.GetTab());
rColumn.SetFormulaCell(nRow, new ScFormulaCell(*rCell.mpFormula, rColumn.GetDoc(), aDestPos));
}
break;
default:
rColumn.DeleteContent(nRow);
}
}
bool hasStringImpl( CellType eType, ScFormulaCell* pFormula )
{
switch (eType)
{
case CELLTYPE_STRING:
case CELLTYPE_EDIT:
return true;
case CELLTYPE_FORMULA:
return !pFormula->IsValue();
default:
return false;
}
}
bool hasNumericImpl( CellType eType, ScFormulaCell* pFormula )
{
switch (eType)
{
case CELLTYPE_VALUE:
return true;
case CELLTYPE_FORMULA:
return pFormula->IsValue();
default:
return false;
}
}
template<typename CellT>
OUString getStringImpl( const CellT& rCell, const ScDocument* pDoc )
{
switch (rCell.meType)
{
case CELLTYPE_VALUE:
return OUString::number(rCell.mfValue);
case CELLTYPE_STRING:
return rCell.mpString->getString();
case CELLTYPE_EDIT:
if (rCell.mpEditText)
return ScEditUtil::GetString(*rCell.mpEditText, pDoc);
break;
case CELLTYPE_FORMULA:
return rCell.mpFormula->GetString().getString();
default:
;
}
return OUString();
}
template<typename CellT>
OUString getRawStringImpl( const CellT& rCell, const ScDocument& rDoc )
{
switch (rCell.meType)
{
case CELLTYPE_VALUE:
return OUString::number(rCell.mfValue);
case CELLTYPE_STRING:
return rCell.mpString->getString();
case CELLTYPE_EDIT:
if (rCell.mpEditText)
return ScEditUtil::GetString(*rCell.mpEditText, &rDoc);
break;
case CELLTYPE_FORMULA:
return rCell.mpFormula->GetRawString().getString();
default:
;
}
return OUString();
}
}
ScCellValue::ScCellValue() : meType(CELLTYPE_NONE), mfValue(0.0) {}
ScCellValue::ScCellValue( const ScRefCellValue& rCell ) : meType(rCell.meType), mfValue(rCell.mfValue)
{
switch (rCell.meType)
{
case CELLTYPE_STRING:
mpString = new svl::SharedString(*rCell.mpString);
break;
case CELLTYPE_EDIT:
mpEditText = rCell.mpEditText->Clone().release();
break;
case CELLTYPE_FORMULA:
mpFormula = rCell.mpFormula->Clone();
break;
default:
;
}
}
ScCellValue::ScCellValue( double fValue ) : meType(CELLTYPE_VALUE), mfValue(fValue) {}
ScCellValue::ScCellValue( const svl::SharedString& rString ) : meType(CELLTYPE_STRING), mpString(new svl::SharedString(rString)) {}
ScCellValue::ScCellValue( const ScCellValue& r ) : meType(r.meType), mfValue(r.mfValue)
{
switch (r.meType)
{
case CELLTYPE_STRING:
mpString = new svl::SharedString(*r.mpString);
break;
case CELLTYPE_EDIT:
mpEditText = r.mpEditText->Clone().release();
break;
case CELLTYPE_FORMULA:
mpFormula = r.mpFormula->Clone();
break;
default:
;
}
}
ScCellValue::ScCellValue(ScCellValue&& r) noexcept
: meType(r.meType)
, mfValue(r.mfValue)
{
switch (r.meType)
{
case CELLTYPE_STRING:
mpString = r.mpString;
break;
case CELLTYPE_EDIT:
mpEditText = r.mpEditText;
break;
case CELLTYPE_FORMULA:
mpFormula = r.mpFormula;
break;
default:
;
}
r.meType = CELLTYPE_NONE;
}
ScCellValue::~ScCellValue()
{
clear();
}
void ScCellValue::clear() noexcept
{
switch (meType)
{
case CELLTYPE_STRING:
delete mpString;
break;
case CELLTYPE_EDIT:
delete mpEditText;
break;
case CELLTYPE_FORMULA:
delete mpFormula;
break;
default:
;
}
// Reset to empty value.
meType = CELLTYPE_NONE;
mfValue = 0.0;
}
void ScCellValue::set( double fValue )
{
clear();
meType = CELLTYPE_VALUE;
mfValue = fValue;
}
void ScCellValue::set( const svl::SharedString& rStr )
{
clear();
meType = CELLTYPE_STRING;
mpString = new svl::SharedString(rStr);
}
void ScCellValue::set( const EditTextObject& rEditText )
{
clear();
meType = CELLTYPE_EDIT;
mpEditText = rEditText.Clone().release();
}
void ScCellValue::set( EditTextObject* pEditText )
{
clear();
meType = CELLTYPE_EDIT;
mpEditText = pEditText;
}
void ScCellValue::set( ScFormulaCell* pFormula )
{
clear();
meType = CELLTYPE_FORMULA;
mpFormula = pFormula;
}
void ScCellValue::assign( const ScDocument& rDoc, const ScAddress& rPos )
{
clear();
ScRefCellValue aRefVal(const_cast<ScDocument&>(rDoc), rPos);
meType = aRefVal.meType;
switch (meType)
{
case CELLTYPE_STRING:
mpString = new svl::SharedString(*aRefVal.mpString);
break;
case CELLTYPE_EDIT:
if (aRefVal.mpEditText)
mpEditText = aRefVal.mpEditText->Clone().release();
break;
case CELLTYPE_VALUE:
mfValue = aRefVal.mfValue;
break;
case CELLTYPE_FORMULA:
mpFormula = aRefVal.mpFormula->Clone();
break;
default:
meType = CELLTYPE_NONE; // reset to empty.
}
}
void ScCellValue::assign(const ScCellValue& rOther, ScDocument& rDestDoc, ScCloneFlags nCloneFlags)
{
clear();
meType = rOther.meType;
switch (meType)
{
case CELLTYPE_STRING:
mpString = new svl::SharedString(*rOther.mpString);
break;
case CELLTYPE_EDIT:
{
// Switch to the pool of the destination document.
ScFieldEditEngine& rEngine = rDestDoc.GetEditEngine();
if (rOther.mpEditText->HasOnlineSpellErrors())
{
EEControlBits nControl = rEngine.GetControlWord();
const EEControlBits nSpellControl = EEControlBits::ONLINESPELLING | EEControlBits::ALLOWBIGOBJS;
bool bNewControl = ((nControl & nSpellControl) != nSpellControl);
if (bNewControl)
rEngine.SetControlWord(nControl | nSpellControl);
rEngine.SetTextCurrentDefaults(*rOther.mpEditText);
mpEditText = rEngine.CreateTextObject().release();
if (bNewControl)
rEngine.SetControlWord(nControl);
}
else
{
rEngine.SetTextCurrentDefaults(*rOther.mpEditText);
mpEditText = rEngine.CreateTextObject().release();
}
}
break;
case CELLTYPE_VALUE:
mfValue = rOther.mfValue;
break;
case CELLTYPE_FORMULA:
// Switch to the destination document.
mpFormula = new ScFormulaCell(*rOther.mpFormula, rDestDoc, rOther.mpFormula->aPos, nCloneFlags);
break;
default:
meType = CELLTYPE_NONE; // reset to empty.
}
}
void ScCellValue::commit( ScDocument& rDoc, const ScAddress& rPos ) const
{
switch (meType)
{
case CELLTYPE_STRING:
{
ScSetStringParam aParam;
aParam.setTextInput();
rDoc.SetString(rPos, mpString->getString(), &aParam);
}
break;
case CELLTYPE_EDIT:
rDoc.SetEditText(rPos, mpEditText->Clone());
break;
case CELLTYPE_VALUE:
rDoc.SetValue(rPos, mfValue);
break;
case CELLTYPE_FORMULA:
rDoc.SetFormulaCell(rPos, mpFormula->Clone());
break;
default:
rDoc.SetEmptyCell(rPos);
}
}
void ScCellValue::commit( ScColumn& rColumn, SCROW nRow ) const
{
commitToColumn(*this, rColumn, nRow);
}
void ScCellValue::release( ScDocument& rDoc, const ScAddress& rPos )
{
switch (meType)
{
case CELLTYPE_STRING:
{
// Currently, string cannot be placed without copying.
ScSetStringParam aParam;
aParam.setTextInput();
rDoc.SetString(rPos, mpString->getString(), &aParam);
delete mpString;
}
break;
case CELLTYPE_EDIT:
// Cell takes the ownership of the text object.
rDoc.SetEditText(rPos, std::unique_ptr<EditTextObject>(mpEditText));
break;
case CELLTYPE_VALUE:
rDoc.SetValue(rPos, mfValue);
break;
case CELLTYPE_FORMULA:
// This formula cell instance is directly placed in the document without copying.
rDoc.SetFormulaCell(rPos, mpFormula);
break;
default:
rDoc.SetEmptyCell(rPos);
}
meType = CELLTYPE_NONE;
mfValue = 0.0;
}
void ScCellValue::release( ScColumn& rColumn, SCROW nRow, sc::StartListeningType eListenType )
{
switch (meType)
{
case CELLTYPE_STRING:
{
// Currently, string cannot be placed without copying.
rColumn.SetRawString(nRow, *mpString);
delete mpString;
}
break;
case CELLTYPE_EDIT:
// Cell takes the ownership of the text object.
rColumn.SetEditText(nRow, std::unique_ptr<EditTextObject>(mpEditText));
break;
case CELLTYPE_VALUE:
rColumn.SetValue(nRow, mfValue);
break;
case CELLTYPE_FORMULA:
// This formula cell instance is directly placed in the document without copying.
rColumn.SetFormulaCell(nRow, mpFormula, eListenType);
break;
default:
rColumn.DeleteContent(nRow);
}
meType = CELLTYPE_NONE;
mfValue = 0.0;
}
OUString ScCellValue::getString( const ScDocument& rDoc ) const
{
return getStringImpl(*this, &rDoc);
}
bool ScCellValue::isEmpty() const
{
return meType == CELLTYPE_NONE;
}
bool ScCellValue::equalsWithoutFormat( const ScCellValue& r ) const
{
return equalsWithoutFormatImpl(*this, r);
}
ScCellValue& ScCellValue::operator= ( const ScCellValue& r )
{
ScCellValue aTmp(r);
swap(aTmp);
return *this;
}
ScCellValue& ScCellValue::operator=(ScCellValue&& rCell) noexcept
{
clear();
meType = rCell.meType;
mfValue = rCell.mfValue;
switch (rCell.meType)
{
case CELLTYPE_STRING:
mpString = rCell.mpString;
break;
case CELLTYPE_EDIT:
mpEditText = rCell.mpEditText;
break;
case CELLTYPE_FORMULA:
mpFormula = rCell.mpFormula;
break;
default:
;
}
//we don't need to reset mpString/mpEditText/mpFormula if we
//set meType to NONE as the ScCellValue dtor keys off the meType
rCell.meType = CELLTYPE_NONE;
return *this;
}
ScCellValue& ScCellValue::operator= ( const ScRefCellValue& r )
{
ScCellValue aTmp(r);
swap(aTmp);
return *this;
}
void ScCellValue::swap( ScCellValue& r )
{
std::swap(meType, r.meType);
// double is 8 bytes, whereas a pointer may be 4 or 8 bytes depending on
// the platform. Swap by double values.
std::swap(mfValue, r.mfValue);
}
ScRefCellValue::ScRefCellValue() : meType(CELLTYPE_NONE), mfValue(0.0) {}
ScRefCellValue::ScRefCellValue( double fValue ) : meType(CELLTYPE_VALUE), mfValue(fValue) {}
ScRefCellValue::ScRefCellValue( const svl::SharedString* pString ) : meType(CELLTYPE_STRING), mpString(pString) {}
ScRefCellValue::ScRefCellValue( const EditTextObject* pEditText ) : meType(CELLTYPE_EDIT), mpEditText(pEditText) {}
ScRefCellValue::ScRefCellValue( ScFormulaCell* pFormula ) : meType(CELLTYPE_FORMULA), mpFormula(pFormula) {}
ScRefCellValue::ScRefCellValue( ScDocument& rDoc, const ScAddress& rPos )
{
assign( rDoc, rPos);
}
ScRefCellValue::ScRefCellValue( ScDocument& rDoc, const ScAddress& rPos, sc::ColumnBlockPosition& rBlockPos )
{
assign( rDoc, rPos, rBlockPos );
}
void ScRefCellValue::clear()
{
// Reset to empty value.
meType = CELLTYPE_NONE;
mfValue = 0.0;
}
void ScRefCellValue::assign( ScDocument& rDoc, const ScAddress& rPos )
{
*this = rDoc.GetRefCellValue(rPos);
}
void ScRefCellValue::assign( ScDocument& rDoc, const ScAddress& rPos, sc::ColumnBlockPosition& rBlockPos )
{
*this = rDoc.GetRefCellValue(rPos, rBlockPos);
}
void ScRefCellValue::commit( ScDocument& rDoc, const ScAddress& rPos ) const
{
switch (meType)
{
case CELLTYPE_STRING:
{
ScSetStringParam aParam;
aParam.setTextInput();
rDoc.SetString(rPos, mpString->getString(), &aParam);
}
break;
case CELLTYPE_EDIT:
rDoc.SetEditText(rPos, ScEditUtil::Clone(*mpEditText, rDoc));
break;
case CELLTYPE_VALUE:
rDoc.SetValue(rPos, mfValue);
break;
case CELLTYPE_FORMULA:
rDoc.SetFormulaCell(rPos, new ScFormulaCell(*mpFormula, rDoc, rPos));
break;
default:
rDoc.SetEmptyCell(rPos);
}
}
bool ScRefCellValue::hasString() const
{
return hasStringImpl(meType, mpFormula);
}
bool ScRefCellValue::hasNumeric() const
{
return hasNumericImpl(meType, mpFormula);
}
bool ScRefCellValue::hasError() const
{
return meType == CELLTYPE_FORMULA && mpFormula->GetErrCode() != FormulaError::NONE;
}
double ScRefCellValue::getValue()
{
switch (meType)
{
case CELLTYPE_VALUE:
return mfValue;
case CELLTYPE_FORMULA:
return mpFormula->GetValue();
default:
;
}
return 0.0;
}
double ScRefCellValue::getRawValue() const
{
switch (meType)
{
case CELLTYPE_VALUE:
return mfValue;
case CELLTYPE_FORMULA:
return mpFormula->GetRawValue();
default:
;
}
return 0.0;
}
OUString ScRefCellValue::getString( const ScDocument* pDoc ) const
{
return getStringImpl(*this, pDoc);
}
OUString ScRefCellValue::getRawString( const ScDocument& rDoc ) const
{
return getRawStringImpl(*this, rDoc);
}
bool ScRefCellValue::isEmpty() const
{
return meType == CELLTYPE_NONE;
}
bool ScRefCellValue::hasEmptyValue()
{
if (isEmpty())
return true;
if (meType == CELLTYPE_FORMULA)
return mpFormula->IsEmpty();
return false;
}
bool ScRefCellValue::equalsWithoutFormat( const ScRefCellValue& r ) const
{
return equalsWithoutFormatImpl(*this, r);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|