diff --git a/src/core/lv_obj.c b/src/core/lv_obj.c index ba6aa02db..7ed8e4da5 100644 --- a/src/core/lv_obj.c +++ b/src/core/lv_obj.c @@ -391,7 +391,37 @@ void lv_obj_add_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, void * user_dat obj->spec_attr->event_dsc[obj->spec_attr->event_dsc_cnt - 1].cb = event_cb; obj->spec_attr->event_dsc[obj->spec_attr->event_dsc_cnt - 1].user_data = user_data; +} +bool lv_obj_remove_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, void * user_data) +{ + LV_ASSERT_OBJ(obj, MY_CLASS); + if(obj->spec_attr == NULL) return false; + + int32_t i = 0; + for(i = 0; i < obj->spec_attr->event_dsc_cnt; i++) { + if(obj->spec_attr->event_dsc[i].cb == event_cb) { + /*Check if user_data matches or if the requested user_data is NULL*/ + if(user_data == NULL || obj->spec_attr->event_dsc[i].user_data == user_data) { + /*Found*/ + break; + } + } + } + if(i >= obj->spec_attr->event_dsc_cnt) { + /*No event handler found*/ + return false; + } + + /*Shift the remaining event handlers forward*/ + for(; i < (obj->spec_attr->event_dsc_cnt-1); i++) { + obj->spec_attr->event_dsc[i].cb = obj->spec_attr->event_dsc[i+1].cb; + obj->spec_attr->event_dsc[i].user_data = obj->spec_attr->event_dsc[i+1].user_data; + } + obj->spec_attr->event_dsc_cnt--; + obj->spec_attr->event_dsc = lv_mem_realloc(obj->spec_attr->event_dsc, obj->spec_attr->event_dsc_cnt * sizeof(lv_event_dsc_t)); + LV_ASSERT_MALLOC(obj->spec_attr->event_dsc); + return true; } void lv_obj_set_base_dir(lv_obj_t * obj, lv_bidi_dir_t dir) diff --git a/src/core/lv_obj.h b/src/core/lv_obj.h index 2ba6cd151..9e3d53613 100644 --- a/src/core/lv_obj.h +++ b/src/core/lv_obj.h @@ -405,15 +405,26 @@ void lv_obj_add_state(lv_obj_t * obj, lv_state_t state); void lv_obj_clear_state(lv_obj_t * obj, lv_state_t state); /** - * Add a an event handler function for an object. + * Add an event handler function for an object. * Used by the user to react on event which happens with the object. - * An object can have multiple event handler. They will be called in the same the order as they were added. + * An object can have multiple event handler. They will be called in the same order as they were added. * @param obj pointer to an object * @param event_cb the new event function * @param user_data custom data data will be available in `event_cb` */ void lv_obj_add_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, void * user_data); +/** + * Remove an event handler function for an object. + * Used by the user to react on event which happens with the object. + * An object can have multiple event handler. They will be called in the same order as they were added. + * @param obj pointer to an object + * @param event_cb the event function to remove + * @param user_data if NULL, remove the first event handler with the same cb, otherwise remove the first event handler with the same cb and user_data + * @return true if any event handlers were removed + */ +bool lv_obj_remove_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, void * user_data); + /** * Set the base direction of the object * @param obj pointer to an object