home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example5 / Example5Dlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  7.2 KB  |  309 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. // Example5Dlg.cpp : implementation file
  12. // The server select dialog
  13. //
  14.  
  15. #include <string>
  16. #include "stdafx.h"
  17. #include "Example5.h"
  18. #include "Example5Dlg.h"
  19. #include "ChatDlg.h"
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CAboutDlg dialog used for App About
  29.  
  30. class CAboutDlg : public CDialog
  31. {
  32. public:
  33.     CAboutDlg();
  34.  
  35. // Dialog Data
  36.     //{{AFX_DATA(CAboutDlg)
  37.     enum { IDD = IDD_ABOUTBOX };
  38.     //}}AFX_DATA
  39.  
  40.     // ClassWizard generated virtual function overrides
  41.     //{{AFX_VIRTUAL(CAboutDlg)
  42.     protected:
  43.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  44.     //}}AFX_VIRTUAL
  45.  
  46. // Implementation
  47. protected:
  48.     //{{AFX_MSG(CAboutDlg)
  49.     //}}AFX_MSG
  50.     DECLARE_MESSAGE_MAP()
  51. };
  52.  
  53. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  54. {
  55.     //{{AFX_DATA_INIT(CAboutDlg)
  56.     //}}AFX_DATA_INIT
  57. }
  58.  
  59. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61.     CDialog::DoDataExchange(pDX);
  62.     //{{AFX_DATA_MAP(CAboutDlg)
  63.     //}}AFX_DATA_MAP
  64. }
  65.  
  66. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  67.     //{{AFX_MSG_MAP(CAboutDlg)
  68.         // No message handlers
  69.     //}}AFX_MSG_MAP
  70. END_MESSAGE_MAP()
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CExample5Dlg dialog
  74.  
  75. CExample5Dlg::CExample5Dlg(CWnd* pParent /*=NULL*/)
  76.     : CDialog(CExample5Dlg::IDD, pParent)
  77. {
  78.     //{{AFX_DATA_INIT(CExample5Dlg)
  79.     //}}AFX_DATA_INIT
  80.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  81.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  82. }
  83.  
  84. void CExample5Dlg::DoDataExchange(CDataExchange* pDX)
  85. {
  86.     CDialog::DoDataExchange(pDX);
  87.     //{{AFX_DATA_MAP(CExample5Dlg)
  88.     DDX_Control(pDX, IDC_CHECK1, m_AllowSpider);
  89.     DDX_Control(pDX, IDC_CHECK2, m_CanBeMaster);
  90.     DDX_Control(pDX, IDC_EDIT2, mChatHandle);
  91.     DDX_Control(pDX, IDC_EDIT1, mJoinEdit);
  92.     DDX_Control(pDX, IDC_LIST1, mServerListControl);
  93.     //}}AFX_DATA_MAP
  94. }
  95.  
  96. BEGIN_MESSAGE_MAP(CExample5Dlg, CDialog)
  97.     //{{AFX_MSG_MAP(CExample5Dlg)
  98.     ON_WM_SYSCOMMAND()
  99.     ON_WM_PAINT()
  100.     ON_WM_QUERYDRAGICON()
  101.     ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
  102.     ON_WM_TIMER()
  103.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  104.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  105.     ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
  106.     ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
  107.     ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
  108.     ON_BN_CLICKED(IDC_CHECK2, OnCheck2)
  109.     //}}AFX_MSG_MAP
  110. END_MESSAGE_MAP()
  111.  
  112. static char *names[8] = {"Fred","John","Clive","Morph","Seb","Wibble","Yippee","Hello"};
  113.  
  114. BOOL CExample5Dlg::OnInitDialog()
  115. {
  116.     CDialog::OnInitDialog();
  117.  
  118.     // Add "About..." menu item to system menu.
  119.  
  120.     // IDM_ABOUTBOX must be in the system command range.
  121.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  122.     ASSERT(IDM_ABOUTBOX < 0xF000);
  123.  
  124.     m_AllowSpider.SetCheck(1);
  125.     m_CanBeMaster.SetCheck(1);
  126.  
  127.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  128.     if (pSysMenu != NULL)
  129.     {
  130.         CString strAboutMenu;
  131.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  132.         if (!strAboutMenu.IsEmpty())
  133.         {
  134.             pSysMenu->AppendMenu(MF_SEPARATOR);
  135.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  136.         }
  137.     }
  138.  
  139.     // Set the icon for this dialog.  The framework does this automatically
  140.     //  when the application's main window is not a dialog
  141.     SetIcon(m_hIcon, TRUE);            // Set big icon
  142.     SetIcon(m_hIcon, FALSE);        // Set small icon
  143.  
  144.     mXPSession = ((CExample5App *)AfxGetApp())->mXPSession;
  145.     
  146.     SetTimer(1,500,0);
  147.     OnButton3();
  148.  
  149.     CTime time = CTime::GetCurrentTime();
  150.     int seed = time.GetSecond();
  151.     srand(seed);
  152.     int index = rand() & 7;
  153.  
  154.     mChatHandle.SetWindowText(names[index]);
  155.     
  156.     return TRUE;  // return TRUE  unless you set the focus to a control
  157. }
  158.  
  159. void CExample5Dlg::OnSysCommand(UINT nID, LPARAM lParam)
  160. {
  161.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  162.     {
  163.         CAboutDlg dlgAbout;
  164.         dlgAbout.DoModal();
  165.     }
  166.     else
  167.     {
  168.         CDialog::OnSysCommand(nID, lParam);
  169.     }
  170. }
  171.  
  172. // If you add a minimize button to your dialog, you will need the code below
  173. //  to draw the icon.  For MFC applications using the document/view model,
  174. //  this is automatically done for you by the framework.
  175.  
  176. void CExample5Dlg::OnPaint() 
  177. {
  178.     if (IsIconic())
  179.     {
  180.         CPaintDC dc(this); // device context for painting
  181.  
  182.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  183.  
  184.         // Center icon in client rectangle
  185.         int cxIcon = GetSystemMetrics(SM_CXICON);
  186.         int cyIcon = GetSystemMetrics(SM_CYICON);
  187.         CRect rect;
  188.         GetClientRect(&rect);
  189.         int x = (rect.Width() - cxIcon + 1) / 2;
  190.         int y = (rect.Height() - cyIcon + 1) / 2;
  191.  
  192.         // Draw the icon
  193.         dc.DrawIcon(x, y, m_hIcon);
  194.     }
  195.     else
  196.     {
  197.         CDialog::OnPaint();
  198.     }
  199. }
  200.  
  201. // The system calls this to obtain the cursor to display while the user drags
  202. //  the minimized window.
  203. HCURSOR CExample5Dlg::OnQueryDragIcon()
  204. {
  205.     return (HCURSOR) m_hIcon;
  206. }
  207.  
  208. void CExample5Dlg::OnButton3() 
  209. {
  210.     // TODO: Add your control notification handler code here
  211.  
  212.     mServerListControl.ResetContent();
  213.     mXPSession->Find();
  214. }
  215.  
  216. void CExample5Dlg::OnTimer(UINT nIDEvent) 
  217. {
  218.     std::string url;
  219.     // TODO: Add your message handler code here and/or call default
  220.     url = mXPSession->EnumerateFound();
  221.     if (url != "")
  222.     {
  223.         mServerListControl.AddString(url.c_str());
  224.     }
  225.     
  226.     CDialog::OnTimer(nIDEvent);
  227. }
  228.  
  229. void CExample5Dlg::OnButton2() 
  230. {
  231.     // Try to get a session allocated on port 2050
  232.     mXPSession->SetGameChannel(2050);
  233.     mXPSession->Create(std::string("Example5"));
  234.  
  235.     ShowWindow(SW_MINIMIZE);
  236.     CChatDlg dlg2;
  237.     mChatHandle.GetWindowText(dlg2.mName,64);
  238.     int nResponse = dlg2.DoModal();
  239.  
  240.  
  241.     EndDialog(IDOK);
  242. }
  243.  
  244. void CExample5Dlg::OnButton1() 
  245. {
  246.     // TODO: Add your control notification handler code here
  247.     char buffer[512];
  248.     mJoinEdit.GetWindowText(buffer,sizeof(buffer));
  249.     if (strlen(buffer) > 0)
  250.     {
  251.         mXPSession->Join(std::string(buffer));
  252.  
  253.         ShowWindow(SW_MINIMIZE);
  254.         CChatDlg dlg2;
  255.         mChatHandle.GetWindowText(dlg2.mName,64);
  256.         int nResponse = dlg2.DoModal();
  257.  
  258.         EndDialog(IDOK);
  259.     }
  260. }
  261.  
  262. void CExample5Dlg::OnSelchangeList1() 
  263. {
  264.     // TODO: Add your control notification handler code here
  265.     int sel = mServerListControl.GetCurSel();
  266.     if (sel != LB_ERR)
  267.     {
  268.         char buffer[512];
  269.         mServerListControl.GetText(sel,buffer);
  270.         mJoinEdit.SetWindowText(buffer);
  271.     }
  272. }
  273.  
  274. void CExample5Dlg::OnChangeEdit2() 
  275. {
  276.     // TODO: If this is a RICHEDIT control, the control will not
  277.     // send this notification unless you override the CDialog::OnInitDialog()
  278.     // function and call CRichEditCtrl().SetEventMask()
  279.     // with the ENM_CHANGE flag ORed into the mask.
  280.     
  281.     // TODO: Add your control notification handler code here
  282. }
  283.  
  284.  
  285. void CExample5Dlg::OnCheck1() 
  286. {
  287.     if (m_AllowSpider.GetCheck())
  288.     {
  289.         mXPSession->SetCanSpider(true);
  290.     }
  291.     else
  292.     {
  293.         mXPSession->SetCanSpider(false);
  294.     }
  295. }
  296.  
  297. void CExample5Dlg::OnCheck2() 
  298. {
  299.     if (m_CanBeMaster.GetCheck())
  300.     {
  301.         mXPSession->SetCanBecomeMaster(true);
  302.     }
  303.     else
  304.     {
  305.         mXPSession->SetCanBecomeMaster(false);
  306.     }
  307. }
  308.  
  309.