1 /**
2  * @file lv_btnm.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_btnm.h"
10 #if LV_USE_BTNM != 0
11 
12 #include "../lv_core/lv_group.h"
13 #include "../lv_draw/lv_draw.h"
14 #include "../lv_core/lv_refr.h"
15 #include "../lv_themes/lv_theme.h"
16 #include "../lv_misc/lv_txt.h"
17 
18 /*********************
19  *      DEFINES
20  *********************/
21 
22 /**********************
23  *      TYPEDEFS
24  **********************/
25 
26 /**********************
27  *  STATIC PROTOTYPES
28  **********************/
29 static lv_res_t lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param);
30 static bool lv_btnm_design(lv_obj_t * btnm, const lv_area_t * mask, lv_design_mode_t mode);
31 static uint8_t get_button_width(lv_btnm_ctrl_t ctrl_bits);
32 static bool button_is_hidden(lv_btnm_ctrl_t ctrl_bits);
33 static bool button_is_repeat_disabled(lv_btnm_ctrl_t ctrl_bits);
34 static bool button_is_inactive(lv_btnm_ctrl_t ctrl_bits);
35 static bool button_is_click_trig(lv_btnm_ctrl_t ctrl_bits);
36 static bool button_is_tgl_enabled(lv_btnm_ctrl_t ctrl_bits);
37 static bool button_get_tgl_state(lv_btnm_ctrl_t ctrl_bits);
38 static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p);
39 static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map);
40 static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx);
41 static bool maps_are_identical(const char ** map1, const char ** map2);
42 static void make_one_button_toggled(lv_obj_t * btnm, uint16_t btn_idx);
43 
44 /**********************
45  *  STATIC VARIABLES
46  **********************/
47 static const char * lv_btnm_def_map[] = {"Btn1", "Btn2", "Btn3", "\n", "Btn4", "Btn5", ""};
48 
49 static lv_design_cb_t ancestor_design_f;
50 static lv_signal_cb_t ancestor_signal;
51 
52 /**********************
53  *      MACROS
54  **********************/
55 
56 /**********************
57  *   GLOBAL FUNCTIONS
58  **********************/
59 
60 /**
61  * Create a button matrix objects
62  * @param par pointer to an object, it will be the parent of the new button matrix
63  * @param copy pointer to a button matrix object, if not NULL then the new object will be copied
64  * from it
65  * @return pointer to the created button matrix
66  */
lv_btnm_create(lv_obj_t * par,const lv_obj_t * copy)67 lv_obj_t * lv_btnm_create(lv_obj_t * par, const lv_obj_t * copy)
68 {
69     LV_LOG_TRACE("button matrix create started");
70 
71     /*Create the ancestor object*/
72     lv_obj_t * new_btnm = lv_obj_create(par, copy);
73     lv_mem_assert(new_btnm);
74     if(new_btnm == NULL) return NULL;
75 
76     if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(new_btnm);
77 
78     /*Allocate the object type specific extended data*/
79     lv_btnm_ext_t * ext = lv_obj_allocate_ext_attr(new_btnm, sizeof(lv_btnm_ext_t));
80     lv_mem_assert(ext);
81     if(ext == NULL) return NULL;
82 
83     ext->btn_cnt                          = 0;
84     ext->btn_id_pr                        = LV_BTNM_BTN_NONE;
85     ext->btn_id_act                       = LV_BTNM_BTN_NONE;
86     ext->button_areas                     = NULL;
87     ext->ctrl_bits                        = NULL;
88     ext->map_p                            = NULL;
89     ext->recolor                          = 0;
90     ext->one_toggle                       = 0;
91     ext->styles_btn[LV_BTN_STATE_REL]     = &lv_style_btn_rel;
92     ext->styles_btn[LV_BTN_STATE_PR]      = &lv_style_btn_pr;
93     ext->styles_btn[LV_BTN_STATE_TGL_REL] = &lv_style_btn_tgl_rel;
94     ext->styles_btn[LV_BTN_STATE_TGL_PR]  = &lv_style_btn_tgl_pr;
95     ext->styles_btn[LV_BTN_STATE_INA]     = &lv_style_btn_ina;
96 
97     if(ancestor_design_f == NULL) ancestor_design_f = lv_obj_get_design_cb(new_btnm);
98 
99     lv_obj_set_signal_cb(new_btnm, lv_btnm_signal);
100     lv_obj_set_design_cb(new_btnm, lv_btnm_design);
101 
102     /*Init the new button matrix object*/
103     if(copy == NULL) {
104         lv_obj_set_size(new_btnm, LV_DPI * 3, LV_DPI * 2);
105         lv_btnm_set_map(new_btnm, lv_btnm_def_map);
106 
107         /*Set the default styles*/
108         lv_theme_t * th = lv_theme_get_current();
109         if(th) {
110             lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BG, th->style.btnm.bg);
111             lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_REL, th->style.btnm.btn.rel);
112             lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_PR, th->style.btnm.btn.pr);
113             lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_TGL_REL, th->style.btnm.btn.tgl_rel);
114             lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_TGL_PR, th->style.btnm.btn.tgl_pr);
115             lv_btnm_set_style(new_btnm, LV_BTNM_STYLE_BTN_INA, th->style.btnm.btn.ina);
116         } else {
117             lv_obj_set_style(new_btnm, &lv_style_pretty);
118         }
119     }
120     /*Copy an existing object*/
121     else {
122         lv_btnm_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
123         memcpy(ext->styles_btn, copy_ext->styles_btn, sizeof(ext->styles_btn));
124         lv_btnm_set_map(new_btnm, lv_btnm_get_map_array(copy));
125     }
126 
127     LV_LOG_INFO("button matrix created");
128 
129     return new_btnm;
130 }
131 
132 /*=====================
133  * Setter functions
134  *====================*/
135 
136 /**
137  * Set a new map. Buttons will be created/deleted according to the map. The
138  * button matrix keeps a reference to the map and so the string array must not
139  * be deallocated during the life of the matrix.
140  * @param btnm pointer to a button matrix object
141  * @param map pointer a string array. The last string has to be: "". Use "\n" to make a line break.
142  */
lv_btnm_set_map(const lv_obj_t * btnm,const char * map[])143 void lv_btnm_set_map(const lv_obj_t * btnm, const char * map[])
144 {
145     if(map == NULL) return;
146 
147     /*
148      * lv_btnm_set_map is called on receipt of signals such as
149      * LV_SIGNAL_CORD_CHG regardless of whether the map has changed (e.g.
150      * calling lv_obj_align on the map will trigger this).
151      *
152      * We check if the map has changed here to avoid overwriting changes made
153      * to hidden/longpress/disabled states after the map was originally set.
154      *
155      * TODO: separate all map set/allocation from layout code below and skip
156      * set/allocation when map hasn't changed.
157      */
158     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
159     if(!maps_are_identical(ext->map_p, map)) {
160 
161         /*Analyze the map and create the required number of buttons*/
162         allocate_btn_areas_and_controls(btnm, map);
163     }
164     ext->map_p = map;
165 
166     /*Set size and positions of the buttons*/
167     const lv_style_t * style_bg = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
168     lv_coord_t max_w            = lv_obj_get_width(btnm) - style_bg->body.padding.left - style_bg->body.padding.right;
169     lv_coord_t max_h            = lv_obj_get_height(btnm) - style_bg->body.padding.top - style_bg->body.padding.bottom;
170     lv_coord_t act_y            = style_bg->body.padding.top;
171 
172     /*Count the lines to calculate button height*/
173     uint8_t line_cnt = 1;
174     uint8_t li;
175     for(li = 0; strlen(map[li]) != 0; li++) {
176         if(strcmp(map[li], "\n") == 0) line_cnt++;
177     }
178 
179     lv_coord_t btn_h = max_h - ((line_cnt - 1) * style_bg->body.padding.inner);
180     btn_h            = btn_h / line_cnt;
181     btn_h--; /*-1 because e.g. height = 100 means 101 pixels (0..100)*/
182 
183     /* Count the units and the buttons in a line
184      * (A button can be 1,2,3... unit wide)*/
185     uint16_t unit_cnt;           /*Number of units in a row*/
186     uint16_t unit_act_cnt;       /*Number of units currently put in a row*/
187     uint16_t btn_cnt;            /*Number of buttons in a row*/
188     uint16_t i_tot          = 0; /*Act. index in the str map*/
189     uint16_t btn_i          = 0; /*Act. index of button areas*/
190     const char ** map_p_tmp = map;
191 
192     /*Count the units and the buttons in a line*/
193     while(1) {
194         unit_cnt = 0;
195         btn_cnt  = 0;
196         /*Count the buttons in a line*/
197         while(strcmp(map_p_tmp[btn_cnt], "\n") != 0 && strlen(map_p_tmp[btn_cnt]) != 0) { /*Check a line*/
198             unit_cnt += get_button_width(ext->ctrl_bits[btn_i + btn_cnt]);
199             btn_cnt++;
200         }
201 
202         /*Make sure the last row is at the bottom of 'btnm'*/
203         if(map_p_tmp[btn_cnt][0] == '\0') { /*Last row?*/
204             btn_h = max_h - act_y + style_bg->body.padding.bottom - 1;
205         }
206 
207         /*Only deal with the non empty lines*/
208         if(btn_cnt != 0) {
209             /*Calculate the width of all units*/
210             lv_coord_t all_unit_w = max_w - ((btn_cnt - 1) * style_bg->body.padding.inner);
211 
212             /*Set the button size and positions and set the texts*/
213             uint16_t i;
214             lv_coord_t act_x = style_bg->body.padding.left;
215             lv_coord_t act_unit_w;
216             unit_act_cnt = 0;
217             for(i = 0; i < btn_cnt; i++) {
218                 /* one_unit_w = all_unit_w / unit_cnt
219                  * act_unit_w = one_unit_w * button_width
220                  * do this two operations but the multiply first to divide a greater number */
221                 act_unit_w = (all_unit_w * get_button_width(ext->ctrl_bits[btn_i])) / unit_cnt;
222                 act_unit_w--; /*-1 because e.g. width = 100 means 101 pixels (0..100)*/
223 
224                 /*Always recalculate act_x because of rounding errors */
225                 act_x = (unit_act_cnt * all_unit_w) / unit_cnt + i * style_bg->body.padding.inner +
226                         style_bg->body.padding.left;
227 
228                 /* Set the button's area.
229                  * If inner padding is zero then use the prev. button x2 as x1 to avoid rounding
230                  * errors*/
231                 if(style_bg->body.padding.inner == 0 && act_x != style_bg->body.padding.left) {
232                     lv_area_set(&ext->button_areas[btn_i], ext->button_areas[btn_i - 1].x2, act_y, act_x + act_unit_w,
233                                 act_y + btn_h);
234                 } else {
235                     lv_area_set(&ext->button_areas[btn_i], act_x, act_y, act_x + act_unit_w, act_y + btn_h);
236                 }
237 
238                 unit_act_cnt += get_button_width(ext->ctrl_bits[btn_i]);
239 
240                 i_tot++;
241                 btn_i++;
242             }
243         }
244         act_y += btn_h + style_bg->body.padding.inner;
245 
246         if(strlen(map_p_tmp[btn_cnt]) == 0) break; /*Break on end of map*/
247         map_p_tmp = &map_p_tmp[btn_cnt + 1];       /*Set the map to the next line*/
248         i_tot++;                                   /*Skip the '\n'*/
249     }
250 
251     lv_obj_invalidate(btnm);
252 }
253 
254 /**
255  * Set the button control map (hidden, disabled etc.) for a button matrix. The
256  * control map array will be copied and so may be deallocated after this
257  * function returns.
258  * @param btnm pointer to a button matrix object
259  * @param ctrl_map pointer to an array of `lv_btn_ctrl_t` control bytes. The
260  *                 length of the array and position of the elements must match
261  *                 the number and order of the individual buttons (i.e. excludes
262  *                 newline entries).
263  *                 An element of the map should look like e.g.:
264  *                 `ctrl_map[0] = width | LV_BTNM_CTRL_NO_REPEAT |  LV_BTNM_CTRL_TGL_ENABLE`
265  */
lv_btnm_set_ctrl_map(const lv_obj_t * btnm,const lv_btnm_ctrl_t ctrl_map[])266 void lv_btnm_set_ctrl_map(const lv_obj_t * btnm, const lv_btnm_ctrl_t ctrl_map[])
267 {
268     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
269     memcpy(ext->ctrl_bits, ctrl_map, sizeof(lv_btnm_ctrl_t) * ext->btn_cnt);
270 
271     lv_btnm_set_map(btnm, ext->map_p);
272 }
273 
274 /**
275  * Set the pressed button i.e. visually highlight it.
276  * Mainly used a when the btnm is in a group to show the selected button
277  * @param btnm pointer to button matrix object
278  * @param id index of the currently pressed button (`LV_BTNM_BTN_NONE` to unpress)
279  */
lv_btnm_set_pressed(const lv_obj_t * btnm,uint16_t id)280 void lv_btnm_set_pressed(const lv_obj_t * btnm, uint16_t id)
281 {
282     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
283 
284     if(id >= ext->btn_cnt && id != LV_BTNM_BTN_NONE) return;
285 
286     if(id == ext->btn_id_pr) return;
287 
288     ext->btn_id_pr = id;
289     lv_obj_invalidate(btnm);
290 }
291 
292 /**
293  * Set a style of a button matrix
294  * @param btnm pointer to a button matrix object
295  * @param type which style should be set
296  * @param style pointer to a style
297  */
lv_btnm_set_style(lv_obj_t * btnm,lv_btnm_style_t type,const lv_style_t * style)298 void lv_btnm_set_style(lv_obj_t * btnm, lv_btnm_style_t type, const lv_style_t * style)
299 {
300     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
301 
302     switch(type) {
303         case LV_BTNM_STYLE_BG: lv_obj_set_style(btnm, style); break;
304         case LV_BTNM_STYLE_BTN_REL:
305             ext->styles_btn[LV_BTN_STATE_REL] = style;
306             lv_obj_invalidate(btnm);
307             break;
308         case LV_BTNM_STYLE_BTN_PR:
309             ext->styles_btn[LV_BTN_STATE_PR] = style;
310             lv_obj_invalidate(btnm);
311             break;
312         case LV_BTNM_STYLE_BTN_TGL_REL:
313             ext->styles_btn[LV_BTN_STATE_TGL_REL] = style;
314             lv_obj_invalidate(btnm);
315             break;
316         case LV_BTNM_STYLE_BTN_TGL_PR:
317             ext->styles_btn[LV_BTN_STATE_TGL_PR] = style;
318             lv_obj_invalidate(btnm);
319             break;
320         case LV_BTNM_STYLE_BTN_INA:
321             ext->styles_btn[LV_BTN_STATE_INA] = style;
322             lv_obj_invalidate(btnm);
323             break;
324     }
325 }
326 
327 /**
328  * Enable recoloring of button's texts
329  * @param btnm pointer to button matrix object
330  * @param en true: enable recoloring; false: disable
331  */
lv_btnm_set_recolor(const lv_obj_t * btnm,bool en)332 void lv_btnm_set_recolor(const lv_obj_t * btnm, bool en)
333 {
334     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
335 
336     ext->recolor = en;
337     lv_obj_invalidate(btnm);
338 }
339 
340 /**
341  * Set the attributes of a button of the button matrix
342  * @param btnm pointer to button matrix object
343  * @param btn_id 0 based index of the button to modify. (Not counting new lines)
344  */
lv_btnm_set_btn_ctrl(const lv_obj_t * btnm,uint16_t btn_id,lv_btnm_ctrl_t ctrl)345 void lv_btnm_set_btn_ctrl(const lv_obj_t * btnm, uint16_t btn_id, lv_btnm_ctrl_t ctrl)
346 {
347     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
348 
349     if(btn_id >= ext->btn_cnt) return;
350 
351     ext->ctrl_bits[btn_id] |= ctrl;
352     invalidate_button_area(btnm, btn_id);
353 }
354 
355 /**
356  * Clear the attributes of a button of the button matrix
357  * @param btnm pointer to button matrix object
358  * @param btn_id 0 based index of the button to modify. (Not counting new lines)
359  */
lv_btnm_clear_btn_ctrl(const lv_obj_t * btnm,uint16_t btn_id,lv_btnm_ctrl_t ctrl)360 void lv_btnm_clear_btn_ctrl(const lv_obj_t * btnm, uint16_t btn_id, lv_btnm_ctrl_t ctrl)
361 {
362     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
363 
364     if(btn_id >= ext->btn_cnt) return;
365 
366     ext->ctrl_bits[btn_id] &= (~ctrl);
367     invalidate_button_area(btnm, btn_id);
368 }
369 
370 /**
371  * Set the attributes of all buttons of a button matrix
372  * @param btnm pointer to a button matrix object
373  * @param ctrl attribute(s) to set from `lv_btnm_ctrl_t`. Values can be ORed.
374  */
lv_btnm_set_btn_ctrl_all(lv_obj_t * btnm,lv_btnm_ctrl_t ctrl)375 void lv_btnm_set_btn_ctrl_all(lv_obj_t * btnm, lv_btnm_ctrl_t ctrl)
376 {
377     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
378     uint16_t i;
379     for(i = 0; i < ext->btn_cnt; i++) {
380         lv_btnm_set_btn_ctrl(btnm, i, ctrl);
381     }
382 }
383 
384 /**
385  * Clear the attributes of all buttons of a button matrix
386  * @param btnm pointer to a button matrix object
387  * @param ctrl attribute(s) to set from `lv_btnm_ctrl_t`. Values can be ORed.
388  * @param en true: set the attributes; false: clear the attributes
389  */
lv_btnm_clear_btn_ctrl_all(lv_obj_t * btnm,lv_btnm_ctrl_t ctrl)390 void lv_btnm_clear_btn_ctrl_all(lv_obj_t * btnm, lv_btnm_ctrl_t ctrl)
391 {
392     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
393     uint16_t i;
394     for(i = 0; i < ext->btn_cnt; i++) {
395         lv_btnm_clear_btn_ctrl(btnm, i, ctrl);
396     }
397 }
398 
399 /**
400  * Set a single buttons relative width.
401  * This method will cause the matrix be regenerated and is a relatively
402  * expensive operation. It is recommended that initial width be specified using
403  * `lv_btnm_set_ctrl_map` and this method only be used for dynamic changes.
404  * @param btnm pointer to button matrix object
405  * @param btn_id 0 based index of the button to modify.
406  * @param width Relative width compared to the buttons in the same row. [1..7]
407  */
lv_btnm_set_btn_width(const lv_obj_t * btnm,uint16_t btn_id,uint8_t width)408 void lv_btnm_set_btn_width(const lv_obj_t * btnm, uint16_t btn_id, uint8_t width)
409 {
410 
411     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
412     if(btn_id >= ext->btn_cnt) return;
413     ext->ctrl_bits[btn_id] &= (~LV_BTNM_WIDTH_MASK);
414     ext->ctrl_bits[btn_id] |= (LV_BTNM_WIDTH_MASK & width);
415 
416     lv_btnm_set_map(btnm, ext->map_p);
417 }
418 
419 /**
420  * Make the button matrix like a selector widget (only one button may be toggled at a time).
421  *
422  * Toggling must be enabled on the buttons you want to be selected with `lv_btnm_set_ctrl` or
423  * `lv_btnm_set_btn_ctrl_all`.
424  *
425  * @param btnm Button matrix object
426  * @param one_toggle Whether "one toggle" mode is enabled
427  */
lv_btnm_set_one_toggle(lv_obj_t * btnm,bool one_toggle)428 void lv_btnm_set_one_toggle(lv_obj_t * btnm, bool one_toggle)
429 {
430     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
431     ext->one_toggle     = one_toggle;
432 
433     /*If more than one button is toggled only the first one should be*/
434     make_one_button_toggled(btnm, 0);
435 }
436 
437 /*=====================
438  * Getter functions
439  *====================*/
440 
441 /**
442  * Get the current map of a button matrix
443  * @param btnm pointer to a button matrix object
444  * @return the current map
445  */
lv_btnm_get_map_array(const lv_obj_t * btnm)446 const char ** lv_btnm_get_map_array(const lv_obj_t * btnm)
447 {
448     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
449     return ext->map_p;
450 }
451 
452 /**
453  * Check whether the button's text can use recolor or not
454  * @param btnm pointer to button matrix object
455  * @return true: text recolor enable; false: disabled
456  */
lv_btnm_get_recolor(const lv_obj_t * btnm)457 bool lv_btnm_get_recolor(const lv_obj_t * btnm)
458 {
459     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
460 
461     return ext->recolor;
462 }
463 
464 /**
465  * Get the index of the lastly "activated" button by the user (pressed, released etc)
466  * Useful in the the `event_cb` to get the text of the button, check if hidden etc.
467  * @param btnm pointer to button matrix object
468  * @return  index of the last released button (LV_BTNM_BTN_NONE: if unset)
469  */
lv_btnm_get_active_btn(const lv_obj_t * btnm)470 uint16_t lv_btnm_get_active_btn(const lv_obj_t * btnm)
471 {
472     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
473     return ext->btn_id_act;
474 }
475 
476 /**
477  * Get the text of the lastly "activated" button by the user (pressed, released etc)
478  * Useful in the the `event_cb`
479  * @param btnm pointer to button matrix object
480  * @return text of the last released button (NULL: if unset)
481  */
lv_btnm_get_active_btn_text(const lv_obj_t * btnm)482 const char * lv_btnm_get_active_btn_text(const lv_obj_t * btnm)
483 {
484     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
485     if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
486         return lv_btnm_get_btn_text(btnm, ext->btn_id_act);
487     } else {
488         return NULL;
489     }
490 }
491 
492 /**
493  * Get the pressed button's index.
494  * The button be really pressed by the user or manually set to pressed with `lv_btnm_set_pressed`
495  * @param btnm pointer to button matrix object
496  * @return  index of the pressed button (LV_BTNM_BTN_NONE: if unset)
497  */
lv_btnm_get_pressed_btn(const lv_obj_t * btnm)498 uint16_t lv_btnm_get_pressed_btn(const lv_obj_t * btnm)
499 {
500     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
501     return ext->btn_id_pr;
502 }
503 
504 /**
505  * Get the button's text
506  * @param btnm pointer to button matrix object
507  * @param btn_id the index a button not counting new line characters. (The return value of
508  * lv_btnm_get_pressed/released)
509  * @return  text of btn_index` button
510  */
lv_btnm_get_btn_text(const lv_obj_t * btnm,uint16_t btn_id)511 const char * lv_btnm_get_btn_text(const lv_obj_t * btnm, uint16_t btn_id)
512 {
513     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
514     if(btn_id > ext->btn_cnt) return NULL;
515 
516     uint16_t txt_i = 0;
517     uint16_t btn_i = 0;
518 
519     /* Search the text of ext->btn_pr the buttons text in the map
520      * Skip "\n"-s*/
521     while(btn_i != btn_id) {
522         btn_i++;
523         txt_i++;
524         if(strcmp(ext->map_p[txt_i], "\n") == 0) txt_i++;
525     }
526 
527     if(btn_i == ext->btn_cnt) return NULL;
528 
529     return ext->map_p[txt_i];
530 }
531 
532 /**
533  * Get the whether a control value is enabled or disabled for button of a button matrix
534  * @param btnm pointer to a button matrix object
535  * @param btn_id the index a button not counting new line characters. (E.g. the return value of
536  * lv_btnm_get_pressed/released)
537  * @param ctrl control values to check (ORed value can be used)
538  * @return true: long press repeat is disabled; false: long press repeat enabled
539  */
lv_btnm_get_btn_ctrl(lv_obj_t * btnm,uint16_t btn_id,lv_btnm_ctrl_t ctrl)540 bool lv_btnm_get_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnm_ctrl_t ctrl)
541 {
542     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
543     if(btn_id >= ext->btn_cnt) return false;
544 
545     return ext->ctrl_bits[btn_id] & ctrl ? true : false;
546 }
547 
548 /**
549  * Get a style of a button matrix
550  * @param btnm pointer to a button matrix object
551  * @param type which style should be get
552  * @return style pointer to a style
553  */
lv_btnm_get_style(const lv_obj_t * btnm,lv_btnm_style_t type)554 const lv_style_t * lv_btnm_get_style(const lv_obj_t * btnm, lv_btnm_style_t type)
555 {
556     const lv_style_t * style = NULL;
557     lv_btnm_ext_t * ext      = lv_obj_get_ext_attr(btnm);
558 
559     switch(type) {
560         case LV_BTNM_STYLE_BG: style = lv_obj_get_style(btnm); break;
561         case LV_BTNM_STYLE_BTN_REL: style = ext->styles_btn[LV_BTN_STATE_REL]; break;
562         case LV_BTNM_STYLE_BTN_PR: style = ext->styles_btn[LV_BTN_STATE_PR]; break;
563         case LV_BTNM_STYLE_BTN_TGL_REL: style = ext->styles_btn[LV_BTN_STATE_TGL_REL]; break;
564         case LV_BTNM_STYLE_BTN_TGL_PR: style = ext->styles_btn[LV_BTN_STATE_TGL_PR]; break;
565         case LV_BTNM_STYLE_BTN_INA: style = ext->styles_btn[LV_BTN_STATE_INA]; break;
566         default: style = NULL; break;
567     }
568 
569     return style;
570 }
571 
572 /**
573  * Find whether "one toggle" mode is enabled.
574  * @param btnm Button matrix object
575  * @return whether "one toggle" mode is enabled
576  */
lv_btnm_get_one_toggle(const lv_obj_t * btnm)577 bool lv_btnm_get_one_toggle(const lv_obj_t * btnm)
578 {
579     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
580 
581     return ext->one_toggle;
582 }
583 
584 /**********************
585  *   STATIC FUNCTIONS
586  **********************/
587 
588 /**
589  * Handle the drawing related tasks of the button matrixs
590  * @param btnm pointer to a button matrix object
591  * @param mask the object will be drawn only in this area
592  * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
593  *                                  (return 'true' if yes)
594  *             LV_DESIGN_DRAW: draw the object (always return 'true')
595  *             LV_DESIGN_DRAW_POST: drawing after every children are drawn
596  * @param return true/false, depends on 'mode'
597  */
lv_btnm_design(lv_obj_t * btnm,const lv_area_t * mask,lv_design_mode_t mode)598 static bool lv_btnm_design(lv_obj_t * btnm, const lv_area_t * mask, lv_design_mode_t mode)
599 {
600     if(mode == LV_DESIGN_COVER_CHK) {
601         return ancestor_design_f(btnm, mask, mode);
602         /*Return false if the object is not covers the mask_p area*/
603     }
604     /*Draw the object*/
605     else if(mode == LV_DESIGN_DRAW_MAIN) {
606 
607         ancestor_design_f(btnm, mask, mode);
608 
609         lv_btnm_ext_t * ext         = lv_obj_get_ext_attr(btnm);
610         const lv_style_t * bg_style = lv_obj_get_style(btnm);
611         const lv_style_t * btn_style;
612         lv_opa_t opa_scale = lv_obj_get_opa_scale(btnm);
613 
614         lv_area_t area_btnm;
615         lv_obj_get_coords(btnm, &area_btnm);
616 
617         lv_area_t area_tmp;
618         lv_coord_t btn_w;
619         lv_coord_t btn_h;
620 
621         uint16_t btn_i = 0;
622         uint16_t txt_i = 0;
623         lv_style_t style_tmp;
624         lv_txt_flag_t txt_flag = LV_TXT_FLAG_NONE;
625 
626         if(ext->recolor) txt_flag = LV_TXT_FLAG_RECOLOR;
627 
628         for(btn_i = 0; btn_i < ext->btn_cnt; btn_i++, txt_i++) {
629             /*Search the next valid text in the map*/
630             while(strcmp(ext->map_p[txt_i], "\n") == 0) {
631                 txt_i++;
632             }
633 
634             /*Skip hidden buttons*/
635             if(button_is_hidden(ext->ctrl_bits[btn_i])) continue;
636 
637             lv_area_copy(&area_tmp, &ext->button_areas[btn_i]);
638             area_tmp.x1 += area_btnm.x1;
639             area_tmp.y1 += area_btnm.y1;
640             area_tmp.x2 += area_btnm.x1;
641             area_tmp.y2 += area_btnm.y1;
642 
643             btn_w = lv_area_get_width(&area_tmp);
644             btn_h = lv_area_get_height(&area_tmp);
645 
646             /*Load the style*/
647             bool tgl_state = button_get_tgl_state(ext->ctrl_bits[btn_i]);
648             if(button_is_inactive(ext->ctrl_bits[btn_i]))
649                 btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_INA);
650             else if(btn_i != ext->btn_id_pr && tgl_state == false)
651                 btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_REL);
652             else if(btn_i == ext->btn_id_pr && tgl_state == false)
653                 btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_PR);
654             else if(btn_i != ext->btn_id_pr && tgl_state == true)
655                 btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_TGL_REL);
656             else if(btn_i == ext->btn_id_pr && tgl_state == true)
657                 btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_TGL_PR);
658             else
659                 btn_style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BTN_REL); /*Not possible option, just to be sure*/
660 
661             lv_style_copy(&style_tmp, btn_style);
662 
663             /*Remove borders on the edges if `LV_BORDER_INTERNAL`*/
664             if(style_tmp.body.border.part & LV_BORDER_INTERNAL) {
665                 if(area_tmp.y1 == btnm->coords.y1 + bg_style->body.padding.top) {
666                     style_tmp.body.border.part &= ~LV_BORDER_TOP;
667                 }
668                 if(area_tmp.y2 == btnm->coords.y2 - bg_style->body.padding.bottom) {
669                     style_tmp.body.border.part &= ~LV_BORDER_BOTTOM;
670                 }
671 
672                 if(txt_i == 0) {
673                     style_tmp.body.border.part &= ~LV_BORDER_LEFT;
674                 } else if(strcmp(ext->map_p[txt_i - 1], "\n") == 0) {
675                     style_tmp.body.border.part &= ~LV_BORDER_LEFT;
676                 }
677 
678                 if(ext->map_p[txt_i + 1][0] == '\0' || strcmp(ext->map_p[txt_i + 1], "\n") == 0) {
679                     style_tmp.body.border.part &= ~LV_BORDER_RIGHT;
680                 }
681             }
682             lv_draw_rect(&area_tmp, mask, &style_tmp, opa_scale);
683 
684             /*Calculate the size of the text*/
685             if(btn_style->glass) btn_style = bg_style;
686             const lv_font_t * font = btn_style->text.font;
687             lv_point_t txt_size;
688             lv_txt_get_size(&txt_size, ext->map_p[txt_i], font, btn_style->text.letter_space,
689                             btn_style->text.line_space, lv_area_get_width(&area_btnm), txt_flag);
690 
691             area_tmp.x1 += (btn_w - txt_size.x) / 2;
692             area_tmp.y1 += (btn_h - txt_size.y) / 2;
693             area_tmp.x2 = area_tmp.x1 + txt_size.x;
694             area_tmp.y2 = area_tmp.y1 + txt_size.y;
695 
696             lv_draw_label(&area_tmp, mask, btn_style, opa_scale, ext->map_p[txt_i], txt_flag, NULL, -1, -1, NULL);
697         }
698     }
699     return true;
700 }
701 
702 /**
703  * Signal function of the button matrix
704  * @param btnm pointer to a button matrix object
705  * @param sign a signal type from lv_signal_t enum
706  * @param param pointer to a signal specific variable
707  * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
708  */
lv_btnm_signal(lv_obj_t * btnm,lv_signal_t sign,void * param)709 static lv_res_t lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param)
710 {
711     lv_res_t res;
712 
713     /* Include the ancient signal function */
714     res = ancestor_signal(btnm, sign, param);
715     if(res != LV_RES_OK) return res;
716 
717     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
718     lv_point_t p;
719     if(sign == LV_SIGNAL_CLEANUP) {
720         lv_mem_free(ext->button_areas);
721         lv_mem_free(ext->ctrl_bits);
722     } else if(sign == LV_SIGNAL_STYLE_CHG || sign == LV_SIGNAL_CORD_CHG) {
723         lv_btnm_set_map(btnm, ext->map_p);
724     } else if(sign == LV_SIGNAL_PRESSED) {
725         lv_indev_t * indev = lv_indev_get_act();
726         if(lv_indev_get_type(indev) == LV_INDEV_TYPE_POINTER || lv_indev_get_type(indev) == LV_INDEV_TYPE_BUTTON) {
727             uint16_t btn_pr;
728             /*Search the pressed area*/
729             lv_indev_get_point(param, &p);
730             btn_pr = get_button_from_point(btnm, &p);
731 
732             invalidate_button_area(btnm, ext->btn_id_pr) /*Invalidate the old area*/;
733             ext->btn_id_pr  = btn_pr;
734             ext->btn_id_act = btn_pr;
735             invalidate_button_area(btnm, ext->btn_id_pr); /*Invalidate the new area*/
736         }
737         if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
738             if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == false &&
739                button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
740                button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
741                 uint32_t b = ext->btn_id_act;
742                 res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
743             }
744         }
745     } else if(sign == LV_SIGNAL_PRESSING) {
746         uint16_t btn_pr;
747         /*Search the pressed area*/
748         lv_indev_get_point(param, &p);
749         btn_pr = get_button_from_point(btnm, &p);
750         /*Invalidate to old and the new areas*/;
751         if(btn_pr != ext->btn_id_pr) {
752             lv_indev_reset_long_press(param); /*Start the log press time again on the new button*/
753             if(ext->btn_id_pr != LV_BTNM_BTN_NONE) {
754                 invalidate_button_area(btnm, ext->btn_id_pr);
755             }
756             if(btn_pr != LV_BTNM_BTN_NONE) {
757                 uint32_t b = ext->btn_id_act;
758                 res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
759                 if(res == LV_RES_OK) {
760                     invalidate_button_area(btnm, btn_pr);
761                 }
762             }
763         }
764 
765         ext->btn_id_pr  = btn_pr;
766         ext->btn_id_act = btn_pr;
767     } else if(sign == LV_SIGNAL_RELEASED) {
768         if(ext->btn_id_pr != LV_BTNM_BTN_NONE) {
769             /*Toggle the button if enabled*/
770             if(button_is_tgl_enabled(ext->ctrl_bits[ext->btn_id_pr])) {
771                 if(button_get_tgl_state(ext->ctrl_bits[ext->btn_id_pr])) {
772                     ext->ctrl_bits[ext->btn_id_pr] &= (~LV_BTNM_CTRL_TGL_STATE);
773                 } else {
774                     ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNM_CTRL_TGL_STATE;
775                 }
776                 if(ext->one_toggle) make_one_button_toggled(btnm, ext->btn_id_pr);
777             }
778 
779             /*Invalidate to old pressed area*/;
780             invalidate_button_area(btnm, ext->btn_id_pr);
781 
782 #if LV_USE_GROUP
783             /*Leave the clicked button when releases if this not the focused object in a group*/
784             lv_group_t * g = lv_obj_get_group(btnm);
785             if(lv_group_get_focused(g) != btnm) {
786                 ext->btn_id_pr = LV_BTNM_BTN_NONE;
787             }
788 #else
789             ext->btn_id_pr = LV_BTNM_BTN_NONE;
790 #endif
791 
792             if(button_is_click_trig(ext->ctrl_bits[ext->btn_id_act]) == true &&
793                button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
794                button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
795                 uint32_t b = ext->btn_id_act;
796                 res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
797             }
798         }
799     } else if(sign == LV_SIGNAL_LONG_PRESS_REP) {
800         if(ext->btn_id_act != LV_BTNM_BTN_NONE) {
801             if(button_is_repeat_disabled(ext->ctrl_bits[ext->btn_id_act]) == false &&
802                button_is_inactive(ext->ctrl_bits[ext->btn_id_act]) == false &&
803                button_is_hidden(ext->ctrl_bits[ext->btn_id_act]) == false) {
804                 uint32_t b = ext->btn_id_act;
805                 res        = lv_event_send(btnm, LV_EVENT_VALUE_CHANGED, &b);
806             }
807         }
808     } else if(sign == LV_SIGNAL_PRESS_LOST || sign == LV_SIGNAL_DEFOCUS) {
809         ext->btn_id_pr  = LV_BTNM_BTN_NONE;
810         ext->btn_id_act = LV_BTNM_BTN_NONE;
811         lv_obj_invalidate(btnm);
812     } else if(sign == LV_SIGNAL_FOCUS) {
813 #if LV_USE_GROUP
814         lv_indev_t * indev         = lv_indev_get_act();
815         lv_indev_type_t indev_type = lv_indev_get_type(indev);
816         if(indev_type == LV_INDEV_TYPE_POINTER) {
817             /*Select the clicked button*/
818             lv_point_t p1;
819             lv_indev_get_point(indev, &p1);
820             uint16_t btn_i = get_button_from_point(btnm, &p1);
821             ext->btn_id_pr = btn_i;
822 
823         } else if(indev_type == LV_INDEV_TYPE_ENCODER) {
824             /*In navigation mode don't select any button but in edit mode select the fist*/
825             if(lv_group_get_editing(lv_obj_get_group(btnm)))
826                 ext->btn_id_pr = 0;
827             else
828                 ext->btn_id_pr = LV_BTNM_BTN_NONE;
829         } else {
830             ext->btn_id_pr = 0;
831         }
832 #else
833         ext->btn_id_pr = 0;
834 #endif
835 
836         ext->btn_id_act = ext->btn_id_pr;
837         lv_obj_invalidate(btnm);
838     } else if(sign == LV_SIGNAL_CONTROL) {
839         char c = *((char *)param);
840         if(c == LV_KEY_RIGHT) {
841             if(ext->btn_id_pr == LV_BTNM_BTN_NONE)
842                 ext->btn_id_pr = 0;
843             else
844                 ext->btn_id_pr++;
845             if(ext->btn_id_pr >= ext->btn_cnt - 1) ext->btn_id_pr = ext->btn_cnt - 1;
846             ext->btn_id_act = ext->btn_id_pr;
847             lv_obj_invalidate(btnm);
848         } else if(c == LV_KEY_LEFT) {
849             if(ext->btn_id_pr == LV_BTNM_BTN_NONE) ext->btn_id_pr = 0;
850             if(ext->btn_id_pr > 0) ext->btn_id_pr--;
851             ext->btn_id_act = ext->btn_id_pr;
852             lv_obj_invalidate(btnm);
853         } else if(c == LV_KEY_DOWN) {
854             const lv_style_t * style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
855             /*Find the area below the the current*/
856             if(ext->btn_id_pr == LV_BTNM_BTN_NONE) {
857                 ext->btn_id_pr = 0;
858             } else {
859                 uint16_t area_below;
860                 lv_coord_t pr_center =
861                     ext->button_areas[ext->btn_id_pr].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_pr]) >> 1);
862 
863                 for(area_below = ext->btn_id_pr; area_below < ext->btn_cnt; area_below++) {
864                     if(ext->button_areas[area_below].y1 > ext->button_areas[ext->btn_id_pr].y1 &&
865                        pr_center >= ext->button_areas[area_below].x1 &&
866                        pr_center <= ext->button_areas[area_below].x2 + style->body.padding.left) {
867                         break;
868                     }
869                 }
870 
871                 if(area_below < ext->btn_cnt) ext->btn_id_pr = area_below;
872             }
873             ext->btn_id_act = ext->btn_id_pr;
874             lv_obj_invalidate(btnm);
875         } else if(c == LV_KEY_UP) {
876             const lv_style_t * style = lv_btnm_get_style(btnm, LV_BTNM_STYLE_BG);
877             /*Find the area below the the current*/
878             if(ext->btn_id_pr == LV_BTNM_BTN_NONE) {
879                 ext->btn_id_pr = 0;
880             } else {
881                 int16_t area_above;
882                 lv_coord_t pr_center =
883                     ext->button_areas[ext->btn_id_pr].x1 + (lv_area_get_width(&ext->button_areas[ext->btn_id_pr]) >> 1);
884 
885                 for(area_above = ext->btn_id_pr; area_above >= 0; area_above--) {
886                     if(ext->button_areas[area_above].y1 < ext->button_areas[ext->btn_id_pr].y1 &&
887                        pr_center >= ext->button_areas[area_above].x1 - style->body.padding.left &&
888                        pr_center <= ext->button_areas[area_above].x2) {
889                         break;
890                     }
891                 }
892                 if(area_above >= 0) ext->btn_id_pr = area_above;
893             }
894             ext->btn_id_act = ext->btn_id_pr;
895             lv_obj_invalidate(btnm);
896         }
897     } else if(sign == LV_SIGNAL_GET_EDITABLE) {
898         bool * editable = (bool *)param;
899         *editable       = true;
900     } else if(sign == LV_SIGNAL_GET_TYPE) {
901         lv_obj_type_t * buf = param;
902         uint8_t i;
903         for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) { /*Find the last set data*/
904             if(buf->type[i] == NULL) break;
905         }
906         buf->type[i] = "lv_btnm";
907     }
908 
909     return res;
910 }
911 
912 /**
913  * Create the required number of buttons and control bytes according to a map
914  * @param btnm pointer to button matrix object
915  * @param map_p pointer to a string array
916  */
allocate_btn_areas_and_controls(const lv_obj_t * btnm,const char ** map)917 static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map)
918 {
919     /*Count the buttons in the map*/
920     uint16_t btn_cnt = 0;
921     uint16_t i       = 0;
922     while(strlen(map[i]) != 0) {
923         if(strcmp(map[i], "\n") != 0) { /*Do not count line breaks*/
924             btn_cnt++;
925         }
926         i++;
927     }
928 
929     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
930 
931     if(ext->button_areas != NULL) {
932         lv_mem_free(ext->button_areas);
933         ext->button_areas = NULL;
934     }
935     if(ext->ctrl_bits != NULL) {
936         lv_mem_free(ext->ctrl_bits);
937         ext->ctrl_bits = NULL;
938     }
939 
940     ext->button_areas = lv_mem_alloc(sizeof(lv_area_t) * btn_cnt);
941     lv_mem_assert(ext->button_areas);
942     ext->ctrl_bits = lv_mem_alloc(sizeof(lv_btnm_ctrl_t) * btn_cnt);
943     lv_mem_assert(ext->ctrl_bits);
944     if(ext->button_areas == NULL || ext->ctrl_bits == NULL) btn_cnt = 0;
945 
946     memset(ext->ctrl_bits, 0, sizeof(lv_btnm_ctrl_t) * btn_cnt);
947 
948     ext->btn_cnt = btn_cnt;
949 }
950 
951 /**
952  * Get the width of a button in units (default is 1).
953  * @param ctrl_bits least significant 3 bits used (1..7 valid values)
954  * @return the width of the button in units
955  */
get_button_width(lv_btnm_ctrl_t ctrl_bits)956 static uint8_t get_button_width(lv_btnm_ctrl_t ctrl_bits)
957 {
958     uint8_t w = ctrl_bits & LV_BTNM_WIDTH_MASK;
959     return w != 0 ? w : 1;
960 }
961 
button_is_hidden(lv_btnm_ctrl_t ctrl_bits)962 static bool button_is_hidden(lv_btnm_ctrl_t ctrl_bits)
963 {
964     return ctrl_bits & LV_BTNM_CTRL_HIDDEN ? true : false;
965 }
966 
button_is_repeat_disabled(lv_btnm_ctrl_t ctrl_bits)967 static bool button_is_repeat_disabled(lv_btnm_ctrl_t ctrl_bits)
968 {
969     return ctrl_bits & LV_BTNM_CTRL_NO_REPEAT ? true : false;
970 }
971 
button_is_inactive(lv_btnm_ctrl_t ctrl_bits)972 static bool button_is_inactive(lv_btnm_ctrl_t ctrl_bits)
973 {
974     return ctrl_bits & LV_BTNM_CTRL_INACTIVE ? true : false;
975 }
976 
button_is_click_trig(lv_btnm_ctrl_t ctrl_bits)977 static bool button_is_click_trig(lv_btnm_ctrl_t ctrl_bits)
978 {
979     return ctrl_bits & LV_BTNM_CTRL_CLICK_TRIG ? true : false;
980 }
981 
button_is_tgl_enabled(lv_btnm_ctrl_t ctrl_bits)982 static bool button_is_tgl_enabled(lv_btnm_ctrl_t ctrl_bits)
983 {
984     return ctrl_bits & LV_BTNM_CTRL_TGL_ENABLE ? true : false;
985 }
986 
button_get_tgl_state(lv_btnm_ctrl_t ctrl_bits)987 static bool button_get_tgl_state(lv_btnm_ctrl_t ctrl_bits)
988 {
989     return ctrl_bits & LV_BTNM_CTRL_TGL_STATE ? true : false;
990 }
991 
992 /**
993  * Gives the button id of a button under a given point
994  * @param btnm pointer to a button matrix object
995  * @param p a point with absolute coordinates
996  * @return the id of the button or LV_BTNM_BTN_NONE.
997  */
get_button_from_point(lv_obj_t * btnm,lv_point_t * p)998 static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p)
999 {
1000     lv_area_t btnm_cords;
1001     lv_area_t btn_area;
1002     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
1003     uint16_t i;
1004     lv_obj_get_coords(btnm, &btnm_cords);
1005 
1006     for(i = 0; i < ext->btn_cnt; i++) {
1007         lv_area_copy(&btn_area, &ext->button_areas[i]);
1008         btn_area.x1 += btnm_cords.x1;
1009         btn_area.y1 += btnm_cords.y1;
1010         btn_area.x2 += btnm_cords.x1;
1011         btn_area.y2 += btnm_cords.y1;
1012         if(lv_area_is_point_on(&btn_area, p) != false) {
1013             break;
1014         }
1015     }
1016 
1017     if(i == ext->btn_cnt) i = LV_BTNM_BTN_NONE;
1018 
1019     return i;
1020 }
1021 
invalidate_button_area(const lv_obj_t * btnm,uint16_t btn_idx)1022 static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx)
1023 {
1024     if(btn_idx == LV_BTNM_BTN_NONE) return;
1025 
1026     lv_area_t btn_area;
1027     lv_area_t btnm_area;
1028 
1029     lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm);
1030     lv_area_copy(&btn_area, &ext->button_areas[btn_idx]);
1031     lv_obj_get_coords(btnm, &btnm_area);
1032 
1033     /* Convert relative coordinates to absolute */
1034     btn_area.x1 += btnm_area.x1;
1035     btn_area.y1 += btnm_area.y1;
1036     btn_area.x2 += btnm_area.x1;
1037     btn_area.y2 += btnm_area.y1;
1038 
1039     lv_inv_area(lv_obj_get_disp(btnm), &btn_area);
1040 }
1041 
1042 /**
1043  * Compares two button matrix maps for equality
1044  * @param map1 map to compare
1045  * @param map2 map to compare
1046  * @return true if maps are identical in length and content
1047  */
maps_are_identical(const char ** map1,const char ** map2)1048 static bool maps_are_identical(const char ** map1, const char ** map2)
1049 {
1050     if(map1 == map2) return true;
1051     if(map1 == NULL || map2 == NULL) return map1 == map2;
1052 
1053     uint16_t i = 0;
1054     while(map1[i][0] != '\0' && map2[i][0] != '\0') {
1055         if(strcmp(map1[i], map2[i]) != 0) return false;
1056         i++;
1057     }
1058     return map1[i][0] == '\0' && map2[i][0] == '\0';
1059 }
1060 
1061 /**
1062  * Enforces a single button being toggled on the button matrix.
1063  * It simply clears the toggle flag on other buttons.
1064  * @param btnm Button matrix object
1065  * @param btn_idx Button that should remain toggled
1066  */
make_one_button_toggled(lv_obj_t * btnm,uint16_t btn_idx)1067 static void make_one_button_toggled(lv_obj_t * btnm, uint16_t btn_idx)
1068 {
1069     /*Save whether the button was toggled*/
1070     bool was_toggled = lv_btnm_get_btn_ctrl(btnm, btn_idx, LV_BTNM_CTRL_TGL_STATE);
1071 
1072     lv_btnm_clear_btn_ctrl_all(btnm, LV_BTNM_CTRL_TGL_STATE);
1073 
1074     if(was_toggled) lv_btnm_set_btn_ctrl(btnm, btn_idx, LV_BTNM_CTRL_TGL_STATE);
1075 }
1076 
1077 #endif
1078