forked from githole/Live-Coder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEditor.h
133 lines (102 loc) · 2.27 KB
/
TextEditor.h
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef _TEXT_EDITOR_
#define _TEXT_EDITOR_
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
namespace LiveCoder {
enum EditorCursorMoveType {
ECMT_UP,
ECMT_DOWN,
ECMT_LEFT,
ECMT_RIGHT,
};
class EditorCursor {
public:
int row;
int col;
EditorCursor() : row(0), col(0) {
}
};
typedef std::vector<std::string> TextEditorBuffer;
typedef std::vector<std::string*> TextEditorPtrBuffer;
class TextEditorLog {
public:
EditorCursor cursor;
TextEditorBuffer buffer;
int lineOffset;
TextEditorLog(const EditorCursor& cursor_, const TextEditorBuffer& buffer_, const int lineOffset_) :
cursor(cursor_), buffer(buffer_), lineOffset(lineOffset_) {
}
};
class TextEditor
{
private:
std::vector<TextEditorLog> log;
int logIndex;
EditorCursor nowCursor;
TextEditorBuffer buffer;
int maxLineNum;
int lineOffset;
bool selectMode;
EditorCursor selectStart, selectEnd;
TextEditorBuffer copyBuffer;
public:
void MoveCursor(enum EditorCursorMoveType type);
void InsertCharacter(char ch, bool historyEnable = true, bool autoIndent = true);
void Backspace();
void Delete();
void ScrollDown();
void ScrollUp();
void MoveHead();
void MoveTail();
void Undo();
void Redo();
void Home();
void End();
void PageUp();
void PageDown();
void Copy();
void DeleteSelectedArea();
void Paste();
void SnapShot();
void BeginSelect();
void EndSelect();
bool IsSelectMode() { return selectMode; }
EditorCursor GetSelectStart();
void UpdateLog();
void Save(std::string filename);
void Load(std::string filename);
EditorCursor GetCursorPosition();
TextEditorPtrBuffer GetText();
TextEditorPtrBuffer GetVisibleText();
const int GetMaxLineNum() { return maxLineNum; }
const int GetLineOffset() {
if (logIndex == -1) {
return lineOffset;
} else {
return log[logIndex].lineOffset;
}
}
const int GetLineNum() {
if (logIndex == -1) {
return buffer.size();
} else {
return log[logIndex].buffer.size();
}
}
const int GetLineLength(const int line) {
if (line+lineOffset >= 0 && line+lineOffset < buffer.size())
return buffer[line + lineOffset].length();
return 0;
}
std::string ToString();
void ClearBuffer();
void Output();
TextEditor(void);
virtual ~TextEditor(void);
};
}
#endif