c2f2f1033ac3d8d24fedbb9eb88f68701a23fbc5
[framework/osp/web.git] / src / controls / FWebCtrl_WebPopup.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_WebPopup.cpp
20  * @brief               The file contains the definition of _WebPopup class.
21  */
22 #include <FBaseColIList.h>
23 #include <FBaseSysLog.h>
24 #include <FGrpColor.h>
25 #include <FUiCtrlButton.h>
26 #include <FUiCtrlPanel.h>
27 #include <FUiVerticalBoxLayout.h>
28 #include <FUi_ResourceManager.h>
29 #include "FWebCtrl_WebManager.h"
30 #include "FWebCtrl_WebPopup.h"
31
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35 using namespace Tizen::Graphics;
36 using namespace Tizen::Io;
37 using namespace Tizen::Ui;
38 using namespace Tizen::Ui::Controls;
39
40
41 namespace Tizen { namespace Web { namespace Controls
42 {
43
44 std::unique_ptr<_WebPopupData> _WebPopup::__pWebPopupData(null);
45
46 _WebPopup::_WebPopup(void)
47         : __isModal(false)
48         , __modal(0)
49 {
50 }
51
52
53 _WebPopup::~_WebPopup(void)
54 {
55         _WebManager* pWebManager = _WebManager::GetInstance();
56         pWebManager->RemoveActivePopup(this);
57
58         if (IsModalPopup())
59         {
60                 HidePopup();
61         }
62         __pWebPopupData.reset();
63 }
64
65
66 result
67 _WebPopup::Construct(bool hasTitle, const Dimension& popupDim)
68 {
69         VerticalBoxLayout layout;
70
71         Dimension dim(popupDim);
72
73         if (hasTitle)
74         {
75                 dim.height += __pWebPopupData->titleHeight;
76         }
77
78         result r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
79         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
80
81         r = Popup::Construct(layout, layout, hasTitle, dim);
82         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
83
84         return E_SUCCESS;
85 }
86
87
88 void
89 _WebPopup::OnActionPerformed(const Control& source, int actionId)
90 {
91         switch (actionId)
92         {
93         case ID_BUTTON_COMMON_CLOSE:
94                 HidePopup();
95                 break;
96         default:
97                 SysAssertf(false, "unknown action ID used");
98                 break;
99         }
100
101         delete this;
102 }
103
104
105 result
106 _WebPopup::ShowPopup(void)
107 {
108         result r = E_SUCCESS;
109
110         _WebManager* pWebManager = _WebManager::GetInstance();
111         pWebManager->SetActivePopup(this);
112
113         r = SetShowState(true);
114         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
115
116         r = Show();
117         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
118
119         return E_SUCCESS;
120 }
121
122
123 result
124 _WebPopup::HidePopup(int modalResult)
125 {
126         if (__isModal)
127         {
128                 __modal = modalResult;
129                 __isModal = false;
130
131                 return EndModal(__modal);
132         }
133         else
134         {
135                 result r = E_SUCCESS;
136
137                 r = SetShowState(false);
138                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
139
140                 return E_SUCCESS;
141         }
142 }
143
144
145 result
146 _WebPopup::ShowAndWait(int& modalResult)
147 {
148         _WebManager* pWebManager = _WebManager::GetInstance();
149         pWebManager->SetActivePopup(this);
150
151         __isModal = true;
152
153         return DoModal(modalResult);
154 }
155
156
157 Panel*
158 _WebPopup::CreateAndAddPanel(void)
159 {
160         std::unique_ptr<Panel> pPanel(new (std::nothrow) Panel());
161         SysTryReturn(NID_WEB_CTRL, pPanel.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
162
163         Rectangle panelRect(0, 0, __pWebPopupData->popupDim.width, __pWebPopupData->panelHeight);
164
165         result r = pPanel->Construct(panelRect, GROUP_STYLE_BOTTOM);
166         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
167
168         Color buttonBgColor(0x00000000);
169         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BG_NORMAL, buttonBgColor);
170         pPanel->SetBackgroundColor(buttonBgColor);
171
172         r = AddControl(*pPanel);
173         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
174
175         return pPanel.release();
176 }
177
178
179 result
180 _WebPopup::CreateAndAddButtons(const IList& buttonIds, const IList& buttonTitles, Panel* pPanel)
181 {
182         SysTryReturnResult(NID_WEB_CTRL, pPanel, E_INVALID_ARG, "[%s] Panel Pointer is null.", GetErrorMessage(E_INVALID_ARG));
183
184         _WebPopup* pParent = dynamic_cast<_WebPopup*>(pPanel->GetParent());
185         SysTryReturnResult(NID_WEB_CTRL, pParent == this, E_INVALID_ARG, "[E_INVALID_ARG] Panel not attached to popup.");
186
187         int idCount = buttonIds.GetCount();
188         int titleCount = buttonTitles.GetCount();
189         SysTryReturnResult(NID_WEB_CTRL, idCount > 0 && titleCount > 0 && idCount == titleCount, E_INVALID_DATA, "[E_INVALID_DATA] mismatch in count of Ids and Ttitles.");
190
191         int buttonMargin = __pWebPopupData->spacePad/2;
192         int buttonWidth = (__pWebPopupData->popupDim.width - buttonMargin*(idCount+1)) / idCount;
193         int buttonTopMargin = (__pWebPopupData->panelHeight - __pWebPopupData->btnDim.height)/2;
194
195         result r = E_SUCCESS;
196
197         //Button Colors
198         Color buttonColorNormal(0x00000000);
199         Color buttonColorPressed(0x00000000);
200         Color buttonColorDisabled(0x00000000);
201         Color buttonColorHighlighted(0x00000000);
202         Color buttonTextNormal(0x00000000);
203         Color buttonTextPressed(0x00000000);
204         Color buttonTextDisabled(0x00000000);
205         Color buttonTextHighlighted(0x00000000);
206
207         Bitmap* pComposedButtonBgNormal = null;
208         Bitmap* pComposedButtonBgPressed = null;
209         Bitmap* pComposedButtonBgDisabled = null;
210         Bitmap* pComposedButtonBgHighlighted = null;
211
212         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_NORMAL, buttonColorNormal);
213         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_PRESSED, buttonColorPressed);
214         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_DISABLED, buttonColorDisabled);
215         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_HIGHLIGHTED, buttonColorHighlighted);
216
217         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_NORMAL, buttonTextNormal);
218         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_PRESSED, buttonTextPressed);
219         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_DISABLED, buttonTextDisabled);
220         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_HIGHLIGHTED, buttonTextHighlighted);
221
222         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorNormal, pComposedButtonBgNormal);
223         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
224         std::unique_ptr< Bitmap > pComposedBtnBgNormal(pComposedButtonBgNormal);
225
226         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorPressed, pComposedButtonBgPressed);
227         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
228         std::unique_ptr< Bitmap > pComposedBtnBgPressed(pComposedButtonBgPressed);
229
230         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorDisabled, pComposedButtonBgDisabled);
231         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
232         std::unique_ptr< Bitmap > pComposedBtnBgDisabled(pComposedButtonBgDisabled);
233
234         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorHighlighted, pComposedButtonBgHighlighted);
235         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
236         std::unique_ptr< Bitmap > pComposedBtnBgHighlighted(pComposedButtonBgHighlighted);
237
238         for (int i = 0; i < idCount; i++)
239         {
240                 const Integer* pButtonId = static_cast<const Integer*>(buttonIds.GetAt(i));
241                 const String* pButtonTitle = static_cast<const String*>(buttonTitles.GetAt(i));
242                 SysTryReturn(NID_WEB_CTRL, pButtonId && pButtonTitle, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
243
244                 //Create button
245                 std::unique_ptr<Button> pButton(new (std::nothrow) Button());
246                 SysTryReturnResult(NID_WEB_CTRL, pButton.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory Allocation failed.");
247
248                 r = pButton->Construct(Rectangle((buttonMargin*(i+1))+(buttonWidth*i), buttonTopMargin, buttonWidth, __pWebPopupData->btnDim.height), *pButtonTitle);
249                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
250
251                 pButton->SetActionId(pButtonId->ToInt());
252
253                 pButton->SetNormalBackgroundBitmap(*pComposedBtnBgNormal);
254                 pButton->SetPressedBackgroundBitmap(*pComposedBtnBgPressed);
255                 pButton->SetDisabledBackgroundBitmap(*pComposedBtnBgDisabled);
256                 pButton->SetHighlightedBackgroundBitmap(*pComposedBtnBgHighlighted);
257                 pButton->SetTextColor(buttonTextNormal);
258                 pButton->SetPressedTextColor(buttonTextPressed);
259                 pButton->SetDisabledTextColor(buttonTextDisabled);
260                 pButton->SetHighlightedTextColor(buttonTextHighlighted);
261
262                 //Add button to panel
263                 r = pPanel->AddControl(*pButton);
264                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
265
266                 pButton->AddActionEventListener(*this);
267                 pButton.release();
268         }
269
270         return r;
271 }
272
273
274 bool
275 _WebPopup::IsModalPopup(void)
276 {
277         return __isModal;
278 }
279
280
281 _WebPopupData*
282 _WebPopup::GetPopupData(bool refresh)
283 {
284         if (!refresh && __pWebPopupData.get())
285         {
286                 return __pWebPopupData.get();
287         }
288
289         __pWebPopupData.reset();
290
291         __pWebPopupData = std::unique_ptr<_WebPopupData> (new (std::nothrow) _WebPopupData());
292         SysTryReturn(NID_WEB_CTRL, __pWebPopupData.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
293
294         _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
295
296         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __pWebPopupData->popupDim.width);
297         GET_SHAPE_CONFIG(MESSAGEBOX::MAX_HEIGHT, orientation, __pWebPopupData->popupDim.height);
298         GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, __pWebPopupData->sideMargin);
299         GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, orientation, __pWebPopupData->bottomMargin);
300         GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, orientation, __pWebPopupData->titleHeight);
301
302         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __pWebPopupData->btnDim.height);
303         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_02, orientation, __pWebPopupData->spacePad);
304         GET_SHAPE_CONFIG(MESSAGEBOX::BOTTOM_HEIGHT, orientation, __pWebPopupData->panelHeight);
305
306         GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, orientation, __pWebPopupData->labelFontSize);
307         GET_DIMENSION_CONFIG(CHECKBUTTON::MIN_DIMENSION, orientation, __pWebPopupData->checkDim);
308
309         GET_DIMENSION_CONFIG(EDIT::MIN_SIZE, orientation, __pWebPopupData->editDim);
310
311         __pWebPopupData->labelDim.width = __pWebPopupData->popupDim.width - 2*__pWebPopupData->sideMargin;
312         __pWebPopupData->labelDim.height = 3*__pWebPopupData->labelFontSize;
313
314         return __pWebPopupData.get();
315 }
316
317
318 result
319 _WebPopup::OnTerminating(void)
320 {
321         return E_SUCCESS;
322 }
323
324
325 }}} // Tizen::Web::Controls