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 listItemCnt, Evas_Object* pWebView)
147 result r = E_SUCCESS;
151 int listItemHeight = 0;
152 int listMaxHeihgt = 0;
153 int popupWinTopMargin = 0;
154 int popupWinBottomMargin = 0;
155 int listViewHeight = 0;
157 __pWebView = pWebView;
159 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
160 GET_SHAPE_CONFIG(POPUP::TOP_BORDER, __orientation, popupWinTopMargin);
161 GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, __orientation, popupWinBottomMargin);
163 __multiSelection = isMultiSelect;
165 _WebPopupData* pPopupData = _WebPopup::GetPopupData();
166 SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
168 listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + 2 * ( pPopupData->spacePad + popupWinTopMargin + popupWinBottomMargin);
170 rect.height = Max< int >(listMaxHeihgt, pPopupData->popupDim.height);
171 rect.width = pPopupData->popupDim.width;
175 r = _WebPopup::Construct(!title.IsEmpty(), Dimension(rect.width, rect.height));
176 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
178 if (!title.IsEmpty())
180 Popup::SetTitleText(title);
183 listViewHeight = GetClientAreaBounds().height - popupWinTopMargin - popupWinBottomMargin - pPopupData->btnDim.height;
184 std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
185 SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
187 r = pListView->Construct(Rectangle(0, popupWinTopMargin, GetClientAreaBounds().width, listViewHeight), false);
188 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
190 r = __listElementArray.Construct();
191 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
193 pListView->SetItemProvider(*this);
194 pListView->AddListViewItemEventListener(*this);
195 pListView->SetTextOfEmptyList(L"");
197 r = AddControl(*pListView);
198 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
200 __pListView = pListView.release();
202 Panel* pButtonPanel = CreateAndAddPanel();
203 SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
205 _SystemResource* pSysResource = _SystemResource::GetInstance();
206 SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
209 r = idList.Construct();
210 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
211 if (__multiSelection)
213 idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
215 idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
218 r = titleList.Construct();
219 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
220 if (__multiSelection)
222 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
224 titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
226 r = CreateAndAddButtons(idList, titleList, pButtonPanel);
227 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
229 std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
230 SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
232 pLayout->SetSpacing(*pButtonPanel, pPopupData->sideMargin);
238 _SelectBox::AddListItem(const String& itemText, int itemType, bool slected)
240 result r = E_SUCCESS;
241 std::unique_ptr<_ListElement> pListElement(new (std::nothrow) _ListElement(itemText));
242 SysTryReturnResult(NID_WEB_CTRL, pListElement.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
244 pListElement->ListElementSetItemType(itemType);
245 pListElement->ListElementSetItemSelected(slected);
247 r = __listElementArray.Add(*pListElement);
248 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
250 pListElement.release();
256 _SelectBox::GetSelectedListN(void) const
258 result r = E_SUCCESS;
260 std::unique_ptr<ArrayList, AllElementsDeleter> pSelectedList(new (std::nothrow) ArrayList());
261 SysTryReturn(NID_WEB_CTRL, pSelectedList.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
263 r = pSelectedList->Construct();
264 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
266 for (int index = 0; index < __pListView->GetItemCount(); index++)
268 if (__pListView->IsItemChecked(index))
270 std::unique_ptr<Integer> pIdxResult(new (std::nothrow) Integer(index));
271 SysTryReturn(NID_WEB_CTRL, pIdxResult.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
273 r = pSelectedList->Add(*pIdxResult);
274 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Memory Allocation failed.",GetErrorMessage(r));
276 pIdxResult.release();
280 return pSelectedList.release();
284 //IListViewItemProvider
286 _SelectBox::CreateItem(int index, int itemWidth)
288 result r = E_SUCCESS;
289 int listItemXPos = 0;
290 int listItemYPos = 0;
291 int checkBoxWidth = 0;
292 int listItemTxtHeight = 0;
293 int listItemHeight = 0;
294 int popupBetweenListNBtnGap = 0;
296 ListAnnexStyle subStyle = (__multiSelection) ? LIST_ANNEX_STYLE_MARK : LIST_ANNEX_STYLE_NORMAL;
297 ListAnnexStyle groupStyle = LIST_ANNEX_STYLE_NORMAL;
299 _ListElement* pListElement = static_cast< _ListElement* >(__listElementArray.GetAt(index));
300 SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
302 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
303 GET_SHAPE_CONFIG(LIST::LIST_ITEM_LEFT_MARGIN, __orientation, listItemXPos);
304 GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
305 GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
306 // GET_SHAPE_CONFIG(LIST::GROUPITEM_DEFAULT_FONT_SIZE, __orientation, listItemTxtHeight);
308 std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
309 SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
311 int itemType = pListElement->ListElementGetItemType();
312 Rectangle popupRect = this->GetClientAreaBounds();
314 int listTxtMaxWidth = popupRect.width - listItemXPos - checkBoxWidth - popupBetweenListNBtnGap;
318 case LIST_ITEM_TYPE_NORMAL:
320 case LIST_ITEM_TYPE_SUB:
321 r = pItem->Construct(Dimension(itemWidth, listItemHeight), subStyle);
322 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
325 case LIST_ITEM_TYPE_GROUP:
326 SysLog(NID_WEB_CTRL, "The current value of index [%d] [%d]", index, pListElement->ListElementIsItemEnabled());
327 r = pItem->Construct(Dimension(itemWidth, listItemHeight), groupStyle);
328 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
335 String elementString = pListElement->ListElementGetString();
337 if (elementString.IsEmpty())
339 elementString.Append(L" ");
342 r = pItem->AddElement(Rectangle(listItemXPos, listItemYPos, listTxtMaxWidth, listItemHeight),
343 ID_FORMAT_STRING, elementString, true);
344 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
346 if (IsMultiSelectable())
348 r = __pListView->SetItemChecked(index, pListElement->ListElementIsItemSelected());
349 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
352 r = __pListView->SetItemEnabled(index, !(itemType == LIST_ITEM_TYPE_GROUP));
353 SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
355 return pItem.release();
360 _SelectBox::DeleteItem(int index, ListItemBase* pItem, int itemWidth)
370 _SelectBox::GetItemCount(void)
372 return __listElementArray.GetCount();
377 _SelectBox::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
383 _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elementId, ListItemStatus status)
385 if (__multiSelection)
390 //In case of single selection ... update the index.
391 __SelectedIndex = index;
392 SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
395 ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
398 result r = HidePopup(__SelectedIndex);
399 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
404 _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
411 _SelectBox::OnActionPerformed(const Control& source, int actionId)
415 case ID_BUTTON_SELECTION:
417 case ID_BUTTON_CANCEL:
421 ewk_view_popup_menu_select(__pWebView, __prevIndex);
424 result r = HidePopup(__SelectedIndex);
425 SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
433 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
435 SysTryReturnResult(NID_WEB_CTRL, __pListView, E_INVALID_STATE, "List View is in an invalid state");
439 __listElementArray.RemoveAll(true);
442 //Create list required for selectBox
443 int itemCount = eina_list_count(pItems);
444 SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
446 __prevIndex = prevIndex;
448 for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
450 Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
451 SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s] A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
453 bool isSelected = (itemIndex == prevIndex) ? true : false;
454 if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
456 String text(ewk_popup_menu_item_text_get(pItem));
460 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);
464 AddListItem(text, _SelectBox::LIST_ITEM_TYPE_NORMAL, isSelected);
469 __pListView->UpdateList();
475 }}} // Tizen::Web::Controls