2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FWebCtrl_DateTimeHandler.cpp
20 * @brief The file contains the definition of _DateTimeHandler class.
22 #include <FBaseDateTime.h>
23 #include <FBaseSysLog.h>
24 #include <FGrpDimension.h>
25 #include <FGrpRectangle.h>
26 #include <FLclDateTimeFormatter.h>
27 #include <FLclLocale.h>
29 #include <FUiCtrlButton.h>
30 #include <FUiCtrlEditField.h>
31 #include <FUiCtrlEditDate.h>
32 #include <FUiCtrlEditTime.h>
33 #include <FUiCtrlLabel.h>
34 #include <FUiIActionEventListener.h>
35 #include <FUiKeyEventInfo.h>
36 #include <FUiLayout.h>
37 #include <FUiVerticalBoxLayout.h>
38 #include <FWebCtrlAuthenticationChallenge.h>
39 #include <FSys_SystemResource.h>
40 #include <FUi_ResourceManager.h>
41 #include "FWebCtrl_WebImpl.h"
42 #include "FWebCtrl_InputPickerPopup.h"
45 using namespace Tizen::Base;
46 using namespace Tizen::Graphics;
47 using namespace Tizen::Locales;
48 using namespace Tizen::System;
49 using namespace Tizen::Ui;
50 using namespace Tizen::Ui::Controls;
53 namespace Tizen { namespace Web { namespace Controls
57 static const int DATE_POPUP_BUTTON_WIDTH = 250;
60 _InputPickerPopup::_InputPickerPopup(void)
64 , __pSelectionBtn(null)
70 , __inputType(EWK_INPUT_TYPE_TIME)
75 _InputPickerPopup::~_InputPickerPopup(void)
77 if (__isModal == true)
85 _InputPickerPopup::Construct(const String& strDate, Ewk_Input_Type inputType, Tizen::Web::Controls::_WebImpl* pImpl)
93 DateTime inputDateTime;
95 __inputPickerMode = INPUT_MODE_DATE;
96 __inputType = inputType;
98 SystemTime::GetCurrentTime(UTC_TIME, inputDateTime);
100 if (!strDate.IsEmpty())
102 r = Parse(strDate, inputDateTime);
105 _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
107 GET_SHAPE_CONFIG(EDITDATE::WIDTH, orientation, dateWidth);
108 GET_SHAPE_CONFIG(EDITDATE::HEIGHT, orientation, dateHeight);
109 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
110 GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __popupWidth);
111 GET_SHAPE_CONFIG(MESSAGEBOX::MIN_HEIGHT, orientation, __popupHeight);
112 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __btnHeight);
114 __popupWidth -= 2*sideMargin;
116 std::unique_ptr<EditDate> pEditDate(new (std::nothrow) EditDate());
117 SysTryReturnResult(NID_WEB_CTRL, pEditDate.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
119 std::unique_ptr<EditTime> pEditTime(new (std::nothrow) EditTime());
120 SysTryReturnResult(NID_WEB_CTRL, pEditTime.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
124 case EWK_INPUT_TYPE_TIME :
126 __popupHeight = __popupHeight + dateHeight + __btnHeight;
128 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
130 __pPopup->SetTitleText("Select time");
132 r = pEditTime->Construct(Point(0, 0), L"Time (Default format) :");
133 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
135 r = __pPopup->AddControl(*pEditTime);
136 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
137 pEditTime->SetTime(inputDateTime);
138 __pEditTime = pEditTime.release();
141 case EWK_INPUT_TYPE_DATETIME :
144 case EWK_INPUT_TYPE_DATETIMELOCAL :
146 __popupHeight = __popupHeight + 2*dateHeight + __btnHeight;
148 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150 __pPopup->SetTitleText("Select datetime");
152 r = pEditDate->Construct(Point(0, 0), L"Date (Default format) :");
153 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
155 r = pEditTime->Construct(Point(0, 0), L"Time (Default format) :");
156 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
158 r = __pPopup->AddControl(*pEditTime);
159 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
161 pEditTime->SetTime(inputDateTime);
162 __pEditTime = pEditTime.release();
164 r = __pPopup->AddControl(*pEditDate);
165 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
167 pEditDate->SetDate(inputDateTime);
168 __pEditDate = pEditDate.release();
170 dateHeight = 2 * dateHeight;
173 case EWK_INPUT_TYPE_WEEK :
175 case EWK_INPUT_TYPE_DATE :
177 case EWK_INPUT_TYPE_MONTH :
179 __popupHeight = __popupHeight + dateHeight + __btnHeight;
181 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
182 __pPopup->SetTitleText("Select date");
184 r = pEditDate->Construct(Point(0, 0), L"Date (Default format) :");
185 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
187 r = __pPopup->AddControl(*pEditDate);
188 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
190 pEditDate->SetDate(inputDateTime);
191 __pEditDate = pEditDate.release();
195 r = E_UNSUPPORTED_OPTION;
196 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
200 r = AddButton(ID_BUTTON_INPUT_DATE_SELECTION);
201 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
203 __pPopup->SetOwner(&pImpl->GetPublic());
204 __pPopup->SetPropagatedKeyEventListener(this);
206 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(__pPopup->GetLayoutN()));
207 SysTryReturnResult(NID_WEB_CTRL, pLayout.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
211 pLayout->SetHorizontalAlignment(*__pEditDate, LAYOUT_HORIZONTAL_ALIGN_LEFT);
212 pLayout->SetSpacing(*__pEditDate, sideMargin);
217 pLayout->SetHorizontalAlignment(*__pEditTime, LAYOUT_HORIZONTAL_ALIGN_LEFT);
218 pLayout->SetSpacing(*__pEditTime, sideMargin);
221 pLayout->SetHorizontalAlignment(*__pSelectionBtn, LAYOUT_HORIZONTAL_ALIGN_CENTER);
222 pLayout->SetSpacing(*__pSelectionBtn, sideMargin);
229 _InputPickerPopup::Construct(const Color& color, Tizen::Web::Controls::_WebImpl* pImpl)
231 result r = E_SUCCESS;
233 __inputPickerMode = INPUT_MODE_COLOR;
235 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
237 CalculateColorPickerPopupSize(orientation);
240 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
242 __pPopup->SetTitleText("Select color");
244 std::unique_ptr<ColorPicker> pColorPicker(new (std::nothrow) ColorPicker());
245 SysTryReturnResult(NID_WEB_CTRL, pColorPicker.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
247 r = pColorPicker->Construct(Point(0,0));
248 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
250 pColorPicker->SetColor(color);
252 __pPopup->SetOwner(&pImpl->GetPublic());
253 __pPopup->SetPropagatedKeyEventListener(this);
254 r = __pPopup->AddControl(*pColorPicker);
255 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
257 __pColorPicker = pColorPicker.release();
258 r = AddButton(ID_BUTTON_INPUT_COLOR_SELECTION);
259 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
261 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(__pPopup->GetLayoutN()));
262 SysTryReturnResult(NID_WEB_CTRL, pLayout.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
264 pLayout->SetHorizontalAlignment(*__pColorPicker, LAYOUT_HORIZONTAL_ALIGN_LEFT);
265 pLayout->SetHorizontalAlignment(*__pSelectionBtn, LAYOUT_HORIZONTAL_ALIGN_CENTER);
268 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
270 pLayout->SetSpacing(*__pColorPicker, sideMargin);
271 pLayout->SetSpacing(*__pSelectionBtn, sideMargin);
279 _InputPickerPopup::CalculateColorPickerPopupSize(_ControlOrientation orientation)
284 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
285 GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __popupWidth);
286 GET_SHAPE_CONFIG(MESSAGEBOX::MIN_HEIGHT, orientation, __popupHeight);
287 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __btnHeight);
289 GET_DIMENSION_CONFIG(COLORPICKER::DEFAULT_SIZE, orientation, dim);
291 dim.width += sideMargin;
292 __popupWidth = dim.width;
293 __popupHeight = __popupHeight + dim.height + __btnHeight;
298 _InputPickerPopup::CreatePopup(void)
300 result r = E_SUCCESS;
302 VerticalBoxLayout layout;
303 r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
304 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
306 std::unique_ptr<Popup> pPopup(new (std::nothrow) Popup());
307 SysTryReturnResult(NID_WEB_CTRL, pPopup.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
309 r = pPopup->Construct(layout, layout, true, Dimension(__popupWidth, __popupHeight));
310 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
312 __pPopup = std::move(pPopup);
319 _InputPickerPopup::AddButton(_InputPickerButtonId buttonId)
321 result r = E_SUCCESS;
325 _SystemResource* pSysResource = _SystemResource::GetInstance();
326 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
328 buttonStr = pSysResource->GetString("sys_string", "IDS_COM_SK_OK");
330 std::unique_ptr<Button> pSelectionBtn(new (std::nothrow) Button());
331 SysTryReturnResult(NID_WEB_CTRL, pSelectionBtn.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
333 r = pSelectionBtn->Construct(Rectangle(0, 0, DATE_POPUP_BUTTON_WIDTH, __btnHeight), buttonStr);
334 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
336 pSelectionBtn->SetActionId(buttonId);
337 __pPopup->AddControl(*pSelectionBtn);
338 pSelectionBtn->AddActionEventListener(*this);
340 __pSelectionBtn = pSelectionBtn.release();
347 _InputPickerPopup::OnActionPerformed(const Control& source, int actionId)
349 result r = E_SUCCESS;
353 case ID_BUTTON_INPUT_DATE_SELECTION:
357 case ID_BUTTON_INPUT_COLOR_SELECTION:
367 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
372 _InputPickerPopup::ChangeLayout(_ControlOrientation orientation)
374 result r = E_SUCCESS;
376 if (__inputPickerMode != INPUT_MODE_COLOR) //Change the layout for color only.
383 Dimension screenRect = _ControlManager::GetInstance()->GetScreenSize();
385 CalculateColorPickerPopupSize(orientation);
387 if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
389 x = (screenRect.width - __popupWidth) / 2;
390 y = (screenRect.height - __popupHeight) / 2;
394 x = (screenRect.height - __popupWidth) / 2;
395 y = (screenRect.width - __popupHeight) / 2;
398 r = __pPopup->SetBounds(Rectangle(x, y, __popupWidth, __popupHeight));
399 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
406 _InputPickerPopup::UpdateDate(void)
408 result r = E_SUCCESS;
410 String formattedString;
413 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
414 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
418 case EWK_INPUT_TYPE_TIME :
419 formattedString = L"HH:mm";
420 pDateFormatter->ApplyPattern(formattedString);
421 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
424 case EWK_INPUT_TYPE_DATETIME :
425 formattedString = L"yyyy-MM-dd";
426 pDateFormatter->ApplyPattern(formattedString);
427 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
429 formattedString = L"\'T\'HH:mm\'Z\'";
430 pDateFormatter->ApplyPattern(formattedString);
431 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
434 case EWK_INPUT_TYPE_DATETIMELOCAL :
435 formattedString = L"yyyy-MM-dd";
436 pDateFormatter->ApplyPattern(formattedString);
437 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
439 formattedString = L"\'T\'HH:mm";
440 pDateFormatter->ApplyPattern(formattedString);
441 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
444 case EWK_INPUT_TYPE_WEEK :
445 formattedString = L"yyyy-\'W\'ww";
446 pDateFormatter->ApplyPattern(formattedString);
447 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
451 case EWK_INPUT_TYPE_DATE :
452 formattedString = L"yyyy-MM-dd";
453 pDateFormatter->ApplyPattern(formattedString);
454 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
457 case EWK_INPUT_TYPE_MONTH :
458 formattedString = L"yyyy-MM";
459 pDateFormatter->ApplyPattern(formattedString);
460 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
464 r = E_UNSUPPORTED_OPTION;
465 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
474 _InputPickerPopup::UpdateColor(void)
476 __Color = __pColorPicker->GetColor();
481 _InputPickerPopup::ShowPopup(void)
483 result r = E_SUCCESS;
485 r = __pPopup->SetShowState(true);
486 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
490 r = __pPopup->DoModal(__modal);
491 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
498 _InputPickerPopup::HidePopup(void)
500 result r = E_SUCCESS;
502 r = __pPopup->SetShowState(false);
503 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
507 r = __pPopup->EndModal(__modal);
508 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
514 _InputPickerPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
520 _InputPickerPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
522 if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
531 _InputPickerPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
537 _InputPickerPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
543 _InputPickerPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
549 _InputPickerPopup::GetDate(void) const
556 _InputPickerPopup::GetColor(void) const
563 _InputPickerPopup::Parse(const String& strDateTime, DateTime & dateTime)
565 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
566 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
576 result r = E_SUCCESS;
578 String formattedString = L"e";
581 const wchar_t* pMchar = static_cast< const wchar_t* >(strDateTime.GetPointer());
582 wchar_t* pTmp = static_cast< wchar_t* >(malloc(sizeof(wchar_t) * 10));
583 SysTryReturnResult(NID_WEB_CTRL, pTmp, E_OUT_OF_MEMORY, "Memory allocation failed.");
587 case EWK_INPUT_TYPE_TIME : //"HH:mm";
590 wcsncpy(pTmp, pMchar, 2);
592 hour = static_cast< int >(wcstol(pTmp, null, 10));
595 wcsncpy(pTmp, pMchar + 3, 2);
597 minute = static_cast< int >(wcstol(pTmp, null, 10));
598 dateTime.SetValue(1, 1, 1, hour,minute);
601 case EWK_INPUT_TYPE_DATETIME : // "yyyy-MM-ddTHH:mmZ";
603 case EWK_INPUT_TYPE_DATETIMELOCAL :
605 wcsncpy(pTmp, pMchar, 4);
608 year = static_cast< int >(wcstol(pTmp, null, 10));
611 wcsncpy(pTmp, pMchar + 5, 2);
614 month = static_cast< int >(wcstol(pTmp, null, 10));
617 wcsncpy(pTmp, pMchar + 8, 2);
620 day = static_cast< int >(wcstol(pTmp, null, 10));
623 wcsncpy(pTmp, pMchar + 11, 2);
626 hour = static_cast< int >(wcstol(pTmp, null, 10));
629 wcsncpy(pTmp, pMchar + 14, 2);
631 minute = static_cast< int >(wcstol(pTmp, null, 10));
632 dateTime.SetValue(year, month, day, hour, minute);
635 case EWK_INPUT_TYPE_WEEK : //"yyyy-Www";
638 wcsncpy(pTmp, pMchar, 4);
641 year = static_cast< int >(wcstol(pTmp, null, 10));
644 wcsncpy(pTmp, pMchar + 6, 2);
646 week = static_cast< int >(wcstol(pTmp, null, 10));
648 dateTime.SetValue(year,1,1);
649 pDateFormatter->ApplyPattern(formattedString);
650 pDateFormatter->Format(dateTime, dayno);
651 Integer::Parse(dayno, day);
652 days = (week*7) - day;
654 GetDateTimeForWeek(year, days, dateTime);
657 case EWK_INPUT_TYPE_DATE : //"yyyy-MM-dd"
659 wcsncpy(pTmp, pMchar, 4);
661 year = static_cast< int >(wcstol(pTmp, null, 10));
664 wcsncpy(pTmp, pMchar + 5, 2);
666 month = static_cast< int >(wcstol(pTmp, null, 10));
669 wcsncpy(pTmp, pMchar + 8, 2);
671 day = static_cast< int >(wcstol(pTmp, null, 10));
673 dateTime.SetValue(year, month, day);
676 case EWK_INPUT_TYPE_MONTH : //"yyyy-MM";
679 wcsncpy(pTmp, pMchar, 4);
681 year = static_cast< int >(wcstol(pTmp, null, 10));
684 wcsncpy(pTmp, pMchar + 5, 2);
686 month = static_cast< int >(wcstol(pTmp, null, 10));
688 dateTime.SetValue(year, month, 1);
692 r = E_UNSUPPORTED_OPTION;
693 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
703 _InputPickerPopup::GetDateTimeForWeek(int year, int days, DateTime& dateTime)
707 for (int month = 1; month <= 12; month++)
709 DateTime::GetDaysInMonth(year, month, monthDays);
711 if (days - monthDays < 0)
713 dateTime.SetValue(year, month, days);
722 }}} // Tizen::Web::Controls