android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / undo / SwUndoFmt.cxx
blob3493ad56f31e6108f2a447cb83aa00a6ed45e3c0
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 <poolfmt.hxx>
21 #include <charfmt.hxx>
22 #include <frmfmt.hxx>
23 #include <SwUndoFmt.hxx>
24 #include <SwRewriter.hxx>
25 #include <swundo.hxx>
26 #include <undobj.hxx>
27 #include <fmtcol.hxx>
28 #include <doc.hxx>
29 #include <strings.hrc>
30 #include <utility>
32 SwUndoFormatCreate::SwUndoFormatCreate
33 (SwUndoId nUndoId, SwFormat * _pNew, SwFormat const * _pDerivedFrom, SwDoc& rDoc)
34 : SwUndo(nUndoId, &rDoc), m_pNew(_pNew),
35 m_rDoc(rDoc), m_nId(0), m_bAuto(false)
37 if (_pDerivedFrom)
38 m_sDerivedFrom = _pDerivedFrom->GetName();
41 SwUndoFormatCreate::~SwUndoFormatCreate()
45 void SwUndoFormatCreate::UndoImpl(::sw::UndoRedoContext &)
47 if (!m_pNew)
48 return;
50 if (m_sNewName.isEmpty())
51 m_sNewName = m_pNew->GetName();
53 if (!m_sNewName.isEmpty())
54 m_pNew = Find(m_sNewName);
56 if (m_pNew)
58 m_pNewSet.reset(new SfxItemSet(m_pNew->GetAttrSet()));
59 m_nId = m_pNew->GetPoolFormatId() & COLL_GET_RANGE_BITS;
60 m_bAuto = m_pNew->IsAuto();
62 Delete();
66 void SwUndoFormatCreate::RedoImpl(::sw::UndoRedoContext &)
68 SwFormat * pDerivedFrom = Find(m_sDerivedFrom);
69 SwFormat * pFormat = Create(pDerivedFrom);
71 if (pFormat && m_pNewSet)
73 pFormat->SetAuto(m_bAuto);
74 m_rDoc.ChgFormat(*pFormat, *m_pNewSet);
75 pFormat->SetPoolFormatId((pFormat->GetPoolFormatId()
76 & ~COLL_GET_RANGE_BITS)
77 | m_nId);
79 m_pNew = pFormat;
81 else
82 m_pNew = nullptr;
85 SwRewriter SwUndoFormatCreate::GetRewriter() const
87 if (m_sNewName.isEmpty() && m_pNew)
88 m_sNewName = m_pNew->GetName();
90 SwRewriter aRewriter;
92 aRewriter.AddRule(UndoArg1, m_sNewName);
94 return aRewriter;
97 SwUndoFormatDelete::SwUndoFormatDelete
98 (SwUndoId nUndoId, SwFormat const * _pOld, SwDoc& rDoc)
99 : SwUndo(nUndoId, &rDoc),
100 m_rDoc(rDoc), m_sOldName(_pOld->GetName()),
101 m_aOldSet(_pOld->GetAttrSet())
103 m_sDerivedFrom = _pOld->DerivedFrom()->GetName();
104 m_nId = _pOld->GetPoolFormatId() & COLL_GET_RANGE_BITS;
105 m_bAuto = _pOld->IsAuto();
108 SwUndoFormatDelete::~SwUndoFormatDelete()
112 void SwUndoFormatDelete::UndoImpl(::sw::UndoRedoContext &)
114 SwFormat * pDerivedFrom = Find(m_sDerivedFrom);
116 SwFormat * pFormat = Create(pDerivedFrom);
118 if (pFormat)
120 m_rDoc.ChgFormat(*pFormat, m_aOldSet);
121 pFormat->SetAuto(m_bAuto);
122 pFormat->SetPoolFormatId((pFormat->GetPoolFormatId() &
123 ~COLL_GET_RANGE_BITS)
124 | m_nId);
128 void SwUndoFormatDelete::RedoImpl(::sw::UndoRedoContext &)
130 SwFormat * pOld = Find(m_sOldName);
132 if (pOld)
134 Delete(pOld);
138 SwRewriter SwUndoFormatDelete::GetRewriter() const
140 SwRewriter aRewriter;
142 aRewriter.AddRule(UndoArg1, m_sOldName);
144 return aRewriter;
147 SwUndoRenameFormat::SwUndoRenameFormat(SwUndoId nUndoId,
148 OUString _sOldName,
149 OUString _sNewName,
150 SwDoc& rDoc)
151 : SwUndo(nUndoId, &rDoc), m_sOldName(std::move(_sOldName)),
152 m_sNewName(std::move(_sNewName)), m_rDoc(rDoc)
156 SwUndoRenameFormat::~SwUndoRenameFormat()
160 void SwUndoRenameFormat::UndoImpl(::sw::UndoRedoContext &)
162 SwFormat * pFormat = Find(m_sNewName);
164 if (pFormat)
166 m_rDoc.RenameFormat(*pFormat, m_sOldName, true);
170 void SwUndoRenameFormat::RedoImpl(::sw::UndoRedoContext &)
172 SwFormat * pFormat = Find(m_sOldName);
174 if (pFormat)
176 m_rDoc.RenameFormat(*pFormat, m_sNewName, true);
180 SwRewriter SwUndoRenameFormat::GetRewriter() const
182 SwRewriter aRewriter;
184 aRewriter.AddRule(UndoArg1, m_sOldName);
185 aRewriter.AddRule(UndoArg2, SwResId(STR_YIELDS));
186 aRewriter.AddRule(UndoArg3, m_sNewName);
188 return aRewriter;
191 SwUndoTextFormatCollCreate::SwUndoTextFormatCollCreate
192 (SwTextFormatColl * _pNew, SwTextFormatColl const * _pDerivedFrom, SwDoc& rDoc)
193 : SwUndoFormatCreate(SwUndoId::TXTFMTCOL_CREATE, _pNew, _pDerivedFrom, rDoc)
197 SwFormat * SwUndoTextFormatCollCreate::Create(SwFormat * pDerivedFrom)
199 return m_rDoc.MakeTextFormatColl(m_sNewName, static_cast<SwTextFormatColl *>(pDerivedFrom), true);
202 void SwUndoTextFormatCollCreate::Delete()
204 m_rDoc.DelTextFormatColl(static_cast<SwTextFormatColl *>(m_pNew), true);
207 SwFormat * SwUndoTextFormatCollCreate::Find(const OUString & rName) const
209 return m_rDoc.FindTextFormatCollByName(rName);
212 SwUndoTextFormatCollDelete::SwUndoTextFormatCollDelete(SwTextFormatColl const * _pOld,
213 SwDoc& rDoc)
214 : SwUndoFormatDelete(SwUndoId::TXTFMTCOL_DELETE, _pOld, rDoc)
218 SwFormat * SwUndoTextFormatCollDelete::Create(SwFormat * pDerivedFrom)
220 return m_rDoc.MakeTextFormatColl(m_sOldName, static_cast<SwTextFormatColl *>(pDerivedFrom), true);
223 void SwUndoTextFormatCollDelete::Delete(SwFormat * pOld)
225 m_rDoc.DelTextFormatColl(static_cast<SwTextFormatColl *>(pOld), true);
228 SwFormat * SwUndoTextFormatCollDelete::Find(const OUString & rName) const
230 return m_rDoc.FindTextFormatCollByName(rName);
233 SwUndoCondTextFormatCollCreate::SwUndoCondTextFormatCollCreate(SwConditionTextFormatColl *_pNew,
234 SwTextFormatColl const *_pDerivedFrom, SwDoc& rDoc)
235 : SwUndoTextFormatCollCreate(_pNew, _pDerivedFrom, rDoc)
239 SwFormat * SwUndoCondTextFormatCollCreate::Create(SwFormat * pDerivedFrom)
241 return m_rDoc.MakeCondTextFormatColl(m_sNewName, static_cast<SwTextFormatColl *>(pDerivedFrom), true);
244 SwUndoCondTextFormatCollDelete::SwUndoCondTextFormatCollDelete(SwTextFormatColl const * _pOld,
245 SwDoc& rDoc)
246 : SwUndoTextFormatCollDelete(_pOld, rDoc)
250 SwFormat * SwUndoCondTextFormatCollDelete::Create(SwFormat * pDerivedFrom)
252 return m_rDoc.MakeCondTextFormatColl(m_sOldName, static_cast<SwTextFormatColl *>(pDerivedFrom), true);
255 SwUndoRenameFormatColl::SwUndoRenameFormatColl(const OUString & sInitOldName,
256 const OUString & sInitNewName,
257 SwDoc& rDoc)
258 : SwUndoRenameFormat(SwUndoId::TXTFMTCOL_RENAME, sInitOldName, sInitNewName, rDoc)
262 SwFormat * SwUndoRenameFormatColl::Find(const OUString & rName) const
264 return m_rDoc.FindTextFormatCollByName(rName);
267 SwUndoCharFormatCreate::SwUndoCharFormatCreate(SwCharFormat * pNewFormat,
268 SwCharFormat const * pDerivedFrom,
269 SwDoc& rDocument)
270 : SwUndoFormatCreate(SwUndoId::CHARFMT_CREATE, pNewFormat, pDerivedFrom, rDocument)
274 SwFormat * SwUndoCharFormatCreate::Create(SwFormat * pDerivedFrom)
276 return m_rDoc.MakeCharFormat(m_sNewName, static_cast<SwCharFormat *>(pDerivedFrom), true);
279 void SwUndoCharFormatCreate::Delete()
281 m_rDoc.DelCharFormat(static_cast<SwCharFormat *>(m_pNew), true);
284 SwFormat * SwUndoCharFormatCreate::Find(const OUString & rName) const
286 return m_rDoc.FindCharFormatByName(rName);
289 SwUndoCharFormatDelete::SwUndoCharFormatDelete(SwCharFormat const * pOld, SwDoc& rDocument)
290 : SwUndoFormatDelete(SwUndoId::CHARFMT_DELETE, pOld, rDocument)
294 SwFormat * SwUndoCharFormatDelete::Create(SwFormat * pDerivedFrom)
296 return m_rDoc.MakeCharFormat(m_sOldName, static_cast<SwCharFormat *>(pDerivedFrom), true);
299 void SwUndoCharFormatDelete::Delete(SwFormat * pFormat)
301 m_rDoc.DelCharFormat(static_cast<SwCharFormat *>(pFormat), true);
304 SwFormat * SwUndoCharFormatDelete::Find(const OUString & rName) const
306 return m_rDoc.FindCharFormatByName(rName);
309 SwUndoRenameCharFormat::SwUndoRenameCharFormat(const OUString & sInitOldName,
310 const OUString & sInitNewName,
311 SwDoc& rDocument)
312 : SwUndoRenameFormat(SwUndoId::CHARFMT_RENAME, sInitOldName, sInitNewName, rDocument)
316 SwFormat * SwUndoRenameCharFormat::Find(const OUString & rName) const
318 return m_rDoc.FindCharFormatByName(rName);
321 SwUndoFrameFormatCreate::SwUndoFrameFormatCreate(SwFrameFormat * pNewFormat,
322 SwFrameFormat const * pDerivedFrom,
323 SwDoc& rDocument)
324 : SwUndoFormatCreate(SwUndoId::FRMFMT_CREATE, pNewFormat, pDerivedFrom, rDocument)
328 SwFormat * SwUndoFrameFormatCreate::Create(SwFormat * pDerivedFrom)
330 return m_rDoc.MakeFrameFormat(m_sNewName, static_cast<SwFrameFormat *>(pDerivedFrom), true, m_pNew->IsAuto());
333 void SwUndoFrameFormatCreate::Delete()
335 m_rDoc.DelFrameFormat(static_cast<SwFrameFormat *>(m_pNew), true);
338 SwFormat * SwUndoFrameFormatCreate::Find(const OUString & rName) const
340 return m_rDoc.FindFrameFormatByName(rName);
343 SwUndoFrameFormatDelete::SwUndoFrameFormatDelete(SwFrameFormat const * pOld, SwDoc& rDocument)
344 : SwUndoFormatDelete(SwUndoId::FRMFMT_DELETE, pOld, rDocument)
348 SwFormat * SwUndoFrameFormatDelete::Create(SwFormat * pDerivedFrom)
350 return m_rDoc.MakeFrameFormat(m_sOldName, static_cast<SwFrameFormat *>(pDerivedFrom), true);
353 void SwUndoFrameFormatDelete::Delete(SwFormat * pFormat)
355 m_rDoc.DelFrameFormat(static_cast<SwFrameFormat *>(pFormat), true);
358 SwFormat * SwUndoFrameFormatDelete::Find(const OUString & rName) const
360 return m_rDoc.FindFrameFormatByName(rName);
363 SwUndoRenameFrameFormat::SwUndoRenameFrameFormat(const OUString & sInitOldName,
364 const OUString & sInitNewName,
365 SwDoc& rDocument)
366 : SwUndoRenameFormat(SwUndoId::FRMFMT_RENAME, sInitOldName, sInitNewName, rDocument)
370 SwFormat * SwUndoRenameFrameFormat::Find(const OUString & rName) const
372 return m_rDoc.FindFrameFormatByName(rName);
375 SwUndoNumruleCreate::SwUndoNumruleCreate(const SwNumRule * _pNew,
376 SwDoc& rDoc)
377 : SwUndo(SwUndoId::NUMRULE_CREATE, &rDoc), m_pNew(_pNew), m_aNew(*_pNew), m_rDoc(rDoc),
378 m_bInitialized(false)
382 void SwUndoNumruleCreate::UndoImpl(::sw::UndoRedoContext &)
384 if (! m_bInitialized)
386 m_aNew = *m_pNew;
387 m_bInitialized = true;
390 m_rDoc.DelNumRule(m_aNew.GetName(), true);
393 void SwUndoNumruleCreate::RedoImpl(::sw::UndoRedoContext &)
395 m_rDoc.MakeNumRule(m_aNew.GetName(), &m_aNew, true);
398 SwRewriter SwUndoNumruleCreate::GetRewriter() const
400 SwRewriter aResult;
402 if (! m_bInitialized)
404 m_aNew = *m_pNew;
405 m_bInitialized = true;
408 aResult.AddRule(UndoArg1, m_aNew.GetName());
410 return aResult;
413 SwUndoNumruleDelete::SwUndoNumruleDelete(const SwNumRule & rRule,
414 SwDoc& rDoc)
415 : SwUndo(SwUndoId::NUMRULE_DELETE, &rDoc), m_aOld(rRule), m_rDoc(rDoc)
419 void SwUndoNumruleDelete::UndoImpl(::sw::UndoRedoContext &)
421 m_rDoc.MakeNumRule(m_aOld.GetName(), &m_aOld, true);
424 void SwUndoNumruleDelete::RedoImpl(::sw::UndoRedoContext &)
426 m_rDoc.DelNumRule(m_aOld.GetName(), true);
429 SwRewriter SwUndoNumruleDelete::GetRewriter() const
431 SwRewriter aResult;
433 aResult.AddRule(UndoArg1, m_aOld.GetName());
435 return aResult;
438 SwUndoNumruleRename::SwUndoNumruleRename(OUString _aOldName,
439 OUString _aNewName,
440 SwDoc& rDoc)
441 : SwUndo(SwUndoId::NUMRULE_RENAME, &rDoc), m_aOldName(std::move(_aOldName)), m_aNewName(std::move(_aNewName)),
442 m_rDoc(rDoc)
446 void SwUndoNumruleRename::UndoImpl(::sw::UndoRedoContext &)
448 m_rDoc.RenameNumRule(m_aNewName, m_aOldName, true);
451 void SwUndoNumruleRename::RedoImpl(::sw::UndoRedoContext &)
453 m_rDoc.RenameNumRule(m_aOldName, m_aNewName, true);
456 SwRewriter SwUndoNumruleRename::GetRewriter() const
458 SwRewriter aRewriter;
460 aRewriter.AddRule(UndoArg1, m_aOldName);
461 aRewriter.AddRule(UndoArg2, SwResId(STR_YIELDS));
462 aRewriter.AddRule(UndoArg3, m_aNewName);
464 return aRewriter;
467 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */