/*************************************************************************\
* "$RCSfile: CDlgConfig.cpp,v $"                                          *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2011-10-20 07:57:53 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.22 $                                 *
\*************************************************************************/
/*! \file CDlgConfig.cpp
*	\brief Implementation CDlgConfig Class
**************************************************************************/

#include <string.h>
#include "VIC_PS.h"
#include "CRegion.h"
#include "CLog.hpp"
#include "CPSApp.hpp"
#include "CDlg.hpp"
#include "CDlgConfig.hpp"

CDlgConfig *CDlgConfig::m_pThis = NULL;	//!< The one and only CDlgConfig instance

/*************************************************************************/
CDlgConfig::CDlgConfig(int x, int y, int Width, int Height)
/*************************************************************************/
/*! @brief Constructor
**************************************************************************/
{
	m_pApp = CPSApp::GetInstance();
	m_pStrings = m_pApp->GetPSStringsIni();
	m_pConfig = NULL;
	m_fKB = m_pApp->GetKeyboardHandle();

	SetID(DialogId::CONFIG);

	m_Width = Width;
	m_Height = Height;
	m_PosX = x;
	m_PosY = y;
	m_Dlg.rgbDlg.Width = 0;

	m_Activity = 0;
	m_bIsVisible = false;
	m_bUpdate = false;
	m_bDynamicUpdate = false;

	m_Sel = 1;
	m_FileSel = 0;
	m_SelBoxText = (char*)VIB_MemAlloc(128 * 1024);
	m_PreDownUp = 0;

	VIB_ChDir(VIB_Drive[1], m_pApp->GetSkinPath());
	m_Icon[ICON(ID_UP)]    = ReadIcon("BI_UP.PNG", 46, 40);
	m_Icon[ICON(ID_DOWN)]  = ReadIcon("BI_DOWN.PNG", 46, 40);
	m_Icon[ICON(ID_LEFT)]  = ReadIcon("BI_LEFT.PNG", 46, 40);
	m_Icon[ICON(ID_RIGHT)] = ReadIcon("BI_RIGHT.PNG", 46, 40);
	m_Icon[ICON(ID_UPA)]   = ReadIcon("BB_UP.PNG", 98, 98);
	m_Icon[ICON(ID_DOWNA)] = ReadIcon("BB_DOWN.PNG", 98, 98);
	VIB_ChDir(VIB_Drive[1], "..");

	m_SIcon[0] = *m_pApp->GetIcon(IconId::SAVE);
	m_SIcon[1] = *m_pApp->GetIcon(IconId::SAVEA);
}

/*************************************************************************/
CDlgConfig::~CDlgConfig()
/*************************************************************************/
/*! @brief Destructor
**************************************************************************/
{
	for (int i = 0; i < INDEX(ID_XXX); i++)
		GUI_FreeCImage(&m_Icon[i]);
	VIB_MemFree(m_SelBoxText);
}

/*************************************************************************/
CDlgConfig *CDlgConfig::CreateInstance(int x, int y, int Width, int Height)
/*************************************************************************/
/*! @brief Creates CDlgConfig instance.
	@param x Horizontal position of dialog
	@param y Vertical position of dialog
	@param Width Width of dialog
	@param Height Height of dialog
	@return Pointer to CDlgConfig instance
**************************************************************************/
{
	if (m_pThis == NULL)
		m_pThis = new CDlgConfig(x, y, Width, Height);
	return m_pThis;
}

/*************************************************************************/
void CDlgConfig::DisposeInstance()
/*************************************************************************/
/*! @brief Disposes CDlgConfig instance.
**************************************************************************/
{
	if (m_pThis)
		delete m_pThis;
	m_pThis = NULL;
}

