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

  1.  
  2. /***************************************************************************
  3. *
  4. * wincmd.c
  5. *
  6. * Windows Kermit menu support module.
  7. *
  8. ***************************************************************************/
  9.      
  10. #include <windows.h>
  11. #include "winkrm.h"
  12.      
  13. /* These functions are local to this segment and so can be declared NEAR */
  14. void NEAR SetBaud(WORD, HMENU);
  15. void NEAR SetState(WORD, HMENU);
  16. void NEAR SetSerial(WORD, HMENU);
  17. void NEAR LoadDCB(void);
  18.      
  19. /***************************************************************************
  20. *
  21. * KerCommand
  22. *
  23. * Called from the window procedure when a WM_COMMAND message is received
  24. *
  25. * Entry: Handle to the Window,
  26. *     the command paramter,
  27. *     the lParam passed to the Windows procedure.
  28. *
  29. * Exit: Command executed
  30. *
  31. ***************************************************************************/
  32. KerCommand(hWnd,cmd,lParam)
  33. HWND hWnd;
  34. WORD cmd;
  35. LONG lParam;
  36. {
  37.      
  38.     HMENU hMenu = GetMenu(hWnd);    /* fetch the menu handle for later */
  39.      
  40.     switch(cmd) {
  41.      
  42.     case CONNECTSTATE:        /* Set one of the three main states */
  43.     case RECEIVESTATE:
  44.     case SENDSTATE:
  45.         SetState(cmd, hMenu);
  46.         break;
  47.      
  48.     case COMMPORT1:            /* Switch or disable a port */
  49.     case COMMPORT2:
  50.         SetSerial(cmd,hMenu);
  51.         break;
  52.     
  53.     case BAUD300:            /* Set the baud rate */
  54.     case BAUD1200:
  55.     case BAUD2400:
  56.         SetBaud(cmd, hMenu);
  57.         break;
  58.     }
  59. }
  60.      
  61. /***************************************************************************
  62. *
  63. * SetBaud
  64. *
  65. * Set the baud rate
  66. *
  67. * Entry: Command parameter,
  68. *     handle to the menu.
  69. *
  70. * Exit: Baud rate set.
  71. *
  72. * Notes:  The baud rate is not changed if the rate asked for is alread set.
  73. *
  74. ***************************************************************************/
  75. void NEAR SetBaud(cmd, hMenu)
  76. WORD cmd;
  77. HMENU hMenu;
  78. {
  79.      
  80.     if (cmd != CurrentBaud) {
  81.     CheckMenuItem(hMenu, CurrentBaud, MF_UNCHECKED);
  82.     CurrentBaud = cmd;
  83.     switch (cmd) {
  84.         case BAUD300:
  85.             ComDCB.BaudRate = 300;
  86.             break;
  87.             case BAUD1200:
  88.             ComDCB.BaudRate = 1200;
  89.             break;
  90.         case BAUD2400:
  91.             ComDCB.BaudRate = 2400;
  92.             break;
  93.         }
  94.         SetCommState( (DCB FAR *)&ComDCB);
  95.         CheckMenuItem(hMenu, CurrentBaud, MF_CHECKED);
  96.     }
  97. }
  98.      
  99. /***************************************************************************
  100. *
  101. * SetState
  102. *
  103. * Set the state of the program among the three possible states of
  104. * Connect, Receive file, or Send file.
  105. *
  106. * Entry: Command word,
  107. *     handle to the menu.
  108. *
  109. * Exit: State selected or disabled.
  110. *
  111. ***************************************************************************/
  112. void NEAR SetState(cmd, hMenu)
  113. WORD cmd;
  114. HMENU hMenu;
  115. {
  116.      
  117.   /* In this case, just set things to no state */
  118.     if (cmd == CurrentState) {
  119.     CheckMenuItem(hMenu, CurrentState, MF_UNCHECKED);
  120.     CurrentState = 0;
  121.     }
  122.   /* Otherwise, gray out the old menu item, set the state, and check it */
  123.     else {
  124.     CheckMenuItem(hMenu, CurrentState, MF_UNCHECKED);
  125.     switch(cmd) {
  126.         case CONNECTSTATE:
  127.             CurrentState = CONNECTSTATE;
  128.         break;
  129.         case RECEIVESTATE:
  130.         CurrentState = RECEIVESTATE;
  131.             break;
  132.         case SENDSTATE:
  133.         CurrentState = SENDSTATE;
  134.         break;
  135.         }
  136.         CheckMenuItem(hMenu, CurrentState, MF_CHECKED);
  137.     }
  138. }
  139.      
  140. /***************************************************************************
  141. *
  142. * SetSerial
  143. *
  144. * Select the active serial port
  145. *
  146. * Entry: Command word,
  147. *     handle to a menu.
  148. *
  149. * Exit: Port selected or deselected.
  150. *
  151. * Notes: The serial port parameters in the DCB control structure are
  152. *     initially set in the initialization module.
  153. *
  154. ***************************************************************************/
  155. void NEAR SetSerial(cmd,hMenu)
  156. WORD cmd;
  157. HMENU hMenu;
  158. {
  159.      
  160.     int result;
  161.     char *comdev = szCom1;    /* has to be COM1 or COM2 */
  162.     char *errorstr = "Device Not Available";
  163.      
  164.     if (cmd == COMMPORT2)
  165.     comdev = szCom2;
  166.      
  167.   /* if cid < 0, then no port is open */
  168.     if (cid < 0) {        /* try opening the device, return if not able */
  169.     result = OpenComm((LPSTR)comdev, RXBUFSIZE, TXBUFSIZE);
  170.   /*
  171.      We could try to respond to specific errors here.  For example,
  172.      we might try reducing the size of the buffers if not enough
  173.      memory is available.  Right now, we just abort the effort.
  174.   */
  175.     if (result < 0) {
  176.         MessageBox(hKermWnd, (LPSTR)errorstr,
  177.             (LPSTR)comdev, MB_OK | MB_ICONEXCLAMATION);
  178.         return;
  179.     }
  180.     cid = result;        /* handle to communications port */
  181.     CurrentPort = cmd;
  182.     LoadDCB();        /* load up the DCB structure */
  183.     CheckMenuItem(hMenu, CurrentPort, MF_CHECKED);    /* fix up menus */
  184.     CheckMenuItem(hMenu, CurrentBaud, MF_CHECKED);
  185.     UnGrayMenuItems(hMenu);    /* activate menus affected by com ports */
  186.     }
  187.     else {
  188.   /* in this case, toggle the port back off and fix up affected menus */
  189.     if (CurrentPort == cmd) {
  190.         CloseComm(cid);
  191.         CheckMenuItem(hMenu, CurrentPort, MF_UNCHECKED);
  192.         cid = MAXCOMMERR;    /* cid < 0 again */
  193.         CurrentPort = 0;
  194.         GrayMenuItems(hKermWnd);
  195.     }
  196.     else {
  197.   /* In this case, toggle the other port on and this one off */
  198.         result = OpenComm((LPSTR)comdev, RXBUFSIZE, TXBUFSIZE);
  199.         if (result < 0) {
  200.             MessageBox(hKermWnd, (LPSTR)errorstr,
  201.             (LPSTR)comdev, MB_OK | MB_ICONEXCLAMATION);
  202.             return;
  203.         }
  204.         CloseComm(cid);        /* close first port */
  205.         CheckMenuItem(hMenu, CurrentPort, MF_UNCHECKED);    /* gray menu */
  206.         cid = result;        /* set cid to the new handle */
  207.         CurrentPort = cmd;        /* set up menu item parameter */
  208.         LoadDCB();            /* load up the port DCB */
  209.         CheckMenuItem(hMenu, CurrentPort, MF_CHECKED); /* activate menus */
  210.         CheckMenuItem(hMenu, CurrentBaud, MF_CHECKED);
  211.         UnGrayMenuItems(hMenu);    /* ungray affected items */
  212.     }
  213.     }
  214. }
  215.      
  216. /***************************************************************************
  217. *
  218. * LoadDCB
  219. *
  220. * Load the communications device control block and set our values
  221. *
  222. * Entry: None
  223. *
  224. * Exit: DCB loaded.
  225. *
  226. * Notes:  The values for the DCB field are initialized in Winint.
  227. *
  228. ***************************************************************************/
  229. void NEAR LoadDCB()
  230. {
  231.      
  232.     DCB OldComDCB;
  233.      
  234.     GetCommState(cid, (DCB FAR *)&OldComDCB);
  235.     ComDCB.Id = OldComDCB.Id;
  236.     SetCommState( (DCB FAR *)&ComDCB);
  237.      
  238. }
  239.