Fixes in EditDate / EditTime w.r.t OnActivation /
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_DateTimeBar.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17
18 /**
19  * @file                FUiCtrl_DateTimeBar.cpp
20  * @brief               This is the implementation file for the _DateTimeBar class.
21  */
22
23 #include "FUiAnim_VisualElement.h"
24 #include "FUi_AccessibilityContainer.h"
25 #include "FUi_AccessibilityElement.h"
26 #include "FUi_ResourceManager.h"
27 #include "FUiCtrl_DateTimeBar.h"
28 #include "FUiCtrl_DateTimeBarPresenter.h"
29 #include "FUiCtrl_ActionEvent.h"
30 #include "FUiCtrl_Frame.h"
31 #include "FUiCtrl_Form.h"
32 #include "FUi_CoordinateSystemUtils.h"
33
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Media;
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Ui::Animations;
39
40 namespace Tizen { namespace Ui { namespace Controls
41 {
42 _DateTimeBar::_DateTimeBar(void)
43         : __pDateTimeBarPresenter(null)
44         , __pActionEvent(null)
45         , __selectedBoxId(DATETIME_ID_NONE)
46         , __alignment(DATETIME_BAR_ALIGN_DOWN)
47         , __pDateTimeChangeEvent(null)
48         , __pGestureFlick(null)
49         , __parentWindowBounds(0.0f, 0.0f, 0.0f, 0.0f)
50         , __pOwner(null)
51 {
52
53 }
54
55 _DateTimeBar::~_DateTimeBar(void)
56 {
57         ReleaseTouchCapture();
58
59         if (__pDateTimeChangeEvent != null)
60         {
61                 delete __pDateTimeChangeEvent;
62                 __pDateTimeChangeEvent = null;
63         }
64
65         if (__pActionEvent)
66         {
67                 delete __pActionEvent;
68                 __pActionEvent = null;
69         }
70
71         delete __pDateTimeBarPresenter;
72         __pDateTimeBarPresenter = null;
73
74         delete __pGestureFlick;
75         __pGestureFlick = null;
76
77         RemoveAllAccessibilityElement();
78
79         ClearLastResult();
80 }
81
82 _DateTimeBar*
83 _DateTimeBar::CreateDateTimeBarN(_Control& owner)
84 {
85         result r = E_SUCCESS;
86         ClearLastResult();
87
88         _DateTimeBar* pDateTimeBar = new (std::nothrow) _DateTimeBar;
89         SysTryReturn(NID_UI_CTRL, pDateTimeBar != null, null, E_OUT_OF_MEMORY,
90                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
91
92         r = pDateTimeBar->CreateRootVisualElement();
93         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
94
95         pDateTimeBar->__pOwner = &owner;
96
97         pDateTimeBar->__pDateTimeBarPresenter = _DateTimeBarPresenter::CreateInstanceN(*pDateTimeBar);
98         r = GetLastResult();
99         SysTryCatch(NID_UI_CTRL, pDateTimeBar->__pDateTimeBarPresenter != null, , r,
100                         "[%s] Propagating.", GetErrorMessage(r));
101
102         pDateTimeBar->__pDateTimeChangeEvent = new (std::nothrow) _DateTimeChangeEvent(*pDateTimeBar);
103         SysTryCatch(NID_UI_CTRL, (pDateTimeBar->__pDateTimeChangeEvent != null), , E_OUT_OF_MEMORY,
104                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
105
106         pDateTimeBar->AcquireHandle();
107
108         if (pDateTimeBar->GetVisualElement() != null)
109         {
110                 pDateTimeBar->GetVisualElement()->SetSurfaceOpaque(false);
111         }
112
113         pDateTimeBar->__pGestureFlick = new (std::nothrow) _TouchFlickGestureDetector();
114         SysTryCatch(NID_UI_CTRL, (pDateTimeBar->__pGestureFlick != null), , E_OUT_OF_MEMORY,
115                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
116
117         pDateTimeBar->AddGestureDetector(*(pDateTimeBar->__pGestureFlick));
118         SysTryCatch(NID_UI_CTRL, (GetLastResult() == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
119
120         r = pDateTimeBar->__pGestureFlick->AddGestureListener(*pDateTimeBar);
121         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
122
123         pDateTimeBar->InitializeAccessibilityElement();
124         return pDateTimeBar;
125
126 CATCH:
127         delete pDateTimeBar;
128         return null;
129 }
130
131 result
132 _DateTimeBar::SetPositionAndAlignment(const FloatPoint& point, _DateTimeBarAlignment alignment)
133 {
134         ClearLastResult();
135
136         result r = E_SUCCESS;
137         SetAlignment(alignment);
138
139         r = __pDateTimeBarPresenter->LoadArrowBitmap();
140         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
141
142         float height = 0.0f;
143
144         r = GET_SHAPE_CONFIG(DATETIMEBAR::ITEM_HEIGHT, GetOrientation(), height);
145         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
146
147         SetMovable(true);
148         SetResizable(true);
149
150         if (alignment == DATETIME_BAR_ALIGN_DOWN)
151         {
152                 SetPosition(FloatPoint(point.x, point.y));
153
154         }
155         else
156         {
157                 SetPosition(FloatPoint(point.x, point.y - height));
158         }
159
160         __pDateTimeBarPresenter->CalculateWindowBounds();
161
162         return r;
163 }
164
165 _DateTimeBarAlignment
166 _DateTimeBar::GetAlignment(void) const
167 {
168         return __alignment;
169 }
170
171 void
172 _DateTimeBar::SetAlignment(_DateTimeBarAlignment alignment)
173 {
174         __alignment = alignment;
175         return;
176 }
177
178 void
179 _DateTimeBar::OnDraw(void)
180 {
181         ClearLastResult();
182
183         __pDateTimeBarPresenter->Draw();
184         return;
185 }
186
187 result
188 _DateTimeBar::RemoveAllItems(void)
189 {
190         ClearLastResult();
191
192         result r = E_SUCCESS;
193
194         r = __pDateTimeBarPresenter->RemoveAllItems();
195         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
196
197         return r;
198 }
199
200 result
201 _DateTimeBar::SetInitialValue(int minValue, int maxValue, int displayValue, _DateTimeId boxId)
202 {
203         ClearLastResult();
204
205         result r = E_SUCCESS;
206
207         SetSelectedBoxId(boxId);
208
209         __pDateTimeBarPresenter->SetMinimumValue(minValue);
210         __pDateTimeBarPresenter->SetMaximumValue(maxValue);
211
212         r = __pDateTimeBarPresenter->AddItems(displayValue);
213         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
214
215         return r;
216 }
217
218 _DateTimeId
219 _DateTimeBar::GetSelectedBoxId(void) const
220 {
221         return __selectedBoxId;
222 }
223
224 void
225 _DateTimeBar::SetSelectedBoxId(_DateTimeId boxId)
226 {
227         __selectedBoxId = boxId;
228         return;
229 }
230
231
232 void
233 _DateTimeBar::InitializeAccessibilityElement(void)
234 {
235         if (likely(!(_AccessibilityManager::IsActivated())))
236         {
237                 return;
238         }
239
240         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
241
242         if (pContainer)
243         {
244                 pContainer->Activate(true);
245                 pContainer->AddListener(*this);
246         }
247
248         return;
249 }
250
251 int
252 _DateTimeBar::GetItemCount(void) const
253 {
254         ClearLastResult();
255
256         return __pDateTimeBarPresenter->GetItemCount();
257 }
258
259 result
260 _DateTimeBar::AddActionEventListener(const _IActionEventListener& listener)
261 {
262         ClearLastResult();
263
264         result r = E_SUCCESS;
265
266         if (__pActionEvent == null)
267         {
268                 __pActionEvent = _ActionEvent::CreateInstanceN(*this);
269                 SysTryReturn(NID_UI_CTRL, __pActionEvent != null, E_SYSTEM, E_SYSTEM,
270                                         "[%s] Propagating.", GetErrorMessage(r));
271         }
272         r = __pActionEvent->AddListener(listener);
273         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
274
275         return r;
276 }
277
278 result
279 _DateTimeBar::RemoveActionEventListener(const _IActionEventListener& listener)
280 {
281         ClearLastResult();
282
283         result r = E_OBJ_NOT_FOUND;
284
285         if (__pActionEvent)
286         {
287                 r = __pActionEvent->RemoveListener(listener);
288         }
289
290         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
291
292         return r;
293 }
294
295 result
296 _DateTimeBar::AddDateTimeChangeEventListener(const _IDateTimeChangeEventListener& listener)
297 {
298         ClearLastResult();
299
300         return __pDateTimeChangeEvent->AddListener(listener);
301 }
302
303 result
304 _DateTimeBar::RemoveDateTimeChangeEventListener(const _IDateTimeChangeEventListener& listener)
305 {
306         ClearLastResult();
307
308         return __pDateTimeChangeEvent->RemoveListener(listener);
309 }
310
311 result
312 _DateTimeBar::FireDateTimeChangeEvent(_DateTimeChangeStatus status)
313 {
314         ClearLastResult();
315
316         _DateTimeChangeEventArg* pDateTimeEventArg = new (std::nothrow) _DateTimeChangeEventArg(status);
317         SysTryReturn(NID_UI_CTRL, (pDateTimeEventArg != null), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
318                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
319
320         __pDateTimeChangeEvent->Fire(*pDateTimeEventArg);
321
322         return E_SUCCESS;
323 }
324
325 result
326 _DateTimeBar::FireActionEvent(int actionId)
327 {
328         ClearLastResult();
329
330         if (__pActionEvent)
331         {
332                 Tizen::Base::Runtime::IEventArg* pEventArg = _ActionEvent::CreateActionEventArgN(actionId);
333                 result r = GetLastResult();
334                 SysTryReturn(NID_UI_CTRL, pEventArg, false, r, "[%s] Propagating.", GetErrorMessage(r));
335
336                 __pActionEvent->Fire(*pEventArg);
337         }
338
339         return E_SUCCESS;
340 }
341
342 result
343 _DateTimeBar::CalculateArrowBounds(const FloatRectangle& bounds)
344 {
345         ClearLastResult();
346
347         FloatRectangle arrowBounds(0.0f, 0.0f, 0.0f, 0.0f);
348
349         arrowBounds = __pDateTimeBarPresenter->GetArrowBounds();
350         arrowBounds.x = bounds.x + (bounds.width / 2.0f) - (arrowBounds.width / 2.0f);
351         arrowBounds.x -= __pDateTimeBarPresenter->GetWindowBounds().x;
352
353         __pDateTimeBarPresenter->SetArrowBounds(arrowBounds);
354
355         return E_SUCCESS;
356 }
357
358 bool
359 _DateTimeBar::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
360 {
361         ClearLastResult();
362
363         return __pDateTimeBarPresenter->OnTouchPressed(source, touchinfo);
364 }
365
366 bool
367 _DateTimeBar::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
368 {
369         ClearLastResult();
370
371         return __pDateTimeBarPresenter->OnTouchReleased(source, touchinfo);
372 }
373
374 bool
375 _DateTimeBar::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
376 {
377         ClearLastResult();
378
379         return __pDateTimeBarPresenter->OnTouchMoved(source, touchinfo);
380 }
381
382 bool
383 _DateTimeBar::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
384 {
385         ClearLastResult();
386
387         return __pDateTimeBarPresenter->OnTouchCanceled(source, touchinfo);
388 }
389
390 bool
391 _DateTimeBar::OnFlickGestureDetected(_TouchFlickGestureDetector& gesture)
392 {
393         int xDistance = 0;
394         int yDistance = 0;
395         gesture.GetDistance(xDistance, yDistance);
396         int duration = gesture.GetDuration();
397         return __pDateTimeBarPresenter->OnFlickGestureDetected(xDistance, yDistance, duration);
398 }
399
400 bool
401 _DateTimeBar::OnFlickGestureCanceled(_TouchFlickGestureDetector& gesture)
402 {
403    return false;
404 }
405
406 void
407 _DateTimeBar::OnTouchMoveHandled(const Tizen::Ui::_Control& control)
408 {
409         ClearLastResult();
410
411         return __pDateTimeBarPresenter->OnTouchMoveHandled(control);
412 }
413
414 result
415 _DateTimeBar::OnAttachedToMainTree(void)
416 {
417         SetOwner(__pOwner);
418
419         return _Window::OnAttachedToMainTree();
420 }
421
422 void
423 _DateTimeBar::OnActivated(void)
424 {
425         RefreshItems();
426         SetTouchCapture(true, true);
427
428         return _Window::OnActivated();
429 }
430
431 void
432 _DateTimeBar::OnDeactivated(void)
433 {
434         ReleaseTouchCapture();
435
436         GetOwner()->Invalidate();
437
438         return  _Window::OnDeactivated();
439 }
440
441 result
442 _DateTimeBar::OnDetachingFromMainTree(void)
443 {
444         return _Window::OnDetachingFromMainTree();
445 }
446
447 void
448 _DateTimeBar::AddAccessibilityElement(const FloatRectangle& itemBounds, const String& itemText)
449 {
450         if (likely(!(_AccessibilityManager::IsActivated())))
451         {
452                 return;
453         }
454
455         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
456         if (pContainer)
457         {
458                 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
459                 SysTryReturnVoidResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
460
461                 String labelText = itemText;
462                 pAccessibilityElement->SetBounds(itemBounds);
463                 pAccessibilityElement->SetLabel(labelText);
464                 pAccessibilityElement->SetTrait(GetAccessibilityTraitValue());
465                 pContainer->AddElement(*pAccessibilityElement);
466                 __accessibilityElements.Add(pAccessibilityElement);
467         }
468
469         return;
470 }
471
472 void
473 _DateTimeBar::RemoveAllAccessibilityElement(void)
474 {
475         if (likely(!(_AccessibilityManager::IsActivated())))
476         {
477                 return;
478         }
479
480         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
481
482         if (pContainer == null)
483         {
484                 return;
485         }
486
487         _AccessibilityElement* pAccessibilityElement = null;
488
489         while (__accessibilityElements.GetCount() > 0)
490         {
491                 if ((__accessibilityElements.GetAt(0, pAccessibilityElement)) == E_SUCCESS)
492                 {
493                         __accessibilityElements.RemoveAt(0);
494                         pContainer->RemoveElement(*pAccessibilityElement);
495                 }
496                 else
497                 {
498                         __accessibilityElements.RemoveAt(0);
499                 }
500         }
501
502         return;
503 }
504
505 AccessibilityTraits
506 _DateTimeBar::GetAccessibilityTraitValue()
507 {
508         AccessibilityTraits traitsValue = ACCESSIBILITY_TRAITS_NONE;
509
510         if (GetSelectedBoxId() == DATETIME_ID_DAY)
511         {
512                 traitsValue = ACCESSIBILITY_TRAITS_DAY;
513         }
514         else if (GetSelectedBoxId() == DATETIME_ID_MONTH)
515         {
516                 traitsValue = ACCESSIBILITY_TRAITS_MONTH;
517         }
518         else if (GetSelectedBoxId() == DATETIME_ID_YEAR)
519         {
520                 traitsValue = ACCESSIBILITY_TRAITS_YEAR;
521         }
522         else if (GetSelectedBoxId() == DATETIME_ID_HOUR)
523         {
524                 traitsValue = ACCESSIBILITY_TRAITS_HOUR;
525         }
526         else if (GetSelectedBoxId() == DATETIME_ID_MINUTE)
527         {
528                 traitsValue = ACCESSIBILITY_TRAITS_MINUTE;
529         }
530
531         return traitsValue;
532 }
533
534 String
535 _DateTimeBar::GetMonthValue(int month) const
536 {
537         String monthValue;
538
539         switch (month)
540         {
541         case 1:
542                 monthValue = L"January";
543                 break;
544         case 2:
545                 monthValue = L"February";
546                 break;
547         case 3:
548                 monthValue = L"March";
549                 break;
550         case 4:
551                 monthValue = L"April";
552                 break;
553         case 5:
554                 monthValue = L"May";
555                 break;
556         case 6:
557                 monthValue = L"June";
558                 break;
559         case 7:
560                 monthValue = L"July";
561                 break;
562         case 8:
563                 monthValue = L"August";
564                 break;
565         case 9:
566                 monthValue = L"September";
567                 break;
568         case 10:
569                 monthValue = L"October";
570                 break;
571         case 11:
572                 monthValue = L"November";
573                 break;
574         case 12:
575                 monthValue = L"December";
576                 break;
577         default:
578                 monthValue = L"";
579         }
580
581         return monthValue;
582 }
583
584 void
585 _DateTimeBar::SetFont(Font& pFont)
586 {
587         __pDateTimeBarPresenter->SetFont(pFont);
588
589         return;
590 }
591
592 void
593 _DateTimeBar::SetParentWindowBounds(FloatRectangle& parentWindowBounds)
594 {
595         __parentWindowBounds = parentWindowBounds;
596         return;
597 }
598
599 FloatRectangle
600 _DateTimeBar::GetParentWindowBounds() const
601 {
602         return __parentWindowBounds;
603 }
604
605 void
606 _DateTimeBar::RefreshItems(void)
607 {
608         float itemWidth = 0;
609
610         if (GetSelectedBoxId() == DATETIME_ID_MONTH)
611         {
612                 GET_SHAPE_CONFIG(DATETIMEBAR::MONTH_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
613         }
614         else if (GetSelectedBoxId() == DATETIME_ID_YEAR)
615         {
616                 GET_SHAPE_CONFIG(DATETIMEBAR::YEAR_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
617         }
618         else
619         {
620                 GET_SHAPE_CONFIG(DATETIMEBAR::DAY_ITEM_WIDTH, _ControlManager::GetInstance()->GetOrientation(), itemWidth);
621         }
622
623         __pDateTimeBarPresenter->SetInitialAnimationValue(itemWidth);
624
625         __pDateTimeBarPresenter->StartAnimationEffect();
626
627         return;
628 }
629
630 bool
631 _DateTimeBar::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
632 {
633         return __pDateTimeBarPresenter->OnAccessibilityFocusMovedNext(control, element);
634 }
635
636 bool
637 _DateTimeBar::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
638 {
639         return __pDateTimeBarPresenter->OnAccessibilityFocusMovedPrevious(control, element);
640 }
641
642 bool
643 _DateTimeBar::OnAccessibilityReadingElement(const _AccessibilityContainer& control, const _AccessibilityElement& element)
644 {
645         return false;
646 }
647 bool
648 _DateTimeBar::OnAccessibilityReadedElement(const _AccessibilityContainer& control, const _AccessibilityElement& element)
649 {
650         return false;
651 }
652 bool
653 _DateTimeBar::OnAccessibilityFocusIn(const _AccessibilityContainer& control, const _AccessibilityElement& element)
654 {
655         return false;
656 }
657
658 bool
659 _DateTimeBar::OnAccessibilityFocusOut(const _AccessibilityContainer& control, const _AccessibilityElement& element)
660 {
661         return false;
662 }
663
664 bool
665 _DateTimeBar::OnAccessibilityActionPerformed(const _AccessibilityContainer& control, const _AccessibilityElement& element)
666 {
667         return false;
668 }
669
670 bool
671 _DateTimeBar::OnAccessibilityValueIncreased(const Tizen::Ui::_AccessibilityContainer&, const Tizen::Ui::_AccessibilityElement&)
672 {
673         return false;
674 }
675
676 bool
677 _DateTimeBar::OnAccessibilityValueDecreased(const Tizen::Ui::_AccessibilityContainer&, const Tizen::Ui::_AccessibilityElement&)
678 {
679         return false;
680 }
681
682 }}} // Tizen::Ui::Controls