Notepad:   Fixed some bugs in Search->Replace
Modified: trunk/reactos/subsys/system/notepad/main.c

Modified: trunk/reactos/subsys/system/notepad/main.c
--- trunk/reactos/subsys/system/notepad/main.c	2005-09-28 13:00:05 UTC (rev 18134)
+++ trunk/reactos/subsys/system/notepad/main.c	2005-09-28 13:20:10 UTC (rev 18135)
@@ -99,14 +99,44 @@
 
 /***********************************************************************
  *
+ *           NOTEPAD_FindTextAt
+ */
+
+static BOOL NOTEPAD_FindTextAt(FINDREPLACE *pFindReplace, LPCTSTR pszText, int iTextLength, DWORD dwPosition)
+{
+    BOOL bMatches;
+    int iTargetLength;
+	
+    iTargetLength = _tcslen(pFindReplace->lpstrFindWhat);
+
+    /* Make proper comparison */
+    if (pFindReplace->Flags & FR_MATCHCASE)
+        bMatches = !_tcsncmp(&pszText[dwPosition], pFindReplace->lpstrFindWhat, iTargetLength);
+    else
+        bMatches = !_tcsnicmp(&pszText[dwPosition], pFindReplace->lpstrFindWhat, iTargetLength);
+
+    if (bMatches && pFindReplace->Flags & FR_WHOLEWORD)
+    {
+        if ((dwPosition > 0) && !_istspace(pszText[dwPosition-1]))
+            bMatches = FALSE;
+        if ((dwPosition < iTextLength - 1) && !_istspace(pszText[dwPosition+1]))
+            bMatches = FALSE;
+    }
+
+    return bMatches;
+}
+
+/***********************************************************************
+ *
  *           NOTEPAD_FindNext
  */
 
-static BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bShowAlert)
+static BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bReplace, BOOL bShowAlert)
 {
     int iTextLength, iTargetLength;
+    int iAdjustment = 0;
     LPTSTR pszText = NULL;
-    DWORD dwPosition, dwDummy;
+    DWORD dwPosition, dwBegin, dwEnd;
     BOOL bMatches = FALSE;
     TCHAR szResource[128], szText[128];
     BOOL bSuccess;
@@ -124,24 +154,20 @@
         GetWindowText(Globals.hEdit, pszText, iTextLength + 1);
     }
 
-    SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwDummy, (LPARAM) &dwPosition);
+    SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwBegin, (LPARAM) &dwEnd);
+    if (bReplace && ((dwEnd - dwBegin) == iTargetLength))
+	{
+        if (NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwBegin))
+		{
+            SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM) pFindReplace->lpstrReplaceWith);
+            iAdjustment = _tcslen(pFindReplace->lpstrReplaceWith) - (dwEnd - dwBegin);
+		}
+	}
 
+    dwPosition = dwEnd;
     while(dwPosition < iTextLength)
     {
-        /* Make proper comparison */
-        if (pFindReplace->Flags & FR_MATCHCASE)
-            bMatches = !_tcsncmp(&pszText[dwPosition], pFindReplace->lpstrFindWhat, iTargetLength);
-        else
-            bMatches = !_tcsnicmp(&pszText[dwPosition], pFindReplace->lpstrFindWhat, iTargetLength);
-
-        if (bMatches && pFindReplace->Flags & FR_WHOLEWORD)
-        {
-            if ((dwPosition > 0) && !_istspace(pszText[dwPosition-1]))
-                bMatches = FALSE;
-            if ((dwPosition < iTextLength - 1) && !_istspace(pszText[dwPosition+1]))
-                bMatches = FALSE;
-        }
-
+        bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
         if (bMatches)
             break;
 
@@ -154,6 +180,8 @@
     if (bMatches)
     {
         /* Found target */
+        if (dwPosition > dwBegin)
+            dwPosition += iAdjustment;
         SendMessage(Globals.hEdit, EM_SETSEL, dwPosition, dwPosition + iTargetLength);
         SendMessage(Globals.hEdit, EM_SCROLLCARET, 0, 0);
         bSuccess = TRUE;
@@ -178,17 +206,6 @@
 
 /***********************************************************************
  *
- *           NOTEPAD_Replace
- */
-
-static VOID NOTEPAD_Replace(FINDREPLACE *pFindReplace)
-{
-    if (NOTEPAD_FindNext(pFindReplace, TRUE))
-        SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM) pFindReplace->lpstrReplaceWith);
-}
-
-/***********************************************************************
- *
  *           NOTEPAD_ReplaceAll
  */
 
@@ -198,9 +215,8 @@
 
     SendMessage(Globals.hEdit, EM_SETSEL, 0, 0);
 
-    while (NOTEPAD_FindNext(pFindReplace, bShowAlert))
+    while (NOTEPAD_FindNext(pFindReplace, TRUE, bShowAlert))
     {
-        SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM) pFindReplace->lpstrReplaceWith);
         bShowAlert = FALSE;
 	}
 }
@@ -333,9 +349,9 @@
             FINDREPLACE *pFindReplace = (FINDREPLACE *) lParam;
 
             if (pFindReplace->Flags & FR_FINDNEXT)
-                NOTEPAD_FindNext(pFindReplace, TRUE);
+                NOTEPAD_FindNext(pFindReplace, FALSE, TRUE);
             else if (pFindReplace->Flags & FR_REPLACE)
-                NOTEPAD_Replace(pFindReplace);
+                NOTEPAD_FindNext(pFindReplace, TRUE, TRUE);
             else if (pFindReplace->Flags & FR_REPLACEALL)
                 NOTEPAD_ReplaceAll(pFindReplace);
             else if (pFindReplace->Flags & FR_DIALOGTERM)