// ========================================================================== // Class Implementation // COXHistoryCtrl // ========================================================================== // Version: 9.3 // This software along with its related components, documentation and files ("The Libraries") // is © 1994-2007 The Code Project (1612916 Ontario Limited) and use of The Libraries is // governed by a software license agreement ("Agreement"). Copies of the Agreement are // available at The Code Project (www.codeproject.com), as part of the package you downloaded // to obtain this file, or directly from our office. For a copy of the license governing // this software, you may contact us at legalaffairs@codeproject.com, or by calling 416-849-8900. // ////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "OXHistoryCtrl.h" #include // default maximum number of history entries displayed in the control window const int OXHISTCTRL_MAXENTRIES=100; IMPLEMENT_DYNCREATE(COXHistoryCtrl, COXScrollWnd) BEGIN_MESSAGE_MAP(COXHistoryCtrl, COXScrollWnd) //{{AFX_MSG_MAP(COXHistoryCtrl) ON_WM_PAINT() ON_WM_ERASEBKGND() //}}AFX_MSG_MAP END_MESSAGE_MAP() COXHistoryCtrl::COXHistoryCtrl() { //setup the critical section variable InitializeCriticalSection(&m_criticalSection); //logging variables m_sLogFileName=_T(""); m_bEnableLog=FALSE; m_bTimeStampedLog=FALSE; m_logDay=0; m_logMonth=0; m_logYear=0; m_bTruncateLogFile=FALSE; //display properties m_clrText=::GetSysColor(COLOR_WINDOWTEXT); m_clrBack=::GetSysColor(COLOR_WINDOW); m_clrLeftOverBack=::GetSysColor(COLOR_WINDOW); m_nAlignment=DT_LEFT; m_nOffset=0; m_sizeSymbol=CSize(0,0); //max number of entries m_nMaxNumEntries=OXHISTCTRL_MAXENTRIES; //max line length m_nMaxLengthEntry=0; //datestamp to string format SetTimeStampFormat(_T("%X")); } COXHistoryCtrl::~COXHistoryCtrl() { CloseLog(); m_arrEntries.RemoveAll(); //release the critical section DeleteCriticalSection(&m_criticalSection); } void COXHistoryCtrl::EnableLog(BOOL bYesNo) { m_bEnableLog=bYesNo; if(m_bEnableLog) OpenLog(); else CloseLog(); } BOOL COXHistoryCtrl::AddLine(CString string, BOOL bAddToLog/*=TRUE*/) { //enter into a critical section EnterCriticalSection(&m_criticalSection); //update the log file if(bAddToLog && m_bEnableLog) WriteToLog(string,TRUE); BOOL bUpdateScroll=FALSE; ASSERT(m_arrEntries.GetCount()<=GetMaxNumEntries()); HISTORYENTRY entry(string,m_clrText,m_clrBack,m_nOffset,m_nAlignment); m_arrEntries.AddTail(entry); if(m_arrEntries.GetCount()==GetMaxNumEntries()+1) { entry=m_arrEntries.RemoveHead(); ASSERT(entry.m_sText.GetLength()<=m_nMaxLengthEntry); if(entry.m_sText.GetLength()==m_nMaxLengthEntry) { UpdateMaxLengthInfo(); bUpdateScroll=TRUE; } } else bUpdateScroll=TRUE; int nLength=string.GetLength(); if(m_nMaxLengthEntryrect.Width()) rect.right=rect.left+sizeTotal.cx; if(sizeTotal.cy>rect.Height()) rect.bottom=rect.top+sizeTotal.cy; CRect rectCopy=rect; HISTORYENTRY entry; POSITION pos=m_arrEntries.GetHeadPosition(); int nEntries=GetNumEntries(); rectCopy.top =0; rectCopy.bottom =0; for(int nIndex=0; nIndexGetLogFont(&lf)!=0); VERIFY(m_font.CreateFontIndirect(&lf)); UpdateFontInfo(); UpdateScrollInfo(); if(bRedraw) RedrawWindow(); } BOOL COXHistoryCtrl::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default UNREFERENCED_PARAMETER(pDC); return TRUE; } void COXHistoryCtrl::UpdateFontInfo() { //get the symbol height and width CDC* pDC=GetDC(); ASSERT_VALID(pDC); CFont* pOldFont=pDC->SelectObject(&m_font); ::GetTextExtentPoint(pDC->GetSafeHdc(),_T("X"),1,&m_sizeSymbol); if(pOldFont!=NULL) { pDC->SelectObject(pOldFont); } ReleaseDC(pDC); } void COXHistoryCtrl::UpdateScrollInfo() { // CPoint ptCurrent=GetDeviceScrollPosition(); CRect rect; GetClientRect(rect); CSize sizeTotal(m_nMaxLengthEntry*m_sizeSymbol.cx, GetNumEntries()*m_sizeSymbol.cy+rect.Height()%m_sizeSymbol.cy); CSize sizePage(m_sizeSymbol.cx,m_sizeSymbol.cy); if(m_sizeSymbol.cxGetNumEntries() ? GetNumEntries() : nResult); return nResult; } void COXHistoryCtrl::ScrollToEnd() { CPoint ptScroll=GetScrollPosition(); ptScroll.y=GetNumEntries()*m_sizeSymbol.cy; ScrollToPosition(ptScroll); } CString COXHistoryCtrl::GetTimeStampedFileName() { COleDateTime date=COleDateTime::GetCurrentTime(); int logDay=date.GetDay(); int logMonth=date.GetMonth(); int logYear=date.GetYear(); m_sTimeStampedLogFileName.Format(_T("%2.2d%2.2d%4.4d"), logDay,logMonth,logYear); //find the extension int nPos=m_sLogFileName.Find(_T(".")); if(nPos!=-1) m_sTimeStampedLogFileName+=m_sLogFileName.Mid(nPos); return m_sTimeStampedLogFileName; }