/*
Copyright 1997 David Coppit.  Permission to use and modify this code for
research purposes is granted to provided that this copyright statement retained
in all derivative software.

Many thanks to Dietmar Kuehl <Dietmar.Kuehl@uni-konstanz.de> for his help.
Some of this code is based on his streams examples at:
http://www.informatik.uni-konstanz.de/~kuehl/c++/iostream/
 
This file, along with winbuff.cpp, PromptDialog.h, and PromptDialog.cpp
implement a Windows MFC compatible text-based input window for cout and cin.  A
tutorial and more information can be found at https://www.coppit.org/code/winbuf/
*/

// PromptDialog.cpp : implementation file
//

#include "stdafx.h"
#include "PromptDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// PromptDialog dialog


PromptDialog::PromptDialog(CWnd* pParent /*=NULL*/)
	: CDialog(PromptDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(PromptDialog)
	m_displaytext = _T("");
	m_strEdit1 = _T("");
	//}}AFX_DATA_INIT
	m_pView = NULL;
}

PromptDialog::PromptDialog(CView* pView)
{
	m_strEdit1 = ("");
	m_displaytext = ("");
	m_pView = pView;
}

BOOL PromptDialog::Create()
{
    return CDialog::Create(PromptDialog::IDD);
}

// Appends text to the output box. Clears it if called with an empty string.
// Also scrolls the box down to the end.

void PromptDialog::PutText(CString text)
{
	if (text == "")
		m_displaytext = "";
	else
		m_displaytext += text;

}

void PromptDialog::WaitForOK()
{
	// Clear the input field.
	m_strEdit1 = "";

	UpdateData(FALSE);
	m_ViewControl.LineScroll(m_ViewControl.GetLineCount());

	// Get a pointer to the input field and disable it.
	CWnd* editCtrl = GetDlgItem(IDC_EDIT2);
	editCtrl->EnableWindow(FALSE);

	OKClicked = FALSE;
	CancelClicked = FALSE;

	// Wait for an "OK"
	while (OKClicked == FALSE && CancelClicked == FALSE) {
		MSG message;

		// This allows the events for the window to occur, namely
		// clicking of the OK button.
		while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) {
			if (!IsDialogMessage(&message)) {
				::TranslateMessage(&message);
				::DispatchMessage(&message);
			}
		}
	}

	// Enable the input field
	editCtrl->EnableWindow(TRUE);
}

void PromptDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(PromptDialog)
	DDX_Control(pDX, IDC_EDIT1, m_ViewControl);
	DDX_Text(pDX, IDC_EDIT1, m_displaytext);
	DDX_Text(pDX, IDC_EDIT2, m_strEdit1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(PromptDialog, CDialog)
	//{{AFX_MSG_MAP(PromptDialog)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// PromptDialog message handlers

void PromptDialog::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);

	OKClicked = TRUE;

	// Don't call this for modeless dialogs
	// CDialog::OnOK();
}

void PromptDialog::OnCancel() 
{
	// TODO: Add extra cleanup here
	CancelClicked = TRUE;
	
	// Don't call this for modeless dialogs
	// CDialog::OnCancel();
}
