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