diff --git a/lv_objx/lv_bar.c b/lv_objx/lv_bar.c index cc0e0f81ed..ace236e28a 100644 --- a/lv_objx/lv_bar.c +++ b/lv_objx/lv_bar.c @@ -28,11 +28,13 @@ * STATIC PROTOTYPES **********************/ static bool lv_bar_design(lv_obj_t * bar, const area_t * mask, lv_design_mode_t mode); +static lv_res_t lv_bar_signal(lv_obj_t * bar, lv_signal_t sign, void * param); /********************** * STATIC VARIABLES **********************/ static lv_design_func_t ancestor_design_f; +static lv_signal_func_t ancestor_signal; /********************** * MACROS @@ -58,6 +60,9 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, lv_obj_t * copy) lv_obj_t * new_bar = lv_obj_create(par, copy); dm_assert(new_bar); + if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_bar); + if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_bar); + /*Allocate the object type specific extended data*/ lv_bar_ext_t * ext = lv_obj_allocate_ext_attr(new_bar, sizeof(lv_bar_ext_t)); dm_assert(ext); @@ -66,10 +71,6 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, lv_obj_t * copy) ext->cur_value = 0; ext->style_inicator = &lv_style_pretty_color; - /* Save the ancient design function. - * It will be used in the bar design function*/ - if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_bar); - lv_obj_set_signal_func(new_bar, lv_bar_signal); lv_obj_set_design_func(new_bar, lv_bar_design); @@ -93,32 +94,6 @@ lv_obj_t * lv_bar_create(lv_obj_t * par, lv_obj_t * copy) return new_bar; } -/** - * Signal function of the bar - * @param bar pointer to a bar object - * @param sign a signal type from lv_signal_t enum - * @param param pointer to a signal specific variable - */ -bool lv_bar_signal(lv_obj_t * bar, lv_signal_t sign, void * param) -{ - bool valid; - - /* Include the ancient signal function */ - valid = lv_obj_signal(bar, sign, param); - - /* The object can be deleted so check its validity and then - * make the object specific signal handling */ - if(valid != false) { - if(sign == LV_SIGNAL_REFR_EXT_SIZE) { - lv_style_t * style_indic = lv_bar_get_style_indicator(bar); - if(style_indic->body.shadow.width > bar->ext_size) bar->ext_size = style_indic->body.shadow.width; - } - - } - - return valid; -} - /*===================== * Setter functions *====================*/ @@ -311,4 +286,28 @@ static bool lv_bar_design(lv_obj_t * bar, const area_t * mask, lv_design_mode_t return true; } +/** + * Signal function of the bar + * @param bar pointer to a bar object + * @param sign a signal type from lv_signal_t enum + * @param param pointer to a signal specific variable + * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted + */ +static lv_res_t lv_bar_signal(lv_obj_t * bar, lv_signal_t sign, void * param) +{ + lv_res_t res; + + /* Include the ancient signal function */ + res = ancestor_signal(bar, sign, param); + if(res != LV_RES_OK) return res; + + if(sign == LV_SIGNAL_REFR_EXT_SIZE) { + lv_style_t * style_indic = lv_bar_get_style_indicator(bar); + if(style_indic->body.shadow.width > bar->ext_size) bar->ext_size = style_indic->body.shadow.width; + } + + return LV_RES_OK; +} + + #endif diff --git a/lv_objx/lv_bar.h b/lv_objx/lv_bar.h index a7fc1ebca9..c5fc7c06e6 100644 --- a/lv_objx/lv_bar.h +++ b/lv_objx/lv_bar.h @@ -52,14 +52,6 @@ typedef struct */ lv_obj_t * lv_bar_create(lv_obj_t * par, lv_obj_t * copy); -/** - * Signal function of the bar - * @param bar pointer to a bar object - * @param sign a signal type from lv_signal_t enum - * @param param pointer to a signal specific variable - */ -bool lv_bar_signal(lv_obj_t * bar, lv_signal_t sign, void * param); - /** * Set a new value on the bar * @param bar pointer to a bar object diff --git a/lv_objx/lv_mbox.c b/lv_objx/lv_mbox.c index 03e0e9cdff..fbaad55292 100644 --- a/lv_objx/lv_mbox.c +++ b/lv_objx/lv_mbox.c @@ -68,6 +68,7 @@ lv_obj_t * lv_mbox_create(lv_obj_t * par, lv_obj_t * copy) ext->style_btn_rel = &lv_style_btn_off_released; ext->style_btn_pr = &lv_style_btn_off_pressed; ext->anim_time = LV_MBOX_CLOSE_ANIM_TIME; + ext->btn_width = 0; /*The signal and design functions are not copied so set them here*/ lv_obj_set_signal_func(new_mbox, lv_mbox_signal); @@ -78,6 +79,7 @@ lv_obj_t * lv_mbox_create(lv_obj_t * par, lv_obj_t * copy) lv_cont_set_fit(new_mbox, true, true); ext->txt = lv_label_create(new_mbox, NULL); + lv_label_set_align(ext->txt, LV_LABEL_ALIGN_CENTER); lv_label_set_text(ext->txt, "Message"); lv_obj_set_style(new_mbox, &lv_style_pretty); @@ -87,6 +89,7 @@ lv_obj_t * lv_mbox_create(lv_obj_t * par, lv_obj_t * copy) lv_mbox_ext_t * copy_ext = lv_obj_get_ext_attr(copy); ext->txt = lv_label_create(new_mbox, copy_ext->txt); + ext->btn_width = copy_ext->btn_width; /*Copy the buttons and the label on them*/ if(copy_ext->btnh != NULL) { @@ -97,6 +100,10 @@ lv_obj_t * lv_mbox_create(lv_obj_t * par, lv_obj_t * copy) lv_mbox_add_btn(new_mbox, btn_txt_copy, lv_btn_get_action(btn_copy, LV_BTN_ACTION_RELEASE)); } } + + lv_mbox_set_style(new_mbox, lv_mbox_get_style_bg(copy), lv_mbox_get_style_btnh(copy)); + lv_mbox_set_style_btn(new_mbox, copy_ext->style_btn_rel, copy_ext->style_btn_pr); + /*Refresh the style with new signal function*/ lv_obj_refresh_style(new_mbox); } @@ -133,7 +140,7 @@ lv_obj_t * lv_mbox_add_btn(lv_obj_t * mbox, const char * btn_txt, lv_action_t re /*Create a button holder if it is not existed yet*/ if(ext->btnh == NULL) { ext->btnh = lv_cont_create(mbox, NULL); - lv_obj_set_style(ext->btnh, &lv_style_plain_color); + lv_obj_set_style(ext->btnh, &lv_style_transp_fit); lv_obj_set_click(ext->btnh, false); lv_cont_set_fit(ext->btnh, false, true); lv_cont_set_layout(ext->btnh, LV_CONT_LAYOUT_PRETTY); @@ -142,7 +149,13 @@ lv_obj_t * lv_mbox_add_btn(lv_obj_t * mbox, const char * btn_txt, lv_action_t re lv_obj_t *btn = lv_btn_create(ext->btnh, NULL); lv_btn_set_action(btn, LV_BTN_ACTION_RELEASE, rel_action); lv_btn_set_style(btn, ext->style_btn_rel, ext->style_btn_pr, NULL, NULL, NULL); - lv_cont_set_fit(btn, true, true); + + if(ext->btn_width) { + lv_btn_set_fit(btn, false, true); + lv_obj_set_width(btn, ext->btn_width); + } else { + lv_btn_set_fit(btn, true, true); + } lv_obj_t *label = lv_label_create(btn, NULL); lv_label_set_text(label, btn_txt); @@ -169,6 +182,44 @@ void lv_mbox_set_text(lv_obj_t * mbox, const char * txt) btnh_resize(mbox); } +/** + * Set the width of the buttons + * @param mbox pointer to message box object + * @param w width of the buttons or 0 to use auto fit + */ +void lv_mbox_set_btn_width(lv_obj_t *mbox, cord_t w) +{ + lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox); + ext->btn_width = w; + if(ext->btnh == NULL) return; + + lv_obj_t *btn = lv_obj_get_child(ext->btnh, NULL); + while(btn != NULL) { + if(w) { + lv_btn_set_fit(btn, false, true); + lv_obj_set_width(btn, w); + } else { + lv_btn_set_fit(btn, true, true); + } + btn = lv_obj_get_child(ext->btnh, btn); + } + + btnh_resize(mbox); +} + +/** + * Set the styles of a message box + * @param mbox pointer to a message box object + * @param bg pointer to the new background style + * @param btnh pointer to the new button holder style + */ +void lv_mbox_set_style(lv_obj_t *mbox, lv_style_t *bg, lv_style_t *btnh) +{ + lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox); + lv_obj_set_style(ext->btnh, btnh); + lv_obj_set_style(mbox, bg); +} + /** * Set styles of the buttons of a message box in each state * @param mbox pointer to a message box object @@ -251,6 +302,17 @@ const char * lv_mbox_get_text(lv_obj_t * mbox) return lv_label_get_text(ext->txt); } +/** + * Get width of the buttons + * @param mbox pointer to a message box object + * @return width of the buttons (0: auto fit enabled) + */ +cord_t lv_mbox_get_text(lv_obj_t * mbox) +{ + lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox); + return ext->btn_width; +} + /** * Get the message box object from one of its button. * It is useful in the button release actions where only the button is known @@ -276,6 +338,18 @@ uint16_t lv_mbox_get_anim_time(lv_obj_t * mbox ) return ext->anim_time; } +/** + * Get the style of a message box's button holder + * @param mbox pointer to a message box object + * @return pointer to the message box's background style + */ +lv_style_t * lv_mbox_get_style_btnh(lv_obj_t *mbox) +{ + lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox); + return lv_obj_get_style(ext->btnh); +} + + /** * Get the style of the buttons on a message box * @param mbox pointer to a message box object @@ -435,8 +509,7 @@ static void btnh_resize(lv_obj_t *mbox) lv_mbox_ext_t * ext = lv_obj_get_ext_attr(mbox); if(ext->btnh == NULL) return; - lv_style_t *bg_style = lv_mbox_get_style(mbox); - lv_style_t *btnh_style = lv_mbox_get_style(ext->btnh); + lv_style_t *btnh_style = lv_mbox_get_style_bg(ext->btnh); cord_t btnh_req_w = 2 * btnh_style->body.padding.hor; lv_obj_t *btn = lv_obj_get_child(ext->btnh, NULL); diff --git a/lv_objx/lv_mbox.h b/lv_objx/lv_mbox.h index 0b2689a58c..bbe1a56a94 100644 --- a/lv_objx/lv_mbox.h +++ b/lv_objx/lv_mbox.h @@ -52,7 +52,8 @@ typedef struct lv_obj_t * btnh; /*Holder of the buttons*/ lv_style_t * style_btn_rel; /*Style of the released buttons*/ lv_style_t * style_btn_pr; /*Style of the pressed buttons*/ - uint16_t anim_time; /*Duration of close animation [ms] (0: no animation)*/ + uint16_t anim_time; /*Duration of close animation [ms] (0: no animation)*/ + cord_t btn_width; /*Button width (0: to auto fit)*/ }lv_mbox_ext_t; /********************** @@ -90,6 +91,21 @@ lv_obj_t * lv_mbox_add_btn(lv_obj_t * mbox, const char * btn_txt, lv_action_t re */ void lv_mbox_set_text(lv_obj_t * mbox, const char * txt); +/** + * Set the width of the buttons + * @param mbox pointer to message box object + * @param w width of the buttons or 0 to use auto fit + */ +void lv_mbox_set_btn_width(lv_obj_t *mbox, cord_t w); + +/** + * Set the styles of a message box + * @param mbox pointer to a message box object + * @param bg pointer to the new background style + * @param btnh pointer to the new button holder style + */ +void lv_mbox_set_style(lv_obj_t *mbox, lv_style_t *bg, lv_style_t *btnh); + /** * Set styles of the buttons of a message box in each state * @param mbox pointer to a message box object @@ -125,6 +141,13 @@ void lv_mbox_stop_auto_close(lv_obj_t * mbox); */ const char * lv_mbox_get_text(lv_obj_t * mbox); +/** + * Get width of the buttons + * @param mbox pointer to a message box object + * @return width of the buttons (0: auto fit enabled) + */ +cord_t lv_mbox_get_text(lv_obj_t * mbox); + /** * Get the message box object from one of its button. * It is useful in the button release actions where only the button is known @@ -148,30 +171,28 @@ uint16_t lv_mbox_get_anim_time(lv_obj_t * mbox ); */ lv_style_t * lv_mbox_get_style_btn(lv_obj_t * mbox, lv_btn_state_t state); -/**************************** - * TRANSPARENT API FUNCTIONS - ***************************/ +/** + * Get the style of a message box's button holder + * @param mbox pointer to a message box object + * @return pointer to the message box's background style + */ +lv_style_t * lv_mbox_get_style_btnh(lv_obj_t *mbox); + +/****************************** + * TRANSPARENT API FUNCTIONS + ******************************/ /** - * Set the style of a message box + * Get the style of a message box's background * @param mbox pointer to a message box object - * @param style pointer to the new style + * @return pointer to the message box's background style */ -static inline void lv_mbox_set_style(lv_obj_t *mbox, lv_style_t * style) -{ - lv_obj_set_style(mbox, style); -} - -/** - * Get the style of a message box - * @param mbox pointer to a message box object - * @return pointer to the message box's style - */ -static inline lv_style_t * lv_mbox_get_style(lv_obj_t *mbox) +static inline lv_style_t * lv_mbox_get_style_bg(lv_obj_t *mbox) { return lv_obj_get_style(mbox); } + /********************** * MACROS **********************/ diff --git a/lv_objx/lv_slider.c b/lv_objx/lv_slider.c index 33b3a4f1d4..4ea6a09cf7 100644 --- a/lv_objx/lv_slider.c +++ b/lv_objx/lv_slider.c @@ -32,6 +32,7 @@ static bool lv_slider_design(lv_obj_t * slider, const area_t * mask, lv_design_m * STATIC VARIABLES **********************/ static lv_design_func_t ancestor_design_f; +static lv_signal_func_t ancestor_signal; /********************** * MACROS @@ -57,6 +58,9 @@ lv_obj_t * lv_slider_create(lv_obj_t * par, lv_obj_t * copy) lv_obj_t * new_slider = lv_bar_create(par, copy); dm_assert(new_slider); + if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_slider); + if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_slider); + /*Allocate the slider type specific extended data*/ lv_slider_ext_t * ext = lv_obj_allocate_ext_attr(new_slider, sizeof(lv_slider_ext_t)); dm_assert(ext); @@ -67,10 +71,6 @@ lv_obj_t * lv_slider_create(lv_obj_t * par, lv_obj_t * copy) ext->style_knob = &lv_style_pretty; ext->knob_in = 0; - /* Save the bar design function. - * It will be used in the sllider design function*/ - if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_func(new_slider); - /*The signal and design functions are not copied so set them here*/ lv_obj_set_signal_func(new_slider, lv_slider_signal); lv_obj_set_design_func(new_slider, lv_slider_design); @@ -105,7 +105,7 @@ bool lv_slider_signal(lv_obj_t * slider, lv_signal_t sign, void * param) bool valid; /* Include the ancient signal function */ - valid = lv_bar_signal(slider, sign, param); + valid = ancestor_signal(slider, sign, param); /* The object can be deleted so check its validity and then * make the object specific signal handling */ diff --git a/lv_objx/lv_slider.h b/lv_objx/lv_slider.h index 3295c5a4f2..361b58f4f2 100644 --- a/lv_objx/lv_slider.h +++ b/lv_objx/lv_slider.h @@ -111,7 +111,6 @@ lv_style_t * lv_slider_get_style_knob(lv_obj_t * slider); */ bool lv_slider_get_knob_in(lv_obj_t * slider); - /****************************** * TRANSPARENT API FUNCTIONS ******************************/ diff --git a/lv_objx/lv_sw.c b/lv_objx/lv_sw.c index c3c8b1323d..c5eaff9761 100644 --- a/lv_objx/lv_sw.c +++ b/lv_objx/lv_sw.c @@ -25,6 +25,7 @@ #if 0 /*Slider design is used*/ static bool lv_sw_design(lv_obj_t * sw, const area_t * mask, lv_design_mode_t mode); #endif +static lv_signal_func_t ancestor_signal; /********************** * STATIC VARIABLES **********************/ @@ -52,6 +53,8 @@ lv_obj_t * lv_sw_create(lv_obj_t * par, lv_obj_t * copy) /*Create the ancestor of switch*/ lv_obj_t * new_sw = lv_slider_create(par, copy); dm_assert(new_sw); + + if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_sw); /*Allocate the switch type specific extended data*/ lv_sw_ext_t * ext = lv_obj_allocate_ext_attr(new_sw, sizeof(lv_sw_ext_t)); @@ -98,7 +101,7 @@ bool lv_sw_signal(lv_obj_t * sw, lv_signal_t sign, void * param) ext->slider.action = NULL; /*Do not let the slider to call the callback. The Switch will do it*/ /* Include the ancient signal function */ - valid = lv_slider_signal(sw, sign, param); + valid = ancestor_signal(sw, sign, param); /* The object can be deleted so check its validity and then * make the object specific signal handling */