edit support custom valid char function

This commit is contained in:
lixianjing 2019-07-11 15:08:37 +08:00
parent fd27cfc0a8
commit 1abcbb8986
5 changed files with 253 additions and 35 deletions

View File

@ -389,7 +389,7 @@
#include "assets/inc/images/visible.data"
#include "assets/inc/images/warn.data"
#include "assets/inc/images/zh.data"
#endif/*WITH_STB_IMAGE*/
#endif /*WITH_STB_IMAGE*/
#ifdef WITH_VGCANVAS
#include "assets/inc/images/ball.bsvg"
#include "assets/inc/images/china.bsvg"
@ -397,13 +397,13 @@
#include "assets/inc/images/pointer.bsvg"
#include "assets/inc/images/pointer_1.bsvg"
#include "assets/inc/images/pointer_4.bsvg"
#endif/*WITH_VGCANVAS*/
#endif /*WITH_VGCANVAS*/
#if defined(WITH_STB_FONT) || defined(WITH_FT_FONT)
#include "assets/inc/fonts/default.res"
#else/*WITH_STB_FONT or WITH_FT_FONT*/
#else /*WITH_STB_FONT or WITH_FT_FONT*/
#include "assets/inc/fonts/default.data"
#endif/*WITH_STB_FONT or WITH_FT_FONT*/
#endif/*WITH_FS_RES*/
#endif /*WITH_STB_FONT or WITH_FT_FONT*/
#endif /*WITH_FS_RES*/
ret_t assets_init(void) {
assets_manager_t* rm = assets_manager();
@ -685,7 +685,7 @@ ret_t assets_init(void) {
assets_manager_add(rm, image_pointer);
assets_manager_add(rm, image_pointer_1);
assets_manager_add(rm, image_pointer_4);
#endif/*WITH_VGCANVAS*/
#endif /*WITH_VGCANVAS*/
#endif
tk_init_assets();

View File

@ -2,7 +2,8 @@
* 2019/07/11
* tab button group 支持 scrollable 属性。
* edit 支持自定义的字符有效性检查函数。
* 2019/07/10
* 修改 wcs 几处编译警告(感谢陈谭提供补丁)。
* 增加生成控件模板的代码生成器。

View File

@ -72,14 +72,14 @@ static ret_t edit_do_input_char(widget_t* widget, wchar_t c) {
return text_edit_paste(EDIT(widget)->model, &c, 1);
}
static ret_t edit_input_char(widget_t* widget, wchar_t c) {
static bool_t edit_is_valid_char_default(widget_t* widget, wchar_t c) {
bool_t ret = FALSE;
wstr_t* text = NULL;
ret_t ret = RET_FAIL;
edit_t* edit = EDIT(widget);
input_type_t input_type = (input_type_t)0;
uint32_t cursor_pos = text_edit_get_cursor(edit->model);
return_value_if_fail(widget != NULL && edit != NULL, RET_BAD_PARAMS);
return_value_if_fail(widget != NULL && edit != NULL, FALSE);
text = &(widget->text);
input_type = edit->limit.type;
@ -88,15 +88,13 @@ static ret_t edit_input_char(widget_t* widget, wchar_t c) {
case INPUT_INT:
case INPUT_UINT: {
if (text->size >= TK_NUM_MAX_LEN) {
return RET_FAIL;
}
if (c >= '0' && c <= '9') {
ret = edit_do_input_char(widget, c);
break;
} else if (c >= '0' && c <= '9') {
ret = TRUE;
break;
} else if (c == '+' || (c == '-' && input_type == INPUT_INT)) {
if (cursor_pos == 0) {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
break;
}
@ -105,19 +103,18 @@ static ret_t edit_input_char(widget_t* widget, wchar_t c) {
case INPUT_FLOAT:
case INPUT_UFLOAT: {
if (text->size >= TK_NUM_MAX_LEN) {
return RET_FAIL;
}
if (c >= '0' && c <= '9') {
ret = edit_do_input_char(widget, c);
break;
} else if (c >= '0' && c <= '9') {
ret = TRUE;
break;
} else if (c == '+' || (c == '-' && input_type == INPUT_FLOAT)) {
if (cursor_pos == 0) {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
break;
} else if (c == '.' || c == 'e') {
if (cursor_pos > 0 && wcs_chr(text->str, c) == NULL) {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
}
break;
@ -125,10 +122,10 @@ static ret_t edit_input_char(widget_t* widget, wchar_t c) {
case INPUT_EMAIL: {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' ||
c == '.' || c == '_') {
ret = edit_do_input_char(widget, c);
ret = TRUE;
} else if (c == '@') {
if (cursor_pos > 0 && wcs_chr(text->str, c) == NULL) {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
}
break;
@ -136,35 +133,55 @@ static ret_t edit_input_char(widget_t* widget, wchar_t c) {
case INPUT_HEX: {
if (text->size > 10) {
break;
}
if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
ret = edit_do_input_char(widget, c);
} else if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
ret = TRUE;
} else if (c == 'X' || c == 'x') {
if (cursor_pos == 1 && text->str[0] == '0') {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
}
break;
}
case INPUT_PHONE: {
if (c >= '0' && c <= '9') {
ret = edit_do_input_char(widget, c);
ret = TRUE;
break;
} else if (c == '-') {
if (cursor_pos > 0 && wcs_chr(text->str, c) == NULL) {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
}
break;
}
default: {
if (widget->text.size < edit->limit.u.t.max) {
ret = edit_do_input_char(widget, c);
ret = TRUE;
}
}
}
edit_dispatch_event(widget, EVT_VALUE_CHANGING);
return ret;
}
bool_t edit_is_valid_char(widget_t* widget, wchar_t c) {
edit_t* edit = EDIT(widget);
return_value_if_fail(edit != NULL, FALSE);
if (edit->is_valid_char != NULL) {
return edit->is_valid_char(widget, c);
} else {
return edit_is_valid_char_default(widget, c);
}
}
ret_t edit_input_char(widget_t* widget, wchar_t c) {
ret_t ret = RET_BAD_PARAMS;
return_value_if_fail(widget != NULL, RET_BAD_PARAMS);
if (edit_is_valid_char(widget, c)) {
ret = edit_do_input_char(widget, c);
edit_dispatch_event(widget, EVT_VALUE_CHANGING);
}
return ret;
}
@ -563,7 +580,9 @@ ret_t edit_set_int_limit(widget_t* widget, int32_t min, int32_t max, uint32_t st
edit->limit.u.i.min = min;
edit->limit.u.i.max = max;
edit->limit.u.i.step = step;
edit->limit.type = INPUT_INT;
if (edit->limit.type != INPUT_UINT) {
edit->limit.type = INPUT_INT;
}
return RET_OK;
}
@ -575,7 +594,10 @@ ret_t edit_set_float_limit(widget_t* widget, double min, double max, double step
edit->limit.u.f.min = min;
edit->limit.u.f.max = max;
edit->limit.u.f.step = step;
edit->limit.type = INPUT_FLOAT;
if (edit->limit.type != INPUT_UFLOAT) {
edit->limit.type = INPUT_FLOAT;
}
return RET_OK;
}
@ -1135,3 +1157,12 @@ widget_t* edit_cast(widget_t* widget) {
return widget;
}
ret_t edit_set_is_valid_char(widget_t* widget, edit_is_valid_char_t is_valid_char) {
edit_t* edit = EDIT(widget);
return_value_if_fail(edit != NULL, RET_BAD_PARAMS);
edit->is_valid_char = is_valid_char;
return RET_OK;
}

View File

@ -49,6 +49,8 @@ typedef struct _input_limit_t {
} u;
} input_limit_t;
typedef bool_t (*edit_is_valid_char_t)(widget_t* widget, wchar_t c);
/**
* @class edit_t
* @parent widget_t
@ -200,6 +202,7 @@ typedef struct _edit_t {
uint32_t timer_id;
input_limit_t limit;
text_edit_t* model;
edit_is_valid_char_t is_valid_char;
} edit_t;
/**
@ -376,12 +379,24 @@ ret_t edit_set_password_visible(widget_t* widget, bool_t password_visible);
*
* @annotation ["scriptable"]
* @param {widget_t*} widget widget对象
* @param {bool_t} focus
* @param {bool_t} focus
*
* @return {ret_t} RET_OK表示成功
*/
ret_t edit_set_focus(widget_t* widget, bool_t focus);
/**
* @method edit_set_is_valid_char
*
*>
*
* @param {widget_t*} widget widget对象
* @param {edit_is_valid_char_t} is_valid_char
*
* @return {ret_t} RET_OK表示成功
*/
ret_t edit_set_is_valid_char(widget_t* widget, edit_is_valid_char_t is_valid_char);
#define EDIT(widget) ((edit_t*)(edit_cast(WIDGET(widget))))
/*public for subclass and runtime type check*/
@ -401,6 +416,8 @@ ret_t edit_inc(edit_t* edit);
ret_t edit_dec(edit_t* edit);
ret_t edit_clear(edit_t* edit);
bool_t edit_is_valid_value(widget_t* widget);
ret_t edit_input_char(widget_t* widget, wchar_t c);
bool_t edit_is_valid_char(widget_t* widget, wchar_t c);
END_C_DECLS

View File

@ -233,3 +233,172 @@ TEST(Edit, focus_prev) {
widget_destroy(w);
}
TEST(Edit, is_valid_chr_4_str) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_text_limit(e, 0, 3);
ASSERT_EQ(edit_input_char(e, L'a'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'b'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'c'), RET_OK);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_EQ(e->text.size, 3);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_int) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_INT);
edit_set_int_limit(e, -1000, 1000, 10);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'-'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_EQ(e->text.size, 5);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_uint) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_UINT);
edit_set_int_limit(e, 0, 1000, 10);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_NE(edit_input_char(e, L'-'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_EQ(e->text.size, 4);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_float) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_FLOAT);
edit_set_float_limit(e, -1000, 1000, 10);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'-'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'.'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_NE(edit_input_char(e, L'.'), RET_OK);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_ufloat) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_UFLOAT);
edit_set_float_limit(e, 0, 1000, 10);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
ASSERT_NE(edit_input_char(e, L'-'), RET_OK);
ASSERT_NE(edit_input_char(e, L'+'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'.'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_NE(edit_input_char(e, L'.'), RET_OK);
ASSERT_NE(edit_input_char(e, L'd'), RET_OK);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_email) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_EMAIL);
ASSERT_NE(edit_input_char(e, L'a'), RET_OK);
ASSERT_NE(edit_input_char(e, L'a'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'a'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'@'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_NE(edit_input_char(e, L'@'), RET_OK);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_hex) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_HEX);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'x'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'2'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'3'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'4'), RET_OK);
ASSERT_NE(edit_input_char(e, L'x'), RET_OK);
widget_destroy(e);
}
TEST(Edit, is_valid_chr_4_phone) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_PHONE);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'2'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'3'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'4'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'-'), RET_OK);
ASSERT_NE(edit_input_char(e, L'x'), RET_OK);
ASSERT_NE(edit_input_char(e, L'a'), RET_OK);
widget_destroy(e);
}
static bool_t any_char_is_valid(widget_t* widget, wchar_t c) {
return TRUE;
}
TEST(Edit, is_valid_chr_4_custom) {
widget_t* e = edit_create(NULL, 10, 20, 30, 40);
edit_set_input_type(e, INPUT_PHONE);
edit_set_is_valid_char(e, any_char_is_valid);
ASSERT_EQ(edit_input_char(e, L'0'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'1'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'2'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'3'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'4'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'-'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'x'), RET_OK);
ASSERT_EQ(edit_input_char(e, L'a'), RET_OK);
widget_destroy(e);
}