~kiwec/ke

ke/src/Event.h -rw-r--r-- 1.4 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
#pragma once
#include <string>

class Buffer;
class Line;

enum class EventType {
	EVENT_WRITE,
	EVENT_ERASE,
};

class Event {
protected:
	bool m_undone = false;

public:
	virtual ~Event() {}

	/// Undo action on the given buffer
	virtual void undo(Buffer& buf) = 0;

	/// Redo action on the given buffer
	virtual void redo(Buffer& buf) = 0;

	/// Try merging with another event
	/// Returns true if merge was successful
	virtual bool merge(Event* ev) = 0;

	/// Return event type
	virtual EventType type() = 0;
};

class WriteEvent : public Event {
protected:
	wchar_t* text;
	uint line;
	uint pos;

	/// Always false in WriteEvent
	bool erase_forwards = false;

	void write(Buffer& buf);
	void erase(Buffer& buf);

public:
	WriteEvent(const wchar_t* ptext, uint line, uint pos) : line(line), pos(pos) {
		// Copy wstr
		text = new wchar_t[wcslen(ptext) + 1];
		wcscpy(text, ptext);
	}

	~WriteEvent() {
		delete[] text;
	}

	virtual void undo(Buffer& buf) override;
	virtual void redo(Buffer& buf) override;
	virtual bool merge(Event* ev) override;
	virtual EventType type() override;
};

class EraseEvent : public WriteEvent {
public:
	virtual ~EraseEvent() {}
	EraseEvent(const wchar_t* text, uint line, uint pos, bool erase_forwards) : WriteEvent(text, line, pos) {
		this->erase_forwards = erase_forwards;
	}

	virtual void undo(Buffer& buf) override;
	virtual void redo(Buffer& buf) override;
	virtual bool merge(Event* ev) override;
	virtual EventType type() override;
};