home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / general / vcterm / setdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  3.5 KB  |  121 lines

  1. // setdlg.cpp : implementation of the CSettingsDlg class
  2.  
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "vcterm.h"
  15. #include "setdlg.h"
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CSettingsDlg dialog
  25.  
  26. // set up initial settings in dialog to mirror
  27. // the current settings in the MSCOMM control.
  28. CSettingsDlg::CSettingsDlg(CMainFrame* pParent)
  29.     : CDialog(CSettingsDlg::IDD, pParent)
  30. {
  31.     int index, baudrate = 300, parity;
  32.     CString str = pParent->GetCommCtrl()->GetSettings();
  33.     index = str.Find((TCHAR)',');
  34.  
  35.     // calculate radio button index for current baud rate setting
  36.     for (int nSetting = 0; baudrate< _ttoi(str.Left(index)); nSetting++)
  37.         baudrate = baudrate << 1;
  38.     baudrate = nSetting;
  39.  
  40.     // calculate radio button index for current parity setting
  41.     switch(str[index+1])
  42.     {
  43.         case (TCHAR)'N':
  44.         case (TCHAR)'n': parity=0; break;
  45.         case (TCHAR)'O':
  46.         case (TCHAR)'o': parity=1; break;
  47.         case (TCHAR)'E':
  48.         case (TCHAR)'e': parity=2; break;
  49.     }
  50.  
  51.     //{{AFX_DATA_INIT(CSettingsDlg)
  52.     m_baud = baudrate;
  53.     m_comport = pParent->GetCommCtrl()->GetCommPort()-1;
  54.     m_databits = str[index+3] - 55;            // convert to integer value -7
  55.     m_echo = pParent->GetEditCtrl()->m_bEcho;
  56.     m_flow = pParent->GetCommCtrl()->GetHandshaking();
  57.     m_parity = parity;
  58.     m_stopbits = str[index+5] - 49;            // convert to integer value -1
  59.     //}}AFX_DATA_INIT
  60. }
  61.  
  62. void CSettingsDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64.     CDialog::DoDataExchange(pDX);
  65.     //{{AFX_DATA_MAP(CSettingsDlg)
  66.     DDX_Radio(pDX, IDC_BAUD1, m_baud);
  67.     DDX_Radio(pDX, IDC_COM1, m_comport);
  68.     DDX_Radio(pDX, IDC_DATABITS1, m_databits);
  69.     DDX_Radio(pDX, IDC_ECHO1, m_echo);
  70.     DDX_Radio(pDX, IDC_FLOW1, m_flow);
  71.     DDX_Radio(pDX, IDC_PARITY1, m_parity);
  72.     DDX_Radio(pDX, IDC_STOPBITS1, m_stopbits);
  73.     //}}AFX_DATA_MAP
  74. }
  75.  
  76. BEGIN_MESSAGE_MAP(CSettingsDlg, CDialog)
  77.     //{{AFX_MSG_MAP(CSettingsDlg)
  78.     //}}AFX_MSG_MAP
  79. END_MESSAGE_MAP()
  80.  
  81. /////////////////////////////////////////////////////////////////////////////
  82. // CSettingsDlg message handlers
  83.  
  84. void CSettingsDlg::OnOK()
  85. {
  86.     // update various dialog data members
  87.     if (!UpdateData(TRUE))
  88.     {
  89.         AfxMessageBox(IDS_UPDATEDATAFAILED);
  90.         return;
  91.     }
  92.  
  93.     // set commctrl properties to current dialog settings
  94.     CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
  95.     int baudrate = 300;
  96.     TCHAR parity;
  97.     CString str;
  98.  
  99.     // calculate baud rate from corresponding radio button index
  100.     for (int nSetting = 0; nSetting<m_baud; nSetting++)
  101.         baudrate = baudrate << 1;
  102.  
  103.     switch(m_parity)
  104.     {
  105.         case 0: parity = (TCHAR)'N'; break;
  106.         case 1: parity = (TCHAR)'O'; break;
  107.         case 2: parity = (TCHAR)'E'; break;
  108.     }
  109.  
  110.     // update commctrl's Settings property
  111.     str.Format(_T("%d,%C,%d,%d"),baudrate,parity,m_databits+7,m_stopbits+1);
  112.     pMainWnd->GetCommCtrl()->SetSettings(str);
  113.  
  114.     // set echo, handshaking (flow) and comm port settings here!!!
  115.     pMainWnd->GetEditCtrl()->m_bEcho = m_echo;
  116.     pMainWnd->GetCommCtrl()->SetHandshaking(m_flow);
  117.     pMainWnd->GetCommCtrl()->SetCommPort(m_comport+1); // port settings start at 1
  118.  
  119.     EndDialog(IDOK);
  120. }
  121.