/*==============================================================================
	Copyright (C) 2008 IT Access.,Co.Ltd. All Rights Reserved.
	
	This software is the confidential and proprietary information 
	of IT Access.,Co.Ltd.
		
	Note:

==============================================================================*/

/*==============================================================================
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> INCLUDE. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
==============================================================================*/
#ifdef	_WIN32
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#endif

//#include "Geal.h"
#include "GealCommon.h"
#include "GealTypes.h"
#include "GealAPI.h"
#include "GealConfigAPI.h"
#include "GealUtilAPI.h"
#include "GealFigureAPI.h"
#include "GealStringAPI.h"
#include "GealTimerAPI.h"
#include "GealTargetAPI.h"
#include "GealLayerAPI.h"
#include "GealWidgetAPI.h"
#include "GealWRectAPI.h"
#include "GealWTextAPI.h"
#include "GealWButtonAPI.h"
#include "GealWPictureAPI.h"
#include "GealWGaugeAPI.h"
#include "GealWListAPI.h"
#include "GealWMenuAPI.h"
#include "GealWFigureAPI.h"

// Unicode lib
#include "GealUtilAPI2.h"

#include "GealRsxEnum.h"
#ifdef	_WIN32
	#include "TargetWin32\TargetTypes.h"
#else	// Target
	#include "Target\TargetTypes.h"
#endif

extern GE_WGTID GxActiveWidgetSet(GE_WGTID eWgtID) ;

#ifdef	_WIN32
// Dummy 
static void LCD_SetBackLight( int n )  { }

#endif

/*==============================================================================
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> CONFIGURATION. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
==============================================================================*/
#define	_TOUCH_ONLY	//　タッチパネルしかない場合に強引にDEMOを動かす

/*==============================================================================
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TYPE. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
==============================================================================*/

/*==============================================================================
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MEMBER. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
==============================================================================*/
//static GE_TCHAR		m_tcEditBox[10];
//static GE_POINT		m_sAnimStartPt;		/* アニメーション開始位置 */

/*==============================================================================
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PRIVATE PROCEDURE. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
==============================================================================*/
/*------------------------------------------------------------------------------
	00 : WIDGET Stage Procedure
------------------------------------------------------------------------------*/
static void Process0(GE_MSG* psMsg)
{

}

