update dev300-m58
[ooovba.git] / sw / source / core / undo / SwUndoFmt.cxx
blob473aeb2cc9ed4f35c1ee9e40fa5396fe7a0a6da7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SwUndoFmt.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #include <tools/resid.hxx>
35 #include <poolfmt.hxx>
36 #include <charfmt.hxx>
37 #include <frmfmt.hxx>
38 #include <SwUndoFmt.hxx>
39 #include <SwRewriter.hxx>
40 #include <swundo.hxx>
41 #include <undobj.hxx>
42 #include <fmtcol.hxx>
43 #include <tools/string.hxx>
44 #include <doc.hxx>
45 #include <comcore.hrc>
47 SwUndoFmtCreate::SwUndoFmtCreate
48 (SwUndoId nUndoId, SwFmt * _pNew, SwFmt * _pDerivedFrom, SwDoc * _pDoc)
49 : SwUndo(nUndoId), pNew(_pNew),
50 pDoc(_pDoc), pNewSet(NULL), nId(0), bAuto(FALSE)
52 if (_pDerivedFrom)
53 sDerivedFrom = _pDerivedFrom->GetName();
56 SwUndoFmtCreate::~SwUndoFmtCreate()
60 void SwUndoFmtCreate::Undo(SwUndoIter &)
62 if (pNew)
64 if (sNewName.Len() == 0 && pNew)
65 sNewName = pNew->GetName();
67 if (sNewName.Len() > 0)
68 pNew = Find(sNewName);
70 if (pNew)
72 pNewSet = new SfxItemSet(pNew->GetAttrSet());
73 nId = pNew->GetPoolFmtId() & COLL_GET_RANGE_BITS;
74 bAuto = pNew->IsAuto();
76 BOOL bDoesUndo = pDoc->DoesUndo();
78 pDoc->DoUndo(FALSE);
79 Delete();
80 pDoc->DoUndo(bDoesUndo);
85 void SwUndoFmtCreate::Redo(SwUndoIter &)
87 BOOL bDoesUndo = pDoc->DoesUndo();
89 pDoc->DoUndo(FALSE);
90 SwFmt * pDerivedFrom = Find(sDerivedFrom);
91 SwFmt * pFmt = Create(pDerivedFrom);
93 if (pFmt && pNewSet)
95 pFmt->SetAuto(bAuto);
96 pDoc->ChgFmt(*pFmt, *pNewSet);
97 pFmt->SetPoolFmtId((pFmt->GetPoolFmtId()
98 & ~COLL_GET_RANGE_BITS)
99 | nId);
101 pNew = pFmt;
103 else
104 pNew = NULL;
106 pDoc->DoUndo(bDoesUndo);
109 SwRewriter SwUndoFmtCreate::GetRewriter() const
111 if (sNewName.Len() == 0 && pNew)
112 sNewName = pNew->GetName();
114 SwRewriter aRewriter;
116 aRewriter.AddRule(UNDO_ARG1, sNewName);
118 return aRewriter;
121 SwUndoFmtDelete::SwUndoFmtDelete
122 (SwUndoId nUndoId, SwFmt * _pOld, SwDoc * _pDoc)
123 : SwUndo(nUndoId),
124 pDoc(_pDoc), sOldName(_pOld->GetName()),
125 aOldSet(_pOld->GetAttrSet())
127 sDerivedFrom = _pOld->DerivedFrom()->GetName();
128 nId = _pOld->GetPoolFmtId() & COLL_GET_RANGE_BITS;
129 bAuto = _pOld->IsAuto();
132 SwUndoFmtDelete::~SwUndoFmtDelete()
136 void SwUndoFmtDelete::Undo(SwUndoIter &)
138 BOOL bDoesUndo = pDoc->DoesUndo();
140 pDoc->DoUndo(FALSE);
142 SwFmt * pDerivedFrom = Find(sDerivedFrom);
144 SwFmt * pFmt = Create(pDerivedFrom);
146 if (pFmt)
148 pDoc->ChgFmt(*pFmt, aOldSet);
149 pFmt->SetAuto(bAuto);
150 pFmt->SetPoolFmtId((pFmt->GetPoolFmtId() &
151 ~COLL_GET_RANGE_BITS)
152 | nId);
156 pDoc->DoUndo(bDoesUndo);
159 void SwUndoFmtDelete::Redo(SwUndoIter &)
161 SwFmt * pOld = Find(sOldName);
163 if (pOld)
165 BOOL bDoesUndo = pDoc->DoesUndo();
167 pDoc->DoUndo(FALSE);
168 Delete(pOld);
169 pDoc->DoUndo(bDoesUndo);
173 SwRewriter SwUndoFmtDelete::GetRewriter() const
175 SwRewriter aRewriter;
177 aRewriter.AddRule(UNDO_ARG1, sOldName);
179 return aRewriter;
182 SwUndoRenameFmt::SwUndoRenameFmt(SwUndoId nUndoId,
183 const String & _sOldName,
184 const String & _sNewName,
185 SwDoc * _pDoc)
186 : SwUndo(nUndoId), sOldName(_sOldName),
187 sNewName(_sNewName), pDoc(_pDoc)
192 SwUndoRenameFmt::~SwUndoRenameFmt()
196 void SwUndoRenameFmt::Undo(SwUndoIter &)
198 SwFmt * pFmt = Find(sNewName);
200 if (pFmt)
202 BOOL bDoesUndo = pDoc->DoesUndo();
204 pDoc->DoUndo(FALSE);
205 pDoc->RenameFmt(*pFmt, sOldName, TRUE);
206 pDoc->DoUndo(bDoesUndo);
210 void SwUndoRenameFmt::Redo(SwUndoIter &)
212 SwFmt * pFmt = Find(sOldName);
214 if (pFmt)
216 BOOL bDoesUndo = pDoc->DoesUndo();
218 pDoc->DoUndo(FALSE);
219 pDoc->RenameFmt(*pFmt, sNewName, TRUE);
220 pDoc->DoUndo(bDoesUndo);
224 SwRewriter SwUndoRenameFmt::GetRewriter() const
226 SwRewriter aRewriter;
228 aRewriter.AddRule(UNDO_ARG1, sOldName);
229 aRewriter.AddRule(UNDO_ARG2, SW_RES(STR_YIELDS));
230 aRewriter.AddRule(UNDO_ARG3, sNewName);
232 return aRewriter;
235 SwUndoTxtFmtCollCreate::SwUndoTxtFmtCollCreate
236 (SwTxtFmtColl * _pNew, SwTxtFmtColl * _pDerivedFrom, SwDoc * _pDoc)
237 : SwUndoFmtCreate(UNDO_TXTFMTCOL_CREATE, _pNew, _pDerivedFrom, _pDoc)
241 SwFmt * SwUndoTxtFmtCollCreate::Create(SwFmt * pDerivedFrom)
243 return pDoc->MakeTxtFmtColl(sNewName, (SwTxtFmtColl *)pDerivedFrom, TRUE);
246 void SwUndoTxtFmtCollCreate::Delete()
248 pDoc->DelTxtFmtColl((SwTxtFmtColl *) pNew, TRUE);
251 SwFmt * SwUndoTxtFmtCollCreate::Find(const String & rName) const
253 return pDoc->FindTxtFmtCollByName(rName);
256 SwUndoTxtFmtCollDelete::SwUndoTxtFmtCollDelete(SwTxtFmtColl * _pOld,
257 SwDoc * _pDoc)
258 : SwUndoFmtDelete(UNDO_TXTFMTCOL_DELETE, _pOld, _pDoc)
262 SwFmt * SwUndoTxtFmtCollDelete::Create(SwFmt * pDerivedFrom)
264 return pDoc->MakeTxtFmtColl(sOldName, (SwTxtFmtColl *) pDerivedFrom, TRUE);
267 void SwUndoTxtFmtCollDelete::Delete(SwFmt * pOld)
269 pDoc->DelTxtFmtColl((SwTxtFmtColl *) pOld, TRUE);
272 SwFmt * SwUndoTxtFmtCollDelete::Find(const String & rName) const
274 return pDoc->FindTxtFmtCollByName(rName);
277 SwUndoRenameFmtColl::SwUndoRenameFmtColl(const String & sInitOldName,
278 const String & sInitNewName,
279 SwDoc * _pDoc)
280 : SwUndoRenameFmt(UNDO_TXTFMTCOL_RENAME, sInitOldName, sInitNewName, _pDoc)
284 SwFmt * SwUndoRenameFmtColl::Find(const String & rName) const
286 return pDoc->FindTxtFmtCollByName(rName);
289 SwUndoCharFmtCreate::SwUndoCharFmtCreate(SwCharFmt * pNewFmt,
290 SwCharFmt * pDerivedFrom,
291 SwDoc * pDocument)
292 : SwUndoFmtCreate(UNDO_CHARFMT_CREATE, pNewFmt, pDerivedFrom, pDocument)
296 SwFmt * SwUndoCharFmtCreate::Create(SwFmt * pDerivedFrom)
298 return pDoc->MakeCharFmt(sNewName, (SwCharFmt *) pDerivedFrom, TRUE);
301 void SwUndoCharFmtCreate::Delete()
303 pDoc->DelCharFmt((SwCharFmt *) pNew, TRUE);
306 SwFmt * SwUndoCharFmtCreate::Find(const String & rName) const
308 return pDoc->FindCharFmtByName(rName);
311 SwUndoCharFmtDelete::SwUndoCharFmtDelete(SwCharFmt * pOld, SwDoc * pDocument)
312 : SwUndoFmtDelete(UNDO_CHARFMT_DELETE, pOld, pDocument)
316 SwFmt * SwUndoCharFmtDelete::Create(SwFmt * pDerivedFrom)
318 return pDoc->MakeCharFmt(sOldName, (SwCharFmt *) pDerivedFrom, TRUE);
321 void SwUndoCharFmtDelete::Delete(SwFmt * pFmt)
323 pDoc->DelCharFmt((SwCharFmt *) pFmt, TRUE);
326 SwFmt * SwUndoCharFmtDelete::Find(const String & rName) const
328 return pDoc->FindCharFmtByName(rName);
331 SwUndoRenameCharFmt::SwUndoRenameCharFmt(const String & sInitOldName,
332 const String & sInitNewName,
333 SwDoc * pDocument)
334 : SwUndoRenameFmt(UNDO_CHARFMT_RENAME, sInitOldName, sInitNewName, pDocument)
338 SwFmt * SwUndoRenameCharFmt::Find(const String & rName) const
340 return pDoc->FindCharFmtByName(rName);
343 SwUndoFrmFmtCreate::SwUndoFrmFmtCreate(SwFrmFmt * pNewFmt,
344 SwFrmFmt * pDerivedFrom,
345 SwDoc * pDocument)
346 : SwUndoFmtCreate(UNDO_FRMFMT_CREATE, pNewFmt, pDerivedFrom, pDocument),
347 bAuto(pNewFmt->IsAuto())
351 SwFmt * SwUndoFrmFmtCreate::Create(SwFmt * pDerivedFrom)
353 return pDoc->MakeFrmFmt(sNewName, (SwFrmFmt *) pDerivedFrom, TRUE, bAuto);
356 void SwUndoFrmFmtCreate::Delete()
358 pDoc->DelFrmFmt((SwFrmFmt *) pNew, TRUE);
361 SwFmt * SwUndoFrmFmtCreate::Find(const String & rName) const
363 return pDoc->FindFrmFmtByName(rName);
366 SwUndoFrmFmtDelete::SwUndoFrmFmtDelete(SwFrmFmt * pOld, SwDoc * pDocument)
367 : SwUndoFmtDelete(UNDO_FRMFMT_DELETE, pOld, pDocument)
371 SwFmt * SwUndoFrmFmtDelete::Create(SwFmt * pDerivedFrom)
373 return pDoc->MakeFrmFmt(sOldName, (SwFrmFmt *) pDerivedFrom, TRUE);
376 void SwUndoFrmFmtDelete::Delete(SwFmt * pFmt)
378 pDoc->DelFrmFmt((SwFrmFmt *) pFmt, TRUE);
381 SwFmt * SwUndoFrmFmtDelete::Find(const String & rName) const
383 return pDoc->FindFrmFmtByName(rName);
386 SwUndoRenameFrmFmt::SwUndoRenameFrmFmt(const String & sInitOldName,
387 const String & sInitNewName,
388 SwDoc * pDocument)
389 : SwUndoRenameFmt(UNDO_FRMFMT_RENAME, sInitOldName, sInitNewName, pDocument)
393 SwFmt * SwUndoRenameFrmFmt::Find(const String & rName) const
395 return pDoc->FindFrmFmtByName(rName);
398 SwUndoNumruleCreate::SwUndoNumruleCreate(const SwNumRule * _pNew,
399 SwDoc * _pDoc)
400 : SwUndo(UNDO_NUMRULE_CREATE), pNew(_pNew), aNew(*_pNew), pDoc(_pDoc),
401 bInitialized(false)
405 void SwUndoNumruleCreate::Undo(SwUndoIter &)
407 BOOL bDoesUndo = pDoc->DoesUndo();
409 pDoc->DoUndo(FALSE);
411 if (! bInitialized)
413 aNew = *pNew;
414 bInitialized = true;
417 pDoc->DelNumRule(aNew.GetName(), TRUE);
418 pDoc->DoUndo(bDoesUndo);
421 void SwUndoNumruleCreate::Redo(SwUndoIter &)
423 BOOL bDoesUndo = pDoc->DoesUndo();
425 pDoc->DoUndo(FALSE);
426 pDoc->MakeNumRule(aNew.GetName(), &aNew, TRUE);
427 pDoc->DoUndo(bDoesUndo);
430 SwRewriter SwUndoNumruleCreate::GetRewriter() const
432 SwRewriter aResult;
434 if (! bInitialized)
436 aNew = *pNew;
437 bInitialized = true;
440 aResult.AddRule(UNDO_ARG1, aNew.GetName());
442 return aResult;
445 SwUndoNumruleDelete::SwUndoNumruleDelete(const SwNumRule & rRule,
446 SwDoc * _pDoc)
447 : SwUndo(UNDO_NUMRULE_DELETE), aOld(rRule), pDoc(_pDoc)
451 void SwUndoNumruleDelete::Undo(SwUndoIter &)
453 BOOL bDoesUndo = pDoc->DoesUndo();
455 pDoc->DoUndo(FALSE);
456 pDoc->MakeNumRule(aOld.GetName(), &aOld, TRUE);
457 pDoc->DoUndo(bDoesUndo);
460 void SwUndoNumruleDelete::Redo(SwUndoIter &)
462 BOOL bDoesUndo = pDoc->DoesUndo();
464 pDoc->DoUndo(FALSE);
465 pDoc->DelNumRule(aOld.GetName(), TRUE);
466 pDoc->DoUndo(bDoesUndo);
469 SwRewriter SwUndoNumruleDelete::GetRewriter() const
471 SwRewriter aResult;
473 aResult.AddRule(UNDO_ARG1, aOld.GetName());
475 return aResult;
478 SwUndoNumruleRename::SwUndoNumruleRename(const String & _aOldName,
479 const String & _aNewName,
480 SwDoc * _pDoc)
481 : SwUndo(UNDO_NUMRULE_RENAME), aOldName(_aOldName), aNewName(_aNewName),
482 pDoc(_pDoc)
486 void SwUndoNumruleRename::Undo(SwUndoIter &)
488 BOOL bDoesUndo = pDoc->DoesUndo();
490 pDoc->DoUndo(FALSE);
491 pDoc->RenameNumRule(aNewName, aOldName, TRUE);
492 pDoc->DoUndo(bDoesUndo);
495 void SwUndoNumruleRename::Redo(SwUndoIter &)
497 BOOL bDoesUndo = pDoc->DoesUndo();
499 pDoc->DoUndo(FALSE);
500 pDoc->RenameNumRule(aOldName, aNewName, TRUE);
501 pDoc->DoUndo(bDoesUndo);
504 SwRewriter SwUndoNumruleRename::GetRewriter() const
506 SwRewriter aRewriter;
508 aRewriter.AddRule(UNDO_ARG1, aOldName);
509 aRewriter.AddRule(UNDO_ARG2, SW_RES(STR_YIELDS));
510 aRewriter.AddRule(UNDO_ARG3, aNewName);
512 return aRewriter;