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