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