1c35aab520b334ebb1b256f8bbf39a0f7ef82da4
[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 <FUiCtrlButton.h>
25 #include <FUiCtrlPanel.h>
26 #include <FUiVerticalBoxLayout.h>
27 #include <FUi_ResourceManager.h>
28 #include "FWebCtrl_WebPopup.h"
29
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Io;
35 using namespace Tizen::Ui;
36 using namespace Tizen::Ui::Controls;
37
38
39 namespace Tizen { namespace Web { namespace Controls
40 {
41
42 std::unique_ptr<_WebPopupData> _WebPopup::__pWebPopupData(null);
43
44 _WebPopup::_WebPopup(void)
45         : __isModal(false)
46         , __modal(0)
47 {
48 }
49
50
51 _WebPopup::~_WebPopup(void)
52 {
53         if (IsModalPopup() == true)
54         {
55                 HidePopup(0);
56         }
57
58         __pWebPopupData.reset();
59 }
60
61
62 result
63 _WebPopup::Construct(bool hasTitle, const Dimension& popupDim)
64 {
65         VerticalBoxLayout layout;
66
67         Dimension dim(popupDim);
68         dim.height += __pWebPopupData->bottomMargin;
69
70         if (hasTitle)
71         {
72                 dim.height += __pWebPopupData->titleHeight;
73         }
74
75         result r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
76         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
77
78         r = Popup::Construct(layout, layout, hasTitle, dim);
79         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
80
81         return E_SUCCESS;
82 }
83
84
85 void
86 _WebPopup::OnActionPerformed(const Control& source, int actionId)
87 {
88         switch (actionId)
89         {
90         case ID_BUTTON_COMMON_CLOSE:
91                 HidePopup();
92                 break;
93         default:
94                 SysAssertf(false, "unknown action ID used");
95                 break;
96         }
97
98         delete this;
99 }
100
101
102 result
103 _WebPopup::ShowPopup(void)
104 {
105         result r = E_SUCCESS;
106
107         r = SetShowState(true);
108         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
109
110         r = Show();
111         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
112
113         return E_SUCCESS;
114 }
115
116
117 result
118 _WebPopup::HidePopup(int modalResult)
119 {
120         result r = E_SUCCESS;
121
122         r = SetShowState(false);
123         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
124
125         if (__isModal)
126         {
127                 __modal = modalResult;
128                 __isModal = false;
129
130                 return EndModal(__modal);
131         }
132
133         return E_SUCCESS;
134 }
135
136
137 result
138 _WebPopup::ShowAndWait(int& modalResult)
139 {
140         __isModal = true;
141
142         return DoModal(modalResult);
143 }
144
145
146 Panel*
147 _WebPopup::CreateAndAddPanel(void)
148 {
149         std::unique_ptr<Panel> pPanel(new (std::nothrow) Panel());
150         SysTryReturn(NID_WEB_CTRL, pPanel.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
151
152         Rectangle panelRect(0, 0, __pWebPopupData->popupDim.width, __pWebPopupData->btnDim.height);
153
154         result r = pPanel->Construct(panelRect);
155         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
156
157         r = AddControl(*pPanel);
158         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
159
160         return pPanel.release();
161 }
162
163
164 result
165 _WebPopup::CreateAndAddButtons(const IList& buttonIds, const IList& buttonTitles, Panel* pPanel)
166 {
167         SysTryReturnResult(NID_WEB_CTRL, pPanel, E_INVALID_ARG, "[%s] Panel Pointer is null.", GetErrorMessage(E_INVALID_ARG));
168
169         _WebPopup* pParent = dynamic_cast<_WebPopup*>(pPanel->GetParent());
170         SysTryReturnResult(NID_WEB_CTRL, pParent == this, E_INVALID_ARG, "[E_INVALID_ARG] Panel not attached to popup.");
171
172         int idCount = buttonIds.GetCount();
173         int titleCount = buttonTitles.GetCount();
174         SysTryReturnResult(NID_WEB_CTRL, idCount > 0 && titleCount > 0 && idCount == titleCount, E_INVALID_DATA, "[E_INVALID_DATA] mismatch in count of Ids and Ttitles.");
175
176         int buttonMargin = __pWebPopupData->spacePad/2;
177         int buttonWidth = (__pWebPopupData->popupDim.width - buttonMargin*(idCount+1)) / idCount;
178
179         result r = E_SUCCESS;
180         for (int i = 0; i < idCount; i++)
181         {
182                 const Integer* pButtonId = static_cast<const Integer*>(buttonIds.GetAt(i));
183                 const String* pButtonTitle = static_cast<const String*>(buttonTitles.GetAt(i));
184                 SysTryReturn(NID_WEB_CTRL, pButtonId && pButtonTitle, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
185
186                 //Create button
187                 std::unique_ptr<Button> pButton(new (std::nothrow) Button());
188                 SysTryReturnResult(NID_WEB_CTRL, pButton.get(), E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory Allocation failed.");
189
190                 r = pButton->Construct(Rectangle((buttonMargin*(i+1))+(buttonWidth*i), 0, buttonWidth, __pWebPopupData->btnDim.height), *pButtonTitle);
191                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
192
193                 pButton->SetActionId(pButtonId->ToInt());
194
195                 //Add button to panel
196                 r = pPanel->AddControl(*pButton);
197                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
198
199                 pButton->AddActionEventListener(*this);
200                 pButton.release();
201         }
202
203         return r;
204 }
205
206
207 bool
208 _WebPopup::IsModalPopup(void)
209 {
210         return __isModal;
211 }
212
213
214 _WebPopupData*
215 _WebPopup::GetPopupData(bool refresh)
216 {
217         if (!refresh && __pWebPopupData.get())
218         {
219                 return __pWebPopupData.get();
220         }
221
222         __pWebPopupData.reset();
223
224         __pWebPopupData = std::unique_ptr<_WebPopupData> (new (std::nothrow) _WebPopupData());
225         SysTryReturn(NID_WEB_CTRL, __pWebPopupData.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
226
227         _ControlOrientation orientation = _CONTROL_ORIENTATION_PORTRAIT;
228
229         GET_SHAPE_CONFIG(MESSAGEBOX::DEFAULT_WIDTH, orientation, __pWebPopupData->popupDim.width);
230         GET_SHAPE_CONFIG(MESSAGEBOX::MAX_HEIGHT, orientation, __pWebPopupData->popupDim.height);
231         GET_SHAPE_CONFIG(POPUP::SIDE_BORDER, orientation, __pWebPopupData->sideMargin);
232         GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, orientation, __pWebPopupData->bottomMargin);
233         GET_SHAPE_CONFIG(POPUP::TITLE_HEIGHT, orientation, __pWebPopupData->titleHeight);
234
235         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_HEIGHT, orientation, __pWebPopupData->btnDim.height);
236         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_SIDE_MARGIN_02, orientation, __pWebPopupData->spacePad);
237
238         GET_SHAPE_CONFIG(LABEL::TEXT_FONT_SIZE, orientation, __pWebPopupData->labelFontSize);
239         GET_DIMENSION_CONFIG(CHECKBUTTON::MIN_DIMENSION, orientation, __pWebPopupData->checkDim);
240
241         GET_DIMENSION_CONFIG(EDIT::MIN_SIZE, orientation, __pWebPopupData->editDim);
242
243         __pWebPopupData->labelDim.width = __pWebPopupData->popupDim.width - 2*__pWebPopupData->sideMargin;
244         __pWebPopupData->labelDim.height = 3*__pWebPopupData->labelFontSize;
245
246         return __pWebPopupData.get();
247 }
248
249
250 }}} // Tizen::Web::Controls