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
55 static const int SELECTBOX_BUTTON_WIDTH = 100;
56 template< class Type > Type
63 //Internal class for maintaining states of list elements
68 _ListElement(const String& itemText);
70 virtual ~_ListElement(void) {}
73 void ListElementSetItemEnabled(bool isEnabled);
74 void ListElementSetItemType(short itemType);
75 void ListElementSetItemSelected(bool isSelected);
78 short ListElementGetItemType(void) const { return __itemType; }
80 String ListElementGetString(void) const { return __itemStr; }
82 bool ListElementIsItemEnabled(void) const { return __isEnabled; }
84 bool ListElementIsItemSelected(void) const { return __isSelected; }
97 _ListElement::_ListElement(const String& itemText)
98 : __itemType(_SelectBox::LIST_ITEM_TYPE_NORMAL)
100 , __isSelected(false)
102 __itemStr = itemText;
107 _ListElement::ListElementSetItemEnabled(bool isEnabled)
109 __isEnabled = isEnabled;
114 _ListElement::ListElementSetItemType(short itemType)
116 __itemType = itemType;
121 _ListElement::ListElementSetItemSelected(bool isSelected)
123 __isSelected = isSelected;
127 _SelectBox::_SelectBox(void)
129 , __multiSelection(false)
130 , __SelectedIndex(-1)
132 , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
138 _SelectBox::~_SelectBox(void)
140 __listElementArray.RemoveAll(true);
145 _SelectBox::Construct(bool isMultiSelect, const String& title, int listCount, Evas_Object* pWebView)
147 result r = E_SUCCESS;
150 int listItemHeight = 0;
151 int listMaxHeihgt = 0;
152 int popupWinTopMargin = 0;
153 int popupWinBottomMargin = 0;
154 int listViewHeight = 0;
156 __pWebView = pWebView;
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);
162 __multiSelection = isMultiSelect;
164 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
165 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
167 listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + 2 * ( pPopupData->spacePad + popupWinTopMargin + popupWinBottomMargin);
169 rect.height = listMaxHeihgt;
170 rect.width = pPopupData->popupDim.width;
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));
177 if (!title.IsEmpty())
179 Popup::SetTitleText(title);
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.");
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));
189 r = __listElementArray.Construct();
190 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
192 pListView->SetItemProvider(*this);
193 pListView->AddListViewItemEventListener(*this);
194 pListView->SetTextOfEmptyList(L"");
196 r = AddControl(*pListView);
197 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
199 __pListView = pListView.release();
201 Panel* pButtonPanel = CreateAndAddPanel();
202 SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
204 _SystemResource* pSysResource = _SystemResource::GetInstance();
205 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
208 r = idList.Construct();
209 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
210 if (__multiSelection)
212 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
214 idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
217 r = titleList.Construct();
218 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
219 if (__multiSelection)
221 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
223 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
225 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
226 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
228 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
229 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
231 pLayout->SetSpacing(*pButtonPanel, pPopupData->sideMargin);
237 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
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.");
243 pListElement->ListElementSetItemType(itemType);
244 pListElement->ListElementSetItemSelected(slected);
246 r = __listElementArray.Add(*pListElement);
247 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
249 pListElement.release();
255 _SelectBox::GetSelectedListN(void) const
257 result r = E_SUCCESS;
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));
262 r = pSelectedList->Construct();
263 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
265 for (int index = 0; index < __pListView->GetItemCount(); index++)
267 if (__pListView->IsItemChecked(index))
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));
272 r = pSelectedList->Add(*pIdxResult);
273 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
275 pIdxResult.release();
279 return pSelectedList.release();
283 //IListViewItemProvider
285 _SelectBox::CreateItem(int index, int itemWidth)
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;
294 ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
295 ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
297 _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
298 SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
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);
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));
309 int itemType = pListElement->ListElementGetItemType();
310 Rectangle popupRect = this->GetClientAreaBounds();
312 int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
316 case LIST_ITEM_TYPE_NORMAL:
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));
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));
333 String elementString = pListElement->ListElementGetString();
335 if (elementString.IsEmpty())
337 elementString.Append(L" ");
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));
344 if (IsMultiSelectable())
346 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
347 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
350 r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
351 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
353 return pItem.release();
358 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
368 _SelectBox::GetItemCount(void)
370 return __listElementArray.GetCount();
375 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
381 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
383 if (__multiSelection)
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);
393 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
396 result r = HidePopup(__SelectedIndex);
397 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
402 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
409 _SelectBox::OnActionPerformed(const Control& source, int actionId)
413 case ID_BUTTON_SELECTION:
415 case ID_BUTTON_CANCEL:
419 ewk_view_popup_menu_select(__pWebView, __prevIndex);
422 result r = HidePopup(__SelectedIndex);
423 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
431 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
433 SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
437 __listElementArray.RemoveAll(true);
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));
444 __prevIndex = prevIndex;
446 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
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));
451 bool isSelected = (itemIndex == prevIndex) ? true : false;
452 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
454 String text(ewk_popup_menu_item_text_get(pItem));
458 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
462 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
467 __pListView->UpdateList();
473 }}} // Tizen::Web::Controls