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