home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_06 / 2n06050a < prev    next >
Text File  |  1991-05-01  |  23KB  |  902 lines

  1. /**********************************************************
  2.     PURPOSE: Links with a Standard C program to
  3.              provide a Windows Application envirnment.
  4.  
  5.  
  6.     FUNCTIONS:
  7.  
  8.         WinMain() - calls initialization function,
  9.             processes message loop
  10.         StdWinInit() - initializes window data and
  11.             registers window
  12.         StdWinWndProc() - processes messages
  13.         About() - processes messages for "About" dialog box
  14.         SelectFont() - select a font
  15.         GetSizes() - get size of current font
  16.         GetFonts() - get available fonts
  17.         SetMyDC() - initializes DC
  18.  
  19.     COMMENTS:   This code started as the Showfont sample
  20.         software that is included in the Windows Software
  21.         Development Kit. Compile this file as a small or
  22.         medium model with the following options:
  23.             CL /A[S or M] /c /Gsw /Oas /Zp -Zi STDWIN.C
  24.  
  25.         The Zi option is only if you are able to use
  26.         CodeView for Windows.
  27.  
  28.         When you link, you must link with the C6.0 linker
  29.         (Version 5.1 or greater), or link4 to get the proper
  30.         exe. You must link with the libraries that come with
  31.         the Windows Software Development kit, not the C
  32.         compiler libraries. These libraries are:
  33.             LIBW.LIB and SLIBCEW.LIB or MLIBCEW.LIB
  34.  
  35.         After linking you must resource compile the
  36.         executable with the resource script. First:
  37.             rc -r stdwin.rc
  38.  
  39.         After linking combine the binary resource file
  40.         with the exe by:
  41.             rc stdwin.res
  42. ***********************************************************/
  43.  
  44. #include "windows.h"
  45. #include "StdWin.h"
  46. #include <string.h>
  47. #include <stdio.h>
  48.  
  49.  
  50.  
  51. /* set this to your own title */
  52. char WindowTitle[] = "Grep";
  53. WORD LineExtents[MAXROWS+2];
  54. HANDLE hInst,hStdWinWnd;
  55. HDC SetMyDC(HDC) ;
  56. HFONT hFont1;
  57. HFONT hUnderlineFont = 0,hBoldFont = 0,hItalicFont;
  58. char FontNameList[32][128];     /* list of added fonts  */
  59. int nFontIndex = 0;             /* position in FontList */
  60. int  BreakSet;                  /* break key flag */
  61. BYTE KeyBuffer[64],KeyHead = 0,KeyTail = 0,KeyMask = 0x3f;
  62.  /* storage for screen text */
  63. SCREEN ScreenBuffer[MAXROWS+2][MAXCOLUMNS];
  64. int VisibleRows,VisibleCols,TopRow,LeftCol,VertScrollInc,
  65.     HorizScrollInc;
  66. TEXTMETRIC TextMetric;
  67. int AveCharacterWidth;
  68. LOGFONT LogFont;
  69. short nBkMode = OPAQUE;
  70. DWORD rgbBkColor = RGB(255, 255, 255);
  71. DWORD rgbTextColor = RGB(0, 0, 0);
  72. DWORD rgbColor;
  73. short nAlignLCR = TA_LEFT;
  74. short nAlignTBB = TA_TOP; 
  75. char FontList[MAXFONT][32];
  76. BYTE CharSet[MAXFONT];
  77. BYTE PitchAndFamily[MAXFONT];
  78. int FontIndex = 0;
  79. int SizeList[MAXSIZE];
  80. int SizeIndex = 0;
  81. int CurrentFont = 0;
  82. int CurrentSize = 0;
  83. int xCurrentPos=0,yCurrentPos =0;
  84. FARPROC lpColors;
  85. FARPROC lpSelectFont;
  86. FARPROC lpEnumFunc;
  87. CATCHBUF   CatchBuf;
  88. short DisplayCol = 0,DisplayLine = 0;
  89. RECT Rect;  /* dimension of the client window  */
  90. BYTE *ptr,*ptr2;
  91.  
  92. /*********************************************************
  93.  
  94.     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
  95.  
  96.     PURPOSE: calls initialization function,
  97.         processes message loop
  98.  
  99. ***********************************************************/
  100.  
  101. int PASCAL WinMain(hInstance, hPrevInstance,
  102.     lpCmdLine, nCmdShow)
  103. HANDLE hInstance;
  104. HANDLE hPrevInstance;
  105. LPSTR lpCmdLine;
  106. int nCmdShow;
  107. {
  108.     HWND hWnd;
  109.     MSG msg;
  110.  
  111.     if (!hPrevInstance)
  112.         if (!StdWinInit(hInstance))
  113.             return (FALSE);
  114.  
  115.     hInst = hInstance;
  116.  
  117.     hWnd = CreateWindow("StdWin",
  118.         WindowTitle,
  119.         WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
  120.         CW_USEDEFAULT,
  121.         CW_USEDEFAULT,
  122.         CW_USEDEFAULT,
  123.         CW_USEDEFAULT,
  124.         NULL,
  125.         NULL,
  126.         hInstance,
  127.         NULL);
  128.  
  129.     if (!hWnd)
  130.         return (FALSE);
  131.     hStdWinWnd = hWnd;
  132.     ShowWindow(hWnd, nCmdShow);
  133.     UpdateWindow(hWnd);
  134.  
  135.     while(PeekMessage(&msg,NULL,0,0xFFFF,PM_REMOVE)){
  136.         TranslateMessage(&msg);
  137.         DispatchMessage(&msg);
  138.     }
  139.     /* store process state for ctrl-c break */
  140.     if(!Catch(CatchBuf))
  141.         main();
  142.     MessageBox(hWnd,"Program Terminated Normally",
  143.         WindowTitle,MB_OK);
  144.     if(hOutFile)
  145.         _lclose(hOutFile);
  146.     hOutFile = Redirected = 0;
  147.  
  148.     while (GetMessage(&msg, NULL, NULL, NULL)) {
  149.         TranslateMessage(&msg);
  150.         DispatchMessage(&msg);}
  151.  
  152.     return (msg.wParam);
  153. }
  154.  
  155. /*********************************************************
  156.  
  157.     FUNCTION: StdWinInit(HANDLE)
  158.  
  159.     PURPOSE: Initializes window data and registers class
  160.  
  161. ***********************************************************/
  162.  
  163. int StdWinInit(hInstance)
  164. HANDLE hInstance;
  165. {
  166.     HANDLE hMemory;
  167.     PWNDCLASS pWndClass;
  168.     BOOL bSuccess;
  169.  
  170.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  171.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  172.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  173.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  174.     pWndClass->lpszMenuName = (LPSTR) "StdWin";
  175.     pWndClass->lpszClassName = (LPSTR) "StdWin";
  176.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  177.     pWndClass->hInstance = hInstance;
  178.     pWndClass->style = NULL;
  179.     pWndClass->lpfnWndProc = StdWinWndProc;
  180.  
  181.     bSuccess = RegisterClass((LPWNDCLASS) pWndClass);
  182.  
  183.     LocalUnlock(hMemory);
  184.     LocalFree(hMemory);
  185.     return (bSuccess);
  186. }
  187.  
  188. /**********************************************************
  189.  
  190.     FUNCTION: SetMyDC(HDC)
  191.  
  192.     PURPOSE: Initializes the DC
  193.  
  194. ***********************************************************/
  195.  
  196. HDC SetMyDC(hDC) 
  197. HDC hDC;
  198. {
  199.     SetBkMode(hDC, nBkMode);
  200.     SetBkColor(hDC, rgbBkColor);
  201.     SetTextColor(hDC, rgbTextColor);
  202.     SetTextAlign(hDC, nAlignLCR | nAlignTBB);
  203.     SelectObject(hDC,hFont1);
  204.     GetTextMetrics(hDC,&TextMetric);
  205. }
  206.  
  207.  
  208.  
  209. /**********************************************************
  210.  
  211.     FUNCTION: Colors(HWND, unsigned, WORD LONG)
  212.  
  213.     PURPOSE: Dialog box for changing text background color
  214.  
  215. ***********************************************************/
  216.  
  217. BOOL FAR PASCAL Colors(hDlg, message, wParam, lParam)
  218. HWND hDlg;
  219. unsigned message;
  220. WORD wParam;
  221. LONG lParam;
  222. {
  223.     int Red, Green, Blue;
  224.  
  225.     switch (message) {
  226.         case WM_INITDIALOG:
  227.             SetDlgItemInt(hDlg, ID_RED,
  228.                 GetRValue(rgbColor), FALSE);
  229.             SetDlgItemInt(hDlg, ID_GREEN,
  230.                 GetGValue(rgbColor), FALSE);
  231.             SetDlgItemInt(hDlg, ID_BLUE,
  232.                 GetBValue(rgbColor), FALSE);
  233.             return (TRUE);
  234.             break;
  235.  
  236.         case WM_COMMAND:
  237.             switch (wParam) {
  238.                 case IDOK:
  239.                     Red = GetDlgItemInt(hDlg, ID_RED,
  240.                         NULL, FALSE);
  241.                     Green = GetDlgItemInt(hDlg, ID_GREEN,
  242.                         NULL, FALSE);
  243.                     Blue = GetDlgItemInt(hDlg, ID_BLUE,
  244.                         NULL, FALSE);
  245.                     rgbColor = RGB(Red, Green, Blue);
  246.                     EndDialog(hDlg, 1);
  247.                     break;
  248.  
  249.                 case IDCANCEL:
  250.                     EndDialog(hDlg, 0);
  251.                     break;
  252.             }
  253.             break;
  254.     }
  255.     return (FALSE);
  256. }
  257.  
  258. /********************************************************
  259.  
  260.     FUNCTION: EnumFunc(LPLOGFONT,LPTEXTMETRIC,short,LPSTR)
  261.  
  262. **********************************************************/
  263.  
  264. int FAR PASCAL EnumFunc(lpLogFont, lpTextMetric,
  265.     FontType, lpData)
  266. LPLOGFONT lpLogFont;
  267. LPTEXTMETRIC lpTextMetric;
  268. short FontType;
  269. LPSTR lpData;
  270. {
  271.     switch (LOWORD(lpData)) {
  272.         case 0:
  273.             if (FontIndex >= MAXFONT)
  274.                 return (0);
  275.             _fstrcpy((LPSTR) FontList[FontIndex],
  276.                 (LPSTR) (lpLogFont->lfFaceName));
  277.             CharSet[FontIndex] = lpLogFont->lfCharSet;
  278.             PitchAndFamily[FontIndex] =
  279.                 lpLogFont->lfPitchAndFamily;
  280.             return (++FontIndex);
  281.  
  282.         case 1:
  283.             if (SizeIndex >= MAXSIZE)
  284.                 return (0);
  285.             SizeList[SizeIndex] = lpLogFont->lfHeight;
  286.             return (++SizeIndex);
  287.     }
  288. }
  289.  
  290. /*******************************************************
  291.  
  292.     FUNCTION: GetFonts(HWND)
  293.  
  294.     PURPOSE: Get available fonts
  295.  
  296. *********************************************************/
  297.  
  298. void GetFonts(hWnd)
  299. HWND hWnd;
  300. {
  301.  
  302.     HDC hDC;
  303.  
  304.     FontIndex = 0;
  305.     SizeIndex = 0;
  306.     hDC = GetDC(hWnd);
  307.     lpEnumFunc = MakeProcInstance(EnumFunc, hInst);
  308.     EnumFonts(hDC, (LPSTR) NULL, lpEnumFunc, (LPSTR) NULL);
  309.     FreeProcInstance(lpEnumFunc);
  310.     ReleaseDC(hWnd, hDC);
  311. }
  312.  
  313. /*********************************************************
  314.  
  315.     FUNCTION: GetSizes(hWnd, CurrentFont)
  316.  
  317.     PURPOSE: Get size of current font
  318.  
  319. **********************************************************/
  320.  
  321. void GetSizes(hWnd, CurrentFont)
  322. HWND hWnd;
  323. int CurrentFont;
  324. {
  325.     HDC hDC;
  326.  
  327.     SizeIndex = 0;
  328.     hDC = GetDC(hWnd);
  329.     lpEnumFunc = MakeProcInstance(EnumFunc, hInst);
  330.     EnumFonts(hDC, FontList[CurrentFont], lpEnumFunc,
  331.         (LPSTR) 1L);
  332.     FreeProcInstance(lpEnumFunc);
  333.     ReleaseDC(hWnd, hDC);
  334. }
  335.  
  336.  
  337. /********************************************************
  338.  
  339.     FUNCTION: SelectFont(HWND, unsigned, WORD, LONG)
  340.  
  341. **********************************************************/
  342.  
  343. BOOL FAR PASCAL SelectFont(hDlg, message, wParam, lParam)
  344. HWND hDlg;
  345. unsigned message;
  346. WORD wParam;
  347. LONG lParam;
  348. {
  349.  
  350.     int i;
  351.     int index;
  352.     char buf[LF_FACESIZE];
  353.  
  354.     switch (message) {
  355.         case WM_INITDIALOG:
  356.             /* displays available fonts */
  357.             for (i=0; i<FontIndex; i++) {
  358.                 SendDlgItemMessage(hDlg, ID_TYPEFACE,
  359.                     LB_ADDSTRING,NULL,
  360.                     (LONG) (LPSTR) FontList[i]);
  361.                 SendDlgItemMessage(hDlg, ID_TYPEFACE,
  362.                     LB_SETCURSEL,
  363.                     0, 0L);
  364.             }
  365.             GetSizes(hDlg, 0);
  366.             /* displays font sizes      */
  367.             for (i=0; i<SizeIndex; i++) {
  368.                 sprintf(buf, "%d", SizeList[i]);
  369.                 SendDlgItemMessage(hDlg, ID_SIZE,
  370.                     LB_ADDSTRING,
  371.                     0, (LONG) (LPSTR) buf);
  372.                 SendDlgItemMessage(hDlg, ID_SIZE,
  373.                     LB_SETCURSEL,
  374.                     0, 0L);
  375.             }
  376.             return (TRUE);
  377.             break;
  378.  
  379.         case WM_COMMAND:
  380.             switch (wParam) {
  381.                 case IDOK:
  382. okay:
  383.                     index=SendDlgItemMessage(hDlg,
  384.                         ID_TYPEFACE,LB_GETCURSEL, 0, 0L);
  385.                     if (index == LB_ERR) {
  386.                         MessageBox(hDlg, "No font selected",
  387.                             "Select Font",
  388.                             MB_OK | MB_ICONEXCLAMATION);
  389.                     break;
  390.             }
  391.             CurrentFont = index;
  392.             index = SendDlgItemMessage(hDlg, ID_SIZE,
  393.                 LB_GETCURSEL, 0, 0L);
  394.             if (index == LB_ERR) {
  395.                 MessageBox(hDlg, "No size selected",
  396.                     "Select Font",
  397.                     MB_OK | MB_ICONEXCLAMATION);
  398.                 break;
  399.             }
  400.             CurrentSize = index;
  401.             EndDialog(hDlg, 1);
  402.             break;
  403.  
  404.         case IDCANCEL:
  405.             EndDialog(hDlg, 0);
  406.             break;
  407.  
  408.         case ID_TYPEFACE:
  409.             switch (HIWORD(lParam)) {
  410.                 case LBN_SELCHANGE:
  411.                     index = SendDlgItemMessage(hDlg,
  412.                         ID_TYPEFACE,
  413.                         LB_GETCURSEL, 0, 0L);
  414.                     if (index == LB_ERR)
  415.                         break;
  416.                     SendDlgItemMessage(hDlg, ID_SIZE,
  417.                         LB_RESETCONTENT, 0, 0L);
  418.                     GetSizes(hDlg, index);
  419.                     for (i = 0; i < SizeIndex; i++) {
  420.                         sprintf(buf, "%d", SizeList[i]);
  421.                         SendDlgItemMessage(hDlg, ID_SIZE,
  422.                             LB_ADDSTRING, 0,
  423.                             (LONG) (LPSTR) buf);
  424.                         SendDlgItemMessage(hDlg,
  425.                             ID_SIZE, LB_SETCURSEL, 0, 0L);
  426.             }
  427.             break;
  428.  
  429.                 case LBN_DBLCLK:
  430.                 goto okay;
  431.                 break;
  432.             }
  433.             break;
  434.  
  435.         case ID_SIZE:
  436.             if(HIWORD(lParam) == LBN_DBLCLK)
  437.                 goto okay;
  438.             break;
  439.         }
  440.         break;
  441.     }
  442.     return (FALSE);
  443. }
  444.  
  445. /*********************************************************
  446.  create special fonts
  447.  *********************************************************/
  448. void GetUnderlineFont(void)
  449. {
  450.  if(hUnderlineFont)
  451.     DeleteObject(hUnderlineFont);
  452.  GetObject(hFont1, sizeof(LOGFONT), (LPSTR) &LogFont);
  453.  LogFont.lfUnderline = TRUE;
  454.  hUnderlineFont = CreateFontIndirect(&LogFont);
  455. }
  456.  
  457. void GetBoldFont(void)
  458. {
  459.  if(hBoldFont)
  460.     DeleteObject(hBoldFont);
  461.  GetObject(hFont1, sizeof(LOGFONT), (LPSTR) &LogFont);
  462.  LogFont.lfWeight = 700;
  463.  hBoldFont = CreateFontIndirect(&LogFont);
  464. }
  465.  
  466. void GetItalicFont(void)
  467. {
  468.  if(hItalicFont)
  469.     DeleteObject(hItalicFont);
  470.  GetObject(hFont1, sizeof(LOGFONT), (LPSTR) &LogFont);
  471.  LogFont.lfItalic = TRUE;
  472.  hItalicFont = CreateFontIndirect(&LogFont);
  473. }
  474.  
  475. /********************************************************
  476.  
  477.     FUNCTION: StdWinWndProc(HWND, unsigned, WORD, LONG)
  478.  
  479.     PURPOSE: Processes messages
  480.  
  481. *********************************************************/
  482.  
  483. long FAR PASCAL StdWinWndProc(hWnd, message, wParam, lParam)
  484. HWND hWnd;
  485. unsigned message;
  486. WORD wParam;
  487. LONG lParam;
  488. {
  489.     FARPROC lpProcAbout;
  490.     HDC hDC;
  491.     PAINTSTRUCT ps;
  492.     HFONT hOldFont;
  493.     int i;
  494.     short Y;
  495.  
  496.     switch(message) {
  497.         case WM_CREATE:
  498.             GetFonts(hWnd);
  499.             hFont1 = CreateFont(
  500.                 12,          /* height           */
  501.                 0,           /* width            */
  502.                 0,           /* escapement       */
  503.                 0,           /* orientation      */
  504.                 400,         /* weight           */
  505.                 FALSE,       /* italic           */
  506.                 FALSE,       /* underline        */
  507.                 FALSE,       /* strikeout        */
  508.                 0,           /* charset          */
  509.                 0,           /* out precision    */
  510.                 0,           /* clip precision   */
  511.                 0,           /* quality          */
  512.                 0x31,        /* pitch and family */
  513.                 "courier");  /* typeface         */
  514.             GetUnderlineFont();
  515.             GetBoldFont();
  516.             GetItalicFont();
  517.             hDC = GetDC(hWnd);
  518.             SetMyDC(hDC);
  519.             /* initialize the screenbuffer array */
  520.             for(TopRow = 0;TopRow < MAXROWS;TopRow++)
  521.                 {
  522.                 for(LeftCol = 0;LeftCol < MAXCOLUMNS;
  523.                     LeftCol++)
  524.                     {
  525.                     ScreenBuffer[TopRow][LeftCol].hFont =
  526.                         hFont1;
  527.                     ScreenBuffer[TopRow][LeftCol].Char = 0;
  528.                     ScreenBuffer[TopRow][LeftCol].xExtent =
  529.                             TextMetric.tmAveCharWidth;
  530.                     ScreenBuffer[TopRow][LeftCol].Color=0L;
  531.                     }
  532.                 LineExtents[TopRow] = TextMetric.tmHeight +
  533.                     TextMetric.tmExternalLeading +
  534.                     TextMetric.tmInternalLeading;
  535.                 }
  536.             TopRow = LeftCol = 0;
  537.             GetClientRect(hWnd,&Rect);
  538.             VisibleRows = min(Rect.bottom / LineExtents[0],
  539.                 MAXROWS);
  540.             VisibleCols = min(Rect.right /
  541.                 TextMetric.tmAveCharWidth,
  542.                 MAXCOLUMNS);
  543.             SetScrollRange(hWnd,SB_VERT,0,
  544.                 MAXROWS-VisibleRows,FALSE);
  545.             SetScrollRange(hWnd,SB_HORZ,0,
  546.                 MAXCOLUMNS-VisibleCols,FALSE);
  547.             SetScrollPos(hWnd,SB_VERT,0,TRUE);
  548.             SetScrollPos(hWnd,SB_HORZ,0,TRUE);
  549.             ReleaseDC(hWnd,hDC);
  550.             break;
  551.  
  552.         case WM_PAINT:
  553.             {
  554.             int column,row;
  555.             int y=0,x=0;
  556.             HFONT hCurFont =
  557.                 ScreenBuffer[LeftCol][TopRow].hFont;
  558.             COLORREF CurColor =
  559.                 ScreenBuffer[LeftCol][TopRow].Color;
  560.             BYTE Buffer[MAXCOLUMNS + 10];
  561.  
  562.             hDC = BeginPaint(hWnd, &ps);
  563.             ptr = Buffer;
  564.             /* anything to print */
  565.             if(DisplayLine || DisplayCol)
  566.             {
  567.             hOldFont = SelectObject(hDC, hCurFont);
  568.             HideCaret(hWnd);
  569.             row = 0;
  570.             for(row = TopRow;row <= DisplayLine; row++)
  571.                 {
  572.                 Buffer[0] = 0;
  573.                 ptr = Buffer;
  574.                 for(column = LeftCol;
  575.                     column < MAXCOLUMNS;
  576.                     column++)
  577.                     { /* if the font or color changes
  578.                          mid line, print */
  579.                     if(((hCurFont !=
  580.                         ScreenBuffer[row][column].hFont) ||
  581.                         (CurColor !=
  582.                         ScreenBuffer[row][column].Color)) &&
  583.                             Buffer[0])
  584.                         {
  585.                         SelectObject(hDC, hCurFont);
  586.                         SetTextColor(hDC, CurColor);
  587.                         TextOut(hDC,x,y,Buffer,ptr-Buffer);
  588.                         Buffer[ptr-Buffer] = 0;
  589.                         x += LOWORD(GetTextExtent(hDC,Buffer,
  590.                             ptr-Buffer));
  591.                         ptr = Buffer;
  592.                         Buffer[0] = 0;
  593.                         hCurFont =
  594.                             ScreenBuffer[row][column].hFont;
  595.                         CurColor =
  596.                             ScreenBuffer[row][column].Color;
  597.                         }
  598.                     if(ScreenBuffer[row][column].Char)
  599.                     /* build string of equal color font */
  600.                         {
  601.                         *ptr++ =
  602.                             ScreenBuffer[row][column].Char;
  603.                         hCurFont =
  604.                             ScreenBuffer[row][column].hFont;
  605.                         CurColor =
  606.                             ScreenBuffer[row][column].Color;
  607.                         }
  608.                     else column = MAXCOLUMNS;
  609.                     }   /* for column */
  610.                 /* if we haven't printed this line */
  611.                 if(Buffer[0])
  612.                     {
  613.                     SelectObject(hDC, hCurFont);
  614.                     SetTextColor(hDC, CurColor);
  615.                     TextOut(hDC,x,y,Buffer,ptr-Buffer);
  616.                     }
  617.                 y += LineExtents[row];
  618.                 x=0;
  619.                 }
  620.  
  621.             ShowCaret(hWnd);
  622.             SelectObject(hDC, hOldFont);
  623.             }
  624.             EndPaint(hWnd, &ps);
  625.             }
  626.             break;
  627.  
  628.         case WM_VSCROLL:
  629.             switch(wParam){
  630.                 case SB_LINEUP:
  631.                     VertScrollInc = -1;
  632.                     break;
  633.                 case SB_LINEDOWN:
  634.                     VertScrollInc = 1;
  635.                     break;
  636.                 case SB_PAGEUP:
  637.                     break;
  638.                 case SB_PAGEDOWN:
  639.                     break;
  640.                 case SB_THUMBTRACK:
  641.                     VertScrollInc = LOWORD(lParam)-TopRow;
  642.                     break;
  643.                 case SB_TOP:
  644.                     VertScrollInc =  -TopRow;
  645.                     break;
  646.                 case SB_BOTTOM:
  647.                     VertScrollInc =
  648.                         (MAXROWS- VisibleRows) - TopRow;
  649.                     break;
  650.                 default:
  651.                     break;
  652.             }
  653.             if(VertScrollInc = max(-TopRow,
  654.                 min(VertScrollInc,
  655.                     MAXROWS-VisibleRows - TopRow)))
  656.                 {
  657.                 ScrollWindow(hWnd,0,(LineExtents[0] *
  658.                     -VertScrollInc),NULL,NULL);
  659.                 TopRow += VertScrollInc;
  660.                 UpdateWindow(hWnd);
  661.                 SetScrollPos(hWnd,SB_VERT,TopRow,TRUE);
  662.                 }
  663.             return(0);
  664.  
  665.         case WM_HSCROLL:
  666.             switch(wParam){
  667.                 case SB_LINEUP:
  668.                     HorizScrollInc = -1;
  669.                     break;
  670.                 case SB_LINEDOWN:
  671.                     HorizScrollInc = 1;
  672.                     break;
  673.                 case SB_PAGEUP:
  674.                     break;
  675.                 case SB_PAGEDOWN:
  676.                     break;
  677.                 case SB_THUMBTRACK:
  678.                     HorizScrollInc = LOWORD(lParam)-LeftCol;
  679.                     break;
  680.                 default:
  681.                     break;
  682.             }
  683.             HorizScrollInc =
  684.                     max(-LeftCol,min(HorizScrollInc,
  685.                     MAXCOLUMNS-VisibleCols - LeftCol));
  686.             if(HorizScrollInc)
  687.                 {
  688.                 ScrollWindow(hWnd,(TextMetric.tmAveCharWidth
  689.                     * -HorizScrollInc),0,NULL,NULL);
  690.                 LeftCol += HorizScrollInc;
  691.                 UpdateWindow(hWnd);
  692.                 SetScrollPos(hWnd,SB_HORZ,LeftCol,TRUE);
  693.                 }
  694.             return(0);
  695.         case WM_COMMAND:
  696.             switch (wParam) {
  697.  
  698.                 /* Run menu */
  699.                 case IDM_RESTART:
  700.                     /* Throw the processor state
  701.                         back to re-execute main() */
  702.                     Throw(CatchBuf,0);
  703.                     break;
  704.                 case IDM_EXIT:
  705.                     /* restore our message loop and
  706.                         take control from main */
  707.                     DestroyWindow(hWnd);
  708.                     Throw(CatchBuf,1);
  709.                     break;
  710.  
  711.                 case IDM_ABOUT:
  712.                     lpProcAbout =
  713.                         MakeProcInstance((FARPROC) About,
  714.                             hInst);
  715.                     DialogBox(hInst, "AboutBox", hWnd,
  716.                             lpProcAbout);
  717.                     FreeProcInstance(lpProcAbout);
  718.                     break;
  719.  
  720.  
  721.  
  722.                 case IDM_SELECTFONT:
  723.                     lpSelectFont =
  724.                         MakeProcInstance(SelectFont, hInst);
  725.                     if (DialogBox(hInst, "SelectFont",
  726.                         hWnd, lpSelectFont)) {
  727.                         hFont1 = CreateFont(
  728.                             SizeList[CurrentSize],
  729.                             0,
  730.                             0,
  731.                             0,
  732.                             FW_NORMAL,
  733.                             FALSE,
  734.                             FALSE,
  735.                             FALSE,
  736.                             CharSet[CurrentFont],
  737.                             OUT_DEFAULT_PRECIS,
  738.                             CLIP_DEFAULT_PRECIS,
  739.                             DEFAULT_QUALITY,
  740.                             PitchAndFamily[CurrentFont],
  741.                             FontList[CurrentFont]);
  742.                         GetUnderlineFont();
  743.                         GetBoldFont();
  744.                         GetItalicFont();
  745.                         hDC = GetDC(hWnd);
  746.                         HideCaret(hWnd);
  747.                         DestroyCaret();
  748.                         SelectObject(hDC,hFont1);
  749.                         GetTextMetrics(hDC,&TextMetric);
  750.                         CreateCaret(hWnd,NULL,1,
  751.                             TextMetric.tmHeight);
  752.                         SetCaretPos(xCurrentPos,
  753.                             yCurrentPos);
  754.                         ShowCaret(hWnd);
  755.                         ReleaseDC(hWnd,hDC);
  756.                         }
  757.                     FreeProcInstance(lpSelectFont);
  758.                     break;
  759.  
  760.  
  761.                 /* Options menu */
  762.  
  763.                 case IDM_TEXTCOLOR:
  764.                     lpColors =
  765.                         MakeProcInstance(Colors, hInst);
  766.                     rgbColor = rgbTextColor;
  767.                     if (DialogBox(hInst, "Colors", hWnd,
  768.                         lpColors))
  769.                         rgbTextColor = rgbColor;
  770.                     FreeProcInstance(lpColors);
  771.                     break;
  772.  
  773.                 case IDM_BACKGROUNDCOLOR:
  774.                     lpColors =
  775.                         MakeProcInstance(Colors, hInst);
  776.                     rgbColor = rgbBkColor;
  777.                     if (DialogBox(hInst, "Colors",
  778.                         hWnd, lpColors))
  779.                         rgbBkColor = rgbColor;
  780.                     FreeProcInstance(lpColors);
  781.                     break;
  782.  
  783.                 case IDM_OPAQUE:
  784.                     nBkMode = OPAQUE;
  785.                     CheckMenuItem(GetMenu(hWnd),
  786.                         IDM_TRANSPARENT, MF_UNCHECKED);
  787.                     CheckMenuItem(GetMenu(hWnd),
  788.                         IDM_OPAQUE, MF_CHECKED);
  789.                     break;
  790.  
  791.                 case IDM_TRANSPARENT:
  792.                     nBkMode = TRANSPARENT;
  793.                     CheckMenuItem(GetMenu(hWnd),
  794.                         IDM_OPAQUE,  MF_UNCHECKED);
  795.                     CheckMenuItem(GetMenu(hWnd),
  796.                         IDM_TRANSPARENT,  MF_CHECKED);
  797.                     break;
  798.  
  799.             }
  800.             break;
  801.  
  802.         case WM_KEYDOWN:
  803.             if(wParam == VK_PAUSE)      /* break key */
  804.                 BreakSet = 1;
  805.             if(wParam == VK_CANCEL)     /* ctrl break */
  806.                 /* restore our message loop and
  807.                     take control from main */
  808.                 Throw(CatchBuf,1);
  809.             break;
  810.  
  811.  
  812.         case WM_CHAR:
  813.             KeyBuffer[KeyHead] = wParam;
  814.             KeyHead = ++KeyHead & KeyMask;
  815.             BreakSet = 0;   /* break key */
  816.             return(1);
  817.  
  818.  
  819.         case WM_FONTCHANGE:
  820.             GetFonts(hWnd);
  821.             break;
  822.  
  823.         case WM_DESTROY:
  824.  
  825.             /* function pointer set with atexit */
  826.             if(ExitCallInit)
  827.                 ExitCallFunc();
  828.  
  829.             PostQuitMessage(0);
  830.             break;
  831.  
  832.         case WM_SETFOCUS:
  833.             CreateCaret(hWnd,NULL,1,TextMetric.tmHeight);
  834.             SetCaretPos(xCurrentPos,yCurrentPos);
  835.             ShowCaret(hWnd);
  836.             break;
  837.  
  838.         case WM_KILLFOCUS:
  839.             HideCaret(hWnd);
  840.             DestroyCaret();
  841.             break;
  842.  
  843.         case WM_SIZE:
  844.             Rect.right = LOWORD(lParam);
  845.             Rect.bottom = HIWORD(lParam);
  846.             /* calculate new visible rows */
  847.             Y = VisibleRows = 0;
  848.             while(VisibleRows < MAXROWS){
  849.                 Y += LineExtents[VisibleRows];
  850.                 if(Y >= Rect.bottom) break;
  851.                 VisibleRows++;
  852.                 }
  853.             VisibleCols = min( Rect.right /
  854.                     TextMetric.tmAveCharWidth,MAXCOLUMNS);
  855.             SetScrollRange(hWnd,SB_VERT,0,
  856.                     MAXROWS-VisibleRows,FALSE);
  857.             SetScrollRange(hWnd,SB_HORZ,0,
  858.                     MAXCOLUMNS-VisibleCols,FALSE);
  859.  
  860.         default:
  861.             return (DefWindowProc(hWnd, message,
  862.                     wParam, lParam));
  863.     }
  864.     return (0L);
  865. }
  866.  
  867.  
  868.  
  869. /**********************************************************
  870.  
  871.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  872.  
  873.     PURPOSE:  Processes messages for "About" dialog box
  874.  
  875.     MESSAGES:
  876.  
  877.         WM_INITDIALOG - initialize dialog box
  878.         WM_COMMAND    - Input received
  879.  
  880. **********************************************************/
  881.  
  882. BOOL FAR PASCAL About(hDlg, message, wParam, lParam)
  883. HWND hDlg;
  884. unsigned message;
  885. WORD wParam;
  886. LONG lParam;
  887. {
  888.     switch (message) {
  889.         case WM_INITDIALOG:
  890.             return (TRUE);
  891.  
  892.         case WM_COMMAND:
  893.             if (wParam == IDOK) {
  894.                 EndDialog(hDlg, TRUE);
  895.                 return (TRUE);
  896.             }
  897.             return (TRUE);
  898.     }
  899.     return (FALSE);
  900. }
  901.  
  902.