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