From da39d692dd4be1b767d3a1f761e23ddd4bb22d3f Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Mon, 26 Apr 2021 12:15:55 +0200 Subject: [PATCH] feat(msgbox) add parent attribute to lv_msgbox_create() It keeps the consistency of create functions and allows craeting non-modal message boxes --- examples/widgets/msgbox/lv_example_msgbox_1.c | 4 +-- src/extra/widgets/msgbox/lv_msgbox.c | 26 ++++++++++--------- src/extra/widgets/msgbox/lv_msgbox.h | 10 ++++--- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/examples/widgets/msgbox/lv_example_msgbox_1.c b/examples/widgets/msgbox/lv_example_msgbox_1.c index d6a84246e..e3674a6e8 100644 --- a/examples/widgets/msgbox/lv_example_msgbox_1.c +++ b/examples/widgets/msgbox/lv_example_msgbox_1.c @@ -3,7 +3,7 @@ static void event_cb(lv_event_t * e) { - lv_obj_t * obj = lv_event_get_target(e); + lv_obj_t * obj = lv_event_get_current_target(e); LV_LOG_USER("Button %s clicked", lv_msgbox_get_active_btn_text(obj)); } @@ -11,7 +11,7 @@ void lv_example_msgbox_1(void) { static const char * btns[] ={"Apply", "Close", ""}; - lv_obj_t * mbox1 = lv_msgbox_create("Hello", "This is a message box with two buttons.", btns, true); + lv_obj_t * mbox1 = lv_msgbox_create(NULL, "Hello", "This is a message box with two buttons.", btns, true); lv_obj_add_event_cb(mbox1, event_cb, LV_EVENT_VALUE_CHANGED, NULL); lv_obj_center(mbox1); } diff --git a/src/extra/widgets/msgbox/lv_msgbox.c b/src/extra/widgets/msgbox/lv_msgbox.c index faa253b26..357eeb8fa 100644 --- a/src/extra/widgets/msgbox/lv_msgbox.c +++ b/src/extra/widgets/msgbox/lv_msgbox.c @@ -13,6 +13,7 @@ /********************* * DEFINES *********************/ +#define LV_MSGBOX_FLAG_AUTO_PARENT LV_OBJ_FLAG_WIDGET_1 /*Mark that the parent was automatically created*/ /********************** * TYPEDEFS @@ -36,23 +37,23 @@ const lv_obj_class_t lv_msgbox_class = {.base_class = &lv_obj_class}; * GLOBAL FUNCTIONS **********************/ -/** - * Create a message box objects - * @param par pointer to an object, it will be the parent of the new message box - * @return pointer to the created message box - */ -lv_obj_t * lv_msgbox_create(const char * title, const char * txt, const char * btn_txts[], bool add_close_btn) +lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char * txt, const char * btn_txts[], bool add_close_btn) { - lv_obj_t * parent = lv_obj_create(lv_scr_act()); - lv_obj_remove_style_all(parent); - lv_obj_set_style_bg_color(parent, lv_palette_main(LV_PALETTE_GREY), 0); - lv_obj_set_style_bg_opa(parent, LV_OPA_50, 0); - lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100)); + bool auto_parent = false; + if(parent == NULL) { + auto_parent = true; + parent = lv_obj_create(lv_scr_act()); + lv_obj_remove_style_all(parent); + lv_obj_set_style_bg_color(parent, lv_palette_main(LV_PALETTE_GREY), 0); + lv_obj_set_style_bg_opa(parent, LV_OPA_50, 0); + lv_obj_set_size(parent, LV_PCT(100), LV_PCT(100)); + } lv_obj_t * mbox = lv_obj_create_from_class(&lv_msgbox_class, parent); LV_ASSERT_MALLOC(mbox); if(mbox == NULL) return NULL; + if(auto_parent) lv_obj_add_flag(mbox, LV_MSGBOX_FLAG_AUTO_PARENT); lv_coord_t w = lv_obj_get_content_width(parent); if(w > 2 * LV_DPI_DEF) w = 2 * LV_DPI_DEF; @@ -130,7 +131,8 @@ const char * lv_msgbox_get_active_btn_text(lv_obj_t * mbox) void lv_msgbox_close(lv_obj_t * mbox) { - lv_obj_del(lv_obj_get_parent(mbox)); + if(lv_obj_has_flag(mbox, LV_MSGBOX_FLAG_AUTO_PARENT)) lv_obj_del(lv_obj_get_parent(mbox)); + else lv_obj_del(mbox); } diff --git a/src/extra/widgets/msgbox/lv_msgbox.h b/src/extra/widgets/msgbox/lv_msgbox.h index 44d314482..6a54f85fa 100644 --- a/src/extra/widgets/msgbox/lv_msgbox.h +++ b/src/extra/widgets/msgbox/lv_msgbox.h @@ -41,10 +41,14 @@ extern const lv_obj_class_t lv_msgbox_class; /** * Create a message box objects - * @param par pointer to an object, it will be the parent of the new message box - * @return pointer to the created message box + * @param parent pointer to parent or NULL to create a full screen modal message box + * @param title the title of the message box + * @param txt the text of the message box + * @param btn_txts the buttons as an array of texts terminated by an "" element. E.g. {"btn1", "btn2", ""} + * @param add_close_btn true: add a close button + * @return pointer to the message box object */ -lv_obj_t * lv_msgbox_create(const char * title, const char * txt, const char * btn_txts[], bool add_close_btn); +lv_obj_t * lv_msgbox_create(lv_obj_t * parent, const char * title, const char * txt, const char * btn_txts[], bool add_close_btn); lv_obj_t * lv_msgbox_get_title(lv_obj_t * mbox);