add patch
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_MessageBox.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
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_MessageBox.cpp
20  * @brief       This is the implementation file for the _MessageBox class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FBaseErrorDefine.h>
25 #include <FGrp_BitmapImpl.h>
26 #include <FSys_SettingInfoImpl.h>
27 #include "FUi_AccessibilityContainer.h"
28 #include "FUi_AccessibilityElement.h"
29 #include "FUi_CoordinateSystemUtils.h"
30 #include "FUi_EcoreEvas.h"
31 #include "FUi_EcoreEvasMgr.h"
32 #include "FUi_ResourceManager.h"
33 #include "FUiCtrl_Form.h"
34 #include "FUiCtrl_Frame.h"
35 #include "FUiCtrl_MessageBox.h"
36 #include "FUiCtrl_MessageBoxPresenter.h"
37
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Graphics;
41 using namespace Tizen::System;
42
43
44 namespace Tizen { namespace Ui { namespace Controls
45 {
46
47
48 _MessageBox::_MessageBox(void)
49         : __pMsgboxPresenter(null)
50         , __msgboxStyle(MSGBOX_STYLE_NONE)
51         , __textColor(Color(0xFFFFFFFF))
52         , __pButtonEffectBitmap(null)
53         , __pTitleEffectBitmap(null)
54         , __pComposedButtonBitmap(null)
55         , __pComposedTitleBitmap(null)
56         , __text(L"")
57         , __timeout(0)
58 {
59         //empty statement
60 }
61
62 _MessageBox::~_MessageBox(void)
63 {
64         delete __pMsgboxPresenter;
65         __pMsgboxPresenter = null;
66
67         delete _pBgBitmap;
68         _pBgBitmap = null;
69
70         delete _pComposedBgBitmap;
71         _pComposedBgBitmap = null;
72
73         delete _pOutlineBitmap;
74         _pOutlineBitmap = null;
75
76         delete __pButtonEffectBitmap;
77         __pButtonEffectBitmap = null;
78
79         delete __pTitleEffectBitmap;
80         __pTitleEffectBitmap = null;
81
82         delete __pComposedButtonBitmap;
83         __pComposedButtonBitmap = null;
84
85         delete __pComposedTitleBitmap;
86         __pComposedTitleBitmap = null;
87
88         _SettingInfoImpl::RemoveSettingEventListenerForInternal(*this);
89 }
90
91 _MessageBox*
92 _MessageBox::CreateMessageBoxN(void)
93 {
94         _MessageBox* pMsgbox = new (std::nothrow) _MessageBox();
95         SysTryReturn(NID_UI_CTRL, pMsgbox != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
96
97         result r = pMsgbox->CreateRootVisualElement(_WINDOW_TYPE_SUB);
98         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
99
100         // for taking touch event
101         pMsgbox->AcquireHandle();
102
103         return pMsgbox;
104
105 CATCH:
106         delete pMsgbox;
107
108         return null;
109 }
110
111 result
112 _MessageBox::Initialize(const String& title, const String& text, MessageBoxStyle style, unsigned long timeout)
113 {
114         result r = E_SUCCESS;
115
116         float titleHeight = 0.0f;
117         Bitmap* buttonBgBitmap = null;
118         Bitmap* titleBgBitmap = null;
119         Color titleBgColor(0x00000000);
120         Color buttonBgColor(0x00000000);
121         _AccessibilityContainer* pContainer = null;
122         SetFocusNavigateEnabled(false);
123
124         _titleText = title;
125         _titleState = !(title.Equals(L"", false));
126
127         __text = text;
128         __msgboxStyle = style;
129         __timeout = timeout;
130
131         _MessageBoxPresenter* pPresenter = new (std::nothrow) _MessageBoxPresenter();
132         SysTryReturn(NID_UI_CTRL, pPresenter != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
133
134         SetPresenter(*pPresenter);
135
136         r = pPresenter->Initialize(*this);
137         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
138
139         SetDimmingEnabled(true);
140
141         GET_COLOR_CONFIG(MESSAGEBOX::BG_NORMAL, _bgColor);
142         GET_COLOR_CONFIG(MESSAGEBOX::TITLE_TEXT_NORMAL, _titleTextColor);
143         GET_COLOR_CONFIG(MESSAGEBOX::TEXT_NORMAL, __textColor);
144         GET_COLOR_CONFIG(MESSAGEBOX::TITLE_BG_NORMAL, titleBgColor);
145         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BG_NORMAL, buttonBgColor);
146
147         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_HEIGHT, GetOrientation(), titleHeight);
148
149         // MsgBox_Outline
150         GET_BITMAP_CONFIG_N(MESSAGEBOX::BG_OUTLINE_EFFECT_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, _pOutlineBitmap);
151         r = GetLastResult();
152         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] failed to load MessageBox Outline Image.");
153
154         // MsgBox BG
155         GET_BITMAP_CONFIG_N(MESSAGEBOX::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, _pBgBitmap);
156         r = GetLastResult();
157         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] failed to load MessageBox BG Image.");
158
159         _pComposedBgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*_pBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), _bgColor);
160         r = GetLastResult();
161         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
162
163         if (style != MSGBOX_STYLE_NONE)
164         {
165                 // MsgBox-BottomBitmap
166                 GET_BITMAP_CONFIG_N(MESSAGEBOX::BG_BUTTON_AREA, BITMAP_PIXEL_FORMAT_ARGB8888, buttonBgBitmap);
167                 r = GetLastResult();
168                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
169
170                 // MsgBox-BottomEffectBitmap
171                 GET_BITMAP_CONFIG_N(MESSAGEBOX::BG_BUTTON_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pButtonEffectBitmap);
172                 r = GetLastResult();
173                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
174
175                 __pComposedButtonBitmap = _BitmapImpl::GetColorReplacedBitmapN(*buttonBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), buttonBgColor);
176                 r = GetLastResult();
177                 SysTryCatch(NID_UI_CTRL, (__pComposedButtonBitmap != null), , r, "[%s] Propagating.", GetErrorMessage(r));
178         }
179
180         if (_titleState)
181         {
182                 // MsgBox-TitleBitmap
183                 GET_BITMAP_CONFIG_N(MESSAGEBOX::BG_TITLE_AREA, BITMAP_PIXEL_FORMAT_ARGB8888, titleBgBitmap);
184                 r = GetLastResult();
185                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
186
187                 // MsgBox-TitleEffectBitmap
188                 GET_BITMAP_CONFIG_N(MESSAGEBOX::BG_TITLE_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTitleEffectBitmap);
189                 r = GetLastResult();
190                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
191
192                 __pComposedTitleBitmap = _BitmapImpl::GetColorReplacedBitmapN(*titleBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), titleBgColor);
193                 r = GetLastResult();
194                 SysTryCatch(NID_UI_CTRL, (__pComposedTitleBitmap != null), , r, "[%s] Propagating.", GetErrorMessage(r));
195         }
196
197         // for clearing canvas
198         if (GetVisualElement() != null)
199         {
200                 GetVisualElement()->SetSurfaceOpaque(false);
201         }
202
203         pContainer = GetAccessibilityContainer();
204         if(pContainer != null)
205         {
206                 pContainer->Activate(true);
207                 pContainer->AddListener(*this);
208         }
209
210         r = _SettingInfoImpl::AddSettingEventListenerForInternal(*this);
211         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
212
213         SetTouchPressThreshold(0.08);
214
215         if (buttonBgBitmap != null)
216         {
217                 delete buttonBgBitmap;
218                 buttonBgBitmap = null;
219         }
220
221         if (titleBgBitmap != null)
222         {
223                 delete titleBgBitmap;
224                 titleBgBitmap = null;
225         }
226
227         return r;
228
229 CATCH:
230         if (buttonBgBitmap != null)
231         {
232                 delete buttonBgBitmap;
233                 buttonBgBitmap = null;
234         }
235
236         if (titleBgBitmap != null)
237         {
238                 delete titleBgBitmap;
239                 titleBgBitmap = null;
240         }
241
242         delete _pComposedBgBitmap;
243         _pComposedBgBitmap = null;
244
245         if (__pComposedButtonBitmap != null)
246         {
247                 delete __pComposedButtonBitmap;
248                 __pComposedButtonBitmap = null;
249         }
250
251         if(__pComposedTitleBitmap != null)
252         {
253                 delete __pComposedTitleBitmap;
254                 __pComposedTitleBitmap = null;
255         }
256
257         return r;
258 }
259
260 result
261 _MessageBox::SetPresenter(const _MessageBoxPresenter& msgboxPresenter)
262 {
263         __pMsgboxPresenter = const_cast <_MessageBoxPresenter*>(&msgboxPresenter);
264
265         return E_SUCCESS;
266 }
267
268 _PopupPresenter*
269 _MessageBox::GetPresenter(void)
270 {
271         return static_cast <_PopupPresenter*>(__pMsgboxPresenter);
272 }
273
274 void
275 _MessageBox::OnDraw()
276 {
277         __pMsgboxPresenter->Draw();
278         if(unlikely((_AccessibilityManager::IsActivated())))
279         {
280                 _AccessibilityManager::GetInstance()->RequestAutoReading(_ACCESSIBILITY_AUTO_READING_MODE_FIRST_ITEM);
281         }
282         return;
283 }
284
285 void
286 _MessageBox::OnFontChanged(Font * pFont)
287 {
288         __pMsgboxPresenter->OnFontChanged(pFont);
289 }
290
291 void
292 _MessageBox::OnFontInfoRequested(unsigned long& style, float& size)
293 {
294         __pMsgboxPresenter->OnFontInfoRequested(style, size);
295 }
296
297 void
298 _MessageBox::OnActivated(void)
299 {
300         _Popup::OnActivated();
301 }
302
303 void
304 _MessageBox::OnDeactivated(void)
305 {
306         _Popup::OnDeactivated();
307 }
308
309 void
310 _MessageBox::InitializeAccessibilityElement(void)
311 {
312         result r = E_SUCCESS;
313
314         _Popup::InitializeAccessibilityElement();
315
316         if (_pTitleTextAccessibilityElement != null)
317         {
318                 _pTitleTextAccessibilityElement->SetName(L"MessageBoxTitleText");
319         }
320
321         __pMsgboxPresenter->InitializeAccessibilityElement();
322         r = GetLastResult();
323         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
324
325         return;
326 }
327
328 result
329 _MessageBox::ShowAndWait(int& modalResult)
330 {
331         __pMsgboxPresenter->ShowAndWait(modalResult);
332
333         return E_SUCCESS;
334 }
335
336 String
337 _MessageBox::GetText(void) const
338 {
339         return __text;
340 }
341
342 unsigned long
343 _MessageBox::GetTimeout(void) const
344 {
345         return __timeout;
346 }
347
348 void
349 _MessageBox::SetTextColor(const Color& color)
350 {
351         __textColor = color;
352 }
353
354 Color
355 _MessageBox::GetTextColor(void) const
356 {
357         return __textColor;
358 }
359
360 MessageBoxStyle
361 _MessageBox::GetMsgBoxStyle(void) const
362 {
363         return __msgboxStyle;
364 }
365
366 float
367 _MessageBox::GetTotalHeight(void) const
368 {
369         float titleHeight = 0.0f;
370         float btnHeight = 0.0f;
371
372         float textTopMargin = 0.0f;
373         float textTopMarginNoButton = 0.0f;
374
375         float btnGap = 0.0f;
376         float btnTopMargin = 0.0f;
377         float btnBottomMargin = 0.0f;
378
379         float transTopMargin = 0.0f;
380         float transBottomMargin = 0.0f;
381
382         _ControlOrientation orientation;
383         orientation = GetOrientation();
384         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_HEIGHT, orientation, titleHeight);
385         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, btnHeight);
386
387         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_TOP_MRAGIN, orientation, textTopMargin);
388         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_TOP_MRAGIN_NO_BUTTON, orientation, textTopMarginNoButton);
389
390         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_INTERNAL_GAP, orientation, btnGap);
391         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, orientation, btnTopMargin);
392         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_BOTTOM_MARGIN, orientation, btnBottomMargin);
393
394         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_TOP_MARGIN, orientation, transTopMargin);
395         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_BOTTOM_MARGIN, orientation, transBottomMargin);
396
397         if (HasTitle() == false)
398         {
399                 titleHeight = 0.0f;
400         }
401
402         float totalH = titleHeight
403                         + transTopMargin
404                         + transBottomMargin
405                         + __pMsgboxPresenter->GetBodyTextObjHeight();
406
407         switch (__msgboxStyle)
408         {
409         case MSGBOX_STYLE_NONE:
410                 totalH += textTopMarginNoButton * 2.0f;
411                 break;
412
413         case MSGBOX_STYLE_OK:
414                         // fall through
415         case MSGBOX_STYLE_CANCEL:
416                         // fall through
417         case MSGBOX_STYLE_OKCANCEL:
418                         // fall through
419         case MSGBOX_STYLE_YESNO:
420                         // fall through
421         case MSGBOX_STYLE_RETRYCANCEL:
422                         // fall through
423         case MSGBOX_STYLE_YESNOCANCEL:
424                         // fall through
425         case MSGBOX_STYLE_ABORTRETRYIGNORE:
426                         // fall through
427         case MSGBOX_STYLE_CANCELTRYCONTINUE:
428                 totalH += textTopMargin * 2.0f
429                                 + btnHeight
430                                 + btnTopMargin
431                                 + btnBottomMargin;
432                 break;
433         }
434
435         return totalH;
436 }
437
438 void
439 _MessageBox::OnChangeLayout(_ControlOrientation orientation)
440 {
441         _Popup::OnChangeLayout(orientation);
442
443         result r = __pMsgboxPresenter->OnChangeLayout(orientation);
444         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
445
446         return;
447 }
448
449 void
450 _MessageBox::OnBoundsChanged(void)
451 {
452         _AccessibilityContainer* pContainer = null;
453
454         pContainer = GetAccessibilityContainer();
455
456         SetClientBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
457
458         if ((_pTitleTextAccessibilityElement != null) && (HasTitle() == true))
459         {
460                 _pTitleTextAccessibilityElement->SetBounds(__pMsgboxPresenter->GetTitleTextBounds());
461         }
462
463         if (GetMsgBoxStyle() == MSGBOX_STYLE_NONE && pContainer != null)
464         {
465                 _AccessibilityElement* pElementMessageBox = pContainer->GetChildElement(L"MessageBox");
466
467                 if (pElementMessageBox != null)
468                 {
469                         pElementMessageBox->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
470                 }
471         }
472
473         return;
474 }
475
476 bool
477 _MessageBox::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
478 {
479         if (&source != this)
480         {
481                 return false;
482         }
483
484         return __pMsgboxPresenter->OnTouchPressed(source, touchinfo);
485 }
486
487 bool
488 _MessageBox::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
489 {
490         if (&source != this)
491         {
492                 return false;
493         }
494
495         return __pMsgboxPresenter->OnTouchReleased(source, touchinfo);
496 }
497
498 bool
499 _MessageBox::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
500 {
501         if (&source != this)
502         {
503                 return false;
504         }
505
506         return __pMsgboxPresenter->OnTouchMoved(source, touchinfo);
507 }
508
509 void
510 _MessageBox::OnSettingChanged(String& key)
511 {
512         const wchar_t* LOCALE_LANGUAGE = L"http://tizen.org/setting/locale.language";
513         if (key == LOCALE_LANGUAGE)
514         {
515                 __pMsgboxPresenter->UpdateButtonString();
516         }
517 }
518
519 bool
520 _MessageBox::OnAccessibilityFocusMovedNext(const _AccessibilityContainer& control, const _AccessibilityElement& element)
521 {
522         return false;
523 }
524
525 bool
526 _MessageBox::OnAccessibilityFocusMovedPrevious(const _AccessibilityContainer& control, const _AccessibilityElement& element)
527 {
528         return false;
529 }
530
531 bool
532 _MessageBox::OnAccessibilityReadElement(const _AccessibilityContainer& control, const _AccessibilityElement& element)
533 {
534         return false;
535 }
536
537 bool
538 _MessageBox::OnAccessibilityReadingElement(const _AccessibilityContainer& control, const _AccessibilityElement& element)
539 {
540         return false;
541 }
542
543 bool
544 _MessageBox::OnAccessibilityFocusIn(const _AccessibilityContainer& control, const _AccessibilityElement& element)
545 {
546         return false;
547 }
548
549 bool
550 _MessageBox::OnAccessibilityFocusOut(const _AccessibilityContainer& control, const _AccessibilityElement& element)
551 {
552         return false;
553 }
554
555 bool
556 _MessageBox::OnAccessibilityActionPerformed(const _AccessibilityContainer& control, const _AccessibilityElement& element)
557 {
558         return __pMsgboxPresenter->OnAccessibilityActionPerformed(control, element);
559 }
560
561 bool
562 _MessageBox::OnAccessibilityValueIncreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
563 {
564         return false;
565 }
566
567 bool
568 _MessageBox::OnAccessibilityValueDecreased(const _AccessibilityContainer& control, const _AccessibilityElement& element)
569 {
570         return false;
571 }
572
573 bool
574 _MessageBox::OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
575 {
576         return __pMsgboxPresenter->OnKeyReleased(source, keyInfo);
577 }
578
579 Bitmap*
580 _MessageBox::GetButtonBackgroundBitmap(void)
581 {
582         return __pComposedButtonBitmap;
583 }
584
585 Bitmap*
586 _MessageBox::GetButtonBackgroundEffectBitmap(void)
587 {
588         return __pButtonEffectBitmap;
589 }
590
591 Bitmap*
592 _MessageBox::GetTitleBackgroundBitmap(void)
593 {
594         return __pComposedTitleBitmap;
595 }
596
597 Bitmap*
598 _MessageBox::GetTitleBackgroundEffectBitmap(void)
599 {
600         return __pTitleEffectBitmap;
601 }
602 }}} // Tizen::Ui::Controls
603