mirror of
https://gitee.com/zlgopen/awtk.git
synced 2024-12-02 03:58:33 +08:00
improve text selector to support escape char
This commit is contained in:
parent
63b9a58b49
commit
dd54687a52
@ -1,6 +1,7 @@
|
||||
# 最新动态
|
||||
2022/06/17
|
||||
* 完善str_unescape
|
||||
* text selector的options支持字符转义,方便使用特殊字符。
|
||||
|
||||
2022/06/16
|
||||
* 完善draggable
|
||||
|
@ -294,11 +294,13 @@ static ret_t text_selector_on_destroy(widget_t* widget) {
|
||||
}
|
||||
|
||||
ret_t text_selector_parse_options(widget_t* widget, const char* str) {
|
||||
str_t s;
|
||||
int32_t i = 0;
|
||||
tokenizer_t tokenizer;
|
||||
tokenizer_t* t = &tokenizer;
|
||||
text_selector_t* text_selector = TEXT_SELECTOR(widget);
|
||||
return_value_if_fail(widget != NULL && text_selector != NULL, RET_BAD_PARAMS);
|
||||
return_value_if_fail(str_init(&s, 100) != NULL, RET_OOM);
|
||||
|
||||
text_selector->options = tk_strdup(str);
|
||||
tokenizer_init(t, str, strlen(str), ";");
|
||||
@ -316,10 +318,13 @@ ret_t text_selector_parse_options(widget_t* widget, const char* str) {
|
||||
value = i;
|
||||
}
|
||||
|
||||
text_selector_append_option(widget, value, text);
|
||||
str_set(&s, text);
|
||||
str_unescape(&s);
|
||||
text_selector_append_option(widget, value, s.str);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
str_reset(&s);
|
||||
tokenizer_deinit(t);
|
||||
|
||||
return RET_OK;
|
||||
@ -349,11 +354,13 @@ ret_t text_selector_set_options(widget_t* widget, const char* options) {
|
||||
|
||||
text_selector_reset_options(widget);
|
||||
if (strchr(options, ':') == NULL && strchr(options, '-') != NULL) {
|
||||
str_t s;
|
||||
int nr = 0;
|
||||
int end = 0;
|
||||
int step = 1;
|
||||
int start = 0;
|
||||
char format[41];
|
||||
ret_t ret = RET_FAIL;
|
||||
memset(format, 0x00, sizeof(format));
|
||||
|
||||
nr = tk_sscanf(options, "%d-%d-%40s", &start, &end, format);
|
||||
@ -369,7 +376,13 @@ ret_t text_selector_set_options(widget_t* widget, const char* options) {
|
||||
}
|
||||
}
|
||||
|
||||
return text_selector_set_range_options_ex(widget, start, end - start + 1, step, format);
|
||||
if (str_init(&s, sizeof(format)) != NULL) {
|
||||
str_set(&s, format);
|
||||
str_unescape(&s);
|
||||
ret = text_selector_set_range_options_ex(widget, start, end - start + 1, step, s.str);
|
||||
str_reset(&s);
|
||||
}
|
||||
return ret;
|
||||
} else {
|
||||
return text_selector_parse_options(widget, options);
|
||||
}
|
||||
|
@ -98,9 +98,13 @@ typedef struct _text_selector_t {
|
||||
/**
|
||||
* @property {char*} options
|
||||
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
|
||||
* 设置可选项(冒号分隔值和文本,分号分隔选项,如:1:red;2:green;3:blue)。
|
||||
* 对于数值选项,也可以指定一个范围,用『-』分隔起始值、结束值和格式。
|
||||
* 设置可选项(英文冒号(:)分隔值和文本,英文分号(;)分隔选项,如:1:red;2:green;3:blue)。
|
||||
* 对于数值选项,也可以指定一个范围,用英文负号(-)分隔起始值、结束值和格式。
|
||||
* 如:"1-7-%02d"表示1到7,格式为『02d』,格式为可选,缺省为『%d』。
|
||||
* > 如果数据本身中有英文冒号(:)、英文分号(;)和英文负号(-)。请用16进制转义。
|
||||
* > * 英文冒号(:)在XML中写为\x3a,在C代码中写为\\x3a
|
||||
* > * 英文冒号(;)在XML中写为\x3b,在C代码中写为\\x3b
|
||||
* > * 英文冒号(-)在XML中写为\x2d,在C代码中写为\\x2d
|
||||
*/
|
||||
char* options;
|
||||
|
||||
|
@ -281,3 +281,47 @@ TEST(TextSelector, localize_options) {
|
||||
|
||||
widget_destroy(win);
|
||||
}
|
||||
|
||||
TEST(TextSelector, parse_options_special_chars) {
|
||||
widget_t* w = text_selector_create(NULL, 10, 20, 30, 40);
|
||||
|
||||
text_selector_parse_options(w, "1:r\\x3a\\x3b;2:gree\\x6e;3:\\x3ablue");
|
||||
ASSERT_EQ(text_selector_count_options(w), 3);
|
||||
|
||||
text_selector_set_selected_index(w, 0);
|
||||
ASSERT_EQ(text_selector_get_value(w), 1);
|
||||
ASSERT_EQ(string(text_selector_get_text(w)), string("r:;"));
|
||||
|
||||
text_selector_set_selected_index(w, 1);
|
||||
ASSERT_EQ(text_selector_get_value(w), 2);
|
||||
ASSERT_EQ(string(text_selector_get_text(w)), string("green"));
|
||||
|
||||
text_selector_set_selected_index(w, 2);
|
||||
ASSERT_EQ(text_selector_get_value(w), 3);
|
||||
ASSERT_EQ(string(text_selector_get_text(w)), string(":blue"));
|
||||
|
||||
text_selector_reset_options(w);
|
||||
ASSERT_EQ(text_selector_count_options(w), 0);
|
||||
|
||||
widget_destroy(w);
|
||||
}
|
||||
|
||||
TEST(TextSelector, parse_options_special_chars1) {
|
||||
widget_t* w = text_selector_create(NULL, 10, 20, 30, 40);
|
||||
|
||||
text_selector_set_options(w, "-12-14-GTM%+02d\\x3a00");
|
||||
ASSERT_EQ(text_selector_count_options(w), 27);
|
||||
|
||||
text_selector_set_selected_index(w, 0);
|
||||
ASSERT_EQ(text_selector_get_value(w), -12);
|
||||
ASSERT_STREQ(text_selector_get_text(w), "GTM-12:00");
|
||||
|
||||
text_selector_set_selected_index(w, 13);
|
||||
ASSERT_EQ(text_selector_get_value(w), 1);
|
||||
ASSERT_STREQ(text_selector_get_text(w), "GTM+1:00");
|
||||
|
||||
text_selector_reset_options(w);
|
||||
ASSERT_EQ(text_selector_count_options(w), 0);
|
||||
|
||||
widget_destroy(w);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user