-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathScintillaDoc.h
192 lines (153 loc) · 4.32 KB
/
ScintillaDoc.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once
#include "StdAfx.h"
#include <string>
struct ScintillaDoc {
HWND hCurrentEditView;
bool inSelection;
struct sciWorkText {
char* text;
intptr_t length;
intptr_t selstart;
operator bool() {
return text != NULL;
}
void FreeMemory() {
if (text) {
delete[] text;
text = NULL;
}
}
};
ScintillaDoc(HWND scHandle) {
hCurrentEditView = scHandle;
inSelection = false;
}
bool IsReadOnly() {
return 0 != (int) ::SendMessage(hCurrentEditView, SCI_GETREADONLY, 0, 0);
}
int GetTabWidth() {
int tabwidth = (int) ::SendMessage(hCurrentEditView, SCI_GETTABWIDTH, 0, 0);
if (tabwidth <= 0) tabwidth = 4;
return tabwidth;
}
bool UseTabs() {
return (bool) ::SendMessage(hCurrentEditView, SCI_GETUSETABS, 0, 0);
}
std::string Tab() {
if (UseTabs()) {
return "\t";
}
else {
std::string tab;
int tabwidth = GetTabWidth();
for (int i = 0; i < tabwidth; i++) {
tab.append(" ");
}
return tab;
}
}
std::string EOL() {
int eol = (int) ::SendMessage(hCurrentEditView, SCI_GETEOLMODE, 0, 0);
switch (eol) {
case SC_EOL_CR: return "\r";
case SC_EOL_LF: return "\n";
case SC_EOL_CRLF:
default: return "\r\n";
}
}
Sci_PositionCR SelectionStart() {
return (Sci_PositionCR) ::SendMessage(hCurrentEditView, SCI_GETSELECTIONSTART, 0, 0);
}
Sci_PositionCR SelectionEnd() {
return (Sci_PositionCR) ::SendMessage(hCurrentEditView, SCI_GETSELECTIONEND, 0, 0);
}
void SetCurrentPosition(Sci_PositionCR selstart) {
::SendMessage(hCurrentEditView, SCI_SETCURRENTPOS, selstart, 0);
}
void SetAnchor(Sci_PositionCR selend) {
::SendMessage(hCurrentEditView, SCI_SETANCHOR, selend, 0);
}
void ClearAll() {
::SendMessage(hCurrentEditView, SCI_CLEARALL, 0, 0);
}
void SetText(const char* text) {
::SendMessage(hCurrentEditView, SCI_SETTEXT, 0, reinterpret_cast<LPARAM>(text));
}
void ReplaceSelection(const char* text) {
::SendMessage(hCurrentEditView, SCI_REPLACESEL, 0, reinterpret_cast<LPARAM>(text));
}
Sci_PositionCR GetTextLength() {
return (Sci_PositionCR) ::SendMessage(hCurrentEditView, SCI_GETLENGTH, 0, 0);
}
char *GetSelectedText(size_t length) {
char *text = new char[length + sizeof(char)];
if (text == NULL) return false; // allocation error, abort check;
text[length] = 0;
::SendMessage(hCurrentEditView, SCI_GETSELTEXT, 0, reinterpret_cast<LPARAM>(text));
return text;
}
sciWorkText GetSelectedText() {
auto selstart = SelectionStart();
auto selend = SelectionEnd();
if (selend > selstart) {
auto len = selend - selstart;
return { GetSelectedText(len), (intptr_t) len, (intptr_t)selstart };
}
return { NULL,0,0 };
}
char* GetText(Sci_PositionCR length) {
char *text = new char[length + sizeof(char)];
if (text == NULL) return false; // allocation error, abort check
text[length] = 0;
::SendMessage(hCurrentEditView, SCI_GETTEXT, length + sizeof(char), reinterpret_cast<LPARAM>(text));
return text;
}
size_t GetText(Sci_PositionCR offset, char *target, Sci_PositionCR length) {
if (target == NULL) return 0; // allocation error, abort check
auto max = GetTextLength();
if (offset >= max) return 0;
Sci_TextRange range;
range.chrg.cpMin = offset;
range.chrg.cpMax = offset+length;
range.lpstrText = target;
if (range.chrg.cpMax > (Sci_PositionCR)max) {
range.chrg.cpMax = (Sci_PositionCR)max;
}
return ::SendMessage(hCurrentEditView, SCI_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&range));
}
sciWorkText GetWorkText() {
auto selstart = this->SelectionStart();
auto selend = this->SelectionEnd();
sciWorkText ret;
if (selend > selstart) { // selection
ret.length = selend - selstart;
inSelection = true;
ret.text = this->GetSelectedText(ret.length);
ret.selstart = selstart;
}
else {
ret.length = this->GetTextLength();
inSelection = false;
ret.text = this->GetText(ret.length);
ret.selstart = -1;
}
return ret;
}
void SetWorkText(const char* text) {
if (inSelection) {
this->ReplaceSelection(text);
}
else {
this->SetText(text);
}
}
void SetXOffset(int offset = 0) {
::SendMessage(hCurrentEditView, SCI_SETXOFFSET, offset, 0);
}
void SetScrollWidth(int width) {
::SendMessage(hCurrentEditView, SCI_SETSCROLLWIDTH, width, 0);
}
void EmptyUndoBuffer() {
::SendMessage(hCurrentEditView, SCI_EMPTYUNDOBUFFER, 0, 0);
}
};