/*------------------------------------------------------------------------------
	01 : MENU Stage Procedure
------------------------------------------------------------------------------*/
static void Process1(GE_MSG* psMsg)
{
	HTARGET		hTarget = UGxTargetGet(0);
	GE_ID		eMenuID = eWGTID_01_Menu;
	GE_BOOL		bUpdateAll = GE_FALSE;

	switch (psMsg->wMsg)
	{
	case eGEMSG_USEREVENT:
		switch (psMsg->wParam) {
		case eEVTID_KEY_UP:
			/* アクティブアイテムを上に移動 */
			GxWMenuNavigate(eMenuID, eGEDIR_UP);
			GxWidgetRender(hTarget, GE_NULL, eMenuID);
			UGxTargetRedraw(hTarget, GE_NULL);
			break;
		case eEVTID_KEY_DOWN:
			/* アクティブアイテムを下に移動 */
			GxWMenuNavigate(eMenuID, eGEDIR_DOWN);
			GxWidgetRender(hTarget, GE_NULL, eMenuID);
			UGxTargetRedraw(hTarget, GE_NULL);
			break;
		case eEVTID_KEY_LEFT:
		case eEVTID_KEY_RIGHT: {
			GE_INT16	nActiveItem = GxWMenuActiveItemGet(eMenuID);
			GE_INT		nValue;

			switch (nActiveItem) {
			case 0:
				/* コントラスト変更 */
				nValue  = GxWidgetValueGet(eWGTID_01_Nmb_Cont);
				nValue += (psMsg->wParam == eEVTID_KEY_LEFT) ? -1 : +1;
				nValue  = GE_MINMAX(nValue, 0, 100);
				GxWidgetValueSet(eWGTID_01_Nmb_Cont, nValue);
				GxWidgetValueSet(eWGTID_01_Gauge_Cont, nValue);
				break;
			case 1:
				/* 明るさ変更 */
				nValue  = GxWidgetValueGet(eWGTID_01_Nmb_Brite);
				nValue += (psMsg->wParam == eEVTID_KEY_LEFT) ? -1 : +1;
				nValue  = GE_MINMAX(nValue, -50, 50);
				GxWidgetValueSet(eWGTID_01_Nmb_Brite, nValue);
				GxWidgetValueSet(eWGTID_01_Gauge_Brite, nValue);
				break;
			case 3:
				/* 色温度変更 */
				nValue  = GxWidgetValueGet(eWGTID_01_Txt_Clrtmp);
				nValue += (psMsg->wParam == eEVTID_KEY_LEFT) ? -1 : +1;
				nValue  = GE_MINMAX(nValue, 1, 3);
				GxWidgetValueSet(eWGTID_01_Txt_Clrtmp, nValue);
				break;
			case 5:
				/* 言語変更 */
//				nValue  = GxLanguageGet();
				nValue  = GxWidgetValueGet(eWGTID_01_Txt_Language);
				nValue += (psMsg->wParam == eEVTID_KEY_LEFT) ? -1 : +1;
				nValue  = GE_MINMAX(nValue, 1, 3);
//				nValue  = GE_MINMAX(nValue, eLNGID_English, eLNGID_Korea);
				GxWidgetValueSet(eWGTID_01_Txt_Language, nValue);
//				GxLanguageSet((GE_ID)nValue);
				bUpdateAll = GE_TRUE;
				break;
			default:
				return;
			}

			if (bUpdateAll) {
				/* 全画面再描画 */
				GxRenderUpdate(hTarget, GE_FALSE);
			}
			else {
				/* メニューの変更箇所のみ再描画 */
	 			GxWMenuItemFlagSet(eMenuID, nActiveItem, GEWMENU_ITEM_DRAW);
				GxWidgetRender(hTarget, GE_NULL, eMenuID);
			}
			UGxTargetRedraw(hTarget, GE_NULL);

			}
			break;
		}
		break;

#ifdef	_TOUCH_ONLY	//　とりあえずKEYを押すと１つ下のMENUに
		case	eGEMSG_MENUITEM_DOWN:
			GxWMenuNavigate(eMenuID, eGEDIR_DOWN);
			GxWidgetRender(hTarget, GE_NULL, eMenuID);
			UGxTargetRedraw(hTarget, GE_NULL);
			break ;

#endif
		case eGEMSG_BUTTON_CLICK:
			if ( psMsg->wParam == eWGTID_01_ToggleBL ) {
				if (GxWButtonIsChecked(eWGTID_01_ToggleBL)) {
					LCD_SetBackLight(0) ;
				}
				else {
					LCD_SetBackLight(1) ;
				}
			}
			break ;
	}
}

