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