xref: /aoo41x/main/sw/source/core/undo/SwUndoField.cxx (revision 69a74367)
1efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10efeef26fSAndrew Rist  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12efeef26fSAndrew Rist  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19efeef26fSAndrew Rist  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <tools/rtti.hxx>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <SwUndoField.hxx>
30cdf0e10cSrcweir #include <swundo.hxx>
31cdf0e10cSrcweir #include <doc.hxx>
32cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
33cdf0e10cSrcweir #include <txtfld.hxx>
34cdf0e10cSrcweir #include <fldbas.hxx>
35cdf0e10cSrcweir #include <ndtxt.hxx>
36cdf0e10cSrcweir #include <fmtfld.hxx>
37cdf0e10cSrcweir #include <dbfld.hxx>
38cdf0e10cSrcweir #include <docsh.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::com::sun::star::uno;
41cdf0e10cSrcweir 
SwUndoField(const SwPosition & rPos,SwUndoId _nId)42cdf0e10cSrcweir SwUndoField::SwUndoField(const SwPosition & rPos, SwUndoId _nId )
43cdf0e10cSrcweir     : SwUndo(_nId)
44cdf0e10cSrcweir {
45cdf0e10cSrcweir     nNodeIndex = rPos.nNode.GetIndex();
46cdf0e10cSrcweir     nOffset = rPos.nContent.GetIndex();
47cdf0e10cSrcweir     pDoc = rPos.GetDoc();
48cdf0e10cSrcweir }
49cdf0e10cSrcweir 
~SwUndoField()50cdf0e10cSrcweir SwUndoField::~SwUndoField()
51cdf0e10cSrcweir {
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
GetPosition()54cdf0e10cSrcweir SwPosition SwUndoField::GetPosition()
55cdf0e10cSrcweir {
56cdf0e10cSrcweir     SwNode * pNode = pDoc->GetNodes()[nNodeIndex];
57cdf0e10cSrcweir     SwNodeIndex aNodeIndex(*pNode);
58cdf0e10cSrcweir     SwIndex aIndex(pNode->GetCntntNode(), nOffset);
59cdf0e10cSrcweir     SwPosition aResult(aNodeIndex, aIndex);
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     return aResult;
62cdf0e10cSrcweir }
63cdf0e10cSrcweir 
SwUndoFieldFromDoc(const SwPosition & rPos,const SwField & rOldField,const SwField & rNewField,SwMsgPoolItem * _pHnt,sal_Bool _bUpdate,SwUndoId _nId)64cdf0e10cSrcweir SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
65cdf0e10cSrcweir                          const SwField & rOldField,
66cdf0e10cSrcweir                          const SwField & rNewField,
67cdf0e10cSrcweir                          SwMsgPoolItem * _pHnt, sal_Bool _bUpdate, SwUndoId _nId)
68cdf0e10cSrcweir     : SwUndoField(rPos,_nId)
69cdf0e10cSrcweir     , pOldField(rOldField.CopyField())
70cdf0e10cSrcweir     , pNewField(rNewField.CopyField())
71cdf0e10cSrcweir     , pHnt(_pHnt)
72cdf0e10cSrcweir     , bUpdate(_bUpdate)
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     ASSERT(pOldField, "No old field!");
75cdf0e10cSrcweir     ASSERT(pNewField, "No new field!");
76cdf0e10cSrcweir     ASSERT(pDoc, "No document!");
77cdf0e10cSrcweir }
78cdf0e10cSrcweir 
~SwUndoFieldFromDoc()79cdf0e10cSrcweir SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     delete pOldField;
82cdf0e10cSrcweir     delete pNewField;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
UndoImpl(::sw::UndoRedoContext &)85cdf0e10cSrcweir void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
86cdf0e10cSrcweir {
87*69a74367SOliver-Rainer Wittmann     SwTxtFld * pTxtFld = SwDoc::GetTxtFldAtPos(GetPosition());
88c0286415SOliver-Rainer Wittmann     const SwField * pField = pTxtFld->GetFmtFld().GetField();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     if (pField)
91cdf0e10cSrcweir     {
92cdf0e10cSrcweir         pDoc->UpdateFld(pTxtFld, *pOldField, pHnt, bUpdate);
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir }
95cdf0e10cSrcweir 
DoImpl()96cdf0e10cSrcweir void SwUndoFieldFromDoc::DoImpl()
97cdf0e10cSrcweir {
98*69a74367SOliver-Rainer Wittmann     SwTxtFld * pTxtFld = SwDoc::GetTxtFldAtPos(GetPosition());
99c0286415SOliver-Rainer Wittmann     const SwField * pField = pTxtFld->GetFmtFld().GetField();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     if (pField)
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         pDoc->UpdateFld(pTxtFld, *pNewField, pHnt, bUpdate);
104c0286415SOliver-Rainer Wittmann 	    SwFmtFld* pDstFmtFld = (SwFmtFld*)&pTxtFld->GetFmtFld();
105cdf0e10cSrcweir 
106c0286415SOliver-Rainer Wittmann 		if ( pDoc->GetFldType(RES_POSTITFLD, aEmptyStr,false) == pDstFmtFld->GetField()->GetTyp() )
107cdf0e10cSrcweir 			pDoc->GetDocShell()->Broadcast( SwFmtFldHint( pDstFmtFld, SWFMTFLD_INSERTED ) );
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
RedoImpl(::sw::UndoRedoContext &)111cdf0e10cSrcweir void SwUndoFieldFromDoc::RedoImpl(::sw::UndoRedoContext &)
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     DoImpl();
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
RepeatImpl(::sw::RepeatContext &)116cdf0e10cSrcweir void SwUndoFieldFromDoc::RepeatImpl(::sw::RepeatContext &)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir     ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
119cdf0e10cSrcweir     DoImpl();
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
SwUndoFieldFromAPI(const SwPosition & rPos,const Any & rOldVal,const Any & rNewVal,sal_uInt16 _nWhich)122cdf0e10cSrcweir SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
123cdf0e10cSrcweir                                        const Any & rOldVal, const Any & rNewVal,
124cdf0e10cSrcweir                                        sal_uInt16 _nWhich)
125cdf0e10cSrcweir     : SwUndoField(rPos), aOldVal(rOldVal), aNewVal(rNewVal), nWhich(_nWhich)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
~SwUndoFieldFromAPI()129cdf0e10cSrcweir SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
130cdf0e10cSrcweir {
131cdf0e10cSrcweir }
132cdf0e10cSrcweir 
UndoImpl(::sw::UndoRedoContext &)133cdf0e10cSrcweir void SwUndoFieldFromAPI::UndoImpl(::sw::UndoRedoContext &)
134cdf0e10cSrcweir {
135*69a74367SOliver-Rainer Wittmann     SwField * pField = SwDoc::GetFieldAtPos(GetPosition());
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     if (pField)
138cdf0e10cSrcweir         pField->PutValue(aOldVal, nWhich);
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
DoImpl()141cdf0e10cSrcweir void SwUndoFieldFromAPI::DoImpl()
142cdf0e10cSrcweir {
143*69a74367SOliver-Rainer Wittmann     SwField * pField = SwDoc::GetFieldAtPos(GetPosition());
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     if (pField)
146cdf0e10cSrcweir         pField->PutValue(aNewVal, nWhich);
147cdf0e10cSrcweir }
148cdf0e10cSrcweir 
RedoImpl(::sw::UndoRedoContext &)149cdf0e10cSrcweir void SwUndoFieldFromAPI::RedoImpl(::sw::UndoRedoContext &)
150cdf0e10cSrcweir {
151cdf0e10cSrcweir     DoImpl();
152cdf0e10cSrcweir }
153cdf0e10cSrcweir 
RepeatImpl(::sw::RepeatContext &)154cdf0e10cSrcweir void SwUndoFieldFromAPI::RepeatImpl(::sw::RepeatContext &)
155cdf0e10cSrcweir {
156cdf0e10cSrcweir     DoImpl();
157cdf0e10cSrcweir }
158cdf0e10cSrcweir 
159