/*------------------------------------------------------------------------------
	02 : BUTTON Stage Procedure
------------------------------------------------------------------------------*/
static void Process2(GE_MSG* psMsg)
{
	HTARGET		hTarget = UGxTargetGet(0);

	if (psMsg->wMsg == eGEMSG_BUTTON_CLICK)
	{
		switch (psMsg->wParam)
		{
		/* 言語ラジオボタンが押されたら表示言語を切換える */
//		case eWGTID_02_RadioA:
//			GxLanguageSet(eLNGID_English);
//			break;
//		case eWGTID_02_RadioB:
//			GxLanguageSet(eLNGID_Japanese);
//			break;

		/* LOCKボタンが押されたら有効／無効を切換える */
		case eWGTID_02_ToggleA:
			if (GxWButtonIsChecked(eWGTID_02_ToggleA)) {
				GxWidgetFlagSet(eWGTID_02_PushB, GEWGT_DISABLE);
				GxWidgetFlagSet(eWGTID_02_ToggleB, GEWGT_DISABLE);
			}
			else {
				GxWidgetFlagClear(eWGTID_02_PushB, GEWGT_DISABLE);
				GxWidgetFlagClear(eWGTID_02_ToggleB, GEWGT_DISABLE);
			}

			break;
		}
		GxRenderUpdate(hTarget, GE_FALSE);
		UGxTargetRedraw(hTarget, GE_NULL);
	}
	else if (psMsg->wMsg == eGEMSG_USEREVENT) {
		switch (psMsg->wParam) {
		case eEVTID_KEY_UP:
			GxActiveWidgetSet(eWGTID_02_PushA);
			break ;
		case eEVTID_KEY_DOWN:
			GxActiveWidgetSet(eWGTID_02_PushB);
			break ;
		case eEVTID_KEY_LEFT:
			GxWButtonCheck(eWGTID_02_ToggleA, !GxWButtonIsChecked(eWGTID_02_ToggleA) );
			GxWButtonCheck(eWGTID_02_ToggleB, !GxWButtonIsChecked(eWGTID_02_ToggleB) );
			break ;
		case eEVTID_KEY_RIGHT:
			if( GxWButtonIsChecked(eWGTID_02_RadioA) )
				GxWButtonCheck(eWGTID_02_RadioB, GE_TRUE);
			else if( GxWButtonIsChecked(eWGTID_02_RadioB) )
				GxWButtonCheck(eWGTID_02_RadioC, GE_TRUE);
			else 
				GxWButtonCheck(eWGTID_02_RadioA, GE_TRUE) ;
			
		}
		GxRenderUpdate(hTarget, GE_FALSE);
		UGxTargetRedraw(hTarget, GE_NULL);
	}

}

/*------------------------------------------------------------------------------
	03 : JPEG File Viewer Stage Procedure
------------------------------------------------------------------------------*/
// jpeg_dec.c のJPEGデコーダを呼ぶ
//extern	int jpegdec_sample( void *vram_addr ) ; // FILEでなくメモリでのテスト
extern	int jpegdec_sample( void *vram_addr, char *fname ) ;

#define	JPGFILE_MAXNUM	32
#define	FNAME_MAX_LEN	256	// LFN MAX

static char JpgFnameList[JPGFILE_MAXNUM][FNAME_MAX_LEN] ;
static int NumJpgFile,CntJpgFile  ;
//#define	JPEGFILE_DIR	""
#define	JPEG_DISP_ADDR	((unsigned int)((TARGET*)hTarget)->pBits + JPEG_YOFFSET)

#ifdef	_WIN32
	// VRAM の横方向改行幅 16bitRGBなのでx2
	#define	JPEG_YOFFSET	((320*2)*30 +30*2 )
	// dsp（プロジェクト）のカレントに下記ファイルを置くこと test用
	#define	JPEG_TESTFILE1	"cat.jpg"
	#define	JPEG_TESTFILE2	"night.jpg"

	// スライドショー更新カウンタ
	#define	SLIDE_TIMER_CNT	1000

#else	// SH LCD向け
	// 現在512で改行　RAM4（Target)に直接描画する場合
	#define	JPEG_YOFFSET	((512*2)*30 +30*2)

	// スライドショー更新カウンタ
	#define	SLIDE_TIMER_CNT	2000

#endif

// File名格納用ワーク
#define	FNAME_TEXT_MAX	16
static GE_TCHAR	m_tcFname[FNAME_TEXT_MAX+1];


static GE_VOID NextStage( void ) ;


