37072: Adjust selectbox height based on number of listview items
[framework/osp/web.git] / src / controls / FWebCtrl_SelectBox.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_SelectBox.cpp
20  * @brief               The file contains the definition of _SelectBox class.
21  *
22  * The file contains the definition of _SelectBox class.
23  */
24 #include <ewk_view.h>
25 #include <ewk_popup_menu_item.h>
26 #include <unique_ptr.h>
27 #include <FBaseColAllElementsDeleter.h>
28 #include <FBaseSysLog.h>
29 #include <FGrpDimension.h>
30 #include <FGrpRectangle.h>
31 #include <FUiCtrlButton.h>
32 #include <FUiCtrlCustomItem.h>
33 #include <FUiCtrlGroupedList.h>
34 #include <FUiCtrlListItemBase.h>
35 #include <FUiCtrlListView.h>
36 #include <FUiCtrlPanel.h>
37 #include <FUiIActionEventListener.h>
38 #include <FUiVerticalBoxLayout.h>
39 #include <FSys_SystemResource.h>
40 #include <FUi_ResourceManager.h>
41 #include "FWebCtrl_SelectBox.h"
42
43
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Graphics;
47 using namespace Tizen::System;
48 using namespace Tizen::Ui;
49 using namespace Tizen::Ui::Controls;
50
51
52 namespace Tizen { namespace Web { namespace Controls
53 {
54
55 static const int SELECTBOX_BUTTON_WIDTH = 100;
56 template< class Type > Type
57 Max(Type a, Type b)
58 {
59         return a > b ? a : b;
60 }
61
62
63 //Internal class for maintaining states of list elements
64 class _ListElement
65         : public Object
66 {
67 public:
68         _ListElement(const String& itemText);
69
70         virtual ~_ListElement(void) {}
71
72         //Set functions
73         void ListElementSetItemEnabled(bool isEnabled);
74         void ListElementSetItemType(short itemType);
75         void ListElementSetItemSelected(bool isSelected);
76
77         //Get functions
78         short ListElementGetItemType(void) const { return __itemType; }
79
80         String ListElementGetString(void) const { return __itemStr; }
81
82         bool ListElementIsItemEnabled(void) const { return __isEnabled; }
83
84         bool ListElementIsItemSelected(void) const { return __isSelected; }
85
86 private:
87         _ListElement(void);
88
89 private:
90         String __itemStr;
91         short __itemType;
92         bool __isEnabled;
93         bool __isSelected;
94 }; // _ListElement
95
96
97 _ListElement::_ListElement(const String& itemText)
98         : __itemType(_SelectBox::LIST_ITEM_TYPE_NORMAL)
99         , __isEnabled(true)
100         , __isSelected(false)
101 {
102         __itemStr = itemText;
103 }
104
105
106 void
107 _ListElement::ListElementSetItemEnabled(bool isEnabled)
108 {
109         __isEnabled = isEnabled;
110 }
111
112
113 void
114 _ListElement::ListElementSetItemType(short itemType)
115 {
116         __itemType = itemType;
117 }
118
119
120 void
121 _ListElement::ListElementSetItemSelected(bool isSelected)
122 {
123         __isSelected = isSelected;
124 }
125
126
127 _SelectBox::_SelectBox(void)
128         : __pListView(null)
129         , __multiSelection(false)
130         , __SelectedIndex(-1)
131         , __prevIndex(-1)
132         , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
133         , __pWebView(null)
134 {
135 }
136
137
138 _SelectBox::~_SelectBox(void)
139 {
140         __listElementArray.RemoveAll(true);
141 }
142
143
144 result
145 _SelectBox::Construct(bool isMultiSelect, const String& title, int listCount, Evas_Object* pWebView)
146 {
147         result r = E_SUCCESS;
148         Rectangle rect;
149
150         int listItemHeight = 0;
151         int listMaxHeihgt = 0;
152         int popupWinTopMargin = 0;
153         int popupWinBottomMargin = 0;
154         int listViewHeight = 0;
155
156         __pWebView = pWebView;
157
158         GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
159         GET_SHAPE_CONFIG(POPUP::TOP_BORDER, __orientation, popupWinTopMargin);
160         GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, __orientation, popupWinBottomMargin);
161
162         __multiSelection = isMultiSelect;
163
164         _WebPopupData* pPopupData = _WebPopup::GetPopupData();
165         SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
166
167         listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + 2 * ( pPopupData->spacePad + popupWinTopMargin + popupWinBottomMargin);
168
169         rect.height = listMaxHeihgt;
170         rect.width = pPopupData->popupDim.width;
171         rect.x = 0;
172         rect.y = 0;
173
174         r = _WebPopup::Construct(!title.IsEmpty(), Dimension(rect.width, rect.height));
175         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
176
177         if (!title.IsEmpty())
178         {
179                 Popup::SetTitleText(title);
180         }
181
182         listViewHeight = GetClientAreaBounds().height - popupWinTopMargin - popupWinBottomMargin - pPopupData->btnDim.height;
183         std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
184         SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
185
186         r = pListView->Construct(Rectangle(0, popupWinTopMargin, GetClientAreaBounds().width, listViewHeight), true, SCROLL_STYLE_FADE_OUT);
187         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
188
189         r = __listElementArray.Construct();
190         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
191
192         pListView->SetItemProvider(*this);
193         pListView->AddListViewItemEventListener(*this);
194         pListView->SetTextOfEmptyList(L"");
195
196         r = AddControl(*pListView);
197         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
198
199         __pListView = pListView.release();
200
201         Panel* pButtonPanel = CreateAndAddPanel();
202         SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
203
204         _SystemResource* pSysResource = _SystemResource::GetInstance();
205         SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
206
207         ArrayList idList;
208         r = idList.Construct();
209         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
210         if (__multiSelection)
211         {
212                 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
213         }
214         idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
215
216         ArrayList titleList;
217         r = titleList.Construct();
218         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
219         if (__multiSelection)
220         {
221                 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
222         }
223         titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
224
225         r = CreateAndAddButtons(idList, titleList, pButtonPanel);
226         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
227
228         std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
229         SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
230
231         pLayout->SetSpacing(*pButtonPanel,  pPopupData->sideMargin);
232
233         return r;
234 }
235
236 result
237 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
238 {
239         result r = E_SUCCESS;
240         std::unique_ptr<_ListElement> pListElement(new (std::nothrow) _ListElement(itemText));
241         SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
242
243         pListElement->ListElementSetItemType(itemType);
244         pListElement->ListElementSetItemSelected(slected);
245
246         r = __listElementArray.Add(*pListElement);
247         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
248
249         pListElement.release();
250         return r;
251 }
252
253
254 IList*
255 _SelectBox::GetSelectedListN(void) const
256 {
257         result r = E_SUCCESS;
258
259         std::unique_ptr<ArrayList, AllElementsDeleter> pSelectedList(new (std::nothrow) ArrayList());
260         SysTryReturn(NID_WEB_CTRL, pSelectedList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
261
262         r = pSelectedList->Construct();
263         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
264
265         for (int index = 0; index < __pListView->GetItemCount(); index++)
266         {
267                 if (__pListView->IsItemChecked(index))
268                 {
269                         std::unique_ptr<Integer> pIdxResult(new (std::nothrow) Integer(index));
270                         SysTryReturn(NID_WEB_CTRL, pIdxResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
271
272                         r = pSelectedList->Add(*pIdxResult);
273                         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
274
275                         pIdxResult.release();
276                 }
277         }
278
279         return pSelectedList.release();
280 }
281
282
283 //IListViewItemProvider
284 ListItemBase*
285 _SelectBox::CreateItem(int index, int itemWidth)
286 {
287         result r = E_SUCCESS;
288         int listItemXPos = 0;
289         int listItemYPos = 0;
290         int checkBoxWidth = 0;
291         int listItemHeight = 0;
292         int popupBetweenListNBtnGap = 0;
293
294         ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
295         ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
296
297         _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
298         SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
299
300         GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
301         GET_SHAPE_CONFIG(LISTVIEW::ITEM_ELEMENT_LEFT_MARGIN, __orientation, listItemXPos);
302         GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
303         GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
304 //      GET_SHAPE_CONFIG(LIST::GROUPITEM_DEFAULT_FONT_SIZE, __orientation, listItemTxtHeight);
305
306         std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
307         SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
308
309         int itemType = pListElement->ListElementGetItemType();
310         Rectangle popupRect = this->GetClientAreaBounds();
311
312         int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
313
314         switch (itemType)
315         {
316         case LIST_ITEM_TYPE_NORMAL:
317         //fall through
318         case LIST_ITEM_TYPE_SUB:
319                 r = pItem->Construct(Dimension(itemWidth, listItemHeight), subStyle);
320                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
321                 break;
322
323         case LIST_ITEM_TYPE_GROUP:
324                 SysLog(NID_WEB_CTRL, "The current value of index [%d] [%d]", index, pListElement->ListElementIsItemEnabled());
325                 r = pItem->Construct(Dimension(itemWidth, listItemHeight), groupStyle);
326                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
327                 break;
328
329         default:
330                 return null;
331         }
332
333         String elementString = pListElement->ListElementGetString();
334
335         if (elementString.IsEmpty())
336         {
337                 elementString.Append(L" ");
338         }
339
340         r = pItem->AddElement(Rectangle(listItemXPos, listItemYPos, listTxtMaxWidth, listItemHeight),
341                                                   ID_FORMAT_STRING, elementString, true);
342         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
343
344         if (IsMultiSelectable())
345         {
346                 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
347                 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
348         }
349
350         r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
351         SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
352
353         return pItem.release();
354 }
355
356
357 bool
358 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
359 {
360         delete pItem;
361         pItem = null;
362
363         return true;
364 }
365
366
367 int
368 _SelectBox::GetItemCount(void)
369 {
370         return __listElementArray.GetCount();
371 }
372
373
374 void
375 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
376 {
377 }
378
379
380 void
381 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
382 {
383         if (__multiSelection)
384         {
385                 return;
386         }
387
388         //In case of single selection ... update the index.
389         __SelectedIndex = index;
390         SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
391         if (__pWebView)
392         {
393                 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
394         }
395
396         result r = HidePopup(__SelectedIndex);
397         SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
398 }
399
400
401 void
402 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
403 {
404
405 }
406
407
408 void
409 _SelectBox::OnActionPerformed(const Control& source, int actionId)
410 {
411         switch (actionId)
412         {
413         case ID_BUTTON_SELECTION:
414         //fall through
415         case ID_BUTTON_CANCEL:
416         {
417                 if (__pWebView)
418                 {
419                         ewk_view_popup_menu_select(__pWebView, __prevIndex);
420                 }
421
422                 result r = HidePopup(__SelectedIndex);
423                 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
424                 break;
425         }
426         }
427 }
428
429
430 result
431 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
432 {
433         SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
434
435         if (clearPrevList)
436         {
437                 __listElementArray.RemoveAll(true);
438         }
439
440         //Create list required for selectBox
441         int itemCount = eina_list_count(pItems);
442         SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s]  A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
443
444         __prevIndex = prevIndex;
445
446         for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
447         {
448                 Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
449                 SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s]  A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
450
451                 bool isSelected = (itemIndex == prevIndex) ? true : false;
452                 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
453                 {
454                         String text(ewk_popup_menu_item_text_get(pItem));
455
456                         if (isGroupdList)
457                         {
458                                 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
459                         }
460                         else
461                         {
462                                 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
463                         }
464                 }
465         }
466
467         __pListView->UpdateList();
468
469         return E_SUCCESS;
470 }
471
472
473 }}} // Tizen::Web::Controls