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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
#include "commons.h"
RichEdit::RichEdit(HWND hwnd)
: hwnd(NULL), ole(NULL), textDocument(NULL), stopped(0), undoEnabled(TRUE)
{
SetHWND(hwnd);
}
RichEdit::~RichEdit()
{
SetHWND(NULL);
}
bool RichEdit::IsValid() const
{
return ole != NULL;
}
HWND RichEdit::GetHWND() const
{
return hwnd;
}
void RichEdit::SetHWND(HWND hwnd)
{
if (textDocument != NULL)
{
textDocument->Release();
textDocument = NULL;
}
if (ole != NULL)
{
ole->Release();
ole = NULL;
}
this->hwnd = hwnd;
if (hwnd == NULL)
return;
SendMessage(EM_GETOLEINTERFACE, 0, (LPARAM) &ole);
if (ole == NULL)
return;
if (ole->QueryInterface(IID_ITextDocument, (void**) &textDocument) != S_OK)
textDocument = NULL;
}
LRESULT RichEdit::SendMessage(UINT Msg, WPARAM wParam, LPARAM lParam) const
{
return ::SendMessage(hwnd, Msg, wParam, lParam);
}
bool RichEdit::IsReadOnly() const
{
return (GetWindowLongPtr(hwnd, GWL_STYLE) & ES_READONLY) == ES_READONLY;
}
void RichEdit::SuspendUndo()
{
if (textDocument != NULL)
{
textDocument->Undo(tomSuspend, NULL);
undoEnabled = FALSE;
}
}
void RichEdit::ResumeUndo()
{
if (textDocument != NULL)
{
textDocument->Undo(tomResume, NULL);
undoEnabled = TRUE;
}
}
void RichEdit::Stop()
{
stopped++;
if (stopped != 1)
return;
SuspendUndo();
// HideCaret(hwnd);
SendMessage(WM_SETREDRAW, FALSE, 0);
SendMessage(EM_GETSCROLLPOS, 0, (LPARAM) &old_scroll_pos);
SendMessage(EM_EXGETSEL, 0, (LPARAM) &old_sel);
GetCaretPos(&caretPos);
old_mask = SendMessage(EM_GETEVENTMASK, 0, 0);
SendMessage(EM_SETEVENTMASK, 0, old_mask & ~ENM_CHANGE);
inverse = (old_sel.cpMin >= LOWORD(SendMessage(EM_CHARFROMPOS, 0, (LPARAM) &caretPos)));
}
void RichEdit::Start()
{
stopped--;
if (stopped < 0)
{
stopped = 0;
return;
}
else if (stopped > 0)
return;
if (inverse)
{
LONG tmp = old_sel.cpMin;
old_sel.cpMin = old_sel.cpMax;
old_sel.cpMax = tmp;
}
SendMessage(EM_SETEVENTMASK, 0, old_mask);
SendMessage(EM_EXSETSEL, 0, (LPARAM) &old_sel);
SendMessage(EM_SETSCROLLPOS, 0, (LPARAM) &old_scroll_pos);
SendMessage(WM_SETREDRAW, TRUE, 0);
InvalidateRect(hwnd, NULL, FALSE);
// ShowCaret(hwnd);
ResumeUndo();
}
BOOL RichEdit::IsStopped()
{
return stopped > 0;
}
int RichEdit::GetCharFromPos(const POINT &pt)
{
return LOWORD(SendMessage(EM_CHARFROMPOS, 0, (LPARAM) &pt));
}
int RichEdit::GetLineCount() const
{
return SendMessage(EM_GETLINECOUNT, 0, 0);
}
void RichEdit::GetLine(int line, TCHAR *text, size_t text_len) const
{
*((WORD*)text) = text_len - 1;
unsigned size = (unsigned) SendMessage(EM_GETLINE, (WPARAM) line, (LPARAM) text);
// Sometimes it likes to return size = lineLen+1, adding an \n at the end, so we remove it here
// to make both implementations return same size
int lineLen = GetLineLength(line);
size = (unsigned) max(0, min((int)text_len - 1, min((int) size, lineLen)));
text[size] = _T('\0');
/*
int len = GetLineLength(line);
if (len < 1)
{
text[0] = 0;
return;
}
int first = GetFirstCharOfLine(line);
TCHAR *tmp = GetText(first, first + GetLineLength(line));
lstrcpyn(text, tmp, text_len);
mir_free(tmp);
*/
}
int RichEdit::GetLineLength(int line) const
{
return SendMessage(EM_LINELENGTH, GetFirstCharOfLine(line), 0);
}
int RichEdit::GetFirstCharOfLine(int line) const
{
return SendMessage(EM_LINEINDEX, (WPARAM) line, 0);
}
int RichEdit::GetLineFromChar(int charPos) const
{
return SendMessage(EM_LINEFROMCHAR, charPos, 0);
}
CHARRANGE RichEdit::GetSel() const
{
CHARRANGE sel;
SendMessage(EM_EXGETSEL, 0, (LPARAM) &sel);
return sel;
}
void RichEdit::SetSel(int start, int end)
{
CHARRANGE sel = { start, end };
SetSel(sel);
}
void RichEdit::SetSel(const CHARRANGE &sel)
{
SendMessage(EM_EXSETSEL, 0, (LPARAM) &sel);
}
int RichEdit::GetTextLength() const
{
return GetWindowTextLength(hwnd);
}
TCHAR *RichEdit::GetText(int start, int end) const
{
if (end <= start)
end = GetTextLength();
if (textDocument != NULL)
{
ITextRange *range;
if (textDocument->Range(start, end, &range) != S_OK)
return mir_tstrdup(_T(""));
BSTR text = NULL;
if (range->GetText(&text) != S_OK || text == NULL)
{
range->Release();
return mir_tstrdup(_T(""));
}
TCHAR *ret = mir_u2t(text);
SysFreeString(text);
range->Release();
return ret;
}
else
{
int len = GetTextLength();
TCHAR *tmp = (TCHAR *) mir_alloc(len * sizeof(TCHAR));
GetWindowText(hwnd, tmp, len);
tmp[len] = 0;
TCHAR *ret = (TCHAR *) mir_alloc((end - start + 1) * sizeof(TCHAR));
memmove(ret, &tmp[start], (end - start) * sizeof(TCHAR));
ret[end - start] = 0;
mir_free(tmp);
return ret;
}
}
void RichEdit::ReplaceSel(const TCHAR *new_text)
{
if (stopped)
{
CHARRANGE sel = GetSel();
ResumeUndo();
SendMessage(EM_REPLACESEL, undoEnabled, (LPARAM) new_text);
SuspendUndo();
FixSel(&old_sel, sel, lstrlen(new_text));
SendMessage(WM_SETREDRAW, FALSE, 0);
SendMessage(EM_SETEVENTMASK, 0, old_mask & ~ENM_CHANGE);
}
else
{
SendMessage(EM_REPLACESEL, undoEnabled, (LPARAM) new_text);
}
}
int RichEdit::Replace(int start, int end, const TCHAR *new_text)
{
CHARRANGE sel = GetSel();
CHARRANGE replace_sel = { start, end };
SetSel(replace_sel);
ReplaceSel(new_text);
int dif = FixSel(&sel, replace_sel, lstrlen(new_text));
SetSel(sel);
return dif;
}
int RichEdit::Insert(int pos, const TCHAR *text)
{
CHARRANGE sel = GetSel();
CHARRANGE replace_sel = { pos, pos };
SetSel(replace_sel);
ReplaceSel(text);
int dif = FixSel(&sel, replace_sel, lstrlen(text));
SetSel(sel);
return dif;
}
int RichEdit::Delete(int start, int end)
{
CHARRANGE sel = GetSel();
CHARRANGE replace_sel = { start, end };
SetSel(replace_sel);
ReplaceSel(_T(""));
int dif = FixSel(&sel, replace_sel, 0);
SetSel(sel);
return dif;
}
int RichEdit::FixSel(CHARRANGE *to_fix, CHARRANGE sel_changed, int new_len)
{
int dif = new_len - (sel_changed.cpMax - sel_changed.cpMin);
if (to_fix->cpMax <= sel_changed.cpMin)
return dif;
int newMax = sel_changed.cpMax + dif;
if (to_fix->cpMin >= sel_changed.cpMax)
to_fix->cpMin += dif;
else if (to_fix->cpMin >= newMax) // For dif < 0, pos beetween sel_changed.cpMax + dif and sel_changed.cpMax
to_fix->cpMin = newMax;
if (to_fix->cpMax >= sel_changed.cpMax)
to_fix->cpMax += dif;
else if (to_fix->cpMax >= newMax) // For dif < 0, pos beetween sel_changed.cpMax + dif and sel_changed.cpMax
to_fix->cpMax = newMax;
return dif;
}
|