/*************************************************************************\
* "$RCSfile: CDlgLog.cpp,v $"                                             *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2009/11/30 14:06:56 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.18 $                                 *
\*************************************************************************/
/*! \file CDlgLog.cpp
*	\brief Implementation CDlgLog Class
**************************************************************************/

#include "VIC_PS.h"
#include "CRegion.h"
#include "CLog.hpp"
#include "CPSApp.hpp"
#include "CDlg.hpp"
#include "CDlgLog.hpp"

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

/*************************************************************************/
CDlgLog::CDlgLog(int x, int y, int Width, int Height) :
	CDlg(x, y, Width, Height, DialogId::LOG)
/*************************************************************************/
/*! @brief Constructor
**************************************************************************/
{
	int iWidth, iHeight;
	int SelWidth, SelHeight;
	int PosX, PosY;

	GUI_DlgSetTitle(&m_Dlg, m_pStrings->getValue("DlgLogTitle", "Log"));
	m_Sel = 1;

	VIB_ChDir(VIB_Drive[1], m_pApp->GetSkinPath());
	m_Icon[ICON(ID_UP)]   = ReadIcon("BB_UP.PNG", 98, 98);
	m_Icon[ICON(ID_DOWN)] = ReadIcon("BB_DOWN.PNG", 98, 98);
	VIB_ChDir(VIB_Drive[1], "..");

	iWidth = m_Icon[ICON(ID_UP)].Width;
	iHeight = m_Icon[ICON(ID_UP)].Height;

	GUI_DlgGetZeroZero(&m_Dlg, &PosX, &PosY);
	PosX += 4;
	PosY += 4;
	GUI_DlgAddIconButton(&m_Dlg, PosX, PosY, iWidth, iHeight, NULL,
		&m_Icon[ICON(ID_UP)], ID_UP);
	GUI_DlgAddIconButton(&m_Dlg, PosX, PosY + iHeight + 4, iWidth, iHeight, NULL,
		&m_Icon[ICON(ID_DOWN)], ID_DOWN);
	m_pDlgItem[INDEX(ID_SAVE)] =
		GUI_DlgAddAnimButton(&m_Dlg, PosX, PosY + 2 * (iHeight + 4), iWidth, iHeight,
			m_pStrings->getValue("DlgLogSave", "_Save"), m_pApp->GetIcon(IconId::SAVE),
			2, ID_SAVE);
	GUI_DlgAddIconButton(&m_Dlg, PosX,	PosY + 3 * (iHeight + 4), iWidth, iHeight,
		m_pApp->GetStr(StringTable::BACK), m_pApp->GetIcon(IconId::BACK), DLG_ABORT);

	SelWidth = m_Dlg.rgbDlg.Width - PosX - iWidth - 12;
	SelHeight = m_Dlg.rgbDlg.Height - 40;
	m_pApp->SetTP(TextStyle::MONOSPACE);
	m_pDlgItem[INDEX(ID_SB)] =
		GUI_DlgAddSelectBox(&m_Dlg, PosX + iWidth + 4, PosY, SelWidth, SelHeight,
			CLog::GetInstance()->GetText(), &m_Sel, ID_SB);
	m_pApp->SetTP(TextStyle::NORMAL);
}

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

/*************************************************************************/
CDlgLog *CDlgLog::CreateInstance(int x, int y, int Width, int Height)
/*************************************************************************/
/*! @brief Creates CDlgMain 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 CDlgLog instance
**************************************************************************/
{
	if (m_pThis == NULL)
		m_pThis = new CDlgLog(x, y, Width, Height);
	return m_pThis;
}

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

/*************************************************************************/
int CDlgLog::OnUserIO(int nKey)
/*************************************************************************/
/*! @brief Function processes the user inputs

	CDlgLog is a modal full screen dialog.\n
	OnUserIO() 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)
**************************************************************************/
{
	CLog *pLog = CLog::GetInstance();

	m_Activity = -1;

	if (m_pApp->IsDlgGettingFocus(m_ID))
	{
		GUI_DlgEnable(&m_Dlg, 1);
		m_Activity = -1;
		m_bIsVisible = true;
		m_bUpdate = true;
		GUI_DlgModifySelectBoxText(m_pDlgItem[INDEX(ID_SB)], pLog->GetText());
	}

	while (m_pApp->IsDlgInFocus(m_ID))
	{
		m_bIsVisible = true;
		nKey = getc(m_fKB);
		if (nKey == EOF)
			_UNSET(m_fKB, _STATEOF);

		Show();
		m_Activity = GUI_DlgWork(&m_Dlg, nKey);
		switch (m_Activity)
		{
		case ID_UP:
			m_Sel -= 20;
			if (m_Sel < 1)
				m_Sel = 1;
			break;
		case ID_DOWN:
			m_Sel += 20;
			break;
		case ID_SAVE:
			GUI_DlgSetAnimButton(m_pDlgItem[INDEX(ID_SAVE)], 1);
			Show();
			pLog->Save();
			GUI_DlgSetAnimButton(m_pDlgItem[INDEX(ID_SAVE)], 0);
			Show();
			break;
		case DLG_ABORT:
			m_pApp->RecBackground(1);
			m_pApp->SetDlgNewFocus(DialogId::TOOLS);
			m_bIsVisible = false;
			break;
		default:
			VIB_WaitBios(25);
			break;
		}
	}
	return m_Activity;
}

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