notepad: Implement replace.

This commit is contained in:
Rico Schüller 2009-04-25 17:53:58 +02:00 committed by Alexandre Julliard
parent 4d4819d8c4
commit 3137600651
8 changed files with 107 additions and 0 deletions

View File

@ -53,6 +53,7 @@ POPUP "&Bearbeiten" {
POPUP "&Suchen" {
MENUITEM "Suchen...\tStrg+F", CMD_SEARCH
MENUITEM "&Weitersuchen\tF3", CMD_SEARCH_NEXT
MENUITEM "&Ersetzen...\tStrg+H", CMD_REPLACE
}
POPUP "&Hilfe" {
MENUITEM "&Inhalt", CMD_HELP_CONTENTS

View File

@ -53,6 +53,7 @@ POPUP "&Edit" {
POPUP "&Search" {
MENUITEM "&Search...\tCtrl+F", CMD_SEARCH
MENUITEM "&Search next\tF3", CMD_SEARCH_NEXT
MENUITEM "&Replace...\tCtrl+H", CMD_REPLACE
}
POPUP "&Help" {
MENUITEM "&Contents", CMD_HELP_CONTENTS

View File

@ -747,6 +747,25 @@ VOID DIALOG_SearchNext(VOID)
NOTEPAD_DoFind(&Globals.lastFind);
}
VOID DIALOG_Replace(VOID)
{
ZeroMemory(&Globals.find, sizeof(Globals.find));
Globals.find.lStructSize = sizeof(Globals.find);
Globals.find.hwndOwner = Globals.hMainWnd;
Globals.find.hInstance = Globals.hInstance;
Globals.find.lpstrFindWhat = Globals.szFindText;
Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
Globals.find.lpstrReplaceWith = Globals.szReplaceText;
Globals.find.wReplaceWithLen = SIZEOF(Globals.szReplaceText);
Globals.find.Flags = FR_DOWN|FR_HIDEWHOLEWORD;
/* We only need to create the modal FindReplace dialog which will */
/* notify us of incoming events using hMainWnd Window Messages */
Globals.hFindReplaceDlg = ReplaceText(&Globals.find);
assert(Globals.hFindReplaceDlg !=0);
}
VOID DIALOG_HelpContents(VOID)
{
WinHelp(Globals.hMainWnd, helpfileW, HELP_INDEX, 0);

View File

@ -38,6 +38,7 @@ VOID DIALOG_EditWrap(VOID);
VOID DIALOG_Search(VOID);
VOID DIALOG_SearchNext(VOID);
VOID DIALOG_Replace(VOID);
VOID DIALOG_SelectFont(VOID);

View File

@ -294,6 +294,7 @@ static int NOTEPAD_MenuCommand(WPARAM wParam)
case CMD_SEARCH: DIALOG_Search(); break;
case CMD_SEARCH_NEXT: DIALOG_SearchNext(); break;
case CMD_REPLACE: DIALOG_Replace(); break;
case CMD_WRAP: DIALOG_EditWrap(); break;
case CMD_FONT: DIALOG_SelectFont(); break;
@ -414,6 +415,77 @@ void NOTEPAD_DoFind(FINDREPLACE *fr)
SendMessage(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
}
void NOTEPAD_DoReplace(FINDREPLACE *fr)
{
LPTSTR content;
int len = lstrlen(fr->lpstrFindWhat);
int fileLen;
DWORD pos;
DWORD pos_start;
fileLen = GetWindowTextLength(Globals.hEdit) + 1;
content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(TCHAR));
if (!content) return;
GetWindowText(Globals.hEdit, content, fileLen);
SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM)&pos_start, (LPARAM)&pos);
switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
{
case FR_DOWN:
if ( pos-pos_start == len && StrCmpNI(fr->lpstrFindWhat, content+pos_start, len) == 0)
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
break;
case FR_DOWN|FR_MATCHCASE:
if ( pos-pos_start == len && StrCmpN(fr->lpstrFindWhat, content+pos_start, len) == 0)
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
break;
default: /* shouldn't happen */
return;
}
HeapFree(GetProcessHeap(), 0, content);
NOTEPAD_DoFind(fr);
}
void NOTEPAD_DoReplaceAll(FINDREPLACE *fr)
{
LPTSTR content;
LPTSTR found;
int len = lstrlen(fr->lpstrFindWhat);
int fileLen;
DWORD pos;
SendMessage(Globals.hEdit, EM_SETSEL, 0, 0);
while(TRUE){
fileLen = GetWindowTextLength(Globals.hEdit) + 1;
content = HeapAlloc(GetProcessHeap(), 0, fileLen * sizeof(TCHAR));
if (!content) return;
GetWindowText(Globals.hEdit, content, fileLen);
SendMessage(Globals.hEdit, EM_GETSEL, 0, (LPARAM)&pos);
switch (fr->Flags & (FR_DOWN|FR_MATCHCASE))
{
case FR_DOWN:
found = StrStrI(content+pos, fr->lpstrFindWhat);
break;
case FR_DOWN|FR_MATCHCASE:
found = StrStr(content+pos, fr->lpstrFindWhat);
break;
default: /* shouldn't happen */
return;
}
HeapFree(GetProcessHeap(), 0, content);
if(found == NULL)
{
SendMessage(Globals.hEdit, EM_SETSEL, 0, 0);
return;
}
SendMessage(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)fr->lpstrReplaceWith);
}
}
/***********************************************************************
*
* NOTEPAD_WndProc
@ -432,6 +504,16 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
Globals.lastFind = *fr;
NOTEPAD_DoFind(fr);
}
if (fr->Flags & FR_REPLACE)
{
Globals.lastFind = *fr;
NOTEPAD_DoReplace(fr);
}
if (fr->Flags & FR_REPLACEALL)
{
Globals.lastFind = *fr;
NOTEPAD_DoReplaceAll(fr);
}
return 0;
}

View File

@ -35,6 +35,7 @@ typedef struct
LOGFONT lfFont;
BOOL bWrapLongLines;
WCHAR szFindText[MAX_PATH];
WCHAR szReplaceText[MAX_PATH];
WCHAR szFileName[MAX_PATH];
WCHAR szFileTitle[MAX_PATH];
WCHAR szFilter[2 * MAX_STRING_LEN + 100];

View File

@ -45,6 +45,7 @@
#define CMD_SEARCH 0x120
#define CMD_SEARCH_NEXT 0x121
#define CMD_REPLACE 0x122
#define CMD_WRAP 0x119
#define CMD_FONT 0x140

View File

@ -30,6 +30,7 @@ ID_ACCEL ACCELERATORS
"^A", CMD_SELECT_ALL
"^C", CMD_COPY
"^F", CMD_SEARCH
"H", CMD_REPLACE, VIRTKEY, CONTROL
"^N", CMD_NEW
"^O", CMD_OPEN
"^P", CMD_PRINT