1 /**
2  * @file lv_btn.h
3  *
4  */
5 
6 #ifndef LV_BTN_H
7 #define LV_BTN_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #include "../lv_conf_internal.h"
17 
18 #if LV_USE_BTN != 0
19 
20 /*Testing of dependencies*/
21 #if LV_USE_CONT == 0
22 #error "lv_btn: lv_cont is required. Enable it in lv_conf.h (LV_USE_CONT  1) "
23 #endif
24 
25 #include "lv_cont.h"
26 #include "../lv_core/lv_indev.h"
27 
28 /*********************
29  *      DEFINES
30  *********************/
31 
32 /**********************
33  *      TYPEDEFS
34  **********************/
35 
36 /** Possible states of a button.
37  * It can be used not only by buttons but other button-like objects too*/
38 enum {
39     LV_BTN_STATE_RELEASED,
40     LV_BTN_STATE_PRESSED,
41     LV_BTN_STATE_DISABLED,
42     LV_BTN_STATE_CHECKED_RELEASED,
43     LV_BTN_STATE_CHECKED_PRESSED,
44     LV_BTN_STATE_CHECKED_DISABLED,
45     _LV_BTN_STATE_LAST, /* Number of states*/
46 };
47 typedef uint8_t lv_btn_state_t;
48 
49 /** Extended data of button*/
50 typedef struct {
51     /** Ext. of ancestor*/
52     lv_cont_ext_t cont;
53 
54     /** 1: Toggle enabled*/
55     uint8_t checkable : 1;
56 } lv_btn_ext_t;
57 
58 /**Styles*/
59 enum {
60     LV_BTN_PART_MAIN = LV_OBJ_PART_MAIN,
61     _LV_BTN_PART_VIRTUAL_LAST,
62     _LV_BTN_PART_REAL_LAST = _LV_OBJ_PART_REAL_LAST,
63 };
64 typedef uint8_t lv_btn_part_t;
65 
66 /**********************
67  * GLOBAL PROTOTYPES
68  **********************/
69 
70 /**
71  * Create a button object
72  * @param par pointer to an object, it will be the parent of the new button
73  * @param copy pointer to a button object, if not NULL then the new object will be copied from it
74  * @return pointer to the created button
75  */
76 lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy);
77 
78 /*=====================
79  * Setter functions
80  *====================*/
81 
82 /**
83  * Enable the toggled states. On release the button will change from/to toggled state.
84  * @param btn pointer to a button object
85  * @param tgl true: enable toggled states, false: disable
86  */
87 void lv_btn_set_checkable(lv_obj_t * btn, bool tgl);
88 
89 /**
90  * Set the state of the button
91  * @param btn pointer to a button object
92  * @param state the new state of the button (from lv_btn_state_t enum)
93  */
94 void lv_btn_set_state(lv_obj_t * btn, lv_btn_state_t state);
95 
96 /**
97  * Toggle the state of the button (ON->OFF, OFF->ON)
98  * @param btn pointer to a button object
99  */
100 void lv_btn_toggle(lv_obj_t * btn);
101 
102 /**
103  * Set the layout on a button
104  * @param btn pointer to a button object
105  * @param layout a layout from 'lv_cont_layout_t'
106  */
lv_btn_set_layout(lv_obj_t * btn,lv_layout_t layout)107 static inline void lv_btn_set_layout(lv_obj_t * btn, lv_layout_t layout)
108 {
109     lv_cont_set_layout(btn, layout);
110 }
111 
112 /**
113  * Set the fit policy in all 4 directions separately.
114  * It tells how to change the button size automatically.
115  * @param btn pointer to a button object
116  * @param left left fit policy from `lv_fit_t`
117  * @param right right fit policy from `lv_fit_t`
118  * @param top top fit policy from `lv_fit_t`
119  * @param bottom bottom fit policy from `lv_fit_t`
120  */
lv_btn_set_fit4(lv_obj_t * btn,lv_fit_t left,lv_fit_t right,lv_fit_t top,lv_fit_t bottom)121 static inline void lv_btn_set_fit4(lv_obj_t * btn, lv_fit_t left, lv_fit_t right, lv_fit_t top, lv_fit_t bottom)
122 {
123     lv_cont_set_fit4(btn, left, right, top, bottom);
124 }
125 
126 /**
127  * Set the fit policy horizontally and vertically separately.
128  * It tells how to change the button size automatically.
129  * @param btn pointer to a button object
130  * @param hor horizontal fit policy from `lv_fit_t`
131  * @param ver vertical fit policy from `lv_fit_t`
132  */
lv_btn_set_fit2(lv_obj_t * btn,lv_fit_t hor,lv_fit_t ver)133 static inline void lv_btn_set_fit2(lv_obj_t * btn, lv_fit_t hor, lv_fit_t ver)
134 {
135     lv_cont_set_fit2(btn, hor, ver);
136 }
137 
138 /**
139  * Set the fit policy in all 4 direction at once.
140  * It tells how to change the button size automatically.
141  * @param btn pointer to a button object
142  * @param fit fit policy from `lv_fit_t`
143  */
lv_btn_set_fit(lv_obj_t * btn,lv_fit_t fit)144 static inline void lv_btn_set_fit(lv_obj_t * btn, lv_fit_t fit)
145 {
146     lv_cont_set_fit(btn, fit);
147 }
148 
149 /*=====================
150  * Getter functions
151  *====================*/
152 
153 /**
154  * Get the current state of the button
155  * @param btn pointer to a button object
156  * @return the state of the button (from lv_btn_state_t enum)
157  * If the button is in disabled state `LV_BTN_STATE_DISABLED` will be ORed to the other button states.
158  */
159 lv_btn_state_t lv_btn_get_state(const lv_obj_t * btn);
160 
161 /**
162  * Get the toggle enable attribute of the button
163  * @param btn pointer to a button object
164  * @return true: checkable enabled, false: disabled
165  */
166 bool lv_btn_get_checkable(const lv_obj_t * btn);
167 
168 /**
169  * Get the layout of a button
170  * @param btn pointer to button object
171  * @return the layout from 'lv_cont_layout_t'
172  */
lv_btn_get_layout(const lv_obj_t * btn)173 static inline lv_layout_t lv_btn_get_layout(const lv_obj_t * btn)
174 {
175     return lv_cont_get_layout(btn);
176 }
177 
178 /**
179  * Get the left fit mode
180  * @param btn pointer to a button object
181  * @return an element of `lv_fit_t`
182  */
lv_btn_get_fit_left(const lv_obj_t * btn)183 static inline lv_fit_t lv_btn_get_fit_left(const lv_obj_t * btn)
184 {
185     return lv_cont_get_fit_left(btn);
186 }
187 
188 /**
189  * Get the right fit mode
190  * @param btn pointer to a button object
191  * @return an element of `lv_fit_t`
192  */
lv_btn_get_fit_right(const lv_obj_t * btn)193 static inline lv_fit_t lv_btn_get_fit_right(const lv_obj_t * btn)
194 {
195     return lv_cont_get_fit_right(btn);
196 }
197 
198 /**
199  * Get the top fit mode
200  * @param btn pointer to a button object
201  * @return an element of `lv_fit_t`
202  */
lv_btn_get_fit_top(const lv_obj_t * btn)203 static inline lv_fit_t lv_btn_get_fit_top(const lv_obj_t * btn)
204 {
205     return lv_cont_get_fit_top(btn);
206 }
207 
208 /**
209  * Get the bottom fit mode
210  * @param btn pointer to a button object
211  * @return an element of `lv_fit_t`
212  */
lv_btn_get_fit_bottom(const lv_obj_t * btn)213 static inline lv_fit_t lv_btn_get_fit_bottom(const lv_obj_t * btn)
214 {
215     return lv_cont_get_fit_bottom(btn);
216 }
217 
218 /**********************
219  *      MACROS
220  **********************/
221 
222 #endif /*LV_USE_BUTTON*/
223 
224 #ifdef __cplusplus
225 } /* extern "C" */
226 #endif
227 
228 #endif /*LV_BTN_H*/
229