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 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 51 SwUndoFmtCreate::~SwUndoFmtCreate() 52 { 53 } 54 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 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 95 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 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 118 SwUndoFmtDelete::~SwUndoFmtDelete() 119 { 120 } 121 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 138 void SwUndoFmtDelete::RedoImpl(::sw::UndoRedoContext &) 139 { 140 SwFmt * pOld = Find(sOldName); 141 142 if (pOld) 143 { 144 Delete(pOld); 145 } 146 } 147 148 SwRewriter SwUndoFmtDelete::GetRewriter() const 149 { 150 SwRewriter aRewriter; 151 152 aRewriter.AddRule(UNDO_ARG1, sOldName); 153 154 return aRewriter; 155 } 156 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 167 SwUndoRenameFmt::~SwUndoRenameFmt() 168 { 169 } 170 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 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 191 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 202 SwUndoTxtFmtCollCreate::SwUndoTxtFmtCollCreate 203 (SwTxtFmtColl * _pNew, SwTxtFmtColl * _pDerivedFrom, SwDoc * _pDoc) 204 : SwUndoFmtCreate(UNDO_TXTFMTCOL_CREATE, _pNew, _pDerivedFrom, _pDoc) 205 { 206 } 207 208 SwFmt * SwUndoTxtFmtCollCreate::Create(SwFmt * pDerivedFrom) 209 { 210 return pDoc->MakeTxtFmtColl(sNewName, (SwTxtFmtColl *)pDerivedFrom, sal_True); 211 } 212 213 void SwUndoTxtFmtCollCreate::Delete() 214 { 215 pDoc->DelTxtFmtColl((SwTxtFmtColl *) pNew, sal_True); 216 } 217 218 SwFmt * SwUndoTxtFmtCollCreate::Find(const String & rName) const 219 { 220 return pDoc->FindTxtFmtCollByName(rName); 221 } 222 223 SwUndoTxtFmtCollDelete::SwUndoTxtFmtCollDelete(SwTxtFmtColl * _pOld, 224 SwDoc * _pDoc) 225 : SwUndoFmtDelete(UNDO_TXTFMTCOL_DELETE, _pOld, _pDoc) 226 { 227 } 228 229 SwFmt * SwUndoTxtFmtCollDelete::Create(SwFmt * pDerivedFrom) 230 { 231 return pDoc->MakeTxtFmtColl(sOldName, (SwTxtFmtColl *) pDerivedFrom, sal_True); 232 } 233 234 void SwUndoTxtFmtCollDelete::Delete(SwFmt * pOld) 235 { 236 pDoc->DelTxtFmtColl((SwTxtFmtColl *) pOld, sal_True); 237 } 238 239 SwFmt * SwUndoTxtFmtCollDelete::Find(const String & rName) const 240 { 241 return pDoc->FindTxtFmtCollByName(rName); 242 } 243 244 SwUndoRenameFmtColl::SwUndoRenameFmtColl(const String & sInitOldName, 245 const String & sInitNewName, 246 SwDoc * _pDoc) 247 : SwUndoRenameFmt(UNDO_TXTFMTCOL_RENAME, sInitOldName, sInitNewName, _pDoc) 248 { 249 } 250 251 SwFmt * SwUndoRenameFmtColl::Find(const String & rName) const 252 { 253 return pDoc->FindTxtFmtCollByName(rName); 254 } 255 256 SwUndoCharFmtCreate::SwUndoCharFmtCreate(SwCharFmt * pNewFmt, 257 SwCharFmt * pDerivedFrom, 258 SwDoc * pDocument) 259 : SwUndoFmtCreate(UNDO_CHARFMT_CREATE, pNewFmt, pDerivedFrom, pDocument) 260 { 261 } 262 263 SwFmt * SwUndoCharFmtCreate::Create(SwFmt * pDerivedFrom) 264 { 265 return pDoc->MakeCharFmt(sNewName, (SwCharFmt *) pDerivedFrom, sal_True); 266 } 267 268 void SwUndoCharFmtCreate::Delete() 269 { 270 pDoc->DelCharFmt((SwCharFmt *) pNew, sal_True); 271 } 272 273 SwFmt * SwUndoCharFmtCreate::Find(const String & rName) const 274 { 275 return pDoc->FindCharFmtByName(rName); 276 } 277 278 SwUndoCharFmtDelete::SwUndoCharFmtDelete(SwCharFmt * pOld, SwDoc * pDocument) 279 : SwUndoFmtDelete(UNDO_CHARFMT_DELETE, pOld, pDocument) 280 { 281 } 282 283 SwFmt * SwUndoCharFmtDelete::Create(SwFmt * pDerivedFrom) 284 { 285 return pDoc->MakeCharFmt(sOldName, (SwCharFmt *) pDerivedFrom, sal_True); 286 } 287 288 void SwUndoCharFmtDelete::Delete(SwFmt * pFmt) 289 { 290 pDoc->DelCharFmt((SwCharFmt *) pFmt, sal_True); 291 } 292 293 SwFmt * SwUndoCharFmtDelete::Find(const String & rName) const 294 { 295 return pDoc->FindCharFmtByName(rName); 296 } 297 298 SwUndoRenameCharFmt::SwUndoRenameCharFmt(const String & sInitOldName, 299 const String & sInitNewName, 300 SwDoc * pDocument) 301 : SwUndoRenameFmt(UNDO_CHARFMT_RENAME, sInitOldName, sInitNewName, pDocument) 302 { 303 } 304 305 SwFmt * SwUndoRenameCharFmt::Find(const String & rName) const 306 { 307 return pDoc->FindCharFmtByName(rName); 308 } 309 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 318 SwFmt * SwUndoFrmFmtCreate::Create(SwFmt * pDerivedFrom) 319 { 320 return pDoc->MakeFrmFmt(sNewName, (SwFrmFmt *) pDerivedFrom, sal_True, bAuto); 321 } 322 323 void SwUndoFrmFmtCreate::Delete() 324 { 325 pDoc->DelFrmFmt((SwFrmFmt *) pNew, sal_True); 326 } 327 328 SwFmt * SwUndoFrmFmtCreate::Find(const String & rName) const 329 { 330 return pDoc->FindFrmFmtByName(rName); 331 } 332 333 SwUndoFrmFmtDelete::SwUndoFrmFmtDelete(SwFrmFmt * pOld, SwDoc * pDocument) 334 : SwUndoFmtDelete(UNDO_FRMFMT_DELETE, pOld, pDocument) 335 { 336 } 337 338 SwFmt * SwUndoFrmFmtDelete::Create(SwFmt * pDerivedFrom) 339 { 340 return pDoc->MakeFrmFmt(sOldName, (SwFrmFmt *) pDerivedFrom, sal_True); 341 } 342 343 void SwUndoFrmFmtDelete::Delete(SwFmt * pFmt) 344 { 345 pDoc->DelFrmFmt((SwFrmFmt *) pFmt, sal_True); 346 } 347 348 SwFmt * SwUndoFrmFmtDelete::Find(const String & rName) const 349 { 350 return pDoc->FindFrmFmtByName(rName); 351 } 352 353 SwUndoRenameFrmFmt::SwUndoRenameFrmFmt(const String & sInitOldName, 354 const String & sInitNewName, 355 SwDoc * pDocument) 356 : SwUndoRenameFmt(UNDO_FRMFMT_RENAME, sInitOldName, sInitNewName, pDocument) 357 { 358 } 359 360 SwFmt * SwUndoRenameFrmFmt::Find(const String & rName) const 361 { 362 return pDoc->FindFrmFmtByName(rName); 363 } 364 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 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 383 void SwUndoNumruleCreate::RedoImpl(::sw::UndoRedoContext &) 384 { 385 pDoc->MakeNumRule(aNew.GetName(), &aNew, sal_True); 386 } 387 388 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 403 SwUndoNumruleDelete::SwUndoNumruleDelete(const SwNumRule & rRule, 404 SwDoc * _pDoc) 405 : SwUndo(UNDO_NUMRULE_DELETE), aOld(rRule), pDoc(_pDoc) 406 { 407 } 408 409 void SwUndoNumruleDelete::UndoImpl(::sw::UndoRedoContext &) 410 { 411 pDoc->MakeNumRule(aOld.GetName(), &aOld, sal_True); 412 } 413 414 void SwUndoNumruleDelete::RedoImpl(::sw::UndoRedoContext &) 415 { 416 pDoc->DelNumRule(aOld.GetName(), sal_True); 417 } 418 419 SwRewriter SwUndoNumruleDelete::GetRewriter() const 420 { 421 SwRewriter aResult; 422 423 aResult.AddRule(UNDO_ARG1, aOld.GetName()); 424 425 return aResult; 426 } 427 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 436 void SwUndoNumruleRename::UndoImpl(::sw::UndoRedoContext &) 437 { 438 pDoc->RenameNumRule(aNewName, aOldName, sal_True); 439 } 440 441 void SwUndoNumruleRename::RedoImpl(::sw::UndoRedoContext &) 442 { 443 pDoc->RenameNumRule(aOldName, aNewName, sal_True); 444 } 445 446 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