-
Notifications
You must be signed in to change notification settings - Fork 3
/
FindReplace.cpp
174 lines (145 loc) · 4.29 KB
/
FindReplace.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
#include "stdafx.h"
#include "FindReplace.h"
#include "StringX.h"
#include "resource.h"
extern HINSTANCE g_hInst;
// Note: Common control v6 edit control always uses WCHAR internally
// so its easier to also use the WCHAR version of Find and Replace
#define MAX_FINDSTRING 100
WCHAR g_szFind[MAX_FINDSTRING] = L"";
WCHAR g_szReplace[MAX_FINDSTRING] = L"";
HWND g_hFind = NULL;
FINDREPLACEW g_fr = {
sizeof(FINDREPLACEW),
NULL,
NULL,
FR_DOWN | FR_HIDEWHOLEWORD,
g_szFind, g_szReplace,
ARRAYSIZE(g_szFind), ARRAYSIZE(g_szReplace),
};
const UINT g_FindMsg = RegisterWindowMessage(FINDMSGSTRING);
BOOL IsFindDialogMessage(LPMSG lpMsg)
{
return IsDialogMessage(g_hFind, lpMsg);
}
void FindReplaceInit(HWND hOwner)
{
g_fr.hwndOwner = hOwner;
}
void DoFindDlg()
{
if (g_hFind == NULL)
{
g_fr.Flags &= ~FR_DIALOGTERM;
g_hFind = FindTextW(&g_fr);
}
else
SetFocus(g_hFind);
}
void DoReplaceDlg()
{
if (g_hFind == NULL)
{
g_fr.Flags &= ~FR_DIALOGTERM;
g_hFind = ReplaceTextW(&g_fr);
}
else
SetFocus(g_hFind);
}
void DoFindNext()
{
if (IsEmpty(g_fr.lpstrFindWhat))
{
g_fr.Flags &= ~FR_DIALOGTERM;
g_hFind = FindTextW(&g_fr);
}
else
{
g_fr.Flags &= ~(FR_FINDNEXT | FR_REPLACE | FR_REPLACEALL | FR_DIALOGTERM);
g_fr.Flags |= FR_FINDNEXT;
SendMessage(g_fr.hwndOwner, g_FindMsg, 0, (LPARAM) &g_fr);
}
}
void OnDlgTerm()
{
g_hFind = NULL;
}
BOOL TextFind(HWND hEdit, const FINDREPLACEW* pfr)
{
PWCHAR pstrFindWhat = pfr->lpstrFindWhat;
if (IsEmpty(pstrFindWhat))
return TRUE;
HLOCAL hMem = (HLOCAL) SendMessage(hEdit, EM_GETHANDLE, 0, 0);
DWORD dwSelStart, dwSelEnd;
SendMessage(hEdit, EM_GETSEL, (WPARAM) &dwSelStart, (LPARAM) &dwSelEnd);
PCWCHAR mbstr = (PCWCHAR) LocalLock(hMem);
PCWCHAR found = pfr->Flags & FR_MATCHCASE
? (pfr->Flags & FR_DOWN
? StrStrW(mbstr + dwSelEnd, pstrFindWhat)
: StrRStrW(mbstr, mbstr + dwSelStart, pstrFindWhat))
: (pfr->Flags & FR_DOWN
? StrStrIW(mbstr + dwSelEnd, pstrFindWhat)
: StrRStrIW(mbstr, mbstr + dwSelStart, pstrFindWhat));
BOOL ret = FALSE;
if (found != nullptr)
{
ret = TRUE;
dwSelStart = (DWORD) (found - mbstr);
dwSelEnd = (DWORD) (dwSelStart + wcslen(pstrFindWhat));
SendMessage(hEdit, EM_SETSEL, (WPARAM) dwSelStart, (LPARAM) dwSelEnd);
SendMessage(hEdit, EM_SCROLLCARET, 0, 0);
}
LocalUnlock(hMem);
return ret;
}
BOOL TextReplace(HWND hEdit, FINDREPLACEW* pfr)
{
PWCHAR pstrFindWhat = pfr->lpstrFindWhat;
PWCHAR lpstrReplaceWith = pfr->lpstrReplaceWith;
if (IsEmpty(pstrFindWhat))
return TRUE;
HLOCAL hMem = (HLOCAL) SendMessage(hEdit, EM_GETHANDLE, 0, 0);
DWORD dwSelStart, dwSelEnd;
SendMessage(hEdit, EM_GETSEL, (WPARAM) &dwSelStart, (LPARAM) &dwSelEnd);
PCWCHAR mbstr = (PCWCHAR) LocalLock(hMem);
DWORD len = (DWORD) wcslen(pstrFindWhat);
if (pfr->Flags & FR_MATCHCASE
? StrCmpNW(mbstr + dwSelStart, pstrFindWhat, len) == 0
: StrCmpNIW(mbstr + dwSelStart, pstrFindWhat, len) == 0)
// NOTE Need a EM_REPLACESELW
{
SendMessage(hEdit, EM_REPLACESEL, TRUE, (LPARAM) lpstrReplaceWith);
dwSelEnd = dwSelStart + (DWORD) wcslen(lpstrReplaceWith);
SendMessage(hEdit, EM_SETSEL, (WPARAM) dwSelStart, (LPARAM) dwSelEnd);
}
LocalUnlock(hMem);
return TextFind(hEdit, pfr);
}
void ShowFindFailed(LPTSTR szTitle, LPTSTR pstrFindWhat)
{
TCHAR sBuffer[1024];
FormatString(sBuffer, IDS_ERR_FIND, pstrFindWhat);
MessageBox(g_hFind, sBuffer, szTitle, MB_OK | MB_ICONINFORMATION);
}
LRESULT OnFindMsg(HWND hEdit, LPTSTR szTitle, WPARAM /*wParam*/, LPARAM lParam)
{
FINDREPLACEW* pfr = (FINDREPLACEW*) lParam;
if (pfr->Flags & FR_DIALOGTERM)
OnDlgTerm();
else if (pfr->Flags & FR_FINDNEXT)
{
if (!TextFind(hEdit, pfr))
ShowFindFailed(szTitle, pfr->lpstrFindWhat);
}
else if (pfr->Flags & FR_REPLACE)
{
if (!TextReplace(hEdit, pfr))
ShowFindFailed(szTitle, pfr->lpstrFindWhat);
}
else if (pfr->Flags & FR_REPLACEALL)
{
while (!TextReplace(hEdit, pfr))
;
}
return 0;
}