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 <FUiLayout.h>
36 #include <FUiVerticalBoxLayout.h>
37 #include <FWebCtrlAuthenticationChallenge.h>
38 #include <FSys_SystemResource.h>
39 #include <FUi_ResourceManager.h>
40 #include "FWebCtrl_WebImpl.h"
41 #include "FWebCtrl_InputPickerPopup.h"
44 using namespace Tizen::Base;
45 using namespace Tizen::Graphics;
46 using namespace Tizen::Locales;
47 using namespace Tizen::System;
48 using namespace Tizen::Ui;
49 using namespace Tizen::Ui::Controls;
52 namespace Tizen { namespace Web { namespace Controls
56 static const int DATE_POPUP_BUTTON_WIDTH = 250;
59 _InputPickerPopup::_InputPickerPopup(void)
63 , __pSelectionBtn(null)
68 , __inputType(EWK_INPUT_TYPE_TIME)
73 _InputPickerPopup::~_InputPickerPopup(void)
79 _InputPickerPopup::Construct(const String& strDate, Ewk_Input_Type inputType)
87 DateTime inputDateTime;
89 __inputPickerMode = INPUT_MODE_DATE;
90 __inputType = inputType;
92 SystemTime::GetCurrentTime(UTC_TIME, inputDateTime);
94 if (!strDate.IsEmpty())
96 r = Parse(strDate, inputDateTime);
99 _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
101 GET_SHAPE_CONFIG(EDITDATE::WIDTH, orientation, dateWidth);
102 GET_SHAPE_CONFIG(EDITDATE::HEIGHT, orientation, dateHeight);
103 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
104 GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __popupWidth);
105 GET_SHAPE_CONFIG(MESSAGEBOX::MIN_HEIGHT, orientation, __popupHeight);
106 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __btnHeight);
108 __popupWidth -= 2*sideMargin;
110 std::unique_ptr<EditDate> pEditDate(new (std::nothrow) EditDate());
111 SysTryReturnResult(NID_WEB_CTRL, pEditDate.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
113 std::unique_ptr<EditTime> pEditTime(new (std::nothrow) EditTime());
114 SysTryReturnResult(NID_WEB_CTRL, pEditTime.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
118 case EWK_INPUT_TYPE_TIME :
120 __popupHeight = __popupHeight + dateHeight + __btnHeight;
122 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
124 __pPopup->SetTitleText("Select time");
126 r = pEditTime->Construct(Point(0, 0), L"Time (Default format) :");
127 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
129 r = __pPopup->AddControl(*pEditTime);
130 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
131 pEditTime->SetTime(inputDateTime);
132 __pEditTime = pEditTime.release();
135 case EWK_INPUT_TYPE_DATETIME :
138 case EWK_INPUT_TYPE_DATETIMELOCAL :
140 __popupHeight = __popupHeight + 2*dateHeight + __btnHeight;
142 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
144 __pPopup->SetTitleText("Select datetime");
146 r = pEditDate->Construct(Point(0, 0), L"Date (Default format) :");
147 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
149 r = pEditTime->Construct(Point(0, 0), L"Time (Default format) :");
150 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
152 r = __pPopup->AddControl(*pEditTime);
153 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
155 pEditTime->SetTime(inputDateTime);
156 __pEditTime = pEditTime.release();
158 r = __pPopup->AddControl(*pEditDate);
159 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
161 pEditDate->SetDate(inputDateTime);
162 __pEditDate = pEditDate.release();
164 dateHeight = 2 * dateHeight;
167 case EWK_INPUT_TYPE_WEEK :
169 case EWK_INPUT_TYPE_DATE :
171 case EWK_INPUT_TYPE_MONTH :
173 __popupHeight = __popupHeight + dateHeight + __btnHeight;
175 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
176 __pPopup->SetTitleText("Select date");
178 r = pEditDate->Construct(Point(0, 0), L"Date (Default format) :");
179 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
181 r = __pPopup->AddControl(*pEditDate);
182 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
184 pEditDate->SetDate(inputDateTime);
185 __pEditDate = pEditDate.release();
189 r = E_UNSUPPORTED_OPTION;
190 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
194 r = AddButton(ID_BUTTON_INPUT_DATE_SELECTION);
195 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
197 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(__pPopup->GetLayoutN()));
198 SysTryReturnResult(NID_WEB_CTRL, pLayout.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
202 pLayout->SetHorizontalAlignment(*__pEditDate, LAYOUT_HORIZONTAL_ALIGN_LEFT);
203 pLayout->SetSpacing(*__pEditDate, sideMargin);
208 pLayout->SetHorizontalAlignment(*__pEditTime, LAYOUT_HORIZONTAL_ALIGN_LEFT);
209 pLayout->SetSpacing(*__pEditTime, sideMargin);
212 pLayout->SetHorizontalAlignment(*__pSelectionBtn, LAYOUT_HORIZONTAL_ALIGN_CENTER);
213 pLayout->SetSpacing(*__pSelectionBtn, sideMargin);
220 _InputPickerPopup::Construct(const Color& color)
222 result r = E_SUCCESS;
224 __inputPickerMode = INPUT_MODE_COLOR;
226 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
228 CalculateColorPickerPopupSize(orientation);
231 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
233 __pPopup->SetTitleText("Select color");
235 std::unique_ptr<ColorPicker> pColorPicker(new (std::nothrow) ColorPicker());
236 SysTryReturnResult(NID_WEB_CTRL, pColorPicker.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
238 r = pColorPicker->Construct(Point(0,0));
239 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
241 pColorPicker->SetColor(color);
243 r = __pPopup->AddControl(*pColorPicker);
244 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
246 __pColorPicker = pColorPicker.release();
247 r = AddButton(ID_BUTTON_INPUT_COLOR_SELECTION);
248 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
250 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(__pPopup->GetLayoutN()));
251 SysTryReturnResult(NID_WEB_CTRL, pLayout.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
253 pLayout->SetHorizontalAlignment(*__pColorPicker, LAYOUT_HORIZONTAL_ALIGN_LEFT);
254 pLayout->SetHorizontalAlignment(*__pSelectionBtn, LAYOUT_HORIZONTAL_ALIGN_CENTER);
257 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
259 pLayout->SetSpacing(*__pColorPicker, sideMargin);
260 pLayout->SetSpacing(*__pSelectionBtn, sideMargin);
268 _InputPickerPopup::CalculateColorPickerPopupSize(_ControlOrientation orientation)
273 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
274 GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __popupWidth);
275 GET_SHAPE_CONFIG(MESSAGEBOX::MIN_HEIGHT, orientation, __popupHeight);
276 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __btnHeight);
278 GET_DIMENSION_CONFIG(COLORPICKER::DEFAULT_SIZE, orientation, dim);
280 dim.width += sideMargin;
281 __popupWidth = dim.width;
282 __popupHeight = __popupHeight + dim.height + __btnHeight;
287 _InputPickerPopup::CreatePopup(void)
289 result r = E_SUCCESS;
291 VerticalBoxLayout layout;
292 r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
293 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
295 std::unique_ptr<Popup> pPopup(new (std::nothrow) Popup());
296 SysTryReturnResult(NID_WEB_CTRL, pPopup.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
298 r = pPopup->Construct(layout, layout, true, Dimension(__popupWidth, __popupHeight));
299 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
301 __pPopup = std::move(pPopup);
308 _InputPickerPopup::AddButton(_InputPickerButtonId buttonId)
310 result r = E_SUCCESS;
314 _SystemResource* pSysResource = _SystemResource::GetInstance();
315 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
317 buttonStr = pSysResource->GetString("sys_string", "IDS_COM_SK_OK");
319 std::unique_ptr<Button> pSelectionBtn(new (std::nothrow) Button());
320 SysTryReturnResult(NID_WEB_CTRL, pSelectionBtn.get(), E_OUT_OF_MEMORY, "Memory Allocation Failed.");
322 r = pSelectionBtn->Construct(Rectangle(0, 0, DATE_POPUP_BUTTON_WIDTH, __btnHeight), buttonStr);
323 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
325 pSelectionBtn->SetActionId(buttonId);
326 __pPopup->AddControl(*pSelectionBtn);
327 pSelectionBtn->AddActionEventListener(*this);
329 __pSelectionBtn = pSelectionBtn.release();
336 _InputPickerPopup::OnActionPerformed(const Control& source, int actionId)
338 result r = E_SUCCESS;
342 case ID_BUTTON_INPUT_DATE_SELECTION:
346 case ID_BUTTON_INPUT_COLOR_SELECTION:
356 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
361 _InputPickerPopup::ChangeLayout(_ControlOrientation orientation)
363 result r = E_SUCCESS;
365 if (__inputPickerMode != INPUT_MODE_COLOR) //Change the layout for color only.
372 Dimension screenRect = _ControlManager::GetInstance()->GetScreenSize();
374 CalculateColorPickerPopupSize(orientation);
376 if (orientation == _CONTROL_ORIENTATION_PORTRAIT)
378 x = (screenRect.width - __popupWidth) / 2;
379 y = (screenRect.height - __popupHeight) / 2;
383 x = (screenRect.height - __popupWidth) / 2;
384 y = (screenRect.width - __popupHeight) / 2;
387 r = __pPopup->SetBounds(Rectangle(x, y, __popupWidth, __popupHeight));
388 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
395 _InputPickerPopup::UpdateDate(void)
397 result r = E_SUCCESS;
399 String formattedString;
402 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
403 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
407 case EWK_INPUT_TYPE_TIME :
408 formattedString = L"HH:mm";
409 pDateFormatter->ApplyPattern(formattedString);
410 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
413 case EWK_INPUT_TYPE_DATETIME :
414 formattedString = L"yyyy-MM-dd";
415 pDateFormatter->ApplyPattern(formattedString);
416 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
418 formattedString = L"\'T\'HH:mm\'Z\'";
419 pDateFormatter->ApplyPattern(formattedString);
420 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
423 case EWK_INPUT_TYPE_DATETIMELOCAL :
424 formattedString = L"yyyy-MM-dd";
425 pDateFormatter->ApplyPattern(formattedString);
426 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
428 formattedString = L"\'T\'HH:mm";
429 pDateFormatter->ApplyPattern(formattedString);
430 pDateFormatter->Format(__pEditTime->GetTime(), __dateStr);
433 case EWK_INPUT_TYPE_WEEK :
434 formattedString = L"yyyy-\'W\'ww";
435 pDateFormatter->ApplyPattern(formattedString);
436 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
440 case EWK_INPUT_TYPE_DATE :
441 formattedString = L"yyyy-MM-dd";
442 pDateFormatter->ApplyPattern(formattedString);
443 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
446 case EWK_INPUT_TYPE_MONTH :
447 formattedString = L"yyyy-MM";
448 pDateFormatter->ApplyPattern(formattedString);
449 pDateFormatter->Format(__pEditDate->GetDate(), __dateStr);
453 r = E_UNSUPPORTED_OPTION;
454 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
463 _InputPickerPopup::UpdateColor(void)
465 __Color = __pColorPicker->GetColor();
470 _InputPickerPopup::ShowPopup(void)
472 result r = E_SUCCESS;
474 r = __pPopup->SetShowState(true);
475 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
477 r = __pPopup->DoModal(__modal);
478 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
485 _InputPickerPopup::HidePopup(void)
487 result r = E_SUCCESS;
489 r = __pPopup->SetShowState(false);
490 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
492 r = __pPopup->EndModal(__modal);
493 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
500 _InputPickerPopup::GetDate(void) const
507 _InputPickerPopup::GetColor(void) const
514 _InputPickerPopup::Parse(const String& strDateTime, DateTime & dateTime)
516 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
517 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
527 result r = E_SUCCESS;
529 String formattedString = L"e";
532 const wchar_t* pMchar = static_cast< const wchar_t* >(strDateTime.GetPointer());
533 wchar_t* pTmp = static_cast< wchar_t* >(malloc(sizeof(wchar_t) * 10));
534 SysTryReturnResult(NID_WEB_CTRL, pTmp, E_OUT_OF_MEMORY, "Memory allocation failed.");
538 case EWK_INPUT_TYPE_TIME : //"HH:mm";
541 wcsncpy(pTmp, pMchar, 2);
543 hour = static_cast< int >(wcstol(pTmp, null, 10));
546 wcsncpy(pTmp, pMchar + 3, 2);
548 minute = static_cast< int >(wcstol(pTmp, null, 10));
549 dateTime.SetValue(1, 1, 1, hour,minute);
552 case EWK_INPUT_TYPE_DATETIME : // "yyyy-MM-ddTHH:mmZ";
554 case EWK_INPUT_TYPE_DATETIMELOCAL :
556 wcsncpy(pTmp, pMchar, 4);
559 year = static_cast< int >(wcstol(pTmp, null, 10));
562 wcsncpy(pTmp, pMchar + 5, 2);
565 month = static_cast< int >(wcstol(pTmp, null, 10));
568 wcsncpy(pTmp, pMchar + 8, 2);
571 day = static_cast< int >(wcstol(pTmp, null, 10));
574 wcsncpy(pTmp, pMchar + 11, 2);
577 hour = static_cast< int >(wcstol(pTmp, null, 10));
580 wcsncpy(pTmp, pMchar + 14, 2);
582 minute = static_cast< int >(wcstol(pTmp, null, 10));
583 dateTime.SetValue(year, month, day, hour, minute);
586 case EWK_INPUT_TYPE_WEEK : //"yyyy-Www";
589 wcsncpy(pTmp, pMchar, 4);
592 year = static_cast< int >(wcstol(pTmp, null, 10));
595 wcsncpy(pTmp, pMchar + 6, 2);
597 week = static_cast< int >(wcstol(pTmp, null, 10));
599 dateTime.SetValue(year,1,1);
600 pDateFormatter->ApplyPattern(formattedString);
601 pDateFormatter->Format(dateTime, dayno);
602 Integer::Parse(dayno, day);
603 days = (week*7) - day;
605 GetDateTimeForWeek(year, days, dateTime);
608 case EWK_INPUT_TYPE_DATE : //"yyyy-MM-dd"
610 wcsncpy(pTmp, pMchar, 4);
612 year = static_cast< int >(wcstol(pTmp, null, 10));
615 wcsncpy(pTmp, pMchar + 5, 2);
617 month = static_cast< int >(wcstol(pTmp, null, 10));
620 wcsncpy(pTmp, pMchar + 8, 2);
622 day = static_cast< int >(wcstol(pTmp, null, 10));
624 dateTime.SetValue(year, month, day);
627 case EWK_INPUT_TYPE_MONTH : //"yyyy-MM";
630 wcsncpy(pTmp, pMchar, 4);
632 year = static_cast< int >(wcstol(pTmp, null, 10));
635 wcsncpy(pTmp, pMchar + 5, 2);
637 month = static_cast< int >(wcstol(pTmp, null, 10));
639 dateTime.SetValue(year, month, 1);
643 r = E_UNSUPPORTED_OPTION;
644 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
654 _InputPickerPopup::GetDateTimeForWeek(int year, int days, DateTime& dateTime)
658 for (int month = 1; month <= 12; month++)
660 DateTime::GetDaysInMonth(year, month, monthDays);
662 if (days - monthDays < 0)
664 dateTime.SetValue(year, month, days);
673 }}} // Tizen::Web::Controls