prompt popup change from sync to async
[framework/osp/web.git] / src / controls / FWebCtrl_PromptPopup.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 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                FWebCtrl_AuthConfirmPopup.cpp
20  * @brief               The file contains the definition of _AuthConfirmPopup class.
21  */
22 #include <ewk_view.h>
23 #include <FBaseColIList.h>
24 #include <FBaseSysLog.h>
25 #include <FGrpColor.h>
26 #include <FGrpDimension.h>
27 #include <FGrpRectangle.h>
28 #include <FUiCtrlButton.h>
29 #include <FUiCtrlEditField.h>
30 #include <FUiCtrlLabel.h>
31 #include <FUiCtrlPanel.h>
32 #include <FUiCtrlTextBox.h>
33 #include <FUiLayout.h>
34 #include <FUiVerticalBoxLayout.h>
35 #include <FWebCtrlAuthenticationChallenge.h>
36 #include <FBase_StringConverter.h>
37 #include <FSys_SystemResource.h>
38 #include <FUi_ControlManager.h>
39 #include <FUi_ResourceManager.h>
40 #include <FUiCtrl_TextBoxImpl.h>
41 #include "FWebCtrl_PromptPopup.h"
42
43
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Graphics;
47 using namespace Tizen::System;
48 using namespace Tizen::Ui;
49 using namespace Tizen::Ui::Controls;
50
51
52 static const int LABEL_HEIGHT = 10;
53
54
55 namespace Tizen { namespace Web { namespace Controls
56 {
57
58
59 _PromptPopup::_PromptPopup(void)
60         : __pEditField(null)
61         , __pWebView(null)
62 {
63 }
64
65 _PromptPopup::~_PromptPopup(void)
66 {
67 }
68
69
70 // Prompt popup
71 result
72 _PromptPopup::Construct(const String& message, const String& defaultVale,  Evas_Object* pView)
73 {
74         result r = E_SUCCESS;
75         Rectangle rect(0, 0, 0, 0);
76
77         int popupHeight = 0;
78
79         __pWebView = pView;
80
81         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
82         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
83
84         popupHeight = LABEL_HEIGHT + (2 * pPopupData->editDim.height + pPopupData->spacePad)  + pPopupData->btnDim.height + (4 * pPopupData->spacePad) ;
85
86         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupHeight));
87         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
88         SetTitleText("Prompt Dialog");
89
90         //Text box
91         rect = GetClientAreaBounds();
92         rect.width = pPopupData->popupDim.width - 2 * pPopupData->sideMargin;
93         rect.y = 0;
94         rect.height = pPopupData->editDim.height + 10;
95
96         std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
97         SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
98
99         r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
100         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
101
102         pTextBox->SetText(message);
103         pTextBox->SetColor(TEXT_BOX_STATUS_NORMAL, GetColor());
104         pTextBox->SetColor(TEXT_BOX_STATUS_HIGHLIGHTED, GetColor());
105
106         r = AddControl(*pTextBox);
107         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
108
109         TextBox* pTmpTextBox = pTextBox.release();
110
111
112         //Edit Field
113         rect.y = 0;
114         rect.height = pPopupData->editDim.height + 10;
115
116         std::unique_ptr<EditField> pEditField(new (std::nothrow) EditField());
117         SysTryReturnResult(NID_WEB_CTRL, pEditField.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
118
119         r = pEditField->Construct(rect, EDIT_FIELD_STYLE_NORMAL);
120         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
121
122         pEditField->SetText(defaultVale);
123
124         r = AddControl(*pEditField);
125         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
126
127         __pEditField = pEditField.release();
128         __pEditField->SetFocus();
129
130         //Panel
131         Panel* pButtonPanel = CreateAndAddPanel();
132         SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
133
134         _SystemResource* pSysResource = _SystemResource::GetInstance();
135         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
136
137         ArrayList idList;
138         r = idList.Construct();
139         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
140         idList.Add(*(new Integer(ID_BUTTON_PROMPT_OK)));
141         idList.Add(*(new Integer(ID_BUTTON_PROMPT_CANCEL)));
142
143         ArrayList titleList;
144         r = titleList.Construct();
145         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
146         titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_SK_OK"))));
147         titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
148
149         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
150         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
151
152         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
153         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
154
155         pLayout->SetHorizontalAlignment(*pTmpTextBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
156         pLayout->SetHorizontalAlignment(*__pEditField, LAYOUT_HORIZONTAL_ALIGN_CENTER);
157         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
158
159         pLayout->SetSpacing(*pTmpTextBox, pPopupData->spacePad);
160         pLayout->SetSpacing(*__pEditField, pPopupData->spacePad);
161         pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
162
163         return r;
164 }
165
166 const String
167 _PromptPopup::GetPromptText()
168 {
169         return __pEditField->GetText();
170 }
171
172
173 void
174 _PromptPopup::OnActionPerformed(const Control& source, int actionId)
175 {
176         switch (actionId)
177         {
178         case ID_BUTTON_PROMPT_OK:
179         {
180                 HidePopup(ID_BUTTON_PROMPT_OK);
181                 std::unique_ptr<char[]> pResult(_StringConverter::CopyToCharArrayN(GetPromptText()));
182                 ewk_view_javascript_prompt_reply(__pWebView, pResult.get());
183                 break;
184         }
185         case ID_BUTTON_PROMPT_CANCEL:
186                 HidePopup(ID_BUTTON_PROMPT_CANCEL);
187                 ewk_view_javascript_prompt_reply(__pWebView, null);
188                 break;
189
190         default:
191                 SysAssertf(false, "unknown action ID used");
192                 break;
193         }
194         delete this;
195 }
196
197
198 }}} // Tizen::Web::Controls