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