improve label

This commit is contained in:
lixianjing 2023-09-14 18:26:13 +08:00
parent e4195d291b
commit 6b31eb8f7a
5 changed files with 56 additions and 10 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2023/09/14
* 修复label自动换行并且置顶时文本会往下移动几个像素的问题(感谢颖健提供补丁)
2023/09/13
* 增加[如何将资源编译到应用程序并使用它们](how_to_compile_res_into_app_and_use_them.md) (感谢雨欣提供补丁)
* 修复当BUILD_DIR指定的路径在SConstruct文件所在目录之外的目录时无法正常编译的问题(感谢培煌提供补丁)

View File

@ -1581,16 +1581,9 @@ ret_t widget_draw_icon_text(widget_t* widget, canvas_t* c, const char* icon, wst
return_value_if_fail(widget->astyle != NULL, RET_BAD_PARAMS);
spacer = style_get_int(style, STYLE_ID_SPACER, 2);
margin = style_get_int(style, STYLE_ID_MARGIN, 0);
margin_top = style_get_int(style, STYLE_ID_MARGIN_TOP, margin);
margin_left = style_get_int(style, STYLE_ID_MARGIN_LEFT, margin);
margin_right = style_get_int(style, STYLE_ID_MARGIN_RIGHT, margin);
margin_bottom = style_get_int(style, STYLE_ID_MARGIN_BOTTOM, margin);
icon_at = style_get_int(style, STYLE_ID_ICON_AT, ICON_AT_AUTO);
w = widget->w - margin_left - margin_right;
h = widget->h - margin_top - margin_bottom;
ir = rect_init(margin_left, margin_top, w, h);
ir = widget_get_content_area_ex(widget, 0);
ellipses = widget_get_prop_bool(widget, WIDGET_PROP_ELLIPSES, FALSE);
@ -5153,9 +5146,13 @@ bool_t widget_get_feedback(widget_t* widget) {
}
rect_t widget_get_content_area(widget_t* widget) {
return widget_get_content_area_ex(widget, 2);
}
rect_t widget_get_content_area_ex(widget_t* widget, int32_t default_margin) {
if (widget != NULL && widget->astyle != NULL) {
style_t* style = widget->astyle;
int32_t margin = style_get_int(style, STYLE_ID_MARGIN, 2);
int32_t margin = style_get_int(style, STYLE_ID_MARGIN, default_margin);
int32_t margin_top = style_get_int(style, STYLE_ID_MARGIN_TOP, margin);
int32_t margin_left = style_get_int(style, STYLE_ID_MARGIN_LEFT, margin);
int32_t margin_right = style_get_int(style, STYLE_ID_MARGIN_RIGHT, margin);

View File

@ -3386,6 +3386,9 @@ ret_t widget_reload_style_recursive(widget_t* widget);
ret_t widget_stroke_border_rect_for_border_type(canvas_t* c, const rect_t* r, color_t bd,
int32_t border, uint32_t border_width);
/* internal use */
rect_t widget_get_content_area_ex(widget_t* widget, int32_t default_margin);
END_C_DECLS
#endif /*TK_WIDGET_H*/

View File

@ -80,7 +80,7 @@ static ret_t label_paint_text(widget_t* widget, canvas_t* c, const wchar_t* str,
line_parser_t p;
ret_t ret = RET_OK;
label_t* label = LABEL(widget);
rect_t r = widget_get_content_area(widget);
rect_t r = widget_get_content_area_ex(widget, 0);
return_value_if_fail((r.w > 0 && widget->h >= c->font_size), RET_FAIL);
return_value_if_fail(line_parser_init(&p, c, widget->text.str, widget->text.size, c->font_size,

View File

@ -1680,3 +1680,46 @@ TEST(Widget, widget_to_screen_and_loacl) {
widget_destroy(w);
}
TEST(Widget, get_content_area_ex) {
rect_t rect0;
rect_t rect1;
rect_t rect30;
widget_t* w = window_create(NULL, 0, 0, 400, 300);
widget_t* label = label_create(w, 0, 0, 60, 60);
if (label != NULL && label->astyle != NULL) {
rect0 = rect_init(0, 0, label->w, label->h);
rect1 = rect_init(1, 1, label->w - 1 - 1, label->h - 1 - 1);
rect30 = rect_init(30, 30, label->w - 30 - 30, label->h - 30 - 30);
} else if (label != NULL) {
rect0 = rect_init(0, 0, label->w, label->h);
rect1 = rect_init(0, 0, label->w, label->h);
rect30 = rect_init(0, 0, label->w, label->h);
} else {
rect0 = rect_init(0, 0, 0, 0);
rect1 = rect_init(0, 0, 0, 0);
rect30 = rect_init(0, 0, 0, 0);
}
rect_t label_rect0 = widget_get_content_area_ex(label, 0);
rect_t label_rect1 = widget_get_content_area_ex(label, 1);
rect_t label_rect30 = widget_get_content_area_ex(label, 30);
ASSERT_EQ(label_rect0.x, rect0.x);
ASSERT_EQ(label_rect0.y, rect0.y);
ASSERT_EQ(label_rect0.w, rect0.w);
ASSERT_EQ(label_rect0.h, rect0.h);
ASSERT_EQ(label_rect1.x, rect1.x);
ASSERT_EQ(label_rect1.y, rect1.y);
ASSERT_EQ(label_rect1.w, rect1.w);
ASSERT_EQ(label_rect1.h, rect1.h);
ASSERT_EQ(label_rect30.x, rect30.x);
ASSERT_EQ(label_rect30.y, rect30.y);
ASSERT_EQ(label_rect30.w, rect30.w);
ASSERT_EQ(label_rect30.h, rect30.h);
widget_destroy(label);
}