win95-initial-port-20010430
[openafs.git] / src / WINNT / win9xpanel / MyFrame.cpp
1 // MyFrame.cpp : implementation file
2 // frame class used to demonstrate UI-threads
3 /* Copyright 2000, International Business Machines Corporation and others.
4         All Rights Reserved.
5  
6         This software has been released under the terms of the IBM Public
7         License.  For details, see the LICENSE file in the top-level source
8         directory or online at http://www.openafs.org/dl/license10.html
9 */
10
11 #include "stdafx.h"
12 #include "MyFrame.h"
13 #include "ProgBarDlg.h"
14 #include "share.h"
15 #include "datalog.h"
16 #include "WinAfsLoad.h"
17 #ifdef _DEBUG
18 #define new DEBUG_NEW
19 #undef THIS_FILE
20 static char THIS_FILE[] = __FILE__;
21 #endif
22
23 //  CMyThread Stuff  //
24
25 HANDLE CMyUIThread::m_hEventThreadKilled=NULL;
26
27 IMPLEMENT_DYNCREATE(CMyUIThread, CWinThread)
28
29 CMyUIThread::CMyUIThread()
30 {
31         m_pLog=NULL;
32 }
33
34 CMyUIThread::~CMyUIThread()
35 {
36 }
37
38 void CMyUIThread::operator delete(void* p)
39 {
40         // The exiting main application thread waits for this event before completely
41         // terminating in order to avoid a false memory leak detection.  See also
42         // CMyUIThread::OnNcDestroy in bounce.cpp.
43
44         SetEvent(m_hEventThreadKilled);
45         CWinThread::operator delete(p);
46 }
47
48 BOOL CMyUIThread::InitInstance()
49 {
50 #ifdef _AFXDLL
51 //      Enable3dControls();                     // Call this when using MFC in a shared DLL
52 #else
53 //      Enable3dControlsStatic();       // Call this when linking to MFC statically
54 #endif
55         // we use the modeless dialog box so we can create it invisable
56
57         m_pMainWnd = new CProgBarDlg;   //we use the window pointer so MFC will delete the thread when Main window is destroyed
58
59         if (m_pMainWnd==NULL)
60         {
61                 AfxMessageBox("AFS - Memory allocation error",MB_OK);
62                 return FALSE;
63         }
64         if (!((CProgBarDlg *)m_pMainWnd)->Create())
65         {
66                 AfxMessageBox("AFS - Thread Memory allocation error",MB_OK);
67                 return FALSE;
68         }
69         // Initialize sockets 
70         CString msg;
71         if (!((CProgBarDlg *)m_pMainWnd)->Connect(msg))
72         {
73                 AfxMessageBox(msg,MB_OK);
74                 return FALSE;
75         }
76         m_pLog = new CDatalog();
77         if (m_pLog->Create() != TRUE)
78         {
79                 delete m_pLog;
80                 m_pLog=NULL;
81         }
82         return TRUE;
83 }
84
85 int CMyUIThread::ExitInstance()
86 {
87         // TODO:  perform any per-thread cleanup here
88         if (m_cPrint.m_hFile!=CFile::hFileNull)
89                 m_cPrint.Close();
90         return CWinThread::ExitInstance();
91 }
92
93 BEGIN_MESSAGE_MAP(CMyUIThread, CWinThread)
94         //{{AFX_MSG_MAP(CMyUIThread)
95                 // NOTE - the ClassWizard will add and remove mapping macros here.
96         //}}AFX_MSG_MAP
97         ON_THREAD_MESSAGE(WM_UIONPARM,OnParm)
98         ON_THREAD_MESSAGE(WM_UICONNECT,OnConnect)
99         ON_THREAD_MESSAGE(WM_LOG,OnLog)
100 END_MESSAGE_MAP()
101
102
103 void CMyUIThread::OnParm( UINT wp, LONG lp)
104 {
105         switch (wp)
106         {
107         case ONPARMCLOSE:
108                 m_pMainWnd->DestroyWindow();    // this will also destroy the datalog window
109                 m_pLog=NULL;
110                 if (m_cPrint.m_hFile!=CFile::hFileNull)
111                         m_cPrint.Close();
112                 break;
113         case ONPARMDISCONNECT:
114                 ((CProgBarDlg *)m_pMainWnd)->DisConnect();
115                 CWINAFSLOADAPP->WSANotifyFromUI(AFS_EXITCODE_NORMAL,NULL);
116                 break;
117         default:
118                 ASSERT(0);
119                 break;
120         }
121
122 }
123
124 // these routines must either return null or point to a message
125 void CMyUIThread::OnConnect( UINT wParam, LONG lp)
126 {
127         CString sStatus;
128         if (((CProgBarDlg *)m_pMainWnd)->Connect(sStatus))
129         {
130                 CWINAFSLOADAPP->NotifyFromUI(wParam,NULL);
131                 return;
132         }
133         CWINAFSLOADAPP->NotifyFromUI(wParam,sStatus);
134 }
135
136 void CMyUIThread::OnLog( UINT wp, LONG lp)
137 {
138 TRY
139 {
140         switch (wp)
141         {
142         case 0:
143                 if ((lp) && (m_cPrint.m_hFile!=CFile::hFileNull))
144                 {
145                         CString *pMsg=(CString *)lp;
146                         m_cPrint.Write((const char *)*pMsg, lstrlen((const char *)*pMsg));
147                         m_cPrint.Flush();
148                 }
149                 if (m_pLog==NULL) break;
150                 if (lp)
151                 {
152                         CString *pMsg=(CString *)lp;
153                         m_pLog->m_sEdit+=*pMsg;
154                         delete pMsg;    //FREE up date
155                 } else
156                         m_pLog->m_sEdit="";
157                 m_pLog->UpdateData(FALSE);
158                 m_pLog->m_cEdit.LineScroll(10000);
159                 if ( (m_pLog->GetStyle() & (WS_VISIBLE|WS_MINIMIZE))==WS_VISIBLE)
160                         m_pLog->SendMessage(WM_PAINT);
161                 break;
162         case LOGSHOWWINDOW:
163                 if (m_pLog==NULL) break;
164                 m_pLog->ShowWindow((lp)?SW_SHOWNORMAL:SW_HIDE);
165                 break;
166         case LOGSHOWPRINT:
167                 if (lp==NULL)
168                 {
169                         if (m_cPrint.m_hFile!=CFile::hFileNull)
170                                 m_cPrint.Close();
171                 } else {
172                         CString *mp=(CString *)lp;
173                         if (m_cPrint.Open((const char *)*mp
174                                 ,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite,&m_cPrintException)==NULL)
175                         {
176                                 TCHAR szError[1024];
177                                 m_cPrintException.GetErrorMessage(szError, 1024);
178                                 LOG("Log file(%s) open error %s",(const char *)lp,szError);
179                                 break;
180                         }
181                         m_cPrint.SeekToEnd();
182                         delete mp;      //this was passed as a pointer to a created CString;
183                         CString mt;
184                         SYSTEMTIME timeDest;
185                         GetSystemTime(&timeDest);
186                         mt.Format("\nNew Session:(%02d:%02d:%2d-%03d)\n",
187                                 timeDest.wHour,
188                                 timeDest.wMinute,
189                                 timeDest.wSecond,
190                                 timeDest.wMilliseconds);
191                         m_cPrint.Write((const char *)mt, lstrlen(mt));
192                 }
193                 break;
194         default:
195                 break;
196         }
197
198 CATCH(CFileException, e)
199 {       
200         if (m_cPrint.m_hFile!=CFile::hFileNull)
201                 m_cPrint.Close();
202         TCHAR szError[1024];
203         e->GetErrorMessage(szError, 1024);
204         LOG("Open Log file error %s",szError);
205 }
206 END_CATCH
207 }