Merge "change popup button negative,positive" into tizen_2.2
[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 <FUiKeyEventInfo.h>
34 #include <FUiLayout.h>
35 #include <FUiVerticalBoxLayout.h>
36 #include <FWebCtrlAuthenticationChallenge.h>
37 #include <FBase_StringConverter.h>
38 #include <FSys_SystemResource.h>
39 #include <FUi_ControlManager.h>
40 #include <FUi_ResourceManager.h>
41 #include <FUiCtrl_TextBoxImpl.h>
42 #include "FWebCtrl_PromptPopup.h"
43 #include "FWebCtrl_WebImpl.h"
44
45
46 using namespace Tizen::Base;
47 using namespace Tizen::Base::Collection;
48 using namespace Tizen::Graphics;
49 using namespace Tizen::System;
50 using namespace Tizen::Ui;
51 using namespace Tizen::Ui::Controls;
52
53
54 namespace Tizen { namespace Web { namespace Controls
55 {
56
57
58 static const int EDIT_TEXT_SIZE = 35;
59
60
61 _PromptPopup::_PromptPopup(void)
62         : __pEditField(null)
63         , __pWebView(null)
64 {
65 }
66
67
68 _PromptPopup::~_PromptPopup(void)
69 {
70 }
71
72
73 // Prompt popup
74 result
75 _PromptPopup::Construct(const String& message, const String& defaultVale,  Evas_Object* pView)
76 {
77         result r = E_SUCCESS;
78         Rectangle rect(0, 0, 0, 0);
79
80         int popupHeight = 0;
81
82         __pWebView = pView;
83
84         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
85         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
86
87         popupHeight = pPopupData->editDim.height + pPopupData->labelDim.height  + pPopupData->btnDim.height + (2 * pPopupData->spacePad);
88
89         r = _WebPopup::Construct(true, Dimension(pPopupData->popupDim.width, popupHeight));
90         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
91         SetTitleText("Prompt Dialog");
92
93         //Text box
94         rect = GetClientAreaBounds();
95         rect.width = pPopupData->labelDim.width;
96         rect.y = 0;
97         rect.height = pPopupData->labelDim.height;
98
99         std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
100         SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
101
102         r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
103         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
104
105         pTextBox->SetTextSize(EDIT_TEXT_SIZE);
106         pTextBox->SetText(message);
107         pTextBox->SetColor(TEXT_BOX_STATUS_NORMAL, GetColor());
108         pTextBox->SetColor(TEXT_BOX_STATUS_HIGHLIGHTED, GetColor());
109
110         r = AddControl(*pTextBox);
111         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
112
113         TextBox* pTmpTextBox = pTextBox.release();
114
115         //Edit Field
116         rect.y = 0;
117         rect.height = pPopupData->editDim.height;
118
119         std::unique_ptr<EditField> pEditField(new (std::nothrow) EditField());
120         SysTryReturnResult(NID_WEB_CTRL, pEditField.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
121
122         r = pEditField->Construct(rect, EDIT_FIELD_STYLE_NORMAL);
123         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
124
125         pEditField->SetTextSize(EDIT_TEXT_SIZE);
126         pEditField->SetText(defaultVale);
127
128         r = AddControl(*pEditField);
129         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
130
131         __pEditField = pEditField.release();
132         __pEditField->SetFocus();
133
134         //Panel
135         Panel* pButtonPanel = CreateAndAddPanel();
136         SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
137
138         _SystemResource* pSysResource = _SystemResource::GetInstance();
139         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
140
141         ArrayList idList;
142         r = idList.Construct();
143         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
144         idList.Add(*(new Integer(ID_BUTTON_PROMPT_CANCEL)));
145         idList.Add(*(new Integer(ID_BUTTON_PROMPT_OK)));
146
147         ArrayList titleList;
148         r = titleList.Construct();
149         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
150         titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
151         titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_SK_OK"))));
152
153         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
154         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
155
156         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
157         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
158
159         pLayout->SetHorizontalAlignment(*pTmpTextBox, LAYOUT_HORIZONTAL_ALIGN_CENTER);
160         pLayout->SetHorizontalAlignment(*__pEditField, LAYOUT_HORIZONTAL_ALIGN_CENTER);
161         pLayout->SetHorizontalAlignment(*pButtonPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
162
163         pLayout->SetSpacing(*__pEditField, pPopupData->spacePad);
164         pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
165
166         SetPropagatedKeyEventListener(this);
167         
168         return r;
169 }
170
171
172 const String
173 _PromptPopup::GetPromptText()
174 {
175         return __pEditField->GetText();
176 }
177
178
179 void
180 _PromptPopup::OnActionPerformed(const Control& source, int actionId)
181 {
182         switch (actionId)
183         {
184         case ID_BUTTON_PROMPT_OK:
185         {
186                 HidePopup(ID_BUTTON_PROMPT_OK);
187                 std::unique_ptr<char[]> pResult(_StringConverter::CopyToCharArrayN(GetPromptText()));
188                 ewk_view_javascript_prompt_reply(__pWebView, pResult.get());
189                 break;
190         }
191         case ID_BUTTON_PROMPT_CANCEL:
192                 HidePopup(ID_BUTTON_PROMPT_CANCEL);
193                 ewk_view_javascript_prompt_reply(__pWebView, null);
194                 break;
195
196         default:
197                 SysAssertf(false, "unknown action ID used");
198                 break;
199         }
200         GetOwner()->SendUserEvent(ID_PROMPT_POPUP_CLOSE, null);
201 }
202
203 bool
204 _PromptPopup::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
205 {
206         return true;
207 }
208
209 bool
210 _PromptPopup::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
211 {
212         if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
213         {
214                 HidePopup(ID_BUTTON_PROMPT_CANCEL);
215                 ewk_view_javascript_prompt_reply(__pWebView, null);
216                 GetOwner()->SendUserEvent(ID_PROMPT_POPUP_CLOSE, null);
217         }
218
219         return true;
220 }
221
222 bool
223 _PromptPopup::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
224 {
225         return false;
226 }
227
228 bool
229 _PromptPopup::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
230 {
231         return false;
232 }
233
234 bool
235 _PromptPopup::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
236 {
237         return false;
238 }
239
240
241 }}} // Tizen::Web::Controls