mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-10 04:37:55 +08:00
d76a346376
Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
52 lines
1.6 KiB
C
52 lines
1.6 KiB
C
#include "../lv_examples.h"
|
|
#if LV_BUILD_EXAMPLES && LV_USE_FLEX
|
|
|
|
static void event_cb(lv_event_t * e)
|
|
{
|
|
/*The original target of the event. Can be the buttons or the container*/
|
|
lv_obj_t * target = lv_event_get_target_obj(e);
|
|
|
|
/*The current target is always the container as the event is added to it*/
|
|
lv_obj_t * cont = lv_event_get_current_target_obj(e);
|
|
|
|
/*If container was clicked do nothing*/
|
|
if(target == cont) return;
|
|
|
|
/*Make the clicked buttons red*/
|
|
lv_obj_set_style_bg_color(target, lv_palette_main(LV_PALETTE_RED), 0);
|
|
}
|
|
|
|
/**
|
|
* @title Event bubbling to a parent
|
|
* @brief Handle clicks on child buttons from a single container callback.
|
|
*
|
|
* A 290x200 container uses `LV_FLEX_FLOW_ROW_WRAP` and holds 30 small
|
|
* buttons, each flagged with `LV_OBJ_FLAG_EVENT_BUBBLE`. One
|
|
* `LV_EVENT_CLICKED` callback on the container reads
|
|
* `lv_event_get_target_obj` to identify which button was clicked and paints
|
|
* its background red; clicks on the container itself are ignored.
|
|
*/
|
|
void lv_example_event_bubble(void)
|
|
{
|
|
|
|
lv_obj_t * cont = lv_obj_create(lv_screen_active());
|
|
lv_obj_set_size(cont, 290, 200);
|
|
lv_obj_center(cont);
|
|
lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_ROW_WRAP);
|
|
|
|
uint32_t i;
|
|
for(i = 0; i < 30; i++) {
|
|
lv_obj_t * btn = lv_button_create(cont);
|
|
lv_obj_set_size(btn, 70, 50);
|
|
lv_obj_add_flag(btn, LV_OBJ_FLAG_EVENT_BUBBLE);
|
|
|
|
lv_obj_t * label = lv_label_create(btn);
|
|
lv_label_set_text_fmt(label, "%" LV_PRIu32, i);
|
|
lv_obj_center(label);
|
|
}
|
|
|
|
lv_obj_add_event_cb(cont, event_cb, LV_EVENT_CLICKED, NULL);
|
|
}
|
|
|
|
#endif
|