home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP05 / TYPER.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  9.5 KB  |  273 lines

  1. /*--------------------------------------
  2.    TYPER.C -- Typing Program
  3.               (c) Charles Petzold, 1996
  4.   --------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #include <stdlib.h>
  8.  
  9. #define BUFFER(x,y) *(pBuffer + y * cxBuffer + x)
  10.  
  11. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  12.  
  13. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  14.                     PSTR szCmdLine, int iCmdShow)
  15.      {
  16.      static char szAppName[] = "Typer" ;
  17.      HWND        hwnd ;
  18.      MSG         msg ;
  19.      WNDCLASSEX  wndclass ;
  20.  
  21.      wndclass.cbSize        = sizeof (wndclass) ;
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      wndclass.hIconSm       = LoadIcon (NULL, IDI_APPLICATION) ;
  33.  
  34.      RegisterClassEx (&wndclass) ;
  35.  
  36.      hwnd = CreateWindow (szAppName, "Typing Program",
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.  
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.  
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.           {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.           }
  50.      return msg.wParam ;
  51.      }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
  54.      {
  55.      static char *pBuffer = NULL ;
  56.      static int  cxChar, cyChar, cxClient, cyClient, cxBuffer, cyBuffer,
  57.                  xCaret, yCaret ;
  58.      HDC         hdc ;
  59.      int         x, y, i ;
  60.      PAINTSTRUCT ps ;
  61.      TEXTMETRIC  tm ;
  62.  
  63.      switch (iMsg)
  64.           {
  65.           case WM_CREATE :
  66.                hdc = GetDC (hwnd) ;
  67.  
  68.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  69.                GetTextMetrics (hdc, &tm) ;
  70.                cxChar = tm.tmAveCharWidth ;
  71.                cyChar = tm.tmHeight ;
  72.  
  73.                ReleaseDC (hwnd, hdc) ;
  74.                return 0 ;
  75.  
  76.           case WM_SIZE :
  77.                                    // obtain window size in pixels
  78.  
  79.                cxClient = LOWORD (lParam) ;
  80.                cyClient = HIWORD (lParam) ;
  81.  
  82.                                    // calculate window size in characters
  83.  
  84.                cxBuffer = max (1, cxClient / cxChar) ;
  85.                cyBuffer = max (1, cyClient / cyChar) ;
  86.  
  87.                                    // allocate memory for buffer and clear it
  88.  
  89.                if (pBuffer != NULL)
  90.                     free (pBuffer) ;
  91.  
  92.                if ((pBuffer = (char *) malloc (cxBuffer * cyBuffer)) == NULL)
  93.                     MessageBox (hwnd, "Window too large.  Cannot "
  94.                                       "allocate enough memory.", "Typer",
  95.                                       MB_ICONEXCLAMATION | MB_OK) ;
  96.                else
  97.                     for (y = 0 ; y < cyBuffer ; y++)
  98.                          for (x = 0 ; x < cxBuffer ; x++)
  99.                               BUFFER(x,y) = ' ' ;
  100.  
  101.                                    // set caret to upper left corner
  102.                xCaret = 0 ;
  103.                yCaret = 0 ;
  104.  
  105.                if (hwnd == GetFocus ())
  106.                     SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  107.  
  108.                return 0 ;
  109.  
  110.           case WM_SETFOCUS :
  111.                                    // create and show the caret
  112.  
  113.                CreateCaret (hwnd, NULL, cxChar, cyChar) ;
  114.                SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  115.                ShowCaret (hwnd) ;
  116.                return 0 ;
  117.  
  118.           case WM_KILLFOCUS :
  119.                                    // hide and destroy the caret
  120.                HideCaret (hwnd) ;
  121.                DestroyCaret () ;
  122.                return 0 ;
  123.  
  124.           case WM_KEYDOWN :
  125.                switch (wParam)
  126.                     {
  127.                     case VK_HOME :
  128.                          xCaret = 0 ;
  129.                          break ;
  130.  
  131.                     case VK_END :
  132.                          xCaret = cxBuffer - 1 ;
  133.                          break ;
  134.  
  135.                     case VK_PRIOR :
  136.                          yCaret = 0 ;
  137.                          break ;
  138.  
  139.                     case VK_NEXT :
  140.                          yCaret = cyBuffer - 1 ;
  141.                          break ;
  142.  
  143.                     case VK_LEFT :
  144.                          xCaret = max (xCaret - 1, 0) ;
  145.                          break ;
  146.  
  147.                     case VK_RIGHT :
  148.                          xCaret = min (xCaret + 1, cxBuffer - 1) ;
  149.                          break ;
  150.  
  151.                     case VK_UP :
  152.                          yCaret = max (yCaret - 1, 0) ;
  153.                          break ;
  154.  
  155.                     case VK_DOWN :
  156.                          yCaret = min (yCaret + 1, cyBuffer - 1) ;
  157.                          break ;
  158.  
  159.                     case VK_DELETE :
  160.                          for (x = xCaret ; x < cxBuffer - 1 ; x++)
  161.                               BUFFER (x, yCaret) = BUFFER (x + 1, yCaret) ;
  162.  
  163.                          BUFFER (cxBuffer - 1, yCaret) = ' ' ;
  164.  
  165.                          HideCaret (hwnd) ;
  166.                          hdc = GetDC (hwnd) ;
  167.  
  168.                          SelectObject (hdc,
  169.                               GetStockObject (SYSTEM_FIXED_FONT)) ;
  170.  
  171.                          TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
  172.                                   & BUFFER (xCaret, yCaret),
  173.                                   cxBuffer - xCaret) ;
  174.  
  175.                          ShowCaret (hwnd) ;
  176.                          ReleaseDC (hwnd, hdc) ;
  177.                          break ;
  178.                     }
  179.  
  180.                SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  181.                return 0 ;
  182.  
  183.           case WM_CHAR :
  184.                for (i = 0 ; i < (int) LOWORD (lParam) ; i++)
  185.                     {
  186.                     switch (wParam)
  187.                          {
  188.                          case '\b' :                    // backspace
  189.                               if (xCaret > 0)
  190.                                    {
  191.                                    xCaret-- ;
  192.                                    SendMessage (hwnd, WM_KEYDOWN,
  193.                                                 VK_DELETE, 1L) ;
  194.                                    }
  195.                               break ;
  196.  
  197.                          case '\t' :                    // tab
  198.                               do
  199.                                    {
  200.                                    SendMessage (hwnd, WM_CHAR, ' ', 1L) ;
  201.                                    }
  202.                               while (xCaret % 8 != 0) ;
  203.                               break ;
  204.  
  205.                          case '\n' :                    // line feed
  206.                               if (++yCaret == cyBuffer)
  207.                                    yCaret = 0 ;
  208.                               break ;
  209.  
  210.                          case '\r' :                    // carriage return
  211.                               xCaret = 0 ;
  212.  
  213.                               if (++yCaret == cyBuffer)
  214.                                    yCaret = 0 ;
  215.                               break ;
  216.  
  217.                          case '\x1B' :                  // escape
  218.                               for (y = 0 ; y < cyBuffer ; y++)
  219.                                    for (x = 0 ; x < cxBuffer ; x++)
  220.                                         BUFFER (x, y) = ' ' ;
  221.  
  222.                               xCaret = 0 ;
  223.                               yCaret = 0 ;
  224.  
  225.                               InvalidateRect (hwnd, NULL, FALSE) ;
  226.                               break ;
  227.  
  228.                          default :                       // character codes
  229.                               BUFFER (xCaret, yCaret) = (char) wParam ;
  230.  
  231.                               HideCaret (hwnd) ;
  232.                               hdc = GetDC (hwnd) ;
  233.  
  234.                               SelectObject (hdc,
  235.                                    GetStockObject (SYSTEM_FIXED_FONT)) ;
  236.  
  237.                               TextOut (hdc, xCaret * cxChar, yCaret * cyChar,
  238.                                        & BUFFER (xCaret, yCaret), 1) ;
  239.  
  240.                               ShowCaret (hwnd) ;
  241.                               ReleaseDC (hwnd, hdc) ;
  242.  
  243.                               if (++xCaret == cxBuffer)
  244.                                    {
  245.                                    xCaret = 0 ;
  246.  
  247.                                    if (++yCaret == cyBuffer)
  248.                                         yCaret = 0 ;
  249.                                    }
  250.                               break ;
  251.                          }
  252.                     }
  253.  
  254.                SetCaretPos (xCaret * cxChar, yCaret * cyChar) ;
  255.                return 0 ;
  256.  
  257.           case WM_PAINT :
  258.                hdc = BeginPaint (hwnd, &ps) ;
  259.                SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
  260.  
  261.                for (y = 0 ; y < cyBuffer ; y++)
  262.                     TextOut (hdc, 0, y * cyChar, & BUFFER(0,y), cxBuffer) ;
  263.  
  264.                EndPaint (hwnd, &ps) ;
  265.                return 0 ;
  266.  
  267.           case WM_DESTROY :
  268.                PostQuitMessage (0) ;
  269.                return 0 ;
  270.           }
  271.      return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
  272.      }
  273.