Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <tools/rtti.hxx>
21 :
22 : #include <SwUndoField.hxx>
23 : #include <swundo.hxx>
24 : #include <doc.hxx>
25 : #include <IDocumentUndoRedo.hxx>
26 : #include <txtfld.hxx>
27 : #include <fldbas.hxx>
28 : #include <ndtxt.hxx>
29 : #include <fmtfld.hxx>
30 : #include <dbfld.hxx>
31 : #include <docsh.hxx>
32 :
33 : using namespace ::com::sun::star::uno;
34 :
35 0 : SwUndoField::SwUndoField(const SwPosition & rPos, SwUndoId _nId )
36 0 : : SwUndo(_nId)
37 : {
38 0 : nNodeIndex = rPos.nNode.GetIndex();
39 0 : nOffset = rPos.nContent.GetIndex();
40 0 : pDoc = rPos.GetDoc();
41 0 : }
42 :
43 0 : SwUndoField::~SwUndoField()
44 : {
45 0 : }
46 :
47 0 : SwPosition SwUndoField::GetPosition()
48 : {
49 0 : SwNode * pNode = pDoc->GetNodes()[nNodeIndex];
50 0 : SwNodeIndex aNodeIndex(*pNode);
51 0 : SwIndex aIndex(pNode->GetCntntNode(), nOffset);
52 0 : SwPosition aResult(aNodeIndex, aIndex);
53 :
54 0 : return aResult;
55 : }
56 :
57 0 : SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
58 : const SwField & rOldField,
59 : const SwField & rNewField,
60 : SwMsgPoolItem * _pHnt, sal_Bool _bUpdate, SwUndoId _nId)
61 : : SwUndoField(rPos,_nId)
62 0 : , pOldField(rOldField.CopyField())
63 0 : , pNewField(rNewField.CopyField())
64 : , pHnt(_pHnt)
65 0 : , bUpdate(_bUpdate)
66 : {
67 : OSL_ENSURE(pOldField, "No old field!");
68 : OSL_ENSURE(pNewField, "No new field!");
69 : OSL_ENSURE(pDoc, "No document!");
70 0 : }
71 :
72 0 : SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
73 : {
74 0 : delete pOldField;
75 0 : delete pNewField;
76 0 : }
77 :
78 0 : void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
79 : {
80 0 : SwTxtFld * pTxtFld = SwDoc::GetTxtFldAtPos(GetPosition());
81 0 : const SwField * pField = pTxtFld ? pTxtFld->GetFmtFld().GetField() : NULL;
82 :
83 0 : if (pField)
84 : {
85 0 : pDoc->UpdateFld(pTxtFld, *pOldField, pHnt, bUpdate);
86 : }
87 0 : }
88 :
89 0 : void SwUndoFieldFromDoc::DoImpl()
90 : {
91 0 : SwTxtFld * pTxtFld = SwDoc::GetTxtFldAtPos(GetPosition());
92 0 : const SwField * pField = pTxtFld ? pTxtFld->GetFmtFld().GetField() : NULL;
93 :
94 0 : if (pField)
95 : {
96 0 : pDoc->UpdateFld(pTxtFld, *pNewField, pHnt, bUpdate);
97 0 : SwFmtFld* pDstFmtFld = (SwFmtFld*)&pTxtFld->GetFmtFld();
98 :
99 0 : if ( pDoc->GetFldType(RES_POSTITFLD, aEmptyOUStr, false) == pDstFmtFld->GetField()->GetTyp() )
100 0 : pDoc->GetDocShell()->Broadcast( SwFmtFldHint( pDstFmtFld, SWFMTFLD_INSERTED ) );
101 : }
102 0 : }
103 :
104 0 : void SwUndoFieldFromDoc::RedoImpl(::sw::UndoRedoContext &)
105 : {
106 0 : DoImpl();
107 0 : }
108 :
109 0 : void SwUndoFieldFromDoc::RepeatImpl(::sw::RepeatContext &)
110 : {
111 0 : ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
112 0 : DoImpl();
113 0 : }
114 :
115 0 : SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
116 : const Any & rOldVal, const Any & rNewVal,
117 : sal_uInt16 _nWhich)
118 0 : : SwUndoField(rPos), aOldVal(rOldVal), aNewVal(rNewVal), nWhich(_nWhich)
119 : {
120 0 : }
121 :
122 0 : SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
123 : {
124 0 : }
125 :
126 0 : void SwUndoFieldFromAPI::UndoImpl(::sw::UndoRedoContext &)
127 : {
128 0 : SwField * pField = SwDoc::GetFieldAtPos(GetPosition());
129 :
130 0 : if (pField)
131 0 : pField->PutValue(aOldVal, nWhich);
132 0 : }
133 :
134 0 : void SwUndoFieldFromAPI::DoImpl()
135 : {
136 0 : SwField * pField = SwDoc::GetFieldAtPos(GetPosition());
137 :
138 0 : if (pField)
139 0 : pField->PutValue(aNewVal, nWhich);
140 0 : }
141 :
142 0 : void SwUndoFieldFromAPI::RedoImpl(::sw::UndoRedoContext &)
143 : {
144 0 : DoImpl();
145 0 : }
146 :
147 0 : void SwUndoFieldFromAPI::RepeatImpl(::sw::RepeatContext &)
148 : {
149 0 : DoImpl();
150 0 : }
151 :
152 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|