/*************************************************************************/
void CDlgConfig::Create(Config *pConfig, int Mode)
/*************************************************************************/
/*! @brief Create dialog assigned to \p pC.

	Before an instance of CDlgConfig can be used, it has to be assigned to
	an instance of the \p Config class by calling Create() with \p pConfig
	being not \p NULL. In this function the dialog is created.
	@param pConfig Pointer to instance of \p Config
	@param Mode 0: Normal\n
	1: Camera preset
**************************************************************************/
{
	int x, y;
	int iWidth, iHeight;
	int IdSBB;
	int SelPosX, SelPosY;
	int SelWidth, SelHeight;
	int PosX, PosY;
	char Text[64];

	if (Mode)
	{
		m_PresetHelp = m_ActiveBox = 1;
	}
	else
	{
		m_PresetHelp = m_ActiveBox = 0;
	}
	m_FileName[0] = '\0';
	m_FileNameOld[0] = '\0';
	if (m_PresetHelp)
	{
		m_pConfig = NULL;
	}
	else
	{
		m_pConfig = pConfig;
		std::snprintf(Text, 64, "Config: %s", m_pConfig->getKeyWord());
	}

	if (m_Dlg.rgbDlg.Width > 0)
		GUI_DlgFree(&m_Dlg);
	m_Dlg = GUI_DlgCreate(m_Width, m_Height, Text);
	m_Dlg.x = m_PosX;
	m_Dlg.y = m_PosY;
	m_bUpdate = true;
	m_bDynamicUpdate = false;
	m_bIsVisible = true;
	m_Sel = 1;
	m_SelOld = m_Sel;
	m_FileSel = 0;
	GUI_DlgSetGradCol(&m_Dlg, m_pApp->GetGradCol());
	m_SelBoxText[0] = '\0';
	m_EditBoxText[0] = '\0';
	m_BoxTextC[0] = '\0';

	GUI_DlgGetZeroZero(&m_Dlg, &PosX, &PosY);
	PosX += 4;
	PosY += 4;
	x = PosX;
	y = PosY;
	if (m_PresetHelp)
	{
		iWidth = m_Icon[ICON(ID_UP)].Width;
		iHeight= m_Icon[ICON(ID_UP)].Height;

		GUI_DlgAddIconButton(&m_Dlg, x, y, iWidth, iHeight, NULL,
			&m_Icon[ICON(ID_UP)], ID_UP);
		GUI_DlgAddIconButton(&m_Dlg, x + iWidth + 6, y, iWidth, iHeight, NULL,
			&m_Icon[ICON(ID_DOWN)], ID_DOWN);

		y += iHeight + 6;
		GUI_DlgAddIconButton(&m_Dlg, x, y, iWidth, iHeight, NULL,
			&m_Icon[ICON(ID_LEFT)], ID_LEFT);
		GUI_DlgAddIconButton(&m_Dlg, x + iWidth + 6, y, iWidth, iHeight, NULL,
			&m_Icon[ICON(ID_RIGHT)], ID_RIGHT);
	}
	else
	{
		iWidth = m_Icon[ICON(ID_UPA)].Width;
		iHeight= m_Icon[ICON(ID_UPA)].Height;

		GUI_DlgAddIconButton(&m_Dlg, x, y, iWidth, iHeight, NULL,
			&m_Icon[ICON(ID_UPA)], ID_UP);

		y += iHeight + 4;
		GUI_DlgAddIconButton(&m_Dlg, x, y, iWidth, iHeight, NULL,
			&m_Icon[ICON(ID_DOWNA)], ID_DOWN);
	}
	iWidth = m_pApp->GetIcon(IconId::OK)->Width;
	iHeight= m_pApp->GetIcon(IconId::OK)->Height;

	y += iHeight + 4;
	m_pDlgItem[INDEX(ID_SAVE)] =
		GUI_DlgAddAnimButton(&m_Dlg, x, y, iWidth, iHeight,
			m_pStrings->getValue("DlgConfigSave", "_Save Param"), m_SIcon, 2, ID_SAVE);

	y += iHeight + 4;
	GUI_DlgAddIconButton(&m_Dlg, x, y, iWidth, iHeight,
		m_pStrings->getValue("DlgConfigSet", "_Set Values"), m_pApp->GetIcon(IconId::OK), ID_OK);

	y += iHeight + 4;
	GUI_DlgAddIconButton(&m_Dlg, x, y, iWidth, iHeight,
		m_pApp->GetStr(StringTable::BACK), m_pApp->GetIcon(IconId::BACK), DLG_ABORT);

	SelPosX = PosX + iWidth + 4;
	SelPosY = PosY;
	SelWidth = 248;
	SelHeight = y + iHeight - PosY;

	// Check for Preset.ini
	if (m_PresetHelp)
	{
		m_pApp->SetTP(TextStyle::MONOSPACE);
		VIB_ChDir(VIB_Drive[1], "PRESET");
		m_pDlgItem[INDEX(ID_SBB)] =
			GUI_DlgAddFileSelectBox(&m_Dlg, SelPosX, SelPosY, SelWidth / 2, SelHeight,
				VIB_Drive[1], m_FileName, 0, "INI", ID_SBB);
		VIB_ChDir(VIB_Drive[1], "..");
		SelPosX += 128;
		SelPosY = PosY;
		SelWidth = 372;
		SelHeight = m_Dlg.rgbDlg.Height - PosY - 32 - 32;

		m_pDlgItem[INDEX(ID_SBA)] =
			GUI_DlgAddSelectBox(&m_Dlg,	SelPosX, SelPosY, SelWidth, SelHeight,
				m_SelBoxText, &m_Sel, ID_SBA);

		m_pApp->SetTP(TextStyle::NORMAL);
		m_EditPosX = SelPosX + 376;
		m_EditPosY = SelPosY + 24;
		m_EditWidth = m_Dlg.rgbDlg.Width - PosX - 16 - 102 - 124 - 372;
		m_pDlgItem[INDEX(ID_EDITBOX)] =
			GUI_DlgAddSEditBox2(&m_Dlg, m_EditPosX, m_EditPosY, m_EditWidth,
				m_EditBoxText, ID_EDITBOX);
		
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_INVISIBLE);
	
		// Put selected line to FileSelectBox
		IdSBB = GUI_DlgGotoItem(&m_Dlg, ID_SBB);
		m_FileSel = GUI_DlgGetFileSelectBoxLine(m_pDlgItem[INDEX(IdSBB)]);
		GUI_DlgGotoItem(&m_Dlg, ID_SBB);
		GUI_DlgRefreshItem(&m_Dlg, m_pDlgItem[INDEX(IdSBB)]);// Get Filename of selected
	}
	else
	{
		BuildSelBoxText();
		SelWidth += 124;
		m_pApp->SetTP(TextStyle::MONOSPACE);
		m_pDlgItem[INDEX(ID_SBA)] =
			GUI_DlgAddSelectBox(&m_Dlg, SelPosX, SelPosY, SelWidth, SelHeight,
				m_SelBoxText, &m_Sel, ID_SBA);
		m_pApp->SetTP(TextStyle::NORMAL);
		m_EditPosX = SelPosX + 252 + 124;
		m_EditPosY = SelPosY + 24;
		m_EditWidth = m_Dlg.rgbDlg.Width - PosX - 12 - 102 - SelWidth;
		m_pDlgItem[INDEX(ID_EDITBOX)] =
			GUI_DlgAddSEditBox2(&m_Dlg, m_EditPosX, m_EditPosY, m_EditWidth,
				m_EditBoxText, ID_EDITBOX);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_INVISIBLE);
		GUI_DlgGotoItem(&m_Dlg, ID_SBA);
	}
	m_ParamName[0] = '\0';
	m_pApp->SetTP(TextStyle::BOLD);
	GUI_DlgAddDynamicText(&m_Dlg, m_EditPosX, SelPosY, m_ParamName);
	m_pApp->SetTP(TextStyle::NORMAL);
	m_ParamComment[0] = '\0';
	GUI_DlgAddDynamicText(&m_Dlg, m_EditPosX, m_EditPosY + 28, m_ParamComment);
	m_ParamRange[0] = '\0';
	GUI_DlgAddDynamicText(&m_Dlg, m_EditPosX, m_EditPosY + 48, m_ParamRange);
	m_Value = 0;
	m_pDlgItem[INDEX(ID_VEDITBOX)] =
		GUI_DlgAddVEditBox(&m_Dlg, m_EditPosX, m_EditPosY, m_EditWidth,
			&m_Value, -(2 ^ 31 - 1), 2 ^ 31, //-2147483647, 2147483648
			ID_VEDITBOX);
	GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_INVISIBLE);
}

