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->SetPropagatedKeyEventListener(this);
205 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(__pPopup->GetLayoutN()));
206 SysTryReturnResult(NID_WEB_CTRL, pLayout.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
210 pLayout->SetHorizontalAlignment(*__pEditDate, LAYOUT_HORIZONTAL_ALIGN_LEFT);
211 pLayout->SetSpacing(*__pEditDate, sideMargin);
216 pLayout->SetHorizontalAlignment(*__pEditTime, LAYOUT_HORIZONTAL_ALIGN_LEFT);
217 pLayout->SetSpacing(*__pEditTime, sideMargin);
220 pLayout->SetHorizontalAlignment(*__pSelectionBtn, LAYOUT_HORIZONTAL_ALIGN_CENTER);
221 pLayout->SetSpacing(*__pSelectionBtn, sideMargin);
228 _InputPickerPopup::Construct(const Color& color, Tizen::Web::Controls::_WebImpl* pImpl)
230 result r = E_SUCCESS;
232 __inputPickerMode = INPUT_MODE_COLOR;
234 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
236 CalculateColorPickerPopupSize(orientation);
239 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
241 __pPopup->SetTitleText("Select color");
243 std::unique_ptr<ColorPicker> pColorPicker(new (std::nothrow) ColorPicker());
244 SysTryReturnResult(NID_WEB_CTRL, pColorPicker.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
246 r = pColorPicker->Construct(Point(0,0));
247 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
249 pColorPicker->SetColor(color);
251 __pPopup->SetPropagatedKeyEventListener(this);
252 r = __pPopup->AddControl(*pColorPicker);
253 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
255 __pColorPicker = pColorPicker.release();
256 r = AddButton(ID_BUTTON_INPUT_COLOR_SELECTION);
257 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
259 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(__pPopup->GetLayoutN()));
260 SysTryReturnResult(NID_WEB_CTRL, pLayout.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
262 pLayout->SetHorizontalAlignment(*__pColorPicker, LAYOUT_HORIZONTAL_ALIGN_LEFT);
263 pLayout->SetHorizontalAlignment(*__pSelectionBtn, LAYOUT_HORIZONTAL_ALIGN_CENTER);
266 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
268 pLayout->SetSpacing(*__pColorPicker, sideMargin);
269 pLayout->SetSpacing(*__pSelectionBtn, sideMargin);
277 _InputPickerPopup::CalculateColorPickerPopupSize(_ControlOrientation orientation)
282 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
283 GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __popupWidth);
284 GET_SHAPE_CONFIG(MESSAGEBOX::MIN_HEIGHT, orientation, __popupHeight);
285 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __btnHeight);
287 GET_DIMENSION_CONFIG(COLORPICKER::DEFAULT_SIZE, orientation, dim);
289 dim.width += sideMargin;
290 __popupWidth = dim.width;
291 __popupHeight = __popupHeight + dim.height + __btnHeight;
296 _InputPickerPopup::CreatePopup(void)
298 result r = E_SUCCESS;
300 VerticalBoxLayout layout;
301 r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
302 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
304 std::unique_ptr<Popup> pPopup(new (std::nothrow) Popup());
305 SysTryReturnResult(NID_WEB_CTRL, pPopup.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
307 r = pPopup->Construct(layout, layout, true, Dimension(__popupWidth, __popupHeight));
308 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
310 __pPopup = std::move(pPopup);
317 _InputPickerPopup::AddButton(_InputPickerButtonId buttonId)
319 result r = E_SUCCESS;
323 _SystemResource* pSysResource = _SystemResource::GetInstance();
324 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
326 buttonStr = pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_OK");
328 std::unique_ptr<Button> pSelectionBtn(new (std::nothrow) Button());
329 SysTryReturnResult(NID_WEB_CTRL, pSelectionBtn.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
331 r = pSelectionBtn->Construct(Rectangle(0, 0, DATE_POPUP_BUTTON_WIDTH, __btnHeight), buttonStr);
332 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
334 pSelectionBtn->SetActionId(buttonId);
335 __pPopup->AddControl(*pSelectionBtn);
336 pSelectionBtn->AddActionEventListener(*this);
338 __pSelectionBtn = pSelectionBtn.release();
345 _InputPickerPopup::OnActionPerformed(const Control& source, int actionId)
347 result r = E_SUCCESS;
351 case ID_BUTTON_INPUT_DATE_SELECTION:
355 case ID_BUTTON_INPUT_COLOR_SELECTION:
365 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
370 _InputPickerPopup::ChangeLayout(_ControlOrientation orientation)
372 result r = E_SUCCESS;
374 if (__inputPickerMode != INPUT_MODE_COLOR) //Change the layout for color only.
381 Dimension screenRect = _ControlManager::GetInstance()->GetScreenSize();
383 CalculateColorPickerPopupSize(orientation);
385 if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
387 x = (screenRect.width - __popupWidth) / 2;
388 y = (screenRect.height - __popupHeight) / 2;
392 x = (screenRect.height - __popupWidth) / 2;
393 y = (screenRect.width - __popupHeight) / 2;
396 r = __pPopup->SetBounds(Rectangle(x, y, __popupWidth, __popupHeight));
397 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
404 _InputPickerPopup::UpdateDate(void)
406 result r = E_SUCCESS;
408 String formattedString;
411 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
412 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
416 case EWK_INPUT_TYPE_TIME :
417 formattedString = L"HH:mm";
418 pDateFormatter->ApplyPattern(formattedString);
419 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
422 case EWK_INPUT_TYPE_DATETIME :
423 formattedString = L"yyyy-MM-dd";
424 pDateFormatter->ApplyPattern(formattedString);
425 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
427 formattedString = L"\'T\'HH:mm\'Z\'";
428 pDateFormatter->ApplyPattern(formattedString);
429 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
432 case EWK_INPUT_TYPE_DATETIMELOCAL :
433 formattedString = L"yyyy-MM-dd";
434 pDateFormatter->ApplyPattern(formattedString);
435 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
437 formattedString = L"\'T\'HH:mm";
438 pDateFormatter->ApplyPattern(formattedString);
439 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
442 case EWK_INPUT_TYPE_WEEK :
443 formattedString = L"yyyy-\'W\'ww";
444 pDateFormatter->ApplyPattern(formattedString);
445 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
449 case EWK_INPUT_TYPE_DATE :
450 formattedString = L"yyyy-MM-dd";
451 pDateFormatter->ApplyPattern(formattedString);
452 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
455 case EWK_INPUT_TYPE_MONTH :
456 formattedString = L"yyyy-MM";
457 pDateFormatter->ApplyPattern(formattedString);
458 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
462 r = E_UNSUPPORTED_OPTION;
463 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
472 _InputPickerPopup::UpdateColor(void)
474 __Color = __pColorPicker->GetColor();
479 _InputPickerPopup::ShowPopup(void)
481 result r = E_SUCCESS;
483 r = __pPopup->SetShowState(true);
484 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
488 r = __pPopup->DoModal(__modal);
489 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
496 _InputPickerPopup::HidePopup(void)
498 result r = E_SUCCESS;
500 r = __pPopup->SetShowState(false);
501 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
505 r = __pPopup->EndModal(__modal);
506 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
512 _InputPickerPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
518 _InputPickerPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
520 if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
529 _InputPickerPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
535 _InputPickerPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
541 _InputPickerPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
547 _InputPickerPopup::GetDate(void) const
554 _InputPickerPopup::GetColor(void) const
561 _InputPickerPopup::Parse(const String& strDateTime, DateTime & dateTime)
563 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
564 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
574 result r = E_SUCCESS;
576 String formattedString = L"e";
579 const wchar_t* pMchar = static_cast< const wchar_t* >(strDateTime.GetPointer());
580 wchar_t* pTmp = static_cast< wchar_t* >(malloc(sizeof(wchar_t) * 10));
581 SysTryReturnResult(NID_WEB_CTRL, pTmp, E_OUT_OF_MEMORY, "Memory allocation failed.");
585 case EWK_INPUT_TYPE_TIME : //"HH:mm";
588 wcsncpy(pTmp, pMchar, 2);
590 hour = static_cast< int >(wcstol(pTmp, null, 10));
593 wcsncpy(pTmp, pMchar + 3, 2);
595 minute = static_cast< int >(wcstol(pTmp, null, 10));
596 dateTime.SetValue(1, 1, 1, hour,minute);
599 case EWK_INPUT_TYPE_DATETIME : // "yyyy-MM-ddTHH:mmZ";
601 case EWK_INPUT_TYPE_DATETIMELOCAL :
603 wcsncpy(pTmp, pMchar, 4);
606 year = static_cast< int >(wcstol(pTmp, null, 10));
609 wcsncpy(pTmp, pMchar + 5, 2);
612 month = static_cast< int >(wcstol(pTmp, null, 10));
615 wcsncpy(pTmp, pMchar + 8, 2);
618 day = static_cast< int >(wcstol(pTmp, null, 10));
621 wcsncpy(pTmp, pMchar + 11, 2);
624 hour = static_cast< int >(wcstol(pTmp, null, 10));
627 wcsncpy(pTmp, pMchar + 14, 2);
629 minute = static_cast< int >(wcstol(pTmp, null, 10));
630 dateTime.SetValue(year, month, day, hour, minute);
633 case EWK_INPUT_TYPE_WEEK : //"yyyy-Www";
636 wcsncpy(pTmp, pMchar, 4);
639 year = static_cast< int >(wcstol(pTmp, null, 10));
642 wcsncpy(pTmp, pMchar + 6, 2);
644 week = static_cast< int >(wcstol(pTmp, null, 10));
646 dateTime.SetValue(year,1,1);
647 pDateFormatter->ApplyPattern(formattedString);
648 pDateFormatter->Format(dateTime, dayno);
649 Integer::Parse(dayno, day);
650 days = (week*7) - day;
652 GetDateTimeForWeek(year, days, dateTime);
655 case EWK_INPUT_TYPE_DATE : //"yyyy-MM-dd"
657 wcsncpy(pTmp, pMchar, 4);
659 year = static_cast< int >(wcstol(pTmp, null, 10));
662 wcsncpy(pTmp, pMchar + 5, 2);
664 month = static_cast< int >(wcstol(pTmp, null, 10));
667 wcsncpy(pTmp, pMchar + 8, 2);
669 day = static_cast< int >(wcstol(pTmp, null, 10));
671 dateTime.SetValue(year, month, day);
674 case EWK_INPUT_TYPE_MONTH : //"yyyy-MM";
677 wcsncpy(pTmp, pMchar, 4);
679 year = static_cast< int >(wcstol(pTmp, null, 10));
682 wcsncpy(pTmp, pMchar + 5, 2);
684 month = static_cast< int >(wcstol(pTmp, null, 10));
686 dateTime.SetValue(year, month, 1);
690 r = E_UNSUPPORTED_OPTION;
691 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
701 _InputPickerPopup::GetDateTimeForWeek(int year, int days, DateTime& dateTime)
705 for (int month = 1; month <= 12; month++)
707 DateTime::GetDaysInMonth(year, month, monthDays);
709 if (days - monthDays < 0)
711 dateTime.SetValue(year, month, days);
720 }}} // Tizen::Web::Controls