elementary/elm_entry : Fix a bug in filter callback. There can be a
[framework/uifw/elementary.git] / src / lib / elm_calendar.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4
5 #ifdef HAVE_EVIL
6 # include <Evil.h>
7 #endif
8
9 #include <Elementary.h>
10 #include "elm_priv.h"
11
12 typedef enum _Day_Color // EINA_DEPRECATED
13 {
14    DAY_WEEKDAY = 0,
15    DAY_SATURDAY = 1,
16    DAY_SUNDAY = 2
17 } Day_Color;
18
19 typedef struct _Widget_Data Widget_Data;
20
21 struct _Widget_Data
22 {
23    Evas_Object *calendar;
24    Eina_List *marks;
25    double interval, first_interval;
26    int year_min, year_max, spin_speed;
27    int today_it, selected_it, first_day_it;
28    Elm_Calendar_Weekday first_week_day;
29    Ecore_Timer *spin, *update_timer;
30    Elm_Calendar_Format_Cb format_func;
31    const char *weekdays[ELM_DAY_LAST];
32    struct tm current_time, selected_time;
33    Day_Color day_color[42]; // EINA_DEPRECATED
34    Eina_Bool selection_enabled : 1;
35 };
36
37 struct _Elm_Calendar_Mark
38 {
39    Evas_Object *obj;
40    Eina_List *node;
41    struct tm mark_time;
42    const char *mark_type;
43    Elm_Calendar_Mark_Repeat_Type repeat;
44 };
45
46 static const char *widtype = NULL;
47 static void _on_focus_hook(void *data, Evas_Object *obj);
48 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
49
50 static const char SIG_CHANGED[] = "changed";
51
52 static const Evas_Smart_Cb_Description _signals[] = {
53    {SIG_CHANGED, ""},
54    {NULL, NULL}
55 };
56
57
58 /* Should not be translated, it's used if we failed
59  * getting from locale. */
60 static const char *_days_abbrev[] =
61 {
62    "Sun", "Mon", "Tue", "Wed",
63    "Thu", "Fri", "Sat"
64 };
65
66 static int _days_in_month[2][12] =
67 {
68      {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
69      {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
70 };
71
72 static Elm_Calendar_Mark *
73 _mark_new(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat_Type repeat)
74 {
75    Widget_Data *wd = elm_widget_data_get(obj);
76    Elm_Calendar_Mark *mark;
77
78    if (!wd) return NULL;
79    mark = calloc(1, sizeof(Elm_Calendar_Mark));
80    if (!mark) return NULL;
81    mark->obj = obj;
82    mark->mark_type = eina_stringshare_add(mark_type);
83    mark->mark_time = *mark_time;
84    mark->repeat = repeat;
85    return mark;
86 }
87
88 static inline void
89 _mark_free(Elm_Calendar_Mark *mark)
90 {
91    eina_stringshare_del(mark->mark_type);
92    free(mark);
93 }
94
95 static void
96 _sizing_eval(Evas_Object *obj)
97 {
98    Widget_Data *wd = elm_widget_data_get(obj);
99    Evas_Coord minw = -1, minh = -1;
100    if (!wd) return;
101    elm_coords_finger_size_adjust(8, &minw, 7, &minh);
102    edje_object_size_min_restricted_calc(wd->calendar, &minw, &minh, minw, minh);
103    evas_object_size_hint_min_set(obj, minw, minh);
104    evas_object_size_hint_max_set(obj, -1, -1);
105 }
106
107 static inline int
108 _maxdays_get(struct tm *selected_time)
109 {
110    int month, year;
111
112    month = selected_time->tm_mon;
113    year = selected_time->tm_year + 1900;
114
115    return _days_in_month[((!(year % 4)) &&
116                           ((!(year % 400)) ||
117                            (year % 100)))]
118       [month];
119 }
120
121 static inline void
122 _unselect(Widget_Data *wd, int selected)
123 {
124    char emission[32];
125    snprintf(emission, sizeof(emission), "cit_%i,unselected", selected);
126    edje_object_signal_emit(wd->calendar, emission, "elm");
127 }
128
129 static inline void
130 _select(Widget_Data *wd, int selected)
131 {
132    char emission[32];
133    snprintf(emission, sizeof(emission), "cit_%i,selected", selected);
134    edje_object_signal_emit(wd->calendar, emission, "elm");
135 }
136
137 static inline void
138 _not_today(Widget_Data *wd)
139 {
140    char emission[32];
141    snprintf(emission, sizeof(emission), "cit_%i,not_today", wd->today_it);
142    edje_object_signal_emit(wd->calendar, emission, "elm");
143    wd->today_it = -1;
144 }
145
146 static inline void
147 _today(Widget_Data *wd, int it)
148 {
149    char emission[32];
150    snprintf(emission, sizeof(emission), "cit_%i,today", it);
151    edje_object_signal_emit(wd->calendar, emission, "elm");
152    wd->today_it = it;
153 }
154
155 static char *
156 _format_month_year(struct tm *selected_time)
157 {
158    char buf[32];
159    if (!strftime(buf, sizeof(buf), E_("%B %Y"), selected_time)) return NULL;
160    return strdup(buf);
161 }
162
163 static inline void
164 _cit_mark(Evas_Object *cal, int cit, const char *mtype)
165 {
166    char sign[64];
167    snprintf(sign, sizeof(sign), "cit_%i,%s", cit, mtype);
168    edje_object_signal_emit(cal, sign, "elm");
169 }
170
171 static inline int
172 _weekday_get(int first_week_day, int day)
173 {
174    return (day + first_week_day - 1) % ELM_DAY_LAST;
175 }
176
177 // EINA_DEPRECATED
178 static void
179 _text_day_color_update(Widget_Data *wd, int pos)
180 {
181    char emission[32];
182
183    switch (wd->day_color[pos])
184      {
185       case DAY_WEEKDAY:
186          snprintf(emission, sizeof(emission), "cit_%i,weekday", pos);
187          break;
188       case DAY_SATURDAY:
189          snprintf(emission, sizeof(emission), "cit_%i,saturday", pos);
190          break;
191       case DAY_SUNDAY:
192          snprintf(emission, sizeof(emission), "cit_%i,sunday", pos);
193          break;
194       default:
195          return;
196      }
197
198    edje_object_signal_emit(wd->calendar, emission, "elm");
199 }
200
201 static void
202 _set_month_year(Widget_Data *wd)
203 {
204    char *buf;
205
206    /* Set selected month */
207    buf = wd->format_func(&wd->selected_time);
208    if (buf)
209      {
210         edje_object_part_text_set(wd->calendar, "month_text", buf);
211         free(buf);
212      }
213    else
214      edje_object_part_text_set(wd->calendar, "month_text", "");
215 }
216
217 static void
218 _populate(Evas_Object *obj)
219 {
220    int maxdays, day, mon, yr, i;
221    Elm_Calendar_Mark *mark;
222    char part[12], day_s[3];
223    struct tm first_day;
224    Eina_List *l;
225    Eina_Bool last_row = EINA_TRUE;
226    Widget_Data *wd = elm_widget_data_get(obj);
227
228    if (!wd) return;
229
230    if (wd->today_it > 0) _not_today(wd);
231
232    maxdays = _maxdays_get(&wd->selected_time);
233    mon = wd->selected_time.tm_mon;
234    yr = wd->selected_time.tm_year;
235
236    _set_month_year(wd);
237
238    /* Set days */
239    day = 0;
240    first_day = wd->selected_time;
241    first_day.tm_mday = 1;
242    mktime(&first_day);
243
244    // Layout of the calendar is changed for removing the unfilled last row.
245    if (first_day.tm_wday < (int)wd->first_week_day)
246      wd->first_day_it = first_day.tm_wday + ELM_DAY_LAST - wd->first_week_day;
247    else
248      wd->first_day_it = first_day.tm_wday - wd->first_week_day;
249
250    if ((35 - wd->first_day_it) > (maxdays - 1)) last_row = EINA_FALSE;
251
252    if (!last_row)
253      {
254         char emission[32];
255
256         for (i = 0; i < 5; i++)
257           {
258              snprintf(emission, sizeof(emission), "cseph_%i,row_hide", i);
259              edje_object_signal_emit(wd->calendar, emission, "elm");
260           }
261         snprintf(emission, sizeof(emission), "cseph_%i,row_invisible", 5);
262         edje_object_signal_emit(wd->calendar, emission, "elm");
263         for (i = 0; i < 35; i++)
264           {
265              snprintf(emission, sizeof(emission), "cit_%i,cell_expanded", i);
266              edje_object_signal_emit(wd->calendar, emission, "elm");
267           }
268         for (i = 35; i < 42; i++)
269           {
270              snprintf(emission, sizeof(emission), "cit_%i,cell_invisible", i);
271              edje_object_signal_emit(wd->calendar, emission, "elm");
272           }
273      }
274    else
275      {
276         char emission[32];
277
278         for (i = 0; i < 6; i++)
279           {
280              snprintf(emission, sizeof(emission), "cseph_%i,row_show", i);
281              edje_object_signal_emit(wd->calendar, emission, "elm");
282           }
283         for (i = 0; i < 42; i++)
284           {
285              snprintf(emission, sizeof(emission), "cit_%i,cell_default", i);
286              edje_object_signal_emit(wd->calendar, emission, "elm");
287           }
288      }
289
290    for (i = 0; i < 42; i++)
291      {
292         _text_day_color_update(wd, i); // EINA_DEPRECATED
293         if ((!day) && (i == wd->first_day_it)) day = 1;
294
295         if ((day == wd->current_time.tm_mday)
296             && (mon == wd->current_time.tm_mon)
297             && (yr == wd->current_time.tm_year))
298           _today(wd, i);
299
300         if (day == wd->selected_time.tm_mday)
301           {
302              if ((wd->selected_it > -1) && (wd->selected_it != i))
303                _unselect(wd, wd->selected_it);
304
305              if (wd->selection_enabled) _select(wd, i);
306
307              wd->selected_it = i;
308           }
309
310         if ((day) && (day <= maxdays))
311           snprintf(day_s, sizeof(day_s), "%i", day++);
312         else
313           day_s[0] = 0;
314
315         snprintf(part, sizeof(part), "cit_%i.text", i);
316         edje_object_part_text_set(wd->calendar, part, day_s);
317         /* Clear previous marks */
318         _cit_mark(wd->calendar, i, "clear");
319      }
320
321    /* Set marks */
322    EINA_LIST_FOREACH(wd->marks, l, mark)
323      {
324         struct tm *mtime = &mark->mark_time;
325         int month = wd->selected_time.tm_mon;
326         int year = wd->selected_time.tm_year;
327         int mday_it = mtime->tm_mday + wd->first_day_it - 1;
328
329         switch (mark->repeat)
330           {
331            case ELM_CALENDAR_UNIQUE:
332               if ((mtime->tm_mon == month) && (mtime->tm_year == year))
333                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
334               break;
335            case ELM_CALENDAR_DAILY:
336               if (((mtime->tm_year == year) && (mtime->tm_mon < month)) ||
337                   (mtime->tm_year < year))
338                 day = 1;
339               else if ((mtime->tm_year == year) && (mtime->tm_mon == month))
340                 day = mtime->tm_mday;
341               else
342                 break;
343               for (; day <= maxdays; day++)
344                 _cit_mark(wd->calendar, day + wd->first_day_it - 1,
345                           mark->mark_type);
346               break;
347            case ELM_CALENDAR_WEEKLY:
348               if (((mtime->tm_year == year) && (mtime->tm_mon < month)) ||
349                   (mtime->tm_year < year))
350                 day = 1;
351               else if ((mtime->tm_year == year) && (mtime->tm_mon == month))
352                 day = mtime->tm_mday;
353               else
354                 break;
355               for (; day <= maxdays; day++)
356                 if (mtime->tm_wday == _weekday_get(wd->first_day_it, day))
357                   _cit_mark(wd->calendar, day + wd->first_day_it - 1,
358                             mark->mark_type);
359               break;
360            case ELM_CALENDAR_MONTHLY:
361               if (((mtime->tm_year < year) ||
362                    ((mtime->tm_year == year) && (mtime->tm_mon <= month))) &&
363                   (mtime->tm_mday <= maxdays))
364                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
365               break;
366            case ELM_CALENDAR_ANNUALLY:
367               if ((mtime->tm_year <= year) && (mtime->tm_mon == month) &&
368                   (mtime->tm_mday <= maxdays))
369                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
370               break;
371           }
372      }
373 }
374
375 static void
376 _set_headers(Evas_Object *obj)
377 {
378    static char part[] = "ch_0.text";
379    int i;
380    Widget_Data *wd = elm_widget_data_get(obj);
381    if (!wd) return;
382
383    for (i = 0; i < ELM_DAY_LAST; i++)
384      {
385         part[3] = i + '0';
386         edje_object_part_text_set(
387            wd->calendar, part,
388            wd->weekdays[(i + wd->first_week_day) % ELM_DAY_LAST]);
389      }
390 }
391
392 static void
393 _del_hook(Evas_Object *obj)
394 {
395    int i;
396    Elm_Calendar_Mark *mark;
397    Widget_Data *wd = elm_widget_data_get(obj);
398
399    if (!wd) return;
400
401    if (wd->spin) ecore_timer_del(wd->spin);
402    if (wd->update_timer) ecore_timer_del(wd->update_timer);
403
404    if (wd->marks)
405      {
406         EINA_LIST_FREE(wd->marks, mark)
407           {
408              _mark_free(mark);
409           }
410      }
411
412    for (i = 0; i < ELM_DAY_LAST; i++)
413      eina_stringshare_del(wd->weekdays[i]);
414
415    free(wd);
416 }
417
418 static void
419 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
420 {
421    Widget_Data *wd = elm_widget_data_get(obj);
422    if (!wd) return;
423    if (elm_widget_focus_get(obj))
424      {
425         edje_object_signal_emit(wd->calendar, "elm,action,focus", "elm");
426         evas_object_focus_set(wd->calendar, EINA_TRUE);
427      }
428    else
429      {
430         edje_object_signal_emit(wd->calendar, "elm,action,unfocus", "elm");
431         evas_object_focus_set(wd->calendar, EINA_FALSE);
432      }
433 }
434
435 static void
436 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
437 {
438    Widget_Data *wd = elm_widget_data_get(obj);
439    if (!wd) return;
440    edje_object_mirrored_set(wd->calendar, rtl);
441 }
442
443 static void
444 _theme_hook(Evas_Object *obj)
445 {
446    Widget_Data *wd = elm_widget_data_get(obj);
447    if (!wd) return;
448    _elm_widget_mirrored_reload(obj);
449    _elm_theme_object_set(obj, wd->calendar, "calendar", "base",
450                          elm_widget_style_get(obj));
451    _mirrored_set(obj, elm_widget_mirrored_get(obj));
452    _set_headers(obj);
453    _populate(obj);
454    edje_object_message_signal_process(wd->calendar);
455    edje_object_scale_set(wd->calendar,
456                          elm_widget_scale_get(obj) * _elm_config->scale);
457    _sizing_eval(obj);
458 }
459
460 static void
461 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
462 {
463    Widget_Data *wd = elm_widget_data_get(obj);
464    if (!wd) return;
465    edje_object_signal_emit(wd->calendar, emission, source);
466 }
467
468 static void
469 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
470 {
471    Widget_Data *wd = elm_widget_data_get(obj);
472    if (!wd) return;
473    edje_object_signal_callback_add(wd->calendar, emission,
474                                    source, func_cb, data);
475 }
476
477 static void
478 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
479 {
480    Widget_Data *wd = elm_widget_data_get(obj);
481    if (!wd) return;
482    edje_object_signal_callback_del_full(wd->calendar, emission, source, func_cb,
483                                         data);
484 }
485
486 /* Set correct tm_wday and tm_yday after other fields changes*/
487 static inline void
488 _fix_selected_time(Widget_Data *wd)
489 {
490    mktime(&wd->selected_time);
491 }
492
493 static Eina_Bool
494 _update_month(Evas_Object *obj, int delta)
495 {
496    struct tm time_check;
497    int maxdays;
498    Widget_Data *wd = elm_widget_data_get(obj);
499    if (!wd) return EINA_FALSE;
500
501    /* check if it's a valid time. for 32 bits, year greater than 2037 is not */
502    time_check = wd->selected_time;
503    time_check.tm_mon += delta;
504    if (mktime(&time_check) == -1)
505      return EINA_FALSE;
506
507    wd->selected_time.tm_mon += delta;
508    if (wd->selected_time.tm_mon < 0)
509      {
510         if (wd->selected_time.tm_year == wd->year_min)
511           {
512              wd->selected_time.tm_mon++;
513              return EINA_FALSE;
514           }
515         wd->selected_time.tm_mon = 11;
516         wd->selected_time.tm_year--;
517      }
518    else if (wd->selected_time.tm_mon > 11)
519      {
520         if (wd->selected_time.tm_year == wd->year_max)
521           {
522              wd->selected_time.tm_mon--;
523              return EINA_FALSE;
524           }
525         wd->selected_time.tm_mon = 0;
526         wd->selected_time.tm_year++;
527      }
528
529    maxdays = _maxdays_get(&wd->selected_time);
530    if (wd->selected_time.tm_mday > maxdays)
531      wd->selected_time.tm_mday = maxdays;
532
533    _fix_selected_time(wd);
534    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
535
536    return EINA_TRUE;
537 }
538
539 static Eina_Bool
540 _spin_value(void *data)
541 {
542    Widget_Data *wd = elm_widget_data_get(data);
543    if (!wd) return ECORE_CALLBACK_CANCEL;
544    if (_update_month(data, wd->spin_speed)) _populate(data);
545    wd->interval = wd->interval / 1.05;
546    ecore_timer_interval_set(wd->spin, wd->interval);
547    return ECORE_CALLBACK_RENEW;
548 }
549
550 static void
551 _button_inc_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
552 {
553    Widget_Data *wd = elm_widget_data_get(data);
554    if (!wd) return;
555    wd->interval = wd->first_interval;
556    wd->spin_speed = 1;
557    if (wd->spin) ecore_timer_del(wd->spin);
558    wd->spin = ecore_timer_add(wd->interval, _spin_value, data);
559    _spin_value(data);
560 }
561
562 static void
563 _button_dec_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
564 {
565    Widget_Data *wd = elm_widget_data_get(data);
566    if (!wd) return;
567    wd->interval = wd->first_interval;
568    wd->spin_speed = -1;
569    if (wd->spin) ecore_timer_del(wd->spin);
570    wd->spin = ecore_timer_add(wd->interval, _spin_value, data);
571    _spin_value(data);
572 }
573
574 static void
575 _button_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
576 {
577    Widget_Data *wd = elm_widget_data_get(data);
578    if (!wd) return;
579    wd->interval = wd->first_interval;
580    if (wd->spin) ecore_timer_del(wd->spin);
581    wd->spin = NULL;
582 }
583
584 static int
585 _get_item_day(Evas_Object *obj, int selected_it)
586 {
587    int day;
588    Widget_Data *wd = elm_widget_data_get(obj);
589    if (!wd) return 0;
590
591    day = selected_it - wd->first_day_it + 1;
592    if ((day < 0) || (day > _maxdays_get(&wd->selected_time)))
593      return 0;
594
595    return day;
596 }
597
598 static void
599 _update_sel_it(Evas_Object *obj, int sel_it)
600 {
601    int day;
602    Widget_Data *wd = elm_widget_data_get(obj);
603    if ((!wd) || (!wd->selection_enabled))
604      return;
605
606    day = _get_item_day(obj, sel_it);
607    if (!day)
608      return;
609
610    _unselect(wd, wd->selected_it);
611
612    wd->selected_it = sel_it;
613    wd->selected_time.tm_mday = day;
614    _select(wd, wd->selected_it);
615    _fix_selected_time(wd);
616    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
617 }
618
619 static void
620 _day_selected(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source)
621 {
622    int sel_it;
623    Widget_Data *wd = elm_widget_data_get(data);
624    if ((!wd) || (!wd->selection_enabled))
625      return;
626    sel_it = atoi(source);
627
628    _update_sel_it(data, sel_it);
629 }
630
631 static inline int
632 _time_to_next_day(struct tm *t)
633 {
634    return ((((24 - t->tm_hour) * 60) - t->tm_min) * 60) - t->tm_sec;
635 }
636
637 static Eina_Bool
638 _update_cur_date(void *data)
639 {
640    time_t current_time;
641    int t, day;
642    Widget_Data *wd = elm_widget_data_get(data);
643    if (!wd) return ECORE_CALLBACK_RENEW;
644
645    if (wd->today_it > 0) _not_today(wd);
646
647    current_time = time(NULL);
648    localtime_r(&current_time, &wd->current_time);
649    t = _time_to_next_day(&wd->current_time);
650    ecore_timer_interval_set(wd->update_timer, t);
651
652    if ((wd->current_time.tm_mon != wd->selected_time.tm_mon) ||
653        (wd->current_time.tm_year!= wd->selected_time.tm_year))
654      return ECORE_CALLBACK_RENEW;
655
656    day = wd->current_time.tm_mday + wd->first_day_it - 1;
657    _today(wd, day);
658
659    return ECORE_CALLBACK_RENEW;
660 }
661
662 static Eina_Bool
663 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
664 {
665    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
666    Evas_Event_Key_Down *ev = event_info;
667    Widget_Data *wd = elm_widget_data_get(obj);
668
669    if (!wd) return EINA_FALSE;
670    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
671    if (!wd->selection_enabled) return EINA_FALSE;
672
673    if ((!strcmp(ev->keyname, "Left")) ||
674        ((!strcmp(ev->keyname, "KP_Left")) && (!ev->string)))
675      {
676         _update_sel_it(obj, wd->selected_it-1);
677      }
678    else if ((!strcmp(ev->keyname, "Right")) ||
679             ((!strcmp(ev->keyname, "KP_Right")) && (!ev->string)))
680      {
681         _update_sel_it(obj, wd->selected_it+1);
682      }
683    else if ((!strcmp(ev->keyname, "Up"))  ||
684             ((!strcmp(ev->keyname, "KP_Up")) && (!ev->string)))
685      {
686         _update_sel_it(obj, wd->selected_it-ELM_DAY_LAST);
687      }
688    else if ((!strcmp(ev->keyname, "Down")) ||
689             ((!strcmp(ev->keyname, "KP_Down")) && (!ev->string)))
690      {
691         _update_sel_it(obj, wd->selected_it+ELM_DAY_LAST);
692      }
693    else if ((!strcmp(ev->keyname, "Prior")) ||
694             ((!strcmp(ev->keyname, "KP_Prior")) && (!ev->string)))
695      {
696         if (_update_month(obj, -1)) _populate(obj);
697      }
698    else if ((!strcmp(ev->keyname, "Next")) ||
699             ((!strcmp(ev->keyname, "KP_Next")) && (!ev->string)))
700      {
701         if (_update_month(obj, 1)) _populate(obj);
702      }
703    else return EINA_FALSE;
704
705    return EINA_TRUE;
706 }
707
708 EAPI Evas_Object *
709 elm_calendar_add(Evas_Object *parent)
710 {
711    time_t current_time;
712    time_t weekday = 259200; /* Just the first sunday since epoch */
713    Evas_Object *obj;
714    Widget_Data *wd;
715    int i, t;
716    Evas *e;
717
718    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
719
720    ELM_SET_WIDTYPE(widtype, "calendar");
721    elm_widget_type_set(obj, "calendar");
722    elm_widget_sub_object_add(parent, obj);
723    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
724    elm_widget_data_set(obj, wd);
725    elm_widget_del_hook_set(obj, _del_hook);
726    elm_widget_theme_hook_set(obj, _theme_hook);
727    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
728    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
729    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
730    elm_widget_can_focus_set(obj, EINA_TRUE);
731    elm_widget_event_hook_set(obj, _event_hook);
732
733    wd->first_interval = 0.85;
734    wd->year_min = 2;
735    wd->year_max = -1;
736    wd->today_it = -1;
737    wd->selected_it = -1;
738    wd->first_day_it = -1;
739    wd->selection_enabled = EINA_TRUE;
740    wd->format_func = _format_month_year;
741    wd->marks = NULL;
742
743    wd->calendar = edje_object_add(e);
744    _elm_theme_object_set(obj, wd->calendar, "calendar", "base", "default");
745    elm_widget_resize_object_set(obj, wd->calendar);
746
747    edje_object_signal_callback_add(wd->calendar, "elm,action,increment,start",
748                                    "*", _button_inc_start, obj);
749    edje_object_signal_callback_add(wd->calendar, "elm,action,decrement,start",
750                                    "*", _button_dec_start, obj);
751    edje_object_signal_callback_add(wd->calendar, "elm,action,stop",
752                                    "*", _button_stop, obj);
753    edje_object_signal_callback_add(wd->calendar, "elm,action,selected",
754                                    "*", _day_selected, obj);
755
756    evas_object_smart_callbacks_descriptions_set(obj, _signals);
757
758    for (i = 0; i < ELM_DAY_LAST; i++)
759      {
760         /* FIXME: I'm not aware of a known max, so if it fails,
761          * just make it larger. :| */
762         char buf[20];
763         /* I don't know of a better way of doing it */
764         if (strftime(buf, sizeof(buf), "%a", gmtime(&weekday)))
765           {
766              wd->weekdays[i] = eina_stringshare_add(buf);
767           }
768         else
769           {
770              /* If we failed getting day, get a default value */
771              wd->weekdays[i] = _days_abbrev[i];
772              WRN("Failed getting weekday name for '%s' from locale.",
773                  _days_abbrev[i]);
774           }
775         weekday += 86400; /* Advance by a day */
776      }
777
778    current_time = time(NULL);
779    localtime_r(&current_time, &wd->selected_time);
780    wd->current_time = wd->selected_time;
781    t = _time_to_next_day(&wd->current_time);
782    wd->update_timer = ecore_timer_add(t, _update_cur_date, obj);
783
784    _set_headers(obj);
785    _populate(obj);
786    _mirrored_set(obj, elm_widget_mirrored_get(obj));
787    _sizing_eval(obj);
788    return obj;
789 }
790
791 EAPI void
792 elm_calendar_weekdays_names_set(Evas_Object *obj, const char *weekdays[])
793 {
794    int i;
795    ELM_CHECK_WIDTYPE(obj, widtype);
796    Widget_Data *wd = elm_widget_data_get(obj);
797    if (!wd) return;
798
799    EINA_SAFETY_ON_NULL_RETURN(weekdays);
800
801    for (i = 0; i < ELM_DAY_LAST; i++)
802      {
803         eina_stringshare_replace(&wd->weekdays[i], weekdays[i]);
804      }
805    _set_headers(obj);
806 }
807
808 EAPI const char **
809 elm_calendar_weekdays_names_get(const Evas_Object *obj)
810 {
811    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
812    Widget_Data *wd = elm_widget_data_get(obj);
813    if (!wd) return NULL;
814    return wd->weekdays;
815 }
816
817 EAPI void
818 elm_calendar_interval_set(Evas_Object *obj, double interval)
819 {
820    ELM_CHECK_WIDTYPE(obj, widtype);
821    Widget_Data *wd = elm_widget_data_get(obj);
822    if (!wd) return;
823    wd->first_interval = interval;
824 }
825
826 EAPI double
827 elm_calendar_interval_get(const Evas_Object *obj)
828 {
829    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
830    Widget_Data *wd = elm_widget_data_get(obj);
831    if (!wd) return 0.0;
832    return wd->first_interval;
833 }
834
835 EAPI void
836 elm_calendar_min_max_year_set(Evas_Object *obj, int min, int max)
837 {
838    ELM_CHECK_WIDTYPE(obj, widtype);
839    Widget_Data *wd = elm_widget_data_get(obj);
840    if (!wd) return;
841    min -= 1900;
842    max -= 1900;
843    if ((wd->year_min == min) && (wd->year_max == max)) return;
844    wd->year_min = min > 2 ? min : 2;
845    if (max > wd->year_min)
846      wd->year_max = max;
847    else
848      wd->year_max = wd->year_min;
849    if (wd->selected_time.tm_year > wd->year_max)
850      wd->selected_time.tm_year = wd->year_max;
851    if (wd->selected_time.tm_year < wd->year_min)
852      wd->selected_time.tm_year = wd->year_min;
853    _fix_selected_time(wd);
854    _populate(obj);
855 }
856
857 EAPI void
858 elm_calendar_min_max_year_get(const Evas_Object *obj, int *min, int *max)
859 {
860    ELM_CHECK_WIDTYPE(obj, widtype);
861    Widget_Data *wd = elm_widget_data_get(obj);
862    if (!wd) return;
863    if (min) *min = wd->year_min + 1900;
864    if (max) *max = wd->year_max + 1900;
865 }
866
867 EAPI void
868 elm_calendar_day_selection_disabled_set(Evas_Object *obj, Eina_Bool disabled)
869 {
870    ELM_CHECK_WIDTYPE(obj, widtype);
871    Widget_Data *wd = elm_widget_data_get(obj);
872    if (!wd) return;
873    wd->selection_enabled = (!disabled);
874    if (!disabled)
875      _select(wd, wd->selected_it);
876    else
877      _unselect(wd, wd->selected_it);
878 }
879
880 EAPI Eina_Bool
881 elm_calendar_day_selection_disabled_get(const Evas_Object *obj)
882 {
883    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
884    Widget_Data *wd = elm_widget_data_get(obj);
885    if (!wd) return EINA_FALSE;
886    return (!wd->selection_enabled);
887 }
888
889 EAPI void
890 elm_calendar_selected_time_set(Evas_Object *obj, struct tm *selected_time)
891 {
892    ELM_CHECK_WIDTYPE(obj, widtype);
893    Widget_Data *wd = elm_widget_data_get(obj);
894    if (!wd) return;
895
896    EINA_SAFETY_ON_NULL_RETURN(selected_time);
897    wd->selected_time = *selected_time;
898    _populate(obj);
899    return;
900 }
901
902 EAPI Eina_Bool
903 elm_calendar_selected_time_get(const Evas_Object *obj, struct tm *selected_time)
904 {
905    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
906    Widget_Data *wd = elm_widget_data_get(obj);
907    if (!wd) return EINA_FALSE;
908    EINA_SAFETY_ON_NULL_RETURN_VAL(selected_time, EINA_FALSE);
909    *selected_time = wd->selected_time;
910    return EINA_TRUE;
911 }
912
913 EAPI void
914 elm_calendar_format_function_set(Evas_Object *obj, Elm_Calendar_Format_Cb format_function)
915 {
916    ELM_CHECK_WIDTYPE(obj, widtype);
917    Widget_Data *wd = elm_widget_data_get(obj);
918    if (!wd) return;
919    wd->format_func = format_function;
920    _set_month_year(wd);
921 }
922
923 EAPI Elm_Calendar_Mark *
924 elm_calendar_mark_add(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat_Type repeat)
925 {
926    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
927    Widget_Data *wd = elm_widget_data_get(obj);
928    Elm_Calendar_Mark *mark;
929    if (!wd) return NULL;
930
931    mark = _mark_new(obj, mark_type, mark_time, repeat);
932    wd->marks = eina_list_append(wd->marks, mark);
933    mark->node = eina_list_last(wd->marks);
934    return mark;
935 }
936
937 EAPI void
938 elm_calendar_mark_del(Elm_Calendar_Mark *mark)
939 {
940    Evas_Object *obj;
941    Widget_Data *wd;
942
943    EINA_SAFETY_ON_NULL_RETURN(mark);
944
945    obj = mark->obj;
946    wd = elm_widget_data_get(obj);
947    if (!wd) return;
948
949    wd->marks = eina_list_remove_list(wd->marks, mark->node);
950    _mark_free(mark);
951 }
952
953 EAPI void
954 elm_calendar_marks_clear(Evas_Object *obj)
955 {
956    ELM_CHECK_WIDTYPE(obj, widtype);
957    Widget_Data *wd = elm_widget_data_get(obj);
958    Elm_Calendar_Mark *mark;
959
960    if (!wd) return;
961    EINA_LIST_FREE(wd->marks, mark)
962       _mark_free(mark);
963 }
964
965 EAPI const Eina_List *
966 elm_calendar_marks_get(const Evas_Object *obj)
967 {
968    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
969    Widget_Data *wd = elm_widget_data_get(obj);
970    if (!wd) return NULL;
971    return wd->marks;
972 }
973
974 EAPI void
975 elm_calendar_marks_draw(Evas_Object *obj)
976 {
977    ELM_CHECK_WIDTYPE(obj, widtype);
978    Widget_Data *wd = elm_widget_data_get(obj);
979    if (!wd) return;
980    _populate(obj);
981 }
982
983 EAPI void
984 elm_calendar_first_day_of_week_set(Evas_Object *obj, Elm_Calendar_Weekday day)
985 {
986    ELM_CHECK_WIDTYPE(obj, widtype);
987    Widget_Data *wd = elm_widget_data_get(obj);
988    if (!wd) return;
989    if (day >= ELM_DAY_LAST) return;
990    if (wd->first_week_day != day)
991      {
992         wd->first_week_day = day;
993         _set_headers(obj);
994         _populate(obj);
995      }
996 }
997
998 EAPI Elm_Calendar_Weekday
999 elm_calendar_first_day_of_week_get(const Evas_Object *obj)
1000 {
1001    ELM_CHECK_WIDTYPE(obj, widtype) -1;
1002    Widget_Data *wd = elm_widget_data_get(obj);
1003    if (!wd) return -1;
1004    return wd->first_week_day;
1005 }