Merge branch 'intefl/svn_merge' of ssh://165.213.149.219:21922/slp/pkgs/e/elementary...
[framework/uifw/elementary.git] / src / lib / elm_datetime.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 #ifdef HAVE_LOCALE_H
5 # include <locale.h>
6 #endif
7
8 #ifdef HAVE_LANGINFO_H
9 # include <langinfo.h>
10 #endif
11
12 typedef struct _Widget_Data Widget_Data;
13 typedef struct _Datetime_Field Datetime_Field;
14 typedef struct _Datetime_Mod_Api Datetime_Mod_Api;
15 typedef struct _Format_Map Format_Map;
16
17 #define DATETIME_TYPE_COUNT         6
18 #define MAX_FORMAT_LEN              64
19 #define MAX_SEPARATOR_LEN           6
20 #define MAX_FIELD_FORMAT_LEN        3
21 #define MIN_DAYS_IN_MONTH           28
22 #define BUFFER_SIZE                 1024
23
24 // interface between EDC & C code. Field names & signal names.
25 // values 0 to DATETIME_TYPE_COUNT are valid range, can be substituted for %d.
26 #define EDC_DATETIME_ENABLE_SIG_STR          "elm,state,enabled"
27 #define EDC_DATETIME_DISABLE_SIG_STR         "elm,state,disabled"
28 #define EDC_DATETIME_FOCUSIN_SIG_STR         "elm,action,focus"
29 #define EDC_DATETIME_FOCUSOUT_SIG_STR        "elm,action,unfocus"
30 #define EDC_PART_FIELD_STR                   "field%d"
31 #define EDC_PART_SEPARATOR_STR               "separator%d"
32 #define EDC_PART_FIELD_ENABLE_SIG_STR        "field%d,enable"
33 #define EDC_PART_FIELD_DISABLE_SIG_STR       "field%d,disable"
34
35 // struct tm  does not define the fields in the order from year, month, date, hour, minute.
36 // values are reassigned to an array for easy handling.
37 #define DATETIME_TM_ARRAY(intptr, tmptr) int *intptr[] = {&(tmptr)->tm_year, \
38            &(tmptr)->tm_mon, &(tmptr)->tm_mday, &(tmptr)->tm_hour, &(tmptr)->tm_min}
39
40 struct _Datetime_Field
41 {
42    Evas_Object *item_obj;
43    char fmt[MAX_FIELD_FORMAT_LEN];
44    Elm_Datetime_Field_Type type;
45    const char *separator;
46    int location;          // location of the field as per the current format
47    int min, max;
48    Eina_Bool fmt_exist:1;  // whether field format is present or not
49    Eina_Bool visible:1;    // whether field can be visible or not
50 };
51
52 struct _Datetime_Mod_Api
53 {
54    Elm_Datetime_Module_Data *(*obj_hook) (Evas_Object *obj);
55    void (*obj_unhook) (Elm_Datetime_Module_Data *module_data);
56    Evas_Object *(*field_create) (Elm_Datetime_Module_Data *module_data,
57                                      Elm_Datetime_Field_Type field_type);
58    void (*field_value_display) (Elm_Datetime_Module_Data *module_data,
59                                     Evas_Object *obj);
60 };
61
62 struct _Widget_Data
63 {
64    Evas_Object *base;
65    Datetime_Field field_list[DATETIME_TYPE_COUNT]; // fixed set of fields.
66    struct tm curr_time, min_limit, max_limit;
67    Elm_Datetime_Module_Data *mod_data;
68    char format[MAX_FORMAT_LEN];
69    Eina_Bool user_format:1; // whether user set format or default format.
70 };
71
72 struct _Format_Map
73 {
74    char *fmt_char;
75    int def_min;
76    int def_max;
77 };
78
79 // default limits for individual fields
80 static const Format_Map mapping[DATETIME_TYPE_COUNT] = {
81    [ELM_DATETIME_YEAR]   =  { "Yy",   0, 137 },
82    [ELM_DATETIME_MONTH]  =  { "mbBh", 0,  11  },
83    [ELM_DATETIME_DATE]   =  { "de",   1,  31  },
84    [ELM_DATETIME_HOUR]   =  { "IHkl", 0,  23  },
85    [ELM_DATETIME_MINUTE] =  { "M",    0,  59  },
86    [ELM_DATETIME_AMPM]   =  { "pP",   0,  1   }
87 };
88
89 static const char *multifield_formats = "cxXrRTDF";
90
91 static Datetime_Mod_Api *dt_mod = NULL;
92 static const char *widtype = NULL;
93
94 static void _del_hook(Evas_Object *obj);
95 static void _disable_hook(Evas_Object *obj);
96 static void _translate_hook(Evas_Object *obj);
97 static void _on_focus_hook(void *data __UNUSED__, Evas_Object *obj);
98 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
99 static void _sizing_eval(Evas_Object *obj);
100 static void _theme_hook(Evas_Object *obj);
101 static void _validate_datetime_limits(struct tm *time1, struct tm *time2, Eina_Bool swap);
102 static void _apply_field_limits(Evas_Object *obj);
103 static void _apply_range_restrictions(Evas_Object *obj, struct tm *time);
104 static const char *_field_format_get(Evas_Object * obj, Elm_Datetime_Field_Type field_type);
105 static void _field_limit_get(Evas_Object * obj, Elm_Datetime_Field_Type field_type,
106                              int *range_min, int *range_max);
107 static void _reload_format(Evas_Object *obj);
108 static void _field_list_display(Evas_Object *obj);
109 static void _field_list_arrange(Evas_Object *obj);
110 static void _field_list_init(Evas_Object *obj);
111
112 static const char SIG_CHANGED[] = "changed";
113 static const char SIG_LANGUAGE_CHANGED[] = "language,changed";
114 static const Evas_Smart_Cb_Description _signals[] = {
115        {SIG_CHANGED, ""},
116        {SIG_LANGUAGE_CHANGED, ""},
117        {NULL, NULL}
118 };
119
120 static Datetime_Mod_Api *
121 _dt_mod_init()
122 {
123    Elm_Module *mod = NULL;
124    if (!(mod = _elm_module_find_as("datetime/api"))) return NULL;
125
126    mod->api = malloc(sizeof(Datetime_Mod_Api));
127    if (!mod->api) return NULL;
128
129    ((Datetime_Mod_Api *)(mod->api))->obj_hook            =   _elm_module_symbol_get(mod, "obj_hook");
130    ((Datetime_Mod_Api *)(mod->api))->obj_unhook          =   _elm_module_symbol_get(mod, "obj_unhook");
131    ((Datetime_Mod_Api *)(mod->api))->field_create        =   _elm_module_symbol_get(mod, "field_create");
132    ((Datetime_Mod_Api *)(mod->api))->field_value_display =   _elm_module_symbol_get(mod, "field_value_display");
133
134    return mod->api;
135 }
136
137 static void
138 _del_hook(Evas_Object *obj)
139 {
140    Widget_Data *wd;
141    Datetime_Field *tmp;
142    unsigned int idx;
143
144    wd = elm_widget_data_get(obj);
145    if (!wd) return;
146
147    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
148      {
149         tmp = wd->field_list + idx;
150         evas_object_del(tmp->item_obj);
151         eina_stringshare_del(tmp->separator);
152      }
153
154    if ((dt_mod) && (dt_mod->obj_unhook))
155      dt_mod->obj_unhook(wd->mod_data); // module - unhook
156
157    free(wd);
158 }
159
160 static void
161 _disable_hook(Evas_Object *obj)
162 {
163    Widget_Data *wd;
164
165    wd = elm_widget_data_get(obj);
166    if (!wd || !wd->base) return;
167    if (elm_widget_disabled_get(obj))
168      edje_object_signal_emit(wd->base, EDC_DATETIME_DISABLE_SIG_STR, "elm");
169    else
170      edje_object_signal_emit(wd->base, EDC_DATETIME_ENABLE_SIG_STR, "elm");
171 }
172
173 static void
174 _translate_hook(Evas_Object *obj)
175 {
176    Widget_Data *wd;
177    wd = elm_widget_data_get(obj);
178    if (!wd) return;
179
180    if (!wd->user_format) _reload_format(obj);
181    else _field_list_display(obj);
182    evas_object_smart_callback_call(obj, SIG_LANGUAGE_CHANGED, NULL);
183 }
184
185 static void
186 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
187 {
188    Widget_Data *wd;
189
190    wd = elm_widget_data_get(obj);
191    if (!wd) return;
192
193    if (elm_widget_focus_get(obj))
194      edje_object_signal_emit(wd->base, EDC_DATETIME_FOCUSIN_SIG_STR, "elm");
195    else
196      edje_object_signal_emit(wd->base, EDC_DATETIME_FOCUSOUT_SIG_STR, "elm");
197 }
198
199 static Eina_List *
200 _datetime_items_get(const Evas_Object *obj)
201 {
202    Widget_Data *wd;
203    Eina_List *items = NULL;
204    Datetime_Field *field;
205    int loc, count = 0;
206    unsigned int idx;
207
208    wd = elm_widget_data_get(obj);
209    if (!wd) return NULL;
210
211    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
212      {
213         field = wd->field_list + idx;
214         if (field->fmt_exist && field->visible) count++;
215      }
216    for (loc = 0; loc < count; loc++)
217      {
218         for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
219           {
220              field = wd->field_list + idx;
221              if (field->location == loc)
222                items = eina_list_append(items, field->item_obj);
223           }
224      }
225
226    return items;
227 }
228
229 static Eina_Bool
230 _elm_datetime_focus_next_hook(const Evas_Object *obj, Elm_Focus_Direction dir, Evas_Object **next)
231 {
232    Widget_Data *wd;
233    const Eina_List *items;
234    void *(*list_data_get) (const Eina_List *list);
235    Eina_List *(*list_free) (Eina_List *list);
236    Eina_Bool ret;
237
238    wd = elm_widget_data_get(obj);
239    if (!wd) return EINA_FALSE;
240
241    if ((items = elm_widget_focus_custom_chain_get(obj)))
242      {
243         list_data_get = eina_list_data_get;
244         list_free = NULL;
245      }
246    else
247      {
248         items = _datetime_items_get(obj);
249         list_data_get = eina_list_data_get;
250         list_free = eina_list_free;
251         if (!items) return EINA_FALSE;
252      }
253
254    ret = elm_widget_focus_list_next_get(obj, items, list_data_get, dir, next);
255    if (list_free) list_free((Eina_List *)items);
256
257    return ret;
258 }
259
260 static void
261 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
262 {
263    Widget_Data *wd;
264
265    wd = elm_widget_data_get(obj);
266    if (!wd) return;
267
268    edje_object_mirrored_set(wd->base, rtl);
269 }
270
271 static void
272 _sizing_eval(Evas_Object *obj)
273 {
274    Widget_Data *wd;
275    Datetime_Field *field;
276    Evas_Coord minw = -1, minh = -1;
277    unsigned int idx, field_count = 0;
278
279    wd = elm_widget_data_get(obj);
280    if (!wd || !wd->base) return;
281    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
282      {
283         field = wd->field_list + idx;
284         if ((field->visible) && (field->fmt_exist)) field_count ++;
285      }
286    if (field_count)
287      elm_coords_finger_size_adjust(field_count, &minw, 1, &minh);
288    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
289    evas_object_size_hint_min_set(obj, minw, minh);
290    evas_object_size_hint_max_set(obj, -1, -1);
291 }
292
293 static void
294 _theme_hook(Evas_Object *obj)
295 {
296    Widget_Data *wd;
297    Datetime_Field *field;
298    char buf[BUFFER_SIZE];
299    unsigned int idx;
300
301    wd = elm_widget_data_get(obj);
302    if (!wd || !wd->base) return;
303
304    _elm_theme_object_set(obj, wd->base, "datetime", "base",
305                          elm_widget_style_get(obj));
306    _elm_widget_mirrored_reload(obj);
307    _mirrored_set(obj, elm_widget_mirrored_get(obj));
308
309    edje_object_scale_set(wd->base, elm_widget_scale_get(obj) * _elm_config->scale);
310
311    if (elm_widget_disabled_get(obj))
312      edje_object_signal_emit(wd->base, EDC_DATETIME_DISABLE_SIG_STR,"elm");
313    else
314      edje_object_signal_emit(wd->base, EDC_DATETIME_ENABLE_SIG_STR, "elm");
315
316    if ((!dt_mod) || (!dt_mod->field_value_display)) return;
317
318    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
319      {
320         field = wd->field_list + idx;
321         if (field->fmt_exist && field->visible)
322           {
323              snprintf(buf, sizeof(buf), EDC_PART_FIELD_ENABLE_SIG_STR, field->location);
324              edje_object_signal_emit(wd->base, buf, "elm");
325              snprintf(buf, sizeof(buf), EDC_PART_SEPARATOR_STR, field->location);
326              edje_object_part_text_set(wd->base, buf, field->separator);
327              dt_mod->field_value_display(wd->mod_data, field->item_obj);
328           }
329         else
330           {
331              snprintf(buf, sizeof(buf),EDC_PART_FIELD_DISABLE_SIG_STR, field->location);
332              edje_object_signal_emit(wd->base, buf, "elm");
333           }
334      }
335    edje_object_message_signal_process(wd->base);
336    _sizing_eval(obj);
337 }
338
339 static int
340 _max_days_get(int year, int month)
341 {
342    struct tm time1;
343    time_t t;
344    int day;
345
346    t = time(NULL);
347    localtime_r(&t, &time1);
348    time1.tm_year = year;
349    time1.tm_mon = month;
350    for(day = MIN_DAYS_IN_MONTH; day <= mapping[ELM_DATETIME_DATE].def_max; day++)
351      {
352         time1.tm_mday = day;
353         mktime(&time1);
354         if (time1.tm_mday == 1) break;
355      }
356    day --;
357    return day;
358 }
359
360 static Eina_Bool
361 _date_cmp(struct tm *time1, struct tm *time2)
362 {
363    unsigned int idx;
364    DATETIME_TM_ARRAY(timearr1, time1);
365    DATETIME_TM_ARRAY(timearr2, time2);
366
367    for (idx = 0; idx < DATETIME_TYPE_COUNT - 1; idx++)
368      {
369         if (*timearr1[idx] != *timearr2[idx])
370           return EINA_FALSE;
371      }
372    return EINA_TRUE;
373 }
374
375 // validates curr_time/min_limt/max_limit according to the newly set value
376 static void
377 _validate_datetime_limits(struct tm *time1, struct tm *time2, Eina_Bool swap)
378 {
379    struct tm *t1, *t2;
380    unsigned int idx;
381    if (!time1 || !time2) return;
382
383    t1 = (swap) ? time2 : time1;
384    t2 = (swap) ? time1 : time2;
385
386    DATETIME_TM_ARRAY(timearr1, time1);
387    DATETIME_TM_ARRAY(timearr2, time2);
388    for (idx = 0; idx < DATETIME_TYPE_COUNT - 1; idx++)
389      {
390        if (*timearr1[idx] < *timearr2[idx])
391          {
392             memcpy(t1, t2, sizeof(struct tm));
393             break;
394          }
395        else if (*timearr1[idx] > *timearr2[idx])
396          break;
397      }
398 }
399
400 static void
401 _apply_field_limits(Evas_Object *obj)
402 {
403    Widget_Data *wd;
404    Datetime_Field *field;
405    int val;
406    unsigned int idx = 0;
407
408    wd = elm_widget_data_get(obj);
409    if (!wd) return;
410
411    DATETIME_TM_ARRAY(timearr, &wd->curr_time);
412    for (idx = 0; idx < DATETIME_TYPE_COUNT - 1; idx++)
413      {
414         field = wd->field_list + idx;
415         val = *timearr[idx];
416         if (val < field->min)
417           *timearr[idx] = field->min;
418         else if (val > field->max)
419           *timearr[idx] = field->max;
420      }
421    _field_list_display(obj);
422 }
423
424 static void
425 _apply_range_restrictions(Evas_Object *obj, struct tm *tim)
426 {
427    Widget_Data *wd;
428    unsigned int idx;
429    int val, min, max;
430
431    wd = elm_widget_data_get(obj);
432    if (!wd || !tim) return;
433
434    DATETIME_TM_ARRAY(timearr, tim);
435    for (idx = ELM_DATETIME_MONTH; idx < DATETIME_TYPE_COUNT - 1; idx++)
436      {
437         val = *timearr[idx];
438         min = mapping[idx].def_min;
439         max = mapping[idx].def_max;
440         if (idx == ELM_DATETIME_DATE)
441           max = _max_days_get(tim->tm_year, tim->tm_mon);
442         if (val < min)
443           *timearr[idx] = min;
444         else if (val > max)
445           *timearr[idx] = max;
446      }
447 }
448
449 static const char *
450 _field_format_get(Evas_Object * obj, Elm_Datetime_Field_Type field_type)
451 {
452    Widget_Data *wd;
453    Datetime_Field *field;
454
455    wd = elm_widget_data_get(obj);
456    if (!wd) return NULL;
457
458    field = wd->field_list + field_type;
459    if (!field) return NULL;
460
461    return field->fmt;
462 }
463
464 static void
465 _field_limit_get(Evas_Object * obj, Elm_Datetime_Field_Type field_type, int *range_min, int *range_max)
466 {
467    Widget_Data *wd;
468    Datetime_Field *field;
469    int min, max, max_days;
470    unsigned int idx;
471
472    wd = elm_widget_data_get(obj);
473    if (!wd) return;
474
475    field = wd->field_list + field_type;
476    if (!field) return;
477
478    min = field->min;
479    max = field->max;
480
481    DATETIME_TM_ARRAY(curr_timearr, &wd->curr_time);
482    DATETIME_TM_ARRAY(min_timearr, &wd->min_limit);
483    DATETIME_TM_ARRAY(max_timearr, &wd->max_limit);
484
485    for (idx = 0; idx < field->type; idx++)
486      if (*curr_timearr[idx] > *min_timearr[idx]) break;
487    if ((idx == field_type) && (min < *min_timearr[field_type]))
488      min = *min_timearr[field_type];
489    if (field_type == ELM_DATETIME_DATE)
490      {
491         max_days = _max_days_get(wd->curr_time.tm_year, wd->curr_time.tm_mon);
492         if (max > max_days) max = max_days;
493      }
494    for (idx = 0; idx < field->type; idx++)
495      if (*curr_timearr[idx] < *max_timearr[idx]) break;
496    if ((idx == field_type) && (max > *max_timearr[field_type]))
497      max = *max_timearr[field_type];
498
499    *range_min = min;
500    *range_max = max;
501 }
502
503 static void
504 _field_list_display(Evas_Object *obj)
505 {
506    Widget_Data *wd;
507    Datetime_Field *field;
508    unsigned int idx= 0;
509
510    wd = elm_widget_data_get(obj);
511    if (!wd) return;
512
513    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
514      {
515         field = wd->field_list + idx;
516         if (field->fmt_exist && field->visible)
517           {
518              if ((dt_mod) && (dt_mod->field_value_display))
519                dt_mod->field_value_display(wd->mod_data, field->item_obj);
520           }
521      }
522 }
523
524 static void
525 _field_list_arrange(Evas_Object *obj)
526 {
527    Widget_Data *wd;
528    Datetime_Field *field;
529    char buf[BUFFER_SIZE];
530    int idx;
531
532    wd = elm_widget_data_get(obj);
533    if (!wd) return;
534
535    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
536      {
537         field = wd->field_list + idx;
538         edje_object_part_unswallow(wd->base, field->item_obj);
539      }
540    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
541      {
542         field = wd->field_list + idx;
543         if (field->visible && field->fmt_exist)
544           {
545              snprintf(buf, sizeof(buf), EDC_PART_FIELD_STR, field->location);
546              edje_object_part_swallow(wd->base, buf, field->item_obj);
547           }
548         else evas_object_hide(field->item_obj);
549      }
550    _sizing_eval(obj);
551    _field_list_display(obj);
552 }
553
554 // FIXME: provide nl_langinfo on Windows if possible
555 // returns expanded format string for corresponding multi-field format character
556 static char *
557 _expanded_fmt_str_get(char ch)
558 {
559    char *exp_fmt = "";
560    switch (ch)
561      {
562       case 'c':
563 #ifdef HAVE_LANGINFO_H
564          exp_fmt = nl_langinfo(D_T_FMT);
565 #else
566          exp_fmt = "";
567 #endif
568          break;
569       case 'x':
570 #ifdef HAVE_LANGINFO_H
571          exp_fmt = nl_langinfo(D_FMT);
572 #else
573          exp_fmt = "";
574 #endif
575          break;
576       case 'X':
577 #ifdef HAVE_LANGINFO_H
578          exp_fmt = nl_langinfo(T_FMT);
579 #else
580          exp_fmt = "";
581 #endif
582          break;
583       case 'r':
584 #ifdef HAVE_LANGINFO_H
585          exp_fmt = nl_langinfo(T_FMT_AMPM);
586 #else
587          exp_fmt = "";
588 #endif
589          break;
590       case 'R':
591          exp_fmt =  "%H:%M";
592          break;
593       case 'T':
594          exp_fmt =  "%H:%M:%S";
595          break;
596       case 'D':
597          exp_fmt =  "%m/%d/%y";
598          break;
599       case 'F':
600          exp_fmt =  "%Y-%m-%d";
601          break;
602       default:
603          exp_fmt =  "";
604          break;
605      }
606    return exp_fmt;
607 }
608
609 static void
610 _expand_format(char * dt_fmt)
611 {
612    char *ptr, *expanded_fmt, ch;
613    char buf[MAX_FORMAT_LEN] = {0,};
614    unsigned int idx = 0, len = 0;
615    Eina_Bool fmt_char = EINA_FALSE;
616
617    ptr = dt_fmt;
618    while ((ch = *ptr))
619      {
620         if ((fmt_char) && (strchr(multifield_formats, ch)))
621           {
622              // replace the multi-field format characters with corresponding expanded format
623              expanded_fmt = _expanded_fmt_str_get(ch);
624              len = strlen(expanded_fmt);
625              buf[--idx] = 0;
626              strncat(buf, expanded_fmt, len);
627              idx += len;
628           }
629         else buf[idx++] = ch;
630         if (ch == '%') fmt_char = EINA_TRUE;
631         else fmt_char = EINA_FALSE;
632         ptr++;
633      }
634    buf[idx] = 0;
635    strncpy(dt_fmt, buf, MAX_FORMAT_LEN);
636 }
637
638 static unsigned int
639 _parse_format(Evas_Object *obj, char *fmt_ptr)
640 {
641    Widget_Data *wd;
642    Datetime_Field *field = NULL;
643    unsigned int len = 0, idx, location = 0;
644    char separator[MAX_SEPARATOR_LEN];
645    char cur;
646    Eina_Bool fmt_parsing = EINA_FALSE, sep_parsing = EINA_FALSE,
647              sep_lookup = EINA_FALSE;
648
649    wd = elm_widget_data_get(obj);
650
651    while ((cur = *fmt_ptr))
652      {
653         if (fmt_parsing)
654           {
655              for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
656                {
657                   if (strchr(mapping[idx].fmt_char, cur))
658                     {
659                        field = wd->field_list + idx;
660                        // ignore the fields already have or disabled
661                        // valid formats, means already parsed & repeated, ignore.
662                        if (!field->visible || field->location != -1) break;
663                        field->fmt[1] = cur;
664                        field->fmt_exist = EINA_TRUE;
665                        field->location = location++;
666                        fmt_parsing = EINA_FALSE;
667                        sep_lookup = EINA_TRUE;
668                        len = 0;
669                        break;
670                     }
671                }
672           }
673         if (cur == ' ') separator[len++] = cur;
674         else if (cur == '%') fmt_parsing = EINA_TRUE;
675         if ((cur == ' ') || (cur == '%'))
676           {
677              sep_parsing = EINA_FALSE;
678              // set the separator to previous field
679              separator[len] = 0;
680              if (field) eina_stringshare_replace(&field->separator, separator);
681           }
682         if (sep_parsing && (len < MAX_SEPARATOR_LEN-1) &&
683             (field->type != ELM_DATETIME_AMPM) &&
684             (!((field->type == ELM_DATETIME_MINUTE) && (cur ==':'))))
685            separator[len++] = cur;
686         if (sep_lookup) sep_parsing = EINA_TRUE;
687         sep_lookup = EINA_FALSE;
688         fmt_ptr++;
689    }
690    // return the number of valid fields parsed.
691    return location;
692 }
693
694 static void
695 _reload_format(Evas_Object *obj)
696 {
697    Widget_Data *wd;
698    Datetime_Field *field;
699    char buf[BUFFER_SIZE];
700    unsigned int idx, field_count;
701    char *dt_fmt;
702
703    wd = elm_widget_data_get(obj);
704    if (!wd) return;
705
706    // FIXME: provide nl_langinfo on Windows if possible
707    // fetch the default format from Libc.
708    if (!wd->user_format)
709 #ifdef HAVE_LANGINFO_H
710      strncpy(wd->format, nl_langinfo(D_T_FMT), MAX_FORMAT_LEN);
711 #else
712      strncpy(wd->format, "", MAX_FORMAT_LEN);
713 #endif
714
715    dt_fmt = (char *)malloc(MAX_FORMAT_LEN);
716    strncpy(dt_fmt, wd->format, MAX_FORMAT_LEN);
717
718    _expand_format(dt_fmt);
719
720    // reset all the fields to disable state
721    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
722      {
723         field = wd->field_list + idx;
724         field->fmt_exist = EINA_FALSE;
725         field->location = -1;
726      }
727
728    field_count = _parse_format(obj, dt_fmt);
729    free(dt_fmt);
730
731    // assign locations to disabled fields for uniform usage
732    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
733      {
734         field = wd->field_list + idx;
735         if (field->location == -1) field->location = field_count++;
736
737         if (field->fmt_exist && field->visible)
738           {
739              snprintf(buf, sizeof(buf), EDC_PART_FIELD_ENABLE_SIG_STR,
740                       field->location);
741              edje_object_signal_emit(wd->base, buf, "elm");
742           }
743         else
744           {
745              snprintf(buf, sizeof(buf),EDC_PART_FIELD_DISABLE_SIG_STR,
746                       field->location);
747              edje_object_signal_emit(wd->base, buf, "elm");
748           }
749         snprintf(buf, sizeof(buf), EDC_PART_SEPARATOR_STR, (field->location + 1));
750         edje_object_part_text_set(wd->base, buf, field->separator);
751      }
752    edje_object_message_signal_process(wd->base);
753    _field_list_arrange(obj);
754 }
755
756 static void
757 _field_list_init(Evas_Object *obj)
758 {
759    Widget_Data *wd;
760    Datetime_Field *field;
761    unsigned int idx;
762    time_t t;
763
764    wd = elm_widget_data_get(obj);
765    if (!wd) return;
766
767    t = time(NULL);
768    localtime_r(&t, &wd->curr_time);
769
770    for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
771      {
772         field = wd->field_list + idx;
773         field->type = ELM_DATETIME_YEAR + idx;
774         field->fmt[0] = '%';
775         field->fmt_exist = EINA_FALSE;
776         field->visible  = EINA_TRUE;
777         field->min = mapping[idx].def_min;
778         field->max = mapping[idx].def_max;
779      }
780    DATETIME_TM_ARRAY(min_timearr, &wd->min_limit);
781    DATETIME_TM_ARRAY(max_timearr, &wd->max_limit);
782    for (idx = 0; idx < DATETIME_TYPE_COUNT-1; idx++)
783      {
784         *min_timearr[idx] = mapping[idx].def_min;
785         *max_timearr[idx] = mapping[idx].def_max;
786      }
787 }
788
789 EAPI Evas_Object *
790 elm_datetime_add(Evas_Object *parent)
791 {
792    Evas_Object *obj;
793    Evas *e;
794    Widget_Data *wd;
795    Datetime_Field *field;
796    int idx;
797
798    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
799
800    ELM_SET_WIDTYPE(widtype, "datetime");
801    elm_widget_type_set(obj, widtype);
802    elm_widget_sub_object_add(parent, obj);
803    elm_widget_data_set(obj, wd);
804    elm_widget_del_hook_set(obj, _del_hook);
805    elm_widget_theme_hook_set(obj, _theme_hook);
806    elm_widget_translate_hook_set(obj, _translate_hook);
807    elm_widget_on_focus_hook_set(obj, _on_focus_hook, NULL);
808    elm_widget_disable_hook_set(obj, _disable_hook);
809    elm_widget_can_focus_set(obj, EINA_TRUE);
810    elm_widget_focus_next_hook_set(obj, _elm_datetime_focus_next_hook);
811
812    wd->base = edje_object_add(e);
813    elm_widget_resize_object_set(obj, wd->base);
814    _elm_theme_object_set(obj, wd->base, "datetime", "base", "default");
815    evas_object_smart_callbacks_descriptions_set(obj, _signals);
816
817    // module - initialise module for datetime
818    if (!dt_mod) dt_mod = _dt_mod_init();
819    if ((dt_mod) && (dt_mod->obj_hook))
820      wd->mod_data = dt_mod->obj_hook(obj);
821    // update module data
822    if (wd->mod_data)
823      {
824         wd->mod_data->base = obj;
825         wd->mod_data->field_limit_get = _field_limit_get;
826         wd->mod_data->field_format_get = _field_format_get;
827      }
828
829    _field_list_init(obj);
830    _reload_format(obj);
831
832    if ((dt_mod)&&(dt_mod->field_create))
833      {
834         for (idx = 0; idx < DATETIME_TYPE_COUNT; idx++)
835           {
836              field = wd->field_list + idx;
837              field->item_obj = dt_mod->field_create(wd->mod_data, idx);
838           }
839      }
840    _field_list_arrange(obj);
841    _mirrored_set(obj, elm_widget_mirrored_get(obj));
842
843    return obj;
844 }
845
846 EAPI const char *
847 elm_datetime_format_get(const Evas_Object *obj)
848 {
849    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
850    Widget_Data *wd;
851
852    wd = elm_widget_data_get(obj);
853    if (!wd) return NULL;
854
855    return wd->format;
856 }
857
858 EAPI void
859 elm_datetime_format_set(Evas_Object *obj, const char *fmt)
860 {
861    ELM_CHECK_WIDTYPE(obj, widtype);
862    Widget_Data *wd;
863
864    wd = elm_widget_data_get(obj);
865    if (!wd) return;
866
867    if (fmt)
868      {
869         strncpy(wd->format, fmt, MAX_FORMAT_LEN);
870         wd->user_format = EINA_TRUE;
871      }
872    else
873      wd->user_format = EINA_FALSE;
874
875    _reload_format(obj);
876 }
877
878 EAPI Eina_Bool
879 elm_datetime_field_visible_get(const Evas_Object *obj, Elm_Datetime_Field_Type
880                                fieldtype)
881 {
882    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
883    Widget_Data *wd;
884    Datetime_Field *field;
885
886    wd = elm_widget_data_get(obj);
887    if (!wd || (fieldtype > ELM_DATETIME_AMPM)) return EINA_FALSE;
888
889    field = wd->field_list + fieldtype;
890    return field->visible;
891 }
892
893 EAPI void
894 elm_datetime_field_visible_set(Evas_Object *obj, Elm_Datetime_Field_Type fieldtype,
895                               Eina_Bool visible)
896 {
897    ELM_CHECK_WIDTYPE(obj, widtype);
898    Widget_Data *wd;
899    Datetime_Field *field;
900
901    wd = elm_widget_data_get(obj);
902    if (!wd || (fieldtype > ELM_DATETIME_AMPM)) return;
903
904    field = wd->field_list + fieldtype;
905    if (field->visible == visible) return;
906
907    field->visible = visible;
908    _reload_format(obj);
909 }
910
911 EAPI void
912 elm_datetime_field_limit_get(const Evas_Object *obj, Elm_Datetime_Field_Type fieldtype,
913                              int *min, int *max)
914 {
915    ELM_CHECK_WIDTYPE(obj, widtype);
916    Widget_Data *wd;
917    Datetime_Field *field;
918
919    wd = elm_widget_data_get(obj);
920    if (!wd || (fieldtype >= ELM_DATETIME_AMPM)) return;
921
922    field = wd->field_list + fieldtype;
923    if (min) *min = field->min;
924    if (max) *max = field->max;
925 }
926
927 EAPI void
928 elm_datetime_field_limit_set(Evas_Object *obj, Elm_Datetime_Field_Type fieldtype,
929                              int min, int max)
930 {
931    ELM_CHECK_WIDTYPE(obj, widtype);
932    Widget_Data *wd;
933    Datetime_Field *field;
934
935    wd = elm_widget_data_get(obj);
936    if (!wd || (fieldtype >= ELM_DATETIME_AMPM)) return;
937
938    if (min > max) return;
939
940    field = wd->field_list + fieldtype;
941    if ((min > mapping[fieldtype].def_min && min < mapping[fieldtype].def_max)
942         || (field->type == ELM_DATETIME_YEAR))
943      field->min = min;
944    if ((max > mapping[fieldtype].def_min && max < mapping[fieldtype].def_max)
945         || (field->type == ELM_DATETIME_YEAR))
946      field->max = max;
947
948    _apply_field_limits(obj);
949 }
950
951 EAPI Eina_Bool
952 elm_datetime_value_get(const Evas_Object *obj, struct tm *currtime)
953 {
954    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
955    EINA_SAFETY_ON_NULL_RETURN_VAL(currtime, EINA_FALSE);
956    Widget_Data *wd;
957
958    wd = elm_widget_data_get(obj);
959    if (!wd) return EINA_FALSE;
960
961    *currtime = wd->curr_time;
962    return EINA_TRUE;
963 }
964
965 EAPI Eina_Bool
966 elm_datetime_value_set(Evas_Object *obj, const struct tm *newtime)
967 {
968    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
969    EINA_SAFETY_ON_NULL_RETURN_VAL(newtime, EINA_FALSE);
970    Widget_Data *wd;
971    struct tm old_time;
972
973    wd = elm_widget_data_get(obj);
974    if (!wd) return EINA_FALSE;
975
976    old_time = wd->curr_time;
977    wd->curr_time = *newtime;
978    // apply default field restrictions for curr_time
979    _apply_range_restrictions(obj, &wd->curr_time);
980    // validate the curr_time according to the min_limt and max_limt
981    _validate_datetime_limits(&wd->curr_time, &wd->min_limit, EINA_FALSE);
982    _validate_datetime_limits(&wd->max_limit, &wd->curr_time, EINA_TRUE);
983    _apply_field_limits(obj);
984
985    if (!_date_cmp(&old_time, &wd->curr_time))
986      evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
987
988    return EINA_TRUE;
989 }
990
991 EAPI Eina_Bool
992 elm_datetime_value_min_get(const Evas_Object *obj, struct tm *mintime)
993 {
994    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
995    EINA_SAFETY_ON_NULL_RETURN_VAL(mintime, EINA_FALSE);
996    Widget_Data *wd;
997
998    wd = elm_widget_data_get(obj);
999    if (!wd) return EINA_FALSE;
1000
1001    *mintime = wd->min_limit;
1002    return EINA_TRUE;
1003 }
1004
1005 EAPI Eina_Bool
1006 elm_datetime_value_min_set(Evas_Object *obj, const struct tm *mintime)
1007 {
1008    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1009    EINA_SAFETY_ON_NULL_RETURN_VAL(mintime, EINA_FALSE);
1010    Widget_Data *wd;
1011    struct tm old_time;
1012
1013    wd = elm_widget_data_get(obj);
1014    if (!wd) return EINA_FALSE;
1015
1016    wd->min_limit = *mintime;
1017    old_time = wd->curr_time;
1018    // apply default field restrictions for min_limit
1019    _apply_range_restrictions(obj, &wd->min_limit);
1020    // validate curr_time and max_limt according to the min_limit
1021    _validate_datetime_limits(&wd->max_limit, &wd->min_limit, EINA_FALSE);
1022    _validate_datetime_limits(&wd->curr_time, &wd->min_limit, EINA_FALSE);
1023    _apply_field_limits(obj);
1024
1025    if (!_date_cmp(&old_time, &wd->curr_time))
1026      evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
1027
1028    return EINA_TRUE;
1029 }
1030
1031 EAPI Eina_Bool
1032 elm_datetime_value_max_get(const Evas_Object *obj, struct tm *maxtime)
1033 {
1034    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1035    EINA_SAFETY_ON_NULL_RETURN_VAL(maxtime, EINA_FALSE);
1036    Widget_Data *wd;
1037
1038    wd = elm_widget_data_get(obj);
1039    if (!wd) return EINA_FALSE;
1040
1041    *maxtime = wd->max_limit;
1042    return EINA_TRUE;
1043 }
1044
1045 EAPI Eina_Bool
1046 elm_datetime_value_max_set(Evas_Object *obj, const struct tm *maxtime)
1047 {
1048    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1049    EINA_SAFETY_ON_NULL_RETURN_VAL(maxtime, EINA_FALSE);
1050    Widget_Data *wd;
1051    struct tm old_time;
1052
1053    wd = elm_widget_data_get(obj);
1054    if (!wd) return EINA_FALSE;
1055
1056    wd->max_limit = *maxtime;
1057    old_time = wd->curr_time;
1058    // apply default field restrictions for max_limit
1059    _apply_range_restrictions(obj, &wd->max_limit);
1060    // validate curr_time and min_limt according to the max_limit
1061    _validate_datetime_limits(&wd->max_limit, &wd->min_limit, EINA_TRUE);
1062    _validate_datetime_limits(&wd->max_limit, &wd->curr_time, EINA_TRUE);
1063    _apply_field_limits(obj);
1064
1065    if (!_date_cmp(&old_time, &wd->curr_time))
1066      evas_object_smart_callback_call(obj, SIG_CHANGED, NULL);
1067
1068    return EINA_TRUE;
1069 }