home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 8090 / ModelEdit.7z / loadltadlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-03-08  |  5.5 KB  |  261 lines

  1. // loadltadlg.cpp : implementation file
  2. //
  3.  
  4. //include stuff for thread sync
  5. #include <afxmt.h>
  6. #include "stdafx.h"
  7. #include "modeledit.h"
  8. #include "precompile.h"
  9. #include "loadltadlg.h"
  10.  
  11.  
  12. //for writing out the log file
  13. #if _MSC_VER >= 1300
  14. #include <fstream>
  15. #else
  16. #include <fstream.h>
  17. #endif
  18.  
  19. //used to sync access to resources
  20. CCriticalSection    g_LoadLogPipeCS;
  21. CCriticalSection    g_ThreadDoneCS;
  22.  
  23. //cross thread resources
  24. CString CLoadLTADlg::sm_sLoadLogPipe;
  25. BOOL    CLoadLTADlg::sm_bLoadThreadDone = TRUE;
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CLoadLTADlg dialog
  29.  
  30.  
  31. CLoadLTADlg::CLoadLTADlg(bool bAutoConfirm, CWnd* pParent /*=NULL*/)
  32.     : CDialog(CLoadLTADlg::IDD, pParent),
  33.       m_bPrevLoaderDone(TRUE), m_bAutoConfirm(bAutoConfirm)
  34. {
  35.     //{{AFX_DATA_INIT(CLoadLTADlg)
  36.         // NOTE: the ClassWizard will add member initialization here
  37.     //}}AFX_DATA_INIT
  38. }
  39.  
  40.  
  41. void CLoadLTADlg::DoDataExchange(CDataExchange* pDX)
  42. {
  43.     CDialog::DoDataExchange(pDX);
  44.     //{{AFX_DATA_MAP(CLoadLTADlg)
  45.         // NOTE: the ClassWizard will add DDX and DDV calls here
  46.     //}}AFX_DATA_MAP
  47. }
  48.  
  49.  
  50. BEGIN_MESSAGE_MAP(CLoadLTADlg, CDialog)
  51.     //{{AFX_MSG_MAP(CLoadLTADlg)
  52.     ON_BN_CLICKED(IDC_SAVELOADLOG, OnSaveLoadLog)
  53.     ON_WM_TIMER()
  54.     ON_WM_DESTROY()
  55.     //}}AFX_MSG_MAP
  56. END_MESSAGE_MAP()
  57.  
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CLoadLTADlg message handlers
  60.  
  61. void CLoadLTADlg::OnSaveLoadLog() 
  62. {
  63.     //load in all the strings
  64.     CString sDefFilename, sDefExtension, sFilter;
  65.     sDefFilename.LoadString(IDS_DEFLTALOGFILENAME);
  66.     sDefExtension.LoadString(IDS_DEFLTALOGEXTENSION);
  67.     sFilter.LoadString(IDS_LTALOGFILTER);
  68.  
  69.     //create the file save dialog
  70.     CFileDialog Dlg(FALSE, sDefExtension, sDefFilename, 
  71.                     OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, sFilter);
  72.  
  73.     //put up the dialog
  74.     if(Dlg.DoModal() == IDOK)
  75.     {
  76.         //get the text from the window
  77.         CString sLogText;
  78.         ((CEdit*)GetDlgItem(IDC_LOADLOG))->GetWindowText(sLogText);
  79.  
  80.         //open up the file
  81. #if _MSC_VER >= 1300
  82.         std::ofstream OutFile(Dlg.GetPathName());
  83. #else
  84.         ofstream OutFile(Dlg.GetPathName());
  85. #endif
  86.  
  87.         //check to ensure that the file opened correctly
  88.         if(OutFile.fail())
  89.         {
  90.             //failed to open the file, throw up a message box and bail
  91.             CString sError;
  92.             sError.Format("Unable to open file: %s", Dlg.GetPathName());
  93.             MessageBox(sError, "Error", MB_ICONEXCLAMATION);
  94.             return;
  95.         }
  96.  
  97.         //write out and close the file
  98.         OutFile << (const char*)(sLogText);
  99.         OutFile.close();
  100.     }    
  101. }
  102.  
  103. void CLoadLTADlg::OnTimer(UINT nIDEvent) 
  104. {
  105.     CEdit* pEdit = ((CEdit*)GetDlgItem(IDC_LOADLOG));
  106.     pEdit->SetSel(-1, 0, TRUE);
  107.  
  108.     if(nIDEvent == TIMER_ID)
  109.     {
  110.         BOOL bDone = IsDone();
  111.  
  112.         //see if we need to update the load log
  113.         //(the check is done so that when it is running, we don't refresh it
  114.         //continually)
  115.         if(m_bPrevLoaderDone == FALSE)
  116.         {
  117.             UpdateLoadLog();
  118.         }
  119.  
  120.         //see if the finishing state has finished at all
  121.         if(bDone != m_bPrevLoaderDone)
  122.         {
  123.             ((CButton*)GetDlgItem(IDC_SAVELOADLOG))->EnableWindow(bDone);
  124.             ((CButton*)GetDlgItem(IDOK))->EnableWindow(bDone);
  125.             m_bPrevLoaderDone = bDone;
  126.         }
  127.  
  128.         // automatically close the dialog on load completion
  129.         if( m_bAutoConfirm && bDone && m_bPrevLoaderDone )
  130.         {
  131.             OnOK();
  132.         }
  133.     }
  134.     
  135.     CDialog::OnTimer(nIDEvent);
  136. }
  137.  
  138.  
  139. //Thread safe way to add text onto the end of the load log
  140. void CLoadLTADlg::AppendLoadLog(const char* pszString)
  141. {
  142.     g_LoadLogPipeCS.Lock(INFINITE);
  143.  
  144.     sm_sLoadLogPipe += pszString;
  145.  
  146.     g_LoadLogPipeCS.Unlock();
  147. }
  148.  
  149. //Thread safe way to set if the loading thread is done
  150. void CLoadLTADlg::SetLoadThreadDone(BOOL bDone)
  151. {
  152.     g_ThreadDoneCS.Lock(INFINITE);
  153.  
  154.     sm_bLoadThreadDone = bDone;
  155.     
  156.     g_ThreadDoneCS.Unlock();
  157. }
  158.  
  159.  
  160. //Thread safe way to update the load log
  161. void CLoadLTADlg::UpdateLoadLog()
  162. {
  163.  
  164.     CString sTextToAdd;
  165.  
  166.     g_LoadLogPipeCS.Lock(INFINITE);
  167.  
  168.  
  169.     sTextToAdd = sm_sLoadLogPipe;
  170.     sm_sLoadLogPipe = "";
  171.  
  172.     g_LoadLogPipeCS.Unlock();
  173.  
  174.     if(!sTextToAdd.IsEmpty())
  175.     {
  176.         CString sLogText;
  177.         CEdit* pEdit = ((CEdit*)GetDlgItem(IDC_LOADLOG));
  178.  
  179.         pEdit->GetWindowText(sLogText);
  180.  
  181.         sLogText += sTextToAdd;
  182.     
  183.         pEdit->SetWindowText(sLogText);
  184.  
  185.         pEdit->LineScroll(pEdit->GetLineCount());
  186.     }
  187. }
  188.  
  189. //Thread safe way to check the done flag, and update the dialog box
  190. //appropriately
  191. BOOL CLoadLTADlg::IsDone()
  192. {
  193.     BOOL bDone = FALSE;
  194.  
  195.     g_ThreadDoneCS.Lock(INFINITE);
  196.  
  197.     bDone = sm_bLoadThreadDone;
  198.  
  199.     g_ThreadDoneCS.Unlock();
  200.  
  201.     return bDone;
  202.  
  203. }
  204.  
  205. BOOL CLoadLTADlg::OnInitDialog() 
  206. {
  207.     CDialog::OnInitDialog();
  208.     
  209.     SetTimer(TIMER_ID, 10, NULL);
  210.  
  211.     //set the text on the dialog to reflect the filename that is being loaded
  212.     CString sText;
  213.     sText.Format("Loading: %s", m_sFilename);
  214.     ((CStatic*)GetDlgItem(IDC_LOADFILENAME))->SetWindowText(sText);
  215.  
  216.  
  217.     //set the dialog box text to whatever is already in the string
  218.     UpdateLoadLog();
  219.  
  220.     
  221.     return TRUE;  // return TRUE unless you set the focus to a control
  222.                   // EXCEPTION: OCX Property Pages should return FALSE
  223. }
  224.  
  225. void CLoadLTADlg::OnDestroy() 
  226. {
  227.     KillTimer(TIMER_ID);
  228.  
  229.     CDialog::OnDestroy();
  230.     
  231. }
  232.  
  233. void CLoadLTADlg::OnOK() 
  234. {
  235.     //don't let them quit while the thread is running
  236.     if(IsDone() == FALSE)
  237.         return;
  238.     
  239.     CDialog::OnOK();
  240. }
  241.  
  242. void CLoadLTADlg::OnCancel() 
  243. {
  244.     //don't let them quit while the thread is running
  245.     if(IsDone() == FALSE)
  246.         return;
  247.     
  248.     CDialog::OnCancel();
  249. }
  250.  
  251. //Thread safe way to clear the load log
  252. void CLoadLTADlg::ClearLoadLog()
  253. {
  254.     g_LoadLogPipeCS.Lock(INFINITE);
  255.  
  256.     sm_sLoadLogPipe = "";
  257.  
  258.     g_LoadLogPipeCS.Unlock();
  259. }
  260.  
  261.