LibreOffice Module sc (master) 1
undostyl.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 <svl/itemset.hxx>
21#include <utility>
22#include <vcl/virdev.hxx>
23#include <osl/diagnose.h>
24
25#include <undostyl.hxx>
26#include <docsh.hxx>
27#include <docpool.hxx>
28#include <stlpool.hxx>
29#include <printfun.hxx>
30#include <scmod.hxx>
31#include <inputhdl.hxx>
32#include <globstr.hrc>
33#include <scresid.hxx>
34
35// modify style (cell or page style)
36
38{
39}
40
42 aName( rOther.aName ),
43 aParent( rOther.aParent )
44{
45 if (rOther.moItems)
46 moItems.emplace(*rOther.moItems);
47}
48
50{
51 if (this != &rOther)
52 {
53 aName = rOther.aName;
54 aParent = rOther.aParent;
55 if (rOther.moItems)
56 moItems.emplace(*rOther.moItems);
57 else
58 moItems.reset();
59 }
60 return *this;
61}
62
64{
65 if ( pSource )
66 {
67 aName = pSource->GetName();
68 aParent = pSource->GetParent();
69 moItems.emplace(const_cast<SfxStyleSheetBase*>(pSource)->GetItemSet());
70 }
71 else
72 {
73 aName.clear();
74 aParent.clear();
75 moItems.reset();
76 }
77}
78
80 const ScStyleSaveData& rOld, const ScStyleSaveData& rNew ) :
81 ScSimpleUndo( pDocSh ),
82 eFamily( eFam ),
83 aOldData( rOld ),
84 aNewData( rNew )
85{
86}
87
89{
90}
91
93{
94 TranslateId pId = (eFamily == SfxStyleFamily::Para) ?
95 STR_UNDO_EDITCELLSTYLE :
96 STR_UNDO_EDITPAGESTYLE;
97 return ScResId(pId);
98}
99
100static void lcl_DocStyleChanged( ScDocument* pDoc, const SfxStyleSheetBase* pStyle, bool bRemoved )
101{
103
105 Point aLogic = pVDev->LogicToPixel(Point(1000,1000), MapMode(MapUnit::MapTwip));
106 double nPPTX = aLogic.X() / 1000.0;
107 double nPPTY = aLogic.Y() / 1000.0;
108 Fraction aZoom(1,1);
109 pDoc->StyleSheetChanged( pStyle, bRemoved, pVDev, nPPTX, nPPTY, aZoom, aZoom );
110
111 ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
112 if (pHdl)
113 pHdl->ForgetLastPattern();
114}
115
116void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const OUString& rName,
117 SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
118{
119 ScDocument& rDoc = pDocSh->GetDocument();
120 ScStyleSheetPool* pStlPool = rDoc.GetStyleSheetPool();
121 const OUString& aNewName = rData.GetName();
122 bool bDelete = aNewName.isEmpty(); // no new name -> delete style
123 bool bNew = ( rName.isEmpty() && !bDelete ); // creating new style
124
125 SfxStyleSheetBase* pStyle = nullptr;
126 if ( !rName.isEmpty() )
127 {
128 // find old style to modify
129 pStyle = pStlPool->Find( rName, eStyleFamily );
130 OSL_ENSURE( pStyle, "style not found" );
131
132 if ( pStyle && !bDelete )
133 {
134 // set new name
135 pStyle->SetName( aNewName );
136 }
137 }
138 else if ( !bDelete )
139 {
140 // create style (with new name)
141 pStyle = &pStlPool->Make( aNewName, eStyleFamily, SfxStyleSearchBits::UserDefined );
142
143 if ( eStyleFamily == SfxStyleFamily::Para )
144 rDoc.GetPool()->CellStyleCreated( aNewName, rDoc );
145 }
146
147 if ( pStyle )
148 {
149 if ( bDelete )
150 {
151 if ( eStyleFamily == SfxStyleFamily::Para )
152 lcl_DocStyleChanged( &rDoc, pStyle, true ); // TRUE: remove usage of style
153 else
154 rDoc.RemovePageStyleInUse( rName );
155
156 // delete style
157 pStlPool->Remove( pStyle );
158 }
159 else
160 {
161 // modify style
162
163 const OUString& aNewParent = rData.GetParent();
164 if ( aNewParent != pStyle->GetParent() )
165 pStyle->SetParent( aNewParent );
166
167 SfxItemSet& rStyleSet = pStyle->GetItemSet();
168 const std::optional<SfxItemSet>& pNewSet = rData.GetItems();
169 OSL_ENSURE( pNewSet, "no ItemSet for style" );
170 if (pNewSet)
171 rStyleSet.Set( *pNewSet, false );
172
173 if ( eStyleFamily == SfxStyleFamily::Para )
174 {
175 lcl_DocStyleChanged( &rDoc, pStyle, false ); // cell styles: row heights
176 }
177 else
178 {
179 // page styles
180
181 if ( bNew && aNewName != rName )
182 rDoc.RenamePageStyleInUse( rName, aNewName );
183
184 if (pNewSet)
185 rDoc.ModifyStyleSheet( *pStyle, *pNewSet );
186
187 pDocSh->PageStyleModified( aNewName, true );
188 }
189 }
190 }
191
193
196}
197
199{
200 BeginUndo();
202 EndUndo();
203}
204
206{
207 BeginRedo();
209 EndRedo();
210}
211
212void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
213{
214}
215
216bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
217{
218 return false; // no repeat possible
219}
220
221// apply page style
222
224 mnTab( nTab ),
225 maOldStyle(std::move( aOldStyle ))
226{
227}
228
230 ScSimpleUndo( pDocSh ),
231 maNewStyle(std::move( aNewStyle ))
232{
233}
234
236{
237}
238
239void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const OUString& rOldStyle )
240{
241 maEntries.emplace_back( nTab, rOldStyle );
242}
243
245{
246 return ScResId( STR_UNDO_APPLYPAGESTYLE );
247}
248
250{
251 BeginUndo();
252 for( const auto& rEntry : maEntries )
253 {
254 pDocShell->GetDocument().SetPageStyle( rEntry.mnTab, rEntry.maOldStyle );
256 }
257 EndUndo();
258}
259
261{
262 BeginRedo();
263 for( const auto& rEntry : maEntries )
264 {
265 pDocShell->GetDocument().SetPageStyle( rEntry.mnTab, maNewStyle );
267 }
268 EndRedo();
269}
270
271void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
272{
274}
275
276bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
277{
278 return false;
279}
280
281/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SCTAB MAXTAB
Definition: address.hxx:70
const ScDocument & GetDocument() const
Definition: docsh.hxx:220
void PostPaint(SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab, SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart, sal_uInt16 nExtFlags=0)
Definition: docsh3.cxx:101
SfxPrinter * GetPrinter(bool bCreateIfNotExist=true)
Definition: docsh3.cxx:451
void PageStyleModified(std::u16string_view rStyleName, bool bApi)
Definition: docsh4.cxx:1768
void CellStyleCreated(std::u16string_view rName, const ScDocument &rDoc)
Definition: docpool.cxx:368
void ModifyStyleSheet(SfxStyleSheetBase &rPageStyle, const SfxItemSet &rChanges)
Definition: documen8.cxx:210
bool RemovePageStyleInUse(std::u16string_view rStrPageStyle)
Definition: documen8.cxx:325
SC_DLLPUBLIC SCCOL MaxCol() const
Definition: document.hxx:891
SC_DLLPUBLIC ScDocumentPool * GetPool()
Definition: document.cxx:6170
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:892
SC_DLLPUBLIC ScStyleSheetPool * GetStyleSheetPool() const
Definition: document.cxx:6175
void StyleSheetChanged(const SfxStyleSheetBase *pStyleSheet, bool bRemoved, OutputDevice *pDev, double nPPTX, double nPPTY, const Fraction &rZoomX, const Fraction &rZoomY)
Definition: document.cxx:5032
SC_DLLPUBLIC void SetPageStyle(SCTAB nTab, const OUString &rName)
Definition: document.cxx:6296
bool RenamePageStyleInUse(std::u16string_view rOld, const OUString &rNew)
Definition: documen8.cxx:340
void ForgetLastPattern()
Definition: inputhdl.cxx:2327
bool UpdatePages()
Definition: printfun.cxx:2426
void BeginRedo()
Definition: undobase.cxx:145
void EndRedo()
Definition: undobase.cxx:154
void EndUndo()
Definition: undobase.cxx:125
ScDocShell * pDocShell
Definition: undobase.hxx:50
void BeginUndo()
Definition: undobase.cxx:90
ScStyleSaveData & operator=(const ScStyleSaveData &rOther)
Definition: undostyl.cxx:49
const std::optional< SfxItemSet > & GetItems() const
Definition: undostyl.hxx:45
OUString aParent
Definition: undostyl.hxx:33
const OUString & GetParent() const
Definition: undostyl.hxx:44
void InitFromStyle(const SfxStyleSheetBase *pSource)
Definition: undostyl.cxx:63
OUString aName
Definition: undostyl.hxx:32
const OUString & GetName() const
Definition: undostyl.hxx:43
std::optional< SfxItemSet > moItems
Definition: undostyl.hxx:34
virtual SfxStyleSheetBase & Make(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits nMask=SfxStyleSearchBits::All) override
Definition: stlpool.cxx:72
virtual void Remove(SfxStyleSheetBase *pStyle) override
Definition: stlpool.cxx:116
void AddSheetAction(SCTAB nTab, const OUString &rOld)
Definition: undostyl.cxx:239
virtual void Undo() override
Definition: undostyl.cxx:249
ScUndoApplyPageStyle(ScDocShell *pDocSh, OUString aNewStyle)
Definition: undostyl.cxx:229
ApplyStyleVec maEntries
Definition: undostyl.hxx:98
virtual OUString GetComment() const override
Definition: undostyl.cxx:244
virtual void Redo() override
Definition: undostyl.cxx:260
virtual ~ScUndoApplyPageStyle() override
Definition: undostyl.cxx:235
virtual bool CanRepeat(SfxRepeatTarget &rTarget) const override
Definition: undostyl.cxx:276
virtual void Repeat(SfxRepeatTarget &rTarget) override
Definition: undostyl.cxx:271
virtual OUString GetComment() const override
Definition: undostyl.cxx:92
ScStyleSaveData aNewData
Definition: undostyl.hxx:53
SfxStyleFamily eFamily
Definition: undostyl.hxx:51
virtual bool CanRepeat(SfxRepeatTarget &rTarget) const override
Definition: undostyl.cxx:216
virtual void Repeat(SfxRepeatTarget &rTarget) override
Definition: undostyl.cxx:212
ScUndoModifyStyle(ScDocShell *pDocSh, SfxStyleFamily eFam, const ScStyleSaveData &rOld, const ScStyleSaveData &rNew)
Definition: undostyl.cxx:79
static void DoChange(ScDocShell *pDocSh, const OUString &rName, SfxStyleFamily eStyleFamily, const ScStyleSaveData &rData)
Definition: undostyl.cxx:116
virtual ~ScUndoModifyStyle() override
Definition: undostyl.cxx:88
ScStyleSaveData aOldData
Definition: undostyl.hxx:52
virtual void Redo() override
Definition: undostyl.cxx:205
virtual void Undo() override
Definition: undostyl.cxx:198
bool Set(const SfxItemSet &, bool bDeep=true)
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All) const
virtual bool SetName(const OUString &rNewName, bool bReindexNow=true)
virtual const OUString & GetParent() const
const OUString & GetName() const
virtual bool SetParent(const OUString &)
virtual SfxItemSet & GetItemSet()
constexpr double nPPTX
constexpr double nPPTY
OUString aName
const SfxItemSet * GetItemSet(const SfxPoolItem &rAttr)
OUString ScResId(TranslateId aId)
Definition: scdll.cxx:90
#define SC_MOD()
Definition: scmod.hxx:249
ApplyStyleEntry(SCTAB nTab, OUString aOldStyle)
Definition: undostyl.cxx:223
SfxStyleFamily
sal_Int16 SCTAB
Definition: types.hxx:22
static void lcl_DocStyleChanged(ScDocument *pDoc, const SfxStyleSheetBase *pStyle, bool bRemoved)
Definition: undostyl.cxx:100