lv_example_event_3.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "../lv_examples.h"
  2. #if LV_BUILD_EXAMPLES && LV_USE_FLEX
  3. static void event_cb(lv_event_t * e)
  4. {
  5. /*The original target of the event. Can be the buttons or the container*/
  6. lv_obj_t * target = lv_event_get_target(e);
  7. /*The current target is always the container as the event is added to it*/
  8. lv_obj_t * cont = lv_event_get_current_target(e);
  9. /*If container was clicked do nothing*/
  10. if(target == cont) return;
  11. /*Make the clicked buttons red*/
  12. lv_obj_set_style_bg_color(target, lv_palette_main(LV_PALETTE_RED), 0);
  13. }
  14. /**
  15. * Demonstrate event bubbling
  16. */
  17. void lv_example_event_3(void)
  18. {
  19. lv_obj_t * cont = lv_obj_create(lv_scr_act());
  20. lv_obj_set_size(cont, 290, 200);
  21. lv_obj_center(cont);
  22. lv_obj_set_flex_flow(cont, LV_FLEX_FLOW_ROW_WRAP);
  23. uint32_t i;
  24. for(i = 0; i < 30; i++) {
  25. lv_obj_t * btn = lv_btn_create(cont);
  26. lv_obj_set_size(btn, 80, 50);
  27. lv_obj_add_flag(btn, LV_OBJ_FLAG_EVENT_BUBBLE);
  28. lv_obj_t * label = lv_label_create(btn);
  29. lv_label_set_text_fmt(label, "%"LV_PRIu32, i);
  30. lv_obj_center(label);
  31. }
  32. lv_obj_add_event_cb(cont, event_cb, LV_EVENT_CLICKED, NULL);
  33. }
  34. #endif