modification of popup single button size
[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         if (idCount == 1)
193         {
194                 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_01, _CONTROL_ORIENTATION_PORTRAIT, buttonMargin);
195         }
196         int buttonWidth = (__pWebPopupData->popupDim.width - buttonMargin*(idCount+1)) / idCount;
197         int buttonTopMargin = (__pWebPopupData->panelHeight - __pWebPopupData->btnDim.height)/2;
198
199         result r = E_SUCCESS;
200
201         //Button Colors
202         Color buttonColorNormal(0x00000000);
203         Color buttonColorPressed(0x00000000);
204         Color buttonColorDisabled(0x00000000);
205         Color buttonColorHighlighted(0x00000000);
206         Color buttonTextNormal(0x00000000);
207         Color buttonTextPressed(0x00000000);
208         Color buttonTextDisabled(0x00000000);
209         Color buttonTextHighlighted(0x00000000);
210
211         Bitmap* pComposedButtonBgNormal = null;
212         Bitmap* pComposedButtonBgPressed = null;
213         Bitmap* pComposedButtonBgDisabled = null;
214         Bitmap* pComposedButtonBgHighlighted = null;
215
216         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_NORMAL, buttonColorNormal);
217         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_PRESSED, buttonColorPressed);
218         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_DISABLED, buttonColorDisabled);
219         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_BG_HIGHLIGHTED, buttonColorHighlighted);
220
221         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_NORMAL, buttonTextNormal);
222         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_PRESSED, buttonTextPressed);
223         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_DISABLED, buttonTextDisabled);
224         GET_COLOR_CONFIG(MESSAGEBOX::BOTTOM_BUTTON_TEXT_HIGHLIGHTED, buttonTextHighlighted);
225
226         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorNormal, pComposedButtonBgNormal);
227         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
228         std::unique_ptr< Bitmap > pComposedBtnBgNormal(pComposedButtonBgNormal);
229
230         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorPressed, pComposedButtonBgPressed);
231         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
232         std::unique_ptr< Bitmap > pComposedBtnBgPressed(pComposedButtonBgPressed);
233
234         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_DISABLED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorDisabled, pComposedButtonBgDisabled);
235         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
236         std::unique_ptr< Bitmap > pComposedBtnBgDisabled(pComposedButtonBgDisabled);
237
238         r = GET_REPLACED_BITMAP_CONFIG_N(MESSAGEBOX::BOTTOM_BUTTON_BG_HIGHLIGHTED, BITMAP_PIXEL_FORMAT_ARGB8888, buttonColorHighlighted, pComposedButtonBgHighlighted);
239         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
240         std::unique_ptr< Bitmap > pComposedBtnBgHighlighted(pComposedButtonBgHighlighted);
241
242         for (int i = 0; i < idCount; i++)
243         {
244                 const Integer* pButtonId = static_cast<const Integer*>(buttonIds.GetAt(i));
245                 const String* pButtonTitle = static_cast<const String*>(buttonTitles.GetAt(i));
246                 SysTryReturn(NID_WEB_CTRL, pButtonId && pButtonTitle, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
247
248                 //Create button
249                 std::unique_ptr<Button> pButton(new (std::nothrow) Button());
250                 SysTryReturnResult(NID_WEB_CTRL, pButton.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory Allocation failed.");
251
252                 r = pButton->Construct(Rectangle((buttonMargin*(i+1))+(buttonWidth*i), buttonTopMargin, buttonWidth, __pWebPopupData->btnDim.height), *pButtonTitle);
253                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
254
255                 pButton->SetActionId(pButtonId->ToInt());
256
257                 pButton->SetNormalBackgroundBitmap(*pComposedBtnBgNormal);
258                 pButton->SetPressedBackgroundBitmap(*pComposedBtnBgPressed);
259                 pButton->SetDisabledBackgroundBitmap(*pComposedBtnBgDisabled);
260                 pButton->SetHighlightedBackgroundBitmap(*pComposedBtnBgHighlighted);
261                 pButton->SetTextColor(buttonTextNormal);
262                 pButton->SetPressedTextColor(buttonTextPressed);
263                 pButton->SetDisabledTextColor(buttonTextDisabled);
264                 pButton->SetHighlightedTextColor(buttonTextHighlighted);
265
266                 //Add button to panel
267                 r = pPanel->AddControl(*pButton);
268                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
269
270                 pButton->AddActionEventListener(*this);
271                 pButton.release();
272         }
273
274         return r;
275 }
276
277
278 bool
279 _WebPopup::IsModalPopup(void)
280 {
281         return __isModal;
282 }
283
284
285 _WebPopupData*
286 _WebPopup::GetPopupData(bool refresh)
287 {
288         if (!refresh && __pWebPopupData.get())
289         {
290                 return __pWebPopupData.get();
291         }
292
293         __pWebPopupData.reset();
294
295         __pWebPopupData = std::unique_ptr<_WebPopupData> (new (std::nothrow) _WebPopupData());
296         SysTryReturn(NID_WEB_CTRL, __pWebPopupData.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
297
298         _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
299
300         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __pWebPopupData->popupDim.width);
301         GET_SHAPE_CONFIG(MESSAGEBOX::MAX_HEIGHT, orientation, __pWebPopupData->popupDim.height);
302         GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, __pWebPopupData->sideMargin);
303         GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, orientation, __pWebPopupData->bottomMargin);
304         GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, orientation, __pWebPopupData->titleHeight);
305
306         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __pWebPopupData->btnDim.height);
307         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_02, orientation, __pWebPopupData->spacePad);
308         GET_SHAPE_CONFIG(MESSAGEBOX::BOTTOM_HEIGHT, orientation, __pWebPopupData->panelHeight);
309
310         GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, orientation, __pWebPopupData->labelFontSize);
311         GET_DIMENSION_CONFIG(CHECKBUTTON::MIN_DIMENSION, orientation, __pWebPopupData->checkDim);
312
313         GET_DIMENSION_CONFIG(EDIT::MIN_SIZE, orientation, __pWebPopupData->editDim);
314
315         __pWebPopupData->labelDim.width = __pWebPopupData->popupDim.width - 2*__pWebPopupData->sideMargin;
316         __pWebPopupData->labelDim.height = 3*__pWebPopupData->labelFontSize;
317
318         return __pWebPopupData.get();
319 }
320
321
322 result
323 _WebPopup::OnTerminating(void)
324 {
325         return E_SUCCESS;
326 }
327
328
329 }}} // Tizen::Web::Controls