1 #include "../../lv_examples.h"
2 #if LV_USE_TABLE && LV_BUILD_EXAMPLES
3 
4 #define ITEM_CNT 200
5 
draw_event_cb(lv_event_t * e)6 static void draw_event_cb(lv_event_t * e)
7 {
8     lv_obj_t * obj = lv_event_get_target(e);
9 
10     lv_draw_task_t * draw_task = lv_event_get_draw_task(e);
11     lv_draw_dsc_base_t * base_dsc = lv_draw_task_get_draw_dsc(draw_task);
12     /*If the cells are drawn...*/
13     if(base_dsc->part == LV_PART_ITEMS && lv_draw_task_get_type(draw_task) == LV_DRAW_TASK_TYPE_FILL) {
14         /*Draw the background*/
15         bool chk = lv_table_has_cell_ctrl(obj, base_dsc->id1, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
16         lv_draw_rect_dsc_t rect_dsc;
17         lv_draw_rect_dsc_init(&rect_dsc);
18         rect_dsc.bg_color = chk ? lv_theme_get_color_primary(obj) : lv_palette_lighten(LV_PALETTE_GREY, 2);
19         rect_dsc.radius = LV_RADIUS_CIRCLE;
20 
21         lv_area_t sw_area;
22         sw_area.x1 = 0;
23         sw_area.x2 = 40;
24         sw_area.y1 = 0;
25         sw_area.y2 = 24;
26         lv_area_t draw_task_area;
27         lv_draw_task_get_area(draw_task, &draw_task_area);
28         lv_area_align(&draw_task_area, &sw_area, LV_ALIGN_RIGHT_MID, -15, 0);
29         lv_draw_rect(base_dsc->layer, &rect_dsc, &sw_area);
30 
31         /*Draw the knob*/
32         rect_dsc.bg_color = lv_color_white();
33         lv_area_t knob_area;
34         knob_area.x1 = 0;
35         knob_area.x2 = 18;
36         knob_area.y1 = 0;
37         knob_area.y2 = 18;
38         if(chk) {
39             lv_area_align(&sw_area, &knob_area, LV_ALIGN_RIGHT_MID, -3, 0);
40         }
41         else {
42             lv_area_align(&sw_area, &knob_area, LV_ALIGN_LEFT_MID, 3, 0);
43         }
44         lv_draw_rect(base_dsc->layer, &rect_dsc, &knob_area);
45     }
46 }
47 
change_event_cb(lv_event_t * e)48 static void change_event_cb(lv_event_t * e)
49 {
50     lv_obj_t * obj = lv_event_get_target(e);
51     uint32_t col;
52     uint32_t row;
53     lv_table_get_selected_cell(obj, &row, &col);
54     bool chk = lv_table_has_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
55     if(chk) lv_table_clear_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
56     else lv_table_add_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
57 }
58 
59 /**
60  * A very light-weighted list created from table
61  */
lv_example_table_2(void)62 void lv_example_table_2(void)
63 {
64     /*Measure memory usage*/
65     lv_mem_monitor_t mon1;
66     lv_mem_monitor(&mon1);
67 
68     uint32_t t = lv_tick_get();
69 
70     lv_obj_t * table = lv_table_create(lv_screen_active());
71 
72     /*Set a smaller height to the table. It'll make it scrollable*/
73     lv_obj_set_size(table, LV_SIZE_CONTENT, 200);
74 
75     lv_table_set_column_width(table, 0, 150);
76     lv_table_set_row_count(table, ITEM_CNT); /*Not required but avoids a lot of memory reallocation lv_table_set_set_value*/
77     lv_table_set_column_count(table, 1);
78 
79     /*Don't make the cell pressed, we will draw something different in the event*/
80     lv_obj_remove_style(table, NULL, LV_PART_ITEMS | LV_STATE_PRESSED);
81 
82     uint32_t i;
83     for(i = 0; i < ITEM_CNT; i++) {
84         lv_table_set_cell_value_fmt(table, i, 0, "Item %"LV_PRIu32, i + 1);
85     }
86 
87     lv_obj_align(table, LV_ALIGN_CENTER, 0, -20);
88 
89     /*Add an event callback to to apply some custom drawing*/
90     lv_obj_add_event_cb(table, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
91     lv_obj_add_event_cb(table, change_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
92     lv_obj_add_flag(table, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
93 
94     lv_mem_monitor_t mon2;
95     lv_mem_monitor(&mon2);
96 
97     size_t mem_used = mon1.free_size - mon2.free_size;
98 
99     uint32_t elaps = lv_tick_elaps(t);
100 
101     lv_obj_t * label = lv_label_create(lv_screen_active());
102     lv_label_set_text_fmt(label, "%"LV_PRIu32" items were created in %"LV_PRIu32" ms\n"
103                           "using %zu bytes of memory",
104                           (uint32_t)ITEM_CNT, elaps, mem_used);
105 
106     lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -10);
107 
108 }
109 
110 #endif
111