/*************************************************************************\
* "$RCSfile: CDlgMain.cpp,v $"                                            *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2011/10/20 07:13:59 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.32 $                                 *
\*************************************************************************/
/*! \file CDlgMain.cpp
*	\brief Implementation CDlgMain Class
**************************************************************************/

#include "VIC_PS.h"
#include "CRegion.h"
#include "CPSApp.hpp"
#include "CDlg.hpp"
#include "CDlgMain.hpp"
#include "CDlgPassword.hpp"

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

/*************************************************************************/
CDlgMain::CDlgMain(int x, int y, int Width, int Height) :
	CDlg(x, y, Width, Height, DialogId::MAIN)
/*************************************************************************/
/*! @brief Constructor
**************************************************************************/
{
	int iWidth, iHeight;
	int PosX, PosY;

	GUI_DlgSetTitle(&m_Dlg, m_pStrings->getValue("DlgMainTitle", "Main"));
	m_bUpdate = true;
	m_bIsVisible = true;		// The main dialog is visible at first

	// Icons are loaded from SD card
	VIB_ChDir(VIB_Drive[1], m_pApp->GetSkinPath());
	m_Icon[ID_CAMERA]  = ReadIcon("B_CAMSET.PNG", 98, 98);
	m_Icon[ID_TOOLS]   = ReadIcon("B_TOOLB.PNG", 98, 98);
	m_Icon[ID_PREVIEW] = ReadIcon("B_PREV.PNG", 98, 98);
	m_Icon[ID_PSCAM]   = ReadIcon("B_PSCAM.PNG", 98, 98);
	VIB_ChDir(VIB_Drive[1], "..");

	// IconButton positions are calculated with iWidth an iHeight
	iWidth = m_Icon[ID_TOOLS].Width;
	iHeight = m_Icon[ID_TOOLS].Height;
	GUI_DlgGetZeroZero(&m_Dlg, &PosX, &PosY);
	PosX += 4;
	PosY += 4;

	m_pDlgItem[ID_CAMERA] =
		GUI_DlgAddIconButton(&m_Dlg, PosX, PosY, iWidth, iHeight,
							 m_pStrings->getValue("DlgMainSensor", "_Sensor"),
							 &m_Icon[ID_CAMERA], ID_CAMERA);
	m_pDlgItem[ID_PSCAM] =
		GUI_DlgAddIconButton(&m_Dlg, PosX + iWidth + 4, PosY, iWidth, iHeight,
							 m_pStrings->getValue("DlgMainCamera", "_Camera"),
							 &m_Icon[ID_PSCAM], ID_PSCAM);

	if (!m_pApp->IsCameraPresent())
	{
		GUI_DlgSetDItemState(m_pDlgItem[ID_CAMERA], DLG_I_DISABLED);
		GUI_DlgSetDItemState(m_pDlgItem[ID_PSCAM], DLG_I_DISABLED);
	}

	GUI_DlgAddIconButton(&m_Dlg, PosX, PosY + iHeight + 4, iWidth, iHeight,
						 m_pStrings->getValue("DlgMainSaveImage", "Save _Pic"),
						 m_pApp->GetIcon(IconId::SAVE), ID_SAVEIMAGE);
	GUI_DlgAddIconButton(&m_Dlg, PosX + iWidth + 4, PosY + iHeight + 4, iWidth, iHeight,
						 m_pStrings->getValue("DlgMainTools", "_Tools"),
						 &m_Icon[ID_TOOLS], ID_TOOLS);

	GUI_DlgAddIconButton(&m_Dlg, PosX, PosY + 2 * (iHeight + 4), iWidth, iHeight,
						 m_pStrings->getValue("DlgMainViewParam", "_View Par."),
						 &m_Icon[ID_PREVIEW], ID_PREVIEW);

	m_pDlgItem[ID_APP] =
		GUI_DlgAddIconButton(&m_Dlg, PosX + iWidth + 4, PosY + 2 * (iHeight + 4), iWidth, iHeight,
							 m_pStrings->getValue("DlgMainApp", "_App"),
							 m_pApp->GetIcon(IconId::APP), ID_APP);

	// ChoiceBox and label for "graph-mode"
	PosY = PosY + 3 * (iHeight + 4) + 8;
	GUI_DlgAddLabel(&m_Dlg, PosX + 4, PosY + 4, m_pStrings->getValue("DlgMainGraph", "_Graph"), ID_GRAPH);
	GUI_DlgAddChoiceBox(&m_Dlg, PosX + 60, PosY, 140, 24,
						"CPU Load\nTemperature\nFramerate\nInfo\n",
						2, &m_pApp->m_ShowGraph, ID_GRAPH);

	// ChoiceBox and label for "view-mode"
	PosY += 28;
	GUI_DlgAddLabel(&m_Dlg, x + 4, PosY + 4, m_pStrings->getValue("DlgMainView", "Vie_w"), ID_VIEW);
	GUI_DlgAddChoiceBox(&m_Dlg, PosX + 60, PosY, 140, 24,
						"Camera\nApplication\n",
						2, &m_pApp->m_View, ID_VIEW);

	// ChoiceBox and label for "info-mode"
	PosY += 28;
	GUI_DlgAddLabel(&m_Dlg, PosX + 4, PosY + 4, m_pStrings->getValue("DlgMainInfo", "_Info"), ID_INFO);
	GUI_DlgAddChoiceBox(&m_Dlg, PosX + 60, PosY, 140, 24,
						"Logfile\nHistogram\nHisto Log\nApplication\n",
						2, &m_pApp->m_Info, ID_INFO);
}

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

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

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

