// debug.cpp : implementation file
//

#include "stdafx.h"
#include "downloadpc.h"
#include "debug.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// Cdebug dialog


Cdebug::Cdebug(CWnd* pParent /*=NULL*/)
	: CDialog(Cdebug::IDD, pParent)
{
	//{{AFX_DATA_INIT(Cdebug)
	//}}AFX_DATA_INIT
}


void Cdebug::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(Cdebug)
	DDX_Control(pDX, IDC_LIST1, m_printf);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(Cdebug, CDialog)
	//{{AFX_MSG_MAP(Cdebug)
	ON_BN_CLICKED(IDC_CLR_BUF, OnClrBuf)
	ON_BN_CLICKED(IDC_Save, OnSave)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Cdebug message handlers

void Cdebug::OnClrBuf() 
{
	// TODO: Add your control notification handler code here
	m_printf.ResetContent();
}

void AdjustList(CListBox *pmyListBox);
void Cdebug::addPrintf(char *log)
{
	if (m_printf.GetCount() >= MAX_PRINTF_NUM) {
		m_printf.DeleteString(0);
	}

	m_printf.AddString(log);
	AdjustList(&m_printf);
}

void Cdebug::OnSave() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE);

	if (dlg.DoModal() == IDOK) {
		FILE *f = fopen(dlg.GetPathName(), "wb");

		if (f == NULL) MessageBox("cannot open file", "ERROR", MB_OK | MB_ICONSTOP);
		else {
			int i;
			CString str;

			for (i=0; i<m_printf.GetCount(); i++) {
				m_printf.GetText(i, str);
				fprintf(f, "%s\n", str);
			}
			fclose(f);
		}
	}
}


BOOL Cdebug::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (pMsg->message == WM_KEYDOWN)
		return true;

	return CDialog::PreTranslateMessage(pMsg);
}
