Merge "addition of formdatawindow implementation" into tizen_2.1
[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
23 #include <ewk_view.h>
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FSysVibrator.h>
27 #include <FUiCtrlCustomItem.h>
28 #include <FUiCtrlListView.h>
29 #include <FUiVerticalBoxLayout.h>
30 #include <FBase_StringConverter.h>
31 #include <FUi_ResourceManager.h>
32 #include "FWebCtrl_WebImpl.h"
33 #include "FWebCtrl_FormDataWindow.h"
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 namespace Tizen { namespace Web { namespace Controls
44 {
45
46 static const int MAX_LIST_ITEM_COUNT = 3;
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_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __listItemHeight);
72         Rectangle windowRect(rect);
73         windowRect.height = MAX_LIST_ITEM_COUNT*__listItemHeight;
74
75         r = Window::Construct(layout, windowRect, true, true);
76         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Error Propogating.", GetErrorMessage(r));
77         SetBounds(rect);
78
79         Window::SetOwner(&pImpl->GetPublic());
80
81         std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
82         SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
83
84         r = pListView->Construct(Rectangle(0, 0, windowRect.width, windowRect.height), true, SCROLL_STYLE_FADE_OUT);
85         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
86
87         r = __listElementArray.Construct();
88         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
89
90         pListView->SetItemProvider(*this);
91         pListView->AddListViewItemEventListener(*this);
92         pListView->SetTextOfEmptyList(L"");
93
94         r = AddControl(*pListView);
95         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
96
97         __pListView = pListView.release();
98
99         std::unique_ptr< VerticalBoxLayout > pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
100         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
101
102         pLayout->SetHorizontalAlignment(*__pListView, LAYOUT_HORIZONTAL_ALIGN_CENTER);
103         pLayout->SetHorizontalFitPolicy(*__pListView, FIT_POLICY_PARENT);
104
105         __pWebImpl = pImpl;
106         __pWebView = pWebView;
107
108         return E_SUCCESS;
109 }
110
111
112 //IListViewItemProvider
113 ListItemBase*
114 _FormDataWindow::CreateItem(int index, int itemWidth)
115 {
116         String* pListElement = static_cast< String* >(__listElementArray.GetAt(index));
117         SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
118
119         std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
120         SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
121
122         result r = pItem->Construct(Dimension(itemWidth, __listItemHeight), LIST_ANNEX_STYLE_NORMAL);
123         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
124
125         r = pItem->AddElement(Rectangle(0, 0, itemWidth, __listItemHeight), ID_FORMAT_STRING, *pListElement, true);
126         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
127
128         return pItem.release();
129 }
130
131
132 bool
133 _FormDataWindow::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
134 {
135         delete pItem;
136
137         return true;
138 }
139
140
141 int
142 _FormDataWindow::GetItemCount(void)
143 {
144         return __listElementArray.GetCount();
145 }
146
147
148 // IListViewItemEventListener
149 void
150 _FormDataWindow::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
151 {
152 }
153
154
155 void
156 _FormDataWindow::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
157 {
158         std::unique_ptr< char[] > pInputValue;
159
160         String* pListElement = static_cast< String* >(__listElementArray.GetAt(index));
161         SysTryCatch(NID_WEB_CTRL, pListElement, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
162
163         pInputValue = std::unique_ptr< char[] >(_StringConverter::CopyToCharArrayN(*pListElement));
164         SysTryCatch(NID_WEB_CTRL, pInputValue.get(), , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
165
166         ewk_view_focused_input_element_value_set(__pWebView, pInputValue.get());
167
168 CATCH:
169         SetShowState(false);
170         __pWebImpl->HideFormDataWindow(false);
171 }
172
173
174 void
175 _FormDataWindow::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
176 {
177 }
178
179
180 result
181 _FormDataWindow::UpdateList(Eina_List* pDataList, const Rectangle& rect)
182 {
183         SysTryReturnResult(NID_WEB_CTRL, pDataList, E_INVALID_ARG, "Invalid argument(s) is used. data list pointer should not be null.");
184
185         __listElementArray.RemoveAll(true);
186
187         int itemCount = eina_list_count(pDataList);
188
189         for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
190         {
191                 char* pItemText = static_cast<char*>(eina_list_nth(pDataList, itemIndex));
192
193                 std::unique_ptr<String> pListElement(new (std::nothrow) String(pItemText));
194                 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
195
196                 result r = __listElementArray.Add(*pListElement);
197                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
198
199                 pListElement.release();
200         }
201
202         __pListView->UpdateList();
203
204         Rectangle rec(rect);
205         rec.height = (itemCount >= MAX_LIST_ITEM_COUNT) ? (MAX_LIST_ITEM_COUNT*__listItemHeight) : (itemCount*__listItemHeight);
206         SetBounds(rec);
207
208         return E_SUCCESS;
209 }
210
211
212 result
213 _FormDataWindow::LaunchFormDataWindow(void)
214 {
215         result r = SetFocusable(false);
216         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
217
218         r = SetShowState(true);
219         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
220
221         r = Show();
222         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
223
224         return E_SUCCESS;
225 }
226
227
228 }}} // Tizen::Web::Controls