//
//	Copyright © 2006 Nemetschek North America 
//	All Rights Reserved.
//
// Created by Vladislav Stanev

#include "StdHeaders.h"

#include "StandardEditTextDlg.h"


#define			kStaticTextControl			4
#define			kEditTextControl			5

using namespace VectorWorks::VWUI;

EVENT_DISPATCH_MAP_BEGIN( CStandardEditTextBoxDlg );
ADD_DISPATCH_EVENT( kEditTextControl, OnEditControl );
EVENT_DISPATCH_MAP_END;

// ------------------------------------------------------------------------------------------
CStandardEditTextBoxDlg::CStandardEditTextBoxDlg(	const TXString& 	strTitle,
									const TXString& 	strLabel,
									const TXString& 	strValue,
									const TXString& 	strOkBtnText,
									const TXString& 	strCancelBntText)
: fStaticTextCtrl( kStaticTextControl ), fEditTextCtrl( kEditTextControl )
{
	fbAllowEmtpyResult	= false;
	fbIsReadOnly		= false;
	fstrTitle			= strTitle;
	fstrLabel			= strLabel;
	fDDXTextValue		= strValue;
	fstrOrgValue		= strValue;
	fnBoxWidth			= 35;
	fnBoxHeight			= 1;
	fstrOkBtnText		= strOkBtnText;
	fstrCancelBntText	= strCancelBntText;
}

CStandardEditTextBoxDlg::CStandardEditTextBoxDlg(	const TXString& 	strTitle,
									const TXString& 	strLabel,
									const TXString& 	strValue,
									size_t				nBoxWidth,
									size_t				nBoxHeight,
									const TXString& 	strOkBtnText,
									const TXString& 	strCancelBntText)
: fStaticTextCtrl( kStaticTextControl ), fEditTextCtrl( kEditTextControl )
{
	fbAllowEmtpyResult	= false;
	fbIsReadOnly		= false;
	fstrTitle			= strTitle;
	fstrLabel			= strLabel;
	fDDXTextValue		= strValue;
	fstrOrgValue		= strValue;
	fnBoxWidth			= nBoxWidth;
	fnBoxHeight			= nBoxHeight;
	fstrOkBtnText		= strOkBtnText;
	fstrCancelBntText	= strCancelBntText;
}

CStandardEditTextBoxDlg::~CStandardEditTextBoxDlg()
{
}

void CStandardEditTextBoxDlg::SetReadOnly(bool bReadOnly)
{
	fbIsReadOnly		= bReadOnly;
}

void CStandardEditTextBoxDlg::SetAllowEmptyResult(bool bAllowEmptyRes)
{
	fbAllowEmtpyResult	= bAllowEmptyRes;
}

TXString CStandardEditTextBoxDlg::GetValue() const
{
	return fDDXTextValue;
}

bool CStandardEditTextBoxDlg::CreateDialogLayout()
{
	if ( ! this->CreateDilaog( fstrTitle, fstrOkBtnText, fstrCancelBntText, false ) )
		return false;

	// create the controls in the dialog
	if ( ! fStaticTextCtrl.CreateControl( this, fstrLabel ) )
		return false;
	if ( ! fEditTextCtrl.CreateControl( this, fDDXTextValue, fnBoxWidth, fnBoxHeight ) )
		return false;

	// add the controls to the dialog
	this->AddFirstGroupControl( & fStaticTextCtrl );
	this->AddBelowControl( & fStaticTextCtrl, & fEditTextCtrl );
   
	return true;
}

void CStandardEditTextBoxDlg::OnDDXInitialize()
{
	// add DDX variables to the control
	this->AddDDX_EditText( kEditTextControl, & fDDXTextValue );
}

void CStandardEditTextBoxDlg::OnSetUpEvent()
{
	VWDialog::OnSetUpEvent();
}

void CStandardEditTextBoxDlg::OnEditControl(TControlID controlID, VWDialogEventArgs& eventArgs)
{
	if ( fbIsReadOnly ) {
		fDDXTextValue	= fstrOrgValue;
		this->UpdateData( true );
	}
}
			
void CStandardEditTextBoxDlg::OnUpdateUI()
{
	VWDialog::OnUpdateUI();

	if ( ! fbAllowEmtpyResult ) {
		this->EnableControl( kDefaultButtonID, ! fDDXTextValue.IsEmpty() );
	}
}