/*************************************************************************/
int CDlgMain::OnUserIO(int nKey)
/*************************************************************************/
/*! @brief Function processes the user inputs.
	@param nKey Last key code the user entered 
	@return CDlg::m_Activity (!= \p DLG_EOF if the user has made an input)
**************************************************************************/
{
	static int s_GraphOld = m_pApp->m_ShowGraph;

	m_Activity = 0;

	if (m_pApp->IsDlgInFocus(m_ID))
	{
		// GUI_DlgWork() returns the ID of the last user input
		m_Activity = GUI_DlgWork(&m_Dlg, nKey);
		switch (m_Activity)
		{
		case ID_PSCAM: // when the ID_PSCAM button is pressed
			Enable(0);
			Show();
			m_pApp->SetDlgNewFocus(DialogId::CAM);
			m_bIsVisible = false;
			break;
		case ID_CAMERA:
			Enable(0);
			Show();
			if (CDlgPassword::GetInstance()->CheckPassword(m_fKB))
			{
				m_pApp->SetDlgNewFocus(DialogId::CAMEX);
				m_bIsVisible = false;
			}
			else
			{
				Enable(1);
			}
			break;
		case ID_SAVEIMAGE:
			Enable(0);
			Show();
			m_pApp->SetDlgNewFocus(DialogId::SAVEPIC);
			m_bIsVisible = false;
			break;
		case ID_PREVIEW:
			Enable(0);
			Show();
			m_pApp->SetDlgNewFocus(DialogId::PREV);
			m_bIsVisible = false;
			break;
		case ID_APP:
			Enable(0);
			Show();
			m_pApp->m_ViewOld = m_pApp->m_View;
			m_pApp->m_View = ViewType::APP;
			m_pApp->SetDlgNewFocus(DialogId::APP);
			m_bIsVisible = false;
			break;
		case ID_TOOLS:
			Enable(0);
			Show();
			m_pApp->SetDlgNewFocus(DialogId::TOOLS);
			m_bIsVisible = false;
			break;
		case DLG_MOUSE_PRESSED:
			m_pApp->ChangeInfoArea(&m_Dlg);
			break;
		}
		if (s_GraphOld != m_pApp->m_ShowGraph)
		{
			m_pApp->m_GraphUpdateCounter = CPSApp::GRAPH_UPDATE_CYCLE;
			s_GraphOld = m_pApp->m_ShowGraph;
		}
	}
	return m_Activity;
}

/*************************************************************************/
void CDlgMain::SetAppIconLabel(const char *pLabel)
/*************************************************************************/
/*! @brief Sets label of application icon.
	@param pLabel New label
**************************************************************************/
{
	CIMAGE *pIcon = m_pApp->GetIcon(IconId::APP);
	const int iWidth  = pIcon->Width;
	const int iHeight = pIcon->Height;
	int x, y;

	GUI_DlgGetZeroZero(&m_Dlg, &x, &y);
	x = x + iWidth + 8;
	y = y + 4 + 2 * (iHeight + 4);

	GUI_DlgModifyIconButton(m_pDlgItem[ID_APP], x, y, iWidth, iHeight, pLabel, pIcon);
}

/*************************************************************************/
int CDlgMain::GetYOrigin()
/*************************************************************************/
/*! @brief Gets y origin of dialog area.
**************************************************************************/
{
	int x, y;

	GUI_DlgGetZeroZero(&m_Dlg, &x, &y);
	return y;
}

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