/*************************************************************************/
void CDlgConfig::Delete()
/*************************************************************************/
/*! @brief Deletes the dialog and disassociates it from CDlgConfig::m_pConfig.

	This function has to be called before a new instance of \p Config can be
	assigned.
**************************************************************************/
{
	m_Activity = 0;
	m_bIsVisible = false;
	m_bUpdate = false;
	m_bDynamicUpdate = false;

	if (m_Dlg.rgbDlg.Width > 0)
		GUI_DlgFree(&m_Dlg);
}

/*************************************************************************/
void CDlgConfig::Update()
/*************************************************************************/
/*! @brief Handles the focus variables.

	CDlgConfig is modal. Since Update() assumes the dialog to be
	non-modal, the update function has to be re-implemented.
**************************************************************************/
{
	const int DlgFocus = m_pApp->GetDlgFocus();
	const int DlgNewFocus = m_pApp->GetDlgNewFocus();

	if (DlgNewFocus != DlgFocus)
	{
		if (DlgNewFocus == m_ID)
		{
			m_Activity = -1;
			m_bIsVisible = true;
			m_bUpdate = true;
		}
		if (DlgFocus == m_ID)
		{
			m_Activity = 0;
			m_bIsVisible = false;
			m_bUpdate = false;
			m_bDynamicUpdate = false;
		}
	}
}

