elm genlist: Remove item from moved_item when an item is deleted and set effect mode...
[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    Elm_Calendar_Select_Mode select_mode;
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->select_mode != ELM_CALENDAR_SELECT_MODE_NONE)
306                _select(wd, i);
307
308              wd->selected_it = i;
309           }
310
311         if ((day) && (day <= maxdays))
312           snprintf(day_s, sizeof(day_s), "%i", day++);
313         else
314           day_s[0] = 0;
315
316         snprintf(part, sizeof(part), "cit_%i.text", i);
317         edje_object_part_text_set(wd->calendar, part, day_s);
318         /* Clear previous marks */
319         _cit_mark(wd->calendar, i, "clear");
320      }
321
322    /* Set marks */
323    EINA_LIST_FOREACH(wd->marks, l, mark)
324      {
325         struct tm *mtime = &mark->mark_time;
326         int month = wd->selected_time.tm_mon;
327         int year = wd->selected_time.tm_year;
328         int mday_it = mtime->tm_mday + wd->first_day_it - 1;
329
330         switch (mark->repeat)
331           {
332            case ELM_CALENDAR_UNIQUE:
333               if ((mtime->tm_mon == month) && (mtime->tm_year == year))
334                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
335               break;
336            case ELM_CALENDAR_DAILY:
337               if (((mtime->tm_year == year) && (mtime->tm_mon < month)) ||
338                   (mtime->tm_year < year))
339                 day = 1;
340               else if ((mtime->tm_year == year) && (mtime->tm_mon == month))
341                 day = mtime->tm_mday;
342               else
343                 break;
344               for (; day <= maxdays; day++)
345                 _cit_mark(wd->calendar, day + wd->first_day_it - 1,
346                           mark->mark_type);
347               break;
348            case ELM_CALENDAR_WEEKLY:
349               if (((mtime->tm_year == year) && (mtime->tm_mon < month)) ||
350                   (mtime->tm_year < year))
351                 day = 1;
352               else if ((mtime->tm_year == year) && (mtime->tm_mon == month))
353                 day = mtime->tm_mday;
354               else
355                 break;
356               for (; day <= maxdays; day++)
357                 if (mtime->tm_wday == _weekday_get(wd->first_day_it, day))
358                   _cit_mark(wd->calendar, day + wd->first_day_it - 1,
359                             mark->mark_type);
360               break;
361            case ELM_CALENDAR_MONTHLY:
362               if (((mtime->tm_year < year) ||
363                    ((mtime->tm_year == year) && (mtime->tm_mon <= month))) &&
364                   (mtime->tm_mday <= maxdays))
365                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
366               break;
367            case ELM_CALENDAR_ANNUALLY:
368               if ((mtime->tm_year <= year) && (mtime->tm_mon == month) &&
369                   (mtime->tm_mday <= maxdays))
370                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
371               break;
372           }
373      }
374 }
375
376 static void
377 _set_headers(Evas_Object *obj)
378 {
379    static char part[] = "ch_0.text";
380    int i;
381    Widget_Data *wd = elm_widget_data_get(obj);
382    if (!wd) return;
383
384    for (i = 0; i < ELM_DAY_LAST; i++)
385      {
386         part[3] = i + '0';
387         edje_object_part_text_set(
388            wd->calendar, part,
389            wd->weekdays[(i + wd->first_week_day) % ELM_DAY_LAST]);
390      }
391 }
392
393 static void
394 _del_hook(Evas_Object *obj)
395 {
396    int i;
397    Elm_Calendar_Mark *mark;
398    Widget_Data *wd = elm_widget_data_get(obj);
399
400    if (!wd) return;
401
402    if (wd->spin) ecore_timer_del(wd->spin);
403    if (wd->update_timer) ecore_timer_del(wd->update_timer);
404
405    if (wd->marks)
406      {
407         EINA_LIST_FREE(wd->marks, mark)
408           {
409              _mark_free(mark);
410           }
411      }
412
413    for (i = 0; i < ELM_DAY_LAST; i++)
414      eina_stringshare_del(wd->weekdays[i]);
415
416    free(wd);
417 }
418
419 static void
420 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
421 {
422    Widget_Data *wd = elm_widget_data_get(obj);
423    if (!wd) return;
424    if (elm_widget_focus_get(obj))
425      {
426         edje_object_signal_emit(wd->calendar, "elm,action,focus", "elm");
427         evas_object_focus_set(wd->calendar, EINA_TRUE);
428      }
429    else
430      {
431         edje_object_signal_emit(wd->calendar, "elm,action,unfocus", "elm");
432         evas_object_focus_set(wd->calendar, EINA_FALSE);
433      }
434 }
435
436 static void
437 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
438 {
439    Widget_Data *wd = elm_widget_data_get(obj);
440    if (!wd) return;
441    edje_object_mirrored_set(wd->calendar, rtl);
442 }
443
444 static void
445 _theme_hook(Evas_Object *obj)
446 {
447    Widget_Data *wd = elm_widget_data_get(obj);
448    if (!wd) return;
449    _elm_widget_mirrored_reload(obj);
450    _elm_theme_object_set(obj, wd->calendar, "calendar", "base",
451                          elm_widget_style_get(obj));
452    _mirrored_set(obj, elm_widget_mirrored_get(obj));
453    _set_headers(obj);
454    _populate(obj);
455    edje_object_message_signal_process(wd->calendar);
456    edje_object_scale_set(wd->calendar,
457                          elm_widget_scale_get(obj) * _elm_config->scale);
458    _sizing_eval(obj);
459 }
460
461 static void
462 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
463 {
464    Widget_Data *wd = elm_widget_data_get(obj);
465    if (!wd) return;
466    edje_object_signal_emit(wd->calendar, emission, source);
467 }
468
469 static void
470 _signal_callback_add_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
471 {
472    Widget_Data *wd = elm_widget_data_get(obj);
473    if (!wd) return;
474    edje_object_signal_callback_add(wd->calendar, emission,
475                                    source, func_cb, data);
476 }
477
478 static void
479 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
480 {
481    Widget_Data *wd = elm_widget_data_get(obj);
482    if (!wd) return;
483    edje_object_signal_callback_del_full(wd->calendar, emission, source, func_cb,
484                                         data);
485 }
486
487 /* Set correct tm_wday and tm_yday after other fields changes*/
488 static inline void
489 _fix_selected_time(Widget_Data *wd)
490 {
491    mktime(&wd->selected_time);
492 }
493
494 static Eina_Bool
495 _update_month(Evas_Object *obj, int delta)
496 {
497    struct tm time_check;
498    int maxdays;
499    Widget_Data *wd = elm_widget_data_get(obj);
500    if (!wd) return EINA_FALSE;
501
502    /* check if it's a valid time. for 32 bits, year greater than 2037 is not */
503    time_check = wd->selected_time;
504    time_check.tm_mon += delta;
505    if (mktime(&time_check) == -1)
506      return EINA_FALSE;
507
508    wd->selected_time.tm_mon += delta;
509    if (wd->selected_time.tm_mon < 0)
510      {
511         if (wd->selected_time.tm_year == wd->year_min)
512           {
513              wd->selected_time.tm_mon++;
514              return EINA_FALSE;
515           }
516         wd->selected_time.tm_mon = 11;
517         wd->selected_time.tm_year--;
518      }
519    else if (wd->selected_time.tm_mon > 11)
520      {
521         if (wd->selected_time.tm_year == wd->year_max)
522           {
523              wd->selected_time.tm_mon--;
524              return EINA_FALSE;
525           }
526         wd->selected_time.tm_mon = 0;
527         wd->selected_time.tm_year++;
528      }
529
530    maxdays = _maxdays_get(&wd->selected_time);
531    if (wd->selected_time.tm_mday > maxdays)
532      wd->selected_time.tm_mday = maxdays;
533
534    _fix_selected_time(wd);
535    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
536
537    return EINA_TRUE;
538 }
539
540 static Eina_Bool
541 _spin_value(void *data)
542 {
543    Widget_Data *wd = elm_widget_data_get(data);
544    if (!wd) return ECORE_CALLBACK_CANCEL;
545    if (_update_month(data, wd->spin_speed)) _populate(data);
546    wd->interval = wd->interval / 1.05;
547    ecore_timer_interval_set(wd->spin, wd->interval);
548    return ECORE_CALLBACK_RENEW;
549 }
550
551 static void
552 _button_inc_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
553 {
554    Widget_Data *wd = elm_widget_data_get(data);
555    if (!wd) return;
556    wd->interval = wd->first_interval;
557    wd->spin_speed = 1;
558    if (wd->spin) ecore_timer_del(wd->spin);
559    wd->spin = ecore_timer_add(wd->interval, _spin_value, data);
560    _spin_value(data);
561 }
562
563 static void
564 _button_dec_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
565 {
566    Widget_Data *wd = elm_widget_data_get(data);
567    if (!wd) return;
568    wd->interval = wd->first_interval;
569    wd->spin_speed = -1;
570    if (wd->spin) ecore_timer_del(wd->spin);
571    wd->spin = ecore_timer_add(wd->interval, _spin_value, data);
572    _spin_value(data);
573 }
574
575 static void
576 _button_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
577 {
578    Widget_Data *wd = elm_widget_data_get(data);
579    if (!wd) return;
580    wd->interval = wd->first_interval;
581    if (wd->spin) ecore_timer_del(wd->spin);
582    wd->spin = NULL;
583 }
584
585 static int
586 _get_item_day(Evas_Object *obj, int selected_it)
587 {
588    int day;
589    Widget_Data *wd = elm_widget_data_get(obj);
590    if (!wd) return 0;
591
592    day = selected_it - wd->first_day_it + 1;
593    if ((day < 0) || (day > _maxdays_get(&wd->selected_time)))
594      return 0;
595
596    return day;
597 }
598
599 static void
600 _update_sel_it(Evas_Object *obj, int sel_it)
601 {
602    int day;
603    Widget_Data *wd = elm_widget_data_get(obj);
604    if ((!wd) || (wd->select_mode == ELM_CALENDAR_SELECT_MODE_NONE))
605      return;
606
607    day = _get_item_day(obj, sel_it);
608    if (!day)
609      return;
610
611    _unselect(wd, wd->selected_it);
612
613    wd->selected_it = sel_it;
614    wd->selected_time.tm_mday = day;
615    _select(wd, wd->selected_it);
616    _fix_selected_time(wd);
617    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
618 }
619
620 static void
621 _day_selected(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source)
622 {
623    int sel_it;
624    Widget_Data *wd = elm_widget_data_get(data);
625    if ((!wd) || (wd->select_mode == ELM_CALENDAR_SELECT_MODE_NONE))
626      return;
627    sel_it = atoi(source);
628
629    _update_sel_it(data, sel_it);
630 }
631
632 static inline int
633 _time_to_next_day(struct tm *t)
634 {
635    return ((((24 - t->tm_hour) * 60) - t->tm_min) * 60) - t->tm_sec;
636 }
637
638 static Eina_Bool
639 _update_cur_date(void *data)
640 {
641    time_t current_time;
642    int t, day;
643    Widget_Data *wd = elm_widget_data_get(data);
644    if (!wd) return ECORE_CALLBACK_RENEW;
645
646    if (wd->today_it > 0) _not_today(wd);
647
648    current_time = time(NULL);
649    localtime_r(&current_time, &wd->current_time);
650    t = _time_to_next_day(&wd->current_time);
651    ecore_timer_interval_set(wd->update_timer, t);
652
653    if ((wd->current_time.tm_mon != wd->selected_time.tm_mon) ||
654        (wd->current_time.tm_year!= wd->selected_time.tm_year))
655      return ECORE_CALLBACK_RENEW;
656
657    day = wd->current_time.tm_mday + wd->first_day_it - 1;
658    _today(wd, day);
659
660    return ECORE_CALLBACK_RENEW;
661 }
662
663 static Eina_Bool
664 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
665 {
666    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
667    Evas_Event_Key_Down *ev = event_info;
668    Widget_Data *wd = elm_widget_data_get(obj);
669
670    if (!wd) return EINA_FALSE;
671    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
672    if (wd->select_mode ==  ELM_CALENDAR_SELECT_MODE_NONE) return EINA_FALSE;
673
674    if ((!strcmp(ev->keyname, "Left")) ||
675        ((!strcmp(ev->keyname, "KP_Left")) && (!ev->string)))
676      {
677         _update_sel_it(obj, wd->selected_it-1);
678      }
679    else if ((!strcmp(ev->keyname, "Right")) ||
680             ((!strcmp(ev->keyname, "KP_Right")) && (!ev->string)))
681      {
682         _update_sel_it(obj, wd->selected_it+1);
683      }
684    else if ((!strcmp(ev->keyname, "Up"))  ||
685             ((!strcmp(ev->keyname, "KP_Up")) && (!ev->string)))
686      {
687         _update_sel_it(obj, wd->selected_it-ELM_DAY_LAST);
688      }
689    else if ((!strcmp(ev->keyname, "Down")) ||
690             ((!strcmp(ev->keyname, "KP_Down")) && (!ev->string)))
691      {
692         _update_sel_it(obj, wd->selected_it+ELM_DAY_LAST);
693      }
694    else if ((!strcmp(ev->keyname, "Prior")) ||
695             ((!strcmp(ev->keyname, "KP_Prior")) && (!ev->string)))
696      {
697         if (_update_month(obj, -1)) _populate(obj);
698      }
699    else if ((!strcmp(ev->keyname, "Next")) ||
700             ((!strcmp(ev->keyname, "KP_Next")) && (!ev->string)))
701      {
702         if (_update_month(obj, 1)) _populate(obj);
703      }
704    else return EINA_FALSE;
705
706    return EINA_TRUE;
707 }
708
709 EAPI Evas_Object *
710 elm_calendar_add(Evas_Object *parent)
711 {
712    time_t current_time;
713    time_t weekday = 259200; /* Just the first sunday since epoch */
714    Evas_Object *obj;
715    Widget_Data *wd;
716    int i, t;
717    Evas *e;
718
719    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
720
721    ELM_SET_WIDTYPE(widtype, "calendar");
722    elm_widget_type_set(obj, "calendar");
723    elm_widget_sub_object_add(parent, obj);
724    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
725    elm_widget_data_set(obj, wd);
726    elm_widget_del_hook_set(obj, _del_hook);
727    elm_widget_theme_hook_set(obj, _theme_hook);
728    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
729    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
730    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
731    elm_widget_can_focus_set(obj, EINA_TRUE);
732    elm_widget_event_hook_set(obj, _event_hook);
733
734    wd->first_interval = 0.85;
735    wd->year_min = 2;
736    wd->year_max = -1;
737    wd->today_it = -1;
738    wd->selected_it = -1;
739    wd->first_day_it = -1;
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 EINA_DEPRECATED EAPI void
868 elm_calendar_day_selection_disabled_set(Evas_Object *obj, Eina_Bool disabled)
869 {
870    if (disabled)
871      elm_calendar_select_mode_set(obj, ELM_CALENDAR_SELECT_MODE_NONE);
872    else
873      elm_calendar_select_mode_set(obj, ELM_CALENDAR_SELECT_MODE_DEFAULT);
874 }
875
876 EINA_DEPRECATED EAPI Eina_Bool
877 elm_calendar_day_selection_disabled_get(const Evas_Object *obj)
878 {
879    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
880    Widget_Data *wd = elm_widget_data_get(obj);
881    if (!wd) return EINA_FALSE;
882    return !!(wd->select_mode == ELM_CALENDAR_SELECT_MODE_NONE);
883 }
884
885 EAPI void
886 elm_calendar_selected_time_set(Evas_Object *obj, struct tm *selected_time)
887 {
888    ELM_CHECK_WIDTYPE(obj, widtype);
889    Widget_Data *wd = elm_widget_data_get(obj);
890    if (!wd) return;
891
892    EINA_SAFETY_ON_NULL_RETURN(selected_time);
893    wd->selected_time = *selected_time;
894    _populate(obj);
895    return;
896 }
897
898 EAPI Eina_Bool
899 elm_calendar_selected_time_get(const Evas_Object *obj, struct tm *selected_time)
900 {
901    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
902    Widget_Data *wd = elm_widget_data_get(obj);
903    if (!wd) return EINA_FALSE;
904    EINA_SAFETY_ON_NULL_RETURN_VAL(selected_time, EINA_FALSE);
905    *selected_time = wd->selected_time;
906    return EINA_TRUE;
907 }
908
909 EAPI void
910 elm_calendar_format_function_set(Evas_Object *obj, Elm_Calendar_Format_Cb format_function)
911 {
912    ELM_CHECK_WIDTYPE(obj, widtype);
913    Widget_Data *wd = elm_widget_data_get(obj);
914    if (!wd) return;
915    wd->format_func = format_function;
916    _set_month_year(wd);
917 }
918
919 EAPI Elm_Calendar_Mark *
920 elm_calendar_mark_add(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat_Type repeat)
921 {
922    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
923    Widget_Data *wd = elm_widget_data_get(obj);
924    Elm_Calendar_Mark *mark;
925    if (!wd) return NULL;
926
927    mark = _mark_new(obj, mark_type, mark_time, repeat);
928    wd->marks = eina_list_append(wd->marks, mark);
929    mark->node = eina_list_last(wd->marks);
930    return mark;
931 }
932
933 EAPI void
934 elm_calendar_mark_del(Elm_Calendar_Mark *mark)
935 {
936    Evas_Object *obj;
937    Widget_Data *wd;
938
939    EINA_SAFETY_ON_NULL_RETURN(mark);
940
941    obj = mark->obj;
942    wd = elm_widget_data_get(obj);
943    if (!wd) return;
944
945    wd->marks = eina_list_remove_list(wd->marks, mark->node);
946    _mark_free(mark);
947 }
948
949 EAPI void
950 elm_calendar_marks_clear(Evas_Object *obj)
951 {
952    ELM_CHECK_WIDTYPE(obj, widtype);
953    Widget_Data *wd = elm_widget_data_get(obj);
954    Elm_Calendar_Mark *mark;
955
956    if (!wd) return;
957    EINA_LIST_FREE(wd->marks, mark)
958       _mark_free(mark);
959 }
960
961 EAPI const Eina_List *
962 elm_calendar_marks_get(const Evas_Object *obj)
963 {
964    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
965    Widget_Data *wd = elm_widget_data_get(obj);
966    if (!wd) return NULL;
967    return wd->marks;
968 }
969
970 EAPI void
971 elm_calendar_marks_draw(Evas_Object *obj)
972 {
973    ELM_CHECK_WIDTYPE(obj, widtype);
974    Widget_Data *wd = elm_widget_data_get(obj);
975    if (!wd) return;
976    _populate(obj);
977 }
978
979 EAPI void
980 elm_calendar_first_day_of_week_set(Evas_Object *obj, Elm_Calendar_Weekday day)
981 {
982    ELM_CHECK_WIDTYPE(obj, widtype);
983    Widget_Data *wd = elm_widget_data_get(obj);
984    if (!wd) return;
985    if (day >= ELM_DAY_LAST) return;
986    if (wd->first_week_day != day)
987      {
988         wd->first_week_day = day;
989         _set_headers(obj);
990         _populate(obj);
991      }
992 }
993
994 EAPI Elm_Calendar_Weekday
995 elm_calendar_first_day_of_week_get(const Evas_Object *obj)
996 {
997    ELM_CHECK_WIDTYPE(obj, widtype) -1;
998    Widget_Data *wd = elm_widget_data_get(obj);
999    if (!wd) return -1;
1000    return wd->first_week_day;
1001 }
1002
1003 EAPI void
1004 elm_calendar_select_mode_set(Evas_Object *obj, Elm_Calendar_Select_Mode mode)
1005 {
1006    ELM_CHECK_WIDTYPE(obj, widtype);
1007    Widget_Data *wd = elm_widget_data_get(obj);
1008    if (!wd) return;
1009    if ((mode <= ELM_CALENDAR_SELECT_MODE_ONDEMAND)
1010        && (wd->select_mode != mode))
1011      {
1012         wd->select_mode = mode;
1013         if (wd->select_mode == ELM_CALENDAR_SELECT_MODE_ALWAYS)
1014           _select(wd, wd->selected_it);
1015         else
1016           _unselect(wd, wd->selected_it);
1017      }
1018 }
1019
1020 EAPI Elm_Calendar_Select_Mode
1021 elm_calendar_select_mode_get(const Evas_Object *obj)
1022 {
1023    ELM_CHECK_WIDTYPE(obj, widtype) -1;
1024    Widget_Data *wd = elm_widget_data_get(obj);
1025    if (!wd) return -1;
1026    return wd->select_mode;
1027 }
1028
1029