-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLineNumEdit.cpp
More file actions
430 lines (385 loc) · 13.2 KB
/
LineNumEdit.cpp
File metadata and controls
430 lines (385 loc) · 13.2 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// LineNumEdit.cpp --- textbox with line numbers
#include "LineNumEdit.hpp"
static INT getLogicalLineIndexFromCharIndex(LPCTSTR psz, INT ich)
{
INT iLine = 0;
while (*psz && ich > 0)
{
if (*psz == TEXT('\n'))
++iLine;
++psz;
--ich;
}
return iLine;
}
/////////////////////////////////////////////////////////////////////////////////////////
// LineNumStatic
LineNumStatic::LineNumStatic(HWND hwnd)
: LineNumBase(hwnd)
, m_rgbText(::GetSysColor(COLOR_WINDOWTEXT))
, m_rgbBack(::GetSysColor(COLOR_3DFACE))
, m_linedelta(1)
, m_hbm(NULL)
, m_siz { 0, 0 }
{
::SHStrDup(TEXT("%d"), &m_format);
}
LineNumStatic::~LineNumStatic()
{
::DeleteObject(m_hbm);
::CoTaskMemFree(m_format);
}
LRESULT CALLBACK
LineNumStatic::WindowProcDx(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
RECT rc;
POINT pt;
PAINTSTRUCT ps;
HWND hwndEdit;
switch (uMsg)
{
case WM_PAINT:
if (HDC hDC = ::BeginPaint(hwnd, &ps))
{
OnDrawClient(hwnd, hDC);
::EndPaint(hwnd, &ps);
}
return 0;
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_MOUSEMOVE:
hwndEdit = GetEdit();
GetClientRect(hwnd, &rc);
pt.x = rc.right + 1;
pt.y = GET_Y_LPARAM(lParam);
::MapWindowPoints(hwnd, hwndEdit, &pt, 1);
return SendMessage(hwndEdit, uMsg, wParam, MAKELPARAM(pt.x, pt.y));
case WM_ERASEBKGND:
return TRUE;
case WM_DESTROY:
DeleteProps(hwnd);
break;
}
return DefWndProc(hwnd, uMsg, wParam, lParam);
}
void LineNumStatic::OnDrawClient(HWND hwnd, HDC hDC)
{
// get the client size
RECT rcClient;
::GetClientRect(hwnd, &rcClient);
SIZE siz = { rcClient.right - rcClient.left, rcClient.bottom - rcClient.top };
if (!siz.cx || !siz.cy)
return;
// prepare for double buffering
HDC hdcMem = ::CreateCompatibleDC(hDC);
HBITMAP hbm;
if (m_hbm && siz.cx <= m_siz.cx && siz.cy <= m_siz.cy)
{
hbm = m_hbm;
}
else
{
hbm = ::CreateCompatibleBitmap(hDC, siz.cx, siz.cy);
m_siz = siz;
}
HGDIOBJ hbmOld = ::SelectObject(hdcMem, hbm);
// fill background
UINT uMsg;
HWND hwndEdit = GetEdit();
if (!::IsWindowEnabled(hwndEdit) || (GetWindowLong(hwndEdit, GWL_STYLE) & ES_READONLY))
uMsg = WM_CTLCOLORSTATIC;
else
uMsg = WM_CTLCOLOREDIT;
HBRUSH hbr = reinterpret_cast<HBRUSH>(
::SendMessage(GetParent(hwndEdit), uMsg,
reinterpret_cast<WPARAM>(hDC), reinterpret_cast<LPARAM>(hwndEdit)));
::FillRect(hdcMem, &rcClient, hbr);
// get top margin and line height
RECT rcEdit;
Edit_GetRect(hwndEdit, &rcEdit);
INT yLine = rcEdit.top, cyLine = GetLineHeight();
// get margins
DWORD dwMargins = DWORD(::SendMessage(hwndEdit, EM_GETMARGINS, 0, 0));
INT leftmargin = LOWORD(dwMargins), rightmargin = HIWORD(dwMargins);
// shrink rectangle
rcClient.right -= leftmargin;
siz.cx -= leftmargin;
// fill background
hbr = ::CreateSolidBrush(m_rgbBack);
::FillRect(hdcMem, &rcClient, hbr);
::DeleteObject(hbr);
// right line
HPEN hPen = ::CreatePen(PS_SOLID, 0, m_rgbText);
HGDIOBJ hPenOld = ::SelectObject(hdcMem, hPen);
::MoveToEx(hdcMem, rcClient.right - 1, rcClient.top, NULL);
::LineTo(hdcMem, rcClient.right - 1, rcClient.bottom);
::DeleteObject(::SelectObject(hdcMem, hPenOld));
// draw lines
WCHAR szText[32];
HFONT hFont = GetWindowFont(hwndEdit);
HGDIOBJ hFontOld = ::SelectObject(hdcMem, hFont);
::SetBkMode(hdcMem, TRANSPARENT);
{
// get the edit text
INT cch = Edit_GetTextLength(hwndEdit);
BSTR bstrText = ::SysAllocStringLen(NULL, cch);
if (bstrText)
{
Edit_GetText(hwndEdit, bstrText, cch + 1);
// initialize variables for lines loop
INT iPhysicalLine = Edit_GetFirstVisibleLine(hwndEdit);
INT ich = Edit_LineIndex(hwndEdit, iPhysicalLine);
if (ich == -1) // beyond the limit
ich = cch;
INT ichOld = ich;
INT cLogicalLines = ::getLogicalLineIndexFromCharIndex(bstrText, MAXLONG);
INT iLogicalLine = ::getLogicalLineIndexFromCharIndex(bstrText, ich);
INT iOldLogicalLine;
if (ich == 0)
iOldLogicalLine = -1;
else if (ich > 0 && bstrText[ich - 1] == L'\n')
iOldLogicalLine = iLogicalLine - 1;
else
iOldLogicalLine = iLogicalLine;
do // for each physical lines
{
RECT rc = { 0, yLine, siz.cx - 1, yLine + cyLine }; // one line
INT nLabel = iLogicalLine + m_linedelta; // label
// fill the background if necessary, and set text color
HANDLE hProp = ::GetProp(hwnd, GetPropName(nLabel));
if (hProp &&
(ich < cch || iOldLogicalLine < iLogicalLine || iLogicalLine < cLogicalLines))
{
COLORREF rgbBack = (COLORREF(reinterpret_cast<ULONG_PTR>(hProp)) & 0xFFFFFF);
HBRUSH hbr = ::CreateSolidBrush(rgbBack);
::FillRect(hdcMem, &rc, hbr);
::DeleteObject(hbr);
INT value = (GetRValue(rgbBack) + GetGValue(rgbBack) + GetBValue(rgbBack)) / 3;
if (value < 255 / 3)
::SetTextColor(hdcMem, RGB(255, 255, 255));
else
::SetTextColor(hdcMem, RGB(0, 0, 0));
}
else
{
::SetTextColor(hdcMem, m_rgbText);
}
// draw line text
if (ich <= cch && iOldLogicalLine != iLogicalLine)
{
StringCchPrintfW(szText, _countof(szText), m_format, nLabel);
rc.right -= rightmargin;
UINT uFormat = DT_RIGHT | DT_VCENTER | DT_SINGLELINE | DT_NOPREFIX;
::DrawTextW(hdcMem, szText, ::lstrlenW(szText), &rc, uFormat);
}
// go to next line
yLine += cyLine;
++iPhysicalLine;
// go to next newline
ichOld = ich;
while (bstrText[ich])
{
if (Edit_LineFromChar(hwndEdit, ich) == iPhysicalLine)
break;
if (bstrText[ich] == L'\n')
{
++ich;
break;
}
++ich;
}
// update logical line if necessary
iOldLogicalLine = iLogicalLine;
iLogicalLine = ::getLogicalLineIndexFromCharIndex(bstrText, ich);
if (iLogicalLine == iOldLogicalLine && ich == ichOld)
break;
} while (yLine < rcClient.bottom); // beyond the client area?
// clean up
::SysFreeString(bstrText);
}
}
::SelectObject(hdcMem, hFontOld);
// send the image to the window
::BitBlt(hDC, 0, 0, rcClient.right, rcClient.bottom, hdcMem, 0, 0, SRCCOPY);
::SelectObject(hdcMem, hbmOld);
// clean up
::DeleteDC(hdcMem);
// do cache
if (m_hbm != hbm)
{
DeleteObject(m_hbm);
m_hbm = hbm;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
// LineNumEdit
void LineNumEdit::Prepare()
{
// sanity check
assert(::IsWindow(m_hwnd));
assert(!!(::GetWindowLong(m_hwnd, GWL_STYLE) & WS_CHILD));
assert(!!(::GetWindowLong(m_hwnd, GWL_STYLE) & ES_MULTILINE));
RECT rcClient;
::GetClientRect(m_hwnd, &rcClient);
INT cxColumn = GetColumnWidth(), cyColumn = rcClient.bottom - rcClient.top;
// get margins
DWORD dwMargins = DWORD(::SendMessage(m_hwnd, EM_GETMARGINS, 0, 0));
INT leftmargin = LOWORD(dwMargins), rightmargin = HIWORD(dwMargins);
// adjust rectangle
RECT rcEdit = rcClient;
rcEdit.left += cxColumn;
rcEdit.right -= rightmargin;
Edit_SetRectNoPaint(m_hwnd, &rcEdit);
if (m_hwndStatic)
{
::MoveWindow(m_hwndStatic, 0, 0, cxColumn, cyColumn, TRUE);
}
else
{
DWORD style = WS_CHILD | WS_VISIBLE | SS_NOTIFY;
HWND hwndStatic = ::CreateWindow(TEXT("STATIC"), NULL, style,
0, 0, cxColumn, cyColumn, m_hwnd,
NULL, ::GetModuleHandle(NULL), NULL);
m_hwndStatic.Attach(hwndStatic);
}
}
LRESULT CALLBACK
LineNumEdit::WindowProcDx(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT ret;
switch (uMsg)
{
case WM_ENABLE: case WM_SYSCOLORCHANGE: case EM_SETREADONLY:
ret = DefWndProc(hwnd, uMsg, wParam, lParam);
RefreshColors();
return ret;
case LNEM_SETLINENUMFORMAT:
SetLineNumberFormat(reinterpret_cast<LPCTSTR>(lParam));
return 0;
case LNEM_SETNUMOFDIGITS:
SetNumberOfDigits(INT(wParam));
return 0;
case LNEM_SETLINEMARK:
{
LPCTSTR pszName = m_hwndStatic.GetPropName(INT(wParam));
::RemoveProp(m_hwndStatic, pszName);
COLORREF rgb = COLORREF(lParam);
if (rgb != CLR_INVALID)
{
lParam |= 0xFF000000;
::SetProp(m_hwndStatic, pszName, reinterpret_cast<HANDLE>(lParam));
}
}
m_hwndStatic.Redraw();
return 0;
case LNEM_CLEARLINEMARKS:
m_hwndStatic.DeleteProps(m_hwndStatic);
return 0;
case LNEM_SETLINEDELTA:
m_hwndStatic.m_linedelta = INT(wParam);
m_hwndStatic.Redraw();
return 0;
case LNEM_SETCOLUMNWIDTH:
m_cxColumn = INT(wParam);
Prepare();
return 0;
case LNEM_GETCOLUMNWIDTH:
return m_cxColumn;
case LNEM_GETLINEMARK:
{
LPCTSTR pszName = m_hwndStatic.GetPropName(INT(wParam));
HANDLE hData = ::GetProp(m_hwndStatic, pszName);
if (hData == NULL)
return CLR_INVALID;
COLORREF rgb = COLORREF(reinterpret_cast<ULONG_PTR>(hData));
rgb &= 0x00FFFFFF;
return rgb;
}
case WM_SETTEXT: case WM_CHAR: case WM_KEYDOWN: case WM_KEYUP: case WM_VSCROLL:
case WM_CUT: case WM_PASTE: case WM_UNDO: case WM_MOUSEWHEEL:
case EM_UNDO: case EM_SCROLL: case EM_SCROLLCARET: case EM_LINESCROLL:
case EM_REPLACESEL: case EM_SETHANDLE:
ret = DefWndProc(hwnd, uMsg, wParam, lParam);
m_hwndStatic.Redraw();
return ret;
case WM_SIZE: case WM_SETFONT:
ret = DefWndProc(hwnd, uMsg, wParam, lParam);
Prepare();
return ret;
case EM_SETMARGINS:
ret = DefWndProc(hwnd, uMsg, wParam, lParam);
m_cxColumn = 0; // clear cache
Prepare();
return ret;
}
return DefWndProc(hwnd, uMsg, wParam, lParam);
}
LRESULT CALLBACK
LineNumEdit::SuperclassWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LineNumEdit* pCtrl = NULL;
if (uMsg == WM_NCCREATE)
{
pCtrl = new LineNumEdit();
pCtrl->m_hwnd = hwnd;
SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR)pCtrl);
pCtrl->m_fnOldWndProc = SuperclassWindow();
}
else if (uMsg == WM_NCDESTROY)
{
pCtrl = reinterpret_cast<LineNumEdit*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
}
LRESULT ret = LineNumBase::WindowProc(hwnd, uMsg, wParam, lParam);
if (uMsg == WM_NCDESTROY)
delete pCtrl;
return ret;
}
INT LineNumEdit::GetColumnWidth()
{
if (m_cxColumn)
return m_cxColumn; // cached
// get text extent
SIZE siz;
HDC hDC = ::GetDC(m_hwnd);
HGDIOBJ hFontOld = ::SelectObject(hDC, GetWindowFont(m_hwnd));
::GetTextExtentPoint32(hDC, TEXT("0"), 1, &siz);
::SelectObject(hDC, hFontOld);
::ReleaseDC(m_hwnd, hDC);
// get margins
DWORD dwMargins = DWORD(::SendMessage(m_hwnd, EM_GETMARGINS, 0, 0));
INT leftmargin = LOWORD(dwMargins), rightmargin = HIWORD(dwMargins);
// save and return
m_cxColumn = leftmargin + (m_num_digits * siz.cx) + rightmargin + leftmargin;
return m_cxColumn;
}
WNDPROC LineNumEdit::SuperclassWindow() // "superclassing"
{
static WNDPROC s_fnOldWndProc = NULL;
if (s_fnOldWndProc)
return s_fnOldWndProc;
WNDCLASSEX wcx = { sizeof(wcx) };
if (!::GetClassInfoEx(NULL, TEXT("EDIT"), &wcx))
return NULL;
s_fnOldWndProc = wcx.lpfnWndProc;
wcx.lpszClassName = SuperWndClassName();
wcx.lpfnWndProc = SuperclassWndProc;
if (::RegisterClassEx(&wcx))
return s_fnOldWndProc;
return NULL;
}
/////////////////////////////////////////////////////////////////////////////////////////
// DllMain --- entry point of the DLL file
#ifdef LINENUMEDIT_DLL
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
return (LineNumEdit::SuperclassWindow() != NULL);
case DLL_PROCESS_DETACH:
::UnregisterClass(LineNumEdit::SuperWndClassName(), NULL);
break;
}
return TRUE;
}
#endif