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  m_aNumRule( rNewRule ),
35  m_pOldNumRule( new SwNumRule( rOldRule )), m_nLRSavePos( 0 )
36 {
37 }
38 
39 SwUndoInsNum::SwUndoInsNum( const SwPaM& rPam, const SwNumRule& rRule )
40  : SwUndo( SwUndoId::INSNUM, rPam.GetDoc() ), SwUndRng( rPam ),
41  m_aNumRule( rRule ),
42  m_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  m_aNumRule( rRule ),
50  m_sReplaceRule( rReplaceRule ), m_nLRSavePos( 0 )
51 {
52  // No selection!
53  m_nEndNode = 0;
55  m_nSttNode = rPos.nNode.GetIndex();
57 }
58 
60 {
61  m_pHistory.reset();
62  m_pOldNumRule.reset();
63 }
64 
66 {
67  SwRewriter aResult;
68  if( SwUndoId::INSFMTATTR == GetId() )
69  aResult.AddRule(UndoArg1, m_aNumRule.GetName());
70  return aResult;
71 }
72 
74 {
75  SwDoc & rDoc = rContext.GetDoc();
76 
77  if( m_pOldNumRule )
79 
80  if( m_pHistory )
81  {
82  if( m_nLRSavePos )
83  {
84  // Update immediately so that potential "old" LRSpaces will be valid again.
85  m_pHistory->TmpRollback( &rDoc, m_nLRSavePos );
86 
87  }
88  m_pHistory->TmpRollback( &rDoc, 0 );
89  m_pHistory->SetTmpEnd( m_pHistory->Count() );
90  }
91 
92  if (m_nSttNode)
93  {
94  AddUndoRedoPaM(rContext);
95  }
96 }
97 
99 {
100  SwDoc & rDoc = rContext.GetDoc();
101 
102  if ( m_pOldNumRule )
104  else if ( m_pHistory )
105  {
106  SwPaM & rPam( AddUndoRedoPaM(rContext) );
107  if( !m_sReplaceRule.isEmpty() )
108  {
110  }
111  else
112  {
113  // #i42921# - adapt to changed signature
114  rDoc.SetNumRule(rPam, m_aNumRule, false);
115  }
116  }
117 }
118 
120 {
121  if( m_pHistory )
122  m_nLRSavePos = m_pHistory->Count();
123 }
124 
126 {
127  SwDoc & rDoc( rContext.GetDoc() );
128  if ( m_nSttNode )
129  {
130  if( m_sReplaceRule.isEmpty() )
131  {
132  // #i42921# - adapt to changed signature
133  rDoc.SetNumRule(rContext.GetRepeatPaM(), m_aNumRule, false);
134  }
135  }
136  else
137  {
138  rDoc.ChgNumRuleFormats( m_aNumRule );
139  }
140 }
141 
143 {
144  if( !m_pHistory )
145  m_pHistory.reset(new SwHistory);
146  return m_pHistory.get();
147 }
148 
150 {
151  if( !m_pOldNumRule )
152  m_pOldNumRule.reset(new SwNumRule( rOld ));
153 }
154 
156  : SwUndo( SwUndoId::DELNUM, rPam.GetDoc() ), SwUndRng( rPam )
157 {
158  m_aNodes.reserve( std::min<sal_uLong>(m_nEndNode - m_nSttNode, 255) );
159  m_pHistory.reset( new SwHistory );
160 }
161 
163 {
164 }
165 
167 {
168  SwDoc & rDoc = rContext.GetDoc();
169 
170  m_pHistory->TmpRollback( &rDoc, 0 );
171  m_pHistory->SetTmpEnd( m_pHistory->Count() );
172 
173  for( const auto& rNode : m_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  m_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  m_nNewStart( 0 ), m_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( m_nNewStart < m_nSttNode ) // moved forwards
222  else
223  m_nEndNode = m_nEndNode + ( m_nNewStart - m_nSttNode );
224  }
226 
227  SwPaM & rPam( AddUndoRedoPaM(rContext) );
228  rContext.GetDoc().MoveParagraph( rPam, -m_nOffset,
230  m_nSttNode = nTmpStt;
231  m_nEndNode = nTmpEnd;
232 }
233 
235 {
236  SwPaM & rPam( AddUndoRedoPaM(rContext) );
237  rContext.GetDoc().MoveParagraph(rPam, m_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 < m_nOffset ? 1 : -1 );
247  }
248  else
249  {
250  rDoc.MoveParagraph(rContext.GetRepeatPaM(), m_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  m_nOffset( nOff )
258 {
259  // nOffset: Down => 1
260  // Up => -1
261 }
262 
264 {
265  SwPaM & rPam( AddUndoRedoPaM(rContext) );
266  rContext.GetDoc().NumUpDown(rPam, 1 != m_nOffset );
267 }
268 
270 {
271  SwPaM & rPam( AddUndoRedoPaM(rContext) );
272  rContext.GetDoc().NumUpDown(rPam, 1 == m_nOffset);
273 }
274 
276 {
277  rContext.GetDoc().NumUpDown(rContext.GetRepeatPaM(), 1 == m_nOffset);
278 }
279 
281  bool bNewNum)
282  : SwUndo( SwUndoId::NUMORNONUM, rIdx.GetNode().GetDoc() ),
283  m_nIndex( rIdx.GetIndex() ), mbNewNum(bNewNum),
284  mbOldNum(bOldNum)
285 {
286 }
287 
288 // #115901#, #i40034#
290 {
291  SwNodeIndex aIdx( rContext.GetDoc().GetNodes(), m_nIndex );
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(), m_nIndex );
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  m_nIndex( rPos.nNode.GetIndex() ), m_nOldStart( USHRT_MAX ),
328  m_nNewStart( USHRT_MAX ), m_bSetStartValue( false ), m_bFlag( bFlg )
329 {
330 }
331 
332 SwUndoNumRuleStart::SwUndoNumRuleStart( const SwPosition& rPos, sal_uInt16 nStt )
333  : SwUndo(SwUndoId::SETNUMRULESTART, rPos.GetDoc())
334  , m_nIndex(rPos.nNode.GetIndex())
335  , m_nOldStart(USHRT_MAX)
336  , m_nNewStart(nStt)
337  , m_bSetStartValue(true)
338  , m_bFlag(false)
339 {
340  SwTextNode* pTextNd = rPos.nNode.GetNode().GetTextNode();
341  if ( pTextNd )
342  {
343  if ( pTextNd->HasAttrListRestartValue() )
344  {
345  m_nOldStart = static_cast<sal_uInt16>(pTextNd->GetAttrListRestartValue());
346  }
347  else
348  {
349  m_nOldStart = 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()[ m_nIndex ] );
358  if( m_bSetStartValue )
359  {
360  rDoc.SetNodeNumStart( aPos, m_nOldStart );
361  }
362  else
363  {
364  rDoc.SetNumRuleStart( aPos, !m_bFlag );
365  }
366 }
367 
369 {
370  SwDoc & rDoc = rContext.GetDoc();
371  SwPosition const aPos( *rDoc.GetNodes()[ m_nIndex ] );
372  if( m_bSetStartValue )
373  {
374  rDoc.SetNodeNumStart( aPos, m_nNewStart );
375  }
376  else
377  {
378  rDoc.SetNumRuleStart( aPos, m_bFlag );
379  }
380 }
381 
383 {
384  SwDoc & rDoc = rContext.GetDoc();
385  if( m_bSetStartValue )
386  {
387  rDoc.SetNodeNumStart(*rContext.GetRepeatPaM().GetPoint(), m_nNewStart);
388  }
389  else
390  {
391  rDoc.SetNumRuleStart(*rContext.GetRepeatPaM().GetPoint(), m_bFlag);
392  }
393 }
394 
395 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SwPaM & AddUndoRedoPaM(::sw::UndoRedoContext &, bool const bCorrToContent=false) const
Definition: undobj.cxx:108
std::unique_ptr< SwHistory > m_pHistory
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:816
std::unique_ptr< SwNumRule > m_pOldNumRule
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:229
bool NumOrNoNum(const SwNodeIndex &rIdx, bool bDel=false)
Definition: docnum.cxx:2349
bool MoveOutlinePara(const SwPaM &rPam, SwOutlineNodes::difference_type nOffset)
Outline - move up / move down.
Definition: docnum.cxx:436
Definition: doc.hxx:184
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:1002
const OUString & GetName() const
Definition: numrule.hxx:223
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:240
bool NumUpDown(const SwPaM &, bool bDown, SwRootFrame const *pLayout=nullptr)
Definition: docnum.cxx:1712
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:301
SwUndoId GetId() const
Definition: undobj.hxx:101
int GetActualListLevel() const
Returns the actual list level of this text node, when it is a list item.
Definition: ndtxt.cxx:4111
virtual ~SwUndoInsNum() override
Definition: unnum.cxx:59
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:269
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:4239
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:1324
void SetNodeNumStart(const SwPosition &rPos, sal_uInt16 nStt)
Definition: docnum.cxx:1024
PaM is Point and Mark: a selection of the document model.
Definition: pam.hxx:136
SwNumRule m_aNumRule
virtual void RepeatImpl(::sw::RepeatContext &) override
Definition: unnum.cxx:382
const SwPosition * GetPoint() const
Definition: pam.hxx:207
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:230
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:2808
Marks a node in the document model.
Definition: ndindex.hxx:31
OUString m_sReplaceRule
SwUndoNumUpDown(const SwPaM &rPam, short nOffset)
Definition: unnum.cxx:254
sal_uInt64 m_nOffset
SwUndoInsNum(const SwPaM &rPam, const SwNumRule &rRule)
Definition: unnum.cxx:39
bool ReplaceNumRule(const SwPosition &rPos, const OUString &rOldRule, const OUString &rNewRule)
Definition: docnum.cxx:1165
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:229
SwTextNode is a paragraph in the document model.
Definition: ndtxt.hxx:80
void SetAttrListLevel(int nLevel)
Sets the list level of this text node.
Definition: ndtxt.cxx:4081
virtual void UndoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:166
std::vector< NodeLevel > m_aNodes
void ChkCondColl()
Definition: node.cxx:2002
SwNumberTree::tSwNumTreeNumber GetAttrListRestartValue() const
Definition: ndtxt.cxx:4188
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:254
std::unique_ptr< SwHistory > m_pHistory
sal_Int32 GetIndex() const
Definition: index.hxx:91
bool MoveParagraph(SwPaM &, long nOffset, bool bIsOutlMv=false)
Move selected paragraphs (not only numberings) according to offsets.
Definition: docnum.cxx:1828
SwNodes & GetNodes()
Definition: doc.hxx:403
virtual void RedoImpl(::sw::UndoRedoContext &) override
Definition: unnum.cxx:186
sal_uLong m_nNewStart
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
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:854
void AddNode(const SwTextNode &rNd)
Definition: unnum.cxx:197
bool HasAttrListRestartValue() const
Definition: ndtxt.cxx:4183
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:230
sal_uInt16 m_nLRSavePos
void SetLRSpaceEndPos()
Definition: unnum.cxx:119
SwTextNode * GetTextNode()
Inline methods from Node.hxx.
Definition: ndtxt.hxx:844
void ChgNumRuleFormats(const SwNumRule &rRule)
Definition: docnum.cxx:1086