diff --git a/lv_objx/lv_calendar.c b/lv_objx/lv_calendar.c index aa66f18bc4..327ac75150 100644 --- a/lv_objx/lv_calendar.c +++ b/lv_objx/lv_calendar.c @@ -34,7 +34,9 @@ typedef uint8_t day_draw_state_t; **********************/ static bool lv_calendar_design(lv_obj_t * calendar, const lv_area_t * mask, lv_design_mode_t mode); static lv_res_t lv_calendar_signal(lv_obj_t * calendar, lv_signal_t sign, void * param); +static bool calculate_touched_day(lv_obj_t * calendar, const lv_point_t * touched_point); static lv_coord_t get_header_height(lv_obj_t * calendar); +static lv_coord_t get_day_names_height(lv_obj_t * calendar); static void draw_header(lv_obj_t * calendar, const lv_area_t * mask); static void draw_day_names(lv_obj_t * calendar, const lv_area_t * mask); static void draw_days(lv_obj_t * calendar, const lv_area_t * mask); @@ -45,6 +47,7 @@ static const char * get_month_name(lv_obj_t * calendar, int32_t month); static uint8_t get_month_length(int32_t year, int32_t month); static uint8_t is_leap_year(uint32_t year); + /********************** * STATIC VARIABLES **********************/ @@ -52,9 +55,9 @@ static lv_signal_func_t ancestor_signal; static lv_design_func_t ancestor_design; static const char * day_name[7] = {"Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"}; static const char * month_name[12] = {"January", "February", "March", "April", - "May", "June", "July", "August", - "September", "October", "November", "December" - }; + "May", "June", "July", "August", + "September", "October", "November", "December" +}; /********************** * MACROS @@ -94,11 +97,19 @@ lv_obj_t * lv_calendar_create(lv_obj_t * par, const lv_obj_t * copy) ext->showed_date.year = 2018; ext->showed_date.month = 1; ext->showed_date.day = 1; - + + ext->pressed_date.year = 0; + ext->pressed_date.month = 0 + ext->pressed_date.day = 0 + ext->highlighted_dates = NULL; ext->highlighted_dates_num = 0; ext->day_names = NULL; ext->month_names = NULL; + ext->actions[LV_CALENDAR_ACTION_PR] = NULL; + ext->actions[LV_CALENDAR_ACTION_CLICK] = NULL; + ext->actions[LV_CALENDAR_ACTION_LONG_PR] = NULL; + ext->actions[LV_CALENDAR_ACTION_LONG_PR_REPEAT] = NULL; ext->style_header = &lv_style_plain_color; ext->style_header_pr = &lv_style_pretty_color; ext->style_highlighted_days = &lv_style_plain_color; @@ -153,6 +164,9 @@ lv_obj_t * lv_calendar_create(lv_obj_t * par, const lv_obj_t * copy) ext->highlighted_dates = copy_ext->highlighted_dates; ext->highlighted_dates_num = copy_ext->highlighted_dates_num; ext->day_names = copy_ext->day_names; + + memcpy(ext->actions, copy_ext->actions, sizeof(ext->actions)); + ext->month_names = copy_ext->month_names; ext->style_header = copy_ext->style_header; ext->style_header_pr = copy_ext->style_header_pr; @@ -183,6 +197,19 @@ lv_obj_t * lv_calendar_create(lv_obj_t * par, const lv_obj_t * copy) * Setter functions *====================*/ +/** + * Set a function to call when a calendar event happens + * @param calendar pointer to a calendar object + * @param action type of event form 'lv_action_t' (press, release, long press, long press repeat) + */ +void lv_calendar_set_action(lv_obj_t * calendar, lv_calendar_action_t type, lv_action_t action) +{ + if(type >= LV_CALENDAR_ACTION_NUM) return; + + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + ext->actions[type] = action; +} + /** * Set the today's date * @param calendar pointer to a calendar object @@ -266,30 +293,30 @@ void lv_calendar_set_style(lv_obj_t * calendar, lv_calendar_style_t type, lv_sty lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); switch(type) { - case LV_CALENDAR_STYLE_BG: - lv_obj_set_style(calendar, style); - break; - case LV_CALENDAR_STYLE_DAY_NAMES: - ext->style_day_names = style; - break; - case LV_CALENDAR_STYLE_HEADER: - ext->style_header = style; - break; - case LV_CALENDAR_STYLE_HEADER_PR: - ext->style_header_pr = style; - break; - case LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS: - ext->style_highlighted_days = style; - break; - case LV_CALENDAR_STYLE_INACTIVE_DAYS: - ext->style_inactive_days = style; - break; - case LV_CALENDAR_STYLE_TODAY_BOX: - ext->style_today_box = style; - break; - case LV_CALENDAR_STYLE_WEEK_BOX: - ext->style_week_box = style; - break; + case LV_CALENDAR_STYLE_BG: + lv_obj_set_style(calendar, style); + break; + case LV_CALENDAR_STYLE_DAY_NAMES: + ext->style_day_names = style; + break; + case LV_CALENDAR_STYLE_HEADER: + ext->style_header = style; + break; + case LV_CALENDAR_STYLE_HEADER_PR: + ext->style_header_pr = style; + break; + case LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS: + ext->style_highlighted_days = style; + break; + case LV_CALENDAR_STYLE_INACTIVE_DAYS: + ext->style_inactive_days = style; + break; + case LV_CALENDAR_STYLE_TODAY_BOX: + ext->style_today_box = style; + break; + case LV_CALENDAR_STYLE_WEEK_BOX: + ext->style_week_box = style; + break; } lv_obj_invalidate(calendar); @@ -299,6 +326,18 @@ void lv_calendar_set_style(lv_obj_t * calendar, lv_calendar_style_t type, lv_sty * Getter functions *====================*/ +/** + * Get the action of a calendar + * @param calendar pointer to a calendar object + * @return pointer to the action function + */ +lv_action_t lv_calendar_get_action(const lv_obj_t * calendar, lv_calendar_action_t type) +{ + if(type >= LV_CALENDAR_ACTION_NUM) return NULL; + + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + return ext->actions[type]; +} /** * Get the today's date @@ -377,24 +416,24 @@ lv_style_t * lv_calendar_get_style(const lv_obj_t * calendar, lv_calendar_style_ lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); switch(type) { - case LV_CALENDAR_STYLE_BG: - return lv_obj_get_style(calendar); - case LV_CALENDAR_STYLE_HEADER: - return ext->style_header; - case LV_CALENDAR_STYLE_HEADER_PR: - return ext->style_header_pr; - case LV_CALENDAR_STYLE_DAY_NAMES: - return ext->style_day_names; - case LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS: - return ext->style_highlighted_days; - case LV_CALENDAR_STYLE_INACTIVE_DAYS: - return ext->style_inactive_days; - case LV_CALENDAR_STYLE_WEEK_BOX: - return ext->style_week_box; - case LV_CALENDAR_STYLE_TODAY_BOX: - return ext->style_today_box; - default: - return NULL; + case LV_CALENDAR_STYLE_BG: + return lv_obj_get_style(calendar); + case LV_CALENDAR_STYLE_HEADER: + return ext->style_header; + case LV_CALENDAR_STYLE_HEADER_PR: + return ext->style_header_pr; + case LV_CALENDAR_STYLE_DAY_NAMES: + return ext->style_day_names; + case LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS: + return ext->style_highlighted_days; + case LV_CALENDAR_STYLE_INACTIVE_DAYS: + return ext->style_inactive_days; + case LV_CALENDAR_STYLE_WEEK_BOX: + return ext->style_week_box; + case LV_CALENDAR_STYLE_TODAY_BOX: + return ext->style_today_box; + default: + return NULL; } /*To avoid warning*/ @@ -465,11 +504,26 @@ static lv_res_t lv_calendar_signal(lv_obj_t * calendar, lv_signal_t sign, void * if(sign == LV_SIGNAL_CLEANUP) { /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/ + } else if(sign == LV_SIGNAL_PRESSED) { + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + /*Call the press action, 'param' is the caller indev_proc*/ + if(ext->actions[LV_CALENDAR_ACTION_PR]) { + lv_indev_t * indev = lv_indev_get_act(); + lv_point_t p; + lv_indev_get_point(indev, &p); + + if(calculate_touched_day(calendar, &p)){ + if(ext->btn_pressing != 0) lv_obj_invalidate(calendar); + ext->btn_pressing = 0; + res = ext->actions[LV_CALENDAR_ACTION_PR](calendar); + } + } } else if(sign == LV_SIGNAL_PRESSING) { lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); lv_area_t header_area; lv_area_copy(&header_area, &calendar->coords); header_area.y2 = header_area.y1 + get_header_height(calendar); + lv_indev_t * indev = lv_indev_get_act(); lv_point_t p; lv_indev_get_point(indev, &p); @@ -482,14 +536,22 @@ static lv_res_t lv_calendar_signal(lv_obj_t * calendar, lv_signal_t sign, void * if(ext->btn_pressing != 1) lv_obj_invalidate(calendar); ext->btn_pressing = 1; } + + ext->pressed_date.year = 0; + } else if(calculate_touched_day(calendar, &p)) { + if(ext->btn_pressing != 0) lv_obj_invalidate(calendar); + ext->btn_pressing = 0; } else { if(ext->btn_pressing != 0) lv_obj_invalidate(calendar); ext->btn_pressing = 0; + ext->pressed_date.year = 0; } } else if(sign == LV_SIGNAL_PRESS_LOST) { lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + ext->pressed_date.year = 0; ext->btn_pressing = 0; lv_obj_invalidate(calendar); + } else if(sign == LV_SIGNAL_RELEASED) { lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); if(ext->btn_pressing < 0) { @@ -507,10 +569,28 @@ static lv_res_t lv_calendar_signal(lv_obj_t * calendar, lv_signal_t sign, void * ext->showed_date.month ++; } } + else if(ext->pressed_date.year != 0) + { + if(ext->actions[LV_CALENDAR_ACTION_CLICK]) { + res = ext->actions[LV_CALENDAR_ACTION_CLICK](calendar); + } + } + ext->pressed_date.year = 0; ext->btn_pressing = 0; lv_obj_invalidate(calendar); + + } else if(sign == LV_SIGNAL_LONG_PRESS) { + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + if(ext->actions[LV_CALENDAR_ACTION_LONG_PR] && (ext->pressed_date.year != 0)) { + res = ext->actions[LV_CALENDAR_ACTION_LONG_PR](calendar); + } + } else if(sign == LV_SIGNAL_LONG_PRESS_REP) { + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + if(ext->actions[LV_CALENDAR_ACTION_LONG_PR_REPEAT] && (ext->pressed_date.year != 0)) { + res = ext->actions[LV_CALENDAR_ACTION_LONG_PR_REPEAT](calendar); + } } else if(sign == LV_SIGNAL_CONTROLL) { uint8_t c = *((uint8_t *) param); lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); @@ -543,6 +623,60 @@ static lv_res_t lv_calendar_signal(lv_obj_t * calendar, lv_signal_t sign, void * return res; } +/** + * It will check if the days part of calendar is touched + * and if it is, it will calculate the day and put it in pressed_date of calendar object. + * @param calendar pointer to a calendar object + * @param pointer to a point + * @return true: days part of calendar is touched and its related date is put in pressed date + * false: the point is out of days part area. + */ +static bool calculate_touched_day(lv_obj_t * calendar, const lv_point_t * touched_point) +{ + lv_area_t days_area; + lv_area_copy(&days_area, &calendar->coords); + lv_style_t * style_bg = lv_calendar_get_style(calendar, LV_CALENDAR_STYLE_BG); + days_area.x1 += style_bg->body.padding.hor; + days_area.x2 -= style_bg->body.padding.hor; + days_area.y1 = calendar->coords.y1 + get_header_height(calendar) + get_day_names_height(calendar) - style_bg->body.padding.ver; + + if(lv_area_is_point_on(&days_area, touched_point)) { + lv_coord_t w = (days_area.x2 - days_area.x1 + 1) / 7; + lv_coord_t h = (days_area.y2 - days_area.y1 + 1) / 6; + uint8_t x_pos = 0; + x_pos = (touched_point->x - days_area.x1) / w; + if(x_pos > 6) x_pos = 6; + uint8_t y_pos = 0; + y_pos = (touched_point->y - days_area.y1) / h; + if(y_pos > 5) y_pos = 5; + + uint8_t i_pos = 0; + i_pos = (y_pos * 7) + x_pos; + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + if(i_pos < get_day_of_week(ext->showed_date.year, ext->showed_date.month, 1) ) { + ext->pressed_date.year = ext->showed_date.year - (ext->showed_date.month == 1 ? 1 : 0); + ext->pressed_date.month = ext->showed_date.month == 1 ? 12 : (ext->showed_date.month - 1); + ext->pressed_date.day = get_month_length(ext->pressed_date.year, ext->pressed_date.month) - + get_day_of_week(ext->showed_date.year, ext->showed_date.month, 1) + 1 + i_pos; + } + else if(i_pos < (get_day_of_week(ext->showed_date.year, ext->showed_date.month, 1) + + get_month_length(ext->showed_date.year, ext->showed_date.month))) { + ext->pressed_date.year = ext->showed_date.year; + ext->pressed_date.month = ext->showed_date.month; + ext->pressed_date.day = i_pos + 1 - get_day_of_week(ext->showed_date.year, ext->showed_date.month, 1); + } + else if(i_pos < 42) { + ext->pressed_date.year = ext->showed_date.year + (ext->showed_date.month == 12 ? 1 : 0); + ext->pressed_date.month = ext->showed_date.month == 12 ? 1 : (ext->showed_date.month + 1); + ext->pressed_date.day = i_pos + 1 - get_day_of_week(ext->showed_date.year, ext->showed_date.month, 1) + - get_month_length(ext->showed_date.year, ext->showed_date.month); + } + return true; + }else { + return false; + } +} + /** * Get the height of a calendar's header based on it's style * @param calendar point to a calendar @@ -555,6 +689,18 @@ static lv_coord_t get_header_height(lv_obj_t * calendar) return lv_font_get_height(ext->style_header->text.font) + ext->style_header->body.padding.ver * 2; } +/** + * Get the height of a calendar's day_names based on it's style + * @param calendar point to a calendar + * @return the day_names's height + */ +static lv_coord_t get_day_names_height(lv_obj_t * calendar) +{ + lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar); + + return lv_font_get_height(ext->style_day_names->text.font) + ext->style_day_names->body.padding.ver * 2; +} + /** * Draw the calendar header with month name and arrows * @param calendar point to a calendar @@ -590,8 +736,8 @@ static void draw_header(lv_obj_t * calendar, const lv_area_t * mask) /*Add the right arrow*/ arrow_style = ext->btn_pressing > 0 ? ext->style_header_pr : ext->style_header; header_area.x1 = header_area.x2 - ext->style_header->body.padding.hor - - lv_txt_get_width(SYMBOL_RIGHT, 1, arrow_style->text.font, - arrow_style->text.line_space, LV_TXT_FLAG_NONE); + lv_txt_get_width(SYMBOL_RIGHT, 1, arrow_style->text.font, + arrow_style->text.line_space, LV_TXT_FLAG_NONE); lv_draw_label(&header_area, mask, arrow_style, opa_scale, SYMBOL_RIGHT, LV_TXT_FLAG_NONE, NULL); } @@ -634,8 +780,8 @@ static void draw_days(lv_obj_t * calendar, const lv_area_t * mask) lv_area_t label_area; lv_opa_t opa_scale = lv_obj_get_opa_scale(calendar); label_area.y1 = calendar->coords.y1 + get_header_height(calendar) + - ext->style_day_names->body.padding.ver + lv_font_get_height(ext->style_day_names->text.font) + - style_bg->body.padding.inner; + ext->style_day_names->body.padding.ver + lv_font_get_height(ext->style_day_names->text.font) + + style_bg->body.padding.inner; label_area.y2 = label_area.y1 + lv_font_get_height(style_bg->text.font); lv_coord_t w = lv_obj_get_width(calendar) - 2 * hpad; @@ -677,7 +823,7 @@ static void draw_days(lv_obj_t * calendar, const lv_area_t * mask) /*Draw the "week box"*/ if(month_of_today_shown && ((draw_state == DAY_DRAW_ACT_MONTH && ext->today.day >= day_cnt && ext->today.day < day_cnt + 7) || - (draw_state == DAY_DRAW_PREV_MONTH && ext->today.day <= 7 - month_start_day && week == 0))) { + (draw_state == DAY_DRAW_PREV_MONTH && ext->today.day <= 7 - month_start_day && week == 0))) { lv_area_t week_box_area; lv_area_copy(&week_box_area, &label_area); /*'label_area' is already set for this row*/ week_box_area.x1 = calendar->coords.x1 + style_bg->body.padding.hor - ext->style_week_box->body.padding.hor; @@ -728,18 +874,18 @@ static void draw_days(lv_obj_t * calendar, const lv_area_t * mask) lv_style_t * final_style; if(draw_state == DAY_DRAW_PREV_MONTH && is_highlighted(calendar, ext->showed_date.year - (ext->showed_date.month == 1 ? 1 : 0), - ext->showed_date.month == 1 ? 12 : ext->showed_date.month - 1, - day_cnt)) { + ext->showed_date.month == 1 ? 12 : ext->showed_date.month - 1, + day_cnt)) { final_style = ext->style_highlighted_days; } else if(draw_state == DAY_DRAW_ACT_MONTH && - is_highlighted(calendar, ext->showed_date.year, - ext->showed_date.month, - day_cnt)) { + is_highlighted(calendar, ext->showed_date.year, + ext->showed_date.month, + day_cnt)) { final_style = ext->style_highlighted_days; } else if(draw_state == DAY_DRAW_NEXT_MONTH && - is_highlighted(calendar, ext->showed_date.year + (ext->showed_date.month == 12 ? 1 : 0), - ext->showed_date.month == 12 ? 1 : ext->showed_date.month + 1, - day_cnt)) { + is_highlighted(calendar, ext->showed_date.year + (ext->showed_date.month == 12 ? 1 : 0), + ext->showed_date.month == 12 ? 1 : ext->showed_date.month + 1, + day_cnt)) { final_style = ext->style_highlighted_days; } else if(month_of_today_shown && day_cnt == ext->today.day && draw_state == DAY_DRAW_ACT_MONTH) final_style = ext->style_today_box; else if(in_week_box && draw_state == DAY_DRAW_ACT_MONTH) final_style = ext->style_week_box; @@ -863,7 +1009,7 @@ static uint8_t get_day_of_week(uint32_t year, uint32_t month, uint32_t day) uint32_t b = year - a; uint32_t day_of_week = (day + (31 * (month - 2 + 12 * a) / 12) + - b + (b / 4) - (b / 100) + (b / 400)) % 7; + b + (b / 4) - (b / 100) + (b / 400)) % 7; return day_of_week; } diff --git a/lv_objx/lv_calendar.h b/lv_objx/lv_calendar.h index 537f6e8f46..2b1d4984c7 100644 --- a/lv_objx/lv_calendar.h +++ b/lv_objx/lv_calendar.h @@ -37,6 +37,16 @@ typedef struct { int8_t day; } lv_calendar_date_t; +enum +{ + LV_CALENDAR_ACTION_CLICK, + LV_CALENDAR_ACTION_PR, + LV_CALENDAR_ACTION_LONG_PR, + LV_CALENDAR_ACTION_LONG_PR_REPEAT, + LV_CALENDAR_ACTION_NUM, +}; +typedef uint8_t lv_calendar_action_t; + /*Data of calendar*/ typedef struct { /*None*/ /*Ext. of ancestor*/ @@ -46,8 +56,10 @@ typedef struct { lv_calendar_date_t * highlighted_dates; /*Apply different style on these days (pointer to an array defined by the user)*/ uint8_t highlighted_dates_num; /*Number of elements in `highlighted_days`*/ int8_t btn_pressing; /*-1: prev month pressing, +1 next month pressing on the header*/ + lv_calendar_date_t pressed_date; const char ** day_names; /*Pointer to an array with the name of the days (NULL: use default names)*/ const char ** month_names; /*Pointer to an array with the name of the month (NULL. use default names)*/ + lv_action_t actions[LV_CALENDAR_ACTION_NUM]; /*Styles*/ lv_style_t * style_header; @@ -74,6 +86,7 @@ typedef uint8_t lv_calendar_style_t; + /********************** * GLOBAL PROTOTYPES **********************/