static void ascii2unicode( GE_UNICODE *str, char *fname, int len )
{
	int	i ;
	for ( i= 0 ; i < len ; i ++ ) {
		*str++ = *fname++  ;
		if (*fname == '\n') { *str = '\n'; break ; } 
	}
}
/*----------------------------------------------------------------------*/
static void SetFileName( void )
{
	GE_UNICODE	strbuf[FNAME_TEXT_MAX+1] ;

	GxWTextTextSet( eWGTID_03_Fname, m_tcFname );
	ascii2unicode( strbuf, JpgFnameList[CntJpgFile], FNAME_TEXT_MAX ) ;
	GxUnicodeToText2( m_tcFname, strbuf, eFNTID_Mintyou ) ;
	GxWTextTextSet( eWGTID_03_Fname, m_tcFname );
}


/*----------------------------------------------------------------------*/
static void ScanFileList( void )
{
#ifdef	_WIN32
	// DUMMY
	NumJpgFile = 2 ;
	strcpy(JpgFnameList[0], JPEG_TESTFILE1) ;
	strcpy(JpgFnameList[1], JPEG_TESTFILE2) ;
#else
	// ディレクトリのファイル一覧を取得(SH SDカード向け　JPEGだけにすること)
	NumJpgFile = GetFileList( "", (char *)JpgFnameList, FNAME_MAX_LEN, JPGFILE_MAXNUM  ) ;
#endif

	if ( NumJpgFile ==0 ) {
		//　ファイルが無い場合
//		GxWidgetFlagSet(eWGTID_03_PushNext, GEWGT_DISABLE);
//		GxWidgetFlagSet(eWGTID_03_PushPrev, GEWGT_DISABLE);
		GxWTextTextSet(eWGTID_03_Fname, GxRscTextGet(eSTRID_String_NOSD, eLNGID_Japanese));
	}
	else {
//		GxWidgetFlagClear(eWGTID_03_PushNext, GEWGT_DISABLE);
//		GxWidgetFlagClear(eWGTID_03_PushPrev, GEWGT_DISABLE);
		// Filename を入れる
		SetFileName() ;
	}
}

static GE_VOID NextStage3( void )
{
	HTARGET		hTarget = UGxTargetGet(0);

	NextStage();

	ScanFileList() ;

	GxRenderUpdate(hTarget, GE_FALSE);
	UGxTargetRedraw(hTarget, GE_NULL);
	if ( NumJpgFile ) {
		jpegdec_sample( (void *)(JPEG_DISP_ADDR) , JpgFnameList[CntJpgFile=0] ) ;
	}
}

static void Process3(GE_MSG* psMsg)
{
	GE_BOOL		bUpdateAll = GE_FALSE;
//	GE_UNICODE	strbuf[FNAME_TEXT_MAX+1] ;

	HTARGET		hTarget = UGxTargetGet(0);

	if (psMsg->wMsg == eGEMSG_BUTTON_CLICK)
	{
		switch (psMsg->wParam)
		{
		case eWGTID_03_PushNext:
			if ( ++CntJpgFile >= NumJpgFile ) CntJpgFile=0 ;
			bUpdateAll = GE_TRUE ;
			break;
		case eWGTID_03_PushPrev:
			if ( CntJpgFile )  --CntJpgFile  ;
			bUpdateAll = GE_TRUE ;
			break;
		case eWGTID_03_SlideON:
			/* タイマーを起動 */
			if ( GxWButtonIsChecked(eWGTID_03_SlideON) ) {
				GxTimerStart(0, SLIDE_TIMER_CNT );
			}
			else {
				GxTimerStop(0);
			}
			bUpdateAll = GE_TRUE ;
			break ;
		}
	} else if (psMsg->wMsg == eGEMSG_TIMER_UPDATE ) {
		// Timer がUpdateされた
		if (psMsg->wParam == 0) {
			if ( ++CntJpgFile >= NumJpgFile ) CntJpgFile=0 ;
			bUpdateAll = GE_TRUE ;
		}
	}

	if (bUpdateAll) {
		// 挿入したものと考えて、SDカードを読みなおす
		if (NumJpgFile==0) 	ScanFileList() ;
//		ascii2unicode( strbuf, JpgFnameList[CntJpgFile], FNAME_TEXT_MAX ) ;
//		GxUnicodeToText2( m_tcFname, strbuf, eFNTID_Mintyou ) ;
//		GxWTextTextSet( eWGTID_03_Fname, m_tcFname );
		SetFileName() ;
		/* 全画面再描画 */
		GxRenderUpdate(hTarget, GE_FALSE);
		UGxTargetRedraw(hTarget, GE_NULL);

		//IMPress JPEGをVRAMにオーバレイ
		jpegdec_sample( (void *)(JPEG_DISP_ADDR) , JpgFnameList[CntJpgFile] ) ;
		
	}
}



