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