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 <FCntDownloadRequest.h>
30 #include <FGrpDimension.h>
31 #include <FGrpRectangle.h>
32 #include <FUiCtrlButton.h>
33 #include <FUiCtrlCustomItem.h>
34 #include <FUiCtrlGroupedList.h>
35 #include <FUiCtrlListItemBase.h>
36 #include <FUiCtrlListView.h>
37 #include <FUiCtrlPanel.h>
38 #include <FUiIActionEventListener.h>
39 #include <FUiKeyEventInfo.h>
40 #include <FUiVerticalBoxLayout.h>
41 #include <FCnt_DownloadManagerImpl.h>
42 #include <FSys_SystemResource.h>
43 #include <FUi_ResourceManager.h>
44 #include "FWebCtrl_SelectBox.h"
47 using namespace Tizen::Base;
48 using namespace Tizen::Base::Collection;
49 using namespace Tizen::Content;
50 using namespace Tizen::Graphics;
51 using namespace Tizen::System;
52 using namespace Tizen::Ui;
53 using namespace Tizen::Ui::Controls;
56 namespace Tizen { namespace Web { namespace Controls
60 static const int POSITION_INVALID = -1;
63 template< class Type > Type
70 int CompareChangedItems(const void* pData1, const void* pData2)
72 SysAssertf(pData1 && pData2, "input parameters are null");
74 const int* pLeft = static_cast<const int*>(pData1);
75 const int* pRight = static_cast<const int*>(pData2);
77 return (*pLeft - *pRight);
81 //Internal class for maintaining states of list elements
86 _ListElement(const String& itemText);
88 virtual ~_ListElement(void) {}
91 void ListElementSetItemEnabled(bool isEnabled);
92 void ListElementSetItemType(short itemType);
93 void ListElementSetItemSelected(bool isSelected);
96 short ListElementGetItemType(void) const { return __itemType; }
98 String ListElementGetString(void) const { return __itemStr; }
100 bool ListElementIsItemEnabled(void) const { return __isEnabled; }
102 bool ListElementIsItemSelected(void) const { return __isSelected; }
115 _ListElement::_ListElement(const String& itemText)
116 : __itemType(_SelectBox::LIST_ITEM_TYPE_NORMAL)
118 , __isSelected(false)
120 __itemStr = itemText;
125 _ListElement::ListElementSetItemEnabled(bool isEnabled)
127 __isEnabled = isEnabled;
132 _ListElement::ListElementSetItemType(short itemType)
134 __itemType = itemType;
139 _ListElement::ListElementSetItemSelected(bool isSelected)
141 __isSelected = isSelected;
145 _SelectBox::_SelectBox(void)
147 , __multiSelection(false)
148 , __SelectedIndex(-1)
150 , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
152 , __pToggledArray(null)
158 _SelectBox::~_SelectBox(void)
160 if (__multiSelection)
162 eina_inarray_flush(__pToggledArray);
163 delete __pToggledArray;
166 __listElementArray.RemoveAll(true);
171 _SelectBox::Construct(bool isMultiSelect, const String& title, int listCount, Evas_Object* pWebView, const String& url)
173 result r = E_SUCCESS;
176 int listItemHeight = 0;
177 int listMaxHeihgt = 0;
178 int listViewHeight = 0;
180 __pWebView = pWebView;
181 __url = const_cast< String& >(url);
183 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
185 __multiSelection = isMultiSelect;
187 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
188 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
190 listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + pPopupData->spacePad;
192 rect.height = listMaxHeihgt;
193 rect.width = pPopupData->popupDim.width;
197 r = _WebPopup::Construct(!title.IsEmpty(), Dimension(rect.width, rect.height));
198 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
200 if (!title.IsEmpty())
202 Popup::SetTitleText(title);
205 listViewHeight = listCount * listItemHeight;
206 std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
207 SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
209 r = pListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, listViewHeight), true, SCROLL_STYLE_FADE_OUT);
210 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
212 r = __listElementArray.Construct();
213 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
215 pListView->SetItemProvider(*this);
216 pListView->AddListViewItemEventListener(*this);
217 pListView->SetTextOfEmptyList(L"");
219 r = AddControl(*pListView);
220 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
222 __pListView = pListView.release();
224 Panel* pButtonPanel = CreateAndAddPanel();
225 SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
227 _SystemResource* pSysResource = _SystemResource::GetInstance();
228 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
231 r = idList.Construct();
232 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
234 idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
235 if (__multiSelection)
237 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
241 r = titleList.Construct();
242 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
244 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
245 if (__multiSelection)
247 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
249 __pToggledArray = eina_inarray_new(sizeof(int), 0);
250 SysTryReturnResult(NID_WEB_CTRL, __pToggledArray, E_OUT_OF_MEMORY, "Memory Allocation failed.");
253 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
254 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
256 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
257 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
259 pLayout->SetSpacing(*pButtonPanel, pPopupData->spacePad);
261 SetPropagatedKeyEventListener(this);
267 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
269 result r = E_SUCCESS;
270 std::unique_ptr<_ListElement> pListElement(new (std::nothrow) _ListElement(itemText));
271 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
273 pListElement->ListElementSetItemType(itemType);
274 pListElement->ListElementSetItemSelected(slected);
276 r = __listElementArray.Add(*pListElement);
277 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
279 pListElement.release();
285 _SelectBox::GetSelectedListN(void) const
287 result r = E_SUCCESS;
289 std::unique_ptr<ArrayList, AllElementsDeleter> pSelectedList(new (std::nothrow) ArrayList());
290 SysTryReturn(NID_WEB_CTRL, pSelectedList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
292 r = pSelectedList->Construct();
293 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
295 for (int index = 0; index < __pListView->GetItemCount(); index++)
297 if (__pListView->IsItemChecked(index))
299 std::unique_ptr<Integer> pIdxResult(new (std::nothrow) Integer(index));
300 SysTryReturn(NID_WEB_CTRL, pIdxResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
302 r = pSelectedList->Add(*pIdxResult);
303 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
305 pIdxResult.release();
309 return pSelectedList.release();
313 //IListViewItemProvider
315 _SelectBox::CreateItem(int index, int itemWidth)
317 result r = E_SUCCESS;
318 int listItemXPos = 0;
319 int listItemYPos = 0;
320 int checkBoxWidth = 0;
321 int listItemHeight = 0;
322 int popupBetweenListNBtnGap = 0;
324 ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
325 ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
327 _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
328 SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
330 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
331 GET_SHAPE_CONFIG(LISTVIEW::ITEM_ELEMENT_LEFT_MARGIN, __orientation, listItemXPos);
332 GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
333 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
335 std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
336 SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
338 int itemType = pListElement->ListElementGetItemType();
339 Rectangle popupRect = this->GetClientAreaBounds();
341 int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
345 case LIST_ITEM_TYPE_NORMAL:
347 case LIST_ITEM_TYPE_SUB:
348 r = pItem->Construct(Dimension(itemWidth, listItemHeight), subStyle);
349 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
352 case LIST_ITEM_TYPE_GROUP:
353 SysLog(NID_WEB_CTRL, "The current value of index [%d] [%d]", index, pListElement->ListElementIsItemEnabled());
354 r = pItem->Construct(Dimension(itemWidth, listItemHeight), groupStyle);
355 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
362 String elementString = pListElement->ListElementGetString();
364 if (elementString.IsEmpty())
366 elementString.Append(L" ");
369 r = pItem->AddElement(Rectangle(listItemXPos, listItemYPos, listTxtMaxWidth, listItemHeight),
370 ID_FORMAT_STRING, elementString, true);
371 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
373 if (IsMultiSelectable())
375 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
376 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
379 r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
380 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
382 return pItem.release();
387 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
397 _SelectBox::GetItemCount(void)
399 return __listElementArray.GetCount();
404 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
410 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
412 if (__multiSelection)
414 int pos = eina_inarray_search(__pToggledArray, &index, CompareChangedItems);
416 if (pos == POSITION_INVALID)
418 eina_inarray_push(__pToggledArray, &index);
422 eina_inarray_remove(__pToggledArray, &index);
427 //In case of single selection ... update the index.
428 if (__url.GetLength() == 0)
430 __SelectedIndex = index;
431 SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
434 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
439 result r = E_SUCCESS;
442 DownloadRequest request(__url);
444 _DownloadManagerImpl* pManagerImpl = _DownloadManagerImpl::GetInstance();
445 SysTryReturnVoidResult(NID_WEB_CTRL, pManagerImpl, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
447 r = pManagerImpl->Start(request, reqId);
448 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
451 result r = HidePopup();
452 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
458 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
465 _SelectBox::OnActionPerformed(const Control& source, int actionId)
467 result r = HidePopup();
468 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
470 if (__url.GetLength() == 0)
474 case ID_BUTTON_SELECTION:
476 ewk_view_popup_menu_multiple_select(__pWebView, __pToggledArray);
479 case ID_BUTTON_CANCEL:
481 if (__multiSelection)
483 ewk_view_popup_menu_multiple_select(__pWebView, null);
487 ewk_view_popup_menu_select(__pWebView, __prevIndex);
492 SysAssertf(false, "Invalid Action ID");
498 _SelectBox::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
504 _SelectBox::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
506 if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
508 result r = HidePopup();
511 SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
514 if (__url.GetLength() == 0)
516 if (__multiSelection)
518 ewk_view_popup_menu_multiple_select(__pWebView, null);
522 ewk_view_popup_menu_select(__pWebView, __prevIndex);
531 _SelectBox::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
537 _SelectBox::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
543 _SelectBox::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
549 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
551 SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
555 __listElementArray.RemoveAll(true);
556 if (__multiSelection)
558 eina_inarray_flush(__pToggledArray);
562 //Create list required for selectBox
563 int itemCount = eina_list_count(pItems);
564 SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
566 bool isSelected = false;
568 if (!__multiSelection)
570 __prevIndex = prevIndex;
573 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
575 Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
576 SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
578 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
580 String text(ewk_popup_menu_item_text_get(pItem));
582 if (__multiSelection)
584 isSelected = ( ewk_popup_menu_item_selected_get(pItem) == EINA_TRUE ) ? true : false;
588 isSelected = (itemIndex == prevIndex) ? true : false;
593 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
597 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
602 __pListView->UpdateList();
608 }}} // Tizen::Web::Controls