LibreOffice Module sw (master)  1
list.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 
20 #include <list.hxx>
21 
22 #include <vector>
23 #include <numrule.hxx>
24 #include <ndarr.hxx>
25 #include <node.hxx>
26 #include <pam.hxx>
27 #include <SwNodeNum.hxx>
28 
29 // implementation class for SwList
31 {
32  public:
33  SwListImpl( const OUString& sListId,
34  SwNumRule& rDefaultListStyle,
35  const SwNodes& rNodes );
36  ~SwListImpl() COVERITY_NOEXCEPT_FALSE;
37 
38  const OUString& GetListId() const { return msListId;}
39 
40  const OUString& GetDefaultListStyleName() const { return msDefaultListStyleName;}
41 
42  void InsertListItem( SwNodeNum& rNodeNum, bool isHiddenRedlines,
43  const int nLevel );
44  static void RemoveListItem( SwNodeNum& rNodeNum );
45 
46  void InvalidateListTree();
47  void ValidateListTree();
48 
49  void MarkListLevel( const int nListLevel,
50  const bool bValue );
51 
52  bool IsListLevelMarked( const int nListLevel ) const;
53 
54  // unique identifier of the list
55  const OUString msListId;
56  // default list style for the list items, identified by the list style name
58 
59  // list trees for certain document ranges
61  {
63  std::unique_ptr<SwNodeNum> pRoot;
72  std::unique_ptr<SwNodeNum> pRootRLHidden;
74  std::unique_ptr<SwPaM> pSection;
75  tListTreeForRange(std::unique_ptr<SwNodeNum> p1, std::unique_ptr<SwNodeNum> p2, std::unique_ptr<SwPaM> p3)
76  : pRoot(std::move(p1)), pRootRLHidden(std::move(p2)), pSection(std::move(p3)) {}
77  };
78  typedef std::vector<tListTreeForRange> tListTrees;
79  tListTrees maListTrees;
80 
82 
83  void NotifyItemsOnListLevel( const int nLevel );
84 };
85 
86 SwListImpl::SwListImpl( const OUString& sListId,
87  SwNumRule& rDefaultListStyle,
88  const SwNodes& rNodes )
89  : msListId( sListId ),
90  msDefaultListStyleName( rDefaultListStyle.GetName() ),
91  maListTrees(),
92  mnMarkedListLevel( MAXLEVEL )
93 {
94  // create empty list trees for the document ranges
95  const SwNode* pNode = rNodes[0];
96  do
97  {
98  SwPaM aPam( *pNode, *pNode->EndOfSectionNode() );
99 
100  maListTrees.emplace_back(
101  std::make_unique<SwNodeNum>( &rDefaultListStyle ),
102  std::make_unique<SwNodeNum>( &rDefaultListStyle ),
103  std::make_unique<SwPaM>( *(aPam.Start()), *(aPam.End()) ));
104 
105  pNode = pNode->EndOfSectionNode();
106  if (pNode != &rNodes.GetEndOfContent())
107  {
108  sal_uLong nIndex = pNode->GetIndex();
109  nIndex++;
110  pNode = rNodes[nIndex];
111  }
112  }
113  while ( pNode != &rNodes.GetEndOfContent() );
114 }
115 
116 SwListImpl::~SwListImpl() COVERITY_NOEXCEPT_FALSE
117 {
118  for ( auto& rNumberTree : maListTrees )
119  {
120  SwNodeNum::HandleNumberTreeRootNodeDelete(*(rNumberTree.pRoot));
121  SwNodeNum::HandleNumberTreeRootNodeDelete(*(rNumberTree.pRootRLHidden));
122  }
123 }
124 
125 void SwListImpl::InsertListItem( SwNodeNum& rNodeNum, bool const isHiddenRedlines,
126  const int nLevel )
127 {
128  const SwPosition aPosOfNodeNum( rNodeNum.GetPosition() );
129  const SwNodes* pNodesOfNodeNum = &(aPosOfNodeNum.nNode.GetNode().GetNodes());
130 
131  for ( const auto& rNumberTree : maListTrees )
132  {
133  const SwPosition* pStart = rNumberTree.pSection->Start();
134  const SwPosition* pEnd = rNumberTree.pSection->End();
135  const SwNodes* pRangeNodes = &(pStart->nNode.GetNode().GetNodes());
136 
137  if ( pRangeNodes == pNodesOfNodeNum &&
138  *pStart <= aPosOfNodeNum && aPosOfNodeNum <= *pEnd)
139  {
140  auto const& pRoot(isHiddenRedlines
141  ? rNumberTree.pRootRLHidden
142  : rNumberTree.pRoot);
143  pRoot->AddChild(&rNodeNum, nLevel);
144  break;
145  }
146  }
147 }
148 
150 {
151  rNodeNum.RemoveMe();
152 }
153 
155 {
156  for ( const auto& rNumberTree : maListTrees )
157  {
158  rNumberTree.pRoot->InvalidateTree();
159  rNumberTree.pRootRLHidden->InvalidateTree();
160  }
161 }
162 
164 {
165  for ( auto& rNumberTree : maListTrees )
166  {
167  rNumberTree.pRoot->NotifyInvalidChildren();
168  rNumberTree.pRootRLHidden->NotifyInvalidChildren();
169  }
170 }
171 
172 void SwListImpl::MarkListLevel( const int nListLevel,
173  const bool bValue )
174 {
175  if ( bValue )
176  {
177  if ( nListLevel != mnMarkedListLevel )
178  {
179  if ( mnMarkedListLevel != MAXLEVEL )
180  {
181  // notify former marked list nodes
183  }
184 
185  mnMarkedListLevel = nListLevel;
186 
187  // notify new marked list nodes
189  }
190  }
191  else
192  {
193  if ( mnMarkedListLevel != MAXLEVEL )
194  {
195  // notify former marked list nodes
197  }
198 
200  }
201 }
202 
203 bool SwListImpl::IsListLevelMarked( const int nListLevel ) const
204 {
205  return nListLevel == mnMarkedListLevel;
206 }
207 
208 void SwListImpl::NotifyItemsOnListLevel( const int nLevel )
209 {
210  for ( auto& rNumberTree : maListTrees )
211  {
212  rNumberTree.pRoot->NotifyNodesOnListLevel( nLevel );
213  rNumberTree.pRootRLHidden->NotifyNodesOnListLevel( nLevel );
214  }
215 }
216 
217 SwList::SwList( const OUString& sListId,
218  SwNumRule& rDefaultListStyle,
219  const SwNodes& rNodes )
220  : mpListImpl( new SwListImpl( sListId, rDefaultListStyle, rNodes ) )
221 {
222 }
223 
225 {
226 }
227 
228 const OUString & SwList::GetListId() const
229 {
230  return mpListImpl->GetListId();
231 }
232 
233 const OUString & SwList::GetDefaultListStyleName() const
234 {
235  return mpListImpl->GetDefaultListStyleName();
236 }
237 
238 void SwList::SetDefaultListStyleName(OUString const& rNew)
239 {
240  mpListImpl->msDefaultListStyleName = rNew;
241 }
242 
243 void SwList::InsertListItem( SwNodeNum& rNodeNum, bool const isHiddenRedlines,
244  const int nLevel )
245 {
246  mpListImpl->InsertListItem( rNodeNum, isHiddenRedlines, nLevel );
247 }
248 
250 {
251  SwListImpl::RemoveListItem( rNodeNum );
252 }
253 
255 {
256  mpListImpl->InvalidateListTree();
257 }
258 
260 {
261  mpListImpl->ValidateListTree();
262 }
263 
264 void SwList::MarkListLevel( const int nListLevel,
265  const bool bValue )
266 {
267  mpListImpl->MarkListLevel( nListLevel, bValue );
268 }
269 
270 bool SwList::IsListLevelMarked( const int nListLevel ) const
271 {
272  return mpListImpl->IsListLevelMarked( nListLevel );
273 }
274 
275 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static void RemoveListItem(SwNodeNum &rNodeNum)
Definition: list.cxx:249
const SwEndNode * EndOfSectionNode() const
Definition: node.hxx:682
static void HandleNumberTreeRootNodeDelete(SwNodeNum &rNodeNum)
Definition: SwNodeNum.cxx:290
void NotifyItemsOnListLevel(const int nLevel)
Definition: list.cxx:208
sal_uLong GetIndex() const
Definition: node.hxx:282
void ValidateListTree()
Definition: list.cxx:163
Marks a position in the document model.
Definition: pam.hxx:35
SW_DLLPUBLIC const OUString & GetDefaultListStyleName() const
Definition: list.cxx:233
const OUString & GetListId() const
Definition: list.cxx:228
SwNodeIndex nNode
Definition: pam.hxx:37
std::unique_ptr< SwNodeNum > pRootRLHidden
Tree that is missing those nodes that are merged or hidden by delete redlines; this is only used if t...
Definition: list.cxx:72
const OUString & GetDefaultListStyleName() const
Definition: list.cxx:40
SwList(const OUString &sListId, SwNumRule &rDefaultListStyle, const SwNodes &rNodes)
Definition: list.cxx:217
sal_uIntPtr sal_uLong
std::unique_ptr< SwPaM > pSection
top-level SwNodes section
Definition: list.cxx:74
SwPosition GetPosition() const
Definition: SwNodeNum.cxx:67
~SwListImpl() COVERITY_NOEXCEPT_FALSE
Definition: list.cxx:116
SwNode & GetNode() const
Definition: ndindex.hxx:118
const OUString msListId
Definition: list.cxx:55
OUString msDefaultListStyleName
Definition: list.cxx:57
void SetDefaultListStyleName(OUString const &)
Definition: list.cxx:238
bool IsListLevelMarked(const int nListLevel) const
Definition: list.cxx:203
void ValidateListTree()
Definition: list.cxx:259
void MarkListLevel(const int nListLevel, const bool bValue)
Definition: list.cxx:172
const sal_uInt8 MAXLEVEL
Definition: swtypes.hxx:95
~SwList()
Definition: list.cxx:224
void MarkListLevel(const int nListLevel, const bool bValue)
Definition: list.cxx:264
std::unique_ptr< SwListImpl, o3tl::default_delete< SwListImpl > > mpListImpl
Definition: list.hxx:65
void InsertListItem(SwNodeNum &rNodeNum, bool isHiddenRedlines, const int nLevel)
Definition: list.cxx:243
SwNode & GetEndOfContent() const
Regular ContentSection (i.e. the BodyText).
Definition: ndarr.hxx:164
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
bool IsListLevelMarked(const int nListLevel) const
Definition: list.cxx:270
const OUString & GetListId() const
Definition: list.cxx:38
void InvalidateListTree()
Definition: list.cxx:254
int mnMarkedListLevel
Definition: list.cxx:81
void RemoveMe()
Remove this child from the tree.
SwNodes & GetNodes()
Node is in which nodes-array/doc?
Definition: node.hxx:693
static void RemoveListItem(SwNodeNum &rNodeNum)
Definition: list.cxx:149
SwListImpl(const OUString &sListId, SwNumRule &rDefaultListStyle, const SwNodes &rNodes)
Definition: list.cxx:86
void InsertListItem(SwNodeNum &rNodeNum, bool isHiddenRedlines, const int nLevel)
Definition: list.cxx:125
void InvalidateListTree()
Definition: list.cxx:154
tListTreeForRange(std::unique_ptr< SwNodeNum > p1, std::unique_ptr< SwNodeNum > p2, std::unique_ptr< SwPaM > p3)
Definition: list.cxx:75
std::vector< tListTreeForRange > tListTrees
Definition: list.cxx:78
tListTrees maListTrees
Definition: list.cxx:79
std::unique_ptr< SwNodeNum > pRoot
tree always corresponds to document model
Definition: list.cxx:63
Base class of the Writer document model elements.
Definition: node.hxx:79