/*------------------------------------------------------------------------------
	04 : LIST & POPUP Stage Procedure
------------------------------------------------------------------------------*/
#ifndef	_WIN32	// No rand()
static rand(void)
{
	static int n= 1;
	return n++ ;
}
#endif

static void Process4(GE_MSG* psMsg)
{
	HTARGET			hTarget = UGxTargetGet(0);
	static GE_BOOL	m_bPopup = GE_FALSE;
	static int	lastItem = 0 ;
	if (!m_bPopup)
	{
		/* 通常状態処理 */
		GE_WGTLIST*		psList;
		GE_INT32		lwValue;
		GE_POINT		sPoint = {40, 30};

		psList = (GE_WGTLIST*)GxRscWidgetGet(eWGTID_04_List);
		switch (psMsg->wMsg)
		{
		case eGEMSG_USEREVENT:
			switch (psMsg->wParam)
			{
			case eEVTID_KEY_UP:
			case eEVTID_KEY_DOWN:
				/* アクティブアイテムを上下に移動 */
				GxWListNavigateEx(psList,
					(psMsg->wParam == eEVTID_KEY_UP) ? 	eGEDIR_UP : eGEDIR_DOWN);
				lwValue = GxWListActiveItemGetEx(psList);
				GxWidgetValueSet(eWGTID_04_Txt_Seiza, lwValue + 1);
				GxWidgetValueSet(eWGTID_04_Pict_Seiza, lwValue + 1);
				break;
			case eEVTID_KEY_OK:
			case eEVTID_KEY_RIGHT:
				/* ポップアップレイヤを表示 */
				GxLayerWkAppend(eLYRID_Layer04Pop, sPoint);
				lwValue = GxWListActiveItemGetEx(psList);
				GxWidgetValueSet(eWGTID_04P_Txt_Seiza, lwValue + 1);
				GxWidgetValueSet(eWGTID_04P_Pict_SeizaA, lwValue + 1);
				GxWidgetValueSet(eWGTID_04P_Txt_Luck, (rand()%4)+1);
				GxLayerWkFlagSet(eLYRID_Layer04, GELYR_DISABLE);
				m_bPopup = GE_TRUE;
				break;
			default:
				return;
			}
			break;
		case eGEMSG_LISTITEM_DOWN:
			/* アクティブアイテムをクリックされたアイテムに移動 */
			lwValue = psMsg->dwParam & 0xFFFF;
#ifdef	_TOUCH_ONLY	// 同じItemを押したら
			if( lwValue == lastItem ) {
								/* ポップアップレイヤを表示 */
				GxLayerWkAppend(eLYRID_Layer04Pop, sPoint);
				lwValue = GxWListActiveItemGetEx(psList);
				GxWidgetValueSet(eWGTID_04P_Txt_Seiza, lwValue + 1);
				GxWidgetValueSet(eWGTID_04P_Pict_SeizaA, lwValue + 1);
				GxWidgetValueSet(eWGTID_04P_Txt_Luck, (rand()%4)+1);
				GxLayerWkFlagSet(eLYRID_Layer04, GELYR_DISABLE);
				m_bPopup = GE_TRUE;
			} else{
				lastItem = lwValue ;
				GxWListActiveItemSetEx(psList, (GE_INT16)lwValue);
				GxWidgetValueSet(eWGTID_04_Txt_Seiza, lwValue + 1);
				GxWidgetValueSet(eWGTID_04_Pict_Seiza, lwValue + 1);
			}
#else
			GxWListActiveItemSetEx(psList, (GE_INT16)lwValue);
			GxWidgetValueSet(eWGTID_04_Txt_Seiza, lwValue + 1);
			GxWidgetValueSet(eWGTID_04_Pict_Seiza, lwValue + 1);
#endif	
			break;
		case eGEMSG_LISTBAR_DOWN:
			lwValue = psMsg->dwParam;
			switch (lwValue) 
			{
			case GE_LISTBAR_LINEUP:
			case GE_LISTBAR_LINEDOWN:
				/* アクティブアイテムを上下に移動 */
				GxWListNavigateEx(psList, 
					(lwValue == GE_LISTBAR_LINEUP) ? eGEDIR_UP : eGEDIR_DOWN);
				lwValue = GxWListActiveItemGetEx(psList);
				GxWidgetValueSet(eWGTID_04_Txt_Seiza, lwValue + 1);
				GxWidgetValueSet(eWGTID_04_Pict_Seiza, lwValue + 1);
				break;
			case GE_LISTBAR_PAGEUP:
			case GE_LISTBAR_PAGEDOWN:
				/* ページを上下に移動 */
				GxWListNavigatePageEx(psList, 
					(lwValue == GE_LISTBAR_PAGEUP) ? eGEDIR_UP : eGEDIR_DOWN);
				lwValue = GxWListActiveItemGetEx(psList);
				GxWidgetValueSet(eWGTID_04_Txt_Seiza, lwValue + 1);
				GxWidgetValueSet(eWGTID_04_Pict_Seiza, lwValue + 1);
				break;
			default:
				break;
			}
			break;
			default:
				return ;
		}
		GxRenderUpdate(hTarget, GE_FALSE);
		UGxTargetRedraw(hTarget, GE_NULL);

	}
	else 
	{
		/* ポップアップ表示状態 */
		switch (psMsg->wMsg)
		{
		case eGEMSG_BUTTON_CLICK:
			if (psMsg->wParam == eWGTID_04P_Btn_Close)
				m_bPopup = GE_FALSE;
			break;
		case eGEMSG_USEREVENT:
			switch (psMsg->wParam)
			{
			case eEVTID_KEY_RIGHT:
			case eEVTID_KEY_LEFT:
			case eEVTID_KEY_OK:
				m_bPopup = GE_FALSE;
				break;
			}
			break;
		default:
			break;
		}

		if (GE_FALSE == m_bPopup)
		{
			GxLayerWkRemove(eLYRID_Layer04Pop);
			GxLayerWkFlagClear(eLYRID_Layer04, GELYR_DISABLE);
			GxRenderUpdate(hTarget, GE_TRUE);
			UGxTargetRedraw(hTarget, GE_NULL);
		}
	}

}

