1 /**
2  * @file lv_xml_table_parser.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_xml_table_parser.h"
10 #if LV_USE_XML
11 
12 #include "../../../lvgl.h"
13 #include "../../../lvgl_private.h"
14 
15 /*********************
16  *      DEFINES
17  *********************/
18 
19 /**********************
20  *      TYPEDEFS
21  **********************/
22 
23 /**********************
24  *  STATIC PROTOTYPES
25  **********************/
26 static lv_table_cell_ctrl_t table_ctrl_to_enum(const char * txt);
27 
28 /**********************
29  *  STATIC VARIABLES
30  **********************/
31 
32 /**********************
33  *      MACROS
34  **********************/
35 
36 /**********************
37  *   GLOBAL FUNCTIONS
38  **********************/
39 
lv_xml_table_create(lv_xml_parser_state_t * state,const char ** attrs)40 void * lv_xml_table_create(lv_xml_parser_state_t * state, const char ** attrs)
41 {
42     LV_UNUSED(attrs);
43     void * item = lv_table_create(lv_xml_state_get_parent(state));
44 
45     return item;
46 }
47 
48 
lv_xml_table_apply(lv_xml_parser_state_t * state,const char ** attrs)49 void lv_xml_table_apply(lv_xml_parser_state_t * state, const char ** attrs)
50 {
51     void * item = lv_xml_state_get_item(state);
52 
53     lv_xml_obj_apply(state, attrs); /*Apply the common properties, e.g. width, height, styles flags etc*/
54 
55     for(int i = 0; attrs[i]; i += 2) {
56         const char * name = attrs[i];
57         const char * value = attrs[i + 1];
58 
59         if(lv_streq("column_conunt", name)) lv_table_set_column_count(item, lv_xml_atoi(value));
60         else if(lv_streq("row_conunt", name)) lv_table_set_row_count(item, lv_xml_atoi(value));
61         else if(lv_streq("selected_cell", name)) {
62 
63             int32_t value1 = lv_xml_atoi_split(&value, ' ');
64             int32_t value2 = lv_xml_atoi_split(&value, ' ');
65             lv_table_set_selected_cell(item, value1, value2);
66         }
67     }
68 }
69 
lv_xml_table_column_create(lv_xml_parser_state_t * state,const char ** attrs)70 void * lv_xml_table_column_create(lv_xml_parser_state_t * state, const char ** attrs)
71 {
72     LV_UNUSED(attrs);
73 
74     /*Nothing to create*/
75     return lv_xml_state_get_parent(state);;
76 }
77 
lv_xml_table_column_apply(lv_xml_parser_state_t * state,const char ** attrs)78 void lv_xml_table_column_apply(lv_xml_parser_state_t * state, const char ** attrs)
79 {
80     LV_UNUSED(state);
81     LV_UNUSED(attrs);
82 
83     lv_obj_t * table = lv_xml_state_get_parent(state);
84     int32_t column = lv_xml_atoi(lv_xml_get_value_of(attrs, "column"));
85 
86     for(int i = 0; attrs[i]; i += 2) {
87         const char * name = attrs[i];
88         const char * value = attrs[i + 1];
89 
90         if(lv_streq("width", name))  lv_table_set_column_width(table, column, lv_xml_atoi(value));
91     }
92 }
93 
lv_xml_table_cell_create(lv_xml_parser_state_t * state,const char ** attrs)94 void * lv_xml_table_cell_create(lv_xml_parser_state_t * state, const char ** attrs)
95 {
96     LV_UNUSED(attrs);
97 
98     /*Nothing to create*/
99     return lv_xml_state_get_parent(state);;
100 }
101 
lv_xml_table_cell_apply(lv_xml_parser_state_t * state,const char ** attrs)102 void lv_xml_table_cell_apply(lv_xml_parser_state_t * state, const char ** attrs)
103 {
104     LV_UNUSED(state);
105     LV_UNUSED(attrs);
106 
107     lv_obj_t * table = lv_xml_state_get_parent(state);
108     int32_t row = lv_xml_atoi(lv_xml_get_value_of(attrs, "row"));
109     int32_t column = lv_xml_atoi(lv_xml_get_value_of(attrs, "column"));
110 
111     for(int i = 0; attrs[i]; i += 2) {
112         const char * name = attrs[i];
113         const char * value = attrs[i + 1];
114 
115         if(lv_streq("value", name))  lv_table_set_cell_value(table, row, column, value);
116         if(lv_streq("ctrl", name)) {
117             lv_table_cell_ctrl_t ctrl = 0;
118             char buf[256];
119             lv_strncpy(buf, value, sizeof(buf));
120             char * buf_p = buf;
121             const char * str;
122             while((str = lv_xml_split_str(&buf_p, ' ')) != NULL) {
123                 ctrl |= table_ctrl_to_enum(str);
124             }
125 
126             lv_table_add_cell_ctrl(table, row, column, ctrl);
127         }
128     }
129 }
130 
131 /**********************
132  *   STATIC FUNCTIONS
133  **********************/
134 
table_ctrl_to_enum(const char * txt)135 static lv_table_cell_ctrl_t table_ctrl_to_enum(const char * txt)
136 {
137     if(lv_streq("none", txt)) return LV_TABLE_CELL_CTRL_NONE;
138     if(lv_streq("merge_right", txt)) return LV_TABLE_CELL_CTRL_MERGE_RIGHT;
139     if(lv_streq("text_crop", txt)) return LV_TABLE_CELL_CTRL_TEXT_CROP;
140     if(lv_streq("custom_1", txt)) return LV_TABLE_CELL_CTRL_CUSTOM_1;
141     if(lv_streq("custom_2", txt)) return LV_TABLE_CELL_CTRL_CUSTOM_2;
142     if(lv_streq("custom_3", txt)) return LV_TABLE_CELL_CTRL_CUSTOM_3;
143     if(lv_streq("custom_4", txt)) return LV_TABLE_CELL_CTRL_CUSTOM_4;
144 
145     LV_LOG_WARN("%s is an unknown value for table's ctrl", txt);
146     return 0; /*Return 0 in lack of a better option. */
147 }
148 
149 #endif /* LV_USE_XML */
150