xref: /aoo42x/main/sw/source/core/undo/SwUndoField.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 
31 #include <tools/rtti.hxx>
32 
33 #include <SwUndoField.hxx>
34 #include <swundo.hxx>
35 #include <doc.hxx>
36 #include <IDocumentUndoRedo.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>
43 
44 using namespace ::com::sun::star::uno;
45 
46 SwUndoField::SwUndoField(const SwPosition & rPos, SwUndoId _nId )
47     : SwUndo(_nId)
48 {
49     nNodeIndex = rPos.nNode.GetIndex();
50     nOffset = rPos.nContent.GetIndex();
51     pDoc = rPos.GetDoc();
52 }
53 
54 SwUndoField::~SwUndoField()
55 {
56 }
57 
58 SwPosition SwUndoField::GetPosition()
59 {
60     SwNode * pNode = pDoc->GetNodes()[nNodeIndex];
61     SwNodeIndex aNodeIndex(*pNode);
62     SwIndex aIndex(pNode->GetCntntNode(), nOffset);
63     SwPosition aResult(aNodeIndex, aIndex);
64 
65     return aResult;
66 }
67 
68 SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
69                          const SwField & rOldField,
70                          const SwField & rNewField,
71                          SwMsgPoolItem * _pHnt, sal_Bool _bUpdate, SwUndoId _nId)
72     : SwUndoField(rPos,_nId)
73     , pOldField(rOldField.CopyField())
74     , pNewField(rNewField.CopyField())
75     , pHnt(_pHnt)
76     , bUpdate(_bUpdate)
77 {
78     ASSERT(pOldField, "No old field!");
79     ASSERT(pNewField, "No new field!");
80     ASSERT(pDoc, "No document!");
81 }
82 
83 SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
84 {
85     delete pOldField;
86     delete pNewField;
87 }
88 
89 void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
90 {
91     SwTxtFld * pTxtFld = SwDoc::GetTxtFld(GetPosition());
92     const SwField * pField = pTxtFld->GetFld().GetFld();
93 
94     if (pField)
95     {
96         pDoc->UpdateFld(pTxtFld, *pOldField, pHnt, bUpdate);
97     }
98 }
99 
100 void SwUndoFieldFromDoc::DoImpl()
101 {
102     SwTxtFld * pTxtFld = SwDoc::GetTxtFld(GetPosition());
103     const SwField * pField = pTxtFld->GetFld().GetFld();
104 
105     if (pField)
106     {
107         pDoc->UpdateFld(pTxtFld, *pNewField, pHnt, bUpdate);
108 	    SwFmtFld* pDstFmtFld = (SwFmtFld*)&pTxtFld->GetFld();
109 
110 		if ( pDoc->GetFldType(RES_POSTITFLD, aEmptyStr,false) == pDstFmtFld->GetFld()->GetTyp() )
111 			pDoc->GetDocShell()->Broadcast( SwFmtFldHint( pDstFmtFld, SWFMTFLD_INSERTED ) );
112     }
113 }
114 
115 void SwUndoFieldFromDoc::RedoImpl(::sw::UndoRedoContext &)
116 {
117     DoImpl();
118 }
119 
120 void SwUndoFieldFromDoc::RepeatImpl(::sw::RepeatContext &)
121 {
122     ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
123     DoImpl();
124 }
125 
126 SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
127                                        const Any & rOldVal, const Any & rNewVal,
128                                        sal_uInt16 _nWhich)
129     : SwUndoField(rPos), aOldVal(rOldVal), aNewVal(rNewVal), nWhich(_nWhich)
130 {
131 }
132 
133 SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
134 {
135 }
136 
137 void SwUndoFieldFromAPI::UndoImpl(::sw::UndoRedoContext &)
138 {
139     SwField * pField = SwDoc::GetField(GetPosition());
140 
141     if (pField)
142         pField->PutValue(aOldVal, nWhich);
143 }
144 
145 void SwUndoFieldFromAPI::DoImpl()
146 {
147     SwField * pField = SwDoc::GetField(GetPosition());
148 
149     if (pField)
150         pField->PutValue(aNewVal, nWhich);
151 }
152 
153 void SwUndoFieldFromAPI::RedoImpl(::sw::UndoRedoContext &)
154 {
155     DoImpl();
156 }
157 
158 void SwUndoFieldFromAPI::RepeatImpl(::sw::RepeatContext &)
159 {
160     DoImpl();
161 }
162 
163