notepad[2/3]: implement Find and Find Next
Mikołaj Zalewski
mikolaj at zalewski.pl
Sun Oct 22 11:58:12 CDT 2006
Searching backward, case-insensitive doesn't work well until the
StrRStrI from shlwapi is fixed by my later patch
-------------- next part --------------
diff --git a/programs/notepad/dialog.c b/programs/notepad/dialog.c
index 9f822d4..f16a0bf 100644
--- a/programs/notepad/dialog.c
+++ b/programs/notepad/dialog.c
@@ -663,7 +663,7 @@ VOID DIALOG_Search(VOID)
Globals.find.hInstance = Globals.hInstance;
Globals.find.lpstrFindWhat = Globals.szFindText;
Globals.find.wFindWhatLen = SIZEOF(Globals.szFindText);
- Globals.find.Flags = FR_DOWN;
+ 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 */
@@ -674,8 +674,10 @@ VOID DIALOG_Search(VOID)
VOID DIALOG_SearchNext(VOID)
{
- /* FIXME: Search Next */
- DIALOG_Search();
+ if (Globals.lastFind.lpstrFindWhat == NULL)
+ DIALOG_Search();
+ else /* use the last find data */
+ NOTEPAD_DoFind(&Globals.lastFind);
}
VOID DIALOG_HelpContents(VOID)
diff --git a/programs/notepad/main.c b/programs/notepad/main.c
index 33c82b2..3ffa14b 100644
--- a/programs/notepad/main.c
+++ b/programs/notepad/main.c
@@ -25,6 +25,7 @@
#define UNICODE
#include <windows.h>
+#include <shlwapi.h>
#include <stdio.h>
#include "main.h"
@@ -314,6 +315,65 @@ static VOID NOTEPAD_InitMenuPopup(HMENU
GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
}
+static LPTSTR NOTEPAD_StrRStr(LPTSTR pszSource, LPTSTR pszLast, LPTSTR pszSrch)
+{
+ int len = lstrlen(pszSrch);
+ pszLast--;
+ while (pszLast >= pszSource)
+ {
+ if (StrCmpN(pszLast, pszSrch, len) == 0)
+ return pszLast;
+ pszLast--;
+ }
+ return NULL;
+}
+
+/***********************************************************************
+ * The user activated the Find dialog
+ */
+void NOTEPAD_DoFind(FINDREPLACE *fr)
+{
+ LPTSTR content;
+ LPTSTR found;
+ int len = lstrlen(fr->lpstrFindWhat);
+ int fileLen;
+ DWORD pos;
+
+ 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 0:
+ found = StrRStrI(content, content+pos-len, fr->lpstrFindWhat);
+ break;
+ case FR_DOWN:
+ found = StrStrI(content+pos, fr->lpstrFindWhat);
+ break;
+ case FR_MATCHCASE:
+ found = NOTEPAD_StrRStr(content, content+pos-len, 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)
+ {
+ DIALOG_StringMsgBox(Globals.hFindReplaceDlg, STRING_NOTFOUND, fr->lpstrFindWhat,
+ MB_ICONINFORMATION|MB_OK);
+ return;
+ }
+
+ SendMessage(Globals.hEdit, EM_SETSEL, found - content, found - content + len);
+}
+
/***********************************************************************
*
* NOTEPAD_WndProc
@@ -321,13 +381,27 @@ static VOID NOTEPAD_InitMenuPopup(HMENU
static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
+ if (msg == aFINDMSGSTRING) /* not a constant so can't be used in switch */
+ {
+ FINDREPLACE *fr = (FINDREPLACE *)lParam;
+
+ if (fr->Flags & FR_DIALOGTERM)
+ Globals.hFindReplaceDlg = NULL;
+ if (fr->Flags & FR_FINDNEXT)
+ {
+ Globals.lastFind = *fr;
+ NOTEPAD_DoFind(fr);
+ }
+ return 0;
+ }
+
switch (msg) {
case WM_CREATE:
{
static const WCHAR editW[] = { 'e','d','i','t',0 };
DWORD dwStyle = WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |
- ES_AUTOVSCROLL | ES_MULTILINE;
+ ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
RECT rc;
GetClientRect(hWnd, &rc);
diff --git a/programs/notepad/main.h b/programs/notepad/main.h
index 3dd6bb0..075a248 100644
--- a/programs/notepad/main.h
+++ b/programs/notepad/main.h
@@ -51,6 +51,7 @@ typedef struct
INT iWindowPosDY;
FINDREPLACE find;
+ FINDREPLACE lastFind;
HGLOBAL hDevMode; /* printer mode */
HGLOBAL hDevNames; /* printer names */
} NOTEPAD_GLOBALS;
@@ -58,3 +59,4 @@ typedef struct
extern NOTEPAD_GLOBALS Globals;
VOID SetFileName(LPCWSTR szFileName);
+void NOTEPAD_DoFind(FINDREPLACE *fr);
--
1.4.2.3
More information about the wine-patches
mailing list