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 <FUiCtrl_PopupImpl.h>
39 #include <FUi_ResourceManager.h>
40 #include <FSys_SystemResource.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::Web::Controls;
48 using namespace Tizen::Ui::Controls;
49 using namespace Tizen::Ui;
50 using namespace Tizen::Locales;
51 using namespace Tizen::System;
54 namespace Tizen { namespace Web { namespace Controls
58 static const int DATE_POPUP_BUTTON_WIDTH = 250;
61 _InputPickerPopup::_InputPickerPopup(void)
65 , __pSelectionBtn(null)
70 , __inputType(EWK_INPUT_TYPE_TIME)
75 _InputPickerPopup::~_InputPickerPopup(void)
81 _InputPickerPopup::Construct(const String& strDate, Ewk_Input_Type inputType)
89 DateTime inputDateTime;
91 __inputPickerMode = INPUT_MODE_DATE;
92 __inputType = inputType;
94 SystemTime::GetCurrentTime(UTC_TIME, inputDateTime);
96 if (!strDate.IsEmpty())
98 r = Parse(strDate, inputDateTime);
101 _ControlOrientation orientation = _ControlManager::GetInstance()->GetOrientation();
103 GET_SHAPE_CONFIG(EDITDATE::WIDTH, orientation, dateWidth);
104 GET_SHAPE_CONFIG(EDITDATE::HEIGHT, orientation, dateHeight);
105 GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
106 GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __popupWidth);
107 GET_SHAPE_CONFIG(MESSAGEBOX::MIN_HEIGHT, orientation, __popupHeight);
108 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __btnHeight);
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, Tizen::Graphics::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 Tizen::System::_SystemResource* pSysResource = Tizen::System::_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 result r = E_SUCCESS;
518 wchar_t* pMchar = (wchar_t*) strDateTime.GetPointer();
519 wchar_t* pTmp = (wchar_t*) malloc(sizeof(wchar_t) * 10);
520 SysTryReturnResult(NID_WEB_CTRL, pTmp, E_OUT_OF_MEMORY, "Memory allocation failed.");
522 std::unique_ptr<DateTimeFormatter> pDateFormatter(DateTimeFormatter::CreateDateFormatterN(DATE_TIME_STYLE_DEFAULT));
523 SysTryReturn(NID_WEB_CTRL, pDateFormatter.get(), GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
532 String formattedString = L"e";
537 case EWK_INPUT_TYPE_TIME : //"HH:mm";
540 wcsncpy(pTmp, pMchar, 2);
542 hour = (int) wcstol(pTmp, null, 10);
545 wcsncpy(pTmp, pMchar + 3, 2);
547 minute = (int) wcstol(pTmp, null, 10);
548 dateTime.SetValue(1, 1, 1, hour,minute);
551 case EWK_INPUT_TYPE_DATETIME : // "yyyy-MM-ddTHH:mmZ";
553 case EWK_INPUT_TYPE_DATETIMELOCAL :
555 wcsncpy(pTmp, pMchar, 4);
558 year = (int) wcstol(pTmp, null, 10);
561 wcsncpy(pTmp, pMchar + 5, 2);
564 month = (int) wcstol(pTmp, null, 10);
567 wcsncpy(pTmp, pMchar + 8, 2);
570 day = (int) wcstol(pTmp, null, 10);
573 wcsncpy(pTmp, pMchar + 11, 2);
576 hour = (int) wcstol(pTmp, null, 10);
579 wcsncpy(pTmp, pMchar + 14, 2);
581 minute = (int) wcstol(pTmp, null, 10);
582 dateTime.SetValue(year, month, day, hour, minute);
585 case EWK_INPUT_TYPE_WEEK : //"yyyy-Www";
588 wcsncpy(pTmp, pMchar, 4);
591 year = (int) wcstol(pTmp, null, 10);
594 wcsncpy(pTmp, pMchar + 6, 2);
596 week = (int) wcstol(pTmp, null, 10);
598 dateTime.SetValue(year,1,1);
599 pDateFormatter->ApplyPattern(formattedString);
600 pDateFormatter->Format(dateTime, dayno);
601 Integer::Parse(dayno, day);
602 days = (week*7) - day;
604 GetDateTimeForWeek(year, days, dateTime);
607 case EWK_INPUT_TYPE_DATE : //"yyyy-MM-dd"
609 wcsncpy(pTmp, pMchar, 4);
611 year = (int) wcstol(pTmp, null, 10);
614 wcsncpy(pTmp, pMchar + 5, 2);
616 month = (int) wcstol(pTmp, null, 10);
619 wcsncpy(pTmp, pMchar + 8, 2);
621 day = (int) wcstol(pTmp, null, 10);
623 dateTime.SetValue(year, month, day);
626 case EWK_INPUT_TYPE_MONTH : //"yyyy-MM";
629 wcsncpy(pTmp, pMchar, 4);
631 year = (int) wcstol(pTmp, null, 10);
634 wcsncpy(pTmp, pMchar + 5, 2);
636 month = (int) wcstol(pTmp, null, 10);
638 dateTime.SetValue(year, month, 1);
642 r = E_UNSUPPORTED_OPTION;
643 SysLogException(NID_WEB_CTRL, r, "[%s] %d is unsupported.", GetErrorMessage(r), __inputType);
657 _InputPickerPopup::GetDateTimeForWeek(int year, int days, DateTime& dateTime)
661 for (int month = 1; month <= 12; month++)
663 DateTime::GetDaysInMonth(year, month, monthDays);
665 if (days - monthDays < 0)
667 dateTime.SetValue(year, month, days);
676 }}} // Tizen::Web::Controls