Click here to Skip to main content
15,885,914 members
Articles / Desktop Programming / WTL

Log Harvester

Rate me:
Please Sign up or sign in to vote.
4.50/5 (3 votes)
17 Sep 2012CPOL3 min read 20.6K   1.8K   13  
A complete log viewer written in C++/WTL.
// MainFrm.cpp : implmentation of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "aboutdlg.h"
#include "HarvesterView.h"
#include "MainFrm.h"
#include "FilterDlg.h"
#include "ReplaceDlg.h"
#include "FindDlg.h"
#include "GotoDlg.h"
#include "TimeRangeDlg.h"
#include "ScanfPatternDlg.h"

#define MRU_KEY L"Software\\AWS\\Harvester"

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg)
{
	if(CFrameWindowImpl<CMainFrame>::PreTranslateMessage(pMsg))
		return TRUE;

	return m_view.PreTranslateMessage(pMsg);
}

BOOL CMainFrame::OnIdle()
{
	UIUpdateToolBar();
	return FALSE;
}

LRESULT CMainFrame::OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	// create command bar window
	HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
	// attach menu
	m_CmdBar.AttachMenu(GetMenu());
	// load command bar images
	m_CmdBar.LoadImages(IDR_MAINFRAME);
	// remove old menu
	SetMenu(NULL);

	m_font.Attach(CreateFont(13, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, EASTEUROPE_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY, FF_DONTCARE, _T("Consolas")));

	CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);
	AddSimpleReBarBand(hWndCmdBar);
	
	HWND hWndToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_MAINFRAME, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
	AddSimpleReBarBand(hWndToolBar, NULL, TRUE, 160, TRUE);

	HWND hWndToolBar2 = CreateSimpleToolBarCtrl(m_hWnd, IDR_TOOLBAR2, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
	AddSimpleReBarBand(hWndToolBar2, NULL, FALSE, 320, TRUE);

	HWND hWndToolBar3 = CreateSimpleToolBarCtrl(m_hWnd, IDR_TOOLBAR3, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE);
	AddSimpleReBarBand(hWndToolBar3, NULL, FALSE);

	// the two following lines are a "magic" for correct Windows XP rebar functionality
	CReBarCtrl(m_hWndToolBar).MaximizeBand(3, TRUE);
	CReBarCtrl(m_hWndToolBar).MaximizeBand(3, TRUE);

	CreateSimpleStatusBar();
	m_sbar.SubclassWindow(m_hWndStatusBar);
	// at present time, there are rather absolute selection start and end in those parts...
	int arrParts[] =
	{
		ID_DEFAULT_PANE,
		ID_ROW_PANE_B,
		ID_COL_PANE_B,
		ID_ROW_PANE_E,
		ID_COL_PANE_E,
		ID_ABS_B,
		ID_ABS_E
	};
	m_sbar.SetPanes(arrParts, sizeof(arrParts) / sizeof(int), false);

	m_hWndClient = m_view.Create(m_hWnd, rcDefault, NULL, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);

	UIAddToolBar(hWndToolBar);
	UIAddToolBar(hWndToolBar2);
	UIAddToolBar(hWndToolBar3);
	UISetCheck(ID_VIEW_TOOLBAR, 1);
	UISetCheck(ID_VIEW_STATUS_BAR, 1);

	HMENU hMenu = m_CmdBar.GetMenu();
	HMENU hFileMenu = ::GetSubMenu(hMenu, 0);
	HMENU hMruMenu = ::GetSubMenu(hFileMenu, 4);	
	m_mru.SetMaxEntries(10);
	m_mru.SetMenuHandle(hMruMenu);
	m_mru.ReadFromRegistry(MRU_KEY);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	CMenuHandle menuMain = m_CmdBar.GetMenu();
	m_view.SetWindowMenu(menuMain.GetSubMenu(WINDOW_MENU_POSITION));

	m_csLastFind = _T("");
	m_bLastCaseSensitivity = false;
	UISetCheck(IDM__WHOLE_FILE, true);

	CScanfPatternDlg dlg;
	dlg.ReadRegistryValues();
	m_LogDoc.m_pcsScanfPattern = new CString(dlg.m_csPattern);
	m_LogDoc.m_nSkipChars = dlg.m_nSkipChars;

	return 0;
}

