fe5f89521f482bd093fe9f3502090f6c4243eb0e
[profile/ivi/webkit-efl.git] / Source / WebKit2 / UIProcess / API / efl / 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 <ctime>
28 #include <stdlib.h>
29 #include <string.h>
30
31
32 #if OS(TIZEN) && ENABLE(TIZEN_INPUT_TAG_EXTENSION)
33
34 namespace WebKit {
35
36 InputPicker::InputPicker(Evas_Object* ewkView)
37 : m_ewkView(ewkView)
38 , m_pickerLayout(0)
39 {
40 }
41
42 InputPicker::~InputPicker()
43 {
44     if (m_pickerLayout) {
45         if (m_pickerLayout->popup)
46             evas_object_del(m_pickerLayout->popup);
47         delete m_pickerLayout;
48     }
49 }
50
51 void InputPicker::show(Ewk_Input_Type inputType, const char* inputValue)
52 {
53     if(inputType == EWK_INPUT_TYPE_DATE)
54         ewk_date_popup(inputValue);
55     else if(inputType == EWK_INPUT_TYPE_TIME)
56         ewk_time_popup(inputValue);
57     else if(inputType == EWK_INPUT_TYPE_DATETIME)
58         ewk_datetime_popup(inputValue, false);
59     else if(inputType == EWK_INPUT_TYPE_DATETIMELOCAL)
60         ewk_datetime_popup(inputValue, true);
61     else if(inputType == EWK_INPUT_TYPE_MONTH)
62         ewk_month_popup(inputValue);
63     else if(inputType == EWK_INPUT_TYPE_WEEK)
64         ewk_week_popup(inputValue);
65     else if(inputType == EWK_INPUT_TYPE_COLOR)
66         ewk_color_popup(inputValue);
67 }
68
69 #if ENABLE(TIZEN_DATALIST_ELEMENT)
70 void InputPicker::showDataList(Ewk_Input_Type inputType, Eina_List* optionList)
71 {
72     // FIXME: Show DataList UI.
73 }
74
75 void InputPicker::hideDataList(Ewk_Input_Type inputType)
76 {
77     // FIXME: Close DataList UI.
78 }
79 #endif
80
81 #if ENABLE(ELM_COLORPALLETE)
82 static void _color_palette_changed_cb(void* data, Evas_Object* obj, void* eventInfo)
83 {
84     int r = 0;
85     int g = 0;
86     int b = 0;
87     int a = 0;
88     Elm_Object_Item *color_it = static_cast<Elm_Object_Item*>(eventInfo);
89     elm_colorselector_palette_item_color_get(color_it, &r, &g, &b, &a);
90     evas_object_color_set(static_cast<Evas_Object*>(data), r, g, b , a);
91 }
92 #endif
93
94 void InputPicker::_color_selected_cb(void* data, Evas* evas, Evas_Object* obj, void* eventInfo)
95 {
96     int r = 0;
97     int g = 0;
98     int b = 0;
99     int a = 0;
100     evas_object_color_get(obj, &r, &g, &b, &a);
101
102     evas_object_color_set(static_cast<Evas_Object*>(data), r, g, b, a);
103 }
104
105 void InputPicker::ewk_color_popup(const char* inputValue)
106 {
107     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
108     Evas_Object* win = parent;
109
110     if (m_pickerLayout)
111         delete m_pickerLayout;
112
113     m_pickerLayout = static_cast<Input_Picker_Layout*>(new Input_Picker_Layout);
114     memset(m_pickerLayout, 0, sizeof(m_pickerLayout));
115
116     while (parent) {
117         const char* type = elm_object_widget_type_get(parent);
118         if (type) {
119             if (!strcmp(type, "win")) {
120                 win =  parent;
121                 break;
122             }
123         }
124         parent = elm_object_parent_widget_get(parent);
125     }
126
127     if (!win)
128     win = m_ewkView;
129
130     m_pickerLayout->popup = elm_popup_add(win);
131     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select color");
132
133     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
134     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","color_picker");
135     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
136     evas_object_show(m_pickerLayout->layout);
137     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
138
139     m_pickerLayout->colorRect = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
140     evas_object_size_hint_weight_set(m_pickerLayout->colorRect, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
141     evas_object_show(m_pickerLayout->colorRect);
142
143     char colorR[3];
144     colorR[2] = '\0';
145     char colorG[3];
146     colorG[2] = '\0';
147     char colorB[3];
148     colorB[2] = '\0';
149
150     colorR[0] = inputValue[1];
151     colorR[1] = inputValue[2];
152     colorG[0] = inputValue[3];
153     colorG[1] = inputValue[4];
154     colorB[0] = inputValue[5];
155     colorB[1] = inputValue[6];
156
157     int decR = 0;
158     int decG = 0;
159     int decB = 0;
160
161     decR = static_cast<int>(strtol(colorR, 0, 16));
162     decG = static_cast<int>(strtol(colorG, 0, 16));
163     decB = static_cast<int>(strtol(colorB, 0, 16));
164
165     printf("\n<<<<<<<< colorR = [%s], colorG = [%s], colorB = [%s] >>>>>>>>>>\n", colorR, colorG, colorB);
166     printf("\n<<<<<<<< decR = [%d], decG = [%d], decB = [%d] >>>>>>>>>>\n", decR, decG, decB);
167
168     evas_object_color_set(m_pickerLayout->colorRect, decR, decG, decB, 255);
169     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color_rect", m_pickerLayout->colorRect);
170 #if ENABLE(ELM_COLORPALLETE)
171     Evas_Object* colorPalette = elm_colorselector_add(m_pickerLayout->popup);
172     elm_colorselector_mode_set(colorPalette, ELM_COLORSELECTOR_PALETTE);
173
174     elm_colorselector_palette_color_add(colorPalette, 128, 0, 0, 255);
175     elm_colorselector_palette_color_add(colorPalette, 255, 0, 128, 255);
176     elm_colorselector_palette_color_add(colorPalette, 255, 0, 0, 255);
177     elm_colorselector_palette_color_add(colorPalette, 255, 127, 39, 255);
178     elm_colorselector_palette_color_add(colorPalette, 255, 255, 0, 255);
179     elm_colorselector_palette_color_add(colorPalette, 0, 255, 0, 255);
180     elm_colorselector_palette_color_add(colorPalette, 0, 255, 255, 255);
181     elm_colorselector_palette_color_add(colorPalette, 0, 0, 255, 255);
182     elm_colorselector_palette_color_add(colorPalette, 0, 0, 128, 255);
183     elm_colorselector_palette_color_add(colorPalette, 64, 0, 64, 255);
184     elm_colorselector_palette_color_add(colorPalette, 0, 0, 0, 255);
185     elm_colorselector_palette_color_add(colorPalette, 255, 255, 255, 255);
186
187     evas_object_size_hint_fill_set(colorPalette, EVAS_HINT_FILL, 0);
188     evas_object_size_hint_weight_set(colorPalette, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
189     evas_object_smart_callback_add(colorPalette, "color,item,selected", _color_palette_changed_cb, m_pickerLayout->colorRect);
190     evas_object_show(colorPalette);
191
192     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color_palette", colorPalette);
193 #else
194     Evas_Object* color1 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
195     evas_object_size_hint_weight_set(color1, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
196     evas_object_color_set(color1, 128, 0, 0, 255);
197     evas_object_event_callback_add(color1, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
198     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color1", color1);
199     evas_object_show(color1);
200
201     Evas_Object* color2 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
202     evas_object_size_hint_weight_set(color2, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
203     evas_object_color_set(color2, 255, 0, 128, 255);
204     evas_object_event_callback_add(color2, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
205     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color2", color2);
206     evas_object_show(color2);
207
208     Evas_Object* color3 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
209     evas_object_size_hint_weight_set(color3, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
210     evas_object_color_set(color3, 255, 0, 0, 255);
211     evas_object_event_callback_add(color3, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
212     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color3", color3);
213     evas_object_show(color3);
214
215     Evas_Object* color4 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
216     evas_object_size_hint_weight_set(color4, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
217     evas_object_color_set(color4, 255, 127, 39, 255);
218     evas_object_event_callback_add(color4, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
219     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color4", color4);
220     evas_object_show(color4);
221
222     Evas_Object* color5 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
223     evas_object_size_hint_weight_set(color5, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
224     evas_object_color_set(color5, 255, 255, 0, 255);
225     evas_object_event_callback_add(color5, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
226     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color5", color5);
227     evas_object_show(color5);
228
229     Evas_Object* color6 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
230     evas_object_size_hint_weight_set(color6, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
231     evas_object_color_set(color6, 0, 255, 0, 255);
232     evas_object_event_callback_add(color6, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
233     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color6", color6);
234     evas_object_show(color6);
235
236     Evas_Object* color7 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
237     evas_object_size_hint_weight_set(color7, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
238     evas_object_color_set(color7, 0, 255, 255, 255);
239     evas_object_event_callback_add(color7, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
240     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color7", color7);
241     evas_object_show(color7);
242
243     Evas_Object* color8 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
244     evas_object_size_hint_weight_set(color8, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
245     evas_object_color_set(color8, 0, 0, 255, 255);
246     evas_object_event_callback_add(color8, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
247     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color8", color8);
248     evas_object_show(color8);
249
250     Evas_Object* color9 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
251     evas_object_size_hint_weight_set(color9, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
252     evas_object_color_set(color9, 0, 0, 128, 255);
253     evas_object_event_callback_add(color9, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
254     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color9", color9);
255     evas_object_show(color9);
256
257     Evas_Object* color10 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
258     evas_object_size_hint_weight_set(color10, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
259     evas_object_color_set(color10, 64, 0, 64, 255);
260     evas_object_event_callback_add(color10, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
261     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color10", color10);
262     evas_object_show(color10);
263
264     Evas_Object* color11 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
265     evas_object_size_hint_weight_set(color11, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
266     evas_object_color_set(color11, 153, 217, 234, 255);
267     evas_object_event_callback_add(color11, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
268     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color11", color11);
269     evas_object_show(color11);
270
271     Evas_Object* color12 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
272     evas_object_size_hint_weight_set(color12, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
273     evas_object_color_set(color12, 128, 128, 128, 255);
274     evas_object_event_callback_add(color12, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
275     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color12", color12);
276     evas_object_show(color12);
277
278     Evas_Object* color13 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
279     evas_object_size_hint_weight_set(color13, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
280     evas_object_color_set(color13, 0, 0, 0, 255);
281     evas_object_event_callback_add(color13, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
282     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color13", color13);
283     evas_object_show(color13);
284
285     Evas_Object* color14 = evas_object_rectangle_add(evas_object_evas_get(m_pickerLayout->layout));
286     evas_object_size_hint_weight_set(color14, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
287     evas_object_color_set(color14, 255, 255, 255, 255);
288     evas_object_event_callback_add(color14, EVAS_CALLBACK_MOUSE_DOWN, _color_selected_cb, m_pickerLayout->colorRect);
289     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.color14", color14);
290     evas_object_show(color14);
291 #endif
292     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
293     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
294     elm_object_text_set(m_pickerLayout->okButton, "OK");
295     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
296     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _color_popup_response_cb, this);
297
298     evas_object_show(m_pickerLayout->popup);
299 }
300
301 void InputPicker::_color_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
302 {
303     InputPicker* inputPicker = static_cast<InputPicker*>(data);
304
305     int r = 0;
306     int g = 0;
307     int b = 0;
308     int a = 0;
309     evas_object_color_get(inputPicker->m_pickerLayout->colorRect, &r, &g, &b, &a);
310
311     char selectColor[10];
312     selectColor[10] = '\0';
313     sprintf(selectColor, "#%02x%02x%02x", r, g, b);
314
315     ewk_view_color_chooser_color_set(inputPicker->m_ewkView, selectColor);
316     ewk_view_color_chooser_close(inputPicker->m_ewkView);
317
318     if(inputPicker->m_pickerLayout->popup){
319         evas_object_del(inputPicker->m_pickerLayout->popup);
320         inputPicker->m_pickerLayout->popup = 0;
321     }
322
323     delete inputPicker->m_pickerLayout;
324     inputPicker->m_pickerLayout = 0;
325 }
326
327 void InputPicker::ewk_date_popup(const char* inputValue)
328 {
329     struct tm* currentTime;
330     time_t cur_time;
331     time(&cur_time);
332     currentTime = localtime(&cur_time);
333
334     Input_Date_Str dateStr;
335     memset(&dateStr, 0, sizeof(Input_Date_Str));
336
337     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
338     Evas_Object* win = parent;
339
340     if (m_pickerLayout)
341         delete m_pickerLayout;
342
343     m_pickerLayout = static_cast<Input_Picker_Layout*>(new Input_Picker_Layout);
344     memset(m_pickerLayout, 0, sizeof(m_pickerLayout));
345
346     if (inputValue && strlen(inputValue)) {
347         char tmpinputValue[30] = {0,};
348
349         sprintf(tmpinputValue, "%s", inputValue);
350         strcpy(dateStr.year, strtok(tmpinputValue,"-"));
351         strcpy(dateStr.mon, strtok(0, "-"));
352         strcpy(dateStr.day, strtok(0, "-"));
353
354         currentTime->tm_year = atoi(dateStr.year);
355         currentTime->tm_mon = atoi(dateStr.mon);
356         currentTime->tm_mday = atoi(dateStr.day);
357
358         currentTime->tm_year = currentTime->tm_year - 1900;
359         currentTime->tm_mon = currentTime->tm_mon - 1;
360     }
361
362     while (parent) {
363         const char* type = elm_object_widget_type_get(parent);
364         if (type) {
365             if (!strcmp(type, "win")) {
366                 win =  parent;
367                 break;
368             }
369         }
370         parent = elm_object_parent_widget_get(parent);
371     }
372
373     if (!win)
374         win = m_ewkView;
375
376     m_pickerLayout->popup = elm_popup_add(win);
377     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select date");
378
379     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
380     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","elm/datepicker");
381     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
382
383     edje_object_part_text_set(elm_layout_edje_get(m_pickerLayout->layout), "title_text", "Date (Default format) :");
384
385     m_pickerLayout->datePicker = elm_datetime_add(m_pickerLayout->layout);
386     elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
387
388     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_HOUR, EINA_FALSE);
389     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_MINUTE, EINA_FALSE);
390     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_AMPM, EINA_FALSE);
391     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.content.date", m_pickerLayout->datePicker);
392
393     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
394     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
395     elm_object_text_set(m_pickerLayout->okButton, "OK");
396     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
397     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _date_popup_response_cb, this);
398
399     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
400     evas_object_show(m_pickerLayout->popup);
401 }
402
403 #define isLeapYear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
404 static int _calculateMonthAndDay(int year, int totalDays, int* remains)
405 {
406     // Jan.
407     if (totalDays - 31 < 0) {
408         *remains = totalDays;
409         return 0;
410     }
411
412     totalDays = totalDays - 31;
413
414     // Feb.
415     if (isLeapYear(year)) {
416         if (totalDays - 29 < 0) {
417             *remains = totalDays;
418             return 1;
419         }
420         totalDays = totalDays - 29;
421     } else {
422         if (totalDays - 28 < 0) {
423             *remains = totalDays;
424             return 1;
425         }
426         totalDays = totalDays - 28;
427     }
428
429     // Mar.
430     if (totalDays - 31 < 0) {
431         *remains = totalDays;
432         return 2;
433     }
434     totalDays = totalDays - 31;
435
436     // Apr.
437     if (totalDays - 30 < 0) {
438         *remains = totalDays;
439         return 3;
440     }
441     totalDays = totalDays - 30;
442
443     // May
444     if (totalDays - 31 < 0) {
445         *remains = totalDays;
446         return 4;
447     }
448     totalDays = totalDays - 31;
449
450     // Jun.
451     if (totalDays - 30 < 0) {
452         *remains = totalDays;
453         return 5;
454     }
455     totalDays = totalDays - 30;
456
457     // Jul.
458     if (totalDays - 31 < 0) {
459         *remains = totalDays;
460         return 6;
461     }
462     totalDays = totalDays - 31;
463
464     // Aug.
465     if (totalDays - 31 < 0) {
466         *remains = totalDays;
467         return 7;
468     }
469     totalDays = totalDays - 31;
470
471     // Sept.
472     if (totalDays - 30 < 0) {
473         *remains = totalDays;
474         return 8;
475     }
476     totalDays = totalDays - 30;
477
478     // Oct.
479     if (totalDays - 31 < 0) {
480         *remains = totalDays;
481         return 9;
482     }
483     totalDays = totalDays - 31;
484
485     // Nov.
486     if (totalDays - 30 < 0) {
487         *remains = totalDays;
488         return 10;
489     }
490     totalDays = totalDays - 30;
491
492     *remains = totalDays;
493     return 11;
494 }
495
496 void InputPicker::ewk_week_popup(const char* inputValue)
497 {
498     struct tm* currentTime;
499     time_t cur_time;
500     time(&cur_time);
501     currentTime = localtime(&cur_time);
502
503     Input_Date_Str dateStr;
504     memset(&dateStr, 0, sizeof(Input_Date_Str));
505
506     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
507     Evas_Object* win = parent;
508
509     if (m_pickerLayout)
510         delete m_pickerLayout;
511
512     m_pickerLayout = static_cast<Input_Picker_Layout*>(new Input_Picker_Layout);
513     memset(m_pickerLayout, 0, sizeof(m_pickerLayout));
514
515     if (inputValue && strlen(inputValue)) {
516         char tmpinputValue[30] = {0,};
517
518         sprintf(tmpinputValue, "%s", inputValue);
519         strcpy(dateStr.year, strtok(tmpinputValue,"-"));
520         const char* week = strstr(inputValue, "W");
521         int weekNum = 1;
522         if (week)
523             weekNum = atoi(week + 1);
524
525         currentTime->tm_year = atoi(dateStr.year);
526         currentTime->tm_year = currentTime->tm_year - 1900;
527
528         struct tm firtTimeOfyear;
529         memset(&firtTimeOfyear, 0, sizeof(struct tm));
530         firtTimeOfyear.tm_year = currentTime->tm_year;
531         firtTimeOfyear.tm_mon = 0;
532         firtTimeOfyear.tm_mday = 1;
533         mktime(&firtTimeOfyear);
534
535         char firstWeek[10] = {0, };
536         strftime(firstWeek, 10, "%w", &firtTimeOfyear);
537         int firstWeekCount = atoi(firstWeek);
538
539         int totalDays = 1;
540
541         totalDays = weekNum * 7 - firstWeekCount;
542
543         int days = 0;
544         int month = _calculateMonthAndDay(currentTime->tm_year, totalDays, &days);
545
546         currentTime->tm_mon = month;
547         currentTime->tm_mday = days;
548     }
549
550     while (parent) {
551         const char* type = elm_object_widget_type_get(parent);
552         if (type) {
553             if (!strcmp(type, "win")) {
554                 win =  parent;
555                 break;
556             }
557         }
558         parent = elm_object_parent_widget_get(parent);
559     }
560
561     if (!win)
562         win = m_ewkView;
563
564     m_pickerLayout->popup = elm_popup_add(win);
565     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select date");
566
567     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
568     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","elm/datepicker");
569     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
570
571     edje_object_part_text_set(elm_layout_edje_get(m_pickerLayout->layout), "title_text", "Date (Default format) :");
572
573     m_pickerLayout->datePicker = elm_datetime_add(m_pickerLayout->layout);
574     elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
575
576     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_HOUR, EINA_FALSE);
577     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_MINUTE, EINA_FALSE);
578     elm_datetime_field_visible_set(m_pickerLayout->datePicker, ELM_DATETIME_AMPM, EINA_FALSE);
579     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.content.date", m_pickerLayout->datePicker);
580
581     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
582     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
583     elm_object_text_set(m_pickerLayout->okButton, "OK");
584     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
585     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _week_popup_response_cb, this);
586
587     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
588     evas_object_show(m_pickerLayout->popup);
589 }
590
591 void InputPicker::ewk_time_popup(const char* inputValue)
592 {
593     struct tm* currentTime;
594     time_t  cur_time;
595     time(&cur_time);
596     currentTime = localtime(&cur_time);
597
598     Input_Date_Str dateStr;
599     memset(&dateStr, 0, sizeof(Input_Date_Str));
600
601     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
602     Evas_Object* win = parent;
603
604     if (m_pickerLayout)
605         delete m_pickerLayout;
606
607     m_pickerLayout = static_cast<Input_Picker_Layout*>(new Input_Picker_Layout);
608     memset(m_pickerLayout, 0, sizeof(m_pickerLayout));
609
610     if (inputValue && strlen(inputValue)) {
611         char tmpinputValue[30] = {0,};
612
613         sprintf(tmpinputValue, "%s", inputValue);
614         strcpy(dateStr.hour, strtok(tmpinputValue,":"));
615         strcpy(dateStr.min, strtok(0, ":"));
616
617         currentTime->tm_hour = atoi(dateStr.hour);
618         currentTime->tm_min = atoi(dateStr.min);
619     }
620
621     while (parent) {
622         const char* type = elm_object_widget_type_get(parent);
623         if (type) {
624             if (!strcmp(type, "win")) {
625                 win =  parent;
626                 break;
627             }
628         }
629         parent = elm_object_parent_widget_get(parent);
630     }
631
632     if (!win)
633         win = m_ewkView;
634
635     m_pickerLayout->popup = elm_popup_add(win);
636     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select time");
637
638     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
639     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","elm/datepicker");
640     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
641
642     edje_object_part_text_set(elm_layout_edje_get(m_pickerLayout->layout), "title_text", "Time (Default format) :");
643
644     m_pickerLayout->timePicker = elm_datetime_add(m_pickerLayout->layout);
645     elm_datetime_format_set(m_pickerLayout->timePicker, "%H:%M");
646     elm_datetime_value_set(m_pickerLayout->timePicker, currentTime);
647
648     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.content.date", m_pickerLayout->timePicker);
649
650     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
651     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
652     elm_object_text_set(m_pickerLayout->okButton, "OK");
653     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
654
655     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _time_popup_response_cb, this);
656     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
657     evas_object_show(m_pickerLayout->popup);
658 }
659
660 void InputPicker::ewk_month_popup(const char* inputValue)
661 {
662     struct tm* currentTime;
663     time_t  cur_time;
664     time(&cur_time);
665     currentTime = localtime(&cur_time);
666
667     Input_Date_Str dateStr;
668     memset(&dateStr, 0, sizeof(Input_Date_Str));
669
670     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
671     Evas_Object* win = parent;
672
673     if (m_pickerLayout)
674         delete m_pickerLayout;
675
676     m_pickerLayout = static_cast<Input_Picker_Layout*>(new Input_Picker_Layout);
677     memset(m_pickerLayout, 0, sizeof(m_pickerLayout));
678
679     if (inputValue && strlen(inputValue)) {
680         char tmpInputValue[30] = {0,};
681
682         sprintf(tmpInputValue, "%s", inputValue);
683         strcpy(dateStr.year, strtok(tmpInputValue,"-"));
684         strcpy(dateStr.mon, strtok(0, "-"));
685
686         currentTime->tm_year = atoi(dateStr.year);
687         currentTime->tm_mon = atoi(dateStr.mon);
688
689         currentTime->tm_year = currentTime->tm_year - 1900;
690         currentTime->tm_mon = currentTime->tm_mon - 1;
691     }
692
693     while (parent) {
694         const char* type = elm_object_widget_type_get(parent);
695         if (type) {
696             if (!strcmp(type, "win")) {
697                 win =  parent;
698                 break;
699             }
700         }
701         parent = elm_object_parent_widget_get(parent);
702     }
703
704     if (!win)
705         win = m_ewkView;
706
707     m_pickerLayout->popup = elm_popup_add(win);
708
709     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select month");
710
711     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
712     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","elm/datepicker");
713
714     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
715     m_pickerLayout->datePicker = elm_datetime_add(m_pickerLayout->layout);
716
717     elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
718
719     edje_object_part_text_set(elm_layout_edje_get(m_pickerLayout->layout), "title_text",  "Month (Default format) :");
720
721     elm_datetime_format_set(m_pickerLayout->datePicker, "%Y -%m");
722     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.content.date",  m_pickerLayout->datePicker);
723
724     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
725     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
726     elm_object_text_set(m_pickerLayout->okButton, "OK");
727     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
728     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _month_popup_response_cb, this);
729
730     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
731     evas_object_show(m_pickerLayout->popup);
732 }
733
734 void InputPicker::ewk_datetime_popup(const char* inputValue, bool local)
735 {
736     struct tm* currentTime;
737     time_t  cur_time;
738     time(&cur_time);
739     currentTime = localtime(&cur_time);
740
741     Input_Date_Str dateStr;
742     memset(&dateStr, 0, sizeof(Input_Date_Str));
743
744     Evas_Object* parent = elm_object_parent_widget_get(m_ewkView);
745     Evas_Object* win = parent;
746
747     if (m_pickerLayout)
748         delete m_pickerLayout;
749
750     m_pickerLayout = static_cast<Input_Picker_Layout*>(new Input_Picker_Layout);
751     memset(m_pickerLayout, 0, sizeof(m_pickerLayout));
752
753     m_pickerLayout->datetimeLocal = local;
754
755     if (inputValue && strlen(inputValue)) {
756         char tmpInputValue[30] = {0, };
757
758         sprintf(tmpInputValue, "%s", inputValue);
759         strcpy(dateStr.year, strtok(tmpInputValue,"-"));
760         strcpy(dateStr.mon, strtok(0, "-"));
761         strcpy(dateStr.day, strtok(0, "T"));
762         strcpy(dateStr.hour, strtok(0, ":"));
763
764         if (m_pickerLayout->datetimeLocal)
765             strcpy(dateStr.min, strtok(0, "Z"));
766         else
767             strcpy(dateStr.min, strtok(0, ":"));
768
769         currentTime->tm_year = atoi(dateStr.year);
770         currentTime->tm_mon = atoi(dateStr.mon);
771         currentTime->tm_mday = atoi(dateStr.day);
772         currentTime->tm_hour = atoi(dateStr.hour);
773         currentTime->tm_min = atoi(dateStr.min);
774
775         currentTime->tm_year = currentTime->tm_year - 1900;
776         currentTime->tm_mon = currentTime->tm_mon - 1;
777     }
778
779     while (parent) {
780         const char* type = elm_object_widget_type_get(parent);
781         if (type) {
782             if (!strcmp(type, "win")) {
783                 win =  parent;
784                 break;
785             }
786         }
787         parent = elm_object_parent_widget_get(parent);
788     }
789
790     if (!win)
791         win = m_ewkView;
792
793     m_pickerLayout->popup = elm_popup_add(win);
794     elm_object_part_text_set(m_pickerLayout->popup, "title,text", "Select datetime");
795
796     m_pickerLayout->layout = elm_layout_add(m_pickerLayout->popup);
797     elm_layout_file_set(m_pickerLayout->layout, EDJE_DIR"/control.edj","datepicker_popup");
798     evas_object_size_hint_weight_set(m_pickerLayout->layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
799
800     m_pickerLayout->datePicker = elm_datetime_add(m_pickerLayout->layout);
801     elm_datetime_value_set(m_pickerLayout->datePicker, currentTime);
802
803     elm_datetime_field_visible_set(m_pickerLayout->datePicker , ELM_DATETIME_HOUR, EINA_FALSE);
804     elm_datetime_field_visible_set(m_pickerLayout->datePicker , ELM_DATETIME_MINUTE, EINA_FALSE);
805     elm_datetime_field_visible_set(m_pickerLayout->datePicker , ELM_DATETIME_AMPM, EINA_FALSE);
806     elm_object_part_content_set(m_pickerLayout->layout , "elm.swallow.content.date", m_pickerLayout->datePicker);
807
808     m_pickerLayout->timePicker = elm_datetime_add(m_pickerLayout->layout);
809     elm_datetime_format_set(m_pickerLayout->timePicker, "%H:%M");
810     elm_datetime_value_set(m_pickerLayout->timePicker, currentTime);
811     elm_object_part_content_set(m_pickerLayout->layout, "elm.swallow.content.time", m_pickerLayout->timePicker);
812
813     m_pickerLayout->okButton = elm_button_add(m_pickerLayout->popup);
814     elm_object_style_set(m_pickerLayout->okButton, "popup_button/default");
815     elm_object_text_set(m_pickerLayout->okButton, "OK");
816     elm_object_part_content_set(m_pickerLayout->popup, "button1", m_pickerLayout->okButton);
817
818     evas_object_smart_callback_add(m_pickerLayout->okButton, "clicked", _datetime_popup_response_cb, this);
819     elm_object_content_set(m_pickerLayout->popup, m_pickerLayout->layout);
820     evas_object_show(m_pickerLayout->popup);
821
822 }
823
824 void InputPicker::_date_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
825 {
826     struct tm currentTime;
827     memset(&currentTime, 0, sizeof(struct tm));
828     Input_Date date;
829     char dateStr[20] = {0, };
830
831     InputPicker* inputPicker = static_cast<InputPicker*>(data);
832
833     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
834
835     date.day =  currentTime.tm_mday;
836     date.mon = currentTime.tm_mon;
837     date.year = currentTime.tm_year;
838
839     if(inputPicker->m_pickerLayout->popup){
840         evas_object_del(inputPicker->m_pickerLayout->popup);
841         inputPicker->m_pickerLayout->popup = 0;
842     }
843
844     date.year = 1900 + date.year;
845     sprintf(dateStr, "%d-%02d-%02d" , date.year, date.mon + 1, date.day);
846
847     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
848
849     delete inputPicker->m_pickerLayout;
850     inputPicker->m_pickerLayout = 0;
851 }
852
853 void InputPicker::_week_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
854 {
855     struct tm currentTime;
856     memset(&currentTime, 0, sizeof(struct tm));
857     Input_Date date;
858     char dateStr[20] = {0, };
859
860     InputPicker* inputPicker = static_cast<InputPicker*>(data);
861
862     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
863
864     date.day =  currentTime.tm_mday;
865     date.mon = currentTime.tm_mon;
866     date.year = currentTime.tm_year;
867     mktime(&currentTime);
868
869     if(inputPicker->m_pickerLayout->popup){
870         evas_object_del(inputPicker->m_pickerLayout->popup);
871         inputPicker->m_pickerLayout->popup = 0;
872     }
873
874     struct tm firtTimeOfyear;
875     memset(&firtTimeOfyear, 0, sizeof(struct tm));
876     firtTimeOfyear.tm_year = date.year;
877     firtTimeOfyear.tm_mon = 0;
878     firtTimeOfyear.tm_mday = 1;
879     mktime(&firtTimeOfyear);
880
881     char firstWeek[10] = {0, };
882     strftime(firstWeek, 10, "%w", &firtTimeOfyear);
883     int firstWeekCount = atoi(firstWeek);
884
885     char totalDay[10] = {0, };
886     strftime(totalDay, 10, "%j", &currentTime);
887     int totalDayCount = atoi(totalDay);
888
889     int weekNum = (totalDayCount + firstWeekCount) / 7;
890     if (totalDayCount + firstWeekCount > weekNum * 7)
891         weekNum++;
892
893     date.year = 1900 + date.year;
894     sprintf(dateStr, "%d-W%02d" , date.year, weekNum);
895
896     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
897
898     delete inputPicker->m_pickerLayout;
899     inputPicker->m_pickerLayout = 0;
900 }
901
902 void InputPicker::_time_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
903 {
904     struct tm currentTime;
905     memset(&currentTime, 0, sizeof(struct tm));
906     Input_Date date;
907     char dateStr[20] = {0, };
908
909     InputPicker* inputPicker = static_cast<InputPicker*>(data);
910
911     elm_datetime_value_get(inputPicker->m_pickerLayout->timePicker, &currentTime);
912
913     date.hour =  currentTime.tm_hour;
914     date.min = currentTime.tm_min;
915
916     if(inputPicker->m_pickerLayout->popup){
917         evas_object_del(inputPicker->m_pickerLayout->popup);
918         inputPicker->m_pickerLayout->popup = 0;
919     }
920
921     sprintf(dateStr, "%02d:%02d" , date.hour, date.min);
922     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
923
924     delete inputPicker->m_pickerLayout;
925     inputPicker->m_pickerLayout = 0;
926 }
927
928 void InputPicker::_month_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
929 {
930     struct tm currentTime;
931     memset(&currentTime, 0, sizeof(struct tm));
932     Input_Date date;
933     char dateStr[20] = {0, };
934
935     InputPicker* inputPicker = static_cast<InputPicker*>(data);
936
937     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
938
939     date.mon = currentTime.tm_mon;
940     date.year = currentTime.tm_year;
941
942     if(inputPicker->m_pickerLayout->popup){
943         evas_object_del(inputPicker->m_pickerLayout->popup);
944         inputPicker->m_pickerLayout->popup = 0;
945     }
946
947     date.year = 1900 + date.year;
948     sprintf(dateStr, "%02d-%02d" , date.year, date.mon + 1);
949
950     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
951
952     delete inputPicker->m_pickerLayout;
953     inputPicker->m_pickerLayout = 0;
954 }
955
956 void InputPicker::_datetime_popup_response_cb(void* data,  Evas_Object* obj, void* event_info)
957 {
958     struct tm currentTime;
959     memset(&currentTime, 0, sizeof(struct tm));
960     Input_Date date;
961     char dateStr[50] = {0, };
962
963     InputPicker* inputPicker = static_cast<InputPicker*>(data);
964
965     elm_datetime_value_get(inputPicker->m_pickerLayout->datePicker, &currentTime);
966     date.year = currentTime.tm_year;
967     date.day =  currentTime.tm_mday;
968     date.mon = currentTime.tm_mon;
969
970     elm_datetime_value_get(inputPicker->m_pickerLayout->timePicker, &currentTime);
971     date.hour =  currentTime.tm_hour;
972     date.min = currentTime.tm_min;
973
974     if (inputPicker->m_pickerLayout->popup) {
975         evas_object_del(inputPicker->m_pickerLayout->popup);
976         inputPicker->m_pickerLayout->popup = 0;
977     }
978
979     date.year = 1900 + date.year;
980
981     if (inputPicker->m_pickerLayout->datetimeLocal)
982         sprintf(dateStr, "%d-%02d-%02dT%02d:%02d" , date.year, date.mon + 1, date.day, date.hour, date.min);
983     else
984         sprintf(dateStr, "%d-%02d-%02dT%02d:%02dZ" , date.year, date.mon + 1, date.day, date.hour, date.min);
985
986     ewk_view_focused_input_element_value_set(inputPicker->m_ewkView, dateStr);
987
988     delete inputPicker->m_pickerLayout;
989     inputPicker->m_pickerLayout = 0;
990 }
991
992 } // namespace WebKit
993
994 #endif // OS(TIZEN) && ENABLE(TIZEN_INPUT_TAG_EXTENSION)