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