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 <DocumentFieldsManager.hxx>
27 : #include <txtfld.hxx>
28 : #include <fldbas.hxx>
29 : #include <ndtxt.hxx>
30 : #include <fmtfld.hxx>
31 : #include <dbfld.hxx>
32 : #include <docsh.hxx>
33 :
34 : using namespace ::com::sun::star::uno;
35 :
36 40 : SwUndoField::SwUndoField(const SwPosition & rPos, SwUndoId _nId )
37 40 : : SwUndo(_nId)
38 : {
39 40 : nNodeIndex = rPos.nNode.GetIndex();
40 40 : nOffset = rPos.nContent.GetIndex();
41 40 : pDoc = rPos.GetDoc();
42 40 : }
43 :
44 40 : SwUndoField::~SwUndoField()
45 : {
46 40 : }
47 :
48 0 : SwPosition SwUndoField::GetPosition()
49 : {
50 0 : SwNode * pNode = pDoc->GetNodes()[nNodeIndex];
51 0 : SwNodeIndex aNodeIndex(*pNode);
52 0 : SwIndex aIndex(pNode->GetContentNode(), nOffset);
53 0 : SwPosition aResult(aNodeIndex, aIndex);
54 :
55 0 : return aResult;
56 : }
57 :
58 0 : SwUndoFieldFromDoc::SwUndoFieldFromDoc(const SwPosition & rPos,
59 : const SwField & rOldField,
60 : const SwField & rNewField,
61 : SwMsgPoolItem * _pHint, bool _bUpdate, SwUndoId _nId)
62 : : SwUndoField(rPos,_nId)
63 0 : , pOldField(rOldField.CopyField())
64 0 : , pNewField(rNewField.CopyField())
65 : , pHint(_pHint)
66 0 : , bUpdate(_bUpdate)
67 : {
68 : OSL_ENSURE(pOldField, "No old field!");
69 : OSL_ENSURE(pNewField, "No new field!");
70 : OSL_ENSURE(pDoc, "No document!");
71 0 : }
72 :
73 0 : SwUndoFieldFromDoc::~SwUndoFieldFromDoc()
74 : {
75 0 : delete pOldField;
76 0 : delete pNewField;
77 0 : }
78 :
79 0 : void SwUndoFieldFromDoc::UndoImpl(::sw::UndoRedoContext &)
80 : {
81 0 : SwTextField * pTextField = sw::DocumentFieldsManager::GetTextFieldAtPos(GetPosition());
82 0 : const SwField * pField = pTextField ? pTextField->GetFormatField().GetField() : NULL;
83 :
84 0 : if (pField)
85 : {
86 0 : pDoc->getIDocumentFieldsAccess().UpdateField(pTextField, *pOldField, pHint, bUpdate);
87 : }
88 0 : }
89 :
90 0 : void SwUndoFieldFromDoc::DoImpl()
91 : {
92 0 : SwTextField * pTextField = sw::DocumentFieldsManager::GetTextFieldAtPos(GetPosition());
93 0 : const SwField * pField = pTextField ? pTextField->GetFormatField().GetField() : NULL;
94 :
95 0 : if (pField)
96 : {
97 0 : pDoc->getIDocumentFieldsAccess().UpdateField(pTextField, *pNewField, pHint, bUpdate);
98 0 : SwFormatField* pDstFormatField = const_cast<SwFormatField*>(&pTextField->GetFormatField());
99 :
100 0 : if ( pDoc->getIDocumentFieldsAccess().GetFieldType(RES_POSTITFLD, aEmptyOUStr, false) == pDstFormatField->GetField()->GetTyp() )
101 0 : pDoc->GetDocShell()->Broadcast( SwFormatFieldHint( pDstFormatField, SwFormatFieldHintWhich::INSERTED ) );
102 : }
103 0 : }
104 :
105 0 : void SwUndoFieldFromDoc::RedoImpl(::sw::UndoRedoContext &)
106 : {
107 0 : DoImpl();
108 0 : }
109 :
110 0 : void SwUndoFieldFromDoc::RepeatImpl(::sw::RepeatContext &)
111 : {
112 0 : ::sw::UndoGuard const undoGuard(pDoc->GetIDocumentUndoRedo());
113 0 : DoImpl();
114 0 : }
115 :
116 40 : SwUndoFieldFromAPI::SwUndoFieldFromAPI(const SwPosition & rPos,
117 : const Any & rOldVal, const Any & rNewVal,
118 : sal_uInt16 _nWhich)
119 40 : : SwUndoField(rPos), aOldVal(rOldVal), aNewVal(rNewVal), nWhich(_nWhich)
120 : {
121 40 : }
122 :
123 80 : SwUndoFieldFromAPI::~SwUndoFieldFromAPI()
124 : {
125 80 : }
126 :
127 0 : void SwUndoFieldFromAPI::UndoImpl(::sw::UndoRedoContext &)
128 : {
129 0 : SwField * pField = sw::DocumentFieldsManager::GetFieldAtPos(GetPosition());
130 :
131 0 : if (pField)
132 0 : pField->PutValue(aOldVal, nWhich);
133 0 : }
134 :
135 0 : void SwUndoFieldFromAPI::DoImpl()
136 : {
137 0 : SwField * pField = sw::DocumentFieldsManager::GetFieldAtPos(GetPosition());
138 :
139 0 : if (pField)
140 0 : pField->PutValue(aNewVal, nWhich);
141 0 : }
142 :
143 0 : void SwUndoFieldFromAPI::RedoImpl(::sw::UndoRedoContext &)
144 : {
145 0 : DoImpl();
146 0 : }
147 :
148 0 : void SwUndoFieldFromAPI::RepeatImpl(::sw::RepeatContext &)
149 : {
150 0 : DoImpl();
151 177 : }
152 :
153 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|