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