LRESULT CMainFrame::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
	// unregister message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->RemoveMessageFilter(this);
	pLoop->RemoveIdleHandler(this);
	delete m_LogDoc.m_pcsScanfPattern;

	bHandled = FALSE;
	return 1;
}

LRESULT CMainFrame::OnFileExit(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	PostMessage(WM_CLOSE);
	return 0;
}

CHarvesterView* CMainFrame::CreateView(int nDocPos)
{
	m_LogDoc.m_nSrcDoc[m_view.GetPageCount()] = m_view.GetActivePage();
	CHarvesterView* pView = new CHarvesterView;
	pView->Create(m_view, rcDefault, NULL, WS_CHILD | WS_VISIBLE | ES_READONLY | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_HSCROLL | WS_VSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_MULTILINE | ES_NOHIDESEL | ES_SAVESEL, 0);
	pView->SetFont(m_font);
	m_LogDoc.m_pViewArr[nDocPos] = pView;
	return pView;
}

void CMainFrame::FinalizeView(CHarvesterView *pView)
{
	m_LogDoc.m_pViewArr[m_view.GetActivePage()] = pView;
	pView->SetFont(m_font);
	pView->PostMessageW(EM_SETSEL, 0, 0);
}

LRESULT CMainFrame::OnFileNew(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CHarvesterView* pView = CreateView(m_view.GetPageCount());
	CString csHeader;
	csHeader.Format(_T("New %02d"), m_view.GetPageCount());
	m_view.AddPage(pView->m_hWnd, csHeader);

	return 0;
}

BOOL CMainFrame::OpenFromCommandLine(CString csFileName, BOOL bSaveMRU)
{
	if (!csFileName.GetLength())
		return FALSE;

	csFileName.TrimLeft('"');
	csFileName.TrimRight('"');

	struct _stat buf;
	if (_tstat(csFileName, &buf) != 0)
		return FALSE;

	CHarvesterView* pView = CreateView(m_view.GetPageCount());
	m_view.AddPage(pView->m_hWnd, csFileName.Right(csFileName.GetLength() - csFileName.ReverseFind('\\') - 1));	

	if (UIGetState(IDM_SPECIFY_TIME_RANGE) <= 1)
		stRangeFrom.wYear = YEARX;
	m_LogDoc.LoadDocument(csFileName, m_view.GetActivePage(), GetTailSpan(), stRangeFrom, stRangeTo);
	FinalizeView(pView);
	// not called when 'misused' from MRU menu
	if (bSaveMRU)
	{
		m_mru.AddToList(csFileName);
		m_mru.WriteToRegistry(MRU_KEY);
	}
	return TRUE;
}

LRESULT CMainFrame::OnFileOpen(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CFileDialog dlg( TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"Log files (*.log)\0*.log\0Text files (*.txt)\0*.txt\0All files (*.*)\0*.*\0\0");

	if (dlg.DoModal(0) == IDOK)
	{
		m_mru.AddToList(dlg.m_ofn.lpstrFile);
		m_mru.WriteToRegistry(MRU_KEY);

		CHarvesterView* pView = CreateView(m_view.GetPageCount());
		m_view.AddPage(pView->m_hWnd, dlg.m_szFileTitle);
		
		if (UIGetState(IDM_SPECIFY_TIME_RANGE) <= 1)
			stRangeFrom.wYear = YEARX;
		m_LogDoc.LoadDocument(dlg.m_szFileName, m_view.GetActivePage(), GetTailSpan(), stRangeFrom, stRangeTo);
		FinalizeView(pView);
	}

	return 0;
}

