add patch
[framework/osp/web.git] / src / controls / FWebCtrl_FormDataWindow.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_FormDataWindow.cpp
20  * @brief               The file contains the definition of _FormDataWindow class.
21  */
22 #include <ewk_view.h>
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include <FSysVibrator.h>
26 #include <FUiCtrlCustomItem.h>
27 #include <FUiCtrlListView.h>
28 #include <FUiVerticalBoxLayout.h>
29 #include <FBase_StringConverter.h>
30 #include <FUi_ResourceManager.h>
31 #include "FWebCtrl_FormDataWindow.h"
32 #include "FWebCtrl_WebImpl.h"
33
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Base::Utility;
38 using namespace Tizen::Graphics;
39 using namespace Tizen::System;
40 using namespace Tizen::Ui;
41 using namespace Tizen::Ui::Controls;
42
43
44 namespace Tizen { namespace Web { namespace Controls
45 {
46
47
48 _FormDataWindow::_FormDataWindow(void)
49                                         : __pListView(null)
50                                         , __pWebView(null)
51                                         , __pWebImpl(null)
52                                         , __listItemHeight(0)
53 {
54 }
55
56
57 _FormDataWindow::~_FormDataWindow(void)
58 {
59         __listElementArray.RemoveAll(true);
60 }
61
62
63 result
64 _FormDataWindow::Construct(const Rectangle& rect, _WebImpl* pImpl, Evas_Object* pWebView)
65 {
66         VerticalBoxLayout layout;
67
68         result r = layout.Construct(VERTICAL_DIRECTION_DOWNWARD);
69         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
70
71         GET_COLOR_CONFIG(LIST::ELEMENT_TEXT_NORMAL, __normalColor);
72         GET_COLOR_CONFIG(LIST::ELEMENT_TEXT_PRESSED, __pressedColor);
73         GET_COLOR_CONFIG(LIST::ELEMENT_TEXT_HIGHLIGHTED, __highlightedColor);
74
75         __listItemHeight = rect.height;
76         Rectangle windowRect(rect);
77
78         r = Window::Construct(layout, windowRect, true, true);
79         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
80         SetBounds(rect);
81
82         Window::SetOwner(&pImpl->GetPublic());
83
84         std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
85         SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
86
87         r = pListView->Construct(Rectangle(0, 0, windowRect.width, windowRect.height), true, SCROLL_STYLE_FADE_OUT);
88         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
89
90         r = __listElementArray.Construct();
91         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
92
93         pListView->SetItemProvider(*this);
94         pListView->AddListViewItemEventListener(*this);
95         pListView->SetTextOfEmptyList(L"");
96
97         r = AddControl(*pListView);
98         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
99
100         __pListView = pListView.release();
101
102         std::unique_ptr< VerticalBoxLayout > pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
103         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
104
105         pLayout->SetHorizontalAlignment(*__pListView, LAYOUT_HORIZONTAL_ALIGN_CENTER);
106         pLayout->SetHorizontalFitPolicy(*__pListView, FIT_POLICY_PARENT);
107
108         __pWebImpl = pImpl;
109         __pWebView = pWebView;
110
111         return E_SUCCESS;
112 }
113
114
115 //IListViewItemProvider
116 ListItemBase*
117 _FormDataWindow::CreateItem(int index, int itemWidth)
118 {
119         String* pListElement = static_cast< String* >(__listElementArray.GetAt(index));
120         SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
121
122         std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
123         SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
124
125         result r = pItem->Construct(Dimension(itemWidth, __listItemHeight), LIST_ANNEX_STYLE_NORMAL);
126         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
127
128         int listItemHeight = 0;
129         int listItemTextSize = 0;
130         int listItemLeftMargin = 0;
131
132         GET_SHAPE_CONFIG(LIST::LIST_DEFAULT_ITEM_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, listItemHeight);
133         GET_SHAPE_CONFIG(LIST::LIST_ITEM_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, listItemTextSize);
134         GET_SHAPE_CONFIG(LIST::LIST_ITEM_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, listItemLeftMargin);
135
136         if (__listItemHeight > listItemHeight)
137         {
138                 __listItemHeight = listItemHeight;
139         }
140
141         if (__listItemHeight*2/3 < listItemTextSize)
142         {
143                 listItemTextSize = __listItemHeight*2/3;
144         }
145
146         Rectangle itemRect = Rectangle(listItemLeftMargin, 0, itemWidth, __listItemHeight);
147         r = pItem->AddElement(itemRect, ID_FORMAT_STRING, *pListElement, listItemTextSize, __normalColor, __pressedColor, __highlightedColor);
148         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
149
150         return pItem.release();
151 }
152
153
154 bool
155 _FormDataWindow::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
156 {
157         delete pItem;
158
159         return true;
160 }
161
162
163 int
164 _FormDataWindow::GetItemCount(void)
165 {
166         return __listElementArray.GetCount();
167 }
168
169
170 // IListViewItemEventListener
171 void
172 _FormDataWindow::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
173 {
174 }
175
176
177 void
178 _FormDataWindow::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
179 {
180         std::unique_ptr< char[] > pInputValue;
181
182         String* pListElement = static_cast< String* >(__listElementArray.GetAt(index));
183         SysTryCatch(NID_WEB_CTRL, pListElement, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
184
185         pInputValue = std::unique_ptr< char[] >(_StringConverter::CopyToCharArrayN(*pListElement));
186         SysTryCatch(NID_WEB_CTRL, pInputValue.get(), , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
187
188         ewk_view_focused_input_element_value_set(__pWebView, pInputValue.get());
189
190 CATCH:
191         SetShowState(false);
192         __pWebImpl->HideFormDataWindow(false);
193 }
194
195
196 void
197 _FormDataWindow::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
198 {
199 }
200
201
202 result
203 _FormDataWindow::UpdateList(Eina_List* pDataList, const Rectangle& rect)
204 {
205         SysTryReturnResult(NID_WEB_CTRL, pDataList, E_INVALID_ARG, "Invalid argument(s) is used. data list pointer should not be null.");
206
207         __listElementArray.RemoveAll(true);
208
209         __listItemHeight = rect.height;
210
211         int itemCount = eina_list_count(pDataList);
212
213         for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
214         {
215                 char* pItemText = static_cast<char*>(eina_list_nth(pDataList, itemIndex));
216
217                 std::unique_ptr<String> pListElement(new (std::nothrow) String(pItemText));
218                 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
219
220                 result r = __listElementArray.Add(*pListElement);
221                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
222
223                 pListElement.release();
224         }
225
226         __pListView->UpdateList();
227
228         int windowHeight = itemCount*__listItemHeight;
229
230         Rectangle webRect = __pWebImpl->GetBounds();//is it better to take application rect
231         int bottomLimit = webRect.y + webRect.height;
232         int topLimit = webRect.y;
233         Rectangle windowRect(rect);
234
235         if (__pWebImpl->IsKeypadVisible())
236         {
237                 Rectangle keyRect = __pWebImpl->GetPreviousKeypadBounds();
238                 bottomLimit = keyRect.y;
239         }
240
241         int windowBottom = rect.y + windowHeight;
242
243         while (true)
244         {
245                 if (windowBottom > bottomLimit)
246                 {
247                         int newWindowTop = rect.y - rect.height - windowHeight;
248                         if (newWindowTop < topLimit)//check in reverse direction
249                         {
250                                 if (--itemCount)
251                                 {
252                                         windowHeight = itemCount*__listItemHeight;
253                                         windowBottom = rect.y + windowHeight;
254                                 }
255                                 else
256                                 {
257                                         break;
258                                 }
259                         }
260                         else
261                         {//reverse direction is good
262                                 windowRect.y = rect.y - rect.height - windowHeight;
263                                 break;
264                         }
265                 }
266                 else
267                 {
268                         //rect is good
269                         break;
270                 }
271         }
272
273         windowRect.height = windowHeight;
274         __pListView->SetBounds(Rectangle(0, 0, windowRect.width, windowRect.height));
275         SetBounds(windowRect);
276
277         return E_SUCCESS;
278 }
279
280
281 result
282 _FormDataWindow::LaunchFormDataWindow(void)
283 {
284         result r = SetFocusable(false);
285         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
286
287         r = SetShowState(true);
288         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
289
290         r = Show();
291         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
292
293         return E_SUCCESS;
294 }
295
296
297 }}} // Tizen::Web::Controls