From 3137600651a203486f44e312b6a23cf577f27ca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rico=20Sch=C3=BCller?= Date: Sat, 25 Apr 2009 17:53:58 +0200 Subject: [PATCH] notepad: Implement replace. --- programs/notepad/De.rc | 1 + programs/notepad/En.rc | 1 + programs/notepad/dialog.c | 19 ++++++++ programs/notepad/dialog.h | 1 + programs/notepad/main.c | 82 ++++++++++++++++++++++++++++++++++ programs/notepad/main.h | 1 + programs/notepad/notepad_res.h | 1 + programs/notepad/rsrc.rc | 1 + 8 files changed, 107 insertions(+) diff --git a/programs/notepad/De.rc b/programs/notepad/De.rc index 24d713a52a8..fb315a863fb 100644 --- a/programs/notepad/De.rc +++ b/programs/notepad/De.rc @@ -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 diff --git a/programs/notepad/En.rc b/programs/notepad/En.rc index c68404e35c5..c4a4baf440e 100644 --- a/programs/notepad/En.rc +++ b/programs/notepad/En.rc @@ -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 diff --git a/programs/notepad/dialog.c b/programs/notepad/dialog.c index d97e102c2f4..ce4bbfa3f0e 100644 --- a/programs/notepad/dialog.c +++ b/programs/notepad/dialog.c @@ -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); diff --git a/programs/notepad/dialog.h b/programs/notepad/dialog.h index d7809915aec..d9271431444 100644 --- a/programs/notepad/dialog.h +++ b/programs/notepad/dialog.h @@ -38,6 +38,7 @@ VOID DIALOG_EditWrap(VOID); VOID DIALOG_Search(VOID); VOID DIALOG_SearchNext(VOID); +VOID DIALOG_Replace(VOID); VOID DIALOG_SelectFont(VOID); diff --git a/programs/notepad/main.c b/programs/notepad/main.c index 1751c90f509..33b48595e1a 100644 --- a/programs/notepad/main.c +++ b/programs/notepad/main.c @@ -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; } diff --git a/programs/notepad/main.h b/programs/notepad/main.h index 51db639dfda..88e7fbedea6 100644 --- a/programs/notepad/main.h +++ b/programs/notepad/main.h @@ -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]; diff --git a/programs/notepad/notepad_res.h b/programs/notepad/notepad_res.h index 9228a5ef43d..8176b855e7f 100644 --- a/programs/notepad/notepad_res.h +++ b/programs/notepad/notepad_res.h @@ -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 diff --git a/programs/notepad/rsrc.rc b/programs/notepad/rsrc.rc index 8018a58a4d7..50b3558f5ad 100644 --- a/programs/notepad/rsrc.rc +++ b/programs/notepad/rsrc.rc @@ -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