/*************************************************************************/
int CDlgConfig::OnUserIO(int nKey)
/*************************************************************************/
/*! @brief Handles the user inputs.

	This function implements a loop, which is only left when the user makes
	an input to leave the dialog.
	@param nKey Last key code the user entered 
	@return \p m_Activity (!= \p DLG_EOF if the user has made an input)
**************************************************************************/
{
	int ID;

	m_Activity = -1;
	if (m_pApp->IsDlgGettingFocus(m_ID))
	{
		GUI_DlgEnable(&m_Dlg, 1);
		m_Activity = -1;
		m_bIsVisible = true;
		m_bUpdate = true;
		if (GUI_DlgIsDItemSelected(m_pDlgItem[INDEX(ID_SBA)]))
			ChangeParam();
	}
	while (m_pApp->IsDlgInFocus(m_ID))
	{
		m_bIsVisible = true;

		// Change select box text if Down/Up button is not pressed, when creating Preset Dlg
		if ((GUI_DlgGetDItemState(m_pDlgItem[INDEX(ID_SBB)])) && m_PreDownUp == 0)
		{
			BuildSelBoxText();
			m_pApp->SetTP(TextStyle::MONOSPACE);
			GUI_DlgModifySelectBoxText(m_pDlgItem[INDEX(ID_SBA)], m_SelBoxText);
			m_pApp->SetTP(TextStyle::NORMAL);
			m_PreDownUp = 1;
		}
		nKey = getc(m_fKB);
		if (nKey == EOF)
			_UNSET(m_fKB, _STATEOF);
		Show();
		// To synchronize keyboard and direction button
		switch (nKey)
		{
		case KB_DOWN:
			m_Activity = ID_DOWN;
			break;
		case KB_UP:
			m_Activity = ID_UP;
			break;
		case KB_LEFT:
			m_Activity = ID_LEFT;
			break;
		case KB_RIGHT:
			m_Activity = ID_RIGHT;
			break;
		case KB_ENTER:
			m_Activity = ID_OK;
			break;
		default:
			m_Activity = GUI_DlgWork(&m_Dlg, nKey);
			break;
		}
		if (m_Activity != DLG_EOF)
		{
			if (m_Activity == DLG_VALUE_CHANGED)
			{
				// To synchronize keyboard, direction button and mouse
				ID = GUI_DlgGetSelectedID(&m_Dlg);
				switch (ID)
				{
				case ID_EDITBOX:
					m_ActiveBox = 3;
					break;
				case ID_VEDITBOX:
					m_ActiveBox = 2;
					break;
				case ID_SBA:
					m_ActiveBox = 0;
					break;
				case ID_SBB:
					m_ActiveBox = 1;
					GUI_DlgUpdate3(&m_Dlg, m_pDlgItem[INDEX(ID_SBB)], m_Dlg.x, m_Dlg.y);
					m_FileSel = GUI_DlgGetFileSelectBoxLine(m_pDlgItem[INDEX(ID_SBB)]);
					PreDownUp();
					m_ParamName[0]    = '\0';
					m_ParamComment[0] = '\0';
					m_ParamRange[0]	  = '\0';
					if (GUI_DlgGetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)]))
						GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_INVISIBLE);
					else if (GUI_DlgGetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)]))
						GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_INVISIBLE);
					break;
				}
			}
			switch (m_Activity)
			{	
			case ID_DOWN:
				if (m_ActiveBox == 0)
				{
					m_Sel++;
					UpdateSelBox();
				}
				else if (m_ActiveBox == 1)
				{
					m_FileSel += 1;
					GUI_DlgModifyFileSelectBoxLine(m_pDlgItem[INDEX(ID_SBB)], m_FileSel);
					GUI_DlgUpdate3(&m_Dlg, m_pDlgItem[INDEX(ID_SBB)], m_Dlg.x, m_Dlg.y);
					PreDownUp();
					m_FileSel = GUI_DlgGetFileSelectBoxLine(m_pDlgItem[INDEX(ID_SBB)]);
				}
				break;
			case ID_UP:
				if (m_ActiveBox == 0)
				{
					m_Sel = (m_Sel > 1) ? m_Sel - 1: 1;
					UpdateSelBox();
				}
				else if (m_ActiveBox == 1)
				{
					m_FileSel = (m_FileSel > 1) ? m_FileSel - 1: 1;
					GUI_DlgModifyFileSelectBoxLine(m_pDlgItem[INDEX(ID_SBB)], m_FileSel);
					GUI_DlgUpdate3(&m_Dlg, m_pDlgItem[INDEX(ID_SBB)], m_Dlg.x, m_Dlg.y);
					PreDownUp();
					m_FileSel = GUI_DlgGetFileSelectBoxLine(m_pDlgItem[INDEX(ID_SBB)]);
				}
				break;
			case ID_LEFT:
				if (m_ActiveBox == 0 && m_PresetHelp != 0)
				{
					GUI_DlgGotoItem(&m_Dlg, ID_SBB);
					m_ActiveBox = 1;  
					m_ParamName[0] 	  = '\0';
					m_ParamComment[0] = '\0';
					m_ParamRange[0]	  = '\0';
					GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_INVISIBLE);
					GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_INVISIBLE);
				}
				else if (m_ActiveBox == 1)
				{
					GUI_DlgGotoItem(&m_Dlg, ID_SBB);
				}
				else if (m_ActiveBox == 2 || m_ActiveBox == 3)
				{
					GUI_DlgGotoItem(&m_Dlg, ID_SBA);
					m_ActiveBox = 0;
				}
				break;
			case ID_RIGHT:
				if (m_ActiveBox == 1)
				{
					GUI_DlgGotoItem(&m_Dlg, ID_SBA);
					m_ActiveBox = 0;
					m_Sel = m_SelOld;
					ChangeParam();
				}
				else if (m_ActiveBox == 0)
				{
					GotoItem();
				}
				else
				{
					if (m_ActiveBox == 2)
						GUI_DlgGotoItem(&m_Dlg, ID_VEDITBOX);
					else if (m_ActiveBox == 3)
						GUI_DlgGotoItem(&m_Dlg, ID_EDITBOX);
				}
				break;
			case ID_OK:
				ID = GUI_DlgGetSelectedID(&m_Dlg);
				if (ID == ID_SBA)
				{
					GotoItem();
				}
				else
				{
					ChangeValue();
					m_Sel++;
					GUI_DlgGotoItem(&m_Dlg, ID_SBA);
					m_ActiveBox = 0;
					UpdateSelBox(1);
				}
				break;
			case DLG_ABORT:
				Delete();
				m_Activity = 0;
				m_bIsVisible = false;
				m_bUpdate = false;
				m_PreDownUp = 0;
				m_bDynamicUpdate = false;
				m_pApp->RecBackground(1);
				m_pApp->SetDlgNewFocus(DialogId::TOOLS);
				break;
			case ID_SAVE:
				GUI_DlgSetAnimButton(m_pDlgItem[INDEX(ID_SAVE)], 1);
				Show();
				if (m_pConfig)
				{
					if (m_PresetHelp)
					{
						VIB_ChDir(VIB_Drive[1], "PRESET");
						m_pConfig->saveToIni(VIB_Drive[1], m_FileName);
						VIB_ChDir(VIB_Drive[1], "..");
					}
					else
					{
						m_pConfig->saveToIni(VIB_Drive[1]);
					}
				}
				GUI_DlgSetAnimButton(m_pDlgItem[INDEX(ID_SAVE)], 0);
				GUI_DlgGotoItem(&m_Dlg, ID_SBA);
				m_ActiveBox = 0;
				Show();
				break;
			}
			if (m_Sel != m_SelOld)
			{
				if (m_Sel)
				{
					ChangeParam();
					m_SelOld = m_Sel;
				}
			}
		}
		else
		{
			VIB_WaitBios(25);
		}
	}
	return m_Activity;
}

