xref: /aoo41x/main/sw/source/core/undo/SwUndoFmt.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 #include <tools/resid.hxx>
28 #include <tools/string.hxx>
29 
30 #include <poolfmt.hxx>
31 #include <charfmt.hxx>
32 #include <frmfmt.hxx>
33 #include <SwUndoFmt.hxx>
34 #include <SwRewriter.hxx>
35 #include <swundo.hxx>
36 #include <undobj.hxx>
37 #include <fmtcol.hxx>
38 #include <doc.hxx>
39 #include <IDocumentUndoRedo.hxx>
40 #include <comcore.hrc>
41 
SwUndoFmtCreate(SwUndoId nUndoId,SwFmt * _pNew,SwFmt * _pDerivedFrom,SwDoc * _pDoc)42 SwUndoFmtCreate::SwUndoFmtCreate
43 (SwUndoId nUndoId, SwFmt * _pNew, SwFmt * _pDerivedFrom, SwDoc * _pDoc)
44     : SwUndo(nUndoId), pNew(_pNew),
45       pDoc(_pDoc), pNewSet(NULL), nId(0), bAuto(sal_False)
46 {
47     if (_pDerivedFrom)
48         sDerivedFrom = _pDerivedFrom->GetName();
49 }
50 
~SwUndoFmtCreate()51 SwUndoFmtCreate::~SwUndoFmtCreate()
52 {
53 }
54 
UndoImpl(::sw::UndoRedoContext &)55 void SwUndoFmtCreate::UndoImpl(::sw::UndoRedoContext &)
56 {
57     if (pNew)
58     {
59         if (sNewName.Len() == 0 && pNew)
60             sNewName = pNew->GetName();
61 
62         if (sNewName.Len() > 0)
63             pNew = Find(sNewName);
64 
65         if (pNew)
66         {
67             pNewSet = new SfxItemSet(pNew->GetAttrSet());
68             nId = pNew->GetPoolFmtId() & COLL_GET_RANGE_BITS;
69             bAuto = pNew->IsAuto();
70 
71             Delete();
72         }
73     }
74 }
75 
RedoImpl(::sw::UndoRedoContext &)76 void SwUndoFmtCreate::RedoImpl(::sw::UndoRedoContext &)
77 {
78     SwFmt * pDerivedFrom = Find(sDerivedFrom);
79     SwFmt * pFmt = Create(pDerivedFrom);
80 
81     if (pFmt && pNewSet)
82     {
83         pFmt->SetAuto(bAuto);
84         pDoc->ChgFmt(*pFmt, *pNewSet);
85         pFmt->SetPoolFmtId((pFmt->GetPoolFmtId()
86                             & ~COLL_GET_RANGE_BITS)
87                            | nId);
88 
89         pNew = pFmt;
90     }
91     else
92         pNew = NULL;
93 }
94 
GetRewriter() const95 SwRewriter SwUndoFmtCreate::GetRewriter() const
96 {
97     if (sNewName.Len() == 0 && pNew)
98         sNewName = pNew->GetName();
99 
100     SwRewriter aRewriter;
101 
102     aRewriter.AddRule(UNDO_ARG1, sNewName);
103 
104     return aRewriter;
105 }
106 
SwUndoFmtDelete(SwUndoId nUndoId,SwFmt * _pOld,SwDoc * _pDoc)107 SwUndoFmtDelete::SwUndoFmtDelete
108 (SwUndoId nUndoId, SwFmt * _pOld, SwDoc * _pDoc)
109     : SwUndo(nUndoId),
110       pDoc(_pDoc), sOldName(_pOld->GetName()),
111       aOldSet(_pOld->GetAttrSet())
112 {
113     sDerivedFrom = _pOld->DerivedFrom()->GetName();
114     nId = _pOld->GetPoolFmtId() & COLL_GET_RANGE_BITS;
115     bAuto = _pOld->IsAuto();
116 }
117 
~SwUndoFmtDelete()118 SwUndoFmtDelete::~SwUndoFmtDelete()
119 {
120 }
121 
UndoImpl(::sw::UndoRedoContext &)122 void SwUndoFmtDelete::UndoImpl(::sw::UndoRedoContext &)
123 {
124     SwFmt * pDerivedFrom = Find(sDerivedFrom);
125 
126     SwFmt * pFmt = Create(pDerivedFrom);
127 
128     if (pFmt)
129     {
130         pDoc->ChgFmt(*pFmt, aOldSet);
131         pFmt->SetAuto(bAuto);
132         pFmt->SetPoolFmtId((pFmt->GetPoolFmtId() &
133                                 ~COLL_GET_RANGE_BITS)
134                                | nId);
135     }
136 }
137 
RedoImpl(::sw::UndoRedoContext &)138 void SwUndoFmtDelete::RedoImpl(::sw::UndoRedoContext &)
139 {
140     SwFmt * pOld = Find(sOldName);
141 
142     if (pOld)
143     {
144         Delete(pOld);
145     }
146 }
147 
GetRewriter() const148 SwRewriter SwUndoFmtDelete::GetRewriter() const
149 {
150     SwRewriter aRewriter;
151 
152     aRewriter.AddRule(UNDO_ARG1, sOldName);
153 
154     return aRewriter;
155 }
156 
SwUndoRenameFmt(SwUndoId nUndoId,const String & _sOldName,const String & _sNewName,SwDoc * _pDoc)157 SwUndoRenameFmt::SwUndoRenameFmt(SwUndoId nUndoId,
158                                  const String & _sOldName,
159                                  const String & _sNewName,
160                                  SwDoc * _pDoc)
161     : SwUndo(nUndoId), sOldName(_sOldName),
162       sNewName(_sNewName), pDoc(_pDoc)
163 {
164 }
165 
166 
~SwUndoRenameFmt()167 SwUndoRenameFmt::~SwUndoRenameFmt()
168 {
169 }
170 
UndoImpl(::sw::UndoRedoContext &)171 void SwUndoRenameFmt::UndoImpl(::sw::UndoRedoContext &)
172 {
173     SwFmt * pFmt = Find(sNewName);
174 
175     if (pFmt)
176     {
177         pDoc->RenameFmt(*pFmt, sOldName, sal_True);
178     }
179 }
180 
RedoImpl(::sw::UndoRedoContext &)181 void SwUndoRenameFmt::RedoImpl(::sw::UndoRedoContext &)
182 {
183     SwFmt *  pFmt = Find(sOldName);
184 
185     if (pFmt)
186     {
187         pDoc->RenameFmt(*pFmt, sNewName, sal_True);
188     }
189 }
190 
GetRewriter() const191 SwRewriter SwUndoRenameFmt::GetRewriter() const
192 {
193     SwRewriter aRewriter;
194 
195     aRewriter.AddRule(UNDO_ARG1, sOldName);
196     aRewriter.AddRule(UNDO_ARG2, SW_RES(STR_YIELDS));
197     aRewriter.AddRule(UNDO_ARG3, sNewName);
198 
199     return aRewriter;
200 }
201 
SwUndoTxtFmtCollCreate(SwTxtFmtColl * _pNew,SwTxtFmtColl * _pDerivedFrom,SwDoc * _pDoc)202 SwUndoTxtFmtCollCreate::SwUndoTxtFmtCollCreate
203 (SwTxtFmtColl * _pNew, SwTxtFmtColl * _pDerivedFrom, SwDoc * _pDoc)
204     : SwUndoFmtCreate(UNDO_TXTFMTCOL_CREATE, _pNew, _pDerivedFrom, _pDoc)
205 {
206 }
207 
Create(SwFmt * pDerivedFrom)208 SwFmt * SwUndoTxtFmtCollCreate::Create(SwFmt * pDerivedFrom)
209 {
210     return pDoc->MakeTxtFmtColl(sNewName, (SwTxtFmtColl *)pDerivedFrom, sal_True);
211 }
212 
Delete()213 void SwUndoTxtFmtCollCreate::Delete()
214 {
215     pDoc->DelTxtFmtColl((SwTxtFmtColl *) pNew, sal_True);
216 }
217 
Find(const String & rName) const218 SwFmt * SwUndoTxtFmtCollCreate::Find(const String & rName) const
219 {
220     return pDoc->FindTxtFmtCollByName(rName);
221 }
222 
SwUndoTxtFmtCollDelete(SwTxtFmtColl * _pOld,SwDoc * _pDoc)223 SwUndoTxtFmtCollDelete::SwUndoTxtFmtCollDelete(SwTxtFmtColl * _pOld,
224                                                SwDoc * _pDoc)
225     : SwUndoFmtDelete(UNDO_TXTFMTCOL_DELETE, _pOld, _pDoc)
226 {
227 }
228 
Create(SwFmt * pDerivedFrom)229 SwFmt * SwUndoTxtFmtCollDelete::Create(SwFmt * pDerivedFrom)
230 {
231     return pDoc->MakeTxtFmtColl(sOldName, (SwTxtFmtColl *) pDerivedFrom, sal_True);
232 }
233 
Delete(SwFmt * pOld)234 void SwUndoTxtFmtCollDelete::Delete(SwFmt * pOld)
235 {
236     pDoc->DelTxtFmtColl((SwTxtFmtColl *) pOld, sal_True);
237 }
238 
Find(const String & rName) const239 SwFmt * SwUndoTxtFmtCollDelete::Find(const String & rName) const
240 {
241     return pDoc->FindTxtFmtCollByName(rName);
242 }
243 
SwUndoRenameFmtColl(const String & sInitOldName,const String & sInitNewName,SwDoc * _pDoc)244 SwUndoRenameFmtColl::SwUndoRenameFmtColl(const String & sInitOldName,
245                                          const String & sInitNewName,
246                                          SwDoc * _pDoc)
247     : SwUndoRenameFmt(UNDO_TXTFMTCOL_RENAME, sInitOldName, sInitNewName, _pDoc)
248 {
249 }
250 
Find(const String & rName) const251 SwFmt * SwUndoRenameFmtColl::Find(const String & rName) const
252 {
253     return pDoc->FindTxtFmtCollByName(rName);
254 }
255 
SwUndoCharFmtCreate(SwCharFmt * pNewFmt,SwCharFmt * pDerivedFrom,SwDoc * pDocument)256 SwUndoCharFmtCreate::SwUndoCharFmtCreate(SwCharFmt * pNewFmt,
257                                          SwCharFmt * pDerivedFrom,
258                                          SwDoc * pDocument)
259     : SwUndoFmtCreate(UNDO_CHARFMT_CREATE, pNewFmt, pDerivedFrom, pDocument)
260 {
261 }
262 
Create(SwFmt * pDerivedFrom)263 SwFmt * SwUndoCharFmtCreate::Create(SwFmt * pDerivedFrom)
264 {
265     return pDoc->MakeCharFmt(sNewName, (SwCharFmt *) pDerivedFrom, sal_True);
266 }
267 
Delete()268 void SwUndoCharFmtCreate::Delete()
269 {
270     pDoc->DelCharFmt((SwCharFmt *) pNew, sal_True);
271 }
272 
Find(const String & rName) const273 SwFmt * SwUndoCharFmtCreate::Find(const String & rName) const
274 {
275     return pDoc->FindCharFmtByName(rName);
276 }
277 
SwUndoCharFmtDelete(SwCharFmt * pOld,SwDoc * pDocument)278 SwUndoCharFmtDelete::SwUndoCharFmtDelete(SwCharFmt * pOld, SwDoc * pDocument)
279     : SwUndoFmtDelete(UNDO_CHARFMT_DELETE, pOld, pDocument)
280 {
281 }
282 
Create(SwFmt * pDerivedFrom)283 SwFmt * SwUndoCharFmtDelete::Create(SwFmt * pDerivedFrom)
284 {
285     return pDoc->MakeCharFmt(sOldName, (SwCharFmt *) pDerivedFrom, sal_True);
286 }
287 
Delete(SwFmt * pFmt)288 void SwUndoCharFmtDelete::Delete(SwFmt * pFmt)
289 {
290     pDoc->DelCharFmt((SwCharFmt *) pFmt, sal_True);
291 }
292 
Find(const String & rName) const293 SwFmt * SwUndoCharFmtDelete::Find(const String & rName) const
294 {
295     return pDoc->FindCharFmtByName(rName);
296 }
297 
SwUndoRenameCharFmt(const String & sInitOldName,const String & sInitNewName,SwDoc * pDocument)298 SwUndoRenameCharFmt::SwUndoRenameCharFmt(const String & sInitOldName,
299                                          const String & sInitNewName,
300                                          SwDoc * pDocument)
301     : SwUndoRenameFmt(UNDO_CHARFMT_RENAME, sInitOldName, sInitNewName, pDocument)
302 {
303 }
304 
Find(const String & rName) const305 SwFmt * SwUndoRenameCharFmt::Find(const String & rName) const
306 {
307     return pDoc->FindCharFmtByName(rName);
308 }
309 
SwUndoFrmFmtCreate(SwFrmFmt * pNewFmt,SwFrmFmt * pDerivedFrom,SwDoc * pDocument)310 SwUndoFrmFmtCreate::SwUndoFrmFmtCreate(SwFrmFmt * pNewFmt,
311                                        SwFrmFmt * pDerivedFrom,
312                                        SwDoc * pDocument)
313     : SwUndoFmtCreate(UNDO_FRMFMT_CREATE, pNewFmt, pDerivedFrom, pDocument),
314       bAuto(pNewFmt->IsAuto())
315 {
316 }
317 
Create(SwFmt * pDerivedFrom)318 SwFmt * SwUndoFrmFmtCreate::Create(SwFmt * pDerivedFrom)
319 {
320     return pDoc->MakeFrmFmt(sNewName, (SwFrmFmt *) pDerivedFrom, sal_True, bAuto);
321 }
322 
Delete()323 void SwUndoFrmFmtCreate::Delete()
324 {
325     pDoc->DelFrmFmt((SwFrmFmt *) pNew, sal_True);
326 }
327 
Find(const String & rName) const328 SwFmt * SwUndoFrmFmtCreate::Find(const String & rName) const
329 {
330     return pDoc->FindFrmFmtByName(rName);
331 }
332 
SwUndoFrmFmtDelete(SwFrmFmt * pOld,SwDoc * pDocument)333 SwUndoFrmFmtDelete::SwUndoFrmFmtDelete(SwFrmFmt * pOld, SwDoc * pDocument)
334     : SwUndoFmtDelete(UNDO_FRMFMT_DELETE, pOld, pDocument)
335 {
336 }
337 
Create(SwFmt * pDerivedFrom)338 SwFmt * SwUndoFrmFmtDelete::Create(SwFmt * pDerivedFrom)
339 {
340     return pDoc->MakeFrmFmt(sOldName, (SwFrmFmt *) pDerivedFrom, sal_True);
341 }
342 
Delete(SwFmt * pFmt)343 void SwUndoFrmFmtDelete::Delete(SwFmt * pFmt)
344 {
345     pDoc->DelFrmFmt((SwFrmFmt *) pFmt, sal_True);
346 }
347 
Find(const String & rName) const348 SwFmt * SwUndoFrmFmtDelete::Find(const String & rName) const
349 {
350     return pDoc->FindFrmFmtByName(rName);
351 }
352 
SwUndoRenameFrmFmt(const String & sInitOldName,const String & sInitNewName,SwDoc * pDocument)353 SwUndoRenameFrmFmt::SwUndoRenameFrmFmt(const String & sInitOldName,
354                                        const String & sInitNewName,
355                                        SwDoc * pDocument)
356     : SwUndoRenameFmt(UNDO_FRMFMT_RENAME, sInitOldName, sInitNewName, pDocument)
357 {
358 }
359 
Find(const String & rName) const360 SwFmt * SwUndoRenameFrmFmt::Find(const String & rName) const
361 {
362     return pDoc->FindFrmFmtByName(rName);
363 }
364 
SwUndoNumruleCreate(const SwNumRule * _pNew,SwDoc * _pDoc)365 SwUndoNumruleCreate::SwUndoNumruleCreate(const SwNumRule * _pNew,
366                                          SwDoc * _pDoc)
367     : SwUndo(UNDO_NUMRULE_CREATE), pNew(_pNew), aNew(*_pNew), pDoc(_pDoc),
368       bInitialized(false)
369 {
370 }
371 
UndoImpl(::sw::UndoRedoContext &)372 void SwUndoNumruleCreate::UndoImpl(::sw::UndoRedoContext &)
373 {
374     if (! bInitialized)
375     {
376         aNew = *pNew;
377         bInitialized = true;
378     }
379 
380     pDoc->DelNumRule(aNew.GetName(), sal_True);
381 }
382 
RedoImpl(::sw::UndoRedoContext &)383 void SwUndoNumruleCreate::RedoImpl(::sw::UndoRedoContext &)
384 {
385     pDoc->MakeNumRule(aNew.GetName(), &aNew, sal_True);
386 }
387 
GetRewriter() const388 SwRewriter SwUndoNumruleCreate::GetRewriter() const
389 {
390     SwRewriter aResult;
391 
392     if (! bInitialized)
393     {
394         aNew = *pNew;
395         bInitialized = true;
396     }
397 
398     aResult.AddRule(UNDO_ARG1, aNew.GetName());
399 
400     return aResult;
401 }
402 
SwUndoNumruleDelete(const SwNumRule & rRule,SwDoc * _pDoc)403 SwUndoNumruleDelete::SwUndoNumruleDelete(const SwNumRule & rRule,
404                                          SwDoc * _pDoc)
405     : SwUndo(UNDO_NUMRULE_DELETE), aOld(rRule), pDoc(_pDoc)
406 {
407 }
408 
UndoImpl(::sw::UndoRedoContext &)409 void SwUndoNumruleDelete::UndoImpl(::sw::UndoRedoContext &)
410 {
411     pDoc->MakeNumRule(aOld.GetName(), &aOld, sal_True);
412 }
413 
RedoImpl(::sw::UndoRedoContext &)414 void SwUndoNumruleDelete::RedoImpl(::sw::UndoRedoContext &)
415 {
416     pDoc->DelNumRule(aOld.GetName(), sal_True);
417 }
418 
GetRewriter() const419 SwRewriter SwUndoNumruleDelete::GetRewriter() const
420 {
421     SwRewriter aResult;
422 
423     aResult.AddRule(UNDO_ARG1, aOld.GetName());
424 
425     return aResult;
426 }
427 
SwUndoNumruleRename(const String & _aOldName,const String & _aNewName,SwDoc * _pDoc)428 SwUndoNumruleRename::SwUndoNumruleRename(const String & _aOldName,
429                                          const String & _aNewName,
430                                          SwDoc * _pDoc)
431     : SwUndo(UNDO_NUMRULE_RENAME), aOldName(_aOldName), aNewName(_aNewName),
432       pDoc(_pDoc)
433 {
434 }
435 
UndoImpl(::sw::UndoRedoContext &)436 void SwUndoNumruleRename::UndoImpl(::sw::UndoRedoContext &)
437 {
438     pDoc->RenameNumRule(aNewName, aOldName, sal_True);
439 }
440 
RedoImpl(::sw::UndoRedoContext &)441 void SwUndoNumruleRename::RedoImpl(::sw::UndoRedoContext &)
442 {
443     pDoc->RenameNumRule(aOldName, aNewName, sal_True);
444 }
445 
GetRewriter() const446 SwRewriter SwUndoNumruleRename::GetRewriter() const
447 {
448     SwRewriter aRewriter;
449 
450     aRewriter.AddRule(UNDO_ARG1, aOldName);
451     aRewriter.AddRule(UNDO_ARG2, SW_RES(STR_YIELDS));
452     aRewriter.AddRule(UNDO_ARG3, aNewName);
453 
454     return aRewriter;
455 }
456