/*********************Font Demos***************/

#define MWINCLUDECOLORS

#include "nano-X.h"
#include <stdio.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

GR_WINDOW_ID wid;
GR_GC_ID gc;
GR_FONT_ID font;

int fontsize = 24;

/*************************************************************************
**			Function:ShowFont
**=========================================================================
**
**	Description:Display Font
**	param : 1:magnify 0:reduce
**	return : 0
****************************************************************************/

int ShowFont(int Expand)
{
	int loop = 0;
	
	if(Expand == 1){
		if(fontsize <40)
			fontsize += 4;
		else fontsize = 24;
	}
	else if(Expand == 0){
		if(fontsize >24)
			fontsize -= 4;
		else fontsize = 40;
	}
	else if(Expand == -1) fontsize = 24;
	
	GrSetFontSize(font,fontsize);
	GrSetGCFont(gc,font);
	GrSetGCForeground(gc,BLACK);
	GrSetGCBackground(gc,WHITE);
	GrClearArea(wid,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,0);
	
	printf("font size = %d\n",fontsize);
	
	for(loop = 0;loop <= 5;loop++){
		GrText(wid, gc,10,10+(loop*fontsize),"Font Demos", -1, GR_TFASCII);
	}
	
	GrShowWindow(wid,gc,0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
	return 0;
}


/*************************************************************************
**			Function:event_handler
**=========================================================================
**
**	Description:responding the event which select by GrSelectEvents()
**	param :event
**	return : 0
****************************************************************************/
int event_handler(GR_EVENT *event)
{
	switch(event->type)
	{
		case GR_EVENT_TYPE_EXPOSURE:
			ShowFont(-1);
		break;
		
		case GR_EVENT_TYPE_CLOSE_REQ:
			printf("close ok.\n");
			GrDestroyFont(font);
			GrDestroyWindow(wid);
			GrDestroyGC(gc);
			GrClose();
			exit (0);
		break;
		
		case GR_EVENT_TYPE_KEY_DOWN:
		switch(event->keystroke.ch){
			case Asc_Top_Key:			//Top Key
			case Asc_Right_Key:			//Right Key
				ShowFont(1);
			break;
			
			case Asc_Bottom_Key:			//Bottom Key
			case Asc_Left_Key:			//Left Key
				ShowFont(0);
			break;
			
			default:
			break;
		}
		break;
		
		default:
		break;
	}
}

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();
	font = GrCreateFont("mfont.mbf",0, NULL);
	GrSetFontSize(font,fontsize);
	GrSetGCFont(gc,font);
	GrSetGCUseBackground(gc, GR_TRUE);
	GrSetGCForeground(gc,BLACK);
	GrSetGCBackground(gc,WHITE);
	while(1){
		GrCheckNextEvent(&event);
		event_handler(&event);
	}	
	return 0;
}
