/*  MRR1CNCM -- Window procedure to process the Configure Communications
 *   dialog box.
 */

#include "windows.h"
#include "mrr1.h"
#include "mrr1glob.h"

extern int Mrr1DoComm(char *);

BOOL FAR PASCAL Mrr1CommDlg(hDlg, iMessage, wParam, lParam)
HWND      hDlg;
unsigned  iMessage;
WORD      wParam;
LONG      lParam;
{
static HWND hCtrlBlock;
static int  MyCommPortID;
static int  MyCommParityID;
static char pszMyCommSpeed[MAXCOMMSPEEDCHARS];

#if 0
  MessageBox(hWndConf,"At Beginning of mrr1cncm",
                     "(debug)",MB_OK | MB_ICONASTERISK);
#endif

  switch(iMessage) {

    case WM_INITDIALOG:
      MyCommPortID = CommPortID;
      MyCommParityID = CommParityID;
      strcpy(pszMyCommSpeed,pszCommSpeed);

      CheckRadioButton(hDlg, IDD_COM1, IDD_COM2, CommPortID);
      CheckRadioButton(hDlg, IDD_7EVEN, IDD_8NONE, CommParityID);
      SetDlgItemText(hDlg,IDD_SPEED,pszMyCommSpeed);
      return TRUE;
      break;

    case WM_COMMAND:
      switch(wParam) {
        case IDOK:
          GetDlgItemText(hDlg,IDD_SPEED,pszCommSpeed,MAXCOMMSPEEDCHARS-1);

          CommPortID = MyCommPortID;
          CommParityID = MyCommParityID;
          CommIDtoStr(CommPortID,CommParityID,pszCommSpeed,szCommString);
          Mrr1DoComm(szCommString);
          EndDialog(hDlg,TRUE);
          break;

        case IDCANCEL:
          EndDialog(hDlg,FALSE);
          break;

        case IDD_COM1:
        case IDD_COM2:
          MyCommPortID = wParam;
          CheckRadioButton(hDlg,IDD_COM1,IDD_COM2,wParam);
          break;

        case IDD_7EVEN:
        case IDD_8NONE:
          MyCommParityID = wParam;
          CheckRadioButton(hDlg,IDD_7EVEN,IDD_8NONE,wParam);
          break;

        default:
          return FALSE;
      }
      break;

#if 0
    case WM_PAINT;
      InvalidateRect(
      break;
#endif
    default:
      return FALSE;
      break;
  }
  return TRUE;
}

/*--- Function CommIDtoStr --------------------------------------
 *
 *   Takes information relating to comm port configuration and creates
 *   a string of the form to give to the MODE command.
 *
 *   Entry    Port      is the port (an IDD_* variable)
 *            Parity    is the parity/data bits infor (an IDD_* variable)
 *            szSpeed     is the speed, in character form
 *
 *   Exit     CommStr    is the string; e.g., "COM1:2400,n,8"
 *            Function value is non-zero if error.
 */
int
CommIDtoStr(Port,Parity,szSpeed,CommStr)
int Port, Parity;
char *szSpeed;
char *CommStr;
{
  register char *ptr;
  int myPort;

  ptr = CommStr;
  strcpy(ptr,"COM");
  ptr += 3;
  *(ptr++) = Port==IDD_COM1 ? '1' : '2';
  *(ptr++) = ':';

  for(;*szSpeed; *(ptr++) = *(szSpeed++));
  *(ptr++) = ',';
  if(Parity == IDD_7EVEN) {
    strcpy(ptr,"e,7");
  } else {
    strcpy(ptr,"n,8");
  }

  return (0);
}
