diff --git a/src/lv_core/lv_obj.c b/src/lv_core/lv_obj.c index 9b785fae7..41b84f0df 100644 --- a/src/lv_core/lv_obj.c +++ b/src/lv_core/lv_obj.c @@ -221,7 +221,7 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy) return lv_obj_create_from_class(&lv_obj, parent, copy); } -lv_obj_t * lv_obj_create_from_class(lv_obj_class_t * class, lv_obj_t * parent, const lv_obj_t * copy) +lv_obj_t * lv_obj_create_from_class(const lv_obj_class_t * class, lv_obj_t * parent, const lv_obj_t * copy) { lv_obj_t * obj = lv_mem_alloc(sizeof(lv_obj_t)); _lv_memset_00(obj, sizeof(lv_obj_t)); @@ -1301,7 +1301,7 @@ lv_obj_t * _lv_obj_get_focused_obj(const lv_obj_t * obj) * @param obj pointer to an object which type should be get * @param buf pointer to an `lv_obj_type_t` buffer to store the types */ -bool lv_obj_check_type(const lv_obj_t * obj, void * class_p) +bool lv_obj_check_type(const lv_obj_t * obj, const void * class_p) { return obj->class_p == class_p ? true : false; } diff --git a/src/lv_core/lv_obj.h b/src/lv_core/lv_obj.h index 638291e5c..11a2ccc02 100644 --- a/src/lv_core/lv_obj.h +++ b/src/lv_core/lv_obj.h @@ -162,6 +162,7 @@ enum { LV_STATE_PRESSED = 0x10, LV_STATE_SCROLLED = 0x20, LV_STATE_DISABLED = 0x40, + LV_STATE_USER = 0x80, /** Free to use by the user */ }; typedef uint8_t lv_state_t; @@ -176,15 +177,27 @@ enum { LV_OBJ_FLAG_SCROLL_MOMENTUM = (1 << 6), LV_OBJ_FLAG_SCROLL_STOP = (1 << 7), LV_OBJ_FLAG_SCROLL_CHAIN = (1 << 8), /** Allow propagating the scroll to a parent */ - LV_OBJ_FLAG_SNAPABLE = (1 << 9), - LV_OBJ_FLAG_PRESS_LOCK = (1 << 10), - LV_OBJ_FLAG_EVENT_BUBBLE = (1 << 11), - LV_OBJ_FLAG_GESTURE_BUBBLE = (1 << 12), - LV_OBJ_FLAG_FOCUS_BUBBLE = (1 << 13), - LV_OBJ_FLAG_SCROLL_ON_FOCUS = (1 << 14), /** Automatically scroll the focused object's ancestors to make the focused object visible*/ + LV_OBJ_FLAG_SCROLL_ON_FOCUS = (1 << 9), /** Automatically scroll the focused object's ancestors to make the focused object visible*/ + LV_OBJ_FLAG_SNAPABLE = (1 << 10), + LV_OBJ_FLAG_PRESS_LOCK = (1 << 11), + LV_OBJ_FLAG_EVENT_BUBBLE = (1 << 12), + LV_OBJ_FLAG_GESTURE_BUBBLE = (1 << 13), + LV_OBJ_FLAG_FOCUS_BUBBLE = (1 << 14), LV_OBJ_FLAG_ADV_HITTEST = (1 << 15), + LV_OBJ_FLAG_LAYOUT_1 = (1 << 16), /** Custom flag, free to use by layouts*/ + LV_OBJ_FLAG_LAYOUT_2 = (1 << 17), /** Custom flag, free to use by layouts*/ + LV_OBJ_FLAG_LAYOUT_3 = (1 << 18), /** Custom flag, free to use by layouts*/ + LV_OBJ_FLAG_LAYOUT_4 = (1 << 19), /** Custom flag, free to use by layouts*/ + LV_OBJ_FLAG_WIDGET_1 = (1 << 20), /** Custom flag, free to use by widget*/ + LV_OBJ_FLAG_WIDGET_2 = (1 << 21), /** Custom flag, free to use by widget*/ + LV_OBJ_FLAG_WIDGET_3 = (1 << 22), /** Custom flag, free to use by widget*/ + LV_OBJ_FLAG_WIDGET_4 = (1 << 23), /** Custom flag, free to use by widget*/ + LV_OBJ_FLAG_USER_1 = (1 << 24), /** Custom flag, free to use by user*/ + LV_OBJ_FLAG_USER_2 = (1 << 25), /** Custom flag, free to use by user*/ + LV_OBJ_FLAG_USER_3 = (1 << 26), /** Custom flag, free to use by user*/ + LV_OBJ_FLAG_USER_4 = (1 << 27), /** Custom flag, free to use by user*/ }; -typedef uint16_t lv_obj_flag_t; +typedef uint32_t lv_obj_flag_t; #include "lv_obj_pos.h" @@ -253,10 +266,27 @@ enum { LV_PART_CONTENT, LV_PART_INDICATOR, LV_PART_KNOB, + LV_PART_HIGHLIGHT, LV_PART_PLACEHOLDER, LV_PART_CURSOR, - LV_PART_HIGHLIGHT, - LV_PART_MARKER, + LV_PART_ITEM, + + LV_PART_CUSTOM_1, + LV_PART_CUSTOM_2, + LV_PART_CUSTOM_3, + LV_PART_CUSTOM_4, + LV_PART_CUSTOM_5, + LV_PART_CUSTOM_6, + LV_PART_CUSTOM_7, + LV_PART_CUSTOM_8, + LV_PART_CUSTOM_9, + LV_PART_CUSTOM_10, + LV_PART_CUSTOM_11, + LV_PART_CUSTOM_12, + LV_PART_CUSTOM_13, + LV_PART_CUSTOM_14, + LV_PART_CUSTOM_15, + LV_PART_CUSTOM_16, }; typedef uint8_t lv_part_t; @@ -301,7 +331,7 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy); void lv_obj_create_finish(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy); -lv_obj_t * lv_obj_create_from_class(lv_obj_class_t * class, lv_obj_t * parent, const lv_obj_t * copy); +lv_obj_t * lv_obj_create_from_class(const lv_obj_class_t * class, lv_obj_t * parent, const lv_obj_t * copy); /** * Delete 'obj' and all of its children @@ -683,7 +713,7 @@ bool lv_obj_hit_test(lv_obj_t * obj, lv_point_t * point); */ void * lv_obj_get_ext_attr(const lv_obj_t * obj); -bool lv_obj_check_type(const lv_obj_t * obj, void * class_p); +bool lv_obj_check_type(const lv_obj_t * obj, const void * class_p); #if LV_USE_USER_DATA /** diff --git a/src/lv_core/lv_obj_pos.c b/src/lv_core/lv_obj_pos.c index 66d901b64..81c0baa0f 100644 --- a/src/lv_core/lv_obj_pos.c +++ b/src/lv_core/lv_obj_pos.c @@ -124,6 +124,9 @@ void lv_obj_set_size(lv_obj_t * obj, lv_coord_t w, lv_coord_t h) if(obj->w_set == LV_SIZE_LAYOUT) w = lv_obj_get_width(obj); if(obj->h_set == LV_SIZE_LAYOUT) h = lv_obj_get_height(obj); + obj->w_set = w; + obj->h_set = h; + /*Calculate the required auto sizes*/ bool x_auto = obj->w_set == LV_SIZE_AUTO ? true : false; bool y_auto = obj->h_set == LV_SIZE_AUTO ? true : false; diff --git a/src/lv_core/lv_style.h b/src/lv_core/lv_style.h index 18ca1e8b9..e8ea025fb 100644 --- a/src/lv_core/lv_style.h +++ b/src/lv_core/lv_style.h @@ -646,7 +646,7 @@ static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) lv_style_set_pad_right(style, value); } -//static inline void lv_style_set_pad_ver(lv_style_t * style, lv_style_int_t value) +//static inline void lv_style_set_pad_ver(lv_style_t * style, lv_coord_t value) //{ // lv_style_set_pad_top(style, state, value); // lv_style_set_pad_bottom(style, state, value); @@ -654,7 +654,7 @@ static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) // //static inline void lv_obj_set_style_margin_all(lv_obj_t * obj, uint8_t part, lv_state_t state, -// lv_style_int_t value) +// lv_coord_t value) //{ // lv_obj_set_style_local_margin_top(obj, part, state, value); // lv_obj_set_style_local_margin_bottom(obj, part, state, value); @@ -663,7 +663,7 @@ static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) //} // // -//static inline void lv_style_set_margin_all(lv_style_t * style, lv_state_t state, lv_style_int_t value) +//static inline void lv_style_set_margin_all(lv_style_t * style, lv_state_t state, lv_coord_t value) //{ // lv_style_set_margin_top(style, state, value); // lv_style_set_margin_bottom(style, state, value); @@ -673,14 +673,14 @@ static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) // // //static inline void lv_obj_set_style_local_margin_hor(lv_obj_t * obj, uint8_t part, lv_state_t state, -// lv_style_int_t value) +// lv_coord_t value) //{ // lv_obj_set_style_local_margin_left(obj, part, state, value); // lv_obj_set_style_local_margin_right(obj, part, state, value); //} // // -//static inline void lv_style_set_margin_hor(lv_style_t * style, lv_state_t state, lv_style_int_t value) +//static inline void lv_style_set_margin_hor(lv_style_t * style, lv_state_t state, lv_coord_t value) //{ // lv_style_set_margin_left(style, state, value); // lv_style_set_margin_right(style, state, value); @@ -688,14 +688,14 @@ static inline void lv_style_set_pad_all(lv_style_t * style, lv_coord_t value) // // //static inline void lv_obj_set_style_local_margin_ver(lv_obj_t * obj, uint8_t part, lv_state_t state, -// lv_style_int_t value) +// lv_coord_t value) //{ // lv_obj_set_style_local_margin_top(obj, part, state, value); // lv_obj_set_style_local_margin_bottom(obj, part, state, value); //} // // -//static inline void lv_style_set_margin_ver(lv_style_t * style, lv_state_t state, lv_style_int_t value) +//static inline void lv_style_set_margin_ver(lv_style_t * style, lv_state_t state, lv_coord_t value) //{ // lv_style_set_margin_top(style, state, value); // lv_style_set_margin_bottom(style, state, value); diff --git a/src/lv_themes/lv_theme_default.c b/src/lv_themes/lv_theme_default.c index 76f3fc227..0d6319a33 100644 --- a/src/lv_themes/lv_theme_default.c +++ b/src/lv_themes/lv_theme_default.c @@ -61,7 +61,7 @@ #define COLOR_BG_TEXT_DIS (IS_LIGHT ? lv_color_hex3(0xaaa) : lv_color_hex3(0x999)) /*SECONDARY BACKGROUND*/ -#define COLOR_GRAY (IS_LIGHT ? lv_color_hex(0xd4d7d9) : lv_color_hex(0x45494d)) +#define COLOR_GRAY (IS_LIGHT ? lv_color_hex(0xd4d7d9) : lv_color_hex(0x45494d)) #define COLOR_BG_SEC_BORDER (IS_LIGHT ? lv_color_hex(0xdfe7ed) : lv_color_hex(0x404040)) #define COLOR_BG_SEC_TEXT (IS_LIGHT ? lv_color_hex(0x31404f) : lv_color_hex(0xa5a8ad)) #define COLOR_BG_SEC_TEXT_DIS (IS_LIGHT ? lv_color_hex(0xaaaaaa) : lv_color_hex(0xa5a8ad)) @@ -82,18 +82,17 @@ typedef struct { lv_style_t scrollbar_scrolled; lv_style_t card; lv_style_t btn; - lv_style_t btn_disabled; - lv_style_t btn_color; - lv_style_t btn_color_checked; /*Utility*/ lv_style_t bg_color_primary; + lv_style_t bg_color_secondary; lv_style_t bg_color_gray; lv_style_t bg_color_white; lv_style_t pressed; lv_style_t disabled; lv_style_t pad_zero; lv_style_t pad_small; + lv_style_t pad_gap; lv_style_t pad_small_negative; lv_style_t focus_border; lv_style_t focus_outline; @@ -253,22 +252,12 @@ static void basic_init(void) style_init_reset(&styles->btn); lv_style_set_radius(&styles->btn, RADIUS_DEFAULT); lv_style_set_bg_opa(&styles->btn, LV_OPA_COVER); - lv_style_set_bg_color(&styles->btn, CARD_COLOR); - lv_style_set_border_color(&styles->btn, CARD_BORDER_COLOR); - lv_style_set_border_width(&styles->btn, BORDER_WIDTH); + lv_style_set_bg_color(&styles->btn, COLOR_GRAY); lv_style_set_text_color(&styles->btn, CARD_TEXT_COLOR); lv_style_set_pad_hor(&styles->btn, LV_DPX(40)); lv_style_set_pad_ver(&styles->btn, LV_DPX(15)); - style_init_reset(&styles->btn_color); - lv_style_set_border_width(&styles->btn_color, 0); - lv_style_set_bg_color(&styles->btn_color, BTN_COLOR); - lv_style_set_text_color(&styles->btn_color, LV_COLOR_WHITE); - - style_init_reset(&styles->btn_color_checked); - lv_style_set_bg_color(&styles->btn_color_checked, BTN_CHK_PR_COLOR); - style_init_reset(&styles->pressed); lv_style_set_color_filter_cb(&styles->pressed, lv_color_darken); lv_style_set_color_filter_opa(&styles->pressed, LV_OPA_20); @@ -281,8 +270,11 @@ static void basic_init(void) lv_style_set_clip_corner(&styles->clip_corner, true); style_init_reset(&styles->pad_small); - lv_coord_t pad_small_value = LV_DPX(10); - lv_style_set_pad_all(&styles->pad_small, pad_small_value); + lv_style_set_pad_all(&styles->pad_small, LV_DPX(10)); + + style_init_reset(&styles->pad_gap); + lv_style_set_pad_row(&styles->pad_gap, LV_DPX(10)); + lv_style_set_pad_column(&styles->pad_gap, LV_DPX(10)); style_init_reset(&styles->pad_small_negative); lv_style_set_pad_all(&styles->pad_small_negative, - LV_DPX(4)); @@ -295,6 +287,11 @@ static void basic_init(void) lv_style_set_text_color(&styles->bg_color_primary, LV_COLOR_WHITE); lv_style_set_bg_opa(&styles->bg_color_primary, LV_OPA_COVER); + style_init_reset(&styles->bg_color_secondary); + lv_style_set_bg_color(&styles->bg_color_primary, theme.color_secondary); + lv_style_set_text_color(&styles->bg_color_primary, LV_COLOR_WHITE); + lv_style_set_bg_opa(&styles->bg_color_primary, LV_OPA_COVER); + style_init_reset(&styles->bg_color_gray); lv_style_set_bg_color(&styles->bg_color_gray, COLOR_GRAY); lv_style_set_bg_opa(&styles->bg_color_gray, LV_OPA_COVER); @@ -350,14 +347,6 @@ static void basic_init(void) -} - -static void btnmatrix_init(void) -{ -#if LV_USE_BTNMATRIX != 0 - style_init_reset(&styles->btnmatrix_btn); - lv_style_set_margin_all(&styles->btnmatrix_btn, LV_STATE_DEFAULT, LV_DPX(10)); -#endif } static void line_init(void) @@ -501,20 +490,20 @@ static void textarea_init(void) //} -static void table_init(void) -{ -#if LV_USE_TABLE != 0 - style_init_reset(&styles->table_cell); - lv_style_set_border_color(&styles->table_cell, LV_STATE_DEFAULT, CARD_BORDER_COLOR); - lv_style_set_border_width(&styles->table_cell, LV_STATE_DEFAULT, 1); - lv_style_set_border_side(&styles->table_cell, LV_STATE_DEFAULT, LV_BORDER_SIDE_TOP | LV_BORDER_SIDE_BOTTOM); - lv_style_set_pad_left(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); - lv_style_set_pad_right(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); - lv_style_set_pad_top(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); - lv_style_set_pad_bottom(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); - -#endif -} +//static void table_init(void) +//{ +//#if LV_USE_TABLE != 0 +// style_init_reset(&styles->table_cell); +// lv_style_set_border_color(&styles->table_cell, LV_STATE_DEFAULT, CARD_BORDER_COLOR); +// lv_style_set_border_width(&styles->table_cell, LV_STATE_DEFAULT, 1); +// lv_style_set_border_side(&styles->table_cell, LV_STATE_DEFAULT, LV_BORDER_SIDE_TOP | LV_BORDER_SIDE_BOTTOM); +// lv_style_set_pad_left(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); +// lv_style_set_pad_right(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); +// lv_style_set_pad_top(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); +// lv_style_set_pad_bottom(&styles->table_cell, LV_STATE_DEFAULT, PAD_DEF); +// +//#endif +//} /********************** @@ -554,7 +543,6 @@ lv_theme_t * lv_theme_default_init(lv_color_t color_primary, lv_color_t color_se theme.flags = flags; basic_init(); - btnmatrix_init(); line_init(); linemeter_init(); gauge_init(); @@ -562,7 +550,6 @@ lv_theme_t * lv_theme_default_init(lv_color_t color_primary, lv_color_t color_se spinner_init(); chart_init(); textarea_init(); - table_init(); theme.apply_cb = theme_apply; @@ -593,15 +580,26 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj) #if LV_USE_BTN else if(lv_obj_check_type(obj, &lv_btn)) { lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->btn); - lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->btn_color); + lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->bg_color_primary); lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->transition_delayed); lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_PRESSED, &styles->pressed); lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_PRESSED, &styles->transition_normal); lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_PRESSED, &styles->grow); - lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_CHECKED, &styles->btn_color_checked); + lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_CHECKED, &styles->bg_color_secondary); } #endif +#if LV_USE_BTNMATRIX + else if(lv_obj_check_type(obj, &lv_btnmatrix)) { + lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->card); + lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->pad_gap); + lv_obj_add_style_no_refresh(obj, LV_PART_ITEM, LV_STATE_DEFAULT, &styles->btn); + lv_obj_add_style_no_refresh(obj, LV_PART_ITEM, LV_STATE_PRESSED, &styles->pressed); + lv_obj_add_style_no_refresh(obj, LV_PART_ITEM, LV_STATE_PRESSED, &styles->grow); + lv_obj_add_style_no_refresh(obj, LV_PART_ITEM, LV_STATE_DEFAULT, &styles->transition_delayed); + lv_obj_add_style_no_refresh(obj, LV_PART_ITEM, LV_STATE_PRESSED, &styles->transition_normal); + } +#endif #if LV_USE_BAR else if(lv_obj_check_type(obj, &lv_bar)) { lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->bg_color_gray); @@ -626,6 +624,15 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj) } #endif +#if LV_USE_TABLE + else if(lv_obj_check_type(obj, &lv_table)) { + lv_obj_add_style_no_refresh(obj, LV_PART_MAIN, LV_STATE_DEFAULT, &styles->card); + lv_obj_add_style_no_refresh(obj, LV_PART_SCROLLBAR, LV_STATE_DEFAULT, &styles->scrollbar); + lv_obj_add_style_no_refresh(obj, LV_PART_SCROLLBAR, LV_STATE_SCROLLED, &styles->scrollbar_scrolled); + lv_obj_add_style_no_refresh(obj, LV_PART_ITEM, LV_STATE_DEFAULT, &styles->card); + } +#endif + #if LV_USE_CHECKBOX else if(lv_obj_check_type(obj, &lv_checkbox)) { lv_obj_add_style_no_refresh(obj, LV_PART_MARKER, LV_STATE_DEFAULT, &styles->cb_marker); @@ -682,23 +689,23 @@ static void theme_apply(lv_theme_t * th, lv_obj_t * obj) } #endif -#if LV_USE_BTNMATRIX -case LV_THEME_BTNMATRIX: - list = _lv_obj_get_style_list(obj, LV_BTNMATRIX_PART_MAIN); - _lv_style_list_add_style(list, &styles->card); - _lv_style_list_add_style(list, &styles->pad_small); - - list = _lv_obj_get_style_list(obj, LV_BTNMATRIX_PART_BTN); - _lv_style_list_add_style(list, &styles->card); - _lv_style_list_add_style(list, &styles->bg_click); - _lv_style_list_add_style(list, &styles->btnmatrix_btn); - - list = _lv_obj_get_style_list(obj, LV_BTNMATRIX_PART_BTN_2); - _lv_style_list_add_style(list, &styles->card); - _lv_style_list_add_style(list, &styles->bg_click); - _lv_style_list_add_style(list, &styles->btnmatrix_btn); - break; -#endif +//#if LV_USE_BTNMATRIX +//case LV_THEME_BTNMATRIX: +// list = _lv_obj_get_style_list(obj, LV_BTNMATRIX_PART_MAIN); +// _lv_style_list_add_style(list, &styles->card); +// _lv_style_list_add_style(list, &styles->pad_small); +// +// list = _lv_obj_get_style_list(obj, LV_BTNMATRIX_PART_BTN); +// _lv_style_list_add_style(list, &styles->card); +// _lv_style_list_add_style(list, &styles->bg_click); +// _lv_style_list_add_style(list, &styles->btnmatrix_btn); +// +// list = _lv_obj_get_style_list(obj, LV_BTNMATRIX_PART_BTN_2); +// _lv_style_list_add_style(list, &styles->card); +// _lv_style_list_add_style(list, &styles->bg_click); +// _lv_style_list_add_style(list, &styles->btnmatrix_btn); +// break; +//#endif #if LV_USE_IMG case LV_THEME_IMAGE: @@ -773,22 +780,6 @@ case LV_THEME_OBJMASK: // _lv_style_list_add_style(list, &styles->chart_series); // break; //#endif -#if LV_USE_TABLE -case LV_THEME_TABLE: { - list = _lv_obj_get_style_list(obj, LV_TABLE_PART_BG); - _lv_style_list_add_style(list, &styles->card); - - int idx = 1; /* start value should be 1, not zero, since cell styles - start at 1 due to presence of LV_TABLE_PART_BG=0 - in the enum (lv_table.h) */ - /* declaring idx outside loop to work with older compilers */ - for(; idx <= LV_TABLE_CELL_STYLE_CNT; idx ++) { - list = _lv_obj_get_style_list(obj, idx); - _lv_style_list_add_style(list, &styles->table_cell); - } - break; -} -#endif #if LV_USE_TEXTAREA case LV_THEME_TEXTAREA: diff --git a/src/lv_widgets/lv_arc.c b/src/lv_widgets/lv_arc.c index 19f3922e4..8aa4cbb6a 100644 --- a/src/lv_widgets/lv_arc.c +++ b/src/lv_widgets/lv_arc.c @@ -817,8 +817,8 @@ static void inv_arc_area(lv_obj_t * obj, uint16_t start_angle, uint16_t end_angl lv_coord_t rout = (LV_MATH_MIN(lv_obj_get_width(obj) - left - right, lv_obj_get_height(obj) - top - bottom)) / 2; lv_coord_t x = arc->coords.x1 + rout + left; lv_coord_t y = arc->coords.y1 + rout + top; - lv_style_int_t w = lv_obj_get_style_line_width(obj, part); - lv_style_int_t rounded = lv_obj_get_style_line_rounded(obj, part); + lv_coord_t w = lv_obj_get_style_line_width(obj, part); + lv_coord_t rounded = lv_obj_get_style_line_rounded(obj, part); lv_coord_t rin = rout - w; lv_coord_t extra_area = 0; diff --git a/src/lv_widgets/lv_btnmatrix.c b/src/lv_widgets/lv_btnmatrix.c index ce90cd6c9..9d636a0f2 100644 --- a/src/lv_widgets/lv_btnmatrix.c +++ b/src/lv_widgets/lv_btnmatrix.c @@ -1,5 +1,5 @@ /** - * @file lv_btnm.c + * @file lv_obj.c * */ @@ -30,8 +30,11 @@ /********************** * STATIC PROTOTYPES **********************/ -static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * param); -static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * clip_area, lv_design_mode_t mode); +static void lv_btnmatrix_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy); +static void lv_btnmatrix_destructor(lv_obj_t * obj); + +static lv_res_t lv_btnmatrix_signal(lv_obj_t * obj, lv_signal_t sign, void * param); +static lv_design_res_t lv_btnmatrix_design(lv_obj_t * obj, const lv_area_t * clip_area, lv_design_mode_t mode); static uint8_t get_button_width(lv_btnmatrix_ctrl_t ctrl_bits); static bool button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits); @@ -40,19 +43,23 @@ static bool button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits); static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits); static bool button_is_tgl_enabled(lv_btnmatrix_ctrl_t ctrl_bits); static bool button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits); -static bool button_is_type2(lv_btnmatrix_ctrl_t ctrl_bits); -static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p); -static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map); -static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx); -static void make_one_button_checked(lv_obj_t * btnm, uint16_t btn_idx); +static uint16_t get_button_from_point(lv_obj_t * obj, lv_point_t * p); +static void allocate_btn_areas_and_controls(const lv_obj_t * obj, const char ** map); +static void invalidate_button_area(const lv_obj_t * obj, uint16_t btn_idx); +static void make_one_button_checked(lv_obj_t * obj, uint16_t btn_idx); /********************** * STATIC VARIABLES **********************/ static const char * lv_btnmatrix_def_map[] = {"Btn1", "Btn2", "Btn3", "\n", "Btn4", "Btn5", ""}; -static lv_design_cb_t ancestor_design_f; -static lv_signal_cb_t ancestor_signal; +const lv_obj_class_t lv_btnmatrix = { + .constructor = lv_btnmatrix_constructor, + .destructor = lv_btnmatrix_destructor, + .signal_cb = lv_btnmatrix_signal, + .design_cb = lv_btnmatrix_design, + .ext_size = sizeof(lv_btnmatrix_ext_t), + }; /********************** * MACROS @@ -69,57 +76,9 @@ static lv_signal_cb_t ancestor_signal; * from it * @return pointer to the created button matrix */ -lv_obj_t * lv_btnmatrix_create(lv_obj_t * par, const lv_obj_t * copy) +lv_obj_t * lv_btnmatrix_create(lv_obj_t * parent, const lv_obj_t * copy) { - LV_LOG_TRACE("button matrix create started"); - - /*Create the ancestor object*/ - lv_obj_t * btnm = lv_obj_create(par, copy); - LV_ASSERT_MEM(btnm); - if(btnm == NULL) return NULL; - - if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(btnm); - - /*Allocate the object type specific extended data*/ - lv_btnmatrix_ext_t * ext = lv_obj_allocate_ext_attr(btnm, sizeof(lv_btnmatrix_ext_t)); - LV_ASSERT_MEM(ext); - if(ext == NULL) { - lv_obj_del(btnm); - return NULL; - } - - ext->btn_cnt = 0; - ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE; - ext->btn_id_focused = LV_BTNMATRIX_BTN_NONE; - ext->btn_id_act = LV_BTNMATRIX_BTN_NONE; - ext->button_areas = NULL; - ext->ctrl_bits = NULL; - ext->map_p = NULL; - ext->recolor = 0; - ext->one_check = 0; - - if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(btnm); - - lv_obj_set_signal_cb(btnm, lv_btnmatrix_signal); - lv_obj_set_design_cb(btnm, lv_btnmatrix_design); - - /*Init the new button matrix object*/ - if(copy == NULL) { - lv_btnmatrix_set_map(btnm, lv_btnmatrix_def_map); - lv_obj_set_size(btnm, LV_DPI * 2, LV_DPI * 1); - lv_theme_apply(btnm, LV_THEME_BTNMATRIX); - } - /*Copy an existing object*/ - else { - lv_btnmatrix_ext_t * copy_ext = lv_obj_get_ext_attr(copy); - lv_btnmatrix_set_map(btnm, copy_ext->map_p); - lv_btnmatrix_set_ctrl_map(btnm, copy_ext->ctrl_bits); - lv_style_list_copy(&ext->style_btn, ©_ext->style_btn); - } - - LV_LOG_INFO("button matrix created"); - - return btnm; + return lv_obj_create_from_class(&lv_btnmatrix, parent, copy); } /*===================== @@ -130,30 +89,30 @@ lv_obj_t * lv_btnmatrix_create(lv_obj_t * par, const lv_obj_t * copy) * Set a new map. Buttons will be created/deleted according to the map. The * button matrix keeps a reference to the map and so the string array must not * be deallocated during the life of the matrix. - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param map pointer a string array. The last string has to be: "". Use "\n" to make a line break. */ -void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[]) +void lv_btnmatrix_set_map(lv_obj_t * obj, const char * map[]) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); LV_ASSERT_NULL(map); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; /*Analyze the map and create the required number of buttons*/ - allocate_btn_areas_and_controls(btnm, map); + allocate_btn_areas_and_controls(obj, map); ext->map_p = map; - lv_bidi_dir_t base_dir = lv_obj_get_base_dir(btnm); + lv_bidi_dir_t base_dir = lv_obj_get_base_dir(obj); /*Set size and positions of the buttons*/ - lv_style_int_t left = lv_obj_get_style_pad_left(btnm, LV_BTNMATRIX_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(btnm, LV_BTNMATRIX_PART_MAIN); - lv_coord_t row_gap = LV_MATH_MAX(lv_obj_get_style_margin_top(btnm, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_bottom(btnm, LV_BTNMATRIX_PART_BTN)); - lv_coord_t col_gap = LV_MATH_MAX(lv_obj_get_style_margin_left(btnm, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_right(btnm, LV_BTNMATRIX_PART_BTN)); + lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN); + lv_coord_t ptop = lv_obj_get_style_pad_top(obj, LV_PART_MAIN); + lv_coord_t prow = lv_obj_get_style_pad_row(obj, LV_PART_MAIN); + lv_coord_t pcol = lv_obj_get_style_pad_column(obj, LV_PART_MAIN); - lv_coord_t max_w = lv_obj_get_width_fit(btnm); - lv_coord_t max_h = lv_obj_get_height_fit(btnm); + lv_coord_t max_w = lv_obj_get_width_fit(obj); + lv_coord_t max_h = lv_obj_get_height_fit(obj); /*Count the lines to calculate button height*/ uint8_t row_cnt = 1; @@ -163,7 +122,7 @@ void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[]) } /*Calculate the position of each row*/ - lv_coord_t max_h_no_gap = max_h - (row_gap * (row_cnt - 1)); + lv_coord_t max_h_no_gap = max_h - (prow * (row_cnt - 1)); /* Count the units and the buttons in a line * (A button can be 1,2,3... unit wide)*/ @@ -188,11 +147,11 @@ void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[]) continue; } - lv_coord_t row_y1 = top + (max_h_no_gap * row) / row_cnt + row * row_gap; - lv_coord_t row_y2 = top + (max_h_no_gap * (row + 1)) / row_cnt + row * row_gap - 1; + lv_coord_t row_y1 = ptop + (max_h_no_gap * row) / row_cnt + row * prow; + lv_coord_t row_y2 = ptop + (max_h_no_gap * (row + 1)) / row_cnt + row * prow - 1; /*Set the button size and positions*/ - lv_coord_t max_w_no_gap = max_w - (col_gap * (btn_cnt - 1)); + lv_coord_t max_w_no_gap = max_w - (pcol * (btn_cnt - 1)); if(max_w_no_gap < 0) max_w_no_gap = 0; uint32_t row_unit_cnt = 0; /*The current unit position in the row*/ @@ -200,8 +159,8 @@ void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[]) for(btn = 0; btn < btn_cnt; btn++, btn_tot_i++, txt_tot_i++) { uint32_t btn_u = get_button_width(ext->ctrl_bits[btn_tot_i]); - lv_coord_t btn_x1 = left + (max_w_no_gap * row_unit_cnt) / unit_cnt + btn * col_gap; - lv_coord_t btn_x2 = left + (max_w_no_gap * (row_unit_cnt + btn_u)) / unit_cnt + btn * col_gap - 1; + lv_coord_t btn_x1 = pleft + (max_w_no_gap * row_unit_cnt) / unit_cnt + btn * pcol; + lv_coord_t btn_x2 = pleft + (max_w_no_gap * (row_unit_cnt + btn_u)) / unit_cnt + btn * pcol - 1; /*If RTL start from the right*/ if(base_dir == LV_BIDI_DIR_RTL) { @@ -220,14 +179,14 @@ void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[]) map_row = &map_row[btn_cnt + 1]; /*Set the map to the next line*/ } - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } /** * Set the button control map (hidden, disabled etc.) for a button matrix. The * control map array will be copied and so may be deallocated after this * function returns. - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param ctrl_map pointer to an array of `lv_btn_ctrl_t` control bytes. The * length of the array and position of the elements must match * the number and order of the individual buttons (i.e. excludes @@ -235,119 +194,119 @@ void lv_btnmatrix_set_map(lv_obj_t * btnm, const char * map[]) * An element of the map should look like e.g.: * `ctrl_map[0] = width | LV_BTNMATRIX_CTRL_NO_REPEAT | LV_BTNMATRIX_CTRL_TGL_ENABLE` */ -void lv_btnmatrix_set_ctrl_map(lv_obj_t * btnm, const lv_btnmatrix_ctrl_t ctrl_map[]) +void lv_btnmatrix_set_ctrl_map(lv_obj_t * obj, const lv_btnmatrix_ctrl_t ctrl_map[]) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr; _lv_memcpy(ext->ctrl_bits, ctrl_map, sizeof(lv_btnmatrix_ctrl_t) * ext->btn_cnt); - lv_btnmatrix_set_map(btnm, ext->map_p); + lv_btnmatrix_set_map(obj, ext->map_p); } /** * Set the focused button i.e. visually highlight it. - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param id index of the button to focus(`LV_BTNMATRIX_BTN_NONE` to remove focus) */ -void lv_btnmatrix_set_focused_btn(lv_obj_t * btnm, uint16_t id) +void lv_btnmatrix_set_focused_btn(lv_obj_t * obj, uint16_t id) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr; if(id >= ext->btn_cnt && id != LV_BTNMATRIX_BTN_NONE) return; if(id == ext->btn_id_focused) return; ext->btn_id_focused = id; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } /** * Enable recoloring of button's texts - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param en true: enable recoloring; false: disable */ -void lv_btnmatrix_set_recolor(const lv_obj_t * btnm, bool en) +void lv_btnmatrix_set_recolor(const lv_obj_t * obj, bool en) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; ext->recolor = en; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } /** * Set the attributes of a button of the button matrix - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param btn_id 0 based index of the button to modify. (Not counting new lines) */ -void lv_btnmatrix_set_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl) +void lv_btnmatrix_set_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(btn_id >= ext->btn_cnt) return; if(ext->one_check && (ctrl & LV_BTNMATRIX_CTRL_CHECKED)) { - lv_btnmatrix_clear_btn_ctrl_all(btnm, LV_BTNMATRIX_CTRL_CHECKED); + lv_btnmatrix_clear_btn_ctrl_all(obj, LV_BTNMATRIX_CTRL_CHECKED); } ext->ctrl_bits[btn_id] |= ctrl; - invalidate_button_area(btnm, btn_id); + invalidate_button_area(obj, btn_id); } /** * Clear the attributes of a button of the button matrix - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param btn_id 0 based index of the button to modify. (Not counting new lines) */ -void lv_btnmatrix_clear_btn_ctrl(const lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl) +void lv_btnmatrix_clear_btn_ctrl(const lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(btn_id >= ext->btn_cnt) return; ext->ctrl_bits[btn_id] &= (~ctrl); - invalidate_button_area(btnm, btn_id); + invalidate_button_area(obj, btn_id); } /** * Set the attributes of all buttons of a button matrix - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param ctrl attribute(s) to set from `lv_btnmatrix_ctrl_t`. Values can be ORed. */ -void lv_btnmatrix_set_btn_ctrl_all(lv_obj_t * btnm, lv_btnmatrix_ctrl_t ctrl) +void lv_btnmatrix_set_btn_ctrl_all(lv_obj_t * obj, lv_btnmatrix_ctrl_t ctrl) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; uint16_t i; for(i = 0; i < ext->btn_cnt; i++) { - lv_btnmatrix_set_btn_ctrl(btnm, i, ctrl); + lv_btnmatrix_set_btn_ctrl(obj, i, ctrl); } } /** * Clear the attributes of all buttons of a button matrix - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param ctrl attribute(s) to set from `lv_btnmatrix_ctrl_t`. Values can be ORed. * @param en true: set the attributes; false: clear the attributes */ -void lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * btnm, lv_btnmatrix_ctrl_t ctrl) +void lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * obj, lv_btnmatrix_ctrl_t ctrl) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; uint16_t i; for(i = 0; i < ext->btn_cnt; i++) { - lv_btnmatrix_clear_btn_ctrl(btnm, i, ctrl); + lv_btnmatrix_clear_btn_ctrl(obj, i, ctrl); } } @@ -356,56 +315,56 @@ void lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * btnm, lv_btnmatrix_ctrl_t ctrl) * This method will cause the matrix be regenerated and is a relatively * expensive operation. It is recommended that initial width be specified using * `lv_btnmatrix_set_ctrl_map` and this method only be used for dynamic changes. - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param btn_id 0 based index of the button to modify. * @param width Relative width compared to the buttons in the same row. [1..7] */ -void lv_btnmatrix_set_btn_width(lv_obj_t * btnm, uint16_t btn_id, uint8_t width) +void lv_btnmatrix_set_btn_width(lv_obj_t * obj, uint16_t btn_id, uint8_t width) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(btn_id >= ext->btn_cnt) return; ext->ctrl_bits[btn_id] &= (~LV_BTNMATRIX_WIDTH_MASK); ext->ctrl_bits[btn_id] |= (LV_BTNMATRIX_WIDTH_MASK & width); - lv_btnmatrix_set_map(btnm, ext->map_p); + lv_btnmatrix_set_map(obj, ext->map_p); } /** * Make the button matrix like a selector widget (only one button may be checked at a time). * `Checkable` must be enabled on the buttons you want to be selected with `lv_btnmatrix_set_ctrl` or * `lv_btnmatrix_set_btn_ctrl_all`. - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param one_chk whether "one check" mode is enabled */ -void lv_btnmatrix_set_one_checked(lv_obj_t * btnm, bool one_chk) +void lv_btnmatrix_set_one_checked(lv_obj_t * obj, bool one_chk) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; ext->one_check = one_chk; /*If more than one button is toggled only the first one should be*/ - make_one_button_checked(btnm, 0); + make_one_button_checked(obj, 0); } /** * Set the align of the map text (left, right or center) - * @param btnm pointer to a btnmatrix object + * @param obj pointer to a btnmatrix object * @param align LV_LABEL_ALIGN_LEFT, LV_LABEL_ALIGN_RIGHT or LV_LABEL_ALIGN_CENTER */ -void lv_btnmatrix_set_text_align(lv_obj_t * btnm, lv_label_align_t align) +void lv_btnmatrix_set_text_align(lv_obj_t * obj, lv_label_align_t align) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(ext->align == align) return; ext->align = align; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } /*===================== @@ -414,27 +373,27 @@ void lv_btnmatrix_set_text_align(lv_obj_t * btnm, lv_label_align_t align) /** * Get the current map of a button matrix - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @return the current map */ -const char ** lv_btnmatrix_get_map_array(const lv_obj_t * btnm) +const char ** lv_btnmatrix_get_map_array(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; return ext->map_p; } /** * Check whether the button's text can use recolor or not - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @return true: text recolor enable; false: disabled */ -bool lv_btnmatrix_get_recolor(const lv_obj_t * btnm) +bool lv_btnmatrix_get_recolor(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; return ext->recolor; } @@ -442,30 +401,30 @@ bool lv_btnmatrix_get_recolor(const lv_obj_t * btnm) /** * Get the index of the lastly "activated" button by the user (pressed, released etc) * Useful in the the `event_cb` to get the text of the button, check if hidden etc. - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @return index of the last released button (LV_BTNMATRIX_BTN_NONE: if unset) */ -uint16_t lv_btnmatrix_get_active_btn(const lv_obj_t * btnm) +uint16_t lv_btnmatrix_get_active_btn(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; return ext->btn_id_act; } /** * Get the text of the lastly "activated" button by the user (pressed, released etc) * Useful in the the `event_cb` - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @return text of the last released button (NULL: if unset) */ -const char * lv_btnmatrix_get_active_btn_text(const lv_obj_t * btnm) +const char * lv_btnmatrix_get_active_btn_text(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(ext->btn_id_act != LV_BTNMATRIX_BTN_NONE) { - return lv_btnmatrix_get_btn_text(btnm, ext->btn_id_act); + return lv_btnmatrix_get_btn_text(obj, ext->btn_id_act); } else { return NULL; @@ -475,29 +434,29 @@ const char * lv_btnmatrix_get_active_btn_text(const lv_obj_t * btnm) /** * Get the pressed button's index. * The button be really pressed by the user or manually set to pressed with `lv_btnmatrix_set_pressed` - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @return index of the pressed button (LV_BTNMATRIX_BTN_NONE: if unset) */ -uint16_t lv_btnmatrix_get_focused_btn(const lv_obj_t * btnm) +uint16_t lv_btnmatrix_get_focused_btn(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; return ext->btn_id_focused; } /** * Get the button's text - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param btn_id the index a button not counting new line characters. (The return value of * lv_btnmatrix_get_pressed/released) * @return text of btn_index` button */ -const char * lv_btnmatrix_get_btn_text(const lv_obj_t * btnm, uint16_t btn_id) +const char * lv_btnmatrix_get_btn_text(const lv_obj_t * obj, uint16_t btn_id) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(btn_id > ext->btn_cnt) return NULL; uint16_t txt_i = 0; @@ -518,17 +477,17 @@ const char * lv_btnmatrix_get_btn_text(const lv_obj_t * btnm, uint16_t btn_id) /** * Get the whether a control value is enabled or disabled for button of a button matrix - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param btn_id the index a button not counting new line characters. (E.g. the return value of * lv_btnmatrix_get_pressed/released) * @param ctrl control values to check (ORed value can be used) * @return true: long press repeat is disabled; false: long press repeat enabled */ -bool lv_btnmatrix_get_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl) +bool lv_btnmatrix_get_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(btn_id >= ext->btn_cnt) return false; return (ext->ctrl_bits[btn_id] & ctrl) ? true : false; @@ -537,34 +496,34 @@ bool lv_btnmatrix_get_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnmatrix_ct /** * Find whether "one check" mode is enabled. - * @param btnm Button matrix object + * @param obj Button matrix object * @return whether "one check" mode is enabled */ -bool lv_btnmatrix_get_one_checked(const lv_obj_t * btnm) +bool lv_btnmatrix_get_one_checked(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; return ext->one_check; } /** * Get the align attribute - * @param btnm pointer to a btnmatrix object + * @param obj pointer to a btnmatrix object * @return LV_LABEL_ALIGN_LEFT, LV_LABEL_ALIGN_RIGHT or LV_LABEL_ALIGN_CENTER */ -lv_label_align_t lv_btnmatrix_get_align(const lv_obj_t * btnm) +lv_label_align_t lv_btnmatrix_get_align(const lv_obj_t * obj) { - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; lv_label_align_t align = ext->align; if(align == LV_LABEL_ALIGN_AUTO) { #if LV_USE_BIDI - lv_bidi_dir_t base_dir = lv_obj_get_base_dir(btnm); + lv_bidi_dir_t base_dir = lv_obj_get_base_dir(obj); if(base_dir == LV_BIDI_DIR_RTL) align = LV_LABEL_ALIGN_RIGHT; else align = LV_LABEL_ALIGN_LEFT; #else @@ -579,9 +538,52 @@ lv_label_align_t lv_btnmatrix_get_align(const lv_obj_t * btnm) * STATIC FUNCTIONS **********************/ +static void lv_btnmatrix_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy) +{ + LV_LOG_TRACE("lv_bar create started"); + + LV_CLASS_CONSTRUCTOR_BEGIN(obj, lv_btnmatrix) + lv_obj.constructor(obj, parent, copy); + + LV_LOG_TRACE("button matrix create started"); + + lv_btnmatrix_ext_t * ext = obj->ext_attr; + ext->btn_cnt = 0; + ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE; + ext->btn_id_focused = LV_BTNMATRIX_BTN_NONE; + ext->btn_id_act = LV_BTNMATRIX_BTN_NONE; + ext->button_areas = NULL; + ext->ctrl_bits = NULL; + ext->map_p = NULL; + ext->recolor = 0; + ext->one_check = 0; + + /*Init the new button matrix object*/ + if(copy == NULL) { + lv_btnmatrix_set_map(obj, lv_btnmatrix_def_map); + lv_obj_set_size(obj, LV_DPI * 2, LV_DPI * 1); + } + /*Copy an existing object*/ + else { + lv_btnmatrix_ext_t * copy_ext = copy->ext_attr; + lv_btnmatrix_set_map(obj, copy_ext->map_p); + lv_btnmatrix_set_ctrl_map(obj, copy_ext->ctrl_bits); + } + + + LV_CLASS_CONSTRUCTOR_END(obj, lv_bar) + LV_LOG_INFO("button matrix created"); +} + +static void lv_btnmatrix_destructor(lv_obj_t * obj) +{ + +// lv_mem_free(ext->button_areas); +// lv_mem_free(ext->ctrl_bits); +} /** * Handle the drawing related tasks of the button matrix - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param clip_area 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) @@ -589,65 +591,60 @@ lv_label_align_t lv_btnmatrix_get_align(const lv_obj_t * btnm) * LV_DESIGN_DRAW_POST: drawing after every children are drawn * @param return an element of `lv_design_res_t` */ -static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * clip_area, lv_design_mode_t mode) +static lv_design_res_t lv_btnmatrix_design(lv_obj_t * obj, const lv_area_t * clip_area, lv_design_mode_t mode) { if(mode == LV_DESIGN_COVER_CHK) { - return ancestor_design_f(btnm, clip_area, mode); + return lv_obj.design_cb(obj, clip_area, mode); } /*Draw the object*/ else if(mode == LV_DESIGN_DRAW_MAIN) { - ancestor_design_f(btnm, clip_area, mode); + lv_obj.design_cb(obj, clip_area, mode); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; if(ext->btn_cnt == 0) return LV_DESIGN_RES_OK; - lv_area_t area_btnm; - lv_obj_get_coords(btnm, &area_btnm); - lv_area_t area_tmp; - lv_coord_t btn_w; - lv_coord_t btn_h; + obj->style_list.skip_trans = 1; + + lv_area_t area_obj; + lv_obj_get_coords(obj, &area_obj); + + lv_area_t btn_area; uint16_t btn_i = 0; uint16_t txt_i = 0; lv_txt_flag_t txt_flag = LV_TXT_FLAG_NONE; if(ext->recolor) txt_flag |= LV_TXT_FLAG_RECOLOR; - lv_label_align_t align = lv_btnmatrix_get_align(btnm); + lv_label_align_t align = lv_btnmatrix_get_align(obj); if(align == LV_LABEL_ALIGN_CENTER) txt_flag |= LV_TXT_FLAG_CENTER; if(align == LV_LABEL_ALIGN_RIGHT) txt_flag |= LV_TXT_FLAG_RIGHT; - lv_draw_rect_dsc_t draw_rect_rel_dsc; - lv_draw_label_dsc_t draw_label_rel_dsc; - - lv_draw_rect_dsc_t draw_rect_chk_dsc; - lv_draw_label_dsc_t draw_label_chk_dsc; - - lv_draw_rect_dsc_t draw_rect_ina_dsc; - lv_draw_label_dsc_t draw_label_ina_dsc; + lv_draw_rect_dsc_t draw_rect_def_dsc; + lv_draw_label_dsc_t draw_label_def_dsc; lv_draw_rect_dsc_t draw_rect_tmp_dsc; lv_draw_label_dsc_t draw_label_tmp_dsc; - lv_state_t state_ori = btnm->state; - _lv_obj_disable_style_caching(btnm, true); - btnm->state = LV_STATE_DEFAULT; - lv_draw_rect_dsc_init(&draw_rect_rel_dsc); - lv_draw_label_dsc_init(&draw_label_rel_dsc); - lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_rel_dsc); - lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_rel_dsc); - draw_label_rel_dsc.flag = txt_flag; - btnm->state = state_ori; - _lv_obj_disable_style_caching(btnm, false); + lv_state_t state_ori = obj->state; + obj->state = LV_STATE_DEFAULT; + obj->style_list.skip_trans = 1; + lv_draw_rect_dsc_init(&draw_rect_def_dsc); + lv_draw_label_dsc_init(&draw_label_def_dsc); + lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEM, &draw_rect_def_dsc); + lv_obj_init_draw_label_dsc(obj, LV_PART_ITEM, &draw_label_def_dsc); + draw_label_def_dsc.flag = txt_flag; + obj->style_list.skip_trans = 0; + obj->state = state_ori; - bool chk_inited = false; - bool disabled_inited = false; - - lv_style_int_t padding_top = lv_obj_get_style_pad_top(btnm, LV_BTNMATRIX_PART_MAIN); - lv_style_int_t padding_bottom = lv_obj_get_style_pad_bottom(btnm, LV_BTNMATRIX_PART_MAIN); + lv_coord_t ptop = lv_obj_get_style_pad_top(obj, LV_PART_MAIN); + lv_coord_t pbottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN); + lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN); + lv_coord_t pright = lv_obj_get_style_pad_right(obj, LV_PART_MAIN); #if LV_USE_ARABIC_PERSIAN_CHARS const size_t txt_ap_size = 256 ; char * txt_ap = _lv_mem_buf_get(txt_ap_size); #endif + for(btn_i = 0; btn_i < ext->btn_cnt; btn_i++, txt_i++) { /*Search the next valid text in the map*/ while(strcmp(ext->map_p[txt_i], "\n") == 0) { @@ -657,16 +654,7 @@ static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * cl /*Skip hidden buttons*/ if(button_is_hidden(ext->ctrl_bits[btn_i])) continue; - lv_area_copy(&area_tmp, &ext->button_areas[btn_i]); - area_tmp.x1 += area_btnm.x1; - area_tmp.y1 += area_btnm.y1; - area_tmp.x2 += area_btnm.x1; - area_tmp.y2 += area_btnm.y1; - - btn_w = lv_area_get_width(&area_tmp); - btn_h = lv_area_get_height(&area_tmp); - - /*Choose the style*/ + /*Get the state of the button*/ lv_draw_rect_dsc_t * draw_rect_dsc_act; lv_draw_label_dsc_t * draw_label_dsc_act; lv_state_t btn_state = LV_STATE_DEFAULT; @@ -677,92 +665,61 @@ static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * cl btn_state |= LV_STATE_FOCUSED; if(state_ori & LV_STATE_EDITED) btn_state |= LV_STATE_EDITED; } - bool type2 = button_is_type2(ext->ctrl_bits[btn_i]); - if(btn_state == LV_STATE_DEFAULT && type2 == false) { - draw_rect_dsc_act = &draw_rect_rel_dsc; - draw_label_dsc_act = &draw_label_rel_dsc; + /*Get the button's area*/ + lv_area_copy(&btn_area, &ext->button_areas[btn_i]); + btn_area.x1 += area_obj.x1; + btn_area.y1 += area_obj.y1; + btn_area.x2 += area_obj.x1; + btn_area.y2 += area_obj.y1; + + /*Use the custom drawer if any*/ + if(ext->custom_drawer_cb) { + obj->state = btn_state; + bool drawn = ext->custom_drawer_cb(obj, btn_i, &btn_area, clip_area); + obj->state = state_ori; + if(drawn) continue; } - else if(btn_state == LV_STATE_CHECKED && type2 == false) { - if(!chk_inited) { - btnm->state = LV_STATE_CHECKED; - _lv_obj_disable_style_caching(btnm, true); - lv_draw_rect_dsc_init(&draw_rect_chk_dsc); - lv_draw_label_dsc_init(&draw_label_chk_dsc); - lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_chk_dsc); - lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_chk_dsc); - draw_label_chk_dsc.flag = txt_flag; - btnm->state = state_ori; - _lv_obj_disable_style_caching(btnm, false); - chk_inited = true; - } - draw_rect_dsc_act = &draw_rect_chk_dsc; - draw_label_dsc_act = &draw_label_chk_dsc; - } - else if(btn_state == LV_STATE_DISABLED && type2 == false) { - if(!disabled_inited) { - btnm->state = LV_STATE_DISABLED; - _lv_obj_disable_style_caching(btnm, true); - lv_draw_rect_dsc_init(&draw_rect_ina_dsc); - lv_draw_label_dsc_init(&draw_label_ina_dsc); - lv_obj_init_draw_rect_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_rect_ina_dsc); - lv_obj_init_draw_label_dsc(btnm, LV_BTNMATRIX_PART_BTN, &draw_label_ina_dsc); - draw_label_ina_dsc.flag = txt_flag; - btnm->state = state_ori; - _lv_obj_disable_style_caching(btnm, false); - disabled_inited = true; - } - draw_rect_dsc_act = &draw_rect_ina_dsc; - draw_label_dsc_act = &draw_label_ina_dsc; + + /*Set up the draw descriptors*/ + if(btn_state == LV_STATE_DEFAULT) { + obj->state = btn_state; + draw_rect_dsc_act = &draw_rect_def_dsc; + draw_label_dsc_act = &draw_label_def_dsc; + obj->state = state_ori; } /*In other cases get the styles directly without caching them*/ else { - btnm->state = btn_state; - _lv_obj_disable_style_caching(btnm, true); + obj->state = btn_state; lv_draw_rect_dsc_init(&draw_rect_tmp_dsc); lv_draw_label_dsc_init(&draw_label_tmp_dsc); - lv_obj_init_draw_rect_dsc(btnm, type2 ? LV_BTNMATRIX_PART_BTN_2 : LV_BTNMATRIX_PART_BTN, &draw_rect_tmp_dsc); - lv_obj_init_draw_label_dsc(btnm, type2 ? LV_BTNMATRIX_PART_BTN_2 : LV_BTNMATRIX_PART_BTN, &draw_label_tmp_dsc); + lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEM, &draw_rect_tmp_dsc); + lv_obj_init_draw_label_dsc(obj, LV_PART_ITEM, &draw_label_tmp_dsc); draw_label_tmp_dsc.flag = txt_flag; draw_rect_dsc_act = &draw_rect_tmp_dsc; draw_label_dsc_act = &draw_label_tmp_dsc; - btnm->state = state_ori; - _lv_obj_disable_style_caching(btnm, false); + obj->state = state_ori; } - lv_style_int_t border_part_ori = draw_rect_dsc_act->border_side; + lv_coord_t border_part_ori = draw_rect_dsc_act->border_side; /*Remove borders on the edges if `LV_BORDER_INTERNAL`*/ if(border_part_ori & LV_BORDER_SIDE_INTERNAL) { - /*Top/Bottom lines*/ - if(area_tmp.y1 == btnm->coords.y1 + padding_top) { - draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_TOP; - } - if(area_tmp.y2 == btnm->coords.y2 - padding_bottom) { - draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_BOTTOM; - } - - /*Left/right columns*/ - if(txt_i == 0) { /*First button*/ - draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_LEFT; - } - else if(strcmp(ext->map_p[txt_i - 1], "\n") == 0) { - draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_LEFT; - } - - if(ext->map_p[txt_i + 1][0] == '\0' || strcmp(ext->map_p[txt_i + 1], "\n") == 0) { - draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_RIGHT; - } + if(btn_area.x1 == obj->coords.x1 + pleft) draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_LEFT; + if(btn_area.y2 == obj->coords.x2 - pright) draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_RIGHT; + if(btn_area.y1 == obj->coords.y1 + ptop) draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_TOP; + if(btn_area.y2 == obj->coords.y2 - pbottom) draw_rect_dsc_act->border_side &= ~LV_BORDER_SIDE_BOTTOM; } - lv_draw_rect(&area_tmp, clip_area, draw_rect_dsc_act); + /*Draw the background*/ + lv_draw_rect(&btn_area, clip_area, draw_rect_dsc_act); - draw_rect_dsc_act->border_side = border_part_ori; + draw_rect_dsc_act->border_side = border_part_ori; /*REstore the original border for the next button*/ /*Calculate the size of the text*/ const lv_font_t * font = draw_label_dsc_act->font; - lv_style_int_t letter_space = draw_label_dsc_act->letter_space; - lv_style_int_t line_space = draw_label_dsc_act->line_space; + lv_coord_t letter_space = draw_label_dsc_act->letter_space; + lv_coord_t line_space = draw_label_dsc_act->line_space; const char * txt = ext->map_p[txt_i]; #if LV_USE_ARABIC_PERSIAN_CHARS @@ -776,90 +733,81 @@ static lv_design_res_t lv_btnmatrix_design(lv_obj_t * btnm, const lv_area_t * cl lv_point_t txt_size; _lv_txt_get_size(&txt_size, txt, font, letter_space, - line_space, lv_area_get_width(&area_btnm), txt_flag); + line_space, lv_area_get_width(&area_obj), txt_flag); - area_tmp.x1 += (btn_w - txt_size.x) / 2; - area_tmp.y1 += (btn_h - txt_size.y) / 2; - area_tmp.x2 = area_tmp.x1 + txt_size.x; - area_tmp.y2 = area_tmp.y1 + txt_size.y; + btn_area.x1 += (lv_area_get_width(&btn_area) - txt_size.x) / 2; + btn_area.y1 += (lv_area_get_height(&btn_area) - txt_size.y) / 2; + btn_area.x2 = btn_area.x1 + txt_size.x; + btn_area.y2 = btn_area.y1 + txt_size.y; - lv_draw_label(&area_tmp, clip_area, draw_label_dsc_act, txt, NULL); + /*Draw the text*/ + lv_draw_label(&btn_area, clip_area, draw_label_dsc_act, txt, NULL); } + obj->style_list.skip_trans = 0; #if LV_USE_ARABIC_PERSIAN_CHARS _lv_mem_buf_release(txt_ap); #endif } else if(mode == LV_DESIGN_DRAW_POST) { - ancestor_design_f(btnm, clip_area, mode); + lv_obj.design_cb(obj, clip_area, mode); } return LV_DESIGN_RES_OK; } /** * Signal function of the button matrix - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix 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_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * param) +static lv_res_t lv_btnmatrix_signal(lv_obj_t * obj, lv_signal_t sign, void * param) { lv_res_t res; - if(sign == LV_SIGNAL_GET_STYLE) { - lv_get_style_info_t * info = param; - info->result = lv_btnmatrix_get_style(btnm, info->part); - if(info->result != NULL) return LV_RES_OK; - else return ancestor_signal(btnm, sign, param); - } /* Include the ancient signal function */ - res = ancestor_signal(btnm, sign, param); + res = lv_obj.signal_cb(obj, sign, param); if(res != LV_RES_OK) return res; - if(sign == LV_SIGNAL_GET_TYPE) return _lv_obj_handle_get_type_signal(param, LV_OBJX_NAME); - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; lv_point_t p; - if(sign == LV_SIGNAL_CLEANUP) { - _lv_obj_reset_style_list_no_refr(btnm, LV_BTNMATRIX_PART_BTN); - lv_mem_free(ext->button_areas); - lv_mem_free(ext->ctrl_bits); - } - else if(sign == LV_SIGNAL_STYLE_CHG) { - lv_btnmatrix_set_map(btnm, ext->map_p); + + if(sign == LV_SIGNAL_STYLE_CHG) { + lv_btnmatrix_set_map(obj, ext->map_p); } else if(sign == LV_SIGNAL_COORD_CHG) { if(param && - (lv_obj_get_width(btnm) != lv_area_get_width(param) || lv_obj_get_height(btnm) != lv_area_get_height(param))) + (lv_obj_get_width(obj) != lv_area_get_width(param) || lv_obj_get_height(obj) != lv_area_get_height(param))) { - lv_btnmatrix_set_map(btnm, ext->map_p); + lv_btnmatrix_set_map(obj, ext->map_p); } } else if(sign == LV_SIGNAL_PRESSED) { - invalidate_button_area(btnm, ext->btn_id_pr); + invalidate_button_area(obj, ext->btn_id_pr); lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act()); if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) { uint16_t btn_pr; /*Search the pressed area*/ lv_indev_get_point(param, &p); - btn_pr = get_button_from_point(btnm, &p); + btn_pr = get_button_from_point(obj, &p); /*Handle the case where there is no button there*/ if(btn_pr != LV_BTNMATRIX_BTN_NONE) { if(button_is_inactive(ext->ctrl_bits[btn_pr]) == false && button_is_hidden(ext->ctrl_bits[btn_pr]) == false) { - invalidate_button_area(btnm, ext->btn_id_pr) /*Invalidate the old area*/; + invalidate_button_area(obj, ext->btn_id_pr) /*Invalidate the old area*/; ext->btn_id_pr = btn_pr; ext->btn_id_act = btn_pr; - invalidate_button_area(btnm, ext->btn_id_pr); /*Invalidate the new area*/ + invalidate_button_area(obj, ext->btn_id_pr); /*Invalidate the new area*/ } } } #if LV_USE_GROUP else if(indev_type == LV_INDEV_TYPE_KEYPAD || (indev_type == LV_INDEV_TYPE_ENCODER && - lv_group_get_editing(lv_obj_get_group(btnm)))) { + lv_group_get_editing(lv_obj_get_group(obj)))) { ext->btn_id_pr = ext->btn_id_focused; - invalidate_button_area(btnm, ext->btn_id_focused); + invalidate_button_area(obj, ext->btn_id_focused); } #endif @@ -868,7 +816,7 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa button_is_inactive(ext->ctrl_bits[ext->btn_id_pr]) == false && button_is_hidden(ext->ctrl_bits[ext->btn_id_pr]) == false) { uint32_t b = ext->btn_id_pr; - res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b); + res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b); } } } @@ -880,11 +828,11 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa if(indev_type == LV_INDEV_TYPE_ENCODER || indev_type == LV_INDEV_TYPE_KEYPAD) return LV_RES_OK; lv_indev_get_point(indev, &p); - btn_pr = get_button_from_point(btnm, &p); + btn_pr = get_button_from_point(obj, &p); /*Invalidate to old and the new areas*/ if(btn_pr != ext->btn_id_pr) { if(ext->btn_id_pr != LV_BTNMATRIX_BTN_NONE) { - invalidate_button_area(btnm, ext->btn_id_pr); + invalidate_button_area(obj, ext->btn_id_pr); } ext->btn_id_pr = btn_pr; @@ -894,11 +842,11 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa if(btn_pr != LV_BTNMATRIX_BTN_NONE && button_is_inactive(ext->ctrl_bits[btn_pr]) == false && button_is_hidden(ext->ctrl_bits[btn_pr]) == false) { - invalidate_button_area(btnm, btn_pr); + invalidate_button_area(obj, btn_pr); /* Send VALUE_CHANGED for the newly pressed button */ if(button_is_click_trig(ext->ctrl_bits[btn_pr]) == false) { uint32_t b = btn_pr; - lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b); + lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b); } } } @@ -914,12 +862,12 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa else { ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNMATRIX_CTRL_CHECKED; } - if(ext->one_check) make_one_button_checked(btnm, ext->btn_id_pr); + if(ext->one_check) make_one_button_checked(obj, ext->btn_id_pr); } /*Invalidate to old pressed area*/; - invalidate_button_area(btnm, ext->btn_id_pr); - invalidate_button_area(btnm, ext->btn_id_focused); + invalidate_button_area(obj, ext->btn_id_pr); + invalidate_button_area(obj, ext->btn_id_focused); lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act()); if(indev_type == LV_INDEV_TYPE_KEYPAD || indev_type == LV_INDEV_TYPE_ENCODER) { @@ -932,7 +880,7 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false && button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) { uint32_t b = ext->btn_id_act; - res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b); + res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b); } } } @@ -942,14 +890,14 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false && button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) { uint32_t b = ext->btn_id_act; - res = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b); + res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b); } } } else if(sign == LV_SIGNAL_PRESS_LOST) { ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE; ext->btn_id_act = LV_BTNMATRIX_BTN_NONE; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } else if(sign == LV_SIGNAL_FOCUS) { #if LV_USE_GROUP @@ -964,7 +912,7 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa if(indev_type == LV_INDEV_TYPE_ENCODER) { /*In navigation mode don't select any button but in edit mode select the fist*/ - if(lv_group_get_editing(lv_obj_get_group(btnm))) { + if(lv_group_get_editing(lv_obj_get_group(obj))) { uint32_t b = 0; while(button_is_hidden(ext->ctrl_bits[b]) || button_is_inactive(ext->ctrl_bits[b])) b++; ext->btn_id_focused = b; @@ -986,8 +934,8 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa #endif } else if(sign == LV_SIGNAL_DEFOCUS || sign == LV_SIGNAL_LEAVE) { - if(ext->btn_id_focused != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(btnm, ext->btn_id_focused); - if(ext->btn_id_pr != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(btnm, ext->btn_id_pr); + if(ext->btn_id_focused != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(obj, ext->btn_id_focused); + if(ext->btn_id_pr != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(obj, ext->btn_id_pr); ext->btn_id_focused = LV_BTNMATRIX_BTN_NONE; ext->btn_id_pr = LV_BTNMATRIX_BTN_NONE; ext->btn_id_act = LV_BTNMATRIX_BTN_NONE; @@ -1006,7 +954,7 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa } ext->btn_id_act = ext->btn_id_focused; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } else if(c == LV_KEY_LEFT) { if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE) ext->btn_id_focused = 0; @@ -1018,10 +966,10 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa } ext->btn_id_act = ext->btn_id_focused; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } else if(c == LV_KEY_DOWN) { - lv_coord_t col_gap = LV_MATH_MAX(lv_obj_get_style_margin_left(btnm, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_right(btnm, LV_BTNMATRIX_PART_BTN)); + lv_coord_t col_gap = LV_MATH_MAX(lv_obj_get_style_margin_left(obj, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_right(obj, LV_BTNMATRIX_PART_BTN)); /*Find the area below the the current*/ if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE) { @@ -1045,10 +993,10 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa if(area_below < ext->btn_cnt) ext->btn_id_focused = area_below; } ext->btn_id_act = ext->btn_id_focused; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } else if(c == LV_KEY_UP) { - lv_coord_t col_gap = LV_MATH_MAX(lv_obj_get_style_margin_left(btnm, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_right(btnm, LV_BTNMATRIX_PART_BTN)); + lv_coord_t col_gap = LV_MATH_MAX(lv_obj_get_style_margin_left(obj, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_right(obj, LV_BTNMATRIX_PART_BTN)); /*Find the area below the the current*/ if(ext->btn_id_focused == LV_BTNMATRIX_BTN_NONE) { ext->btn_id_focused = 0; @@ -1070,7 +1018,7 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa if(area_above >= 0) ext->btn_id_focused = area_above; } ext->btn_id_act = ext->btn_id_focused; - lv_obj_invalidate(btnm); + lv_obj_invalidate(obj); } #endif } @@ -1083,43 +1031,12 @@ static lv_res_t lv_btnmatrix_signal(lv_obj_t * btnm, lv_signal_t sign, void * pa return res; } -/** - * Get the style descriptor of a part of the object - * @param btnm pointer the object - * @param part the part of the object. (LV_BTNMATRIX_PART_...) - * @return pointer to the style descriptor of the specified part - */ -static lv_style_list_t * lv_btnmatrix_get_style(lv_obj_t * btnm, uint8_t part) -{ - LV_ASSERT_OBJ(btnm, LV_OBJX_NAME); - - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); - - lv_style_list_t * style_dsc_p; - - switch(part) { - case LV_BTNMATRIX_PART_MAIN: - style_dsc_p = &btnm->style_list; - break; - case LV_BTNMATRIX_PART_BTN: - style_dsc_p = &ext->style_btn; - break; - case LV_BTNMATRIX_PART_BTN_2: - style_dsc_p = &ext->style_btn2; - break; - default: - style_dsc_p = NULL; - } - - return style_dsc_p; -} - /** * Create the required number of buttons and control bytes according to a map - * @param btnm pointer to button matrix object + * @param obj pointer to button matrix object * @param map_p pointer to a string array */ -static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map) +static void allocate_btn_areas_and_controls(const lv_obj_t * obj, const char ** map) { /*Count the buttons in the map*/ uint16_t btn_cnt = 0; @@ -1131,7 +1048,7 @@ static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** i++; } - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; /*Do not allocate memory for the same amount of buttons*/ if(btn_cnt == ext->btn_cnt) return; @@ -1198,61 +1115,54 @@ static bool button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits) return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKED) ? true : false; } -static bool button_is_type2(lv_btnmatrix_ctrl_t ctrl_bits) -{ - return (ctrl_bits & LV_BTNMATRIX_CTRL_TYPE_2) ? true : false; -} - - /** * Gives the button id of a button under a given point - * @param btnm pointer to a button matrix object + * @param obj pointer to a button matrix object * @param p a point with absolute coordinates * @return the id of the button or LV_BTNMATRIX_BTN_NONE. */ -static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p) +static uint16_t get_button_from_point(lv_obj_t * obj, lv_point_t * p) { - lv_area_t btnm_cords; + lv_area_t obj_cords; lv_area_t btn_area; - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; uint16_t i; - lv_obj_get_coords(btnm, &btnm_cords); - - lv_coord_t w = lv_obj_get_width(btnm); - lv_coord_t h = lv_obj_get_height(btnm); - lv_style_int_t pleft = lv_obj_get_style_pad_left(btnm, LV_BTNMATRIX_PART_MAIN); - lv_style_int_t pright = lv_obj_get_style_pad_right(btnm, LV_BTNMATRIX_PART_MAIN); - lv_style_int_t ptop = lv_obj_get_style_pad_top(btnm, LV_BTNMATRIX_PART_MAIN); - lv_style_int_t pbottom = lv_obj_get_style_pad_bottom(btnm, LV_BTNMATRIX_PART_MAIN); - lv_coord_t row_gap = LV_MATH_MAX(lv_obj_get_style_margin_top(btnm, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_bottom(btnm, LV_BTNMATRIX_PART_BTN)); - lv_coord_t col_gap = LV_MATH_MAX(lv_obj_get_style_margin_left(btnm, LV_BTNMATRIX_PART_BTN), lv_obj_get_style_margin_right(btnm, LV_BTNMATRIX_PART_BTN)); + lv_obj_get_coords(obj, &obj_cords); + lv_coord_t w = lv_obj_get_width(obj); + lv_coord_t h = lv_obj_get_height(obj); + lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN); + lv_coord_t pright = lv_obj_get_style_pad_right(obj, LV_PART_MAIN); + lv_coord_t ptop = lv_obj_get_style_pad_top(obj, LV_PART_MAIN); + lv_coord_t pbottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN); + lv_coord_t prow = lv_obj_get_style_pad_row(obj, LV_PART_MAIN); + lv_coord_t pcol = lv_obj_get_style_pad_column(obj, LV_PART_MAIN); /*Get the half gap. Button look larger with this value. (+1 for rounding error)*/ - row_gap = (row_gap / 2) + 1 + (row_gap & 1); - col_gap = (col_gap / 2) + 1 + (col_gap & 1); + prow = (prow / 2) + 1 + (prow & 1); + pcol = (pcol / 2) + 1 + (pcol & 1); - row_gap = LV_MATH_MIN(row_gap, BTN_EXTRA_CLICK_AREA_MAX); - col_gap = LV_MATH_MIN(col_gap, BTN_EXTRA_CLICK_AREA_MAX); + prow = LV_MATH_MIN(prow, BTN_EXTRA_CLICK_AREA_MAX); + pcol = LV_MATH_MIN(pcol, BTN_EXTRA_CLICK_AREA_MAX); pright = LV_MATH_MIN(pright, BTN_EXTRA_CLICK_AREA_MAX); ptop = LV_MATH_MIN(ptop, BTN_EXTRA_CLICK_AREA_MAX); pbottom = LV_MATH_MIN(pbottom, BTN_EXTRA_CLICK_AREA_MAX); for(i = 0; i < ext->btn_cnt; i++) { lv_area_copy(&btn_area, &ext->button_areas[i]); - if(btn_area.x1 <= pleft) btn_area.x1 += btnm_cords.x1 - LV_MATH_MIN(pleft, BTN_EXTRA_CLICK_AREA_MAX); - else btn_area.x1 += btnm_cords.x1 - col_gap; + if(btn_area.x1 <= pleft) btn_area.x1 += obj_cords.x1 - LV_MATH_MIN(pleft, BTN_EXTRA_CLICK_AREA_MAX); + else btn_area.x1 += obj_cords.x1 - pcol; - if(btn_area.y1 <= ptop) btn_area.y1 += btnm_cords.y1 - LV_MATH_MIN(ptop, BTN_EXTRA_CLICK_AREA_MAX); - else btn_area.y1 += btnm_cords.y1 - row_gap; + if(btn_area.y1 <= ptop) btn_area.y1 += obj_cords.y1 - LV_MATH_MIN(ptop, BTN_EXTRA_CLICK_AREA_MAX); + else btn_area.y1 += obj_cords.y1 - prow; - if(btn_area.x2 >= w - pright - 2) btn_area.x2 += btnm_cords.x1 + LV_MATH_MIN(pright, + if(btn_area.x2 >= w - pright - 2) btn_area.x2 += obj_cords.x1 + LV_MATH_MIN(pright, BTN_EXTRA_CLICK_AREA_MAX); /*-2 for rounding error*/ - else btn_area.x2 += btnm_cords.x1 + col_gap; + else btn_area.x2 += obj_cords.x1 + pcol; - if(btn_area.y2 >= h - pbottom - 2) btn_area.y2 += btnm_cords.y1 + LV_MATH_MIN(pbottom, + if(btn_area.y2 >= h - pbottom - 2) btn_area.y2 += obj_cords.y1 + LV_MATH_MIN(pbottom, BTN_EXTRA_CLICK_AREA_MAX); /*-2 for rounding error*/ - else btn_area.y2 += btnm_cords.y1 + row_gap; + else btn_area.y2 += obj_cords.y1 + prow; if(_lv_area_is_point_on(&btn_area, p, 0) != false) { break; @@ -1264,40 +1174,40 @@ static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p) return i; } -static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx) +static void invalidate_button_area(const lv_obj_t * obj, uint16_t btn_idx) { if(btn_idx == LV_BTNMATRIX_BTN_NONE) return; lv_area_t btn_area; - lv_area_t btnm_area; + lv_area_t obj_area; - lv_btnmatrix_ext_t * ext = lv_obj_get_ext_attr(btnm); + lv_btnmatrix_ext_t * ext = obj->ext_attr;; lv_area_copy(&btn_area, &ext->button_areas[btn_idx]); - lv_obj_get_coords(btnm, &btnm_area); + lv_obj_get_coords(obj, &obj_area); /* Convert relative coordinates to absolute */ - btn_area.x1 += btnm_area.x1; - btn_area.y1 += btnm_area.y1; - btn_area.x2 += btnm_area.x1; - btn_area.y2 += btnm_area.y1; + btn_area.x1 += obj_area.x1; + btn_area.y1 += obj_area.y1; + btn_area.x2 += obj_area.x1; + btn_area.y2 += obj_area.y1; - lv_obj_invalidate_area(btnm, &btn_area); + lv_obj_invalidate_area(obj, &btn_area); } /** * Enforces a single button being toggled on the button matrix. * It simply clears the toggle flag on other buttons. - * @param btnm Button matrix object + * @param obj Button matrix object * @param btn_idx Button that should remain toggled */ -static void make_one_button_checked(lv_obj_t * btnm, uint16_t btn_idx) +static void make_one_button_checked(lv_obj_t * obj, uint16_t btn_idx) { /*Save whether the button was toggled*/ - bool was_toggled = lv_btnmatrix_get_btn_ctrl(btnm, btn_idx, LV_BTNMATRIX_CTRL_CHECKED); + bool was_toggled = lv_btnmatrix_get_btn_ctrl(obj, btn_idx, LV_BTNMATRIX_CTRL_CHECKED); - lv_btnmatrix_clear_btn_ctrl_all(btnm, LV_BTNMATRIX_CTRL_CHECKED); + lv_btnmatrix_clear_btn_ctrl_all(obj, LV_BTNMATRIX_CTRL_CHECKED); - if(was_toggled) lv_btnmatrix_set_btn_ctrl(btnm, btn_idx, LV_BTNMATRIX_CTRL_CHECKED); + if(was_toggled) lv_btnmatrix_set_btn_ctrl(obj, btn_idx, LV_BTNMATRIX_CTRL_CHECKED); } #endif diff --git a/src/lv_widgets/lv_btnmatrix.h b/src/lv_widgets/lv_btnmatrix.h index 881a58185..6d6044317 100644 --- a/src/lv_widgets/lv_btnmatrix.h +++ b/src/lv_widgets/lv_btnmatrix.h @@ -42,10 +42,11 @@ enum { LV_BTNMATRIX_CTRL_CHECKABLE = 0x0040, /**< Button *can* be toggled. */ LV_BTNMATRIX_CTRL_CHECKED = 0x0080, /**< Button is currently toggled (e.g. checked). */ LV_BTNMATRIX_CTRL_CLICK_TRIG = 0x0100, /**< 1: Send LV_EVENT_VALUE_CHANGE on CLICK, 0: Send LV_EVENT_VALUE_CHANGE on PRESS*/ - LV_BTNMATRIX_CTRL_TYPE_2 = 0x0200, /**< Render the button with `LV_BTNMATRIX_PART_BTN2` style*/ }; typedef uint16_t lv_btnmatrix_ctrl_t; +typedef bool (*lv_btnmatrix_btn_drawer_cb_t)(lv_obj_t * btnm, uint32_t btn_id, const lv_area_t * draw_area, const lv_area_t * clip_area); + /*Data of button matrix*/ typedef struct { /*No inherited ext.*/ /*Ext. of ancestor*/ @@ -53,6 +54,7 @@ typedef struct { const char ** map_p; /*Pointer to the current map*/ lv_area_t * button_areas; /*Array of areas of buttons*/ lv_btnmatrix_ctrl_t * ctrl_bits; /*Array of control bytes*/ + lv_btnmatrix_btn_drawer_cb_t custom_drawer_cb; uint16_t btn_cnt; /*Number of button in 'map_p'(Handled by the library)*/ uint16_t btn_id_pr; /*Index of the currently pressed button or LV_BTNMATRIX_BTN_NONE*/ uint16_t btn_id_focused; /*Index of the currently focused button or LV_BTNMATRIX_BTN_NONE*/ @@ -62,12 +64,8 @@ typedef struct { uint8_t align : 2; /*Align type from 'lv_label_align_t'*/ } lv_btnmatrix_ext_t; -enum { - LV_BTNMATRIX_PART_MAIN, - LV_BTNMATRIX_PART_BTN, - LV_BTNMATRIX_PART_BTN_2, -}; -typedef uint8_t lv_btnmatrix_part_t; + +extern const lv_obj_class_t lv_btnmatrix; /********************** * GLOBAL PROTOTYPES diff --git a/src/lv_widgets/lv_gauge.c b/src/lv_widgets/lv_gauge.c index c8fa0cbe5..116801acd 100644 --- a/src/lv_widgets/lv_gauge.c +++ b/src/lv_widgets/lv_gauge.c @@ -199,10 +199,10 @@ void lv_gauge_set_value(lv_obj_t * gauge, uint8_t needle_id, int32_t value) // lv_obj_invalidate(gauge); - lv_style_int_t pad = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_NEEDLE); - lv_style_int_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t pad = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_NEEDLE); + lv_coord_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN); lv_coord_t r = (lv_obj_get_width(gauge) - left - right) / 2 - pad; lv_coord_t x_ofs = gauge->coords.x1 + r + left + pad; lv_coord_t y_ofs = gauge->coords.y1 + r + top + pad; @@ -509,11 +509,11 @@ static lv_style_list_t * lv_gauge_get_style(lv_obj_t * gauge, uint8_t part) static void lv_gauge_draw_labels(lv_obj_t * gauge, const lv_area_t * mask) { lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge); - lv_style_int_t scale_width = lv_obj_get_style_scale_width(gauge, LV_GAUGE_PART_MAJOR); - lv_style_int_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t txt_pad = left; + lv_coord_t scale_width = lv_obj_get_style_scale_width(gauge, LV_GAUGE_PART_MAJOR); + lv_coord_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t txt_pad = left; lv_coord_t r = (lv_obj_get_width(gauge) - left - right) / 2 - scale_width - txt_pad; lv_coord_t x_ofs = gauge->coords.x1 + r + left + scale_width + txt_pad; lv_coord_t y_ofs = gauge->coords.y1 + r + top + scale_width + txt_pad; @@ -569,10 +569,10 @@ static void lv_gauge_draw_needle(lv_obj_t * gauge, const lv_area_t * clip_area) { lv_gauge_ext_t * ext = lv_obj_get_ext_attr(gauge); - lv_style_int_t pad = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_NEEDLE); - lv_style_int_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t pad = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_NEEDLE); + lv_coord_t left = lv_obj_get_style_pad_left(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t right = lv_obj_get_style_pad_right(gauge, LV_GAUGE_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(gauge, LV_GAUGE_PART_MAIN); lv_coord_t r = (lv_obj_get_width(gauge) - left - right) / 2 - pad; lv_coord_t x_ofs = gauge->coords.x1 + r + left + pad; @@ -638,7 +638,7 @@ static void lv_gauge_draw_needle(lv_obj_t * gauge, const lv_area_t * clip_area) lv_draw_rect_dsc_t mid_dsc; lv_draw_rect_dsc_init(&mid_dsc); lv_obj_init_draw_rect_dsc(gauge, LV_GAUGE_PART_NEEDLE, &mid_dsc); - lv_style_int_t size = lv_obj_get_style_size(gauge, LV_GAUGE_PART_NEEDLE) / 2; + lv_coord_t size = lv_obj_get_style_size(gauge, LV_GAUGE_PART_NEEDLE) / 2; lv_area_t nm_cord; nm_cord.x1 = x_ofs - size; nm_cord.y1 = y_ofs - size; diff --git a/src/lv_widgets/lv_img.c b/src/lv_widgets/lv_img.c index e3767a62d..5703fb4b9 100644 --- a/src/lv_widgets/lv_img.c +++ b/src/lv_widgets/lv_img.c @@ -202,8 +202,8 @@ void lv_img_set_src(lv_obj_t * img, const void * src_img) if(src_type == LV_IMG_SRC_SYMBOL) { /*`lv_img_dsc_get_info` couldn't set the with and height of a font so set it here*/ const lv_font_t * font = lv_obj_get_style_text_font(img, LV_IMG_PART_MAIN); - lv_style_int_t letter_space = lv_obj_get_style_text_letter_space(img, LV_IMG_PART_MAIN); - lv_style_int_t line_space = lv_obj_get_style_text_line_space(img, LV_IMG_PART_MAIN); + lv_coord_t letter_space = lv_obj_get_style_text_letter_space(img, LV_IMG_PART_MAIN); + lv_coord_t line_space = lv_obj_get_style_text_line_space(img, LV_IMG_PART_MAIN); lv_point_t size; _lv_txt_get_size(&size, src_img, font, letter_space, line_space, LV_COORD_MAX, LV_TXT_FLAG_NONE); @@ -274,10 +274,10 @@ void lv_img_set_pivot(lv_obj_t * img, lv_coord_t x, lv_coord_t y) lv_img_ext_t * ext = lv_obj_get_ext_attr(img); if(ext->pivot.x == x && ext->pivot.y == y) return; - lv_style_int_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); + lv_coord_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); transf_zoom = (transf_zoom * ext->zoom) >> 8; - lv_style_int_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); + lv_coord_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); transf_angle += ext->angle; lv_coord_t w = lv_obj_get_width(img); @@ -315,10 +315,10 @@ void lv_img_set_angle(lv_obj_t * img, int16_t angle) lv_img_ext_t * ext = lv_obj_get_ext_attr(img); if(angle == ext->angle) return; - lv_style_int_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); + lv_coord_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); transf_zoom = (transf_zoom * ext->zoom) >> 8; - lv_style_int_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); + lv_coord_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); lv_coord_t w = lv_obj_get_width(img); lv_coord_t h = lv_obj_get_height(img); @@ -358,9 +358,9 @@ void lv_img_set_zoom(lv_obj_t * img, uint16_t zoom) if(zoom == 0) zoom = 1; - lv_style_int_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); + lv_coord_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); - lv_style_int_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); + lv_coord_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); transf_angle += ext->angle; lv_coord_t w = lv_obj_get_width(img); @@ -755,10 +755,10 @@ static lv_res_t lv_img_signal(lv_obj_t * img, lv_signal_t sign, void * param) else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) { lv_coord_t * s = param; - lv_style_int_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); + lv_coord_t transf_zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); transf_zoom = (transf_zoom * ext->zoom) >> 8; - lv_style_int_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); + lv_coord_t transf_angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); transf_angle += ext->angle; /*If the image has angle provide enough room for the rotated corners */ @@ -775,10 +775,10 @@ static lv_res_t lv_img_signal(lv_obj_t * img, lv_signal_t sign, void * param) } /*Handle the padding of the background*/ - lv_style_int_t left = lv_obj_get_style_pad_left(img, LV_IMG_PART_MAIN); - lv_style_int_t right = lv_obj_get_style_pad_right(img, LV_IMG_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(img, LV_IMG_PART_MAIN); - lv_style_int_t bottom = lv_obj_get_style_pad_bottom(img, LV_IMG_PART_MAIN); + lv_coord_t left = lv_obj_get_style_pad_left(img, LV_IMG_PART_MAIN); + lv_coord_t right = lv_obj_get_style_pad_right(img, LV_IMG_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(img, LV_IMG_PART_MAIN); + lv_coord_t bottom = lv_obj_get_style_pad_bottom(img, LV_IMG_PART_MAIN); *s = LV_MATH_MAX(*s, left); *s = LV_MATH_MAX(*s, right); @@ -787,10 +787,10 @@ static lv_res_t lv_img_signal(lv_obj_t * img, lv_signal_t sign, void * param) } else if(sign == LV_SIGNAL_HIT_TEST) { lv_hit_test_info_t * info = param; - lv_style_int_t zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); + lv_coord_t zoom = lv_obj_get_style_transform_zoom(img, LV_IMG_PART_MAIN); zoom = (zoom * ext->zoom) >> 8; - lv_style_int_t angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); + lv_coord_t angle = lv_obj_get_style_transform_angle(img, LV_IMG_PART_MAIN); angle += ext->angle; /* If the object is exactly image sized (not cropped, not mosaic) and transformed diff --git a/src/lv_widgets/lv_imgbtn.c b/src/lv_widgets/lv_imgbtn.c index 6c678bdb1..e30430156 100644 --- a/src/lv_widgets/lv_imgbtn.c +++ b/src/lv_widgets/lv_imgbtn.c @@ -460,10 +460,10 @@ static lv_res_t lv_imgbtn_signal(lv_obj_t * imgbtn, lv_signal_t sign, void * par } else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) { /*Handle the padding of the background*/ - lv_style_int_t left = lv_obj_get_style_pad_left(imgbtn, LV_IMGBTN_PART_MAIN); - lv_style_int_t right = lv_obj_get_style_pad_right(imgbtn, LV_IMGBTN_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(imgbtn, LV_IMGBTN_PART_MAIN); - lv_style_int_t bottom = lv_obj_get_style_pad_bottom(imgbtn, LV_IMGBTN_PART_MAIN); + lv_coord_t left = lv_obj_get_style_pad_left(imgbtn, LV_IMGBTN_PART_MAIN); + lv_coord_t right = lv_obj_get_style_pad_right(imgbtn, LV_IMGBTN_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(imgbtn, LV_IMGBTN_PART_MAIN); + lv_coord_t bottom = lv_obj_get_style_pad_bottom(imgbtn, LV_IMGBTN_PART_MAIN); lv_coord_t * s = param; *s = LV_MATH_MAX(*s, left); diff --git a/src/lv_widgets/lv_label.c b/src/lv_widgets/lv_label.c index 9e04d09b5..e346f07a4 100644 --- a/src/lv_widgets/lv_label.c +++ b/src/lv_widgets/lv_label.c @@ -39,10 +39,8 @@ /********************** * STATIC PROTOTYPES **********************/ -lv_label_class_t lv_label; - static void lv_label_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy); -static void lv_label_destructor(void * obj); +static void lv_label_destructor(lv_obj_t * obj); static lv_res_t lv_label_signal(lv_obj_t * label, lv_signal_t sign, void * param); static lv_design_res_t lv_label_design(lv_obj_t * label, const lv_area_t * clip_area, lv_design_mode_t mode); static void lv_label_revert_dots(lv_obj_t * label); @@ -60,6 +58,13 @@ static void get_txt_coords(const lv_obj_t * label, lv_area_t * area); /********************** * STATIC VARIABLES **********************/ +const lv_obj_class_t lv_label = { + .constructor = lv_label_constructor, + .destructor = lv_label_destructor, + .signal_cb = lv_label_signal, + .design_cb = lv_label_design, + .ext_size = sizeof(lv_label_ext_t), +}; /********************** * MACROS @@ -78,20 +83,7 @@ static void get_txt_coords(const lv_obj_t * label, lv_area_t * area); */ lv_obj_t * lv_label_create(lv_obj_t * parent, const lv_obj_t * copy) { - if(!lv_label._inited) { - LV_CLASS_INIT(lv_label, lv_obj); - lv_label.constructor = lv_label_constructor; - lv_label.destructor = lv_label_destructor; - lv_label.design_cb = lv_label_design; - lv_label.signal_cb = lv_label_signal; - } - - lv_obj_t * obj = lv_class_new(&lv_label); - lv_label.constructor(obj, parent, copy); - - lv_obj_create_finish(obj, parent, copy); - - return obj; + return lv_obj_create_from_class(&lv_label, parent, copy); } /*===================== @@ -106,62 +98,62 @@ lv_obj_t * lv_label_create(lv_obj_t * parent, const lv_obj_t * copy) void lv_label_set_text(lv_obj_t * obj, const char * text) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; lv_obj_invalidate(obj); /*If text is NULL then just refresh with the current text */ - if(text == NULL) text = label->text; + if(text == NULL) text = ext->text; LV_ASSERT_STR(text); - if(label->text == text && label->static_txt == 0) { + if(ext->text == text && ext->static_txt == 0) { /*If set its own text then reallocate it (maybe its size changed)*/ #if LV_USE_ARABIC_PERSIAN_CHARS /*Get the size of the text and process it*/ size_t len = _lv_txt_ap_calc_bytes_cnt(text); - label->text = lv_mem_realloc(label->text, len); - LV_ASSERT_MEM(label->text); - if(label->text == NULL) return; + ext->text = lv_mem_realloc(ext->text, len); + LV_ASSERT_MEM(ext->text); + if(ext->text == NULL) return; - _lv_txt_ap_proc(label->text, label->text); + _lv_txt_ap_proc(ext->text, ext->text); #else - label->text = lv_mem_realloc(label->text, strlen(label->text) + 1); + ext->text = lv_mem_realloc(ext->text, strlen(ext->text) + 1); #endif - LV_ASSERT_MEM(label->text); - if(label->text == NULL) return; + LV_ASSERT_MEM(ext->text); + if(ext->text == NULL) return; } else { /*Free the old text*/ - if(label->text != NULL && label->static_txt == 0) { - lv_mem_free(label->text); - label->text = NULL; + if(ext->text != NULL && ext->static_txt == 0) { + lv_mem_free(ext->text); + ext->text = NULL; } #if LV_USE_ARABIC_PERSIAN_CHARS /*Get the size of the text and process it*/ size_t len = _lv_txt_ap_calc_bytes_cnt(text); - label->text = lv_mem_alloc(len); - LV_ASSERT_MEM(label->text); - if(label->text == NULL) return; + ext->text = lv_mem_alloc(len); + LV_ASSERT_MEM(ext->text); + if(ext->text == NULL) return; - _lv_txt_ap_proc(text, label->text); + _lv_txt_ap_proc(text, ext->text); #else /*Get the size of the text*/ size_t len = strlen(text) + 1; /*Allocate space for the new text*/ - label->text = lv_mem_alloc(len); - LV_ASSERT_MEM(label->text); - if(label->text == NULL) return; - strcpy(label->text, text); + ext->text = lv_mem_alloc(len); + LV_ASSERT_MEM(ext->text); + if(ext->text == NULL) return; + strcpy(ext->text, text); #endif /*Now the text is dynamically allocated*/ - label->static_txt = 0; + ext->static_txt = 0; } lv_label_refr_text(obj); @@ -178,7 +170,7 @@ void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) LV_ASSERT_STR(fmt); lv_obj_invalidate(obj); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; /*If text is NULL then refresh */ if(fmt == NULL) { @@ -186,16 +178,16 @@ void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) return; } - if(label->text != NULL && label->static_txt == 0) { - lv_mem_free(label->text); - label->text = NULL; + if(ext->text != NULL && ext->static_txt == 0) { + lv_mem_free(ext->text); + ext->text = NULL; } va_list args; va_start(args, fmt); - label->text = _lv_txt_set_text_vfmt(fmt, args); + ext->text = _lv_txt_set_text_vfmt(fmt, args); va_end(args); - label->static_txt = 0; /*Now the text is dynamically allocated*/ + ext->static_txt = 0; /*Now the text is dynamically allocated*/ lv_label_refr_text(obj); } @@ -209,16 +201,16 @@ void lv_label_set_text_fmt(lv_obj_t * obj, const char * fmt, ...) void lv_label_set_text_static(lv_obj_t * obj, const char * text) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; - if(label->static_txt == 0 && label->text != NULL) { - lv_mem_free(label->text); - label->text = NULL; + if(ext->static_txt == 0 && ext->text != NULL) { + lv_mem_free(ext->text); + ext->text = NULL; } if(text != NULL) { - label->static_txt = 1; - label->text = (char *)text; + ext->static_txt = 1; + ext->text = (char *)text; } lv_label_refr_text(obj); @@ -235,7 +227,7 @@ void lv_label_set_long_mode(lv_obj_t * obj, lv_label_long_mode_t long_mode) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; #if LV_USE_ANIMATION /*Delete the old animation (if exists)*/ @@ -244,20 +236,20 @@ void lv_label_set_long_mode(lv_obj_t * obj, lv_label_long_mode_t long_mode) lv_anim_del(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_x); lv_anim_del(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_y); #endif - label->offset.x = 0; - label->offset.y = 0; + ext->offset.x = 0; + ext->offset.y = 0; if(long_mode == LV_LABEL_LONG_SROLL || long_mode == LV_LABEL_LONG_SROLL_CIRC || long_mode == LV_LABEL_LONG_CLIP) - label->expand = 1; + ext->expand = 1; else - label->expand = 0; + ext->expand = 0; /*Restore the character under the dots*/ - if(label->long_mode == LV_LABEL_LONG_DOT && label->dot_end != LV_LABEL_DOT_END_INV) { + if(ext->long_mode == LV_LABEL_LONG_DOT && ext->dot_end != LV_LABEL_DOT_END_INV) { lv_label_revert_dots(obj); } - label->long_mode = long_mode; + ext->long_mode = long_mode; lv_label_refr_text(obj); } @@ -270,10 +262,10 @@ void lv_label_set_align(lv_obj_t * obj, lv_label_align_t align) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; - if(label->align == align) return; + lv_label_ext_t * ext = obj->ext_attr; + if(ext->align == align) return; - label->align = align; + ext->align = align; /*Enough to invalidate because alignment is only drawing related (lv_refr_label_text() not required)*/ lv_obj_invalidate(obj); @@ -288,10 +280,10 @@ void lv_label_set_recolor(lv_obj_t * obj, bool en) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; - if(label->recolor == en) return; + lv_label_ext_t * ext = obj->ext_attr; + if(ext->recolor == en) return; - label->recolor = en == false ? 0 : 1; + ext->recolor = en == false ? 0 : 1; /*Refresh the text because the potential color codes in text needs to be hidden or revealed*/ lv_label_refr_text(obj); @@ -307,12 +299,12 @@ void lv_label_set_anim_speed(lv_obj_t * obj, uint16_t anim_speed) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); #if LV_USE_ANIMATION - lv_label_t * label = (lv_label_t *) obj; - if(label->anim_speed == anim_speed) return; + lv_label_ext_t * ext = obj->ext_attr; + if(ext->anim_speed == anim_speed) return; - label->anim_speed = anim_speed; + ext->anim_speed = anim_speed; - if(label->long_mode == LV_LABEL_LONG_SROLL || label->long_mode == LV_LABEL_LONG_SROLL_CIRC) { + if(ext->long_mode == LV_LABEL_LONG_SROLL || ext->long_mode == LV_LABEL_LONG_SROLL_CIRC) { lv_label_refr_text(obj); } #else @@ -326,8 +318,8 @@ void lv_label_set_text_sel_start(lv_obj_t * obj, uint32_t index) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); #if LV_LABEL_TEXT_SEL - lv_label_t * label = (lv_label_t *) obj; - label->sel_start = index; + lv_label_ext_t * ext = obj->ext_attr; + ext->sel_start = index; lv_obj_invalidate(obj); #else (void)obj; /*Unused*/ @@ -340,8 +332,8 @@ void lv_label_set_text_sel_end(lv_obj_t * obj, uint32_t index) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); #if LV_LABEL_TEXT_SEL - lv_label_t * label = (lv_label_t *) obj; - label->sel_end = index; + lv_label_ext_t * ext = obj->ext_attr; + ext->sel_end = index; lv_obj_invalidate(obj); #else (void)obj; /*Unused*/ @@ -361,8 +353,8 @@ void lv_label_set_text_sel_end(lv_obj_t * obj, uint32_t index) char * lv_label_get_text(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; - return label->text; + lv_label_ext_t * ext = obj->ext_attr; + return ext->text; } /** @@ -373,8 +365,8 @@ char * lv_label_get_text(const lv_obj_t * obj) lv_label_long_mode_t lv_label_get_long_mode(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; - return label->long_mode; + lv_label_ext_t * ext = obj->ext_attr; + return ext->long_mode; } /** @@ -386,13 +378,13 @@ lv_label_align_t lv_label_get_align(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; - lv_label_align_t align = label->align; + lv_label_ext_t * ext = obj->ext_attr; + lv_label_align_t align = ext->align; if(align == LV_LABEL_ALIGN_AUTO) { #if LV_USE_BIDI lv_bidi_dir_t base_dir = lv_obj_get_base_dir(label); - if(base_dir == LV_BIDI_DIR_AUTO) base_dir = _lv_bidi_detect_base_dir(label->text); + if(base_dir == LV_BIDI_DIR_AUTO) base_dir = _lv_bidi_detect_base_dir(ext->text); if(base_dir == LV_BIDI_DIR_LTR) align = LV_LABEL_ALIGN_LEFT; else if(base_dir == LV_BIDI_DIR_RTL) align = LV_LABEL_ALIGN_RIGHT; @@ -413,8 +405,8 @@ bool lv_label_get_recolor(const lv_obj_t * obj) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; - return label->recolor == 0 ? false : true; + lv_label_ext_t * ext = obj->ext_attr; + return ext->recolor == 0 ? false : true; } /** @@ -427,8 +419,8 @@ uint16_t lv_label_get_anim_speed(const lv_obj_t * obj) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); #if LV_USE_ANIMATION - lv_label_t * label = (lv_label_t *) obj; - return label->anim_speed; + lv_label_ext_t * ext = obj->ext_attr; + return ext->anim_speed; #else (void)label; /*Unused*/ return 0; @@ -469,7 +461,7 @@ void lv_label_get_letter_pos(const lv_obj_t * obj, uint32_t char_id, lv_point_t lv_area_t txt_coords; get_txt_coords(obj, &txt_coords); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; uint32_t line_start = 0; uint32_t new_line_start = 0; lv_coord_t max_w = lv_area_get_width(&txt_coords); @@ -480,9 +472,9 @@ void lv_label_get_letter_pos(const lv_obj_t * obj, uint32_t char_id, lv_point_t lv_coord_t y = 0; lv_txt_flag_t flag = LV_TXT_FLAG_NONE; - if(label->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; - if(label->expand != 0) flag |= LV_TXT_FLAG_EXPAND; - if(label->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; + if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; + if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND; + if(ext->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; if(align == LV_LABEL_ALIGN_CENTER) flag |= LV_TXT_FLAG_CENTER; if(align == LV_LABEL_ALIGN_RIGHT) flag |= LV_TXT_FLAG_RIGHT; @@ -568,7 +560,7 @@ uint32_t lv_label_get_letter_on(const lv_obj_t * obj, lv_point_t * pos_in) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); LV_ASSERT_NULL(pos_in); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; lv_point_t pos; pos.x = pos_in->x - lv_obj_get_style_pad_left(obj, LV_PART_MAIN); @@ -589,9 +581,9 @@ uint32_t lv_label_get_letter_on(const lv_obj_t * obj, lv_point_t * pos_in) uint32_t logical_pos; char * bidi_txt; - if(label->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; - if(label->expand != 0) flag |= LV_TXT_FLAG_EXPAND; - if(label->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; + if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; + if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND; + if(ext->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; lv_label_align_t align = lv_label_get_align(obj); if(align == LV_LABEL_ALIGN_CENTER) flag |= LV_TXT_FLAG_CENTER; @@ -700,8 +692,8 @@ uint32_t lv_label_get_text_sel_start(const lv_obj_t * obj) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); #if LV_LABEL_TEXT_SEL - lv_label_t * label = (lv_label_t *) obj; - return label->sel_start; + lv_label_ext_t * ext = obj->ext_attr; + return ext->sel_start; #else (void)obj; /*Unused*/ @@ -719,8 +711,8 @@ uint32_t lv_label_get_text_sel_end(const lv_obj_t * obj) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); #if LV_LABEL_TEXT_SEL - lv_label_t * label = (lv_label_t *) obj; - return label->sel_end; + lv_label_ext_t * ext = obj->ext_attr; + return ext->sel_end; #else (void)obj; /*Unused*/ return LV_LABEL_TEXT_SEL_OFF; @@ -753,9 +745,9 @@ uint32_t lv_label_get_text_sel_end(const lv_obj_t * obj) // lv_txt_flag_t flag = LV_TXT_FLAG_NONE; // lv_label_align_t align = lv_label_get_align(label); // -// if(label->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; -// if(label->expand != 0) flag |= LV_TXT_FLAG_EXPAND; -// if(label->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; +// if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; +// if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND; +// if(ext->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; // if(align == LV_LABEL_ALIGN_CENTER) flag |= LV_TXT_FLAG_CENTER; // // /*Search the line of the index letter */; @@ -835,23 +827,23 @@ void lv_label_ins_text(lv_obj_t * obj, uint32_t pos, const char * txt) LV_ASSERT_OBJ(obj, LV_OBJX_NAME); LV_ASSERT_STR(txt); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; /*Can not append to static text*/ - if(label->static_txt != 0) return; + if(ext->static_txt != 0) return; lv_obj_invalidate(obj); /*Allocate space for the new text*/ - size_t old_len = strlen(label->text); + size_t old_len = strlen(ext->text); size_t ins_len = strlen(txt); size_t new_len = ins_len + old_len; - label->text = lv_mem_realloc(label->text, new_len + 1); - LV_ASSERT_MEM(label->text); - if(label->text == NULL) return; + ext->text = lv_mem_realloc(ext->text, new_len + 1); + LV_ASSERT_MEM(ext->text); + if(ext->text == NULL) return; if(pos == LV_LABEL_POS_LAST) { - pos = _lv_txt_get_encoded_length(label->text); + pos = _lv_txt_get_encoded_length(ext->text); } #if LV_USE_BIDI @@ -860,11 +852,11 @@ void lv_label_ins_text(lv_obj_t * obj, uint32_t pos, const char * txt) if(bidi_buf == NULL) return; _lv_bidi_process(txt, bidi_buf, lv_obj_get_base_dir(label)); - _lv_txt_ins(label->text, pos, bidi_buf); + _lv_txt_ins(ext->text, pos, bidi_buf); _lv_mem_buf_release(bidi_buf); #else - _lv_txt_ins(label->text, pos, txt); + _lv_txt_ins(ext->text, pos, txt); #endif lv_label_set_text(obj, NULL); } @@ -879,10 +871,10 @@ void lv_label_ins_text(lv_obj_t * obj, uint32_t pos, const char * txt) void lv_label_cut_text(lv_obj_t * obj, uint32_t pos, uint32_t cnt) { LV_ASSERT_OBJ(obj, LV_OBJX_NAME); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; /*Can not append to static text*/ - if(label->static_txt != 0) return; + if(ext->static_txt != 0) return; lv_obj_invalidate(obj); @@ -900,10 +892,10 @@ void lv_label_cut_text(lv_obj_t * obj, uint32_t pos, uint32_t cnt) */ void lv_label_refr_text(lv_obj_t * obj) { - lv_label_t * label = (lv_label_t *) obj; - if(label->text == NULL) return; + lv_label_ext_t * ext = obj->ext_attr; + if(ext->text == NULL) return; #if LV_LABEL_LONG_TXT_HINT - label->hint.line_start = -1; /*The hint is invalid if the text changes*/ + ext->hint.line_start = -1; /*The hint is invalid if the text changes*/ #endif lv_area_t txt_coords; @@ -916,26 +908,26 @@ void lv_label_refr_text(lv_obj_t * obj) /*Calc. the height and longest line*/ lv_point_t size; lv_txt_flag_t flag = LV_TXT_FLAG_NONE; - if(label->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; - if(label->expand != 0) flag |= LV_TXT_FLAG_EXPAND; - if(label->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; - _lv_txt_get_size(&size, label->text, font, letter_space, line_space, max_w, flag); + if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; + if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND; + if(ext->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; + _lv_txt_get_size(&size, ext->text, font, letter_space, line_space, max_w, flag); /*Set the full size in expand mode*/ - if(label->long_mode == LV_LABEL_LONG_EXPAND) { + if(ext->long_mode == LV_LABEL_LONG_EXPAND) { size.x += lv_obj_get_style_pad_left(obj, LV_PART_MAIN) + lv_obj_get_style_pad_right(obj, LV_PART_MAIN); size.y += lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN); lv_obj_set_size(obj, size.x, size.y); } /*In roll mode keep the size but start offset animations*/ - else if(label->long_mode == LV_LABEL_LONG_SROLL) { + else if(ext->long_mode == LV_LABEL_LONG_SROLL) { #if LV_USE_ANIMATION lv_anim_t a; lv_anim_init(&a); - lv_anim_set_var(&a, label); + lv_anim_set_var(&a, obj); lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); lv_anim_set_playback_delay(&a, (((lv_font_get_glyph_width(font, ' ', ' ') + letter_space) * 1000) / - label->anim_speed) * + ext->anim_speed) * LV_LABEL_WAIT_CHAR_COUNT); lv_anim_set_repeat_delay(&a, a.playback_delay); @@ -943,10 +935,10 @@ void lv_label_refr_text(lv_obj_t * obj) if(size.x > lv_area_get_width(&txt_coords)) { lv_anim_set_values(&a, 0, lv_area_get_width(&txt_coords) - size.x); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_label_set_offset_x); - lv_anim_set_time(&a, lv_anim_speed_to_time(label->anim_speed, a.start, a.end)); + lv_anim_set_time(&a, lv_anim_speed_to_time(ext->anim_speed, a.start, a.end)); lv_anim_set_playback_time(&a, a.time); - lv_anim_t * anim_cur = lv_anim_get(label, (lv_anim_exec_xcb_t)lv_label_set_offset_x); + lv_anim_t * anim_cur = lv_anim_get(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_x); int32_t act_time = 0; bool playback_now = false; if(anim_cur) { @@ -971,17 +963,17 @@ void lv_label_refr_text(lv_obj_t * obj) } else { /*Delete the offset animation if not required*/ - lv_anim_del(label, (lv_anim_exec_xcb_t)lv_label_set_offset_x); - label->offset.x = 0; + lv_anim_del(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_x); + ext->offset.x = 0; } if(size.y > lv_area_get_height(&txt_coords) && hor_anim == false) { lv_anim_set_values(&a, 0, lv_area_get_height(&txt_coords) - size.y - (lv_font_get_line_height(font))); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_label_set_offset_y); - lv_anim_set_time(&a, lv_anim_speed_to_time(label->anim_speed, a.start, a.end)); + lv_anim_set_time(&a, lv_anim_speed_to_time(ext->anim_speed, a.start, a.end)); lv_anim_set_playback_time(&a, a.time); - lv_anim_t * anim_cur = lv_anim_get(label, (lv_anim_exec_xcb_t)lv_label_set_offset_y); + lv_anim_t * anim_cur = lv_anim_get(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_y); int32_t act_time = 0; bool playback_now = false; if(anim_cur) { @@ -1005,26 +997,26 @@ void lv_label_refr_text(lv_obj_t * obj) } else { /*Delete the offset animation if not required*/ - lv_anim_del(label, (lv_anim_exec_xcb_t)lv_label_set_offset_y); - label->offset.y = 0; + lv_anim_del(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_y); + ext->offset.y = 0; } #endif } /*In roll inf. mode keep the size but start offset animations*/ - else if(label->long_mode == LV_LABEL_LONG_SROLL_CIRC) { + else if(ext->long_mode == LV_LABEL_LONG_SROLL_CIRC) { #if LV_USE_ANIMATION lv_anim_t a; lv_anim_init(&a); - lv_anim_set_var(&a, label); + lv_anim_set_var(&a, obj); lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); bool hor_anim = false; if(size.x > lv_area_get_width(&txt_coords)) { lv_anim_set_values(&a, 0, -size.x - lv_font_get_glyph_width(font, ' ', ' ') * LV_LABEL_WAIT_CHAR_COUNT); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_label_set_offset_x); - lv_anim_set_time(&a, lv_anim_speed_to_time(label->anim_speed, a.start, a.end)); + lv_anim_set_time(&a, lv_anim_speed_to_time(ext->anim_speed, a.start, a.end)); - lv_anim_t * anim_cur = lv_anim_get(label, (lv_anim_exec_xcb_t)lv_label_set_offset_x); + lv_anim_t * anim_cur = lv_anim_get(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_x); int32_t act_time = anim_cur ? anim_cur->act_time : 0; if(act_time < a.time) { a.act_time = act_time; /*To keep the old position*/ @@ -1036,16 +1028,16 @@ void lv_label_refr_text(lv_obj_t * obj) } else { /*Delete the offset animation if not required*/ - lv_anim_del(label, (lv_anim_exec_xcb_t)lv_label_set_offset_x); - label->offset.x = 0; + lv_anim_del(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_x); + ext->offset.x = 0; } if(size.y > lv_area_get_height(&txt_coords) && hor_anim == false) { lv_anim_set_values(&a, 0, -size.y - (lv_font_get_line_height(font))); lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_label_set_offset_y); - lv_anim_set_time(&a, lv_anim_speed_to_time(label->anim_speed, a.start, a.end)); + lv_anim_set_time(&a, lv_anim_speed_to_time(ext->anim_speed, a.start, a.end)); - lv_anim_t * anim_cur = lv_anim_get(label, (lv_anim_exec_xcb_t)lv_label_set_offset_y); + lv_anim_t * anim_cur = lv_anim_get(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_y); int32_t act_time = anim_cur ? anim_cur->act_time : 0; if(act_time < a.time) { a.act_time = act_time; /*To keep the old position*/ @@ -1056,17 +1048,17 @@ void lv_label_refr_text(lv_obj_t * obj) } else { /*Delete the offset animation if not required*/ - lv_anim_del(label, (lv_anim_exec_xcb_t)lv_label_set_offset_y); - label->offset.y = 0; + lv_anim_del(obj, (lv_anim_exec_xcb_t)lv_label_set_offset_y); + ext->offset.y = 0; } #endif } - else if(label->long_mode == LV_LABEL_LONG_DOT) { + else if(ext->long_mode == LV_LABEL_LONG_DOT) { if(size.y <= lv_area_get_height(&txt_coords)) { /*No dots are required, the text is short enough*/ - label->dot_end = LV_LABEL_DOT_END_INV; + ext->dot_end = LV_LABEL_DOT_END_INV; } - else if(_lv_txt_get_encoded_length(label->text) <= LV_LABEL_DOT_NUM) { /*Don't turn to dots all the characters*/ - label->dot_end = LV_LABEL_DOT_END_INV; + else if(_lv_txt_get_encoded_length(ext->text) <= LV_LABEL_DOT_NUM) { /*Don't turn to dots all the characters*/ + ext->dot_end = LV_LABEL_DOT_END_INV; } else { lv_point_t p; @@ -1090,10 +1082,10 @@ void lv_label_refr_text(lv_obj_t * obj) /*Be sure there is space for the dots*/ - size_t txt_len = strlen(label->text); - uint32_t byte_id = _lv_txt_encoded_get_byte_id(label->text, letter_id); + size_t txt_len = strlen(ext->text); + uint32_t byte_id = _lv_txt_encoded_get_byte_id(ext->text, letter_id); while(byte_id + LV_LABEL_DOT_NUM > txt_len) { - _lv_txt_encoded_prev(label->text, &byte_id); + _lv_txt_encoded_prev(ext->text, &byte_id); letter_id--; } @@ -1102,26 +1094,26 @@ void lv_label_refr_text(lv_obj_t * obj) uint32_t i; uint8_t len = 0; for(i = 0; i <= LV_LABEL_DOT_NUM; i++) { - len += _lv_txt_encoded_size(&label->text[byte_id]); - _lv_txt_encoded_next(label->text, &byte_id); + len += _lv_txt_encoded_size(&ext->text[byte_id]); + _lv_txt_encoded_next(ext->text, &byte_id); } - if(lv_label_set_dot_tmp(obj, &label->text[byte_id_ori], len)) { + if(lv_label_set_dot_tmp(obj, &ext->text[byte_id_ori], len)) { for(i = 0; i < LV_LABEL_DOT_NUM; i++) { - label->text[byte_id_ori + i] = '.'; + ext->text[byte_id_ori + i] = '.'; } - label->text[byte_id_ori + LV_LABEL_DOT_NUM] = '\0'; - label->dot_end = letter_id + LV_LABEL_DOT_NUM; + ext->text[byte_id_ori + LV_LABEL_DOT_NUM] = '\0'; + ext->dot_end = letter_id + LV_LABEL_DOT_NUM; } } } /*In break mode only the height can change*/ - else if(label->long_mode == LV_LABEL_LONG_WRAP) { + else if(ext->long_mode == LV_LABEL_LONG_WRAP) { size.y += lv_obj_get_style_pad_top(obj, LV_PART_MAIN) + lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN); lv_obj_set_height(obj, size.y); } /*Do not set the size in Clip mode*/ - else if(label->long_mode == LV_LABEL_LONG_CLIP) { + else if(ext->long_mode == LV_LABEL_LONG_CLIP) { /*Do nothing*/ } @@ -1138,34 +1130,34 @@ static void lv_label_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj LV_LOG_TRACE("label create started"); LV_CLASS_CONSTRUCTOR_BEGIN(obj, lv_label) - lv_label.base_p->constructor(obj, parent, copy); + lv_obj.constructor(obj, parent, copy); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; - label->text = NULL; - label->static_txt = 0; - label->recolor = 0; - label->align = LV_LABEL_ALIGN_AUTO; - label->dot_end = LV_LABEL_DOT_END_INV; - label->long_mode = LV_LABEL_LONG_EXPAND; + ext->text = NULL; + ext->static_txt = 0; + ext->recolor = 0; + ext->align = LV_LABEL_ALIGN_AUTO; + ext->dot_end = LV_LABEL_DOT_END_INV; + ext->long_mode = LV_LABEL_LONG_EXPAND; #if LV_USE_ANIMATION - label->anim_speed = LV_LABEL_DEF_SCROLL_SPEED; + ext->anim_speed = LV_LABEL_DEF_SCROLL_SPEED; #endif - label->offset.x = 0; - label->offset.y = 0; + ext->offset.x = 0; + ext->offset.y = 0; #if LV_LABEL_LONG_TXT_HINT - label->hint.line_start = -1; - label->hint.coord_y = 0; - label->hint.y = 0; + ext->hint.line_start = -1; + ext->hint.coord_y = 0; + ext->hint.y = 0; #endif #if LV_LABEL_TEXT_SEL - label->sel_start = LV_DRAW_LABEL_NO_TXT_SEL; - label->sel_end = LV_DRAW_LABEL_NO_TXT_SEL; + ext->sel_start = LV_DRAW_LABEL_NO_TXT_SEL; + ext->sel_end = LV_DRAW_LABEL_NO_TXT_SEL; #endif - label->dot.tmp_ptr = NULL; - label->dot_tmp_alloc = 0; + ext->dot.tmp_ptr = NULL; + ext->dot_tmp_alloc = 0; lv_obj_clear_flag(obj, LV_OBJ_FLAG_CLICKABLE); lv_label_set_long_mode(obj, LV_LABEL_LONG_EXPAND); @@ -1176,10 +1168,10 @@ static void lv_label_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj LV_LOG_INFO("label created"); } -static void lv_label_destructor(void * obj) +static void lv_label_destructor(lv_obj_t * obj) { -// lv_label_dot_tmp_free(label->dot_...); -// lv_label_dot_tmp_free(label->text); +// lv_label_dot_tmp_free(ext->dot_...); +// lv_label_dot_tmp_free(ext->text); // lv_bar_t * bar = obj; // @@ -1211,7 +1203,7 @@ static lv_design_res_t lv_label_design(lv_obj_t * obj, const lv_area_t * clip_ar lv_obj.design_cb(obj, clip_area, mode); - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; lv_area_t txt_coords; get_txt_coords(obj, &txt_coords); @@ -1222,17 +1214,17 @@ static lv_design_res_t lv_label_design(lv_obj_t * obj, const lv_area_t * clip_ar lv_label_align_t align = lv_label_get_align(obj); lv_txt_flag_t flag = LV_TXT_FLAG_NONE; - if(label->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; - if(label->expand != 0) flag |= LV_TXT_FLAG_EXPAND; - if(label->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; + if(ext->recolor != 0) flag |= LV_TXT_FLAG_RECOLOR; + if(ext->expand != 0) flag |= LV_TXT_FLAG_EXPAND; + if(ext->long_mode == LV_LABEL_LONG_EXPAND) flag |= LV_TXT_FLAG_FIT; if(align == LV_LABEL_ALIGN_CENTER) flag |= LV_TXT_FLAG_CENTER; if(align == LV_LABEL_ALIGN_RIGHT) flag |= LV_TXT_FLAG_RIGHT; lv_draw_label_dsc_t label_draw_dsc; lv_draw_label_dsc_init(&label_draw_dsc); - label_draw_dsc.ofs_x = label->offset.x; - label_draw_dsc.ofs_y = label->offset.y; + label_draw_dsc.ofs_x = ext->offset.x; + label_draw_dsc.ofs_y = ext->offset.y; label_draw_dsc.flag = flag; lv_obj_init_draw_label_dsc(obj, LV_PART_MAIN, &label_draw_dsc); @@ -1245,10 +1237,10 @@ static lv_design_res_t lv_label_design(lv_obj_t * obj, const lv_area_t * clip_ar /* In SROLL and SROLL_CIRC mode the CENTER and RIGHT are pointless so remove them. * (In addition they will result misalignment is this case)*/ - if((label->long_mode == LV_LABEL_LONG_SROLL || label->long_mode == LV_LABEL_LONG_SROLL_CIRC) && - (label->align == LV_LABEL_ALIGN_CENTER || label->align == LV_LABEL_ALIGN_RIGHT)) { + if((ext->long_mode == LV_LABEL_LONG_SROLL || ext->long_mode == LV_LABEL_LONG_SROLL_CIRC) && + (ext->align == LV_LABEL_ALIGN_CENTER || ext->align == LV_LABEL_ALIGN_RIGHT)) { lv_point_t size; - _lv_txt_get_size(&size, label->text, label_draw_dsc.font, label_draw_dsc.letter_space, label_draw_dsc.line_space, + _lv_txt_get_size(&size, ext->text, label_draw_dsc.font, label_draw_dsc.letter_space, label_draw_dsc.line_space, LV_COORD_MAX, flag); if(size.x > lv_area_get_width(&txt_coords)) { label_draw_dsc.flag &= ~LV_TXT_FLAG_RIGHT; @@ -1256,8 +1248,8 @@ static lv_design_res_t lv_label_design(lv_obj_t * obj, const lv_area_t * clip_ar } } #if LV_LABEL_LONG_TXT_HINT - lv_draw_label_hint_t * hint = &label->hint; - if(label->long_mode == LV_LABEL_LONG_SROLL_CIRC || lv_area_get_height(&txt_coords) < LV_LABEL_HINT_HEIGHT_LIMIT) + lv_draw_label_hint_t * hint = &ext->hint; + if(ext->long_mode == LV_LABEL_LONG_SROLL_CIRC || lv_area_get_height(&txt_coords) < LV_LABEL_HINT_HEIGHT_LIMIT) hint = NULL; #else @@ -1265,28 +1257,28 @@ static lv_design_res_t lv_label_design(lv_obj_t * obj, const lv_area_t * clip_ar lv_draw_label_hint_t * hint = NULL; #endif - lv_draw_label(&txt_coords, &txt_clip, &label_draw_dsc, label->text, hint); + lv_draw_label(&txt_coords, &txt_clip, &label_draw_dsc, ext->text, hint); - if(label->long_mode == LV_LABEL_LONG_SROLL_CIRC) { + if(ext->long_mode == LV_LABEL_LONG_SROLL_CIRC) { lv_point_t size; - _lv_txt_get_size(&size, label->text, label_draw_dsc.font, label_draw_dsc.letter_space, label_draw_dsc.line_space, + _lv_txt_get_size(&size, ext->text, label_draw_dsc.font, label_draw_dsc.letter_space, label_draw_dsc.line_space, LV_COORD_MAX, flag); /*Draw the text again nlabel to the original to make an circular effect */ if(size.x > lv_area_get_width(&txt_coords)) { - label_draw_dsc.ofs_x = label->offset.x + size.x + + label_draw_dsc.ofs_x = ext->offset.x + size.x + lv_font_get_glyph_width(label_draw_dsc.font, ' ', ' ') * LV_LABEL_WAIT_CHAR_COUNT; - label_draw_dsc.ofs_y = label->offset.y; + label_draw_dsc.ofs_y = ext->offset.y; - lv_draw_label(&txt_coords, &txt_clip, &label_draw_dsc, label->text, hint); + lv_draw_label(&txt_coords, &txt_clip, &label_draw_dsc, ext->text, hint); } /*Draw the text again below the original to make an circular effect */ if(size.y > lv_area_get_height(&txt_coords)) { - label_draw_dsc.ofs_x = label->offset.x; - label_draw_dsc.ofs_y = label->offset.y + size.y + lv_font_get_line_height(label_draw_dsc.font); + label_draw_dsc.ofs_x = ext->offset.x; + label_draw_dsc.ofs_y = ext->offset.y + size.y + lv_font_get_line_height(label_draw_dsc.font); - lv_draw_label(&txt_coords, &txt_clip, &label_draw_dsc, label->text, hint); + lv_draw_label(&txt_coords, &txt_clip, &label_draw_dsc, ext->text, hint); } } } else if(mode == LV_DESIGN_DRAW_POST) { @@ -1308,11 +1300,11 @@ static lv_res_t lv_label_signal(lv_obj_t * obj, lv_signal_t sign, void * param) lv_res_t res; /* Include the ancient signal function */ - res = lv_label.base_p->signal_cb(obj, sign, param); + res = lv_obj.signal_cb(obj, sign, param); if(res != LV_RES_OK) return res; - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; if(sign == LV_SIGNAL_STYLE_CHG) { /*Revert dots for proper refresh*/ @@ -1320,15 +1312,15 @@ static lv_res_t lv_label_signal(lv_obj_t * obj, lv_signal_t sign, void * param) lv_label_refr_text(obj); } else if(sign == LV_SIGNAL_COORD_CHG) { - if(lv_area_get_width(&label->coords) != lv_area_get_width(param) || - lv_area_get_height(&label->coords) != lv_area_get_height(param)) { + if(lv_area_get_width(&obj->coords) != lv_area_get_width(param) || + lv_area_get_height(&obj->coords) != lv_area_get_height(param)) { lv_label_revert_dots(obj); lv_label_refr_text(obj); } } else if(sign == LV_SIGNAL_BASE_DIR_CHG) { #if LV_USE_BIDI - if(label->static_txt == 0) lv_label_set_text(label, NULL); + if(ext->static_txt == 0) lv_label_set_text(label, NULL); #endif } @@ -1338,38 +1330,38 @@ static lv_res_t lv_label_signal(lv_obj_t * obj, lv_signal_t sign, void * param) static void lv_label_revert_dots(lv_obj_t * obj) { - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; - if(label->long_mode != LV_LABEL_LONG_DOT) return; - if(label->dot_end == LV_LABEL_DOT_END_INV) return; - uint32_t letter_i = label->dot_end - LV_LABEL_DOT_NUM; - uint32_t byte_i = _lv_txt_encoded_get_byte_id(label->text, letter_i); + if(ext->long_mode != LV_LABEL_LONG_DOT) return; + if(ext->dot_end == LV_LABEL_DOT_END_INV) return; + uint32_t letter_i = ext->dot_end - LV_LABEL_DOT_NUM; + uint32_t byte_i = _lv_txt_encoded_get_byte_id(ext->text, letter_i); /*Restore the characters*/ uint8_t i = 0; char * dot_tmp = lv_label_get_dot_tmp(obj); - while(label->text[byte_i + i] != '\0') { - label->text[byte_i + i] = dot_tmp[i]; + while(ext->text[byte_i + i] != '\0') { + ext->text[byte_i + i] = dot_tmp[i]; i++; } - label->text[byte_i + i] = dot_tmp[i]; + ext->text[byte_i + i] = dot_tmp[i]; lv_label_dot_tmp_free(obj); - label->dot_end = LV_LABEL_DOT_END_INV; + ext->dot_end = LV_LABEL_DOT_END_INV; } #if LV_USE_ANIMATION static void lv_label_set_offset_x(lv_obj_t * obj, lv_coord_t x) { - lv_label_t * label = (lv_label_t *) obj; - label->offset.x = x; + lv_label_ext_t * ext = obj->ext_attr; + ext->offset.x = x; lv_obj_invalidate(obj); } static void lv_label_set_offset_y(lv_obj_t * obj, lv_coord_t y) { - lv_label_t * label = (lv_label_t *) obj; - label->offset.y = y; + lv_label_ext_t * ext = obj->ext_attr; + ext->offset.y = y; lv_obj_invalidate(obj); } #endif @@ -1384,24 +1376,24 @@ static void lv_label_set_offset_y(lv_obj_t * obj, lv_coord_t y) static bool lv_label_set_dot_tmp(lv_obj_t * obj, char * data, uint32_t len) { - lv_label_t * label = (lv_label_t *) obj; + lv_label_ext_t * ext = obj->ext_attr; lv_label_dot_tmp_free(obj); /* Deallocate any existing space */ if(len > sizeof(char *)) { /* Memory needs to be allocated. Allocates an additional byte * for a NULL-terminator so it can be copied. */ - label->dot.tmp_ptr = lv_mem_alloc(len + 1); - if(label->dot.tmp_ptr == NULL) { + ext->dot.tmp_ptr = lv_mem_alloc(len + 1); + if(ext->dot.tmp_ptr == NULL) { LV_LOG_ERROR("Failed to allocate memory for dot_tmp_ptr"); return false; } - _lv_memcpy(label->dot.tmp_ptr, data, len); - label->dot.tmp_ptr[len] = '\0'; - label->dot_tmp_alloc = true; + _lv_memcpy(ext->dot.tmp_ptr, data, len); + ext->dot.tmp_ptr[len] = '\0'; + ext->dot_tmp_alloc = true; } else { /* Characters can be directly stored in object */ - label->dot_tmp_alloc = false; - _lv_memcpy(label->dot.tmp, data, len); + ext->dot_tmp_alloc = false; + _lv_memcpy(ext->dot.tmp, data, len); } return true; } @@ -1413,12 +1405,12 @@ static bool lv_label_set_dot_tmp(lv_obj_t * obj, char * data, uint32_t len) */ static char * lv_label_get_dot_tmp(lv_obj_t * obj) { - lv_label_t * label = (lv_label_t *) obj; - if(label->dot_tmp_alloc) { - return label->dot.tmp_ptr; + lv_label_ext_t * ext = obj->ext_attr; + if(ext->dot_tmp_alloc) { + return ext->dot.tmp_ptr; } else { - return label->dot.tmp; + return ext->dot.tmp; } } @@ -1429,12 +1421,12 @@ static char * lv_label_get_dot_tmp(lv_obj_t * obj) */ static void lv_label_dot_tmp_free(lv_obj_t * obj) { - lv_label_t * label = (lv_label_t *) obj; - if(label->dot_tmp_alloc && label->dot.tmp_ptr) { - lv_mem_free(label->dot.tmp_ptr); + lv_label_ext_t * ext = obj->ext_attr; + if(ext->dot_tmp_alloc && ext->dot.tmp_ptr) { + lv_mem_free(ext->dot.tmp_ptr); } - label->dot_tmp_alloc = false; - label->dot.tmp_ptr = NULL; + ext->dot_tmp_alloc = false; + ext->dot.tmp_ptr = NULL; } static void get_txt_coords(const lv_obj_t * obj, lv_area_t * area) diff --git a/src/lv_widgets/lv_label.h b/src/lv_widgets/lv_label.h index fbf5d99c6..b5df34533 100644 --- a/src/lv_widgets/lv_label.h +++ b/src/lv_widgets/lv_label.h @@ -60,54 +60,37 @@ enum { typedef uint8_t lv_label_align_t; -LV_CLASS_DECLARE_START(lv_label, lv_obj); -#define _lv_label_constructor void (*constructor)(struct _lv_obj_t * obj, struct _lv_obj_t * parent, const struct _lv_obj_t * copy) +typedef struct { + char * text; + union { + char * tmp_ptr; /* Pointer to the allocated memory containing the character replaced by dots*/ + char tmp[LV_LABEL_DOT_NUM + 1]; /* Directly store the characters if <=4 characters */ + } dot; + uint32_t dot_end; /*The real text length, used in dot mode*/ #if LV_USE_ANIMATION -#define LV_LABEL_ANIM_DATA uint32_t anim_speed; -#else -#define LV_LABEL_ANIM_DATA + uint32_t anim_speed; #endif #if LV_LABEL_LONG_TXT_HINT -#define LV_LABEL_HINT_DATA lv_draw_label_hint_t hint; -#else -#define LV_LABEL_HINT_DATA + lv_draw_label_hint_t hint; #endif #if LV_LABEL_TEXT_SEL -#define LV_LABEL_TEXT_SEL_DATA uint32_t sel_start; uint32_t sel_end; -#else -#define LV_LABEL_TEXT_SEL_DATA + uint32_t sel_start; uint32_t sel_end; #endif + lv_point_t offset; /*Text draw position offset*/ + lv_label_long_mode_t long_mode : 3; /*Determinate what to do with the long texts*/ + uint8_t static_txt : 1; /*Flag to indicate the text is static*/ + uint8_t align : 2; /*Align type from 'lv_label_align_t'*/ + uint8_t recolor : 1; /*Enable in-line letter re-coloring*/ + uint8_t expand : 1; /*Ignore real width (used by the library with LV_LABEL_LONG_SROLL)*/ + uint8_t dot_tmp_alloc : 1; /*1: dot_tmp has been allocated;.0: dot_tmp directly holds up to 4 bytes of characters */ +}lv_label_ext_t; -#define _lv_label_data \ - _lv_obj_data \ - char * text; \ - union { \ - char * tmp_ptr; /* Pointer to the allocated memory containing the character replaced by dots*/ \ - char tmp[LV_LABEL_DOT_NUM + 1]; /* Directly store the characters if <=4 characters */ \ - } dot; \ - uint32_t dot_end; /*The real text length, used in dot mode*/ \ - LV_LABEL_ANIM_DATA /*Speed of scroll and roll animation in px/sec unit*/ \ - LV_LABEL_HINT_DATA /*Used to buffer info about large text*/ \ - LV_LABEL_TEXT_SEL_DATA \ - lv_point_t offset; /*Text draw position offset*/ \ - lv_label_long_mode_t long_mode : 3; /*Determinate what to do with the long texts*/ \ - uint8_t static_txt : 1; /*Flag to indicate the text is static*/ \ - uint8_t align : 2; /*Align type from 'lv_label_align_t'*/ \ - uint8_t recolor : 1; /*Enable in-line letter re-coloring*/ \ - uint8_t expand : 1; /*Ignore real width (used by the library with LV_LABEL_LONG_SROLL)*/ \ - uint8_t dot_tmp_alloc : 1; /*1: dot_tmp has been allocated;.0: dot_tmp directly holds up to 4 bytes of characters */ \ - -#define _lv_label_class_dsc \ - _lv_obj_class_dsc - -LV_CLASS_DECLARE_END(lv_label, lv_obj); - -extern lv_label_class_t lv_label; +extern const lv_obj_class_t lv_label; /********************** * GLOBAL PROTOTYPES diff --git a/src/lv_widgets/lv_line.c b/src/lv_widgets/lv_line.c index a40eb5b3b..c1b9631f9 100644 --- a/src/lv_widgets/lv_line.c +++ b/src/lv_widgets/lv_line.c @@ -236,7 +236,7 @@ static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param) } else if(sign == LV_SIGNAL_REFR_EXT_DRAW_PAD) { /*The corner of the skew lines is out of the intended area*/ - lv_style_int_t line_width = lv_obj_get_style_line_width(line, LV_LINE_PART_MAIN); + lv_coord_t line_width = lv_obj_get_style_line_width(line, LV_LINE_PART_MAIN); lv_coord_t * s = param; if(*s < line_width) *s = line_width; } @@ -253,7 +253,7 @@ static lv_res_t lv_line_signal(lv_obj_t * line, lv_signal_t sign, void * param) h = LV_MATH_MAX(ext->point_array[i].y, h); } - lv_style_int_t line_width = lv_obj_get_style_line_width(line, LV_LINE_PART_MAIN); + lv_coord_t line_width = lv_obj_get_style_line_width(line, LV_LINE_PART_MAIN); w += line_width; h += line_width; p->x = w; diff --git a/src/lv_widgets/lv_linemeter.c b/src/lv_widgets/lv_linemeter.c index d9f72873f..162bf5e99 100644 --- a/src/lv_widgets/lv_linemeter.c +++ b/src/lv_widgets/lv_linemeter.c @@ -308,9 +308,9 @@ void lv_linemeter_draw_scale(lv_obj_t * lmeter, const lv_area_t * clip_area, uin { lv_linemeter_ext_t * ext = lv_obj_get_ext_attr(lmeter); - lv_style_int_t left = lv_obj_get_style_pad_left(lmeter, LV_LINEMETER_PART_MAIN); - lv_style_int_t right = lv_obj_get_style_pad_right(lmeter, LV_LINEMETER_PART_MAIN); - lv_style_int_t top = lv_obj_get_style_pad_top(lmeter, LV_LINEMETER_PART_MAIN); + lv_coord_t left = lv_obj_get_style_pad_left(lmeter, LV_LINEMETER_PART_MAIN); + lv_coord_t right = lv_obj_get_style_pad_right(lmeter, LV_LINEMETER_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(lmeter, LV_LINEMETER_PART_MAIN); lv_coord_t r_out = (lv_obj_get_width(lmeter) - left - right) / 2 ; lv_coord_t r_in = r_out - lv_obj_get_style_scale_width(lmeter, part); @@ -335,7 +335,7 @@ void lv_linemeter_draw_scale(lv_obj_t * lmeter, const lv_area_t * clip_area, uin line_dsc.raw_end = 1; #endif - lv_style_int_t end_line_width = lv_obj_get_style_scale_end_line_width(lmeter, part); + lv_coord_t end_line_width = lv_obj_get_style_scale_end_line_width(lmeter, part); #if LV_LINEMETER_PRECISE > 0 lv_area_t mask_area; @@ -467,8 +467,8 @@ void lv_linemeter_draw_scale(lv_obj_t * lmeter, const lv_area_t * clip_area, uin #endif if(part == LV_LINEMETER_PART_MAIN && level < ext->line_cnt - 1) { - lv_style_int_t border_width = lv_obj_get_style_scale_border_width(lmeter, part); - lv_style_int_t end_border_width = lv_obj_get_style_scale_end_border_width(lmeter, part); + lv_coord_t border_width = lv_obj_get_style_scale_border_width(lmeter, part); + lv_coord_t end_border_width = lv_obj_get_style_scale_end_border_width(lmeter, part); if(border_width || end_border_width) { int16_t end_angle = ((level) * ext->scale_angle) / (ext->line_cnt - 1) + angle_ofs; diff --git a/src/lv_widgets/lv_table.c b/src/lv_widgets/lv_table.c index 098f5ff88..8422487eb 100644 --- a/src/lv_widgets/lv_table.c +++ b/src/lv_widgets/lv_table.c @@ -30,20 +30,25 @@ /********************** * STATIC PROTOTYPES **********************/ +static void lv_table_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy); +static void lv_table_destructor(lv_obj_t * obj); static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_area, lv_design_mode_t mode); static lv_res_t lv_table_signal(lv_obj_t * table, lv_signal_t sign, void * param); -static lv_style_list_t * lv_table_get_style(lv_obj_t * table, uint8_t part); -static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id, const lv_font_t ** font, - lv_style_int_t * letter_space, lv_style_int_t * line_space, - lv_style_int_t * cell_left, lv_style_int_t * cell_right, lv_style_int_t * cell_top, lv_style_int_t * cell_bottom); +static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id, const lv_font_t * font, + lv_coord_t letter_space, lv_coord_t line_space, + lv_coord_t cell_left, lv_coord_t cell_right, lv_coord_t cell_top, lv_coord_t cell_bottom); static void refr_size(lv_obj_t * table); /********************** * STATIC VARIABLES **********************/ -static lv_signal_cb_t ancestor_signal; -static lv_design_cb_t ancestor_design; - +const lv_obj_class_t lv_table = { + .constructor = lv_table_constructor, + .destructor = lv_table_destructor, + .signal_cb = lv_table_signal, + .design_cb = lv_table_design, + .ext_size = sizeof(lv_table_ext_t), +}; /********************** * MACROS **********************/ @@ -58,74 +63,9 @@ static lv_design_cb_t ancestor_design; * @param copy pointer to a table object, if not NULL then the new object will be copied from it * @return pointer to the created table */ -lv_obj_t * lv_table_create(lv_obj_t * par, const lv_obj_t * copy) +lv_obj_t * lv_table_create(lv_obj_t * parent, const lv_obj_t * copy) { - LV_LOG_TRACE("table create started"); - - /*Create the ancestor of table*/ - lv_obj_t * table = lv_obj_create(par, copy); - LV_ASSERT_MEM(table); - if(table == NULL) return NULL; - - /*Allocate the table type specific extended data*/ - lv_table_ext_t * ext = lv_obj_allocate_ext_attr(table, sizeof(lv_table_ext_t)); - LV_ASSERT_MEM(ext); - if(ext == NULL) { - lv_obj_del(table); - return NULL; - } - - if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(table); - if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(table); - - /*Initialize the allocated 'ext' */ - ext->cell_data = NULL; - ext->col_cnt = 0; - ext->row_cnt = 0; - ext->row_h = NULL; - ext->col_w = NULL; - ext->cell_types = 1; - - uint16_t i; - for(i = 0; i < LV_TABLE_CELL_STYLE_CNT; i++) { - lv_style_list_init(&ext->cell_style[i]); - } - - /*The signal and design functions are not copied so set them here*/ - lv_obj_set_signal_cb(table, lv_table_signal); - lv_obj_set_design_cb(table, lv_table_design); - - /*Init the new table table*/ - if(copy == NULL) { - ext->col_cnt = 1; - ext->row_cnt = 1; - ext->col_w = lv_mem_alloc(ext->col_cnt * sizeof(ext->col_w[0])); - ext->row_h = lv_mem_alloc(ext->row_cnt * sizeof(ext->row_h[0])); - ext->col_w[0] = LV_DPI; - ext->row_h[0] = LV_DPI; /*It will be overwritten when the theme is applied*/ - ext->cell_data = lv_mem_realloc(ext->cell_data, ext->row_cnt * ext->col_cnt * sizeof(char *)); - ext->cell_data[0] = NULL; - - lv_obj_set_size(table, LV_SIZE_AUTO, LV_SIZE_AUTO); - - lv_theme_apply(table, LV_THEME_TABLE); - } - /*Copy an existing table*/ - else { - lv_table_ext_t * copy_ext = lv_obj_get_ext_attr(copy); - for(i = 0; i < LV_TABLE_CELL_STYLE_CNT; i++) { - lv_style_list_copy(&ext->cell_style[i], ©_ext->cell_style[i]); - } - lv_table_set_row_cnt(table, copy_ext->row_cnt); - lv_table_set_col_cnt(table, copy_ext->col_cnt); - - /*Refresh the style with new signal function*/ - _lv_obj_refresh_style(table, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL); - } - - LV_LOG_INFO("table created"); - - return table; + return lv_obj_create_from_class(&lv_table, parent, copy); } /*===================== @@ -145,7 +85,7 @@ void lv_table_set_cell_value(lv_obj_t * table, uint16_t row, uint16_t col, const LV_ASSERT_OBJ(table, LV_OBJX_NAME); LV_ASSERT_NULL(txt); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; /*Auto expand*/ if(col >= ext->col_cnt) lv_table_set_col_cnt(table, col + 1); @@ -170,7 +110,6 @@ void lv_table_set_cell_value(lv_obj_t * table, uint16_t row, uint16_t col, const format.s.align = LV_LABEL_ALIGN_LEFT; #endif format.s.right_merge = 0; - format.s.type = 0; format.s.crop = 0; } @@ -207,7 +146,7 @@ void lv_table_set_cell_value_fmt(lv_obj_t * table, uint16_t row, uint16_t col, c LV_ASSERT_OBJ(table, LV_OBJX_NAME); LV_ASSERT_STR(fmt); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; if(col >= ext->col_cnt) { LV_LOG_WARN("lv_table_set_cell_value: invalid column"); return; @@ -237,7 +176,6 @@ void lv_table_set_cell_value_fmt(lv_obj_t * table, uint16_t row, uint16_t col, c format.s.align = LV_LABEL_ALIGN_LEFT; #endif format.s.right_merge = 0; - format.s.type = 0; format.s.crop = 0; } @@ -299,7 +237,7 @@ void lv_table_set_row_cnt(lv_obj_t * table, uint16_t row_cnt) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; uint16_t old_row_cnt = ext->row_cnt; ext->row_cnt = row_cnt; @@ -330,7 +268,7 @@ void lv_table_set_col_cnt(lv_obj_t * table, uint16_t col_cnt) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; uint16_t old_col_cnt = ext->col_cnt; ext->col_cnt = col_cnt; ext->col_w = lv_mem_realloc(ext->col_w, col_cnt * sizeof(ext->row_h[0])); @@ -366,7 +304,7 @@ void lv_table_set_col_width(lv_obj_t * table, uint16_t col_id, lv_coord_t w) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; /*Auto expand*/ if(col_id >= ext->col_cnt) lv_table_set_col_cnt(table, col_id + 1); @@ -386,7 +324,7 @@ void lv_table_set_cell_align(lv_obj_t * table, uint16_t row, uint16_t col, lv_la { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; /*Auto expand*/ if(col >= ext->col_cnt) lv_table_set_col_cnt(table, col + 1); @@ -409,47 +347,6 @@ void lv_table_set_cell_align(lv_obj_t * table, uint16_t row, uint16_t col, lv_la ext->cell_data[cell][0] = format.format_byte; } -/** - * Set the type of a cell. - * @param table pointer to a Table object - * @param row id of the row [0 .. row_cnt -1] - * @param col id of the column [0 .. col_cnt -1] - * @param type 1,2,3 or 4. The cell style will be chosen accordingly. - */ -void lv_table_set_cell_type(lv_obj_t * table, uint16_t row, uint16_t col, uint8_t type) -{ - LV_ASSERT_OBJ(table, LV_OBJX_NAME); - - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); - - /*Auto expand*/ - if(col >= ext->col_cnt) lv_table_set_col_cnt(table, col + 1); - if(row >= ext->row_cnt) lv_table_set_row_cnt(table, row + 1); - - uint32_t cell = row * ext->col_cnt + col; - - if(ext->cell_data[cell] == NULL) { - ext->cell_data[cell] = lv_mem_alloc(2); /*+1: trailing '\0; +1: format byte*/ - LV_ASSERT_MEM(ext->cell_data[cell]); - if(ext->cell_data[cell] == NULL) return; - - ext->cell_data[cell][0] = 0; - ext->cell_data[cell][1] = '\0'; - } - - if(type > 0) type--; /*User gives 1,2,3,4 but easier to handle 0, 1, 2, 3*/ - if(type >= LV_TABLE_CELL_STYLE_CNT) type = LV_TABLE_CELL_STYLE_CNT - 1; - - lv_table_cell_format_t format; - format.format_byte = ext->cell_data[cell][0]; - format.s.type = type; - ext->cell_data[cell][0] = format.format_byte; - - ext->cell_types |= 1 << type; - - lv_obj_invalidate(table); -} - /** * Set the cell crop. (Don't adjust the height of the cell according to its content) * @param table pointer to a Table object @@ -461,7 +358,7 @@ void lv_table_set_cell_crop(lv_obj_t * table, uint16_t row, uint16_t col, bool c { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; /*Auto expand*/ if(col >= ext->col_cnt) lv_table_set_col_cnt(table, col + 1); @@ -495,7 +392,7 @@ void lv_table_set_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col, { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; /*Auto expand*/ if(col >= ext->col_cnt) lv_table_set_col_cnt(table, col + 1); @@ -519,6 +416,15 @@ void lv_table_set_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col, refr_size(table); } +void lv_table_set_cell_drawer(lv_obj_t * obj, lv_table_cell_drawer_cb_t drawer_cb) +{ + LV_ASSERT_OBJ(obj, LV_OBJX_NAME); + + lv_table_ext_t * ext = obj->ext_attr; + ext->drawer_cb = drawer_cb; + lv_obj_invalidate(obj); +} + /*===================== * Getter functions *====================*/ @@ -534,7 +440,7 @@ const char * lv_table_get_cell_value(lv_obj_t * table, uint16_t row, uint16_t co { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; if(row >= ext->row_cnt || col >= ext->col_cnt) { LV_LOG_WARN("lv_table_set_cell_value: invalid row or column"); return ""; @@ -555,7 +461,7 @@ uint16_t lv_table_get_row_cnt(lv_obj_t * table) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; return ext->row_cnt; } @@ -568,7 +474,7 @@ uint16_t lv_table_get_col_cnt(lv_obj_t * table) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; return ext->col_cnt; } @@ -582,7 +488,7 @@ lv_coord_t lv_table_get_col_width(lv_obj_t * table, uint16_t col_id) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; if(col_id >= ext->col_cnt) { LV_LOG_WARN("lv_table_set_col_width: too big 'col_id'. Must be < LV_TABLE_COL_MAX."); @@ -604,7 +510,7 @@ lv_label_align_t lv_table_get_cell_align(lv_obj_t * table, uint16_t row, uint16_ { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; if(row >= ext->row_cnt || col >= ext->col_cnt) { LV_LOG_WARN("lv_table_set_cell_align: invalid row or column"); return LV_LABEL_ALIGN_LEFT; /*Just return with something*/ @@ -620,33 +526,6 @@ lv_label_align_t lv_table_get_cell_align(lv_obj_t * table, uint16_t row, uint16_ } } -/** - * Get the type of a cell - * @param table pointer to a Table object - * @param row id of the row [0 .. row_cnt -1] - * @param col id of the column [0 .. col_cnt -1] - * @return 1,2,3 or 4 - */ -lv_label_align_t lv_table_get_cell_type(lv_obj_t * table, uint16_t row, uint16_t col) -{ - LV_ASSERT_OBJ(table, LV_OBJX_NAME); - - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); - if(row >= ext->row_cnt || col >= ext->col_cnt) { - LV_LOG_WARN("lv_table_get_cell_type: invalid row or column"); - return 1; /*Just return with something*/ - } - uint32_t cell = row * ext->col_cnt + col; - - if(ext->cell_data[cell] == NULL) - return 1; /*Just return with something*/ - else { - lv_table_cell_format_t format; - format.format_byte = ext->cell_data[cell][0]; - return format.s.type + 1; /*0,1,2,3 is stored but user sees 1,2,3,4*/ - } -} - /** * Get the crop property of a cell * @param table pointer to a Table object @@ -658,7 +537,7 @@ lv_label_align_t lv_table_get_cell_crop(lv_obj_t * table, uint16_t row, uint16_t { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; if(row >= ext->row_cnt || col >= ext->col_cnt) { LV_LOG_WARN("lv_table_get_cell_crop: invalid row or column"); return false; /*Just return with something*/ @@ -685,7 +564,7 @@ bool lv_table_get_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col) { LV_ASSERT_OBJ(table, LV_OBJX_NAME); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; if(row >= ext->row_cnt || col >= ext->col_cnt) { LV_LOG_WARN("lv_table_get_cell_merge_right: invalid row or column"); return false; @@ -711,7 +590,7 @@ bool lv_table_get_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col) */ lv_res_t lv_table_get_pressed_cell(lv_obj_t * table, uint16_t * row, uint16_t * col) { - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; lv_indev_type_t type = lv_indev_get_type(lv_indev_get_act()); if(type != LV_INDEV_TYPE_POINTER && type != LV_INDEV_TYPE_BUTTON) { @@ -727,7 +606,7 @@ lv_res_t lv_table_get_pressed_cell(lv_obj_t * table, uint16_t * row, uint16_t * if(col) { lv_coord_t x = p.x; x -= table->coords.x1; - x -= lv_obj_get_style_pad_left(table, LV_TABLE_PART_BG); + x -= lv_obj_get_style_pad_left(table, LV_PART_MAIN); *col = 0; tmp = 0; for(*col = 0; *col < ext->col_cnt; (*col)++) { @@ -739,7 +618,7 @@ lv_res_t lv_table_get_pressed_cell(lv_obj_t * table, uint16_t * row, uint16_t * if(row) { lv_coord_t y = p.y; y -= table->coords.y1; - y -= lv_obj_get_style_pad_top(table, LV_TABLE_PART_BG); + y -= lv_obj_get_style_pad_top(table, LV_PART_MAIN); *row = 0; tmp = 0; @@ -757,6 +636,64 @@ lv_res_t lv_table_get_pressed_cell(lv_obj_t * table, uint16_t * row, uint16_t * * STATIC FUNCTIONS **********************/ +static void lv_table_constructor(lv_obj_t * obj, lv_obj_t * parent, const lv_obj_t * copy) +{ + LV_LOG_TRACE("lv_table create started"); + + LV_CLASS_CONSTRUCTOR_BEGIN(obj, lv_table) + lv_obj.constructor(obj, parent, copy); + + + lv_table_ext_t * ext = obj->ext_attr; + + /*Initialize the allocated 'ext' */ + ext->cell_data = NULL; + ext->col_cnt = 0; + ext->row_cnt = 0; + ext->row_h = NULL; + ext->col_w = NULL; + + /*Init the new table table*/ + if(copy == NULL) { + ext->col_cnt = 1; + ext->row_cnt = 1; + ext->col_w = lv_mem_alloc(ext->col_cnt * sizeof(ext->col_w[0])); + ext->row_h = lv_mem_alloc(ext->row_cnt * sizeof(ext->row_h[0])); + ext->col_w[0] = LV_DPI; + ext->row_h[0] = LV_DPI; /*It will be overwritten when the theme is applied*/ + ext->cell_data = lv_mem_realloc(ext->cell_data, ext->row_cnt * ext->col_cnt * sizeof(char *)); + ext->cell_data[0] = NULL; + + lv_obj_set_size(obj, LV_SIZE_AUTO, LV_SIZE_AUTO); + } + /*Copy an existing table*/ + else { + lv_table_ext_t * copy_ext = copy->ext_attr; + lv_table_set_row_cnt(obj, copy_ext->row_cnt); + lv_table_set_col_cnt(obj, copy_ext->col_cnt); + } + LV_CLASS_CONSTRUCTOR_END(obj, lv_table) + LV_LOG_INFO("table created"); +} + +static void lv_table_destructor(lv_obj_t * obj) +{ +// /*Free the cell texts*/ +// uint16_t i; +// for(i = 0; i < ext->col_cnt * ext->row_cnt; i++) { +// if(ext->cell_data[i]) { +// lv_mem_free(ext->cell_data[i]); +// ext->cell_data[i] = NULL; +// } +// } +// +// if(ext->cell_data) lv_mem_free(ext->cell_data); +// if(ext->row_h) lv_mem_free(ext->row_h); +// +// for(i = 0; i < LV_TABLE_CELL_STYLE_CNT; i++) { +// _lv_obj_reset_style_list_no_refr(table, LV_TABLE_PART_CELL1 + i); +// } +} /** * Handle the drawing related tasks of the tables * @param table pointer to an object @@ -771,50 +708,39 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ { /*Return false if the object is not covers the mask_p area*/ if(mode == LV_DESIGN_COVER_CHK) { - return ancestor_design(table, clip_area, mode); + return lv_obj.design_cb(table, clip_area, mode); } /*Draw the object*/ else if(mode == LV_DESIGN_DRAW_MAIN) { /*Draw the background*/ - ancestor_design(table, clip_area, mode); + lv_obj.design_cb(table, clip_area, mode); - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; lv_point_t txt_size; lv_area_t cell_area; lv_area_t txt_area; lv_txt_flag_t txt_flags; - lv_style_int_t bg_top = lv_obj_get_style_pad_top(table, LV_TABLE_PART_BG); - lv_style_int_t bg_bottom = lv_obj_get_style_pad_bottom(table, LV_TABLE_PART_BG); - lv_style_int_t bg_left = lv_obj_get_style_pad_left(table, LV_TABLE_PART_BG); - lv_style_int_t bg_right = lv_obj_get_style_pad_right(table, LV_TABLE_PART_BG); + lv_coord_t bg_top = lv_obj_get_style_pad_top(table, LV_PART_MAIN); + lv_coord_t bg_bottom = lv_obj_get_style_pad_bottom(table, LV_PART_MAIN); + lv_coord_t bg_left = lv_obj_get_style_pad_left(table, LV_PART_MAIN); + lv_coord_t bg_right = lv_obj_get_style_pad_right(table, LV_PART_MAIN); - lv_draw_rect_dsc_t rect_dsc[LV_TABLE_CELL_STYLE_CNT]; - lv_draw_label_dsc_t label_dsc[LV_TABLE_CELL_STYLE_CNT]; - lv_draw_line_dsc_t line_dsc[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_left[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_right[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_top[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_bottom[LV_TABLE_CELL_STYLE_CNT]; + lv_coord_t cell_left = lv_obj_get_style_pad_left(table, LV_PART_ITEM); + lv_coord_t cell_right = lv_obj_get_style_pad_right(table, LV_PART_ITEM); + lv_coord_t cell_top = lv_obj_get_style_pad_top(table, LV_PART_ITEM); + lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(table, LV_PART_ITEM); - uint16_t i; - for(i = 0; i < LV_TABLE_CELL_STYLE_CNT; i++) { - if((ext->cell_types & (1 << i)) == 0) continue; /*Skip unused cell types*/ - lv_draw_rect_dsc_init(&rect_dsc[i]); - lv_obj_init_draw_rect_dsc(table, LV_TABLE_PART_CELL1 + i, &rect_dsc[i]); + lv_draw_rect_dsc_t rect_dsc_base; + lv_draw_rect_dsc_t rect_dsc_drawer; /*Passed to the drawer_cb to modify it*/ + lv_draw_rect_dsc_init(&rect_dsc_base); + lv_obj_init_draw_rect_dsc(table, LV_PART_ITEM, &rect_dsc_base); - lv_draw_label_dsc_init(&label_dsc[i]); - lv_obj_init_draw_label_dsc(table, LV_TABLE_PART_CELL1 + i, &label_dsc[i]); - - lv_draw_line_dsc_init(&line_dsc[i]); - lv_obj_init_draw_line_dsc(table, LV_TABLE_PART_CELL1 + i, &line_dsc[i]); - - cell_left[i] = lv_obj_get_style_pad_left(table, LV_TABLE_PART_CELL1 + i); - cell_right[i] = lv_obj_get_style_pad_right(table, LV_TABLE_PART_CELL1 + i); - cell_top[i] = lv_obj_get_style_pad_top(table, LV_TABLE_PART_CELL1 + i); - cell_bottom[i] = lv_obj_get_style_pad_bottom(table, LV_TABLE_PART_CELL1 + i); - } + lv_draw_label_dsc_t label_dsc_base; + lv_draw_label_dsc_t label_dsc_drawer; /*Passed to the drawer_cb to modify it*/ + lv_draw_label_dsc_init(&label_dsc_base); + lv_obj_init_draw_label_dsc(table, LV_PART_ITEM, &label_dsc_base); uint16_t col; uint16_t row; @@ -836,7 +762,6 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ else cell_area.x2 = table->coords.x1 + bg_left - 1 - scroll_x; for(col = 0; col < ext->col_cnt; col++) { - lv_table_cell_format_t format; if(ext->cell_data[cell]) { format.format_byte = ext->cell_data[cell][0]; @@ -844,7 +769,6 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ else { format.s.right_merge = 0; format.s.align = LV_LABEL_ALIGN_LEFT; - format.s.type = 0; format.s.crop = 1; } @@ -859,13 +783,14 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ uint16_t col_merge = 0; for(col_merge = 0; col_merge + col < ext->col_cnt - 1; col_merge++) { - if(ext->cell_data[cell + col_merge] != NULL) { + if(ext->cell_data[cell + col_merge]) { format.format_byte = ext->cell_data[cell + col_merge][0]; if(format.s.right_merge) if(rtl) cell_area.x1 -= ext->col_w[col + col_merge + 1]; else cell_area.x2 += ext->col_w[col + col_merge + 1]; - else + else { break; + } } else { break; @@ -878,46 +803,53 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ continue; } - uint8_t cell_type = format.s.type; - /*Expand the cell area with a half border to avoid drawing 2 borders next to each other*/ lv_area_t cell_area_border; lv_area_copy(&cell_area_border, &cell_area); - if((rect_dsc[cell_type].border_side & LV_BORDER_SIDE_LEFT) && cell_area_border.x1 > table->coords.x1 + bg_left) { - cell_area_border.x1 -= rect_dsc[cell_type].border_width / 2; + if((rect_dsc_base.border_side & LV_BORDER_SIDE_LEFT) && cell_area_border.x1 > table->coords.x1 + bg_left) { + cell_area_border.x1 -= rect_dsc_base.border_width / 2; } - if((rect_dsc[cell_type].border_side & LV_BORDER_SIDE_TOP) && cell_area_border.y1 > table->coords.y1 + bg_top) { - cell_area_border.y1 -= rect_dsc[cell_type].border_width / 2; + if((rect_dsc_base.border_side & LV_BORDER_SIDE_TOP) && cell_area_border.y1 > table->coords.y1 + bg_top) { + cell_area_border.y1 -= rect_dsc_base.border_width / 2; } - if((rect_dsc[cell_type].border_side & LV_BORDER_SIDE_RIGHT) && cell_area_border.x2 < table->coords.x2 - bg_right - 1) { - cell_area_border.x2 += rect_dsc[cell_type].border_width / 2 + (rect_dsc[cell_type].border_width & 0x1); + if((rect_dsc_base.border_side & LV_BORDER_SIDE_RIGHT) && cell_area_border.x2 < table->coords.x2 - bg_right - 1) { + cell_area_border.x2 += rect_dsc_base.border_width / 2 + (rect_dsc_base.border_width & 0x1); } - if((rect_dsc[cell_type].border_side & LV_BORDER_SIDE_BOTTOM) && + if((rect_dsc_base.border_side & LV_BORDER_SIDE_BOTTOM) && cell_area_border.y2 < table->coords.y2 - bg_bottom - 1) { - cell_area_border.y2 += rect_dsc[cell_type].border_width / 2 + (rect_dsc[cell_type].border_width & 0x1); + cell_area_border.y2 += rect_dsc_base.border_width / 2 + (rect_dsc_base.border_width & 0x1); } - lv_draw_rect(&cell_area_border, clip_area, &rect_dsc[cell_type]); + lv_draw_rect_dsc_t * rect_dsc_act = &rect_dsc_base; + lv_draw_label_dsc_t * label_dsc_act = &label_dsc_base; + + if(ext->drawer_cb) { + _lv_memcpy(&rect_dsc_drawer, &rect_dsc_base, sizeof(lv_draw_rect_dsc_t)); + _lv_memcpy(&label_dsc_drawer, &label_dsc_base, sizeof(lv_draw_label_dsc_t)); + bool drawn = ext->drawer_cb(table, row, col, &rect_dsc_drawer, &label_dsc_drawer, &cell_area_border, clip_area); + if(drawn) continue; + + rect_dsc_act = &rect_dsc_drawer; + label_dsc_act = &label_dsc_drawer; + } + + lv_draw_rect(&cell_area_border, clip_area, rect_dsc_act); if(ext->cell_data[cell]) { - txt_area.x1 = cell_area.x1 + cell_left[cell_type]; - txt_area.x2 = cell_area.x2 - cell_right[cell_type]; - txt_area.y1 = cell_area.y1 + cell_top[cell_type]; - txt_area.y2 = cell_area.y2 - cell_bottom[cell_type]; + txt_area.x1 = cell_area.x1 + cell_left; + txt_area.x2 = cell_area.x2 - cell_right; + txt_area.y1 = cell_area.y1 + cell_top; + txt_area.y2 = cell_area.y2 - cell_bottom; /*Align the content to the middle if not cropped*/ - if(format.s.crop == 0) { - txt_flags = LV_TXT_FLAG_NONE; - } - else { - txt_flags = LV_TXT_FLAG_EXPAND; - } + if(format.s.crop == 0) txt_flags = LV_TXT_FLAG_NONE; + else txt_flags = LV_TXT_FLAG_EXPAND; - _lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, label_dsc[cell_type].font, - label_dsc[cell_type].letter_space, label_dsc[cell_type].line_space, + _lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, label_dsc_base.font, + label_dsc_base.letter_space, label_dsc_base.line_space, lv_area_get_width(&txt_area), txt_flags); - label_dsc[cell_type].flag = 0; + label_dsc_base.flag = 0; /*Align the content to the middle if not cropped*/ if(format.s.crop == 0) { txt_area.y1 = cell_area.y1 + h_row / 2 - txt_size.y / 2; @@ -927,13 +859,13 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ switch(format.s.align) { default: case LV_LABEL_ALIGN_LEFT: - label_dsc[cell_type].flag |= LV_TXT_FLAG_NONE; + label_dsc_base.flag |= LV_TXT_FLAG_NONE; break; case LV_LABEL_ALIGN_RIGHT: - label_dsc[cell_type].flag |= LV_TXT_FLAG_RIGHT; + label_dsc_base.flag |= LV_TXT_FLAG_RIGHT; break; case LV_LABEL_ALIGN_CENTER: - label_dsc[cell_type].flag |= LV_TXT_FLAG_CENTER; + label_dsc_base.flag |= LV_TXT_FLAG_CENTER; break; } @@ -941,27 +873,7 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ bool label_mask_ok; label_mask_ok = _lv_area_intersect(&label_mask, clip_area, &cell_area); if(label_mask_ok) { - lv_draw_label(&txt_area, &label_mask, &label_dsc[cell_type], ext->cell_data[cell] + 1, NULL); - } - - /*Draw lines after '\n's*/ - lv_point_t p1; - lv_point_t p2; - p1.x = cell_area.x1; - p2.x = cell_area.x2; - for(i = 1; ext->cell_data[cell][i] != '\0'; i++) { - if(ext->cell_data[cell][i] == '\n') { - ext->cell_data[cell][i] = '\0'; - _lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, label_dsc[cell_type].font, - label_dsc[cell_type].letter_space, label_dsc[cell_type].line_space, - lv_area_get_width(&txt_area), txt_flags); - - p1.y = txt_area.y1 + txt_size.y + label_dsc[cell_type].line_space / 2; - p2.y = txt_area.y1 + txt_size.y + label_dsc[cell_type].line_space / 2; - lv_draw_line(&p1, &p2, clip_area, &line_dsc[cell_type]); - - ext->cell_data[cell][i] = '\n'; - } + lv_draw_label(&txt_area, &label_mask, label_dsc_act, ext->cell_data[cell] + 1, NULL); } } @@ -972,7 +884,7 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ } /*Post draw when the children are drawn*/ else if(mode == LV_DESIGN_DRAW_POST) { - ancestor_design(table, clip_area, mode); + lv_obj.design_cb(table, clip_area, mode); } return LV_DESIGN_RES_OK; @@ -985,44 +897,18 @@ static lv_design_res_t lv_table_design(lv_obj_t * table, const lv_area_t * clip_ * @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_table_signal(lv_obj_t * table, lv_signal_t sign, void * param) +static lv_res_t lv_table_signal(lv_obj_t * obj, lv_signal_t sign, void * param) { lv_res_t res; /* Include the ancient signal function */ - res = ancestor_signal(table, sign, param); + res = lv_obj.signal_cb(obj, sign, param); if(res != LV_RES_OK) return res; - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = obj->ext_attr; - if(sign == LV_SIGNAL_GET_TYPE) { - return _lv_obj_handle_get_type_signal(param, LV_OBJX_NAME); - } - else if(sign == LV_SIGNAL_GET_STYLE) { - lv_get_style_info_t * info = param; - info->result = lv_table_get_style(table, info->part); - if(info->result != NULL) return LV_RES_OK; - else return ancestor_signal(table, sign, param); - } - else if(sign == LV_SIGNAL_CLEANUP) { - /*Free the cell texts*/ - uint16_t i; - for(i = 0; i < ext->col_cnt * ext->row_cnt; i++) { - if(ext->cell_data[i]) { - lv_mem_free(ext->cell_data[i]); - ext->cell_data[i] = NULL; - } - } - - if(ext->cell_data) lv_mem_free(ext->cell_data); - if(ext->row_h) lv_mem_free(ext->row_h); - - for(i = 0; i < LV_TABLE_CELL_STYLE_CNT; i++) { - _lv_obj_reset_style_list_no_refr(table, LV_TABLE_PART_CELL1 + i); - } - } - else if(sign == LV_SIGNAL_STYLE_CHG) { - refr_size(table); + if(sign == LV_SIGNAL_STYLE_CHG) { + refr_size(obj); } else if(sign == LV_SIGNAL_GET_SELF_SIZE) { lv_point_t * p = param; @@ -1041,55 +927,20 @@ static lv_res_t lv_table_signal(lv_obj_t * table, lv_signal_t sign, void * param } -/** - * Get the style descriptor of a part of the object - * @param table pointer the object - * @param part the part from. (LV_TABLE_PART_...) - * @return pointer to the style descriptor of the specified part - */ -static lv_style_list_t * lv_table_get_style(lv_obj_t * table, uint8_t part) -{ - LV_ASSERT_OBJ(table, LV_OBJX_NAME); - - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); - - /* Because of the presence of LV_TABLE_PART_BG, LV_TABLE_PART_CELL has an integer value - of . This comes in useful to extend above code with more cell types as follows */ - if(part == LV_TABLE_PART_BG) { - return &table->style_list; - } - else if(part >= 1 && part <= LV_TABLE_CELL_STYLE_CNT) { - return &ext->cell_style[part - 1]; - } - - return NULL; -} - static void refr_size(lv_obj_t * table) { - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; uint32_t i; - lv_style_int_t cell_left[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_right[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_top[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t cell_bottom[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t letter_space[LV_TABLE_CELL_STYLE_CNT]; - lv_style_int_t line_space[LV_TABLE_CELL_STYLE_CNT]; - const lv_font_t * font[LV_TABLE_CELL_STYLE_CNT]; - - for(i = 0; i < LV_TABLE_CELL_STYLE_CNT; i++) { - if((ext->cell_types & (1 << i)) == 0) continue; /*Skip unused cell types*/ - cell_left[i] = lv_obj_get_style_pad_left(table, LV_TABLE_PART_CELL1 + i); - cell_right[i] = lv_obj_get_style_pad_right(table, LV_TABLE_PART_CELL1 + i); - cell_top[i] = lv_obj_get_style_pad_top(table, LV_TABLE_PART_CELL1 + i); - cell_bottom[i] = lv_obj_get_style_pad_bottom(table, LV_TABLE_PART_CELL1 + i); - letter_space[i] = lv_obj_get_style_text_letter_space(table, LV_TABLE_PART_CELL1 + i); - line_space[i] = lv_obj_get_style_text_line_space(table, LV_TABLE_PART_CELL1 + i); - font[i] = lv_obj_get_style_text_font(table, LV_TABLE_PART_CELL1 + i); - } + lv_coord_t cell_left = lv_obj_get_style_pad_left(table, LV_PART_ITEM); + lv_coord_t cell_right = lv_obj_get_style_pad_right(table, LV_PART_ITEM); + lv_coord_t cell_top = lv_obj_get_style_pad_top(table, LV_PART_ITEM); + lv_coord_t cell_bottom = lv_obj_get_style_pad_bottom(table, LV_PART_ITEM); + lv_coord_t letter_space = lv_obj_get_style_text_letter_space(table, LV_PART_ITEM); + lv_coord_t line_space = lv_obj_get_style_text_line_space(table, LV_PART_ITEM); + const lv_font_t * font = lv_obj_get_style_text_font(table, LV_PART_ITEM); for(i = 0; i < ext->row_cnt; i++) { ext->row_h[i] = get_row_height(table, i, font, letter_space, line_space, @@ -1099,18 +950,18 @@ static void refr_size(lv_obj_t * table) _lv_obj_handle_self_size_chg(table); } -static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id, const lv_font_t ** font, - lv_style_int_t * letter_space, lv_style_int_t * line_space, - lv_style_int_t * cell_left, lv_style_int_t * cell_right, lv_style_int_t * cell_top, lv_style_int_t * cell_bottom) +static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id, const lv_font_t * font, + lv_coord_t letter_space, lv_coord_t line_space, + lv_coord_t cell_left, lv_coord_t cell_right, lv_coord_t cell_top, lv_coord_t cell_bottom) { - lv_table_ext_t * ext = lv_obj_get_ext_attr(table); + lv_table_ext_t * ext = table->ext_attr; lv_point_t txt_size; lv_coord_t txt_w; uint16_t row_start = row_id * ext->col_cnt; uint16_t cell; uint16_t col; - lv_coord_t h_max = lv_font_get_line_height(font[0]) + cell_top[0] + cell_bottom[0]; + lv_coord_t h_max = lv_font_get_line_height(font) + cell_top + cell_bottom; for(cell = row_start, col = 0; cell < row_start + ext->col_cnt; cell++, col++) { if(ext->cell_data[cell] != NULL) { @@ -1133,21 +984,20 @@ static lv_coord_t get_row_height(lv_obj_t * table, uint16_t row_id, const lv_fon lv_table_cell_format_t format; format.format_byte = ext->cell_data[cell][0]; - uint8_t cell_type = format.s.type; /*With text crop assume 1 line*/ if(format.s.crop) { - h_max = LV_MATH_MAX(lv_font_get_line_height(font[cell_type]) + cell_top[cell_type] + cell_bottom[cell_type], + h_max = LV_MATH_MAX(lv_font_get_line_height(font) + cell_top + cell_bottom, h_max); } /*Without text crop calculate the height of the text in the cell*/ else { - txt_w -= cell_left[cell_type] + cell_right[cell_type]; + txt_w -= cell_left + cell_right; - _lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, font[cell_type], - letter_space[cell_type], line_space[cell_type], txt_w, LV_TXT_FLAG_NONE); + _lv_txt_get_size(&txt_size, ext->cell_data[cell] + 1, font, + letter_space, line_space, txt_w, LV_TXT_FLAG_NONE); - h_max = LV_MATH_MAX(txt_size.y + cell_top[cell_type] + cell_bottom[cell_type], h_max); + h_max = LV_MATH_MAX(txt_size.y + cell_top + cell_bottom, h_max); cell += col_merge; col += col_merge; } diff --git a/src/lv_widgets/lv_table.h b/src/lv_widgets/lv_table.h index 69e295931..0cf2170a6 100644 --- a/src/lv_widgets/lv_table.h +++ b/src/lv_widgets/lv_table.h @@ -28,26 +28,13 @@ extern "C" { /********************* * DEFINES *********************/ -#ifndef LV_TABLE_COL_MAX -#define LV_TABLE_COL_MAX 12 -#endif - -/* - Maximum allowable value of LV_TABLE_CELL_STYLE_CNT is 16 - because of restriction of lv_table_cell_format_t.type to no more than - 4 bits so that lv_table_cell_format_t.s will not exceed 8 bits -*/ -#ifndef LV_TABLE_CELL_STYLE_CNT -# define LV_TABLE_CELL_STYLE_CNT 4 -#endif -#if (LV_TABLE_CELL_STYLE_CNT > 16) -# error "LV_TABLE_CELL_STYLE_CNT cannot exceed 16" -#endif /********************** * TYPEDEFS **********************/ +typedef bool (*lv_table_cell_drawer_cb_t)(lv_obj_t * table, uint32_t row, uint32_t cell, lv_draw_rect_dsc_t * rect_draw_dsc, lv_draw_label_dsc_t * label_draw_dsc, const lv_area_t * draw_area, const lv_area_t * clip_area); + /** * Internal table cell format structure. * @@ -57,7 +44,6 @@ typedef union { struct { uint8_t align : 2; uint8_t right_merge : 1; - uint8_t type : 4; // upto 16 values uint8_t crop : 1; } s; uint8_t format_byte; @@ -70,20 +56,11 @@ typedef struct { uint16_t row_cnt; char ** cell_data; lv_coord_t * row_h; - lv_style_list_t cell_style[LV_TABLE_CELL_STYLE_CNT]; lv_coord_t * col_w; - uint16_t cell_types : LV_TABLE_CELL_STYLE_CNT; /*Keep track which cell types exists to avoid dealing with unused ones*/ + lv_table_cell_drawer_cb_t drawer_cb; } lv_table_ext_t; -/*Parts of the table*/ -enum { - LV_TABLE_PART_BG, /* Because of this member, LV_PART.*CELL1 has enum value of 1, */ - LV_TABLE_PART_CELL1, /* LV_PART.*CELL2 has an enum value of 2 and so on upto the maximum */ - LV_TABLE_PART_CELL2, /* number of styles specified by LV_TABLE_CELL_STYLE_CNT */ - LV_TABLE_PART_CELL3, - LV_TABLE_PART_CELL4, /* CELL 5-16 are not needed to be defined, the values in this enum - are there for backward compatibility */ -}; +extern const lv_obj_class_t lv_table; /********************** * GLOBAL PROTOTYPES @@ -151,15 +128,6 @@ void lv_table_set_col_width(lv_obj_t * table, uint16_t col_id, lv_coord_t w); */ void lv_table_set_cell_align(lv_obj_t * table, uint16_t row, uint16_t col, lv_label_align_t align); -/** - * Set the type of a cell. - * @param table pointer to a Table object - * @param row id of the row [0 .. row_cnt -1] - * @param col id of the column [0 .. col_cnt -1] - * @param type 1,2,3 or 4. The cell style will be chosen accordingly. - */ -void lv_table_set_cell_type(lv_obj_t * table, uint16_t row, uint16_t col, uint8_t type); - /** * Set the cell crop. (Don't adjust the height of the cell according to its content) * @param table pointer to a Table object @@ -178,6 +146,8 @@ void lv_table_set_cell_crop(lv_obj_t * table, uint16_t row, uint16_t col, bool c */ void lv_table_set_cell_merge_right(lv_obj_t * table, uint16_t row, uint16_t col, bool en); +void lv_table_set_cell_drawer(lv_obj_t * obj, lv_table_cell_drawer_cb_t drawer_cb); + /*===================== * Getter functions *====================*/ diff --git a/src/lv_widgets/lv_textarea.c b/src/lv_widgets/lv_textarea.c index 7551f2429..5ac69e26b 100644 --- a/src/lv_widgets/lv_textarea.c +++ b/src/lv_widgets/lv_textarea.c @@ -1160,7 +1160,7 @@ void lv_textarea_cursor_down(lv_obj_t * ta) /*Increment the y with one line and keep the valid x*/ - lv_style_int_t line_space = lv_obj_get_style_text_line_space(ta, LV_TEXTAREA_PART_MAIN); + lv_coord_t line_space = lv_obj_get_style_text_line_space(ta, LV_TEXTAREA_PART_MAIN); const lv_font_t * font = lv_obj_get_style_text_font(ta, LV_TEXTAREA_PART_MAIN); lv_coord_t font_h = lv_font_get_line_height(font); pos.y += font_h + line_space + 1; @@ -1192,7 +1192,7 @@ void lv_textarea_cursor_up(lv_obj_t * ta) lv_label_get_letter_pos(ext->label, lv_textarea_get_cursor_pos(ta), &pos); /*Decrement the y with one line and keep the valid x*/ - lv_style_int_t line_space = lv_obj_get_style_text_line_space(ta, LV_TEXTAREA_PART_MAIN); + lv_coord_t line_space = lv_obj_get_style_text_line_space(ta, LV_TEXTAREA_PART_MAIN); const lv_font_t * font = lv_obj_get_style_text_font(ta, LV_TEXTAREA_PART_MAIN); lv_coord_t font_h = lv_font_get_line_height(font); pos.y -= font_h + line_space - 1; @@ -1277,8 +1277,8 @@ static lv_res_t lv_textarea_signal(lv_obj_t * ta, lv_signal_t sign, void * param else if(sign == LV_SIGNAL_STYLE_CHG) { if(ext->label) { if(ext->one_line) { - lv_style_int_t top = lv_obj_get_style_pad_top(ta, LV_TEXTAREA_PART_MAIN); - lv_style_int_t bottom = lv_obj_get_style_pad_bottom(ta, LV_TEXTAREA_PART_MAIN); + lv_coord_t top = lv_obj_get_style_pad_top(ta, LV_TEXTAREA_PART_MAIN); + lv_coord_t bottom = lv_obj_get_style_pad_bottom(ta, LV_TEXTAREA_PART_MAIN); const lv_font_t * font = lv_obj_get_style_text_font(ta, LV_TEXTAREA_PART_MAIN); /*In one line mode refresh the Text Area height because 'vpad' can modify it*/ @@ -1501,7 +1501,7 @@ static void refr_cursor_area(lv_obj_t * ta) lv_textarea_ext_t * ext = lv_obj_get_ext_attr(ta); const lv_font_t * font = lv_obj_get_style_text_font(ta, LV_TEXTAREA_PART_MAIN); - lv_style_int_t line_space = lv_obj_get_style_text_line_space(ta, LV_TEXTAREA_PART_MAIN); + lv_coord_t line_space = lv_obj_get_style_text_line_space(ta, LV_TEXTAREA_PART_MAIN); uint32_t cur_pos = lv_textarea_get_cursor_pos(ta); const char * txt = lv_label_get_text(ext->label); @@ -1549,10 +1549,10 @@ static void refr_cursor_area(lv_obj_t * ta) ext->cursor.txt_byte_pos = byte_pos; /*Calculate the cursor according to its type*/ - lv_style_int_t top = lv_obj_get_style_pad_top(ta, LV_TEXTAREA_PART_CURSOR); - lv_style_int_t bottom = lv_obj_get_style_pad_bottom(ta, LV_TEXTAREA_PART_CURSOR); - lv_style_int_t left = lv_obj_get_style_pad_left(ta, LV_TEXTAREA_PART_CURSOR); - lv_style_int_t right = lv_obj_get_style_pad_right(ta, LV_TEXTAREA_PART_CURSOR); + lv_coord_t top = lv_obj_get_style_pad_top(ta, LV_TEXTAREA_PART_CURSOR); + lv_coord_t bottom = lv_obj_get_style_pad_bottom(ta, LV_TEXTAREA_PART_CURSOR); + lv_coord_t left = lv_obj_get_style_pad_left(ta, LV_TEXTAREA_PART_CURSOR); + lv_coord_t right = lv_obj_get_style_pad_right(ta, LV_TEXTAREA_PART_CURSOR); lv_area_t cur_area; cur_area.x1 = letter_pos.x - left; @@ -1774,8 +1774,8 @@ static void draw_cursor(lv_obj_t * ta, const lv_area_t * clip_area) _lv_memcpy(letter_buf, &txt[ext->cursor.txt_byte_pos], _lv_txt_encoded_size(&txt[ext->cursor.txt_byte_pos])); if(cur_dsc.bg_opa == LV_OPA_COVER) { - lv_style_int_t left = lv_obj_get_style_pad_left(ta, LV_TEXTAREA_PART_CURSOR); - lv_style_int_t top = lv_obj_get_style_pad_top(ta, LV_TEXTAREA_PART_CURSOR); + lv_coord_t left = lv_obj_get_style_pad_left(ta, LV_TEXTAREA_PART_CURSOR); + lv_coord_t top = lv_obj_get_style_pad_top(ta, LV_TEXTAREA_PART_CURSOR); cur_area.x1 += left; cur_area.y1 += top; diff --git a/tests/lv_test_core/lv_test_style.c b/tests/lv_test_core/lv_test_style.c index 0d1f3a5ed..4697750fb 100644 --- a/tests/lv_test_core/lv_test_style.c +++ b/tests/lv_test_core/lv_test_style.c @@ -72,7 +72,7 @@ static void empty_style(void) lv_style_list_init(&style_list); lv_res_t found; - lv_style_int_t value; + lv_coord_t value; lv_opa_t opa; const void * ptr; lv_color_t color; @@ -107,7 +107,7 @@ static void add_remove_read_prop(void) _lv_style_list_add_style(&style_list, &style); lv_res_t found; - lv_style_int_t value; + lv_coord_t value; lv_opa_t opa; const void * ptr; lv_color_t color; @@ -185,7 +185,7 @@ static void cascade(void) _lv_style_list_add_style(&style_list, &style_second); lv_res_t found; - lv_style_int_t value; + lv_coord_t value; lv_opa_t opa; const void * ptr; lv_color_t color; @@ -281,7 +281,7 @@ static void copy(void) lv_style_copy(&style_dest, &style_src); int16_t weight; - lv_style_int_t value; + lv_coord_t value; weight = _lv_style_get_int(&style_dest, LV_STYLE_TEXT_LINE_SPACE, &value); lv_test_assert_int_eq(0, weight, "Get a copied property from a style"); @@ -336,7 +336,7 @@ static void states(void) _lv_style_set_int(&style_first, LV_STYLE_TEXT_LINE_SPACE | (LV_STATE_PRESSED) << LV_STYLE_STATE_POS, 7); lv_res_t found; - lv_style_int_t value; + lv_coord_t value; found = _lv_style_list_get_int(&style_list, LV_STYLE_TEXT_LINE_SPACE, &value); lv_test_assert_int_eq(LV_RES_OK, found, "Get an 'int' property in normal state");