LRESULT CMainFrame::OnFileRecent(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	// get file name from the MRU list
	TCHAR szFile[MAX_PATH];
	if(m_mru.GetFromList(wID, szFile, MAX_PATH))
	{		
		// open file
		if (OpenFromCommandLine(szFile, FALSE))
			m_mru.MoveToTop(wID);
		else
			m_mru.RemoveFromList(wID);

		m_mru.WriteToRegistry(MRU_KEY);
	}

	return 0;
}

LRESULT CMainFrame::OnFileSave(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount())
		return -1;

	CFileDialog dlg (FALSE, _T(".log"), m_view.GetPageTitle(m_view.GetActivePage()), OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		L"Log files (*.log)\0*.log\0Text files (*.txt)\0*.txt\0All files (*.*)\0*.*\0\0");
	
	if (dlg.DoModal(0) == IDOK)
	{
		m_mru.AddToList(dlg.m_ofn.lpstrFile);
		m_mru.WriteToRegistry(MRU_KEY);

		m_LogDoc.SaveDocument(dlg.m_szFileName, m_view.GetActivePage());
		m_view.SetPageTitle(m_view.GetActivePage(), dlg.m_szFileTitle);
	}

	return 0;
}

// Ctrl-C
LRESULT CMainFrame::OnEditCopy(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (m_view.GetActivePage() == (-1))
		return -1;

	CHarvesterView *pView = m_LogDoc.m_pViewArr[m_view.GetActivePage()];
	CHARRANGE cr;
	pView->GetSel(cr);

	const wchar_t* output = new wchar_t[cr.cpMax - cr.cpMin + 1];
	m_LogDoc.m_pViewArr[m_view.GetActivePage()]->GetSelText((LPTSTR)output);
	
	const size_t len = sizeof(wchar_t) * wcslen(output) + 2;
	HGLOBAL hMem =  GlobalAlloc(GMEM_MOVEABLE, len);
	memcpy(GlobalLock(hMem), output, len);
	GlobalUnlock(hMem);
	if (OpenClipboard())
	{
		EmptyClipboard();
		SetClipboardData(CF_UNICODETEXT, hMem);
		CloseClipboard();
	}

	delete[] output;
	return 0;
}

LRESULT CMainFrame::OnEditPaste(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (OpenClipboard()) 
	{
		HANDLE hClipboardData = GetClipboardData(CF_UNICODETEXT);
		wchar_t *pchData = (wchar_t*)GlobalLock(hClipboardData);
		CString strFromClipboard = pchData;

		CHarvesterView* pView = CreateView(m_view.GetPageCount());
		CString csHeader;
		csHeader.Format(_T("Clipboard %02d"), m_view.GetPageCount());
		m_view.AddPage(pView->m_hWnd, csHeader);
		
		m_LogDoc.SetDocument(m_view.GetActivePage(), strFromClipboard.GetBuffer(strFromClipboard.GetLength()));
		FinalizeView(pView);
	
		GlobalUnlock(hClipboardData);
		CloseClipboard();
	}
	
	return 0;
}

LRESULT CMainFrame::OnFind(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (m_view.GetActivePage() >= 0 && m_LogDoc.GetDocumentLength(m_view.GetActivePage()))
	{
		CFindDlg dlg;
	
		INT_PTR nModRes = dlg.DoModal();
		m_bLastCaseSensitivity = dlg.m_bCaseSensitivity;
		m_csLastFind = dlg.m_csFindText;

		if (nModRes == IDOK && m_csLastFind.GetLength() && m_view.GetActivePage() >= 0 && m_LogDoc.GetDocumentLength(m_view.GetActivePage()))
			m_LogDoc.m_pViewArr[m_view.GetActivePage()]->SetSel(m_LogDoc.FindNext(m_view.GetActivePage(), m_csLastFind, 0, m_bLastCaseSensitivity, false));
	}
	
	return 0;
}