/*************************************************************************/
void CDlgConfig::BuildSelBoxText()
/*************************************************************************/
/*! @brief Creates text for SelectBox according to the \p pC parameter list.
**************************************************************************/
{
	char row[64], Text[64];
	int j;

	m_SelBoxText[0] = '\0';
	if (m_pConfig == NULL)
	{
		VIB_ChDir(VIB_Drive[1], "PRESET");
		m_pConfig = Config::loadFromIni(VIB_Drive[1], m_FileName, "PRESET_INI");
		VIB_ChDir(VIB_Drive[1], "..");
	}
	
	std::snprintf(Text, 64, "Config: %s", m_pConfig->getKeyWord());
	GUI_DlgSetTitle(&m_Dlg, Text);
	if (m_pConfig)
	{
		for (m_pParam = m_pConfig->getFirstParam(); m_pParam; m_pParam = m_pParam->next)
		{
			if (m_pParam->type != TypeCommentLine)
			{
				switch (m_pParam->type)
				{
				// Conversion of each parameter value to char
				case TypeInteger:
					sprintf(m_BoxTextC, "%d", m_pParam->value.Int);
					break;
				case TypeDouble:
					sprintf(m_BoxTextC, "%f", m_pParam->value.Double);
					break;
				case TypeString:
					sprintf(m_BoxTextC, "%s", m_pParam->value.String);
					break;
				}
				strcpy(row, m_pParam->name);
				j = strlen(m_pParam->name);
				memset(&row[j], '.', 30 - j);
				strcpy(row + 30, m_BoxTextC);
				strcat(row, "\n");
			}
			else
			{
				std::snprintf(row, 64, "--%s--\n", m_pParam->comment);
				row[62] = '\n';
			}
			strcat(m_SelBoxText, row);
		}
	}
}