/*------------------------------------------------------------------------------
	07 : FIGURE Stage Procedure
------------------------------------------------------------------------------*/
static void Process7(GE_MSG* psMsg)
{

}

/*------------------------------------------------------------------------------
	08 : FONT Stage Procedure
------------------------------------------------------------------------------*/
static void Process8(GE_MSG* psMsg)
{
	HTARGET		hTarget = UGxTargetGet(0);
	int pushed = 0 ;

	if (psMsg->wMsg == eGEMSG_BUTTON_CLICK)
	{
		if (psMsg->wParam == eWGTID_08_Btn_Font) {
			pushed = 1 ;
		}
	}
	else if (psMsg->wMsg == eGEMSG_USEREVENT) {
		// Keyが押された
		if (psMsg->wParam != eEVTID_KEY_STAGE)		pushed = 1 ;
//		switch (psMsg->wParam) {
//		}
	}
	if ( pushed ) {
			/* ボタンが押されたときに、表示テキストを切り替える */
			GE_INT16 nMulti = GxWTextMultiGet(eWGTID_08_Label);

			GxWTextMultiSet(eWGTID_08_Label, (GE_INT16)(nMulti==1 ? 2 : 1));
			GxWTextStringSet(eWGTID_08_Text, 
							 (GE_ID)(nMulti==1 ? eSTRID_String091 : eSTRID_String090));

			GxRenderUpdate(hTarget, GE_FALSE);
			UGxTargetRedraw(hTarget, GE_NULL);
	}
}

