#include "GUI.h"
#include "WM.h"
#include "DIALOG.h"
#include "EDIT.h"
#include "TEXT.h"
#include "DROPDOWN.h"
#include "FRAMEWIN.h"
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "FRAMEWIN_Private.h"
/******************************************************
*
* widget ids 定义一些控件的ID
*/
#define DATEID (GUI_ID_USER+200)
#define YEAR_SCROLLBAR (GUI_ID_USER+20)
#define TIME_SCROLLBAR (GUI_ID_USER+21)
#define TIME_TICK (GUI_ID_EDIT0)
#define YEAR (GUI_ID_EDIT1)
#define YEAR_ADD (GUI_ID_USER+100)
#define YEAR_DEC (GUI_ID_USER+101)
#define TIME_ADD (GUI_ID_USER+103)
#define TIME_DEC (GUI_ID_USER+104)
#define WEEK_HEAD (GUI_ID_USER+29)
#define MONTH (GUI_ID_DROPDOWN0)
#define TIME (GUI_ID_EDIT2)
#define SHOW_TIME (GUI_ID_USER+110)
#define TIMECOUNT GUI_ID_USER+300
// calendar setting
#define DATEWITHE 15
#define DATEHEIGHT 15
#define DATESPAN 7
#define DATEX 11
/******************************************************
*
* types 选中时间的定位结构
*/
typedef struct
{
int start,end;
} TIMESEL;
/******************************************************
*
* funtion declare
*/
static char* itos( I32 n,char* p,int mod,int len );
static I32 stoi( char*p,int len,int mod );
static time_t convertStr2Time( char* p );
static char* convertTime2Str( char* s );
static void adjustTime( int methor );
static void drawDate( WM_HWIN hParent,int wday,int end,int x0,int y0,int width0,int height0 );
static void clearDate( WM_HWIN hParent );
static int days( int year,int month );
static void setDate(WM_HWIN hWin,int year,int month,int date);
static void _setDate(WM_HWIN hWin,int year,int month,int date);
static char* getDateTimeStr(WM_HWIN calendar,char * dt);
static void _getDateTime(WM_HWIN hWin,struct tm *dt);
static void _setDateTime(WM_HWIN hWin,const struct tm *dt);
static void reset(WM_HWIN hWin);
static void saveDateTime(WM_HWIN hWin);
/******************************************************
*
* calendar config 日历的界面的参数设置
*/
static time_t mTime;
static TIMESEL timeSel;
static WM_CALLBACK* cbTxtDate;
static TEXT_Handle htxtLastSel;
static I32 scrollerPosYear,scrollerPosTime,scrollerMax;
static WM_HWIN ghCalendar;
//static WM_HMEM gTimer;
static GUI_TIMER_HANDLE gTimer;
static const GUI_WIDGET_CREATE_INFO calendar[] =
{
{ FRAMEWIN_CreateIndirect, "Calendar", 0, 10, 10, 180, 190,FRAMEWIN_CF_MOVEABLE},//FRAMEWIN_CF_MOVEABLE
{ EDIT_CreateIndirect, "year", YEAR, 10, 5, 80, 20, EDIT_CF_LEFT, 50 },
{ SCROLLBAR_CreateIndirect, " scroll year", YEAR_SCROLLBAR, 90, 5, 10, 20,SCROLLBAR_CF_VERTICAL },
{ DROPDOWN_CreateIndirect, "month", MONTH, 110, 5, 50, 100 },
{ EDIT_CreateIndirect, "time", TIME, 90, 128, 55, 20, EDIT_CF_LEFT , 50 },
{ SCROLLBAR_CreateIndirect, " scroll time", TIME_SCROLLBAR, 145, 128, 10, 20,SCROLLBAR_CF_VERTICAL },
{ TEXT_CreateIndirect, "Sun Mon Tue Wed Thu Fri Sat", WEEK_HEAD, 8, 32, 150, 15, GUI_TA_VCENTER| GUI_TA_HCENTER },
//{ BUTTON_CreateIndirect, "ShowTime", SHOW_TIME, 20, 150, 55, 20 },
{ BUTTON_CreateIndirect, "OK", GUI_ID_OK, 20, 150, 55, 20 },
{ BUTTON_CreateIndirect, "Cancel", GUI_ID_CANCEL, 85 , 150, 55, 20 }
};
static void initailDialog(WM_HWIN hWin)
{
/* 初始化日历 */
char tmpi[11];
I32 i;
WM_HWIN hdrpMonth=WM_GetDialogItem( hWin,MONTH );
WM_HWIN hedtYear=WM_GetDialogItem( hWin,YEAR );
WM_HWIN hedtTime=WM_GetDialogItem( hWin,TIME );
WM_HWIN htxtWeek=WM_GetDialogItem( hWin,WEEK_HEAD );
WM_HWIN hscrYear=WM_GetDialogItem(hWin,YEAR_SCROLLBAR);
WM_HWIN hscrTime=WM_GetDialogItem(hWin,TIME_SCROLLBAR);
htxtLastSel=0;
timeSel.start=0;
timeSel.end=1;
SCROLLBAR_SetNumItems(hscrYear,0x7fffffff);
SCROLLBAR_SetValue(hscrYear, 0x3fffffff);
SCROLLBAR_SetNumItems(hscrTime,0x7fffffff);
SCROLLBAR_SetValue(hscrTime, 0x3fffffff);
scrollerPosYear=scrollerPosTime=0x3fffffff;
for ( i=0; i<12; i++ )
{
DROPDOWN_AddString( hdrpMonth,itos( i+1,tmpi,10,2 ) );
}
DROPDOWN_SetAutoScroll( hdrpMonth,1 );
DROPDOWN_SetBkColor(hdrpMonth,1,GUI_WHITE);
DROPDOWN_SetTextColor(hdrpMonth,1, GUI_BLACK);
EDIT_SetMaxLen( hedtYear,4 );
EDIT_SetDecMode( hedtYear, 2009, 1000, 9999, 0, 0 );
TEXT_SetBkColor( htxtWeek,GUI_BLUE );
TEXT_SetTextColor( htxtWeek,GUI_WHITE );
{
WM_Obj* pWin;
WM_LOCK();
pWin = WM_H2P(WM_GetDialogItem( hWin,WEEK_HEAD ));
cbTxtDate=pWin->cb;
WM_UNLOCK();
}
EDIT_SetInsertMode(hedtTime,GUI_EDIT_MODE_OVERWRITE);
EDIT_SetMaxLen( hedtTime,8 );
reset(hWin);
//EDIT_SetText( hedtTime,convertTime2Str( tmpi ) );
//WM_CreateTimer(WM_GetClientWindow(hWin),TIMECOUNT,1000,0);
/*setDate(
hWin ,
EDIT_GetValue(hedtYear),
DROPDOWN_GetSel(hdrpMonth)+1,
0);*/
}
static void buttonsAction(WM_HWIN hWin,I32 ID)
{/* 日历上控件的响应动作 */
switch ( ID )
{
case GUI_ID_OK: /* OK Button */
saveDateTime(hWin);
GUI_EndDialog(hWin, 0);
break;
case GUI_ID_CANCEL : /* Cancel Button */
GUI_EndDialog(hWin, 1);
break;
case SHOW_TIME:
{
char datetimeStr[50];
getDateTimeStr(hWin,datetimeStr);
GUI_MessageBox(datetimeStr, "Date Time ", 1);
}
break;
case YEAR_SCROLLBAR:
{
WM_HWIN hscrYear=WM_GetDialogItem( hWin,YEAR_SCROLLBAR );
WM_HWIN hdrpMonth=WM_GetDialogItem( hWin,MONTH );
WM_HWIN hedtYear=WM_GetDialogItem( hWin,YEAR );
I32 j=SCROLLBAR_GetValue(hscrYear);
I32 i=(j<scrollerPosYear)?1:-1;
scrollerPosYear=j;
j=EDIT_GetValue( hedtYear );
EDIT_SetValue( hedtYear,j+i );
setDate(hWin ,j+i, DROPDOWN_GetSel(hdrpMonth)+1,0);
}
break;
case TIME_SCROLLBAR:
{
char tmpi[11];
WM_HWIN hscrTime=WM_GetDialogItem( hWin,TIME_SCROLLBAR);
WM_HWIN hedtTime=WM_GetDialogItem( hWin,TIME );
I32 j=SCROLLBAR_GetValue(hscrTime);
I32 i=(j< scrollerPosTime)?1:-1;
scrollerPosTime=j;
adjustTime( i );
EDIT_SetText( hedtTime,convertTime2Str( tmpi ) );
WM_SetFocus( hedtTime );
EDIT_SetSel( hedtTime,timeSel.start,timeSel.end );
}
break;
}
}
/*********************************************************************
*
* 日历的回调函数
*/
static void _cbCalendar( WM_MESSAGE * pMsg )
{
switch ( pMsg->MsgId )
{
case WM_INIT_DIALOG:
/* Initialize all widgets */
break;
case WM_NOTIFY_PARENT://WM_NOTIFY_CLIENTCHANGE:
{
I32 Id = WM_GetId( pMsg->hWinSrc ); /* Id of widget */
I32 NCode = pMsg->Data.v; /* Notification code */
switch ( NCode )
{
case WM_NOTIFICATION_RELEASED:
buttonsAction(pMsg->hWin,Id);
break;
case WM_NOTIFICATION_CLICKED:
if ( Id==TIME )
{
WM_HWIN hedtTime=WM_GetDialogItem( pMsg->hWin,TIME );
EDIT_SetSel( hedtTime,timeSel.start,timeSel.end );
}
break;
case WM_NOTIFICATION_SEL_CHANGED:
{
WM_HWIN hdrpMonth=WM_GetDialogItem( pMsg->hWin,MONTH );
WM_HWIN hedtYear=WM_GetDialogItem( pMsg->hWin,YEAR );
setDate(
pMsg->hWin,
EDIT_GetValue(hedtYear),
DROPDOWN_GetSel(hdrpMonth)+1,
0);
}
break;
}
}
break;
case WM_TOUCH_CHILD:
{
I32 Id = WM_GetId( pMsg->hWinSrc );
if ( Id ==TIME )
{
GUI_PID_STATE * pState;
pState = ( GUI_PID_STATE * )( ( WM_MESSAGE * )pMsg->Data.p )->Data.p;
if (( pState!=0 )&&(pState->Pressed))
{
timeSel.start=(pState->x<16)?0:(pState->x<32)?3:6;
timeSel.end=timeSel.start+1;
}
}
}
break;
case WM_TIMER:
{
WM_HWIN hedtTime=WM_GetDialogItem( pMsg->hWin,TIME );
char tmpi[11];
//gTimer=WM_CreateTimer(WM_GetClientWindow(hWin),TIMECOUNT,1000,0);
EDIT_SetText( hedtTime,convertTime2Str( tmpi ) );
EDIT_SetSel( hedtTime,timeSel.start,timeSel.end );
//mTime++;
}
break;
case WM_TOUCH:
if (htxtLastSel)
WM_SetFocus( htxtLastSel );
//WM_DefaultProc( pMsg );
break;
case WM_KEY:
switch (((WM_KEY_INFO*)(pMsg->Data.p))->Key)
{
case GUI_KEY_ESCAPE:
GUI_EndDialog(pMsg->hWin, 1);
break;
case GUI_KEY_ENTER:
if (WM_GetId(pMsg->hWinSrc)==YEAR)
{
WM_HWIN hdrpMonth=WM_GetDialogItem( pMsg->hWin,MONTH );
WM_HWIN hedtYear=WM_GetDialogItem( pMsg->hWin,YEAR );
setDate(
pMsg->hWin,
EDIT_GetValue(hedtYear),
DROPDOWN_GetSel(hdrpMonth)+1,
0);
}
break;
case GUI_ID_OK:
//GUI_EndDialog(hWin, 0);
break;
}
break;
case WM_DELETE:
//WM_DeleteTimer(gTimer);
GUI_TIMER_Delete(gTimer);
default:
WM_DefaultProc( pMsg );
}
}
static void _cbDate( WM_MESSAGE * pMsg )
{ /* 单个日期显示的回调函数 */
WM_HWIN hWin = pMsg->hWin;
GUI_PID_STATE *pState;
pState=(GUI_PID_STATE *)pMsg->Data.p;
if (( pMsg->MsgId==WM_TOUCH ) && (pState) && (pState->Pressed))
//if ( pMsg->MsgId==WM_TOUCH )
{
if ( WM_IsWindow( htxtLastSel ) )
{
TEXT_SetBkColor( htxtLastSel,0xc3c3c3 );
TEXT_SetTextColor( htxtLastSel,GUI_BLACK );
}
TEXT_SetBkColor( hWin,GUI_BLUE );
TEXT_SetTextColor( hWin,GUI_WHITE );
htxtLastSel=hWin;
WM_SetFocus( hWin );
}
//WM_DefaultProc(pMsg);
cbTxtDate( pMsg );
}
static void _cbTimer( GUI_TIMER_MESSAGE* pTM )
{ /* 时钟计时器回调函数 */
GUI_TIMER_Restart(gTimer);
WM_SendMessageNoPara( WM_GetClientWindow(ghCalendar),WM_TIMER );
mTime++;
}
/******************************************************
*
* funtions
*/
static char* itos( I32 n,char* p,int mod,int len )
{
char tmp[11];
int si,i;
//tmp[11]=0;
memset( tmp,0,11 );
//memset(p,0,len+1);
for ( i=0; i<10 && n>=0 && i<len; i++ )
{
si=n%mod;
tmp[9-i]=( si<10 )?si+'0':si-10+'A';
n=n/mod;
if ( ( n==0 ) && ( len==0 ) )
break;
}
strcpy( p,tmp+10-i );
//strcpy_s(p,i+1,tmp+10-i);
return p;
}
static I32 stoi( char*p,int len,int mod )
{
I32 r=0,tmp;
int i;
for ( i=0; i<len; i++ )
{
tmp=( p[i]<'A' )?p[i]-'0':p[i]-'A'+10;
r=r*mod+tmp;
}
return r;
}
static time_t convertStr2Time( char* p )
{
struct tm dt;
char* s;
I32 r=0;
s=p;
//s=strstr(s,":")-s;
dt=*localtime(&mTime);
//localtime_s(&dt,&mTime);
dt.tm_hour=atoi(s);
s=strchr(s,':')+1;
dt.tm_min=atoi(s);
s=strchr(s,':')+1;
dt.tm_sec=atoi(s);
/*
dt->tm_hour=stoi( p,2,10 );
dt->tm_min=stoi( p+3,2,10 );
dt->tm_sec=stoi( p+6,2,10 );
*/
mTime=mktime(&dt);
return mTime;
}
static char* convertTime2Str( char* s )
{
struct tm dt;
// char t[3];
dt=*localtime(&mTime);
//localtime_s(&dt,&mTime);
sprintf(s,"%02d:%02d:%02d",dt.tm_hour,dt.tm_min,dt.tm_sec);
return s;
}
static void adjustTime( int methor )
{
I32 v;
struct tm dt;
dt=*localtime(&mTime);
// localtime_s(&dt,&mTime);
switch ( timeSel.start )
{
case 0:
v=dt.tm_hour+methor;
v=v>0?v%24:24+v;
dt.tm_hour=v;
break;
case 3:
v=dt.tm_min+methor;
v=v>0?v%60:60+v;
dt.tm_min=v;
break;
case 6:
v=dt.tm_sec+methor;
v=v>0?v%60:60+v;
dt.tm_sec=v;
break;
}
mTime=mktime(&dt);
}
static void reset(WM_HWIN hWin)
{
//time(&mTime);
struct tm dt;
dt=*localtime(&mTime);
//localtime_s(&dt,&mTime);
_setDateTime(hWin,&dt);
}
static void drawDate( WM_HWIN hWin,int wday,int end,int x0,int y0,int width0,int height0 )
{
//int wday,end,width0,height0,x0,y0;
//WM_HWIN hWin;
int i,pos;
char date[3];
TEXT_Handle htmp;
hWin=WM_GetClientWindow(hWin);
pos=x0;
x0=( wday )*(width0 +DATESPAN)+pos;
for ( i=wday; i<( end+wday ); i++ )
{
htmp=TEXT_CreateAsChild( x0,y0, width0,height0,hWin, DATEID+i-wday,WM_CF_SHOW, itos( i-wday+1,date,10,2 ), GUI_TA_VCENTER| GUI_TA_HCENTER );
WM_SetCallback( htmp,_cbDate );
x0+=width0+DATESPAN;
if ( (i+1)%7==0 )
{
x0=pos;
y0+=height0;
}
}
}
static void clearDate( WM_HWIN hParent )
{
I32 i;
WM_HWIN hWin;
for ( i=DATEID; i<DATEID+31; i++ )
{
hWin=WM_GetDialogItem( hParent,i );
if (WM_IsWindow(hWin))
WM_DeleteWindow( hWin );
}
}
static int days( int year,int month )
{
int Days;
if ( 2 == month )
Days = !( year % 4 ) && ( year % 100 ) || !( year % 400 ) ? 29 : 28;
else if ( month < 8 )
Days = 30 + ( month & 1 );
else
Days = 31 - ( month & 1 );
return Days;
}
static void setDate(WM_HWIN hWin,int year,int month,int date)
{
struct tm dt;
char* tStr;
int days31,week0,dateSel;
_setDate(hWin,year,month,date);
/* update */
dt=*localtime(&mTime);
//localtime_s(&dt,&mTime);
tStr=asctime(&dt);
days31=days(year, month);
week0=(dt.tm_wday+(8-dt.tm_mday%7))%7;
dateSel=(WM_IsWindow(htxtLastSel))?WM_GetId(htxtLastSel):DATEID;
clearDate(hWin);
drawDate(hWin,week0,days31, DATEX,50,DATEWITHE,DATEHEIGHT );
EDIT_SetValue(WM_GetDialogItem(hWin,YEAR),year);
DROPDOWN_SetSel(WM_GetDialogItem(hWin,MONTH),month-1);
{
WM_MESSAGE msg={0};
GUI_PID_STATE State={0};
State.Pressed=1;
msg.MsgId=WM_TOUCH;
msg.Data.p=&State;
if ((date>0)&&(date<=days31))
//WM_SendMessageNoPara(WM_GetDialogItem(hWin,date+DATEID-1) , WM_TOUCH);
WM_SendMessage(WM_GetDialogItem(hWin,date+DATEID-1), &msg);
else if ((dateSel-DATEID>=0) &&(dateSel-DATEID<=days31) )
//WM_SendMessageNoPara(WM_GetDialogItem(hWin, dateSel) , WM_TOUCH);
WM_SendMessage(WM_GetDialogItem(hWin, dateSel) , &msg);
}
}
static void _setDate(WM_HWIN hWin,int year,int month,int date)
{
struct tm dt;
dt=*localtime(&mTime);
//localtime_s(&dt,&mTime);
dt.tm_year=year-1900;
dt.tm_mon=month-1;
if (date!=0)
dt.tm_mday=date;
mTime=mktime(&dt);
}
static void _setTime(I8 h,I8 m,I8 s)
{
struct tm dt;
dt=*localtime(&mTime);
//localtime_s(&dt,&mTime);
dt.tm_hour=h%24;
dt.tm_min=m%60;
dt.tm_sec=s%60;
mTime=mktime(&dt);
}
void _setDateTime(WM_HWIN hWin,const struct tm* dt)
{
mTime=mktime(dt);
setDate(hWin,dt->tm_year+1900,dt->tm_mon+1,dt->tm_mday);
}
void setDateTime(const struct tm* dt)
{
_setDateTime(ghCalendar,dt);
}
void _getDateTime(WM_HWIN hWin,struct tm *dt)
{
saveDateTime(hWin);
dt=localtime(&mTime);
//localtime_s(&dt,&mTime);
//return dt;
}
static void saveDateTime(WM_HWIN hWin)
{
TEXT_Handle hedtYear;
DROPDOWN_Handle hdrpMonth;
//struct tm *dt;
hdrpMonth=WM_GetDialogItem( hWin,MONTH );
hedtYear=WM_GetDialogItem( hWin,YEAR );
_setDate(hWin,EDIT_GetValue(hedtYear),DROPDOWN_GetSel(hdrpMonth)+1,WM_GetId(htxtLastSel)-DATEID+1);
}
void getDateTime(struct tm *dt)
{
//struct tm* dtt;
_getDateTime(ghCalendar,dt);
//*dt=*dtt;
}
static char* getDateTimeStr(WM_HWIN hWin,char * dts)
{
struct tm dt;
_getDateTime(hWin,&dt);
strftime(dts,50,"%a %b,%d.%Y %H:%M:%S",&dt);
return dts;
}
int CreateCalendar(WM_HWIN hParent ,struct tm* dt,int modal)
{
//WM_HWIN hWin;
struct tm *dtt;
int flag=1;
mTime=dt?mktime(dt):1251643198l;
ghCalendar=GUI_CreateDialogBox( calendar, GUI_COUNTOF( calendar ), NULL,hParent , 0, 0 );
initailDialog(ghCalendar);
{
FRAMEWIN_Obj* pObj;
GUI_LOCK();
pObj = FRAMEWIN_H2P(ghCalendar);
pObj->cb= _cbCalendar;
GUI_UNLOCK();
}
/*
if (dt)
{_setDateTime(ghCalendar,dt);
}*/
//WM_SendMessageNoPara( WM_GetClientWindow(ghCalendar),WM_TIMER );
gTimer=GUI_TIMER_Create(&_cbTimer,0,0,0);
GUI_TIMER_SetPeriod(gTimer,1000);
//gTimer=WM_CreateTimer(WM_GetClientWindow(ghCalendar),TIMECOUNT,1000,0);
//
if (modal)
{
WM_MakeModal(ghCalendar);
flag=GUI_ExecCreatedDialog(ghCalendar);
}
if ((flag==0)&&(dt))
{
*dt=*localtime(&mTime);
//localtime_s(dt,&mTime);
}
return flag;
}
void MainTask( void )
{
WM_HWIN hWin;
struct tm DateTime,*dt,dt2={0};
time_t ltime;
char* tStr;
int i;
GUI_Init();
WM_SetDesktopColor( GUI_BLACK );
GUI_Delay( 1000 );
//time(<ime);
ltime=0;
dt=localtime(<ime);
dt->tm_year=2009-1900;
ltime=mktime(dt);
dt2=*localtime(<ime);
i=CreateCalendar(WM_HBKWIN,&dt2,1);//
while ( 1 )
{
if(!WM_IsWindow( ghCalendar ))
//WM_SendMessageNoPara( WM_GetClientWindow( hWin ),WM_TIMER );//275
//else
{
//ghCalendar=GUI_CreateDialogBox( calendar, GUI_COUNTOF( calendar ), _cbCalendar,0 , 0, 0 );
//gTimer=WM_CreateTimer(WM_GetClientWindow(ghCalendar),TIMECOUNT,1000,0);
CreateCalendar(0,NULL,0);
}
GUI_Delay( 1000 );
}
}