/*************************************************************************/
void CDlgConfig::ChangeParam()
/*************************************************************************/
/*! @brief Updates the dialog elements when a new parameter is selected.

	The necessary dialog elements to edit the selected parameter are 
	enabled and filled with value, minimum, maximum, comment, ...
	The elements not used for the current type of parameter are set to
	\p DLG_I_INVISIBLE. 
**************************************************************************/
{
	if (m_pConfig == NULL)
		return;

	m_pParam = m_pConfig->getFirstParam();
	
	for (int i = 1; i != m_Sel; i++)
		m_pParam = m_pParam->next;

	if (m_pParam->type != TypeCommentLine)
	{
		VIB_SStrCpy(m_ParamName, m_pParam->name, 32);
		if (m_pParam->comment != NULL)
			VIB_SStrCpy(m_ParamComment, m_pParam->comment, 80);
		else
			m_ParamComment[0] = '\0';
		if (m_pParam->hasLimits)
		{
			switch (m_pParam->type)
			{
			case TypeInteger:
				sprintf(m_ParamRange, "Range: %d...%d", m_pParam->min.Int, m_pParam->max.Int);
				break;
			case TypeDouble:
				sprintf(m_ParamRange, "Range: %f...%f", m_pParam->min.Double, m_pParam->max.Double);
				break;
			case TypeString:
				sprintf(m_ParamRange, "Maximum string length: %d", m_pParam->maxS);
				break;
			}
		}
		else
		{
			m_ParamRange[0] = '\0';
		}
	}
	else
	{
		m_ParamComment[0] = '\0';
		m_ParamName[0] = '\0';
		m_ParamRange[0] = '\0';
	}

	switch (m_pParam->type)
	{
	case TypeInteger:
		UpdateVEditBox();
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_INVISIBLE);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_ENABLED);
		break;
	case TypeDouble:
		sprintf(m_EditBoxText, "%f", m_pParam->value.Double);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_ENABLED);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_INVISIBLE);
		break;
	case TypeCommentLine:
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_INVISIBLE);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_INVISIBLE);
		break;
	case TypeString:
		strcpy(m_EditBoxText, m_pParam->value.String);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)], DLG_I_ENABLED);
		GUI_DlgSetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)], DLG_I_INVISIBLE);
		break;
	}
}

