www.pudn.com > microwindows.example10.rar > PDAgui.c


 /****************************************************
 **PDA_GUI demo by zhang kaohua 2004,5,20 kaohua@126.com**
 *****************************************************/
#include 
#include 
#include 
#include "touchscreen_s3c44b0_ads7843.h"
//#include "pthread.h"
//#include 
#if UNIX | DOS_DJGPP

#endif
#define MWINCLUDECOLORS
#include "nano-X.h"

#define WIDTH 		320
#define HEIGHT 		215

/**********************************************************/
  GR_WINDOW_ID window;
  GR_WINDOW_ID bt1,bt2,bt3;
  GR_WINDOW_ID smb[10];
  GR_WINDOW_ID scb[10];
  //GR_WINDOW_ID startMenuWin;
	GR_GC_ID gc;
  GR_FONT_ID fontid;
  GR_IMAGE_ID picID;
  GR_IMAGE_INFO picIF;
  GR_EVENT event;
  
  struct DATE_TIME
  {
   char year[10];
   char mon[10];
   char day[10];
   char hour[10];
   char min[10];
   char sec[10];
   char week[10];
   char date[50];
   char dateTime[50];
  };
  
  char *fileName[10]={"退  出","记事本","名片","通讯录","画板","游戏","闹钟","音乐","file8","file9"};
  struct DATE_TIME datetime;
  static int ret;
