android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / undo / unnum.cxx
blob1251635a3135bdd53942160aab41df076de0fa38
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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 <utility>
28 #include <osl/diagnose.h>
30 SwUndoInsNum::SwUndoInsNum( const SwNumRule& rOldRule,
31 const SwNumRule& rNewRule,
32 const SwDoc& rDoc,
33 SwUndoId nUndoId)
34 : SwUndo( nUndoId, &rDoc ),
35 m_aNumRule( rNewRule ),
36 m_pOldNumRule( new SwNumRule( rOldRule )), m_nLRSavePos( 0 )
40 SwUndoInsNum::SwUndoInsNum( const SwPaM& rPam, const SwNumRule& rRule )
41 : SwUndo( SwUndoId::INSNUM, &rPam.GetDoc() ), SwUndRng( rPam ),
42 m_aNumRule( rRule ),
43 m_nLRSavePos( 0 )
47 SwUndoInsNum::SwUndoInsNum( const SwPosition& rPos, const SwNumRule& rRule,
48 OUString aReplaceRule )
49 : SwUndo( SwUndoId::INSNUM, &rPos.GetNode().GetDoc() ),
50 m_aNumRule( rRule ),
51 m_sReplaceRule(std::move( aReplaceRule )), m_nLRSavePos( 0 )
53 // No selection!
54 m_nEndNode = SwNodeOffset(0);
55 m_nEndContent = COMPLETE_STRING;
56 m_nSttNode = rPos.GetNodeIndex();
57 m_nSttContent = rPos.GetContentIndex();
60 SwUndoInsNum::~SwUndoInsNum()
62 m_pHistory.reset();
63 m_pOldNumRule.reset();
66 SwRewriter SwUndoInsNum::GetRewriter() const
68 SwRewriter aResult;
69 if( SwUndoId::INSFMTATTR == GetId() )
70 aResult.AddRule(UndoArg1, m_aNumRule.GetName());
71 return aResult;
74 void SwUndoInsNum::UndoImpl(::sw::UndoRedoContext & rContext)
76 SwDoc & rDoc = rContext.GetDoc();
78 if( m_pOldNumRule )
79 rDoc.ChgNumRuleFormats( *m_pOldNumRule );
81 if( m_pHistory )
83 if( m_nLRSavePos )
85 // Update immediately so that potential "old" LRSpaces will be valid again.
86 m_pHistory->TmpRollback( &rDoc, m_nLRSavePos );
89 m_pHistory->TmpRollback( &rDoc, 0 );
90 m_pHistory->SetTmpEnd( m_pHistory->Count() );
93 if (m_nSttNode)
95 AddUndoRedoPaM(rContext);
99 void SwUndoInsNum::RedoImpl(::sw::UndoRedoContext & rContext)
101 SwDoc & rDoc = rContext.GetDoc();
103 if ( m_pOldNumRule )
104 rDoc.ChgNumRuleFormats( m_aNumRule );
105 else if ( m_pHistory )
107 SwPaM & rPam( AddUndoRedoPaM(rContext) );
108 if( !m_sReplaceRule.isEmpty() )
110 rDoc.ReplaceNumRule( *rPam.GetPoint(), m_sReplaceRule, m_aNumRule.GetName() );
112 else
114 // #i42921# - adapt to changed signature
115 rDoc.SetNumRule(rPam, m_aNumRule, false);
120 void SwUndoInsNum::SetLRSpaceEndPos()
122 if( m_pHistory )
123 m_nLRSavePos = m_pHistory->Count();
126 void SwUndoInsNum::RepeatImpl(::sw::RepeatContext & rContext)
128 SwDoc & rDoc( rContext.GetDoc() );
129 if ( m_nSttNode )
131 if( m_sReplaceRule.isEmpty() )
133 // #i42921# - adapt to changed signature
134 rDoc.SetNumRule(rContext.GetRepeatPaM(), m_aNumRule, false);
137 else
139 rDoc.ChgNumRuleFormats( m_aNumRule );
143 SwHistory* SwUndoInsNum::GetHistory()
145 if( !m_pHistory )
146 m_pHistory.reset(new SwHistory);
147 return m_pHistory.get();
150 void SwUndoInsNum::SaveOldNumRule( const SwNumRule& rOld )
152 if( !m_pOldNumRule )
153 m_pOldNumRule.reset(new SwNumRule( rOld ));
156 SwUndoDelNum::SwUndoDelNum( const SwPaM& rPam )
157 : SwUndo( SwUndoId::DELNUM, &rPam.GetDoc() ), SwUndRng( rPam )
159 if (m_nEndNode > m_nSttNode)
160 m_aNodes.reserve( std::min<sal_Int32>(sal_Int32(m_nEndNode - m_nSttNode), 255) );
161 m_pHistory.reset( new SwHistory );
164 SwUndoDelNum::~SwUndoDelNum()
168 void SwUndoDelNum::UndoImpl(::sw::UndoRedoContext & rContext)
170 SwDoc & rDoc = rContext.GetDoc();
172 m_pHistory->TmpRollback( &rDoc, 0 );
173 m_pHistory->SetTmpEnd( m_pHistory->Count() );
175 for( const auto& rNode : m_aNodes )
177 SwTextNode* pNd = rDoc.GetNodes()[ rNode.index ]->GetTextNode();
178 OSL_ENSURE( pNd, "Where has the TextNode gone?" );
179 pNd->SetAttrListLevel( rNode.level );
181 if( pNd->GetCondFormatColl() )
182 pNd->ChkCondColl();
185 AddUndoRedoPaM(rContext);
188 void SwUndoDelNum::RedoImpl(::sw::UndoRedoContext & rContext)
190 SwPaM & rPam( AddUndoRedoPaM(rContext) );
191 rContext.GetDoc().DelNumRules(rPam);
194 void SwUndoDelNum::RepeatImpl(::sw::RepeatContext & rContext)
196 rContext.GetDoc().DelNumRules(rContext.GetRepeatPaM());
199 void SwUndoDelNum::AddNode( const SwTextNode& rNd )
201 if( rNd.GetNumRule() )
203 m_aNodes.emplace_back( rNd.GetIndex(), rNd.GetActualListLevel() );
207 SwUndoMoveNum::SwUndoMoveNum( const SwPaM& rPam, SwNodeOffset nOff, bool bIsOutlMv )
208 : SwUndo( bIsOutlMv ? SwUndoId::OUTLINE_UD : SwUndoId::MOVENUM, &rPam.GetDoc() ),
209 SwUndRng( rPam ),
210 m_nNewStart( 0 ), m_nOffset( nOff )
212 // nOffset: Down => 1
213 // Up => -1
216 void SwUndoMoveNum::UndoImpl(::sw::UndoRedoContext & rContext)
218 SwNodeOffset nTmpStt = m_nSttNode, nTmpEnd = m_nEndNode;
220 if (m_nEndNode || m_nEndContent != COMPLETE_STRING) // section?
222 if( m_nNewStart < m_nSttNode ) // moved forwards
223 m_nEndNode = m_nEndNode - ( m_nSttNode - m_nNewStart );
224 else
225 m_nEndNode = m_nEndNode + ( m_nNewStart - m_nSttNode );
227 m_nSttNode = m_nNewStart;
229 SwPaM & rPam( AddUndoRedoPaM(rContext) );
230 rContext.GetDoc().MoveParagraph( rPam, -m_nOffset,
231 SwUndoId::OUTLINE_UD == GetId() );
232 m_nSttNode = nTmpStt;
233 m_nEndNode = nTmpEnd;
236 void SwUndoMoveNum::RedoImpl(::sw::UndoRedoContext & rContext)
238 SwPaM & rPam( AddUndoRedoPaM(rContext) );
239 rContext.GetDoc().MoveParagraph(rPam, m_nOffset, SwUndoId::OUTLINE_UD == GetId());
242 void SwUndoMoveNum::RepeatImpl(::sw::RepeatContext & rContext)
244 SwDoc & rDoc = rContext.GetDoc();
245 if( SwUndoId::OUTLINE_UD == GetId() )
247 rDoc.MoveOutlinePara(rContext.GetRepeatPaM(),
248 SwNodeOffset(0) < m_nOffset ? 1 : -1 );
250 else
252 rDoc.MoveParagraph(rContext.GetRepeatPaM(), m_nOffset);
256 SwUndoNumUpDown::SwUndoNumUpDown( const SwPaM& rPam, short nOff )
257 : SwUndo( nOff > 0 ? SwUndoId::NUMUP : SwUndoId::NUMDOWN, &rPam.GetDoc() ),
258 SwUndRng( rPam ),
259 m_nOffset( nOff )
261 // nOffset: Down => 1
262 // Up => -1
265 void SwUndoNumUpDown::UndoImpl(::sw::UndoRedoContext & rContext)
267 SwPaM & rPam( AddUndoRedoPaM(rContext) );
268 rContext.GetDoc().NumUpDown(rPam, 1 != m_nOffset );
271 void SwUndoNumUpDown::RedoImpl(::sw::UndoRedoContext & rContext)
273 SwPaM & rPam( AddUndoRedoPaM(rContext) );
274 rContext.GetDoc().NumUpDown(rPam, 1 == m_nOffset);
277 void SwUndoNumUpDown::RepeatImpl(::sw::RepeatContext & rContext)
279 rContext.GetDoc().NumUpDown(rContext.GetRepeatPaM(), 1 == m_nOffset);
282 SwUndoNumOrNoNum::SwUndoNumOrNoNum( const SwNode& rIdx, bool bOldNum,
283 bool bNewNum)
284 : SwUndo( SwUndoId::NUMORNONUM, &rIdx.GetDoc() ),
285 m_nIndex( rIdx.GetIndex() ), mbNewNum(bNewNum),
286 mbOldNum(bOldNum)
290 // #115901#, #i40034#
291 void SwUndoNumOrNoNum::UndoImpl(::sw::UndoRedoContext & rContext)
293 SwNodeIndex aIdx( rContext.GetDoc().GetNodes(), m_nIndex );
294 SwTextNode * pTextNd = aIdx.GetNode().GetTextNode();
296 if (nullptr != pTextNd)
298 pTextNd->SetCountedInList(mbOldNum);
302 // #115901#, #i40034#
303 void SwUndoNumOrNoNum::RedoImpl(::sw::UndoRedoContext & rContext)
305 SwNodeIndex aIdx( rContext.GetDoc().GetNodes(), m_nIndex );
306 SwTextNode * pTextNd = aIdx.GetNode().GetTextNode();
308 if (nullptr != pTextNd)
310 pTextNd->SetCountedInList(mbNewNum);
314 void SwUndoNumOrNoNum::RepeatImpl(::sw::RepeatContext & rContext)
316 SwDoc & rDoc = rContext.GetDoc();
317 if (mbOldNum && ! mbNewNum)
319 rDoc.NumOrNoNum(rContext.GetRepeatPaM().GetPoint()->GetNode());
321 else if ( ! mbOldNum && mbNewNum )
323 rDoc.NumOrNoNum(rContext.GetRepeatPaM().GetPoint()->GetNode(), true);
327 SwUndoNumRuleStart::SwUndoNumRuleStart( const SwPosition& rPos, bool bFlg )
328 : SwUndo( SwUndoId::SETNUMRULESTART, &rPos.GetDoc() ),
329 m_nIndex( rPos.GetNodeIndex() ), m_nOldStart( USHRT_MAX ),
330 m_nNewStart( USHRT_MAX ), m_bSetStartValue( false ), m_bFlag( bFlg )
334 SwUndoNumRuleStart::SwUndoNumRuleStart( const SwPosition& rPos, sal_uInt16 nStt )
335 : SwUndo(SwUndoId::SETNUMRULESTART, &rPos.GetDoc())
336 , m_nIndex(rPos.GetNodeIndex())
337 , m_nOldStart(USHRT_MAX)
338 , m_nNewStart(nStt)
339 , m_bSetStartValue(true)
340 , m_bFlag(false)
342 SwTextNode* pTextNd = rPos.GetNode().GetTextNode();
343 if ( pTextNd )
345 if ( pTextNd->HasAttrListRestartValue() )
347 m_nOldStart = o3tl::narrowing<sal_uInt16>(pTextNd->GetAttrListRestartValue());
349 else
351 m_nOldStart = USHRT_MAX; // indicating, that the list restart value is not set
356 void SwUndoNumRuleStart::UndoImpl(::sw::UndoRedoContext & rContext)
358 SwDoc & rDoc = rContext.GetDoc();
359 SwPosition const aPos( *rDoc.GetNodes()[ m_nIndex ] );
360 if( m_bSetStartValue )
362 rDoc.SetNodeNumStart( aPos, m_nOldStart );
364 else
366 rDoc.SetNumRuleStart( aPos, !m_bFlag );
370 void SwUndoNumRuleStart::RedoImpl(::sw::UndoRedoContext & rContext)
372 SwDoc & rDoc = rContext.GetDoc();
373 SwPosition const aPos( *rDoc.GetNodes()[ m_nIndex ] );
374 if( m_bSetStartValue )
376 rDoc.SetNodeNumStart( aPos, m_nNewStart );
378 else
380 rDoc.SetNumRuleStart( aPos, m_bFlag );
384 void SwUndoNumRuleStart::RepeatImpl(::sw::RepeatContext & rContext)
386 SwDoc & rDoc = rContext.GetDoc();
387 if( m_bSetStartValue )
389 rDoc.SetNodeNumStart(*rContext.GetRepeatPaM().GetPoint(), m_nNewStart);
391 else
393 rDoc.SetNumRuleStart(*rContext.GetRepeatPaM().GetPoint(), m_bFlag);
397 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */