LibreOffice Module sw (master)  1
parachangetrackinginfo.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
21 
22 #include <wrong.hxx>
23 #include <com/sun/star/text/TextMarkupType.hpp>
24 #include <osl/diagnose.h>
25 
26 #include <txtfrm.hxx>
27 #include <rootfrm.hxx>
28 #include <ndtxt.hxx>
30 #include <docary.hxx>
31 #include <redline.hxx>
32 
33 #include <algorithm>
34 
35 namespace {
36  void initChangeTrackTextMarkupLists( const SwTextFrame& rTextFrame,
37  std::unique_ptr<SwWrongList>& opChangeTrackInsertionTextMarkupList,
38  std::unique_ptr<SwWrongList>& opChangeTrackDeletionTextMarkupList,
39  std::unique_ptr<SwWrongList>& opChangeTrackFormatChangeTextMarkupList )
40  {
41  opChangeTrackInsertionTextMarkupList.reset( new SwWrongList( WRONGLIST_CHANGETRACKING ) );
42  opChangeTrackDeletionTextMarkupList.reset( new SwWrongList( WRONGLIST_CHANGETRACKING ) );
43  opChangeTrackFormatChangeTextMarkupList.reset( new SwWrongList( WRONGLIST_CHANGETRACKING ) );
44 
45  if (!rTextFrame.GetTextNodeFirst())
46  {
47  OSL_FAIL( "<initChangeTrackTextMarkupLists(..) - missing <SwTextNode> instance!" );
48  return;
49  }
50  // sw_redlinehide: the first node is sufficient - there are only
51  // multiple ones in Hide case and the code below returns early then
52  const SwTextNode& rTextNode(*(rTextFrame.GetTextNodeFirst()));
53 
54  const IDocumentRedlineAccess& rIDocChangeTrack( rTextNode.getIDocumentRedlineAccess() );
55 
56  if (!IDocumentRedlineAccess::IsShowChanges(rIDocChangeTrack.GetRedlineFlags())
57  || rTextFrame.getRootFrame()->IsHideRedlines()
58  || rIDocChangeTrack.GetRedlineTable().empty())
59  {
60  // nothing to do --> empty change track text markup lists.
61  return;
62  }
63 
64  const SwRedlineTable::size_type nIdxOfFirstRedlineForTextNode =
65  rIDocChangeTrack.GetRedlinePos( rTextNode, RedlineType::Any );
66  if ( nIdxOfFirstRedlineForTextNode == SwRedlineTable::npos )
67  {
68  // nothing to do --> empty change track text markup lists.
69  return;
70  }
71 
72  // sw_redlinehide: rely on the Hide early return above & cast
73  // TextFrameIndex to SwIndex directly
74  const sal_Int32 nTextFrameTextStartPos = rTextFrame.IsFollow()
75  ? sal_Int32(rTextFrame.GetOffset())
76  : 0;
77  const sal_Int32 nTextFrameTextEndPos = rTextFrame.HasFollow()
78  ? sal_Int32(rTextFrame.GetFollow()->GetOffset())
79  : rTextFrame.GetText().getLength();
80 
81  // iteration over the redlines which overlap with the text node.
82  const SwRedlineTable& rRedlineTable = rIDocChangeTrack.GetRedlineTable();
83  const SwRedlineTable::size_type nRedlineCount( rRedlineTable.size() );
84  for ( SwRedlineTable::size_type nActRedline = nIdxOfFirstRedlineForTextNode;
85  nActRedline < nRedlineCount;
86  ++nActRedline)
87  {
88  const SwRangeRedline* pActRedline = rRedlineTable[ nActRedline ];
89  if ( pActRedline->Start()->nNode > rTextNode.GetIndex() )
90  {
91  break;
92  }
93 
94  sal_Int32 nTextNodeChangeTrackStart(COMPLETE_STRING);
95  sal_Int32 nTextNodeChangeTrackEnd(COMPLETE_STRING);
96  pActRedline->CalcStartEnd( rTextNode.GetIndex(),
97  nTextNodeChangeTrackStart,
98  nTextNodeChangeTrackEnd );
99  if ( nTextNodeChangeTrackStart > nTextFrameTextEndPos ||
100  nTextNodeChangeTrackEnd < nTextFrameTextStartPos )
101  {
102  // Consider only redlines which overlap with the text frame's text.
103  continue;
104  }
105 
106  SwWrongList* pMarkupList( nullptr );
107  switch ( pActRedline->GetType() )
108  {
109  case RedlineType::Insert:
110  {
111  pMarkupList = opChangeTrackInsertionTextMarkupList.get();
112  }
113  break;
114  case RedlineType::Delete:
115  {
116  pMarkupList = opChangeTrackDeletionTextMarkupList.get();
117  }
118  break;
119  case RedlineType::Format:
120  {
121  pMarkupList = opChangeTrackFormatChangeTextMarkupList.get();
122  }
123  break;
124  default:
125  {
126  // other types are not considered
127  }
128  }
129  if ( pMarkupList )
130  {
131  const sal_Int32 nTextFrameChangeTrackStart =
132  std::max(nTextNodeChangeTrackStart, nTextFrameTextStartPos);
133 
134  const sal_Int32 nTextFrameChangeTrackEnd =
135  std::min(nTextNodeChangeTrackEnd, nTextFrameTextEndPos);
136 
137  pMarkupList->Insert( OUString(), nullptr,
138  nTextFrameChangeTrackStart,
139  nTextFrameChangeTrackEnd - nTextFrameChangeTrackStart,
140  pMarkupList->Count() );
141  }
142  } // eof iteration over the redlines which overlap with the text node
143  }
144 } // eof anonymous namespace
145 
147  : mrTextFrame( rTextFrame )
148 {
149 }
150 
152 {
153  reset();
154 }
155 
157 {
161 }
162 
164 {
165  SwWrongList* pChangeTrackingTextMarkupList = nullptr;
166 
167  if ( mpChangeTrackInsertionTextMarkupList == nullptr )
168  {
169  OSL_ENSURE( mpChangeTrackDeletionTextMarkupList == nullptr,
170  "<SwParaChangeTrackingInfo::getChangeTrackingTextMarkupList(..) - <mpChangeTrackDeletionTextMarkupList> expected to be NULL." );
171  OSL_ENSURE( mpChangeTrackFormatChangeTextMarkupList == nullptr,
172  "<SwParaChangeTrackingInfo::getChangeTrackingTextMarkupList(..) - <mpChangeTrackFormatChangeTextMarkupList> expected to be NULL." );
173  initChangeTrackTextMarkupLists( mrTextFrame,
177  }
178 
179  switch ( nTextMarkupType )
180  {
181  case css::text::TextMarkupType::TRACK_CHANGE_INSERTION:
182  {
183  pChangeTrackingTextMarkupList = mpChangeTrackInsertionTextMarkupList.get();
184  }
185  break;
186  case css::text::TextMarkupType::TRACK_CHANGE_DELETION:
187  {
188  pChangeTrackingTextMarkupList = mpChangeTrackDeletionTextMarkupList.get();
189  }
190  break;
191  case css::text::TextMarkupType::TRACK_CHANGE_FORMATCHANGE:
192  {
193  pChangeTrackingTextMarkupList = mpChangeTrackFormatChangeTextMarkupList.get();
194  }
195  break;
196  default:
197  {
198  OSL_FAIL( "<SwParaChangeTrackingInfo::getChangeTrackingTextMarkupList(..)> - misusage - unexpected text markup type for change tracking." );
199  }
200  }
201 
202  return pChangeTrackingTextMarkupList;
203 }
204 
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwNodeOffset min(const SwNodeOffset &a, const SwNodeOffset &b)
Definition: nodeoffset.hxx:35
Represents the visualization of a paragraph.
Definition: txtfrm.hxx:159
bool IsFollow() const
Definition: flowfrm.hxx:166
const SwWrongList * getChangeTrackingTextMarkupList(const sal_Int32 nTextMarkupType)
SwNodeIndex nNode
Definition: pam.hxx:38
void CalcStartEnd(SwNodeOffset nNdIdx, sal_Int32 &rStart, sal_Int32 &rEnd) const
Calculates the intersection with text node number nNdIdx.
Definition: docredln.cxx:1388
bool HasFollow() const
Definition: flowfrm.hxx:165
std::unique_ptr< SwWrongList > mpChangeTrackFormatChangeTextMarkupList
const OUString & GetText() const
Returns the text portion we want to edit (for inline see underneath)
Definition: txtfrm.cxx:1294
static bool IsShowChanges(const RedlineFlags eM)
size_type size() const
Definition: docary.hxx:268
SwNodeOffset GetIndex() const
Definition: ndindex.hxx:154
SwTextFrame * GetFollow()
Definition: txtfrm.hxx:856
TextFrameIndex GetOffset() const
Definition: txtfrm.hxx:439
SwTextNode * GetTextNodeFirst()
Definition: txtfrm.hxx:458
virtual SwRedlineTable::size_type GetRedlinePos(const SwNode &rNode, RedlineType nType) const =0
vector_type::size_type size_type
Definition: docary.hxx:223
SwParaChangeTrackingInfo(const SwTextFrame &rTextFrame)
std::unique_ptr< SwWrongList > mpChangeTrackInsertionTextMarkupList
const SwPosition * Start() const
Definition: pam.hxx:213
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
RedlineType GetType(sal_uInt16 nPos=0) const
Definition: docredln.cxx:1896
std::unique_ptr< SwWrongList > mpChangeTrackDeletionTextMarkupList
bool IsHideRedlines() const
Replacement for sw::DocumentRedlineManager::GetRedlineFlags() (this is layout-level redline hiding)...
Definition: rootfrm.hxx:422
constexpr sal_Int32 COMPLETE_STRING
Definition: swtypes.hxx:58
static constexpr size_type npos
Definition: docary.hxx:224
SwRootFrame * getRootFrame()
Definition: frame.hxx:680