1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SwUndoFmt.cxx,v $
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>
38 #include <SwUndoFmt.hxx>
39 #include <SwRewriter.hxx>
43 #include <tools/string.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
)
53 sDerivedFrom
= _pDerivedFrom
->GetName();
56 SwUndoFmtCreate::~SwUndoFmtCreate()
60 void SwUndoFmtCreate::Undo(SwUndoIter
&)
64 if (sNewName
.Len() == 0 && pNew
)
65 sNewName
= pNew
->GetName();
67 if (sNewName
.Len() > 0)
68 pNew
= Find(sNewName
);
72 pNewSet
= new SfxItemSet(pNew
->GetAttrSet());
73 nId
= pNew
->GetPoolFmtId() & COLL_GET_RANGE_BITS
;
74 bAuto
= pNew
->IsAuto();
76 BOOL bDoesUndo
= pDoc
->DoesUndo();
80 pDoc
->DoUndo(bDoesUndo
);
85 void SwUndoFmtCreate::Redo(SwUndoIter
&)
87 BOOL bDoesUndo
= pDoc
->DoesUndo();
90 SwFmt
* pDerivedFrom
= Find(sDerivedFrom
);
91 SwFmt
* pFmt
= Create(pDerivedFrom
);
96 pDoc
->ChgFmt(*pFmt
, *pNewSet
);
97 pFmt
->SetPoolFmtId((pFmt
->GetPoolFmtId()
98 & ~COLL_GET_RANGE_BITS
)
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
);
121 SwUndoFmtDelete::SwUndoFmtDelete
122 (SwUndoId nUndoId
, SwFmt
* _pOld
, SwDoc
* _pDoc
)
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();
142 SwFmt
* pDerivedFrom
= Find(sDerivedFrom
);
144 SwFmt
* pFmt
= Create(pDerivedFrom
);
148 pDoc
->ChgFmt(*pFmt
, aOldSet
);
149 pFmt
->SetAuto(bAuto
);
150 pFmt
->SetPoolFmtId((pFmt
->GetPoolFmtId() &
151 ~COLL_GET_RANGE_BITS
)
156 pDoc
->DoUndo(bDoesUndo
);
159 void SwUndoFmtDelete::Redo(SwUndoIter
&)
161 SwFmt
* pOld
= Find(sOldName
);
165 BOOL bDoesUndo
= pDoc
->DoesUndo();
169 pDoc
->DoUndo(bDoesUndo
);
173 SwRewriter
SwUndoFmtDelete::GetRewriter() const
175 SwRewriter aRewriter
;
177 aRewriter
.AddRule(UNDO_ARG1
, sOldName
);
182 SwUndoRenameFmt::SwUndoRenameFmt(SwUndoId nUndoId
,
183 const String
& _sOldName
,
184 const String
& _sNewName
,
186 : SwUndo(nUndoId
), sOldName(_sOldName
),
187 sNewName(_sNewName
), pDoc(_pDoc
)
192 SwUndoRenameFmt::~SwUndoRenameFmt()
196 void SwUndoRenameFmt::Undo(SwUndoIter
&)
198 SwFmt
* pFmt
= Find(sNewName
);
202 BOOL bDoesUndo
= pDoc
->DoesUndo();
205 pDoc
->RenameFmt(*pFmt
, sOldName
, TRUE
);
206 pDoc
->DoUndo(bDoesUndo
);
210 void SwUndoRenameFmt::Redo(SwUndoIter
&)
212 SwFmt
* pFmt
= Find(sOldName
);
216 BOOL bDoesUndo
= pDoc
->DoesUndo();
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
);
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
,
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
,
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
,
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
,
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
,
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
,
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
,
400 : SwUndo(UNDO_NUMRULE_CREATE
), pNew(_pNew
), aNew(*_pNew
), pDoc(_pDoc
),
405 void SwUndoNumruleCreate::Undo(SwUndoIter
&)
407 BOOL bDoesUndo
= pDoc
->DoesUndo();
417 pDoc
->DelNumRule(aNew
.GetName(), TRUE
);
418 pDoc
->DoUndo(bDoesUndo
);
421 void SwUndoNumruleCreate::Redo(SwUndoIter
&)
423 BOOL bDoesUndo
= pDoc
->DoesUndo();
426 pDoc
->MakeNumRule(aNew
.GetName(), &aNew
, TRUE
);
427 pDoc
->DoUndo(bDoesUndo
);
430 SwRewriter
SwUndoNumruleCreate::GetRewriter() const
440 aResult
.AddRule(UNDO_ARG1
, aNew
.GetName());
445 SwUndoNumruleDelete::SwUndoNumruleDelete(const SwNumRule
& rRule
,
447 : SwUndo(UNDO_NUMRULE_DELETE
), aOld(rRule
), pDoc(_pDoc
)
451 void SwUndoNumruleDelete::Undo(SwUndoIter
&)
453 BOOL bDoesUndo
= pDoc
->DoesUndo();
456 pDoc
->MakeNumRule(aOld
.GetName(), &aOld
, TRUE
);
457 pDoc
->DoUndo(bDoesUndo
);
460 void SwUndoNumruleDelete::Redo(SwUndoIter
&)
462 BOOL bDoesUndo
= pDoc
->DoesUndo();
465 pDoc
->DelNumRule(aOld
.GetName(), TRUE
);
466 pDoc
->DoUndo(bDoesUndo
);
469 SwRewriter
SwUndoNumruleDelete::GetRewriter() const
473 aResult
.AddRule(UNDO_ARG1
, aOld
.GetName());
478 SwUndoNumruleRename::SwUndoNumruleRename(const String
& _aOldName
,
479 const String
& _aNewName
,
481 : SwUndo(UNDO_NUMRULE_RENAME
), aOldName(_aOldName
), aNewName(_aNewName
),
486 void SwUndoNumruleRename::Undo(SwUndoIter
&)
488 BOOL bDoesUndo
= pDoc
->DoesUndo();
491 pDoc
->RenameNumRule(aNewName
, aOldName
, TRUE
);
492 pDoc
->DoUndo(bDoesUndo
);
495 void SwUndoNumruleRename::Redo(SwUndoIter
&)
497 BOOL bDoesUndo
= pDoc
->DoesUndo();
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
);