home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CHATTER.PAK / SENDVW.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  3.9 KB  |  190 lines

  1. // sendvw.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 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 "chatter.h"
  15.  
  16. #include "chatdoc.h"
  17. #include "sendvw.h"
  18.  
  19. #include <stdlib.h>
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. static CString RandomString();
  27.  
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CSendView
  30.  
  31. IMPLEMENT_DYNCREATE(CSendView, CEditView)
  32.  
  33. BEGIN_MESSAGE_MAP(CSendView, CEditView)
  34.     //{{AFX_MSG_MAP(CSendView)
  35.     ON_WM_CHAR()
  36.     ON_WM_TIMER()
  37.     ON_COMMAND(ID_AUTOCHATTER, OnAutochatter)
  38.     ON_UPDATE_COMMAND_UI(ID_AUTOCHATTER, OnUpdateAutochatter)
  39.     //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CSendView construction/destruction
  44.  
  45. CSendView::CSendView()
  46. {
  47.     m_TimerID = 0;
  48. }
  49.  
  50. CSendView::~CSendView()
  51. {
  52. }
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CSendView drawing
  57.  
  58. void CSendView::OnDraw(CDC* pDC)
  59. {
  60. }
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // CSendView diagnostics
  64.  
  65. #ifdef _DEBUG
  66. void CSendView::AssertValid() const
  67. {
  68.     CEditView::AssertValid();
  69. }
  70.  
  71. void CSendView::Dump(CDumpContext& dc) const
  72. {
  73.     CEditView::Dump(dc);
  74. }
  75.  
  76. CChatDoc* CSendView::GetDocument() // non-debug version is inline
  77. {
  78.     return STATIC_DOWNCAST(CChatDoc, m_pDocument);
  79. }
  80. #endif //_DEBUG
  81.  
  82. /////////////////////////////////////////////////////////////////////////////
  83. // CSendView message handlers
  84.  
  85. BOOL CSendView::PreCreateWindow(CREATESTRUCT& cs) 
  86. {
  87.     BOOL ret = CEditView::PreCreateWindow(cs);
  88.     cs.style = AFX_WS_DEFAULT_VIEW | WS_VSCROLL | ES_AUTOHSCROLL |
  89.         ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL;
  90.     return ret;
  91. }
  92.  
  93. void CSendView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
  94. {
  95.     if ((nChar != VK_RETURN) || (nRepCnt!=1))
  96.     {
  97.         CEditView::OnChar(nChar, nRepCnt, nFlags);
  98.         return;
  99.     }
  100.     else
  101.     {
  102.         CChatDoc* pDoc = GetDocument();
  103.         ASSERT_VALID(pDoc);
  104.  
  105.         CString strText;
  106.         GetEditCtrl().GetWindowText(strText);
  107.  
  108.         pDoc->SendMsg(strText);
  109.  
  110.         strText=_T("");
  111.         GetEditCtrl().SetWindowText(strText);
  112.     }
  113. }
  114.  
  115. BOOL CSendView::DestroyWindow() 
  116. {
  117.     if (m_TimerID != 0)
  118.         KillTimer(m_TimerID);
  119.         
  120.     return CEditView::DestroyWindow();
  121. }
  122.  
  123. void CSendView::OnTimer(UINT nIDEvent) 
  124. {
  125.     CChatDoc* pDoc = GetDocument();
  126.     ASSERT_VALID(pDoc);
  127.  
  128.     if (pDoc->m_bAutoChat)
  129.     {
  130.         CString temp = RandomString();
  131.  
  132.         if (!temp.IsEmpty())
  133.             pDoc->SendMsg(temp);
  134.     }
  135.     else
  136.     {
  137.         KillTimer(m_TimerID);
  138.         m_TimerID = 0;
  139.     }
  140.     
  141.     CEditView::OnTimer(nIDEvent);
  142. }
  143.  
  144. void CSendView::OnAutochatter() 
  145. {
  146.     CChatDoc* pDoc = GetDocument();
  147.     ASSERT_VALID(pDoc);
  148.  
  149.     if (!pDoc->m_bAutoChat)
  150.     {
  151.         pDoc->m_bAutoChat = TRUE;
  152.         m_TimerID = SetTimer(1, 1000, NULL);
  153.     }
  154.     else
  155.     {
  156.         pDoc->m_bAutoChat = FALSE;
  157.         KillTimer(m_TimerID);
  158.     }
  159. }
  160.  
  161. void CSendView::OnUpdateAutochatter(CCmdUI* pCmdUI) 
  162. {        
  163.     CChatDoc* pDoc = GetDocument();
  164.     ASSERT_VALID(pDoc);
  165.  
  166.     pCmdUI->SetCheck(pDoc->m_bAutoChat);
  167. }
  168.  
  169. static CString RandomString()
  170. {
  171.     CString strResult;
  172.  
  173.     UINT val1 = (rand()%36) + IDS_STATEMENT01;
  174.     UINT val2 = (rand()%6) + IDS_ADJECTIVE01;
  175.  
  176.     CString strStatement, strAdjective;
  177.  
  178.     if(!strStatement.LoadString(val1))
  179.         return strResult;
  180.  
  181.     if(!strAdjective.LoadString(val2))
  182.         return strResult;
  183.  
  184.     wsprintf(strResult.GetBuffer(strStatement.GetLength()+strAdjective.GetLength()),
  185.         (LPCTSTR)strStatement,(LPCTSTR)strAdjective);
  186.     strResult.ReleaseBuffer();
  187.  
  188.     return strResult;
  189. }
  190.