LRESULT CMainFrame::OnFindNext(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (m_view.GetActivePage() >= 0 && m_LogDoc.GetDocumentLength(m_view.GetActivePage()) && m_csLastFind.GetLength())
	{
		CHarvesterView *pView = m_LogDoc.m_pViewArr[m_view.GetActivePage()];
		CHARRANGE crCurr;
		pView->GetSel(crCurr);
		pView->SetSel(m_LogDoc.FindNext(m_view.GetActivePage(), m_csLastFind, crCurr.cpMin + 1, m_bLastCaseSensitivity, false));
	}

	return 0;
}

LRESULT CMainFrame::OnFindNextUp(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (m_view.GetActivePage() >= 0 && m_LogDoc.GetDocumentLength(m_view.GetActivePage()) && m_csLastFind.GetLength())
	{
		CHarvesterView *pView = m_LogDoc.m_pViewArr[m_view.GetActivePage()];
		CHARRANGE crCurr;
		pView->GetSel(crCurr);
		pView->SetSel(m_LogDoc.FindNext(m_view.GetActivePage(), m_csLastFind, crCurr.cpMin - 1, m_bLastCaseSensitivity, true));
	}

	return 0;
}

LRESULT CMainFrame::OnGotoLine(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (m_view.GetActivePage() >= 0 && m_LogDoc.GetDocumentLength(m_view.GetActivePage()))
	{
		CGotoDlg dlg;
		if (dlg.DoModal() == IDOK)
		{
			CHARRANGE cr = m_LogDoc.GetLinePos(m_view.GetActivePage(), dlg.m_nGotoLine);
			CHarvesterView *pView = m_LogDoc.m_pViewArr[m_view.GetActivePage()];
			pView->SetSel(cr);
		}
	}
	
	return 0;
}

LRESULT CMainFrame::OnGoLeft(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CHARRANGE cr;
	m_LogDoc.m_pViewArr[m_view.GetActivePage()]->GetSel(cr);	
	int srcLine = m_LogDoc.m_mapTr[m_view.GetActivePage()][(m_LogDoc.GetLineFromPos(m_view.GetActivePage(), cr))];

	int nPage = m_LogDoc.GetSourcePage(m_view.GetActivePage());
	if (nPage >= 0 && nPage < m_view.GetPageCount())
	{
		m_view.SetActivePage(nPage);	
		CHarvesterView *pView = m_LogDoc.m_pViewArr[m_view.GetActivePage()];
		pView->SetSel(m_LogDoc.GetLinePos(m_view.GetActivePage(), srcLine));
	}
	
	return 0;
}

LRESULT CMainFrame::OnGoRight(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	int nPage = m_LogDoc.GetDerivedPage(m_view.GetActivePage());
	if (nPage >= 0 && nPage < m_view.GetPageCount())
		m_view.SetActivePage(nPage);
	return 0;
}

// Select (F5)
LRESULT CMainFrame::OnFilterSelect(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount() || !m_LogDoc.GetDocumentLength(m_view.GetActivePage()))
		return -1;
	OnFilterSelect(false);
	return 0;
}

// Select inverse (Shift-F5)
LRESULT CMainFrame::OnFilterSelectInverse(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount() || !m_LogDoc.GetDocumentLength(m_view.GetActivePage()))
		return -1;
	OnFilterSelect(true);
	return 0;
}

void CMainFrame::OnFilterSelect(bool inverse)
{
	CString csRet;
	bool bCaseSensitive, bSimilarity;
	int nMaxLevenstheinDist;
	int nSourcePage = m_view.GetActivePage();
	CHarvesterView* pView = AddFilterView(csRet, inverse, bCaseSensitive, bSimilarity, nMaxLevenstheinDist);

	if (pView != 0)
	{
		m_LogDoc.SelectFilter(csRet, inverse, bCaseSensitive, bSimilarity, nMaxLevenstheinDist, nSourcePage, m_view.GetPageCount() - 1);
		FinalizeView(pView);
	}
}

