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