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 <FUiKeyEventInfo.h>
39 #include <FUiVerticalBoxLayout.h>
40 #include <FSys_SystemResource.h>
41 #include <FUi_ResourceManager.h>
42 #include "FWebCtrl_SelectBox.h"
45 using namespace Tizen::Base;
46 using namespace Tizen::Base::Collection;
47 using namespace Tizen::Graphics;
48 using namespace Tizen::System;
49 using namespace Tizen::Ui;
50 using namespace Tizen::Ui::Controls;
53 namespace Tizen { namespace Web { namespace Controls
57 static const int POSITION_INVALID = -1;
60 template< class Type > Type
67 int CompareChangedItems(const void* pData1, const void* pData2)
69 SysAssertf(pData1 && pData2, "input parameters are null");
71 const int* pLeft = static_cast<const int*>(pData1);
72 const int* pRight = static_cast<const int*>(pData2);
74 return (*pLeft - *pRight);
78 //Internal class for maintaining states of list elements
83 _ListElement(const String& itemText);
85 virtual ~_ListElement(void) {}
88 void ListElementSetItemEnabled(bool isEnabled);
89 void ListElementSetItemType(short itemType);
90 void ListElementSetItemSelected(bool isSelected);
93 short ListElementGetItemType(void) const { return __itemType; }
95 String ListElementGetString(void) const { return __itemStr; }
97 bool ListElementIsItemEnabled(void) const { return __isEnabled; }
99 bool ListElementIsItemSelected(void) const { return __isSelected; }
112 _ListElement::_ListElement(const String& itemText)
113 : __itemType(_SelectBox::LIST_ITEM_TYPE_NORMAL)
115 , __isSelected(false)
117 __itemStr = itemText;
122 _ListElement::ListElementSetItemEnabled(bool isEnabled)
124 __isEnabled = isEnabled;
129 _ListElement::ListElementSetItemType(short itemType)
131 __itemType = itemType;
136 _ListElement::ListElementSetItemSelected(bool isSelected)
138 __isSelected = isSelected;
142 _SelectBox::_SelectBox(void)
144 , __multiSelection(false)
145 , __SelectedIndex(-1)
147 , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
149 , __pToggledArray(null)
154 _SelectBox::~_SelectBox(void)
156 if (__multiSelection)
158 eina_inarray_flush(__pToggledArray);
159 delete __pToggledArray;
162 __listElementArray.RemoveAll(true);
167 _SelectBox::Construct(bool isMultiSelect, const String& title, int listCount, Evas_Object* pWebView)
169 result r = E_SUCCESS;
172 int listItemHeight = 0;
173 int listMaxHeihgt = 0;
174 int listViewHeight = 0;
176 __pWebView = pWebView;
178 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
180 __multiSelection = isMultiSelect;
182 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
183 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
185 listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + pPopupData->spacePad;
187 rect.height = listMaxHeihgt;
188 rect.width = pPopupData->popupDim.width;
192 r = _WebPopup::Construct(!title.IsEmpty(), Dimension(rect.width, rect.height));
193 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
195 if (!title.IsEmpty())
197 Popup::SetTitleText(title);
200 listViewHeight = listCount * listItemHeight;
201 std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
202 SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
204 r = pListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, listViewHeight), true, SCROLL_STYLE_FADE_OUT);
205 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
207 r = __listElementArray.Construct();
208 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
210 pListView->SetItemProvider(*this);
211 pListView->AddListViewItemEventListener(*this);
212 pListView->SetTextOfEmptyList(L"");
214 r = AddControl(*pListView);
215 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
217 __pListView = pListView.release();
219 Panel* pButtonPanel = CreateAndAddPanel();
220 SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
222 _SystemResource* pSysResource = _SystemResource::GetInstance();
223 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
226 r = idList.Construct();
227 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
229 idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
230 if (__multiSelection)
232 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
236 r = titleList.Construct();
237 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
239 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
240 if (__multiSelection)
242 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
244 __pToggledArray = eina_inarray_new(sizeof(int), 0);
245 SysTryReturnResult(NID_WEB_CTRL, __pToggledArray, E_OUT_OF_MEMORY, "Memory Allocation failed.");
248 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
249 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
251 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
252 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
254 pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
256 SetPropagatedKeyEventListener(this);
262 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
264 result r = E_SUCCESS;
265 std::unique_ptr<_ListElement> pListElement(new (std::nothrow) _ListElement(itemText));
266 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
268 pListElement->ListElementSetItemType(itemType);
269 pListElement->ListElementSetItemSelected(slected);
271 r = __listElementArray.Add(*pListElement);
272 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
274 pListElement.release();
280 _SelectBox::GetSelectedListN(void) const
282 result r = E_SUCCESS;
284 std::unique_ptr<ArrayList, AllElementsDeleter> pSelectedList(new (std::nothrow) ArrayList());
285 SysTryReturn(NID_WEB_CTRL, pSelectedList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
287 r = pSelectedList->Construct();
288 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
290 for (int index = 0; index < __pListView->GetItemCount(); index++)
292 if (__pListView->IsItemChecked(index))
294 std::unique_ptr<Integer> pIdxResult(new (std::nothrow) Integer(index));
295 SysTryReturn(NID_WEB_CTRL, pIdxResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
297 r = pSelectedList->Add(*pIdxResult);
298 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
300 pIdxResult.release();
304 return pSelectedList.release();
308 //IListViewItemProvider
310 _SelectBox::CreateItem(int index, int itemWidth)
312 result r = E_SUCCESS;
313 int listItemXPos = 0;
314 int listItemYPos = 0;
315 int checkBoxWidth = 0;
316 int listItemHeight = 0;
317 int popupBetweenListNBtnGap = 0;
319 ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
320 ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
322 _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
323 SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
325 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
326 GET_SHAPE_CONFIG(LISTVIEW::ITEM_ELEMENT_LEFT_MARGIN, __orientation, listItemXPos);
327 GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
328 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
330 std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
331 SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
333 int itemType = pListElement->ListElementGetItemType();
334 Rectangle popupRect = this->GetClientAreaBounds();
336 int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
340 case LIST_ITEM_TYPE_NORMAL:
342 case LIST_ITEM_TYPE_SUB:
343 r = pItem->Construct(Dimension(itemWidth, listItemHeight), subStyle);
344 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
347 case LIST_ITEM_TYPE_GROUP:
348 SysLog(NID_WEB_CTRL, "The current value of index [%d] [%d]", index, pListElement->ListElementIsItemEnabled());
349 r = pItem->Construct(Dimension(itemWidth, listItemHeight), groupStyle);
350 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
357 String elementString = pListElement->ListElementGetString();
359 if (elementString.IsEmpty())
361 elementString.Append(L" ");
364 r = pItem->AddElement(Rectangle(listItemXPos, listItemYPos, listTxtMaxWidth, listItemHeight),
365 ID_FORMAT_STRING, elementString, true);
366 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
368 if (IsMultiSelectable())
370 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
371 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
374 r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
375 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
377 return pItem.release();
382 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
392 _SelectBox::GetItemCount(void)
394 return __listElementArray.GetCount();
399 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
405 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
407 if (__multiSelection)
409 int pos = eina_inarray_search(__pToggledArray, &index, CompareChangedItems);
411 if (pos == POSITION_INVALID)
413 eina_inarray_push(__pToggledArray, &index);
417 eina_inarray_remove(__pToggledArray, &index);
422 //In case of single selection ... update the index.
423 __SelectedIndex = index;
424 SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
427 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
430 result r = HidePopup(__SelectedIndex);
431 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
437 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
444 _SelectBox::OnActionPerformed(const Control& source, int actionId)
448 case ID_BUTTON_SELECTION:
450 ewk_view_popup_menu_multiple_select(__pWebView, __pToggledArray);
453 case ID_BUTTON_CANCEL:
455 if (__multiSelection)
457 ewk_view_popup_menu_multiple_select(__pWebView, null);
461 ewk_view_popup_menu_select(__pWebView, __prevIndex);
466 SysAssertf(false, "Invalid Action ID");
469 result r = HidePopup(__SelectedIndex);
470 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
474 _SelectBox::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
480 _SelectBox::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
482 if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
484 if (__multiSelection)
486 ewk_view_popup_menu_multiple_select(__pWebView, null);
490 ewk_view_popup_menu_select(__pWebView, __prevIndex);
492 result r = HidePopup(__SelectedIndex);
495 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
503 _SelectBox::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
509 _SelectBox::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
515 _SelectBox::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
521 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
523 SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
527 __listElementArray.RemoveAll(true);
528 if (__multiSelection)
530 eina_inarray_flush(__pToggledArray);
534 //Create list required for selectBox
535 int itemCount = eina_list_count(pItems);
536 SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
538 bool isSelected = false;
540 if (!__multiSelection)
542 __prevIndex = prevIndex;
545 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
547 Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
548 SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
550 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
552 String text(ewk_popup_menu_item_text_get(pItem));
554 if (__multiSelection)
556 isSelected = ( ewk_popup_menu_item_selected_get(pItem) == EINA_TRUE ) ? true : false;
560 isSelected = (itemIndex == prevIndex) ? true : false;
565 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
569 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
574 __pListView->UpdateList();
580 }}} // Tizen::Web::Controls