~kiwec/ke

ke/src/Event.cc -rw-r--r-- 1.5 KiB
1cc8481d — Wolf Clément Fix compilation 3 years ago
                                                                                
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
#include "Event.h"

#include "Buffer.h"
#include "Syntax.h"

void WriteEvent::write(Buffer& buf) {
	buf.move_to(line);
	buf.set_x(pos);
	buf.insert(text, false);

	// Don't move on forward erase
	if(erase_forwards) {
		buf.move_to(line);
		buf.set_x(pos);
	}

	line = buf.get_current_line_nb();
	pos = buf.get_x();
}

void WriteEvent::erase(Buffer& buf) {
	buf.move_to(line);
	buf.set_x(pos);

	for(auto ptr = text; *ptr != L'\0'; ptr++) {
		if(erase_forwards) {
			buf.erase_forwards(false);
		} else {
			buf.erase_backwards(false);
		}
	}

	line = buf.get_current_line_nb();
	pos = buf.get_x();
}

void WriteEvent::redo(Buffer& buf) {
	// Sanity check
	if(!m_undone) return;

	write(buf);
	m_undone = false;
}

void WriteEvent::undo(Buffer& buf) {
	// Sanity check
	if(m_undone) return;

	erase(buf);
	m_undone = true;
}

bool WriteEvent::merge(Event* ev) {
	if(ev->type() != EventType::EVENT_WRITE) return false;
	auto we = static_cast<WriteEvent*>(ev);
	// TODO
	return false;
}

EventType WriteEvent::type() {
	return EventType::EVENT_WRITE;
}

void EraseEvent::redo(Buffer& buf) {
	// Sanity check
	if(!m_undone) return;

	erase(buf);
	m_undone = false;
}

void EraseEvent::undo(Buffer& buf) {
	// Sanity check
	if(m_undone) return;

	write(buf);
	m_undone = true;
}

bool EraseEvent::merge(Event* ev) {
	if(ev->type() != EventType::EVENT_ERASE) return false;
	auto ee = static_cast<EraseEvent*>(ev);
	// TODO
	return false;
}

EventType EraseEvent::type() {
	return EventType::EVENT_ERASE;
}