2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FWebCtrl_SelectBox.cpp
20 * @brief The file contains the definition of _SelectBox class.
22 * The file contains the definition of _SelectBox class.
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"
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;
52 namespace Tizen { namespace Web { namespace Controls
56 static const int POSITION_INVALID = -1;
59 template< class Type > Type
66 int CompareChangedItems(const void* pData1, const void* pData2)
68 SysAssertf(pData1 && pData2, "input parameters are null");
70 const int* pLeft = static_cast<const int*>(pData1);
71 const int* pRight = static_cast<const int*>(pData2);
73 return (*pLeft - *pRight);
77 //Internal class for maintaining states of list elements
82 _ListElement(const String& itemText);
84 virtual ~_ListElement(void) {}
87 void ListElementSetItemEnabled(bool isEnabled);
88 void ListElementSetItemType(short itemType);
89 void ListElementSetItemSelected(bool isSelected);
92 short ListElementGetItemType(void) const { return __itemType; }
94 String ListElementGetString(void) const { return __itemStr; }
96 bool ListElementIsItemEnabled(void) const { return __isEnabled; }
98 bool ListElementIsItemSelected(void) const { return __isSelected; }
111 _ListElement::_ListElement(const String& itemText)
112 : __itemType(_SelectBox::LIST_ITEM_TYPE_NORMAL)
114 , __isSelected(false)
116 __itemStr = itemText;
121 _ListElement::ListElementSetItemEnabled(bool isEnabled)
123 __isEnabled = isEnabled;
128 _ListElement::ListElementSetItemType(short itemType)
130 __itemType = itemType;
135 _ListElement::ListElementSetItemSelected(bool isSelected)
137 __isSelected = isSelected;
141 _SelectBox::_SelectBox(void)
143 , __multiSelection(false)
144 , __SelectedIndex(-1)
146 , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
148 , __pToggledArray(null)
153 _SelectBox::~_SelectBox(void)
155 if (__multiSelection)
157 eina_inarray_flush(__pToggledArray);
158 delete __pToggledArray;
161 __listElementArray.RemoveAll(true);
166 _SelectBox::Construct(bool isMultiSelect, const String& title, int listCount, Evas_Object* pWebView)
168 result r = E_SUCCESS;
171 int listItemHeight = 0;
172 int listMaxHeihgt = 0;
173 int listViewHeight = 0;
175 __pWebView = pWebView;
177 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
179 __multiSelection = isMultiSelect;
181 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
182 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
184 listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + pPopupData->spacePad;
186 rect.height = listMaxHeihgt;
187 rect.width = pPopupData->popupDim.width;
191 r = _WebPopup::Construct(!title.IsEmpty(), Dimension(rect.width, rect.height));
192 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
194 if (!title.IsEmpty())
196 Popup::SetTitleText(title);
199 listViewHeight = listCount * listItemHeight;
200 std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
201 SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
203 r = pListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, listViewHeight), true, SCROLL_STYLE_FADE_OUT);
204 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
206 r = __listElementArray.Construct();
207 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
209 pListView->SetItemProvider(*this);
210 pListView->AddListViewItemEventListener(*this);
211 pListView->SetTextOfEmptyList(L"");
213 r = AddControl(*pListView);
214 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
216 __pListView = pListView.release();
218 Panel* pButtonPanel = CreateAndAddPanel();
219 SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
221 _SystemResource* pSysResource = _SystemResource::GetInstance();
222 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
225 r = idList.Construct();
226 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
227 if (__multiSelection)
229 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
231 idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
234 r = titleList.Construct();
235 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
236 if (__multiSelection)
238 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
240 __pToggledArray = eina_inarray_new(sizeof(int), 0);
241 SysTryReturnResult(NID_WEB_CTRL, __pToggledArray, E_OUT_OF_MEMORY, "Memory Allocation failed.");
243 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
245 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
246 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
248 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
249 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
251 pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
257 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
259 result r = E_SUCCESS;
260 std::unique_ptr<_ListElement> pListElement(new (std::nothrow) _ListElement(itemText));
261 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
263 pListElement->ListElementSetItemType(itemType);
264 pListElement->ListElementSetItemSelected(slected);
266 r = __listElementArray.Add(*pListElement);
267 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
269 pListElement.release();
275 _SelectBox::GetSelectedListN(void) const
277 result r = E_SUCCESS;
279 std::unique_ptr<ArrayList, AllElementsDeleter> pSelectedList(new (std::nothrow) ArrayList());
280 SysTryReturn(NID_WEB_CTRL, pSelectedList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
282 r = pSelectedList->Construct();
283 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
285 for (int index = 0; index < __pListView->GetItemCount(); index++)
287 if (__pListView->IsItemChecked(index))
289 std::unique_ptr<Integer> pIdxResult(new (std::nothrow) Integer(index));
290 SysTryReturn(NID_WEB_CTRL, pIdxResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
292 r = pSelectedList->Add(*pIdxResult);
293 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
295 pIdxResult.release();
299 return pSelectedList.release();
303 //IListViewItemProvider
305 _SelectBox::CreateItem(int index, int itemWidth)
307 result r = E_SUCCESS;
308 int listItemXPos = 0;
309 int listItemYPos = 0;
310 int checkBoxWidth = 0;
311 int listItemHeight = 0;
312 int popupBetweenListNBtnGap = 0;
314 ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
315 ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
317 _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
318 SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
320 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
321 GET_SHAPE_CONFIG(LISTVIEW::ITEM_ELEMENT_LEFT_MARGIN, __orientation, listItemXPos);
322 GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
323 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
325 std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
326 SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
328 int itemType = pListElement->ListElementGetItemType();
329 Rectangle popupRect = this->GetClientAreaBounds();
331 int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
335 case LIST_ITEM_TYPE_NORMAL:
337 case LIST_ITEM_TYPE_SUB:
338 r = pItem->Construct(Dimension(itemWidth, listItemHeight), subStyle);
339 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
342 case LIST_ITEM_TYPE_GROUP:
343 SysLog(NID_WEB_CTRL, "The current value of index [%d] [%d]", index, pListElement->ListElementIsItemEnabled());
344 r = pItem->Construct(Dimension(itemWidth, listItemHeight), groupStyle);
345 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
352 String elementString = pListElement->ListElementGetString();
354 if (elementString.IsEmpty())
356 elementString.Append(L" ");
359 r = pItem->AddElement(Rectangle(listItemXPos, listItemYPos, listTxtMaxWidth, listItemHeight),
360 ID_FORMAT_STRING, elementString, true);
361 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
363 if (IsMultiSelectable())
365 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
366 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
369 r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
370 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
372 return pItem.release();
377 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
387 _SelectBox::GetItemCount(void)
389 return __listElementArray.GetCount();
394 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
400 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
402 if (__multiSelection)
404 int pos = eina_inarray_search(__pToggledArray, &index, CompareChangedItems);
406 if (pos == POSITION_INVALID)
408 eina_inarray_push(__pToggledArray, &index);
412 eina_inarray_remove(__pToggledArray, &index);
417 //In case of single selection ... update the index.
418 __SelectedIndex = index;
419 SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
422 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
425 result r = HidePopup(__SelectedIndex);
426 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
432 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
439 _SelectBox::OnActionPerformed(const Control& source, int actionId)
443 case ID_BUTTON_SELECTION:
445 ewk_view_popup_menu_multiple_select(__pWebView, __pToggledArray);
448 case ID_BUTTON_CANCEL:
450 if (__multiSelection)
452 ewk_view_popup_menu_multiple_select(__pWebView, null);
456 ewk_view_popup_menu_select(__pWebView, __prevIndex);
461 SysAssertf(false, "Invalid Action ID");
464 result r = HidePopup(__SelectedIndex);
465 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
470 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
472 SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
476 __listElementArray.RemoveAll(true);
477 if (__multiSelection)
479 eina_inarray_flush(__pToggledArray);
483 //Create list required for selectBox
484 int itemCount = eina_list_count(pItems);
485 SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
487 bool isSelected = false;
489 if (!__multiSelection)
491 __prevIndex = prevIndex;
494 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
496 Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
497 SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
499 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
501 String text(ewk_popup_menu_item_text_get(pItem));
503 if (__multiSelection)
505 isSelected = ( ewk_popup_menu_item_selected_get(pItem) == EINA_TRUE ) ? true : false;
509 isSelected = (itemIndex == prevIndex) ? true : false;
514 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
518 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
523 __pListView->UpdateList();
529 }}} // Tizen::Web::Controls