//-------------------------------------------------------------------------------
//
//	FILE: HandleEvent.c
//
//	IMenuCtl interfaceのサブプログラム 
//
//-------------------------------------------------------------------------------

#include "MenuCtl.h"
#include "main_res.h"
#include "menu_res.h"


//	myHandleEvent :: アプレットのイベントハンドラ
boolean myHandleEvent( IApplet *pi, AEEEvent eCode, uint16 wParam, uint32 dwParam )
{
	MyApp			*pApp = (MyApp*)pi;
	boolean			bRet;
	uint32			unProp;
	
	// 初期化
	bRet = FALSE;
	
	// NULLチェック
	if ( pApp == NULL )
	{
		DBGPRINTF( "pApp is NULL" );
		return bRet;
	}
	
	// 各コントロールのイベント処理
	// IMENUCTL
	if ( pApp->pimc && test_HandleEvent( pApp, eCode, wParam, dwParam ) )
	{
		bRet = TRUE;
		return bRet;
	}
	// IHTMLVIEWER
	if ( pApp->m_pihv && IHTMLVIEWER_HandleEvent(pApp->m_pihv, eCode, wParam, dwParam) )
	{
		bRet = TRUE;
		return bRet;
	}
	
	//	メインイベント処理系
	switch ( eCode )
	{
	case EVT_APP_START:										// アプリ開始
		MainHtml( pApp );								
		bRet = TRUE;
		break;
	case EVT_APP_STOP:										// アプリ終了
		myAppCleanup( pApp );
		bRet = TRUE;
		break;
	case EVT_CTL_TAB:										// タブイベント
		test_GetProperties( pApp, &unProp );
		if ( unProp == MP_MULTI_SEL )
		{
			bRet = TRUE;
		}
		break;
	case EVT_KEY:											// キーイベント処理
		bRet = on_KeyWork( pApp, wParam, dwParam );
		break;
	case EVT_COMMAND:										// コマンド受信
		bRet = on_Command( pApp, wParam, dwParam );
		break;
	default:
		break;
	}
	return bRet;
}

