Rico Schüller : notepad: Implement replace.

Alexandre Julliard julliard at winehq.org
Mon Apr 27 08:04:10 CDT 2009


Module: wine
Branch: master
Commit: 3137600651a203486f44e312b6a23cf577f27ca6
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=3137600651a203486f44e312b6a23cf577f27ca6

Author: Rico Schüller <kgbricola at web.de>
Date:   Sat Apr 25 17:53:58 2009 +0200

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(+), 0 deletions(-)

diff --git a/programs/notepad/De.rc b/programs/notepad/De.rc
index 24d713a..fb315a8 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 c68404e..c4a4baf 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 d97e102..ce4bbfa 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 d780991..d927143 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 1751c90..33b4859 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 51db639..88e7fbe 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 9228a5e..8176b85 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 8018a58..50b3558 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




More information about the wine-cvs mailing list