/*************************************************************************\
* "$RCSfile: CDlgSaveImage.cpp,v $"                                       *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2010-06-24 07:58:33 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.19 $                                 *
\*************************************************************************/
/*! \file CDlgSaveImage.cpp
*	\brief Implementation CDlgSaveImage Class
**************************************************************************/

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

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

/*************************************************************************/
CDlgSaveImage::CDlgSaveImage(int x, int y, int Width, int Height) :
	CDlg(x, y, Width, Height, DialogId::SAVEPIC)
/*************************************************************************/
/*! @brief Constructor of CDlgSaveImage
	global settings.
	@param x Horizontal position of dialog
	@param y Vertical position of dialog
	@param Width Width of dialog
	@param Height Height of dialog
**************************************************************************/
{
	int iWidth, iHeight;
	int PosX, PosY;

	GUI_DlgSetTitle(&m_Dlg, m_pStrings->getValue("DlgSITitle", "Save Image"));
	m_VicPs = m_pApp->GetVicIni();
	m_pSI = m_pApp->GetImageSettings();
	m_Activity = 0;
	m_bUpdate = true;
	m_bDirty = false;
	iWidth  = m_pApp->GetIcon(IconId::SAVE)->Width;
	iHeight = m_pApp->GetIcon(IconId::SAVE)->Height;

	// Icons are loaded from directory 
	VIB_ChDir(VIB_Drive[1], m_pApp->GetSkinPath());
	m_Icon[ICON(ID_LOAD)] = ReadIcon("B_LOAD.PNG", 98, 98);
	VIB_ChDir(VIB_Drive[1], "..");

	GUI_DlgGetZeroZero(&m_Dlg, &PosX, &PosY);
	PosX += 4;
	PosY += 4;

	// ID_SAVE button is animated. It looks different while saving to SD card.
	m_pDlgItem[INDEX(ID_SAVE)] = GUI_DlgAddAnimButton(&m_Dlg, PosX, PosY, iWidth, iHeight,
		m_pStrings->getValue("DlgSISaveImage", "Save _Pic"), m_pApp->GetIcon(IconId::SAVE), 2, ID_SAVE);
	GUI_DlgAddIconButton(&m_Dlg, PosX + iWidth + 4, PosY, iWidth, iHeight,
		m_pStrings->getValue("DlgSILoadImage", "_Load Pic"), &m_Icon[ICON(ID_LOAD)], ID_LOAD);

	PosY += iHeight + 8;
	GUI_DlgAddLabel(&m_Dlg, PosX, PosY, "_File type", ID_FORMAT);

	PosY += 24;
	m_pDlgItem[INDEX(ID_FORMAT)] = GUI_DlgAddChoiceBox(&m_Dlg, PosX, PosY, 200, 24,
		"BMP\nPNG\nJPG", 2, &m_pSI->Format, ID_FORMAT);

	PosY += 32;
	m_pDlgItem[INDEX(ID_COUNT_CK)] = GUI_DlgAddCheckBox2(&m_Dlg, PosX, PosY, 200, 24,
		m_pStrings->getValue("DlgSIChAutonumber", "_Auto increment"), &m_pSI->Autonumber, ID_COUNT_CK);

	PosY += 32;
	GUI_DlgAddLabel(&m_Dlg, PosX, PosY, m_pStrings->getValue("DlgSIStartValue", "Start _Value"), ID_COUNT_EB);
	m_pDlgItem[INDEX(ID_COUNT_EB)] = GUI_DlgAddVEditBox(&m_Dlg, PosX + 136, PosY, 64,
		&m_pSI->Counter, 0, 999, ID_COUNT_EB);

	PosY += 24;
	GUI_DlgAddLabel(&m_Dlg, PosX, PosY, m_pStrings->getValue("DlgSINameprefix", "_Name prefix"), ID_FN_PREF_EB);
	m_pDlgItem[INDEX(ID_FN_PREF_EB)] = GUI_DlgAddSEditBox2(&m_Dlg, PosX + 136, PosY, 64,
		m_pSI->FilenamePrefix, ID_FN_PREF_EB);

	PosY += 28;
	m_pApp->GenerateFilename();
	GUI_DlgAddDynamicText(&m_Dlg, PosX, PosY, m_pSI->FullFilename);

	GUI_DlgAddIconButton(&m_Dlg, PosX + iWidth + 4, Height - (iHeight + 8), iWidth, iHeight,
		m_pApp->GetStr(StringTable::BACK), m_pApp->GetIcon(IconId::BACK), DLG_ABORT);
}

/*************************************************************************/
CDlgSaveImage::~CDlgSaveImage()
/*************************************************************************/
/*! @brief Destructor
**************************************************************************/
{
}

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

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

/*************************************************************************/
int CDlgSaveImage::OnUserIO(int nKey)
/*************************************************************************/
/*! @brief Function processes the user inputs.
	@param nKey Last key code the user has entered
	@return \p m_Activity (!= \p DLG_EOF if the user has made an input)
**************************************************************************/
{
	char *pExt;
	char Filename[256];

	m_Activity = 0;

	if (m_pApp->IsDlgInFocus(DialogId::SAVEPIC))
	{
		m_Activity = GUI_DlgWork(&m_Dlg, nKey);
		if (m_Activity)
			m_pApp->GenerateFilename();

		switch(m_Activity)
		{
		case ID_SAVE:
			// Animation of save button
			GUI_DlgSetAnimButton(m_pDlgItem[INDEX(ID_SAVE)], 1);
			Show();

			m_pApp->SaveImage();

			// Animation of save button end
			GUI_DlgSetAnimButton(m_pDlgItem[INDEX(ID_SAVE)], 0);
			Show();
			break;
		case ID_LOAD:
			m_pApp->StopGrabTask();
			switch (m_pSI->Format)
			{
				case ImageFormat::BMP: pExt = "BMP"; break;
				case ImageFormat::PNG: pExt = "PNG"; break;
				case ImageFormat::JPG: pExt = "JPG"; break;
				default: pExt = ""; break;
			}
			Enable(0);
			Show();
			VIB_ChDir(VIB_Drive[1], m_pSI->Path);
			m_pApp->SetTP(TextStyle::MONOSPACE);
			if (GUI_OpenFileBox(VIB_Drive[1], m_pApp->GetDispImage(), "Load image", Filename, pExt, m_fKB))
				m_pApp->LoadImage(Filename);
			VIB_ChDir(VIB_Drive[1], "..");
			m_pApp->SetTP(TextStyle::NORMAL);
			m_pApp->RecBackground(1);
			Enable(1);
			m_pApp->StartGrabTask();
			break;
		case DLG_MOUSE_PRESSED:	// Handle mouse clicks in info and graph windows
			m_pApp->ChangeInfoArea(&m_Dlg);
			break;
		case DLG_VALUE_CHANGED:
			m_bDirty = true;
			break;
		case DLG_ABORT:// *** Exit ***
			m_VicPs->setValue("SIPath", m_pSI->Path);
			m_VicPs->setValue("SIFormat", m_pSI->Format);
			m_VicPs->setValue("SIAutonumber", m_pSI->Autonumber);
			m_VicPs->setValue("SICounter", m_pSI->Counter);

			m_pApp->SetDlgNewFocus(DialogId::MAIN);
			m_bIsVisible = false;
			break;
		}
	}
	return m_Activity;
}

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