aboutsummaryrefslogtreecommitdiff
path: root/undo-redo.h
blob: 5c42fdb00472a24a89eceecabf834922a2ffd700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 *
 * Copyright (C) 2022 Jeremias Stotter <jeremias@stotter.eu>
 *
 * This file is part of network-simulator.
 *
 * network-simulator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * network-simulator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with network-simulator. If not, see <https://www.gnu.org/licenses/>. 
*/

#include <stdlib.h>

#include "device.h"
#include "connection.h"
#include "helpers.h"
#include "decorations.h"

enum undo_actions {
	undo_nothing,
	undo_move,
	undo_move_deco,
	undo_add_dev,
	undo_del_dev,
	undo_add_con,
	undo_ren_dev,
	undo_del_con,
	undo_add_deco,
	undo_del_deco,
	undo_set_deco
};

struct undo_entry {
	LL_HEAD(struct undo_entry);
	// In case multiple entries are supposed to bundled together, use this
	struct undo_entry* next_sibling;
	struct undo_entry* prev_sibling;
	
	enum undo_actions action;
	// This contains data specific to the undo action
	void* undo_data;
	int undone; // Set to 1 once this action is undone, and to 0 again if it is redone, start out as 0
};

void undo_add_entry(struct undo_entry* entry);
void free_all_undo();
int undo();
int redo();

// Following are structs containing the data for each the above actions needed to undo and redo
struct undo_move_data {
	// devices should be multiple (dev_n) strings seperated by \0
	int dev_n;
	char* devices;
	// This will be how much the device was moved
	double X_off;
	double Y_off;
};

struct undo_move_deco_data {
	struct deco* deco;
	double X_off;
	double Y_off;
};

struct undo_add_dev_data {
	struct device* dev;
	struct dev_template* template;
};

struct undo_del_dev_data {
	struct device* dev;
};

struct undo_add_con_data {
	struct connection* con;
	struct interface* int1;
	struct interface* int2;
	struct connection_template* template;
};

struct undo_del_con_data {
	struct interface* int1;
	struct interface* int2;
	struct connection_template* template;
};

struct undo_ren_data {
	struct device* dev;
	char* old_name;
	char* new_name;
};

struct undo_add_deco_data {
	struct deco* deco;	
};

struct undo_del_deco_data {
	struct deco* deco;
};

struct undo_set_deco_data {
	char* old_text;
	char* new_text;
	struct deco* deco;
};

#define NEW_UNDO_STRUCT(undo_action, data_type, undo_struct_name, data_struct_name)	\
				undo_struct_name = malloc_or_abort(sizeof(struct undo_entry)); \
				data_struct_name = malloc_or_abort(sizeof(struct data_type)); \
				undo_struct_name->action = undo_action; \
				undo_struct_name->next = NULL, undo_struct_name->prev = NULL; \
				undo_struct_name->undo_data = data_struct_name; \
				undo_struct_name->next_sibling = NULL; \
				undo_struct_name->prev_sibling = NULL; \
				undo_struct_name->undone = 0;
Jeremias Stotters git repositories generated by CGIT