LibreOffice Module sw (master) 1
SwUndoPageDesc.cxx
Go to the documentation of this file.
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 <doc.hxx>
21#include <editsh.hxx>
22#include <IDocumentUndoRedo.hxx>
23#include <swundo.hxx>
24#include <pagedesc.hxx>
25#include <SwUndoPageDesc.hxx>
26#include <SwRewriter.hxx>
27#include <undobj.hxx>
28#include <strings.hrc>
29#include <fmtcntnt.hxx>
30#include <fmthdft.hxx>
31#include <osl/diagnose.h>
32
34 const SwPageDesc & _aNew,
35 SwDoc * _pDoc)
36 : SwUndo( _aOld.GetName() != _aNew.GetName() ?
39 _pDoc ),
40 m_aOld(_aOld, _pDoc), m_aNew(_aNew, _pDoc), m_pDoc(_pDoc), m_bExchange( false )
41{
42 OSL_ENSURE(nullptr != m_pDoc, "no document?");
43
44 /*
45 The page description changes.
46 If there are no header/footer content changes like header on/off or change from shared content
47 to unshared etc., there is no reason to duplicate the content nodes (Crash i55547)
48 But this happens, this Undo Ctor will destroy the unnecessary duplicate and manipulate the
49 content pointer of the both page descriptions.
50 */
51 SwPageDesc &rOldDesc = m_aOld.m_PageDesc;
52 SwPageDesc &rNewDesc = m_aNew.m_PageDesc;
53 const SwFormatHeader& rOldHead = rOldDesc.GetMaster().GetHeader();
54 const SwFormatHeader& rNewHead = rNewDesc.GetMaster().GetHeader();
55 const SwFormatFooter& rOldFoot = rOldDesc.GetMaster().GetFooter();
56 const SwFormatFooter& rNewFoot = rNewDesc.GetMaster().GetFooter();
57 /* bExchange must not be set, if the old page descriptor will stay active.
58 Two known situations:
59 #i67735#: renaming a page descriptor
60 #i67334#: changing the follow style
61 If header/footer will be activated or deactivated, this undo will not work.
62 */
64 ( _aOld.GetFollow() == _aNew.GetFollow() ) &&
65 ( rOldHead.IsActive() == rNewHead.IsActive() ) &&
66 ( rOldFoot.IsActive() == rNewFoot.IsActive() );
67 if( rOldHead.IsActive() && ( rOldDesc.IsHeaderShared() != rNewDesc.IsHeaderShared() ) )
68 m_bExchange = false;
69 if( rOldFoot.IsActive() && ( rOldDesc.IsFooterShared() != rNewDesc.IsFooterShared() ) )
70 m_bExchange = false;
71 if( ( rOldHead.IsActive() || rOldFoot.IsActive() ) && ( rOldDesc.IsFirstShared() != rNewDesc.IsFirstShared() ) )
72 m_bExchange = false;
73 if( !m_bExchange )
74 return;
75
76 if( rNewHead.IsActive() )
77 {
78 SwFrameFormat* pFormat = new SwFrameFormat( *rNewHead.GetHeaderFormat() );
79 // The Ctor of this object will remove the duplicate!
80 SwFormatHeader aFormatHeader(pFormat);
81 (void)aFormatHeader;
82 if (!rNewDesc.IsHeaderShared())
83 {
84 pFormat = new SwFrameFormat( *rNewDesc.GetLeft().GetHeader().GetHeaderFormat() );
85 // The Ctor of this object will remove the duplicate!
86 SwFormatHeader aLeftHeader(pFormat);
87 (void)aLeftHeader;
88 }
89 if (!rNewDesc.IsFirstShared())
90 {
91 pFormat = new SwFrameFormat( *rNewDesc.GetFirstMaster().GetHeader().GetHeaderFormat() );
92 // The Ctor of this object will remove the duplicate!
93 SwFormatHeader aFirstHeader(pFormat);
94 (void)aFirstHeader;
95 }
96 }
97 // Same procedure for footers...
98 if( rNewFoot.IsActive() )
99 {
100 SwFrameFormat* pFormat = new SwFrameFormat( *rNewFoot.GetFooterFormat() );
101 // The Ctor of this object will remove the duplicate!
102 SwFormatFooter aFormatFooter(pFormat);
103 (void)aFormatFooter;
104 if (!rNewDesc.IsFooterShared())
105 {
106 pFormat = new SwFrameFormat( *rNewDesc.GetLeft().GetFooter().GetFooterFormat() );
107 // The Ctor of this object will remove the duplicate!
108 SwFormatFooter aLeftFooter(pFormat);
109 (void)aLeftFooter;
110 }
111 if (!rNewDesc.IsFirstShared())
112 {
113 pFormat = new SwFrameFormat( *rNewDesc.GetFirstMaster().GetFooter().GetFooterFormat() );
114 // The Ctor of this object will remove the duplicate!
115 SwFormatFooter aFirstFooter(pFormat);
116 (void)aFirstFooter;
117 }
118 }
119
120 // After this exchange method the old page description will point to zero,
121 // the new one will point to the node position of the original content nodes.
123}
124
126{
127}
128
130{
131 OSL_ENSURE( m_bExchange, "You shouldn't do that." );
132 const SwFormatHeader& rDestHead = rDest.GetMaster().GetHeader();
133 const SwFormatHeader& rSourceHead = rSource.GetMaster().GetHeader();
134 if( rDestHead.IsActive() )
135 {
136 // Let the destination page description point to the source node position,
137 // from now on this descriptor is responsible for the content nodes!
138 const SwFormatHeader* pItem = rDest.GetMaster().GetAttrSet().GetItemIfSet( RES_HEADER, false );
139 std::unique_ptr<SwFormatHeader> pNewItem(pItem->Clone());
140 SwFrameFormat* pNewFormat = pNewItem->GetHeaderFormat();
141 pNewFormat->SetFormatAttr( rSourceHead.GetHeaderFormat()->GetContent() );
142
143 // Let the source page description point to zero node position,
144 // it loses the responsible and can be destroyed without removing the content nodes.
145 pItem = rSource.GetMaster().GetAttrSet().GetItemIfSet( RES_HEADER, false );
146 pNewItem.reset(pItem->Clone());
147 pNewFormat = pNewItem->GetHeaderFormat();
148 pNewFormat->SetFormatAttr( SwFormatContent() );
149
150 if( !rDest.IsHeaderShared() )
151 {
152 // Same procedure for unshared header...
153 const SwFormatHeader& rSourceLeftHead = rSource.GetLeft().GetHeader();
154 pItem = rDest.GetLeft().GetAttrSet().GetItemIfSet( RES_HEADER, false );
155 pNewItem.reset(pItem->Clone());
156 pNewFormat = pNewItem->GetHeaderFormat();
157 pNewFormat->SetFormatAttr( rSourceLeftHead.GetHeaderFormat()->GetContent() );
158 pItem = rSource.GetLeft().GetAttrSet().GetItemIfSet( RES_HEADER, false );
159 pNewItem.reset(pItem->Clone());
160 pNewFormat = pNewItem->GetHeaderFormat();
161 pNewFormat->SetFormatAttr( SwFormatContent() );
162 }
163 if (!rDest.IsFirstShared())
164 {
165 // Same procedure for unshared header...
166 const SwFormatHeader& rSourceFirstMasterHead = rSource.GetFirstMaster().GetHeader();
167 pItem = rDest.GetFirstMaster().GetAttrSet().GetItemIfSet( RES_HEADER, false );
168 pNewItem.reset(pItem->Clone());
169 pNewFormat = pNewItem->GetHeaderFormat();
170 pNewFormat->SetFormatAttr( rSourceFirstMasterHead.GetHeaderFormat()->GetContent() );
171 pItem = rSource.GetFirstMaster().GetAttrSet().GetItemIfSet( RES_HEADER, false );
172 pNewItem.reset(pItem->Clone());
173 pNewFormat = pNewItem->GetHeaderFormat();
174 pNewFormat->SetFormatAttr( SwFormatContent() );
175 }
176 }
177 // Same procedure for footers...
178 const SwFormatFooter& rDestFoot = rDest.GetMaster().GetFooter();
179 const SwFormatFooter& rSourceFoot = rSource.GetMaster().GetFooter();
180 if( !rDestFoot.IsActive() )
181 return;
182
183 const SwFormatFooter* pItem;
184 pItem = rDest.GetMaster().GetAttrSet().GetItemIfSet( RES_FOOTER, false );
185 std::unique_ptr<SwFormatFooter> pNewItem(pItem->Clone());
186 SwFrameFormat *pNewFormat = pNewItem->GetFooterFormat();
187 pNewFormat->SetFormatAttr( rSourceFoot.GetFooterFormat()->GetContent() );
188
189 pItem = rSource.GetMaster().GetAttrSet().GetItemIfSet( RES_FOOTER, false );
190 pNewItem.reset(pItem->Clone());
191 pNewFormat = pNewItem->GetFooterFormat();
192 pNewFormat->SetFormatAttr( SwFormatContent() );
193
194 if( !rDest.IsFooterShared() )
195 {
196 const SwFormatFooter& rSourceLeftFoot = rSource.GetLeft().GetFooter();
197 pItem = rDest.GetLeft().GetAttrSet().GetItemIfSet( RES_FOOTER, false );
198 pNewItem.reset(pItem->Clone());
199 pNewFormat = pNewItem->GetFooterFormat();
200 pNewFormat->SetFormatAttr( rSourceLeftFoot.GetFooterFormat()->GetContent() );
201 pItem = rSource.GetLeft().GetAttrSet().GetItemIfSet( RES_FOOTER, false );
202 pNewItem.reset(pItem->Clone());
203 pNewFormat = pNewItem->GetFooterFormat();
204 pNewFormat->SetFormatAttr( SwFormatContent() );
205 }
206 if (rDest.IsFirstShared())
207 return;
208
209 const SwFormatFooter& rSourceFirstMasterFoot = rSource.GetFirstMaster().GetFooter();
210 pItem = rDest.GetFirstMaster().GetAttrSet().GetItemIfSet( RES_FOOTER, false );
211 pNewItem.reset(pItem->Clone());
212 pNewFormat = pNewItem->GetFooterFormat();
213 pNewFormat->SetFormatAttr( rSourceFirstMasterFoot.GetFooterFormat()->GetContent() );
214 pItem = rSource.GetFirstMaster().GetAttrSet().GetItemIfSet( RES_FOOTER, false );
215 pNewItem.reset(pItem->Clone());
216 pNewFormat = pNewItem->GetFooterFormat();
217 pNewFormat->SetFormatAttr( SwFormatContent() );
218}
219
221{
223 if (!pESh)
224 return;
225 if (pESh->IsHeaderFooterEdit())
227}
228
230{
231 // Move (header/footer)content node responsibility from new page descriptor to old one again.
232 if( m_bExchange )
236}
237
239{
240 // Move (header/footer)content node responsibility from old page descriptor to new one again.
241 if( m_bExchange )
245}
246
248{
249 SwRewriter aResult;
250
251 aResult.AddRule(UndoArg1, m_aOld.GetName());
252 aResult.AddRule(UndoArg2, SwResId(STR_YIELDS));
253 aResult.AddRule(UndoArg3, m_aNew.GetName());
254
255 return aResult;
256}
257
259 SwDoc * _pDoc)
260 : SwUndo(SwUndoId::CREATE_PAGEDESC, _pDoc), m_pDesc(pNew), m_aNew(*pNew, _pDoc),
261 m_pDoc(_pDoc)
262{
263 OSL_ENSURE(nullptr != m_pDoc, "no document?");
264}
265
267{
268}
269
271{
272 if (m_pDesc)
273 {
274 m_aNew = *m_pDesc;
275 m_pDesc = nullptr;
276 }
277
279}
280
282{
283 SwPageDesc aPageDesc = m_aNew;
284 m_pDoc->MakePageDesc(m_aNew.GetName(), &aPageDesc, false, true);
285}
286
288{
289 DoImpl();
290}
291
293{
294 ::sw::UndoGuard const undoGuard(m_pDoc->GetIDocumentUndoRedo());
295 DoImpl();
296}
297
299{
300 SwRewriter aResult;
301
302 if (m_pDesc)
303 aResult.AddRule(UndoArg1, m_pDesc->GetName());
304 else
305 aResult.AddRule(UndoArg1, m_aNew.GetName());
306
307 return aResult;
308}
309
311 SwDoc * _pDoc)
312 : SwUndo(SwUndoId::DELETE_PAGEDESC, _pDoc), m_aOld(_aOld, _pDoc), m_pDoc(_pDoc)
313{
314 OSL_ENSURE(nullptr != m_pDoc, "no document?");
315}
316
318{
319}
320
322{
323 SwPageDesc aPageDesc = m_aOld;
324 m_pDoc->MakePageDesc(m_aOld.GetName(), &aPageDesc, false, true);
325}
326
328{
330}
331
333{
334 DoImpl();
335}
336
338{
339 ::sw::UndoGuard const undoGuard(m_pDoc->GetIDocumentUndoRedo());
340 DoImpl();
341}
342
344{
345 SwRewriter aResult;
346
347 aResult.AddRule(UndoArg1, m_aOld.GetName());
348
349 return aResult;
350}
351
352/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
@ UndoArg1
Definition: SwRewriter.hxx:29
@ UndoArg3
Definition: SwRewriter.hxx:31
@ UndoArg2
Definition: SwRewriter.hxx:30
const T * GetItemIfSet(TypedWhichId< T > nWhich, bool bSrchInParent=true) const
Definition: doc.hxx:195
IDocumentUndoRedo & GetIDocumentUndoRedo()
Definition: doc.cxx:152
SwPageDesc * MakePageDesc(const OUString &rName, const SwPageDesc *pCpy=nullptr, bool bRegardLanguage=true, bool bBroadcast=false)
Definition: docdesc.cxx:765
SwEditShell const * GetEditShell() const
Definition: doccorr.cxx:330
void ChgPageDesc(const OUString &rName, const SwPageDesc &)
Definition: docdesc.cxx:978
void DelPageDesc(const OUString &rName, bool bBroadcast=false)
Definition: docdesc.cxx:970
Content, content of frame (header, footer, fly).
Definition: fmtcntnt.hxx:32
Footer, for pageformats Client of FrameFormat describing the footer.
Definition: fmthdft.hxx:65
bool IsActive() const
Definition: fmthdft.hxx:89
virtual SwFormatFooter * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrfrm.cxx:573
const SwFrameFormat * GetFooterFormat() const
Definition: fmthdft.hxx:85
Header, for PageFormats Client of FrameFormat describing the header.
Definition: fmthdft.hxx:34
bool IsActive() const
Definition: fmthdft.hxx:58
virtual SwFormatHeader * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrfrm.cxx:523
const SwFrameFormat * GetHeaderFormat() const
Definition: fmthdft.hxx:54
const SwFormatFooter & GetFooter(bool=true) const
Definition: fmthdft.hxx:99
const SwFormatHeader & GetHeader(bool=true) const
Definition: fmthdft.hxx:97
const SwAttrSet & GetAttrSet() const
For querying the attribute array.
Definition: format.hxx:136
virtual bool SetFormatAttr(const SfxPoolItem &rAttr)
Definition: format.cxx:447
const SwFormatContent & GetContent(bool=true) const
Definition: fmtcntnt.hxx:55
Style of a layout element.
Definition: frmfmt.hxx:62
OUString const & GetName() const
Definition: pagedesc.cxx:697
SwPageDesc m_PageDesc
Definition: pagedesc.hxx:379
const OUString & GetName() const
Definition: pagedesc.hxx:196
SwFrameFormat & GetMaster()
Definition: pagedesc.hxx:238
bool IsHeaderShared() const
Definition: pagedesc.hxx:319
bool IsFirstShared() const
Definition: pagedesc.cxx:396
bool IsFooterShared() const
Definition: pagedesc.hxx:323
SwFrameFormat & GetFirstMaster()
Definition: pagedesc.hxx:240
SwFrameFormat & GetLeft()
Definition: pagedesc.hxx:239
const SwPageDesc * GetFollow() const
Definition: pagedesc.hxx:267
void AddRule(SwUndoArg eWhat, const OUString &rWith)
Definition: SwRewriter.cxx:25
const SwPageDesc * m_pDesc
virtual void UndoImpl(::sw::UndoRedoContext &) override
virtual ~SwUndoPageDescCreate() override
SwUndoPageDescCreate(const SwPageDesc *pNew, SwDoc *pDoc)
virtual SwRewriter GetRewriter() const override
Returns the rewriter for this object.
virtual void RepeatImpl(::sw::RepeatContext &) override
virtual void RedoImpl(::sw::UndoRedoContext &) override
virtual ~SwUndoPageDescDelete() override
virtual void UndoImpl(::sw::UndoRedoContext &) override
virtual void RepeatImpl(::sw::RepeatContext &) override
SwUndoPageDescDelete(const SwPageDesc &aOld, SwDoc *pDoc)
virtual SwRewriter GetRewriter() const override
Returns the rewriter for this object.
virtual void RedoImpl(::sw::UndoRedoContext &) override
void ExchangeContentNodes(SwPageDesc &rSource, SwPageDesc &rDest)
SwPageDescExt m_aNew
virtual ~SwUndoPageDesc() override
SwPageDescExt m_aOld
virtual SwRewriter GetRewriter() const override
Returns the rewriter for this object.
void ExitHeaderFooterEdit()
SwUndoPageDesc(const SwPageDesc &aOld, const SwPageDesc &aNew, SwDoc *pDoc)
virtual void RedoImpl(::sw::UndoRedoContext &) override
virtual void UndoImpl(::sw::UndoRedoContext &) override
bool IsHeaderFooterEdit() const
Acts both for headers / footers, depending on the bShow(Header|Footer)Separator flags.
Definition: viewsh.hxx:573
void ToggleHeaderFooterEdit()
Acts both for headers / footers, depending on the bShow(Header|Footer)Separator flags.
Definition: viewsh.cxx:111
virtual OUString GetName() const override
constexpr TypedWhichId< SwFormatHeader > RES_HEADER(96)
constexpr TypedWhichId< SwFormatFooter > RES_FOOTER(97)
OUString SwResId(TranslateId aId)
Definition: swmodule.cxx:168
SwUndoId
Definition: swundo.hxx:30
@ CREATE_PAGEDESC
@ DELETE_PAGEDESC
@ RENAME_PAGEDESC
@ CHANGE_PAGEDESC