-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtfeditor.c
144 lines (104 loc) · 4.87 KB
/
rtfeditor.c
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
#include <richedit.h>
#include "editor.h"
#define UNICODE
#define _UNICODE
DWORD CALLBACK saveCallback(DWORD dwCookie, LPBYTE byteBuffer, LONG bufferOffset, LONG* resultingBufferOffset);
DWORD CALLBACK loadCallback(DWORD dwCookie, LPBYTE byteBuffer, LONG bufferOffset, LONG* resultingBufferOffset);
void initEditor() {
HINSTANCE richEditLibModuleHandle;
wchar_t* richEditLibName = L"Msftedit.dll"; // L"RICHED32.DLL"
richEditLibModuleHandle = LoadLibraryW(richEditLibName);
if (!richEditLibModuleHandle) {
// MessageBoxW(NULL, L"Error while starting application: could not load RICHED32.DLL", L"Note", MB_OK);
MessageBoxW(NULL, L"Error while starting application: could not load Msftedit.dll", L"Note", MB_OK);
}
}
HWND createEditorWindow(HINSTANCE appModuleHandle, HWND parentWinHandle, int visible) {
HWND editorWinHandle;
wchar_t buf[256];
// ES_NOHIDESEL is important because without it selection will not be shown when "Find" dialog is active
DWORD richEditStyle = WS_CHILD | WS_BORDER | WS_HSCROLL | WS_VSCROLL | ES_NOHIDESEL | ES_MULTILINE | ES_SAVESEL | ES_DISABLENOSCROLL;
richEditStyle = visible ? (richEditStyle | WS_VISIBLE) : richEditStyle;
wchar_t* richEditWindowClassName = MSFTEDIT_CLASS; // included from richedit.h. If using older richedit.dll, use RICHEDIT_CLASS
editorWinHandle = CreateWindowExW(0L, richEditWindowClassName, L"", richEditStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, parentWinHandle, NULL, appModuleHandle, NULL);
if (editorWinHandle == NULL) {
// wsprintfW(buf, L"Error %d", GetLastError());
// MessageBoxW(NULL, &buf, L"Note", MB_OK);
return NULL;
}
// SetFocus(editorWinHandle);
SendMessageW(editorWinHandle, EM_SETEVENTMASK, 0, ENM_SELCHANGE);
return editorWinHandle;
}
BOOL saveToFile(HWND activeEditorHandle, wchar_t* fileName) {
HANDLE outputFileHandle;
EDITSTREAM es;
outputFileHandle = CreateFileW(fileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (outputFileHandle == INVALID_HANDLE_VALUE) {
return FALSE;
}
es.dwCookie = (DWORD)outputFileHandle;
es.dwError = 0;
es.pfnCallback = saveCallback;
// wcsupr(&ofn.lpstrFile[ofn.nFileExtension]);
// if(!strncmp(&ofn.lpstrFile[ofn.nFileExtension],L"RTF",3))
// SendMessage(hwndEdit,EM_STREAMOUT,SF_RTF, (LPARAM)&es);
//else
SendMessageW(activeEditorHandle, EM_STREAMOUT, SF_TEXT, (LPARAM)&es);
CloseHandle(outputFileHandle);
return TRUE;
}
DWORD CALLBACK saveCallback(DWORD dwCookie, LPBYTE byteBuffer, LONG bytesToWrite, LONG* bytesActuallyWritten) {
BOOL result;
result = WriteFile((HFILE)dwCookie, byteBuffer, bytesToWrite, bytesActuallyWritten, NULL);
// TODO analyze result
return 0;
}
BOOL loadFromFile(HWND activeEditorHandle, wchar_t* fileName) {
HANDLE inputFileHandle;
EDITSTREAM es;
inputFileHandle = CreateFileW(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (inputFileHandle == INVALID_HANDLE_VALUE) {
return FALSE;
}
es.dwCookie = (DWORD)inputFileHandle;
es.dwError = 0;
es.pfnCallback = loadCallback;
// _strupr(&ofn.lpstrFile[ofn.nFileExtension]);
// if(!strncmp(&ofn.lpstrFile[ofn.nFileExtension],"RTF",3))
// SendMessage(hwndEdit,EM_STREAMIN,SF_RTF,(LPARAM)&es);
// else
SendMessageW(activeEditorHandle, EM_STREAMIN, SF_TEXT, (LPARAM)&es);
CloseHandle(inputFileHandle);
}
DWORD CALLBACK loadCallback(DWORD dwCookie, LPBYTE byteBuffer, LONG bytesToRead, LONG* bytesActuallyRead) {
BOOL result;
result = ReadFile((HANDLE)dwCookie, byteBuffer, bytesToRead, bytesActuallyRead, NULL);
// TODO analyze result
if (*bytesActuallyRead <= 0) {
*bytesActuallyRead = 0;
}
return 0;
}
BOOL findText(HWND editorWindowHandle, int* searchOffset, wchar_t* searchString) {
int textLength, foundOccurenceStartOffset;
wchar_t *textBuffer, *textPosition;
// Read in the edit document
textLength = GetWindowTextLengthW(editorWindowHandle);
// allocate memory for text from editor
textBuffer = HeapAlloc(GetProcessHeap(), 0, (textLength + 1) * sizeof(wchar_t));
GetWindowTextW(editorWindowHandle, textBuffer, textLength + 1);
textPosition = wcsstr(textBuffer + *searchOffset, searchString);
HeapFree(GetProcessHeap(), 0, textBuffer);
// Return an error code if the string cannot be found
if (textPosition == NULL) {
return FALSE;
}
// Find the position in the document and the new start offset
foundOccurenceStartOffset = textPosition - textBuffer;
*searchOffset = foundOccurenceStartOffset + wcslen(searchString);
// Select the found text
SendMessageW(editorWindowHandle, EM_SETSEL, foundOccurenceStartOffset, *searchOffset);
SendMessageW(editorWindowHandle, EM_SCROLLCARET, 0, 0);
return TRUE;
}