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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
|
/* -*- 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <memory>
#include <markdata.hxx>
#include <markarr.hxx>
#include <markmulti.hxx>
#include <rangelst.hxx>
#include <segmenttree.hxx>
#include <sheetlimits.hxx>
#include <document.hxx>
#include <columnspanset.hxx>
#include <fstalgorithm.hxx>
#include <unordered_map>
#include <osl/diagnose.h>
#include <mdds/flat_segment_tree.hpp>
#include <cassert>
ScMarkData::ScMarkData(const ScSheetLimits& rSheetLimits) :
aMultiSel(rSheetLimits),
mrSheetLimits(rSheetLimits)
{
ResetMark();
}
ScMarkData& ScMarkData::operator=(const ScMarkData& rOther)
{
maTabMarked = rOther.maTabMarked;
aMarkRange = rOther.aMarkRange;
aMultiRange = rOther.aMultiRange;
aMultiSel = rOther.aMultiSel;
aTopEnvelope = rOther.aTopEnvelope;
aBottomEnvelope = rOther.aBottomEnvelope;
aLeftEnvelope = rOther.aLeftEnvelope;
aRightEnvelope = rOther.aRightEnvelope;
bMarked = rOther.bMarked;
bMultiMarked = rOther.bMultiMarked;
bMarking = rOther.bMarking;
bMarkIsNeg = rOther.bMarkIsNeg;
return *this;
}
ScMarkData& ScMarkData::operator=(ScMarkData&& rOther)
{
maTabMarked = std::move(rOther.maTabMarked);
aMarkRange = std::move(rOther.aMarkRange);
aMultiRange = std::move(rOther.aMultiRange);
aMultiSel = std::move(rOther.aMultiSel);
aTopEnvelope = std::move(rOther.aTopEnvelope);
aBottomEnvelope = std::move(rOther.aBottomEnvelope);
aLeftEnvelope = std::move(rOther.aLeftEnvelope);
aRightEnvelope = std::move(rOther.aRightEnvelope);
bMarked = rOther.bMarked;
bMultiMarked = rOther.bMultiMarked;
bMarking = rOther.bMarking;
bMarkIsNeg = rOther.bMarkIsNeg;
return *this;
}
void ScMarkData::ResetMark()
{
aMultiSel.Clear();
bMarked = bMultiMarked = false;
bMarking = bMarkIsNeg = false;
aTopEnvelope.RemoveAll();
aBottomEnvelope.RemoveAll();
aLeftEnvelope.RemoveAll();
aRightEnvelope.RemoveAll();
}
void ScMarkData::SetMarkArea( const ScRange& rRange )
{
aMarkRange = rRange;
aMarkRange.PutInOrder();
if ( !bMarked )
{
// Upon creation of a document ScFormatShell GetTextAttrState
// may query (default) attributes although no sheet is marked yet.
// => mark that one.
if ( !GetSelectCount() )
maTabMarked.insert( aMarkRange.aStart.Tab() );
bMarked = true;
}
}
void ScMarkData::SetMultiMarkArea( const ScRange& rRange, bool bMark, bool bSetupMulti )
{
if ( aMultiSel.IsEmpty() )
{
// if simple mark range is set, copy to multi marks
if ( bMarked && !bMarkIsNeg && !bSetupMulti )
{
bMarked = false;
SCCOL nStartCol = aMarkRange.aStart.Col();
SCCOL nEndCol = aMarkRange.aEnd.Col();
PutInOrder( nStartCol, nEndCol );
SetMultiMarkArea( aMarkRange, true, true );
}
}
SCCOL nStartCol = rRange.aStart.Col();
SCROW nStartRow = rRange.aStart.Row();
SCCOL nEndCol = rRange.aEnd.Col();
SCROW nEndRow = rRange.aEnd.Row();
PutInOrder( nStartRow, nEndRow );
PutInOrder( nStartCol, nEndCol );
aMultiSel.SetMarkArea( nStartCol, nEndCol, nStartRow, nEndRow, bMark );
if ( bMultiMarked ) // Update aMultiRange
{
if ( nStartCol < aMultiRange.aStart.Col() )
aMultiRange.aStart.SetCol( nStartCol );
if ( nStartRow < aMultiRange.aStart.Row() )
aMultiRange.aStart.SetRow( nStartRow );
if ( nEndCol > aMultiRange.aEnd.Col() )
aMultiRange.aEnd.SetCol( nEndCol );
if ( nEndRow > aMultiRange.aEnd.Row() )
aMultiRange.aEnd.SetRow( nEndRow );
}
else
{
aMultiRange = rRange; // new
bMultiMarked = true;
}
}
void ScMarkData::SetAreaTab( SCTAB nTab )
{
aMarkRange.aStart.SetTab(nTab);
aMarkRange.aEnd.SetTab(nTab);
aMultiRange.aStart.SetTab(nTab);
aMultiRange.aEnd.SetTab(nTab);
}
void ScMarkData::SelectTable( SCTAB nTab, bool bNew )
{
if ( bNew )
{
maTabMarked.insert( nTab );
}
else
{
maTabMarked.erase( nTab );
}
}
bool ScMarkData::GetTableSelect( SCTAB nTab ) const
{
return (maTabMarked.find( nTab ) != maTabMarked.end());
}
void ScMarkData::SelectOneTable( SCTAB nTab )
{
maTabMarked.clear();
maTabMarked.insert( nTab );
}
SCTAB ScMarkData::GetSelectCount() const
{
return static_cast<SCTAB> ( maTabMarked.size() );
}
SCTAB ScMarkData::GetFirstSelected() const
{
if (!maTabMarked.empty())
return (*maTabMarked.begin());
OSL_FAIL("GetFirstSelected: nothing selected");
return 0;
}
SCTAB ScMarkData::GetLastSelected() const
{
if (!maTabMarked.empty())
return (*maTabMarked.rbegin());
OSL_FAIL("GetLastSelected: nothing selected");
return 0;
}
void ScMarkData::SetSelectedTabs(const MarkedTabsType& rTabs)
{
MarkedTabsType aTabs(rTabs.begin(), rTabs.end());
maTabMarked.swap(aTabs);
}
void ScMarkData::MarkToMulti()
{
if ( bMarked && !bMarking )
{
SetMultiMarkArea( aMarkRange, !bMarkIsNeg );
bMarked = false;
// check if all multi mark ranges have been removed
if ( bMarkIsNeg && !HasAnyMultiMarks() )
ResetMark();
}
}
void ScMarkData::MarkToSimple()
{
if ( bMarking )
return;
if ( bMultiMarked && bMarked )
MarkToMulti(); // may result in bMarked and bMultiMarked reset
if ( !bMultiMarked )
return;
ScRange aNew = aMultiRange;
bool bOk = false;
SCCOL nStartCol = aNew.aStart.Col();
SCCOL nEndCol = aNew.aEnd.Col();
while ( nStartCol < nEndCol && !aMultiSel.HasMarks( nStartCol ) )
++nStartCol;
while ( nStartCol < nEndCol && !aMultiSel.HasMarks( nEndCol ) )
--nEndCol;
// Rows are only taken from MarkArray
SCROW nStartRow, nEndRow;
if ( aMultiSel.HasOneMark( nStartCol, nStartRow, nEndRow ) )
{
bOk = true;
SCROW nCmpStart, nCmpEnd;
for (SCCOL nCol=nStartCol+1; nCol<=nEndCol && bOk; nCol++)
if ( !aMultiSel.HasOneMark( nCol, nCmpStart, nCmpEnd )
|| nCmpStart != nStartRow || nCmpEnd != nEndRow )
bOk = false;
}
if (bOk)
{
aNew.aStart.SetCol(nStartCol);
aNew.aStart.SetRow(nStartRow);
aNew.aEnd.SetCol(nEndCol);
aNew.aEnd.SetRow(nEndRow);
ResetMark();
aMarkRange = aNew;
bMarked = true;
bMarkIsNeg = false;
}
}
bool ScMarkData::IsCellMarked( SCCOL nCol, SCROW nRow, bool bNoSimple ) const
{
if ( bMarked && !bNoSimple && !bMarkIsNeg )
if ( aMarkRange.aStart.Col() <= nCol && aMarkRange.aEnd.Col() >= nCol &&
aMarkRange.aStart.Row() <= nRow && aMarkRange.aEnd.Row() >= nRow )
return true;
if (bMultiMarked)
{
//TODO: test here for negative Marking ?
return aMultiSel.GetMark( nCol, nRow );
}
return false;
}
bool ScMarkData::IsColumnMarked( SCCOL nCol ) const
{
// bMarkIsNeg meanwhile also for columns heads
//TODO: GetMarkColumnRanges for completely marked column
if ( bMarked && !bMarkIsNeg &&
aMarkRange.aStart.Col() <= nCol && aMarkRange.aEnd.Col() >= nCol &&
aMarkRange.aStart.Row() == 0 && aMarkRange.aEnd.Row() == mrSheetLimits.mnMaxRow )
return true;
if ( bMultiMarked && aMultiSel.IsAllMarked( nCol, 0, mrSheetLimits.mnMaxRow ) )
return true;
return false;
}
bool ScMarkData::IsRowMarked( SCROW nRow ) const
{
// bMarkIsNeg meanwhile also for row heads
//TODO: GetMarkRowRanges for completely marked rows
if ( bMarked && !bMarkIsNeg &&
aMarkRange.aStart.Col() == 0 && aMarkRange.aEnd.Col() == mrSheetLimits.mnMaxCol &&
aMarkRange.aStart.Row() <= nRow && aMarkRange.aEnd.Row() >= nRow )
return true;
if ( bMultiMarked )
return aMultiSel.IsRowMarked( nRow );
return false;
}
void ScMarkData::MarkFromRangeList( const ScRangeList& rList, bool bReset )
{
if (bReset)
{
maTabMarked.clear();
ResetMark();
}
size_t nCount = rList.size();
if ( nCount == 1 && !bMarked && !bMultiMarked )
{
const ScRange& rRange = rList[ 0 ];
SetMarkArea( rRange );
SelectTable( rRange.aStart.Tab(), true );
}
else
{
for (size_t i=0; i < nCount; i++)
{
const ScRange& rRange = rList[ i ];
SetMultiMarkArea( rRange );
SelectTable( rRange.aStart.Tab(), true );
}
}
}
/**
Optimise the case of constructing from a range list, speeds up import.
*/
ScMarkData::ScMarkData(const ScSheetLimits& rLimits, const ScRangeList& rList)
: aMultiSel(rLimits),
mrSheetLimits(rLimits)
{
ResetMark();
for (const ScRange& rRange : rList)
maTabMarked.insert( rRange.aStart.Tab() );
if (rList.size() > 1)
{
bMultiMarked = true;
aMultiRange = rList.Combine();
aMultiSel.Set( rList );
}
else if (rList.size() == 1)
{
const ScRange& rRange = rList[ 0 ];
SetMarkArea( rRange );
}
}
void ScMarkData::FillRangeListWithMarks( ScRangeList* pList, bool bClear, SCTAB nForTab ) const
{
if (!pList)
return;
if (bClear)
pList->RemoveAll();
//TODO: for multiple selected tables enter multiple ranges !!!
if ( bMultiMarked )
{
SCTAB nTab = (nForTab < 0 ? aMultiRange.aStart.Tab() : nForTab);
SCCOL nStartCol = aMultiRange.aStart.Col();
SCCOL nEndCol = aMultiRange.aEnd.Col();
for (SCCOL nCol=nStartCol; nCol<=nEndCol; nCol++)
{
if (aMultiSel.HasMarks( nCol ))
{
// Feeding column-wise fragments to ScRangeList::Join() is a
// huge bottleneck, speed this up for multiple columns
// consisting of identical row sets by building a column span
// first. This is usually the case for filtered data, for
// example.
SCCOL nToCol = nCol+1;
for ( ; nToCol <= nEndCol; ++nToCol)
{
if (!aMultiSel.HasEqualRowsMarked(nCol, nToCol))
break;
}
--nToCol;
ScRange aRange( nCol, 0, nTab, nToCol, 0, nTab );
SCROW nTop, nBottom;
ScMultiSelIter aMultiIter( aMultiSel, nCol );
while ( aMultiIter.Next( nTop, nBottom ) )
{
aRange.aStart.SetRow( nTop );
aRange.aEnd.SetRow( nBottom );
pList->Join( aRange );
}
nCol = nToCol;
}
}
}
if ( bMarked )
{
if (nForTab < 0)
pList->push_back( aMarkRange );
else
{
ScRange aRange( aMarkRange );
aRange.aStart.SetTab( nForTab );
aRange.aEnd.SetTab( nForTab );
pList->push_back( aRange );
}
}
}
void ScMarkData::ExtendRangeListTables( ScRangeList* pList ) const
{
if (!pList)
return;
ScRangeList aOldList(*pList);
pList->RemoveAll(); //TODO: or skip the existing below
for (const auto& rTab : maTabMarked)
for ( size_t i=0, nCount = aOldList.size(); i<nCount; i++)
{
ScRange aRange = aOldList[ i ];
aRange.aStart.SetTab(rTab);
aRange.aEnd.SetTab(rTab);
pList->push_back( aRange );
}
}
ScRangeList ScMarkData::GetMarkedRanges() const
{
ScRangeList aRet;
FillRangeListWithMarks(&aRet, false);
return aRet;
}
ScRangeList ScMarkData::GetMarkedRangesForTab( SCTAB nTab ) const
{
ScRangeList aRet;
FillRangeListWithMarks(&aRet, false, nTab);
return aRet;
}
std::vector<sc::ColRowSpan> ScMarkData::GetMarkedRowSpans() const
{
typedef mdds::flat_segment_tree<SCCOLROW, bool> SpansType;
ScRangeList aRanges = GetMarkedRanges();
SpansType aSpans(0, mrSheetLimits.mnMaxRow+1, false);
SpansType::const_iterator itPos = aSpans.begin();
for (size_t i = 0, n = aRanges.size(); i < n; ++i)
{
const ScRange& r = aRanges[i];
itPos = aSpans.insert(itPos, r.aStart.Row(), r.aEnd.Row()+1, true).first;
}
return sc::toSpanArray<SCCOLROW,sc::ColRowSpan>(aSpans);
}
std::vector<sc::ColRowSpan> ScMarkData::GetMarkedColSpans() const
{
if (bMultiMarked)
{
SCCOL nStartCol = aMultiRange.aStart.Col();
SCCOL nEndCol = aMultiRange.aEnd.Col();
if (bMarked)
{
// Use segment tree to merge marked with multi marked.
typedef mdds::flat_segment_tree<SCCOLROW, bool> SpansType;
SpansType aSpans(0, mrSheetLimits.mnMaxCol+1, false);
SpansType::const_iterator itPos = aSpans.begin();
do
{
if (aMultiSel.GetRowSelArray().HasMarks())
{
itPos = aSpans.insert(itPos, nStartCol, nEndCol+1, true).first;
break; // do; all columns marked
}
/* XXX if it turns out that span insert is too slow for lots of
* subsequent columns we could gather each span first and then
* insert. */
for (SCCOL nCol = nStartCol; nCol <= nEndCol; ++nCol)
{
const ScMarkArray* pMultiArray = aMultiSel.GetMultiSelArray( nCol );
if (pMultiArray && pMultiArray->HasMarks())
itPos = aSpans.insert(itPos, nCol, nCol+1, true).first;
}
}
while(false);
// Merge marked.
aSpans.insert(itPos, aMarkRange.aStart.Col(), aMarkRange.aEnd.Col()+1, true);
return sc::toSpanArray<SCCOLROW,sc::ColRowSpan>(aSpans);
}
else
{
// A plain vector is sufficient, avoid segment tree and conversion
// to vector overhead.
std::vector<sc::ColRowSpan> aVec;
if (aMultiSel.GetRowSelArray().HasMarks())
{
aVec.emplace_back( nStartCol, nEndCol);
return aVec; // all columns marked
}
sc::ColRowSpan aSpan( -1, -1);
for (SCCOL nCol = nStartCol; nCol <= nEndCol; ++nCol)
{
const ScMarkArray* pMultiArray = aMultiSel.GetMultiSelArray( nCol );
if (pMultiArray && pMultiArray->HasMarks())
{
if (aSpan.mnStart == -1)
aSpan.mnStart = nCol;
aSpan.mnEnd = nCol;
}
else
{
// Add span gathered so far, if any.
if (aSpan.mnStart != -1)
{
aVec.push_back( aSpan);
aSpan.mnStart = -1;
}
}
}
// Add last span, if any.
if (aSpan.mnStart != -1)
aVec.push_back( aSpan);
return aVec;
}
}
// Only reached if not multi marked.
std::vector<sc::ColRowSpan> aVec;
if (bMarked)
{
aVec.emplace_back( aMarkRange.aStart.Col(), aMarkRange.aEnd.Col());
}
return aVec;
}
bool ScMarkData::IsAllMarked( const ScRange& rRange ) const
{
SCCOL nStartCol = rRange.aStart.Col();
SCROW nStartRow = rRange.aStart.Row();
SCCOL nEndCol = rRange.aEnd.Col();
SCROW nEndRow = rRange.aEnd.Row();
if ( !bMultiMarked )
{
if ( bMarked && !bMarkIsNeg &&
aMarkRange.aStart.Col() <= nStartCol && aMarkRange.aEnd.Col() >= nEndCol &&
aMarkRange.aStart.Row() <= nStartRow && aMarkRange.aEnd.Row() >= nEndRow )
return true;
return false;
}
bool bOk = true;
if ( nStartCol == 0 && nEndCol == mrSheetLimits.mnMaxCol )
return aMultiSel.IsRowRangeMarked( nStartRow, nEndRow );
for (SCCOL nCol=nStartCol; nCol<=nEndCol && bOk; nCol++)
if ( !aMultiSel.IsAllMarked( nCol, nStartRow, nEndRow ) )
bOk = false;
return bOk;
}
SCCOL ScMarkData::GetStartOfEqualColumns( SCCOL nLastCol, SCCOL nMinCol ) const
{
if( !bMultiMarked )
{
if ( bMarked && !bMarkIsNeg )
{
if( aMarkRange.aEnd.Col() >= nMinCol && aMarkRange.aStart.Col() < nLastCol )
return aMarkRange.aEnd.Col() + 1;
if( aMarkRange.aEnd.Col() >= nLastCol && aMarkRange.aStart.Col() <= nMinCol )
return aMarkRange.aStart.Col();
}
return nMinCol;
}
return aMultiSel.GetStartOfEqualColumns( nLastCol, nMinCol );
}
SCROW ScMarkData::GetNextMarked( SCCOL nCol, SCROW nRow, bool bUp ) const
{
if ( !bMultiMarked )
return nRow;
return aMultiSel.GetNextMarked( nCol, nRow, bUp );
}
bool ScMarkData::HasMultiMarks( SCCOL nCol ) const
{
if ( !bMultiMarked )
return false;
return aMultiSel.HasMarks( nCol );
}
bool ScMarkData::HasAnyMultiMarks() const
{
if ( !bMultiMarked )
return false;
return aMultiSel.HasAnyMarks();
}
void ScMarkData::InsertTab( SCTAB nTab )
{
std::set<SCTAB> tabMarked;
for (const auto& rTab : maTabMarked)
{
if (rTab < nTab)
tabMarked.insert(rTab);
else
tabMarked.insert(rTab + 1);
}
maTabMarked.swap(tabMarked);
}
void ScMarkData::DeleteTab( SCTAB nTab )
{
std::set<SCTAB> tabMarked;
for (const auto& rTab : maTabMarked)
{
if (rTab < nTab)
tabMarked.insert(rTab);
else if (rTab > nTab)
tabMarked.insert(rTab - 1);
}
maTabMarked.swap(tabMarked);
}
void ScMarkData::ShiftCols(const ScDocument& rDoc, SCCOL nStartCol, sal_Int32 nColOffset)
{
if (bMarked)
{
aMarkRange.IncColIfNotLessThan(rDoc, nStartCol, nColOffset);
}
else if (bMultiMarked)
{
aMultiSel.ShiftCols(nStartCol, nColOffset);
aMultiRange.IncColIfNotLessThan(rDoc, nStartCol, nColOffset);
}
}
void ScMarkData::ShiftRows(const ScDocument& rDoc, SCROW nStartRow, sal_Int32 nRowOffset)
{
if (bMarked)
{
aMarkRange.IncRowIfNotLessThan(rDoc, nStartRow, nRowOffset);
}
else if (bMultiMarked)
{
aMultiSel.ShiftRows(nStartRow, nRowOffset);
aMultiRange.IncRowIfNotLessThan(rDoc, nStartRow, nRowOffset);
}
}
static void lcl_AddRanges(ScRange& rRangeDest, const ScRange& rNewRange )
{
SCCOL nStartCol = rNewRange.aStart.Col();
SCROW nStartRow = rNewRange.aStart.Row();
SCCOL nEndCol = rNewRange.aEnd.Col();
SCROW nEndRow = rNewRange.aEnd.Row();
PutInOrder( nStartRow, nEndRow );
PutInOrder( nStartCol, nEndCol );
if ( nStartCol < rRangeDest.aStart.Col() )
rRangeDest.aStart.SetCol( nStartCol );
if ( nStartRow < rRangeDest.aStart.Row() )
rRangeDest.aStart.SetRow( nStartRow );
if ( nEndCol > rRangeDest.aEnd.Col() )
rRangeDest.aEnd.SetCol( nEndCol );
if ( nEndRow > rRangeDest.aEnd.Row() )
rRangeDest.aEnd.SetRow( nEndRow );
}
void ScMarkData::GetSelectionCover( ScRange& rRange )
{
if( bMultiMarked )
{
rRange = aMultiRange;
SCCOL nStartCol = aMultiRange.aStart.Col(), nEndCol = aMultiRange.aEnd.Col();
PutInOrder( nStartCol, nEndCol );
nStartCol = ( nStartCol == 0 ) ? nStartCol : nStartCol - 1;
nEndCol = ( nEndCol == mrSheetLimits.mnMaxCol ) ? nEndCol : nEndCol + 1;
std::unique_ptr<ScFlatBoolRowSegments> pPrevColMarkedRows;
std::unique_ptr<ScFlatBoolRowSegments> pCurColMarkedRows;
std::unordered_map<SCROW,ScFlatBoolColSegments> aRowToColSegmentsInTopEnvelope;
std::unordered_map<SCROW,ScFlatBoolColSegments> aRowToColSegmentsInBottomEnvelope;
ScFlatBoolRowSegments aNoRowsMarked(mrSheetLimits.mnMaxRow);
aNoRowsMarked.setFalse( 0, mrSheetLimits.mnMaxRow );
bool bPrevColUnMarked = false;
for ( SCCOL nCol=nStartCol; nCol <= nEndCol; nCol++ )
{
SCROW nTop, nBottom;
bool bCurColUnMarked = !aMultiSel.HasMarks( nCol );
if ( !bCurColUnMarked )
{
pCurColMarkedRows.reset( new ScFlatBoolRowSegments(mrSheetLimits.mnMaxRow) );
pCurColMarkedRows->setFalse( 0, mrSheetLimits.mnMaxRow );
ScMultiSelIter aMultiIter( aMultiSel, nCol );
ScFlatBoolRowSegments::ForwardIterator aPrevItr(
pPrevColMarkedRows ? *pPrevColMarkedRows
: aNoRowsMarked); // For finding left envelope
ScFlatBoolRowSegments::ForwardIterator aPrevItr1(
pPrevColMarkedRows ? *pPrevColMarkedRows
: aNoRowsMarked); // For finding right envelope
SCROW nTopPrev = 0, nBottomPrev = 0; // For right envelope
while ( aMultiIter.Next( nTop, nBottom ) )
{
pCurColMarkedRows->setTrue( nTop, nBottom );
if( bPrevColUnMarked && ( nCol > nStartCol ))
{
ScRange aAddRange(nCol - 1, nTop, aMultiRange.aStart.Tab(),
nCol - 1, nBottom, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Left envelope
aLeftEnvelope.push_back( aAddRange );
}
else if( nCol > nStartCol )
{
SCROW nTop1 = nTop, nBottom1 = nTop;
while( nTop1 <= nBottom && nBottom1 <= nBottom )
{
bool bRangeMarked = false;
const bool bHasValue = aPrevItr.getValue( nTop1, bRangeMarked );
assert(bHasValue); (void)bHasValue;
if( bRangeMarked )
{
nTop1 = aPrevItr.getLastPos() + 1;
nBottom1 = nTop1;
}
else
{
nBottom1 = aPrevItr.getLastPos();
if( nBottom1 > nBottom )
nBottom1 = nBottom;
ScRange aAddRange( nCol - 1, nTop1, aMultiRange.aStart.Tab(),
nCol - 1, nBottom1, aMultiRange.aStart.Tab() );
lcl_AddRanges( rRange, aAddRange ); // Left envelope
aLeftEnvelope.push_back( aAddRange );
nTop1 = ++nBottom1;
}
}
while( nTopPrev <= nBottom && nBottomPrev <= nBottom )
{
bool bRangeMarked;
const bool bHasValue = aPrevItr1.getValue( nTopPrev, bRangeMarked );
assert(bHasValue); (void)bHasValue;
if( bRangeMarked )
{
nBottomPrev = aPrevItr1.getLastPos();
if( nTopPrev < nTop )
{
if( nBottomPrev >= nTop )
{
nBottomPrev = nTop - 1;
ScRange aAddRange( nCol, nTopPrev, aMultiRange.aStart.Tab(),
nCol, nBottomPrev, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Right envelope
aRightEnvelope.push_back( aAddRange );
nTopPrev = nBottomPrev = (nBottom + 1);
}
else
{
ScRange aAddRange( nCol, nTopPrev, aMultiRange.aStart.Tab(),
nCol, nBottomPrev, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Right envelope
aRightEnvelope.push_back( aAddRange );
nTopPrev = ++nBottomPrev;
}
}
else
nTopPrev = nBottomPrev = ( nBottom + 1 );
}
else
{
nBottomPrev = aPrevItr1.getLastPos();
nTopPrev = ++nBottomPrev;
}
}
}
if( nTop )
{
ScRange aAddRange( nCol, nTop - 1, aMultiRange.aStart.Tab(),
nCol, nTop - 1, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Top envelope
auto it = aRowToColSegmentsInTopEnvelope.find(nTop - 1);
if (it == aRowToColSegmentsInTopEnvelope.end())
it = aRowToColSegmentsInTopEnvelope.emplace(nTop - 1, ScFlatBoolColSegments(mrSheetLimits.mnMaxCol)).first;
it->second.setTrue( nCol, nCol );
}
if( nBottom < mrSheetLimits.mnMaxRow )
{
ScRange aAddRange(nCol, nBottom + 1, aMultiRange.aStart.Tab(),
nCol, nBottom + 1, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Bottom envelope
auto it = aRowToColSegmentsInBottomEnvelope.find(nBottom + 1);
if (it == aRowToColSegmentsInBottomEnvelope.end())
it = aRowToColSegmentsInBottomEnvelope.emplace(nBottom + 1, ScFlatBoolColSegments(mrSheetLimits.mnMaxCol)).first;
it->second.setTrue( nCol, nCol );
}
}
while( nTopPrev <= mrSheetLimits.mnMaxRow && nBottomPrev <= mrSheetLimits.mnMaxRow && ( nCol > nStartCol ) )
{
bool bRangeMarked;
const bool bHasValue = aPrevItr1.getValue( nTopPrev, bRangeMarked );
assert(bHasValue); (void)bHasValue;
if( bRangeMarked )
{
nBottomPrev = aPrevItr1.getLastPos();
ScRange aAddRange(nCol, nTopPrev, aMultiRange.aStart.Tab(),
nCol, nBottomPrev, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Right envelope
aRightEnvelope.push_back( aAddRange );
nTopPrev = ++nBottomPrev;
}
else
{
nBottomPrev = aPrevItr1.getLastPos();
nTopPrev = ++nBottomPrev;
}
}
}
else if( nCol > nStartCol )
{
bPrevColUnMarked = true;
SCROW nTopPrev = 0, nBottomPrev = 0;
bool bRangeMarked = false;
ScFlatBoolRowSegments::ForwardIterator aPrevItr(
pPrevColMarkedRows ? *pPrevColMarkedRows : aNoRowsMarked);
while( nTopPrev <= mrSheetLimits.mnMaxRow && nBottomPrev <= mrSheetLimits.mnMaxRow )
{
const bool bHasValue = aPrevItr.getValue(nTopPrev, bRangeMarked);
assert(bHasValue); (void)bHasValue;
if( bRangeMarked )
{
nBottomPrev = aPrevItr.getLastPos();
ScRange aAddRange(nCol, nTopPrev, aMultiRange.aStart.Tab(),
nCol, nBottomPrev, aMultiRange.aStart.Tab());
lcl_AddRanges( rRange, aAddRange ); // Right envelope
aRightEnvelope.push_back( aAddRange );
nTopPrev = ++nBottomPrev;
}
else
{
nBottomPrev = aPrevItr.getLastPos();
nTopPrev = ++nBottomPrev;
}
}
}
if ( bCurColUnMarked )
pPrevColMarkedRows.reset();
else
pPrevColMarkedRows = std::move( pCurColMarkedRows );
}
for( auto& rKV : aRowToColSegmentsInTopEnvelope )
{
SCCOL nStart = nStartCol;
ScFlatBoolColSegments::RangeData aRange;
while( nStart <= nEndCol )
{
if( !rKV.second.getRangeData( nStart, aRange ) )
break;
if( aRange.mbValue ) // is marked
aTopEnvelope.push_back( ScRange( aRange.mnCol1, rKV.first, aMultiRange.aStart.Tab(),
aRange.mnCol2, rKV.first, aMultiRange.aStart.Tab() ) );
nStart = aRange.mnCol2 + 1;
}
}
for( auto& rKV : aRowToColSegmentsInBottomEnvelope )
{
SCCOL nStart = nStartCol;
ScFlatBoolColSegments::RangeData aRange;
while( nStart <= nEndCol )
{
if( !rKV.second.getRangeData( nStart, aRange ) )
break;
if( aRange.mbValue ) // is marked
aBottomEnvelope.push_back( ScRange( aRange.mnCol1, rKV.first, aMultiRange.aStart.Tab(),
aRange.mnCol2, rKV.first, aMultiRange.aStart.Tab() ) );
nStart = aRange.mnCol2 + 1;
}
}
}
else if( bMarked )
{
aMarkRange.PutInOrder();
SCROW nRow1, nRow2, nRow1New, nRow2New;
SCCOL nCol1, nCol2, nCol1New, nCol2New;
SCTAB nTab1, nTab2;
aMarkRange.GetVars( nCol1, nRow1, nTab1, nCol2, nRow2, nTab2 );
nCol1New = nCol1;
nCol2New = nCol2;
nRow1New = nRow1;
nRow2New = nRow2;
// Each envelope will have zero or more ranges for single rectangle selection.
if( nCol1 > 0 )
{
aLeftEnvelope.push_back( ScRange( nCol1 - 1, nRow1, nTab1, nCol1 - 1, nRow2, nTab2 ) );
--nCol1New;
}
if( nRow1 > 0 )
{
aTopEnvelope.push_back( ScRange( nCol1, nRow1 - 1, nTab1, nCol2, nRow1 - 1, nTab2 ) );
--nRow1New;
}
if( nCol2 < mrSheetLimits.mnMaxCol )
{
aRightEnvelope.push_back( ScRange( nCol2 + 1, nRow1, nTab1, nCol2 + 1, nRow2, nTab2 ) );
++nCol2New;
}
if( nRow2 < mrSheetLimits.mnMaxRow )
{
aBottomEnvelope.push_back( ScRange( nCol1, nRow2 + 1, nTab1, nCol2, nRow2 + 1, nTab2 ) );
++nRow2New;
}
rRange = ScRange( nCol1New, nRow1New, nTab1, nCol2New, nRow2New, nTab2 );
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|