1 /**
2 * @file lv_demo_keypad_encoder.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_demo_keypad_encoder.h"
10
11 #if LV_USE_DEMO_KEYPAD_AND_ENCODER
12
13 /*********************
14 * DEFINES
15 *********************/
16
17 /**********************
18 * TYPEDEFS
19 **********************/
20
21 /**********************
22 * STATIC PROTOTYPES
23 **********************/
24 static void selectors_create(lv_obj_t * parent);
25 static void text_input_create(lv_obj_t * parent);
26 static void msgbox_create(void);
27
28 static void msgbox_event_cb(lv_event_t * e);
29 static void ta_event_cb(lv_event_t * e);
30
31 /**********************
32 * STATIC VARIABLES
33 **********************/
34 static lv_group_t* g;
35 static lv_obj_t * tv;
36 static lv_obj_t * t1;
37 static lv_obj_t * t2;
38
39 /**********************
40 * MACROS
41 **********************/
42
43 /**********************
44 * GLOBAL FUNCTIONS
45 **********************/
46
lv_demo_keypad_encoder(void)47 void lv_demo_keypad_encoder(void)
48 {
49 g = lv_group_create();
50 lv_group_set_default(g);
51
52 lv_indev_t* cur_drv = NULL;
53 for (;;) {
54 cur_drv = lv_indev_get_next(cur_drv);
55 if (!cur_drv) {
56 break;
57 }
58
59 if (cur_drv->driver->type == LV_INDEV_TYPE_KEYPAD) {
60 lv_indev_set_group(cur_drv, g);
61 }
62
63 if (cur_drv->driver->type == LV_INDEV_TYPE_ENCODER) {
64 lv_indev_set_group(cur_drv, g);
65 }
66 }
67
68 tv = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, LV_DPI_DEF / 3);
69
70 t1 = lv_tabview_add_tab(tv, "Selectors");
71 t2 = lv_tabview_add_tab(tv, "Text input");
72
73 selectors_create(t1);
74 text_input_create(t2);
75
76 msgbox_create();
77 }
78
79 /**********************
80 * STATIC FUNCTIONS
81 **********************/
82
selectors_create(lv_obj_t * parent)83 static void selectors_create(lv_obj_t * parent)
84 {
85 lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
86 lv_obj_set_flex_align(parent, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
87
88 lv_obj_t * obj;
89
90 obj = lv_table_create(parent);
91 lv_table_set_cell_value(obj, 0, 0, "00");
92 lv_table_set_cell_value(obj, 0, 1, "01");
93 lv_table_set_cell_value(obj, 1, 0, "10");
94 lv_table_set_cell_value(obj, 1, 1, "11");
95 lv_table_set_cell_value(obj, 2, 0, "20");
96 lv_table_set_cell_value(obj, 2, 1, "21");
97 lv_table_set_cell_value(obj, 3, 0, "30");
98 lv_table_set_cell_value(obj, 3, 1, "31");
99 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
100
101 obj = lv_calendar_create(parent);
102 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
103
104 obj = lv_btnmatrix_create(parent);
105 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
106
107 obj = lv_checkbox_create(parent);
108 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
109
110 obj = lv_slider_create(parent);
111 lv_slider_set_range(obj, 0, 10);
112 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
113
114 obj = lv_switch_create(parent);
115 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
116
117 obj = lv_spinbox_create(parent);
118 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
119
120 obj = lv_dropdown_create(parent);
121 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
122
123 obj = lv_roller_create(parent);
124 lv_obj_add_flag(obj, LV_OBJ_FLAG_SCROLL_ON_FOCUS);
125
126 lv_obj_t * list = lv_list_create(parent);
127 lv_obj_update_layout(list);
128 if(lv_obj_get_height(list) > lv_obj_get_content_height(parent)) {
129 lv_obj_set_height(list, lv_obj_get_content_height(parent));
130 }
131
132 lv_list_add_btn(list, LV_SYMBOL_OK, "Apply");
133 lv_list_add_btn(list, LV_SYMBOL_CLOSE, "Close");
134 lv_list_add_btn(list, LV_SYMBOL_EYE_OPEN, "Show");
135 lv_list_add_btn(list, LV_SYMBOL_EYE_CLOSE, "Hide");
136 lv_list_add_btn(list, LV_SYMBOL_TRASH, "Delete");
137 lv_list_add_btn(list, LV_SYMBOL_COPY, "Copy");
138 lv_list_add_btn(list, LV_SYMBOL_PASTE, "Paste");
139 }
140
text_input_create(lv_obj_t * parent)141 static void text_input_create(lv_obj_t * parent)
142 {
143 lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
144
145 lv_obj_t * ta1 = lv_textarea_create(parent);
146 lv_obj_set_width(ta1, LV_PCT(100));
147 lv_textarea_set_one_line(ta1, true);
148 lv_textarea_set_placeholder_text(ta1, "Click with an encoder to show a keyboard");
149
150 lv_obj_t * ta2 = lv_textarea_create(parent);
151 lv_obj_set_width(ta2, LV_PCT(100));
152 lv_textarea_set_one_line(ta2, true);
153 lv_textarea_set_placeholder_text(ta2, "Type something");
154
155 lv_obj_t *kb = lv_keyboard_create(lv_scr_act());
156 lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
157
158 lv_obj_add_event_cb(ta1, ta_event_cb, LV_EVENT_ALL, kb);
159 lv_obj_add_event_cb(ta2, ta_event_cb, LV_EVENT_ALL, kb);
160 }
161
msgbox_create(void)162 static void msgbox_create(void)
163 {
164 static const char * btns[] = {"Ok", "Cancel", ""};
165 lv_obj_t * mbox = lv_msgbox_create(NULL, "Hi", "Welcome to the keyboard and encoder demo", btns, false);
166 lv_obj_add_event_cb(mbox, msgbox_event_cb, LV_EVENT_ALL, NULL);
167 lv_group_focus_obj(lv_msgbox_get_btns(mbox));
168 lv_obj_add_state(lv_msgbox_get_btns(mbox), LV_STATE_FOCUS_KEY);
169 #if LV_EX_MOUSEWHEEL
170 lv_group_set_editing(g, true);
171 #endif
172 lv_group_focus_freeze(g, true);
173
174 lv_obj_align(mbox, LV_ALIGN_CENTER, 0, 0);
175
176 lv_obj_t * bg = lv_obj_get_parent(mbox);
177 lv_obj_set_style_bg_opa(bg, LV_OPA_70, 0);
178 lv_obj_set_style_bg_color(bg, lv_palette_main(LV_PALETTE_GREY), 0);
179 }
180
msgbox_event_cb(lv_event_t * e)181 static void msgbox_event_cb(lv_event_t * e)
182 {
183 lv_event_code_t code = lv_event_get_code(e);
184 lv_obj_t * msgbox = lv_event_get_current_target(e);
185
186 if(code == LV_EVENT_VALUE_CHANGED) {
187 const char * txt = lv_msgbox_get_active_btn_text(msgbox);
188 if(txt) {
189 lv_msgbox_close(msgbox);
190 lv_group_focus_freeze(g, false);
191 lv_group_focus_obj(lv_obj_get_child(t1, 0));
192 lv_obj_scroll_to(t1, 0, 0, LV_ANIM_OFF);
193
194 }
195 }
196 }
197
ta_event_cb(lv_event_t * e)198 static void ta_event_cb(lv_event_t * e)
199 {
200 lv_indev_t * indev = lv_indev_get_act();
201 if(indev == NULL) return;
202 lv_indev_type_t indev_type = lv_indev_get_type(indev);
203
204 lv_event_code_t code = lv_event_get_code(e);
205 lv_obj_t * ta = lv_event_get_target(e);
206 lv_obj_t * kb = lv_event_get_user_data(e);
207
208 if(code == LV_EVENT_CLICKED && indev_type == LV_INDEV_TYPE_ENCODER) {
209 lv_keyboard_set_textarea(kb, ta);
210 lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
211 lv_group_focus_obj(kb);
212 lv_group_set_editing(lv_obj_get_group(kb), kb);
213 lv_obj_set_height(tv, LV_VER_RES / 2);
214 lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);
215 }
216
217 if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
218 lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
219 lv_obj_set_height(tv, LV_VER_RES);
220 }
221 }
222
223 #endif
224