-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextEditor.cpp
608 lines (508 loc) · 19.4 KB
/
TextEditor.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "TextEditor.h"
#include <commctrl.h>
#include <windowsx.h>
constexpr int FILE_MENU_NEW = 1;
constexpr int FILE_MENU_OPEN = 2;
constexpr int FILE_MENU_EXIT = 3;
constexpr int FILE_MENU_SAVE = 4;
constexpr int FILE_MENU_SAVE_AS = 5;
constexpr int FILE_MENU_SAVE_ALL = 6;
constexpr int EDIT_MENU_UNDO = 101;
constexpr int EDIT_MENU_REDO = 102;
TextEditor::TextEditor() {
registerWindowClass();
createMainWindow();
addMenus();
addControls();
m_nFontHeight = 16;
tabControl->onTabRemoved = [this](int index) {
if (index >= 0 && index < documents.size()) {
// Erase the document at the given index
documents.erase(documents.begin() + index);
// Ensure that the current tab index is within bounds
if (tabControl->getCurrentTabIndex() >= documents.size()) {
tabControl->setCurrentTab(documents.size() - 1); // Set to last tab if out of bounds
}
}
};
}
void TextEditor::show() {
ShowWindow(hMainWindow, SW_SHOW);
UpdateWindow(hMainWindow);
}
LRESULT CALLBACK TextEditor::WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
TextEditor* pThis = reinterpret_cast<TextEditor*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
if (pThis) {
return pThis->handleMessage(hWnd, msg, wp, lp);
}
else if (msg == WM_NCCREATE) {
CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lp);
pThis = reinterpret_cast<TextEditor*>(pCreate->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
return pThis->handleMessage(hWnd, msg, wp, lp);
}
return DefWindowProc(hWnd, msg, wp, lp);
}
void TextEditor::setInstance(HINSTANCE hInst) {
hInstance = hInst;
}
void TextEditor::registerWindowClass() {
WNDCLASSW wc = { 0 };
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpszClassName = L"TextEditorClass";
wc.lpfnWndProc = WindowProcedure;
RegisterClassW(&wc);
}
void TextEditor::createMainWindow() {
hMainWindow = CreateWindowW(
L"TextEditorClass", L"Nickolas Text Editor - new file",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0, 300, 700, 700,
NULL, NULL, hInstance, this
);
}
void TextEditor::addMenus() {
hMenu = CreateMenu();
HMENU hFileMenu = CreateMenu();
HMENU hEditMenu = CreateMenu();
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_NEW, L"New File");
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_OPEN, L"Open File");
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_SAVE, L"Save");
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_SAVE_AS, L"Save As");
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_SAVE_ALL, L"Save All");
AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
AppendMenu(hFileMenu, MF_STRING, FILE_MENU_EXIT, L"Exit");
AppendMenu(hEditMenu, MF_STRING, EDIT_MENU_UNDO, L"Undo\tCtrl+Z");
AppendMenu(hEditMenu, MF_STRING, EDIT_MENU_REDO, L"Redo\tCtrl+Y");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hEditMenu, L"Edit");
SetMenu(hMainWindow, hMenu);
}
void TextEditor::addControls() {
tabControl = new TabControl(hMainWindow);
HWND hEditFile = CreateWindowW(
L"EDIT", NULL,
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | WS_VSCROLL | WS_HSCROLL,
0, 30, 700, 670,
hMainWindow, NULL, GetModuleHandle(NULL), NULL
);
documents.push_back(std::make_unique<DocumentText>(hEditFile));
tabControl->addTab(L"Untitled", hEditFile);
SubclassEditControl(hEditFile); // Apply subclassing to the first edit control
}
void TextEditor::SubclassEditControl(HWND hEditControl) {
SetWindowSubclass(hEditControl, SubclassEditProc, 0, reinterpret_cast<DWORD_PTR>(this));
}
LRESULT TextEditor::handleMessage(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
try {
switch (msg) {
case WM_COMMAND:
return handleCommand(wp, lp);
case WM_NOTIFY: {
NMHDR* nmhdr = (NMHDR*)lp;
if (nmhdr->hwndFrom == tabControl->getTabControlHandle() && nmhdr->code == TCN_SELCHANGE) {
int selectedTabIndex = tabControl->getCurrentTabIndex();
tabControl->setCurrentTab(selectedTabIndex);
}
break;
}
case WM_SIZE:
if (tabControl) {
RECT rcClient;
GetClientRect(hWnd, &rcClient);
tabControl->Resize(rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
return OnPaint(hWnd);
case WM_KEYDOWN:
if (GetKeyState(VK_CONTROL) & 0x8000) {
switch (wp) {
case 'Z':
undo();
return 0;
case 'Y':
redo();
return 0;
}
}
break;
default:
return DefWindowProc(hWnd, msg, wp, lp);
}
return 0;
}
catch (const std::exception& e) {
handleException(e);
return 0;
}
catch (...) {
handleUnknownException();
return 0;
}
}
void TextEditor::handleException(const std::exception& e) {
std::wstring errorMessage = L"An error occurred: ";
errorMessage += std::wstring(e.what(), e.what() + strlen(e.what()));
MessageBoxW(hMainWindow, errorMessage.c_str(), L"Error", MB_OK | MB_ICONERROR);
}
void TextEditor::handleUnknownException() {
MessageBoxW(hMainWindow, L"An unknown error occurred.", L"Error", MB_OK | MB_ICONERROR);
}
LRESULT TextEditor::handleCommand(WPARAM wp, LPARAM lp) {
switch (wp) {
case FILE_MENU_NEW:
createNewTab();
break;
case FILE_MENU_OPEN:
openFile();
break;
case FILE_MENU_SAVE:
saveFile();
break;
case FILE_MENU_SAVE_AS:
saveFileAs();
break;
case FILE_MENU_SAVE_ALL:
saveAllFiles();
break;
case FILE_MENU_EXIT:
if (MessageBoxW(hMainWindow, L"Are you sure?", L"Exit", MB_YESNO | MB_ICONWARNING) == IDYES) {
DestroyWindow(hMainWindow);
}
break;
case EDIT_MENU_UNDO:
undo();
return 0;
case EDIT_MENU_REDO:
redo();
return 0;
}
return 0;
}
void TextEditor::createNewTab() {
RECT rcClient;
GetClientRect(hMainWindow, &rcClient);
HWND newEditFile = CreateWindowW(
L"EDIT", NULL,
WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | WS_VSCROLL | WS_HSCROLL,
0, 0, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hMainWindow, NULL, GetModuleHandle(NULL), NULL
);
documents.push_back(std::make_unique<DocumentText>(newEditFile));
tabControl->addTab(L"Untitled", newEditFile);
SubclassEditControl(newEditFile);
tabControl->setCurrentTab(tabControl->getTabCount() - 1);
RECT rcMain;
GetClientRect(hMainWindow, &rcMain);
tabControl->Resize(rcMain.right - rcMain.left, rcMain.bottom - rcMain.top);
}
void TextEditor::openFile() {
OPENFILENAMEW ofn = { 0 };
wchar_t filePath[MAX_PATH] = L"";
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.hwndOwner = hMainWindow;
ofn.lpstrFile = filePath;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
if (GetOpenFileNameW(&ofn)) {
wchar_t* fileName = wcsrchr(filePath, L'\\');
if (fileName != NULL) {
fileName++;
}
else {
fileName = filePath;
}
HWND newEditFile = CreateWindowW(
L"EDIT", NULL,
WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | WS_VSCROLL | WS_HSCROLL,
0, 30, 700, 670,
hMainWindow, NULL, GetModuleHandle(NULL), NULL
);
// Initialize the document
auto newDocument = std::make_unique<DocumentText>(newEditFile);
if (newDocument->initFile(filePath)) {
documents.push_back(std::move(newDocument));
tabControl->addTab(fileName, newEditFile, filePath);
SubclassEditControl(newEditFile);
tabControl->setCurrentTab(tabControl->getTabCount() - 1);
displayFile(documents.back().get(), newEditFile);
updateWindowTitle();
}
else {
// Handle failure to initialize document
DestroyWindow(newEditFile); // Cleanup the edit control if file load failed
MessageBoxW(hMainWindow, L"Failed to open file", L"Error", MB_OK | MB_ICONERROR);
}
}
}
void TextEditor::saveFile() {
std::wstring currentFilePath = tabControl->getCurrentFilePath();
if (currentFilePath.empty()) {
saveFileAs();
}
else {
int currentTabIndex = tabControl->getCurrentTabIndex();
if (currentTabIndex >= 0 && currentTabIndex < documents.size()) {
writeFile(currentFilePath, documents[currentTabIndex].get());
}
}
}
void TextEditor::saveFileAs() {
OPENFILENAMEW ofn = { 0 };
wchar_t filePath[MAX_PATH] = L"";
ofn.lStructSize = sizeof(OPENFILENAMEW);
ofn.hwndOwner = hMainWindow;
ofn.lpstrFile = filePath;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = L"Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrDefExt = L"txt";
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
if (GetSaveFileNameW(&ofn)) {
wchar_t* fileName = wcsrchr(filePath, L'\\');
if (fileName != NULL) {
fileName++;
}
else {
fileName = filePath;
}
int currentTabIndex = tabControl->getCurrentTabIndex();
if (currentTabIndex >= 0 && currentTabIndex < documents.size()) {
writeFile(filePath, documents[currentTabIndex].get());
tabControl->setCurrentFilePath(filePath);
updateWindowTitle();
tabControl->changeTabName(fileName);
InvalidateRect(hMainWindow, NULL, TRUE);
}
}
}
void TextEditor::saveAllFiles() {
for (int i = 0; i < tabControl->getTabCount(); ++i) {
tabControl->setCurrentTab(i);
saveFile();
}
}
void TextEditor::displayFile(DocumentText* document, HWND editControl) {
std::string content;
char buf[LONGEST_LINE];
for (size_t i = 0; i < document->lineStarts.size(); ++i) {
ULONG len = document->getline(i, buf, LONGEST_LINE);
content.append(buf, len);
content.append("\r\n");
}
// Convert UTF-8 to wide string
int wideSize = MultiByteToWideChar(CP_UTF8, 0, content.c_str(), -1, nullptr, 0);
std::vector<wchar_t> wideBuffer(wideSize);
MultiByteToWideChar(CP_UTF8, 0, content.c_str(), -1, wideBuffer.data(), wideSize);
SetWindowTextW(editControl, wideBuffer.data());
}
void TextEditor::writeFile(const std::wstring& path, DocumentText* document) {
HANDLE hFile = CreateFileW(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) {
MessageBoxW(hMainWindow, L"Failed to save file", L"Error", MB_OK | MB_ICONERROR);
return;
}
char buf[LONGEST_LINE];
for (size_t i = 0; i < document->lineStarts.size(); ++i) {
ULONG len = document->getline(i, buf, LONGEST_LINE);
DWORD bytesWritten;
WriteFile(hFile, buf, len, &bytesWritten, NULL);
if (i < document->lineStarts.size() - 1) { // Don't add newline after the last line
WriteFile(hFile, "\r\n", 2, &bytesWritten, NULL);
}
}
CloseHandle(hFile);
}
void TextEditor::updateWindowTitle() {
std::wstring currentFilePath = tabControl->getCurrentFilePath();
std::wstring title = L"Nickolas Text Editor - " + (currentFilePath.empty() ? L"Untitled" : currentFilePath);
SetWindowTextW(hMainWindow, title.c_str());
}
LRESULT TextEditor::OnPaint(HWND hWnd) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// Paint the document content
int currentTab = tabControl->getCurrentTabIndex();
if (currentTab >= 0 && currentTab < documents.size()) {
DocumentText* currentDoc = documents[currentTab].get();
RECT rcClient;
GetClientRect(tabControl->getCurrentEditControl(), &rcClient);
int first = ps.rcPaint.top / m_nFontHeight;
int last = ps.rcPaint.bottom / m_nFontHeight;
for (ULONG i = first; i <= last; i++) {
PaintLine(hdc, i, currentDoc, rcClient);
}
}
EndPaint(hWnd, &ps);
return 0;
}
LONG TextEditor::PaintLine(HDC hdc, ULONG nLineNo, DocumentText* document, const RECT& clientRect) {
char buf[LONGEST_LINE];
ULONG len;
RECT rect = clientRect;
rect.top = nLineNo * m_nFontHeight;
rect.bottom = rect.top + m_nFontHeight;
len = document->getline(nLineNo, buf, LONGEST_LINE);
TCHAR wbuf[LONGEST_LINE];
MultiByteToWideChar(CP_ACP, 0, buf, -1, wbuf, LONGEST_LINE);
ExtTextOutW(hdc, rect.left, rect.top, ETO_OPAQUE, &rect, wbuf, len, NULL);
return 0;
}
LRESULT CALLBACK TextEditor::SubclassEditProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
TextEditor* pThis = reinterpret_cast<TextEditor*>(dwRefData);
switch (uMsg) {
case WM_CHAR: {
if (wParam >= 32 || wParam == VK_TAB || wParam == VK_RETURN) {
// Get current selection
DWORD start, end;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
// Create and execute insert command
char ch = static_cast<char>(wParam);
std::string insertText(1, ch);
pThis->commandHistory.executeCommand(std::make_unique<InsertCommand>(*(pThis->getCurrentDocument()), insertText, start));
// Let the default proc handle the actual insertion
LRESULT result = DefSubclassProc(hWnd, uMsg, wParam, lParam);
pThis->SynchronizeDocumentWithEdit(hWnd);
return result;
}
break;
}
case WM_KEYDOWN: {
if (GetKeyState(VK_CONTROL) & 0x8000) {
switch (wParam) {
case 'Z':
pThis->undo();
return 0; // Prevent default handling
case 'Y':
pThis->redo();
return 0; // Prevent default handling
}
}
if (wParam == VK_BACK || wParam == VK_DELETE) {
// Get current selection
DWORD start, end;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
if (start == end) {
if (wParam == VK_BACK && start > 0) {
--start;
}
else if (wParam == VK_DELETE && end < pThis->getCurrentDocument()->getLength()) {
++end;
}
}
if (start != end) {
// Create and execute delete command
pThis->commandHistory.executeCommand(std::make_unique<DeleteCommand>(*(pThis->getCurrentDocument()), start, end - start));
}
// Let the default proc handle the actual deletion
LRESULT result = DefSubclassProc(hWnd, uMsg, wParam, lParam);
pThis->SynchronizeDocumentWithEdit(hWnd);
return result;
}
break;
}
case WM_PASTE: {
// Handle paste operation
if (OpenClipboard(hWnd)) {
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData != NULL) {
char* pszText = static_cast<char*>(GlobalLock(hData));
if (pszText != NULL) {
// Get current selection
DWORD start, end;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
// Create and execute insert command
pThis->commandHistory.executeCommand(std::make_unique<InsertCommand>(*(pThis->getCurrentDocument()), pszText, start));
GlobalUnlock(hData);
}
}
CloseClipboard();
}
// Let the default proc handle the actual paste
LRESULT result = DefSubclassProc(hWnd, uMsg, wParam, lParam);
pThis->SynchronizeDocumentWithEdit(hWnd);
return result;
}
case WM_CUT: {
// Handle cut operation
// Get current selection
DWORD start, end;
SendMessage(hWnd, EM_GETSEL, (WPARAM)&start, (LPARAM)&end);
if (start != end) {
// Create and execute delete command
pThis->commandHistory.executeCommand(std::make_unique<DeleteCommand>(*(pThis->getCurrentDocument()), start, end - start));
}
// Let the default proc handle the actual cut
LRESULT result = DefSubclassProc(hWnd, uMsg, wParam, lParam);
pThis->SynchronizeDocumentWithEdit(hWnd);
return result;
}
}
return DefSubclassProc(hWnd, uMsg, wParam, lParam);
}
DocumentText* TextEditor::getCurrentDocument() {
int currentTabIndex = tabControl->getCurrentTabIndex();
if (currentTabIndex >= 0 && currentTabIndex < documents.size()) {
return documents[currentTabIndex].get();
}
return nullptr;
}
void TextEditor::SynchronizeDocumentWithEdit(HWND hEditControl) {
int currentTabIndex = tabControl->getCurrentTabIndex();
if (currentTabIndex < 0 || currentTabIndex >= documents.size()) {
return;
}
DocumentText* currentDoc = documents[currentTabIndex].get();
// Get the entire text from the edit control
int textLength = GetWindowTextLength(hEditControl);
std::vector<wchar_t> buffer(textLength + 1);
GetWindowText(hEditControl, buffer.data(), textLength + 1);
// Convert wide string to UTF-8
int utf8Length = WideCharToMultiByte(CP_UTF8, 0, buffer.data(), -1, nullptr, 0, nullptr, nullptr);
std::vector<char> utf8Buffer(utf8Length);
WideCharToMultiByte(CP_UTF8, 0, buffer.data(), -1, utf8Buffer.data(), utf8Length, nullptr, nullptr);
// Clear the current document content
currentDoc->deleteText(0, currentDoc->getLength());
// Insert the new content
currentDoc->insertText(utf8Buffer.data(), utf8Length - 1, 0); // -1 to exclude null terminator
}
void TextEditor::undo() {
size_t currentPosition = getCurrentDocument()->getCaretPosition();
commandHistory.undo();
updateEditControl();
size_t newPosition = commandHistory.getLastCursorPosition();
if (newPosition == 0) {
newPosition = currentPosition;
}
setCursorPosition(newPosition);
}
void TextEditor::redo() {
size_t currentPosition = getCurrentDocument()->getCaretPosition();
commandHistory.redo();
updateEditControl();
size_t newPosition = commandHistory.getLastCursorPosition();
if (newPosition == 0) {
newPosition = currentPosition;
}
setCursorPosition(newPosition);
}
void TextEditor::setCursorPosition(size_t position) {
HWND currentEditControl = tabControl->getCurrentEditControl();
SendMessage(currentEditControl, EM_SETSEL, position, position);
SendMessage(currentEditControl, EM_SCROLLCARET, 0, 0);
}
void TextEditor::updateEditControl() {
int currentTabIndex = tabControl->getCurrentTabIndex();
if (currentTabIndex >= 0 && currentTabIndex < documents.size()) {
DocumentText* currentDoc = documents[currentTabIndex].get();
HWND editControl = tabControl->getCurrentEditControl();
displayFile(currentDoc, editControl);
}
}