LRESULT CMainFrame::OnSelectColumn(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount())
		return -1;

	CHarvesterView* pView = CreateView(m_view.GetPageCount());
	int nSourcePage = m_view.GetActivePage();
	CHARRANGE cr = GetSelection(nSourcePage);
	CString csFmt;
	csFmt.Format(L"Column %d:%d", cr.cpMin, cr.cpMax);
	m_view.AddPage(pView->m_hWnd, csFmt);
	m_LogDoc.ColumnFilter(nSourcePage,  m_view.GetPageCount() - 1, cr);
	FinalizeView(pView);

	return 0;
}

CHarvesterView* CMainFrame::AddFilterView(CString& csRet, bool bInvertedAction, bool &bCaseSensitive, bool &bSimilarity, int &levenstheinDist)
{
	if (!m_view.GetPageCount())
		return 0;

	CFilterDlg dlg;
	dlg.SetValues(bInvertedAction, false, false, 3);
	if (dlg.DoModal() == IDOK)
	{
		csRet = dlg.m_csSearchText;
		bCaseSensitive = dlg.m_bCaseSensitivity;
		bSimilarity = dlg.m_bSimilarity;
		levenstheinDist = dlg.m_nMaxLevensthein;

		CHarvesterView* pView = CreateView(m_view.GetPageCount());
		CString csHeader = csRet;
		if (bInvertedAction)
			csHeader = _T("[NOT] ") + csRet;
		m_view.AddPage(pView->m_hWnd, csHeader);

		return pView;
	}
	else
		return 0;
}

// Transform-Replace (F4)
LRESULT CMainFrame::OnReplace(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount())
	return -1;

	CReplaceDlg dlg;
	if (dlg.DoModal() == IDOK && dlg.m_csFindText.GetLength())
	{
		CHarvesterView* pView = CreateView(m_view.GetPageCount());
		
		CString csHeader;
		csHeader.Format(_T("%s > %s"), dlg.m_csFindText, dlg.m_csReplaceText);
		int nSourcePage = m_view.GetActivePage();
		m_view.AddPage(pView->m_hWnd, csHeader);
		m_LogDoc.ReplaceFilter(dlg.m_csFindText, dlg.m_csReplaceText, dlg.m_bCaseSensitivity, nSourcePage,  m_view.GetPageCount() - 1);
		FinalizeView(pView);
	}
	
	return 0;
}

CHARRANGE CMainFrame::GetSelection(int nSourcePage)
{
	CHARRANGE cr;
	m_LogDoc.m_pViewArr[nSourcePage]->GetSel(cr);	
	if (cr.cpMax == cr.cpMin)
	{
		cr.cpMax = 0;
		cr.cpMin = 0;
		return cr;
	}
	
	int nSelLength = cr.cpMax - cr.cpMin;
	int nLine = m_LogDoc.GetLineFromPos(nSourcePage, cr);
	CHARRANGE cr2 = m_LogDoc.GetLinePos(nSourcePage, nLine);
	cr.cpMin = cr.cpMin - cr2.cpMin;
	cr.cpMax = cr.cpMin + nSelLength;

	return cr;
}

LRESULT CMainFrame::OnSort(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount())
		return -1;

	CHarvesterView* pView = CreateView(m_view.GetPageCount());
	int nSourcePage = m_view.GetActivePage();
	m_view.AddPage(pView->m_hWnd, _T("Sort"));
	CHARRANGE cr = GetSelection(nSourcePage);
	m_LogDoc.SortFilter(nSourcePage,  m_view.GetPageCount() - 1, false, cr.cpMin, cr.cpMax - cr.cpMin);
	FinalizeView(pView);

	return 0;
}

