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 #ifdef LV_CONF_INCLUDE_SIMPLE
17 #include "lv_conf.h"
18 #else
19 #include "../../lv_conf.h"
20 #endif
21 
22 #if LV_USE_BTN != 0
23 
24 /*Testing of dependencies*/
25 #if LV_USE_CONT == 0
26 #error "lv_btn: lv_cont is required. Enable it in lv_conf.h (LV_USE_CONT  1) "
27 #endif
28 
29 #include "lv_cont.h"
30 #include "../lv_core/lv_indev.h"
31 
32 /*********************
33  *      DEFINES
34  *********************/
35 
36 /**********************
37  *      TYPEDEFS
38  **********************/
39 
40 /** Possible states of a button.
41  * It can be used not only by buttons but other button-like objects too*/
42 enum {
43     /**Released*/
44     LV_BTN_STATE_REL,
45 
46     /**Pressed*/
47     LV_BTN_STATE_PR,
48 
49     /**Toggled released*/
50     LV_BTN_STATE_TGL_REL,
51 
52     /**Toggled pressed*/
53     LV_BTN_STATE_TGL_PR,
54 
55     /**Inactive*/
56     LV_BTN_STATE_INA,
57 
58     /**Number of states*/
59     _LV_BTN_STATE_NUM,
60 };
61 typedef uint8_t lv_btn_state_t;
62 
63 /** Extended data of button*/
64 typedef struct
65 {
66     /** Ext. of ancestor*/
67     lv_cont_ext_t cont;
68 
69     /*New data for this type */
70 
71     /**Styles in each state*/
72     const lv_style_t * styles[_LV_BTN_STATE_NUM];
73 #if LV_BTN_INK_EFFECT
74     /** [ms] Time of ink fill effect (0: disable ink effect)*/
75     uint16_t ink_in_time;
76 
77     /** [ms] Wait before the ink disappears */
78     uint16_t ink_wait_time;
79 
80     /** [ms] Time of ink disappearing*/
81     uint16_t ink_out_time;
82 #endif
83 
84     /** Current state of the button from 'lv_btn_state_t' enum*/
85     lv_btn_state_t state : 3;
86 
87     /** 1: Toggle enabled*/
88     uint8_t toggle : 1;
89 } lv_btn_ext_t;
90 
91 /**Styles*/
92 enum {
93     /** Release style */
94     LV_BTN_STYLE_REL,
95 
96     /**Pressed style*/
97     LV_BTN_STYLE_PR,
98 
99     /** Toggle released style*/
100     LV_BTN_STYLE_TGL_REL,
101 
102     /** Toggle pressed style */
103     LV_BTN_STYLE_TGL_PR,
104 
105     /** Inactive style*/
106     LV_BTN_STYLE_INA,
107 };
108 typedef uint8_t lv_btn_style_t;
109 
110 /**********************
111  * GLOBAL PROTOTYPES
112  **********************/
113 
114 /**
115  * Create a button object
116  * @param par pointer to an object, it will be the parent of the new button
117  * @param copy pointer to a button object, if not NULL then the new object will be copied from it
118  * @return pointer to the created button
119  */
120 lv_obj_t * lv_btn_create(lv_obj_t * par, const lv_obj_t * copy);
121 
122 /*=====================
123  * Setter functions
124  *====================*/
125 
126 /**
127  * Enable the toggled states. On release the button will change from/to toggled state.
128  * @param btn pointer to a button object
129  * @param tgl true: enable toggled states, false: disable
130  */
131 void lv_btn_set_toggle(lv_obj_t * btn, bool tgl);
132 
133 /**
134  * Set the state of the button
135  * @param btn pointer to a button object
136  * @param state the new state of the button (from lv_btn_state_t enum)
137  */
138 void lv_btn_set_state(lv_obj_t * btn, lv_btn_state_t state);
139 
140 /**
141  * Toggle the state of the button (ON->OFF, OFF->ON)
142  * @param btn pointer to a button object
143  */
144 void lv_btn_toggle(lv_obj_t * btn);
145 
146 /**
147  * Set the layout on a button
148  * @param btn pointer to a button object
149  * @param layout a layout from 'lv_cont_layout_t'
150  */
lv_btn_set_layout(lv_obj_t * btn,lv_layout_t layout)151 static inline void lv_btn_set_layout(lv_obj_t * btn, lv_layout_t layout)
152 {
153     lv_cont_set_layout(btn, layout);
154 }
155 
156 /**
157  * Set the fit policy in all 4 directions separately.
158  * It tells how to change the button size automatically.
159  * @param btn pointer to a button object
160  * @param left left fit policy from `lv_fit_t`
161  * @param right right fit policy from `lv_fit_t`
162  * @param top top fit policy from `lv_fit_t`
163  * @param bottom bottom fit policy from `lv_fit_t`
164  */
lv_btn_set_fit4(lv_obj_t * btn,lv_fit_t left,lv_fit_t right,lv_fit_t top,lv_fit_t bottom)165 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)
166 {
167     lv_cont_set_fit4(btn, left, right, top, bottom);
168 }
169 
170 /**
171  * Set the fit policy horizontally and vertically separately.
172  * It tells how to change the button size automatically.
173  * @param btn pointer to a button object
174  * @param hor horizontal fit policy from `lv_fit_t`
175  * @param ver vertical fit policy from `lv_fit_t`
176  */
lv_btn_set_fit2(lv_obj_t * btn,lv_fit_t hor,lv_fit_t ver)177 static inline void lv_btn_set_fit2(lv_obj_t * btn, lv_fit_t hor, lv_fit_t ver)
178 {
179     lv_cont_set_fit2(btn, hor, ver);
180 }
181 
182 /**
183  * Set the fit policy in all 4 direction at once.
184  * It tells how to change the button size automatically.
185  * @param btn pointer to a button object
186  * @param fit fit policy from `lv_fit_t`
187  */
lv_btn_set_fit(lv_obj_t * btn,lv_fit_t fit)188 static inline void lv_btn_set_fit(lv_obj_t * btn, lv_fit_t fit)
189 {
190     lv_cont_set_fit(btn, fit);
191 }
192 
193 /**
194  * Set time of the ink effect (draw a circle on click to animate in the new state)
195  * @param btn pointer to a button object
196  * @param time the time of the ink animation
197  */
198 void lv_btn_set_ink_in_time(lv_obj_t * btn, uint16_t time);
199 
200 /**
201  * Set the wait time before the ink disappears
202  * @param btn pointer to a button object
203  * @param time the time of the ink animation
204  */
205 void lv_btn_set_ink_wait_time(lv_obj_t * btn, uint16_t time);
206 
207 /**
208  * Set time of the ink out effect (animate to the released state)
209  * @param btn pointer to a button object
210  * @param time the time of the ink animation
211  */
212 void lv_btn_set_ink_out_time(lv_obj_t * btn, uint16_t time);
213 
214 /**
215  * Set a style of a button.
216  * @param btn pointer to button object
217  * @param type which style should be set
218  * @param style pointer to a style
219  *  */
220 void lv_btn_set_style(lv_obj_t * btn, lv_btn_style_t type, const lv_style_t * style);
221 
222 /*=====================
223  * Getter functions
224  *====================*/
225 
226 /**
227  * Get the current state of the button
228  * @param btn pointer to a button object
229  * @return the state of the button (from lv_btn_state_t enum)
230  */
231 lv_btn_state_t lv_btn_get_state(const lv_obj_t * btn);
232 
233 /**
234  * Get the toggle enable attribute of the button
235  * @param btn pointer to a button object
236  * @return true: toggle enabled, false: disabled
237  */
238 bool lv_btn_get_toggle(const lv_obj_t * btn);
239 
240 /**
241  * Get the layout of a button
242  * @param btn pointer to button object
243  * @return the layout from 'lv_cont_layout_t'
244  */
lv_btn_get_layout(const lv_obj_t * btn)245 static inline lv_layout_t lv_btn_get_layout(const lv_obj_t * btn)
246 {
247     return lv_cont_get_layout(btn);
248 }
249 
250 /**
251  * Get the left fit mode
252  * @param btn pointer to a button object
253  * @return an element of `lv_fit_t`
254  */
lv_btn_get_fit_left(const lv_obj_t * btn)255 static inline lv_fit_t lv_btn_get_fit_left(const lv_obj_t * btn)
256 {
257     return lv_cont_get_fit_left(btn);
258 }
259 
260 /**
261  * Get the right fit mode
262  * @param btn pointer to a button object
263  * @return an element of `lv_fit_t`
264  */
lv_btn_get_fit_right(const lv_obj_t * btn)265 static inline lv_fit_t lv_btn_get_fit_right(const lv_obj_t * btn)
266 {
267     return lv_cont_get_fit_right(btn);
268 }
269 
270 /**
271  * Get the top fit mode
272  * @param btn pointer to a button object
273  * @return an element of `lv_fit_t`
274  */
lv_btn_get_fit_top(const lv_obj_t * btn)275 static inline lv_fit_t lv_btn_get_fit_top(const lv_obj_t * btn)
276 {
277     return lv_cont_get_fit_top(btn);
278 }
279 
280 /**
281  * Get the bottom fit mode
282  * @param btn pointer to a button object
283  * @return an element of `lv_fit_t`
284  */
lv_btn_get_fit_bottom(const lv_obj_t * btn)285 static inline lv_fit_t lv_btn_get_fit_bottom(const lv_obj_t * btn)
286 {
287     return lv_cont_get_fit_bottom(btn);
288 }
289 
290 /**
291  * Get time of the ink in effect (draw a circle on click to animate in the new state)
292  * @param btn pointer to a button object
293  * @return the time of the ink animation
294  */
295 uint16_t lv_btn_get_ink_in_time(const lv_obj_t * btn);
296 
297 /**
298  * Get the wait time before the ink disappears
299  * @param btn pointer to a button object
300  * @return the time of the ink animation
301  */
302 uint16_t lv_btn_get_ink_wait_time(const lv_obj_t * btn);
303 
304 /**
305  * Get time of the ink out effect (animate to the releases state)
306  * @param btn pointer to a button object
307  * @return the time of the ink animation
308  */
309 uint16_t lv_btn_get_ink_out_time(const lv_obj_t * btn);
310 
311 /**
312  * Get style of a button.
313  * @param btn pointer to button object
314  * @param type which style should be get
315  * @return style pointer to the style
316  *  */
317 const lv_style_t * lv_btn_get_style(const lv_obj_t * btn, lv_btn_style_t type);
318 
319 /**********************
320  *      MACROS
321  **********************/
322 
323 #endif /*LV_USE_BUTTON*/
324 
325 #ifdef __cplusplus
326 } /* extern "C" */
327 #endif
328 
329 #endif /*LV_BTN_H*/
330