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