/*==============================================================================
 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PUBLIC PROCEDURE. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
==============================================================================*/

/* -----------------------------------------------------------------------------
	This API initializes user application.
	ユーザアプリケーション初期化処理
------------------------------------------------------------------------------*/

static	GE_USERFIG_ENTRY  dummyEntry ;

GE_VOID UGxAppInitialize()
{
	HTARGET		hTarget = UGxTargetGet(0);

	// 初期化なしRAMがZEROでなく不定の場合、Figureエントリをゼロに初期化すること
	UGxMemset( &dummyEntry, 0, sizeof(dummyEntry));
	GxUserFigureEntry(&dummyEntry) ;

	/* ステージの切換え& 再描画 */
	GxStageChange(hTarget, eSTGID_Stage000, GE_TRUE);

	//IMPress JPEGデコーダテスト
//	jpegdec_sample( (void *)(JPEG_DISP_ADDR) , JPEG_TESTFILE1) ;

//	GxRenderUpdate(hTarget, GE_FALSE);
//	UGxTargetRedraw(hTarget, GE_NULL);
}


/*------------------------------------------------------------------------------
	次ステージに切り替え
------------------------------------------------------------------------------*/
static GE_VOID NextStage( void )
{
	HTARGET		hTarget = UGxTargetGet(0);
	GE_ID		eStageID;
	
	/* 現在のステージを取得 */
	eStageID = GxCurrentStageGet();

	/* 次のステージを選択 */
	switch (eStageID) {
	case eSTGID_Stage000:	eStageID = eSTGID_Stage001;		break;
	case eSTGID_Stage001:	eStageID = eSTGID_Stage002;		break;
	case eSTGID_Stage002:	eStageID = eSTGID_Stage003;		break;
	case eSTGID_Stage003:	eStageID = eSTGID_Stage004;		break;
	case eSTGID_Stage004:	eStageID = eSTGID_Stage007;		break;
	case eSTGID_Stage007:	eStageID = eSTGID_Stage008;		break;
	default:
	case eSTGID_Stage008:	eStageID = eSTGID_Stage000;
	break;
/*	case eSTGID_Stage000:	eStageID = eSTGID_Stage001;		break;
	case eSTGID_Stage001:	eStageID = eSTGID_Stage002;		break;
	case eSTGID_Stage002:	eStageID = eSTGID_Stage003;		break;
	case eSTGID_Stage003:	eStageID = eSTGID_Stage004;		break;
	case eSTGID_Stage004:	eStageID = eSTGID_Stage005;		break;
	case eSTGID_Stage005:	eStageID = eSTGID_Stage006_a;	break;
	case eSTGID_Stage006_a:	eStageID = eSTGID_Stage007;		break;
	case eSTGID_Stage006_b:	eStageID = eSTGID_Stage007;		break;
	case eSTGID_Stage006_c:	eStageID = eSTGID_Stage007;		break;
	case eSTGID_Stage006_d:	eStageID = eSTGID_Stage007;		break;
	case eSTGID_Stage007:	eStageID = eSTGID_Stage008;		break;
	default:
	case eSTGID_Stage008:	eStageID = eSTGID_Stage000;		break;
*/
	}
	/* ステージの切換え& 再描画 */
	GxStageChange(hTarget, eStageID, GE_TRUE);
}


