diff --git a/docs/src/widgets/msgbox.rst b/docs/src/widgets/msgbox.rst index a2d7187bb9..4a6658eaa7 100644 --- a/docs/src/widgets/msgbox.rst +++ b/docs/src/widgets/msgbox.rst @@ -84,6 +84,7 @@ Functions that add something to the message box return a pointer to the newly ad .. code:: c lv_obj_t * lv_msgbox_add_text(lv_obj_t * msgbox, const char * text); + lv_obj_t * lv_msgbox_add_text_fmt(lv_obj_t * obj, const char * fmt, ...); lv_obj_t * lv_msgbox_add_title(lv_obj_t * msgbox, const char * title); lv_obj_t * lv_msgbox_add_close_button(lv_obj_t * msgbox); lv_obj_t * lv_msgbox_add_header_button(lv_obj_t * msgbox, const void * symbol); diff --git a/src/widgets/msgbox/lv_msgbox.c b/src/widgets/msgbox/lv_msgbox.c index 1713fb9a54..0f72238f4e 100644 --- a/src/widgets/msgbox/lv_msgbox.c +++ b/src/widgets/msgbox/lv_msgbox.c @@ -14,6 +14,7 @@ #include "../label/lv_label.h" #include "../image/lv_image.h" #include "../../misc/lv_assert.h" +#include "../../misc/lv_text_private.h" #include "../../display/lv_display.h" #include "../../layouts/flex/lv_flex.h" #include "../../stdlib/lv_string.h" @@ -202,6 +203,20 @@ lv_obj_t * lv_msgbox_add_text(lv_obj_t * obj, const char * text) return label; } +lv_obj_t * lv_msgbox_add_text_fmt(lv_obj_t * obj, const char * fmt, ...) +{ + lv_msgbox_t * mbox = (lv_msgbox_t *)obj; + + va_list args; + va_start(args, fmt); + lv_obj_t * label = lv_label_create(mbox->content); + lv_label_set_text_vfmt(label, fmt, args); + lv_obj_set_width(label, lv_pct(100)); + va_end(args); + + return label; +} + lv_obj_t * lv_msgbox_add_footer_button(lv_obj_t * obj, const char * text) { lv_msgbox_t * mbox = (lv_msgbox_t *)obj; diff --git a/src/widgets/msgbox/lv_msgbox.h b/src/widgets/msgbox/lv_msgbox.h index faf3c63c74..f35a2a9f3e 100644 --- a/src/widgets/msgbox/lv_msgbox.h +++ b/src/widgets/msgbox/lv_msgbox.h @@ -64,6 +64,14 @@ lv_obj_t * lv_msgbox_add_header_button(lv_obj_t * obj, const void * icon); */ lv_obj_t * lv_msgbox_add_text(lv_obj_t * obj, const char * text); +/** + * Add a formatted text to the content area of message box. Multiple texts will be created below each other. + * @param obj pointer to a message box + * @param fmt `printf`-like format string + * @return the created label + */ +lv_obj_t * lv_msgbox_add_text_fmt(lv_obj_t * obj, const char * fmt, ...) LV_FORMAT_ATTRIBUTE(2, 3); + /** * Add a button to the footer of to the message box. It also creates a footer. * @param obj pointer to a message box diff --git a/tests/src/test_cases/widgets/test_msgbox.c b/tests/src/test_cases/widgets/test_msgbox.c index 68c943cc22..6ebc89b7c1 100644 --- a/tests/src/test_cases/widgets/test_msgbox.c +++ b/tests/src/test_cases/widgets/test_msgbox.c @@ -68,6 +68,7 @@ void test_msgbox_creation_successful_modal(void) msgbox = lv_msgbox_create(NULL); lv_msgbox_add_title(msgbox, "The title"); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); lv_msgbox_add_footer_button(msgbox, "Apply"); lv_msgbox_add_footer_button(msgbox, "Close"); lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO); @@ -87,6 +88,7 @@ void test_msgbox_get_title(void) msgbox = lv_msgbox_create(active_screen); lv_msgbox_add_title(msgbox, "The title"); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); lv_msgbox_add_footer_button(msgbox, "Apply"); lv_msgbox_add_footer_button(msgbox, "Close"); lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO); @@ -109,6 +111,7 @@ void test_msgbox_close(void) { msgbox = lv_msgbox_create(active_screen); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); lv_msgbox_close(msgbox); @@ -120,6 +123,7 @@ void test_msgbox_close_modal(void) { msgbox = lv_msgbox_create(NULL); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); lv_msgbox_close(msgbox); @@ -131,6 +135,7 @@ void test_msgbox_close_async(void) { msgbox = lv_msgbox_create(active_screen); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); // lv_msgbox_close deletes the message box TEST_ASSERT_NOT_NULL(msgbox); @@ -140,6 +145,7 @@ void test_msgbox_close_async_modal(void) { msgbox = lv_msgbox_create(NULL); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); // lv_msgbox_close deletes the message box TEST_ASSERT_NOT_NULL(msgbox); @@ -151,6 +157,7 @@ void test_msgbox_content_auto_height(void) msgbox = lv_msgbox_create(NULL); lv_msgbox_add_title(msgbox, "The title"); lv_msgbox_add_text(msgbox, "The text"); + lv_msgbox_add_text_fmt(msgbox, "The %s text", "fmt"); lv_msgbox_add_footer_button(msgbox, "Apply"); lv_msgbox_add_footer_button(msgbox, "Close"); lv_msgbox_add_header_button(msgbox, LV_SYMBOL_AUDIO);