Init Tizen 2.2.1
[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         result r = E_SUCCESS;
127
128         r = SetShowState(false);
129         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
130
131         if (__isModal)
132         {
133                 __modal = modalResult;
134                 __isModal = false;
135
136                 r = EndModal(__modal);
137                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
138         }
139
140         return E_SUCCESS;
141 }
142
143
144 result
145 _WebPopup::ShowAndWait(int& modalResult)
146 {
147         _WebManager* pWebManager = _WebManager::GetInstance();
148         pWebManager->SetActivePopup(this);
149
150         __isModal = true;
151
152         return DoModal(modalResult);
153 }
154
155
156 Panel*
157 _WebPopup::CreateAndAddPanel(void)
158 {
159         std::unique_ptr<Panel> pPanel(new (std::nothrow) Panel());
160         SysTryReturn(NID_WEB_CTRL, pPanel.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
161
162         Rectangle panelRect(0, 0, GetSize().width, __pWebPopupData->panelHeight);
163
164         result r = pPanel->Construct(panelRect, GROUP_STYLE_BOTTOM);
165         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
166
167         Color buttonBgColor(0x00000000);
168         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BG_NORMAL, buttonBgColor);
169         pPanel->SetBackgroundColor(buttonBgColor);
170
171         r = AddControl(*pPanel);
172         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
173
174         return pPanel.release();
175 }
176
177
178 result
179 _WebPopup::CreateAndAddButtons(const IList& buttonIds, const IList& buttonTitles, Panel* pPanel)
180 {
181         SysTryReturnResult(NID_WEB_CTRL, pPanel, E_INVALID_ARG, "[%s] Panel Pointer is null.", GetErrorMessage(E_INVALID_ARG));
182
183         _WebPopup* pParent = dynamic_cast<_WebPopup*>(pPanel->GetParent());
184         SysTryReturnResult(NID_WEB_CTRL, pParent == this, E_INVALID_ARG, "[E_INVALID_ARG] Panel not attached to popup.");
185
186         int idCount = buttonIds.GetCount();
187         int titleCount = buttonTitles.GetCount();
188         SysTryReturnResult(NID_WEB_CTRL, idCount > 0 && titleCount > 0 && idCount == titleCount, E_INVALID_DATA, "[E_INVALID_DATA] mismatch in count of Ids and Ttitles.");
189
190         int buttonMargin = __pWebPopupData->spacePad/2;
191         if (idCount == 1)
192         {
193                 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_01, _CONTROL_ORIENTATION_PORTRAIT, buttonMargin);
194         }
195         int buttonWidth = (GetSize().width - buttonMargin*(idCount+1)) / idCount;
196         int buttonTopMargin = (__pWebPopupData->panelHeight - __pWebPopupData->btnDim.height)/2;
197
198         result r = E_SUCCESS;
199
200         //Button Colors
201         Color buttonColorNormal(0x00000000);
202         Color buttonColorPressed(0x00000000);
203         Color buttonColorDisabled(0x00000000);
204         Color buttonColorHighlighted(0x00000000);
205         Color buttonTextNormal(0x00000000);
206         Color buttonTextPressed(0x00000000);
207         Color buttonTextDisabled(0x00000000);
208         Color buttonTextHighlighted(0x00000000);
209
210         Bitmap* pComposedButtonBgNormal = null;
211         Bitmap* pComposedButtonBgPressed = null;
212         Bitmap* pComposedButtonBgDisabled = null;
213         Bitmap* pComposedButtonBgHighlighted = null;
214
215         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_NORMAL, buttonColorNormal);
216         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_PRESSED, buttonColorPressed);
217         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_DISABLED, buttonColorDisabled);
218         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_HIGHLIGHTED, buttonColorHighlighted);
219
220         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_NORMAL, buttonTextNormal);
221         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_PRESSED, buttonTextPressed);
222         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_DISABLED, buttonTextDisabled);
223         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_HIGHLIGHTED, buttonTextHighlighted);
224
225         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorNormal, pComposedButtonBgNormal);
226         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
227         std::unique_ptr< Bitmap > pComposedBtnBgNormal(pComposedButtonBgNormal);
228
229         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorPressed, pComposedButtonBgPressed);
230         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
231         std::unique_ptr< Bitmap > pComposedBtnBgPressed(pComposedButtonBgPressed);
232
233         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorDisabled, pComposedButtonBgDisabled);
234         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
235         std::unique_ptr< Bitmap > pComposedBtnBgDisabled(pComposedButtonBgDisabled);
236
237         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorHighlighted, pComposedButtonBgHighlighted);
238         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
239         std::unique_ptr< Bitmap > pComposedBtnBgHighlighted(pComposedButtonBgHighlighted);
240
241         for (int i = 0; i < idCount; i++)
242         {
243                 const Integer* pButtonId = static_cast<const Integer*>(buttonIds.GetAt(i));
244                 const String* pButtonTitle = static_cast<const String*>(buttonTitles.GetAt(i));
245                 SysTryReturn(NID_WEB_CTRL, pButtonId && pButtonTitle, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
246
247                 //Create button
248                 std::unique_ptr<Button> pButton(new (std::nothrow) Button());
249                 SysTryReturnResult(NID_WEB_CTRL, pButton.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory Allocation failed.");
250
251                 r = pButton->Construct(Rectangle((buttonMargin*(i+1))+(buttonWidth*i), buttonTopMargin, buttonWidth, __pWebPopupData->btnDim.height), *pButtonTitle);
252                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
253
254                 pButton->SetActionId(pButtonId->ToInt());
255
256                 pButton->SetNormalBackgroundBitmap(*pComposedBtnBgNormal);
257                 pButton->SetPressedBackgroundBitmap(*pComposedBtnBgPressed);
258                 pButton->SetDisabledBackgroundBitmap(*pComposedBtnBgDisabled);
259                 pButton->SetHighlightedBackgroundBitmap(*pComposedBtnBgHighlighted);
260                 pButton->SetTextColor(buttonTextNormal);
261                 pButton->SetPressedTextColor(buttonTextPressed);
262                 pButton->SetDisabledTextColor(buttonTextDisabled);
263                 pButton->SetHighlightedTextColor(buttonTextHighlighted);
264
265                 //Add button to panel
266                 r = pPanel->AddControl(*pButton);
267                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
268
269                 pButton->AddActionEventListener(*this);
270                 pButton.release();
271         }
272
273         return r;
274 }
275
276
277 bool
278 _WebPopup::IsModalPopup(void)
279 {
280         return __isModal;
281 }
282
283
284 _WebPopupData*
285 _WebPopup::GetPopupData(bool refresh)
286 {
287         if (!refresh && __pWebPopupData.get())
288         {
289                 return __pWebPopupData.get();
290         }
291
292         __pWebPopupData.reset();
293
294         __pWebPopupData = std::unique_ptr<_WebPopupData> (new (std::nothrow) _WebPopupData());
295         SysTryReturn(NID_WEB_CTRL, __pWebPopupData.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
296
297         _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
298
299         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __pWebPopupData->popupDim.width);
300         GET_SHAPE_CONFIG(MESSAGEBOX::MAX_HEIGHT, orientation, __pWebPopupData->popupDim.height);
301         GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, __pWebPopupData->sideMargin);
302         GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, orientation, __pWebPopupData->bottomMargin);
303         GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, orientation, __pWebPopupData->titleHeight);
304
305         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __pWebPopupData->btnDim.height);
306         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_02, orientation, __pWebPopupData->spacePad);
307         GET_SHAPE_CONFIG(MESSAGEBOX::BOTTOM_HEIGHT, orientation, __pWebPopupData->panelHeight);
308
309         GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, orientation, __pWebPopupData->labelFontSize);
310         GET_DIMENSION_CONFIG(CHECKBUTTON::MIN_DIMENSION, orientation, __pWebPopupData->checkDim);
311
312         GET_DIMENSION_CONFIG(EDIT::MIN_SIZE, orientation, __pWebPopupData->editDim);
313
314         __pWebPopupData->labelDim.width = __pWebPopupData->popupDim.width - 2*__pWebPopupData->sideMargin;
315         __pWebPopupData->labelDim.height = 3*__pWebPopupData->labelFontSize;
316
317         return __pWebPopupData.get();
318 }
319
320
321 result
322 _WebPopup::OnTerminating(void)
323 {
324         return E_SUCCESS;
325 }
326
327
328 }}} // Tizen::Web::Controls