/*************************************************************************/
void CDlgConfig::UpdateSelBox(bool Rebuild)
/*************************************************************************/
/*! @brief Updates text of SelectBox and modifies the dialog element.
**************************************************************************/
{
	if (Rebuild)
		BuildSelBoxText();
	m_pApp->SetTP(TextStyle::MONOSPACE);
	GUI_DlgModifySelectBoxText(m_pDlgItem[INDEX(ID_SBA)], m_SelBoxText);
	m_pApp->SetTP(TextStyle::NORMAL);
}

/*************************************************************************/
void CDlgConfig::UpdateVEditBox()
/*************************************************************************/
/*! @brief Modifies the VEditBox.
**************************************************************************/
{
	int nMin = (-1000000);
	int nMax = 10000000;

	if (m_pParam->hasLimits)
	{
		nMin = m_pParam->min.Int;
		nMax = m_pParam->max.Int;
	}
	m_Value = m_pParam->value.Int;
	GUI_DlgModifyVEditBox(m_pDlgItem[INDEX(ID_VEDITBOX)],
		m_EditPosX, m_EditPosY, m_EditWidth, nMin, nMax);
}

/*************************************************************************/
int CDlgConfig::ChangeValue()
/*************************************************************************/
/*! @brief Writes the changed parameter value back to the list in CDlgConfig::m_pConfig.
**************************************************************************/
{
	int ret = 0;

	if (m_pConfig == NULL)
		return -1;

	switch (m_pParam->type)
	{
	case TypeInteger:
		m_pConfig->setValue(m_pParam->name, m_Value);
		break;
	case TypeDouble:
		m_pConfig->setValue(m_pParam->name, (double)atof(m_EditBoxText));
		break;
	case TypeString:
		m_pConfig->setValue(m_pParam->name, m_EditBoxText);
		break;
	default:
		ret = -1;
		break;
	}
	return ret;
}

/*************************************************************************/
void CDlgConfig::PreDownUp()
/*************************************************************************/
/*! @brief Changes dialog if preset button down or up are set.
**************************************************************************/
{
	if (strcmp(m_FileNameOld, m_FileName) != 0)
	{
		m_pConfig = NULL;
		BuildSelBoxText();
		m_pApp->SetTP(TextStyle::MONOSPACE);
		GUI_DlgModifySelectBoxText(m_pDlgItem[INDEX(ID_SBA)], m_SelBoxText);
		m_pApp->SetTP(TextStyle::NORMAL);
		GUI_DlgUpdate3(&m_Dlg, m_pDlgItem[INDEX(ID_SBA)], m_Dlg.x, m_Dlg.y);
		strcpy(m_FileNameOld, m_FileName);
		m_PreDownUp = 1;
		m_Sel = 1;
		m_SelOld = m_Sel;
	}
}

/****************************************************************************/
void CDlgConfig::GotoItem()
/****************************************************************************/
/*! @brief Go to current selected item.
*****************************************************************************/
{
	if (GUI_DlgGetDItemState(m_pDlgItem[INDEX(ID_VEDITBOX)]))
	{
		GUI_DlgGotoItem(&m_Dlg, ID_VEDITBOX);
		m_ActiveBox = 2;
	}
	else if (GUI_DlgGetDItemState(m_pDlgItem[INDEX(ID_EDITBOX)]))
	{
		GUI_DlgGotoItem(&m_Dlg, ID_EDITBOX);
		m_ActiveBox = 3;
	}
}

/****************************************************************************/
