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