2021-04-08 13:07:48 +02:00
|
|
|
#include "../../lv_examples.h"
|
2021-02-24 05:10:48 +01:00
|
|
|
#if LV_USE_TABLE && LV_BUILD_EXAMPLES
|
|
|
|
|
|
|
|
#define ITEM_CNT 200
|
|
|
|
|
2021-04-19 11:15:28 +02:00
|
|
|
static void draw_event_cb(lv_event_t * e)
|
2021-02-24 05:10:48 +01:00
|
|
|
{
|
2021-04-14 15:31:54 +02:00
|
|
|
lv_obj_t * obj = lv_event_get_target(e);
|
2021-05-12 12:49:32 +02:00
|
|
|
lv_obj_draw_part_dsc_t * dsc = lv_event_get_draw_part_dsc(e);
|
2021-04-19 11:15:28 +02:00
|
|
|
/*If the cells are drawn...*/
|
|
|
|
if(dsc->part == LV_PART_ITEMS) {
|
|
|
|
bool chk = lv_table_has_cell_ctrl(obj, dsc->id, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
|
|
|
|
|
|
|
|
lv_draw_rect_dsc_t rect_dsc;
|
|
|
|
lv_draw_rect_dsc_init(&rect_dsc);
|
2021-04-23 12:46:14 +02:00
|
|
|
rect_dsc.bg_color = chk ? lv_theme_get_color_primary(obj) : lv_palette_lighten(LV_PALETTE_GREY, 2);
|
2021-04-19 11:15:28 +02:00
|
|
|
rect_dsc.radius = LV_RADIUS_CIRCLE;
|
|
|
|
|
|
|
|
lv_area_t sw_area;
|
|
|
|
sw_area.x1 = dsc->draw_area->x2 - 50;
|
|
|
|
sw_area.x2 = sw_area.x1 + 40;
|
|
|
|
sw_area.y1 = dsc->draw_area->y1 + lv_area_get_height(dsc->draw_area) / 2 - 10;
|
|
|
|
sw_area.y2 = sw_area.y1 + 20;
|
|
|
|
lv_draw_rect(&sw_area, dsc->clip_area, &rect_dsc);
|
|
|
|
|
|
|
|
rect_dsc.bg_color = lv_color_white();
|
|
|
|
if(chk) {
|
|
|
|
sw_area.x2 -= 2;
|
|
|
|
sw_area.x1 = sw_area.x2 - 16;
|
|
|
|
} else {
|
|
|
|
sw_area.x1 += 2;
|
|
|
|
sw_area.x2 = sw_area.x1 + 16;
|
2021-02-24 05:10:48 +01:00
|
|
|
}
|
2021-04-19 11:15:28 +02:00
|
|
|
sw_area.y1 += 2;
|
|
|
|
sw_area.y2 -= 2;
|
|
|
|
lv_draw_rect(&sw_area, dsc->clip_area, &rect_dsc);
|
2021-02-24 05:10:48 +01:00
|
|
|
}
|
2021-04-19 11:15:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void change_event_cb(lv_event_t * e)
|
|
|
|
{
|
|
|
|
lv_obj_t * obj = lv_event_get_target(e);
|
|
|
|
uint16_t col;
|
|
|
|
uint16_t row;
|
|
|
|
lv_table_get_selected_cell(obj, &row, &col);
|
|
|
|
bool chk = lv_table_has_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
|
|
|
|
if(chk) lv_table_clear_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
|
|
|
|
else lv_table_add_cell_ctrl(obj, row, 0, LV_TABLE_CELL_CTRL_CUSTOM_1);
|
2021-02-24 05:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A very light-weighted list created from table
|
|
|
|
*/
|
|
|
|
void lv_example_table_2(void)
|
|
|
|
{
|
|
|
|
/*Measure memory usage*/
|
|
|
|
lv_mem_monitor_t mon1;
|
|
|
|
lv_mem_monitor(&mon1);
|
|
|
|
|
|
|
|
uint32_t t = lv_tick_get();
|
|
|
|
|
2021-03-25 13:36:50 +01:00
|
|
|
lv_obj_t * table = lv_table_create(lv_scr_act());
|
2021-02-24 09:56:44 +01:00
|
|
|
|
|
|
|
/*Set a smaller height to the table. It'll make it scrollable*/
|
2021-05-27 16:06:17 +02:00
|
|
|
lv_obj_set_size(table, LV_SIZE_CONTENT, 200);
|
2021-02-24 09:56:44 +01:00
|
|
|
|
|
|
|
lv_table_set_col_width(table, 0, 150);
|
2021-03-15 02:03:27 +08:00
|
|
|
lv_table_set_row_cnt(table, ITEM_CNT); /*Not required but avoids a lot of memory reallocation lv_table_set_set_value*/
|
2021-02-24 05:10:48 +01:00
|
|
|
lv_table_set_col_cnt(table, 1);
|
|
|
|
|
2021-02-24 09:56:44 +01:00
|
|
|
/*Don't make the cell pressed, we will draw something different in the event*/
|
2021-03-31 19:57:14 +02:00
|
|
|
lv_obj_remove_style(table, NULL, LV_PART_ITEMS | LV_STATE_PRESSED);
|
2021-02-24 09:56:44 +01:00
|
|
|
|
2021-02-24 05:10:48 +01:00
|
|
|
uint32_t i;
|
|
|
|
for(i = 0; i < ITEM_CNT; i++) {
|
|
|
|
lv_table_set_cell_value_fmt(table, i, 0, "Item %d", i + 1);
|
|
|
|
}
|
|
|
|
|
2021-03-25 16:14:17 +01:00
|
|
|
lv_obj_align(table, LV_ALIGN_CENTER, 0, -20);
|
2021-02-24 05:10:48 +01:00
|
|
|
|
|
|
|
/*Add an event callback to to apply some custom drawing*/
|
2021-04-19 15:59:38 +02:00
|
|
|
lv_obj_add_event_cb(table, draw_event_cb, LV_EVENT_DRAW_PART_END, NULL);
|
2021-04-19 11:15:28 +02:00
|
|
|
lv_obj_add_event_cb(table, change_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
|
2021-02-24 05:10:48 +01:00
|
|
|
|
|
|
|
lv_mem_monitor_t mon2;
|
|
|
|
lv_mem_monitor(&mon2);
|
|
|
|
|
|
|
|
uint32_t mem_used = mon1.free_size - mon2.free_size;
|
|
|
|
|
|
|
|
uint32_t elaps = lv_tick_elaps(t);
|
|
|
|
|
2021-03-25 13:36:50 +01:00
|
|
|
lv_obj_t * label = lv_label_create(lv_scr_act());
|
2021-04-19 15:59:38 +02:00
|
|
|
lv_label_set_text_fmt(label, "%d items were created in %d ms\n"
|
|
|
|
"using %d bytes of memory",
|
|
|
|
ITEM_CNT, elaps, mem_used);
|
2021-02-24 05:10:48 +01:00
|
|
|
|
2021-03-25 16:14:17 +01:00
|
|
|
lv_obj_align(label, LV_ALIGN_BOTTOM_MID, 0, -10);
|
2021-02-24 05:10:48 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|