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));
228 if (__multiSelection)
230 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
232 idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
235 r = titleList.Construct();
236 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
237 if (__multiSelection)
239 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
241 __pToggledArray = eina_inarray_new(sizeof(int), 0);
242 SysTryReturnResult(NID_WEB_CTRL, __pToggledArray, E_OUT_OF_MEMORY, "Memory Allocation failed.");
244 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
246 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
247 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
249 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
250 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
252 pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
254 SetPropagatedKeyEventListener(this);
260 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
262 result r = E_SUCCESS;
263 std::unique_ptr<_ListElement> pListElement(new (std::nothrow) _ListElement(itemText));
264 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
266 pListElement->ListElementSetItemType(itemType);
267 pListElement->ListElementSetItemSelected(slected);
269 r = __listElementArray.Add(*pListElement);
270 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
272 pListElement.release();
278 _SelectBox::GetSelectedListN(void) const
280 result r = E_SUCCESS;
282 std::unique_ptr<ArrayList, AllElementsDeleter> pSelectedList(new (std::nothrow) ArrayList());
283 SysTryReturn(NID_WEB_CTRL, pSelectedList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
285 r = pSelectedList->Construct();
286 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
288 for (int index = 0; index < __pListView->GetItemCount(); index++)
290 if (__pListView->IsItemChecked(index))
292 std::unique_ptr<Integer> pIdxResult(new (std::nothrow) Integer(index));
293 SysTryReturn(NID_WEB_CTRL, pIdxResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
295 r = pSelectedList->Add(*pIdxResult);
296 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
298 pIdxResult.release();
302 return pSelectedList.release();
306 //IListViewItemProvider
308 _SelectBox::CreateItem(int index, int itemWidth)
310 result r = E_SUCCESS;
311 int listItemXPos = 0;
312 int listItemYPos = 0;
313 int checkBoxWidth = 0;
314 int listItemHeight = 0;
315 int popupBetweenListNBtnGap = 0;
317 ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
318 ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
320 _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
321 SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
323 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
324 GET_SHAPE_CONFIG(LISTVIEW::ITEM_ELEMENT_LEFT_MARGIN, __orientation, listItemXPos);
325 GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
326 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
328 std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
329 SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
331 int itemType = pListElement->ListElementGetItemType();
332 Rectangle popupRect = this->GetClientAreaBounds();
334 int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
338 case LIST_ITEM_TYPE_NORMAL:
340 case LIST_ITEM_TYPE_SUB:
341 r = pItem->Construct(Dimension(itemWidth, listItemHeight), subStyle);
342 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
345 case LIST_ITEM_TYPE_GROUP:
346 SysLog(NID_WEB_CTRL, "The current value of index [%d] [%d]", index, pListElement->ListElementIsItemEnabled());
347 r = pItem->Construct(Dimension(itemWidth, listItemHeight), groupStyle);
348 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
355 String elementString = pListElement->ListElementGetString();
357 if (elementString.IsEmpty())
359 elementString.Append(L" ");
362 r = pItem->AddElement(Rectangle(listItemXPos, listItemYPos, listTxtMaxWidth, listItemHeight),
363 ID_FORMAT_STRING, elementString, true);
364 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
366 if (IsMultiSelectable())
368 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
369 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
372 r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
373 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
375 return pItem.release();
380 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
390 _SelectBox::GetItemCount(void)
392 return __listElementArray.GetCount();
397 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
403 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
405 if (__multiSelection)
407 int pos = eina_inarray_search(__pToggledArray, &index, CompareChangedItems);
409 if (pos == POSITION_INVALID)
411 eina_inarray_push(__pToggledArray, &index);
415 eina_inarray_remove(__pToggledArray, &index);
420 //In case of single selection ... update the index.
421 __SelectedIndex = index;
422 SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
425 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
428 result r = HidePopup(__SelectedIndex);
429 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
435 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
442 _SelectBox::OnActionPerformed(const Control& source, int actionId)
446 case ID_BUTTON_SELECTION:
448 ewk_view_popup_menu_multiple_select(__pWebView, __pToggledArray);
451 case ID_BUTTON_CANCEL:
453 if (__multiSelection)
455 ewk_view_popup_menu_multiple_select(__pWebView, null);
459 ewk_view_popup_menu_select(__pWebView, __prevIndex);
464 SysAssertf(false, "Invalid Action ID");
467 result r = HidePopup(__SelectedIndex);
468 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
472 _SelectBox::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
478 _SelectBox::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
480 if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
482 if (__multiSelection)
484 ewk_view_popup_menu_multiple_select(__pWebView, null);
488 ewk_view_popup_menu_select(__pWebView, __prevIndex);
490 result r = HidePopup(__SelectedIndex);
493 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
501 _SelectBox::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
507 _SelectBox::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
513 _SelectBox::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
519 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
521 SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
525 __listElementArray.RemoveAll(true);
526 if (__multiSelection)
528 eina_inarray_flush(__pToggledArray);
532 //Create list required for selectBox
533 int itemCount = eina_list_count(pItems);
534 SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
536 bool isSelected = false;
538 if (!__multiSelection)
540 __prevIndex = prevIndex;
543 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
545 Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
546 SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
548 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
550 String text(ewk_popup_menu_item_text_get(pItem));
552 if (__multiSelection)
554 isSelected = ( ewk_popup_menu_item_selected_get(pItem) == EINA_TRUE ) ? true : false;
558 isSelected = (itemIndex == prevIndex) ? true : false;
563 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
567 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
572 __pListView->UpdateList();
578 }}} // Tizen::Web::Controls