/*************************************************************************\
* "$RCSfile: CDlgApp.cpp,v $"                                             *
***************************************************************************
* Information by CVS or VSS:                                              *
*     Last check in on: $Date: 2009-08-11 15:59:38 $                      *
*     by                $Author: schluckebier $
*     Revision        : $Revision: 1.10 $                                 *
\*************************************************************************/
/*! \file CDlgApp.cpp
*	\brief Implementation CDlgApp Class
*	\author (c) 2007 - 2013 IMAGO Technologies GmbH
*
*	$Revision: 1.10 $
*	$Date: 2009-08-11 15:59:38 $
**************************************************************************/

#include "VIC_PS.h"
#include "CRegion.h"
#include "CLog.hpp"
#include "CPSApp.hpp"
#include "CDlg.hpp"
#include "CDlgPreviewSettings.hpp"
#include "CDlgApp.hpp"

#pragma DATA_SECTION("VersionDate")
const char g_VersionString[60] = "PSC SDK "__DATE__;	//!< Version string

/*************************************************************************/
CDlgApp::CDlgApp(int x, int y, int Width, int Height)
		: CDlg(x, y, Width, Height)
/*************************************************************************/
/*! @brief Constructor of CDlgApp

	Use this constructor to create elements for user dialog and
	initialization of user variables.\n
	Call CPSApp::SetApplicationIcon() and CPSApp::SetApplicationName() to
	set an application specific name and icon.
	@param x Horizontal position of dialog
	@param y Vertical position of dialog
	@param Width Width of dialog
	@param Height Height of dialog
	@par Example:
	@code
CDlgApp::CDlgApp(int x, int y, int Width, int Height)
	   : CDlg(x, y, Width, Height)
{
	SetID(DialogId::APP);
	GUI_DlgSetTitle(&m_Dlg, "Example 1");

	// Add cancel button
	int iHeight = m_pApp->GetIcon(IconId::BACK).Height;
	GUI_DlgAddIconButton(&m_Dlg, 110, Height - (iHeight + 8),
		98, 98, "_Back", m_pApp->GetIcon(IconId::BACK), DLG_ABORT);
}
	@endcode
**************************************************************************/
{
	SetID(DialogId::APP);
	GUI_DlgSetTitle(&m_Dlg, "App Dialog");

	// Icon is loaded from memory card
	VIB_ChDir(VIB_Drive[1], m_pApp->GetSkinPath());
	m_Icon[ICON(ID_APP_ROI)] = ReadIcon("B_ROI.PNG", 98, 98);
	VIB_ChDir(VIB_Drive[1], "..");

	// Create buttons
	int iWidth  = m_Icon[ICON(ID_APP_ROI)].Width;
	int iHeight = m_Icon[ICON(ID_APP_ROI)].Height;
	x = 8;
	GUI_DlgAddIconButton(&m_Dlg, x + iWidth + 4, y + 8, iWidth , iHeight, "_ROI",
						 &m_Icon[ICON(ID_APP_ROI)], ID_APP_ROI);
	y = m_Dlg.rgbDlg.Height - (iHeight + 8);
	GUI_DlgAddIconButton(&m_Dlg, x, y, 98, 98, "_OK",
						 m_pApp->GetIcon(IconId::OK), ID_APP_OK);
	GUI_DlgAddIconButton(&m_Dlg, x + 102, y, 98, 98, "_Back",
						 m_pApp->GetIcon(IconId::BACK), DLG_ABORT);

	m_SetROI = 0;
	m_Cursor.x = 0;
	m_Cursor.y = 0;
	m_ShowCursor = 0;

	// Create region of interest
	m_ROI.NewWH(100, 100, 200,200, "Application");

	// Create mailboxes for communication between 'TSK_Main' and 'TaskLive'
	m_MbxGui2Work = MBX_create(sizeof(APPMSG_GUI2WORK), APP_MAILBOXSIZE, NULL);
	m_MbxWork2Gui = MBX_create(sizeof(APPMSG_WORK2GUI), APP_MAILBOXSIZE, NULL);
}

/*************************************************************************/
CDlgApp::~CDlgApp()
/*************************************************************************/
/*! @brief Destructor

	Free user initialized data and dialog.
	@par Example:
	@code
CDlgApp::~CDlgApp()
{
	GUI_FreeCImage(&m_Icon[1]));
}
	@endcode
**************************************************************************/
{
	GUI_FreeCImage(&m_Icon[ICON(ID_APP_ROI)]);
}

/******************************************************************************/
int CDlgApp::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)
	@par Example:
	@code
int CDlgApp::OnUserIO(int nKey)
{
	m_Activity = GUI_DlgWork(&m_Dlg, nKey)
}
	@endcode
