ChatDlg.cpp
上传用户:zslianheng
上传日期:2013-04-03
资源大小:946k
文件大小:5k
源码类别:

Linux/Unix编程

开发平台:

Visual C++

  1. /***************************************************************************
  2.  *                                                                         *
  3.  *   This program is free software; you can redistribute it and/or modify  *
  4.  *   it under the terms of the GNU General Public License as published by  *
  5.  *   the Free Software Foundation; either version 2 of the License, or     *
  6.  *   (at your option) any later version.                                   *
  7.  *                                                                         *
  8.  *   copyright            : (C) 2002 by Zhang Yong                         *
  9.  *   email                : z-yong163@163.com                              *
  10.  ***************************************************************************/
  11. // ChatDlg.cpp : implementation file
  12. //
  13. #include "stdafx.h"
  14. #include "chat.h"
  15. #include "contactinfo.h"
  16. #include "icqlinkbase.h"
  17. #include "ChatDlg.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. #define IDT_TOTAL_TIME 1001
  24. static CWnd *pWnd;
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CChatDlg dialog
  27. static void CALLBACK onTimer(HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime)
  28. {
  29. KillTimer(hwnd, idEvent);
  30. delete pWnd;
  31. }
  32. CChatDlg::CChatDlg(ChatSession *s, CWnd* pParent /*=NULL*/)
  33. : CDialog(CChatDlg::IDD, pParent)
  34. {
  35. //{{AFX_DATA_INIT(CChatDlg)
  36. // NOTE: the ClassWizard will add member initialization here
  37. //}}AFX_DATA_INIT
  38. session = s;
  39. tcp = s->tcp;
  40. icqLink = tcp->getLink();
  41. }
  42. CChatDlg::~CChatDlg()
  43. {
  44. if (session)
  45. delete session;
  46. }
  47. BOOL CChatDlg::initSpeech()
  48. {
  49. waveIn.open(1, 8000, session->getBitsPerSample(), m_hWnd);
  50. int n = session->getFrameSize() * 10;
  51. waveIn.addBuffer(n);
  52. waveIn.addBuffer(n);
  53. waveOut.open(1, 8000, session->getBitsPerSample(), m_hWnd);
  54. m_sendVol.SetRange32(0, 32768);
  55. m_recvVol.SetRange32(0, 32768);
  56. waveIn.start();
  57. return TRUE;
  58. }
  59. void CChatDlg::onChatText(const char *text)
  60. {
  61. SetDlgItemText(IDC_RECV_TEXT, text);
  62. }
  63. void CChatDlg::onSpeechData(const char *frame, int n)
  64. {
  65. short val = *(short *) frame;
  66. m_recvVol.SetPos(abs(val));
  67. waveOut.play(frame, n);
  68. }
  69. void CChatDlg::onClose()
  70. {
  71. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  72. CString text;
  73. text.LoadString(IDS_REMOTE_CLOSE);
  74. MessageBox(text);
  75. DestroyWindow();
  76. }
  77. void CChatDlg::DoDataExchange(CDataExchange* pDX)
  78. {
  79. CDialog::DoDataExchange(pDX);
  80. //{{AFX_DATA_MAP(CChatDlg)
  81. DDX_Control(pDX, IDC_VOLUME_SEND, m_sendVol);
  82. DDX_Control(pDX, IDC_VOLUME_RECV, m_recvVol);
  83. DDX_Control(pDX, IDC_SEND_TEXT, m_sendEdit);
  84. DDX_Control(pDX, IDC_CONTACT_FACE, m_contactFace);
  85. DDX_Control(pDX, IDC_MY_FACE, m_myFace);
  86. //}}AFX_DATA_MAP
  87. }
  88. BEGIN_MESSAGE_MAP(CChatDlg, CDialog)
  89. //{{AFX_MSG_MAP(CChatDlg)
  90. ON_WM_TIMER()
  91. ON_EN_CHANGE(IDC_SEND_TEXT, OnChangeSendText)
  92. //}}AFX_MSG_MAP
  93. ON_MESSAGE(MM_WIM_DATA, onWaveInData)
  94. ON_MESSAGE(MM_WOM_DONE, onWaveOutDone)
  95. END_MESSAGE_MAP()
  96. /////////////////////////////////////////////////////////////////////////////
  97. // CChatDlg message handlers
  98. BOOL CChatDlg::OnInitDialog() 
  99. {
  100. CDialog::OnInitDialog();
  101. HICON icon = AfxGetApp()->LoadIcon(IDI_CHAT);
  102. SetIcon(icon, FALSE);
  103. ContactInfo *my = icqLink->getContactInfo(0);
  104. CString str;
  105. str.Format("MyICQ#: %lu", my->uin);
  106. m_myFace.SetIcon((HICON) icqLink->getFaceIcon(my->face, STATUS_ONLINE));
  107. SetDlgItemText(IDC_MY_UIN, str);
  108. ContactInfo *c = icqLink->getContactInfo(tcp->getRemoteUIN());
  109. str.Format("MyICQ#: %lu", c->uin);
  110. m_contactFace.SetIcon((HICON) icqLink->getFaceIcon(c->face, STATUS_ONLINE));
  111. SetDlgItemText(IDC_CONTACT_UIN, str);
  112. str.Format(IDS_CHAT_TITLE, my->nick.c_str(), c->nick.c_str());
  113. SetWindowText(str);
  114. startTime = CTime::GetCurrentTime();
  115. SetTimer(IDT_TOTAL_TIME, 1000, NULL);
  116. initSpeech();
  117. return TRUE;  // return TRUE unless you set the focus to a control
  118.               // EXCEPTION: OCX Property Pages should return FALSE
  119. }
  120. void CChatDlg::OnTimer(UINT nIDEvent) 
  121. {
  122. if (nIDEvent == IDT_TOTAL_TIME) {
  123. CTime now = CTime::GetCurrentTime();
  124. CTimeSpan span = now - startTime;
  125. SetDlgItemText(IDC_TOTAL_TIME, span.Format("%H:%M:%S"));
  126. } else
  127. CDialog::OnTimer(nIDEvent);
  128. }
  129. void CChatDlg::OnChangeSendText() 
  130. {
  131. CString str;
  132. GetDlgItemText(IDC_SEND_TEXT, str);
  133. session->sendChatText(str);
  134. }
  135. void CChatDlg::OnCancel()
  136. {
  137. CString str;
  138. str.LoadString(IDS_PROMPT_CLOSE);
  139. if (MessageBox(str, NULL, MB_YESNO) == IDYES)
  140. DestroyWindow();
  141. }
  142. void CChatDlg::PostNcDestroy() 
  143. {
  144. session->setListener(NULL);
  145. pWnd = this;
  146. ::SetTimer(NULL, 0, 0, onTimer);
  147. }
  148. LRESULT CChatDlg::onWaveInData(WPARAM wParam, LPARAM lParam)
  149. {
  150. HWAVEIN hwi = (HWAVEIN) wParam;
  151. LPWAVEHDR pwh = (LPWAVEHDR) lParam;
  152. waveInUnprepareHeader(hwi, pwh, sizeof(WAVEHDR));
  153. short val = *(short *) pwh->lpData;
  154. m_sendVol.SetPos(abs(val));
  155. session->sendSpeechData(pwh->lpData, pwh->dwBytesRecorded);
  156. waveInPrepareHeader(hwi, pwh, sizeof(WAVEHDR));
  157. waveInAddBuffer(hwi, pwh, sizeof(WAVEHDR));
  158. return 0;
  159. }
  160. LRESULT CChatDlg::onWaveOutDone(WPARAM wParam, LPARAM lParam)
  161. {
  162. HWAVEOUT hwo = (HWAVEOUT) wParam;
  163. LPWAVEHDR pwh = (LPWAVEHDR) lParam;
  164. waveOutUnprepareHeader(hwo, pwh, sizeof(WAVEHDR));
  165. delete []pwh->lpData;
  166. delete pwh;
  167. return 0;
  168. }