home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pc3270sa.zip / apps / winaping / mainfrm.cpp < prev    next >
Text File  |  2002-02-28  |  8KB  |  258 lines

  1. // mainfrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <afxpriv.h>          //for WM_IDLEUPDATECMDUI
  6. #include "winaping.h"
  7. #include "constdef.h"
  8.  
  9. #include "mainfrm.h"
  10. #include "setupdlg.h"
  11. #include "winapdoc.h"
  12. #include "goview.h"
  13.  
  14. #ifdef _DEBUG
  15. #undef THIS_FILE
  16. static char BASED_CODE THIS_FILE[] = __FILE__;
  17. #endif
  18.  
  19. /////////////////////////////////////////////////////////////////////////////
  20. // CMainFrame
  21.  
  22. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  23.  
  24. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  25.         //{{AFX_MSG_MAP(CMainFrame)
  26.         ON_WM_CREATE()
  27.         ON_MESSAGE(WM_APING_DONE, OnApingDone)    // user msg
  28.         ON_COMMAND(ID_ACTION_GO, OnActionGo)
  29.         ON_COMMAND(ID_ACTION_SETUP, OnActionSetup)
  30.         ON_COMMAND(ID_ACTION_STOP, OnActionStop)
  31.         ON_UPDATE_COMMAND_UI(ID_ACTION_GO, OnUpdateActionGo)
  32.         ON_UPDATE_COMMAND_UI(ID_ACTION_STOP, OnUpdateActionStop)
  33.         ON_UPDATE_COMMAND_UI(ID_ACTION_SETUP, OnUpdateActionSetup)
  34.         //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // arrays of IDs used to initialize control bars
  39.  
  40. // toolbar buttons - IDs are command buttons
  41. static UINT BASED_CODE buttons[] =
  42. {
  43.         // same order as in the bitmap 'toolbar.bmp'
  44.         ID_ACTION_GO,
  45.         ID_ACTION_STOP,
  46.         ID_ACTION_SETUP,
  47.         ID_APP_ABOUT,
  48. /*      ID_FILE_NEW,
  49.         ID_FILE_OPEN,
  50.         ID_FILE_SAVE,
  51.                 ID_SEPARATOR,
  52.         ID_EDIT_CUT,
  53.         ID_EDIT_COPY,
  54.         ID_EDIT_PASTE,
  55.                 ID_SEPARATOR,
  56.         ID_FILE_PRINT,          */
  57. };
  58.  
  59. static UINT BASED_CODE indicators[] =
  60. {
  61.         ID_SEPARATOR,           // status line indicator
  62. // -------------------------------
  63. // Remove all 3 default indicators
  64. //      ID_INDICATOR_CAPS,
  65. //      ID_INDICATOR_NUM,
  66. //      ID_INDICATOR_SCRL,
  67. };
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CMainFrame construction/destruction
  71.  
  72. CMainFrame::CMainFrame() : pDoc(NULL), pView(NULL), m_running(FALSE)
  73. {
  74. }
  75.  
  76. CMainFrame::~CMainFrame()
  77. {
  78.   if (pDoc != NULL)
  79.     if (pDoc->aping != NULL) delete pDoc->aping;
  80. }
  81.  
  82. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  83. {
  84.         if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  85.                 return -1;
  86.  
  87.         if (!m_wndToolBar.Create(this) ||
  88.                 !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
  89.                 !m_wndToolBar.SetButtons(buttons,
  90.                   sizeof(buttons)/sizeof(UINT)))
  91.         {
  92.                 TRACE0("Failed to create toolbar\n");
  93.                 return -1;      // fail to create
  94.         }
  95.         m_wndToolBar.SetWindowText(_T("winaping"));
  96. /* -------------------------------------------------------------------------
  97. // No Status bar display
  98.         if (!m_wndStatusBar.Create(this) ||
  99.                 !m_wndStatusBar.SetIndicators(indicators,
  100.                   sizeof(indicators)/sizeof(UINT)))
  101.         {
  102.                 TRACE0("Failed to create status bar\n");
  103.                 return -1;      // fail to create
  104.         }
  105. ------------------------------------------------------------------------- */
  106.  
  107.         // TODO: Delete these three lines if you don't want the toolbar to
  108.         //  be dockable
  109.         m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  110.         EnableDocking(CBRS_ALIGN_ANY);
  111.         DockControlBar(&m_wndToolBar);
  112.  
  113.         // TODO: Remove this if you don't want tool tips
  114.         m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  115.                 CBRS_TOOLTIPS | CBRS_FLYBY);
  116.  
  117.         PostMessage(WM_COMMAND, ID_ACTION_SETUP);
  118.  
  119.         return 0;
  120. }
  121.  
  122. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  123. {
  124.         // TODO: Add your specialized code here and/or call the base class
  125. //------------------------------------------------------------
  126. // Remove FWS_ADDTOTITLE if you don't want main window resizable
  127. //      cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE
  128. //              | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
  129. //-----------------------------------------------------------------
  130. // Hard code the size for now ....... Do a better way later .......
  131.         cs.cx = 33*13+6;
  132.         cs.cy = 59*7;
  133.         return CFrameWnd::PreCreateWindow(cs);
  134. }
  135.  
  136. /////////////////////////////////////////////////////////////////////////////
  137. // CMainFrame diagnostics
  138.  
  139. #ifdef _DEBUG
  140. void CMainFrame::AssertValid() const
  141. {
  142. //      CFrameWnd::AssertValid();          ????? ASSERT FAILED HERE ??????
  143. }
  144.  
  145. void CMainFrame::Dump(CDumpContext& dc) const
  146. {
  147.         CFrameWnd::Dump(dc);
  148. }
  149.  
  150. #endif //_DEBUG
  151.  
  152. /////////////////////////////////////////////////////////////////////////////
  153. // CMainFrame message handlers
  154.  
  155. void CMainFrame::OnActionGo()
  156. {
  157.   //----------------------------------------
  158.   //Initial house cleaning work before start
  159.   m_running     = TRUE;
  160.   pDoc->alloctime = 0;
  161.   pDoc->confirmtime = 0;
  162.   pDoc->partner.Empty();
  163.   pDoc->cmcall.Empty();
  164.   pDoc->cmrcmsg.Empty();
  165.   pDoc->stoprunning = FALSE;
  166.   pDoc->buffer = NULL;
  167.  
  168.   pView->Refresh0();
  169.   CString str = _T("Running...");
  170.   pView->RefreshMsg(str);
  171.   m_start_time = m_start_time.GetCurrentTime();
  172.  
  173.   //-------------------------------------------------------------------
  174.   //Create object aping (if not exist) and start a new execution thread
  175.   if (pDoc->aping == NULL) pDoc->aping = DEBUG_NEW Aping();
  176.  
  177. //pDoc->SetTitle((LPCTSTR)pDoc->pingInfo.destination);
  178.  
  179. //pRunningThread = (Aping*) AfxBeginThread(pDoc->aping->PingIt, (LPVOID)this);
  180.   pRunningThread = (Aping*) AfxBeginThread(pDoc->aping->PingIt, (LPVOID)this,
  181.                                            THREAD_PRIORITY_BELOW_NORMAL);
  182.   if (pRunningThread == NULL) {
  183.     AfxMessageBox("Creating worker thread failed .....");
  184.     PostMessage(WM_COMMAND, ID_APP_EXIT);
  185.     }
  186.  
  187. }
  188.  
  189. void CMainFrame::OnActionStop()
  190. {
  191.   pDoc->stoprunning = TRUE;
  192.   CString str = _T("Stopping ....");
  193.   pView->RefreshMsg(str);
  194.  
  195. }
  196.  
  197. void CMainFrame::OnActionSetup()
  198. {
  199.   //------------------------------------------------
  200.   // Get and save ptr to Doc & View
  201.   // Safe to do so since we only have 1 doc & 1 view
  202.   if (pDoc == NULL) {
  203.     pDoc  = (CWinapingDoc*) GetActiveDocument();
  204.     pView = (CGoView*) GetActiveView();
  205.     }
  206.  
  207.   CSetupdlg dlg(pDoc->pingInfo);
  208.   dlg.DoModal();
  209. }
  210.  
  211. void CMainFrame::OnUpdateActionGo(CCmdUI* pCmdUI)
  212. {
  213.   pCmdUI->Enable(!m_running);
  214.  
  215. }
  216.  
  217. void CMainFrame::OnUpdateActionStop(CCmdUI* pCmdUI)
  218. {
  219.   pCmdUI->Enable(m_running);
  220.  
  221. }
  222.  
  223. void CMainFrame::OnUpdateActionSetup(CCmdUI* pCmdUI)
  224. {
  225.   pCmdUI->Enable(!m_running);
  226.  
  227. }
  228.  
  229. LONG CMainFrame::OnApingDone(UINT a, LONG b) {
  230.   
  231.   SendMessageToDescendants(WM_IDLEUPDATECMDUI, (WPARAM) TRUE);
  232.  
  233.   CTime     m_end_time = m_end_time.GetCurrentTime();
  234.   CTimeSpan          t = m_end_time - m_start_time;
  235.   CString str = _T("Aping completed.");
  236.   if (pDoc->stoprunning) str = _T("Aping stopped.");
  237.   str += _T(" Total clock time =");
  238.   CString tmp;
  239.   UINT i = t.GetDays();
  240.   if (i>0) { tmp.Format(_T( "%d day"), i); str+=tmp; if (i>1) str+=_T("s"); }
  241.   i = t.GetHours();
  242.   if (i>0) { tmp.Format(_T(" %d hour"), i); str+=tmp; if (i>1) str+=_T("s"); }
  243.   i = t.GetMinutes();
  244.   if (i>0) { tmp.Format(_T(" %d minute"), i); str+=tmp; if (i>1) str+=_T("s"); }
  245.   i = t.GetSeconds();
  246.   tmp.Format(_T(" %d second"), i); str+=tmp; if (i>1) str+=_T("s");
  247.  
  248.   pView->RefreshMsg(str);
  249.   pDoc->stoprunning = FALSE;
  250.  
  251.   MessageBeep(0xFFFFFFFF);
  252.   // Moved the setting of m_running to ensure that the start button
  253.   // does not get enabled before the DONE processing is finished.
  254.   m_running = FALSE;
  255.   return 0;
  256. }
  257.  
  258.