/* keybd1.cpp   simple keyboard interface */

#include <windows.h>
#include "keybd1.h"

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
    int nCmdShow)
{
    HWND        hWnd ;      /* the window's "handle" */
    MSG         msg ;       /* a message structure */
    WNDCLASS    wndclass ;  /* window class structure */

        wndclass.style          = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;
        wndclass.lpfnWndProc    = WndProc ;
        wndclass.cbClsExtra     = 0 ;
        wndclass.cbWndExtra     = 0 ;
        wndclass.hInstance      = hInstance ;
        wndclass.hIcon          = NULL ;
        wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground  = GetStockObject (WHITE_BRUSH) ;
        wndclass.lpszMenuName   = "MYMENU" ;
        wndclass.lpszClassName  = "MyClass" ;
                        /* register the window class */
        if (!RegisterClass (&wndclass))
            return 0 ;

    hWnd = CreateWindow ("MyClass","Type Something!", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT,  CW_USEDEFAULT,  CW_USEDEFAULT,  CW_USEDEFAULT,
        NULL, NULL, hInstance, NULL) ;
    ShowWindow (hWnd, nCmdShow) ;   /* display the window */
    UpdateWindow (hWnd) ;

    while (GetMessage (&msg, NULL, 0, 0))   /* message loop */
    {
        TranslateMessage (&msg) ;   /* translate keyboard messages */
        DispatchMessage (&msg) ;    /* send message to WndProc() */
    }
    return (msg.wParam) ;
}

LRESULT CALLBACK WndProc (HWND hWnd, UINT wMessage, 
                                     WPARAM wParam, LPARAM lParam)
{
    static  char    cBuf [LINELONG] ;
    static  int     nCurPos = 0 ;
    PAINTSTRUCT     ps ;
    HDC             hDC ;

    switch (wMessage)                       /* process windows messages */
    {
        case WM_COMMAND:
            switch (LOWORD(wParam))         /* menu items */
            {
                case IDM_QUIT:
                    DestroyWindow (hWnd) ;  /* destory window, */
                    break ;                 /* terminating application */
            }
            break ;
        case WM_PAINT:
            hDC = BeginPaint (hWnd, &ps) ;
            SetTextColor (hDC, RGB (0, 0, 255)) ;    /* blue letters */
            TextOut (hDC, 0, 0, cBuf, lstrlen (cBuf)) ;
            EndPaint (hWnd, &ps) ;
            break ;
        case WM_CHAR:                         /* char input */
          if (nCurPos < LINELONG - 1 &&       /* or punctuation */
           (IsCharAlphaNumeric ((char)wParam) || IsAnsiPunc ((char)wParam)))
           {
                cBuf [nCurPos++] = (char) wParam ;  /* add new letter */
                cBuf [nCurPos] = 0 ;                /* null term. string */
                InvalidateRect (hWnd, NULL, TRUE) ; /* force WM_PAINT */
           }
            break ;
        case WM_KEYDOWN:                        /* non ANSI key input */
            switch (wParam)
            {
                case VK_BACK:                   /* backspace key */
                    if (nCurPos > 0)
                    {
                        nCurPos-- ;            /* back up one char */
                        cBuf [nCurPos] = 0 ;   /* replace with null */
                        InvalidateRect (hWnd, NULL, TRUE) ;
                    }
            }
            break ;
        case WM_DESTROY:                        /* stop application */
            PostQuitMessage (0) ;
            break ;
        default:        /* default windows message processing */
            return DefWindowProc (hWnd, wMessage, wParam, lParam) ;
    }
    return (0) ;
}

BOOL IsAnsiPunc (WORD wChar) /* returns true if wChar is punctuation char */
{
    if ((wChar >= ' ' && wChar <= '@') || (wChar >= '[' && wChar <= '`') ||
        (wChar >= '{' && wChar <= '~') || (wChar >= 0xA0 && wChar <= 0xBF))
        return (TRUE) ;
    else
        return (FALSE) ;
}