xref: /haiku/src/apps/haikudepot/edits_generic/CompoundEdit.cpp (revision 1403313c7fab3f265ed48b14f18a82f08aaeca78)
1651e8326SStephan Aßmus /*
2651e8326SStephan Aßmus  * Copyright 2006-2112, Stephan Aßmus <superstippi@gmx.de>
3*1403313cSAndrew Lindesay  * Copyright 2021, Andrew Lindesay <apl@lindesay.co.nz>
4651e8326SStephan Aßmus  * Distributed under the terms of the MIT License.
5651e8326SStephan Aßmus  */
6651e8326SStephan Aßmus 
7651e8326SStephan Aßmus #include "CompoundEdit.h"
8651e8326SStephan Aßmus 
9651e8326SStephan Aßmus #include <stdio.h>
10651e8326SStephan Aßmus 
11651e8326SStephan Aßmus 
CompoundEdit(const char * name)12651e8326SStephan Aßmus CompoundEdit::CompoundEdit(const char* name)
13651e8326SStephan Aßmus 	: UndoableEdit()
14651e8326SStephan Aßmus 	, fEdits()
15651e8326SStephan Aßmus 	, fName(name)
16651e8326SStephan Aßmus {
17651e8326SStephan Aßmus }
18651e8326SStephan Aßmus 
19651e8326SStephan Aßmus 
~CompoundEdit()20651e8326SStephan Aßmus CompoundEdit::~CompoundEdit()
21651e8326SStephan Aßmus {
22651e8326SStephan Aßmus }
23651e8326SStephan Aßmus 
24651e8326SStephan Aßmus 
25651e8326SStephan Aßmus status_t
InitCheck()26651e8326SStephan Aßmus CompoundEdit::InitCheck()
27651e8326SStephan Aßmus {
28651e8326SStephan Aßmus 	return B_OK;
29651e8326SStephan Aßmus }
30651e8326SStephan Aßmus 
31651e8326SStephan Aßmus 
32651e8326SStephan Aßmus status_t
Perform(EditContext & context)33651e8326SStephan Aßmus CompoundEdit::Perform(EditContext& context)
34651e8326SStephan Aßmus {
35651e8326SStephan Aßmus 	status_t status = B_OK;
36651e8326SStephan Aßmus 
37*1403313cSAndrew Lindesay 	int32 count = static_cast<int32>(fEdits.size());
38651e8326SStephan Aßmus 	int32 i = 0;
39651e8326SStephan Aßmus 	for (; i < count; i++) {
40*1403313cSAndrew Lindesay 		status = fEdits[i]->Perform(context);
41651e8326SStephan Aßmus 		if (status != B_OK)
42651e8326SStephan Aßmus 			break;
43651e8326SStephan Aßmus 	}
44651e8326SStephan Aßmus 
45651e8326SStephan Aßmus 	if (status != B_OK) {
46651e8326SStephan Aßmus 		// roll back
47651e8326SStephan Aßmus 		i--;
48651e8326SStephan Aßmus 		for (; i >= 0; i--) {
49*1403313cSAndrew Lindesay 			fEdits[i]->Undo(context);
50651e8326SStephan Aßmus 		}
51651e8326SStephan Aßmus 	}
52651e8326SStephan Aßmus 
53651e8326SStephan Aßmus 	return status;
54651e8326SStephan Aßmus }
55651e8326SStephan Aßmus 
56651e8326SStephan Aßmus 
57651e8326SStephan Aßmus status_t
Undo(EditContext & context)58651e8326SStephan Aßmus CompoundEdit::Undo(EditContext& context)
59651e8326SStephan Aßmus {
60651e8326SStephan Aßmus 	status_t status = B_OK;
61651e8326SStephan Aßmus 
62*1403313cSAndrew Lindesay 	int32 count = static_cast<int32>(fEdits.size());
63651e8326SStephan Aßmus 	int32 i = count - 1;
64651e8326SStephan Aßmus 	for (; i >= 0; i--) {
65*1403313cSAndrew Lindesay 		status = fEdits[i]->Undo(context);
66651e8326SStephan Aßmus 		if (status != B_OK)
67651e8326SStephan Aßmus 			break;
68651e8326SStephan Aßmus 	}
69651e8326SStephan Aßmus 
70651e8326SStephan Aßmus 	if (status != B_OK) {
71651e8326SStephan Aßmus 		// roll back
72651e8326SStephan Aßmus 		i++;
73651e8326SStephan Aßmus 		for (; i < count; i++) {
74*1403313cSAndrew Lindesay 			fEdits[i]->Redo(context);
75651e8326SStephan Aßmus 		}
76651e8326SStephan Aßmus 	}
77651e8326SStephan Aßmus 
78651e8326SStephan Aßmus 	return status;
79651e8326SStephan Aßmus }
80651e8326SStephan Aßmus 
81651e8326SStephan Aßmus 
82651e8326SStephan Aßmus status_t
Redo(EditContext & context)83651e8326SStephan Aßmus CompoundEdit::Redo(EditContext& context)
84651e8326SStephan Aßmus {
85651e8326SStephan Aßmus 	status_t status = B_OK;
86651e8326SStephan Aßmus 
87*1403313cSAndrew Lindesay 	int32 count = static_cast<int32>(fEdits.size());
88651e8326SStephan Aßmus 	int32 i = 0;
89651e8326SStephan Aßmus 	for (; i < count; i++) {
90*1403313cSAndrew Lindesay 		status = fEdits[i]->Redo(context);
91651e8326SStephan Aßmus 		if (status != B_OK)
92651e8326SStephan Aßmus 			break;
93651e8326SStephan Aßmus 	}
94651e8326SStephan Aßmus 
95651e8326SStephan Aßmus 	if (status != B_OK) {
96651e8326SStephan Aßmus 		// roll back
97651e8326SStephan Aßmus 		i--;
98651e8326SStephan Aßmus 		for (; i >= 0; i--) {
99*1403313cSAndrew Lindesay 			fEdits[i]->Undo(context);
100651e8326SStephan Aßmus 		}
101651e8326SStephan Aßmus 	}
102651e8326SStephan Aßmus 
103651e8326SStephan Aßmus 	return status;
104651e8326SStephan Aßmus }
105651e8326SStephan Aßmus 
106651e8326SStephan Aßmus 
107651e8326SStephan Aßmus void
GetName(BString & name)108651e8326SStephan Aßmus CompoundEdit::GetName(BString& name)
109651e8326SStephan Aßmus {
110651e8326SStephan Aßmus 	name << fName;
111651e8326SStephan Aßmus }
112651e8326SStephan Aßmus 
113651e8326SStephan Aßmus 
114*1403313cSAndrew Lindesay void
AppendEdit(const UndoableEditRef & edit)115651e8326SStephan Aßmus CompoundEdit::AppendEdit(const UndoableEditRef& edit)
116651e8326SStephan Aßmus {
117*1403313cSAndrew Lindesay 	fEdits.push_back(edit);
118651e8326SStephan Aßmus }
119