diff --git a/lv_objx/lv_list.c b/lv_objx/lv_list.c index 6e7a232fd..62b258503 100644 --- a/lv_objx/lv_list.c +++ b/lv_objx/lv_list.c @@ -38,6 +38,7 @@ static lv_res_t lv_list_signal(lv_obj_t * list, lv_signal_t sign, void * param); static lv_res_t lv_list_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * param); static void refr_btn_width(lv_obj_t * list); +static void lv_list_btn_single_selected(lv_obj_t *btn); /********************** * STATIC VARIABLES @@ -89,6 +90,8 @@ lv_obj_t * lv_list_create(lv_obj_t * par, const lv_obj_t * copy) ext->styles_btn[LV_BTN_STATE_TGL_PR] = &lv_style_btn_tgl_pr; ext->styles_btn[LV_BTN_STATE_INA] = &lv_style_btn_ina; ext->anim_time = LV_LIST_FOCUS_TIME; + ext->single_mode = false; + #if USE_LV_GROUP ext->last_sel = NULL; ext->selected_btn = NULL; @@ -256,6 +259,18 @@ bool lv_list_remove(const lv_obj_t * list, uint32_t index) * Setter functions *====================*/ +/** + * Set single button selected mode, only one button will be selected if enabled. + * @param list pointer to the currently pressed list object + * @param mode, enable(true)/disable(false) single selected mode. + */ +void lv_list_set_single_mode(lv_obj_t *list, bool mode) +{ + lv_list_ext_t * ext = lv_obj_get_ext_attr(list); + + ext->single_mode = mode; +} + #if USE_LV_GROUP /** @@ -368,6 +383,17 @@ void lv_list_set_style(lv_obj_t * list, lv_list_style_t type, lv_style_t * style * Getter functions *====================*/ +/** + * Get single button selected mode. + * @param list pointer to the currently pressed list object. + */ +bool lv_list_get_single_mode(lv_obj_t *list) +{ + lv_list_ext_t * ext = lv_obj_get_ext_attr(list); + + return (ext->single_mode); +} + /** * Get the text of a list element * @param btn pointer to list element @@ -859,7 +885,10 @@ static lv_res_t lv_list_btn_signal(lv_obj_t * btn, lv_signal_t sign, void * para * to mark it as selected (pressed state)*/ last_clicked_btn = btn; #endif - + if(lv_indev_is_dragging(lv_indev_get_act()) == false && ext->single_mode) + { + lv_list_btn_single_selected(btn); + } } else if(sign == LV_SIGNAL_PRESS_LOST) { lv_obj_t * list = lv_obj_get_parent(lv_obj_get_parent(btn)); @@ -900,5 +929,27 @@ static void refr_btn_width(lv_obj_t * list) } } +/** + * Make a single button selected in the list, deselect others, should be called in list btns call back. + * @param btn pointer to the currently pressed list btn object + */ +static void lv_list_btn_single_selected(lv_obj_t *btn) +{ + lv_obj_t *list = lv_obj_get_parent(lv_obj_get_parent(btn)); + + lv_obj_t * e = lv_list_get_next_btn(list, NULL); + do + { + if(e == btn) + { + lv_btn_set_state(e, LV_BTN_STATE_TGL_REL); + } + else + { + lv_btn_set_state(e, LV_BTN_STATE_REL); + } + e = lv_list_get_next_btn(list, e); + } while (e != NULL); +} #endif diff --git a/lv_objx/lv_list.h b/lv_objx/lv_list.h index f31a155a7..572f0189e 100644 --- a/lv_objx/lv_list.h +++ b/lv_objx/lv_list.h @@ -57,6 +57,7 @@ typedef struct lv_style_t *styles_btn[LV_BTN_STATE_NUM]; /*Styles of the list element buttons*/ lv_style_t *style_img; /*Style of the list element images on buttons*/ uint32_t size; /*the number of items(buttons) in the list*/ + bool single_mode; /* whether single selected mode is enabled */ #if USE_LV_GROUP lv_obj_t * last_sel; /* Last btn selected */ lv_obj_t * selected_btn; @@ -120,6 +121,14 @@ bool lv_list_remove(const lv_obj_t * list, uint32_t index); /*===================== * Setter functions *====================*/ + +/** + * Set single button selected mode, only one button will be selected if enabled. + * @param list pointer to the currently pressed list object + * @param mode, enable(true)/disable(false) single selected mode. + */ +void lv_list_set_single_mode(lv_obj_t *list, bool mode); + #if USE_LV_GROUP /** @@ -179,6 +188,12 @@ void lv_list_set_style(lv_obj_t *list, lv_list_style_t type, lv_style_t *style); * Getter functions *====================*/ +/** + * Get single button selected mode. + * @param list pointer to the currently pressed list object. + */ +bool lv_list_get_single_mode(lv_obj_t *list); + /** * Get the text of a list element * @param btn pointer to list element