1 #include "../lv_examples.h"
2 #if LV_BUILD_EXAMPLES
3 
4 static lv_obj_t  * panel;
5 static lv_obj_t  * save_button;
6 static lv_obj_t  * restore_button;
7 static int         saved_scroll_x;
8 static int         saved_scroll_y;
9 
10 static void scroll_update_cb(lv_event_t * e);
11 static void button_event_cb(lv_event_t * e);
12 
scroll_update_cb(lv_event_t * e)13 static void scroll_update_cb(lv_event_t * e)
14 {
15     LV_UNUSED(e);
16 
17     LV_LOG("scroll info: x:%3"LV_PRId32", y:%3"LV_PRId32", top:%3"LV_PRId32", "
18            "bottom:%3"LV_PRId32", left:%3"LV_PRId32", right:%3"LV_PRId32"\n",
19            lv_obj_get_scroll_x(panel),
20            lv_obj_get_scroll_y(panel),
21            lv_obj_get_scroll_top(panel),
22            lv_obj_get_scroll_bottom(panel),
23            lv_obj_get_scroll_left(panel),
24            lv_obj_get_scroll_right(panel)
25           );
26 }
27 
button_event_cb(lv_event_t * e)28 static void button_event_cb(lv_event_t * e)
29 {
30     lv_obj_t * obj = lv_event_get_target_obj(e);
31 
32     if(obj == save_button) {
33         saved_scroll_x = lv_obj_get_scroll_x(panel);
34         saved_scroll_y = lv_obj_get_scroll_y(panel);
35     }
36     else {
37         lv_obj_scroll_to(panel, saved_scroll_x, saved_scroll_y, LV_ANIM_ON);
38     }
39 }
40 
41 /**
42  * Demonstrate how scrolling appears automatically
43  */
lv_example_scroll_1(void)44 void lv_example_scroll_1(void)
45 {
46     /*Create an object with the new style*/
47     lv_obj_t * scr;
48     scr = lv_screen_active();
49     panel = lv_obj_create(scr);
50     lv_obj_set_size(panel, 200, 200);
51     lv_obj_align(panel, LV_ALIGN_CENTER, 44, 0);
52 
53     lv_obj_t * child;
54     lv_obj_t * label;
55 
56     child = lv_obj_create(panel);
57     lv_obj_set_pos(child, 0, 0);
58     lv_obj_set_size(child, 70, 70);
59     label = lv_label_create(child);
60     lv_label_set_text(label, "Zero");
61     lv_obj_center(label);
62 
63     child = lv_obj_create(panel);
64     lv_obj_set_pos(child, 160, 80);
65     lv_obj_set_size(child, 80, 80);
66 
67     lv_obj_t * child2 = lv_button_create(child);
68     lv_obj_set_size(child2, 100, 50);
69 
70     label = lv_label_create(child2);
71     lv_label_set_text(label, "Right");
72     lv_obj_center(label);
73 
74     child = lv_obj_create(panel);
75     lv_obj_set_pos(child, 40, 160);
76     lv_obj_set_size(child, 100, 70);
77     label = lv_label_create(child);
78     lv_label_set_text(label, "Bottom");
79     lv_obj_center(label);
80 
81     /* When LV_OBJ_FLAG_SCROLL_ELASTIC is cleared, scrolling does not go past edge bounaries. */
82     /* lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLL_ELASTIC); */
83 
84     /* Call `scroll_update_cb` while panel is being scrolled. */
85     lv_obj_add_event_cb(panel, scroll_update_cb, LV_EVENT_SCROLL, NULL);
86 
87     /* Set up buttons that save and restore scroll position. */
88     save_button = lv_button_create(scr);
89     restore_button = lv_button_create(scr);
90     lv_obj_t * lbl;
91     lbl = lv_label_create(save_button);
92     lv_label_set_text_static(lbl, "Save");
93     lbl = lv_label_create(restore_button);
94     lv_label_set_text_static(lbl, "Restore");
95     lv_obj_align_to(save_button, panel, LV_ALIGN_OUT_LEFT_MID, -10, -20);
96     lv_obj_align_to(restore_button, panel, LV_ALIGN_OUT_LEFT_MID, -10, 20);
97     lv_obj_add_event_cb(save_button, button_event_cb, LV_EVENT_CLICKED, NULL);
98     lv_obj_add_event_cb(restore_button, button_event_cb, LV_EVENT_CLICKED, NULL);
99 }
100 
101 #endif
102