home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n10 / cppq1195.exe / MAINFRM.CPP < prev    next >
C/C++ Source or Header  |  1995-10-01  |  7KB  |  282 lines

  1. ////////////////////////////////////////////////////////////////
  2. // Main frame window for TRACEWIN applet
  3. //
  4. #include "stdafx.h"
  5. #include "app.h"
  6. #include "mainfrm.h"
  7. #include "tracewin.h"
  8. #include <direct.h>
  9.  
  10. // Registered message sent from app to Trace Window
  11. //
  12. UINT WM_TRACE_MSG = RegisterWindowMessage(TRACEWND_MESSAGE);
  13.  
  14. // Registry key for profile settings
  15. //
  16. const char PROFILE[] = "Settings";
  17.  
  18. IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
  19.  
  20. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  21.     //{{AFX_MSG_MAP(CMainFrame)
  22.     ON_REGISTERED_MESSAGE(WM_TRACE_MSG, OnTraceMsg)
  23.     ON_WM_CREATE()
  24.     ON_WM_SETFOCUS()
  25.  
  26.     ON_COMMAND(ID_FILE_SAVE_AS,                    OnFileSaveAs)
  27.     ON_COMMAND(ID_OUTPUT_OFF,                        OnOutputOff)
  28.     ON_COMMAND(ID_OUTPUT_TO_FILE,                    OnOutputToFile)
  29.     ON_COMMAND(ID_OUTPUT_TO_WINDOW,                OnOutputToWindow)
  30.     ON_UPDATE_COMMAND_UI(ID_FILE_SAVE_AS,        OnUpdateFileSaveAs)
  31.     ON_UPDATE_COMMAND_UI(ID_OUTPUT_OFF,            OnUpdateOutputOff)
  32.     ON_UPDATE_COMMAND_UI(ID_OUTPUT_TO_FILE,    OnUpdateOutputToFile)
  33.     ON_UPDATE_COMMAND_UI(ID_OUTPUT_TO_WINDOW, OnUpdateOutputToWindow)
  34.     ON_WM_CLOSE()
  35.     //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37.  
  38. CMainFrame::CMainFrame() 
  39. {
  40.     // Load state from profile
  41.     m_nOutputWhere = ID_OUTPUT_TO_WINDOW;
  42.     CString dir = AfxGetApp()->GetProfileString(PROFILE,"Directory");
  43.     if (!dir.IsEmpty())
  44.         _chdir(dir);
  45. }
  46.  
  47. CMainFrame::~CMainFrame()
  48. {
  49. }
  50.  
  51. /////////////////
  52. // Override pre-create function to use "MfxTraceWindow" as 
  53. // class name so applications can find us.
  54. //
  55. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  56. {
  57.     static LPCSTR className = NULL;
  58.  
  59.     if (!CFrameWnd::PreCreateWindow(cs))
  60.         return FALSE;
  61.  
  62.     if (className==NULL) {
  63.         // One-time class registration
  64.         // The only purpose is to make the class name something meaningful
  65.         // instead of "Afx:0x4d:27:32:hup1hup:hike!"
  66.         //
  67.         WNDCLASS wndcls;
  68.         ::GetClassInfo(AfxGetInstanceHandle(), cs.lpszClass, &wndcls);
  69.         wndcls.lpszClassName = TRACEWND_CLASSNAME;
  70.         wndcls.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71.         VERIFY(AfxRegisterClass(&wndcls));
  72.         className=TRACEWND_CLASSNAME;
  73.     }
  74.     cs.lpszClass = className;
  75.  
  76.     // Load window position from profile
  77.     CWinApp *pApp = AfxGetApp();
  78.     cs.x = pApp->GetProfileInt(PROFILE, "x", CW_USEDEFAULT);
  79.     cs.y = pApp->GetProfileInt(PROFILE, "y", CW_USEDEFAULT);
  80.     cs.cx = pApp->GetProfileInt(PROFILE, "cx", CW_USEDEFAULT);
  81.     cs.cy = pApp->GetProfileInt(PROFILE, "cy", CW_USEDEFAULT);
  82.  
  83.     return TRUE;
  84. }
  85.  
  86. //////////////////
  87. // Main frame created.
  88. //
  89. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  90. {
  91.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  92.         return -1;
  93.  
  94.     // Create buffer window
  95.     if (!m_wndBuffer.Create(this, AFX_IDW_PANE_FIRST)) {
  96.         TRACE("Failed to create buffer window\n");
  97.         return -1;      // fail to create
  98.     }
  99.     TRACE("Created TRACE Window.\n");
  100.     return 0;
  101. }
  102.  
  103. //////////////////
  104. // If I get focus, give to buffer
  105. //
  106. void CMainFrame::OnSetFocus(CWnd* pOldWnd)
  107. {
  108.     m_wndBuffer.SetFocus();
  109. }
  110.  
  111. //////////////////
  112. // Got command message: pass to trace buffer
  113. // Without this, m_wndBuffer doesn't get a chance to handle 
  114. // menu commands with ON_COMMAND, etc.
  115. //
  116. BOOL CMainFrame::OnCmdMsg(UINT nID, int nCode, void* pExtra,
  117.     AFX_CMDHANDLERINFO* pHandlerInfo)
  118. {
  119.     if (m_wndBuffer.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
  120.         return TRUE;    // handled by buffer window
  121.  
  122.     return CFrameWnd::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
  123. }
  124.  
  125. //////////////////
  126. // Someone sent me a message: display it
  127. // Messages are passed as global atoms.
  128. //
  129. LRESULT CMainFrame::OnTraceMsg(WPARAM wParam, LPARAM)
  130. {
  131.     if (!wParam || m_nOutputWhere==ID_OUTPUT_OFF)
  132.         return 0;
  133.  
  134.     char buf[256];
  135.     UINT len = GlobalGetAtomName((ATOM)wParam, buf, sizeof(buf));
  136.  
  137.     if (m_nOutputWhere==ID_OUTPUT_TO_WINDOW) {
  138.  
  139.         // Convert \n to \n\r for Windows brain-damaged edit control
  140.         // It's 1995, and I'm still writing code like this!
  141.  
  142.         char buf2[512];
  143.         char* src = buf;
  144.         char* dst = buf2;
  145.         while (*src) {
  146.             if (*src == '\n')
  147.                 *dst++ = '\r';
  148.             *dst++ = *src++;
  149.         }
  150.         *dst = 0;
  151.  
  152.         // Append string to contents of trace buffer
  153.         m_wndBuffer.SetSel(-1, -1);        // end of edit text
  154.         m_wndBuffer.ReplaceSel(buf2);        // append string..
  155.         m_wndBuffer.SendMessage(EM_SCROLLCARET);    // ..and show caret
  156.  
  157.     } else if (m_nOutputWhere==ID_OUTPUT_TO_FILE) {
  158.         m_file.Write(buf, len);
  159.  
  160.     }
  161.  
  162.     return 0;
  163. }
  164.  
  165. //////////////////
  166. // Save contents of trace buffer to a file
  167. //
  168. void CMainFrame::OnFileSaveAs() 
  169. {
  170.     CFileDialog dlg(FALSE);
  171.     if (dlg.DoModal()!=IDOK) 
  172.         return;
  173.  
  174.     CFile f;
  175.     if (OpenFile(f, dlg.GetPathName())) {
  176.         char buf[256];
  177.         int count = m_wndBuffer.GetLineCount();
  178.         for (int line=0; line<count; line++) {
  179.             int len = m_wndBuffer.GetLine(line, buf, sizeof(buf)-1);
  180.             buf[len++]='\n';
  181.             f.Write(buf, len);
  182.         }
  183.         f.Close();
  184.     }
  185. }
  186.  
  187. //////////////////
  188. // Helper to open a file
  189. //
  190. BOOL CMainFrame::OpenFile(CFile& f, LPCSTR lpszPathName)
  191. {
  192.     BOOL bOpen = FALSE;
  193.     TRY {
  194.         bOpen = f.Open(lpszPathName,
  195.             CFile::modeCreate | CFile::modeWrite | CFile::shareDenyWrite);
  196.  
  197.     } CATCH_ALL(e) {
  198.     } END_CATCH_ALL
  199.  
  200.     if (!bOpen) {
  201.         MessageBox("Failed to open file.","TRACEWIN", MB_OK|MB_ICONEXCLAMATION);
  202.         MessageBeep(MB_ICONEXCLAMATION);
  203.     }
  204.  
  205.     return bOpen;
  206. }
  207.  
  208. void CMainFrame::OnUpdateFileSaveAs(CCmdUI* pCmdUI) 
  209. {
  210.     pCmdUI->Enable(!m_wndBuffer.IsEmpty());
  211. }
  212.  
  213. void CMainFrame::OnOutputOff() 
  214. {
  215.     if (m_file.m_hFile)
  216.         m_file.Close();
  217.     m_nOutputWhere = ID_OUTPUT_OFF;
  218. }
  219.  
  220. void CMainFrame::OnUpdateOutputOff(CCmdUI* pCmdUI) 
  221. {
  222.     pCmdUI->SetRadio(m_nOutputWhere==ID_OUTPUT_OFF);
  223. }
  224.  
  225. void CMainFrame::OnOutputToFile() 
  226. {
  227.     CFileDialog dlg(FALSE);
  228.     if (dlg.DoModal()!=IDOK) 
  229.         return;
  230.  
  231.     if (m_file.m_hFile)
  232.         m_file.Close();
  233.     if (OpenFile(m_file, dlg.GetPathName()))
  234.         m_nOutputWhere = ID_OUTPUT_TO_FILE;
  235. }
  236.  
  237. void CMainFrame::OnUpdateOutputToFile(CCmdUI* pCmdUI) 
  238. {
  239.     pCmdUI->SetRadio(m_nOutputWhere==ID_OUTPUT_TO_FILE);
  240. }
  241.  
  242. void CMainFrame::OnOutputToWindow() 
  243. {
  244.     if (m_file.m_hFile)
  245.         m_file.Close();
  246.     m_nOutputWhere = ID_OUTPUT_TO_WINDOW;
  247. }
  248.  
  249. void CMainFrame::OnUpdateOutputToWindow(CCmdUI* pCmdUI) 
  250. {
  251.     pCmdUI->SetRadio(m_nOutputWhere==ID_OUTPUT_TO_WINDOW);
  252. }
  253.  
  254. //////////////////
  255. // Frame window closed: save position, size in profile.
  256. //
  257. void CMainFrame::OnClose() 
  258. {
  259.     SaveSettings();
  260.     CFrameWnd::OnClose();
  261. }
  262.  
  263. //////////////////
  264. // Save window pos, etc. in registration file.
  265. //
  266. void CMainFrame::SaveSettings()
  267. {
  268.     CRect rc;
  269.     GetWindowRect(&rc);
  270.     
  271.     CWinApp *pApp = AfxGetApp();
  272.     pApp->WriteProfileInt(PROFILE, "x", rc.left);
  273.     pApp->WriteProfileInt(PROFILE, "y", rc.top);
  274.     pApp->WriteProfileInt(PROFILE, "cx", rc.Width());
  275.     pApp->WriteProfileInt(PROFILE, "cy", rc.Height());
  276.     pApp->WriteProfileInt(PROFILE, "TraceWhere", m_nOutputWhere);
  277.  
  278.     char buf[_MAX_PATH];
  279.     _getcwd(buf, sizeof(buf));
  280.     pApp->WriteProfileString(PROFILE, "Directory", buf);
  281. }
  282.