LibreOffice Module sw (master)  1
unnum.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 <UndoNumbering.hxx>
21 #include <doc.hxx>
22 #include <swundo.hxx>
23 #include <pam.hxx>
24 #include <ndtxt.hxx>
25 #include <UndoCore.hxx>
26 #include <rolbck.hxx>
27 #include <osl/diagnose.h>
28 
30  const SwNumRule& rNewRule,
31  const SwDoc* pDoc,
32  SwUndoId nUndoId)
33  : SwUndo( nUndoId, pDoc ),
34  aNumRule( rNewRule ),
35  pOldNumRule( new SwNumRule( rOldRule )), nLRSavePos( 0 )
36 {
37 }
38 
39 SwUndoInsNum::SwUndoInsNum( const SwPaM& rPam, const SwNumRule& rRule )
40  : SwUndo( SwUndoId::INSNUM, rPam.GetDoc() ), SwUndRng( rPam ),
41  aNumRule( rRule ),
42  nLRSavePos( 0 )
43 {
44 }
45 
46 SwUndoInsNum::SwUndoInsNum( const SwPosition& rPos, const SwNumRule& rRule,
47  const OUString& rReplaceRule )
48  : SwUndo( SwUndoId::INSNUM, rPos.nNode.GetNode().GetDoc() ),
49  aNumRule( rRule ),
50  sReplaceRule( rReplaceRule ), nLRSavePos( 0 )
51 {
52  // No selection!
53  m_nEndNode = 0;
55  m_nSttNode = rPos.nNode.GetIndex();
57 }
58 
60 {
61  pHistory.reset();
62  pOldNumRule.reset();
63 }
64 
66 {
67  SwRewriter aResult;
68  if( SwUndoId::INSFMTATTR == GetId() )
69  aResult.AddRule(UndoArg1, aNumRule.GetName());
70  return aResult;
71 }
72 
74 {
75  SwDoc & rDoc = rContext.GetDoc();
76 
77  if( pOldNumRule )
79 
80  if( pHistory )
81  {
82  if( nLRSavePos )
83  {
84  // Update immediately so that potential "old" LRSpaces will be valid again.
85  pHistory->TmpRollback( &rDoc, nLRSavePos );
86 
87  }
88  pHistory->TmpRollback( &rDoc, 0 );
89  pHistory->SetTmpEnd( pHistory->Count() );
90  }
91 
92  if (m_nSttNode)
93  {
94  AddUndoRedoPaM(rContext);
95  }
96 }
97 
99 {
100  SwDoc & rDoc = rContext.GetDoc();
101 
102  if ( pOldNumRule )
103  rDoc.ChgNumRuleFormats( aNumRule );
104  else if ( pHistory )
105  {
106  SwPaM & rPam( AddUndoRedoPaM(rContext) );
107  if( !sReplaceRule.isEmpty() )
108  {
110  }
111  else
112  {
113  // #i42921# - adapt to changed signature
114  rDoc.SetNumRule(rPam, aNumRule, false);
115  }
116  }
117 }
118 
120 {
121  if( pHistory )
122  nLRSavePos = pHistory->Count();
123 }
124 
126 {
127  SwDoc & rDoc( rContext.GetDoc() );
128  if ( m_nSttNode )
129  {
130  if( sReplaceRule.isEmpty() )
131  {
132  // #i42921# - adapt to changed signature
133  rDoc.SetNumRule(rContext.GetRepeatPaM(), aNumRule, false);
134  }
135  }
136  else
137  {
138  rDoc.ChgNumRuleFormats( aNumRule );
139  }
140 }
141 
143 {
144  if( !pHistory )
145  pHistory.reset(new SwHistory);
146  return pHistory.get();
147 }
148 
150 {
151  if( !pOldNumRule )
152  pOldNumRule.reset(new SwNumRule( rOld ));
153 }
154 
156  : SwUndo( SwUndoId::DELNUM, rPam.GetDoc() ), SwUndRng( rPam )
157 {
158  aNodes.reserve( std::min<sal_uLong>(m_nEndNode - m_nSttNode, 255) );
159  pHistory.reset( new SwHistory );
160 }
161 
163 {
164 }
165 
167 {
168  SwDoc & rDoc = rContext.GetDoc();
169 
170  pHistory->TmpRollback( &rDoc, 0 );
171  pHistory->SetTmpEnd( pHistory->Count() );
172 
173  for( const auto& rNode : aNodes )
174  {
175  SwTextNode* pNd = rDoc.GetNodes()[ rNode.index ]->GetTextNode();
176  OSL_ENSURE( pNd, "Where has the TextNode gone?" );
177  pNd->SetAttrListLevel( rNode.level );
178 
179  if( pNd->GetCondFormatColl() )
180  pNd->ChkCondColl();
181  }
182 
183  AddUndoRedoPaM(rContext);
184 }
185 
187 {
188  SwPaM & rPam( AddUndoRedoPaM(rContext) );
189  rContext.GetDoc().DelNumRules(rPam);
190 }
191 
193 {
194  rContext.GetDoc().DelNumRules(rContext.GetRepeatPaM());
195 }
196 
198 {
199  if( rNd.GetNumRule() )
200  {
201  aNodes.emplace_back( rNd.GetIndex(), rNd.GetActualListLevel() );
202  }
203 }
204 
205 SwUndoMoveNum::SwUndoMoveNum( const SwPaM& rPam, long nOff, bool bIsOutlMv )
206  : SwUndo( bIsOutlMv ? SwUndoId::OUTLINE_UD : SwUndoId::MOVENUM, rPam.GetDoc() ),
207  SwUndRng( rPam ),
208  nNewStt( 0 ), nOffset( nOff )
209 {
210  // nOffset: Down => 1
211  // Up => -1
212 }
213 
215 {
216  sal_uLong nTmpStt = m_nSttNode, nTmpEnd = m_nEndNode;
217 
218  if (m_nEndNode || m_nEndContent != COMPLETE_STRING) // section?
219  {
220  if( nNewStt < m_nSttNode ) // moved forwards
222  else
223  m_nEndNode = m_nEndNode + ( nNewStt - m_nSttNode );
224  }
226 
227  SwPaM & rPam( AddUndoRedoPaM(rContext) );
228  rContext.GetDoc().MoveParagraph( rPam, -nOffset,
230  m_nSttNode = nTmpStt;
231  m_nEndNode = nTmpEnd;
232 }
233 
235 {
236  SwPaM & rPam( AddUndoRedoPaM(rContext) );
237  rContext.GetDoc().MoveParagraph(rPam, nOffset, SwUndoId::OUTLINE_UD == GetId());
238 }
239 
241 {
242  SwDoc & rDoc = rContext.GetDoc();
243  if( SwUndoId::OUTLINE_UD == GetId() )
244  {
245  rDoc.MoveOutlinePara(rContext.GetRepeatPaM(),
246  0 < nOffset ? 1 : -1 );
247  }
248  else
249  {
250  rDoc.MoveParagraph(rContext.GetRepeatPaM(), nOffset);
251  }
252 }
253 
254 SwUndoNumUpDown::SwUndoNumUpDown( const SwPaM& rPam, short nOff )
255  : SwUndo( nOff > 0 ? SwUndoId::NUMUP : SwUndoId::NUMDOWN, rPam.GetDoc() ),
256  SwUndRng( rPam ),
257  nOffset( nOff )
258 {
259  // nOffset: Down => 1
260  // Up => -1
261 }
262 
264 {
265  SwPaM & rPam( AddUndoRedoPaM(rContext) );
266  rContext.GetDoc().NumUpDown(rPam, 1 != nOffset );
267 }
268 
270 {
271  SwPaM & rPam( AddUndoRedoPaM(rContext) );
272  rContext.GetDoc().NumUpDown(rPam, 1 == nOffset);
273 }
274 
276 {
277  rContext.GetDoc().NumUpDown(rContext.GetRepeatPaM(), 1 == nOffset);
278 }
279 
281  bool bNewNum)
282  : SwUndo( SwUndoId::NUMORNONUM, rIdx.GetNode().GetDoc() ),
283  nIdx( rIdx.GetIndex() ), mbNewNum(bNewNum),
284  mbOldNum(bOldNum)
285 {
286 }
287 
288 // #115901#, #i40034#
290 {
291  SwNodeIndex aIdx( rContext.GetDoc().GetNodes(), nIdx );
292  SwTextNode * pTextNd = aIdx.GetNode().GetTextNode();
293 
294  if (nullptr != pTextNd)
295  {
296  pTextNd->SetCountedInList(mbOldNum);
297  }
298 }
299 
300 // #115901#, #i40034#
302 {
303  SwNodeIndex aIdx( rContext.GetDoc().GetNodes(), nIdx );
304  SwTextNode * pTextNd = aIdx.GetNode().GetTextNode();
305 
306  if (nullptr != pTextNd)
307  {
308  pTextNd->SetCountedInList(mbNewNum);
309  }
310 }
311 
313 {
314  SwDoc & rDoc = rContext.GetDoc();
315  if (mbOldNum && ! mbNewNum)
316  {
317  rDoc.NumOrNoNum(rContext.GetRepeatPaM().GetPoint()->nNode);
318  }
319  else if ( ! mbOldNum && mbNewNum )
320  {
321  rDoc.NumOrNoNum(rContext.GetRepeatPaM().GetPoint()->nNode, true);
322  }
323 }
324 
326  : SwUndo( SwUndoId::SETNUMRULESTART, rPos.GetDoc() ),
327  nIdx( rPos.nNode.GetIndex() ), nOldStt( USHRT_MAX ),
328  nNewStt( USHRT_MAX ), bSetSttValue( false ), bFlag( bFlg )
329 {
330 }
331 
332 SwUndoNumRuleStart::SwUndoNumRuleStart( const SwPosition& rPos, sal_uInt16 nStt )
333  : SwUndo(SwUndoId::SETNUMRULESTART, rPos.GetDoc())
334  , nIdx(rPos.nNode.GetIndex())
335  , nOldStt(USHRT_MAX)
336  , nNewStt(nStt)
337  , bSetSttValue(true)
338  , bFlag(false)
339 {
340  SwTextNode* pTextNd = rPos.nNode.GetNode().GetTextNode();
341  if ( pTextNd )
342  {
343  if ( pTextNd->HasAttrListRestartValue() )
344  {
345  nOldStt = static_cast<sal_uInt16>(pTextNd->GetAttrListRestartValue());
346  }
347  else
348  {
349  nOldStt = USHRT_MAX; // indicating, that the list restart value is not set
350  }
351  }
352 }
353 
355 {
356  SwDoc & rDoc = rContext.GetDoc();
357  SwPosition const aPos( *rDoc.GetNodes()[ nIdx ] );
358  if( bSetSttValue )
359  {
360  rDoc.SetNodeNumStart( aPos, nOldStt );
361  }
362  else
363  {
364  rDoc.SetNumRuleStart( aPos, !bFlag );
365  }
366 }
367 
369 {
370  SwDoc & rDoc = rContext.GetDoc();
371  SwPosition const aPos( *rDoc.GetNodes()[ nIdx ] );
372  if( bSetSttValue )
373  {
374  rDoc.SetNodeNumStart( aPos, nNewStt );
375  }
376  else
377  {
378  rDoc.SetNumRuleStart( aPos, bFlag );
379  }
380 }
381 
383 {
384  SwDoc & rDoc = rContext.GetDoc();
385  if( bSetSttValue )
386  {
387  rDoc.SetNodeNumStart(*rContext.GetRepeatPaM().GetPoint(), nNewStt);
388  }
389  else
390  {
391  rDoc.SetNumRuleStart(*rContext.GetRepeatPaM().GetPoint(), bFlag);
392  }
393 }
394 
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwPaM & AddUndoRedoPaM(::sw::UndoRedoContext &, bool const bCorrToContent=false) const
Definition: undobj.cxx:105
sal_uLong GetIndex() const
Definition: node.hxx:282
void SaveOldNumRule(const SwNumRule &rOld)
Definition: unnum.cxx:149
Marks a position in the document model.
Definition: pam.hxx:35
SwContentNode * GetNode(SwPaM &rPam, bool &rbFirst, SwMoveFnCollection const &fnMove, bool const bInReadOnly, SwRootFrame const *const i_pLayout)
This function returns the next node in direction of search.
Definition: pam.cxx:768
SwUndoId
Definition: swundo.hxx:29
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:289
SwFormatColl * GetCondFormatColl() const
Definition: node.hxx:711
SwNodeIndex nNode
Definition: pam.hxx:37
sal_uIntPtr sal_uLong
SwPaM & GetRepeatPaM()
Definition: UndoCore.hxx:128
sal_uLong m_nEndNode
Definition: undobj.hxx:223
bool NumOrNoNum(const SwNodeIndex &rIdx, bool bDel=false)
Definition: docnum.cxx:2347
bool MoveOutlinePara(const SwPaM &rPam, SwOutlineNodes::difference_type nOffset)
Outline - move up / move down.
Definition: docnum.cxx:434
Definition: doc.hxx:185
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:275
SwNode & GetNode() const
Definition: ndindex.hxx:119
void SetNumRuleStart(const SwPosition &rPos, bool bFlag=true)
Definition: docnum.cxx:1000
sal_uLong const nIdx
const OUString & GetName() const
Definition: numrule.hxx:222
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:240
bool NumUpDown(const SwPaM &, bool bDown, SwRootFrame const *pLayout=nullptr)
Definition: docnum.cxx:1711
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:301
SwUndoId GetId() const
Definition: undobj.hxx:100
std::unique_ptr< SwHistory > pHistory
sal_uLong nNewStt
int GetActualListLevel() const
Returns the actual list level of this text node, when it is a list item.
Definition: ndtxt.cxx:4088
std::unique_ptr< SwHistory > pHistory
virtual ~SwUndoInsNum() override
Definition: unnum.cxx:59
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:269
SwNumRule const aNumRule
virtual SwRewriter GetRewriter() const override
Returns the rewriter for this object.
Definition: unnum.cxx:65
SwIndex nContent
Definition: pam.hxx:38
SwDoc & GetDoc() const
Definition: UndoCore.hxx:126
sal_uLong GetIndex() const
Definition: ndindex.hxx:152
virtual ~SwUndoDelNum() override
Definition: unnum.cxx:162
void SetCountedInList(bool bCounted)
Definition: ndtxt.cxx:4216
SwHistory * GetHistory()
Definition: unnum.cxx:142
SwDoc & GetDoc() const
Definition: UndoCore.hxx:94
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:125
SwUndoNumOrNoNum(const SwNodeIndex &rIdx, bool mbOldNum, bool mbNewNum)
Definition: unnum.cxx:280
SwUndoDelNum(const SwPaM &rPam)
Definition: unnum.cxx:155
void DelNumRules(const SwPaM &, SwRootFrame const *pLayout=nullptr)
Definition: docnum.cxx:1322
void SetNodeNumStart(const SwPosition &rPos, sal_uInt16 nStt)
Definition: docnum.cxx:1022
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:382
const SwPosition * GetPoint() const
Definition: pam.hxx:207
long const nOffset
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:354
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:98
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:234
sal_Int32 m_nEndContent
Definition: undobj.hxx:224
void AddRule(SwUndoArg eWhat, const OUString &rWith)
Definition: SwRewriter.cxx:29
SwNumRule * GetNumRule(bool bInParent=true) const
Returns numbering rule of this text node.
Definition: ndtxt.cxx:2804
sal_uInt16 nLRSavePos
Marks a node in the document model.
Definition: ndindex.hxx:31
SwUndoNumUpDown(const SwPaM &rPam, short nOffset)
Definition: unnum.cxx:254
SwUndoInsNum(const SwPaM &rPam, const SwNumRule &rRule)
Definition: unnum.cxx:39
std::vector< NodeLevel > aNodes
bool ReplaceNumRule(const SwPosition &rPos, const OUString &rOldRule, const OUString &rNewRule)
Definition: docnum.cxx:1163
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:263
SwUndoMoveNum(const SwPaM &rPam, long nOffset, bool bIsOutlMv)
Definition: unnum.cxx:205
sal_uLong m_nSttNode
Definition: undobj.hxx:223
sal_uLong const nIdx
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:79
OUString const sReplaceRule
void SetAttrListLevel(int nLevel)
Sets the list level of this text node.
Definition: ndtxt.cxx:4058
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:166
void ChkCondColl()
Definition: node.cxx:1946
SwNumberTree::tSwNumTreeNumber GetAttrListRestartValue() const
Definition: ndtxt.cxx:4165
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:253
sal_Int32 GetIndex() const
Definition: index.hxx:95
bool MoveParagraph(SwPaM &, long nOffset, bool bIsOutlMv=false)
Move selected paragraphs (not only numberings) according to offsets.
Definition: docnum.cxx:1827
SwNodes & GetNodes()
Definition: doc.hxx:403
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:186
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:312
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:192
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:368
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:73
std::unique_ptr< SwNumRule > pOldNumRule
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:214
OUString SetNumRule(const SwPaM &, const SwNumRule &, bool bCreateNewList, SwRootFrame const *pLayout=nullptr, const OUString &sContinuedListId=OUString(), bool bSetItem=true, const bool bResetIndentAttrs=false)
Accept changes of outline styles for OutlineRule.
Definition: docnum.cxx:852
void AddNode(const SwTextNode &rNd)
Definition: unnum.cxx:197
bool HasAttrListRestartValue() const
Definition: ndtxt.cxx:4160
SwUndoNumRuleStart(const SwPosition &rPos, bool bDelete)
Definition: unnum.cxx:325
const sal_Int32 COMPLETE_STRING
Definition: swtypes.hxx:61
sal_Int32 m_nSttContent
Definition: undobj.hxx:224
void SetLRSpaceEndPos()
Definition: unnum.cxx:119
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:842
short const nOffset
void ChgNumRuleFormats(const SwNumRule &rRule)
Definition: docnum.cxx:1084