Tizen 2.2.1
[framework/osp/web.git] / src / controls / FWebCtrl_SelectBox.cpp
index 62f898e..ac80e60 100755 (executable)
@@ -26,6 +26,7 @@
 #include <unique_ptr.h>
 #include <FBaseColAllElementsDeleter.h>
 #include <FBaseSysLog.h>
+#include <FCntDownloadRequest.h>
 #include <FGrpDimension.h>
 #include <FGrpRectangle.h>
 #include <FUiCtrlButton.h>
 #include <FUiCtrlListView.h>
 #include <FUiCtrlPanel.h>
 #include <FUiIActionEventListener.h>
+#include <FUiKeyEventInfo.h>
 #include <FUiVerticalBoxLayout.h>
+#include <FCnt_DownloadManagerImpl.h>
 #include <FSys_SystemResource.h>
 #include <FUi_ResourceManager.h>
 #include "FWebCtrl_SelectBox.h"
+#include "FWebCtrl_WebImpl.h"
 
 
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
+using namespace Tizen::Content;
 using namespace Tizen::Graphics;
 using namespace Tizen::System;
 using namespace Tizen::Ui;
@@ -52,7 +57,10 @@ using namespace Tizen::Ui::Controls;
 namespace Tizen { namespace Web { namespace Controls
 {
 
-static const int SELECTBOX_BUTTON_WIDTH = 100;
+
+static const int POSITION_INVALID = -1;
+
+
 template< class Type > Type
 Max(Type a, Type b)
 {
@@ -60,6 +68,17 @@ Max(Type a, Type b)
 }
 
 
+int CompareChangedItems(const void* pData1, const void* pData2)
+{
+       SysAssertf(pData1 && pData2, "input parameters are null");
+
+    const int* pLeft = static_cast<const int*>(pData1);
+    const int* pRight = static_cast<const int*>(pData2);
+
+    return (*pLeft - *pRight);
+}
+
+
 //Internal class for maintaining states of list elements
 class _ListElement
        : public Object
@@ -131,43 +150,49 @@ _SelectBox::_SelectBox(void)
        , __prevIndex(-1)
        , __orientation(_CONTROL_ORIENTATION_PORTRAIT)
        , __pWebView(null)
+       , __pToggledArray(null)
+       , __url(L"")
+       , __pImpl(null)
 {
 }
 
 
 _SelectBox::~_SelectBox(void)
 {
+       if (__multiSelection)
+       {
+               eina_inarray_flush(__pToggledArray);
+               delete __pToggledArray;
+       }
+
        __listElementArray.RemoveAll(true);
 }
 
 
 result
-_SelectBox::Construct(bool isMultiSelect, const String& title, int listItemCnt, Evas_Object* pWebView)
+_SelectBox::Construct(Tizen::Web::Controls::_WebImpl* pImpl, bool isMultiSelect, const String& title, int listCount, Evas_Object* pWebView, const String& url)
 {
        result r = E_SUCCESS;
        Rectangle rect;
 
-       int listCount = 4;
        int listItemHeight = 0;
        int listMaxHeihgt = 0;
-       int popupWinTopMargin = 0;
-       int popupWinBottomMargin = 0;
        int listViewHeight = 0;
 
        __pWebView = pWebView;
+       __url = const_cast< String& >(url);
+       __pImpl = pImpl;
 
        GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
-       GET_SHAPE_CONFIG(POPUP::TOP_BORDER, __orientation, popupWinTopMargin);
-       GET_SHAPE_CONFIG(POPUP::BOTTOM_BORDER, __orientation, popupWinBottomMargin);
 
        __multiSelection = isMultiSelect;
 
        _WebPopupData* pPopupData = _WebPopup::GetPopupData();
        SysTryReturn(NID_WEB_CTRL, pPopupData, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
 
-       listMaxHeihgt = listCount * listItemHeight + pPopupData->btnDim.height + 2 * ( pPopupData->spacePad + popupWinTopMargin + popupWinBottomMargin);
+       listMaxHeihgt = listCount * listItemHeight;
 
-       rect.height = Max< int >(listMaxHeihgt, pPopupData->popupDim.height);
+       rect.height = listMaxHeihgt;
        rect.width = pPopupData->popupDim.width;
        rect.x = 0;
        rect.y = 0;
@@ -180,11 +205,11 @@ _SelectBox::Construct(bool isMultiSelect, const String& title, int listItemCnt,
                Popup::SetTitleText(title);
        }
 
-       listViewHeight = GetClientAreaBounds().height - popupWinTopMargin - popupWinBottomMargin - pPopupData->btnDim.height;
+       listViewHeight = listCount * listItemHeight;
        std::unique_ptr<ListView> pListView(new (std::nothrow) ListView());
        SysTryReturnResult(NID_WEB_CTRL, pListView.get(), E_OUT_OF_MEMORY, "Memory Allocation failed.");
 
-       r = pListView->Construct(Rectangle(0, popupWinTopMargin, GetClientAreaBounds().width, listViewHeight), false);
+       r = pListView->Construct(Rectangle(0, 0, GetClientAreaBounds().width, listViewHeight), true, SCROLL_STYLE_FADE_OUT);
        SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
        r = __listElementArray.Construct();
@@ -199,38 +224,35 @@ _SelectBox::Construct(bool isMultiSelect, const String& title, int listItemCnt,
 
        __pListView = pListView.release();
 
-       Panel* pButtonPanel = CreateAndAddPanel();
-       SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
-
-       _SystemResource* pSysResource = _SystemResource::GetInstance();
-       SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
-
-       ArrayList idList;
-       r = idList.Construct();
-       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
        if (__multiSelection)
        {
+               Panel* pButtonPanel = CreateAndAddPanel();
+               SysTryReturn(NID_WEB_CTRL, pButtonPanel, r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
+
+               _SystemResource* pSysResource = _SystemResource::GetInstance();
+               SysAssertf(pSysResource != null, "Failed to get _SystemResource instance");
+
+               ArrayList idList;
+               r = idList.Construct();
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+
                idList.Add(*(new Integer(ID_BUTTON_SELECTION)));
-       }
-       idList.Add(*(new Integer(ID_BUTTON_CANCEL)));
 
-       ArrayList titleList;
-       r = titleList.Construct();
-       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
-       if (__multiSelection)
-       {
-               titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_BODY_DONE"))));
-       }
-       titleList.Add(*(new String(pSysResource->GetString("sys_string", "IDS_COM_POP_CANCEL"))));
+               ArrayList titleList;
+               r = titleList.Construct();
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
 
-       r = CreateAndAddButtons(idList, titleList, pButtonPanel);
-       SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+               titleList.Add(*(new String(pSysResource->GetString(_RESOURCE_DOMAIN_ID_OSP, "IDS_TPLATFORM_BUTTON_OK"))));
 
-       std::unique_ptr<VerticalBoxLayout> pLayout(dynamic_cast< VerticalBoxLayout* >(GetLayoutN()));
-       SysTryReturn(NID_WEB_CTRL, pLayout.get(), r = GetLastResult(), r, "[%s] Propagating.", GetErrorMessage(r));
+               __pToggledArray = eina_inarray_new(sizeof(int), 0);
+               SysTryReturnResult(NID_WEB_CTRL, __pToggledArray, E_OUT_OF_MEMORY, "Memory Allocation failed.");
 
-       pLayout->SetSpacing(*pButtonPanel,  pPopupData->sideMargin);
+               r = CreateAndAddButtons(idList, titleList, pButtonPanel);
+               SysTryReturn(NID_WEB_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
+       }
 
+       SetPropagatedKeyEventListener(this);
+       
        return r;
 }
 
@@ -289,7 +311,6 @@ _SelectBox::CreateItem(int index, int itemWidth)
        int listItemXPos = 0;
        int listItemYPos = 0;
        int checkBoxWidth = 0;
-       int listItemTxtHeight = 0;
        int listItemHeight = 0;
        int popupBetweenListNBtnGap = 0;
 
@@ -300,10 +321,9 @@ _SelectBox::CreateItem(int index, int itemWidth)
        SysTryReturn(NID_WEB_CTRL, pListElement, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        GET_SHAPE_CONFIG(CONTEXTMENU::LIST_ITEM_HEIGHT, __orientation, listItemHeight);
-       GET_SHAPE_CONFIG(LIST::LIST_ITEM_LEFT_MARGIN, __orientation, listItemXPos);
+       GET_SHAPE_CONFIG(LISTVIEW::ITEM_ELEMENT_LEFT_MARGIN, __orientation, listItemXPos);
        GET_SHAPE_CONFIG(LIST::LIST_CHECK_ITEM_WIDTH, __orientation, checkBoxWidth);
        GET_SHAPE_CONFIG(MESSAGEBOX::BUTTON_TOP_MARGIN, __orientation, popupBetweenListNBtnGap);
-//     GET_SHAPE_CONFIG(LIST::GROUPITEM_DEFAULT_FONT_SIZE, __orientation, listItemTxtHeight);
 
        std::unique_ptr<CustomItem> pItem(new (std::nothrow) CustomItem());
        SysTryReturn(NID_WEB_CTRL, pItem.get(), null, E_OUT_OF_MEMORY, "[%s] Memory Allocation failed.",GetErrorMessage(E_OUT_OF_MEMORY));
@@ -384,19 +404,48 @@ _SelectBox::OnListViewItemStateChanged(ListView& listView, int index, int elemen
 {
        if (__multiSelection)
        {
-               return;
-       }
+               int pos = eina_inarray_search(__pToggledArray, &index, CompareChangedItems);
 
-       //In case of single selection ... update the index.
-       __SelectedIndex = index;
-       SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
-       if (__pWebView)
-       {
-               ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
+               if (pos == POSITION_INVALID)
+               {
+                       eina_inarray_push(__pToggledArray, &index);
+               }
+               else
+               {
+                       eina_inarray_remove(__pToggledArray, &index);
+               }
        }
+       else
+       {
+               //In case of single selection ... update the index.
+               if (__url.GetLength() == 0)
+               {
+                       __SelectedIndex = index;
+                       SysLog(NID_WEB_CTRL,"The current value of Update Selected index is %d",index);
+                       if (__pWebView)
+                       {
+                               ewk_view_popup_menu_select(__pWebView, __SelectedIndex);
+                       }
+               }
+               else
+               {
+                       result r = E_SUCCESS;
 
-       result r = HidePopup(__SelectedIndex);
-       SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+                       RequestId reqId = 0;
+                       DownloadRequest request(__url);
+
+                       _DownloadManagerImpl* pManagerImpl = _DownloadManagerImpl::GetInstance();
+                       SysTryReturnVoidResult(NID_WEB_CTRL, pManagerImpl, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
+
+                       r = pManagerImpl->Start(request, reqId);
+                       SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+               }
+
+               result r = HidePopup();
+               SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+               
+               __pImpl->SendUserEvent(ID_USER_SCRIPT_SELECTBOX_CLOSE, null);
+       }
 }
 
 
@@ -410,24 +459,90 @@ _SelectBox::OnListViewItemSwept(ListView& listView, int index, SweepDirection di
 void
 _SelectBox::OnActionPerformed(const Control& source, int actionId)
 {
-       switch (actionId)
+       result r = HidePopup();
+       SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
+
+       if (__url.GetLength() == 0)
        {
-       case ID_BUTTON_SELECTION:
-       //fall through
-       case ID_BUTTON_CANCEL:
+               switch (actionId)
+               {
+               case ID_BUTTON_SELECTION:
+               {
+                       ewk_view_popup_menu_multiple_select(__pWebView, __pToggledArray);
+                       break;
+               }
+               case ID_BUTTON_CANCEL:
+               {
+                       if (__multiSelection)
+                       {
+                               ewk_view_popup_menu_multiple_select(__pWebView, null);
+                       }
+                       else
+                       {
+                               ewk_view_popup_menu_select(__pWebView, __prevIndex);
+                       }
+                       break;
+               }
+               default:
+                       SysAssertf(false, "Invalid Action ID");
+               }
+       }
+
+       __pImpl->SendUserEvent(ID_USER_SCRIPT_SELECTBOX_CLOSE, null);
+}
+
+bool
+_SelectBox::OnKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+bool
+_SelectBox::OnKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       if ((keyEventInfo.GetKeyCode() == KEY_ESC || keyEventInfo.GetKeyCode() == KEY_BACK) && source.GetShowState() == true)
        {
-               if (__pWebView)
+               result r = HidePopup();
+               if (IsFailed(r))
                {
-                       ewk_view_popup_menu_select(__pWebView, __prevIndex);
+                       SysLogException(NID_WEB_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
                }
 
-               result r = HidePopup(__SelectedIndex);
-               SysTryReturnVoidResult(NID_WEB_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
-               break;
-       }
+               if (__url.GetLength() == 0)
+               {
+                       if (__multiSelection)
+                       {
+                               ewk_view_popup_menu_multiple_select(__pWebView, null);
+                       }
+                       else
+                       {
+                               ewk_view_popup_menu_select(__pWebView, __prevIndex);
+                       }
+               }
+
+               __pImpl->SendUserEvent(ID_USER_SCRIPT_SELECTBOX_CLOSE, null);
        }
+
+       return false;
 }
 
+bool
+_SelectBox::OnPreviewKeyPressed(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+bool
+_SelectBox::OnPreviewKeyReleased(Control& source, const KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
+
+bool
+_SelectBox::TranslateKeyEventInfo(Control& source, KeyEventInfo& keyEventInfo)
+{
+       return false;
+}
 
 result
 _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, bool isGroupdList)
@@ -437,24 +552,41 @@ _SelectBox::UpdateList(Eina_List* pItems, int prevIndex, bool clearPrevList, boo
        if (clearPrevList)
        {
                __listElementArray.RemoveAll(true);
+               if (__multiSelection)
+               {
+                       eina_inarray_flush(__pToggledArray);
+               }
        }
 
        //Create list required for selectBox
        int itemCount = eina_list_count(pItems);
        SysTryReturn(NID_WEB_CTRL, itemCount > 0, E_SYSTEM, E_SYSTEM, "[%s]  A system error has been occurred. ItemCount is invalid.", GetErrorMessage(E_SYSTEM));
 
-       __prevIndex = prevIndex;
+       bool isSelected = false;
+
+       if (!__multiSelection)
+       {
+               __prevIndex = prevIndex;
+       }
 
        for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
        {
                Ewk_Popup_Menu_Item* pItem = static_cast<Ewk_Popup_Menu_Item*>(eina_list_nth(pItems, itemIndex));
                SysTryReturn(NID_WEB_CTRL, pItem, E_SYSTEM, E_SYSTEM, "[%s]  A system error has been occurred. Failed to get item.", GetErrorMessage(E_SYSTEM));
 
-               bool isSelected = (itemIndex == prevIndex) ? true : false;
                if (ewk_popup_menu_item_type_get(pItem) == EWK_POPUP_MENU_ITEM)
                {
                        String text(ewk_popup_menu_item_text_get(pItem));
 
+                       if (__multiSelection)
+                       {
+                               isSelected = ( ewk_popup_menu_item_selected_get(pItem) == EINA_TRUE ) ? true : false;
+                       }
+                       else
+                       {
+                               isSelected = (itemIndex == prevIndex) ? true : false;
+                       }
+
                        if (isGroupdList)
                        {
                                AddListItem(text, _SelectBox::LIST_ITEM_TYPE_GROUP, isSelected);