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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163 | /* -*- 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 <sal/config.h>
#include <string_view>
#include <tools/gen.hxx>
#include <editeng/protitem.hxx>
#include <cntfrm.hxx>
#include <pagefrm.hxx>
#include <doc.hxx>
#include <IDocumentLayoutAccess.hxx>
#include <docary.hxx>
#include <pam.hxx>
#include <pamtyp.hxx>
#include <txtfrm.hxx>
#include <fmtcntnt.hxx>
#include <frmatr.hxx>
#include <flyfrm.hxx>
#include <fmteiro.hxx>
#include <section.hxx>
#include <sectfrm.hxx>
#include <ndtxt.hxx>
#include <swcrsr.hxx>
#include <IMark.hxx>
#include <DocumentSettingManager.hxx>
#include <hints.hxx>
#include <txatbase.hxx>
#include <xmloff/odffields.hxx>
#include <rtl/ustrbuf.hxx>
#include <editsh.hxx>
// for the dump "MSC-" compiler
static sal_Int32 GetSttOrEnd( bool bCondition, const SwContentNode& rNd )
{
return bCondition ? 0 : rNd.Len();
}
SwPosition::SwPosition( const SwNodeIndex & rNodeIndex, const SwIndex & rContent )
: nNode( rNodeIndex ), nContent( rContent )
{
}
SwPosition::SwPosition( const SwNodeIndex & rNodeIndex )
: nNode( rNodeIndex ), nContent( nNode.GetNode().GetContentNode() )
{
}
SwPosition::SwPosition( const SwNode& rNode )
: nNode( rNode ), nContent( nNode.GetNode().GetContentNode() )
{
}
SwPosition::SwPosition( SwContentNode & rNode, const sal_Int32 nOffset )
: nNode( rNode ), nContent( &rNode, nOffset )
{
}
bool SwPosition::operator<(const SwPosition &rPos) const
{
if( nNode < rPos.nNode )
return true;
if( nNode == rPos.nNode )
{
// note that positions with text node but no SwIndex registered are
// created for text frames anchored at para (see SwXFrame::getAnchor())
SwIndexReg const*const pThisReg(nContent.GetIdxReg());
SwIndexReg const*const pOtherReg(rPos.nContent.GetIdxReg());
if (pThisReg && pOtherReg)
{
return (nContent < rPos.nContent);
}
else // by convention position with no index is smaller
{
return pOtherReg != nullptr;
}
}
return false;
}
bool SwPosition::operator>(const SwPosition &rPos) const
{
if(nNode > rPos.nNode )
return true;
if( nNode == rPos.nNode )
{
// note that positions with text node but no SwIndex registered are
// created for text frames anchored at para (see SwXFrame::getAnchor())
SwIndexReg const*const pThisReg(nContent.GetIdxReg());
SwIndexReg const*const pOtherReg(rPos.nContent.GetIdxReg());
if (pThisReg && pOtherReg)
{
return (nContent > rPos.nContent);
}
else // by convention position with no index is smaller
{
return pThisReg != nullptr;
}
}
return false;
}
bool SwPosition::operator<=(const SwPosition &rPos) const
{
if(nNode < rPos.nNode )
return true;
if( nNode == rPos.nNode )
{
// note that positions with text node but no SwIndex registered are
// created for text frames anchored at para (see SwXFrame::getAnchor())
SwIndexReg const*const pThisReg(nContent.GetIdxReg());
SwIndexReg const*const pOtherReg(rPos.nContent.GetIdxReg());
if (pThisReg && pOtherReg)
{
return (nContent <= rPos.nContent);
}
else // by convention position with no index is smaller
{
return pThisReg == nullptr;
}
}
return false;
}
bool SwPosition::operator>=(const SwPosition &rPos) const
{
if(nNode > rPos.nNode )
return true;
if( nNode == rPos.nNode )
{
// note that positions with text node but no SwIndex registered are
// created for text frames anchored at para (see SwXFrame::getAnchor())
SwIndexReg const*const pThisReg(nContent.GetIdxReg());
SwIndexReg const*const pOtherReg(rPos.nContent.GetIdxReg());
if (pThisReg && pOtherReg)
{
return (nContent >= rPos.nContent);
}
else // by convention position with no index is smaller
{
return pOtherReg == nullptr;
}
}
return false;
}
bool SwPosition::operator==(const SwPosition &rPos) const
{
return (nNode == rPos.nNode)
&& (nContent == rPos.nContent);
}
bool SwPosition::operator!=(const SwPosition &rPos) const
{
return (nNode != rPos.nNode)
|| (nContent != rPos.nContent);
}
SwDoc * SwPosition::GetDoc() const
{
return nNode.GetNode().GetDoc();
}
void SwPosition::dumpAsXml(xmlTextWriterPtr pWriter) const
{
xmlTextWriterStartElement(pWriter, BAD_CAST("SwPosition"));
xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nNode"), BAD_CAST(OString::number(nNode.GetIndex()).getStr()));
xmlTextWriterWriteAttribute(pWriter, BAD_CAST("nContent"), BAD_CAST(OString::number(nContent.GetIndex()).getStr()));
xmlTextWriterEndElement(pWriter);
}
std::ostream &operator <<(std::ostream& s, const SwPosition& position)
{
return s << "SwPosition (node " << position.nNode.GetIndex() << ", offset " << position.nContent.GetIndex() << ")";
}
namespace {
enum CHKSECTION { Chk_Both, Chk_One, Chk_None };
}
static CHKSECTION lcl_TstIdx( sal_uLong nSttIdx, sal_uLong nEndIdx, const SwNode& rEndNd )
{
sal_uLong nStt = rEndNd.StartOfSectionIndex(), nEnd = rEndNd.GetIndex();
CHKSECTION eSec = nStt < nSttIdx && nEnd >= nSttIdx ? Chk_One : Chk_None;
if( nStt < nEndIdx && nEnd >= nEndIdx )
return( eSec == Chk_One ? Chk_Both : Chk_One );
return eSec;
}
static bool lcl_ChkOneRange( CHKSECTION eSec, bool bChkSections,
const SwNode& rBaseEnd, sal_uLong nStt, sal_uLong nEnd )
{
if( eSec != Chk_Both )
return false;
if( !bChkSections )
return true;
// search the surrounding section
const SwNodes& rNds = rBaseEnd.GetNodes();
const SwNode *pTmp, *pNd = rNds[ nStt ];
if( !pNd->IsStartNode() )
pNd = pNd->StartOfSectionNode();
if( pNd == rNds[ nEnd ]->StartOfSectionNode() )
return true; // same StartNode, same section
// already on a base node => error
if( !pNd->StartOfSectionIndex() )
return false;
for (;;)
{
pTmp = pNd->StartOfSectionNode();
if (pTmp->EndOfSectionNode() == &rBaseEnd )
break;
pNd = pTmp;
}
sal_uLong nSttIdx = pNd->GetIndex(), nEndIdx = pNd->EndOfSectionIndex();
return nSttIdx <= nStt && nStt <= nEndIdx &&
nSttIdx <= nEnd && nEnd <= nEndIdx;
}
/** Check if the given range is inside one of the defined top-level sections.
*
* The top-level sections are Content, AutoText, PostIts, Inserts, and Redlines.
*
* @param bChkSection if true, also check that the given range is inside
* a single second-level section inside any of the
* top-level sections, except for the Content section.
*
* @return <true> if valid range
*/
bool CheckNodesRange( const SwNodeIndex& rStt,
const SwNodeIndex& rEnd, bool bChkSection )
{
const SwNodes& rNds = rStt.GetNodes();
sal_uLong nStt = rStt.GetIndex(), nEnd = rEnd.GetIndex();
CHKSECTION eSec = lcl_TstIdx( nStt, nEnd, rNds.GetEndOfContent() );
if( Chk_None != eSec )
return eSec == Chk_Both;
eSec = lcl_TstIdx( nStt, nEnd, rNds.GetEndOfAutotext() );
if( Chk_None != eSec )
return lcl_ChkOneRange( eSec, bChkSection,
rNds.GetEndOfAutotext(), nStt, nEnd );
eSec = lcl_TstIdx( nStt, nEnd, rNds.GetEndOfPostIts() );
if( Chk_None != eSec )
return lcl_ChkOneRange( eSec, bChkSection,
rNds.GetEndOfPostIts(), nStt, nEnd );
eSec = lcl_TstIdx( nStt, nEnd, rNds.GetEndOfInserts() );
if( Chk_None != eSec )
return lcl_ChkOneRange( eSec, bChkSection,
rNds.GetEndOfInserts(), nStt, nEnd );
eSec = lcl_TstIdx( nStt, nEnd, rNds.GetEndOfRedlines() );
if( Chk_None != eSec )
return lcl_ChkOneRange( eSec, bChkSection,
rNds.GetEndOfRedlines(), nStt, nEnd );
return false; // somewhere in between => error
}
bool GoNext(SwNode* pNd, SwIndex * pIdx, sal_uInt16 nMode )
{
if( pNd->IsContentNode() )
return static_cast<SwContentNode*>(pNd)->GoNext( pIdx, nMode );
return false;
}
bool GoPrevious( SwNode* pNd, SwIndex * pIdx, sal_uInt16 nMode )
{
if( pNd->IsContentNode() )
return static_cast<SwContentNode*>(pNd)->GoPrevious( pIdx, nMode );
return false;
}
SwContentNode* GoNextNds( SwNodeIndex* pIdx, bool bChk )
{
SwNodeIndex aIdx( *pIdx );
SwContentNode* pNd = aIdx.GetNodes().GoNext( &aIdx );
if( pNd )
{
if( bChk && 1 != aIdx.GetIndex() - pIdx->GetIndex() &&
!CheckNodesRange( *pIdx, aIdx, true ) )
pNd = nullptr;
else
*pIdx = aIdx;
}
return pNd;
}
SwContentNode* GoPreviousNds( SwNodeIndex * pIdx, bool bChk )
{
SwNodeIndex aIdx( *pIdx );
SwContentNode* pNd = SwNodes::GoPrevious( &aIdx );
if( pNd )
{
if( bChk && 1 != pIdx->GetIndex() - aIdx.GetIndex() &&
!CheckNodesRange( *pIdx, aIdx, true ) )
pNd = nullptr;
else
*pIdx = aIdx;
}
return pNd;
}
SwPaM::SwPaM( const SwPosition& rPos, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rPos )
, m_Bound2( rPos.nNode.GetNode().GetNodes() ) // default initialize
, m_pPoint( &m_Bound1 )
, m_pMark( m_pPoint )
, m_bIsInFrontOfLabel( false )
{
}
SwPaM::SwPaM( const SwPosition& rMark, const SwPosition& rPoint, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rMark )
, m_Bound2( rPoint )
, m_pPoint( &m_Bound2 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
}
SwPaM::SwPaM( const SwNodeIndex& rMark, const SwNodeIndex& rPoint,
long nMarkOffset, long nPointOffset, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rMark )
, m_Bound2( rPoint )
, m_pPoint( &m_Bound2 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
if ( nMarkOffset )
{
m_pMark->nNode += nMarkOffset;
}
if ( nPointOffset )
{
m_pPoint->nNode += nPointOffset;
}
m_Bound1.nContent.Assign( m_Bound1.nNode.GetNode().GetContentNode(), 0 );
m_Bound2.nContent.Assign( m_Bound2.nNode.GetNode().GetContentNode(), 0 );
}
SwPaM::SwPaM( const SwNode& rMark, const SwNode& rPoint,
long nMarkOffset, long nPointOffset, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rMark )
, m_Bound2( rPoint )
, m_pPoint( &m_Bound2 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
if ( nMarkOffset )
{
m_pMark->nNode += nMarkOffset;
}
if ( nPointOffset )
{
m_pPoint->nNode += nPointOffset;
}
m_Bound1.nContent.Assign( m_Bound1.nNode.GetNode().GetContentNode(), 0 );
m_Bound2.nContent.Assign( m_Bound2.nNode.GetNode().GetContentNode(), 0 );
}
SwPaM::SwPaM( const SwNodeIndex& rMark, sal_Int32 nMarkContent,
const SwNodeIndex& rPoint, sal_Int32 nPointContent, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rMark )
, m_Bound2( rPoint )
, m_pPoint( &m_Bound2 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
m_pPoint->nContent.Assign( rPoint.GetNode().GetContentNode(), nPointContent);
m_pMark ->nContent.Assign( rMark .GetNode().GetContentNode(), nMarkContent );
}
SwPaM::SwPaM( const SwNode& rMark, sal_Int32 nMarkContent,
const SwNode& rPoint, sal_Int32 nPointContent, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rMark )
, m_Bound2( rPoint )
, m_pPoint( &m_Bound2 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
m_pPoint->nContent.Assign( m_pPoint->nNode.GetNode().GetContentNode(),
nPointContent);
m_pMark ->nContent.Assign( m_pMark ->nNode.GetNode().GetContentNode(),
nMarkContent );
}
SwPaM::SwPaM( const SwNode& rNode, sal_Int32 nContent, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rNode )
, m_Bound2( m_Bound1.nNode.GetNode().GetNodes() ) // default initialize
, m_pPoint( &m_Bound1 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
m_pPoint->nContent.Assign( m_pPoint->nNode.GetNode().GetContentNode(),
nContent );
}
SwPaM::SwPaM( const SwNodeIndex& rNodeIdx, sal_Int32 nContent, SwPaM* pRing )
: Ring( pRing )
, m_Bound1( rNodeIdx )
, m_Bound2( rNodeIdx.GetNode().GetNodes() ) // default initialize
, m_pPoint( &m_Bound1 )
, m_pMark( &m_Bound1 )
, m_bIsInFrontOfLabel( false )
{
m_pPoint->nContent.Assign( rNodeIdx.GetNode().GetContentNode(), nContent );
}
SwPaM::~SwPaM() {}
SwPaM::SwPaM(SwPaM const& rPam, SwPaM *const pRing)
: Ring(pRing)
, m_Bound1( *(rPam.m_pPoint) )
, m_Bound2( *(rPam.m_pMark) )
, m_pPoint( &m_Bound1 ), m_pMark( rPam.HasMark() ? &m_Bound2 : m_pPoint )
, m_bIsInFrontOfLabel( false )
{
}
// @@@ semantic: no copy assignment for super class Ring.
SwPaM &SwPaM::operator=( const SwPaM &rPam )
{
if(this == &rPam)
return *this;
*m_pPoint = *( rPam.m_pPoint );
if ( rPam.HasMark() )
{
SetMark();
*m_pMark = *( rPam.m_pMark );
}
else
{
DeleteMark();
}
return *this;
}
void SwPaM::SetMark()
{
if (m_pPoint == &m_Bound1)
{
m_pMark = &m_Bound2;
}
else
{
m_pMark = &m_Bound1;
}
(*m_pMark) = *m_pPoint;
}
#ifdef DBG_UTIL
void SwPaM::Exchange()
{
if (m_pPoint != m_pMark)
{
SwPosition *pTmp = m_pPoint;
m_pPoint = m_pMark;
m_pMark = pTmp;
}
}
#endif
/// movement of cursor
bool SwPaM::Move( SwMoveFnCollection const & fnMove, SwGoInDoc fnGo )
{
const bool bRet = (*fnGo)( *this, fnMove );
m_bIsInFrontOfLabel = false;
return bRet;
}
namespace sw {
/** make a new region
Sets the first SwPaM onto the given SwPaM, or to the beginning or end of a
document. SPoint stays at its position, GetMark will be changed respectively.
@param fnMove Contains information if beginning or end of document.
@param pOrigRg The given region.
@return Newly created range, in Ring with parameter pOrigRg.
*/
std::unique_ptr<SwPaM> MakeRegion(SwMoveFnCollection const & fnMove,
const SwPaM & rOrigRg)
{
std::unique_ptr<SwPaM> pPam;
{
pPam.reset(new SwPaM(rOrigRg, const_cast<SwPaM*>(&rOrigRg))); // given search range
// make sure that SPoint is on the "real" start position
// FORWARD: SPoint always smaller than GetMark
// BACKWARD: SPoint always bigger than GetMark
if( (pPam->GetMark()->*fnMove.fnCmpOp)( *pPam->GetPoint() ) )
pPam->Exchange();
}
return pPam;
}
} // namespace sw
void SwPaM::Normalize(bool bPointFirst)
{
if (HasMark())
if ( ( bPointFirst && *m_pPoint > *m_pMark) ||
(!bPointFirst && *m_pPoint < *m_pMark) )
{
Exchange();
}
}
/// return page number at cursor (for reader and page bound frames)
sal_uInt16 SwPaM::GetPageNum( bool bAtPoint, const Point* pLayPos )
{
const SwContentFrame* pCFrame;
const SwPageFrame *pPg;
const SwContentNode *pNd ;
const SwPosition* pPos = bAtPoint ? m_pPoint : m_pMark;
std::pair<Point, bool> tmp;
if (pLayPos)
{
tmp.first = *pLayPos;
tmp.second = false;
}
if( nullptr != ( pNd = pPos->nNode.GetNode().GetContentNode() ) &&
nullptr != (pCFrame = pNd->getLayoutFrame(pNd->GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout(), pPos, pLayPos ? &tmp : nullptr)) &&
nullptr != ( pPg = pCFrame->FindPageFrame() ))
return pPg->GetPhyPageNum();
return 0;
}
// form view - see also SwCursorShell::IsCursorReadonly()
static const SwFrame* lcl_FindEditInReadonlyFrame( const SwFrame& rFrame )
{
const SwFrame* pRet = nullptr;
const SwFlyFrame* pFly;
const SwSectionFrame* pSectionFrame;
if( rFrame.IsInFly() &&
(pFly = rFrame.FindFlyFrame())->GetFormat()->GetEditInReadonly().GetValue() &&
pFly->Lower() &&
!pFly->Lower()->IsNoTextFrame() )
{
pRet = pFly;
}
else if ( rFrame.IsInSct() &&
nullptr != ( pSectionFrame = rFrame.FindSctFrame() )->GetSection() &&
pSectionFrame->GetSection()->IsEditInReadonlyFlag() )
{
pRet = pSectionFrame;
}
return pRet;
}
/// is in protected section or selection surrounds something protected
bool SwPaM::HasReadonlySel( bool bFormView ) const
{
bool bRet = false;
const SwContentNode* pNd = GetPoint()->nNode.GetNode().GetContentNode();
const SwContentFrame *pFrame = nullptr;
if ( pNd != nullptr )
{
Point aTmpPt;
std::pair<Point, bool> const tmp(aTmpPt, false);
pFrame = pNd->getLayoutFrame(
pNd->GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout(),
GetPoint(), &tmp);
}
// Will be set if point are inside edit-in-readonly environment
const SwFrame* pPointEditInReadonlyFrame = nullptr;
if ( pFrame != nullptr
&& ( pFrame->IsProtected()
|| ( bFormView
&& nullptr == ( pPointEditInReadonlyFrame = lcl_FindEditInReadonlyFrame( *pFrame ) ) ) ) )
{
bRet = true;
}
else if( pNd != nullptr )
{
const SwSectionNode* pSNd = pNd->GetSectionNode();
if ( pSNd != nullptr
&& ( pSNd->GetSection().IsProtectFlag()
|| ( bFormView
&& !pSNd->GetSection().IsEditInReadonlyFlag()) ) )
{
bRet = true;
}
else
{
const SwSectionNode* pParentSectionNd = pNd->FindSectionNode();
if ( pParentSectionNd != nullptr
&& ( pParentSectionNd->GetSection().IsProtectFlag()
|| ( bFormView && !pParentSectionNd->GetSection().IsEditInReadonlyFlag()) ) )
{
bRet = true;
}
}
}
if ( !bRet
&& HasMark()
&& GetPoint()->nNode != GetMark()->nNode )
{
pNd = GetMark()->nNode.GetNode().GetContentNode();
pFrame = nullptr;
if ( pNd != nullptr )
{
Point aTmpPt;
std::pair<Point, bool> const tmp(aTmpPt, false);
pFrame = pNd->getLayoutFrame(
pNd->GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout(),
GetMark(), &tmp);
}
const SwFrame* pMarkEditInReadonlyFrame = nullptr;
if ( pFrame != nullptr
&& ( pFrame->IsProtected()
|| ( bFormView
&& nullptr == ( pMarkEditInReadonlyFrame = lcl_FindEditInReadonlyFrame( *pFrame ) ) ) ) )
{
bRet = true;
}
else if( pNd != nullptr )
{
const SwSectionNode* pSNd = pNd->GetSectionNode();
if ( pSNd != nullptr
&& ( pSNd->GetSection().IsProtectFlag()
|| ( bFormView
&& !pSNd->GetSection().IsEditInReadonlyFlag()) ) )
{
bRet = true;
}
}
if ( !bRet && bFormView )
{
// Check if start and end frame are inside the _same_
// edit-in-readonly-environment. Otherwise we better return 'true'
if ( pPointEditInReadonlyFrame != pMarkEditInReadonlyFrame )
bRet = true;
}
// check for protected section inside the selection
if( !bRet )
{
sal_uLong nSttIdx = GetMark()->nNode.GetIndex(),
nEndIdx = GetPoint()->nNode.GetIndex();
if( nEndIdx <= nSttIdx )
{
sal_uLong nTmp = nSttIdx;
nSttIdx = nEndIdx;
nEndIdx = nTmp;
}
// If a protected section should be between nodes, then the
// selection needs to contain already x nodes.
// (TextNd, SectNd, TextNd, EndNd, TextNd )
if( nSttIdx + 3 < nEndIdx )
{
const SwSectionFormats& rFormats = GetDoc()->GetSections();
for( SwSectionFormats::size_type n = rFormats.size(); n; )
{
const SwSectionFormat* pFormat = rFormats[ --n ];
if( pFormat->GetProtect().IsContentProtected() )
{
const SwFormatContent& rContent = pFormat->GetContent(false);
OSL_ENSURE( rContent.GetContentIdx(), "where is the SectionNode?" );
sal_uLong nIdx = rContent.GetContentIdx()->GetIndex();
if( nSttIdx <= nIdx && nEndIdx >= nIdx &&
rContent.GetContentIdx()->GetNode().GetNodes().IsDocNodes() )
{
bRet = true;
break;
}
}
}
}
}
}
const SwDoc *pDoc = GetDoc();
// Legacy text/combo/checkbox: never return read-only when inside these form fields.
const IDocumentMarkAccess* pMarksAccess = pDoc->getIDocumentMarkAccess();
sw::mark::IFieldmark* pA = GetPoint() ? pMarksAccess->getFieldmarkFor( *GetPoint( ) ) : nullptr;
sw::mark::IFieldmark* pB = GetMark() ? pMarksAccess->getFieldmarkFor( *GetMark( ) ) : pA;
// prevent the user from accidentally deleting the field itself when modifying the text.
const bool bAtStartA = (pA != nullptr) && (pA->GetMarkStart() == *GetPoint());
const bool bAtStartB = (pB != nullptr) && (pB->GetMarkStart() == *GetMark());
if (!bRet)
{
bool bUnhandledMark = pA && pA->GetFieldname( ) == ODF_UNHANDLED;
// Unhandled fieldmarks case shouldn't be edited manually to avoid breaking anything
if ( ( pA == pB ) && bUnhandledMark )
bRet = true;
else
{
if ((pA == pB) && (bAtStartA != bAtStartB))
bRet = true;
else if (pA != pB)
{
// If both points are either outside or at marks edges (i.e. selection either
// touches fields, or fully encloses it), then don't disable editing
bRet = !( ( !pA || bAtStartA ) && ( !pB || bAtStartB ) );
}
if( !bRet && pDoc->GetDocumentSettingManager().get( DocumentSettingId::PROTECT_FORM ) && (pA || pB) )
{
// Form protection case
bRet = ( pA == nullptr ) || ( pB == nullptr ) || bAtStartA || bAtStartB;
}
}
}
else
{
// Allow editing when the cursor/selection is fully inside of a legacy form field.
bRet = !( pA != nullptr && !bAtStartA && !bAtStartB && pA == pB );
}
if (!bRet)
{
// Paragraph Signatures and Classification fields are read-only.
if (pDoc && pDoc->GetEditShell())
bRet = pDoc->GetEditShell()->IsCursorInParagraphMetadataField();
}
if (!bRet &&
pDoc->getIDocumentSettingAccess().get(DocumentSettingId::PROTECT_BOOKMARKS))
{
if (pDoc->getIDocumentMarkAccess()->isBookmarkDeleted(*this))
{
return true;
}
}
if (!bRet &&
pDoc->getIDocumentSettingAccess().get(DocumentSettingId::PROTECT_FIELDS))
{
SwPosition const& rStart(*Start());
SwPosition const& rEnd(*End());
for (SwNodeIndex n = rStart.nNode; n <= rEnd.nNode; ++n)
{
if (SwTextNode const*const pNode = n.GetNode().GetTextNode())
{
if (SwpHints const*const pHints = pNode->GetpSwpHints())
{
for (size_t i = 0; i < pHints->Count(); ++i)
{
SwTextAttr const*const pHint(pHints->Get(i));
if (n == rStart.nNode && pHint->GetStart() < rStart.nContent.GetIndex())
{
continue; // before selection
}
if (n == rEnd.nNode && rEnd.nContent.GetIndex() <= pHint->GetStart())
{
break; // after selection
}
if (pHint->Which() == RES_TXTATR_FIELD
// placeholders don't work if you can't delete them
&& pHint->GetFormatField().GetField()->GetTyp()->Which() != SwFieldIds::JumpEdit)
{
return true;
}
}
}
}
}
}
return bRet;
}
/// This function returns the next node in direction of search. If there is no
/// left or the next is out of the area, then a null-pointer is returned.
/// @param rbFirst If <true> then first time request. If so than the position of
/// the PaM must not be changed!
SwContentNode* GetNode( SwPaM & rPam, bool& rbFirst, SwMoveFnCollection const & fnMove,
bool const bInReadOnly, SwRootFrame const*const i_pLayout)
{
SwRootFrame const*const pLayout(i_pLayout ? i_pLayout :
rPam.GetDoc()->getIDocumentLayoutAccess().GetCurrentLayout());
SwContentNode * pNd = nullptr;
if( ((*rPam.GetPoint()).*fnMove.fnCmpOp)( *rPam.GetMark() ) ||
( *rPam.GetPoint() == *rPam.GetMark() && rbFirst ) )
{
if( rbFirst )
{
rbFirst = false;
pNd = rPam.GetContentNode();
if( pNd )
{
SwContentFrame const*const pFrame(pNd->getLayoutFrame(pLayout));
if(
(
nullptr == pFrame ||
( !bInReadOnly && pFrame->IsProtected() ) ||
(pFrame->IsTextFrame() && static_cast<SwTextFrame const*>(pFrame)->IsHiddenNow())
) ||
( !bInReadOnly && pNd->FindSectionNode() &&
pNd->FindSectionNode()->GetSection().IsProtect()
)
)
{
pNd = nullptr;
}
}
}
if( !pNd ) // is the cursor not on a ContentNode?
{
SwPosition aPos( *rPam.GetPoint() );
bool bSrchForward = &fnMove == &fnMoveForward;
SwNodes& rNodes = aPos.nNode.GetNodes();
// go to next/previous ContentNode
while( true )
{
if (i_pLayout && aPos.nNode.GetNode().IsTextNode())
{
auto const fal(sw::GetFirstAndLastNode(*pLayout, aPos.nNode));
aPos.nNode = bSrchForward ? *fal.second : *fal.first;
}
pNd = bSrchForward
? rNodes.GoNextSection( &aPos.nNode, true, !bInReadOnly )
: SwNodes::GoPrevSection( &aPos.nNode, true, !bInReadOnly );
if( pNd )
{
aPos.nContent.Assign( pNd, ::GetSttOrEnd( bSrchForward,*pNd ));
// is the position still in the area
if( (aPos.*fnMove.fnCmpOp)( *rPam.GetMark() ) )
{
// only in AutoTextSection can be nodes that are hidden
SwContentFrame const*const pFrame(pNd->getLayoutFrame(pLayout));
if (nullptr == pFrame ||
( !bInReadOnly && pFrame->IsProtected() ) ||
( pFrame->IsTextFrame() &&
static_cast<SwTextFrame const*>(pFrame)->IsHiddenNow()))
{
pNd = nullptr;
continue;
}
*rPam.GetPoint() = aPos;
}
else
pNd = nullptr; // no valid node
break;
}
break;
}
}
}
return pNd;
}
void GoStartDoc( SwPosition * pPos )<--- The function 'GoStartDoc' is never used.
{
SwNodes& rNodes = pPos->nNode.GetNodes();
pPos->nNode = *rNodes.GetEndOfContent().StartOfSectionNode();
// we always need to find a ContentNode!
SwContentNode* pCNd = rNodes.GoNext( &pPos->nNode );
if( pCNd )
pCNd->MakeStartIndex( &pPos->nContent );
}
void GoEndDoc( SwPosition * pPos )<--- The function 'GoEndDoc' is never used.
{
SwNodes& rNodes = pPos->nNode.GetNodes();
pPos->nNode = rNodes.GetEndOfContent();
SwContentNode* pCNd = GoPreviousNds( &pPos->nNode, true );
if( pCNd )
pCNd->MakeEndIndex( &pPos->nContent );
}
void GoStartSection( SwPosition * pPos )<--- The function 'GoStartSection' is never used.
{
// jump to section's beginning
SwNodes& rNodes = pPos->nNode.GetNodes();
sal_uInt16 nLevel = SwNodes::GetSectionLevel( pPos->nNode );
if( pPos->nNode < rNodes.GetEndOfContent().StartOfSectionIndex() )
nLevel--;
do { SwNodes::GoStartOfSection( &pPos->nNode ); } while( nLevel-- );
// already on a ContentNode
pPos->nNode.GetNode().GetContentNode()->MakeStartIndex( &pPos->nContent );
}
/// go to the end of the current base section
void GoEndSection( SwPosition * pPos )
{
// jump to section's beginning/end
SwNodes& rNodes = pPos->nNode.GetNodes();
sal_uInt16 nLevel = SwNodes::GetSectionLevel( pPos->nNode );
if( pPos->nNode < rNodes.GetEndOfContent().StartOfSectionIndex() )
nLevel--;
do { SwNodes::GoEndOfSection( &pPos->nNode ); } while( nLevel-- );
// now on an EndNode, thus to the previous ContentNode
if( GoPreviousNds( &pPos->nNode, true ) )
pPos->nNode.GetNode().GetContentNode()->MakeEndIndex( &pPos->nContent );
}
bool GoInDoc( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
(*fnMove.fnDoc)( rPam.GetPoint() );
return true;
}
bool GoInSection( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
(*fnMove.fnSections)( rPam.GetPoint() );
return true;
}
bool GoInNode( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
SwContentNode *pNd = (*fnMove.fnNds)( &rPam.GetPoint()->nNode, true );
if( pNd )
rPam.GetPoint()->nContent.Assign( pNd,
::GetSttOrEnd( &fnMove == &fnMoveForward, *pNd ) );
return pNd;
}
bool GoInContent( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
if( (*fnMove.fnNd)( &rPam.GetPoint()->nNode.GetNode(),
&rPam.GetPoint()->nContent, CRSR_SKIP_CHARS ))
return true;
return GoInNode( rPam, fnMove );
}
bool GoInContentCells( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
if( (*fnMove.fnNd)( &rPam.GetPoint()->nNode.GetNode(),
&rPam.GetPoint()->nContent, CRSR_SKIP_CELLS ))
return true;
return GoInNode( rPam, fnMove );
}
bool GoInContentSkipHidden( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
if( (*fnMove.fnNd)( &rPam.GetPoint()->nNode.GetNode(),
&rPam.GetPoint()->nContent, CRSR_SKIP_CHARS | CRSR_SKIP_HIDDEN ) )
return true;
return GoInNode( rPam, fnMove );
}
bool GoInContentCellsSkipHidden( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
if( (*fnMove.fnNd)( &rPam.GetPoint()->nNode.GetNode(),
&rPam.GetPoint()->nContent, CRSR_SKIP_CELLS | CRSR_SKIP_HIDDEN ) )
return true;
return GoInNode( rPam, fnMove );
}
bool GoPrevPara( SwPaM & rPam, SwMoveFnCollection const & aPosPara )
{
if( rPam.Move( fnMoveBackward, GoInNode ) )
{
// always on a ContentNode
SwPosition& rPos = *rPam.GetPoint();
SwContentNode * pNd = rPos.nNode.GetNode().GetContentNode();
rPos.nContent.Assign( pNd,
::GetSttOrEnd( &aPosPara == &fnMoveForward, *pNd ) );
return true;
}
return false;
}
bool GoCurrPara( SwPaM & rPam, SwMoveFnCollection const & aPosPara )
{
SwPosition& rPos = *rPam.GetPoint();
SwContentNode * pNd = rPos.nNode.GetNode().GetContentNode();
if( pNd )
{
const sal_Int32 nOld = rPos.nContent.GetIndex();
const sal_Int32 nNew = &aPosPara == &fnMoveForward ? 0 : pNd->Len();
// if already at beginning/end then to the next/previous
if( nOld != nNew )
{
rPos.nContent.Assign( pNd, nNew );
return true;
}
}
// move node to next/previous ContentNode
if( ( &aPosPara==&fnParaStart && nullptr != ( pNd =
GoPreviousNds( &rPos.nNode, true ))) ||
( &aPosPara==&fnParaEnd && nullptr != ( pNd =
GoNextNds( &rPos.nNode, true ))) )
{
rPos.nContent.Assign( pNd,
::GetSttOrEnd( &aPosPara == &fnMoveForward, *pNd ));
return true;
}
return false;
}
bool GoNextPara( SwPaM & rPam, SwMoveFnCollection const & aPosPara )
{
if( rPam.Move( fnMoveForward, GoInNode ) )
{
// always on a ContentNode
SwPosition& rPos = *rPam.GetPoint();
SwContentNode * pNd = rPos.nNode.GetNode().GetContentNode();
rPos.nContent.Assign( pNd,
::GetSttOrEnd( &aPosPara == &fnMoveForward, *pNd ) );
return true;
}
return false;
}
bool GoCurrSection( SwPaM & rPam, SwMoveFnCollection const & fnMove )
{
SwPosition& rPos = *rPam.GetPoint();
SwPosition aSavePos( rPos ); // position for comparison
(fnMove.fnSection)( &rPos.nNode );
SwContentNode *pNd;
if( nullptr == ( pNd = rPos.nNode.GetNode().GetContentNode()) &&
nullptr == ( pNd = (*fnMove.fnNds)( &rPos.nNode, true )) )
{
rPos = aSavePos; // do not change cursor
return false;
}
rPos.nContent.Assign( pNd,
::GetSttOrEnd( &fnMove == &fnMoveForward, *pNd ) );
return aSavePos != rPos;
}
OUString SwPaM::GetText() const
{
OUStringBuffer aResult;
SwNodeIndex aNodeIndex = Start()->nNode;
// The first node can be already the end node.
// Use a "forever" loop with an exit condition in the middle
// of its body, in order to correctly handle all cases.
bool bIsStartNode = true;
for (;;)
{
const bool bIsEndNode = aNodeIndex == End()->nNode;
SwTextNode * pTextNode = aNodeIndex.GetNode().GetTextNode();
if (pTextNode != nullptr)
{
if (!bIsStartNode)
{
aResult.append(CH_TXTATR_NEWLINE); // use newline for para break
}
const OUString& aTmpStr = pTextNode->GetText();
if (bIsStartNode || bIsEndNode)
{
// Handle corner cases of start/end node(s)
const sal_Int32 nStart = bIsStartNode
? Start()->nContent.GetIndex()
: 0;
const sal_Int32 nEnd = bIsEndNode
? End()->nContent.GetIndex()
: aTmpStr.getLength();
aResult.append(std::u16string_view(aTmpStr).substr(nStart, nEnd-nStart));
}
else
{
aResult.append(aTmpStr);
}
}
if (bIsEndNode)
{
break;
}
++aNodeIndex;
bIsStartNode = false;
}
return aResult.makeStringAndClear();
}
void SwPaM::InvalidatePaM()
{
const SwNode &_pNd = GetNode();
const SwTextNode *_pTextNd = _pNd.GetTextNode();
if (_pTextNd != nullptr)
{
// pretend that the PaM marks inserted text to recalc the portion...
SwInsText aHint( Start()->nContent.GetIndex(),
End()->nContent.GetIndex() - Start()->nContent.GetIndex() + 1 );
SwModify *_pModify=const_cast<SwModify*>(static_cast<SwModify const *>(_pTextNd));
_pModify->ModifyNotification( nullptr, &aHint);
}
}
void SwPaM::dumpAsXml(xmlTextWriterPtr pWriter) const
{
xmlTextWriterStartElement(pWriter, BAD_CAST("SwPaM"));
xmlTextWriterStartElement(pWriter, BAD_CAST("point"));
GetPoint()->dumpAsXml(pWriter);
xmlTextWriterEndElement(pWriter);
if (HasMark())
{
xmlTextWriterStartElement(pWriter, BAD_CAST("mark"));
GetMark()->dumpAsXml(pWriter);
xmlTextWriterEndElement(pWriter);
}
xmlTextWriterEndElement(pWriter);
}
std::ostream &operator <<(std::ostream& s, const SwPaM& pam)
{
if( pam.HasMark())
return s << "SwPaM (point " << *pam.GetPoint() << ", mark " << *pam.GetMark() << ")";
else
return s << "SwPaM (point " << *pam.GetPoint() << ")";
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|