boolean on_KeyWork( MyApp *pApp, uint16 wParam, uint32 dwParam )
{
	AEERect			adummy;
	boolean			bRet;
	AEEDeviceInfo	di;
	
	// 初期化
	bRet = FALSE;
	
	switch ( wParam )
	{
	case AVK_CLR:
		if ( pApp->pimc != NULL && IMENUCTL_IsActive( pApp->pimc ) == TRUE )
		{
			SETAEERECT( &adummy, 0, 0, 0, 0 );
			// IMENUCTL_SetRect()
			test_SetRect( pApp, &adummy );
			// メニュー画面非アクティブ化
			test_SetActive( pApp, FALSE );
		}
		
		if ( pApp->m_pihv != NULL && IHTMLVIEWER_IsActive(pApp->m_pihv) == FALSE )
		{
			ISHELL_GetDeviceInfo( pApp->app.m_pIShell, &di );
			SETAEERECT( &pApp->arect, 0, 0, di.cxScreen, di.cyScreen );
			IHTMLVIEWER_SetRect( pApp->m_pihv, &pApp->arect );
			IHTMLVIEWER_SetActive( pApp->m_pihv, TRUE );
		}
		
		IDISPLAY_ClearScreen( pApp->app.m_pIDisplay );
		
		if ( pApp->bMyPage == TRUE )
		{
			MainHtml( pApp );
		}
		else
		{
			ShowMainMenu( pApp );
		}
		
		IHTMLVIEWER_SetNotifyFn( pApp->m_pihv, (PFNHVIEWNOTIFY)HtmlNotify_MENU, pApp );
		bRet = TRUE;
		break;
	case AVK_POUND:
		if ( pApp->apl_state == LOG_ALL_DISP )
		{
			//Log表示次のページへ
			LogDispChange( pApp, NEXT );
		}
		else
		{
			pApp->ncon = CON_DEFAULT;
			if ( pApp->pimc != NULL )
			{
				test_Release( pApp, pApp->pimc );
			}
			if ( TRUE != CreateMenuCtl(pApp) )
			{
				DBGPRINTF( "CreateMenuCtl ERROR" );
			}
		}
		bRet = TRUE;
		break;
	case AVK_STAR:
		if ( pApp->apl_state == LOG_ALL_DISP )
		{
			//Log表示前のページへ
			LogDispChange( pApp, BEFORE );
		}
		else
		{
			switch( pApp->apl_state )
			{
			case DRW_ITEM1:
				SetItem_Test2(pApp);
				MoveState( pApp, DRW_ITEM2 );
				break;
			case DRW_ITEM2:
				SetItem_Test3(pApp);
				MoveState( pApp, DRW_ITEM3 );
				break;
			case DRW_ITEM3:
				SetItem_Test4(pApp);
				MoveState( pApp, DRW_ITEM4 );
				break;
			case DRW_ITEM4:
				SetItem_Test5(pApp);
				MoveState( pApp, DRW_ITEM5 );
				break;
			case DRW_ITEM5:
				SetItem_Test6(pApp);
				MoveState( pApp, DRW_ITEM6 );
				break;
			case DRW_ITEM6:
				SetItem_Test7(pApp);
				MoveState( pApp, DRW_ITEM7 );
				break;
			case DRW_DELI:
				SetDelItem_Test2( pApp );
				MoveState( pApp, DRW_DELI1 );
				break;
			case DRW_DELA:
				SetDelAll_Test2( pApp );
				MoveState( pApp, DRW_DELA1 );
				break;
			case DRW_SETS1:
				pApp->ncon = CON_DEFAULT;
				SetSel_Test2( pApp );
				MoveState( pApp, DRW_SETS2 );
				break;
			case DRW_MOVE1:
				Move_Test1( pApp, TRUE );
				MoveState( pApp, DRW_MOVE2 );
				break;
			case DRW_MOVE2:
				Move_Test1( pApp, FALSE );
				MoveState( pApp, DRW_MOVE3 );
				break;
			case DRW_MOVE3:
				Move_Test1( pApp, FALSE );
				MoveState( pApp, DRW_MOVE4 );
				break;
			case DRW_MOVE4:
				Move_Test1( pApp, TRUE );
				MoveState( pApp, DRW_MOVE5 );
				break;
			case DRW_SETC1:
				STRCAT( pApp->m_szHtmlBufWk, pApp->m_szHtmlBuf );
				SetColors_Test( pApp );
				STRCAT( pApp->m_szHtmlBufWk, pApp->m_szHtmlBuf );
				STRCPY( pApp->m_szHtmlBuf, pApp->m_szHtmlBufWk );
				MoveState( pApp, DRW_SETC2 );
				break;
			case DRW_RESET1:
				Reset_Test( pApp );
				MoveState( pApp, DRW_RESET2 );
				break;
			case DRW_END:
				CreateMenuCtl(pApp);
				MoveState( pApp, MENU_CTL );
				break;
			case DRW_SELEX1:
				test_SetSelEx1( pApp, (uint16)-1 );
				MoveState( pApp, DRW_SELEX2 );
				break;
			case DRW_SELEX2:
				test_SetSelEx2( pApp, (uint16)-1 );
				MoveState( pApp, DRW_SELEX3 );
				break;
			case DRW_ITEM7:
			case DRW_DELI1:
			case DRW_DELA1:
			case DRW_PRO:
			case DRW_ENA:
			case DRW_SETS2:
			case DRW_MOVE5:
			case DRW_SETC2:
			case DRW_SORT:
			case DRW_RESET2:
			case DRW_SELEX3:
				// メニュー画面非アクティブ化
				test_SetActive( pApp, FALSE );
				
				LogDispUpdate(pApp);
				
				MoveState( pApp, DRW_END );
				break;
			default:
				break;
			}
		}
		bRet = TRUE;
		break;
	default:
		break;
	}
	
	return bRet;
}

