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