Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_MessageBoxPresenter.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_MessageBoxPresenter.cpp
20  * @brief       This is the implementation file for the _MessageBoxPresenter class.
21  */
22
23 #include <FUiCtrlMessageBox.h>
24 #include <FBaseSysLog.h>
25 #include <FBaseErrorDefine.h>
26 #include <FGrp_CanvasImpl.h>
27 #include <FGrp_TextTextSimple.h>
28 #include "FUi_AccessibilityContainer.h"
29 #include "FUi_AccessibilityElement.h"
30 #include "FUi_ModalLoopManager.h"
31 #include "FUi_ResourceManager.h"
32 #include "FUi_TouchManager.h"
33 #include "FUiCtrl_ButtonImpl.h"
34 #include "FUiCtrl_Button.h"
35 #include "FUiCtrl_Scroll.h"
36 #include "FUiCtrl_MessageBox.h"
37 #include "FUiCtrl_MessageBoxPresenter.h"
38
39
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Runtime;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Graphics::_Text;
44 using namespace Tizen::Ui;
45 using namespace Tizen::Ui::Animations;
46
47
48 namespace Tizen { namespace Ui { namespace Controls
49 {
50
51
52 _MessageBoxPresenter::_MessageBoxPresenter(void)
53         : __pMessageBox(null)
54         , __pButtonListener(null)
55         , __msgboxResult(MSGBOX_RESULT_CLOSE)
56         , __pTextAccessibilityElement(null)
57         , __pTitleTextObject(null)
58         , __pBodyTextObject(null)
59         , __titleBounds(0, 0, 0, 0)
60         , __textBounds(0, 0, 0, 0)
61         , __pScroll(null)
62         , __fontStyle(0)
63         , __fontSize(0)
64         , __scrollPos(0)
65         , __buttonNum(0)
66         , __prevPositionY(-1)
67         , __textObjHeight(0)
68         , __touchPressFlag(false)
69         , __scrollStart(false)
70         , __btnClickFlag(false)
71         , __bodyClickFlag(false)
72         , __touchOutBounds(false)
73 {
74         for (int i = 0; i < 3; i++)
75         {
76                 __pButtons[i] = null;
77         }
78 }
79
80 _MessageBoxPresenter::~_MessageBoxPresenter(void)
81 {
82         for (int i = 0; i < __buttonNum; i++)
83         {
84                 delete __pButtons[i];
85                 __pButtons[i] = null;
86         }
87
88         delete __pButtonListener;
89         __pButtonListener = null;
90
91         if (__pTitleTextObject != null)
92         {
93                 __pTitleTextObject->RemoveAll();
94                 delete __pTitleTextObject;
95                 __pTitleTextObject = null;
96         }
97
98         if (__pBodyTextObject != null)
99         {
100                 __pBodyTextObject->RemoveAll();
101                 delete __pBodyTextObject;
102                 __pBodyTextObject = null;
103         }
104
105         if (__pScroll != null)
106         {
107                 __pMessageBox->DetachSystemChild(*__pScroll);
108                 delete __pScroll;
109                 __pScroll = null;
110         }
111
112         if (__pTextAccessibilityElement)
113         {
114                 __pTextAccessibilityElement->Activate(false);
115                 __pTextAccessibilityElement = null;
116         }
117 }
118
119 result
120 _MessageBoxPresenter::Initialize(_MessageBox& msgbox)
121 {
122         result r = E_SUCCESS;
123         String titleText = L"";
124         String bodyText = L"";
125         TextSimple* pSimpleTextForBodyText = null;
126
127         int titleTextSize = 0;
128         int bodyTextSize = 0;
129
130         int defaultWidth = 0;
131         int titleHeight = 0;
132         int bottomHeight = 0;
133         int maxHeight = 0;
134
135         int btnBottomMargin = 0;
136         int textTopMargin = 0;
137         int textLeftMargin = 0;
138         int textRightMargin = 0;
139
140         int titleTopMargin = 0;
141         int titleLeftMargin = 0;
142         int titleRightMargin = 0;
143
144         int transTopMargin = 0;
145         int transBottomMargin = 0;
146         int transLeftMargin = 0;
147         int transRightMargin = 0;
148
149         int totalHeightExceptTextObj = 0;
150         int tempHeight = 0;
151
152         __pMessageBox = &msgbox;
153
154         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_TEXT_SIZE, __pMessageBox->GetOrientation(), titleTextSize);
155         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_SIZE, __pMessageBox->GetOrientation(), bodyTextSize);
156
157         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, __pMessageBox->GetOrientation(), defaultWidth);
158         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_HEIGHT, __pMessageBox->GetOrientation(), titleHeight);
159         GET_SHAPE_CONFIG(MESSAGEBOX::BOTTOM_HEIGHT, __pMessageBox->GetOrientation(), bottomHeight);
160         GET_SHAPE_CONFIG(MESSAGEBOX::MAX_HEIGHT, __pMessageBox->GetOrientation(), maxHeight);
161
162         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_BOTTOM_MARGIN, __pMessageBox->GetOrientation(), btnBottomMargin);
163         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_TOP_MRAGIN, __pMessageBox->GetOrientation(), textTopMargin);
164         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_LEFT_MARGIN, __pMessageBox->GetOrientation(), textLeftMargin);
165         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_RIGHT_MARGIN, __pMessageBox->GetOrientation(), textRightMargin);
166
167         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_TEXT_TOP_MARGIN, __pMessageBox->GetOrientation(), titleTopMargin);
168         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_TEXT_LEFT_MARGIN, __pMessageBox->GetOrientation(), titleLeftMargin);
169         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_TEXT_RIGHT_MARGIN, __pMessageBox->GetOrientation(), titleRightMargin);
170
171         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_TOP_MARGIN, __pMessageBox->GetOrientation(), transTopMargin);
172         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_BOTTOM_MARGIN, __pMessageBox->GetOrientation(), transBottomMargin);
173         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_LEFT_MARGIN, __pMessageBox->GetOrientation(), transLeftMargin);
174         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_RIGHT_MARGIN, __pMessageBox->GetOrientation(), transRightMargin);
175
176         // TitleText
177         if (__pMessageBox->HasTitle() == true)
178         {
179                 r = SetFontInfo(FONT_STYLE_PLAIN, titleTextSize);
180                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to set font.", GetErrorMessage(r));
181
182                 __pTitleTextObject = new (std::nothrow) TextObject();
183                 SysTryReturn(NID_UI_CTRL, __pTitleTextObject != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create TitleTextObject.");
184
185                 __pTitleTextObject->Construct();
186                 TextSimple* pSimpleTextForTitleText = new (std::nothrow) TextSimple(const_cast <wchar_t*>(__pMessageBox->GetTitleText().GetPointer()),
187                                                                                                                                                                                 __pMessageBox->GetTitleText().GetLength(),
188                                                                                                                                                                                 TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
189                 SysTryCatch(NID_UI_CTRL, pSimpleTextForTitleText != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
190
191                 __pTitleTextObject->AppendElement(*pSimpleTextForTitleText);
192                 __pTitleTextObject->SetForegroundColor(__pMessageBox->GetTitleTextColor(), 0, __pTitleTextObject->GetTextLength());
193                 __pTitleTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
194                 __pTitleTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
195                 __pTitleTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
196
197                 __pTitleTextObject->SetFont(_pFont, 0, __pTitleTextObject->GetTextLength());
198                 __pTitleTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT);
199                 __pTitleTextObject->SetBounds(Rectangle(titleLeftMargin + transLeftMargin,
200                                                                                         titleTopMargin + transTopMargin,
201                                                                                         defaultWidth - (titleLeftMargin + titleRightMargin + transLeftMargin + transRightMargin),
202                                                                                         titleHeight - titleTopMargin));
203                 __pTitleTextObject->Compose();
204
205                 __titleBounds = Rectangle(titleLeftMargin + transLeftMargin,
206                                                         titleTopMargin + transTopMargin,
207                                                         defaultWidth - (titleLeftMargin + titleRightMargin + transLeftMargin + transRightMargin),
208                                                         __pTitleTextObject->GetTotalHeight());
209
210         }
211         else
212         {
213                 titleHeight = 0;
214         }
215
216         // Text
217         r = SetFontInfo(FONT_STYLE_PLAIN, bodyTextSize);
218         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set font.", GetErrorMessage(r));
219
220         __pBodyTextObject = new (std::nothrow) TextObject();
221         SysTryCatch(NID_UI_CTRL, __pBodyTextObject != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
222
223         __pBodyTextObject->Construct();
224
225         pSimpleTextForBodyText = new (std::nothrow) TextSimple(const_cast <wchar_t*>(__pMessageBox->GetText().GetPointer()),
226                                                                                                                                                         __pMessageBox->GetText().GetLength(),
227                                                                                                                                                         TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
228         SysTryCatch(NID_UI_CTRL, pSimpleTextForBodyText != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
229
230         __pBodyTextObject->AppendElement(*pSimpleTextForBodyText);
231         __pBodyTextObject->SetForegroundColor(__pMessageBox->GetTextColor(), 0, __pBodyTextObject->GetTextLength());
232         __pBodyTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
233         __pBodyTextObject->SetFont(_pFont, 0, __pBodyTextObject->GetTextLength());
234         __pBodyTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT);
235         __pBodyTextObject->SetBounds(Rectangle(textLeftMargin + transLeftMargin,
236                                                                                         titleHeight + textTopMargin + transTopMargin,
237                                                                                         defaultWidth - (textLeftMargin + textRightMargin + transLeftMargin + transRightMargin),
238                                                                                         maxHeight - titleHeight));
239         __pBodyTextObject->Compose();
240
241         __textObjHeight = __pBodyTextObject->GetTotalHeight();
242
243         // Text bounds
244         totalHeightExceptTextObj = titleHeight + (textTopMargin * 2) + bottomHeight + (transTopMargin + transBottomMargin);
245         tempHeight = maxHeight - (totalHeightExceptTextObj + __textObjHeight);
246
247         __textObjHeight = (tempHeight > 0) ? __textObjHeight : __textObjHeight + tempHeight;
248
249         __textBounds.SetBounds(textLeftMargin+ transLeftMargin,
250                                                         titleHeight + textTopMargin + transTopMargin,
251                                                         defaultWidth - (textLeftMargin + textRightMargin + transLeftMargin + transRightMargin),
252                                                         __textObjHeight);
253
254         __pBodyTextObject->SetBounds(__textBounds);
255
256         // Scroll in Text
257         if (__pBodyTextObject->GetTotalHeight() > __textObjHeight)
258         {
259                 int maxH = __pBodyTextObject->GetTotalHeight();
260
261                 __pScroll = _Scroll::CreateScrollN(*__pMessageBox,
262                                                                         SCROLL_DIRECTION_VERTICAL,
263                                                                         true,
264                                                                         true,
265                                                                         false,
266                                                                         false,
267                                                                         __textObjHeight,
268                                                                         maxH,
269                                                                         0);
270
271                 SysTryCatch(NID_UI_CTRL, __pScroll != null, , E_OUT_OF_MEMORY, "[E_SYSTEM] Failed to create the scroll.");
272
273                 __pMessageBox->AttachSystemChild(*__pScroll);
274         }
275
276         r = CreateButtons();
277         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
278
279         return r;
280
281 CATCH:
282         delete __pTitleTextObject;
283         __pTitleTextObject = null;
284
285         delete __pBodyTextObject;
286         __pBodyTextObject = null;
287
288         delete pSimpleTextForBodyText;
289
290         SetLastResult(r);
291         return r;
292 }
293
294 void
295 _MessageBoxPresenter::OnFontChanged(Font* pFont)
296 {
297         _pFont = pFont;
298 }
299
300 void
301 _MessageBoxPresenter::OnFontInfoRequested(unsigned long& style, int& size)
302 {
303         style = __fontStyle;
304         size = __fontSize;
305 }
306
307 result
308 _MessageBoxPresenter::SetFontInfo(unsigned long style, int size)
309 {
310         __fontStyle = style;
311         __fontSize = size;
312
313         _pFont = __pMessageBox->GetFallbackFont();
314         SysTryReturnResult(NID_UI_CTRL, _pFont != null, GetLastResult(), "Failed to get a font.");
315
316         return E_SUCCESS;
317 }
318
319 void
320 _MessageBoxPresenter::Draw(void)
321 {
322         result r = E_SUCCESS;
323
324         int defaultWidth = 0;
325         int titleHeight = 0;
326         int titleTextSize = 0;
327         int titleTopMargin = 0;
328
329         int btnHeight = 0;
330         int btnTopMargin = 0;
331         int btnBottomMargin = 0;
332
333         int textSize = 0;
334         int textTopMarginNoButton = 0;
335         int textTopMargin = 0;
336
337         int transTopMargin = 0;
338         int transBottomMargin = 0;
339         int transLeftMargin = 0;
340         int transRightMargin = 0;
341
342         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, __pMessageBox->GetOrientation(), defaultWidth);
343         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_HEIGHT, __pMessageBox->GetOrientation(), titleHeight);
344         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_TEXT_SIZE, __pMessageBox->GetOrientation(), titleTextSize);
345         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_TEXT_TOP_MARGIN, __pMessageBox->GetOrientation(), titleTopMargin);
346
347         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_SIZE, __pMessageBox->GetOrientation(), textSize);
348         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_TOP_MRAGIN, __pMessageBox->GetOrientation(), textTopMargin);
349         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_TOP_MRAGIN_NO_BUTTON, __pMessageBox->GetOrientation(), textTopMarginNoButton);
350
351         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, __pMessageBox->GetOrientation(), btnHeight);
352         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __pMessageBox->GetOrientation(), btnTopMargin);
353         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_BOTTOM_MARGIN, __pMessageBox->GetOrientation(), btnBottomMargin);
354
355         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_TOP_MARGIN, __pMessageBox->GetOrientation(), transTopMargin);
356         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_BOTTOM_MARGIN, __pMessageBox->GetOrientation(), transBottomMargin);
357         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_LEFT_MARGIN, __pMessageBox->GetOrientation(), transLeftMargin);
358         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_RIGHT_MARGIN, __pMessageBox->GetOrientation(), transRightMargin);
359
360         Canvas* pCanvas = __pMessageBox->GetCanvasN();
361         SysTryReturnVoidResult(NID_UI_CTRL, (pCanvas != null), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create canvas.");
362
363         pCanvas->SetBackgroundColor(Color(0x00000000));
364         pCanvas->Clear();
365
366         bool hasBgBitmap = (__pMessageBox->GetBackgroundBitmap() != null);
367
368         int totalH = __pMessageBox->GetTotalHeight();
369
370         // Draw BG
371         if (hasBgBitmap == true)
372         {
373                 Rectangle bounds(0, 0, defaultWidth, totalH);
374
375                 const Bitmap* pBackgroundBitmap = __pMessageBox->GetBackgroundBitmap();
376                 if (pBackgroundBitmap->IsNinePatchedBitmap() == true)
377                 {
378                         pCanvas->DrawNinePatchedBitmap(bounds, *pBackgroundBitmap);
379                 }
380                 else
381                 {
382                         pCanvas->DrawBitmap(bounds, *pBackgroundBitmap);
383                 }
384         }
385
386         // Draw Title
387         if (__pTitleTextObject != null)
388         {
389                 r = SetFontInfo(FONT_STYLE_PLAIN, titleTextSize);
390                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
391
392                 __pTitleTextObject->SetFont(_pFont, 0, __pTitleTextObject->GetTextLength());
393                 __pTitleTextObject->SetBounds(__titleBounds);
394                 __pTitleTextObject->SetForegroundColor(__pMessageBox->GetTitleTextColor(), 0, __pTitleTextObject->GetTextLength());
395
396                 __pTitleTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
397         }
398
399         // Draw Text
400         if (__pBodyTextObject != null)
401         {
402                 r = SetFontInfo(FONT_STYLE_PLAIN, textSize);
403                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
404
405                 __pBodyTextObject->SetFont(_pFont, 0, __pBodyTextObject->GetTextLength());
406                 __pBodyTextObject->SetForegroundColor(__pMessageBox->GetTextColor(), 0, __pBodyTextObject->GetTextLength());
407
408                 __pBodyTextObject->SetBounds(__textBounds);
409                 if ((__pMessageBox->HasTitle() == false) && (__pMessageBox->GetMsgBoxStyle() == MSGBOX_STYLE_NONE))
410                 {
411                         __textBounds.y = transTopMargin + textTopMarginNoButton;
412                         __pBodyTextObject->SetBounds(__textBounds);
413                 }
414
415                 __pBodyTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
416         }
417
418         //fall thtrough
419 CATCH:
420         delete pCanvas;
421
422         return;
423 }
424
425 result
426 _MessageBoxPresenter::ShowAndWait(int& modalResult)
427 {
428         result r = E_SUCCESS;
429
430         for (int i = 0; i < __buttonNum; i++)
431         {
432                 _ControlImpl* pImpl = _ControlImpl::GetInstance(*__pButtons[i]);
433                 __pMessageBox->AttachChild(pImpl->GetCore());
434         }
435
436         r = __pMessageBox->Open();
437
438         if (__pMessageBox->GetMsgBoxStyle() == MSGBOX_STYLE_NONE)
439         {
440                 __pMessageBox->SetTouchCapture(true);
441         }
442         else
443         {
444                 __pMessageBox->SetTouchCapture();
445         }
446
447         ProcessEvent();
448
449         __pMessageBox->ReleaseTouchCapture();
450
451         __pMessageBox->Close();
452
453
454         modalResult = GetReturnValue();
455
456         return r;
457 }
458
459 void
460 _MessageBoxPresenter::ProcessEvent(void)
461 {
462         _ControlHandle handle;
463
464         _TouchManager* pTouchManager = _TouchManager::GetInstance();
465         if (pTouchManager)
466         {
467                 _Control* pControl = pTouchManager->GetTouchControlSource();
468                 if (pControl)
469                 {
470                         pControl->SetEventReceivable(false);
471                 }
472         }
473
474         _ModalLoopManager::GetInstance()->BeginMainLoop(__pMessageBox->GetTimeout(), MSGBOX_RESULT_CLOSE);
475
476         int exitCode = _ModalLoopManager::GetInstance()->GetLastExitCode();
477         switch (exitCode)
478         {
479         case MSGBOX_BUTTON_CLICKED:
480                 if (__pButtonListener->IsButtonClicked())
481                 {
482                         handle = __pButtonListener->GetClickedButtonHandle();
483                         FindButtonAndResult(handle);
484                         break;
485                 }
486                 // fall through
487         case _ModalLoopManager::MODAL_LOOP_EXIT_CODE_APP_TERMINATION:
488         case _ModalLoopManager::MODAL_LOOP_EXIT_CODE_CONTINUOUS_TERMINATION:
489                 _ModalLoopManager::GetInstance()->EndMainLoop(exitCode, false);
490                 break;
491         }
492  }
493
494 result
495 _MessageBoxPresenter::CreateButtons(void)
496 {
497         String buttonText[3];
498
499         switch (__pMessageBox->GetMsgBoxStyle())
500         {
501         case MSGBOX_STYLE_NONE:
502                 __buttonNum = 0;
503                 break;
504
505         case MSGBOX_STYLE_OK:
506                 __buttonNum = 1;
507                 GET_STRING_CONFIG(IDS_COM_SK_OK, buttonText[0]);
508                 break;
509
510         case MSGBOX_STYLE_CANCEL:
511                 __buttonNum = 1;
512                 GET_STRING_CONFIG(IDS_COM_SK_CANCEL, buttonText[0]);
513                 break;
514
515         case MSGBOX_STYLE_OKCANCEL:
516                 __buttonNum = 2;
517                 GET_STRING_CONFIG(IDS_COM_SK_OK, buttonText[0]);
518                 GET_STRING_CONFIG(IDS_COM_SK_CANCEL, buttonText[1]);
519                 break;
520
521         case MSGBOX_STYLE_YESNO:
522                 __buttonNum = 2;
523                 GET_STRING_CONFIG(IDS_COM_SK_YES, buttonText[0]);
524                 GET_STRING_CONFIG(IDS_COM_SK_NO, buttonText[1]);
525                 break;
526
527         case MSGBOX_STYLE_YESNOCANCEL:
528                 __buttonNum = 3;
529                 GET_STRING_CONFIG(IDS_COM_SK_YES, buttonText[0]);
530                 GET_STRING_CONFIG(IDS_COM_SK_NO, buttonText[1]);
531                 GET_STRING_CONFIG(IDS_COM_SK_CANCEL, buttonText[2]);
532                 break;
533
534         case MSGBOX_STYLE_ABORTRETRYIGNORE:
535                 __buttonNum = 3;
536                 GET_STRING_CONFIG(IDS_COM_SK_ABORT, buttonText[0]);
537                 GET_STRING_CONFIG(IDS_COM_SK_RETRY, buttonText[1]);
538                 GET_STRING_CONFIG(IDS_COM_SK_IGNORE, buttonText[2]);
539                 break;
540
541         case MSGBOX_STYLE_CANCELTRYCONTINUE:
542                 __buttonNum = 3;
543                 GET_STRING_CONFIG(IDS_COM_SK_CANCEL, buttonText[0]);
544                 GET_STRING_CONFIG(IDS_COM_SK_TRY, buttonText[1]);
545                 GET_STRING_CONFIG(IDS_COM_BODY_CONTINUE, buttonText[2]);
546                 break;
547
548         case MSGBOX_STYLE_RETRYCANCEL:
549                 __buttonNum = 2;
550                 GET_STRING_CONFIG(IDS_COM_SK_RETRY, buttonText[0]);
551                 GET_STRING_CONFIG(IDS_COM_SK_CANCEL, buttonText[1]);
552                 break;
553
554         default:
555                 break;
556         }
557
558         __pButtonListener = new (std::nothrow) _MessageBoxButtonListener();
559         SysTryReturn(NID_UI_CTRL, __pButtonListener != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
560
561         result r = E_SUCCESS;
562
563         for (int i = 0; i < __buttonNum; i++)
564         {
565                 _ControlImpl* pImpl = null;
566                 _Button* pButtonCore = null;
567
568                 __pButtons[i] = new (std::nothrow) Button();
569                 SysTryCatch(NID_UI_CTRL, __pButtons[i] != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
570
571                 r = __pButtons[i]->Construct(CalculateButtonPositionAndSize(i));
572                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
573
574                 __pButtons[i]->SetText(buttonText[i]);
575                 __pButtons[i]->AddTouchEventListener(*__pButtonListener);
576
577                 pImpl = _ControlImpl::GetInstance(*__pButtons[i]);
578                 pButtonCore = dynamic_cast<_Button*>(&pImpl->GetCore());
579
580                 if (pButtonCore != null)
581                 {
582                         pButtonCore->SetTouchMoveAllowance(TOUCH_MOVE_ALLOWANCE_INSENSITIVE);
583                 }
584         }
585
586         return r;
587
588 CATCH:
589         delete __pButtonListener;
590         __pButtonListener = null;
591
592         for (int i = 0; i < __buttonNum; i++)
593         {
594                 delete __pButtons[i];
595                 __pButtons[i] = null;
596         }
597
598         return r;
599 }
600
601 Rectangle
602 _MessageBoxPresenter::CalculateButtonPositionAndSize(int buttonIndex)
603 {
604         int x = 0;
605         int y = 0;
606         int w = 0;
607         int h = 0;
608
609         int defaultWidth = 0;
610         int btnHeight = 0;
611
612         int btnSideMargin1 = 0;
613         int btnSideMargin2 = 0;
614         int btnSideMargin3 = 0;
615         int btnTopMargin = 0;
616
617         int btnGap = 0;
618         int textTopMargin = 0;
619
620         int titleHeight = 0;
621         int transTopMargin = 0;
622
623         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, __pMessageBox->GetOrientation(), defaultWidth);
624         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, __pMessageBox->GetOrientation(), btnHeight);
625
626         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_01, __pMessageBox->GetOrientation(), btnSideMargin1);
627         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_02, __pMessageBox->GetOrientation(), btnSideMargin2);
628         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_03, __pMessageBox->GetOrientation(), btnSideMargin3);
629         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __pMessageBox->GetOrientation(), btnTopMargin);
630
631         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_INTERNAL_GAP, __pMessageBox->GetOrientation(), btnGap);
632         GET_SHAPE_CONFIG(MESSAGEBOX::TEXT_TOP_MRAGIN, __pMessageBox->GetOrientation(), textTopMargin);
633
634         GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_HEIGHT, __pMessageBox->GetOrientation(), titleHeight);
635         GET_SHAPE_CONFIG(MESSAGEBOX::BG_IMAGE_TRANSPARENT_TOP_MARGIN, __pMessageBox->GetOrientation(), transTopMargin);
636
637         if (__pMessageBox->HasTitle() == false)
638         {
639                 titleHeight = 0;
640         }
641
642         switch (__buttonNum)
643         {
644         case 1:
645                 w = defaultWidth - (btnSideMargin1 * 2);
646                 h = btnHeight;
647                 x = btnSideMargin1;
648                 y = transTopMargin + titleHeight + (textTopMargin * 2) + __textObjHeight + btnTopMargin;
649                 break;
650
651         case 2:
652                 w = (defaultWidth - (btnSideMargin2 * 2) - btnGap) / 2;
653                 h = btnHeight;
654                 x = btnSideMargin2 + (buttonIndex * (w + btnGap));
655                 y = transTopMargin + titleHeight + (textTopMargin * 2) + __textObjHeight + btnTopMargin;
656                 break;
657
658         case 3:
659         {
660                 switch (buttonIndex)
661                 {
662                 case 0:
663                         // fall through
664                 case 1:
665                         // fall through
666                 case 2:
667                         w = (defaultWidth - ((btnSideMargin3 + btnGap) * 2)) / 3;
668                         h = btnHeight;
669                         x = btnSideMargin3 + (buttonIndex * (w + btnGap));
670                         y = transTopMargin + titleHeight + (textTopMargin * 2) + __textObjHeight + btnTopMargin;
671                         break;
672                 }
673                 break;
674         }
675         default:
676                 break;
677         }
678
679         return Rectangle(x, y, w, h);
680 }
681
682 void
683 _MessageBoxPresenter::FindButtonAndResult(const _ControlHandle handle)
684 {
685         int btnPos = -1;
686
687         for (int i = 0; i < __buttonNum; i++)
688         {
689                 _ControlImpl* pImpl = _ControlImpl::GetInstance(*__pButtons[i]);
690                 if (pImpl->GetCore().GetHandle() == handle)
691                 {
692                         btnPos = i;
693                         break;
694                 }
695         }
696
697         switch (__pMessageBox->GetMsgBoxStyle())
698         {
699         case MSGBOX_STYLE_NONE:
700                 SetReturnValue(MSGBOX_RESULT_CLOSE);
701                 break;
702
703         case MSGBOX_STYLE_OK:
704                 SetReturnValue(MSGBOX_RESULT_OK);
705                 break;
706
707         case MSGBOX_STYLE_CANCEL:
708                 SetReturnValue(MSGBOX_RESULT_CANCEL);
709                 break;
710
711         case MSGBOX_STYLE_OKCANCEL:
712                 switch (btnPos)
713                 {
714                 case 0:
715                         SetReturnValue(MSGBOX_RESULT_OK);
716                         break;
717
718                 case 1:
719                         SetReturnValue(MSGBOX_RESULT_CANCEL);
720                         break;
721                 }
722                 break;
723
724         case MSGBOX_STYLE_YESNO:
725                 switch (btnPos)
726                 {
727                 case 0:
728                         SetReturnValue(MSGBOX_RESULT_YES);
729                         break;
730
731                 case 1:
732                         SetReturnValue(MSGBOX_RESULT_NO);
733                         break;
734                 }
735                 break;
736
737         case MSGBOX_STYLE_YESNOCANCEL:
738                 switch (btnPos)
739                 {
740                 case 0:
741                         SetReturnValue(MSGBOX_RESULT_YES);
742                         break;
743
744                 case 1:
745                         SetReturnValue(MSGBOX_RESULT_NO);
746                         break;
747
748                 case 2:
749                         SetReturnValue(MSGBOX_RESULT_CANCEL);
750                         break;
751                 }
752                 break;
753
754         case MSGBOX_STYLE_ABORTRETRYIGNORE:
755                 switch (btnPos)
756                 {
757                 case 0:
758                         SetReturnValue(MSGBOX_RESULT_ABORT);
759                         break;
760
761                 case 1:
762                         SetReturnValue(MSGBOX_RESULT_RETRY);
763                         break;
764
765                 case 2:
766                         SetReturnValue(MSGBOX_RESULT_IGNORE);
767                         break;
768                 }
769                 break;
770
771         case MSGBOX_STYLE_CANCELTRYCONTINUE:
772                 switch (btnPos)
773                 {
774                 case 0:
775                         SetReturnValue(MSGBOX_RESULT_CANCEL);
776                         break;
777
778                 case 1:
779                         SetReturnValue(MSGBOX_RESULT_TRY);
780                         break;
781
782                 case 2:
783                         SetReturnValue(MSGBOX_RESULT_CONTINUE);
784                         break;
785                 }
786                 break;
787
788         case MSGBOX_STYLE_RETRYCANCEL:
789                 switch (btnPos)
790                 {
791                 case 0:
792                         SetReturnValue(MSGBOX_RESULT_RETRY);
793                         break;
794
795                 case 1:
796                         SetReturnValue(MSGBOX_RESULT_CANCEL);
797                         break;
798                 }
799                 break;
800
801         default:
802                 break;
803         }
804 }
805
806 int
807 _MessageBoxPresenter::CalculateButtonAreaHeight(void)
808 {
809         int btnHeight = 0;
810
811         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, __pMessageBox->GetOrientation(), btnHeight);
812
813         switch (__pMessageBox->GetMsgBoxStyle())
814         {
815         case MSGBOX_STYLE_NONE:
816                 return 0;
817
818         case MSGBOX_STYLE_OK:
819                                         // fall through
820         case MSGBOX_STYLE_CANCEL:
821                                         // fall through
822         case MSGBOX_STYLE_OKCANCEL:
823                                         // fall through
824         case MSGBOX_STYLE_YESNO:
825                                         // fall through
826         case MSGBOX_STYLE_RETRYCANCEL:
827                                         // fall through
828         case MSGBOX_STYLE_YESNOCANCEL:
829                                         // fall through
830         case MSGBOX_STYLE_ABORTRETRYIGNORE:
831                                         // fall through
832         case MSGBOX_STYLE_CANCELTRYCONTINUE:
833                 return btnHeight;
834         }
835
836         return -1;
837 }
838
839 int
840 _MessageBoxPresenter::GetBodyTextHeight(void) const
841 {
842         return (__pBodyTextObject == null) ? 0 : __pBodyTextObject->GetTotalHeight();
843 }
844
845 Rectangle
846 _MessageBoxPresenter::GetTitleBounds(void) const
847 {
848         return __titleBounds;
849 }
850
851 Rectangle
852 _MessageBoxPresenter::GetTextBounds(void) const
853 {
854         return __textBounds;
855 }
856
857 void
858 _MessageBoxPresenter::SetReturnValue(MessageBoxModalResult rtn)
859 {
860         __msgboxResult = rtn;
861 }
862
863 int
864 _MessageBoxPresenter::GetReturnValue(void) const
865 {
866         return (int) __msgboxResult;
867 }
868
869 result
870 _MessageBoxPresenter::OnAttachedToMainTree(void)
871 {
872         result r = E_SUCCESS;
873         _AccessibilityElement* pTitleTextAccessibility = null;
874
875         if (likely(!(_AccessibilityManager::IsActivated())))
876         {
877                 return r;
878         }
879
880         pTitleTextAccessibility = __pMessageBox->GetTitleTextAccessibilityElement();
881         if ((pTitleTextAccessibility != null) && (__pTextAccessibilityElement != null))
882         {
883                 return r;
884         }
885
886         _AccessibilityContainer* pContainer = __pMessageBox->GetAccessibilityContainer();
887         if (pContainer != null)
888         {
889                 int titleHeight = 0;
890                 GET_SHAPE_CONFIG(MESSAGEBOX::TITLE_HEIGHT, __pMessageBox->GetOrientation(), titleHeight);
891
892                 if (pTitleTextAccessibility == null)
893                 {
894                         pTitleTextAccessibility = new (std::nothrow) _AccessibilityElement(true);
895                         SysTryReturn(NID_UI_CTRL, pTitleTextAccessibility != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,"[E_OUT_OF_MEMORY] Memory allocation failed.");
896
897                         pTitleTextAccessibility->Construct(L"MessageBoxTitleText", __titleBounds);
898                         pTitleTextAccessibility->SetLabel(__pMessageBox->GetTitleText());
899                         pTitleTextAccessibility->SetTrait(ACCESSIBILITY_TRAITS_TEXT_FIELD);
900
901                         pContainer->AddElement(*pTitleTextAccessibility);
902                 }
903
904                 if (__pTextAccessibilityElement == null)
905                 {
906                         __pTextAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
907                         SysTryReturn(NID_UI_CTRL, __pTextAccessibilityElement != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,"[E_OUT_OF_MEMORY] Memory allocation failed.");
908
909                         __pTextAccessibilityElement->Construct(L"MessageBoxText", __textBounds);
910                         __pTextAccessibilityElement->SetLabel(__pMessageBox->GetText());
911                         __pTextAccessibilityElement->SetTrait(ACCESSIBILITY_TRAITS_TEXT_FIELD);
912
913                         pContainer->AddElement(*__pTextAccessibilityElement);
914                 }
915
916                 if (__pMessageBox->GetMsgBoxStyle() != MSGBOX_STYLE_NONE)
917                 {
918                         for (int i = 0; i < __buttonNum; i++)
919                         {
920                                 _ControlImpl* pImpl = null;
921                                 _Button* pButtonCore = null;
922
923                                 pImpl = _ControlImpl::GetInstance(*__pButtons[i]);
924
925                                 pButtonCore = dynamic_cast<_Button*>(&pImpl->GetCore());
926                                 r = GetLastResult();
927                                 SysTryReturn(NID_UI_CTRL, pButtonCore != null, r, r, "[%s] Propagating.", GetErrorMessage(r));
928
929                                 _AccessibilityContainer* pButtonContainer = pButtonCore->GetAccessibilityContainer();
930                                 if (pButtonContainer != null)
931                                 {
932                                         _AccessibilityElement* pButtonElement = pButtonContainer->GetChildElement(L"ButtonText");
933                                         if (pButtonElement != null)
934                                         {
935                                                 String numberString = null;
936                                                 numberString = Integer::ToString(i + 1);
937
938                                                 String buttonName = L"MessageBoxButton";
939                                                 buttonName.Append(numberString);
940
941                                                 pButtonElement->SetName(buttonName);
942                                         }
943
944                                         // Add Container
945                                         pContainer->AddChildContainer(*pButtonContainer);
946                                 }
947                         }
948                 }
949         }
950
951         return r;
952
953 }
954
955 bool
956 _MessageBoxPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
957 {
958         Point point = touchinfo.GetCurrentPosition();
959
960         Rectangle rect(0, 0, __pMessageBox->GetBounds().width, __pMessageBox->GetBounds().height);
961
962         // Touch-event's starting point is MessageBox's leftTop
963         if (rect.Contains(touchinfo.GetCurrentPosition()) == false)
964         {
965                 __touchOutBounds = true;
966         }
967         else
968         {
969                 __touchOutBounds = false;
970         }
971
972         __touchPressFlag = true;
973
974         if ((__pScroll != null) && (__textBounds.Contains(touchinfo.GetCurrentPosition()) == true))
975         {
976                 __pScroll->SetScrollVisibility(true);
977
978                 __scrollStart = true;
979         }
980
981         return true;
982 }
983
984 bool
985 _MessageBoxPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
986 {
987         if (__touchOutBounds == true)
988         {
989                 if (__pMessageBox->GetMsgBoxStyle() == MSGBOX_STYLE_NONE)
990                 {
991                         SetReturnValue(MSGBOX_RESULT_CLOSE);
992                         _ModalLoopManager::GetInstance()->EndMainLoop(MSGBOX_RESULT_CLOSE, false);
993                 }
994         }
995
996         if (__touchPressFlag == false)
997         {
998                 __prevPositionY = -1;
999
1000                 __scrollStart = false;
1001         }
1002
1003         if (__pScroll != null)
1004         {
1005                 __pScroll->SetScrollVisibility(false);
1006         }
1007
1008         return true;
1009 }
1010
1011 bool
1012 _MessageBoxPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1013 {
1014         __touchOutBounds = false;
1015
1016         if ((__pScroll != null) && (__scrollStart == true))
1017         {
1018                 int scrollAmountView = -1;
1019                 int scrollAmountData = -1;
1020                 int currentPosition = touchinfo.GetCurrentPosition().y;
1021
1022                 // TextObject
1023                 if (__prevPositionY == -1)
1024                 {
1025                         __prevPositionY = touchinfo.GetCurrentPosition().y;
1026                 }
1027
1028                 int distance = __prevPositionY - currentPosition;
1029
1030                 int newFirstDspY = __pBodyTextObject->GetFirstDisplayPositionY();
1031                 newFirstDspY += distance;
1032
1033                 if (newFirstDspY < 0)
1034                 {
1035                         newFirstDspY = 0;
1036                 }
1037
1038                 if (newFirstDspY > __textBounds.height)
1039                 {
1040                         newFirstDspY = __textBounds.height;
1041                 }
1042
1043                 int moveLine = __pBodyTextObject->GetLineIndexAtPositionY(newFirstDspY);
1044
1045                 __pBodyTextObject->SetFirstDisplayLineIndex(moveLine);
1046                 __pBodyTextObject->SetFirstDisplayPositionY(newFirstDspY);
1047
1048                 // _Scroll
1049                 __pScroll->GetScrollRange(&scrollAmountView, &scrollAmountData);
1050                 __scrollPos += distance;
1051
1052                 if (__scrollPos < 0)
1053                 {
1054                         __scrollPos = 0;
1055                 }
1056                 else if (__scrollPos > scrollAmountView)
1057                 {
1058                         __scrollPos = scrollAmountView;
1059                 }
1060
1061                 __pScroll->SetScrollPosition(__scrollPos);
1062
1063                 __prevPositionY = currentPosition;
1064
1065                 __touchPressFlag = false;
1066
1067                 Draw();
1068                 __pMessageBox->Show();
1069         }
1070
1071         return true;
1072
1073 }
1074
1075
1076 ////////////////////////////////////////////////////////////////////////
1077 ///////////////////////////MsgBoxButtonListener/////////////////////////
1078 ////////////////////////////////////////////////////////////////////////
1079
1080 _MessageBoxPresenter::_MessageBoxButtonListener::_MessageBoxButtonListener(void)
1081         : __buttonHandle()
1082         , __buttonPressFlag(false)
1083         , __buttonReleaseState(false)
1084         , __point(0, 0)
1085 {
1086         // empty statement
1087 }
1088
1089 _MessageBoxPresenter::_MessageBoxButtonListener::~_MessageBoxButtonListener(void)
1090 {
1091         // empty statement
1092 }
1093
1094 _ControlHandle
1095 _MessageBoxPresenter::_MessageBoxButtonListener::GetClickedButtonHandle(void) const
1096 {
1097         return __buttonHandle;
1098 }
1099
1100 bool
1101 _MessageBoxPresenter::_MessageBoxButtonListener::IsButtonClicked(void)
1102 {
1103         return __buttonReleaseState;
1104 }
1105
1106 void
1107 _MessageBoxPresenter::_MessageBoxButtonListener::OnTouchPressed(const Control& source, const Point& currentPosition,
1108                                                                                                                                                 const TouchEventInfo& touchInfo)
1109 {
1110         __buttonPressFlag = true;
1111         __buttonReleaseState = false;
1112
1113         Point point = touchInfo.GetCurrentPosition();
1114         __point = point;
1115 }
1116
1117 void
1118 _MessageBoxPresenter::_MessageBoxButtonListener::OnTouchMoved(const Control& source, const Point& currentPosition,
1119                                                                                                                                                 const TouchEventInfo& touchInfo)
1120 {
1121         Point point = touchInfo.GetCurrentPosition();
1122
1123         __buttonPressFlag = false;
1124 }
1125
1126 void
1127 _MessageBoxPresenter::_MessageBoxButtonListener::OnTouchReleased(const Control& source, const Point& currentPosition,
1128                                                                                                                                                 const TouchEventInfo& touchInfo)
1129 {
1130         if (__buttonPressFlag == true)
1131         {
1132                 __buttonHandle = _ControlImpl::GetInstance(source)->GetCore().GetHandle();
1133                 SysTryReturnVoidResult(NID_UI_CTRL, __buttonHandle.IsValid() == true, E_SYSTEM, "[E_SYSTEM] Button's Handle is invalid");
1134
1135                 __buttonPressFlag = false;
1136                 __buttonReleaseState = true;
1137
1138                 _ModalLoopManager::GetInstance()->EndMainLoop(MSGBOX_BUTTON_CLICKED, false);
1139
1140                 return;
1141         }
1142 }
1143
1144
1145 }}}  // Tizen::Ui::Controls
1146