**************************************************************************/
{
	CDlgPreviewSettings *pDlgPreviewSettings = CDlgPreviewSettings::GetInstance();

	m_Activity = 0;

	// Application dialog is active
	if (m_pApp->IsDlgInFocus(m_ID))
	{
		if (m_SetROI)
		{
			CLog::Print("ROI.left = %d\n", m_ROI.m_Left);
			CLog::Print("ROI.top = %d\n", m_ROI.m_Top);
			CLog::Print("ROI.right = %d\n", m_ROI.m_Right);
			CLog::Print("ROI.bottom = %d\n", m_ROI.m_Bottom);

			pDlgPreviewSettings->FreeROI();
			pDlgPreviewSettings->FreeCursor();
			m_ShowROI = 0;
			m_ShowCursor = 0;
			m_SetROI = 0;
			GUI_DlgRefresh(&m_Dlg);
		}

		m_Activity = GUI_DlgWork(&m_Dlg, nKey);
		// m_Activity contains an event ID of the last user input
		switch (m_Activity)
		{
		case DLG_ABORT:
			m_pApp->SetDlgNewFocus(DialogId::MAIN);
//			m_pA->m_View = m_pA->m_ViewOld;
			m_bIsVisible = false;
			break;
		case ID_APP_ROI: // Change to CDlgPreviewSettings!!!
			m_SetROI = 1;
			// Setting DlgNewFocus to a value different to DLG_APP will bring
			// an other dialog in front.
			m_pApp->SetDlgNewFocus(DialogId::PREV);
			m_Cursor.x = m_ROI.m_Left;
			m_Cursor.y = m_ROI.m_Top;
			pDlgPreviewSettings->Enable(1);
			pDlgPreviewSettings->SetROI(&m_ROI);
			m_ShowCursor = 1;
			m_ShowROI = 1;
			pDlgPreviewSettings->SetCursor(&m_Cursor);
			pDlgPreviewSettings->UpdateROIValues();
			m_bIsVisible = false;
			break;
		case DLG_MOUSE_PRESSED:	// Handle mouse clicks in info and graph windows
			m_pApp->ChangeInfoArea(&m_Dlg);
			break;
		}

		if (m_Activity)
			GUI_DlgRefresh(&m_Dlg);
	}
	return m_Activity;
}

/******************************************************************************/
int CDlgApp::OnImage(YIMAGE *pSrc, CIMAGE *pDst)
/******************************************************************************/
/*! @brief Application process function

	The function is called from the grab task for every grabbed image. The
	function runs simultaneous to the grabbing of the next image. The image
	\p pDst is displayed by the main task via Ethernet.\n
	The priority of the grab task is higher than of the main task, so a slow
	network connection will not slow down the grab task. If the main task
	becomes too slow to display all, it will drop images.
	@param pSrc Pointer to source image (Grabbed)
	@param pDst Pointer to display image (Displayed)
	@return Image type\n(#AppImageType)\n
	When using a gray image only the red channel of \p pDst is used.
	@par Example:
	@code
int CDlgApp::OnImage(YIMAGE *pSrc, CIMAGE *pDst)
{
	if (m_pApp->m_View != ViewType::APP)	// Application window is not active
		return AppImageType::NO_IMAGE;

	// Copy the image from pSrc to pDst
	GUI_CopyYArea2RGB(pSrc, pDst, 0, 0, pSrc->Width, pSrc->Height, 0, 0);

	return AppImageType::COLOR;
}
	@endcode
**************************************************************************/
{
	int LineWidth;

	if (m_pApp->m_View != ViewType::APP)	// Application window is not active
		return AppImageType::NO_IMAGE;

	// Copy the image from pSrc to pDst
	GUI_CopyYArea2RGB(pSrc, pDst, 0, 0, pSrc->Width, pSrc->Height, 0, 0);

	LineWidth = (m_pApp->GetScale() < 1.0) ? 2 : 1;

	// A rectangle is drawn into the image to show the region of interest
	if (m_ShowROI)
	{
		GUI_Rect2(pDst, m_ROI.m_Left, m_ROI.m_Top, m_ROI.GetWidth(), m_ROI.GetHeight(), LineWidth, &rgbRed);
	}

	// A cross is drawn into the image to show cursor position
	if (m_ShowCursor)
	{
		GUI_Line2(pDst, m_Cursor.x, 0, m_Cursor.x, pDst->Height-1, &rgbMagenta, LineWidth, 0);
		GUI_Line2(pDst, 0, m_Cursor.y, pDst->Width-1, m_Cursor.y, &rgbMagenta, LineWidth, 0);
	}

	// Parse commands from main task. (Not used in this simple example!)
	APPMSG_GUI2WORK msg;
	if (MBX_pend(m_MbxGui2Work, &msg, 0))
	{
		switch (msg.command)
		{
		case APPCMD_A:
			break;
		case APPCMD_B:
			break;
		}
	}
	return AppImageType::COLOR;
}

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