/***********************************************
**
**Frame.c
**
************************************************/

#define MWINCLUDECOLORS

#include "nano-X.h"

#include <stdio.h>

#include <pthread.h>

#include <fcntl.h>

#include <sys/ioctl.h>

#include <stdlib.h>

#define SCREEN_WIDTH 600
#define SCREEN_HEIGHT 800

#define Asc_Top_Key  63490
#define Asc_Bottom_Key  63491
#define Asc_Right_Key  63489
#define Asc_Left_Key  63488
#define Asc_Enter_Key  48

#define Asc_F3_Key  50

#define CM_SW_OFF	110
#define CM_PWR		112

typedef struct _DisBmpInfo		//
{
	char FileName[40];
	GR_COORD x;
	GR_COORD y;
	GR_SIZE	width;
	GR_SIZE height;
}DisBmpInfo,*pDisBmpInfo;

typedef struct _DisFrameInfo		//frame struct
{
	GR_COORD x;
	GR_COORD y;
	GR_SIZE width;
	GR_SIZE height;
}DisFrameInfo,*pDisFrameInfo;

enum FrameType {Main_Frame,Child_Frame};	// type

GR_WINDOW_ID wid;

GR_GC_ID gc;

static int gLastIndex = 0;

enum FrameType gframetype = Main_Frame;


/***********************************************************
		function :DisplayPart
*============================================================
**	Description:Display the part which has changed
**	param :
**	picon : 
**		pframe:
**		LastIndex:Last Select
**		NewIndex:
**	return  0
****************************************************************/
int DisplayPart(pDisFrameInfo pframe,int LastIndex,int NowIndex)
{

	GrSetGCBackground(gc,WHITE);
	GrSetGCForeground(gc,WHITE);
	
	if((LastIndex >= 0 )&& (LastIndex <= 1)){
		
		GrRect(wid,gc,pframe[LastIndex].x,pframe[LastIndex].y,pframe[LastIndex].width,pframe[LastIndex].height);
		GrRect(wid,gc,pframe[LastIndex].x+1,pframe[LastIndex].y+1,pframe[LastIndex].width-2,pframe[LastIndex].height-2);
	}
	
	GrSetGCBackground(gc,WHITE);
	GrSetGCForeground(gc,BLACK);

	if((NowIndex >= 0)&&(NowIndex<=1)){
		GrRect(wid,gc,pframe[NowIndex].x,pframe[NowIndex].y,pframe[NowIndex].width,pframe[NowIndex].height);
		GrRect(wid,gc,pframe[NowIndex].x+1,pframe[NowIndex].y+1,pframe[NowIndex].width-2,pframe[NowIndex].height-2);
	}
	GrSetGCForeground(gc,BLACK);
		
	return 0;
}

/***********************************************************
		function :DisplayAll
*============================================================
**	Description:Show the Screen
**	param :	
		picon : struct _DisBmpInfo
		iconcount :
		ptitle : struct _DisBmpInfo
		titlecount : 
**	return  0
****************************************************************/

int DisplayAll(pDisBmpInfo picon,int iconcount,pDisBmpInfo ptitle,int titlecount)
{	
	int loop = 0;
	
	if(picon){
		for(loop = 0;loop < iconcount;loop++){
			GrDrawImageFromFile(wid,gc,picon[loop].x,picon[loop].y,picon[loop].width,picon[loop].height,picon[loop].FileName,0);
		}
	}
	if(ptitle){
		for(loop = 0;loop < titlecount;loop++){
			GrDrawImageFromFile(wid,gc,ptitle[loop].x,ptitle[loop].y,ptitle[loop].width,ptitle[loop].height,ptitle[loop].FileName,0);
		}
	}
	return 0;
}
/***********************************************************
		function :DisplayMainMenu
*============================================================
**	Description:Display the main Frame
**	param :
		LastIndex :last select index
		NowIndex:new selet
		Select :
**	return  0
****************************************************************/

int DisplayMainMenu(int LastIndex,int NowIndex,int Select)
{
	DisBmpInfo Icon[2] = {
		{"/pic/checka.bmp",30,350,265,85},
		{"/pic/checkb.bmp",30,440,265,85}
		
	};
	
/*	DisBmpInfo Title[2] = {
		
	};
*/	
	DisFrameInfo Frame[2] = {
		{28,348,270,90},
		{28,438,270,90}
	};
	

	
	if(LastIndex == NowIndex){
		GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
		DisplayAll(Icon,2,/*Title*/NULL,0);
		DisplayPart(Frame,-1,Select);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	}
	
	else{
		DisplayPart(Frame,LastIndex,NowIndex);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	}
	return 0; 	
}

