Merge branch 'master' into svn_merge
[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  * @ingroup Elementary
15  *
16  * A calendar is a widget that allows the user to select a date. It has
17  * support to adding check marks (holidays and checks by default). The calendar
18  * is displayed one month at a time.
19  *
20  * Weekday names and the function used to format month and year to
21  * be displayed can be set, giving more flexibility to this widget.
22  *
23  * Signals that you can add callbacks for are:
24  *
25  * "changed" - emitted when the user selects a day or changes the displayed
26  *             month, what actually changes the selected day as well.
27  */
28
29 typedef enum _Day_Color // EINA_DEPRECATED
30 {
31    DAY_WEEKDAY = 0,
32    DAY_SATURDAY = 1,
33    DAY_SUNDAY = 2
34 } Day_Color;
35
36 typedef struct _Widget_Data Widget_Data;
37
38 struct _Widget_Data
39 {
40    Evas_Object *calendar;
41    Eina_List *marks;
42    double interval, first_interval;
43    int year_min, year_max, spin_speed;
44    int today_it, selected_it, first_day_it;
45    Ecore_Timer *spin, *update_timer;
46    char * (*format_func) (struct tm *selected_time);
47    const char *weekdays[7];
48    struct tm current_time, selected_time;
49    Day_Color day_color[42]; // EINA_DEPRECATED
50    Eina_Bool selection_enabled : 1;
51 };
52
53 struct _Elm_Calendar_Mark
54 {
55    Evas_Object *obj;
56    Eina_List *node;
57    struct tm mark_time;
58    const char *mark_type;
59    Elm_Calendar_Mark_Repeat repeat;
60 };
61
62 static const char *widtype = NULL;
63 static void _on_focus_hook(void *data, Evas_Object *obj);
64 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
65
66 static const char SIG_CHANGED[] = "changed";
67
68 static const Evas_Smart_Cb_Description _signals[] = {
69    {SIG_CHANGED, ""},
70    {NULL, NULL}
71 };
72
73
74 /* Should not be translated, it's used if we failed
75  * getting from locale. */
76 static const char *_days_abbrev[] =
77 {
78    "Sun", "Mon", "Tue", "Wed",
79    "Thu", "Fri", "Sat"
80 };
81
82 static int _days_in_month[2][12] =
83 {
84      {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
85      {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
86 };
87
88 static Elm_Calendar_Mark *
89 _mark_new(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat repeat)
90 {
91    Widget_Data *wd = elm_widget_data_get(obj);
92    Elm_Calendar_Mark *mark;
93
94    if (!wd) return NULL;
95    mark = calloc(1, sizeof(Elm_Calendar_Mark));
96    if (!mark) return NULL;
97    mark->obj = obj;
98    mark->mark_type = eina_stringshare_add(mark_type);
99    mark->mark_time = *mark_time;
100    mark->repeat = repeat;
101    return mark;
102 }
103
104 static inline void
105 _mark_free(Elm_Calendar_Mark *mark)
106 {
107    eina_stringshare_del(mark->mark_type);
108    free(mark);
109 }
110
111 static void
112 _sizing_eval(Evas_Object *obj)
113 {
114    Widget_Data *wd = elm_widget_data_get(obj);
115    Evas_Coord minw = -1, minh = -1;
116    if (!wd) return;
117    elm_coords_finger_size_adjust(8, &minw, 7, &minh);
118    edje_object_size_min_restricted_calc(wd->calendar, &minw, &minh, minw, minh);
119    evas_object_size_hint_min_set(obj, minw, minh);
120    evas_object_size_hint_max_set(obj, -1, -1);
121 }
122
123 static inline int
124 _maxdays_get(struct tm *selected_time)
125 {
126    int month, year;
127
128    month = selected_time->tm_mon;
129    year = selected_time->tm_year + 1900;
130
131    return _days_in_month[((!(year % 4)) &&
132                           ((!(year % 400)) ||
133                            (year % 100)))]
134       [month];
135 }
136
137 static inline void
138 _unselect(Widget_Data *wd, int selected)
139 {
140    char emission[32];
141    snprintf(emission, sizeof(emission), "cit_%i,unselected", selected);
142    edje_object_signal_emit(wd->calendar, emission, "elm");
143 }
144
145 static inline void
146 _select(Widget_Data *wd, int selected)
147 {
148    char emission[32];
149    snprintf(emission, sizeof(emission), "cit_%i,selected", selected);
150    edje_object_signal_emit(wd->calendar, emission, "elm");
151 }
152
153 static inline void
154 _not_today(Widget_Data *wd)
155 {
156    char emission[32];
157    snprintf(emission, sizeof(emission), "cit_%i,not_today", wd->today_it);
158    edje_object_signal_emit(wd->calendar, emission, "elm");
159    wd->today_it = -1;
160 }
161
162 static inline void
163 _today(Widget_Data *wd, int it)
164 {
165    char emission[32];
166    snprintf(emission, sizeof(emission), "cit_%i,today", it);
167    edje_object_signal_emit(wd->calendar, emission, "elm");
168    wd->today_it = it;
169 }
170
171 static char *
172 _format_month_year(struct tm *selected_time)
173 {
174    char buf[32];
175    if (!strftime(buf, sizeof(buf), "%B %Y", selected_time)) return NULL;
176    return strdup(buf);
177 }
178
179 static inline void
180 _cit_mark(Evas_Object *cal, int cit, const char *mtype)
181 {
182    char sign[64];
183    snprintf(sign, sizeof(sign), "cit_%i,%s", cit, mtype);
184    edje_object_signal_emit(cal, sign, "elm");
185 }
186
187 static inline int
188 _weekday_get(int first_week_day, int day)
189 {
190    return (day + first_week_day - 1) % 7;
191 }
192
193 // EINA_DEPRECATED
194 static void
195 _text_day_color_update(Widget_Data *wd, int pos)
196 {
197    char emission[32];
198
199    switch (wd->day_color[pos])
200      {
201       case DAY_WEEKDAY:
202          snprintf(emission, sizeof(emission), "cit_%i,weekday", pos);
203          break;
204       case DAY_SATURDAY:
205          snprintf(emission, sizeof(emission), "cit_%i,saturday", pos);
206          break;
207       case DAY_SUNDAY:
208          snprintf(emission, sizeof(emission), "cit_%i,sunday", pos);
209          break;
210       default:
211          return;
212      }
213
214    edje_object_signal_emit(wd->calendar, emission, "elm");
215 }
216
217 // EINA_DEPRECATED
218 static void
219 _text_day_color_set(Widget_Data *wd, Day_Color col, int pos)
220 {
221    if ((pos < 0) || (pos >= 42)) return;
222    if (wd->day_color[pos] == col) return;
223    wd->day_color[pos] = col;
224    _text_day_color_update(wd, pos);
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    char *buf;
236    Eina_Bool last_row = EINA_TRUE;
237    Widget_Data *wd = elm_widget_data_get(obj);
238
239    if (!wd) return;
240
241    if (wd->today_it > 0) _not_today(wd);
242
243    maxdays = _maxdays_get(&wd->selected_time);
244    mon = wd->selected_time.tm_mon;
245    year = wd->selected_time.tm_year;
246
247    /* Set selected month */
248    buf = wd->format_func(&wd->selected_time);
249    if (buf)
250      {
251         edje_object_part_text_set(wd->calendar, "month_text", buf);
252         free(buf);
253      }
254    else
255      edje_object_part_text_set(wd->calendar, "month_text", "");
256
257    /* Set days */
258    day = 0;
259    first_day = wd->selected_time;
260    first_day.tm_mday = 1;
261    mktime(&first_day);
262
263    // Layout of the calendar is changed for removing the unfilled last row.
264    wd->first_day_it = first_day.tm_wday;
265
266    if ((35 - wd->first_day_it) > (maxdays - 1)) last_row = EINA_FALSE;
267
268    if (!last_row)
269      {
270         char emission[32];
271
272         for (i = 0; i < 5; i++)
273           {
274              snprintf(emission, sizeof(emission), "cseph_%i,row_hide", i);
275              edje_object_signal_emit(wd->calendar, emission, "elm");
276           }
277         snprintf(emission, sizeof(emission), "cseph_%i,row_invisible", 5);
278         edje_object_signal_emit(wd->calendar, emission, "elm");
279         for (i = 0; i < 35; i++)
280           {
281              snprintf(emission, sizeof(emission), "cit_%i,cell_expanded", i);
282              edje_object_signal_emit(wd->calendar, emission, "elm");
283           }
284         for (i = 35; i < 42; i++)
285           {
286              snprintf(emission, sizeof(emission), "cit_%i,cell_invisible", i);
287              edje_object_signal_emit(wd->calendar, emission, "elm");
288           }
289      }
290    else
291      {
292         char emission[32];
293
294         for (i = 0; i < 6; i++)
295           {
296              snprintf(emission, sizeof(emission), "cseph_%i,row_show", i);
297              edje_object_signal_emit(wd->calendar, emission, "elm");
298           }
299         for (i = 0; i < 42; i++)
300           {
301              snprintf(emission, sizeof(emission), "cit_%i,cell_default", i);
302              edje_object_signal_emit(wd->calendar, emission, "elm");
303           }
304      }
305
306    for (i = 0; i < 42; i++)
307      {
308         _text_day_color_update(wd, i); // EINA_DEPRECATED
309         if ((!day) && (i == first_day.tm_wday)) day = 1;
310
311         if ((day == wd->current_time.tm_mday)
312             && (mon == wd->current_time.tm_mon)
313             && (year == wd->current_time.tm_year))
314           _today(wd, i);
315
316         if (day == wd->selected_time.tm_mday)
317           {
318              if ((wd->selected_it > -1) && (wd->selected_it != i))
319                _unselect(wd, wd->selected_it);
320
321              if (wd->selection_enabled) _select(wd, i);
322
323              wd->selected_it = i;
324           }
325
326         if ((day) && (day <= maxdays))
327           snprintf(day_s, sizeof(day_s), "%i", day++);
328         else
329           day_s[0] = 0;
330
331         snprintf(part, sizeof(part), "cit_%i.text", i);
332         edje_object_part_text_set(wd->calendar, part, day_s);
333         /* Clear previous marks */
334         _cit_mark(wd->calendar, i, "clear");
335      }
336
337    /* Set marks */
338    EINA_LIST_FOREACH(wd->marks, l, mark)
339      {
340         struct tm *mtime = &mark->mark_time;
341         int month = wd->selected_time.tm_mon;
342         int year = wd->selected_time.tm_year;
343         int mday_it = mtime->tm_mday + wd->first_day_it - 1;
344
345         switch (mark->repeat)
346           {
347            case ELM_CALENDAR_UNIQUE:
348               if ((mtime->tm_mon == month) && (mtime->tm_year == year))
349                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
350               break;
351            case ELM_CALENDAR_DAILY:
352               if (((mtime->tm_year == year) && (mtime->tm_mon < month)) ||
353                   (mtime->tm_year < year))
354                 day = 1;
355               else if ((mtime->tm_year == year) && (mtime->tm_mon == month))
356                 day = mtime->tm_mday;
357               else
358                 break;
359               for (; day <= maxdays; day++)
360                 _cit_mark(wd->calendar, day + wd->first_day_it - 1,
361                           mark->mark_type);
362               break;
363            case ELM_CALENDAR_WEEKLY:
364               if (((mtime->tm_year == year) && (mtime->tm_mon < month)) ||
365                   (mtime->tm_year < year))
366                 day = 1;
367               else if ((mtime->tm_year == year) && (mtime->tm_mon == month))
368                 day = mtime->tm_mday;
369               else
370                 break;
371               for (; day <= maxdays; day++)
372                 if (mtime->tm_wday == _weekday_get(wd->first_day_it, day))
373                   _cit_mark(wd->calendar, day + wd->first_day_it - 1,
374                             mark->mark_type);
375               break;
376            case ELM_CALENDAR_MONTHLY:
377               if (((mtime->tm_year < year) ||
378                    ((mtime->tm_year == year) && (mtime->tm_mon <= month))) &&
379                   (mtime->tm_mday <= maxdays))
380                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
381               break;
382            case ELM_CALENDAR_ANNUALLY:
383               if ((mtime->tm_year <= year) && (mtime->tm_mon == month) &&
384                   (mtime->tm_mday <= maxdays))
385                 _cit_mark(wd->calendar, mday_it, mark->mark_type);
386               break;
387           }
388      }
389 }
390
391 static void
392 _set_headers(Evas_Object *obj)
393 {
394    static char part[] = "ch_0.text";
395    int i;
396    Widget_Data *wd = elm_widget_data_get(obj);
397    if (!wd) return;
398
399    for (i = 0; i < 7; i++)
400      {
401         part[3] = i + '0';
402         edje_object_part_text_set(wd->calendar, part, wd->weekdays[i]);
403      }
404 }
405
406 static void
407 _del_hook(Evas_Object *obj)
408 {
409    int i;
410    Elm_Calendar_Mark *mark;
411    Widget_Data *wd = elm_widget_data_get(obj);
412
413    if (!wd) return;
414
415    if (wd->spin) ecore_timer_del(wd->spin);
416    if (wd->update_timer) ecore_timer_del(wd->update_timer);
417
418    if (wd->marks)
419      {
420         EINA_LIST_FREE(wd->marks, mark)
421           {
422              _mark_free(mark);
423           }
424      }
425
426    for (i = 0; i < 7; i++)
427      eina_stringshare_del(wd->weekdays[i]);
428
429    free(wd);
430 }
431
432 static void
433 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
434 {
435    Widget_Data *wd = elm_widget_data_get(obj);
436    if (!wd) return;
437    if (elm_widget_focus_get(obj))
438      {
439         edje_object_signal_emit(wd->calendar, "elm,action,focus", "elm");
440         evas_object_focus_set(wd->calendar, EINA_TRUE);
441      }
442    else
443      {
444         edje_object_signal_emit(wd->calendar, "elm,action,unfocus", "elm");
445         evas_object_focus_set(wd->calendar, EINA_FALSE);
446      }
447 }
448
449 static void
450 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
451 {
452    Widget_Data *wd = elm_widget_data_get(obj);
453    if (!wd) return;
454    edje_object_mirrored_set(wd->calendar, rtl);
455 }
456
457 static void
458 _theme_hook(Evas_Object *obj)
459 {
460    Widget_Data *wd = elm_widget_data_get(obj);
461    if (!wd) return;
462    _elm_widget_mirrored_reload(obj);
463    _elm_theme_object_set(obj, wd->calendar, "calendar", "base",
464                          elm_widget_style_get(obj));
465    _mirrored_set(obj, elm_widget_mirrored_get(obj));
466    _set_headers(obj);
467    _populate(obj);
468    edje_object_message_signal_process(wd->calendar);
469    edje_object_scale_set(wd->calendar,
470                          elm_widget_scale_get(obj) * _elm_config->scale);
471    _sizing_eval(obj);
472 }
473
474 static void
475 _signal_emit_hook(Evas_Object *obj, const char *emission, const char *source)
476 {
477    Widget_Data *wd = elm_widget_data_get(obj);
478    if (!wd) return;
479    edje_object_signal_emit(wd->calendar, emission, source);
480 }
481
482 static void
483 _signal_callback_add_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_add(wd->calendar, emission,
488                                    source, func_cb, data);
489 }
490
491 static void
492 _signal_callback_del_hook(Evas_Object *obj, const char *emission, const char *source, Edje_Signal_Cb func_cb, void *data)
493 {
494    Widget_Data *wd = elm_widget_data_get(obj);
495    if (!wd) return;
496    edje_object_signal_callback_del_full(wd->calendar, emission, source, func_cb,
497                                         data);
498 }
499
500 /* Set correct tm_wday and tm_yday after other fields changes*/
501 static inline void
502 _fix_selected_time(Widget_Data *wd)
503 {
504    mktime(&wd->selected_time);
505 }
506
507 static Eina_Bool
508 _update_month(Evas_Object *obj, int delta)
509 {
510    struct tm time_check;
511    int maxdays;
512    Widget_Data *wd = elm_widget_data_get(obj);
513    if (!wd) return EINA_FALSE;
514
515    /* check if it's a valid time. for 32 bits, year greater than 2037 is not */
516    time_check = wd->selected_time;
517    time_check.tm_mon += delta;
518    if (mktime(&time_check) == -1)
519      return EINA_FALSE;
520
521    wd->selected_time.tm_mon += delta;
522    if (wd->selected_time.tm_mon < 0)
523      {
524         if (wd->selected_time.tm_year == wd->year_min)
525           {
526              wd->selected_time.tm_mon++;
527              return EINA_FALSE;
528           }
529         wd->selected_time.tm_mon = 11;
530         wd->selected_time.tm_year--;
531      }
532    else if (wd->selected_time.tm_mon > 11)
533      {
534         if (wd->selected_time.tm_year == wd->year_max)
535           {
536              wd->selected_time.tm_mon--;
537              return EINA_FALSE;
538           }
539         wd->selected_time.tm_mon = 0;
540         wd->selected_time.tm_year++;
541      }
542
543    maxdays = _maxdays_get(&wd->selected_time);
544    if (wd->selected_time.tm_mday > maxdays)
545      wd->selected_time.tm_mday = maxdays;
546
547    _fix_selected_time(wd);
548    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
549
550    return EINA_TRUE;
551 }
552
553 static Eina_Bool
554 _spin_value(void *data)
555 {
556    Widget_Data *wd = elm_widget_data_get(data);
557    if (!wd) return ECORE_CALLBACK_CANCEL;
558    if (_update_month(data, wd->spin_speed)) _populate(data);
559    wd->interval = wd->interval / 1.05;
560    ecore_timer_interval_set(wd->spin, wd->interval);
561    return ECORE_CALLBACK_RENEW;
562 }
563
564 static void
565 _button_inc_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
566 {
567    Widget_Data *wd = elm_widget_data_get(data);
568    if (!wd) return;
569    wd->interval = wd->first_interval;
570    wd->spin_speed = 1;
571    if (wd->spin) ecore_timer_del(wd->spin);
572    wd->spin = ecore_timer_add(wd->interval, _spin_value, data);
573    _spin_value(data);
574 }
575
576 static void
577 _button_dec_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
578 {
579    Widget_Data *wd = elm_widget_data_get(data);
580    if (!wd) return;
581    wd->interval = wd->first_interval;
582    wd->spin_speed = -1;
583    if (wd->spin) ecore_timer_del(wd->spin);
584    wd->spin = ecore_timer_add(wd->interval, _spin_value, data);
585    _spin_value(data);
586 }
587
588 static void
589 _button_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
590 {
591    Widget_Data *wd = elm_widget_data_get(data);
592    if (!wd) return;
593    wd->interval = wd->first_interval;
594    if (wd->spin) ecore_timer_del(wd->spin);
595    wd->spin = NULL;
596 }
597
598 static int
599 _get_item_day(Evas_Object *obj, int selected_it)
600 {
601    int day;
602    Widget_Data *wd = elm_widget_data_get(obj);
603    if (!wd) return 0;
604
605    day = selected_it - wd->first_day_it + 1;
606    if ((day < 0) || (day > _maxdays_get(&wd->selected_time)))
607      return 0;
608
609    return day;
610 }
611
612 static void
613 _update_sel_it(Evas_Object *obj, int sel_it)
614 {
615    int day;
616    Widget_Data *wd = elm_widget_data_get(obj);
617    if ((!wd) || (!wd->selection_enabled))
618      return;
619
620    day = _get_item_day(obj, sel_it);
621    if (!day)
622      return;
623
624    _unselect(wd, wd->selected_it);
625
626    wd->selected_it = sel_it;
627    wd->selected_time.tm_mday = day;
628    _select(wd, wd->selected_it);
629    _fix_selected_time(wd);
630    evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
631 }
632
633 static void
634 _day_selected(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source)
635 {
636    int sel_it;
637    Widget_Data *wd = elm_widget_data_get(data);
638    if ((!wd) || (!wd->selection_enabled))
639      return;
640    sel_it = atoi(source);
641
642    _update_sel_it(data, sel_it);
643 }
644
645 static inline int
646 _time_to_next_day(struct tm *t)
647 {
648    return ((((24 - t->tm_hour) * 60) - t->tm_min) * 60) - t->tm_sec;
649 }
650
651 static Eina_Bool
652 _update_cur_date(void *data)
653 {
654    time_t current_time;
655    int t, day;
656    Widget_Data *wd = elm_widget_data_get(data);
657    if (!wd) return ECORE_CALLBACK_RENEW;
658
659    if (wd->today_it > 0) _not_today(wd);
660
661    current_time = time(NULL);
662    localtime_r(&current_time, &wd->current_time);
663    t = _time_to_next_day(&wd->current_time);
664    ecore_timer_interval_set(wd->update_timer, t);
665
666    if ((wd->current_time.tm_mon != wd->selected_time.tm_mon) ||
667        (wd->current_time.tm_year!= wd->selected_time.tm_year))
668      return ECORE_CALLBACK_RENEW;
669
670    day = wd->current_time.tm_mday + wd->first_day_it - 1;
671    _today(wd, day);
672
673    return ECORE_CALLBACK_RENEW;
674 }
675
676 static Eina_Bool
677 _event_hook(Evas_Object *obj, Evas_Object *src __UNUSED__, Evas_Callback_Type type, void *event_info)
678 {
679    if (type != EVAS_CALLBACK_KEY_DOWN) return EINA_FALSE;
680    Evas_Event_Key_Down *ev = event_info;
681    Widget_Data *wd = elm_widget_data_get(obj);
682
683    if (!wd) return EINA_FALSE;
684    if (elm_widget_disabled_get(obj)) return EINA_FALSE;
685    if (!wd->selection_enabled) return EINA_FALSE;
686
687    if ((!strcmp(ev->keyname, "Left")) ||
688        (!strcmp(ev->keyname, "KP_Left")))
689      {
690         _update_sel_it(obj, wd->selected_it-1);
691      }
692    else if ((!strcmp(ev->keyname, "Right")) ||
693             (!strcmp(ev->keyname, "KP_Right")))
694      {
695         _update_sel_it(obj, wd->selected_it+1);
696      }
697    else if ((!strcmp(ev->keyname, "Up"))  ||
698             (!strcmp(ev->keyname, "KP_Up")))
699      {
700         _update_sel_it(obj, wd->selected_it-7);
701      }
702    else if ((!strcmp(ev->keyname, "Down")) ||
703             (!strcmp(ev->keyname, "KP_Down")))
704      {
705         _update_sel_it(obj, wd->selected_it+7);
706      }
707    else if ((!strcmp(ev->keyname, "Prior")) ||
708             (!strcmp(ev->keyname, "KP_Prior")))
709      {
710         if (_update_month(obj, -1)) _populate(obj);
711      }
712    else if ((!strcmp(ev->keyname, "Next")) ||
713             (!strcmp(ev->keyname, "KP_Next")))
714      {
715         if (_update_month(obj, 1)) _populate(obj);
716      }
717    else return EINA_FALSE;
718
719    return EINA_TRUE;
720 }
721
722 /**
723  * Add a new calendar to the parent
724  *
725  * @param parent The parent object
726  * @return The new object or NULL if it cannot be created
727  *
728  * @ingroup Calendar
729  */
730 EAPI Evas_Object *
731 elm_calendar_add(Evas_Object *parent)
732 {
733    time_t current_time;
734    time_t weekday = 259200; /* Just the first sunday since epoch */
735    Evas_Object *obj;
736    Widget_Data *wd;
737    int i, t;
738    Evas *e;
739
740    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
741
742    ELM_SET_WIDTYPE(widtype, "calendar");
743    elm_widget_type_set(obj, "calendar");
744    elm_widget_sub_object_add(parent, obj);
745    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
746    elm_widget_data_set(obj, wd);
747    elm_widget_del_hook_set(obj, _del_hook);
748    elm_widget_theme_hook_set(obj, _theme_hook);
749    elm_widget_signal_emit_hook_set(obj, _signal_emit_hook);
750    elm_widget_signal_callback_add_hook_set(obj, _signal_callback_add_hook);
751    elm_widget_signal_callback_del_hook_set(obj, _signal_callback_del_hook);
752    elm_widget_can_focus_set(obj, EINA_TRUE);
753    elm_widget_event_hook_set(obj, _event_hook);
754
755    wd->first_interval = 0.85;
756    wd->year_min = 2;
757    wd->year_max = -1;
758    wd->today_it = -1;
759    wd->selected_it = -1;
760    wd->first_day_it = -1;
761    wd->selection_enabled = EINA_TRUE;
762    wd->format_func = _format_month_year;
763    wd->marks = NULL;
764
765    wd->calendar = edje_object_add(e);
766    _elm_theme_object_set(obj, wd->calendar, "calendar", "base", "default");
767    elm_widget_resize_object_set(obj, wd->calendar);
768
769    edje_object_signal_callback_add(wd->calendar, "elm,action,increment,start",
770                                    "*", _button_inc_start, obj);
771    edje_object_signal_callback_add(wd->calendar, "elm,action,decrement,start",
772                                    "*", _button_dec_start, obj);
773    edje_object_signal_callback_add(wd->calendar, "elm,action,stop",
774                                    "*", _button_stop, obj);
775    edje_object_signal_callback_add(wd->calendar, "elm,action,selected",
776                                    "*", _day_selected, obj);
777
778    evas_object_smart_callbacks_descriptions_set(obj, _signals);
779
780    for (i = 0; i < 7; i++)
781      {
782         /* FIXME: I'm not aware of a known max, so if it fails,
783          * just make it larger. :| */
784         char buf[20];
785         /* I don't know of a better way of doing it */
786         if (strftime(buf, sizeof(buf), "%a", gmtime(&weekday)))
787           {
788              wd->weekdays[i] = eina_stringshare_add(buf);
789           }
790         else
791           {
792              /* If we failed getting day, get a default value */
793              wd->weekdays[i] = _days_abbrev[i];
794              WRN("Failed getting weekday name for '%s' from locale.",
795                  _days_abbrev[i]);
796           }
797         weekday += 86400; /* Advance by a day */
798      }
799
800    current_time = time(NULL);
801    localtime_r(&current_time, &wd->selected_time);
802    wd->current_time = wd->selected_time;
803    t = _time_to_next_day(&wd->current_time);
804    wd->update_timer = ecore_timer_add(t, _update_cur_date, obj);
805
806    _set_headers(obj);
807    _populate(obj);
808    _mirrored_set(obj, elm_widget_mirrored_get(obj));
809    _sizing_eval(obj);
810    return obj;
811 }
812
813 /**
814  * Set weekdays names to display in the calendar.
815  *
816  * By default, the following abbreviations are displayed:
817  * "Sun, Mon, Tue, Wed, Thu, Fri, Sat"
818  * The first string should be related to Sunday, the second to Monday...
819  *
820  * The usage should be like this:
821  * @code
822  *   const char *weekdays[] =
823  *   {
824  *      "Sunday", "Monday", "Tuesday", "Wednesday",
825  *      "Thursday", "Friday", "Saturday"
826  *   };
827  *   elm_calendar_weekdays_names_set(calendar, weekdays);
828  * @endcode
829  *
830  * @param obj The calendar object
831  * @param weedays Array of seven strings to be used as weekday names.
832  * Warning: it must have 7 elements, or it will access invalid memory.
833  * The strings must be NULL terminated ('@\0').
834  *
835  * @ingroup Calendar
836  */
837 EAPI void
838 elm_calendar_weekdays_names_set(Evas_Object *obj, const char *weekdays[])
839 {
840    int i;
841    ELM_CHECK_WIDTYPE(obj, widtype);
842    Widget_Data *wd = elm_widget_data_get(obj);
843    if (!wd) return;
844
845    EINA_SAFETY_ON_NULL_RETURN(weekdays);
846
847    for (i = 0; i < 7; i++)
848      {
849         eina_stringshare_replace(&wd->weekdays[i], weekdays[i]);
850      }
851    _set_headers(obj);
852 }
853
854 /**
855  * Get weekdays names displayed in the calendar.
856  *
857  * By default, the following abbreviations are displayed:
858  * "Sun, Mon, Tue, Wed, Thu, Fri, Sat"
859  * The first string is related to Sunday, the second to Monday...
860  *
861  * @param obj The calendar object
862  * @return Array of seven strings to used as weekday names.
863  *
864  * @ingroup Calendar
865  */
866 EAPI const char **
867 elm_calendar_weekdays_names_get(const Evas_Object *obj)
868 {
869    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
870    Widget_Data *wd = elm_widget_data_get(obj);
871    if (!wd) return NULL;
872    return wd->weekdays;
873 }
874
875 /**
876  * Set the interval for the calendar
877  *
878  * The interval value is decreased while the user increments or decrements
879  * the calendar value. The next interval value is the previous interval / 1.05,
880  * so it speed up a bit. Default value is 0.85 seconds.
881  *
882  * @param obj The calendar object
883  * @param interval The interval value in seconds
884  *
885  * @ingroup Calendar
886  */
887 EAPI void
888 elm_calendar_interval_set(Evas_Object *obj, double interval)
889 {
890    ELM_CHECK_WIDTYPE(obj, widtype);
891    Widget_Data *wd = elm_widget_data_get(obj);
892    if (!wd) return;
893    wd->first_interval = interval;
894 }
895
896 /**
897  * Get the interval of the calendar
898  *
899  * The interval value is decreased while the user increments or decrements
900  * the calendar value. The next interval value is the previous interval / 1.05,
901  * so it speed up a bit. Default value is 0.85 seconds.
902  *
903  * @param obj The calendar object
904  * @return The value of the first interval in seconds
905  *
906  * @ingroup Calendar
907  */
908 EAPI double
909 elm_calendar_interval_get(const Evas_Object *obj)
910 {
911    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
912    Widget_Data *wd = elm_widget_data_get(obj);
913    if (!wd) return 0.0;
914    return wd->first_interval;
915 }
916
917 /**
918  * Set the minimum and maximum values for the year
919  *
920  * Maximum must be greater than minimum, except if you don't wan't to set
921  * maximum year.
922  * Default values are 1902 and -1.
923  *
924  * If the maximum year is a negative value, it will be limited depending of the
925  * platform architecture (2037 for 32 bits);
926  *
927  * @param obj The calendar object
928  * @param min The minimum year, greater than 1901;
929  * @param max The maximum year;
930  *
931  * @ingroup Calendar
932  */
933 EAPI void
934 elm_calendar_min_max_year_set(Evas_Object *obj, int min, int max)
935 {
936    ELM_CHECK_WIDTYPE(obj, widtype);
937    Widget_Data *wd = elm_widget_data_get(obj);
938    if (!wd) return;
939    min -= 1900;
940    max -= 1900;
941    if ((wd->year_min == min) && (wd->year_max == max)) return;
942    wd->year_min = min > 2 ? min : 2;
943    if (max > wd->year_min)
944           wd->year_max = max;
945    else
946           wd->year_max = wd->year_min;
947    if (wd->selected_time.tm_year > wd->year_max)
948      wd->selected_time.tm_year = wd->year_max;
949    if (wd->selected_time.tm_year < wd->year_min)
950      wd->selected_time.tm_year = wd->year_min;
951    _fix_selected_time(wd);
952    _populate(obj);
953 }
954
955 /**
956  * Get the minimum and maximum values for the year
957  *
958  * Default values are 1902 and -1.
959  *
960  * If the maximum year is a negative value, it will be limited depending of the
961  * platform architecture (2037 for 32 bits);
962  *
963  * @param obj The calendar object
964  * @param min The minimum year
965  * @param max The maximum year
966  *
967  * @ingroup Calendar
968  */
969 EAPI void
970 elm_calendar_min_max_year_get(const Evas_Object *obj, int *min, int *max)
971 {
972    ELM_CHECK_WIDTYPE(obj, widtype);
973    Widget_Data *wd = elm_widget_data_get(obj);
974    if (!wd) return;
975    if (min) *min = wd->year_min + 1900;
976    if (max) *max = wd->year_max + 1900;
977 }
978
979 /**
980  * Enable or disable day selection
981  *
982  * Enabled by default. If disabled, the user can select months, but not days.
983  * It should be used if you won't need such selection for the widget usage.
984  *
985  * @param obj The calendar object
986  * @param enabled Boolean to enable (true) or disable (false) day selection
987  *
988  * @ingroup Calendar
989  */
990 EAPI void
991 elm_calendar_day_selection_enabled_set(Evas_Object *obj, Eina_Bool enabled)
992 {
993    ELM_CHECK_WIDTYPE(obj, widtype);
994    Widget_Data *wd = elm_widget_data_get(obj);
995    if (!wd) return;
996    wd->selection_enabled = enabled;
997    if (enabled)
998      _select(wd, wd->selected_it);
999    else
1000      _unselect(wd, wd->selected_it);
1001 }
1002
1003 /**
1004  * Get day selection state
1005  *
1006  * Enabled by default. If disabled, the user can select months, but not days.
1007  * It should be used if you won't need such selection for the widget usage.
1008  *
1009  * @param obj The calendar object
1010  * @return True if day selection is enabled, or false otherwise. It will
1011  * return false if it can't get widget data.
1012  *
1013  * @ingroup Calendar
1014  */
1015 EAPI Eina_Bool
1016 elm_calendar_day_selection_enabled_get(const Evas_Object *obj)
1017 {
1018    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1019    Widget_Data *wd = elm_widget_data_get(obj);
1020    if (!wd) return EINA_FALSE;
1021    return wd->selection_enabled;
1022 }
1023
1024 /**
1025  * Set selected time
1026  *
1027  * Set the time selected, changing the displayed month if needed.
1028  * Selected time changes when the user changes the month or select a day.
1029  *
1030  * @param obj The calendar object
1031  * @param selected_time A tm struct to represent the selected date
1032  *
1033  * @ingroup Calendar
1034  */
1035 EAPI void
1036 elm_calendar_selected_time_set(Evas_Object *obj, struct tm *selected_time)
1037 {
1038    ELM_CHECK_WIDTYPE(obj, widtype);
1039    Widget_Data *wd = elm_widget_data_get(obj);
1040    if (!wd) return;
1041
1042    EINA_SAFETY_ON_NULL_RETURN(selected_time);
1043    wd->selected_time = *selected_time;
1044    _populate(obj);
1045    return;
1046 }
1047
1048 /**
1049  * Get selected time
1050  *
1051  * Get the time selected by the user.
1052  * Selected time changes when the user changes the month or select a day.
1053  *
1054  * @param obj The calendar object
1055  * @param selected_time A tm struct to represent the selected date
1056  * @return It will return false if it can't get widget data, or true otherwise
1057  *
1058  * @ingroup Calendar
1059  */
1060 EAPI Eina_Bool
1061 elm_calendar_selected_time_get(const Evas_Object *obj, struct tm *selected_time)
1062 {
1063    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1064    Widget_Data *wd = elm_widget_data_get(obj);
1065    if (!wd) return EINA_FALSE;
1066    EINA_SAFETY_ON_NULL_RETURN_VAL(selected_time, EINA_FALSE);
1067    *selected_time = wd->selected_time;
1068    return EINA_TRUE;
1069 }
1070
1071 /**
1072  * Set a function to format the string that will be used to display
1073  * month - year
1074  *
1075  * By default it uses strftime with "%B %Y" format string.
1076  * It should allocate the memory that will be used by the string,
1077  * that will be freed by the widget after usage.
1078  * A pointer to the string and a pointer to the time struct will be provided.
1079  *
1080  * Example:
1081  * @code
1082  * static char *
1083  * _format_month_year(struct tm *selected_time)
1084  * {
1085  *    char buf[32];
1086  *    if (!strftime(buf, sizeof(buf), "%B %Y", selected_time)) return NULL;
1087  *    return strdup(buf);
1088  * }
1089  * elm_calendar_format_function_set(calendar, _format_month_year);
1090  * @endcode
1091  *
1092  * @param obj The calendar object
1093  * @param format_function Function to set the month-year string given
1094  * the selected date
1095  *
1096  * @ingroup Calendar
1097  */
1098 EAPI void
1099 elm_calendar_format_function_set(Evas_Object *obj, char * (*format_function) (struct tm *selected_time))
1100 {
1101    ELM_CHECK_WIDTYPE(obj, widtype);
1102    Widget_Data *wd = elm_widget_data_get(obj);
1103    if (!wd) return;
1104    wd->format_func = format_function;
1105 }
1106
1107 /**
1108  * Add a new mark to the calendar
1109  *
1110  * Add a mark that will be drawn in the calendar respecting the insertion time
1111  * and periodicity. It will emit the type as signal to the widget theme.
1112  * By default, it supports "holiday" and "checked", but it can be extended.
1113  *
1114  * It won't immediately update the calendar, drawing the marks. For this, call
1115  * elm_calendar_marks_draw().
1116  *
1117  * Example
1118  * @code
1119  * struct tm selected_time;
1120  * time_t current_time;
1121  *
1122  * current_time = time(NULL) + 5 * 84600;
1123  * localtime_r(&current_time, &selected_time);
1124  * elm_calendar_mark_add(cal, "holiday", selected_time, ELM_CALENDAR_ANNUALLY);
1125  *
1126  * current_time = time(NULL) + 1 * 84600;
1127  * localtime_r(&current_time, &selected_time);
1128  * elm_calendar_mark_add(cal, "checked", selected_time, ELM_CALENDAR_UNIQUE);
1129  *
1130  * elm_calendar_marks_draw(cal);
1131  * @endcode
1132  *
1133  * @param obj The calendar object
1134  * @param mark_type A string used to define the type of mark. It will be
1135  * emitted to the theme, that should display a related modification on these
1136  * days representation.
1137  * @param mark_time A time struct to represent the date of inclusion of the
1138  * mark. For marks that repeats it will just be displayed after the inclusion
1139  * date in the calendar.
1140  * @param repeat Repeat the event following this periodicity. Can be a unique
1141  * mark (that don't repeat), daily, weekly, monthly or annually.
1142  *
1143  * @return The created mark or NULL upon failure
1144  *
1145  * @ingroup Calendar
1146  */
1147 EAPI Elm_Calendar_Mark *
1148 elm_calendar_mark_add(Evas_Object *obj, const char *mark_type, struct tm *mark_time, Elm_Calendar_Mark_Repeat repeat)
1149 {
1150    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1151    Widget_Data *wd = elm_widget_data_get(obj);
1152    Elm_Calendar_Mark *mark;
1153    if (!wd) return NULL;
1154
1155    mark = _mark_new(obj, mark_type, mark_time, repeat);
1156    wd->marks = eina_list_append(wd->marks, mark);
1157    mark->node = eina_list_last(wd->marks);
1158    return mark;
1159 }
1160
1161 /**
1162  * Delete mark from the calendar.
1163  *
1164  * @param mark The mark to delete
1165  *
1166  * @ingroup Calendar
1167  */
1168 EAPI void
1169 elm_calendar_mark_del(Elm_Calendar_Mark *mark)
1170 {
1171    Evas_Object *obj;
1172    Widget_Data *wd;
1173
1174    EINA_SAFETY_ON_NULL_RETURN(mark);
1175
1176    obj = mark->obj;
1177    wd = elm_widget_data_get(obj);
1178    if (!wd) return;
1179
1180    wd->marks = eina_list_remove_list(wd->marks, mark->node);
1181    _mark_free(mark);
1182 }
1183
1184 /**
1185  * Remove all the marks from the calendar
1186  *
1187  * @param obj The calendar object
1188  *
1189  * @ingroup Calendar
1190  */
1191 EAPI void
1192 elm_calendar_marks_clear(Evas_Object *obj)
1193 {
1194    ELM_CHECK_WIDTYPE(obj, widtype);
1195    Widget_Data *wd = elm_widget_data_get(obj);
1196    Elm_Calendar_Mark *mark;
1197
1198    if (!wd) return;
1199    EINA_LIST_FREE(wd->marks, mark)
1200       _mark_free(mark);
1201 }
1202
1203 /**
1204  * Returns a list of all the calendar marks.
1205  *
1206  * @param obj The calendar object
1207  * @return An Eina_List* of the calendar marks, or NULL on failure
1208  *
1209  * @ingroup Calendar
1210  */
1211 EAPI const Eina_List *
1212 elm_calendar_marks_get(const Evas_Object *obj)
1213 {
1214    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
1215    Widget_Data *wd = elm_widget_data_get(obj);
1216    if (!wd) return NULL;
1217    return wd->marks;
1218 }
1219
1220 /**
1221  * Draw calendar marks.
1222  *
1223  * Should be used after adding, removing or clearing marks.
1224  * It will go through the entire marks list updating the calendar
1225  * (not a cheap function). So if lots of marks will be added,
1226  * add all the marks and then call this function.
1227  *
1228  * When the month is changed marks will be drawed.
1229  *
1230  * @param obj The calendar object
1231  *
1232  * @ingroup Calendar
1233  */
1234 EAPI void
1235 elm_calendar_marks_draw(Evas_Object *obj)
1236 {
1237    ELM_CHECK_WIDTYPE(obj, widtype);
1238    Widget_Data *wd = elm_widget_data_get(obj);
1239    if (!wd) return;
1240    _populate(obj);
1241 }
1242
1243 /**
1244  * Set a text color to the saturday color.
1245  *
1246  * Deprecated. use elm_calendar_mark_add() instead like:
1247  *
1248  * @code
1249  * struct tm t = { 0, 0, 12, 6, 0, 0, 5, 5, -1 };
1250  * elm_calendar_mark_add(obj, "sat", &t, ELM_CALENDAR_WEEKLY);
1251  * @endcode
1252  *
1253  * @param obj The calendar object
1254  * @param pos The text position
1255  *
1256  * @ingroup Calendar
1257  */
1258 EINA_DEPRECATED EAPI void
1259 elm_calendar_text_saturday_color_set(Evas_Object *obj, int pos)
1260 {
1261    ELM_CHECK_WIDTYPE(obj, widtype);
1262    Widget_Data *wd = elm_widget_data_get(obj);
1263    if (!wd) return;
1264    _text_day_color_set(wd, DAY_SATURDAY, pos);
1265 }
1266
1267 /**
1268  * Set a text color to the sunday color.
1269  *
1270  * Deprecated. use elm_calendar_mark_add() instead like:
1271  *
1272  * @code
1273  * struct tm t = { 0, 0, 12, 7, 0, 0, 6, 6, -1 };
1274  * elm_calendar_mark_add(obj, "sun", &t, ELM_CALENDAR_WEEKLY);
1275  * @endcode
1276  *
1277  * @param obj The calendar object
1278  * @param pos The text position
1279  *
1280  * @ingroup Calendar
1281  */
1282 EINA_DEPRECATED EAPI void
1283 elm_calendar_text_sunday_color_set(Evas_Object *obj, int pos)
1284 {
1285    ELM_CHECK_WIDTYPE(obj, widtype);
1286    Widget_Data *wd = elm_widget_data_get(obj);
1287    if (!wd) return;
1288    _text_day_color_set(wd, DAY_SUNDAY, pos);
1289 }
1290
1291 /**
1292  * Set a text color to the weekday color.
1293  *
1294  * Deprecated. use elm_calendar_mark_add() instead like:
1295  *
1296  * @code
1297  * struct tm t = { 0, 0, 12, 1, 0, 0, 0, 0, -1 };
1298  *
1299  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // monday
1300  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1301  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // tuesday
1302  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1303  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // wednesday
1304  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1305  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // thursday
1306  * t.tm_tm_mday++; t.tm_wday++; t.tm_yday++;
1307  * elm_calendar_mark_add(obj, "week", &t, ELM_CALENDAR_WEEKLY); // friday
1308  * @endcode
1309  *
1310  * @param obj The calendar object
1311  * @param pos The text position
1312  *
1313  * @ingroup Calendar
1314  */
1315 EINA_DEPRECATED EAPI void
1316 elm_calendar_text_weekday_color_set(Evas_Object *obj, int pos)
1317 {
1318    ELM_CHECK_WIDTYPE(obj, widtype);
1319    Widget_Data *wd = elm_widget_data_get(obj);
1320    if (!wd) return;
1321    _text_day_color_set(wd, DAY_WEEKDAY, pos);
1322 }