update dev300-m58
[ooovba.git] / sw / source / core / undo / SwUndoField.cxx
bloba324432c9002f6b39efebb2d906e949ba9a0cfc6
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: SwUndoField.cxx,v $
10 * $Revision: 1.11 $
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"
33 #include <tools/rtti.hxx>
34 #include <SwUndoField.hxx>
35 #include <swundo.hxx>
36 #include <doc.hxx>
37 #include <txtfld.hxx>
38 #include <fldbas.hxx>
39 #include <ndtxt.hxx>
40 #include <fmtfld.hxx>
41 #include <dbfld.hxx>
42 #include <docsh.hxx>
44 using namespace ::com::sun::star::uno;
46 SwUndoField::SwUndoField(const SwPosition & rPos, SwUndoId _nId )
47 : SwUndo(_nId)
49 nNodeIndex = rPos.nNode.GetIndex();
50 nOffset = rPos.nContent.GetIndex();
51 pDoc = rPos.GetDoc();
54 SwUndoField::~SwUndoField()
58 SwPosition SwUndoField::GetPosition()
60 SwNode * pNode = pDoc->GetNodes()[nNodeIndex];
61 SwNodeIndex aNodeIndex(*pNode);
62 SwIndex aIndex(pNode->GetCntntNode(), nOffset);
63 SwPosition aResult(aNodeIndex, aIndex);
65 return aResult;
68 SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
69 const SwField & _aOldField,
70 const SwField & _aNewField,
71 SwMsgPoolItem * _pHnt, BOOL _bUpdate, SwUndoId _nId)
72 : SwUndoField(rPos,_nId), pOldField(_aOldField.Copy()),
73 pNewField(_aNewField.Copy()), pHnt(_pHnt),
74 bUpdate(_bUpdate)
76 ASSERT(pOldField, "No old field!");
77 ASSERT(pNewField, "No new field!");
78 ASSERT(pDoc, "No document!");
81 SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
83 delete pOldField;
84 delete pNewField;
87 void SwUndoFieldFromDoc::Undo( SwUndoIter& )
89 SwTxtFld * pTxtFld = SwDoc::GetTxtFld(GetPosition());
90 const SwField * pField = pTxtFld->GetFld().GetFld();
92 if (pField)
94 BOOL bUndo = pDoc->DoesUndo();
96 pDoc->DoUndo(FALSE);
97 pDoc->UpdateFld(pTxtFld, *pOldField, pHnt, bUpdate);
98 pDoc->DoUndo(bUndo);
102 void SwUndoFieldFromDoc::Redo( SwUndoIter& )
104 SwTxtFld * pTxtFld = SwDoc::GetTxtFld(GetPosition());
105 const SwField * pField = pTxtFld->GetFld().GetFld();
107 if (pField)
109 BOOL bUndo = pDoc->DoesUndo();
111 pDoc->DoUndo(FALSE);
112 pDoc->UpdateFld(pTxtFld, *pNewField, pHnt, bUpdate);
113 SwFmtFld* pDstFmtFld = (SwFmtFld*)&pTxtFld->GetFld();
115 if ( pDoc->GetFldType(RES_POSTITFLD, aEmptyStr,false) == pDstFmtFld->GetFld()->GetTyp() )
116 pDoc->GetDocShell()->Broadcast( SwFmtFldHint( pDstFmtFld, SWFMTFLD_INSERTED ) );
117 pDoc->DoUndo(bUndo);
121 void SwUndoFieldFromDoc::Repeat(SwUndoIter & rIt)
123 Redo(rIt);
126 SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
127 const Any & rOldVal, const Any & rNewVal,
128 USHORT _nWhich)
129 : SwUndoField(rPos), aOldVal(rOldVal), aNewVal(rNewVal), nWhich(_nWhich)
133 SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
137 void SwUndoFieldFromAPI::Undo( SwUndoIter& )
139 SwField * pField = SwDoc::GetField(GetPosition());
141 if (pField)
142 pField->PutValue(aOldVal, nWhich);
145 void SwUndoFieldFromAPI::Redo( SwUndoIter& )
147 SwField * pField = SwDoc::GetField(GetPosition());
149 if (pField)
150 pField->PutValue(aNewVal, nWhich);
154 void SwUndoFieldFromAPI::Repeat(SwUndoIter & rIter)
156 Redo(rIter);