lv_example_style_14.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "../lv_examples.h"
  2. #if LV_BUILD_EXAMPLES && LV_USE_IMG
  3. static lv_style_t style_btn;
  4. /*Will be called when the styles of the base theme are already added
  5. to add new styles*/
  6. static void new_theme_apply_cb(lv_theme_t * th, lv_obj_t * obj)
  7. {
  8. LV_UNUSED(th);
  9. if(lv_obj_check_type(obj, &lv_btn_class)) {
  10. lv_obj_add_style(obj, &style_btn, 0);
  11. }
  12. }
  13. static void new_theme_init_and_set(void)
  14. {
  15. /*Initialize the styles*/
  16. lv_style_init(&style_btn);
  17. lv_style_set_bg_color(&style_btn, lv_palette_main(LV_PALETTE_GREEN));
  18. lv_style_set_border_color(&style_btn, lv_palette_darken(LV_PALETTE_GREEN, 3));
  19. lv_style_set_border_width(&style_btn, 3);
  20. /*Initialize the new theme from the current theme*/
  21. lv_theme_t * th_act = lv_disp_get_theme(NULL);
  22. static lv_theme_t th_new;
  23. th_new = *th_act;
  24. /*Set the parent theme and the style apply callback for the new theme*/
  25. lv_theme_set_parent(&th_new, th_act);
  26. lv_theme_set_apply_cb(&th_new, new_theme_apply_cb);
  27. /*Assign the new theme to the current display*/
  28. lv_disp_set_theme(NULL, &th_new);
  29. }
  30. /**
  31. * Extending the current theme
  32. */
  33. void lv_example_style_14(void)
  34. {
  35. lv_obj_t * btn;
  36. lv_obj_t * label;
  37. btn = lv_btn_create(lv_scr_act());
  38. lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, 20);
  39. label = lv_label_create(btn);
  40. lv_label_set_text(label, "Original theme");
  41. new_theme_init_and_set();
  42. btn = lv_btn_create(lv_scr_act());
  43. lv_obj_align(btn, LV_ALIGN_BOTTOM_MID, 0, -20);
  44. label = lv_label_create(btn);
  45. lv_label_set_text(label, "New theme");
  46. }
  47. #endif