add combo_box_remove_option

This commit is contained in:
lixianjing 2021-08-04 11:34:19 +08:00
parent 37fa4eacde
commit b29ac810f2
4 changed files with 65 additions and 0 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2021/08/04
* 增加函数combo\_box\_remove\_option。
2021/08/03
* 键盘支持跟随编辑(指定floating=true即可)
* 更新文档。

View File

@ -576,6 +576,34 @@ widget_t* combo_box_create(widget_t* parent, xy_t x, xy_t y, wh_t w, wh_t h) {
return widget;
}
ret_t combo_box_remove_option(widget_t* widget, int32_t value) {
combo_box_option_t* iter = NULL;
combo_box_option_t* prev = NULL;
combo_box_t* combo_box = COMBO_BOX(widget);
return_value_if_fail(combo_box != NULL, RET_BAD_PARAMS);
iter = combo_box->option_items;
prev = combo_box->option_items;
while (iter != NULL) {
if (iter->value == value) {
if (iter == combo_box->option_items) {
combo_box->option_items = iter->next;
} else {
prev->next = iter->next;
}
TKMEM_FREE(iter->text);
TKMEM_FREE(iter);
return RET_OK;
}
prev = iter;
iter = iter->next;
}
return RET_NOT_FOUND;
}
ret_t combo_box_reset_options(widget_t* widget) {
combo_box_option_t* iter = NULL;
combo_box_option_t* next = NULL;

View File

@ -326,6 +326,17 @@ ret_t combo_box_set_item_height(widget_t* widget, uint32_t item_height);
*/
ret_t combo_box_append_option(widget_t* widget, int32_t value, const char* text);
/**
* @method combo_box_remove_option
*
* @annotation ["scriptable"]
* @param {widget_t*} widget combo_box对象
* @param {int32_t} value
*
* @return {ret_t} RET_OK表示成功
*/
ret_t combo_box_remove_option(widget_t* widget, int32_t value);
/**
* @method combo_box_set_options
*

View File

@ -288,3 +288,26 @@ TEST(ComboBox, events) {
widget_destroy(w);
}
TEST(ComboBox, remove_option) {
widget_t* w = combo_box_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(combo_box_append_option(w, 1, "red"), RET_OK);
ASSERT_EQ(combo_box_append_option(w, 2, "green"), RET_OK);
ASSERT_EQ(combo_box_append_option(w, 3, "blue"), RET_OK);
ASSERT_EQ(combo_box_count_options(w), 3);
ASSERT_EQ(combo_box_remove_option(w, 1), RET_OK);
ASSERT_EQ(combo_box_count_options(w), 2);
ASSERT_EQ(combo_box_remove_option(w, 1), RET_NOT_FOUND);
ASSERT_EQ(combo_box_count_options(w), 2);
ASSERT_EQ(combo_box_remove_option(w, 3), RET_OK);
ASSERT_EQ(combo_box_count_options(w), 1);
ASSERT_EQ(combo_box_remove_option(w, 2), RET_OK);
ASSERT_EQ(combo_box_count_options(w), 0);
widget_destroy(w);
}