fix building with MSVC
[reactos.git] / reactos / subsys / system / notepad / main.c
index 0c9a800..be3ba17 100644 (file)
  *
  */
 
-#define UNICODE
-#define _UNICODE
-
-#include <windows.h>
-#include <stdio.h>
-#include <tchar.h>
-
-#include "main.h"
-#include "dialog.h"
-#include "notepad_res.h"
+#include <notepad.h>
 
 NOTEPAD_GLOBALS Globals;
 static ATOM aFINDMSGSTRING;
@@ -46,7 +37,7 @@ VOID SetFileName(LPCWSTR szFileName)
 {
     lstrcpy(Globals.szFileName, szFileName);
     Globals.szFileTitle[0] = 0;
-    GetFileTitle(szFileName, Globals.szFileTitle, sizeof(Globals.szFileTitle));
+    GetFileTitle(szFileName, Globals.szFileTitle, wcslen(Globals.szFileTitle));
 }
 
 /***********************************************************************
@@ -92,7 +83,7 @@ static int NOTEPAD_MenuCommand(WPARAM wParam)
     case CMD_ABOUT_WINE:       DIALOG_HelpAboutWine(); break;
 
     default:
-       break;
+    break;
     }
    return 0;
 }
@@ -106,7 +97,7 @@ static BOOL NOTEPAD_FindTextAt(FINDREPLACE *pFindReplace, LPCTSTR pszText, int i
 {
     BOOL bMatches;
     int iTargetLength;
-       
+
     iTargetLength = _tcslen(pFindReplace->lpstrFindWhat);
 
     /* Make proper comparison */
@@ -143,8 +134,8 @@ static BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bReplace, BOOL bSho
 
     iTargetLength = _tcslen(pFindReplace->lpstrFindWhat);
 
+    /* Retrieve the window text */
     iTextLength = GetWindowTextLength(Globals.hEdit);
-
     if (iTextLength > 0)
     {
         pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (iTextLength + 1) * sizeof(TCHAR));
@@ -156,25 +147,37 @@ static BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bReplace, BOOL bSho
 
     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)
+    if (pFindReplace->Flags & FR_DOWN)
     {
-        bMatches = NOTEPAD_FindTextAt(pFindReplace, pszText, iTextLength, dwPosition);
-        if (bMatches)
-            break;
-
-        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)
@@ -186,18 +189,18 @@ static BOOL NOTEPAD_FindNext(FINDREPLACE *pFindReplace, BOOL bReplace, BOOL bSho
         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.hFindReplaceDlg, szText, szResource, MB_OK);
-               }
+        }
         bSuccess = FALSE;
-       }
+    }
 
     if (pszText)
         HeapFree(GetProcessHeap(), 0, pszText);
@@ -218,7 +221,7 @@ static VOID NOTEPAD_ReplaceAll(FINDREPLACE *pFindReplace)
     while (NOTEPAD_FindNext(pFindReplace, TRUE, bShowAlert))
     {
         bShowAlert = FALSE;
-       }
+    }
 }
 
 /***********************************************************************
@@ -258,6 +261,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,
@@ -286,12 +292,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;
     }
 
@@ -421,8 +429,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] == '"')
@@ -431,12 +439,12 @@ static void HandleCommandLine(LPWSTR cmdline)
             cmdline[lstrlen(cmdline) - 1] = 0;
         }
 
-        if (FileExists(cmdline))
+        file_name = cmdline;
+        if (FileExists(file_name))
         {
             file_exists = TRUE;
-            file_name = cmdline;
         }
-        else
+        else if (!HasFileExtension(cmdline))
         {
             static const WCHAR txtW[] = { '.','t','x','t',0 };
 
@@ -444,14 +452,13 @@ static void HandleCommandLine(LPWSTR cmdline)
             if (!lstrcmp(txtW, cmdline + lstrlen(cmdline) - lstrlen(txtW)))
             {
                 file_exists = FALSE;
-                file_name = cmdline;
             }
             else
             {
                 lstrcpyn(buf, cmdline, MAX_PATH - lstrlen(txtW) - 1);
                 lstrcat(buf, txtW);
                 file_name = buf;
-                file_exists = FileExists(buf);
+                file_exists = FileExists(file_name);
             }
         }
 
@@ -492,6 +499,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);
@@ -537,5 +545,6 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
             DispatchMessage(&msg);
         }
     }
+    SaveSettings();
     return msg.wParam;
 }