8d2e30769ae6eccd86f4279145ba8c0d1c54a131
[framework/web/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / tizen / InputPicker.cpp
1 /*
2  * Copyright (C) 2012 Samsung Electronics
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #include "config.h"
21 #include "InputPicker.h"
22
23 #include "ewk_view.h"
24
25 #include <Elementary.h>
26
27 #include <stdlib.h>
28 #include <string.h>
29 #include <vconf/vconf.h>
30
31 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
32 #include <dlfcn.h>
33 #include <efl_assist.h>
34 extern void* EflAssistHandle;
35 #endif
36
37 #if OS(TIZEN) && ENABLE(TIZEN_INPUT_TAG_EXTENSION)
38
39 static const unsigned maxDatetimeLength = 32;
40 static const unsigned maxDateStringLength = 10;
41 static const char defaultDatetimeFormat[] = "%Y/%m/%d %H:%M";
42
43 struct Input_Date {
44     int year;
45     int mon;
46     int day;
47     int hour;
48     int min;
49     int sec;
50 };
51
52 struct Input_Date_Str {
53     char year[maxDateStringLength];
54     char mon[maxDateStringLength];
55     char day[maxDateStringLength];
56     char hour[maxDateStringLength];
57     char min[maxDateStringLength];
58     char sec[maxDateStringLength];
59 };
60
61 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
62 static inline void _picker_back_cb(void* data, Evas_Object* obj, void* event_info)
63 {
64     elm_object_signal_emit(obj, "picker,hide", "");
65 }
66
67 static void _edit_start_cb(void* data, Evas_Object* obj, void* event_info)
68 {
69     if (!EflAssistHandle)
70         return;
71
72     void (*webkit_ea_object_event_callback_add)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *);
73     webkit_ea_object_event_callback_add = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *))dlsym(EflAssistHandle, "ea_object_event_callback_add");
74     (*webkit_ea_object_event_callback_add)(obj, EA_CALLBACK_BACK, _picker_back_cb, 0);
75 }
76
77 static void _edit_end_cb(void* data, Evas_Object* obj, void* event_info)
78 {
79     // Set focus on 1st popup.
80     Input_Picker_Layout* pickerLayout = (Input_Picker_Layout*)data;
81     elm_object_focus_set(pickerLayout->popup, true);
82
83     if (!EflAssistHandle)
84         return;
85
86     void (*webkit_ea_object_event_callback_del)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func);
87     webkit_ea_object_event_callback_del = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func))dlsym(EflAssistHandle, "ea_object_event_callback_del");
88     (*webkit_ea_object_event_callback_del)(obj, EA_CALLBACK_BACK, _picker_back_cb);
89 }
90 #endif // ENABLE(TIZEN_HW_MORE_BACK_KEY)
91
92 static char* datetimeFormat()
93 {
94     char* datetimeFormat = NULL;
95     char* regionFormat = NULL;
96     char* language = NULL;
97     char buf[256] = { 0, };
98     int timeValue = 0;
99     int isHour24 = 0;
100     int returnValue = 0;
101
102     language = getenv("LANGUAGE");
103     setenv("LANGUAGE", "en_US", 1);
104
105     regionFormat = vconf_get_str(VCONFKEY_REGIONFORMAT);
106     if (!regionFormat)
107         return 0;
108
109     returnValue = vconf_get_int(VCONFKEY_REGIONFORMAT_TIME1224, &timeValue);
110     if (returnValue < 0)
111         isHour24 = 0;
112     else if(timeValue == VCONFKEY_TIME_FORMAT_12 || timeValue == VCONFKEY_TIME_FORMAT_24)
113         isHour24 = timeValue - 1;
114
115     if (isHour24)
116         snprintf(buf, sizeof(buf), "%s_DTFMT_24HR", regionFormat);
117     else
118         snprintf(buf, sizeof(buf), "%s_DTFMT_12HR", regionFormat);
119
120     free(regionFormat);
121
122     datetimeFormat = dgettext("dt_fmt", buf);
123
124     if(!language || !strcmp(language, ""))
125         unsetenv("LANGUAGE");
126     else
127         setenv("LANGUAGE", language, 1);
128
129     return strdup(datetimeFormat);
130 }
131
132 Input_Picker_Layout::Input_Picker_Layout(Evas_Object* ewkView)
133     : m_ewkView(ewkView)
134     , conformant(0)
135     , popup(0)
136     , layout(0)
137     , datePicker(0)
138     , colorRect(0)
139     , okButton(0)
140     , dataListEditField(0)
141 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
142     , initial_r(0)
143     , initial_g(0)
144     , initial_b(0)
145 #endif
146     , datetimeLocal(false)
147 {
148     evas_object_focus_set(m_ewkView, false);
149
150     /* FIXME : Workaround. OSP requirement.
151        OSP want to block own touch event while webkit internal picker is running. */
152     evas_object_smart_callback_call(m_ewkView, "input,picker,show", 0);
153 }
154
155 Input_Picker_Layout::~Input_Picker_Layout()
156 {
157     /* FIXME : Workaround. OSP requirement.
158        OSP want to block own touch event while webkit internal picker is running. */
159     evas_object_smart_callback_call(m_ewkView, "input,picker,hide", 0);
160 }
161
162 namespace WebKit {
163
164 InputPicker::InputPicker(Evas_Object* ewkView)
165     : m_ewkView(ewkView)
166     , m_pickerLayout(0)
167     , m_dataList(0)
168 #if ENABLE(TIZEN_INPUT_COLOR_PICKER)
169     , m_isColorPickerShown(false)
170 #endif
171     , m_color1(0)
172     , m_color2(0)
173     , m_color3(0)
174     , m_color4(0)
175     , m_color5(0)
176     , m_color6(0)
177     , m_color7(0)
178     , m_color8(0)
179     , m_color9(0)
180     , m_color10(0)
181     , m_color11(0)
182     , m_color12(0)
183     , m_color13(0)
184     , m_color14(0)
185 {
186 }
187
188 InputPicker::~InputPicker()
189 {
190     if (m_pickerLayout) {
191         if (m_pickerLayout->popup)
192             evas_object_del(m_pickerLayout->popup);
193         delete m_pickerLayout;
194     }
195 }
196
197 void InputPicker::show(Ewk_Input_Type inputType, const char* inputValue)
198 {
199     TIZEN_SECURE_LOGI("input value: %s", inputValue);
200
201     if(inputType == EWK_INPUT_TYPE_DATE)
202         ewk_date_popup(inputValue);
203     else if(inputType == EWK_INPUT_TYPE_TIME)
204         ewk_time_popup(inputValue);
205     else if(inputType == EWK_INPUT_TYPE_DATETIME)
206         ewk_datetime_popup(inputValue, false);
207     else if(inputType == EWK_INPUT_TYPE_DATETIMELOCAL)
208         ewk_datetime_popup(inputValue, true);
209     else if(inputType == EWK_INPUT_TYPE_MONTH)
210         ewk_month_popup(inputValue);
211     else if(inputType == EWK_INPUT_TYPE_WEEK)
212         ewk_week_popup(inputValue);
213 }
214
215 #if ENABLE(TIZEN_DATALIST_ELEMENT)
216 void InputPicker::_data_list_popup_response_cb(void* data,  Evas_Object* obj, void* eventInfo)
217 {
218     InputPicker* inputPicker = static_cast<InputPicker*>(data);
219     const char* selectedItem = elm_entry_entry_get(inputPicker->m_pickerLayout->dataListEditField);
220     ewk_view_data_list_close(inputPicker->m_ewkView, selectedItem);
221
222     if (inputPicker->m_pickerLayout->popup) {
223         evas_object_del(inputPicker->m_pickerLayout->popup);
224         inputPicker->m_pickerLayout->popup = 0;
225     }
226
227     delete inputPicker->m_pickerLayout;
228     inputPicker->m_pickerLayout = 0;
229
230     inputPicker->m_dataList = 0;
231 }
232
233 void InputPicker::_data_list_popup_response_cancel_cb(void* data,  Evas_Object* obj, void* eventInfo)
234 {
235     InputPicker* inputPicker = static_cast<InputPicker*>(data);
236
237     if (inputPicker->m_pickerLayout->popup) {
238         evas_object_del(inputPicker->m_pickerLayout->popup);
239         inputPicker->m_pickerLayout->popup = 0;
240     }
241
242     delete inputPicker->m_pickerLayout;
243     inputPicker->m_pickerLayout = 0;
244
245     inputPicker->m_dataList = 0;
246 }
247
248 void InputPicker::_data_list_selected_cb(void* data, Evas_Object* obj, void* eventInfo)
249 {
250     InputPicker* inputPicker = static_cast<InputPicker*>(data);
251     Elm_Object_Item* selectedItem = static_cast<Elm_Object_Item*>(eventInfo);
252     Elm_Object_Item* item = elm_list_first_item_get(obj);
253
254     int index = 0;
255     while (item) {
256         if (item == selectedItem)
257             break;
258         index++;
259         item = elm_list_item_next(item);
260     }
261
262     Eina_List* list = 0;
263     void* listItem = 0;
264     int listIndex = 0;
265     EINA_LIST_FOREACH(inputPicker->m_dataList, list, listItem) {
266         if (listItem && index == listIndex)
267             elm_entry_entry_set(inputPicker->m_pickerLayout->dataListEditField, static_cast<char*>(listItem));
268
269         listIndex++;
270     }
271 }
272
273 static void _eraser_clicked_cb(void* data, Evas_Object* obj, const char* emission, const char* source)
274 {
275     Evas_Object* entry = static_cast<Evas_Object*>(data);
276     elm_entry_entry_set(entry, "");
277 }
278
279 static void _changed_cb(void* data, Evas_Object* obj, void* eventInfo)
280 {
281     Evas_Object* layout = static_cast<Evas_Object*>(data);
282
283     if (elm_object_focus_get(layout)) {
284         if (elm_entry_is_empty(obj))
285             elm_object_signal_emit(layout, "elm,state,eraser,hide", "elm");
286         else
287             elm_object_signal_emit(layout, "elm,state,eraser,show", "elm");
288     }
289 }
290
291 static void _focused_cb(void* data, Evas_Object* obj, void* eventInfo)
292 {
293     Evas_Object* layout = static_cast<Evas_Object*>(data);
294
295     if (!elm_entry_is_empty(obj))
296         elm_object_signal_emit(layout, "elm,state,eraser,show", "elm");
297 }
298
299 static void _unfocused_cb(void* data, Evas_Object* obj, void* eventInfo)
300 {
301     Evas_Object* layout = static_cast<Evas_Object*>(data);
302     elm_object_signal_emit(layout, "elm,state,eraser,hide", "elm");
303 }
304
305 void InputPicker::showDataList(Ewk_Input_Type inputType, Eina_List* optionList)
306 {
307     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
308     Evas_Object* win = parent;
309
310     m_dataList = optionList;
311
312     if (m_pickerLayout)
313         return;
314
315     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
316
317     /* FIXME: If the entry is in the popup, the parent should be naviframe for uifw limitation. */
318     Evas_Object* naviframe = elm_naviframe_add(m_ewkView);
319     while (parent) {
320         const char* type = elm_object_widget_type_get(parent);
321         if (type) {
322             if (!strcmp(type, elm_object_widget_type_get(naviframe))) {
323                 win =  parent;
324                 break;
325             }
326         }
327         parent = elm_object_parent_widget_get(parent);
328     }
329
330     evas_object_del(naviframe);
331
332     if (!win)
333         win = m_ewkView;
334
335     m_pickerLayout->popup = elm_popup_add(win);
336     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select data list");
337
338 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
339     if (EflAssistHandle) {
340         void (*webkit_ea_object_event_callback_add)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *);
341         webkit_ea_object_event_callback_add = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *))dlsym(EflAssistHandle, "ea_object_event_callback_add");
342         (*webkit_ea_object_event_callback_add)(m_pickerLayout->popup, EA_CALLBACK_BACK, _data_list_popup_response_cancel_cb, this);
343     }
344 #endif
345
346     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
347     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","data_list_picker");
348     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
349     evas_object_show(m_pickerLayout->layout);
350     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
351
352     Evas_Object* elmList = elm_list_add(m_pickerLayout->layout);
353     Eina_List* list = 0;
354     void* listItem = 0;
355     EINA_LIST_FOREACH(optionList, list, listItem) {
356         if (listItem)
357             elm_list_item_append(elmList, static_cast<char*>(listItem), 0, 0, _data_list_selected_cb, this);
358     }
359     evas_object_size_hint_weight_set(elmList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
360     evas_object_show(elmList);
361     elm_object_part_content_set(m_pickerLayout->layout, "data_list", elmList);
362
363     Evas_Object* editFieldLayout = elm_layout_add(m_pickerLayout->layout);
364     elm_layout_theme_set(editFieldLayout, "layout", "editfield", "default");
365
366     m_pickerLayout->dataListEditField = elm_entry_add(editFieldLayout);
367     elm_object_part_content_set(editFieldLayout, "elm.swallow.content", m_pickerLayout->dataListEditField);
368
369     elm_object_signal_emit(editFieldLayout, "elm,state,guidetext,hide", "elm");
370     elm_object_signal_callback_add(editFieldLayout, "elm,eraser,clicked", "elm", _eraser_clicked_cb, m_pickerLayout->dataListEditField);
371     evas_object_smart_callback_add(m_pickerLayout->dataListEditField, "changed", _changed_cb, editFieldLayout);
372     evas_object_smart_callback_add(m_pickerLayout->dataListEditField, "focused", _focused_cb, editFieldLayout);
373     evas_object_smart_callback_add(m_pickerLayout->dataListEditField, "unfocused", _unfocused_cb, editFieldLayout);
374
375     elm_object_part_content_set(m_pickerLayout->layout, "selected_item", editFieldLayout);
376     evas_object_show(editFieldLayout);
377
378     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
379     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
380     elm_object_text_set(m_pickerLayout->okButton, "OK");
381     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
382     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _data_list_popup_response_cb, this);
383     evas_object_show(m_pickerLayout->popup);
384     evas_object_focus_set(m_pickerLayout->okButton, true);
385
386     Evas_Object* cancelButton = elm_button_add(m_pickerLayout->popup);
387     elm_object_style_set(cancelButton, "popup_button/default");
388     elm_object_text_set(cancelButton, "Cancel");
389     elm_object_part_content_set(m_pickerLayout->popup, "button2", cancelButton);
390     evas_object_smart_callback_add(cancelButton, "clicked", _data_list_popup_response_cancel_cb, this);
391     evas_object_show(cancelButton);
392 }
393
394 void InputPicker::hideDataList(Ewk_Input_Type inputType)
395 {
396     if (!m_dataList)
397         return;
398
399     if(m_pickerLayout->popup){
400         evas_object_del(m_pickerLayout->popup);
401         m_pickerLayout->popup = 0;
402     }
403
404     delete m_pickerLayout;
405     m_pickerLayout = 0;
406
407     m_dataList = 0;
408 }
409 #endif
410
411 #if ENABLE(TIZEN_INPUT_COLOR_PICKER)
412 void InputPicker::showColorPicker(int r, int g, int b, int)
413 {
414     m_isColorPickerShown = true;
415     ewk_color_popup(r, g, b);
416 }
417
418 void InputPicker::hideColorPicker()
419 {
420     m_isColorPickerShown = false;
421     if (!m_pickerLayout)
422         return;
423
424     if (m_pickerLayout->popup) {
425         evas_object_del(m_pickerLayout->popup);
426         m_pickerLayout->popup = 0;
427     }
428
429     delete m_pickerLayout;
430     m_pickerLayout = 0;
431 }
432 #endif
433
434 #if ENABLE(ELM_COLORPALLETE)
435 static void _color_palette_changed_cb(void* data, Evas_Object* obj, void* eventInfo)
436 {
437     int r = 0;
438     int g = 0;
439     int b = 0;
440     int a = 0;
441     Elm_Object_Item *color_it = static_cast<Elm_Object_Item*>(eventInfo);
442     elm_colorselector_palette_item_color_get(color_it, &r, &g, &b, &a);
443     evas_object_color_set(static_cast<Evas_Object*>(data), r, g, b , a);
444 }
445 #endif
446
447 void InputPicker::_color_selected_cb(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
448 {
449     int r = 0;
450     int g = 0;
451     int b = 0;
452     int a = 0;
453     evas_object_color_get(obj, &r, &g, &b, &a);
454
455     evas_object_color_set(static_cast<Evas_Object*>(data), r, g, b, a);
456 }
457
458 void InputPicker::ewk_color_popup(int r, int g, int b)
459 {
460     if (m_pickerLayout)
461         return;
462
463     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
464
465     Evas_Object* parent = parentWindow();
466     m_pickerLayout->popup = elm_popup_add(parent);
467     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select color");
468
469 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
470     m_pickerLayout->initial_r = r;
471     m_pickerLayout->initial_g = g;
472     m_pickerLayout->initial_b = b;
473     if (EflAssistHandle) {
474         void (*webkit_ea_object_event_callback_add)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *);
475         webkit_ea_object_event_callback_add = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *))dlsym(EflAssistHandle, "ea_object_event_callback_add");
476         (*webkit_ea_object_event_callback_add)(m_pickerLayout->popup, EA_CALLBACK_BACK, _color_back_cb, this);
477     }
478 #endif
479
480     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
481     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","color_picker");
482     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
483     evas_object_show(m_pickerLayout->layout);
484     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
485
486     m_pickerLayout->colorRect = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
487     evas_object_size_hint_weight_set(m_pickerLayout->colorRect, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
488     evas_object_show(m_pickerLayout->colorRect);
489
490     evas_object_color_set(m_pickerLayout->colorRect, r, g, b, 255);
491     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color_rect", m_pickerLayout->colorRect);
492 #if ENABLE(ELM_COLORPALLETE)
493     Evas_Object* colorPalette = elm_colorselector_add(m_pickerLayout->popup);
494     elm_colorselector_mode_set(colorPalette, ELM_COLORSELECTOR_PALETTE);
495
496     elm_colorselector_palette_color_add(colorPalette, 128, 0, 0, 255);
497     elm_colorselector_palette_color_add(colorPalette, 255, 0, 128, 255);
498     elm_colorselector_palette_color_add(colorPalette, 255, 0, 0, 255);
499     elm_colorselector_palette_color_add(colorPalette, 255, 127, 39, 255);
500     elm_colorselector_palette_color_add(colorPalette, 255, 255, 0, 255);
501     elm_colorselector_palette_color_add(colorPalette, 0, 255, 0, 255);
502     elm_colorselector_palette_color_add(colorPalette, 0, 255, 255, 255);
503     elm_colorselector_palette_color_add(colorPalette, 0, 0, 255, 255);
504     elm_colorselector_palette_color_add(colorPalette, 0, 0, 128, 255);
505     elm_colorselector_palette_color_add(colorPalette, 64, 0, 64, 255);
506     elm_colorselector_palette_color_add(colorPalette, 0, 0, 0, 255);
507     elm_colorselector_palette_color_add(colorPalette, 255, 255, 255, 255);
508
509     evas_object_size_hint_fill_set(colorPalette, EVAS_HINT_FILL, 0);
510     evas_object_size_hint_weight_set(colorPalette, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
511     evas_object_smart_callback_add(colorPalette, "color,item,selected", _color_palette_changed_cb, m_pickerLayout->colorRect);
512     evas_object_show(colorPalette);
513
514     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color_palette", colorPalette);
515 #else
516     Evas_Object* accessObject = 0;
517     Evas_Object* color1 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
518     evas_object_size_hint_weight_set(color1, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
519     evas_object_color_set(color1, 128, 0, 0, 255);
520     evas_object_event_callback_add(color1, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
521     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color1", color1);
522     evas_object_show(color1);
523
524     m_color1 = color1;
525     accessObject = elm_access_object_register(color1, m_pickerLayout->layout);
526     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
527     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color1KeyDownCallback, this);
528
529     Evas_Object* color2 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
530     evas_object_size_hint_weight_set(color2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
531     evas_object_color_set(color2, 255, 0, 128, 255);
532     evas_object_event_callback_add(color2, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
533     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color2", color2);
534     evas_object_show(color2);
535
536     m_color2 = color2;
537     accessObject = elm_access_object_register(color2, m_pickerLayout->layout);
538     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
539     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color2KeyDownCallback, this);
540
541     Evas_Object* color3 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
542     evas_object_size_hint_weight_set(color3, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
543     evas_object_color_set(color3, 255, 0, 0, 255);
544     evas_object_event_callback_add(color3, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
545     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color3", color3);
546     evas_object_show(color3);
547
548     m_color3 = color3;
549     accessObject = elm_access_object_register(color3, m_pickerLayout->layout);
550     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
551     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color3KeyDownCallback, this);
552
553     Evas_Object* color4 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
554     evas_object_size_hint_weight_set(color4, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
555     evas_object_color_set(color4, 255, 127, 39, 255);
556     evas_object_event_callback_add(color4, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
557     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color4", color4);
558     evas_object_show(color4);
559
560     m_color4 = color4;
561     accessObject = elm_access_object_register(color4, m_pickerLayout->layout);
562     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
563     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color4KeyDownCallback, this);
564
565     Evas_Object* color5 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
566     evas_object_size_hint_weight_set(color5, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
567     evas_object_color_set(color5, 255, 255, 0, 255);
568     evas_object_event_callback_add(color5, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
569     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color5", color5);
570     evas_object_show(color5);
571
572     m_color5 = color5;
573     accessObject = elm_access_object_register(color5, m_pickerLayout->layout);
574     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
575     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color5KeyDownCallback, this);
576
577     Evas_Object* color6 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
578     evas_object_size_hint_weight_set(color6, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
579     evas_object_color_set(color6, 0, 255, 0, 255);
580     evas_object_event_callback_add(color6, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
581     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color6", color6);
582     evas_object_show(color6);
583
584     m_color6 = color6;
585     accessObject = elm_access_object_register(color6, m_pickerLayout->layout);
586     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
587     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color6KeyDownCallback, this);
588
589     Evas_Object* color7 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
590     evas_object_size_hint_weight_set(color7, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
591     evas_object_color_set(color7, 0, 255, 255, 255);
592     evas_object_event_callback_add(color7, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
593     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color7", color7);
594     evas_object_show(color7);
595
596     m_color7 = color7;
597     accessObject = elm_access_object_register(color7, m_pickerLayout->layout);
598     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
599     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color7KeyDownCallback, this);
600
601     Evas_Object* color8 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
602     evas_object_size_hint_weight_set(color8, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
603     evas_object_color_set(color8, 0, 0, 255, 255);
604     evas_object_event_callback_add(color8, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
605     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color8", color8);
606     evas_object_show(color8);
607
608     m_color8 = color8;
609     accessObject = elm_access_object_register(color8, m_pickerLayout->layout);
610     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
611     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color8KeyDownCallback, this);
612
613     Evas_Object* color9 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
614     evas_object_size_hint_weight_set(color9, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
615     evas_object_color_set(color9, 0, 0, 128, 255);
616     evas_object_event_callback_add(color9, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
617     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color9", color9);
618     evas_object_show(color9);
619
620     m_color9 = color9;
621     accessObject = elm_access_object_register(color9, m_pickerLayout->layout);
622     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
623     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color9KeyDownCallback, this);
624
625     Evas_Object* color10 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
626     evas_object_size_hint_weight_set(color10, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
627     evas_object_color_set(color10, 64, 0, 64, 255);
628     evas_object_event_callback_add(color10, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
629     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color10", color10);
630     evas_object_show(color10);
631
632     m_color10 = color10;
633     accessObject = elm_access_object_register(color10, m_pickerLayout->layout);
634     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
635     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color10KeyDownCallback, this);
636
637     Evas_Object* color11 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
638     evas_object_size_hint_weight_set(color11, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
639     evas_object_color_set(color11, 153, 217, 234, 255);
640     evas_object_event_callback_add(color11, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
641     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color11", color11);
642     evas_object_show(color11);
643
644     m_color11 = color11;
645     accessObject = elm_access_object_register(color11, m_pickerLayout->layout);
646     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
647     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color11KeyDownCallback, this);
648
649     Evas_Object* color12 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
650     evas_object_size_hint_weight_set(color12, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
651     evas_object_color_set(color12, 128, 128, 128, 255);
652     evas_object_event_callback_add(color12, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
653     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color12", color12);
654     evas_object_show(color12);
655
656     m_color12 = color12;
657     accessObject = elm_access_object_register(color12, m_pickerLayout->layout);
658     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
659     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color12KeyDownCallback, this);
660
661     Evas_Object* color13 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
662     evas_object_size_hint_weight_set(color13, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
663     evas_object_color_set(color13, 0, 0, 0, 255);
664     evas_object_event_callback_add(color13, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
665     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color13", color13);
666     evas_object_show(color13);
667
668     m_color13 = color13;
669     accessObject = elm_access_object_register(color13, m_pickerLayout->layout);
670     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
671     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color13KeyDownCallback, this);
672
673     Evas_Object* color14 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
674     evas_object_size_hint_weight_set(color14, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
675     evas_object_color_set(color14, 255, 255, 255, 255);
676     evas_object_event_callback_add(color14, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
677     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color14", color14);
678     evas_object_show(color14);
679
680     m_color14 = color14;
681     accessObject = elm_access_object_register(color14, m_pickerLayout->layout);
682     elm_object_focus_custom_chain_append(m_pickerLayout->layout, accessObject, NULL);
683     evas_object_event_callback_add(accessObject, EVAS_CALLBACK_KEY_DOWN, color14KeyDownCallback, this);
684 #endif
685     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
686     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
687     elm_object_text_set(m_pickerLayout->okButton, "OK");
688     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
689     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _color_popup_response_cb, this);
690
691     evas_object_show(m_pickerLayout->popup);
692 }
693
694 void InputPicker::_color_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
695 {
696     InputPicker* inputPicker = static_cast<InputPicker*>(data);
697
698     int r = 0;
699     int g = 0;
700     int b = 0;
701     int a = 0;
702     evas_object_color_get(inputPicker->m_pickerLayout->colorRect, &r, &g, &b, &a);
703
704     ewk_view_color_picker_color_set(inputPicker->m_ewkView, r, g, b, a);
705
706     inputPicker->hideColorPicker();
707 }
708
709 void InputPicker::color1KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
710 {
711     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
712     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
713         InputPicker* inputPicker = static_cast<InputPicker*>(data);
714         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color1, 0);
715     }
716 }
717
718 void InputPicker::color2KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
719 {
720     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
721     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
722         InputPicker* inputPicker = static_cast<InputPicker*>(data);
723         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color2, 0);
724     }
725 }
726
727 void InputPicker::color3KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
728 {
729     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
730     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
731         InputPicker* inputPicker = static_cast<InputPicker*>(data);
732         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color3, 0);
733     }
734 }
735
736 void InputPicker::color4KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
737 {
738     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
739     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
740         InputPicker* inputPicker = static_cast<InputPicker*>(data);
741         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color4, 0);
742     }
743 }
744
745 void InputPicker::color5KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
746 {
747     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
748     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
749         InputPicker* inputPicker = static_cast<InputPicker*>(data);
750         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color5, 0);
751     }
752 }
753
754 void InputPicker::color6KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
755 {
756     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
757     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
758         InputPicker* inputPicker = static_cast<InputPicker*>(data);
759         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color6, 0);
760     }
761 }
762
763 void InputPicker::color7KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
764 {
765     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
766     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
767         InputPicker* inputPicker = static_cast<InputPicker*>(data);
768         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color7, 0);
769     }
770 }
771
772 void InputPicker::color8KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
773 {
774     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
775     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
776         InputPicker* inputPicker = static_cast<InputPicker*>(data);
777         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color8, 0);
778     }
779 }
780
781 void InputPicker::color9KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
782 {
783     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
784     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
785         InputPicker* inputPicker = static_cast<InputPicker*>(data);
786         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color9, 0);
787     }
788 }
789
790 void InputPicker::color10KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
791 {
792     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
793     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
794         InputPicker* inputPicker = static_cast<InputPicker*>(data);
795         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color10, 0);
796     }
797 }
798
799 void InputPicker::color11KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
800 {
801     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
802     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
803         InputPicker* inputPicker = static_cast<InputPicker*>(data);
804         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color11, 0);
805     }
806 }
807
808 void InputPicker::color12KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
809 {
810     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
811     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
812         InputPicker* inputPicker = static_cast<InputPicker*>(data);
813         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color12, 0);
814     }
815 }
816
817 void InputPicker::color13KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
818 {
819     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
820     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
821         InputPicker* inputPicker = static_cast<InputPicker*>(data);
822         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color13, 0);
823     }
824 }
825
826 void InputPicker::color14KeyDownCallback(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
827 {
828     Evas_Event_Key_Down *event = (Evas_Event_Key_Down *)eventInfo;
829     if (!strncmp("Return", event->keyname, 6) || !strncmp("space", event->keyname, 5)) {
830         InputPicker* inputPicker = static_cast<InputPicker*>(data);
831         inputPicker->_color_selected_cb(inputPicker->m_pickerLayout->colorRect, 0, inputPicker->m_color14, 0);
832     }
833 }
834
835
836 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
837 void InputPicker::_color_back_cb(void* data,  Evas_Object* obj, void* event_info)
838 {
839     InputPicker* inputPicker = static_cast<InputPicker*>(data);
840
841     int r = inputPicker->m_pickerLayout->initial_r;
842     int g = inputPicker->m_pickerLayout->initial_g;
843     int b = inputPicker->m_pickerLayout->initial_b;
844     int a = 255;
845     ewk_view_color_picker_color_set(inputPicker->m_ewkView, r, g, b, a);
846
847     inputPicker->hideColorPicker();
848 }
849 #endif
850
851 void InputPicker::ewk_date_popup(const char* inputValue)
852 {
853     struct tm* currentTime;
854     time_t cur_time;
855     time(&cur_time);
856     currentTime = localtime(&cur_time);
857
858     Input_Date_Str dateStr;
859     memset(&dateStr, 0, sizeof(Input_Date_Str));
860
861     if (inputValue && strlen(inputValue)) {
862         char tmpinputValue[maxDatetimeLength] = { 0, };
863
864         snprintf(tmpinputValue, maxDatetimeLength, "%s", inputValue);
865         char* token = strtok(tmpinputValue,"-");
866         if (token)
867             strncpy(dateStr.year, token, maxDateStringLength);
868         token = strtok(0, "-");
869         if (token)
870             strncpy(dateStr.mon, token, maxDateStringLength);
871         token = strtok(0, "-");
872         if (token)
873             strncpy(dateStr.day, token, maxDateStringLength);
874
875         if (dateStr.year)
876             currentTime->tm_year = atoi(dateStr.year);
877         if (dateStr.mon)
878             currentTime->tm_mon = atoi(dateStr.mon);
879         if (dateStr.day)
880             currentTime->tm_mday = atoi(dateStr.day);
881
882         currentTime->tm_year = currentTime->tm_year - 1900;
883         currentTime->tm_mon = currentTime->tm_mon - 1;
884     }
885
886     if (m_pickerLayout) {
887         // Just update the value.
888         elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
889         evas_object_focus_set(m_pickerLayout->okButton, true);
890         return;
891     }
892
893     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
894
895     createDatetimePopup("Select date", currentTime);
896     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_HOUR, EINA_FALSE);
897     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_MINUTE, EINA_FALSE);
898     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_AMPM, EINA_FALSE);
899
900     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
901     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
902     elm_object_text_set(m_pickerLayout->okButton, "OK");
903     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
904     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _date_popup_response_cb, this);
905     evas_object_focus_set(m_pickerLayout->okButton, true);
906
907     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
908     evas_object_show(m_pickerLayout->popup);
909 }
910
911 #define isLeapYear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
912 static int _calculateMonthAndDay(int year, int totalDays, int* remains)
913 {
914     // Jan.
915     if (totalDays - 31 < 0) {
916         *remains = totalDays;
917         return 0;
918     }
919
920     totalDays = totalDays - 31;
921
922     // Feb.
923     if (isLeapYear(year)) {
924         if (totalDays - 29 < 0) {
925             *remains = totalDays;
926             return 1;
927         }
928         totalDays = totalDays - 29;
929     } else {
930         if (totalDays - 28 < 0) {
931             *remains = totalDays;
932             return 1;
933         }
934         totalDays = totalDays - 28;
935     }
936
937     // Mar.
938     if (totalDays - 31 < 0) {
939         *remains = totalDays;
940         return 2;
941     }
942     totalDays = totalDays - 31;
943
944     // Apr.
945     if (totalDays - 30 < 0) {
946         *remains = totalDays;
947         return 3;
948     }
949     totalDays = totalDays - 30;
950
951     // May
952     if (totalDays - 31 < 0) {
953         *remains = totalDays;
954         return 4;
955     }
956     totalDays = totalDays - 31;
957
958     // Jun.
959     if (totalDays - 30 < 0) {
960         *remains = totalDays;
961         return 5;
962     }
963     totalDays = totalDays - 30;
964
965     // Jul.
966     if (totalDays - 31 < 0) {
967         *remains = totalDays;
968         return 6;
969     }
970     totalDays = totalDays - 31;
971
972     // Aug.
973     if (totalDays - 31 < 0) {
974         *remains = totalDays;
975         return 7;
976     }
977     totalDays = totalDays - 31;
978
979     // Sept.
980     if (totalDays - 30 < 0) {
981         *remains = totalDays;
982         return 8;
983     }
984     totalDays = totalDays - 30;
985
986     // Oct.
987     if (totalDays - 31 < 0) {
988         *remains = totalDays;
989         return 9;
990     }
991     totalDays = totalDays - 31;
992
993     // Nov.
994     if (totalDays - 30 < 0) {
995         *remains = totalDays;
996         return 10;
997     }
998     totalDays = totalDays - 30;
999
1000     *remains = totalDays;
1001     return 11;
1002 }
1003
1004 void InputPicker::ewk_week_popup(const char* inputValue)
1005 {
1006     struct tm* currentTime;
1007     time_t cur_time;
1008     time(&cur_time);
1009     currentTime = localtime(&cur_time);
1010
1011     Input_Date_Str dateStr;
1012     memset(&dateStr, 0, sizeof(Input_Date_Str));
1013
1014     if (inputValue && strlen(inputValue)) {
1015         char tmpinputValue[maxDatetimeLength] = { 0, };
1016
1017         snprintf(tmpinputValue, maxDatetimeLength, "%s", inputValue);
1018         char* token = strtok(tmpinputValue,"-");
1019         if (token)
1020             strncpy(dateStr.year, token, maxDateStringLength);
1021         const char* week = strstr(inputValue, "W");
1022         int weekNum = 1;
1023         if (week)
1024             weekNum = atoi(week + 1);
1025
1026         if (dateStr.year)
1027             currentTime->tm_year = atoi(dateStr.year);
1028         currentTime->tm_year = currentTime->tm_year - 1900;
1029
1030         struct tm firtTimeOfyear;
1031         memset(&firtTimeOfyear, 0, sizeof(struct tm));
1032         firtTimeOfyear.tm_year = currentTime->tm_year;
1033         firtTimeOfyear.tm_mon = 0;
1034         firtTimeOfyear.tm_mday = 1;
1035         mktime(&firtTimeOfyear);
1036
1037         char firstWeek[10] = {0, };
1038         strftime(firstWeek, 10, "%w", &firtTimeOfyear);
1039         int firstWeekCount = atoi(firstWeek);
1040
1041         int totalDays = 1;
1042
1043         totalDays = weekNum * 7 - firstWeekCount;
1044
1045         int days = 0;
1046         int month = _calculateMonthAndDay(currentTime->tm_year, totalDays, &days);
1047
1048         currentTime->tm_mon = month;
1049         currentTime->tm_mday = days;
1050     }
1051
1052     if (m_pickerLayout) {
1053         // Just update the value.
1054         elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
1055         evas_object_focus_set(m_pickerLayout->okButton, true);
1056         return;
1057     }
1058
1059     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
1060
1061     createDatetimePopup("Select date", currentTime);
1062     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_HOUR, EINA_FALSE);
1063     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_MINUTE, EINA_FALSE);
1064     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_AMPM, EINA_FALSE);
1065
1066     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
1067     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
1068     elm_object_text_set(m_pickerLayout->okButton, "OK");
1069     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
1070     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _week_popup_response_cb, this);
1071     evas_object_focus_set(m_pickerLayout->okButton, true);
1072
1073     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
1074     evas_object_show(m_pickerLayout->popup);
1075 }
1076
1077 void InputPicker::ewk_time_popup(const char* inputValue)
1078 {
1079     struct tm* currentTime;
1080     time_t  cur_time;
1081     time(&cur_time);
1082     currentTime = localtime(&cur_time);
1083
1084     Input_Date_Str dateStr;
1085     memset(&dateStr, 0, sizeof(Input_Date_Str));
1086
1087     if (inputValue && strlen(inputValue)) {
1088         char tmpinputValue[maxDatetimeLength] = { 0, };
1089
1090         snprintf(tmpinputValue, maxDatetimeLength, "%s", inputValue);
1091         char* token = strtok(tmpinputValue,":");
1092         if (token)
1093             strncpy(dateStr.hour, token, maxDateStringLength);
1094         token = strtok(0, ":");
1095         if (token)
1096             strncpy(dateStr.min, token, maxDateStringLength);
1097
1098         if (dateStr.hour)
1099             currentTime->tm_hour = atoi(dateStr.hour);
1100         if (dateStr.min)
1101             currentTime->tm_min = atoi(dateStr.min);
1102     }
1103
1104     if (m_pickerLayout) {
1105         // Just update the value.
1106         elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
1107         evas_object_focus_set(m_pickerLayout->okButton, true);
1108         return;
1109     }
1110
1111     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
1112
1113     createDatetimePopup("Select time", currentTime);
1114     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_YEAR, EINA_FALSE);
1115     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_MONTH, EINA_FALSE);
1116     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_DATE, EINA_FALSE);
1117
1118     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
1119     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
1120     elm_object_text_set(m_pickerLayout->okButton, "OK");
1121     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
1122     evas_object_focus_set(m_pickerLayout->okButton, true);
1123
1124     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _time_popup_response_cb, this);
1125     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
1126     evas_object_show(m_pickerLayout->popup);
1127 }
1128
1129 void InputPicker::ewk_month_popup(const char* inputValue)
1130 {
1131     struct tm* currentTime;
1132     time_t  cur_time;
1133     time(&cur_time);
1134     currentTime = localtime(&cur_time);
1135
1136     Input_Date_Str dateStr;
1137     memset(&dateStr, 0, sizeof(Input_Date_Str));
1138
1139     if (inputValue && strlen(inputValue)) {
1140         char tmpInputValue[maxDatetimeLength] = { 0, };
1141
1142         snprintf(tmpInputValue, maxDatetimeLength, "%s", inputValue);
1143         char* token = strtok(tmpInputValue,"-");
1144         if (token)
1145             strncpy(dateStr.year, token, maxDateStringLength);
1146         token = strtok(0, "-");
1147         if (token)
1148             strncpy(dateStr.mon, token, maxDateStringLength);
1149
1150         if (dateStr.year)
1151             currentTime->tm_year = atoi(dateStr.year);
1152         if (dateStr.mon)
1153             currentTime->tm_mon = atoi(dateStr.mon);
1154
1155         currentTime->tm_year = currentTime->tm_year - 1900;
1156         currentTime->tm_mon = currentTime->tm_mon - 1;
1157     }
1158
1159     if (m_pickerLayout) {
1160         // Just update the value.
1161         elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
1162         evas_object_focus_set(m_pickerLayout->okButton, true);
1163         return;
1164     }
1165
1166     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
1167
1168     createDatetimePopup("Select month", currentTime);
1169     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_DATE, EINA_FALSE);
1170     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_HOUR, EINA_FALSE);
1171     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_MINUTE, EINA_FALSE);
1172     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_AMPM, EINA_FALSE);
1173
1174     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
1175     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
1176     elm_object_text_set(m_pickerLayout->okButton, "OK");
1177     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
1178     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _month_popup_response_cb, this);
1179     evas_object_focus_set(m_pickerLayout->okButton, true);
1180
1181     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
1182     evas_object_show(m_pickerLayout->popup);
1183 }
1184
1185 void InputPicker::ewk_datetime_popup(const char* inputValue, bool local)
1186 {
1187     struct tm* currentTime;
1188     time_t  cur_time;
1189     time(&cur_time);
1190     currentTime = localtime(&cur_time);
1191
1192     Input_Date_Str dateStr;
1193     memset(&dateStr, 0, sizeof(Input_Date_Str));
1194
1195     if (inputValue && strlen(inputValue)) {
1196         char tmpInputValue[maxDatetimeLength] = { 0, };
1197
1198         snprintf(tmpInputValue, maxDatetimeLength, "%s", inputValue);
1199         char* token = strtok(tmpInputValue,"-");
1200         if (token)
1201             strncpy(dateStr.year, token, maxDateStringLength);
1202         token = strtok(0, "-");
1203         if (token)
1204             strncpy(dateStr.mon, token, maxDateStringLength);
1205         token = strtok(0, "T");
1206         if (token)
1207             strncpy(dateStr.day, token, maxDateStringLength);
1208         token = strtok(0, ":");
1209         if (token)
1210             strncpy(dateStr.hour, token, maxDateStringLength);
1211
1212         if (local) {
1213             token = strtok(0, "Z");
1214             if (token)
1215                 strncpy(dateStr.min, token, maxDateStringLength);
1216         } else {
1217             token = strtok(0, ":");
1218             if (token)
1219                 strncpy(dateStr.min, token, maxDateStringLength);
1220         }
1221
1222         if (dateStr.year)
1223             currentTime->tm_year = atoi(dateStr.year);
1224         if (dateStr.mon)
1225             currentTime->tm_mon = atoi(dateStr.mon);
1226         if (dateStr.day)
1227             currentTime->tm_mday = atoi(dateStr.day);
1228         if (dateStr.hour)
1229             currentTime->tm_hour = atoi(dateStr.hour);
1230         if (dateStr.min)
1231             currentTime->tm_min = atoi(dateStr.min);
1232
1233         currentTime->tm_year = currentTime->tm_year - 1900;
1234         currentTime->tm_mon = currentTime->tm_mon - 1;
1235     }
1236
1237     if (m_pickerLayout) {
1238         // Just update the value.
1239         m_pickerLayout->datetimeLocal = local;
1240
1241         elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
1242         evas_object_focus_set(m_pickerLayout->okButton, true);
1243         return;
1244     }
1245
1246     m_pickerLayout = new Input_Picker_Layout(m_ewkView);
1247
1248     m_pickerLayout->datetimeLocal = local;
1249
1250     createDatetimePopup("Select datetime", currentTime);
1251
1252     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
1253     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
1254     elm_object_text_set(m_pickerLayout->okButton, "OK");
1255     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
1256     evas_object_focus_set(m_pickerLayout->okButton, true);
1257
1258     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _datetime_popup_response_cb, this);
1259     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
1260     evas_object_show(m_pickerLayout->popup);
1261
1262 }
1263
1264 void InputPicker::createDatetimePopup(const char* title, struct tm* currentTime)
1265 {
1266     Evas_Object* parent = parentWindow();
1267     m_pickerLayout->popup = elm_popup_add(parent);
1268     evas_object_size_hint_weight_set(m_pickerLayout->popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1269     elm_object_part_text_set(m_pickerLayout->popup, "title,text", title);
1270
1271 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
1272     if (EflAssistHandle) {
1273         void (*webkit_ea_object_event_callback_add)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *);
1274         webkit_ea_object_event_callback_add = (void (*)(Evas_Object *, Ea_Callback_Type , Ea_Event_Cb func, void *))dlsym(EflAssistHandle, "ea_object_event_callback_add");
1275         (*webkit_ea_object_event_callback_add)(m_pickerLayout->popup, EA_CALLBACK_BACK, _popup_back_cb, this);
1276     }
1277 #endif
1278
1279     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
1280     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","datetime_popup");
1281     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1282
1283     m_pickerLayout->datePicker = elm_datetime_add(m_pickerLayout->layout);
1284     elm_object_part_content_set(m_pickerLayout->layout , "elm.swallow.datetime", m_pickerLayout->datePicker);
1285
1286     char* format = datetimeFormat();
1287     if (format) {
1288         elm_datetime_format_set(m_pickerLayout->datePicker, format);
1289         free(format);
1290     } else
1291         elm_datetime_format_set(m_pickerLayout->datePicker, defaultDatetimeFormat);
1292
1293     elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
1294
1295 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
1296     evas_object_smart_callback_add(m_pickerLayout->datePicker, "edit,start", _edit_start_cb, 0);
1297     evas_object_smart_callback_add(m_pickerLayout->datePicker, "edit,end", _edit_end_cb, m_pickerLayout);
1298 #endif
1299 }
1300
1301 Evas_Object* InputPicker::parentWindow()
1302 {
1303     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
1304     Evas_Object* win = parent;
1305
1306     while (parent) {
1307         const char* type = elm_object_widget_type_get(parent);
1308         if (type) {
1309             if (!strncmp(type, "elm_win", strlen("elm_win"))) {
1310                 win = parent;
1311                 break;
1312             }
1313         }
1314         parent = elm_object_parent_widget_get(parent);
1315     }
1316
1317     if (!win)
1318         win = m_ewkView;
1319
1320     Evas_Object* conformant = elm_conformant_add(win);
1321     elm_win_conformant_set(win, EINA_TRUE);
1322     elm_win_resize_object_add(win, conformant);
1323     evas_object_size_hint_weight_set(conformant, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
1324     evas_object_size_hint_align_set(conformant, EVAS_HINT_FILL, EVAS_HINT_FILL);
1325     evas_object_show(conformant);
1326     m_pickerLayout->conformant = conformant;
1327
1328     Evas_Object* content = elm_layout_add(conformant);
1329     elm_object_content_set(conformant, content);
1330
1331     return content;
1332 }
1333
1334 void InputPicker::deletePopupLayout()
1335 {
1336     if(m_pickerLayout->conformant){
1337         evas_object_del(m_pickerLayout->conformant);
1338         m_pickerLayout->conformant = 0;
1339     }
1340 }
1341
1342 void InputPicker::_date_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
1343 {
1344     struct tm currentTime;
1345     memset(&currentTime, 0, sizeof(struct tm));
1346     Input_Date date;
1347     char dateStr[20] = {0, };
1348
1349     InputPicker* inputPicker = static_cast<InputPicker*>(data);
1350
1351     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
1352
1353     date.day =  currentTime.tm_mday;
1354     date.mon = currentTime.tm_mon;
1355     date.year = currentTime.tm_year;
1356
1357     date.year = 1900 + date.year;
1358     sprintf(dateStr, "%d-%02d-%02d" , date.year, date.mon + 1, date.day);
1359
1360     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
1361     ewk_view_command_execute(inputPicker->m_ewkView, "Unselect", 0);
1362
1363     inputPicker->deletePopupLayout();
1364     delete inputPicker->m_pickerLayout;
1365     inputPicker->m_pickerLayout = 0;
1366 }
1367
1368 void InputPicker::_week_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
1369 {
1370     struct tm currentTime;
1371     memset(&currentTime, 0, sizeof(struct tm));
1372     Input_Date date;
1373     char dateStr[20] = {0, };
1374
1375     InputPicker* inputPicker = static_cast<InputPicker*>(data);
1376
1377     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
1378
1379     date.day =  currentTime.tm_mday;
1380     date.mon = currentTime.tm_mon;
1381     date.year = currentTime.tm_year;
1382     mktime(&currentTime);
1383
1384     struct tm firtTimeOfyear;
1385     memset(&firtTimeOfyear, 0, sizeof(struct tm));
1386     firtTimeOfyear.tm_year = date.year;
1387     firtTimeOfyear.tm_mon = 0;
1388     firtTimeOfyear.tm_mday = 1;
1389     mktime(&firtTimeOfyear);
1390
1391     char firstWeek[10] = {0, };
1392     strftime(firstWeek, 10, "%w", &firtTimeOfyear);
1393     int firstWeekCount = atoi(firstWeek);
1394
1395     char totalDay[10] = {0, };
1396     strftime(totalDay, 10, "%j", &currentTime);
1397     int totalDayCount = atoi(totalDay);
1398
1399     int weekNum = (totalDayCount + firstWeekCount) / 7;
1400     if (totalDayCount + firstWeekCount > weekNum * 7)
1401         weekNum++;
1402
1403     date.year = 1900 + date.year;
1404     sprintf(dateStr, "%d-W%02d" , date.year, weekNum);
1405
1406     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
1407     ewk_view_command_execute(inputPicker->m_ewkView, "Unselect", 0);
1408
1409     inputPicker->deletePopupLayout();
1410     delete inputPicker->m_pickerLayout;
1411     inputPicker->m_pickerLayout = 0;
1412 }
1413
1414 void InputPicker::_time_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
1415 {
1416     struct tm currentTime;
1417     memset(&currentTime, 0, sizeof(struct tm));
1418     Input_Date date;
1419     char dateStr[20] = {0, };
1420
1421     InputPicker* inputPicker = static_cast<InputPicker*>(data);
1422
1423     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
1424
1425     date.hour =  currentTime.tm_hour;
1426     date.min = currentTime.tm_min;
1427
1428     sprintf(dateStr, "%02d:%02d" , date.hour, date.min);
1429     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
1430     ewk_view_command_execute(inputPicker->m_ewkView, "Unselect", 0);
1431
1432     inputPicker->deletePopupLayout();
1433     delete inputPicker->m_pickerLayout;
1434     inputPicker->m_pickerLayout = 0;
1435 }
1436
1437 void InputPicker::_month_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
1438 {
1439     struct tm currentTime;
1440     memset(&currentTime, 0, sizeof(struct tm));
1441     Input_Date date;
1442     char dateStr[20] = {0, };
1443
1444     InputPicker* inputPicker = static_cast<InputPicker*>(data);
1445
1446     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
1447
1448     date.mon = currentTime.tm_mon;
1449     date.year = currentTime.tm_year;
1450
1451     date.year = 1900 + date.year;
1452     sprintf(dateStr, "%02d-%02d" , date.year, date.mon + 1);
1453
1454     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
1455     ewk_view_command_execute(inputPicker->m_ewkView, "Unselect", 0);
1456
1457     inputPicker->deletePopupLayout();
1458     delete inputPicker->m_pickerLayout;
1459     inputPicker->m_pickerLayout = 0;
1460 }
1461
1462 void InputPicker::_datetime_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
1463 {
1464     struct tm currentTime;
1465     memset(&currentTime, 0, sizeof(struct tm));
1466     Input_Date date;
1467     char dateStr[50] = {0, };
1468
1469     InputPicker* inputPicker = static_cast<InputPicker*>(data);
1470
1471     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
1472     date.year = currentTime.tm_year;
1473     date.day =  currentTime.tm_mday;
1474     date.mon = currentTime.tm_mon;
1475     date.hour =  currentTime.tm_hour;
1476     date.min = currentTime.tm_min;
1477
1478     date.year = 1900 + date.year;
1479
1480     if (inputPicker->m_pickerLayout->datetimeLocal)
1481         sprintf(dateStr, "%d-%02d-%02dT%02d:%02d" , date.year, date.mon + 1, date.day, date.hour, date.min);
1482     else
1483         sprintf(dateStr, "%d-%02d-%02dT%02d:%02dZ" , date.year, date.mon + 1, date.day, date.hour, date.min);
1484
1485     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
1486     ewk_view_command_execute(inputPicker->m_ewkView, "Unselect", 0);
1487
1488     inputPicker->deletePopupLayout();
1489     delete inputPicker->m_pickerLayout;
1490     inputPicker->m_pickerLayout = 0;
1491 }
1492
1493 #if ENABLE(TIZEN_HW_MORE_BACK_KEY)
1494 void InputPicker::_popup_back_cb(void* data,  Evas_Object* obj, void* event_info)
1495 {
1496     InputPicker* inputPicker = static_cast<InputPicker*>(data);
1497
1498     ewk_view_command_execute(inputPicker->m_ewkView, "Unselect", 0);
1499
1500     if (!inputPicker->m_pickerLayout)
1501         return;
1502
1503     inputPicker->deletePopupLayout();
1504     delete inputPicker->m_pickerLayout;
1505     inputPicker->m_pickerLayout = 0;
1506 }
1507 #endif
1508
1509 } // namespace WebKit
1510
1511 #endif // OS(TIZEN) && ENABLE(TIZEN_INPUT_TAG_EXTENSION)