Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / source / core / undo / SwUndoField.cxx
blob10193dd89135f2f9d7a8cf78f46571fe489139e5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <SwUndoField.hxx>
22 #include <swundo.hxx>
23 #include <doc.hxx>
24 #include <IDocumentUndoRedo.hxx>
25 #include <DocumentFieldsManager.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 #include <pam.hxx>
34 using namespace ::com::sun::star::uno;
36 SwUndoField::SwUndoField(const SwPosition & rPos )
37 : SwUndo(SwUndoId::FIELD, rPos.GetDoc())
39 nNodeIndex = rPos.nNode.GetIndex();
40 nOffset = rPos.nContent.GetIndex();
41 pDoc = rPos.GetDoc();
44 SwUndoField::~SwUndoField()
48 SwPosition SwUndoField::GetPosition()
50 SwNode * pNode = pDoc->GetNodes()[nNodeIndex];
51 SwNodeIndex aNodeIndex(*pNode);
52 SwIndex aIndex(pNode->GetContentNode(), nOffset);
53 SwPosition aResult(aNodeIndex, aIndex);
55 return aResult;
58 SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
59 const SwField & rOldField,
60 const SwField & rNewField,
61 SwMsgPoolItem * _pHint, bool _bUpdate)
62 : SwUndoField(rPos)
63 , pOldField(rOldField.CopyField())
64 , pNewField(rNewField.CopyField())
65 , pHint(_pHint)
66 , bUpdate(_bUpdate)
68 OSL_ENSURE(pOldField, "No old field!");
69 OSL_ENSURE(pNewField, "No new field!");
70 OSL_ENSURE(pDoc, "No document!");
73 SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
75 delete pOldField;
76 delete pNewField;
79 void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
81 SwTextField * pTextField = sw::DocumentFieldsManager::GetTextFieldAtPos(GetPosition());
82 const SwField * pField = pTextField ? pTextField->GetFormatField().GetField() : nullptr;
84 if (pField)
86 pDoc->getIDocumentFieldsAccess().UpdateField(pTextField, *pOldField, pHint, bUpdate);
90 void SwUndoFieldFromDoc::DoImpl()
92 SwTextField * pTextField = sw::DocumentFieldsManager::GetTextFieldAtPos(GetPosition());
93 const SwField * pField = pTextField ? pTextField->GetFormatField().GetField() : nullptr;
95 if (pField)
97 pDoc->getIDocumentFieldsAccess().UpdateField(pTextField, *pNewField, pHint, bUpdate);
98 SwFormatField* pDstFormatField = const_cast<SwFormatField*>(&pTextField->GetFormatField());
100 if ( pDoc->getIDocumentFieldsAccess().GetFieldType(SwFieldIds::Postit, aEmptyOUStr, false) == pDstFormatField->GetField()->GetTyp() )
101 pDoc->GetDocShell()->Broadcast( SwFormatFieldHint( pDstFormatField, SwFormatFieldHintWhich::INSERTED ) );
105 void SwUndoFieldFromDoc::RedoImpl(::sw::UndoRedoContext &)
107 DoImpl();
110 void SwUndoFieldFromDoc::RepeatImpl(::sw::RepeatContext &)
112 ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
113 DoImpl();
116 SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
117 const Any & rOldVal, const Any & rNewVal,
118 sal_uInt16 _nWhich)
119 : SwUndoField(rPos), aOldVal(rOldVal), aNewVal(rNewVal), nWhich(_nWhich)
123 SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
127 void SwUndoFieldFromAPI::UndoImpl(::sw::UndoRedoContext &)
129 SwField * pField = sw::DocumentFieldsManager::GetFieldAtPos(GetPosition());
131 if (pField)
132 pField->PutValue(aOldVal, nWhich);
135 void SwUndoFieldFromAPI::DoImpl()
137 SwField * pField = sw::DocumentFieldsManager::GetFieldAtPos(GetPosition());
139 if (pField)
140 pField->PutValue(aNewVal, nWhich);
143 void SwUndoFieldFromAPI::RedoImpl(::sw::UndoRedoContext &)
145 DoImpl();
148 void SwUndoFieldFromAPI::RepeatImpl(::sw::RepeatContext &)
150 DoImpl();
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */