edit/mledit support tips translation

This commit is contained in:
xianjimli 2020-04-27 08:36:41 +08:00
parent 6d08f5a2b5
commit 37d1228cc1
9 changed files with 191 additions and 14 deletions

View File

@ -430,7 +430,9 @@ ret_t widget_set_tr_text(widget_t* widget, const char* text) {
}
ret_t widget_re_translate_text(widget_t* widget) {
if (widget->tr_text != NULL) {
if (widget->vt->on_re_translate != NULL) {
widget->vt->on_re_translate(widget);
} else if (widget->tr_text != NULL) {
value_t v;
const char* tr_text = locale_info_tr(widget_get_locale_info(widget), widget->tr_text);
char* tmp = widget->tr_text;

View File

@ -62,6 +62,7 @@ typedef ret_t (*widget_on_paint_end_t)(widget_t* widget, canvas_t* c);
typedef ret_t (*widget_on_wheel_t)(widget_t* widget, wheel_event_t* e);
typedef ret_t (*widget_on_keydown_t)(widget_t* widget, key_event_t* e);
typedef ret_t (*widget_on_keyup_t)(widget_t* widget, key_event_t* e);
typedef ret_t (*widget_on_re_translate_t)(widget_t* widget);
typedef ret_t (*widget_on_pointer_down_t)(widget_t* widget, pointer_event_t* e);
typedef ret_t (*widget_on_pointer_move_t)(widget_t* widget, pointer_event_t* e);
typedef ret_t (*widget_on_pointer_up_t)(widget_t* widget, pointer_event_t* e);
@ -160,6 +161,7 @@ struct _widget_vtable_t {
widget_on_keyup_t on_keyup;
widget_on_keydown_t on_keydown;
widget_on_wheel_t on_wheel;
widget_on_re_translate_t on_re_translate;
widget_on_paint_background_t on_paint_background;
widget_on_paint_self_t on_paint_self;
widget_on_paint_children_t on_paint_children;

View File

@ -347,6 +347,12 @@ BEGIN_C_DECLS
*/
#define WIDGET_PROP_TIPS "tips"
/**
* @const WIDGET_PROP_TR_TIPS
*
*/
#define WIDGET_PROP_TR_TIPS "tr_tips"
/**
* @const WIDGET_PROP_INPUT_TYPE
*

View File

@ -37,7 +37,7 @@ static ret_t mledit_dispatch_event(widget_t* widget, event_type_t type) {
return RET_OK;
}
ret_t mledit_set_input_tips(widget_t* widget, const char* tips) {
ret_t mledit_set_tips(widget_t* widget, const char* tips) {
mledit_t* mledit = MLEDIT(widget);
return_value_if_fail(mledit != NULL && tips != NULL, RET_BAD_PARAMS);
@ -47,6 +47,34 @@ ret_t mledit_set_input_tips(widget_t* widget, const char* tips) {
return RET_OK;
}
static ret_t mledit_apply_tr_text_before_paint(void* ctx, event_t* e) {
widget_t* widget = WIDGET(ctx);
mledit_t* mledit = MLEDIT(widget);
if (mledit->tr_tips != NULL) {
const char* tr_tips = locale_info_tr(widget_get_locale_info(widget), mledit->tr_tips);
mledit_set_tips(widget, tr_tips);
}
return RET_REMOVE;
}
ret_t mledit_set_tr_tips(widget_t* widget, const char* tr_tips) {
mledit_t* mledit = MLEDIT(widget);
widget_t* win = widget_get_window(widget);
return_value_if_fail(mledit != NULL && tr_tips != NULL, RET_BAD_PARAMS);
mledit->tr_tips = tk_str_copy(mledit->tr_tips, tr_tips);
if (win != NULL) {
tr_tips = locale_info_tr(widget_get_locale_info(widget), tr_tips);
mledit_set_tips(widget, tr_tips);
} else {
widget_on(widget, EVT_BEFORE_PAINT, mledit_apply_tr_text_before_paint, widget);
}
return RET_OK;
}
ret_t mledit_set_keyboard(widget_t* widget, const char* keyboard) {
mledit_t* mledit = MLEDIT(widget);
return_value_if_fail(mledit != NULL && keyboard != NULL, RET_BAD_PARAMS);
@ -126,6 +154,9 @@ static ret_t mledit_get_prop(widget_t* widget, const char* name, value_t* v) {
} else if (tk_str_eq(name, WIDGET_PROP_TIPS)) {
value_set_str(v, mledit->tips);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_TR_TIPS)) {
value_set_str(v, mledit->tr_tips);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_KEYBOARD)) {
value_set_str(v, mledit->keyboard);
return RET_OK;
@ -219,7 +250,10 @@ static ret_t mledit_set_prop(widget_t* widget, const char* name, const value_t*
mledit_set_focus(widget, value_bool(v));
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_TIPS)) {
mledit_set_input_tips(widget, value_str(v));
mledit_set_tips(widget, value_str(v));
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_TR_TIPS)) {
mledit_set_tr_tips(widget, value_str(v));
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_KEYBOARD)) {
mledit_set_keyboard(widget, value_str(v));
@ -244,6 +278,7 @@ static ret_t mledit_on_destroy(widget_t* widget) {
wstr_reset(&(mledit->temp));
TKMEM_FREE(mledit->tips);
TKMEM_FREE(mledit->tr_tips);
TKMEM_FREE(mledit->keyboard);
text_edit_destroy(mledit->model);
@ -550,6 +585,16 @@ static ret_t mledit_on_event(widget_t* widget, event_t* e) {
return ret;
}
static ret_t mledit_on_re_translate(widget_t* widget) {
mledit_t* mledit = MLEDIT(widget);
if (mledit->tr_tips != NULL) {
const char* tr_tips = locale_info_tr(widget_get_locale_info(widget), mledit->tr_tips);
mledit_set_tips(widget, tr_tips);
}
return RET_OK;
}
static ret_t mledit_sync_line_number(widget_t* widget, text_edit_state_t* state) {
mledit_t* mledit = MLEDIT(widget);
widget_t* line_number = widget_lookup_by_type(widget, WIDGET_TYPE_LINE_NUMBER, TRUE);
@ -642,11 +687,19 @@ static ret_t mledit_init_idle_func(const idle_info_t* info) {
return RET_REMOVE;
}
const char* s_mledit_properties[] = {
WIDGET_PROP_READONLY, WIDGET_PROP_MARGIN, WIDGET_PROP_LEFT_MARGIN,
WIDGET_PROP_RIGHT_MARGIN, WIDGET_PROP_TOP_MARGIN, WIDGET_PROP_BOTTOM_MARGIN,
WIDGET_PROP_TIPS, WIDGET_PROP_KEYBOARD, MLEDIT_PROP_MAX_LINES,
MLEDIT_PROP_WRAP_WORD, MLEDIT_PROP_SCROLL_LINE, NULL};
const char* s_mledit_properties[] = {WIDGET_PROP_READONLY,
WIDGET_PROP_MARGIN,
WIDGET_PROP_LEFT_MARGIN,
WIDGET_PROP_RIGHT_MARGIN,
WIDGET_PROP_TOP_MARGIN,
WIDGET_PROP_BOTTOM_MARGIN,
WIDGET_PROP_TIPS,
WIDGET_PROP_TR_TIPS,
WIDGET_PROP_KEYBOARD,
MLEDIT_PROP_MAX_LINES,
MLEDIT_PROP_WRAP_WORD,
MLEDIT_PROP_SCROLL_LINE,
NULL};
TK_DECL_VTABLE(mledit) = {.size = sizeof(mledit_t),
.type = WIDGET_TYPE_MLEDIT,
@ -657,6 +710,7 @@ TK_DECL_VTABLE(mledit) = {.size = sizeof(mledit_t),
.parent = TK_PARENT_VTABLE(widget),
.create = mledit_create,
.on_paint_self = mledit_on_paint_self,
.on_re_translate = mledit_on_re_translate,
.set_prop = mledit_set_prop,
.get_prop = mledit_get_prop,
.on_event = mledit_on_event,

View File

@ -95,6 +95,12 @@ typedef struct _mledit_t {
*
*/
char* tips;
/**
* @property {char*} tr_tips
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
*
*/
char* tr_tips;
/**
* @property {char*} keyboard
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
@ -199,7 +205,7 @@ ret_t mledit_set_wrap_word(widget_t* widget, bool_t wrap_word);
ret_t mledit_set_max_lines(widget_t* widget, uint32_t max_lines);
/**
* @method mledit_set_input_tips
* @method mledit_set_tips
*
* @annotation ["scriptable"]
* @param {widget_t*} widget widget对象
@ -207,7 +213,18 @@ ret_t mledit_set_max_lines(widget_t* widget, uint32_t max_lines);
*
* @return {ret_t} RET_OK表示成功
*/
ret_t mledit_set_input_tips(widget_t* widget, const char* tips);
ret_t mledit_set_tips(widget_t* widget, const char* tips);
/**
* @method mledit_set_tr_tips
* mledit_set_tips
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {const char*} tr_tips
*
* @return {ret_t} RET_OK表示成功
*/
ret_t mledit_set_tr_tips(widget_t* widget, const char* tr_tips);
/**
* @method mledit_set_keyboard
@ -260,6 +277,9 @@ widget_t* mledit_cast(widget_t* widget);
#define MLEDIT_PROP_SCROLL_LINE "scroll_line"
#define MLEDIT(widget) ((mledit_t*)(mledit_cast(WIDGET(widget))))
/*for compatability*/
#define mledit_set_input_tips(w, t) mledit_set_tips(w, t)
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(mledit);

View File

@ -662,6 +662,16 @@ ret_t edit_on_event(widget_t* widget, event_t* e) {
return ret;
}
static ret_t edit_on_re_translate(widget_t* widget) {
edit_t* edit = EDIT(widget);
if (edit->tr_tips != NULL) {
const char* tr_tips = locale_info_tr(widget_get_locale_info(widget), edit->tr_tips);
edit_set_tips(widget, tr_tips);
}
return RET_OK;
}
ret_t edit_set_text_limit(widget_t* widget, uint32_t min, uint32_t max) {
edit_t* edit = EDIT(widget);
return_value_if_fail(edit != NULL, RET_BAD_PARAMS);
@ -752,7 +762,7 @@ ret_t edit_set_input_type(widget_t* widget, input_type_t type) {
return RET_OK;
}
ret_t edit_set_input_tips(widget_t* widget, const char* tips) {
ret_t edit_set_tips(widget_t* widget, const char* tips) {
edit_t* edit = EDIT(widget);
return_value_if_fail(edit != NULL && tips != NULL, RET_BAD_PARAMS);
@ -762,6 +772,34 @@ ret_t edit_set_input_tips(widget_t* widget, const char* tips) {
return RET_OK;
}
static ret_t edit_apply_tr_text_before_paint(void* ctx, event_t* e) {
widget_t* widget = WIDGET(ctx);
edit_t* edit = EDIT(widget);
if (edit->tr_tips != NULL) {
const char* tr_tips = locale_info_tr(widget_get_locale_info(widget), edit->tr_tips);
edit_set_tips(widget, tr_tips);
}
return RET_REMOVE;
}
ret_t edit_set_tr_tips(widget_t* widget, const char* tr_tips) {
edit_t* edit = EDIT(widget);
widget_t* win = widget_get_window(widget);
return_value_if_fail(edit != NULL && tr_tips != NULL, RET_BAD_PARAMS);
edit->tr_tips = tk_str_copy(edit->tr_tips, tr_tips);
if (win != NULL) {
tr_tips = locale_info_tr(widget_get_locale_info(widget), tr_tips);
edit_set_tips(widget, tr_tips);
} else {
widget_on(widget, EVT_BEFORE_PAINT, edit_apply_tr_text_before_paint, widget);
}
return RET_OK;
}
ret_t edit_set_keyboard(widget_t* widget, const char* keyboard) {
edit_t* edit = EDIT(widget);
return_value_if_fail(edit != NULL && keyboard != NULL, RET_BAD_PARAMS);
@ -842,6 +880,9 @@ ret_t edit_get_prop(widget_t* widget, const char* name, value_t* v) {
} else if (tk_str_eq(name, WIDGET_PROP_TIPS)) {
value_set_str(v, edit->tips);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_TR_TIPS)) {
value_set_str(v, edit->tr_tips);
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_KEYBOARD)) {
value_set_str(v, edit->keyboard);
return RET_OK;
@ -975,7 +1016,10 @@ ret_t edit_set_prop(widget_t* widget, const char* name, const value_t* v) {
edit_set_focus(widget, value_bool(v));
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_TIPS)) {
edit_set_input_tips(widget, value_str(v));
edit_set_tips(widget, value_str(v));
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_TR_TIPS)) {
edit_set_tr_tips(widget, value_str(v));
return RET_OK;
} else if (tk_str_eq(name, WIDGET_PROP_KEYBOARD)) {
edit_set_keyboard(widget, value_str(v));
@ -1259,6 +1303,7 @@ ret_t edit_on_destroy(widget_t* widget) {
}
TKMEM_FREE(edit->tips);
TKMEM_FREE(edit->tr_tips);
TKMEM_FREE(edit->keyboard);
text_edit_destroy(edit->model);
@ -1288,6 +1333,7 @@ const char* const s_edit_properties[] = {WIDGET_PROP_MIN,
WIDGET_PROP_TOP_MARGIN,
WIDGET_PROP_BOTTOM_MARGIN,
WIDGET_PROP_TIPS,
WIDGET_PROP_TR_TIPS,
WIDGET_PROP_KEYBOARD,
WIDGET_PROP_PASSWORD_VISIBLE,
NULL};
@ -1297,6 +1343,7 @@ ret_t edit_on_copy(widget_t* widget, widget_t* other) {
edit_t* edit_other = EDIT(other);
edit->tips = tk_str_copy(edit->tips, edit_other->tips);
edit->tr_tips = tk_str_copy(edit->tr_tips, edit_other->tr_tips);
edit->min = edit_other->min;
edit->max = edit_other->max;
@ -1321,6 +1368,7 @@ TK_DECL_VTABLE(edit) = {.size = sizeof(edit_t),
.persistent_properties = s_edit_properties,
.parent = TK_PARENT_VTABLE(widget),
.create = edit_create,
.on_re_translate = edit_on_re_translate,
.on_paint_self = edit_on_paint_self,
.set_prop = edit_set_prop,
.get_prop = edit_get_prop,

View File

@ -160,6 +160,13 @@ typedef struct _edit_t {
*/
char* tips;
/**
* @property {char*} tr_tips
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
*
*/
char* tr_tips;
/**
* @property {char*} keyboard
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
@ -373,7 +380,7 @@ ret_t edit_set_open_im_when_focused(widget_t* widget, bool_t open_im_when_focuse
ret_t edit_set_input_type(widget_t* widget, input_type_t type);
/**
* @method edit_set_input_tips
* @method edit_set_tips
*
* @annotation ["scriptable"]
* @param {widget_t*} widget widget对象
@ -381,7 +388,18 @@ ret_t edit_set_input_type(widget_t* widget, input_type_t type);
*
* @return {ret_t} RET_OK表示成功
*/
ret_t edit_set_input_tips(widget_t* widget, const char* tips);
ret_t edit_set_tips(widget_t* widget, const char* tips);
/**
* @method edit_set_tr_tips
* edit_set_tips
* @annotation ["scriptable"]
* @param {widget_t*} widget
* @param {const char*} tr_tips
*
* @return {ret_t} RET_OK表示成功
*/
ret_t edit_set_tr_tips(widget_t* widget, const char* tr_tips);
/**
* @method edit_set_keyboard
@ -463,6 +481,9 @@ 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);
/*for compatability*/
#define edit_set_input_tips(w, t) edit_set_tips(w, t)
END_C_DECLS
#endif /*TK_EDIT_H*/

View File

@ -45,6 +45,18 @@ TEST(Edit, int) {
widget_destroy(b);
}
TEST(Edit, tips) {
widget_t* b = edit_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(widget_set_prop_str(b, WIDGET_PROP_TIPS, "tips"), RET_OK);
ASSERT_STREQ(EDIT(b)->tips, "tips");
ASSERT_EQ(widget_set_prop_str(b, WIDGET_PROP_TR_TIPS, "tr_tips"), RET_OK);
ASSERT_STREQ(EDIT(b)->tr_tips, "tr_tips");
widget_destroy(b);
}
TEST(Edit, set_text) {
text_edit_state_t state;
widget_t* b = edit_create(NULL, 10, 20, 30, 40);

View File

@ -36,6 +36,18 @@ TEST(MLEdit, int) {
widget_destroy(b);
}
TEST(MLEdit, tips) {
widget_t* b = mledit_create(NULL, 10, 20, 30, 40);
ASSERT_EQ(widget_set_prop_str(b, WIDGET_PROP_TIPS, "tips"), RET_OK);
ASSERT_STREQ(MLEDIT(b)->tips, "tips");
ASSERT_EQ(widget_set_prop_str(b, WIDGET_PROP_TR_TIPS, "tr_tips"), RET_OK);
ASSERT_STREQ(MLEDIT(b)->tr_tips, "tr_tips");
widget_destroy(b);
}
TEST(MLEdit, events) {
string event_log;
widget_t* b = mledit_create(NULL, 10, 20, 30, 40);