/*------------------------------------------------------------------------------
	This API is periodic user application process.
	ユーザアプリケーションメインループ処理
------------------------------------------------------------------------------*/
GE_BOOL UGxAppProcess(GE_MSG* psMsg)
{
	switch (psMsg->wMsg) {
	/* ステージキーによるステージの切換え */
	case eGEMSG_USEREVENT:
		if (psMsg->wParam == eEVTID_KEY_STAGE)
			NextStage();
		break;

	/* 右矢印ボタンによるステージの切換え */
	case eGEMSG_BUTTON_CLICK:
		switch (psMsg->wParam) {
		case eWGTID_00_NextBtn:		NextStage();	break;
		case eWGTID_01_NextBtn:		NextStage();	break;
		case eWGTID_02_NextBtn:		NextStage3();	break;
		case eWGTID_03_NextBtn:		NextStage();	break;
		case eWGTID_04_NextBtn:		NextStage();	break;
//		case eWGTID_05_NextBtn:		NextStage();	break;
//		case eWGTID_06_NextBtn:		NextStage();	break;
		case eWGTID_07_NextBtn:		NextStage();	break;
		case eWGTID_08_NextBtn:		NextStage();	break;
		}
	}

	/* 各ステージ処理 */
	switch (GxCurrentStageGet())
	{
	case eSTGID_Stage000:		Process0(psMsg);		break;
	case eSTGID_Stage001:		Process1(psMsg);		break;
	case eSTGID_Stage002:		Process2(psMsg);		break;
	case eSTGID_Stage003:		Process3(psMsg);		break;
	case eSTGID_Stage004:		Process4(psMsg);		break;
//	case eSTGID_Stage005:		Process5(psMsg);		break;
//	case eSTGID_Stage006_a:
//	case eSTGID_Stage006_b:
//	case eSTGID_Stage006_c:
//	case eSTGID_Stage006_d:		Process6(psMsg);		break;
	case eSTGID_Stage007:		Process7(psMsg);		break;
	case eSTGID_Stage008:		Process8(psMsg);		break;
	default:
		break;
	}
	return GE_FALSE;
}

/*------------------------------------------------------------------------------
	This API finalizes user application.
	ユーザアプリケーション終了処理
------------------------------------------------------------------------------*/
GE_VOID UGxAppFinalize()
{

}

/*------------------------------------------------------------------------------
	This API is called in entering the stage.
	Stage 開始処理
------------------------------------------------------------------------------*/
GE_VOID UGxStageEnter(GE_ID eStageID)
{
	/* 現在の選択されている言語にあわせて有効なラジオボタンを設定 */
	switch (GxLanguageGet())
	{
//	case eLNGID_English:	GxWButtonCheck(eWGTID_02_RadioA, GE_TRUE);		break;
//	case eLNGID_Japanese:	GxWButtonCheck(eWGTID_02_RadioB, GE_TRUE);		break;
	}
}

/*------------------------------------------------------------------------------
	This API is called in exiting the stage.
	Stage 終了処理
------------------------------------------------------------------------------*/
GE_VOID UGxStageExit(GE_ID eStageID)
{
}

/*------------------------------------------------------------------------------
	This API called before redering the layer.
------------------------------------------------------------------------------*/
GE_BOOL UGxLayerRender(HTARGET hTarget,
					   GE_ID   eLayerID)
{
	return GE_FALSE;
}

/*------------------------------------------------------------------------------
	This API called before redering the widget.
------------------------------------------------------------------------------*/
GE_BOOL UGxWidgetRender(HTARGET  hTarget,
						GE_POINT sOffset,
						GE_ID    eWidgetID)
{
	return GE_FALSE;
}

/*------------------------------------------------------------------------------
	This API called before redering widget button by clicked.
------------------------------------------------------------------------------*/
GE_BOOL UGxButtonClickRender(HTARGET hTarget,
							 GE_ID   eWBtnID,
							 GE_BOOL bPushed)
{
	return GE_FALSE;	
}
