1 #include "../../lv_examples.h"
2 #if LV_USE_TABLE && LV_BUILD_EXAMPLES
3 
draw_event_cb(lv_event_t * e)4 static void draw_event_cb(lv_event_t * e)
5 {
6     lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
7     lv_draw_dsc_base_t * base_dsc = lv_draw_task_get_draw_dsc(draw_task);
8     /*If the cells are drawn...*/
9     if(base_dsc->part == LV_PART_ITEMS) {
10         uint32_t row = base_dsc->id1;
11         uint32_t col = base_dsc->id2;
12 
13         /*Make the texts in the first cell center aligned*/
14         if(row == 0) {
15             lv_draw_label_dsc_t * label_draw_dsc = lv_draw_task_get_label_dsc(draw_task);
16             if(label_draw_dsc) {
17                 label_draw_dsc->align = LV_TEXT_ALIGN_CENTER;
18             }
19             lv_draw_fill_dsc_t * fill_draw_dsc = lv_draw_task_get_fill_dsc(draw_task);
20             if(fill_draw_dsc) {
21                 fill_draw_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_BLUE), fill_draw_dsc->color, LV_OPA_20);
22                 fill_draw_dsc->opa = LV_OPA_COVER;
23             }
24         }
25         /*In the first column align the texts to the right*/
26         else if(col == 0) {
27             lv_draw_label_dsc_t * label_draw_dsc = lv_draw_task_get_label_dsc(draw_task);
28             if(label_draw_dsc) {
29                 label_draw_dsc->align = LV_TEXT_ALIGN_RIGHT;
30             }
31         }
32 
33         /*Make every 2nd row grayish*/
34         if((row != 0 && row % 2) == 0) {
35             lv_draw_fill_dsc_t * fill_draw_dsc = lv_draw_task_get_fill_dsc(draw_task);
36             if(fill_draw_dsc) {
37                 fill_draw_dsc->color = lv_color_mix(lv_palette_main(LV_PALETTE_GREY), fill_draw_dsc->color, LV_OPA_10);
38                 fill_draw_dsc->opa = LV_OPA_COVER;
39             }
40         }
41     }
42 }
43 
lv_example_table_1(void)44 void lv_example_table_1(void)
45 {
46     lv_obj_t * table = lv_table_create(lv_screen_active());
47 
48     /*Fill the first column*/
49     lv_table_set_cell_value(table, 0, 0, "Name");
50     lv_table_set_cell_value(table, 1, 0, "Apple");
51     lv_table_set_cell_value(table, 2, 0, "Banana");
52     lv_table_set_cell_value(table, 3, 0, "Lemon");
53     lv_table_set_cell_value(table, 4, 0, "Grape");
54     lv_table_set_cell_value(table, 5, 0, "Melon");
55     lv_table_set_cell_value(table, 6, 0, "Peach");
56     lv_table_set_cell_value(table, 7, 0, "Nuts");
57 
58     /*Fill the second column*/
59     lv_table_set_cell_value(table, 0, 1, "Price");
60     lv_table_set_cell_value(table, 1, 1, "$7");
61     lv_table_set_cell_value(table, 2, 1, "$4");
62     lv_table_set_cell_value(table, 3, 1, "$6");
63     lv_table_set_cell_value(table, 4, 1, "$2");
64     lv_table_set_cell_value(table, 5, 1, "$5");
65     lv_table_set_cell_value(table, 6, 1, "$1");
66     lv_table_set_cell_value(table, 7, 1, "$9");
67 
68     /*Set a smaller height to the table. It'll make it scrollable*/
69     lv_obj_set_height(table, 200);
70     lv_obj_center(table);
71 
72     /*Add an event callback to to apply some custom drawing*/
73     lv_obj_add_event_cb(table, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
74     lv_obj_add_flag(table, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
75 }
76 
77 #endif
78