Tizen 2.1 base
[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 <FGrpDimension.h>
23 #include <FGrpRectangle.h>
24 #include <FUiCtrlButton.h>
25 #include <FUiCtrlEditField.h>
26 #include <FUiCtrlLabel.h>
27 #include <FUiCtrlPanel.h>
28 #include <FUiLayout.h>
29 #include <FUiVerticalBoxLayout.h>
30 #include <FWebCtrlAuthenticationChallenge.h>
31 #include <FBaseSysLog.h>
32 #include <FUiCtrl_PopupImpl.h>
33 #include <FUi_ControlManager.h>
34 #include <FUi_ResourceManager.h>
35 #include <FGrpColor.h>
36 #include <FUiCtrlTextBox.h>
37 #include <FUiCtrl_TextBoxImpl.h>
38 #include <FSys_SystemResource.h>
39 #include "FWebCtrl_WebImpl.h"
40 #include "FWebCtrl_PromptPopup.h"
41
42 using namespace Tizen::Base;
43 using namespace Tizen::Graphics;
44 using namespace Tizen::Web::Controls;
45 using namespace Tizen::Ui::Controls;
46 using namespace Tizen::Ui;
47
48 static const int LABEL_HEIGHT = 10;
49
50 namespace Tizen { namespace Web { namespace Controls
51 {
52
53
54 _PromptPopup::_PromptPopup(void)
55         : __pPopup(null)
56         , __pEditField(null)
57 {
58 }
59
60 _PromptPopup::~_PromptPopup(void)
61 {
62 }
63
64
65 // Prompt popup
66 result
67 _PromptPopup::Construct(const Tizen::Base::String& message, const Tizen::Base::String& defaultVale)
68 {
69         result r = E_SUCCESS;
70         String buttonStr;
71         Dimension dim;
72         Dimension editDim;
73         Rectangle rect(0, 0, 0, 0);
74
75         int spacePad = 0;
76         int buttonHeight = 0;
77         int sideMargin = 0;
78         int buttonWidth = 0;
79         int popupWidth = 0;
80         int popupHeight = 0;
81         int gapForControls = 0;
82
83         _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
84
85         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, popupWidth);
86         GET_SHAPE_CONFIG(MESSAGEBOX::MAX_HEIGHT, orientation, popupHeight);
87         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, buttonHeight);
88         GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, sideMargin);
89         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_02, orientation, spacePad);
90         r = GET_DIMENSION_CONFIG(EDIT::MIN_SIZE, orientation, editDim);
91         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, orientation, gapForControls);
92
93         VerticalBoxLayout layout;
94         r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
95
96         Tizen::System::_SystemResource* pSysResource = Tizen::System::_SystemResource::GetInstance();
97         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
98
99         std::unique_ptr<Popup> pPopup(new (std::nothrow) Popup());
100         SysTryReturnResult(NID_WEB_CTRL, pPopup.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
101
102         popupHeight = LABEL_HEIGHT + (2 * editDim.height + gapForControls)  + buttonHeight + (4 * spacePad) ;
103
104         r = pPopup->Construct(layout, layout, true, Tizen::Graphics::Dimension(popupWidth, popupHeight));
105         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
106
107         pPopup->SetTitleText("Prompt Dialog");
108
109         //Temp Label for Y position adjustment using Vertical layout.
110         rect.x = 0;
111         rect.y = 0;
112         rect.height = LABEL_HEIGHT;
113         rect.width = popupWidth - 2*sideMargin;;
114
115         std::unique_ptr<Label> pLabel(new (std::nothrow) Label());
116         SysTryReturnResult(NID_WEB_CTRL, pLabel.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
117
118         r = pLabel->Construct(rect, L"");
119         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
120
121         r = pPopup->AddControl(*pLabel);
122         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
123
124         Label* pTmpLabel = pLabel.release();
125
126         //Text box
127         rect = pPopup->GetClientAreaBounds();
128         rect.width = popupWidth - 2*sideMargin;
129         rect.y = 0;
130         rect.height = editDim.height;
131         
132         std::unique_ptr<TextBox> pTextBox(new (std::nothrow) TextBox());
133         SysTryReturnResult(NID_WEB_CTRL, pTextBox.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
134
135         r = pTextBox->Construct(rect, TEXT_BOX_BORDER_ROUNDED);
136         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
137
138         pTextBox->SetText(message);
139         pTextBox->SetColor(TEXT_BOX_STATUS_NORMAL, pPopup->GetColor());
140         pTextBox->SetColor(TEXT_BOX_STATUS_HIGHLIGHTED, pPopup->GetColor());
141
142         r = pPopup->AddControl(*pTextBox);
143         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
144
145         TextBox* pTmpTextBox = pTextBox.release();
146
147
148         //Edit Field
149         rect.y = 0;
150         rect.height = editDim.height;
151
152         std::unique_ptr<EditField> pEditField(new (std::nothrow) EditField());
153         SysTryReturnResult(NID_WEB_CTRL, pEditField.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
154
155         r = pEditField->Construct(rect, EDIT_FIELD_STYLE_NORMAL);
156         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
157
158         pEditField->SetText(defaultVale);
159
160         r = pPopup->AddControl(*pEditField);
161         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
162
163         __pEditField = pEditField.release();
164
165         //Panel
166         buttonWidth = (popupWidth / 2) - (spacePad) - (sideMargin * 2);
167
168         std::unique_ptr<Panel> pPanel(new Panel());
169         SysTryReturnResult(NID_WEB_CTRL, pPanel.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
170
171         r = pPanel->Construct(Rectangle(0, 0, rect.width, buttonHeight + gapForControls));
172         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
173
174         r = pPopup->AddControl(*pPanel);
175         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
176
177         Panel* pTempPanel = pPanel.release();
178
179         //Button OK
180         buttonStr = pSysResource->GetString("sys_string", "IDS_COM_SK_OK");
181         std::unique_ptr<Button> pBtnOk(new (std::nothrow) Button());
182         SysTryReturnResult(NID_WEB_CTRL, pBtnOk.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
183
184         r = pBtnOk->Construct(Rectangle(sideMargin, gapForControls, buttonWidth, buttonHeight), buttonStr);
185         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
186
187         pBtnOk->SetActionId(ID_BUTTON_PROMPT_OK);
188
189         r = pTempPanel->AddControl(*pBtnOk);
190         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
191
192         pBtnOk->AddActionEventListener(*this);
193         pBtnOk.release();
194
195         // Button Cancel
196         buttonStr = pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL");
197         std::unique_ptr<Button> pBtnCancel(new (std::nothrow) Button());
198         SysTryReturnResult(NID_WEB_CTRL, pBtnCancel.get(), E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
199
200         r = pBtnCancel->Construct(Rectangle(buttonWidth + spacePad + sideMargin, gapForControls, buttonWidth, buttonHeight), buttonStr);
201         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
202
203         pBtnCancel->SetActionId(ID_BUTTON_PROMPT_CANCEL);
204
205         r = pTempPanel->AddControl(*pBtnCancel);
206         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
207
208         pBtnCancel->AddActionEventListener(*this);
209         pBtnCancel.release();
210
211         __pEditField->SetFocus();
212
213         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(pPopup->GetLayoutN()));
214         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
215
216         pLayout->SetHorizontalFitPolicy(*pTmpLabel, FIT_POLICY_PARENT);
217         pLayout->SetHorizontalFitPolicy(*pTempPanel, FIT_POLICY_PARENT);
218         pLayout->SetHorizontalFitPolicy(*__pEditField, FIT_POLICY_PARENT);
219
220         pLayout->SetHorizontalAlignment(*pTmpTextBox, LAYOUT_HORIZONTAL_ALIGN_LEFT);
221         pLayout->SetHorizontalAlignment(*__pEditField, LAYOUT_HORIZONTAL_ALIGN_LEFT);
222         pLayout->SetHorizontalAlignment(*pTempPanel, LAYOUT_HORIZONTAL_ALIGN_CENTER);
223
224         pLayout->SetSpacing(*pTmpTextBox, gapForControls);
225         pLayout->SetSpacing(*__pEditField, gapForControls);
226         pLayout->SetSpacing(*pTempPanel, gapForControls);
227
228         __pPopup = std::move(pPopup);
229
230         return r;
231 }
232
233 const String
234 _PromptPopup::GetPromptText()
235 {
236         return __pEditField->GetText();
237 }
238
239
240 void
241 _PromptPopup::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
242 {
243         switch (actionId)
244         {
245         case ID_BUTTON_PROMPT_OK:
246         {
247                 __pPopup->SetShowState(false);
248                 EndModal(ID_BUTTON_PROMPT_OK);
249         }
250         break;
251
252         case ID_BUTTON_PROMPT_CANCEL:
253                 __pPopup->SetShowState(false);
254                 EndModal(ID_BUTTON_PROMPT_CANCEL);
255                 break;
256
257         default:
258                 break;
259         }
260 }
261
262 result
263 _PromptPopup::ShowAndWait(int& modalResult)
264 {
265         return __pPopup->DoModal(modalResult);
266 }
267
268
269 result
270 _PromptPopup::EndModal(int modalResult)
271 {
272         return __pPopup->EndModal(modalResult);
273 }
274
275 }}} // Tizen::Web::Controls