1 /**
2  * @file lv_btnmatrix.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_btnmatrix.h"
10 #if LV_USE_BTNMATRIX != 0
11 
12 #include "../misc/lv_assert.h"
13 #include "../core/lv_indev.h"
14 #include "../core/lv_group.h"
15 #include "../draw/lv_draw.h"
16 #include "../core/lv_refr.h"
17 #include "../misc/lv_txt.h"
18 #include "../misc/lv_txt_ap.h"
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 #define MY_CLASS &lv_btnmatrix_class
24 
25 #define BTN_EXTRA_CLICK_AREA_MAX (LV_DPI_DEF / 10)
26 #define LV_BTNMATRIX_WIDTH_MASK 0x0007
27 
28 /**********************
29  *      TYPEDEFS
30  **********************/
31 
32 /**********************
33  *  STATIC PROTOTYPES
34  **********************/
35 static void lv_btnmatrix_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
36 static void lv_btnmatrix_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
37 static void lv_btnmatrix_event(const lv_obj_class_t * class_p, lv_event_t * e);
38 static void draw_main(lv_event_t * e);
39 
40 static uint8_t get_button_width(lv_btnmatrix_ctrl_t ctrl_bits);
41 static bool button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits);
42 static bool button_is_checked(lv_btnmatrix_ctrl_t ctrl_bits);
43 static bool button_is_repeat_disabled(lv_btnmatrix_ctrl_t ctrl_bits);
44 static bool button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits);
45 static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits);
46 static bool button_is_popover(lv_btnmatrix_ctrl_t ctrl_bits);
47 static bool button_is_checkable(lv_btnmatrix_ctrl_t ctrl_bits);
48 static bool button_is_recolor(lv_btnmatrix_ctrl_t ctrl_bits);
49 static bool button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits);
50 static uint16_t get_button_from_point(lv_obj_t * obj, lv_point_t * p);
51 static void allocate_btn_areas_and_controls(const lv_obj_t * obj, const char ** map);
52 static void invalidate_button_area(const lv_obj_t * obj, uint16_t btn_idx);
53 static void make_one_button_checked(lv_obj_t * obj, uint16_t btn_idx);
54 static bool has_popovers_in_top_row(lv_obj_t * obj);
55 
56 /**********************
57  *  STATIC VARIABLES
58  **********************/
59 static const char * lv_btnmatrix_def_map[] = {"Btn1", "Btn2", "Btn3", "\n", "Btn4", "Btn5", ""};
60 
61 const lv_obj_class_t lv_btnmatrix_class = {
62     .constructor_cb = lv_btnmatrix_constructor,
63     .destructor_cb = lv_btnmatrix_destructor,
64     .event_cb = lv_btnmatrix_event,
65     .width_def = LV_DPI_DEF * 2,
66     .height_def = LV_DPI_DEF,
67     .instance_size = sizeof(lv_btnmatrix_t),
68     .editable = LV_OBJ_CLASS_EDITABLE_TRUE,
69     .group_def = LV_OBJ_CLASS_GROUP_DEF_TRUE,
70     .base_class = &lv_obj_class
71 };
72 
73 /**********************
74  *      MACROS
75  **********************/
76 
77 /**********************
78  *   GLOBAL FUNCTIONS
79  **********************/
80 
lv_btnmatrix_create(lv_obj_t * parent)81 lv_obj_t * lv_btnmatrix_create(lv_obj_t * parent)
82 {
83     LV_LOG_INFO("begin");
84     lv_obj_t * obj = lv_obj_class_create_obj(MY_CLASS, parent);
85     lv_obj_class_init_obj(obj);
86     return obj;
87 }
88 
89 /*=====================
90  * Setter functions
91  *====================*/
92 
lv_btnmatrix_set_map(lv_obj_t * obj,const char * map[])93 void lv_btnmatrix_set_map(lv_obj_t * obj, const char * map[])
94 {
95     LV_ASSERT_OBJ(obj, MY_CLASS);
96     if(map == NULL) return;
97 
98     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
99 
100     /*Analyze the map and create the required number of buttons*/
101     allocate_btn_areas_and_controls(obj, map);
102     btnm->map_p = map;
103 
104     lv_base_dir_t base_dir = lv_obj_get_style_base_dir(obj, LV_PART_MAIN);
105 
106     /*Set size and positions of the buttons*/
107     lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
108     lv_coord_t ptop = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
109     lv_coord_t prow = lv_obj_get_style_pad_row(obj, LV_PART_MAIN);
110     lv_coord_t pcol = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
111 
112     lv_coord_t max_w            = lv_obj_get_content_width(obj);
113     lv_coord_t max_h            = lv_obj_get_content_height(obj);
114 
115     /*Calculate the position of each row*/
116     lv_coord_t max_h_no_gap = max_h - (prow * (btnm->row_cnt - 1));
117 
118     /*Count the units and the buttons in a line
119      *(A button can be 1,2,3... unit wide)*/
120     uint32_t txt_tot_i = 0; /*Act. index in the str map*/
121     uint32_t btn_tot_i = 0; /*Act. index of button areas*/
122     const char ** map_row = map;
123 
124     /*Count the units and the buttons in a line*/
125     uint32_t row;
126     for(row = 0; row < btnm->row_cnt; row++) {
127         uint16_t unit_cnt = 0;           /*Number of units in a row*/
128         uint16_t btn_cnt = 0;            /*Number of buttons in a row*/
129         /*Count the buttons and units in this row*/
130         while(map_row[btn_cnt] && strcmp(map_row[btn_cnt], "\n") != 0 && map_row[btn_cnt][0] != '\0') {
131             unit_cnt += get_button_width(btnm->ctrl_bits[btn_tot_i + btn_cnt]);
132             btn_cnt++;
133         }
134 
135         /*Only deal with the non empty lines*/
136         if(btn_cnt == 0) {
137             map_row = &map_row[btn_cnt + 1];       /*Set the map to the next row*/
138             continue;
139         }
140 
141         lv_coord_t row_y1 = ptop + (max_h_no_gap * row) / btnm->row_cnt + row * prow;
142         lv_coord_t row_y2 = ptop + (max_h_no_gap * (row + 1)) / btnm->row_cnt + row * prow - 1;
143 
144         /*Set the button size and positions*/
145         lv_coord_t max_w_no_gap = max_w - (pcol * (btn_cnt - 1));
146         if(max_w_no_gap < 0) max_w_no_gap = 0;
147 
148         uint32_t row_unit_cnt = 0;  /*The current unit position in the row*/
149         uint32_t btn;
150         for(btn = 0; btn < btn_cnt; btn++, btn_tot_i++, txt_tot_i++) {
151             uint32_t btn_u = get_button_width(btnm->ctrl_bits[btn_tot_i]);
152 
153             lv_coord_t btn_x1 = (max_w_no_gap * row_unit_cnt) / unit_cnt + btn * pcol;
154             lv_coord_t btn_x2 = (max_w_no_gap * (row_unit_cnt + btn_u)) / unit_cnt + btn * pcol - 1;
155 
156             /*If RTL start from the right*/
157             if(base_dir == LV_BASE_DIR_RTL) {
158                 lv_coord_t tmp = btn_x1;
159                 btn_x1 = btn_x2;
160                 btn_x2 = tmp;
161 
162                 btn_x1 = max_w - btn_x1;
163                 btn_x2 = max_w - btn_x2;
164             }
165 
166             btn_x1 += pleft;
167             btn_x2 += pleft;
168 
169             lv_area_set(&btnm->button_areas[btn_tot_i], btn_x1, row_y1, btn_x2, row_y2);
170 
171             row_unit_cnt += btn_u;
172         }
173 
174         map_row = &map_row[btn_cnt + 1];       /*Set the map to the next line*/
175     }
176 
177     /*Popovers in the top row will draw outside the widget and the extended draw size depends on
178      *the row height which may have changed when setting the new map*/
179     lv_obj_refresh_ext_draw_size(obj);
180 
181     lv_obj_invalidate(obj);
182 }
183 
lv_btnmatrix_set_ctrl_map(lv_obj_t * obj,const lv_btnmatrix_ctrl_t ctrl_map[])184 void lv_btnmatrix_set_ctrl_map(lv_obj_t * obj, const lv_btnmatrix_ctrl_t ctrl_map[])
185 {
186     LV_ASSERT_OBJ(obj, MY_CLASS);
187 
188     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
189     lv_memcpy(btnm->ctrl_bits, ctrl_map, sizeof(lv_btnmatrix_ctrl_t) * btnm->btn_cnt);
190 
191     lv_btnmatrix_set_map(obj, btnm->map_p);
192 }
193 
lv_btnmatrix_set_selected_btn(lv_obj_t * obj,uint16_t btn_id)194 void lv_btnmatrix_set_selected_btn(lv_obj_t * obj, uint16_t btn_id)
195 {
196     LV_ASSERT_OBJ(obj, MY_CLASS);
197 
198     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
199 
200     if(btn_id >= btnm->btn_cnt && btn_id != LV_BTNMATRIX_BTN_NONE) return;
201 
202     invalidate_button_area(obj, btnm->btn_id_sel);
203     btnm->btn_id_sel = btn_id;
204     invalidate_button_area(obj, btn_id);
205 }
206 
lv_btnmatrix_set_btn_ctrl(lv_obj_t * obj,uint16_t btn_id,lv_btnmatrix_ctrl_t ctrl)207 void lv_btnmatrix_set_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
208 {
209     LV_ASSERT_OBJ(obj, MY_CLASS);
210 
211     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
212 
213     if(btn_id >= btnm->btn_cnt) return;
214 
215     if(btnm->one_check && (ctrl & LV_BTNMATRIX_CTRL_CHECKED)) {
216         lv_btnmatrix_clear_btn_ctrl_all(obj, LV_BTNMATRIX_CTRL_CHECKED);
217     }
218 
219     btnm->ctrl_bits[btn_id] |= ctrl;
220     invalidate_button_area(obj, btn_id);
221 
222     if(ctrl & LV_BTNMATRIX_CTRL_POPOVER) {
223         lv_obj_refresh_ext_draw_size(obj);
224     }
225 }
226 
lv_btnmatrix_clear_btn_ctrl(lv_obj_t * obj,uint16_t btn_id,lv_btnmatrix_ctrl_t ctrl)227 void lv_btnmatrix_clear_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
228 {
229     LV_ASSERT_OBJ(obj, MY_CLASS);
230 
231     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
232 
233     if(btn_id >= btnm->btn_cnt) return;
234 
235     btnm->ctrl_bits[btn_id] &= (~ctrl);
236     invalidate_button_area(obj, btn_id);
237 
238     if(ctrl & LV_BTNMATRIX_CTRL_POPOVER) {
239         lv_obj_refresh_ext_draw_size(obj);
240     }
241 }
242 
lv_btnmatrix_set_btn_ctrl_all(lv_obj_t * obj,lv_btnmatrix_ctrl_t ctrl)243 void lv_btnmatrix_set_btn_ctrl_all(lv_obj_t * obj, lv_btnmatrix_ctrl_t ctrl)
244 {
245     LV_ASSERT_OBJ(obj, MY_CLASS);
246 
247     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
248     uint16_t i;
249     for(i = 0; i < btnm->btn_cnt; i++) {
250         lv_btnmatrix_set_btn_ctrl(obj, i, ctrl);
251     }
252 }
253 
lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * obj,lv_btnmatrix_ctrl_t ctrl)254 void lv_btnmatrix_clear_btn_ctrl_all(lv_obj_t * obj, lv_btnmatrix_ctrl_t ctrl)
255 {
256     LV_ASSERT_OBJ(obj, MY_CLASS);
257 
258     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
259     uint16_t i;
260     for(i = 0; i < btnm->btn_cnt; i++) {
261         lv_btnmatrix_clear_btn_ctrl(obj, i, ctrl);
262     }
263 }
264 
lv_btnmatrix_set_btn_width(lv_obj_t * obj,uint16_t btn_id,uint8_t width)265 void lv_btnmatrix_set_btn_width(lv_obj_t * obj, uint16_t btn_id, uint8_t width)
266 {
267     LV_ASSERT_OBJ(obj, MY_CLASS);
268 
269     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
270     if(btn_id >= btnm->btn_cnt) return;
271     btnm->ctrl_bits[btn_id] &= (~LV_BTNMATRIX_WIDTH_MASK);
272     btnm->ctrl_bits[btn_id] |= (LV_BTNMATRIX_WIDTH_MASK & width);
273 
274     lv_btnmatrix_set_map(obj, btnm->map_p);
275 }
276 
lv_btnmatrix_set_one_checked(lv_obj_t * obj,bool en)277 void lv_btnmatrix_set_one_checked(lv_obj_t * obj, bool en)
278 {
279     LV_ASSERT_OBJ(obj, MY_CLASS);
280 
281     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
282     btnm->one_check     = en;
283 
284     /*If more than one button is toggled only the first one should be*/
285     make_one_button_checked(obj, 0);
286 }
287 
288 /*=====================
289  * Getter functions
290  *====================*/
291 
lv_btnmatrix_get_map(const lv_obj_t * obj)292 const char ** lv_btnmatrix_get_map(const lv_obj_t * obj)
293 {
294     LV_ASSERT_OBJ(obj, MY_CLASS);
295 
296     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
297     return btnm->map_p;
298 }
299 
lv_btnmatrix_get_selected_btn(const lv_obj_t * obj)300 uint16_t lv_btnmatrix_get_selected_btn(const lv_obj_t * obj)
301 {
302     LV_ASSERT_OBJ(obj, MY_CLASS);
303 
304     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
305     return btnm->btn_id_sel;
306 }
307 
lv_btnmatrix_get_btn_text(const lv_obj_t * obj,uint16_t btn_id)308 const char * lv_btnmatrix_get_btn_text(const lv_obj_t * obj, uint16_t btn_id)
309 {
310     LV_ASSERT_OBJ(obj, MY_CLASS);
311 
312     if(btn_id == LV_BTNMATRIX_BTN_NONE) return NULL;
313 
314     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
315     if(btn_id > btnm->btn_cnt) return NULL;
316 
317     uint16_t txt_i = 0;
318     uint16_t btn_i = 0;
319 
320     /*Search the text of btnm->btn_pr the buttons text in the map
321      *Skip "\n"-s*/
322     while(btn_i != btn_id) {
323         btn_i++;
324         txt_i++;
325         if(strcmp(btnm->map_p[txt_i], "\n") == 0) txt_i++;
326     }
327 
328     if(btn_i == btnm->btn_cnt) return NULL;
329 
330     return btnm->map_p[txt_i];
331 }
332 
lv_btnmatrix_has_btn_ctrl(lv_obj_t * obj,uint16_t btn_id,lv_btnmatrix_ctrl_t ctrl)333 bool lv_btnmatrix_has_btn_ctrl(lv_obj_t * obj, uint16_t btn_id, lv_btnmatrix_ctrl_t ctrl)
334 {
335     LV_ASSERT_OBJ(obj, MY_CLASS);
336 
337     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
338     if(btn_id >= btnm->btn_cnt) return false;
339 
340     return ((btnm->ctrl_bits[btn_id] & ctrl) == ctrl) ? true : false;
341 }
342 
lv_btnmatrix_get_one_checked(const lv_obj_t * obj)343 bool lv_btnmatrix_get_one_checked(const lv_obj_t * obj)
344 {
345     LV_ASSERT_OBJ(obj, MY_CLASS);
346 
347     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
348 
349     return btnm->one_check;
350 }
351 
352 /**********************
353  *   STATIC FUNCTIONS
354  **********************/
355 
lv_btnmatrix_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)356 static void lv_btnmatrix_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
357 {
358     LV_UNUSED(class_p);
359     LV_TRACE_OBJ_CREATE("begin");
360     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
361     btnm->btn_cnt        = 0;
362     btnm->row_cnt        = 0;
363     btnm->btn_id_sel     = LV_BTNMATRIX_BTN_NONE;
364     btnm->button_areas   = NULL;
365     btnm->ctrl_bits      = NULL;
366     btnm->map_p          = NULL;
367     btnm->one_check      = 0;
368 
369     lv_btnmatrix_set_map(obj, lv_btnmatrix_def_map);
370 
371     LV_TRACE_OBJ_CREATE("finished");
372 }
373 
lv_btnmatrix_destructor(const lv_obj_class_t * class_p,lv_obj_t * obj)374 static void lv_btnmatrix_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
375 {
376     LV_TRACE_OBJ_CREATE("begin");
377     LV_UNUSED(class_p);
378     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
379     lv_mem_free(btnm->button_areas);
380     lv_mem_free(btnm->ctrl_bits);
381     btnm->button_areas = NULL;
382     btnm->ctrl_bits = NULL;
383     LV_TRACE_OBJ_CREATE("finished");
384 }
385 
lv_btnmatrix_event(const lv_obj_class_t * class_p,lv_event_t * e)386 static void lv_btnmatrix_event(const lv_obj_class_t * class_p, lv_event_t * e)
387 {
388     LV_UNUSED(class_p);
389 
390     lv_res_t res;
391 
392     /*Call the ancestor's event handler*/
393     res = lv_obj_event_base(MY_CLASS, e);
394     if(res != LV_RES_OK) return;
395 
396     lv_event_code_t code = lv_event_get_code(e);
397     lv_obj_t * obj = lv_event_get_target(e);
398     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
399     lv_point_t p;
400 
401     if(code == LV_EVENT_REFR_EXT_DRAW_SIZE) {
402         if(has_popovers_in_top_row(obj)) {
403             /*reserve one row worth of extra space to account for popovers in the top row*/
404             lv_coord_t s = btnm->row_cnt > 0 ? lv_obj_get_content_height(obj) / btnm->row_cnt : 0;
405             lv_event_set_ext_draw_size(e, s);
406         }
407     }
408     if(code == LV_EVENT_STYLE_CHANGED) {
409         lv_btnmatrix_set_map(obj, btnm->map_p);
410     }
411     else if(code == LV_EVENT_SIZE_CHANGED) {
412         lv_btnmatrix_set_map(obj, btnm->map_p);
413     }
414     else if(code == LV_EVENT_PRESSED) {
415         void * param = lv_event_get_param(e);
416         invalidate_button_area(obj, btnm->btn_id_sel);
417 
418         lv_indev_type_t indev_type = lv_indev_get_type(lv_indev_get_act());
419         if(indev_type == LV_INDEV_TYPE_POINTER || indev_type == LV_INDEV_TYPE_BUTTON) {
420             uint16_t btn_pr;
421             /*Search the pressed area*/
422             lv_indev_get_point(param, &p);
423             btn_pr = get_button_from_point(obj, &p);
424             /*Handle the case where there is no button there*/
425             if(btn_pr != LV_BTNMATRIX_BTN_NONE) {
426                 if(button_is_inactive(btnm->ctrl_bits[btn_pr]) == false &&
427                    button_is_hidden(btnm->ctrl_bits[btn_pr]) == false) {
428                     btnm->btn_id_sel = btn_pr;
429                     invalidate_button_area(obj, btnm->btn_id_sel); /*Invalidate the new area*/
430                 }
431             }
432         }
433 
434         if(btnm->btn_id_sel != LV_BTNMATRIX_BTN_NONE) {
435             if(button_is_click_trig(btnm->ctrl_bits[btnm->btn_id_sel]) == false &&
436                button_is_popover(btnm->ctrl_bits[btnm->btn_id_sel]) == false &&
437                button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel]) == false &&
438                button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) == false) {
439                 uint32_t b = btnm->btn_id_sel;
440                 res        = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b);
441                 if(res != LV_RES_OK) return;
442             }
443         }
444     }
445     else if(code == LV_EVENT_PRESSING) {
446         void * param = lv_event_get_param(e);
447         uint16_t btn_pr = LV_BTNMATRIX_BTN_NONE;
448         /*Search the pressed area*/
449         lv_indev_t * indev = lv_indev_get_act();
450         lv_indev_type_t indev_type = lv_indev_get_type(indev);
451         if(indev_type == LV_INDEV_TYPE_ENCODER || indev_type == LV_INDEV_TYPE_KEYPAD) return;
452 
453         lv_indev_get_point(indev, &p);
454         btn_pr = get_button_from_point(obj, &p);
455         /*Invalidate to old and the new areas*/
456         if(btn_pr != btnm->btn_id_sel) {
457             if(btnm->btn_id_sel != LV_BTNMATRIX_BTN_NONE) {
458                 invalidate_button_area(obj, btnm->btn_id_sel);
459             }
460 
461             btnm->btn_id_sel = btn_pr;
462 
463             lv_indev_reset_long_press(param); /*Start the log press time again on the new button*/
464             if(btn_pr != LV_BTNMATRIX_BTN_NONE &&
465                button_is_inactive(btnm->ctrl_bits[btn_pr]) == false &&
466                button_is_hidden(btnm->ctrl_bits[btn_pr]) == false) {
467                 invalidate_button_area(obj, btn_pr);
468                 /*Send VALUE_CHANGED for the newly pressed button*/
469                 if(button_is_click_trig(btnm->ctrl_bits[btn_pr]) == false &&
470                    button_is_popover(btnm->ctrl_bits[btnm->btn_id_sel]) == false) {
471                     uint32_t b = btn_pr;
472                     res = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b);
473                     if(res != LV_RES_OK) return;
474                 }
475             }
476         }
477     }
478     else if(code == LV_EVENT_RELEASED) {
479         if(btnm->btn_id_sel != LV_BTNMATRIX_BTN_NONE) {
480             /*Toggle the button if enabled*/
481             if(button_is_checkable(btnm->ctrl_bits[btnm->btn_id_sel]) &&
482                !button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel])) {
483                 if(button_get_checked(btnm->ctrl_bits[btnm->btn_id_sel]) && !btnm->one_check) {
484                     btnm->ctrl_bits[btnm->btn_id_sel] &= (~LV_BTNMATRIX_CTRL_CHECKED);
485                 }
486                 else {
487                     btnm->ctrl_bits[btnm->btn_id_sel] |= LV_BTNMATRIX_CTRL_CHECKED;
488                 }
489                 if(btnm->one_check) make_one_button_checked(obj, btnm->btn_id_sel);
490             }
491 
492 
493             if((button_is_click_trig(btnm->ctrl_bits[btnm->btn_id_sel]) == true ||
494                 button_is_popover(btnm->ctrl_bits[btnm->btn_id_sel]) == true) &&
495                button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel]) == false &&
496                button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) == false) {
497                 uint32_t b = btnm->btn_id_sel;
498                 res        = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b);
499                 if(res != LV_RES_OK) return;
500             }
501         }
502 
503         /*Invalidate to old pressed area*/;
504         invalidate_button_area(obj, btnm->btn_id_sel);
505 
506     }
507     else if(code == LV_EVENT_LONG_PRESSED_REPEAT) {
508         if(btnm->btn_id_sel != LV_BTNMATRIX_BTN_NONE) {
509             if(button_is_repeat_disabled(btnm->ctrl_bits[btnm->btn_id_sel]) == false &&
510                button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel]) == false &&
511                button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) == false) {
512                 uint32_t b = btnm->btn_id_sel;
513                 res        = lv_event_send(obj, LV_EVENT_VALUE_CHANGED, &b);
514                 if(res != LV_RES_OK) return;
515             }
516         }
517     }
518     else if(code == LV_EVENT_PRESS_LOST) {
519         invalidate_button_area(obj, btnm->btn_id_sel);
520         btnm->btn_id_sel = LV_BTNMATRIX_BTN_NONE;
521     }
522     else if(code == LV_EVENT_FOCUSED) {
523         lv_indev_t * indev = lv_event_get_param(e);
524         lv_indev_type_t indev_type = lv_indev_get_type(indev);
525 
526         /*If not focused by an input device assume the last input device*/
527         if(indev == NULL) {
528             indev = lv_indev_get_next(NULL);
529             indev_type = lv_indev_get_type(indev);
530         }
531 
532         bool editing = lv_group_get_editing(lv_obj_get_group(obj));
533         /*Focus the first button if there is not selected button*/
534         if(btnm->btn_id_sel == LV_BTNMATRIX_BTN_NONE) {
535             if(indev_type == LV_INDEV_TYPE_KEYPAD || (indev_type == LV_INDEV_TYPE_ENCODER && editing)) {
536                 uint32_t b = 0;
537                 if(btnm->one_check) {
538                     while(button_is_hidden(btnm->ctrl_bits[b]) || button_is_inactive(btnm->ctrl_bits[b]) ||
539                           button_is_checked(btnm->ctrl_bits[b]) == false) b++;
540                 }
541                 else {
542                     while(button_is_hidden(btnm->ctrl_bits[b]) || button_is_inactive(btnm->ctrl_bits[b])) b++;
543                 }
544 
545                 btnm->btn_id_sel = b;
546             }
547             else {
548                 btnm->btn_id_sel = LV_BTNMATRIX_BTN_NONE;
549             }
550         }
551     }
552     else if(code == LV_EVENT_DEFOCUSED || code == LV_EVENT_LEAVE) {
553         if(btnm->btn_id_sel != LV_BTNMATRIX_BTN_NONE) invalidate_button_area(obj, btnm->btn_id_sel);
554         btnm->btn_id_sel = LV_BTNMATRIX_BTN_NONE;
555     }
556     else if(code == LV_EVENT_KEY) {
557 
558         invalidate_button_area(obj, btnm->btn_id_sel);
559 
560         char c = *((char *)lv_event_get_param(e));
561         if(c == LV_KEY_RIGHT) {
562             if(btnm->btn_id_sel == LV_BTNMATRIX_BTN_NONE)  btnm->btn_id_sel = 0;
563             else btnm->btn_id_sel++;
564             if(btnm->btn_id_sel >= btnm->btn_cnt) btnm->btn_id_sel = 0;
565 
566             while(button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) || button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel])) {
567                 btnm->btn_id_sel++;
568                 if(btnm->btn_id_sel >= btnm->btn_cnt) btnm->btn_id_sel = 0;
569             }
570         }
571         else if(c == LV_KEY_LEFT) {
572             if(btnm->btn_id_sel == LV_BTNMATRIX_BTN_NONE) btnm->btn_id_sel = 0;
573 
574             if(btnm->btn_id_sel == 0) btnm->btn_id_sel = btnm->btn_cnt - 1;
575             else if(btnm->btn_id_sel > 0) btnm->btn_id_sel--;
576 
577             while(button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) || button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel])) {
578                 if(btnm->btn_id_sel > 0) btnm->btn_id_sel--;
579                 else btnm->btn_id_sel = btnm->btn_cnt - 1;
580             }
581         }
582         else if(c == LV_KEY_DOWN) {
583             lv_coord_t col_gap = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
584             /*Find the area below the current*/
585             if(btnm->btn_id_sel == LV_BTNMATRIX_BTN_NONE) {
586                 btnm->btn_id_sel = 0;
587                 while(button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) || button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel])) {
588                     btnm->btn_id_sel++;
589                     if(btnm->btn_id_sel >= btnm->btn_cnt) btnm->btn_id_sel = 0;
590                 }
591             }
592             else {
593                 uint16_t area_below;
594                 lv_coord_t pr_center =
595                     btnm->button_areas[btnm->btn_id_sel].x1 + (lv_area_get_width(&btnm->button_areas[btnm->btn_id_sel]) >> 1);
596 
597                 for(area_below = btnm->btn_id_sel; area_below < btnm->btn_cnt; area_below++) {
598                     if(btnm->button_areas[area_below].y1 > btnm->button_areas[btnm->btn_id_sel].y1 &&
599                        pr_center >= btnm->button_areas[area_below].x1 &&
600                        pr_center <= btnm->button_areas[area_below].x2 + col_gap &&
601                        button_is_inactive(btnm->ctrl_bits[area_below]) == false &&
602                        button_is_hidden(btnm->ctrl_bits[area_below]) == false) {
603                         break;
604                     }
605                 }
606 
607                 if(area_below < btnm->btn_cnt) btnm->btn_id_sel = area_below;
608             }
609         }
610         else if(c == LV_KEY_UP) {
611             lv_coord_t col_gap = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
612             /*Find the area below the current*/
613             if(btnm->btn_id_sel == LV_BTNMATRIX_BTN_NONE) {
614                 btnm->btn_id_sel = 0;
615                 while(button_is_hidden(btnm->ctrl_bits[btnm->btn_id_sel]) || button_is_inactive(btnm->ctrl_bits[btnm->btn_id_sel])) {
616                     btnm->btn_id_sel++;
617                     if(btnm->btn_id_sel >= btnm->btn_cnt) btnm->btn_id_sel = 0;
618                 }
619             }
620             else {
621                 int16_t area_above;
622                 lv_coord_t pr_center =
623                     btnm->button_areas[btnm->btn_id_sel].x1 + (lv_area_get_width(&btnm->button_areas[btnm->btn_id_sel]) >> 1);
624 
625                 for(area_above = btnm->btn_id_sel; area_above >= 0; area_above--) {
626                     if(btnm->button_areas[area_above].y1 < btnm->button_areas[btnm->btn_id_sel].y1 &&
627                        pr_center >= btnm->button_areas[area_above].x1 - col_gap &&
628                        pr_center <= btnm->button_areas[area_above].x2 &&
629                        button_is_inactive(btnm->ctrl_bits[area_above]) == false &&
630                        button_is_hidden(btnm->ctrl_bits[area_above]) == false) {
631                         break;
632                     }
633                 }
634                 if(area_above >= 0) btnm->btn_id_sel = area_above;
635             }
636         }
637 
638         invalidate_button_area(obj, btnm->btn_id_sel);
639     }
640     else if(code == LV_EVENT_DRAW_MAIN) {
641         draw_main(e);
642     }
643 
644 }
645 
draw_main(lv_event_t * e)646 static void draw_main(lv_event_t * e)
647 {
648     lv_obj_t * obj = lv_event_get_target(e);
649     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
650     if(btnm->btn_cnt == 0) return;
651 
652     lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
653     obj->skip_trans = 1;
654 
655     lv_area_t area_obj;
656     lv_obj_get_coords(obj, &area_obj);
657 
658     lv_area_t btn_area;
659 
660     uint16_t btn_i = 0;
661     uint16_t txt_i = 0;
662 
663     lv_draw_rect_dsc_t draw_rect_dsc_act;
664     lv_draw_label_dsc_t draw_label_dsc_act;
665 
666     lv_draw_rect_dsc_t draw_rect_dsc_def;
667     lv_draw_label_dsc_t draw_label_dsc_def;
668 
669     lv_state_t state_ori = obj->state;
670     obj->state = LV_STATE_DEFAULT;
671     obj->skip_trans = 1;
672     lv_draw_rect_dsc_init(&draw_rect_dsc_def);
673     lv_draw_label_dsc_init(&draw_label_dsc_def);
674     lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &draw_rect_dsc_def);
675     lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &draw_label_dsc_def);
676     obj->skip_trans = 0;
677     obj->state = state_ori;
678 
679     lv_coord_t ptop = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
680     lv_coord_t pbottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
681     lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
682     lv_coord_t pright = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
683 
684 #if LV_USE_ARABIC_PERSIAN_CHARS
685     const size_t txt_ap_size = 256 ;
686     char * txt_ap = lv_mem_buf_get(txt_ap_size);
687 #endif
688 
689     lv_obj_draw_part_dsc_t part_draw_dsc;
690     lv_obj_draw_dsc_init(&part_draw_dsc, draw_ctx);
691     part_draw_dsc.part = LV_PART_ITEMS;
692     part_draw_dsc.class_p = MY_CLASS;
693     part_draw_dsc.type = LV_BTNMATRIX_DRAW_PART_BTN;
694     part_draw_dsc.rect_dsc = &draw_rect_dsc_act;
695     part_draw_dsc.label_dsc = &draw_label_dsc_act;
696 
697     for(btn_i = 0; btn_i < btnm->btn_cnt; btn_i++, txt_i++) {
698         /*Search the next valid text in the map*/
699         while(strcmp(btnm->map_p[txt_i], "\n") == 0) {
700             txt_i++;
701         }
702 
703         /*Skip hidden buttons*/
704         if(button_is_hidden(btnm->ctrl_bits[btn_i])) continue;
705 
706         /*Get the state of the button*/
707         lv_state_t btn_state = LV_STATE_DEFAULT;
708         if(button_get_checked(btnm->ctrl_bits[btn_i])) btn_state |= LV_STATE_CHECKED;
709 
710         if(button_is_inactive(btnm->ctrl_bits[btn_i])) btn_state |= LV_STATE_DISABLED;
711         else if(btn_i == btnm->btn_id_sel) {
712             if(state_ori & LV_STATE_PRESSED) btn_state |= LV_STATE_PRESSED;
713             if(state_ori & LV_STATE_FOCUSED) btn_state |= LV_STATE_FOCUSED;
714             if(state_ori & LV_STATE_FOCUS_KEY) btn_state |= LV_STATE_FOCUS_KEY;
715             if(state_ori & LV_STATE_EDITED) btn_state |= LV_STATE_EDITED;
716         }
717 
718         /*Get the button's area*/
719         lv_area_copy(&btn_area, &btnm->button_areas[btn_i]);
720         btn_area.x1 += area_obj.x1;
721         btn_area.y1 += area_obj.y1;
722         btn_area.x2 += area_obj.x1;
723         btn_area.y2 += area_obj.y1;
724 
725         /*Set up the draw descriptors*/
726         if(btn_state == LV_STATE_DEFAULT) {
727             lv_memcpy(&draw_rect_dsc_act, &draw_rect_dsc_def, sizeof(lv_draw_rect_dsc_t));
728             lv_memcpy(&draw_label_dsc_act, &draw_label_dsc_def, sizeof(lv_draw_label_dsc_t));
729         }
730         /*In other cases get the styles directly without caching them*/
731         else {
732             obj->state = btn_state;
733             obj->skip_trans = 1;
734             lv_draw_rect_dsc_init(&draw_rect_dsc_act);
735             lv_draw_label_dsc_init(&draw_label_dsc_act);
736             lv_obj_init_draw_rect_dsc(obj, LV_PART_ITEMS, &draw_rect_dsc_act);
737             lv_obj_init_draw_label_dsc(obj, LV_PART_ITEMS, &draw_label_dsc_act);
738             obj->state = state_ori;
739             obj->skip_trans = 0;
740         }
741 
742         bool recolor = button_is_recolor(btnm->ctrl_bits[btn_i]);
743         if(recolor) draw_label_dsc_act.flag |= LV_TEXT_FLAG_RECOLOR;
744         else draw_label_dsc_act.flag &= ~LV_TEXT_FLAG_RECOLOR;
745 
746 
747         part_draw_dsc.draw_area = &btn_area;
748         part_draw_dsc.id = btn_i;
749         lv_event_send(obj, LV_EVENT_DRAW_PART_BEGIN, &part_draw_dsc);
750 
751         /*Remove borders on the edges if `LV_BORDER_SIDE_INTERNAL`*/
752         if(draw_rect_dsc_act.border_side & LV_BORDER_SIDE_INTERNAL) {
753             draw_rect_dsc_act.border_side = LV_BORDER_SIDE_FULL;
754             if(btn_area.x1 == obj->coords.x1 + pleft) draw_rect_dsc_act.border_side &= ~LV_BORDER_SIDE_LEFT;
755             if(btn_area.x2 == obj->coords.x2 - pright) draw_rect_dsc_act.border_side &= ~LV_BORDER_SIDE_RIGHT;
756             if(btn_area.y1 == obj->coords.y1 + ptop) draw_rect_dsc_act.border_side &= ~LV_BORDER_SIDE_TOP;
757             if(btn_area.y2 == obj->coords.y2 - pbottom) draw_rect_dsc_act.border_side &= ~LV_BORDER_SIDE_BOTTOM;
758         }
759 
760         lv_coord_t btn_height = lv_area_get_height(&btn_area);
761 
762         if((btn_state & LV_STATE_PRESSED) && (btnm->ctrl_bits[btn_i] & LV_BTNMATRIX_CTRL_POPOVER)) {
763             /*Push up the upper boundary of the btn area to create the popover*/
764             btn_area.y1 -= btn_height;
765         }
766 
767         /*Draw the background*/
768         lv_draw_rect(draw_ctx, &draw_rect_dsc_act, &btn_area);
769 
770         /*Calculate the size of the text*/
771         const lv_font_t * font = draw_label_dsc_act.font;
772         lv_coord_t letter_space = draw_label_dsc_act.letter_space;
773         lv_coord_t line_space = draw_label_dsc_act.line_space;
774         const char * txt = btnm->map_p[txt_i];
775 
776 #if LV_USE_ARABIC_PERSIAN_CHARS
777         /*Get the size of the Arabic text and process it*/
778         size_t len_ap = _lv_txt_ap_calc_bytes_cnt(txt);
779         if(len_ap < txt_ap_size) {
780             _lv_txt_ap_proc(txt, txt_ap);
781             txt = txt_ap;
782         }
783 #endif
784         lv_point_t txt_size;
785         lv_txt_get_size(&txt_size, txt, font, letter_space,
786                         line_space, lv_area_get_width(&area_obj), draw_label_dsc_act.flag);
787 
788         btn_area.x1 += (lv_area_get_width(&btn_area) - txt_size.x) / 2;
789         btn_area.y1 += (lv_area_get_height(&btn_area) - txt_size.y) / 2;
790         btn_area.x2 = btn_area.x1 + txt_size.x;
791         btn_area.y2 = btn_area.y1 + txt_size.y;
792 
793         if((btn_state & LV_STATE_PRESSED) && (btnm->ctrl_bits[btn_i] & LV_BTNMATRIX_CTRL_POPOVER)) {
794             /*Push up the button text into the popover*/
795             btn_area.y1 -= btn_height / 2;
796             btn_area.y2 -= btn_height / 2;
797         }
798 
799         /*Draw the text*/
800         lv_draw_label(draw_ctx, &draw_label_dsc_act, &btn_area, txt, NULL);
801 
802         lv_event_send(obj, LV_EVENT_DRAW_PART_END, &part_draw_dsc);
803     }
804 
805     obj->skip_trans = 0;
806 #if LV_USE_ARABIC_PERSIAN_CHARS
807     lv_mem_buf_release(txt_ap);
808 #endif
809 }
810 /**
811  * Create the required number of buttons and control bytes according to a map
812  * @param obj pointer to button matrix object
813  * @param map_p pointer to a string array
814  */
allocate_btn_areas_and_controls(const lv_obj_t * obj,const char ** map)815 static void allocate_btn_areas_and_controls(const lv_obj_t * obj, const char ** map)
816 {
817     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
818     btnm->row_cnt = 1;
819     /*Count the buttons in the map*/
820     uint16_t btn_cnt = 0;
821     uint16_t i       = 0;
822     while(map[i] && map[i][0] != '\0') {
823         if(strcmp(map[i], "\n") != 0) { /*Do not count line breaks*/
824             btn_cnt++;
825         }
826         else {
827             btnm->row_cnt++;
828         }
829         i++;
830     }
831 
832     /*Do not allocate memory for the same amount of buttons*/
833     if(btn_cnt == btnm->btn_cnt) return;
834 
835     if(btnm->button_areas != NULL) {
836         lv_mem_free(btnm->button_areas);
837         btnm->button_areas = NULL;
838     }
839     if(btnm->ctrl_bits != NULL) {
840         lv_mem_free(btnm->ctrl_bits);
841         btnm->ctrl_bits = NULL;
842     }
843 
844     btnm->button_areas = lv_mem_alloc(sizeof(lv_area_t) * btn_cnt);
845     LV_ASSERT_MALLOC(btnm->button_areas);
846     btnm->ctrl_bits = lv_mem_alloc(sizeof(lv_btnmatrix_ctrl_t) * btn_cnt);
847     LV_ASSERT_MALLOC(btnm->ctrl_bits);
848     if(btnm->button_areas == NULL || btnm->ctrl_bits == NULL) btn_cnt = 0;
849 
850     lv_memset_00(btnm->ctrl_bits, sizeof(lv_btnmatrix_ctrl_t) * btn_cnt);
851 
852     btnm->btn_cnt = btn_cnt;
853 }
854 
855 /**
856  * Get the width of a button in units (default is 1).
857  * @param ctrl_bits least significant 3 bits used (1..7 valid values)
858  * @return the width of the button in units
859  */
get_button_width(lv_btnmatrix_ctrl_t ctrl_bits)860 static uint8_t get_button_width(lv_btnmatrix_ctrl_t ctrl_bits)
861 {
862     uint8_t w = ctrl_bits & LV_BTNMATRIX_WIDTH_MASK;
863     return w != 0 ? w : 1;
864 }
865 
button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits)866 static bool button_is_hidden(lv_btnmatrix_ctrl_t ctrl_bits)
867 {
868     return (ctrl_bits & LV_BTNMATRIX_CTRL_HIDDEN) ? true : false;
869 }
870 
button_is_checked(lv_btnmatrix_ctrl_t ctrl_bits)871 static bool button_is_checked(lv_btnmatrix_ctrl_t ctrl_bits)
872 {
873     return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKED) ? true : false;
874 }
875 
button_is_repeat_disabled(lv_btnmatrix_ctrl_t ctrl_bits)876 static bool button_is_repeat_disabled(lv_btnmatrix_ctrl_t ctrl_bits)
877 {
878     return (ctrl_bits & LV_BTNMATRIX_CTRL_NO_REPEAT) ? true : false;
879 }
880 
button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits)881 static bool button_is_inactive(lv_btnmatrix_ctrl_t ctrl_bits)
882 {
883     return (ctrl_bits & LV_BTNMATRIX_CTRL_DISABLED) ? true : false;
884 }
885 
button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits)886 static bool button_is_click_trig(lv_btnmatrix_ctrl_t ctrl_bits)
887 {
888     return (ctrl_bits & LV_BTNMATRIX_CTRL_CLICK_TRIG) ? true : false;
889 }
890 
button_is_popover(lv_btnmatrix_ctrl_t ctrl_bits)891 static bool button_is_popover(lv_btnmatrix_ctrl_t ctrl_bits)
892 {
893     return (ctrl_bits & LV_BTNMATRIX_CTRL_POPOVER) ? true : false;
894 }
895 
button_is_checkable(lv_btnmatrix_ctrl_t ctrl_bits)896 static bool button_is_checkable(lv_btnmatrix_ctrl_t ctrl_bits)
897 {
898     return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKABLE) ? true : false;
899 }
900 
button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits)901 static bool button_get_checked(lv_btnmatrix_ctrl_t ctrl_bits)
902 {
903     return (ctrl_bits & LV_BTNMATRIX_CTRL_CHECKED) ? true : false;
904 }
905 
button_is_recolor(lv_btnmatrix_ctrl_t ctrl_bits)906 static bool button_is_recolor(lv_btnmatrix_ctrl_t ctrl_bits)
907 {
908     return (ctrl_bits & LV_BTNMATRIX_CTRL_RECOLOR) ? true : false;
909 }
910 /**
911  * Gives the button id of a button under a given point
912  * @param obj pointer to a button matrix object
913  * @param p a point with absolute coordinates
914  * @return the id of the button or LV_BTNMATRIX_BTN_NONE.
915  */
get_button_from_point(lv_obj_t * obj,lv_point_t * p)916 static uint16_t get_button_from_point(lv_obj_t * obj, lv_point_t * p)
917 {
918     lv_area_t obj_cords;
919     lv_area_t btn_area;
920     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
921     uint16_t i;
922     lv_obj_get_coords(obj, &obj_cords);
923 
924     lv_coord_t w = lv_obj_get_width(obj);
925     lv_coord_t h = lv_obj_get_height(obj);
926     lv_coord_t pleft = lv_obj_get_style_pad_left(obj, LV_PART_MAIN);
927     lv_coord_t pright = lv_obj_get_style_pad_right(obj, LV_PART_MAIN);
928     lv_coord_t ptop = lv_obj_get_style_pad_top(obj, LV_PART_MAIN);
929     lv_coord_t pbottom = lv_obj_get_style_pad_bottom(obj, LV_PART_MAIN);
930     lv_coord_t prow = lv_obj_get_style_pad_row(obj, LV_PART_MAIN);
931     lv_coord_t pcol = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
932 
933     /*Get the half gap. Button look larger with this value. (+1 for rounding error)*/
934     prow = (prow / 2) + 1 + (prow & 1);
935     pcol = (pcol / 2) + 1 + (pcol & 1);
936 
937     prow = LV_MIN(prow, BTN_EXTRA_CLICK_AREA_MAX);
938     pcol = LV_MIN(pcol, BTN_EXTRA_CLICK_AREA_MAX);
939     pright = LV_MIN(pright, BTN_EXTRA_CLICK_AREA_MAX);
940     ptop = LV_MIN(ptop, BTN_EXTRA_CLICK_AREA_MAX);
941     pbottom = LV_MIN(pbottom, BTN_EXTRA_CLICK_AREA_MAX);
942 
943     for(i = 0; i < btnm->btn_cnt; i++) {
944         lv_area_copy(&btn_area, &btnm->button_areas[i]);
945         if(btn_area.x1 <= pleft) btn_area.x1 += obj_cords.x1 - LV_MIN(pleft, BTN_EXTRA_CLICK_AREA_MAX);
946         else btn_area.x1 += obj_cords.x1 - pcol;
947 
948         if(btn_area.y1 <= ptop) btn_area.y1 += obj_cords.y1 - LV_MIN(ptop, BTN_EXTRA_CLICK_AREA_MAX);
949         else btn_area.y1 += obj_cords.y1 - prow;
950 
951         if(btn_area.x2 >= w - pright - 2) btn_area.x2 += obj_cords.x1 + LV_MIN(pright,
952                                                                                    BTN_EXTRA_CLICK_AREA_MAX);  /*-2 for rounding error*/
953         else btn_area.x2 += obj_cords.x1 + pcol;
954 
955         if(btn_area.y2 >= h - pbottom - 2) btn_area.y2 += obj_cords.y1 + LV_MIN(pbottom,
956                                                                                     BTN_EXTRA_CLICK_AREA_MAX); /*-2 for rounding error*/
957         else btn_area.y2 += obj_cords.y1 + prow;
958 
959         if(_lv_area_is_point_on(&btn_area, p, 0) != false) {
960             break;
961         }
962     }
963 
964     if(i == btnm->btn_cnt) i = LV_BTNMATRIX_BTN_NONE;
965 
966     return i;
967 }
968 
invalidate_button_area(const lv_obj_t * obj,uint16_t btn_idx)969 static void invalidate_button_area(const lv_obj_t * obj, uint16_t btn_idx)
970 {
971     if(btn_idx == LV_BTNMATRIX_BTN_NONE) return;
972 
973     lv_area_t btn_area;
974     lv_area_t obj_area;
975 
976     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;;
977     if(btn_idx >= btnm->btn_cnt) return;
978 
979     lv_area_copy(&btn_area, &btnm->button_areas[btn_idx]);
980     lv_obj_get_coords(obj, &obj_area);
981 
982     /*The buttons might have outline and shadow so make the invalidation larger with the gaps between the buttons.
983      *It assumes that the outline or shadow is smaller than the gaps*/
984     lv_coord_t row_gap = lv_obj_get_style_pad_row(obj, LV_PART_MAIN);
985     lv_coord_t col_gap = lv_obj_get_style_pad_column(obj, LV_PART_MAIN);
986 
987     /*Be sure to have a minimal extra space if row/col_gap is small*/
988     lv_coord_t dpi = lv_disp_get_dpi(lv_obj_get_disp(obj));
989     row_gap = LV_MAX(row_gap, dpi / 10);
990     col_gap = LV_MAX(col_gap, dpi / 10);
991 
992     /*Convert relative coordinates to absolute*/
993     btn_area.x1 += obj_area.x1 - row_gap;
994     btn_area.y1 += obj_area.y1 - col_gap;
995     btn_area.x2 += obj_area.x1 + row_gap;
996     btn_area.y2 += obj_area.y1 + col_gap;
997 
998     if((btn_idx == btnm->btn_id_sel) && (btnm->ctrl_bits[btn_idx] & LV_BTNMATRIX_CTRL_POPOVER)) {
999         /*Push up the upper boundary of the btn area to also invalidate the popover*/
1000         btn_area.y1 -= lv_area_get_height(&btn_area);
1001     }
1002 
1003     lv_obj_invalidate_area(obj, &btn_area);
1004 }
1005 
1006 /**
1007  * Enforces a single button being toggled on the button matrix.
1008  * It simply clears the toggle flag on other buttons.
1009  * @param obj Button matrix object
1010  * @param btn_idx Button that should remain toggled
1011  */
make_one_button_checked(lv_obj_t * obj,uint16_t btn_idx)1012 static void make_one_button_checked(lv_obj_t * obj, uint16_t btn_idx)
1013 {
1014     /*Save whether the button was toggled*/
1015     bool was_toggled = lv_btnmatrix_has_btn_ctrl(obj, btn_idx, LV_BTNMATRIX_CTRL_CHECKED);
1016 
1017     lv_btnmatrix_clear_btn_ctrl_all(obj, LV_BTNMATRIX_CTRL_CHECKED);
1018 
1019     if(was_toggled) lv_btnmatrix_set_btn_ctrl(obj, btn_idx, LV_BTNMATRIX_CTRL_CHECKED);
1020 }
1021 
1022 /**
1023  * Check if any of the buttons in the first row has the LV_BTNMATRIX_CTRL_POPOVER control flag set.
1024  * @param obj Button matrix object
1025  * @return true if at least one button has the flag, false otherwise
1026  */
has_popovers_in_top_row(lv_obj_t * obj)1027 static bool has_popovers_in_top_row(lv_obj_t * obj)
1028 {
1029     lv_btnmatrix_t * btnm = (lv_btnmatrix_t *)obj;
1030 
1031     if(btnm->row_cnt <= 0) {
1032         return false;
1033     }
1034 
1035     const char ** map_row = btnm->map_p;
1036     uint16_t btn_cnt = 0;
1037 
1038     while(map_row[btn_cnt] && strcmp(map_row[btn_cnt], "\n") != 0 && map_row[btn_cnt][0] != '\0') {
1039         if(button_is_popover(btnm->ctrl_bits[btn_cnt])) {
1040             return true;
1041         }
1042         btn_cnt++;
1043     }
1044 
1045     return false;
1046 }
1047 
1048 #endif
1049