android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / undo / SwUndoField.cxx
blob57d615e04f8ae0825794f4053db5fab588f33888
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 <fmtfld.hxx>
29 #include <docsh.hxx>
30 #include <pam.hxx>
31 #include <utility>
32 #include <osl/diagnose.h>
34 using namespace ::com::sun::star::uno;
36 SwUndoField::SwUndoField(const SwPosition & rPos )
37 : SwUndo(SwUndoId::FIELD, &rPos.GetDoc())
39 m_nNodeIndex = rPos.GetNodeIndex();
40 m_nOffset = rPos.GetContentIndex();
41 m_pDoc = &rPos.GetDoc();
44 SwUndoField::~SwUndoField()
48 SwPosition SwUndoField::GetPosition()
50 SwNode * pNode = m_pDoc->GetNodes()[m_nNodeIndex];
51 SwContentIndex aIndex(pNode->GetContentNode(), m_nOffset);
52 SwPosition aResult(*pNode, aIndex);
54 return aResult;
57 SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
58 const SwField & rOldField,
59 const SwField & rNewField,
60 bool _bUpdate)
61 : SwUndoField(rPos)
62 , m_pOldField(rOldField.CopyField())
63 , m_pNewField(rNewField.CopyField())
64 , m_bUpdate(_bUpdate)
66 OSL_ENSURE(m_pOldField, "No old field!");
67 OSL_ENSURE(m_pNewField, "No new field!");
68 OSL_ENSURE(m_pDoc, "No document!");
71 SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
75 void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
77 SwTextField * pTextField = sw::DocumentFieldsManager::GetTextFieldAtPos(GetPosition());
78 const SwField * pField = pTextField ? pTextField->GetFormatField().GetField() : nullptr;
80 if (pField)
82 m_pDoc->getIDocumentFieldsAccess().UpdateField(pTextField, *m_pOldField, m_bUpdate);
86 void SwUndoFieldFromDoc::DoImpl()
88 SwTextField * pTextField = sw::DocumentFieldsManager::GetTextFieldAtPos(GetPosition());
89 const SwField * pField = pTextField ? pTextField->GetFormatField().GetField() : nullptr;
91 if (pField)
93 m_pDoc->getIDocumentFieldsAccess().UpdateField(pTextField, *m_pNewField, m_bUpdate);
94 SwFormatField* pDstFormatField = const_cast<SwFormatField*>(&pTextField->GetFormatField());
96 if (m_pDoc->getIDocumentFieldsAccess().GetFieldType(SwFieldIds::Postit, OUString(), false) == pDstFormatField->GetField()->GetTyp())
97 m_pDoc->GetDocShell()->Broadcast( SwFormatFieldHint( pDstFormatField, SwFormatFieldHintWhich::INSERTED ) );
101 void SwUndoFieldFromDoc::RedoImpl(::sw::UndoRedoContext &)
103 DoImpl();
106 void SwUndoFieldFromDoc::RepeatImpl(::sw::RepeatContext &)
108 ::sw::UndoGuard const undoGuard(m_pDoc->GetIDocumentUndoRedo());
109 DoImpl();
112 SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
113 Any aOldVal, Any aNewVal,
114 sal_uInt16 _nWhich)
115 : SwUndoField(rPos), m_aOldVal(std::move(aOldVal)), m_aNewVal(std::move(aNewVal)), m_nWhich(_nWhich)
119 SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
123 void SwUndoFieldFromAPI::UndoImpl(::sw::UndoRedoContext &)
125 SwField * pField = sw::DocumentFieldsManager::GetFieldAtPos(GetPosition());
127 if (pField)
128 pField->PutValue(m_aOldVal, m_nWhich);
131 void SwUndoFieldFromAPI::DoImpl()
133 SwField * pField = sw::DocumentFieldsManager::GetFieldAtPos(GetPosition());
135 if (pField)
136 pField->PutValue(m_aNewVal, m_nWhich);
139 void SwUndoFieldFromAPI::RedoImpl(::sw::UndoRedoContext &)
141 DoImpl();
144 void SwUndoFieldFromAPI::RepeatImpl(::sw::RepeatContext &)
146 DoImpl();
149 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */