elm bin: Use convenient utility function elm_win_util_standard_add()
[framework/uifw/elementary.git] / src / bin / test_calendar.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4 #include <Elementary.h>
5 #ifndef ELM_LIB_QUICKLAUNCH
6 struct _api_data
7 {
8    unsigned int state;  /* What state we are testing       */
9    void *box;           /* box used in set_api_state       */
10 };
11 typedef struct _api_data api_data;
12
13 enum _api_state
14 {
15    STATE_MARK_MONTHLY,
16    STATE_MARK_WEEKLY,
17    STATE_SUNDAY_HIGHLIGHT,
18    STATE_SELECT_DATE_DISABLED_WITH_MARKS,
19    STATE_SELECT_DATE_DISABLED_NO_MARKS,
20    API_STATE_LAST
21 };
22 typedef enum _api_state api_state;
23
24 static void
25 set_api_state(api_data *api)
26 {
27    const Eina_List *items = elm_box_children_get(api->box);
28    static Elm_Calendar_Mark *m = NULL;
29    if (!eina_list_count(items))
30      return;
31
32    switch(api->state)
33      { /* Put all api-changes under switch */
34       case STATE_MARK_MONTHLY:
35            {
36               Evas_Object *cal = eina_list_nth(items, 0);
37               time_t sec_per_day = (60*60*24);
38               time_t sec_per_year = sec_per_day * 365;
39               time_t the_time = (sec_per_year * 41) + (sec_per_day * 9); /* Set date to DEC 31, 2010 */
40               elm_calendar_min_max_year_set(cal, 2010, 2011);
41               m = elm_calendar_mark_add(cal, "checked", gmtime(&the_time), ELM_CALENDAR_MONTHLY);
42               elm_calendar_selected_time_set(cal, gmtime(&the_time));
43            }
44          break;
45       case STATE_MARK_WEEKLY:
46            {
47               Evas_Object *cal = eina_list_nth(items, 0);
48               time_t sec_per_day = (60*60*24);
49               time_t sec_per_year = sec_per_day * 365;
50               time_t the_time = (sec_per_year * 41) + (sec_per_day * 4); /* Set date to DEC 26, 2010 */
51               elm_calendar_mark_del(m);
52               m = elm_calendar_mark_add(cal, "checked", gmtime(&the_time), ELM_CALENDAR_WEEKLY);
53               elm_calendar_selected_time_set(cal, gmtime(&the_time));
54            }
55          break;
56       case STATE_SUNDAY_HIGHLIGHT:
57            {
58               Evas_Object *cal = eina_list_nth(items, 0);
59               time_t sec_per_day = (60*60*24);
60               time_t sec_per_year = sec_per_day * 365;
61               time_t the_time = (sec_per_year * 41) + (sec_per_day * 3); /* Set date to DEC 25, 2010 */
62               /* elm_calendar_mark_del(m); */
63               m = elm_calendar_mark_add(cal, "holiday", gmtime(&the_time), ELM_CALENDAR_WEEKLY);
64               elm_calendar_selected_time_set(cal, gmtime(&the_time));
65            }
66          break;
67       case STATE_SELECT_DATE_DISABLED_WITH_MARKS:
68            {
69               Evas_Object *cal = eina_list_nth(items, 0);
70               time_t sec_per_day = (60*60*24);
71               time_t sec_per_year = sec_per_day * 365;
72               time_t the_time = (sec_per_year * 41) + (sec_per_day * 10); /* Set date to JAN 01, 2011 */
73               elm_calendar_day_selection_disabled_set(cal, EINA_TRUE);
74               elm_calendar_selected_time_set(cal, gmtime(&the_time));
75            }
76          break;
77       case STATE_SELECT_DATE_DISABLED_NO_MARKS:
78            {
79               Evas_Object *cal = eina_list_nth(items, 0);
80               time_t sec_per_day = (60*60*24);
81               time_t sec_per_year = sec_per_day * 365;
82               time_t the_time = (sec_per_year * 41) + (sec_per_day * 40); /* Set date to FEB 01, 2011 */
83               elm_calendar_marks_clear(cal);
84               elm_calendar_day_selection_disabled_set(cal, EINA_TRUE);
85               elm_calendar_selected_time_set(cal, gmtime(&the_time));
86            }
87          break;
88       case API_STATE_LAST:
89          break;
90       default:
91          return;
92      }
93 }
94
95 static void
96 _api_bt_clicked(void *data, Evas_Object *obj, void *event_info __UNUSED__)
97 {  /* Will add here a SWITCH command containing code to modify test-object */
98    /* in accordance a->state value. */
99    api_data *a = data;
100    char str[128];
101
102    printf("clicked event on API Button: api_state=<%d>\n", a->state);
103    set_api_state(a);
104    a->state++;
105    sprintf(str, "Next API function (%u)", a->state);
106    elm_object_text_set(obj, str);
107    elm_object_disabled_set(obj, a->state == API_STATE_LAST);
108 }
109
110 static void
111 _cleanup_cb(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
112 {
113    free(data);
114 }
115
116 /* A simple test, just displaying calendar in it's default state */
117 void
118 test_calendar(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
119 {
120    Evas_Object *win, *cal, *bx, *bxx, *bt;
121    api_data *api = calloc(1, sizeof(api_data));
122
123    win = elm_win_util_standard_add("calendar", "Calendar");
124    elm_win_autodel_set(win, EINA_TRUE);
125    evas_object_event_callback_add(win, EVAS_CALLBACK_FREE, _cleanup_cb, api);
126
127    bxx = elm_box_add(win);
128    elm_win_resize_object_add(win, bxx);
129    evas_object_size_hint_weight_set(bxx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
130    evas_object_show(bxx);
131
132    bx = elm_box_add(win);
133    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
134    api->box = bx;
135    evas_object_show(bx);
136
137    bt = elm_button_add(win);
138    elm_object_text_set(bt, "Next API function");
139    evas_object_smart_callback_add(bt, "clicked", _api_bt_clicked, (void *) api);
140    elm_box_pack_end(bxx, bt);
141    elm_object_disabled_set(bt, api->state == API_STATE_LAST);
142    evas_object_show(bt);
143
144    elm_box_pack_end(bxx, bx);
145
146    cal = elm_calendar_add(win);
147    elm_calendar_first_day_of_week_set(cal, ELM_DAY_MONDAY);
148    evas_object_size_hint_weight_set(cal, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
149    elm_box_pack_end(bx, cal);
150
151    time_t sec_per_day = (60*60*24);
152    time_t sec_per_year = sec_per_day * 365;
153    time_t the_time = (sec_per_year * 41) + (sec_per_day * 9); /* Set date to DEC 31, 2010 */
154    elm_calendar_selected_time_set(cal, gmtime(&the_time));
155    elm_calendar_min_max_year_set(cal, 2010, 2012);
156
157    evas_object_show(cal);
158
159    evas_object_show(win);
160 }
161
162 void
163 _print_cal_info(Evas_Object *cal, Evas_Object *en)
164 {
165    char info[1024];
166    double interval;
167    int year_min, year_max;
168    Eina_Bool sel_enabled;
169    const char **wds;
170    struct tm stm;
171
172    if (!elm_calendar_selected_time_get(cal, &stm))
173      return;
174
175    interval = elm_calendar_interval_get(cal);
176    elm_calendar_min_max_year_get(cal, &year_min, &year_max);
177    sel_enabled = !elm_calendar_day_selection_disabled_get(cal);
178    wds = elm_calendar_weekdays_names_get(cal);
179
180    snprintf(info, sizeof(info),
181             "  Day: %i, Mon: %i, Year %i, WeekDay: %i<br/>"
182             "  Interval: %0.2f, Year_Min: %i, Year_Max %i, Sel Enabled : %i<br/>"
183             "  Weekdays: %s, %s, %s, %s, %s, %s, %s<br/>",
184             stm.tm_mday, stm.tm_mon, stm.tm_year + 1900, stm.tm_wday,
185             interval, year_min, year_max, sel_enabled,
186             wds[0], wds[1], wds[2], wds[3], wds[4], wds[5], wds[6]);
187
188    elm_object_text_set(en, info);
189 }
190
191 static void
192 _print_cal_info_cb(void *data, Evas_Object *obj, void *event_info __UNUSED__)
193 {
194    _print_cal_info(obj, data);
195 }
196
197 static char *
198 _format_month_year(struct tm *stm)
199 {
200    char buf[32];
201    if (!strftime(buf, sizeof(buf), "%b %y", stm)) return NULL;
202    return strdup(buf);
203 }
204
205 /* A test intended to cover all the calendar api and much use cases as
206    possible */
207 void
208 test_calendar2(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
209 {
210    Evas_Object *win, *bx, *bxh, *cal, *cal2, *cal3, *en;
211    Elm_Calendar_Mark *mark;
212    struct tm selected_time;
213    time_t current_time;
214    const char *weekdays[] =
215    {
216       "Sunday", "Monday", "Tuesday", "Wednesday",
217       "Thursday", "Friday", "Saturday"
218    };
219
220    win = elm_win_util_standard_add("calendar2", "Calendar 2");
221    elm_win_autodel_set(win, EINA_TRUE);
222
223    bx = elm_box_add(win);
224    evas_object_size_hint_weight_set(bx, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
225    elm_win_resize_object_add(win, bx);
226    evas_object_show(bx);
227
228    bxh = elm_box_add(win);
229    elm_box_horizontal_set(bxh, EINA_TRUE);
230    evas_object_size_hint_weight_set(bxh, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
231    evas_object_size_hint_align_set(bxh, EVAS_HINT_FILL, EVAS_HINT_FILL);
232    evas_object_show(bxh);
233    elm_box_pack_end(bx, bxh);
234
235    cal = elm_calendar_add(win);
236    evas_object_size_hint_weight_set(cal, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
237    evas_object_size_hint_align_set(cal, EVAS_HINT_FILL, EVAS_HINT_FILL);
238    evas_object_show(cal);
239    elm_box_pack_end(bx, cal);
240
241    cal2 = elm_calendar_add(win);
242    evas_object_size_hint_weight_set(cal2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
243    evas_object_size_hint_align_set(cal2, EVAS_HINT_FILL, EVAS_HINT_FILL);
244    elm_calendar_day_selection_disabled_set(cal2, EINA_TRUE);
245    evas_object_show(cal2);
246    elm_box_pack_end(bxh, cal2);
247
248    cal3 = elm_calendar_add(win);
249    evas_object_size_hint_weight_set(cal3, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
250    evas_object_size_hint_align_set(cal3, EVAS_HINT_FILL, EVAS_HINT_FILL);
251    current_time = time(NULL) + 34 * 84600;
252    localtime_r(&current_time, &selected_time);
253    elm_calendar_selected_time_set(cal3, &selected_time);
254    current_time = time(NULL) + 1 * 84600;
255    localtime_r(&current_time, &selected_time);
256    elm_calendar_mark_add(cal3, "checked", &selected_time, ELM_CALENDAR_UNIQUE);
257    elm_calendar_marks_clear(cal3);
258    current_time = time(NULL);
259    localtime_r(&current_time, &selected_time);
260    elm_calendar_mark_add(cal3, "checked", &selected_time, ELM_CALENDAR_DAILY);
261    elm_calendar_mark_add(cal3, "holiday", &selected_time, ELM_CALENDAR_DAILY);
262    elm_calendar_marks_draw(cal3);
263    evas_object_show(cal3);
264    elm_box_pack_end(bxh, cal3);
265
266    en = elm_entry_add(win);
267    evas_object_size_hint_weight_set(en, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
268    evas_object_size_hint_align_set(en, EVAS_HINT_FILL, EVAS_HINT_FILL);
269    evas_object_show(en);
270    elm_box_pack_end(bx, en);
271    elm_entry_editable_set(en, EINA_FALSE);
272    evas_object_show(win);
273
274    elm_calendar_min_max_year_set(cal3, -1, -1);
275
276    elm_calendar_weekdays_names_set(cal, weekdays);
277    elm_calendar_first_day_of_week_set(cal, ELM_DAY_SATURDAY);
278    elm_calendar_interval_set(cal, 0.4);
279    elm_calendar_format_function_set(cal, _format_month_year);
280    elm_calendar_min_max_year_set(cal, 2010, 2020);
281
282    current_time = time(NULL) + 4 * 84600;
283    localtime_r(&current_time, &selected_time);
284    elm_calendar_mark_add(cal, "holiday", &selected_time, ELM_CALENDAR_ANNUALLY);
285
286    current_time = time(NULL) + 1 * 84600;
287    localtime_r(&current_time, &selected_time);
288    elm_calendar_mark_add(cal, "checked", &selected_time, ELM_CALENDAR_UNIQUE);
289
290    current_time = time(NULL) - 363 * 84600;
291    localtime_r(&current_time, &selected_time);
292    elm_calendar_mark_add(cal, "checked", &selected_time, ELM_CALENDAR_MONTHLY);
293
294    current_time = time(NULL) - 5 * 84600;
295    localtime_r(&current_time, &selected_time);
296    mark = elm_calendar_mark_add(cal, "holiday", &selected_time,
297                                 ELM_CALENDAR_WEEKLY);
298
299    current_time = time(NULL) + 1 * 84600;
300    localtime_r(&current_time, &selected_time);
301    elm_calendar_mark_add(cal, "holiday", &selected_time, ELM_CALENDAR_WEEKLY);
302
303    elm_calendar_mark_del(mark);
304    elm_calendar_marks_draw(cal);
305
306    _print_cal_info(cal, en);
307    evas_object_smart_callback_add(cal, "changed", _print_cal_info_cb, en);
308 }
309
310 #endif