home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / win100b.zip / wktint.c < prev    next >
C/C++ Source or Header  |  1991-10-20  |  17KB  |  570 lines

  1. /* 
  2.  * Initialization segment for the Windows Win100 emulator.
  3.  * This code is discarded after it is used.
  4.  *
  5.  * Copyright (c) 1990, 1991 by
  6.  * William S. Hall
  7.  * 3665 Benton Street, #66
  8.  * Santa Clara, CA 95051
  9.  *
  10.  * $Date$
  11.  * $Revision$
  12.  * $RCSfile$
  13.  * $Source$
  14.  * $State$
  15.  */
  16.  
  17. #define NOKANJI
  18. #define NOMINMAX
  19. #define NOSOUND
  20. #include <windows.h>
  21. #include <stdlib.h>
  22. #include <limits.h>
  23. #include <ascii.h>
  24. #include <string.h>
  25. #ifdef COLUMBIA
  26. #include "wktsmt.h"
  27. #else
  28. #include "smterm.h"
  29. #endif
  30. #ifdef WIN600
  31. #include "grterm.h"
  32. #endif
  33. #ifdef COLUMBIA
  34. #include "wkt100.h"
  35. #else
  36. #include "win600.h"
  37. #endif
  38.  
  39. static char *szInitKeyStr;
  40. static int Initlen;
  41. static BOOL WinIniModified = FALSE;
  42. #define INITKEYSIZE    1024
  43.  
  44. /* local function declarations */
  45. static BOOL NEAR RegisterWindowClass(HANDLE);
  46. static void NEAR GetCurrentInstanceData(HANDLE hInstance);
  47. static void NEAR GetPrevInstanceData(HANDLE);
  48. static BOOL NEAR MakeAndShowMainWnd(HANDLE, HANDLE, int);
  49. static BOOL NEAR ReadInitialData(HANDLE hInstance);
  50. static BOOL NEAR OpenAndSetCommPort(HANDLE hInstance);
  51. static BOOL NEAR SearchKey(char *str, char *key, int len);
  52. static int NEAR GetInitData(int id, int defval);
  53.  
  54. /* This routine is FAR since it is called from another segment */
  55. BOOL FAR InitProgram(hInstance,hPrevInstance, lpszCmdLine, cmdShow)
  56. HANDLE hInstance, hPrevInstance;
  57. LPSTR lpszCmdLine;
  58. int cmdShow;
  59. {
  60.  
  61.   /*** GET COMMAND LINE ? ***/
  62.     hInst = hInstance;
  63.     DoMessage = DoPeekMessage;
  64.     
  65.   /* if this is the first instance of the program ... */
  66.     if (!hPrevInstance) {
  67.     GetCurrentInstanceData(hInstance);
  68.     
  69.     if (!RegisterWindowClass(hInstance))
  70.         return FALSE;
  71.  
  72.     hAccel = LoadAccelerators(hInstance, (LPSTR)szAppName);
  73.     }
  74.  
  75.   /* A previous instance already exists so get global data from there */
  76.     else
  77.     GetPrevInstanceData(hPrevInstance);
  78.  
  79.     if (!ReadInitialData(hInstance))
  80.     return FALSE;
  81.     
  82.   /* Create and show the window */
  83.     if (!MakeAndShowMainWnd(hInstance,hPrevInstance, cmdShow))
  84.     return FALSE;
  85.  
  86.     hWndText = MakeAndShowTermWindow(hInstance,
  87.                    hPrevInstance,
  88.                        MWnd.hWnd,
  89.                    &Smt,
  90.                    ColorTable[Initdata.TextColor],
  91.                    ColorTable[Initdata.BGColor],
  92.                     0,0,
  93.                        Initdata.TermRows,
  94.                        Initdata.TermCols,
  95.                    Initdata.Wrap,
  96.                    Initdata.Fontface,
  97.                    Initdata.NFontWidth,
  98.                    Initdata.NFontHeight,
  99.                    Initdata.SFontWidth,
  100.                    Initdata.SFontHeight);
  101.     if (hWndText == 0)
  102.     PostMessage(MWnd.hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  103.  
  104. #ifdef WIN600
  105.     if (!MakeAndShowGraphWindow(hInstance,
  106.                 hPrevInstance,
  107.                 MWnd.hWnd,
  108.                 &Grt,
  109.                 ColorTable[Initdata.TextColor],
  110.                 ColorTable[Initdata.BGColor],
  111.                 0,0,
  112.                 1024,800
  113.                 ))
  114.     PostMessage(MWnd.hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  115. #endif
  116.  
  117.     hWndNext = SetClipboardViewer(MWnd.hWnd);
  118.  
  119.     curproto.type = 'A';
  120.     if (Initdata.EWidth == 7)
  121.     curproto.mask = 0x7f;
  122.     else
  123.     curproto.mask = 0xff;
  124.  
  125.     hWndActive = hWndText;
  126.     SetAlphaParams(Initdata.AlphaEmulation);
  127.  
  128.     LocalEcho = Initdata.LocalEcho;
  129.     if (Initdata.LocalMode) {
  130.     LineState = LOCAL_ON;
  131.     DoMessage = DoGetMessage;
  132.     } else {
  133.     LineState = LINE_ON + LocalEcho;
  134.     DoMessage = DoPeekMessage;
  135.     }
  136.  
  137.     if (Initdata.ReverseVideo) {
  138.     InvertScreen(TRUE);
  139.     MWnd.Reverse = TRUE;
  140.     }
  141.  
  142.     cid = INT_MIN;
  143.     if (!OpenAndSetCommPort(hInstance))
  144.     PostMessage(MWnd.hWnd, WM_SYSCOMMAND, SC_CLOSE, 0L);
  145.     else {
  146.         MWnd.Cursor = Initdata.Cursor ? TRUE : FALSE;
  147.         MWnd.CursorType = Initdata.CursorType ? TRUE : FALSE;
  148.         MWnd.BreakEnabled = Initdata.BreakEnabled ? TRUE : FALSE;
  149.         MWnd.CurKeyMode = Initdata.CursorKeys ? TRUE : FALSE;
  150.         MWnd.DeleteKey = Initdata.DeleteKey ? TRUE : FALSE;
  151.         MWnd.AutoAnswer = Initdata.AutoAnswer ? TRUE : FALSE;
  152.         MWnd.LFCR = Initdata.LFCR ? TRUE : FALSE;
  153.         MWnd.ApplMode = Initdata.AltKeypad ? TRUE : FALSE;
  154.         MWnd.WarningBell = Initdata.WarningBell ? TRUE : FALSE;
  155.     MWnd.Wrap = Initdata.Wrap;
  156.  
  157.     MWnd.ControlRep = Initdata.ControlRep;
  158.     if (MWnd.ControlRep) {
  159.         if (curproto.atype == 52)
  160.         vt52SetState(-1);
  161.         else
  162.         vt100SetState(-1);
  163.             SendMessage(hWndActive, SMT_SETATTRIBUTE, SM_AUTOWRAP, (LONG)TRUE);
  164.     }
  165.  
  166.         SendMessage(hWndText,SMT_SETATTRIBUTE,SM_MARGINBELL,
  167.                         (LONG)Initdata.MarginBell);
  168.         SendMessage(hWndText,SMT_SETATTRIBUTE,SM_SMOOTHSCROLL,
  169.                         (LONG)Initdata.SmoothScroll);
  170.         SendMessage(hWndText,SMT_SETATTRIBUTE,SM_SCROLLUNITS,
  171.                         (LONG)Initdata.ScrollUnits);
  172.  
  173.     /* finally, display the window and show success */
  174.     ShowWindow(MWnd.hWnd, cmdShow);
  175.     UpdateWindow(MWnd.hWnd);
  176.     }
  177.     return TRUE;
  178. }
  179.  
  180. void FAR MainWndCreate(HWND hWnd, LONG lParam)
  181. {
  182.     MWnd.TextColor = Initdata.TextColor;
  183.     MWnd.BGColor = Initdata.BGColor;
  184.     MWnd.hBrush = CreateSolidBrush(ColorTable[Initdata.BGColor]);
  185. }
  186.  
  187. static BOOL NEAR SearchKey(char *str, char *key, int len)
  188. {
  189.     int i;
  190.     char *startptr;
  191.     startptr = str;
  192.  
  193.     for (i = 0; i < len; i++) {
  194.     if (*str++)
  195.         ;
  196.     else {
  197.         if (strcmp(key, startptr) == 0)
  198.         return(TRUE);
  199.         startptr = str;
  200.     }
  201.     }
  202.     return (FALSE);
  203. }
  204.  
  205. static BOOL NEAR ReadInitialData(HANDLE hInstance)
  206. {
  207.  
  208.     char szKeyStr[80];
  209.     char szDefStr[80];
  210.     HANDLE hInitKeyStr;
  211.     register int i, val;
  212.     HDC hIC;
  213.     int cwidth, cheight;
  214.     HFONT hOldFont;
  215.     TEXTMETRIC tm;
  216.  
  217.     GetModuleFileName(hInstance, szHelpName, sizeof(szHelpName));
  218.     lstrcpy(szIniName, szHelpName);
  219.     lstrcpy(szHelpName + strlen(szHelpName) - 3, "HLP");
  220.     lstrcpy(szIniName + strlen(szIniName) - 3, "INI");
  221.  
  222.     hIC = CreateIC("DISPLAY", NULL, NULL, NULL);    
  223.     MWnd.PelSize = GetDeviceCaps(hIC,BITSPIXEL) * GetDeviceCaps(hIC,PLANES);
  224.     hOldFont = SelectObject(hIC, GetStockObject(SYSTEM_FIXED_FONT));
  225.     GetTextMetrics(hIC, &tm);
  226.     cwidth = tm.tmAveCharWidth;
  227.     cheight = tm.tmHeight + tm.tmExternalLeading;
  228.     SelectObject(hIC, hOldFont);
  229.     DeleteDC(hIC);
  230.  
  231.     hInitKeyStr = LocalAlloc(LPTR, INITKEYSIZE);
  232.     if ((szInitKeyStr = LocalLock(hInitKeyStr)) == NULL)
  233.     return FALSE;
  234.  
  235.     Initlen = GetPrivateProfileString(szAppName, NULL, "", szInitKeyStr,
  236.                       INITKEYSIZE, szIniName);
  237.  
  238.   /* default ports */
  239.     for (i = 0; i < MAXCOMPORTS; i++) {
  240.         LoadString(hInstance,IDS_FIRSTPORT+i,(LPSTR)szKeyStr,sizeof(szKeyStr));
  241.         LoadString(hInstance,IDS_COM1 + i, (LPSTR)szDefStr, sizeof(szDefStr));
  242.         if (!SearchKey(szInitKeyStr, szKeyStr, Initlen)) {
  243.         WinIniModified = TRUE;
  244.         WritePrivateProfileString(szAppName,szKeyStr,szDefStr, szIniName);
  245.     }
  246.         GetPrivateProfileString(szAppName,szKeyStr,szDefStr,
  247.                 Initdata.portname[i],
  248.                 sizeof(Initdata.portname[i]), szIniName);
  249.     }
  250.  
  251.   /* comm parameters */
  252.     Initdata.BaudRate = (WORD)GetInitData(IDS_BAUD, DEFBAUD);
  253.     for (i = 0; i < BAUDRATETABLESIZE; i++)
  254.     if (BaudRateTable[i] == Initdata.BaudRate)
  255.         break;
  256.     if (i == BAUDRATETABLESIZE)
  257.     Initdata.BaudRate = DEFBAUD;
  258.         
  259.     Initdata.Parity = (BYTE)GetInitData(IDS_PARITY, DEFPARITY);
  260.     Initdata.ByteSize = (BYTE)GetInitData(IDS_BYTESIZE, DEFBYTESIZE);
  261.     for (i = 0; i < PARITYTABLESIZE; i++)
  262.     if ((ParityTable[i].Parity == Initdata.Parity) &&
  263.         (ParityTable[i].ByteSize == Initdata.ByteSize))
  264.         break;
  265.     if (i == PARITYTABLESIZE) {
  266.     Initdata.ByteSize = DEFBYTESIZE;
  267.     Initdata.Parity = DEFPARITY;
  268.     }
  269.  
  270.     Initdata.StopBits = (BYTE)GetInitData(IDS_STOPBITS, DEFSTOP);
  271.  
  272.     Initdata.fParity = (BYTE)GetInitData(IDS_PARCHECK, DEFPECHECK);
  273.  
  274.     Initdata.XonXoff = (BYTE)GetInitData(IDS_XONXOFF, DEFXONXOFF);
  275.  
  276.     Initdata.XoffLim = (WORD)GetInitData(IDS_XOFFLIM, DEFXOFFLIM);
  277.     for (i = 0; i < XOFFLIMSIZE; i++)
  278.     if (XoffSizeTable[i] == Initdata.XoffLim)
  279.         break;
  280.     if (i == XOFFLIMSIZE)
  281.     Initdata.XoffLim = DEFXOFFLIM;
  282.  
  283.     Initdata.LocalEcho = GetInitData(IDS_LOCALECHO, DEFLOCALECHO) ? 1 : 0;
  284.  
  285.     Initdata.Wrap = GetInitData(IDS_WRAP, DEFWRAP) ? TRUE : FALSE;
  286.  
  287.     Initdata.LFCR = GetInitData(IDS_AUTOLFCR, DEFLFCR) ? TRUE : FALSE;
  288.  
  289.     Initdata.MarginBell = GetInitData(IDS_MARGINBELL, DEFMBELL) ? TRUE : FALSE;
  290.  
  291.     Initdata.ControlRep = 
  292.     GetInitData(IDS_CONTROLREP, DEFCONTROLREP) ? TRUE : FALSE;
  293.  
  294.     Initdata.Cursor = GetInitData(IDS_CURSOR, DEFCURSOR) ? TRUE : FALSE;
  295.  
  296.     Initdata.CursorType = 
  297.     GetInitData(IDS_CURSORTYPE, DEFCURSORTYPE) ? TRUE : FALSE;
  298.  
  299.     val = COLORTABLESIZE - 1;
  300.     Initdata.BGColor = GetInitData(IDS_BGCOLOR, DEFBGCOLOR) & val;
  301.     Initdata.TextColor = GetInitData(IDS_TEXTCOLOR, DEFTEXTCOLOR) & val;
  302.     if (MWnd.PelSize == 1) {
  303.     if (Initdata.BGColor) {
  304.         Initdata.BGColor = val;
  305.         Initdata.TextColor = 0;
  306.     } else {
  307.         Initdata.TextColor = val;
  308.         Initdata.BGColor = 0;
  309.     }
  310.     } else {
  311.         if (Initdata.TextColor == Initdata.BGColor)
  312.         Initdata.TextColor = (~Initdata.BGColor) & val;
  313.     }
  314.  
  315.     Initdata.WarningBell = 
  316.     GetInitData(IDS_WARNINGBELL, DEFWBELL) ? TRUE : FALSE;
  317.  
  318.     val = GetInitData(IDS_TERMTYPE, DEFTERMTYPE);
  319.     Initdata.AlphaEmulation =
  320.             ((val == 52) || (val == 100)) ? val : DEFTERMTYPE;
  321.  
  322.     val = GetInitData(IDS_EMULATIONWIDTH, DEFEWIDTH);
  323.     Initdata.EWidth = ((val == 7) || (val == 8)) ? val : DEFEWIDTH;
  324.     if (Initdata.AlphaEmulation == 52)
  325.     Initdata.EWidth = 7;
  326.     
  327.     Initdata.ReverseVideo = 
  328.     GetInitData(IDS_REVERSEVIDEO, DEFVIDEO) ? TRUE : FALSE;
  329.  
  330.     Initdata.SmoothScroll = 
  331.     GetInitData(IDS_SMOOTHSCROLL, DEFSCROLL) ? TRUE : FALSE;
  332.  
  333.     val = GetInitData(IDS_SCROLLUNITS, DEFSCROLLUNITS);
  334.     Initdata.ScrollUnits = 
  335.     ((val >= 0) && (val <= MAXSCROLLUNITS)) ? val : DEFSCROLLUNITS;
  336.  
  337.     Initdata.LocalMode = 
  338.     GetInitData(IDS_LOCALMODE, DEFLOCALMODE) ? TRUE : FALSE;
  339.  
  340.     Initdata.AltKeypad = 
  341.     GetInitData(IDS_ALTKEYPAD, DEFALTKEYPAD) ? TRUE : FALSE;
  342.  
  343.     Initdata.CursorKeys = 
  344.     GetInitData(IDS_CURSORKEYS, DEFCURSORKEYS) ? TRUE : FALSE;
  345.  
  346.     Initdata.BreakEnabled = 
  347.     GetInitData(IDS_BREAKKEYENABLED, DEFBREAKENABLED) ? TRUE : FALSE;
  348.  
  349.     Initdata.DeleteKey = 
  350.     GetInitData(IDS_DELETEKEY, DEFDELETEKEY) ? TRUE : FALSE;
  351.  
  352.     Initdata.AutoAnswer = 
  353.     GetInitData(IDS_AUTOANSWER, DEFAUTOANSWER) ? TRUE : FALSE;
  354.  
  355.     val = GetInitData(IDS_ROWS, DEFROWS);
  356.     Initdata.TermRows = ((val >= 2) && (val <= MAXROWS)) ? val : DEFROWS;
  357.  
  358.     val = GetInitData(IDS_COLS, DEFCOLS);
  359.     Initdata.TermCols = val > 80 ? 132 : 80;
  360.  
  361.     LoadString(hInstance, IDS_FONTFACE, (LPSTR)szKeyStr,sizeof(szKeyStr));
  362.     LoadString(hInstance, IDS_DEFAULTFONT, szDefStr, sizeof(szDefStr));
  363.     if (!SearchKey(szInitKeyStr, szKeyStr, Initlen)) {
  364.     WinIniModified = TRUE;
  365.     WritePrivateProfileString(szAppName, szKeyStr, szDefStr, szIniName);
  366.     }
  367.     GetPrivateProfileString(szAppName, szKeyStr, szDefStr, Initdata.Fontface,
  368.                 sizeof(Initdata.Fontface), szIniName);
  369.  
  370.     Initdata.NFontWidth = GetInitData(IDS_NORMALWIDTH, cwidth);
  371.     if (Initdata.NFontWidth == 0)
  372.     Initdata.NFontWidth = cwidth;
  373.  
  374.     Initdata.NFontHeight = GetInitData(IDS_NORMALHEIGHT, cheight);
  375.     if (Initdata.NFontHeight == 0)
  376.     Initdata.NFontHeight = cheight;
  377.  
  378.     Initdata.SFontWidth = GetInitData(IDS_SMALLWIDTH, cwidth);
  379.     if (Initdata.SFontWidth == 0)
  380.     Initdata.SFontWidth = cwidth;
  381.  
  382.     Initdata.SFontHeight = GetInitData(IDS_SMALLHEIGHT, cheight);
  383.     if (Initdata.SFontHeight == 0)
  384.     Initdata.SFontHeight = cheight;
  385.  
  386.     LocalUnlock(hInitKeyStr);
  387.     LocalFree(hInitKeyStr);
  388.  
  389.     if (WinIniModified) 
  390.     BroadcastWinIniChange();
  391.  
  392.     return TRUE;
  393. }
  394.  
  395. static int NEAR GetInitData(int id, int defval)
  396. {
  397.  
  398.     char szKeyStr[80];
  399.     char buf[40];
  400.  
  401.     LoadString(hInst, id, szKeyStr,sizeof(szKeyStr));
  402.     if (!SearchKey(szInitKeyStr, szKeyStr, Initlen)) {
  403.     WinIniModified = TRUE;
  404.     WritePrivateProfileString(szAppName,szKeyStr,
  405.                   itoa(defval, buf, 10), szIniName);
  406.     }
  407.     return GetPrivateProfileInt(szAppName, szKeyStr, defval, szIniName);
  408. }
  409.  
  410. static BOOL NEAR OpenAndSetCommPort(HANDLE hInstance)
  411. {
  412.     int i, reply;
  413.     char szMessage[80];
  414.     char szBigMsg[160];
  415.  
  416.     for (i = 0; i < MAXCOMPORTS; i++) {
  417.         if ((cid = OpenComm(Initdata.portname[i], DEFRXQUE, DEFTXQUE)) < 0) {
  418.         LoadString(hInstance,cid,szBigMsg,sizeof(szMessage));
  419.         LoadString(hInstance,IDS_TRYANOTHER,szMessage,sizeof(szMessage));
  420.         strcat(szBigMsg, szMessage);
  421.             reply = MessageBox(MWnd.hWnd,(LPSTR)szBigMsg,
  422.            (LPSTR)Initdata.portname[i], MB_ICONQUESTION | MB_YESNO);
  423.         if (reply == IDNO)
  424.         return FALSE;
  425.     }
  426.     else
  427.         break;
  428.     }
  429.     if (i == MAXCOMPORTS) {
  430. //    LoadString(hInstance, IDS_NOCOM, szMessage, sizeof(szMessage));
  431.         ShowMessage(MWnd.hWnd, IDS_NOCOM, MB_ICONEXCLAMATION | MB_OK);
  432.     return FALSE;
  433.     }
  434.     LoadString(hInstance, IDS_TITLE, szMessage, sizeof(szMessage));
  435.     strcat(szMessage, Initdata.portname[i]);
  436.     SetWindowText(MWnd.hWnd, szMessage);
  437.  
  438.     if ((reply = GetCommState(cid, (DCB FAR *)&CommData)) >= 0) {
  439.     CommData.BaudRate = Initdata.BaudRate;
  440.     CommData.ByteSize = Initdata.ByteSize;
  441.     CommData.Parity = Initdata.Parity;
  442.     CommData.StopBits =
  443.         (BYTE)(Initdata.StopBits == 2 ? TWOSTOPBITS : ONESTOPBIT);
  444.     CommData.fParity = (BYTE)(Initdata.fParity ? 1 : 0);
  445.         CommData.fPeChar = CommData.fParity;
  446.         CommData.PeChar = DEFPECHAR;
  447.  
  448.         CommData.fOutX = CommData.fInX =
  449.                 (BYTE)(Initdata.XonXoff ? TRUE : FALSE);
  450.         CommData.XonChar = DEFXONCHAR;
  451.         CommData.XoffChar = DEFXOFFCHAR;
  452.  
  453.         CommData.XonLim = DEFXONLIM;
  454.         CommData.XoffLim = DEFRXQUE - Initdata.XoffLim;
  455.     CommData.fBinary = TRUE;
  456.     CommData.fDtrDisable = FALSE;
  457.     CommData.fNull = TRUE;
  458.     if ((reply = SetCommState((DCB FAR *)&CommData)) >= 0)
  459.         return TRUE;
  460.     }
  461.     ShowMessage(MWnd.hWnd, reply, MB_OK | MB_ICONEXCLAMATION);
  462.     CloseCommPort(MWnd.hWnd, &cid);
  463.     return FALSE;
  464. }
  465.  
  466. static void NEAR GetCurrentInstanceData(HANDLE hInstance)
  467. {
  468.     LoadString(hInstance, IDS_ICON, (LPSTR)szIcon, sizeof(szIcon));
  469. }
  470.  
  471. /*
  472.    If this not the first instance, we can retrieve static data from a
  473.    previous invocation of the program
  474. */
  475. static void NEAR GetPrevInstanceData(HANDLE hPrevInstance)
  476. {
  477.  
  478.     GetInstanceData(hPrevInstance, (PSTR)szAppName, sizeof(szAppName));
  479.     GetInstanceData(hPrevInstance, (PSTR)szIcon, sizeof(szIcon));
  480.     GetInstanceData(hPrevInstance, (PSTR)&hAccel, sizeof(hAccel));
  481. }
  482.  
  483. /* Every window must belong to a class. We register ours here */
  484. static BOOL NEAR RegisterWindowClass(HANDLE hInstance)
  485. {
  486.  
  487.     PWNDCLASS pWndClass;
  488.     HANDLE hTemp;
  489.  
  490.   /* Load the name string from resources */
  491.     LoadString(hInstance, IDS_APPNAME,(LPSTR)szAppName,sizeof(szAppName));
  492.  
  493.   /* allocate space for the WNDCLASS structure and lock it down */
  494.     hTemp = LocalAlloc(LPTR,sizeof(WNDCLASS));
  495.     pWndClass = (PWNDCLASS)LocalLock(hTemp);
  496.  
  497.   /* fill the structure */    
  498.     pWndClass->hCursor    = LoadCursor(NULL, IDC_ARROW);  /* standard cursor */
  499.     pWndClass->hIcon    = NULL;                /* no icon */
  500.     pWndClass->lpszMenuName = (LPSTR)szAppName;
  501.     pWndClass->lpszClassName = (LPSTR)szAppName;    /* our class name */
  502.     pWndClass->hbrBackground = (HBRUSH)NULL;
  503.     pWndClass->hInstance = hInstance;        /* instance handle */
  504.     pWndClass->style = CS_VREDRAW | CS_HREDRAW; /* standard redraw values */
  505.     pWndClass->lpfnWndProc = MainWndProc;  /* pointer to our window proc */
  506.     pWndClass->cbClsExtra = 0;
  507.     pWndClass->cbWndExtra = sizeof(pMWND);
  508.  
  509.   /* register the class.  if fail, abort */
  510.     if (!RegisterClass((LPWNDCLASS)pWndClass))
  511.     return FALSE;
  512.  
  513.   /* free the memory used */
  514.     LocalUnlock(hTemp);
  515.     LocalFree(hTemp);
  516.  
  517.   /* show success */
  518.     return TRUE;
  519. }
  520.  
  521. /*
  522.  Create the window, making sure that its position and size are suitable
  523.  for the display.
  524. */
  525. static BOOL NEAR MakeAndShowMainWnd(HANDLE hInstance, HANDLE hPrevInstance, 
  526.                     int cmdShow)
  527. {
  528.  
  529.     char szTitle[50];
  530.     DWORD style = WS_HSCROLL | WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN;
  531.     int x, y, cx, cy;
  532.     int cwidth, cheight;
  533.  
  534.     LoadString(hInstance, IDS_TITLE,(LPSTR)szTitle,sizeof(szTitle));
  535.  
  536.     if (hPrevInstance)
  537.     x = CW_USEDEFAULT;
  538.     else
  539.         x = 0;
  540.     y = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYFRAME);
  541.  
  542.     if (Initdata.TermCols == MAXCOLUMNS) {
  543.     cwidth = Initdata.SFontWidth;
  544.     cheight = Initdata.SFontHeight;
  545.     }
  546.     else {
  547.     cwidth = Initdata.NFontWidth;
  548.     cheight = Initdata.NFontHeight;
  549.     }
  550.     cx = Initdata.TermCols * cwidth + 2 * GetSystemMetrics(SM_CXFRAME);
  551.     cx = min(GetSystemMetrics(SM_CXSCREEN), cx);
  552.     cy = Initdata.TermRows * cheight + 2 * GetSystemMetrics(SM_CYFRAME) +
  553.      GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYMENU) +
  554.      GetSystemMetrics(SM_CYHSCROLL);
  555.     cy = min(GetSystemMetrics(SM_CYSCREEN) - y, cy);
  556.     
  557.     if (MWnd.hWnd = CreateWindow((LPSTR)szAppName,
  558.                  (LPSTR)szTitle,
  559.                  style,
  560.                  x, y, cx, cy,
  561.                  (HWND)NULL,
  562.                  (HMENU)NULL,
  563.                  (HANDLE)hInstance,
  564.                  (LPSTR)NULL))
  565.     return TRUE;
  566.  
  567.     return FALSE;
  568. }
  569.  
  570.