1 /* 2 * Copyright 2006-2112, Stephan Aßmus <superstippi@gmx.de> 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include "CompoundEdit.h" 7 8 #include <stdio.h> 9 10 11 CompoundEdit::CompoundEdit(const char* name) 12 : UndoableEdit() 13 , fEdits() 14 , fName(name) 15 { 16 } 17 18 19 CompoundEdit::~CompoundEdit() 20 { 21 } 22 23 24 status_t 25 CompoundEdit::InitCheck() 26 { 27 return B_OK; 28 } 29 30 31 status_t 32 CompoundEdit::Perform(EditContext& context) 33 { 34 status_t status = B_OK; 35 36 int32 count = fEdits.CountItems(); 37 int32 i = 0; 38 for (; i < count; i++) { 39 status = fEdits.ItemAtFast(i)->Perform(context); 40 if (status != B_OK) 41 break; 42 } 43 44 if (status != B_OK) { 45 // roll back 46 i--; 47 for (; i >= 0; i--) { 48 fEdits.ItemAtFast(i)->Undo(context); 49 } 50 } 51 52 return status; 53 } 54 55 56 status_t 57 CompoundEdit::Undo(EditContext& context) 58 { 59 status_t status = B_OK; 60 61 int32 count = fEdits.CountItems(); 62 int32 i = count - 1; 63 for (; i >= 0; i--) { 64 status = fEdits.ItemAtFast(i)->Undo(context); 65 if (status != B_OK) 66 break; 67 } 68 69 if (status != B_OK) { 70 // roll back 71 i++; 72 for (; i < count; i++) { 73 fEdits.ItemAtFast(i)->Redo(context); 74 } 75 } 76 77 return status; 78 } 79 80 81 status_t 82 CompoundEdit::Redo(EditContext& context) 83 { 84 status_t status = B_OK; 85 86 int32 count = fEdits.CountItems(); 87 int32 i = 0; 88 for (; i < count; i++) { 89 status = fEdits.ItemAtFast(i)->Redo(context); 90 if (status != B_OK) 91 break; 92 } 93 94 if (status != B_OK) { 95 // roll back 96 i--; 97 for (; i >= 0; i--) { 98 fEdits.ItemAtFast(i)->Undo(context); 99 } 100 } 101 102 return status; 103 } 104 105 106 void 107 CompoundEdit::GetName(BString& name) 108 { 109 name << fName; 110 } 111 112 113 bool 114 CompoundEdit::AppendEdit(const UndoableEditRef& edit) 115 { 116 return fEdits.Add(edit); 117 } 118