LRESULT CMainFrame::OnSortReverse(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	if (!m_view.GetPageCount())
		return -1;

	CHarvesterView* pView = CreateView(m_view.GetPageCount());
	int nSourcePage = m_view.GetActivePage();
	m_view.AddPage(pView->m_hWnd, _T("RevSort"));
	CHARRANGE cr = GetSelection(nSourcePage);
	m_LogDoc.SortFilter(nSourcePage,  m_view.GetPageCount() - 1, true, cr.cpMin, cr.cpMax - cr.cpMin);
	FinalizeView(pView);

	return 0;
}

LRESULT CMainFrame::OnViewToolBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	static BOOL bVisible = TRUE;	// initially visible
	bVisible = !bVisible;
	CReBarCtrl rebar = m_hWndToolBar;
	int nBandIndex = rebar.IdToIndex(ATL_IDW_BAND_FIRST + 1);	// toolbar is 2nd added band
	rebar.ShowBand(nBandIndex, bVisible);
	rebar.ShowBand(++nBandIndex, bVisible);
	rebar.ShowBand(++nBandIndex, bVisible);
	UISetCheck(ID_VIEW_TOOLBAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnViewStatusBar(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	BOOL bVisible = !::IsWindowVisible(m_hWndStatusBar);
	::ShowWindow(m_hWndStatusBar, bVisible ? SW_SHOWNOACTIVATE : SW_HIDE);
	UISetCheck(ID_VIEW_STATUS_BAR, bVisible);
	UpdateLayout();
	return 0;
}

LRESULT CMainFrame::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}

LRESULT CMainFrame::OnHelpContents(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	ShellExecute(NULL, L"open", L"LogHarvester.chm", NULL, NULL, SW_SHOW);

	return 0;
}

LRESULT CMainFrame::OnWindowClose(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	int nActivePage = m_view.GetActivePage();
	if (nActivePage != -1)
	{		
		m_view.RemovePage(nActivePage);
		delete m_LogDoc.m_pViewArr[nActivePage];
		m_LogDoc.RemoveDocument(nActivePage);
	}
	else
		::MessageBeep((UINT)-1);

	return 0;
}

LRESULT CMainFrame::OnWindowCloseAll(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	m_view.RemoveAllPages();
	for (int i = 0; i < DOC_COUNT; i++)
		if (m_LogDoc.m_pViewArr[i] != 0)
			delete m_LogDoc.m_pViewArr[i];

	return 0;
}

LRESULT CMainFrame::OnWindowActivate(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	int nPage = wID - ID_WINDOW_TABFIRST;
	m_view.SetActivePage(nPage);

	return 0;
}

void CMainFrame::UncheckTailMenuItems()
{
	UISetCheck(IDM_2_MIN__TAIL, false);
	UISetCheck(IDM_5_MIN__TAIL, false);
	UISetCheck(IDM_10_MIN__TAIL, false);
	UISetCheck(IDM__WHOLE_FILE, false);
	UISetCheck(IDM_SPECIFY_TIME_RANGE, false);
}

LRESULT CMainFrame::OnSetTail(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	UncheckTailMenuItems();
	UISetCheck(wID, true);
	return 0;
}

LRESULT CMainFrame::OnSpecifyTimeRange(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CTimeRangeDlg dlg;
	if (dlg.DoModal() == IDOK)
	{
		UncheckTailMenuItems();
		UISetCheck(wID, true);

		stRangeFrom.wYear = dlg.m_lpDateFrom->wYear;
		stRangeFrom.wMonth = dlg.m_lpDateFrom->wMonth;
		stRangeFrom.wDay = dlg.m_lpDateFrom->wDay;
		stRangeFrom.wDayOfWeek = dlg.m_lpDateFrom->wDayOfWeek;
		delete dlg.m_lpDateFrom;
		stRangeFrom.wHour = dlg.m_lpTimeFrom->wHour;
		stRangeFrom.wMinute = dlg.m_lpTimeFrom->wMinute;
		stRangeFrom.wSecond = dlg.m_lpTimeFrom->wSecond;
		stRangeFrom.wMilliseconds = dlg.m_lpTimeFrom->wMilliseconds;
		delete dlg.m_lpTimeFrom;
		stRangeTo.wYear = dlg.m_lpDateTo->wYear;
		stRangeTo.wMonth = dlg.m_lpDateTo->wMonth;
		stRangeTo.wDay = dlg.m_lpDateTo->wDay;
		stRangeTo.wDayOfWeek = dlg.m_lpDateTo->wDayOfWeek;
		delete dlg.m_lpDateTo;
		stRangeTo.wHour = dlg.m_lpTimeTo->wHour;
		stRangeTo.wMinute = dlg.m_lpTimeTo->wMinute;
		stRangeTo.wSecond = dlg.m_lpTimeTo->wSecond;
		stRangeTo.wMilliseconds = dlg.m_lpTimeTo->wMilliseconds;
		delete dlg.m_lpTimeTo;
	}
	return 0;
}

