home *** CD-ROM | disk | FTP | other *** search
- /////////////////////////////////////////////////////////////////////////////
- //
- // File : OutputDoc.cpp
- // Project : MsgTrace
- // Component : MsgTracer
- //---------------------------------------------------------------------------
- // Description : output-document
- //
- /////////////////////////////////////////////////////////////////////////////
- //
- // SourceSafe Strings. Do not change.
- //---------------------------------------------------------------------------
- // $Author: jeskes $
- // $Date: $
- // $Revision: $
- //
- /////////////////////////////////////////////////////////////////////////////
-
- #include "stdafx.h"
- #include "MsgTracerApp.h"
- #include "OutputDoc.h"
- #include "OutputLine.h"
-
- /////////////////////////////////////////////////////////////////////////////
-
- #ifdef _DEBUG
- #undef THIS_FILE
- static char BASED_CODE THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
-
- #define CMD_EXT_UPDATE_ALL_VIEWS (999)
-
- /////////////////////////////////////////////////////////////////////////////
- // COutputDoc
- /////////////////////////////////////////////////////////////////////////////
-
- IMPLEMENT_SERIAL(COutputDoc, CDocument, 0 /* schema number*/ )
-
- BEGIN_MESSAGE_MAP(COutputDoc, CDocument)
- //{{AFX_MSG_MAP(COutputDoc)
- ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
- //}}AFX_MSG_MAP
- ON_COMMAND_EX( CMD_EXT_UPDATE_ALL_VIEWS, OnExtUpdateAllViews )
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // COutputDoc construction
- /////////////////////////////////////////////////////////////////////////////
-
- COutputDoc::COutputDoc() :
-
- m_nMaxBufferedLines( ThisApp().m_nBufferedLines ),
- m_psLineArray( NULL ),
- m_nMaxLines( 0 ),
- m_nNextFreeLine( 0 ),
- m_nMaxLineLen( 80 ),
- m_pCurrentFormat( NULL )
-
- {
- m_pCurrentFormat = new COutputLineFormat;
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- COutputDoc::~COutputDoc()
- {
- delete[] m_psLineArray;
- m_psLineArray = NULL;
-
- delete m_pCurrentFormat;
- m_pCurrentFormat = NULL;
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL COutputDoc::OnNewDocument()
- {
- if( !CDocument::OnNewDocument() )
- {
- return( FALSE );
- }
-
- //-----------------------------------------------------------------------
- // setup some things
- //-----------------------------------------------------------------------
-
- m_nMaxLines = 0;
- m_nNextFreeLine = 0;
- m_nMaxLineLen = 80;
-
- m_psLineArray = new COutputLine[ m_nMaxBufferedLines ];
-
- return( NULL != m_psLineArray );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::DeleteContents()
- {
- CDocument::DeleteContents();
-
- if( NULL != m_psLineArray )
- {
- delete [] m_psLineArray;
- m_psLineArray = NULL;
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::Serialize( CArchive& ar )
- {
- if( !ar.IsStoring() )
- {
- return;
- }
-
- // support only writing to file
-
- for( int n = 0; n < m_nMaxLines; n++ )
- {
- CString& sLine = GetLine( n );
-
- ar.Write( (const char*) sLine, sLine.GetLength() );
- ar << (BYTE) '\r' << (BYTE) '\n';
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // max-buffered-lines
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::SetMaxBufferedLines( int nMaxBufferedLines )
- {
- if( m_nMaxBufferedLines != nMaxBufferedLines )
- {
- m_nMaxBufferedLines = nMaxBufferedLines;
- OnEditClearAll();
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Current Format for Output Lines
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::InitTabStops( int nTabStops )
- {
- m_pCurrentFormat->InitTabStops( nTabStops );
- }
-
- void COutputDoc::SetTabStop( int nIndex, int nTabPos )
- {
- m_pCurrentFormat->SetTabStop( nIndex, nTabPos );
- }
-
- const COutputLineFormat& COutputDoc::GetCurrentFormat() const
- {
- return( *m_pCurrentFormat );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // COutputDoc line stuff
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::AddLine( LPCSTR lpsz )
- {
- CSingleLock lock( &m_mutexAddLine );
- if( !lock.Lock( 1000 ) )
- {
- return;
- }
-
- LPSTR lpszText = (LPSTR) lpsz;
-
- if( NULL == lpszText )
- {
- lpszText = "";
- }
-
- LPSTR lpszLF = strchr( lpszText, '\n' );
-
- if( NULL != lpszLF )
- {
- int nLen = (int)(DWORD)( lpszLF - lpszText );
-
- if( 0 < nLen )
- {
- if( '\r' == lpszText[ nLen - 1 ] )
- {
- nLen--;
- }
- }
-
- if( 0 == nLen )
- {
- m_psLineArray[ m_nNextFreeLine ] = "";
- }
- else
- {
- LPSTR lpszTemp =
- m_psLineArray[ m_nNextFreeLine ].GetBufferSetLength( nLen + 1 );
-
- lstrcpyn( lpszTemp, lpszText, nLen + 1 );
- m_psLineArray[ m_nNextFreeLine ].ReleaseBuffer();
- }
- }
- else
- {
- // ignore empty lines
- if( '\0' == *lpszText )
- {
- return;
- }
- m_psLineArray[ m_nNextFreeLine ] = lpszText;
- }
-
-
- m_psLineArray[ m_nNextFreeLine ].SetFormat( GetCurrentFormat() );
-
- m_nMaxLines = min( m_nMaxLines + 1, m_nMaxBufferedLines );
- m_nMaxLineLen = max( m_nMaxLineLen, lstrlen( lpszText ) );
-
- ThreadSafeUpdateAllViews( NULL, OUTPUTDOC_LINEADDED, (CObject*) &m_psLineArray[ m_nNextFreeLine ] );
- m_nNextFreeLine = ( m_nNextFreeLine + 1 ) % m_nMaxBufferedLines;
-
- if( NULL != lpszLF )
- {
- AddLine( lpszLF + 1 );
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- COutputLine& COutputDoc::GetLine( int nLine )
- {
- if( nLine > m_nMaxLines )
- {
- return( COutputLine( "" ) );
- }
-
- if( m_nMaxLines == m_nMaxBufferedLines )
- {
- nLine = ( m_nNextFreeLine + nLine ) % m_nMaxBufferedLines;
- }
-
- return( m_psLineArray[ nLine ] );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::OnEditClearAll()
- {
- OnNewDocument();
-
- ThreadSafeUpdateAllViews( NULL, OUTPUTDOC_CLEARED, NULL );
-
- CString sMessage;
- sMessage.LoadString( IDS_OUTPUT_CLEARED );
-
- AddLine( sMessage );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::OnChangedViewList()
- {
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- HMENU COutputDoc::GetDefaultMenu()
- {
- return( NULL );
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // Threadsafe view notifications
- /////////////////////////////////////////////////////////////////////////////
-
- void COutputDoc::ThreadSafeUpdateAllViews( CView* pView, LPARAM lHint, CObject* pHint )
- {
- // if running thread is the main thread normal operation
-
- if( ( AfxGetThread() == AfxGetApp() ) ||
- ( NULL == AfxGetApp()->m_pMainWnd ) ||
- !::IsWindow( AfxGetApp()->m_pMainWnd->m_hWnd ) )
- {
- UpdateAllViews( pView, lHint, pHint );
- return;
- }
-
- CSingleLock lock( &m_mutexThreadSafeUpdateAllViews );
-
- // get m_sUpdateAllViews to do notification
-
- if( !lock.Lock( 5000 ) )
- {
- return;
- }
-
- m_sUpdateAllViews.m_pView = pView;
- m_sUpdateAllViews.m_lHint = lHint;
- m_sUpdateAllViews.m_pHint = pHint;
-
- AfxGetApp()->m_pMainWnd->SendMessage( WM_COMMAND, MAKELONG( CMD_EXT_UPDATE_ALL_VIEWS, 0 ), 0 );
- }
-
- /////////////////////////////////////////////////////////////////////////////
-
- BOOL COutputDoc::OnExtUpdateAllViews( UINT u )
- {
- UpdateAllViews( m_sUpdateAllViews.m_pView,
- m_sUpdateAllViews.m_lHint,
- m_sUpdateAllViews.m_pHint );
-
- return( TRUE );
- }
-