/***********************************************************
		function :DisplayChildFrame
*============================================================
**	Description:Show Child frame
**	param :
		LastIndex :last select index
		NowIndex:new selet
		Select :
**	return  0
****************************************************************/

int DisplayChildFrame(int LastIndex,int NowIndex,int Select)
{
	DisBmpInfo Icon[2] = {
		{"/pic/cchecka.bmp",30,350,265,85},
		{"/pic/ccheckb.bmp",30,440,265,85}
		
	};
	
/*	DisBmpInfo Title[2] = {
		
	};
*/	
	DisFrameInfo Frame[2] = {
		{28,348,270,90},
		{28,438,270,90}
	};
	
	
	if(LastIndex == NowIndex){
		GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
		DisplayAll(Icon,2,NULL,0);
		DisplayPart(Frame,-1,Select);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	}
	
	else{
		DisplayPart(Frame,LastIndex,NowIndex);
		GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	}
	return 0;
}

/***********************************************************
		function :KeyMainFrame
*============================================================
**	Description:key function in main frame
**	param :key value
**	return  0
****************************************************************/

int KeyMainFrame(int key)
{
	int NowIndex = gLastIndex;

	switch(key)
	{
		case Asc_Top_Key:
		case Asc_Right_Key:
			if(NowIndex > 0) NowIndex--;
			else NowIndex = 1;
			DisplayMainMenu(gLastIndex,NowIndex,-1);
		break;
		
		case Asc_Left_Key:
		case Asc_Bottom_Key:
			if(NowIndex < 1) NowIndex++;
			else NowIndex = 0;
			DisplayMainMenu(gLastIndex,NowIndex,-1);
		break;
		
		case Asc_Enter_Key:
			if(gLastIndex == 1){
				gframetype = Child_Frame;
				gLastIndex = 0;
				DisplayChildFrame(0,0,gLastIndex);
			}
			else;
			return 0;
		break;
		
		default:	
		break;	
	}
	gLastIndex = NowIndex;	
	return 0;
}


/***********************************************************
		function :KeyChildFrame
*============================================================
**	Description:key function in child frame
**	param :key value
**	return  0
****************************************************************/
int KeyChildFrame(int key)
{
	int NowIndex = gLastIndex;
	switch(key)
	{
		case Asc_Top_Key:
		case Asc_Right_Key:
			if(NowIndex > 0) NowIndex--;
			else NowIndex = 1;
			DisplayChildFrame(gLastIndex,NowIndex,-1);
		break;
		
		case Asc_Left_Key:
		case Asc_Bottom_Key:
			if(NowIndex < 1) NowIndex++;
			else NowIndex = 0;
			DisplayChildFrame(gLastIndex,NowIndex,-1);
		break;
		
		case Asc_F2_Key:
			gframetype = Main_Frame;
			gLastIndex = 1;
			DisplayMainMenu(0,0,gLastIndex);
			return 0;

		break;
		
		default:
		break;
	}
	gLastIndex = NowIndex;
	return 0;	
}
/*************************************************************************
**			Function:event_handler
**=========================================================================
**
**	Description:responding the event which select by GrSelectEvents()
**	param : event
**	return : 0
****************************************************************************/
int event_header(GR_EVENT *event)
{
	switch(event->type)
	{	
		case GR_EVENT_TYPE_EXPOSURE:
					DisplayMainMenu(0,0,gLastIndex);
		break;
		
		case GR_EVENT_TYPE_CLOSE_REQ:
					GrDestroyWindow(wid);
					GrDestroyGC(gc);
					GrClose();
		break;
		
		case GR_EVENT_TYPE_KEY_DOWN:
		
			switch(gframetype){
				
				case Main_Frame:		//in the Main Frame
					KeyMainFrame(event->keystroke.ch);
				break;
				
				case Child_Frame:		//in the Child frame
					KeyChildFrame(event->keystroke.ch);
				break;
			
				default:
				break;	
			}	
		default:
		break;
	}		
	return 0;
}

int main(void)
{
	GR_EVENT event;
	
	if(GrOpen()<0){
		fprintf(stderr,"GrOpen failed");
		return 1;
	}
	
	wid = GrNewWindowEx(GR_WM_PROPS_APPWINDOW,NULL,GR_ROOT_WINDOW_ID, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,WHITE);
	GrSelectEvents(wid, GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_EXPOSURE|GR_EVENT_MASK_KEY_DOWN);
	GrMapWindow(wid);
	gc = GrNewGC();
	
	GrSetGCUseBackground(gc,GR_TRUE);
	GrSetGCForeground(gc,BLACK);
	GrSetGCBackground(gc,WHITE); 
	
	
	while(1)
	{
		GrCheckNextEvent(&event);
		event_header(&event);
	}
	return 0;
}