LRESULT CMainFrame::OnSpecifyDatetimeFormat(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CScanfPatternDlg dlg;

	if (dlg.DoModal())
	{
		if (m_LogDoc.m_pcsScanfPattern)
			delete m_LogDoc.m_pcsScanfPattern;
		m_LogDoc.m_pcsScanfPattern = new CString(dlg.m_csPattern);
		m_LogDoc.m_nSkipChars = dlg.m_nSkipChars;
	}
	
	return 0;
}

int CMainFrame::GetTailSpan()
{
	if (UIGetState(IDM_2_MIN__TAIL) > 1)
		return 2 * 60;
	else if (UIGetState(IDM_5_MIN__TAIL) > 1)
		return 5 * 60;
	else if (UIGetState(IDM_10_MIN__TAIL) > 1)
		return 10 * 60;
	else
		return 0;
}

LRESULT CMainFrame::OnUpdateRowCol(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	LONG nStartPos(0), nEndPos(0), nRowB(0), nColB(0), nRowE(0), nColE(0);

	if (m_view.GetActivePage() >= 0)
	{
		CHarvesterView *pView = m_LogDoc.m_pViewArr[m_view.GetActivePage()];
		pView->GetSel(nStartPos, nEndPos);
		nRowB = pView->LineFromChar(nStartPos);
		nRowE = pView->LineFromChar(nEndPos);
	
		for (int n = 0; n != 2; n++)
		{
			LONG nChar = n ? nEndPos - pView->LineIndex() : nStartPos - pView->LineIndex();
			if(nChar > 0)
			{
				LPTSTR lpstrLine = (LPTSTR)_alloca(max(2, (nChar + 1) * sizeof(TCHAR)));	// min = WORD for length
				nChar = pView->GetLine(n ? nRowE : nRowB, lpstrLine, nChar);
				for(LONG i = 0; i < nChar; i++)
				{
					if(lpstrLine[i] == _T('\t'))
						n ? nColE = ((nColE / cchTAB) + 1) * cchTAB : nColB = ((nColB / cchTAB) + 1) * cchTAB;
					else
						n ? nColE++ : nColB++;
				}
			}
		}
	}
	
	TCHAR szBuff[100];
	wsprintf(szBuff, _T("RowB: %i"), nRowB);
	m_sbar.SetPaneText(ID_ROW_PANE_B, szBuff);
	wsprintf(szBuff, _T("ColB: %i"), nColB);
	m_sbar.SetPaneText(ID_COL_PANE_B, szBuff);
	wsprintf(szBuff, _T("RowE: %i"), nRowE);
	m_sbar.SetPaneText(ID_ROW_PANE_E, szBuff);
	wsprintf(szBuff, _T("ColE: %i"), nColE);
	m_sbar.SetPaneText(ID_COL_PANE_E, szBuff);
	wsprintf(szBuff, _T("AbsB: %i"), nStartPos);
	m_sbar.SetPaneText(ID_ABS_B, szBuff);
	wsprintf(szBuff, _T("AbsE: %i"), nEndPos);
	m_sbar.SetPaneText(ID_ABS_E, szBuff);
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions