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
|
/*
Copyright (C) 2006-2010 Ricardo Pescuma Domenecci
This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "commons.h"
static LRESULT CALLBACK EditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
static INT_PTR CALLBACK AddReplacementDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam);
struct Data
{
Dictionary *dict;
tstring find;
tstring replace;
BOOL useVariables;
BOOL modal;
BOOL findReadOnly;
AutoReplaceDialogCallback callback;
void *param;
WNDPROC old_edit_proc;
};
BOOL ShowAutoReplaceDialog(HWND parent, BOOL modal,
Dictionary *dict, const TCHAR *find, const TCHAR *replace, BOOL useVariables,
BOOL findReadOnly, AutoReplaceDialogCallback callback, void *param)
{
Data *data = new Data();
data->dict = dict;
data->useVariables = useVariables;
data->modal = modal;
data->findReadOnly = findReadOnly;
data->callback = callback;
data->param = param;
if (find != NULL)
data->find = find;
if (replace != NULL)
data->replace = replace;
if (modal)
{
return DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_ADD_REPLACEMENT), parent,
AddReplacementDlgProc, (LPARAM) data);
}
else
{
HWND hwnd = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_ADD_REPLACEMENT), parent,
AddReplacementDlgProc, (LPARAM) data);
SetForegroundWindow(hwnd);
SetFocus(hwnd);
SetFocus(GetDlgItem(hwnd, IDC_NEW));
ShowWindow(hwnd, SW_SHOW);
return TRUE;
}
}
static LRESULT CALLBACK OnlyCharsEditProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Data *data = (Data *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
switch(msg)
{
case WM_CHAR:
{
if (wParam == VK_BACK)
break;
if (lParam & (1 << 28)) // ALT key
break;
if (GetKeyState(VK_CONTROL) & 0x8000) // CTRL key
break;
TCHAR c = (TCHAR) wParam;
if (!data->dict->autoReplace->isWordChar(c))
return 1;
TCHAR tmp[2] = { c, 0 };
CharLower(tmp);
wParam = tmp[0];
break;
}
}
LRESULT ret = CallWindowProc(data->old_edit_proc, hwnd, msg, wParam, lParam);
switch(msg)
{
case EM_PASTESPECIAL:
case WM_PASTE:
{
TCHAR text[256];
GetWindowText(hwnd, text, SIZEOF(text));
scoped_free<TCHAR> dest = data->dict->autoReplace->filterText(text);
SetWindowText(hwnd, dest);
break;
}
}
return ret;
}
static BOOL CenterParent(HWND hwnd)
{
HWND hwndParent = GetParent(hwnd);
if (hwndParent == NULL)
return FALSE;
RECT rect, rectP;
GetWindowRect(hwnd, &rect);
GetWindowRect(hwndParent, &rectP);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int x = ((rectP.right-rectP.left) - width) / 2 + rectP.left;
int y = ((rectP.bottom-rectP.top) - height) / 2 + rectP.top;
int screenwidth = GetSystemMetrics(SM_CXSCREEN);
int screenheight = GetSystemMetrics(SM_CYSCREEN);
if (x + width > screenwidth) x = screenwidth - width;
if (y + height > screenheight) y = screenheight - height;
if (x < 0) x = 0;
if (y < 0) y = 0;
MoveWindow(hwnd, x, y, width, height, FALSE);
return TRUE;
}
static void Close(HWND hwndDlg, int ret)
{
Data *data = (Data *) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
if (!ret)
{
data->callback(TRUE, data->dict,
data->find.c_str(), data->replace.c_str(), data->useVariables,
data->find.c_str(), data->param);
}
SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_OLD), GWLP_WNDPROC, (LONG_PTR) data->old_edit_proc);
data->old_edit_proc = NULL;
if (data->modal)
EndDialog(hwndDlg, ret);
else
DestroyWindow(hwndDlg);
delete data;
}
static INT_PTR CALLBACK AddReplacementDlgProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_INITDIALOG:
TranslateDialogDefault(hwndDlg);
{
Data *data = (Data *) lParam;
SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR) data);
SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_OLD), GWLP_USERDATA, (LONG_PTR) data);
data->old_edit_proc = (WNDPROC) SetWindowLongPtr(GetDlgItem(hwndDlg, IDC_OLD), GWLP_WNDPROC,
(LONG_PTR) OnlyCharsEditProc);
HICON hIcon = Skin_GetIcon("spellchecker_enabled");
SendMessage(hwndDlg, WM_SETICON, ICON_BIG, (LPARAM) hIcon);
Skin_ReleaseIcon(hIcon);
SendDlgItemMessage(hwndDlg, IDC_OLD, EM_LIMITTEXT, 256, 0);
SendDlgItemMessage(hwndDlg, IDC_NEW, EM_LIMITTEXT, 256, 0);
if (!data->find.empty()) {
scoped_free<TCHAR> tmp = data->dict->autoReplace->filterText(data->find.c_str());
SetDlgItemText(hwndDlg, IDC_OLD, tmp);
}
if (!data->replace.empty())
SetDlgItemText(hwndDlg, IDC_NEW, data->replace.c_str());
CheckDlgButton(hwndDlg, IDC_VARIABLES, data->useVariables ? BST_CHECKED : BST_UNCHECKED);
if (data->findReadOnly) {
SendDlgItemMessage(hwndDlg, IDC_OLD, EM_SETREADONLY, TRUE, 0);
EnableWindow(GetDlgItem(hwndDlg, IDC_OLD_PS), FALSE);
}
if (!variables_enabled) {
ShowWindow(GetDlgItem(hwndDlg, IDC_VARIABLES), FALSE);
ShowWindow(GetDlgItem(hwndDlg, IDC_VAR_HELP), FALSE);
RECT rc_old;
GetWindowRect(GetDlgItem(hwndDlg, IDC_OLD), &rc_old);
RECT rc_new;
GetWindowRect(GetDlgItem(hwndDlg, IDC_NEW), &rc_new);
rc_new.right = rc_old.right;
SetWindowPos(GetDlgItem(hwndDlg, IDC_NEW), NULL, 0, 0,
rc_new.right - rc_new.left, rc_new.bottom - rc_new.top,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOREDRAW | SWP_NOZORDER);
}
else {
variables_skin_helpbutton(hwndDlg, IDC_VAR_HELP);
EnableWindow(GetDlgItem(hwndDlg, IDC_VAR_HELP), IsDlgButtonChecked(hwndDlg, IDC_VARIABLES));
}
CenterParent(hwndDlg);
}
return TRUE;
case WM_COMMAND:
switch(wParam) {
case IDOK:
{
Data *data = (Data *) GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
TCHAR find[256];
if (data->findReadOnly)
lstrcpyn(find, data->find.c_str(), SIZEOF(find));
else {
GetDlgItemText(hwndDlg, IDC_OLD, find, SIZEOF(find));
lstrtrim(find);
}
TCHAR replace[256];
GetDlgItemText(hwndDlg, IDC_NEW, replace, SIZEOF(replace));
lstrtrim(replace);
if (!data->findReadOnly && find[0] == 0) {
MessageBox(hwndDlg, TranslateT("The wrong word can't be empty!"), TranslateT("Wrong Correction"),
MB_OK | MB_ICONERROR);
}
else if (replace[0] == 0) {
MessageBox(hwndDlg, TranslateT("The correction can't be empty!"), TranslateT("Wrong Correction"),
MB_OK | MB_ICONERROR);
}
else if (_tcscmp(find, replace) == 0) {
MessageBox(hwndDlg, TranslateT("The correction can't be equal to the wrong word!"), TranslateT("Wrong Correction"),
MB_OK | MB_ICONERROR);
}
else {
data->callback(FALSE, data->dict,
find, replace, IsDlgButtonChecked(hwndDlg, IDC_VARIABLES),
data->find.c_str(), data->param);
Close(hwndDlg, 1);
}
}
break;
case IDCANCEL:
Close(hwndDlg, 0);
break;
case IDC_VARIABLES:
EnableWindow(GetDlgItem(hwndDlg, IDC_VAR_HELP), IsDlgButtonChecked(hwndDlg, IDC_VARIABLES));
break;
case IDC_VAR_HELP:
variables_showhelp(hwndDlg, IDC_NEW, VHF_FULLDLG, NULL, "The wrong word typed by the user");
break;
}
break;
case WM_CLOSE:
Close(hwndDlg, 0);
break;
}
return FALSE;
}
|