1. Moved windows styles pertaining to the edit control to main.h
[reactos.git] / reactos / subsys / system / notepad / main.c
index 18c9faf..5f21ae1 100644 (file)
  */
 
 #define UNICODE
+#define _UNICODE
 
 #include <windows.h>
 #include <stdio.h>
+#include <tchar.h>
 
 #include "main.h"
 #include "dialog.h"
@@ -93,6 +95,78 @@ static int NOTEPAD_MenuCommand(WPARAM wParam)
    return 0;
 }
 
+/***********************************************************************
+ *
+ *           NOTEPAD_FindNext
+ */
+
+static VOID NOTEPAD_FindNext(FINDREPLACE *pFindReplace)
+{
+    int iTextLength, iTargetLength;
+    LPTSTR pszText = NULL;
+    DWORD dwPosition, dwDummy;
+    BOOL bMatches = FALSE;
+
+    iTargetLength = _tcslen(pFindReplace->lpstrFindWhat);
+
+    iTextLength = GetWindowTextLength(Globals.hEdit);
+
+    if (iTextLength > 0)
+    {
+        pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (iTextLength + 1) * sizeof(TCHAR));
+        if (!pszText)
+            return;
+
+        GetWindowText(Globals.hEdit, pszText, iTextLength + 1);
+    }
+
+    SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwDummy, (LPARAM) &dwPosition);
+
+    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;
+        }
+
+        if (bMatches)
+            break;
+
+        if (pFindReplace->Flags & FR_DOWN) 
+            dwPosition++;
+        else
+            dwPosition--;
+    }
+
+    if (bMatches)
+    {
+        SendMessage(Globals.hEdit, EM_SETSEL, dwPosition, dwPosition + iTargetLength);
+        SendMessage(Globals.hEdit, EM_SCROLLCARET, 0, 0);
+    }
+
+    if (pszText)
+        HeapFree(GetProcessHeap(), 0, pszText);
+}
+
+/***********************************************************************
+ *
+ *           NOTEPAD_FindTerm
+ */
+
+static VOID NOTEPAD_FindTerm(VOID)
+{
+    Globals.hFindReplaceDlg = NULL;
+}
+
 /***********************************************************************
  * Data Initialization
  */
@@ -113,6 +187,27 @@ static VOID NOTEPAD_InitData(VOID)
     *p = '\0';
 }
 
+/***********************************************************************
+ * Enable/disable items on the menu based on control state
+ */
+static VOID NOTEPAD_InitMenuPopup(HMENU menu, int index)
+{
+    int enable;
+
+    EnableMenuItem(menu, CMD_UNDO,
+        SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
+    EnableMenuItem(menu, CMD_PASTE,
+        IsClipboardFormatAvailable(CF_TEXT) ? MF_ENABLED : MF_GRAYED);
+    enable = SendMessage(Globals.hEdit, EM_GETSEL, 0, 0);
+    enable = (HIWORD(enable) == LOWORD(enable)) ? MF_GRAYED : MF_ENABLED;
+    EnableMenuItem(menu, CMD_CUT, enable);
+    EnableMenuItem(menu, CMD_COPY, enable);
+    EnableMenuItem(menu, CMD_DELETE, enable);
+    
+    EnableMenuItem(menu, CMD_SELECT_ALL,
+        GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
+}
+
 /***********************************************************************
  *
  *           NOTEPAD_WndProc
@@ -127,9 +222,7 @@ 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(WS_EX_CLIENTEDGE, editW, NULL,
-                             WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | WS_HSCROLL |
-                             ES_AUTOVSCROLL | ES_MULTILINE,
+        Globals.hEdit = CreateWindowEx(EDIT_EXSTYLE, editW, NULL, EDIT_STYLE,
                              0, 0, rc.right, rc.bottom, hWnd,
                              NULL, Globals.hInstance, NULL);
         break;
@@ -178,8 +271,23 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam,
         DoOpenFile(szFileName);
         break;
     }
+    
+    case WM_INITMENUPOPUP:
+        NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
+        break;
 
     default:
+        if (msg == aFINDMSGSTRING)
+        {
+            FINDREPLACE *pFindReplace = (FINDREPLACE *) lParam;
+
+            if (pFindReplace->Flags & FR_FINDNEXT)
+                NOTEPAD_FindNext(pFindReplace);
+            else if (pFindReplace->Flags & FR_DIALOGTERM)
+                NOTEPAD_FindTerm();
+            break;
+        }
+
         return DefWindowProc(hWnd, msg, wParam, lParam);
     }
     return 0;
@@ -194,7 +302,7 @@ static int AlertFileDoesNotExist(LPCWSTR szFileName)
    LoadString(Globals.hInstance, STRING_DOESNOTEXIST, szResource, SIZEOF(szResource));
    wsprintf(szMessage, szResource, szFileName);
 
-   LoadString(Globals.hInstance, STRING_ERROR, szResource, SIZEOF(szResource));
+   LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
 
    nResult = MessageBox(Globals.hMainWnd, szMessage, szResource,
                         MB_ICONEXCLAMATION | MB_YESNO);
@@ -320,7 +428,7 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
     class.hInstance     = Globals.hInstance;
     class.hIcon         = LoadIcon(0, IDI_APPLICATION);
     class.hCursor       = LoadCursor(0, IDC_ARROW);
-    class.hbrBackground = (HBRUSH)(COLOR_WINDOW);
+    class.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
     class.lpszMenuName  = MAKEINTRESOURCE(MAIN_MENU);
     class.lpszClassName = className;
 
@@ -351,11 +459,12 @@ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
 
     while (GetMessage(&msg, 0, 0, 0))
     {
-       if (!TranslateAccelerator(Globals.hMainWnd, hAccel, &msg) && !IsDialogMessage(Globals.hFindReplaceDlg, &msg))
-       {
-           TranslateMessage(&msg);
-           DispatchMessage(&msg);
-       }
+        if (!IsDialogMessage(Globals.hFindReplaceDlg, &msg) &&
+            !TranslateAccelerator(Globals.hMainWnd, hAccel, &msg))
+        {
+            TranslateMessage(&msg);
+            DispatchMessage(&msg);
+        }
     }
     return msg.wParam;
 }