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