Notepad will no longer add .txt if you invoke it specifying a file that does not...
[reactos.git] / reactos / subsys / system / notepad / main.c
index bcfcb70..c0f1ed2 100644 (file)
@@ -25,6 +25,8 @@
 #define UNICODE
 #define _UNICODE
 
+#define _CRT_SECURE_NO_DEPRECATE
+
 #include <windows.h>
 #include <stdio.h>
 #include <tchar.h>
@@ -92,29 +94,59 @@ static int NOTEPAD_MenuCommand(WPARAM wParam)
     case CMD_ABOUT_WINE:       DIALOG_HelpAboutWine(); break;
 
     default:
-       break;
+    break;
     }
    return 0;
 }
 
+/***********************************************************************
+ *
+ *           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;
 
     iTargetLength = _tcslen(pFindReplace->lpstrFindWhat);
 
+    /* Retrieve the window text */
     iTextLength = GetWindowTextLength(Globals.hEdit);
-
     if (iTextLength > 0)
     {
         pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (iTextLength + 1) * sizeof(TCHAR));
@@ -124,69 +156,68 @@ static BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bShowAlert)
         GetWindowText(Globals.hEdit, pszText, iTextLength + 1);
     }
 
-    SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwDummy, (LPARAM) &dwPosition);
-
-    while(dwPosition < iTextLength)
+    SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwBegin, (LPARAM) &dwEnd);
+    if (bReplace && ((dwEnd - dwBegin) == iTargetLength))
     {
-        /* 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 (NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwBegin))
         {
-            if ((dwPosition > 0) && !_istspace(pszText[dwPosition-1]))
-                bMatches = FALSE;
-            if ((dwPosition < iTextLength - 1) && !_istspace(pszText[dwPosition+1]))
-                bMatches = FALSE;
+            SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM) pFindReplace->lpstrReplaceWith);
+            iAdjustment = _tcslen(pFindReplace->lpstrReplaceWith) - (dwEnd - dwBegin);
         }
+    }
 
-        if (bMatches)
-            break;
-
-        if (pFindReplace->Flags & FR_DOWN) 
+    if (pFindReplace->Flags & FR_DOWN)
+    {
+        /* Find Down */
+        dwPosition = dwEnd;
+        while(dwPosition < iTextLength)
+        {
+            bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
+            if (bMatches)
+                break;
             dwPosition++;
-        else
+        }
+    }
+    else
+    {
+        /* Find Up */
+        dwPosition = dwBegin;
+        while(dwPosition > 0)
+        {
             dwPosition--;
+            bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
+            if (bMatches)
+                break;
+        }
     }
 
     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;
     }
-       else
-       {
+    else
+    {
         /* Can't find target */
         if (bShowAlert)
-               {
+        {
             LoadString(Globals.hInstance, STRING_CANNOTFIND, szResource, SIZEOF(szResource));
             _sntprintf(szText, SIZEOF(szText), szResource, pFindReplace->lpstrFindWhat);
             LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
-            MessageBox(Globals.hEdit, szText, szResource, MB_OK);
-               }
+            MessageBox(Globals.hFindReplaceDlg, szText, szResource, MB_OK);
+        }
         bSuccess = FALSE;
-       }
+    }
 
     if (pszText)
         HeapFree(GetProcessHeap(), 0, pszText);
     return bSuccess;
 }
 
-/***********************************************************************
- *
- *           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,11 +229,10 @@ static VOID NOTEPAD_ReplaceAll(FINDREPLACE *pFindReplace)
 
     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;
-       }
+    }
 }
 
 /***********************************************************************
@@ -242,6 +272,9 @@ static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
 {
     int enable;
 
+    CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
+        MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
+
     EnableMenuItem(menu, CMD_UNDO,
         SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
     EnableMenuItem(menu, CMD_PASTE,
@@ -270,12 +303,14 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
         static const WCHAR editW[] = { 'e','d','i','t',0 };
         RECT rc;
         GetClientRect(hWnd, &rc);
-        Globals.hEdit = CreateWindowEx(EDIT_EXSTYLE, editW, NULL, EDIT_STYLE,
+        Globals.hEdit = CreateWindowEx(EDIT_EXSTYLE, editW, NULL, Globals.bWrapLongLines ? EDIT_STYLE_WRAP : EDIT_STYLE,
                              0, 0, rc.right, rc.bottom, hWnd,
                              NULL, Globals.hInstance, NULL);
         if (!Globals.hEdit)
             return -1;
         SendMessage(Globals.hEdit, EM_LIMITTEXT, 0, 0);
+        if (Globals.hFont)
+            SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE);
         break;
     }
 
@@ -333,9 +368,9 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
             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)
@@ -405,8 +440,8 @@ static void HandleCommandLine(LPWSTR cmdline)
     if (*cmdline)
     {
         /* file name is passed in the command line */
-        LPCWSTR file_name;
-        BOOL file_exists;
+        LPCWSTR file_name = NULL;
+        BOOL file_exists = FALSE;
         WCHAR buf[MAX_PATH];
 
         if (cmdline[0] == '"')
@@ -420,7 +455,7 @@ static void HandleCommandLine(LPWSTR cmdline)
             file_exists = TRUE;
             file_name = cmdline;
         }
-        else
+        else if (!HasFileExtension(cmdline))
         {
             static const WCHAR txtW[] = { '.','t','x','t',0 };
 
@@ -428,7 +463,6 @@ static void HandleCommandLine(LPWSTR cmdline)
             if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
             {
                 file_exists = FALSE;
-                file_name = cmdline;
             }
             else
             {
@@ -441,6 +475,7 @@ static void HandleCommandLine(LPWSTR cmdline)
 
         if (file_exists)
         {
+            file_name = cmdline;
             DoOpenFile(file_name);
             InvalidateRect(Globals.hMainWnd, NULL, FALSE);
             if (opt_print)
@@ -476,6 +511,7 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
 
     ZeroMemory(&Globals, sizeof(Globals));
     Globals.hInstance       = hInstance;
+    LoadSettings();
 
     ZeroMemory(&class, sizeof(class));
     class.cbSize        = sizeof(class);
@@ -521,5 +557,6 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
             DispatchMessage(&msg);
         }
     }
+    SaveSettings();
     return msg.wParam;
 }