Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_Popup.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_Popup.cpp
20  * @brief       This is the implementation file for the _Popup class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FBaseErrorDefine.h>
25 #include <FGrp_BitmapImpl.h>
26 #include "FUi_AccessibilityContainer.h"
27 #include "FUi_AccessibilityElement.h"
28 #include "FUi_ControlManager.h"
29 #include "FUi_ModalLoopManager.h"
30 #include "FUi_ResourceManager.h"
31 #include "FUi_DataBindingContext.h"
32 #include "FUiCtrl_Popup.h"
33 #include "FUiCtrl_PopupPresenter.h"
34 #include "FUiCtrl_Form.h"
35 #include "FUiCtrl_Frame.h"
36
37
38 using namespace Tizen::Graphics;
39 using namespace Tizen::Ui::Animations;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Base;
42
43
44 namespace Tizen { namespace Ui { namespace Controls
45 {
46
47
48 _Popup::_Popup(void)
49         : _titleState(false)
50         , _ownerEnableState(false)
51         , _titleText(L"")
52         , _bgColor(Color(0xFFFFFFFF))
53         , _titleTextColor(Color(0xFFFFFFFF))
54         , _pBgBitmap(null)
55         , _pComposedBgBitmap(null)
56         , _pOutlineBitmap(null)
57         , _pDimmingLayer(null)
58         , _pTitleTextAccessibilityElement(null)
59         , __pPopupPresenter(null)
60         , __bounds(0, 0, 0, 0)
61         , __popupResult(POPUP_RESULT_NONE)
62 {
63         // empty statement
64 }
65
66 _Popup::~_Popup(void)
67 {
68         _Control* pOwner = GetOwner();
69         if (pOwner != null)
70         {
71                 pOwner->SetInputEnableState(_ownerEnableState);
72         }
73
74         ReleaseTouchCapture();
75
76         delete __pPopupPresenter;
77         __pPopupPresenter = null;
78
79         delete _pBgBitmap;
80         _pBgBitmap = null;
81
82         delete _pComposedBgBitmap;
83         _pComposedBgBitmap = null;
84
85         delete _pOutlineBitmap;
86         _pOutlineBitmap = null;
87
88         delete _pDimmingLayer;
89         _pDimmingLayer = null;
90
91         if (_pTitleTextAccessibilityElement)
92         {
93                 _pTitleTextAccessibilityElement->Activate(false);
94                 _pTitleTextAccessibilityElement = null;
95         }
96 }
97
98 _Popup*
99 _Popup::CreatePopupN(void)
100 {
101         _Popup* pPopup = new (std::nothrow) _Popup();
102         SysTryReturn(NID_UI_CTRL, pPopup != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
103
104 #if defined(MULTI_WINDOW)
105         result r = pPopup->CreateRootVisualElement();
106         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
107 #endif
108
109         // for taking touch event
110         pPopup->AcquireHandle();
111
112         return pPopup;
113
114 #if defined(MULTI_WINDOW)
115 CATCH:
116         delete pPopup;
117         return null;
118 #endif
119 }
120
121 result
122 _Popup::Initialize(bool hasTitle, const Tizen::Graphics::Rectangle& bounds)
123 {
124         result r = E_SUCCESS;
125
126         _AccessibilityContainer* pContainer = null;
127
128         _titleState = hasTitle;
129         __bounds = bounds;
130         SetClientBounds(GetPopupClientArea());
131
132         _PopupPresenter* pPresenter = new (std::nothrow) _PopupPresenter();
133         SysTryReturn(NID_UI_CTRL, pPresenter != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
134
135         SetPresenter(*pPresenter);
136         pPresenter->Initialize(*this);
137
138         _pDimmingLayer = new (std::nothrow) _DimmingLayer();
139         SysTryReturn(NID_UI_CTRL, _pDimmingLayer != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
140
141         r = _pDimmingLayer->Construct(*this);
142         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
143
144         GET_COLOR_CONFIG(POPUP::BG_NORMAL, _bgColor);
145         GET_COLOR_CONFIG(POPUP::TITLE_TEXT_NORMAL, _titleTextColor);
146
147         // Popup-Outline
148         GET_BITMAP_CONFIG_N(POPUP::BG_OUTLINE_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, _pOutlineBitmap);
149         r = GetLastResult();
150         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
151
152         // Popup BG
153         GET_BITMAP_CONFIG_N(POPUP::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, _pBgBitmap);
154         r = GetLastResult();
155         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
156
157         _pComposedBgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*_pBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), _bgColor);
158
159         // for clearing canvas
160         r = GetVisualElement()->SetSurfaceOpaque(false);
161         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
162
163         //create data binding context
164         _DataBindingContext* pContext = new (std::nothrow) _DataBindingContext(*this);
165         SysTryCatch(NID_UI_CTRL, pContext != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
166         r = GetLastResult();
167         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
168
169         SetDataBindingContext(pContext);
170
171         pContainer = GetAccessibilityContainer();
172         if(pContainer != null)
173         {
174                 pContainer->Activate(true);
175         }
176
177         return r;
178
179 CATCH:
180         delete pContext;
181
182         return r;
183 }
184
185 result
186 _Popup::SetPresenter(const _PopupPresenter& popupPresenter)
187 {
188         __pPopupPresenter = const_cast <_PopupPresenter*>(&popupPresenter);
189
190         return E_SUCCESS;
191 }
192
193 result
194 _Popup::DoModal(int& modalResult)
195 {
196         result r = E_SUCCESS;
197
198         __pPopupPresenter->DoModal(modalResult);
199         r = GetLastResult();
200         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
201
202         return r;
203 }
204
205 result
206 _Popup::EndModal(int modalResult)
207 {
208         _ModalLoopManager::GetInstance()->EndMainLoop(POPUP_RESULT_END_MODAL, false);
209
210         SetReturnValue(static_cast <PopupModalResult>(modalResult));
211
212         return E_SUCCESS;
213 }
214
215 void
216 _Popup::SetReturnValue(PopupModalResult rtn)
217 {
218         __popupResult = rtn;
219 }
220
221 int
222 _Popup::GetPopupReturnValue(void) const
223 {
224         return (int) __popupResult;
225 }
226
227 bool
228 _Popup::HasTitle(void) const
229 {
230         return _titleState;
231 }
232
233 Point
234 _Popup::TranslateFromClientAreaPosition(const Point& clientPosition) const
235 {
236         Rectangle leftTop = GetClientBounds();
237         result r = GetLastResult();
238         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, Point(-1, -1), r, "[%s] Propagating.", GetErrorMessage(r));
239
240         return Point(clientPosition.x + leftTop.x, clientPosition.y + leftTop.y);
241 }
242
243 Point
244 _Popup::TranslateToClientAreaPosition(const Point& position) const
245 {
246         Rectangle leftTop = GetClientBounds();
247         result r = GetLastResult();
248         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, Point(-1, -1), r, "[%s] Propagating.", GetErrorMessage(r));
249
250         return Point(position.x - leftTop.x, position.y - leftTop.y);
251 }
252
253 Canvas*
254 _Popup::GetClientAreaCanvasN(void) const
255 {
256         Canvas* pCanvas = GetCanvasN(GetClientBounds());
257         if ((pCanvas == null) || (GetLastResult() != E_SUCCESS))
258         {
259                 SysLog(NID_UI_CTRL, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
260                 delete pCanvas;
261                 return null;
262         }
263
264         return pCanvas;
265 }
266
267 void
268 _Popup::SetColor(const Color& color)
269 {
270         if (_pComposedBgBitmap != null)
271         {
272                 delete _pComposedBgBitmap;
273                 _pComposedBgBitmap = null;
274         }
275
276         _pComposedBgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*_pBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), color);
277
278         _bgColor = color;
279 }
280
281 Color
282 _Popup::GetColor() const
283 {
284         return _bgColor;
285 }
286
287 void
288 _Popup::SetTitleTextColor(const Color& color)
289 {
290         _titleTextColor = color;
291 }
292
293 Color
294 _Popup::GetTitleTextColor() const
295 {
296         return _titleTextColor;
297 }
298
299 result
300 _Popup::SetTitleText(const String& title)
301 {
302         if (HasTitle() == true)
303         {
304                 _titleText = title;
305
306                 if (__pPopupPresenter != null)
307                 {
308                         result r = __pPopupPresenter->SetTitleTextObject(title);
309                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
310                 }
311
312                 if (_pTitleTextAccessibilityElement != null)
313                 {
314                     _pTitleTextAccessibilityElement->SetLabel(title);
315                 }
316
317                 return E_SUCCESS;
318         }
319         else
320         {
321                 return E_SYSTEM;
322         }
323 }
324
325 String
326 _Popup::GetTitleText(void) const
327 {
328         return _titleText;
329 }
330
331 const Bitmap*
332 _Popup::GetBackgroundBitmap(void) const
333 {
334         return _pComposedBgBitmap;
335 }
336
337 const Bitmap*
338 _Popup::GetOutlineBitmap(void) const
339 {
340         return _pOutlineBitmap;
341 }
342
343 Rectangle
344 _Popup::GetPopupClientArea(void) const
345 {
346         int x = 0;
347         int y = 0;
348         int width = 0;
349         int height = 0;
350
351         int titleHeight = 0;
352         int titleLetfMargin = 0;
353         int titleRightMargin = 0;
354
355         int transTopMargin = 0;
356         int transBottomMargin = 0;
357         int transLeftMargin = 0;
358         int transRightMargin = 0;
359
360
361         GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, GetOrientation(), titleHeight);
362         GET_SHAPE_CONFIG(POPUP::TITLE_TEXT_LEFT_MARGIN, GetOrientation(), titleLetfMargin);
363         GET_SHAPE_CONFIG(POPUP::TITLE_TEXT_RIGHT_MARGIN, GetOrientation(), titleRightMargin);
364
365         GET_SHAPE_CONFIG(POPUP::BG_IMAGE_TRANSPARENT_TOP_MARGIN, GetOrientation(), transTopMargin);
366         GET_SHAPE_CONFIG(POPUP::BG_IMAGE_TRANSPARENT_BOTTOM_MARGIN, GetOrientation(), transBottomMargin);
367         GET_SHAPE_CONFIG(POPUP::BG_IMAGE_TRANSPARENT_LEFT_MARGIN, GetOrientation(), transLeftMargin);
368         GET_SHAPE_CONFIG(POPUP::BG_IMAGE_TRANSPARENT_RIGHT_MARGIN, GetOrientation(), transRightMargin);
369
370         x = titleLetfMargin;
371         width = GetBounds().width - (titleLetfMargin + titleRightMargin);
372
373         if (HasTitle() == false)
374         {
375                 titleHeight = 0;
376         }
377
378         y = titleHeight;
379         height = GetBounds().height - y;
380
381         return Rectangle(x, y, width, height);
382 }
383
384 _AccessibilityElement*
385 _Popup::GetTitleTextAccessibilityElement(void)
386 {
387         return _pTitleTextAccessibilityElement;
388 }
389
390 void
391 _Popup::OnDraw(void)
392 {
393         __pPopupPresenter->OnDraw();
394         result r = GetLastResult();
395         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
396
397         return;
398 }
399
400 void
401 _Popup::OnFontChanged(Font * pFont)
402 {
403         if (__pPopupPresenter != null)
404         {
405                 __pPopupPresenter->OnFontChanged(pFont);
406         }
407 }
408
409 void
410 _Popup::OnFontInfoRequested(unsigned long& style, int& size)
411 {
412         if (__pPopupPresenter != null)
413         {
414                 __pPopupPresenter->OnFontInfoRequested(style, size);
415         }
416 }
417
418 void
419 _Popup::OnActivated(void)
420 {
421         SysTryReturnVoidResult(NID_UI_CTRL, _pDimmingLayer != null, E_SYSTEM, "[E_SYSTEM] There is no Dimming Layer.");
422
423         result r = E_SUCCESS;
424
425         if (__pPopupPresenter != null)
426         {
427                 __pPopupPresenter->OnActivated();
428         }
429
430         if (GetVisibleState())
431         {
432                 r = _pDimmingLayer->SetDimmingEnabled(true);
433                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
434
435                 SetTouchCapture();
436         }
437         else
438         {
439                 r = _pDimmingLayer->SetDimmingEnabled(false);
440                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
441         }
442
443         if (GetOwner() == null)
444         {
445                 _Frame* pFrame = dynamic_cast<_Frame*>(_ControlManager::GetInstance()->GetCurrentFrame());
446                 SysTryReturnVoidResult(NID_UI_CTRL, pFrame != null, E_SYSTEM,"[E_SYSTEM] This instance is not constructed.");
447
448                 _Form* pForm = pFrame->GetCurrentForm();
449                 if (pForm != null)
450                 {
451                         SetOwner(pForm);
452                 }
453                 else
454                 {
455                         SetOwner(pFrame);
456                 }
457         }
458
459         _Control* pOwner = GetOwner();
460
461         if (pOwner != null)
462         {
463                 _ownerEnableState = pOwner->GetInputEnableState();
464                 pOwner->SetInputEnableState(false);
465         }
466
467         _Window::OnActivated();
468 }
469
470 void
471 _Popup::OnDeactivated(void)
472 {
473         SysTryReturnVoidResult(NID_UI_CTRL, _pDimmingLayer != null, E_SYSTEM, "[E_SYSTEM] There is no Dimming Layer.");
474
475 #if !defined(MULTI_WINDOW)
476         result r = E_SUCCESS;
477
478         if (IsAttached() == false)
479         {
480                 r = _pDimmingLayer->SetDimmingEnabled(false);
481                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
482         }
483 #endif
484
485         _Control* pOwner = GetOwner();
486         if (pOwner != null)
487         {
488                 pOwner->SetInputEnableState(_ownerEnableState);
489         }
490
491         ReleaseTouchCapture();
492
493         _Window::OnDeactivated();
494 }
495
496 void
497 _Popup::OnVisibleStateChanged(void)
498 {
499         result r = E_SUCCESS;
500
501         if (GetVisibleState() == true)
502         {
503                 r = _pDimmingLayer->SetDimmingEnabled(true);
504                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
505         }
506         else
507         {
508                 r = _pDimmingLayer->SetDimmingEnabled(false);
509                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
510         }
511
512         _Window::OnVisibleStateChanged();
513 }
514
515 result
516 _Popup::OnAttachingToMainTree(const _Control* pParent)
517 {
518         return _Window::OnAttachingToMainTree(pParent);
519 }
520
521 result
522 _Popup::OnAttachedToMainTree(void)
523 {
524         SysTryReturn(NID_UI_CTRL, GetVisibleState() != false, E_INVALID_OPERATION,
525                                 E_INVALID_OPERATION, "[E_INVALID_OPERATION] This control is not 'displayable'");
526
527         InitializeAccessibilityElement();
528
529         return E_SUCCESS;
530 }
531
532 void
533 _Popup::InitializeAccessibilityElement(void)
534 {
535         if (likely(!(_AccessibilityManager::IsActivated())))
536         {
537                 return;
538         }
539
540         if (_pTitleTextAccessibilityElement != null)
541         {
542                 return;
543         }
544
545         _AccessibilityContainer* pContainer = GetAccessibilityContainer();
546         if (pContainer != null)
547         {
548                 int titleHeight = 0;
549                 GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, GetOrientation(), titleHeight);
550
551                 _pTitleTextAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
552                 SysTryReturnVoidResult(NID_UI_CTRL, _pTitleTextAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
553
554                 _pTitleTextAccessibilityElement->Construct(L"PopupTitleText", __pPopupPresenter->GetTitleTextBounds());
555                 _pTitleTextAccessibilityElement->SetLabel(GetTitleText());
556                 _pTitleTextAccessibilityElement->SetTrait(ACCESSIBILITY_TRAITS_TEXT_FIELD);
557
558                 pContainer->AddElement(*_pTitleTextAccessibilityElement);
559
560                 _Label* pLabelCore = __pPopupPresenter->GetSystemLabel();
561                 _AccessibilityContainer* pLabelContainer = pLabelCore->GetAccessibilityContainer();
562                 if (pLabelContainer != null)
563                 {
564                         pLabelContainer->Activate(false);
565                 }
566         }
567
568         return;
569 }
570
571 result
572 _Popup::OnDetachingFromMainTree(void)
573 {
574         return _Window::OnDetachingFromMainTree();
575 }
576
577 void
578 _Popup::OnChangeLayout(_ControlOrientation orientation)
579 {
580         SysTryReturnVoidResult(NID_UI_CTRL, _pDimmingLayer != null, E_SYSTEM, "[E_SYSTEM] There is no Dimming Layer.");
581
582         result r = E_SUCCESS;
583
584         r = _pDimmingLayer->Rearrange();
585         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
586
587         return;
588 }
589
590 void
591 _Popup::OnBoundsChanged(void)
592 {
593         SetClientBounds(GetPopupClientArea());
594
595         if (__pPopupPresenter != null)
596         {
597                 __pPopupPresenter->UpdateEffectBounds();
598         }
599
600         if (_pTitleTextAccessibilityElement != null)
601         {
602             int titleHeight = 0;
603                 GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, GetOrientation(), titleHeight);
604
605                 _pTitleTextAccessibilityElement->SetBounds(Rectangle(0, 0, GetPopupClientArea().width, titleHeight));
606         }
607 }
608
609 #if defined(MULTI_WINDOW)
610 result
611 _Popup::OnBoundsChanging(const Rectangle& bounds)
612 {
613         // Full-screen window
614         return E_SUCCESS;
615 }
616
617 bool
618 _Popup::IsLayoutChangable(void) const
619 {
620         // Full-screen window
621         return true;
622 }
623 #endif
624
625 bool
626 _Popup::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
627 {
628         if (&source != this)
629         {
630                 return false;
631         }
632
633         return __pPopupPresenter->OnTouchPressed(source, touchinfo);
634 }
635
636 bool
637 _Popup::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
638 {
639         if (&source != this)
640         {
641                 return false;
642         }
643
644         return __pPopupPresenter->OnTouchReleased(source, touchinfo);
645 }
646
647
648 }}} // Tizen::Ui::Controls
649