boolean on_Command( MyApp *pApp, uint16 wParam, uint32 dwParam )
{
	boolean		bRet;
	
	// 初期化
	bRet = FALSE;
	
	switch ( wParam )
	{
	// メインメニュー
	case 0:
		SetItem_Test1( pApp );
		MoveState( pApp, DRW_ITEM1 );
		bRet = TRUE;
		break;
	case 1:
		Property_Menu( pApp );
		MoveState( pApp, MENU_CTL_PRO );
		break;
	case 2:
		SetDelItem_Test1( pApp );
		MoveState( pApp, DRW_DELI );
		bRet = TRUE;
		break;
	case 3:
		SetDelAll_Test1( pApp );
		MoveState( pApp, DRW_DELA );
		bRet = TRUE;
		break;
	case 4:
		pApp->ncon = CON_1;
		Con_Menu( pApp );
		MoveState( pApp, MENU_CTL_YN );
		break;
	case 5:
		Test_Menu( pApp, (uint32)MP_MULTI_SEL );
		MoveState( pApp, DRW_SELEX1 );
		break;
	case 6:
		test_Enum( pApp );
		Move_Test( pApp );
		MoveState( pApp, DRW_MOVE1 );
		bRet = TRUE;
		break;
	case 7:
		GetCDI_Test( pApp );
		// メニュー画面非アクティブ化
		test_SetActive( pApp, FALSE );
		LogDispUpdate( pApp );
		MoveState( pApp, DRW_END );
		bRet = TRUE;
		break;
	case 8:
		MEMSET( pApp->m_szHtmlBufWk, NULL, BUFSIZE_HTML );
		SetColors_Test( pApp );
		MoveState( pApp, DRW_SETC1 );
		bRet = TRUE;
		break;
	case 9:
		Sort_Menu( pApp );
		MoveState( pApp, MENU_CTL_SORT );
		bRet = TRUE;
		break;
	case 10:
		Test_Menu( pApp, 0 );
		SortCustom_Test( pApp );
		MoveState( pApp, DRW_SORT );
		bRet = TRUE;
		break;
	case 11:
		pApp->ncon = CON_2;
		Con_Menu( pApp );
		MoveState( pApp, MENU_CTL_YN );
		break;
	case 12:
		SetSel_Test1( pApp );
		MoveState( pApp, DRW_SETS1 );
		break;
	case 13:
		Test_Menu(pApp, 0);
		MoveState( pApp, DRW_RESET1 );
		bRet = TRUE;
		break;
	
	// ソート
	case 70:
		Sort_Test( pApp, MCS_CUSTOM );
		MoveState( pApp, DRW_SORT );
		bRet = TRUE;
		break;
	case 71:
		Sort_Test( pApp, MCS_NAME_DESCENDING );
		MoveState( pApp, DRW_SORT );
		bRet = TRUE;
		break;
	case 72:
		Sort_Test( pApp, MCS_NAME_ASCENDING );
		MoveState( pApp, DRW_SORT );
		bRet = TRUE;
		break;
	case 73:
// chg sta  05.05.04 N.Kanagawa #003
//		Sort_Test( pApp, MCS_PARAM_ASCENDING );
		Sort_Test( pApp, MCS_PARAM_DESCENDING );
// chg end  05.05.04 N.Kanagawa #003
		MoveState( pApp, DRW_SORT );
		bRet = TRUE;
		break;
	case 74:
// chg sta  05.05.04 N.Kanagawa #003
//		Sort_Test( pApp, MCS_PARAM_DESCENDING );
		Sort_Test( pApp, MCS_PARAM_ASCENDING );
// chg end  05.05.04 N.Kanagawa #003
		MoveState( pApp, DRW_SORT );
		bRet = TRUE;
		break;
	
	// TRUE or FALSE
	case 80:
		switch ( pApp->ncon )
		{
		case CON_1:
			EnableCommand_Test( pApp, TRUE );
			MoveState( pApp, DRW_ENA );
			bRet = TRUE;
			break;
		case CON_2:
			Active_TEST( pApp, TRUE );
			MoveState( pApp, DRW_ENA );
			bRet = TRUE;
			break;
		default:
			break;
		}
		break;
	case 81:
		switch ( pApp->ncon )
		{
		case CON_1:
			EnableCommand_Test( pApp, FALSE );
			MoveState( pApp, DRW_ENA );
			bRet = TRUE;
			break;
		case CON_2:
			Active_TEST( pApp, FALSE );
			MoveState( pApp, DRW_ENA );
			bRet = TRUE;
			break;
		default:
			break;
		}
		break;
	
	// Properties
	case 90:
		Test_Menu( pApp, MP_WRAPSCROLL );
		
		// IMENUCTL_Redraw()の試験
		test_Redraw( pApp );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 91:
		Test_Menu( pApp, MP_NO_ARROWS );
		Disp_NoArraws( pApp );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 92:
		Test_Menu( pApp, MP_NO_REDRAW );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 93:
		Test_Menu( pApp, MP_ICON_TEXT_TOP );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 94:
		Test_Menu( pApp, MP_ICON_SINGLE_FRAME );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 95:
		Test_Menu( pApp, MP_UNDERLINE_TITLE );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 96:
// chg sta  05.05.26 t.morishita #004
#if 0
//		Test_Menu( pApp, MP_ICON_DELAY_IMAGE );
#else
		Test_Menu( pApp, MP_ICON_SINGLE_FRAME | MP_ICON_DELAY_IMAGE );
#endif
// chg end  05.05.26 t.morishita #004
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 97:
		Test_Menu( pApp, MP_ICON_ANIMATED );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 98:
		Test_Menu( pApp, MP_ICON_NO_CENTERSEL );
		SetProp_Test_NoCenter( pApp );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 99:
		Calendar_Menu( pApp, MP_CALENDAR );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 100:
		Calendar_Menu( pApp, MP_AUTOSCROLLTIME );
		MoveState( pApp, DRW_PRO );
		bRet = TRUE;
		break;
	case 104:
		Test_Menu( pApp, MP_MULTI_SEL );
		MoveState( pApp, DRW_PRO );
		break;
	case 105:
		Test_Menu( pApp, 0xFFFFFFFF );
		MoveState( pApp, DRW_PRO );
		break;
	default:
		break;
	}
	
	return bRet;
}