1 /**
2 * @file lv_calendar_header_arrow.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_calendar_header_arrow.h"
10 #if LV_USE_CALENDAR_HEADER_ARROW
11
12 #include "lv_calendar.h"
13 #include "../../../widgets/lv_btn.h"
14 #include "../../../widgets/lv_label.h"
15 #include "../../layouts/flex/lv_flex.h"
16
17 /*********************
18 * DEFINES
19 *********************/
20
21 /**********************
22 * TYPEDEFS
23 **********************/
24
25 /**********************
26 * STATIC PROTOTYPES
27 **********************/
28 static void my_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj);
29 static void month_event_cb(lv_event_t * e);
30 static void value_changed_event_cb(lv_event_t * e);
31
32 /**********************
33 * STATIC VARIABLES
34 **********************/
35 const lv_obj_class_t lv_calendar_header_arrow_class = {
36 .base_class = &lv_obj_class,
37 .constructor_cb = my_constructor,
38 .width_def = LV_PCT(100),
39 .height_def = LV_DPI_DEF / 3
40 };
41
42 static const char * month_names_def[12] = LV_CALENDAR_DEFAULT_MONTH_NAMES;
43
44 /**********************
45 * MACROS
46 **********************/
47
48 /**********************
49 * GLOBAL FUNCTIONS
50 **********************/
51
lv_calendar_header_arrow_create(lv_obj_t * parent)52 lv_obj_t * lv_calendar_header_arrow_create(lv_obj_t * parent)
53 {
54 lv_obj_t * obj = lv_obj_class_create_obj(&lv_calendar_header_arrow_class, parent);
55 lv_obj_class_init_obj(obj);
56 return obj;
57 }
58
59 /**********************
60 * STATIC FUNCTIONS
61 **********************/
62
my_constructor(const lv_obj_class_t * class_p,lv_obj_t * obj)63 static void my_constructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
64 {
65 LV_TRACE_OBJ_CREATE("begin");
66
67 LV_UNUSED(class_p);
68
69 lv_obj_move_to_index(obj, 0);
70
71 lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW);
72 lv_obj_set_flex_align(obj, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START);
73
74 lv_obj_t * mo_prev = lv_btn_create(obj);
75 lv_obj_set_style_bg_img_src(mo_prev, LV_SYMBOL_LEFT, 0);
76 lv_obj_set_height(mo_prev, lv_pct(100));
77 lv_obj_update_layout(mo_prev);
78 lv_coord_t btn_size = lv_obj_get_height(mo_prev);
79 lv_obj_set_width(mo_prev, btn_size);
80
81 lv_obj_add_event_cb(mo_prev, month_event_cb, LV_EVENT_CLICKED, NULL);
82 lv_obj_clear_flag(mo_prev, LV_OBJ_FLAG_CLICK_FOCUSABLE);
83
84 lv_obj_t * label = lv_label_create(obj);
85 lv_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR);
86 lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
87 lv_obj_set_flex_grow(label, 1);
88
89 lv_obj_t * mo_next = lv_btn_create(obj);
90 lv_obj_set_style_bg_img_src(mo_next, LV_SYMBOL_RIGHT, 0);
91 lv_obj_set_size(mo_next, btn_size, btn_size);
92
93 lv_obj_add_event_cb(mo_next, month_event_cb, LV_EVENT_CLICKED, NULL);
94 lv_obj_clear_flag(mo_next, LV_OBJ_FLAG_CLICK_FOCUSABLE);
95
96 lv_obj_add_event_cb(obj, value_changed_event_cb, LV_EVENT_VALUE_CHANGED, NULL);
97 /*Refresh the drop downs*/
98 lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
99 }
100
month_event_cb(lv_event_t * e)101 static void month_event_cb(lv_event_t * e)
102 {
103 lv_obj_t * btn = lv_event_get_target(e);
104
105 lv_obj_t * header = lv_obj_get_parent(btn);
106 lv_obj_t * calendar = lv_obj_get_parent(header);
107
108 const lv_calendar_date_t * d;
109 d = lv_calendar_get_showed_date(calendar);
110 lv_calendar_date_t newd = *d;
111
112 /*The last child is the right button*/
113 if(lv_obj_get_child(header, 0) == btn) {
114 if(newd.month == 1) {
115 newd.month = 12;
116 newd.year --;
117 }
118 else {
119 newd.month --;
120 }
121 }
122 else {
123 if(newd.month == 12) {
124 newd.month = 1;
125 newd.year ++;
126 }
127 else {
128 newd.month ++;
129 }
130 }
131
132 lv_calendar_set_showed_date(calendar, newd.year, newd.month);
133
134 lv_obj_t * label = lv_obj_get_child(header, 1);
135 lv_label_set_text_fmt(label, "%d %s", newd.year, month_names_def[newd.month - 1]);
136 }
137
value_changed_event_cb(lv_event_t * e)138 static void value_changed_event_cb(lv_event_t * e)
139 {
140 lv_obj_t * header = lv_event_get_target(e);
141 lv_obj_t * calendar = lv_obj_get_parent(header);
142
143 const lv_calendar_date_t * cur_date = lv_calendar_get_showed_date(calendar);
144 lv_obj_t * label = lv_obj_get_child(header, 1);
145 lv_label_set_text_fmt(label, "%d %s", cur_date->year, month_names_def[cur_date->month - 1]);
146 }
147
148 #endif /*LV_USE_CALENDAR_HEADER_ARROW*/
149
150