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 if (eFamily == SfxStyleFamily::Frame)
95 return ScResId(STR_UNDO_EDITGRAPHICSTYLE);
96 if (eFamily == SfxStyleFamily::Page)
97 return ScResId(STR_UNDO_EDITPAGESTYLE);
98
99 return ScResId(STR_UNDO_EDITCELLSTYLE);
100}
101
102static void lcl_DocStyleChanged( ScDocument* pDoc, const SfxStyleSheetBase* pStyle, bool bRemoved )
103{
105
107 Point aLogic = pVDev->LogicToPixel(Point(1000,1000), MapMode(MapUnit::MapTwip));
108 double nPPTX = aLogic.X() / 1000.0;
109 double nPPTY = aLogic.Y() / 1000.0;
110 Fraction aZoom(1,1);
111 pDoc->StyleSheetChanged( pStyle, bRemoved, pVDev, nPPTX, nPPTY, aZoom, aZoom );
112
113 ScInputHandler* pHdl = SC_MOD()->GetInputHdl();
114 if (pHdl)
115 pHdl->ForgetLastPattern();
116}
117
118void ScUndoModifyStyle::DoChange( ScDocShell* pDocSh, const OUString& rName,
119 SfxStyleFamily eStyleFamily, const ScStyleSaveData& rData )
120{
121 ScDocument& rDoc = pDocSh->GetDocument();
122 ScStyleSheetPool* pStlPool = rDoc.GetStyleSheetPool();
123 const OUString& aNewName = rData.GetName();
124 bool bDelete = aNewName.isEmpty(); // no new name -> delete style
125 bool bNew = ( rName.isEmpty() && !bDelete ); // creating new style
126
127 SfxStyleSheetBase* pStyle = nullptr;
128 if ( !rName.isEmpty() )
129 {
130 // find old style to modify
131 pStyle = pStlPool->Find( rName, eStyleFamily );
132 OSL_ENSURE( pStyle, "style not found" );
133
134 if ( pStyle && !bDelete )
135 {
136 // set new name
137 pStyle->SetName( aNewName );
138 }
139 }
140 else if ( !bDelete )
141 {
142 // create style (with new name)
143 pStyle = &pStlPool->Make( aNewName, eStyleFamily, SfxStyleSearchBits::UserDefined );
144
145 if ( eStyleFamily == SfxStyleFamily::Para )
146 rDoc.GetPool()->CellStyleCreated( aNewName, rDoc );
147 }
148
149 if ( pStyle )
150 {
151 if ( bDelete )
152 {
153 if ( eStyleFamily == SfxStyleFamily::Para )
154 lcl_DocStyleChanged( &rDoc, pStyle, true ); // TRUE: remove usage of style
155 else if ( eStyleFamily == SfxStyleFamily::Page )
156 rDoc.RemovePageStyleInUse( rName );
157
158 // delete style
159 pStlPool->Remove( pStyle );
160 }
161 else
162 {
163 // modify style
164
165 const OUString& aNewParent = rData.GetParent();
166 if ( aNewParent != pStyle->GetParent() )
167 pStyle->SetParent( aNewParent );
168
169 SfxItemSet& rStyleSet = pStyle->GetItemSet();
170 const std::optional<SfxItemSet>& pNewSet = rData.GetItems();
171 OSL_ENSURE( pNewSet, "no ItemSet for style" );
172 if (pNewSet)
173 rStyleSet.Set( *pNewSet, false );
174
175 if ( eStyleFamily == SfxStyleFamily::Para )
176 {
177 lcl_DocStyleChanged( &rDoc, pStyle, false ); // cell styles: row heights
178 }
179 else if ( eStyleFamily == SfxStyleFamily::Page )
180 {
181 // page styles
182
183 if ( bNew && aNewName != rName )
184 rDoc.RenamePageStyleInUse( rName, aNewName );
185
186 if (pNewSet)
187 rDoc.ModifyStyleSheet( *pStyle, *pNewSet );
188
189 pDocSh->PageStyleModified( aNewName, true );
190 }
191 else
192 static_cast<SfxStyleSheet*>(pStyle)->Broadcast(SfxHint(SfxHintId::DataChanged));
193 }
194 }
195
197
200}
201
203{
204 BeginUndo();
206 EndUndo();
207}
208
210{
211 BeginRedo();
213 EndRedo();
214}
215
216void ScUndoModifyStyle::Repeat(SfxRepeatTarget& /* rTarget */)
217{
218}
219
220bool ScUndoModifyStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
221{
222 return false; // no repeat possible
223}
224
225// apply page style
226
228 mnTab( nTab ),
229 maOldStyle(std::move( aOldStyle ))
230{
231}
232
234 ScSimpleUndo( pDocSh ),
235 maNewStyle(std::move( aNewStyle ))
236{
237}
238
240{
241}
242
243void ScUndoApplyPageStyle::AddSheetAction( SCTAB nTab, const OUString& rOldStyle )
244{
245 maEntries.emplace_back( nTab, rOldStyle );
246}
247
249{
250 return ScResId( STR_UNDO_APPLYPAGESTYLE );
251}
252
254{
255 BeginUndo();
256 for( const auto& rEntry : maEntries )
257 {
258 pDocShell->GetDocument().SetPageStyle( rEntry.mnTab, rEntry.maOldStyle );
260 }
261 EndUndo();
262}
263
265{
266 BeginRedo();
267 for( const auto& rEntry : maEntries )
268 {
269 pDocShell->GetDocument().SetPageStyle( rEntry.mnTab, maNewStyle );
271 }
272 EndRedo();
273}
274
275void ScUndoApplyPageStyle::Repeat(SfxRepeatTarget& /* rTarget */)
276{
278}
279
280bool ScUndoApplyPageStyle::CanRepeat(SfxRepeatTarget& /* rTarget */) const
281{
282 return false;
283}
284
285/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const SCTAB MAXTAB
Definition: address.hxx:70
constexpr tools::Long Y() const
constexpr tools::Long X() const
const ScDocument & GetDocument() const
Definition: docsh.hxx:219
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:1778
void CellStyleCreated(std::u16string_view rName, const ScDocument &rDoc)
Definition: docpool.cxx:369
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:892
SC_DLLPUBLIC ScDocumentPool * GetPool()
Definition: document.cxx:6050
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:893
SC_DLLPUBLIC ScStyleSheetPool * GetStyleSheetPool() const
Definition: document.cxx:6055
void StyleSheetChanged(const SfxStyleSheetBase *pStyleSheet, bool bRemoved, OutputDevice *pDev, double nPPTX, double nPPTY, const Fraction &rZoomX, const Fraction &rZoomY)
Definition: document.cxx:4924
SC_DLLPUBLIC void SetPageStyle(SCTAB nTab, const OUString &rName)
Definition: document.cxx:6170
bool RenamePageStyleInUse(std::u16string_view rOld, const OUString &rNew)
Definition: documen8.cxx:340
void ForgetLastPattern()
Definition: inputhdl.cxx:2361
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:243
virtual void Undo() override
Definition: undostyl.cxx:253
ScUndoApplyPageStyle(ScDocShell *pDocSh, OUString aNewStyle)
Definition: undostyl.cxx:233
ApplyStyleVec maEntries
Definition: undostyl.hxx:98
virtual OUString GetComment() const override
Definition: undostyl.cxx:248
virtual void Redo() override
Definition: undostyl.cxx:264
virtual ~ScUndoApplyPageStyle() override
Definition: undostyl.cxx:239
virtual bool CanRepeat(SfxRepeatTarget &rTarget) const override
Definition: undostyl.cxx:280
virtual void Repeat(SfxRepeatTarget &rTarget) override
Definition: undostyl.cxx:275
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:220
virtual void Repeat(SfxRepeatTarget &rTarget) override
Definition: undostyl.cxx:216
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:118
virtual ~ScUndoModifyStyle() override
Definition: undostyl.cxx:88
ScStyleSaveData aOldData
Definition: undostyl.hxx:52
virtual void Redo() override
Definition: undostyl.cxx:209
virtual void Undo() override
Definition: undostyl.cxx:202
bool Set(const SfxItemSet &, bool bDeep=true)
virtual SfxStyleSheetBase * Find(const OUString &, SfxStyleFamily eFam, SfxStyleSearchBits n=SfxStyleSearchBits::All)
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:247
ApplyStyleEntry(SCTAB nTab, OUString aOldStyle)
Definition: undostyl.cxx:227
SfxStyleFamily
sal_Int16 SCTAB
Definition: types.hxx:22
static void lcl_DocStyleChanged(ScDocument *pDoc, const SfxStyleSheetBase *pStyle, bool bRemoved)
Definition: undostyl.cxx:102