#include "include.h" #include "func.h" #include "app_variable.h" #define CALE_CONTEXT_X_START_GAP 10 // x方向边界间隙 #define CALE_CONTEXT_X_GAP 15 // 间隔间隙 #define CALE_CONTEXT_WIDTH ((GUI_SCREEN_WIDTH - 6 * CALE_CONTEXT_X_GAP - 2 * CALE_CONTEXT_X_START_GAP) / 7) // 宽度 #define CALE_CONTEXT_y_START_GAP 88 // y方向上边界间隙 #define CALE_CONTEXT_y_GAP 14 // 间隔间隙 #define CALE_CONTEXT_HEIGHT 18 // 字高 #define CALE_CONTEXT_MAX 37 #define CALE_CONTEXT_NUM_COUNT_MAX 2 #define CALE_CONTEXT_TXT_W 28 #define CALE_CONTEXT_TXT_H 30 typedef struct calendar_info_t_ { u16 today_year; u8 today_mon; u8 today_day; u16 update_year; u8 update_mon; widget_icon_t *icon[CALE_CONTEXT_MAX]; } calendar_info_t; static calendar_info_t calendar_info = {0}; typedef struct f_calendar_t_ { } f_calendar_t; enum { CALE_SUNDAY = 0, CALE_MONDAY, CALE_TUESDAY, CALE_WEDNESDAY, CALE_THURSDAY, CALE_FRIDAY, CALE_SATURDAY, } e_cale_week; //基姆拉尔森计算公式,求某天的星期(当月份为1月或2月时,当作上一年的13月和14月) #define CAL_DAY_OF_WEEK(year, month, day) (uint16_t)(((day) + 1 + 2 * (month) + 3 * (month + 1) / 5 + \ (year) + (year) / 4 - (year) / 100 + (year) / 400) % 7) enum{ //文本 COMPO_ID_YEAR_TEXT = 256, COMPO_ID_DATE_TEXT_START, COMPO_ID_DATE_TEXT_END = (COMPO_ID_DATE_TEXT_START + CALE_CONTEXT_MAX), //按钮 COMPO_ID_LAST_BTN, COMPO_ID_NEXT_BTN, }; //根据当前月获取上个月 static uint8_t cal_last_month(uint8_t cur_month) { return (uint8_t)(cur_month > 1 ? (--cur_month) : 12); } //获取每个月最大天数 static uint8_t cal_max_of_days_per_month(uint16_t year, uint8_t month) { uint8_t day_max; switch(month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day_max = 31; break; case 4: case 6: case 9: case 11: day_max = 30; break; case 2: day_max = IS_LEAP_YEAR(year) ? 29 : 28; break; default: day_max = 30; break; } return day_max; } //刷新日历内容 static void func_calender_refresh(uint16_t year, uint8_t month, uint8_t today_day) { static uint16_t frist_day_week; static uint8_t day; static uint8_t day_max; static uint8_t last_day_max; static uint8_t i; static char buf[3] = {0}; day = 1; day_max = cal_max_of_days_per_month(year, month); last_day_max = cal_max_of_days_per_month(year, cal_last_month(month)); if (1 == month || 2 == month) { month += 12; year--; } frist_day_week = CAL_DAY_OF_WEEK(year, month, 1); for (i = 0; i < CALE_CONTEXT_MAX; i++) { compo_label_t *cale_label = compo_getobj_byid(COMPO_ID_DATE_TEXT_START + i); if (calendar_info.icon[i] && cale_label) { if (i < frist_day_week) { buf[0] = 48 + (last_day_max - frist_day_week + i + 1) / 10; buf[1] = 48 + (last_day_max - frist_day_week + i + 1) % 10; compo_label_set_forecolor(cale_label, COLOR_GRAY); compo_label_set(cale_label, buf); /* 最新UI需求,不显示不需要本月日期 */ compo_label_set_visible(cale_label, false); widget_set_visible(calendar_info.icon[i], false); } else { if (day <= day_max) { if (day < 10) { buf[0] = 48 + day; buf[1] = 0; } else { buf[0] = 48 + day / 10; buf[1] = 48 + day % 10; } if (today_day == day) { compo_label_set_forecolor(cale_label, COLOR_WHITE); widget_icon_set(calendar_info.icon[i], UI_BUF_CALENDAR_CURRENT_BIN); widget_set_visible(calendar_info.icon[i], true); } else { /* 周六、周日显示灰色背景 */ if (!(i % 7) || !((i + 1) % 7)) { compo_label_set_forecolor(cale_label, COLOR_GRAY); widget_set_visible(calendar_info.icon[i], false); } else { compo_label_set_forecolor(cale_label, COLOR_BLACK); widget_icon_set(calendar_info.icon[i], UI_BUF_CALENDAR_NONE_BIN); widget_set_visible(calendar_info.icon[i], true); } } compo_label_set(cale_label, buf); compo_label_set_visible(cale_label, true); } else { if ((day - day_max) < 10) { buf[0] = 48 + (day - day_max); buf[1] = 0; } else { buf[0] = 48 + (day - day_max) / 10; buf[1] = 48 + (day - day_max) % 10; } compo_label_set_forecolor(cale_label, COLOR_GRAY); compo_label_set(cale_label, buf); /* 最新UI需求,不显示不需要本月日期 */ compo_label_set_visible(cale_label, false); widget_set_visible(calendar_info.icon[i], false); } day++; } } } } //创建日历主界面 void func_calender_content_show(compo_form_t *frm) { if (frm == NULL) { return; } compo_label_t *cale_label = NULL; static uint8_t i; static s16 x_pos = CALE_CONTEXT_X_START_GAP + CALE_CONTEXT_WIDTH / 2; static s16 y_pos = CALE_CONTEXT_y_START_GAP; static char week_text[7][4] = {"S", "M", "T", "W", "T", "F", "S"}; // 新建日历文本内容 for (i = 0; i < CALE_CONTEXT_MAX; i++) { if (!(i % 7)) { x_pos = CALE_CONTEXT_X_START_GAP + CALE_CONTEXT_WIDTH / 2; y_pos = CALE_CONTEXT_y_START_GAP + (i / 7) * (CALE_CONTEXT_HEIGHT + CALE_CONTEXT_y_GAP); } calendar_info.icon[i] = widget_icon_create(frm->page_body, UI_BUF_CALENDAR_NONE_BIN); widget_set_pos(calendar_info.icon[i], x_pos + 1, y_pos + 2); widget_set_visible(calendar_info.icon[i], false); cale_label = compo_label_create(frm, CALE_CONTEXT_NUM_COUNT_MAX); compo_label_set_font(cale_label, UI_BUF_0FONT_FONT_CALENDAR_BIN); compo_setid(cale_label, COMPO_ID_DATE_TEXT_START + i); compo_label_set_pos(cale_label, x_pos, y_pos); x_pos += (CALE_CONTEXT_WIDTH + CALE_CONTEXT_X_GAP); } // 新建(日 一 二 三 四 五 六)文本 x_pos = CALE_CONTEXT_X_START_GAP + CALE_CONTEXT_WIDTH / 2; y_pos = CALE_CONTEXT_y_START_GAP - 40; for (i = 0; i < 7; i++) { cale_label = compo_label_create(frm, 1); compo_label_set_location(cale_label, x_pos, y_pos, CALE_CONTEXT_TXT_W, CALE_CONTEXT_TXT_H); #if defined(__LANGUAGE_SM_CHINESE__) if (tjd_Get_Language_Current_Index() == LANGUAGE_TYPE_Chinese) compo_label_set(cale_label, i18n[STR_SUNDAY_SIMPLE + i]); else #endif compo_label_set(cale_label, week_text[i]); /* 周六、周日颜色单独区分 */ if (!i || i == 6) { compo_label_set_forecolor(cale_label, COLOR_GRAY); } x_pos += (CALE_CONTEXT_WIDTH + CALE_CONTEXT_X_GAP); } compo_shape_t *line_shape = compo_shape_create(frm, COMPO_SHAPE_TYPE_RECTANGLE); compo_shape_set_location(line_shape, GUI_SCREEN_CENTER_X, y_pos + CALE_CONTEXT_HEIGHT, GUI_SCREEN_WIDTH, 2); compo_shape_set_color(line_shape, COLOR_GRAY); // year_text char str_txt[TEXTBOX_TEXT_BUF_LEN] = {0}; cale_label = compo_label_create(frm, 8); compo_label_set_font(cale_label, UI_BUF_0FONT_FONT_BIN); compo_label_set_pos(cale_label, GUI_SCREEN_CENTER_X, GUI_SCREEN_HEIGHT - 19); sprintf(str_txt, "%04d.%02d", SysVariable.Rtc.year, SysVariable.Rtc.mon); compo_label_set(cale_label, str_txt); compo_setid(cale_label, COMPO_ID_YEAR_TEXT); // last_btn compo_form_add_image(frm, UI_BUF_COMMON_LEFT_BIN, GUI_SCREEN_CENTER_X - 71, GUI_SCREEN_HEIGHT - 18); compo_button_t *btn = compo_button_create(frm); compo_setid(btn, COMPO_ID_LAST_BTN); compo_button_set_location(btn, GUI_SCREEN_CENTER_X - 71, GUI_SCREEN_HEIGHT - 18, 40, 40); // next_btn compo_form_add_image(frm, UI_BUF_COMMON_RIGHT_BIN, GUI_SCREEN_CENTER_X + 71, GUI_SCREEN_HEIGHT - 18); btn = compo_button_create(frm); compo_setid(btn, COMPO_ID_NEXT_BTN); compo_button_set_location(btn, GUI_SCREEN_CENTER_X + 71, GUI_SCREEN_HEIGHT - 18, 40, 40); } //创建日历主界面 compo_form_t *func_calender_form_create(void) { /* 初始化日历基础数据 */ calendar_info.today_year = SysVariable.Rtc.year; calendar_info.today_mon = SysVariable.Rtc.mon; calendar_info.today_day = SysVariable.Rtc.day; calendar_info.update_year = calendar_info.today_year; calendar_info.update_mon = calendar_info.today_mon; // 新建窗体 compo_form_t *frm = compo_form_create(true); // 设置标题栏 compo_form_set_mode(frm, COMPO_FORM_MODE_SHOW_TITLE | COMPO_FORM_MODE_SHOW_TIME); compo_form_set_title(frm, i18n[STR_SETTING_CALENDAR]); /* 显示日历内容 */ func_calender_content_show(frm); /* 刷新日历信息 */ func_calender_refresh(SysVariable.Rtc.year, SysVariable.Rtc.mon, SysVariable.Rtc.day); return frm; } //切换日期并刷新 static void func_calendar_date_update(bool next) { uint8_t today_day; component_t *comop; char str_txt[TEXTBOX_TEXT_BUF_LEN] = {0}; if(next) { calendar_info.update_mon ++; if(calendar_info.update_mon > 12) { calendar_info.update_mon = 1; calendar_info.update_year ++; } } else { calendar_info.update_mon --; if(0 == calendar_info.update_mon) { calendar_info.update_mon = 12; calendar_info.update_year --; } } comop = compo_getobj_byid(COMPO_ID_YEAR_TEXT); sprintf(str_txt, "%04d.%02d", calendar_info.update_year, calendar_info.update_mon); compo_label_set((compo_label_t *)comop, str_txt); //刷新当前日期 today_day = 0; if(calendar_info.today_year == calendar_info.update_year && calendar_info.today_mon == calendar_info.update_mon) { today_day = calendar_info.today_day; } //刷新日历内容 func_calender_refresh(calendar_info.update_year, calendar_info.update_mon, today_day); } //单击按钮 static void func_calendar_button_click(void) { int id = compo_get_button_id(); if (COMPO_ID_LAST_BTN == id) { func_calendar_date_update(false); } else if (COMPO_ID_NEXT_BTN == id) { func_calendar_date_update(true); } } //公共事件处理 static void func_calendar_comm_process(void) { func_process(); } //消息处理 static void func_calendar_message(size_msg_t msg) { switch (msg) { case MSG_CTP_CLICK: func_calendar_button_click(); break; case MSG_CTP_SHORT_LEFT: case MSG_QDEC_FORWARD: func_calendar_date_update(true); break; case MSG_CTP_SHORT_RIGHT: case MSG_QDEC_BACKWARD: if (msg == MSG_CTP_SHORT_RIGHT && ctp_get_sxy().x < 20) { func_message(msg); break; } func_calendar_date_update(false); break; default: func_message(msg); break; } } //进入日历功能 static void func_calendar_enter(void) { func_cb.f_cb = func_zalloc(sizeof(f_calendar_t)); func_cb.frm_main = func_calender_form_create(); } //退出日历功能 static void func_calendar_exit(void) { func_cb.last = FUNC_CALENDAER; } //日历功能 void func_calendar(void) { printf("%s\n", __func__); func_calendar_enter(); while (func_cb.sta == FUNC_CALENDAER) { func_calendar_comm_process(); func_calendar_message(msg_dequeue()); } func_calendar_exit(); }