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