// LargeFileReading_MFCDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LargeFileReading_MFC.h"
#include "LargeFileReadingNoSpell.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CLargeFileReadingDlg::CLargeFileReadingDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLargeFileReadingDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLargeFileReadingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_WHOLE_TEXT, m_cWholeFile);
DDX_Control(pDX, IDC_READ_PROGRESS, m_cReadProgress);
}
BEGIN_MESSAGE_MAP(CLargeFileReadingDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_OPEN_FILE, &CLargeFileReadingDlg::OnBnClickedOpenFile)
ON_MESSAGE(WM_TEXT_READ_MESSAGE, OnTextReadMessage)
END_MESSAGE_MAP()
// CLargeFileDlg message handlers
BOOL CLargeFileReadingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_cWholeFile.SetTextMode( TM_PLAINTEXT | TM_SINGLELEVELUNDO);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLargeFileReadingDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLargeFileReadingDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
UINT LargeFileReaderThread(void* pArg)
{
CLargeFileReadingDlg* pDialog = static_cast<CLargeFileReadingDlg*> ( pArg );
pDialog->LargeFileReaderProc();
return 0;
}
void CLargeFileReadingDlg::LargeFileReaderProc()
{
// Though file pointer be at beginning, we set it anyway.
m_cTextFile.SeekToBegin();
CString strOneLine;
// Though using TCHAR* would be more efficeint than using CString object.
// But I am using CString for easiness.
//pBuffer->SetLength( )
CString* pBuffer = new CString();
int nLines=0;
while (m_cTextFile.ReadString(strOneLine))
{
(*pBuffer) += strOneLine + _T("\n");
if(++nLines>10000)
{
PostMessage(WM_TEXT_READ_MESSAGE, (WPARAM)pBuffer, FALSE);
pBuffer = new CString();
nLines=0;
}
}
// Send the last set, specifying TRUE that thread finished work.
PostMessage(WM_TEXT_READ_MESSAGE, (WPARAM)pBuffer, TRUE);
// AfxMessageBox(_T("done"));
}
LRESULT CLargeFileReadingDlg::OnTextReadMessage(WPARAM wLineInfo, LPARAM lParam)
{
// Updating the (Rich) text box doesnt work well as I expected
// Some solution might exist for better approach for 'appending'
// text to the text/richtext control, than the one I used here.
// But I found out that textbox uses ARRAY of characters, but
// richtext doesnt. That's why rich text is somewhat faster.
// Few smart editors in market implement their own 'text' controls.
CString* pString = (CString*)wLineInfo;
int nTextLen = m_cWholeFile.GetWindowTextLength();
m_cWholeFile.SetSel(nTextLen, nTextLen);
m_cWholeFile.ReplaceSel(*pString);
nTextLen = m_cWholeFile.GetTextLength();
m_cReadProgress.SetPos(nTextLen/1024);
// LPARAM would be set to TRUE, if thread has finished reading
// Thus we can enable controls
if(lParam == TRUE)
{
GetDlgItem(IDC_OPEN_FILE)->EnableWindow();
m_cReadProgress.ShowWindow(SW_HIDE);
}
// m_cWholeFileR.SetSel(0,0);
delete pString;
return 0;
}
void CLargeFileReadingDlg::OnBnClickedOpenFile()
{
CFileDialog fdOpen(TRUE, _T(".TXT"), NULL, 6,
_T("Text Files|*.txt;*.log||") );
if( fdOpen.DoModal() != IDOK )
return;
if (m_cTextFile.m_pStream)
{
m_cTextFile.Close();
m_cTextFile ;
}
m_cTextFile.Open(fdOpen.GetPathName(), CFile::typeText | CFile::modeRead | CFile::shareDenyNone);
// m_cWholeFileR.set;
m_nFileSize = m_cTextFile.GetLength();
m_cWholeFile.SetWindowText( _T(""));
// Update progress on every 1KB
m_cReadProgress.SetRange32(1,static_cast<int> (m_nFileSize/1024) );
m_cReadProgress.ShowWindow(SW_SHOW);
GetDlgItem(IDC_OPEN_FILE)->EnableWindow(false);
AfxBeginThread(LargeFileReaderThread, this);
}