make all widgets use a standard setup macro. cuts code down and
[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 /**
13  * @defgroup Calendar
14  *
15  * A calendar is a widget that allows the user to select a date. It has
16  * support to adding check marks (holidays and checks by default). The calendar
17  * is displayed one month at a time.
18  *
19  * Weekday names and the function used to format month and year to
20  * be displayed can be set, giving more flexibility to this widget.
21  *
22  * Signals that you can add callbacks for are:
23  *
24  * changed - emitted when the user selects a day or changes the displayed
25  * month, what actually changes the selected day as well.
26  */
27
28 typedef enum _Day_Color // EINA_DEPRECATED
29 {
30   DAY_WEEKDAY = 0,
31   DAY_SATURDAY = 1,
32   DAY_SUNDAY = 2
33 } Day_Color;
34
35 typedef struct _Widget_Data Widget_Data;
36
37 struct _Widget_Data
38 {
39    Evas_Object *calendar;
40    Eina_List *marks;
41    double interval, first_interval;
42    int year_min, year_max, spin_speed;
43    int today_it, selected_it, first_day_it;
44    Ecore_Timer *spin, *update_timer;
45    char * (*format_func) (struct tm *stime);
46    const char *weekdays[7];
47    struct tm current_time, selected_time;
48    Day_Color day_color[42]; // EINA_DEPRECATED
49    Eina_Bool selection_enabled : 1;
50 };
51
52 struct _Elm_Calendar_Mark
53 {
54    Evas_Object *obj;
55    Eina_List *node;
56    struct tm mark_time;
57    const char *mark_type;
58    Elm_Calendar_Mark_Repeat repeat;
59 };
60
61 static const char *widtype = NULL;
62 static void _on_focus_hook(void *data, Evas_Object *obj);
63 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
64
65 /* Should not be translated, it's used if we failed
66  * getting from locale. */
67 static const char *_days_abbrev[] =
68 {
69    "Sun", "Mon", "Tue", "Wed",
70    "Thu", "Fri", "Sat"
71 };
72
73 static int _days_in_month[2][12] =
74 {
75    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
76    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
77 };
78
79 static Elm_Calendar_Mark *
80 _mark_new(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat repeat)
81 {
82    Widget_Data *wd = elm_widget_data_get(obj);
83    Elm_Calendar_Mark *mark;
84
85    if (!wd) return NULL;
86    mark = calloc(1, sizeof(Elm_Calendar_Mark));
87    if (!mark) return NULL;
88    mark->obj = obj;
89    mark->mark_type = eina_stringshare_add(mark_type);
90    mark->mark_time = *mark_time;
91    mark->repeat = repeat;
92    return mark;
93 }
94
95 static inline void
96 _mark_free(Elm_Calendar_Mark *mark)
97 {
98    eina_stringshare_del(mark->mark_type);
99    free(mark);
100 }
101
102 static void
103 _sizing_eval(Evas_Object *obj)
104 {
105    Widget_Data *wd = elm_widget_data_get(obj);
106    Evas_Coord minw = -1, minh = -1;
107    if (!wd) return;
108    elm_coords_finger_size_adjust(8, &minw, 7, &minh);
109    edje_object_size_min_restricted_calc(wd->calendar, &minw, &minh, minw, minh);
110    evas_object_size_hint_min_set(obj, minw, minh);
111    evas_object_size_hint_max_set(obj, -1, -1);
112 }
113
114 static inline int
115 _maxdays_get(struct tm *time)
116 {
117    int month, year;
118
119    month = time->tm_mon;
120    year = time->tm_year + 1900;
121
122    return _days_in_month[((!(year % 4)) && 
123                           ((!(year % 400)) || 
124                               (year % 100)))]
125                         [month];
126 }
127
128 static inline void
129 _unselect(Widget_Data *wd, int selected)
130 {
131    char emission[32];
132    snprintf(emission, sizeof(emission), "cit_%i,unselected", selected);
133    edje_object_signal_emit(wd->calendar, emission, "elm");
134 }
135
136 static inline void
137 _select(Widget_Data *wd, int selected)
138 {
139    char emission[32];
140    snprintf(emission, sizeof(emission), "cit_%i,selected", selected);
141    edje_object_signal_emit(wd->calendar, emission, "elm");
142 }
143
144 static inline void
145 _not_today(Widget_Data *wd)
146 {
147    char emission[32];
148    snprintf(emission, sizeof(emission), "cit_%i,not_today", wd->today_it);
149    edje_object_signal_emit(wd->calendar, emission, "elm");
150    wd->today_it = -1;
151 }
152
153 static inline void
154 _today(Widget_Data *wd, int it)
155 {
156    char emission[32];
157    snprintf(emission, sizeof(emission), "cit_%i,today", it);
158    edje_object_signal_emit(wd->calendar, emission, "elm");
159    wd->today_it = it;
160 }
161
162 static char *
163 _format_month_year(struct tm *stime)
164 {
165    char buf[32];
166    if (!strftime(buf, sizeof(buf), "%B %Y", stime)) return NULL;
167    return strdup(buf);
168 }
169
170 static inline void
171 _cit_mark(Evas_Object *cal, int cit, const char *mtype)
172 {
173    char sign[64];
174    snprintf(sign, sizeof(sign), "cit_%i,%s", cit, mtype);
175    edje_object_signal_emit(cal, sign, "elm");
176 }
177
178 static inline int
179 _weekday_get(int first_week_day, int day)
180 {
181    return (day + first_week_day - 1) % 7;
182 }
183
184 // EINA_DEPRECATED
185 static void
186 _text_day_color_update(Widget_Data *wd, int pos)
187 {
188    char emission[32];
189   
190    switch (wd->day_color[pos])
191     {
192     case DAY_WEEKDAY:
193       snprintf(emission, sizeof(emission), "cit_%i,weekday", pos);
194       break;
195     case DAY_SATURDAY:
196       snprintf(emission, sizeof(emission), "cit_%i,saturday", pos);
197       break;
198     case DAY_SUNDAY:
199       snprintf(emission, sizeof(emission), "cit_%i,sunday", pos);
200       break;
201     default:
202       return;
203     }
204   
205    edje_object_signal_emit(wd->calendar, emission, "elm");
206 }
207
208 // EINA_DEPRECATED
209 static void
210 _text_day_color_set(Widget_Data *wd, Day_Color col, int pos)
211 {
212    if ((pos < 0) || (pos >= 42)) return;
213    if (wd->day_color[pos] == col) return;
214    wd->day_color[pos] = col;
215    _text_day_color_update(wd, pos);
216 }
217
218 static void
219 _populate(Evas_Object *obj)
220 {
221    int maxdays, day, mon, year, i;
222    Elm_Calendar_Mark *mark;
223    char part[12], day_s[3];
224    struct tm first_day;
225    Eina_List *l;
226    char *buf;
227    Eina_Bool last_row = EINA_TRUE;
228    Widget_Data *wd = elm_widget_data_get(obj);
229
230    if (!wd) return;
231
232    if (wd->today_it > 0) _not_today(wd);
233
234    maxdays = _maxdays_get(&wd->selected_time);
235    mon = wd->selected_time.tm_mon;
236    year = wd->selected_time.tm_year;
237
238    /* Set selected month */
239    buf = wd->format_func(&wd->selected_time);
240    if (buf)
241      {
242        edje_object_part_text_set(wd->calendar, "month_text", buf);
243        free(buf);
244      }
245    else
246      edje_object_part_text_set(wd->calendar, "month_text", "");
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 mon = 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 == mon) && (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 < mon)) ||
344                     (mtime->tm_year < year))
345                 day = 1;
346               else if ((mtime->tm_year == year) && (mtime->tm_mon == mon))
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 < mon)) ||
356                     (mtime->tm_year < year))
357                 day = 1;
358               else if ((mtime->tm_year == year) && (mtime->tm_mon == mon))
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 <= mon))) &&
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 == mon) &&
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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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, void (*func_cb) (void *data, Evas_Object *o, const char *emission, const char *source), 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, "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, "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 /**
714  * Add a new calendar to the parent
715  *
716  * @param parent The parent object
717  * @return The new object or NULL if it cannot be created
718  *
719  * @ingroup Calendar
720  */
721 EAPI Evas_Object *
722 elm_calendar_add(Evas_Object *parent)
723 {
724    time_t current_time;
725    time_t weekday = 259200; /* Just the first sunday since epoch */
726    Evas_Object *obj;
727    Widget_Data *wd;
728    int i, t;
729    Evas *e;
730
731    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
732    
733    ELM_SET_WIDTYPE(widtype, "calendar");
734    elm_widget_type_set(obj, "calendar");
735    elm_widget_sub_object_add(parent, obj);
736    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
737    elm_widget_data_set(obj, wd);
738    elm_widget_del_hook_set(obj, _del_hook);
739    elm_widget_theme_hook_set(obj, _theme_hook);
740    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
741    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
742    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
743    elm_widget_can_focus_set(obj, EINA_TRUE);
744    elm_widget_event_hook_set(obj, _event_hook);
745
746    wd->first_interval = 0.85;
747    wd->year_min = 2;
748    wd->year_max = -1;
749    wd->today_it = -1;
750    wd->selected_it = -1;
751    wd->first_day_it = -1;
752    wd->selection_enabled = EINA_TRUE;
753    wd->format_func = _format_month_year;
754    wd->marks = NULL;
755
756    wd->calendar = edje_object_add(e);
757    _elm_theme_object_set(obj, wd->calendar, "calendar", "base", "default");
758    elm_widget_resize_object_set(obj, wd->calendar);
759
760    edje_object_signal_callback_add(wd->calendar, "elm,action,increment,start",
761                                    "*", _button_inc_start, obj);
762    edje_object_signal_callback_add(wd->calendar, "elm,action,decrement,start",
763                                    "*", _button_dec_start, obj);
764    edje_object_signal_callback_add(wd->calendar, "elm,action,stop",
765                                    "*", _button_stop, obj);
766    edje_object_signal_callback_add(wd->calendar, "elm,action,selected",
767                                    "*", _day_selected, obj);
768
769    for (i = 0; i < 7; i++)
770      {
771         /* FIXME: I'm not aware of a known max, so if it fails,
772          * just make it larger. :| */
773         char buf[20];
774         /* I don't know of a better way of doing it */
775         if (strftime(buf, sizeof(buf), "%a", gmtime(&weekday)))
776           {
777              wd->weekdays[i] = eina_stringshare_add(buf);
778           }
779         else
780           {
781              /* If we failed getting day, get a default value */
782              wd->weekdays[i] = _days_abbrev[i];
783              WRN("Failed getting weekday name for '%s' from locale.",
784                   _days_abbrev[i]);
785           }
786         weekday += 86400; /* Advance by a day */
787      }
788
789    current_time = time(NULL);
790    localtime_r(&current_time, &wd->selected_time);
791    wd->current_time = wd->selected_time;
792    t = _time_to_next_day(&wd->current_time);
793    wd->update_timer = ecore_timer_add(t, _update_cur_date, obj);
794
795    _set_headers(obj);
796    _populate(obj);
797    _mirrored_set(obj, elm_widget_mirrored_get(obj));
798    _sizing_eval(obj);
799    return obj;
800 }
801
802 /**
803  * Set weekdays names to display in the calendar.
804  *
805  * By default, the following abbreviations are displayed:
806  * "Sun, Mon, Tue, Wed, Thu, Fri, Sat"
807  * The first string should be related to Sunday, the second to Monday...
808  *
809  * The usage should be like this:
810  * @code
811  *   const char *weekdays[] =
812  *   {
813  *      "Sunday", "Monday", "Tuesday", "Wednesday",
814  *      "Thursday", "Friday", "Saturday"
815  *   };
816  *   elm_calendar_weekdays_names_set(calendar, weekdays);
817  * @endcode
818  *
819  * @param obj The calendar object
820  * @param weedays Array of seven strings to be used as weekday names.
821  * Warning: it must have 7 elements, or it will access invalid memory.
822  * The strings must be NULL terminated ('@\0').
823  *
824  * @ingroup Calendar
825  */
826 EAPI void
827 elm_calendar_weekdays_names_set(Evas_Object *obj, const char *weekdays[])
828 {
829    int i;
830    ELM_CHECK_WIDTYPE(obj, widtype);
831    Widget_Data *wd = elm_widget_data_get(obj);
832    if (!wd) return;
833
834    EINA_SAFETY_ON_NULL_RETURN(weekdays);
835
836    for (i = 0; i < 7; i++)
837      {
838         eina_stringshare_replace(&wd->weekdays[i], weekdays[i]);
839      }
840    _set_headers(obj);
841 }
842
843 /**
844  * Get weekdays names displayed in the calendar.
845  *
846  * By default, the following abbreviations are displayed:
847  * "Sun, Mon, Tue, Wed, Thu, Fri, Sat"
848  * The first string is related to Sunday, the second to Monday...
849  *
850  * @param obj The calendar object
851  * @return Array of seven strings to used as weekday names.
852  *
853  * @ingroup Calendar
854  */
855 EAPI const char **
856 elm_calendar_weekdays_names_get(const Evas_Object *obj)
857 {
858    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
859    Widget_Data *wd = elm_widget_data_get(obj);
860    if (!wd) return NULL;
861    return wd->weekdays;
862 }
863
864 /**
865  * Set the interval for the calendar
866  *
867  * The interval value is decreased while the user increments or decrements
868  * the calendar value. The next interval value is the previous interval / 1.05,
869  * so it speed up a bit. Default value is 0.85 seconds.
870  *
871  * @param obj The calendar object
872  * @param interval The interval value in seconds
873  *
874  * @ingroup Calendar
875  */
876 EAPI void
877 elm_calendar_interval_set(Evas_Object *obj, double interval)
878 {
879    ELM_CHECK_WIDTYPE(obj, widtype);
880    Widget_Data *wd = elm_widget_data_get(obj);
881    if (!wd) return;
882    wd->first_interval = interval;
883 }
884
885 /**
886  * Get the interval of the calendar
887  *
888  * The interval value is decreased while the user increments or decrements
889  * the calendar value. The next interval value is the previous interval / 1.05,
890  * so it speed up a bit. Default value is 0.85 seconds.
891  *
892  * @param obj The calendar object
893  * @return The value of the first interval in seconds
894  *
895  * @ingroup Calendar
896  */
897 EAPI double
898 elm_calendar_interval_get(const Evas_Object *obj)
899 {
900    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
901    Widget_Data *wd = elm_widget_data_get(obj);
902    if (!wd) return 0.0;
903    return wd->first_interval;
904 }
905
906 /**
907  * Set the minimum and maximum values for the year
908  *
909  * Maximum must be greater than minimum, except if you don't wan't to set
910  * maximum year.
911  * Default values are 1902 and -1.
912  *
913  * If the maximum year is a negative value, it will be limited depending of the
914  * platform architecture (2037 for 32 bits);
915  *
916  * @param obj The calendar object
917  * @param min The minimum year, greater than 1901;
918  * @param max The maximum year;
919  *
920  * @ingroup Calendar
921  */
922 EAPI void
923 elm_calendar_min_max_year_set(Evas_Object *obj, int min, int max)
924 {
925    ELM_CHECK_WIDTYPE(obj, widtype);
926    Widget_Data *wd = elm_widget_data_get(obj);
927    if (!wd) return;
928    min -= 1900;
929    max -= 1900;
930    if ((wd->year_min == min) && (wd->year_max == max)) return;
931    wd->year_min = min > 2 ? min : 2;
932    wd->year_max = max;
933    if (wd->selected_time.tm_year > wd->year_max)
934      wd->selected_time.tm_year = wd->year_max;
935    if (wd->selected_time.tm_year < wd->year_min)
936      wd->selected_time.tm_year = wd->year_min;
937    _fix_selected_time(wd);
938    _populate(obj);
939 }
940
941 /**
942  * Get the minimum and maximum values for the year
943  *
944  * Default values are 1902 and -1.
945  *
946  * If the maximum year is a negative value, it will be limited depending of the
947  * platform architecture (2037 for 32 bits);
948  *
949  * @param obj The calendar object
950  * @param min The minimum year
951  * @param max The maximum year
952  *
953  * @ingroup Calendar
954  */
955 EAPI void
956 elm_calendar_min_max_year_get(const Evas_Object *obj, int *min, int *max)
957 {
958    ELM_CHECK_WIDTYPE(obj, widtype);
959    Widget_Data *wd = elm_widget_data_get(obj);
960    if (!wd) return;
961    if (min) *min = wd->year_min + 1900;
962    if (max) *max = wd->year_max + 1900;
963 }
964
965 /**
966  * Enable or disable day selection
967  *
968  * Enabled by default. If disabled, the user can select months, but not days.
969  * It should be used if you won't need such selection for the widget usage.
970  *
971  * @param obj The calendar object
972  * @param enabled Boolean to enable (true) or disable (false) day selection
973  *
974  * @ingroup Calendar
975  */
976 EAPI void
977 elm_calendar_day_selection_enabled_set(Evas_Object *obj, Eina_Bool enabled)
978 {
979    ELM_CHECK_WIDTYPE(obj, widtype);
980    Widget_Data *wd = elm_widget_data_get(obj);
981    if (!wd) return;
982    wd->selection_enabled = enabled;
983    if (enabled)
984      _select(wd, wd->selected_it);
985    else
986      _unselect(wd, wd->selected_it);
987 }
988
989 /**
990  * Get day selection state
991  *
992  * Enabled by default. If disabled, the user can select months, but not days.
993  * It should be used if you won't need such selection for the widget usage.
994  *
995  * @param obj The calendar object
996  * @return True if day selection is enabled, or false otherwise. It will
997  * return false if it can't get widget data.
998  *
999  * @ingroup Calendar
1000  */
1001 EAPI Eina_Bool
1002 elm_calendar_day_selection_enabled_get(const Evas_Object *obj)
1003 {
1004    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1005    Widget_Data *wd = elm_widget_data_get(obj);
1006    if (!wd) return EINA_FALSE;
1007    return wd->selection_enabled;
1008 }
1009
1010 /**
1011  * Set selected time
1012  *
1013  * Set the time selected, changing the displayed month if needed.
1014  * Selected time changes when the user changes the month or select a day.
1015  *
1016  * @param obj The calendar object
1017  * @param selected_time A tm struct to represent the selected date
1018  *
1019  * @ingroup Calendar
1020  */
1021 EAPI void
1022 elm_calendar_selected_time_set(Evas_Object *obj, struct tm *selected_time)
1023 {
1024    ELM_CHECK_WIDTYPE(obj, widtype);
1025    Widget_Data *wd = elm_widget_data_get(obj);
1026    if (!wd) return;
1027
1028    EINA_SAFETY_ON_NULL_RETURN(selected_time);
1029    wd->selected_time = *selected_time;
1030    _populate(obj);
1031    return;
1032 }
1033
1034 /**
1035  * Get selected time
1036  *
1037  * Get the time selected by the user.
1038  * Selected time changes when the user changes the month or select a day.
1039  *
1040  * @param obj The calendar object
1041  * @param selected_time A tm struct to represent the selected date
1042  * @return It will return false if it can't get widget data, or true otherwise
1043  *
1044  * @ingroup Calendar
1045  */
1046 EAPI Eina_Bool
1047 elm_calendar_selected_time_get(const Evas_Object *obj, struct tm *selected_time)
1048 {
1049    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1050    Widget_Data *wd = elm_widget_data_get(obj);
1051    if (!wd) return EINA_FALSE;
1052    EINA_SAFETY_ON_NULL_RETURN_VAL(selected_time, EINA_FALSE);
1053    *selected_time = wd->selected_time;
1054    return EINA_TRUE;
1055 }
1056
1057 /**
1058  * Set a function to format the string that will be used to display
1059  * month - year
1060  *
1061  * By default it uses strftime with "%B %Y" format string.
1062  * It should allocate the memory that will be used by the string,
1063  * that will be freed by the widget after usage.
1064  * A pointer to the string and a pointer to the time struct will be provided.
1065  *
1066  * Example:
1067  * @code
1068  * static char *
1069  * _format_month_year(struct tm *stime)
1070  * {
1071  *    char buf[32];
1072  *    if (!strftime(buf, sizeof(buf), "%B %Y", stime)) return NULL;
1073  *    return strdup(buf);
1074  * }
1075  * elm_calendar_format_function_set(calendar, _format_month_year);
1076  * @endcode
1077  *
1078  * @param obj The calendar object
1079  * @param format_function Function to set the month-year string given
1080  * the selected date
1081  *
1082  * @ingroup Calendar
1083  */
1084 EAPI void
1085 elm_calendar_format_function_set(Evas_Object *obj, char * (*format_function) (struct tm *stime))
1086 {
1087    ELM_CHECK_WIDTYPE(obj, widtype);
1088    Widget_Data *wd = elm_widget_data_get(obj);
1089    if (!wd) return;
1090    wd->format_func = format_function;
1091 }
1092
1093 /**
1094  * Add a new mark to the calendar
1095  *
1096  * Add a mark that will be drawn in the calendar respecting the insertion time
1097  * and periodicity. It will emit the type as signal to the widget theme.
1098  * By default, it supports "holiday" and "checked", but it can be extended.
1099  *
1100  * It won't immediately update the calendar, drawing the marks. For this, call
1101  * elm_calendar_marks_draw().
1102  *
1103  * Example
1104  * @code
1105  * struct tm selected_time;
1106  * time_t current_time;
1107  *
1108  * current_time = time(NULL) + 5 * 84600;
1109  * localtime_r(&current_time, &selected_time);
1110  * elm_calendar_mark_add(cal, "holiday", selected_time, ELM_CALENDAR_ANNUALLY);
1111  *
1112  * current_time = time(NULL) + 1 * 84600;
1113  * localtime_r(&current_time, &selected_time);
1114  * elm_calendar_mark_add(cal, "checked", selected_time, ELM_CALENDAR_UNIQUE);
1115  *
1116  * elm_calendar_marks_draw(cal);
1117  * @endcode
1118  *
1119  * @param obj The calendar object
1120  * @param mark_type A string used to define the type of mark. It will be
1121  * emitted to the theme, that should display a related modification on these
1122  * days representation.
1123  * @param mark_time A time struct to represent the date of inclusion of the
1124  * mark. For marks that repeats it will just be displayed after the inclusion
1125  * date in the calendar.
1126  * @param repeat Repeat the event following this periodicity. Can be a unique
1127  * mark (that don't repeat), daily, weekly, monthly or annually.
1128  *
1129  * @return The created mark or NULL upon failure
1130  *
1131  * @ingroup Calendar
1132  */
1133 EAPI Elm_Calendar_Mark *
1134 elm_calendar_mark_add(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat repeat)
1135 {
1136    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1137    Widget_Data *wd = elm_widget_data_get(obj);
1138    Elm_Calendar_Mark *mark;
1139    if (!wd) return NULL;
1140
1141    mark = _mark_new(obj, mark_type, mark_time, repeat);
1142    wd->marks = eina_list_append(wd->marks, mark);
1143    mark->node = eina_list_last(wd->marks);
1144    return mark;
1145 }
1146
1147 /**
1148  * Delete mark from the calendar.
1149  *
1150  * @param mark The mark to delete
1151  *
1152  * @ingroup Calendar
1153  */
1154 EAPI void
1155 elm_calendar_mark_del(Elm_Calendar_Mark *mark)
1156 {
1157    Evas_Object *obj;
1158    Widget_Data *wd;
1159
1160    EINA_SAFETY_ON_NULL_RETURN(mark);
1161
1162    obj = mark->obj;
1163    wd = elm_widget_data_get(obj);
1164    if (!wd) return;
1165
1166    wd->marks = eina_list_remove_list(wd->marks, mark->node);
1167    _mark_free(mark);
1168 }
1169
1170 /**
1171  * Remove all the marks from the calendar
1172  *
1173  * @param obj The calendar object
1174  *
1175  * @ingroup Calendar
1176  */
1177 EAPI void
1178 elm_calendar_marks_clear(Evas_Object *obj)
1179 {
1180    ELM_CHECK_WIDTYPE(obj, widtype);
1181    Widget_Data *wd = elm_widget_data_get(obj);
1182    Elm_Calendar_Mark *mark;
1183
1184    if (!wd) return;
1185    EINA_LIST_FREE(wd->marks, mark)
1186       _mark_free(mark);
1187 }
1188
1189 /**
1190  * Returns a list of all the calendar marks.
1191  *
1192  * @param obj The calendar object
1193  * @return An Eina_List* of the calendar marks, or NULL on failure
1194  *
1195  * @ingroup Calendar
1196  */
1197 EAPI const Eina_List *
1198 elm_calendar_marks_get(const Evas_Object *obj)
1199 {
1200    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1201    Widget_Data *wd = elm_widget_data_get(obj);
1202    if (!wd) return NULL;
1203    return wd->marks;
1204 }
1205
1206 /**
1207  * Draw calendar marks.
1208  *
1209  * Should be used after adding, removing or clearing marks.
1210  * It will go through the entire marks list updating the calendar
1211  * (not a cheap function). So if lots of marks will be added,
1212  * add all the marks and then call this function.
1213  *
1214  * When the month is changed marks will be drawed.
1215  *
1216  * @param obj The calendar object
1217  *
1218  * @ingroup Calendar
1219  */
1220 EAPI void
1221 elm_calendar_marks_draw(Evas_Object *obj)
1222 {
1223    ELM_CHECK_WIDTYPE(obj, widtype);
1224    Widget_Data *wd = elm_widget_data_get(obj);
1225    if (!wd) return;
1226    _populate(obj);
1227 }
1228
1229 /**
1230  * Set a text color to the saturday color.
1231  * 
1232  * Deprecated. use elm_calendar_mark_add() instead like:
1233  * 
1234  * @code
1235  * struct tm t = { 0, 0, 12, 6, 0, 0, 5, 5, -1 };
1236  * elm_calendar_mark_add(obj, "sat", &t, ELM_CALENDAR_WEEKLY);
1237  * @endcode
1238  * 
1239  * @param obj The calendar object
1240  * @param pos The text position
1241  *
1242  * @ingroup Calendar
1243  */
1244 EINA_DEPRECATED EAPI void
1245 elm_calendar_text_saturday_color_set(Evas_Object *obj, int pos)
1246 {
1247   ELM_CHECK_WIDTYPE(obj, widtype);
1248   Widget_Data *wd = elm_widget_data_get(obj);
1249   if (!wd) return;
1250   _text_day_color_set(wd, DAY_SATURDAY, pos);
1251 }
1252
1253 /**
1254  * Set a text color to the sunday color.
1255  *
1256  * Deprecated. use elm_calendar_mark_add() instead like:
1257  * 
1258  * @code
1259  * struct tm t = { 0, 0, 12, 7, 0, 0, 6, 6, -1 };
1260  * elm_calendar_mark_add(obj, "sun", &t, ELM_CALENDAR_WEEKLY);
1261  * @endcode
1262  * 
1263  * @param obj The calendar object
1264  * @param pos The text position
1265  *
1266  * @ingroup Calendar
1267  */
1268 EINA_DEPRECATED EAPI void
1269 elm_calendar_text_sunday_color_set(Evas_Object *obj, int pos)
1270 {
1271   ELM_CHECK_WIDTYPE(obj, widtype);
1272   Widget_Data *wd = elm_widget_data_get(obj);
1273   if (!wd) return;
1274   _text_day_color_set(wd, DAY_SUNDAY, pos);
1275 }
1276
1277 /**
1278  * Set a text color to the weekday color.
1279  *
1280  * Deprecated. use elm_calendar_mark_add() instead like:
1281  * 
1282  * @code
1283  * struct tm t = { 0, 0, 12, 1, 0, 0, 0, 0, -1 };
1284  * 
1285  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // monday
1286  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1287  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // tuesday
1288  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1289  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // wednesday
1290  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1291  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // thursday
1292  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1293  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // friday
1294  * @endcode
1295  * 
1296  * @param obj The calendar object
1297  * @param pos The text position
1298  *
1299  * @ingroup Calendar
1300  */
1301 EINA_DEPRECATED EAPI void
1302 elm_calendar_text_weekday_color_set(Evas_Object *obj, int pos)
1303 {
1304   ELM_CHECK_WIDTYPE(obj, widtype);
1305   Widget_Data *wd = elm_widget_data_get(obj);
1306   if (!wd) return;
1307   _text_day_color_set(wd, DAY_WEEKDAY, pos);
1308 }