home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / netds / rpc / mazelord / textwnd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-07  |  6.2 KB  |  249 lines

  1. /***********************************************************************
  2. File:   TextWnd.c
  3.  
  4. Abstract:
  5.     This module contains functions which deal with the Text window. This
  6.     includes a function to print to said window, and scroll.
  7.  
  8.  
  9.  
  10. Contents:
  11.  
  12.     RefreshText() -- Redraws text in the bottom of the Text message window
  13.     lstrdup() -- same as strdup() but for LPSTR's
  14.     PrintTextLine() -- Prints a string to the text message window, scrolls
  15.     TextWndProc() -- Windows message processing loop for text message window
  16.  
  17.  
  18. ************************************************************************/
  19.  
  20. #include "winmaze.h"
  21. #include "mazproto.h"
  22. #include "net.h"
  23. #include <mmsystem.h>
  24. #include <string.h>
  25.  
  26.  
  27. #define MAX_TEXT_LINES 80
  28.  
  29. LPSTR cText[MAX_TEXT_LINES];
  30. TEXTMETRIC tmTextInfo;
  31. int iMaxNumLines,       // Number of lines that will fit in the TextWnd display.
  32.     iCurLine,           // Line # in the text display we're on.
  33.     iTextLines;         // # of lines of text which are valid in cText.
  34.  
  35.  
  36. /*=====================================================================
  37. Function: RefreshText()
  38.  
  39. Inputs: none
  40.  
  41. Outputs: none
  42.  
  43. Abstract:
  44.     RefreshText() is responsible for drawing the bottom n lines of the
  45.     display, however many can fit into it. This should only be called when
  46.     a clipping region is in effect and has already been cleared.
  47. ======================================================================*/
  48.  
  49. void RefreshText(
  50.     )
  51. {
  52.     HDC hDC;
  53.     int i;
  54.  
  55.     hDC = GetDC(hWndText);
  56.     iCurLine = 0;
  57.     for(i=0;i<iMaxNumLines;i++) {
  58.         if ((iTextLines -(iMaxNumLines-i)) >= 0) {
  59.             TextOut(hDC,10,iCurLine * tmTextInfo.tmHeight + 2,
  60.                     cText[iTextLines-(iMaxNumLines-i)],
  61.                     lstrlen(cText[iTextLines-(iMaxNumLines-i)]));
  62.             iCurLine++;
  63.             }
  64.         }
  65.  
  66.     ReleaseDC(hWndText,hDC);
  67. }
  68.  
  69.  
  70.  
  71. /*=====================================================================
  72. Function: lstrdup()
  73.  
  74. Inputs: String to duplicate
  75.  
  76. Outputs:Returns a copy of the string.
  77.  
  78. Abstract:
  79.     Same as strdup, but works with LPSTR's
  80. ======================================================================*/
  81.  
  82. LPSTR lstrdup(
  83.     LPSTR s
  84.     )
  85. {
  86.     HGLOBAL hMem;
  87.     LPSTR lpRet;
  88.  
  89.     hMem = GlobalAlloc(GHND,lstrlen(s)+1);
  90.     lpRet = GlobalLock(hMem);
  91.     lstrcpy(lpRet,s);
  92.  
  93.     return(lpRet);
  94. }
  95.  
  96.  
  97.  
  98.  
  99. /*=====================================================================
  100. Function: PrintTextLine()
  101.  
  102. Inputs: Line of text to print
  103.  
  104. Outputs: none
  105.  
  106. Abstract:
  107.     PrintTextLine will print a line of text to the text display window, and
  108.     scroll as necessary.
  109. ======================================================================*/
  110.  
  111. void PrintTextLine(
  112.     LPSTR cLine
  113.     )
  114. {
  115.     HDC hDC;
  116.     int i;
  117.     RECT rScroll;
  118.     HGLOBAL hMem;
  119.  
  120.     hDC = GetDC(hWndText);
  121.  
  122.     //
  123.     // If we already have MAX_TEXT_LINES of text remembered,
  124.     // we need to zap one to make room for the new line.
  125.     //
  126.     if (iTextLines == MAX_TEXT_LINES) {
  127.         hMem = (HGLOBAL) GlobalHandle(SELECTOROF(cText[0]));
  128.         GlobalUnlock(hMem);
  129.         GlobalFree(hMem);
  130.         for(i=0;i<MAX_TEXT_LINES-1;i++) {
  131.             cText[i] = cText[i+1];
  132.             }
  133.         iTextLines--;
  134.         }
  135.  
  136.     //
  137.     // Copy the line to be displayed into our buffer
  138.     //
  139.     cText[iTextLines++] = lstrdup(cLine);
  140.  
  141.     //
  142.     // Scroll if necessary
  143.     //
  144.     if (iCurLine >= iMaxNumLines) {
  145.         GetClientRect(hWndText,&rScroll);
  146.         rScroll.top += 2;
  147.         ScrollWindow(hWndText,0,-tmTextInfo.tmHeight,&rScroll,&rScroll);
  148.         UpdateWindow(hWndText);
  149.         iCurLine--;
  150.         }
  151.  
  152.     TextOut(hDC,10,iCurLine * tmTextInfo.tmHeight + 2,
  153.             cText[iTextLines-1],
  154.             lstrlen(cText[iTextLines-1]));
  155.  
  156.     iCurLine++;
  157.  
  158.     ReleaseDC(hWndText,hDC);
  159.  
  160.     return;
  161. }
  162.  
  163.  
  164. /*=====================================================================
  165. Function: TextWndProc()
  166.  
  167. Inputs: Standard windows entrypoint parms
  168.  
  169. Outputs: success
  170.  
  171. Abstract:
  172.     This is the main procedure to take care of drawing, resizing etc.
  173.     the text message window.
  174. ======================================================================*/
  175.  
  176.  
  177. LONG FAR PASCAL TextWndProc(
  178.     HWND hWnd,
  179.     UINT Message,
  180.     WPARAM wParam,
  181.     LPARAM lParam
  182.     )
  183. {
  184.     PAINTSTRUCT ps;
  185.     HDC hDC;
  186.     int i;
  187.     HGLOBAL hMem;
  188.  
  189.     switch (Message) {
  190.  
  191.         case WM_CREATE:
  192.             for(i=0;i<MAX_TEXT_LINES;i++) {
  193.                 cText[i] = (LPSTR) NULL;
  194. //                hMem = GlobalAlloc(GHND,sizeof(char));
  195. //                cText[i] = GlobalLock(hMem);
  196. //                cText[i][0] = '\0';
  197.                 }
  198.  
  199.             iTextLines = iCurLine = 0;
  200.             hDC = GetDC(hWnd);
  201.             if(!GetTextMetrics(hDC,&tmTextInfo)) {
  202.                 MessageBox((HWND)NULL,GetStringRes(IDS_GETTXTMTRCSFAIL),
  203.                            "TextWndProc",MB_ICONEXCLAMATION|MB_APPLMODAL);
  204.                 }
  205.             ReleaseDC(hWnd,hDC);
  206.             GetClientRect(hWnd,&rText);
  207.             iMaxNumLines = (rText.bottom - rText.top-5)/tmTextInfo.tmHeight;
  208.             break;
  209.  
  210.         case WM_KEYDOWN:
  211.             SendMessage(hWndMaze,WM_KEYDOWN,wParam,lParam);
  212.             break;
  213.  
  214.         case WM_MOVE:
  215.             break;
  216.  
  217.         case WM_SIZE:
  218.             GetClientRect(hWnd,&rText);
  219.             iMaxNumLines = (rText.bottom - rText.top - 5)/tmTextInfo.tmHeight;
  220.             RefreshText();
  221.             break;
  222.  
  223.         case WM_PAINT:
  224.             GetClientRect(hWnd,&rText);
  225.             hDC = BeginPaint(hWnd, &ps);
  226.             SetBkMode(hDC, OPAQUE);
  227.             RefreshText();
  228.             EndPaint(hWnd, &ps);
  229.             break;
  230.  
  231.         case WM_CLOSE:
  232.             for (i=0; i<iTextLines ; i++) {
  233.                 if (cText[i] != NULL) {
  234.                     hMem = (HGLOBAL) GlobalHandle(SELECTOROF(cText[i]));
  235.                     GlobalUnlock(hMem);
  236.                     GlobalFree(hMem);
  237.                 }
  238.             }
  239.             DestroyWindow(hWnd);
  240.             break;
  241.  
  242.         default:
  243.             return DefWindowProc(hWnd, Message, wParam, lParam);
  244.         }
  245.  
  246.     return(0);
  247. }
  248.  
  249.