merge calendar
This commit is contained in:
@@ -203,6 +203,9 @@
|
|||||||
#define LV_TA_PWD_SHOW_TIME 1500 /*ms*/
|
#define LV_TA_PWD_SHOW_TIME 1500 /*ms*/
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*Calendar (dependencies: -)*/
|
||||||
|
#define USE_LV_CALENDAR 1
|
||||||
|
|
||||||
/*************************
|
/*************************
|
||||||
* User input objects
|
* User input objects
|
||||||
*************************/
|
*************************/
|
||||||
|
|||||||
@@ -119,4 +119,3 @@ static void (*map_fp)(const lv_area_t * cords_p, const lv_area_t * mask_p,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,6 @@
|
|||||||
/*********************
|
/*********************
|
||||||
* DEFINES
|
* DEFINES
|
||||||
*********************/
|
*********************/
|
||||||
#define LINE_WIDTH_CORR_BASE 64
|
|
||||||
#define LINE_WIDTH_CORR_SHIFT 6
|
|
||||||
#if LV_COMPILER_VLA_SUPPORTED == 0
|
#if LV_COMPILER_VLA_SUPPORTED == 0
|
||||||
#define LINE_MAX_WIDTH 64
|
#define LINE_MAX_WIDTH 64
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -482,6 +482,46 @@ static void lv_draw_rect_border_straight(const lv_area_t * coords, const lv_area
|
|||||||
corner_size = radius + LV_ANTIALIAS;
|
corner_size = radius + LV_ANTIALIAS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*If radius == 0 is a special case*/
|
||||||
|
if(style->body.radius == 0) {
|
||||||
|
/*Left top corner*/
|
||||||
|
if(part & LV_BORDER_TOP) {
|
||||||
|
work_area.x1 = coords->x1;
|
||||||
|
work_area.x2 = coords->x2;
|
||||||
|
work_area.y1 = coords->y1;
|
||||||
|
work_area.y2 = coords->y1 + bwidth;
|
||||||
|
fill_fp(&work_area, mask, color, opa);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Right top corner*/
|
||||||
|
if(part & LV_BORDER_RIGHT) {
|
||||||
|
work_area.x1 = coords->x2 - bwidth;
|
||||||
|
work_area.x2 = coords->x2;
|
||||||
|
work_area.y1 = coords->y1 + (part & LV_BORDER_TOP ? bwidth + 1 : 0);
|
||||||
|
work_area.y2 = coords->y2 - (part & LV_BORDER_BOTTOM ? bwidth + 1 : 0);
|
||||||
|
fill_fp(&work_area, mask, color, opa);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Left bottom corner*/
|
||||||
|
if(part & LV_BORDER_LEFT) {
|
||||||
|
work_area.x1 = coords->x1;
|
||||||
|
work_area.x2 = coords->x1 + bwidth;
|
||||||
|
work_area.y1 = coords->y1 + (part & LV_BORDER_TOP ? bwidth + 1 : 0);
|
||||||
|
work_area.y2 = coords->y2 - (part & LV_BORDER_BOTTOM ? bwidth + 1 : 0);
|
||||||
|
fill_fp(&work_area, mask, color, opa);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Right bottom corner*/
|
||||||
|
if(part & LV_BORDER_BOTTOM) {
|
||||||
|
work_area.x1 = coords->x1;
|
||||||
|
work_area.x2 = coords->x2;
|
||||||
|
work_area.y1 = coords->y2 - bwidth;
|
||||||
|
work_area.y2 = coords->y2;
|
||||||
|
fill_fp(&work_area, mask, color, opa);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Modify the corner_size if corner is drawn */
|
/* Modify the corner_size if corner is drawn */
|
||||||
corner_size ++;
|
corner_size ++;
|
||||||
|
|
||||||
|
|||||||
@@ -213,7 +213,7 @@ const uint8_t * lv_font_get_bitmap(const lv_font_t * font_p, uint32_t letter)
|
|||||||
/**
|
/**
|
||||||
* Get the width of a letter in a font
|
* Get the width of a letter in a font
|
||||||
* @param font_p pointer to a font
|
* @param font_p pointer to a font
|
||||||
* @param letter a letter
|
* @param letter an UNICODE character code
|
||||||
* @return the width of a letter
|
* @return the width of a letter
|
||||||
*/
|
*/
|
||||||
uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter)
|
uint8_t lv_font_get_width(const lv_font_t * font_p, uint32_t letter)
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font,
|
|||||||
/**
|
/**
|
||||||
* Give the length of a text with a given font
|
* Give the length of a text with a given font
|
||||||
* @param txt a '\0' terminate string
|
* @param txt a '\0' terminate string
|
||||||
* @param length length of 'txt' in bytes
|
* @param length length of 'txt' in character count and not bytes(UTF-8 can be 1,2,3 or 4 bytes long)
|
||||||
* @param font pointer to a font
|
* @param font pointer to a font
|
||||||
* @param letter_space letter space
|
* @param letter_space letter space
|
||||||
* @param flags settings for the text from 'txt_flag_t' enum
|
* @param flags settings for the text from 'txt_flag_t' enum
|
||||||
@@ -199,6 +199,8 @@ lv_coord_t lv_txt_get_width(const char * txt, uint16_t length,
|
|||||||
width += letter_space;
|
width += letter_space;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
width -= letter_space; /*Trim the last letter space because there is no letter after it*/
|
||||||
|
|
||||||
/*Trim closing spaces. Important when the text is aligned to the middle */
|
/*Trim closing spaces. Important when the text is aligned to the middle */
|
||||||
for(i = length - 1; i > 0; i--) {
|
for(i = length - 1; i > 0; i--) {
|
||||||
if(txt[i] == ' ') {
|
if(txt[i] == ' ') {
|
||||||
|
|||||||
@@ -74,14 +74,14 @@ uint16_t lv_txt_get_next_line(const char * txt, const lv_font_t * font_p,
|
|||||||
/**
|
/**
|
||||||
* Give the length of a text with a given font
|
* Give the length of a text with a given font
|
||||||
* @param txt a '\0' terminate string
|
* @param txt a '\0' terminate string
|
||||||
* @param char_num number of characters in 'txt'
|
* @param length length of 'txt' in character count and not bytes(UTF-8 can be 1,2,3 or 4 bytes long)
|
||||||
* @param font_p pointer to a font
|
* @param font pointer to a font
|
||||||
* @param letter_space letter space
|
* @param letter_space letter space
|
||||||
* @param flags settings for the text from 'txt_flag_t' enum
|
* @param flags settings for the text from 'txt_flag_t' enum
|
||||||
* @return length of a char_num long text
|
* @return length of a char_num long text
|
||||||
*/
|
*/
|
||||||
lv_coord_t lv_txt_get_width(const char * txt, uint16_t char_num,
|
lv_coord_t lv_txt_get_width(const char * txt, uint16_t length,
|
||||||
const lv_font_t * font_p, lv_coord_t letter_space, lv_txt_flag_t flag);
|
const lv_font_t * font, lv_coord_t letter_space, lv_txt_flag_t flag);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check next character in a string and decide if te character is part of the command or not
|
* Check next character in a string and decide if te character is part of the command or not
|
||||||
|
|||||||
841
lv_objx/lv_calendar.c
Normal file
841
lv_objx/lv_calendar.c
Normal file
@@ -0,0 +1,841 @@
|
|||||||
|
/**
|
||||||
|
* @file lv_calendar.c
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* INCLUDES
|
||||||
|
*********************/
|
||||||
|
#include "../../lv_conf.h"
|
||||||
|
#if USE_LV_CALENDAR != 0
|
||||||
|
|
||||||
|
#include "lv_calendar.h"
|
||||||
|
#include "../lv_draw/lv_draw.h"
|
||||||
|
#include "../lv_hal/lv_hal_indev.h"
|
||||||
|
#include "../lv_misc/lv_math.h"
|
||||||
|
#include "../lv_core/lv_indev.h"
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* DEFINES
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* TYPEDEFS
|
||||||
|
**********************/
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
DAY_DRAW_PREV_MONTH,
|
||||||
|
DAY_DRAW_ACT_MONTH,
|
||||||
|
DAY_DRAW_NEXT_MONTH,
|
||||||
|
}day_draw_state_t;
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* STATIC PROTOTYPES
|
||||||
|
**********************/
|
||||||
|
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 lv_coord_t get_header_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);
|
||||||
|
static uint8_t get_day_of_week(uint32_t year, uint32_t month, uint32_t day);
|
||||||
|
static bool is_highlighted(lv_obj_t * calendar, int32_t year, int32_t month, int32_t day);
|
||||||
|
static const char * get_day_name(lv_obj_t * calendar,uint8_t day);
|
||||||
|
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
|
||||||
|
**********************/
|
||||||
|
static lv_signal_func_t ancestor_signal;
|
||||||
|
static lv_design_func_t ancestor_design;
|
||||||
|
static const char * day_name[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
||||||
|
static const char * month_name[12] = {"January", "February", "March", "April",
|
||||||
|
"May", "June", "July", "August",
|
||||||
|
"September", "October", "November", "December"};
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* MACROS
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* GLOBAL FUNCTIONS
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a calendar object
|
||||||
|
* @param par pointer to an object, it will be the parent of the new calendar
|
||||||
|
* @param copy pointer to a calendar object, if not NULL then the new object will be copied from it
|
||||||
|
* @return pointer to the created calendar
|
||||||
|
*/
|
||||||
|
lv_obj_t * lv_calendar_create(lv_obj_t * par, lv_obj_t * copy)
|
||||||
|
{
|
||||||
|
/*Create the ancestor of calendar*/
|
||||||
|
lv_obj_t * new_calendar = lv_obj_create(par, copy);
|
||||||
|
lv_mem_assert(new_calendar);
|
||||||
|
|
||||||
|
/*Allocate the calendar type specific extended data*/
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_allocate_ext_attr(new_calendar, sizeof(lv_calendar_ext_t));
|
||||||
|
lv_mem_assert(ext);
|
||||||
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_calendar);
|
||||||
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_calendar);
|
||||||
|
|
||||||
|
/*Initialize the allocated 'ext' */
|
||||||
|
ext->today.year = 2018;
|
||||||
|
ext->today.month= 1;
|
||||||
|
ext->today.day= 1;
|
||||||
|
|
||||||
|
ext->showed_date.year = 2018;
|
||||||
|
ext->showed_date.month = 1;
|
||||||
|
ext->showed_date.day= 1;
|
||||||
|
|
||||||
|
ext->highlighted_dates = NULL;
|
||||||
|
ext->highlighted_dates_num = 0;
|
||||||
|
ext->day_names = NULL;
|
||||||
|
ext->month_names = NULL;
|
||||||
|
ext->style_header = &lv_style_plain_color;
|
||||||
|
ext->style_header_pr = &lv_style_pretty_color;
|
||||||
|
ext->style_highlighted = &lv_style_plain_color;
|
||||||
|
ext->style_inactive_days = &lv_style_btn_ina;
|
||||||
|
ext->style_week_box = &lv_style_plain_color;
|
||||||
|
ext->style_today_box = &lv_style_pretty_color;
|
||||||
|
ext->style_day_names = &lv_style_pretty;
|
||||||
|
|
||||||
|
/*The signal and design functions are not copied so set them here*/
|
||||||
|
lv_obj_set_signal_func(new_calendar, lv_calendar_signal);
|
||||||
|
lv_obj_set_design_func(new_calendar, lv_calendar_design);
|
||||||
|
|
||||||
|
/*Init the new calendar calendar*/
|
||||||
|
if(copy == NULL) {
|
||||||
|
lv_obj_set_size(new_calendar, LV_DPI * 2, LV_DPI * 2);
|
||||||
|
lv_obj_set_style(new_calendar, &lv_style_pretty);
|
||||||
|
|
||||||
|
}
|
||||||
|
/*Copy an existing calendar*/
|
||||||
|
else {
|
||||||
|
lv_calendar_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
|
||||||
|
ext->today.year = copy_ext->today.year;
|
||||||
|
ext->today.month= copy_ext->today.month;
|
||||||
|
ext->today.day= copy_ext->today.day;
|
||||||
|
|
||||||
|
ext->showed_date.year = copy_ext->showed_date.year;
|
||||||
|
ext->showed_date.month = copy_ext->showed_date.month;
|
||||||
|
ext->showed_date.day= copy_ext->showed_date.day;
|
||||||
|
|
||||||
|
ext->highlighted_dates = copy_ext->highlighted_dates;
|
||||||
|
ext->highlighted_dates_num = copy_ext->highlighted_dates_num;
|
||||||
|
ext->day_names = copy_ext->day_names;
|
||||||
|
ext->month_names = copy_ext->month_names;
|
||||||
|
ext->style_header = copy_ext->style_header;
|
||||||
|
ext->style_header_pr = copy_ext->style_header_pr;
|
||||||
|
ext->style_highlighted = copy_ext->style_highlighted;
|
||||||
|
ext->style_inactive_days = copy_ext->style_inactive_days;
|
||||||
|
ext->style_week_box = copy_ext->style_week_box;
|
||||||
|
ext->style_today_box = copy_ext->style_today_box;
|
||||||
|
ext->style_day_names = copy_ext->style_day_names;
|
||||||
|
/*Refresh the style with new signal function*/
|
||||||
|
lv_obj_refresh_style(new_calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_calendar;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*======================
|
||||||
|
* Add/remove functions
|
||||||
|
*=====================*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* New object specific "add" or "remove" functions come here
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*=====================
|
||||||
|
* Setter functions
|
||||||
|
*====================*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the today's date
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param today pointer to an `lv_calendar_date_t` variable containing the date of today. The value will be saved it can be local variable too.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_today_date(lv_obj_t * calendar, lv_calendar_date_t * today)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
ext->today.year = today->year;
|
||||||
|
ext->today.month = today->month;
|
||||||
|
ext->today.day = today->day;
|
||||||
|
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the currently showed
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param showed pointer to an `lv_calendar_date_t` variable containing the date to show. The value will be saved it can be local variable too.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_showed_date(lv_obj_t * calendar, lv_calendar_date_t * showed)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
ext->showed_date.year = showed->year;
|
||||||
|
ext->showed_date.month = showed->month;
|
||||||
|
ext->showed_date.day = showed->day;
|
||||||
|
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the the highlighted dates
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param highlighted pointer to an `lv_calendar_date_t` array containing the dates. ONLY A POINTER WILL BE SAVED! CAN'T BE LOCAL ARRAY.
|
||||||
|
* @param date_num number of dates in the array
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_highlighted_dates(lv_obj_t * calendar, lv_calendar_date_t * highlighted, uint16_t date_num)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
ext->highlighted_dates = highlighted;
|
||||||
|
ext->highlighted_dates_num = date_num;
|
||||||
|
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the name of the days
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param day_names pointer to an array with the names. E.g. `const char * days[7] = {"Sun", "Mon", ...}`
|
||||||
|
* Only the pointer will be saved so this variable can't be local which will be destroyed later.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_day_names(lv_obj_t * calendar, const char ** day_names)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
ext->day_names = day_names;
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the name of the month
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param day_names pointer to an array with the names. E.g. `const char * days[12] = {"Jan", "Feb", ...}`
|
||||||
|
* Only the pointer will be saved so this variable can't be local which will be destroyed later.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_month_names(lv_obj_t * calendar, const char ** day_names)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
ext->month_names = day_names;
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a style of a calendar.
|
||||||
|
* @param calendar pointer to calendar object
|
||||||
|
* @param type which style should be set
|
||||||
|
* @param style pointer to a style
|
||||||
|
* */
|
||||||
|
void lv_calendar_set_style(lv_obj_t * calendar, lv_calendar_style_t type, lv_style_t *style)
|
||||||
|
{
|
||||||
|
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 = 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*=====================
|
||||||
|
* Getter functions
|
||||||
|
*====================*/
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the today's date
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return return pointer to an `lv_calendar_date_t` variable containing the date of today.
|
||||||
|
*/
|
||||||
|
lv_calendar_date_t * lv_calendar_get_today_date(lv_obj_t * calendar)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
return &ext->today;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the currently showed
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to an `lv_calendar_date_t` variable containing the date is being shown.
|
||||||
|
*/
|
||||||
|
lv_calendar_date_t * lv_calendar_get_showed_date(lv_obj_t * calendar)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
return &ext->showed_date;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the the highlighted dates
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to an `lv_calendar_date_t` array containing the dates.
|
||||||
|
*/
|
||||||
|
lv_calendar_date_t * lv_calendar_get_highlighted_dates(lv_obj_t * calendar)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
return ext->highlighted_dates;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of the highlighted dates
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return number of highlighted days
|
||||||
|
*/
|
||||||
|
uint16_t lv_calendar_get_highlighted_dates_num(lv_obj_t * calendar)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
return ext->highlighted_dates_num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the days
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to the array of day names
|
||||||
|
*/
|
||||||
|
const char ** lv_calendar_get_day_names(lv_obj_t * calendar, const char ** day_names)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
return ext->day_names;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the month
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to the array of month names
|
||||||
|
*/
|
||||||
|
const char ** lv_calendar_get_month_names(lv_obj_t * calendar, const char ** day_names)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t *ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
return ext->month_names;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get style of a calendar.
|
||||||
|
* @param calendar pointer to calendar object
|
||||||
|
* @param type which style should be get
|
||||||
|
* @return style pointer to the style
|
||||||
|
* */
|
||||||
|
lv_style_t * lv_calendar_get_style(lv_obj_t * calendar, lv_calendar_style_t type)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
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*/
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*=====================
|
||||||
|
* Other functions
|
||||||
|
*====================*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* New object specific "other" functions come here
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* STATIC FUNCTIONS
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the drawing related tasks of the calendars
|
||||||
|
* @param calendar pointer to an object
|
||||||
|
* @param mask the object will be drawn only in this area
|
||||||
|
* @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
|
||||||
|
* (return 'true' if yes)
|
||||||
|
* LV_DESIGN_DRAW: draw the object (always return 'true')
|
||||||
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
||||||
|
* @param return true/false, depends on 'mode'
|
||||||
|
*/
|
||||||
|
static bool lv_calendar_design(lv_obj_t * calendar, const lv_area_t * mask, lv_design_mode_t mode)
|
||||||
|
{
|
||||||
|
/*Return false if the object is not covers the mask_p area*/
|
||||||
|
if(mode == LV_DESIGN_COVER_CHK) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
/*Draw the object*/
|
||||||
|
else if(mode == LV_DESIGN_DRAW_MAIN) {
|
||||||
|
lv_draw_rect(&calendar->coords, mask, lv_calendar_get_style(calendar, LV_CALENDAR_STYLE_BG));
|
||||||
|
|
||||||
|
draw_header(calendar, mask);
|
||||||
|
draw_day_names(calendar, mask);
|
||||||
|
draw_days(calendar, mask);
|
||||||
|
|
||||||
|
}
|
||||||
|
/*Post draw when the children are drawn*/
|
||||||
|
else if(mode == LV_DESIGN_DRAW_POST) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signal function of the calendar
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param sign a signal type from lv_signal_t enum
|
||||||
|
* @param param pointer to a signal specific variable
|
||||||
|
* @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
|
||||||
|
*/
|
||||||
|
static lv_res_t lv_calendar_signal(lv_obj_t * calendar, lv_signal_t sign, void * param)
|
||||||
|
{
|
||||||
|
lv_res_t res;
|
||||||
|
|
||||||
|
/* Include the ancient signal function */
|
||||||
|
res = ancestor_signal(calendar, sign, param);
|
||||||
|
if(res != LV_RES_OK) return res;
|
||||||
|
|
||||||
|
|
||||||
|
if(sign == LV_SIGNAL_CLEANUP) {
|
||||||
|
/*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
if(lv_area_is_point_on(&header_area, &p)) {
|
||||||
|
if(p.x < header_area.x1 + lv_area_get_width(&header_area) / 2) {
|
||||||
|
if(ext->btn_pressing != -1) lv_obj_invalidate(calendar);
|
||||||
|
ext->btn_pressing = -1;
|
||||||
|
} else {
|
||||||
|
if(ext->btn_pressing != 1) lv_obj_invalidate(calendar);
|
||||||
|
ext->btn_pressing = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(ext->btn_pressing != 0) lv_obj_invalidate(calendar);
|
||||||
|
ext->btn_pressing = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(sign == LV_SIGNAL_PRESS_LOST) {
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
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) {
|
||||||
|
if(ext->showed_date.month <= 1) {
|
||||||
|
ext->showed_date.month = 12;
|
||||||
|
ext->showed_date.year --;
|
||||||
|
} else {
|
||||||
|
ext->showed_date.month --;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(ext->btn_pressing > 0) {
|
||||||
|
if(ext->showed_date.month >= 12) {
|
||||||
|
ext->showed_date.month = 1;
|
||||||
|
ext->showed_date.year ++;
|
||||||
|
} else {
|
||||||
|
ext->showed_date.month ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ext->btn_pressing = 0;
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(sign == LV_SIGNAL_CONTROLL) {
|
||||||
|
uint8_t c = *((uint8_t*) param);
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
if(c == LV_GROUP_KEY_RIGHT || c == LV_GROUP_KEY_UP) {
|
||||||
|
if(ext->showed_date.month >= 12) {
|
||||||
|
ext->showed_date.month = 1;
|
||||||
|
ext->showed_date.year ++;
|
||||||
|
} else {
|
||||||
|
ext->showed_date.month ++;
|
||||||
|
}
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
else if(c == LV_GROUP_KEY_LEFT || c == LV_GROUP_KEY_DOWN) {
|
||||||
|
if(ext->showed_date.month <= 1) {
|
||||||
|
ext->showed_date.month = 12;
|
||||||
|
ext->showed_date.year --;
|
||||||
|
} else {
|
||||||
|
ext->showed_date.month --;
|
||||||
|
}
|
||||||
|
lv_obj_invalidate(calendar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(sign == LV_SIGNAL_GET_TYPE) {
|
||||||
|
lv_obj_type_t * buf = param;
|
||||||
|
uint8_t i;
|
||||||
|
for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set date*/
|
||||||
|
if(buf->type[i] == NULL) break;
|
||||||
|
}
|
||||||
|
buf->type[i] = "lv_calendar";
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the height of a calendar's header based on it's style
|
||||||
|
* @param calendar point to a calendar
|
||||||
|
* @return the header's height
|
||||||
|
*/
|
||||||
|
static lv_coord_t get_header_height(lv_obj_t * calendar)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
|
||||||
|
return lv_font_get_height(ext->style_header->text.font) + ext->style_header->body.padding.ver * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the calendar header with month name and arrows
|
||||||
|
* @param calendar point to a calendar
|
||||||
|
* @param mask a mask for drawing
|
||||||
|
*/
|
||||||
|
static void draw_header(lv_obj_t * calendar, const lv_area_t * mask)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
|
||||||
|
lv_area_t header_area;
|
||||||
|
header_area.x1 = calendar->coords.x1;
|
||||||
|
header_area.x2 = calendar->coords.x2;
|
||||||
|
header_area.y1 = calendar->coords.y1;
|
||||||
|
header_area.y2 = calendar->coords.y1 + get_header_height(calendar);
|
||||||
|
|
||||||
|
lv_draw_rect(&header_area, mask, ext->style_header);
|
||||||
|
|
||||||
|
/*Add the month name*/
|
||||||
|
header_area.y1 += ext->style_header->body.padding.ver;
|
||||||
|
lv_draw_label(&header_area, mask, ext->style_header, get_month_name(calendar, ext->showed_date.month), LV_TXT_FLAG_CENTER, NULL);
|
||||||
|
|
||||||
|
/*Add the left arrow*/
|
||||||
|
lv_style_t * arrow_style = ext->btn_pressing < 0 ? ext->style_header_pr : ext->style_header;
|
||||||
|
header_area.x1 += ext->style_header->body.padding.hor;
|
||||||
|
lv_draw_label(&header_area, mask, arrow_style, SYMBOL_LEFT, LV_TXT_FLAG_NONE, NULL);
|
||||||
|
|
||||||
|
/*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_draw_label(&header_area, mask, arrow_style, SYMBOL_RIGHT, LV_TXT_FLAG_NONE, NULL);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the day's name below the header
|
||||||
|
* @param calendar point to a calendar
|
||||||
|
* @param mask a mask for drawing
|
||||||
|
*/
|
||||||
|
static void draw_day_names(lv_obj_t * calendar, const lv_area_t * mask)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
|
||||||
|
lv_coord_t hpad = ext->style_day_names->body.padding.hor;
|
||||||
|
lv_coord_t w = lv_obj_get_width(calendar) - 2 * hpad;
|
||||||
|
lv_coord_t box_w = w / 7;
|
||||||
|
lv_area_t label_area;
|
||||||
|
label_area.y1 = calendar->coords.y1 + get_header_height(calendar) + ext->style_day_names->body.padding.ver;
|
||||||
|
label_area.y2 = label_area.y1 + lv_font_get_height(ext->style_day_names->text.font);
|
||||||
|
uint32_t i;
|
||||||
|
for(i = 0; i < 7; i++) {
|
||||||
|
label_area.x1 = calendar->coords.x1 + (w * i) / 7 + hpad;
|
||||||
|
label_area.x2 = label_area.x1 + box_w;
|
||||||
|
lv_draw_label(&label_area, mask, ext->style_day_names, get_day_name(calendar, i), LV_TXT_FLAG_CENTER, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Draw the date numbers in a matrix
|
||||||
|
* @param calendar point to a calendar
|
||||||
|
* @param mask a mask for drawing
|
||||||
|
*/
|
||||||
|
static void draw_days(lv_obj_t * calendar, const lv_area_t * mask)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
lv_style_t * style_bg = lv_calendar_get_style(calendar, LV_CALENDAR_STYLE_BG);
|
||||||
|
lv_coord_t hpad = style_bg->body.padding.hor;
|
||||||
|
lv_area_t label_area;
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
lv_coord_t h = calendar->coords.y2 - label_area.y1 - style_bg->body.padding.ver;
|
||||||
|
lv_coord_t box_w = w / 7;
|
||||||
|
lv_coord_t vert_space = (h - (6 * lv_font_get_height(style_bg->text.font))) / 5;
|
||||||
|
|
||||||
|
uint32_t week;
|
||||||
|
uint8_t day_cnt;
|
||||||
|
uint8_t month_start_day = get_day_of_week(ext->showed_date.year, ext->showed_date.month, 1);
|
||||||
|
day_draw_state_t draw_state; /*true: Not the prev. or next month is drawn*/
|
||||||
|
lv_style_t * act_style;
|
||||||
|
|
||||||
|
/*If starting with the first day of the week then the previous month is not visible*/
|
||||||
|
if(month_start_day == 0) {
|
||||||
|
day_cnt = 1;
|
||||||
|
draw_state = DAY_DRAW_ACT_MONTH;
|
||||||
|
act_style = style_bg;
|
||||||
|
} else {
|
||||||
|
draw_state = DAY_DRAW_PREV_MONTH;
|
||||||
|
day_cnt = get_month_length(ext->showed_date.year, ext->showed_date.month - 1); /*Length of the previous month*/
|
||||||
|
day_cnt -= month_start_day - 1; /*First visible number of the previous month*/
|
||||||
|
act_style = ext->style_inactive_days;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool month_of_today_shown = false;
|
||||||
|
if(ext->showed_date.year == ext->today.year &&
|
||||||
|
ext->showed_date.month == ext->today.month)
|
||||||
|
{
|
||||||
|
month_of_today_shown = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
char buf[3];
|
||||||
|
bool in_week_box = false;
|
||||||
|
|
||||||
|
/*Draw 6 weeks*/
|
||||||
|
for(week = 0; week < 6; week++) {
|
||||||
|
|
||||||
|
/*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)))
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
week_box_area.x2 = calendar->coords.x2 - style_bg->body.padding.hor + ext->style_week_box->body.padding.hor;
|
||||||
|
|
||||||
|
week_box_area.y1 -= ext->style_week_box->body.padding.ver;
|
||||||
|
week_box_area.y2 += ext->style_week_box->body.padding.ver;
|
||||||
|
lv_draw_rect(&week_box_area, mask, ext->style_week_box);
|
||||||
|
|
||||||
|
in_week_box = true;
|
||||||
|
} else {
|
||||||
|
in_week_box = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Draw the 7 days of a week*/
|
||||||
|
uint32_t day;
|
||||||
|
for(day = 0; day < 7; day++) {
|
||||||
|
/*The previous month is over*/
|
||||||
|
if(draw_state == DAY_DRAW_PREV_MONTH && day == month_start_day) {
|
||||||
|
draw_state = DAY_DRAW_ACT_MONTH;
|
||||||
|
day_cnt = 1;
|
||||||
|
act_style = style_bg;
|
||||||
|
}
|
||||||
|
/*The current month is over*/
|
||||||
|
if(draw_state == DAY_DRAW_ACT_MONTH &&
|
||||||
|
day_cnt > get_month_length(ext->showed_date.year, ext->showed_date.month))
|
||||||
|
{
|
||||||
|
draw_state = DAY_DRAW_NEXT_MONTH;
|
||||||
|
day_cnt = 1;
|
||||||
|
act_style = ext->style_inactive_days;
|
||||||
|
}
|
||||||
|
|
||||||
|
label_area.x1 = calendar->coords.x1 + (w * day) / 7 + hpad;
|
||||||
|
label_area.x2 = label_area.x1 + box_w;
|
||||||
|
|
||||||
|
/*Draw the "today box"*/
|
||||||
|
if(draw_state == DAY_DRAW_ACT_MONTH && month_of_today_shown && ext->today.day == day_cnt) {
|
||||||
|
lv_area_t today_box_area;
|
||||||
|
lv_area_copy(&today_box_area, &label_area);
|
||||||
|
today_box_area.x1 = label_area.x1;
|
||||||
|
today_box_area.x2 = label_area.x2;
|
||||||
|
|
||||||
|
today_box_area.y1 = label_area.y1 - ext->style_today_box->body.padding.ver;
|
||||||
|
today_box_area.y2 = label_area.y2 + ext->style_today_box->body.padding.ver;
|
||||||
|
lv_draw_rect(&today_box_area, mask, ext->style_today_box);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Get the final style : highlighted/week box/today box/normal*/
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
final_style = ext->style_highlighted;
|
||||||
|
}
|
||||||
|
else if (draw_state == DAY_DRAW_ACT_MONTH &&
|
||||||
|
is_highlighted(calendar, ext->showed_date.year,
|
||||||
|
ext->showed_date.month,
|
||||||
|
day_cnt))
|
||||||
|
{
|
||||||
|
final_style = ext->style_highlighted;
|
||||||
|
}
|
||||||
|
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))
|
||||||
|
{
|
||||||
|
final_style = ext->style_highlighted;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
else final_style = act_style;
|
||||||
|
|
||||||
|
/*Write the day's number*/
|
||||||
|
lv_math_num_to_str(day_cnt, buf);
|
||||||
|
lv_draw_label(&label_area, mask, final_style, buf, LV_TXT_FLAG_CENTER, NULL);
|
||||||
|
|
||||||
|
/*Go to the next day*/
|
||||||
|
day_cnt ++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*Got to the next weeks row*/
|
||||||
|
label_area.y1 += vert_space + lv_font_get_height(style_bg->text.font);
|
||||||
|
label_area.y2 += vert_space + lv_font_get_height(style_bg->text.font);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check weather a date is highlighted or not
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param year a year
|
||||||
|
* @param month a month [1..12]
|
||||||
|
* @param day a day [1..31]
|
||||||
|
* @return true: highlighted
|
||||||
|
*/
|
||||||
|
static bool is_highlighted(lv_obj_t * calendar, int32_t year, int32_t month, int32_t day)
|
||||||
|
{
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
|
||||||
|
if(ext->highlighted_dates == NULL || ext->highlighted_dates_num == 0) return false;
|
||||||
|
|
||||||
|
uint32_t i;
|
||||||
|
for(i = 0; i < ext->highlighted_dates_num; i++) {
|
||||||
|
if(ext->highlighted_dates[i].year == year &&
|
||||||
|
ext->highlighted_dates[i].month == month &&
|
||||||
|
ext->highlighted_dates[i].day == day)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the day name
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param day a day in [0..6]
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
static const char * get_day_name(lv_obj_t * calendar,uint8_t day)
|
||||||
|
{
|
||||||
|
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
if(ext->day_names) return ext->day_names[day];
|
||||||
|
else return day_name[day];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the month name
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param month a month. The range is basically [1..12] but [-11..1] is also supported to handle previous year
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
static const char * get_month_name(lv_obj_t * calendar, int32_t month)
|
||||||
|
{
|
||||||
|
month --; /*Range of months id [1..12] but range of indexes is [0..11]*/
|
||||||
|
if(month < 0) month = 12 + month;
|
||||||
|
|
||||||
|
lv_calendar_ext_t * ext = lv_obj_get_ext_attr(calendar);
|
||||||
|
if(ext->month_names) return ext->month_names[month];
|
||||||
|
else return month_name[month];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of days in a month
|
||||||
|
* @param year a year
|
||||||
|
* @param month a month. The range is basically [1..12] but [-11..1] is also supported to handle previous year
|
||||||
|
* @return [28..31]
|
||||||
|
*/
|
||||||
|
static uint8_t get_month_length(int32_t year, int32_t month)
|
||||||
|
{
|
||||||
|
month --; /*Range of months id [1..12] but range of indexes is [0..11]*/
|
||||||
|
if(month < 0) {
|
||||||
|
year--; /*Already in the previous year (won't be less then -12 to skip a whole year)*/
|
||||||
|
month = 12 + month; /*`month` is negative, the result will be < 12*/
|
||||||
|
}
|
||||||
|
if(month >= 12) {
|
||||||
|
year ++;
|
||||||
|
month -= 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*month == 1 is february*/
|
||||||
|
return (month == 1) ? (28 + is_leap_year(year)) : 31 - month % 7 % 2;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells whether a year is leap year or not
|
||||||
|
* @param year a year
|
||||||
|
* @return 0: not leap year; 1: leap year
|
||||||
|
*/
|
||||||
|
static uint8_t is_leap_year(uint32_t year)
|
||||||
|
{
|
||||||
|
return (year % 4) || ((year % 100 == 0) && (year % 400)) ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the day of the week
|
||||||
|
* @param year a year
|
||||||
|
* @param month a month
|
||||||
|
* @param day a day
|
||||||
|
* @return [0..6] which means [Sun..Sat]
|
||||||
|
*/
|
||||||
|
static uint8_t get_day_of_week(uint32_t year, uint32_t month, uint32_t day)
|
||||||
|
{
|
||||||
|
uint32_t a = month < 3 ? 1 : 0;
|
||||||
|
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;
|
||||||
|
|
||||||
|
return day_of_week;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
207
lv_objx/lv_calendar.h
Normal file
207
lv_objx/lv_calendar.h
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
/**
|
||||||
|
* @file lv_calendar.h
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LV_CALENDAR_H
|
||||||
|
#define LV_CALENDAR_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* INCLUDES
|
||||||
|
*********************/
|
||||||
|
#include "../../lv_conf.h"
|
||||||
|
#if USE_LV_CALENDAR != 0
|
||||||
|
|
||||||
|
#include "../lv_core/lv_obj.h"
|
||||||
|
|
||||||
|
/*********************
|
||||||
|
* DEFINES
|
||||||
|
*********************/
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* TYPEDEFS
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint16_t year;
|
||||||
|
int8_t month;
|
||||||
|
int8_t day;
|
||||||
|
}lv_calendar_date_t;
|
||||||
|
|
||||||
|
/*Data of calendar*/
|
||||||
|
typedef struct {
|
||||||
|
/*None*/ /*Ext. of ancestor*/
|
||||||
|
/*New data for this type */
|
||||||
|
lv_calendar_date_t today; /*Date of today*/
|
||||||
|
lv_calendar_date_t showed_date; /*Currently visible month (day is ignored)*/
|
||||||
|
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*/
|
||||||
|
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)*/
|
||||||
|
|
||||||
|
/*Styles*/
|
||||||
|
lv_style_t * style_header;
|
||||||
|
lv_style_t * style_header_pr;
|
||||||
|
lv_style_t * style_day_names;
|
||||||
|
lv_style_t * style_highlighted;
|
||||||
|
lv_style_t * style_inactive_days;
|
||||||
|
lv_style_t * style_week_box;
|
||||||
|
lv_style_t * style_today_box;
|
||||||
|
}lv_calendar_ext_t;
|
||||||
|
|
||||||
|
/*Styles*/
|
||||||
|
typedef enum {
|
||||||
|
LV_CALENDAR_STYLE_BG, /*Also the style of the "normal" date numbers*/
|
||||||
|
LV_CALENDAR_STYLE_HEADER,
|
||||||
|
LV_CALENDAR_STYLE_HEADER_PR,
|
||||||
|
LV_CALENDAR_STYLE_DAY_NAMES,
|
||||||
|
LV_CALENDAR_STYLE_HIGHLIGHTED_DAYS,
|
||||||
|
LV_CALENDAR_STYLE_INACTIVE_DAYS,
|
||||||
|
LV_CALENDAR_STYLE_WEEK_BOX,
|
||||||
|
LV_CALENDAR_STYLE_TODAY_BOX,
|
||||||
|
}lv_calendar_style_t;
|
||||||
|
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* GLOBAL PROTOTYPES
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a calendar objects
|
||||||
|
* @param par pointer to an object, it will be the parent of the new calendar
|
||||||
|
* @param copy pointer to a calendar object, if not NULL then the new object will be copied from it
|
||||||
|
* @return pointer to the created calendar
|
||||||
|
*/
|
||||||
|
lv_obj_t * lv_calendar_create(lv_obj_t * par, lv_obj_t * copy);
|
||||||
|
|
||||||
|
/*======================
|
||||||
|
* Add/remove functions
|
||||||
|
*=====================*/
|
||||||
|
|
||||||
|
|
||||||
|
/*=====================
|
||||||
|
* Setter functions
|
||||||
|
*====================*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the today's date
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param today pointer to an `lv_calendar_date_t` variable containing the date of today. The value will be saved it can be local variable too.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_today_date(lv_obj_t * calendar, lv_calendar_date_t * today);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the currently showed
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param showed pointer to an `lv_calendar_date_t` variable containing the date to show. The value will be saved it can be local variable too.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_showed_date(lv_obj_t * calendar, lv_calendar_date_t * showed);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the the highlighted dates
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param highlighted pointer to an `lv_calendar_date_t` array containing the dates. ONLY A POINTER WILL BE SAVED! CAN'T BE LOCAL ARRAY.
|
||||||
|
* @param date_num number of dates in the array
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_highlighted_dates(lv_obj_t * calendar, lv_calendar_date_t * highlighted, uint16_t date_num);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the name of the days
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param day_names pointer to an array with the names. E.g. `const char * days[7] = {"Sun", "Mon", ...}`
|
||||||
|
* Only the pointer will be saved so this variable can't be local which will be destroyed later.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_day_names(lv_obj_t * calendar, const char ** day_names);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the name of the month
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @param day_names pointer to an array with the names. E.g. `const char * days[12] = {"Jan", "Feb", ...}`
|
||||||
|
* Only the pointer will be saved so this variable can't be local which will be destroyed later.
|
||||||
|
*/
|
||||||
|
void lv_calendar_set_month_names(lv_obj_t * calendar, const char ** day_names);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a style of a calendar.
|
||||||
|
* @param calendar pointer to calendar object
|
||||||
|
* @param type which style should be set
|
||||||
|
* @param style pointer to a style
|
||||||
|
* */
|
||||||
|
void lv_calendar_set_style(lv_obj_t * calendar, lv_calendar_style_t type, lv_style_t *style);
|
||||||
|
|
||||||
|
/*=====================
|
||||||
|
* Getter functions
|
||||||
|
*====================*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the today's date
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return return pointer to an `lv_calendar_date_t` variable containing the date of today.
|
||||||
|
*/
|
||||||
|
lv_calendar_date_t * lv_calendar_get_today_date(lv_obj_t * calendar);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the currently showed
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to an `lv_calendar_date_t` variable containing the date is being shown.
|
||||||
|
*/
|
||||||
|
lv_calendar_date_t * lv_calendar_get_showed_date(lv_obj_t * calendar);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the the highlighted dates
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to an `lv_calendar_date_t` array containing the dates.
|
||||||
|
*/
|
||||||
|
lv_calendar_date_t * lv_calendar_get_highlighted_dates(lv_obj_t * calendar);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the number of the highlighted dates
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return number of highlighted days
|
||||||
|
*/
|
||||||
|
uint16_t lv_calendar_get_highlighted_dates_num(lv_obj_t * calendar);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the days
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to the array of day names
|
||||||
|
*/
|
||||||
|
const char ** lv_calendar_get_day_names(lv_obj_t * calendar, const char ** day_names);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the month
|
||||||
|
* @param calendar pointer to a calendar object
|
||||||
|
* @return pointer to the array of month names
|
||||||
|
*/
|
||||||
|
const char ** lv_calendar_get_month_names(lv_obj_t * calendar, const char ** day_names);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get style of a calendar.
|
||||||
|
* @param calendar pointer to calendar object
|
||||||
|
* @param type which style should be get
|
||||||
|
* @return style pointer to the style
|
||||||
|
* */
|
||||||
|
lv_style_t * lv_calendar_get_style(lv_obj_t * calendar, lv_calendar_style_t type);
|
||||||
|
|
||||||
|
/*=====================
|
||||||
|
* Other functions
|
||||||
|
*====================*/
|
||||||
|
|
||||||
|
/**********************
|
||||||
|
* MACROS
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
#endif /*USE_LV_CALENDAR*/
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} /* extern "C" */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /*LV_CALENDAR_H*/
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
/**********************
|
/**********************
|
||||||
* STATIC PROTOTYPES
|
* STATIC PROTOTYPES
|
||||||
**********************/
|
**********************/
|
||||||
static bool lv_templ_design(lv_obj_t * templ, const area_t * mask, lv_design_mode_t mode);
|
static bool lv_templ_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode);
|
||||||
static lv_res_t lv_templ_signal(lv_obj_t * templ, lv_signal_t sign, void * param);
|
static lv_res_t lv_templ_signal(lv_obj_t * templ, lv_signal_t sign, void * param);
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
@@ -57,11 +57,11 @@ lv_obj_t * lv_templ_create(lv_obj_t * par, lv_obj_t * copy)
|
|||||||
/*Create the ancestor of template*/
|
/*Create the ancestor of template*/
|
||||||
/*TODO modify it to the ancestor create function */
|
/*TODO modify it to the ancestor create function */
|
||||||
lv_obj_t * new_templ = lv_ANCESTOR_create(par, copy);
|
lv_obj_t * new_templ = lv_ANCESTOR_create(par, copy);
|
||||||
dm_assert(new_templ);
|
lv_mem_assert(new_templ);
|
||||||
|
|
||||||
/*Allocate the template type specific extended data*/
|
/*Allocate the template type specific extended data*/
|
||||||
lv_templ_ext_t * ext = lv_obj_alloc_ext(new_templ, sizeof(lv_templ_ext_t));
|
lv_templ_ext_t * ext = lv_obj_allocate_ext_attr(new_templ, sizeof(lv_templ_ext_t));
|
||||||
dm_assert(ext);
|
lv_mem_assert(ext);
|
||||||
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_templ);
|
if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_templ);
|
||||||
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_templ);
|
if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_templ);
|
||||||
|
|
||||||
@@ -69,19 +69,19 @@ lv_obj_t * lv_templ_create(lv_obj_t * par, lv_obj_t * copy)
|
|||||||
ext->xyz = 0;
|
ext->xyz = 0;
|
||||||
|
|
||||||
/*The signal and design functions are not copied so set them here*/
|
/*The signal and design functions are not copied so set them here*/
|
||||||
lv_obj_set_signal_f(new_templ, lv_templ_signal);
|
lv_obj_set_signal_func(new_templ, lv_templ_signal);
|
||||||
lv_obj_set_design_f(new_templ, lv_templ_design);
|
lv_obj_set_design_func(new_templ, lv_templ_design);
|
||||||
|
|
||||||
/*Init the new template template*/
|
/*Init the new template template*/
|
||||||
if(copy == NULL) {
|
if(copy == NULL) {
|
||||||
lv_obj_set_style(new_templ, lv_style_get(LV_STYLE_PRETTY, NULL));
|
|
||||||
}
|
}
|
||||||
/*Copy an existing template*/
|
/*Copy an existing template*/
|
||||||
else {
|
else {
|
||||||
lv_templ_ext_t * copy_ext = lv_obj_get_ext(copy);
|
lv_templ_ext_t * copy_ext = lv_obj_get_ext_attr((copy);
|
||||||
|
|
||||||
/*Refresh the style with new signal function*/
|
/*Refresh the style with new signal function*/
|
||||||
lv_obj_refr_style(new_templ);
|
lv_obj_refresh_style(new_templ);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new_templ;
|
return new_templ;
|
||||||
@@ -137,7 +137,7 @@ void lv_templ_set_style(lv_obj_t * templ, lv_templ_style_t type, lv_style_t *sty
|
|||||||
* @param type which style should be get
|
* @param type which style should be get
|
||||||
* @return style pointer to the style
|
* @return style pointer to the style
|
||||||
* */
|
* */
|
||||||
lv_style_t * lv_btn_get_style(lv_obj_t * templ, lv_templ_style_t type)
|
lv_style_t * lv_templ_get_style(lv_obj_t * templ, lv_templ_style_t type)
|
||||||
{
|
{
|
||||||
lv_templ_ext_t *ext = lv_obj_get_ext_attr(templ);
|
lv_templ_ext_t *ext = lv_obj_get_ext_attr(templ);
|
||||||
|
|
||||||
@@ -173,7 +173,7 @@ lv_style_t * lv_btn_get_style(lv_obj_t * templ, lv_templ_style_t type)
|
|||||||
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
* LV_DESIGN_DRAW_POST: drawing after every children are drawn
|
||||||
* @param return true/false, depends on 'mode'
|
* @param return true/false, depends on 'mode'
|
||||||
*/
|
*/
|
||||||
static bool lv_templ_design(lv_obj_t * templ, const area_t * mask, lv_design_mode_t mode)
|
static bool lv_templ_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode)
|
||||||
{
|
{
|
||||||
/*Return false if the object is not covers the mask_p area*/
|
/*Return false if the object is not covers the mask_p area*/
|
||||||
if(mode == LV_DESIGN_COVER_CHK) {
|
if(mode == LV_DESIGN_COVER_CHK) {
|
||||||
@@ -203,7 +203,7 @@ static lv_res_t lv_templ_signal(lv_obj_t * templ, lv_signal_t sign, void * param
|
|||||||
lv_res_t res;
|
lv_res_t res;
|
||||||
|
|
||||||
/* Include the ancient signal function */
|
/* Include the ancient signal function */
|
||||||
res = lv_ancestor_signal(templ, sign, param);
|
res = ancestor_signal(templ, sign, param);
|
||||||
if(res != LV_RES_OK) return res;
|
if(res != LV_RES_OK) return res;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ void lv_templ_set_style(lv_obj_t * templ, lv_templ_style_t type, lv_style_t *sty
|
|||||||
* @param type which style should be get
|
* @param type which style should be get
|
||||||
* @return style pointer to the style
|
* @return style pointer to the style
|
||||||
* */
|
* */
|
||||||
lv_style_t * lv_btn_get_style(lv_obj_t * templ, lv_templ_style_t type);
|
lv_style_t * lv_templ_get_style(lv_obj_t * templ, lv_templ_style_t type);
|
||||||
|
|
||||||
/*=====================
|
/*=====================
|
||||||
* Other functions
|
* Other functions
|
||||||
|
|||||||
1
lvgl.h
1
lvgl.h
@@ -49,6 +49,7 @@ extern "C" {
|
|||||||
#include "lv_objx/lv_lmeter.h"
|
#include "lv_objx/lv_lmeter.h"
|
||||||
#include "lv_objx/lv_sw.h"
|
#include "lv_objx/lv_sw.h"
|
||||||
#include "lv_objx/lv_kb.h"
|
#include "lv_objx/lv_kb.h"
|
||||||
|
#include "lv_objx/lv_calendar.h"
|
||||||
|
|
||||||
/*********************
|
/*********************
|
||||||
* DEFINES
|
* DEFINES
|
||||||
|
|||||||
Reference in New Issue
Block a user