/**********************************************************/
void drawMainWin(GR_GC_ID gc);
void drawBar(GR_GC_ID gc);
void setBackGround(char *picfile,GR_WINDOW_ID win);
void doTime(GR_GC_ID gc);
void doStartMenu(int fileNnum,GR_GC_ID gc);
void doShortCut(char *fileName,char *picfile,int fileNum,GR_GC_ID gc,int x,int y);
void doEventButtonDown(GR_EVENT_BUTTON *bevent,GR_GC_ID gc);
void doEventButtonUp(GR_EVENT_BUTTON *bevent);
void doEventMouseExit(GR_EVENT_MOUSE *mevent,GR_GC_ID gc);
void doEventMouseEnter(GR_EVENT_MOUSE *mevent,GR_GC_ID gc);
void highLightStartMenu(int i);
/**********************************************************/
int main(int ac, char **av)
{
	//pthread_t th_com;  
	if (GrOpen() < 0)
		exit(1);

  gc = GrNewGC();
	GrSetGCUseBackground(gc, GR_FALSE);
	GrSetGCBackground(gc, BLACK);
  GrSetGCForeground(gc, 255);
  fontid = GrCreateFont("HZKFONT", 0, NULL);
  GrSetFontSize(fontid, 16);
  GrSetFontRotation(fontid, 330);         /* 33 degrees */
  GrSetFontAttr(fontid, GR_TFKERNING | GR_TFANTIALIAS, 0);
  GrSetGCFont(gc, fontid);
  drawMainWin(gc);
  drawBar(gc);

  //pthread_create(&th_com, NULL, doTime(gc), 0);
while (1)
  {
    doTime(gc);
    
    GrCheckNextEvent(&event);
		switch(event.type)
    {
      case GR_EVENT_TYPE_CLOSE_REQ:
           GrClose();
			      exit(0);
      case GR_EVENT_TYPE_BUTTON_DOWN:
          doEventButtonDown(&event.button,gc);
          break;
      case GR_EVENT_TYPE_BUTTON_UP:
          doEventButtonUp(&event.button);
          break;
      case GR_EVENT_TYPE_MOUSE_ENTER:
          doEventMouseEnter(&event.mouse,gc);
          break;
      case GR_EVENT_TYPE_MOUSE_EXIT:
          doEventMouseExit(&event.mouse,gc);
          break;
      //case GR_EVENT_TYPE_TIMEOUT:
    }
		
		//GrDestroyFont(fontid);
	}
  GrFlush();
  GrDestroyFont(fontid);
	GrClose();
	return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void drawMainWin(GR_GC_ID gc)
{
  //window = GrNewWindowEx(GR_WM_PROPS_APPWINDOW,
	//	"fonts demo",
	//	GR_ROOT_WINDOW_ID, 0, 0, WIDTH, HEIGHT, BLACK);
  window = GrNewWindow(GR_ROOT_WINDOW_ID, 0, 0, WIDTH, HEIGHT-2,1,BLACK,BLUE);
	GrSelectEvents(window,
		GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_CLOSE_REQ |
           GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_BUTTON_UP |
           GR_EVENT_MASK_MOUSE_ENTER | GR_EVENT_MASK_MOUSE_EXIT);
	GrMapWindow(window);
  setBackGround("./image/car8.bmp",window);
  doShortCut(fileName[1],"./image/jsb.jpg",0,gc,20,20);
  doShortCut(fileName[2],"./image/js.jpg",1,gc,20,80);
  doShortCut(fileName[3],"./image/j1.jpg",2,gc,20,140);
  doShortCut(fileName[4],"./image/hb.jpg",3,gc,80,20);
  //doShortCut(fileName[5],"./image/j2.jpg",4,gc,80,80);
  //doShortCut(fileName[6],"./image/wj2.jpg",5,gc,80,140);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
void drawBar(GR_GC_ID gc)
{
  bt1=GrNewWindow(GR_ROOT_WINDOW_ID,0,215,70,23,2,GREEN,BLUE);
  bt2=GrNewWindow(GR_ROOT_WINDOW_ID,70,215,180,23,2,GREEN,BLUE);
  bt3=GrNewWindow(GR_ROOT_WINDOW_ID,250,215,70,23,2,GREEN,BLUE);
  GrSelectEvents(bt1,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN);
  GrSelectEvents(bt2,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN);
  GrSelectEvents(bt3,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN);
  GrMapWindow(bt1);
  GrMapWindow(bt2);
  GrMapWindow(bt3);

  GrSetGCForeground(gc, WHITE);
  GrSetGCUseBackground(gc, GR_FALSE);
	GrSetGCFont(gc, fontid);

  GrText(bt1, gc, 13, 15,
           "开 始", -1, GR_TFASCII);
  GrText(bt2, gc,1, 15,
           "博创PDAwww.up-tech.com", -1, GR_TFASCII);
     
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void setBackGround(char *picfile,GR_WINDOW_ID win)
{
   if(!(picID = GrLoadImageFromFile(picfile, 0)))
        {
		      fprintf(stderr, "Failed to load image file \"%s\"\n", picfile);
          //return 0;
	      }
  GrGetImageInfo(picID, &picIF);
  GrDrawImageToFit(win, gc, 0, 0, picIF.width, picIF.height, picID);
  GrFreeImage(picID);
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void doStartMenu(int fileNum,GR_GC_ID gc)
{
  int i;
  char* startMenuFileName[fileNum+1];
  int smbW,smbH;
  smbW=100; smbH=20;

  GrSetGCForeground(gc, 255);
  GrSetGCUseBackground(gc, GR_FALSE);
	GrSetGCFont(gc, fontid);
  for(i=0;iwid==smb[0])
       {
         GrText(smb[0],gc,13,15,fileName[0],-1,GR_FALSE);
       }
  if(mevent->wid==smb[1])
       {
         GrText(smb[1],gc,13,15,fileName[1],-1,GR_FALSE);
       }
  if(mevent->wid==smb[2])
       {
         GrText(smb[2],gc,13,15,fileName[2],-1,GR_FALSE);
       }
  if(mevent->wid==smb[3])
       {
         GrText(smb[3],gc,13,15,fileName[3],-1,GR_FALSE);
       }
  if(mevent->wid==smb[4])
       {
         GrText(smb[4],gc,13,15,fileName[4],-1,GR_FALSE);
       }
  if(mevent->wid==smb[5])
       {
         GrText(smb[5],gc,13,15,fileName[5],-1,GR_FALSE);
       }
  if(mevent->wid==smb[6])
       {
         GrText(smb[6],gc,13,15,fileName[6],-1,GR_FALSE);
       }
  if(mevent->wid==smb[7])
       {
         GrText(smb[7],gc,13,15,fileName[7],-1,GR_FALSE);
       } 
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
void doEventMouseExit(GR_EVENT_MOUSE *mevent,GR_GC_ID gc)
{
  GrSetGCFont(gc, fontid);
  GrSetGCForeground(gc, 255);
  if(mevent->wid==smb[0])
       {
         GrText(smb[0],gc,13,15,fileName[0],-1,GR_FALSE);
       }
  if(mevent->wid==smb[1])
       {
         GrText(smb[1],gc,13,15,fileName[1],-1,GR_FALSE);
       }
  if(mevent->wid==smb[2])
       {
         GrText(smb[2],gc,13,15,fileName[2],-1,GR_FALSE);
       }
  if(mevent->wid==smb[3])
       {
         GrText(smb[3],gc,13,15,fileName[3],-1,GR_FALSE);
       }
  if(mevent->wid==smb[4])
       {
         GrText(smb[4],gc,13,15,fileName[4],-1,GR_FALSE);
       }
  if(mevent->wid==smb[5])
       {
         GrText(smb[5],gc,13,15,fileName[5],-1,GR_FALSE);
       }
  if(mevent->wid==smb[6])
       {
         GrText(smb[6],gc,13,15,fileName[6],-1,GR_FALSE);
       }
  if(mevent->wid==smb[7])
       {
         GrText(smb[7],gc,13,15,fileName[7],-1,GR_FALSE);
       }
  
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
void doEventButtonDown(GR_EVENT_BUTTON *bevent,GR_GC_ID gc)
{
  if(bevent->wid==window)
      {
        GrRaiseWindow(window);
        drawMainWin(gc);
      }
  if(bevent->wid==bt1)
      {
        //fileName = {"tuichu","file1","file2","file3","file4","file5","file6","file7","file8","file9"};
        doStartMenu(7,gc);
      }
  if(bevent->wid==bt2)
       {
         GrSetGCForeground(gc,BLACK);
         GrSetGCBackground(gc,BLACK);
         GrFillRect(window,gc,0,0,320,200);
       }
  if(bevent->wid==bt3)
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==smb[0])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==smb[1])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==smb[2])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==smb[3])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==smb[4])
       {
         ret=system("./demoevent");
       }
  if(bevent->wid==smb[5])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==smb[6])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==scb[0])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==scb[1])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==scb[2])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==scb[3])
       {
         ret=system("./demoevent");
       }
  if(bevent->wid==scb[4])
       {
         GrClose();
         exit(0);
       }
  if(bevent->wid==scb[5])
       {
         
       } 
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
void doEventButtonUp(GR_EVENT_BUTTON *bevent)
{
  
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
void doTime(GR_GC_ID gc)
{
  struct tm *tm_ptr;
  time_t timer;
  
  time(&timer);
  tm_ptr=localtime(&timer);
  strftime(datetime.year,10,"%Y",tm_ptr);
  strftime(datetime.mon,10,"%m",tm_ptr);
  strftime(datetime.day,10,"%d",tm_ptr);
  strftime(datetime.week,10,"%w",tm_ptr);
  strftime(datetime.hour,10,"%I",tm_ptr);
  strftime(datetime.min,10,"%M",tm_ptr);
  strftime(datetime.sec,10,"%S",tm_ptr);
  snprintf(datetime.date,sizeof(datetime.date),"%s/%s/%s",datetime.year,datetime.mon,datetime.day);
  snprintf(datetime.dateTime,sizeof(datetime.dateTime),"%s:%s:%s",datetime.hour,datetime.min,datetime.sec);

  GrSetGCUseBackground(gc, GR_FALSE);
	GrSetGCFont(gc, fontid);
  GrSetGCForeground(gc, 255);
	bt3=GrNewWindow(GR_ROOT_WINDOW_ID,250,215,70,23,2,GREEN,BLUE);
  //GrSelectEvents(bt3,GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN);
  GrMapWindow(bt3);
  GrText(bt3, gc, 5, 15,
         datetime.dateTime, -1, GR_TFASCII);
  //usleep(100);       
	
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
void doShortCut(char *fileName,char *picfile,int fileNum,GR_GC_ID gc,int x,int y)
{
  int scbW,scbH;
  scbW=40; scbH=30;

    scb[fileNum]=GrNewWindow(window,x,y,scbW,scbH,1,GREEN,BLUE);
    GrSelectEvents(scb[fileNum],GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_UP |
    GR_EVENT_MASK_BUTTON_DOWN | GR_EVENT_MASK_MOUSE_ENTER |
    GR_EVENT_MASK_MOUSE_EXIT);
    GrMapWindow(scb[fileNum]);
    //GrRaiseWindow(scb[fileNum]);
    //GR_ROOT_WINDOW_ID
    GrSetGCForeground(gc, MWRGB(255,250,55));
    GrSetGCUseBackground(gc, GR_FALSE);
	  GrSetGCFont(gc, fontid);
    GrSetFontSize(fontid, 12);
    setBackGround(picfile,scb[fileNum]);
    GrText(window,gc,x+1,y+scbH+20,fileName,-1,GR_FALSE);
  }
  
///////////////////////////////////////////////////////////////////////////////////////////////////////
void highLightStartMenu(int i)
{
  //char* startMenuFileName[fileNum+1];
  int smbW,smbH;
  smbW=100; smbH=20;
  
  
  //startMemuFileName[fileNum+1]={"tuichu","file1","file2","file3","file4","file5","file6"};
  
    smb[i]=GrNewWindow(GR_ROOT_WINDOW_ID,1,192-i*smbH,smbW,smbH,2,YELLOW,BLUE);
    GrSelectEvents(smb[i],GR_EVENT_MASK_EXPOSURE | GR_EVENT_MASK_BUTTON_DOWN |
           GR_EVENT_MASK_MOUSE_ENTER | GR_EVENT_MASK_MOUSE_EXIT);
    GrMapWindow(smb[i]);
    

    //startMenuFileName[i]=fileName[i];
    
  
}