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