home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / winkerm.zip / WININT.C < prev    next >
C/C++ Source or Header  |  1986-10-10  |  11KB  |  363 lines

  1.  
  2. /***************************************************************************
  3. *
  4. * Winint.c
  5. *
  6. * Winint is the initialization segment.  It can be discarded after its use.
  7. *
  8. ***************************************************************************/
  9.      
  10. #include <windows.h>
  11. #include "winkrm.h"
  12.      
  13. /* These local functions are declared NEAR */
  14.      
  15. BOOL NEAR KermRegClass(HANDLE);
  16. void NEAR GetFirstInstStrings(HANDLE);
  17. void NEAR GetNextInstData(HANDLE, HANDLE);
  18. void NEAR CreateKermWnd(HANDLE);
  19. void NEAR InsertAbout(HANDLE);
  20. BOOL NEAR InitGlobals(void);
  21. void NEAR GetTextSize(void);
  22. void NEAR CreateScreenFont(void);
  23.      
  24. /***************************************************************************
  25. *
  26. * WinKermInit
  27. *
  28. * Initialize the program.
  29. *
  30. * Entry: Same parameters passed to WinMain from Windows.
  31. *
  32. * Exit:  TRUE if successful, FALSE if not.
  33. *
  34. ***************************************************************************/
  35. BOOL FAR WinKermInit(hInstance, hPrevInstance,lpszCmdLine,cmdShow)
  36. HANDLE hInstance;
  37. HANDLE hPrevInstance;
  38. LPSTR lpszCmdLine;
  39. int cmdShow;
  40. {
  41.      
  42.     if (!hPrevInstance) {
  43.     GetFirstInstStrings(hInstance);        /* load the strings or ... */
  44.     if (!KermRegClass(hInstance))
  45.         return(FALSE);
  46.     }
  47.     else
  48.         GetNextInstData(hInstance, hPrevInstance); /* get them from prev inst */
  49.      
  50.     hInst = hInstance;        /* save this for later */
  51.     CreateKermWnd(hInstance);    /* create the main tiled window */
  52.     hKermDC = GetDC(hKermWnd);    /* get a private DC */
  53.     InsertAbout(hInstance);    /* insert About into system menu */
  54.     CreateScreenFont();        /* create a logical font and select it */
  55.     GetTextSize();        /* measure the text size */
  56.     if (!InitGlobals())
  57.     return FALSE;        /* failed */
  58.     ShowWindow(hKermWnd,cmdShow);    /* show the window if globals OK */
  59.     return(TRUE);
  60. }
  61.      
  62. /***************************************************************************
  63. *
  64. * KermRegClass
  65. *
  66. * Registe the Kermit window class.
  67. *
  68. * Entry:  Instance handle
  69. *
  70. * Exit:  TRUE if successful, FALSE if not.
  71. *
  72. * Notes: As currently implemented, the ICON is hot and we own our own DC.
  73. *
  74. ***************************************************************************/
  75. BOOL NEAR KermRegClass(hInstance)
  76. HANDLE hInstance;
  77. {
  78.     PWNDCLASS    pKermWndClass;
  79.      
  80.     pKermWndClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS));
  81.     pKermWndClass->hCursor        = LoadCursor(NULL, IDC_ARROW);
  82.     pKermWndClass->hIcon        = NULL;
  83.     pKermWndClass->lpszMenuName        = (LPSTR)szAppName;
  84.     pKermWndClass->lpszClassName    = (LPSTR)szAppName;
  85.     pKermWndClass->hbrBackground    = (HBRUSH)GetStockObject( WHITE_BRUSH);
  86.     pKermWndClass->hInstance        = hInstance;
  87.     pKermWndClass->style        = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  88.     pKermWndClass->lpfnWndProc        = WinKermWndProc;
  89.      
  90.     if (!RegisterClass((LPWNDCLASS)pKermWndClass))
  91.     return(FALSE);
  92.     LocalFree((HANDLE)pKermWndClass);
  93.     return(TRUE);
  94. }
  95.      
  96. /***************************************************************************
  97. *
  98. * GetFirstInstStrings
  99. *
  100. * If first instance of program, load the strings from resource segment.
  101. *
  102. * Entry: Instance handle,
  103. *
  104. * Exit: None
  105. *
  106. ***************************************************************************/
  107. void NEAR GetFirstInstStrings(hInstance)
  108. HANDLE hInstance;
  109. {
  110.      
  111.     LoadString(hInstance, APPNAME, (LPSTR)szAppName, sizeof(szAppName));
  112.     LoadString(hInstance, APPABOUT, (LPSTR)szAbout, sizeof(szAbout));
  113.     LoadString(hInstance, TITLE, (LPSTR)szWinTitle,sizeof(szWinTitle));
  114.     LoadString(hInstance, COM1STR, (LPSTR)szCom1, 5);
  115.     LoadString(hInstance, COM2STR, (LPSTR)szCom2, 5);
  116.     LoadString(hInstance, STATESTRING, (LPSTR)StateLabel, sizeof(StateLabel));
  117.     LoadString(hInstance, FILESTRING, (LPSTR)FileLabel, sizeof(FileLabel));
  118.     LoadString(hInstance, PACKETSTRING, (LPSTR)PacketLabel, sizeof(PacketLabel));
  119. }
  120.      
  121. /***************************************************************************
  122. *
  123. * GetNextInstData
  124. *
  125. * If not first instance of program, load the data from previous instance.
  126. *
  127. * Entry: Instance handle,
  128. *
  129. * Exit: None
  130. *
  131. ***************************************************************************/
  132. void NEAR GetNextInstData(hInstance, hPrevInstance)
  133. HANDLE hInstance;
  134. HANDLE hPrevInstance;
  135. {
  136.      
  137.     GetInstanceData(hPrevInstance, (PSTR)szAppName, sizeof(szAppName));
  138.     GetInstanceData(hPrevInstance, (PSTR)szAbout, sizeof(szAbout));
  139.     GetInstanceData(hPrevInstance, (PSTR)szWinTitle, sizeof(szWinTitle));
  140.     GetInstanceData(hPrevInstance, (PSTR)szCom1, 5);
  141.     GetInstanceData(hPrevInstance, (PSTR)szCom2, 5);
  142.     GetInstanceData(hPrevInstance, (PSTR)StateLabel, sizeof(StateLabel));
  143.     GetInstanceData(hPrevInstance, (PSTR)PacketLabel, sizeof(PacketLabel));
  144.     GetInstanceData(hPrevInstance, (PSTR)FileLabel, sizeof(FileLabel));
  145.      
  146.   /* this little trick gives a unique window title to each invocation */
  147.     szWinTitle[strlen(szWinTitle)-1] += 1;
  148. }
  149.      
  150. /***************************************************************************
  151. *
  152. * CreateKermWind
  153. *
  154. * Create the tiled, main window for the program.
  155. *
  156. * Entry: Instance handle,
  157. *
  158. * Exit: None
  159. *
  160. ***************************************************************************/
  161. void NEAR CreateKermWnd(hInstance)
  162. HANDLE hInstance;
  163. {
  164.      
  165.     hKermWnd = CreateWindow((LPSTR)szAppName,
  166.                  (LPSTR)szWinTitle,
  167.                  WS_TILEDWINDOW,
  168.                  0,0,0,0,
  169.                  (HWND)NULL,
  170.                  (HMENU)NULL,    /* uses class menu */
  171.                  (HANDLE)hInstance,
  172.                  (LPSTR)NULL
  173.                  );
  174.      
  175. }
  176.      
  177. /***************************************************************************
  178. *
  179. * InsertAbout
  180. *
  181. * Insert 'About' into the system menu.
  182. *
  183. * Entry: Instance handle,
  184. *
  185. * Exit: None
  186. *
  187. ***************************************************************************/
  188. void NEAR InsertAbout(hInstance)
  189. HANDLE hInstance;
  190. {
  191.     HMENU hMenu;
  192.      
  193.     lpprocAbout = MakeProcInstance((FARPROC)About, hInstance);
  194.     hMenu = GetSystemMenu(hKermWnd, FALSE);
  195.     ChangeMenu(hMenu,0,NULL,999, MF_APPEND | MF_SEPARATOR);
  196.     ChangeMenu(hMenu,0,(LPSTR)szAbout, APPABOUT, MF_APPEND | MF_STRING);
  197. }
  198.      
  199. /***************************************************************************
  200. *
  201. * InitGlobals
  202. *
  203. * Initialize Global variables.
  204. *
  205. * Entry: Instance handle,
  206. *
  207. * Exit: None
  208. *
  209. * Notes:  Many of these steps can also be carried out in response
  210. *      to the WM_CREATE message.
  211. *
  212. ***************************************************************************/
  213. BOOL NEAR InitGlobals()
  214. {
  215.     int temp;
  216.      
  217.     cid = MAXCOMMERR;        /* cid < 0 means comm port not open */
  218.     CurrentPort = 0;        /* menu tracking items */
  219.     CurrentState = 0;
  220.     Xpos = 0;            /* x and y logical coordinates on screen */
  221.     Ypos = 0;
  222.     if ((ScreenBuf = (char *)LocalAlloc(LPTR, MaxChars)) == NULL) {
  223.     MessageBox(hKermWnd, (LPSTR)"Cannot create screen buffer",
  224.             (LPSTR)szWinTitle, MB_OK | MB_ICONEXCLAMATION);
  225.     return FALSE;
  226.     }
  227.     ScreenBufHead = 0;        /* initialize memory screen pointers */
  228.     PosInLine = 0;        /* -- and values */
  229.     CurrentLine = 0;
  230.     FirstLine = 0;
  231.      
  232.   /* Make proc instance for the send dialog box */
  233.     lpprocSendBox = MakeProcInstance((FARPROC)GetSendFile, hInst);
  234.      
  235.   /* now set all the comm port values */
  236.     ComDCB.ByteSize =    8;
  237.     ComDCB.Parity =    NOPARITY;
  238.     ComDCB.StopBits =    ONESTOPBIT;
  239.     ComDCB.RlsTimeout =    0;
  240.     ComDCB.CtsTimeout = 0;
  241.     ComDCB.DsrTimeout = 0;
  242.     ComDCB.fBinary =    TRUE;
  243.     ComDCB.fRtsDisable = FALSE;
  244.     ComDCB.fParity    = FALSE;
  245.     ComDCB.fOutxCtsFlow = FALSE;
  246.     ComDCB.fOutxDsrFlow = FALSE;
  247.     ComDCB.fDtrDisable = FALSE;
  248.     ComDCB.fOutX =    FALSE;
  249.     ComDCB.fInX =    TRUE;
  250.     ComDCB.PeChar =    NULL;
  251.     ComDCB.fNull =    TRUE;
  252.     ComDCB.fChEvt =    FALSE;
  253.     ComDCB.fDtrflow =   FALSE;
  254.     ComDCB.fRtsflow =   FALSE;
  255.     ComDCB.XonChar =    17;
  256.     ComDCB.XoffChar =    19;
  257.     ComDCB.XonLim   =    RXBUFSIZE /8;
  258.     ComDCB.XoffLim  =   RXBUFSIZE /8;
  259.     ComDCB.PeChar   =   NULL;
  260.     ComDCB.EvtChar  =    NULL;
  261.     ComDCB.EofChar  =   26;
  262.     ComDCB.TxDelay  =   0;
  263.      
  264.   /* Read the initialization stuff from win.ini */
  265.     temp = GetProfileInt( (LPSTR)"WinKrm", (LPSTR)"Baud", 1200);
  266.     switch (temp) {
  267.     case 300:
  268.         ComDCB.BaudRate = 300;
  269.         CurrentBaud = BAUD300;
  270.         break;
  271.     case 2400:
  272.         ComDCB.BaudRate = 2400;
  273.         CurrentBaud = BAUD2400;
  274.         break;
  275.     case 1200:
  276.     default:
  277.         ComDCB.BaudRate = 1200;
  278.         CurrentBaud = BAUD1200;
  279.         break;
  280.     }
  281.     CheckMenuItem(GetMenu(hKermWnd), CurrentBaud, MF_CHECKED);
  282.     return TRUE;
  283. }
  284.      
  285. /***************************************************************************
  286. *
  287. * CreateScreenFont
  288. *
  289. * Change the font to the OEM terminal font.  Gives visible control
  290. * characters and a few extra lines.
  291. *
  292. * Entry: None
  293. *
  294. * Exit: Logical font created and font selected to DC.
  295. *
  296. *
  297. ***************************************************************************/
  298. void NEAR CreateScreenFont()
  299. {
  300.      
  301.   /*
  302.      Fill in the data structure for the logical font.  Most entries
  303.      are zero so that the font mapper gets the default values for
  304.      the OEM terminal font.  By inserting different values, we could
  305.      get a different sized font suitable for, say 132 chars per line, etc.
  306.   */
  307.     ScreenFont.lfHeight = 0;
  308.     ScreenFont.lfWidth = 0;
  309.     ScreenFont.lfEscapement = 0;
  310.     ScreenFont.lfOrientation = 0;
  311.     ScreenFont.lfWeight = 400;
  312.     ScreenFont.lfItalic = 0;
  313.     ScreenFont.lfUnderline = 0;
  314.     ScreenFont.lfStrikeOut = 0;
  315.     ScreenFont.lfCharSet = OEM_CHARSET;        /* OEM character set */
  316.     ScreenFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
  317.     ScreenFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  318.     ScreenFont.lfQuality = DEFAULT_QUALITY;
  319.     ScreenFont.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  320.     strcpy(ScreenFont.lfFaceName, "Terminal");
  321.      
  322.     hScreenFont = CreateFontIndirect((LPLOGFONT)&ScreenFont);
  323.     hOldScreenFont = SelectObject(hKermDC, hScreenFont);
  324.      
  325. }
  326.      
  327. /***************************************************************************
  328. *
  329. * GetTextSize
  330. *
  331. * Measure the size of the font in terms of logical screen coordinates
  332. * Necessary to determine number of lines to write to a screen and
  333. * number of characters per line.
  334. *
  335. * Entry: None
  336. *
  337. * Exit: Character width and height determined for the current font.
  338. *    In addition, the maximum number of characters that could show
  339. *    on the screen are calculated.  These values are then used
  340. *       to determine the memory screen size.
  341. *
  342. ***************************************************************************/
  343. void NEAR GetTextSize()
  344. {
  345.      
  346.     TEXTMETRIC TM;
  347.      
  348.   /* This gets the metrics for the font */
  349.     GetTextMetrics(hKermDC, (LPTEXTMETRIC)&TM);
  350.     CharWidth = TM.tmAveCharWidth;
  351.     CharHeight = TM.tmHeight + TM.tmExternalLeading;
  352.   /* Now, see how many lines we could have on the whole screen */
  353.     MaxDevCoorLen = GetDeviceCaps(hKermDC, HORZRES);
  354.   /* Determine the line size in characters */
  355.     MaxCharLineLen = MaxDevCoorLen / CharWidth;
  356.   /* Determine the maximum number of lines on the screen */
  357.     MaxLines = GetDeviceCaps(hKermDC, VERTRES) / CharHeight;
  358.   /* Initialize related variables */
  359.     LineIncrement = MaxCharLineLen + 2;
  360.     MaxChars = LineIncrement * MaxLines;
  361.      
  362. }
  363.