1/*
2 * Copyright (C) 2012 Intel Corporation
3 * Copyright (C) 2012 Samsung Electronics
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "config.h"
28#include "ewk_popup_menu.h"
29
30#include "EwkView.h"
31#include "WKAPICast.h"
32#include "WKArray.h"
33#include "WKPopupMenuListener.h"
34#include "ewk_popup_menu_item_private.h"
35#include "ewk_popup_menu_private.h"
36
37EwkPopupMenu::EwkPopupMenu(EwkView* view, WKPopupMenuListenerRef popupMenuListener, WKArrayRef items, unsigned selectedIndex)
38    : m_view(view)
39    , m_popupMenuListener(popupMenuListener)
40    , m_popupMenuItems(0)
41    , m_selectedIndex(selectedIndex)
42{
43    size_t size = WKArrayGetSize(items);
44    for (size_t i = 0; i < size; ++i) {
45        WKPopupItemRef wkItem = static_cast<WKPopupItemRef>(WKArrayGetItemAtIndex(items, i));
46        m_popupMenuItems = eina_list_append(m_popupMenuItems, EwkPopupMenuItem::create(wkItem).leakPtr());
47    }
48}
49
50EwkPopupMenu::~EwkPopupMenu()
51{
52    void* item;
53    EINA_LIST_FREE(m_popupMenuItems, item)
54        delete static_cast<EwkPopupMenuItem*>(item);
55}
56
57void EwkPopupMenu::close()
58{
59    // Setting selected item will cause the popup menu to close.
60    WKPopupMenuListenerSetSelection(m_popupMenuListener.get(), m_selectedIndex);
61}
62
63const Eina_List* EwkPopupMenu::items() const
64{
65    return m_popupMenuItems;
66}
67
68unsigned EwkPopupMenu::selectedIndex() const
69{
70    return m_selectedIndex;
71}
72
73bool EwkPopupMenu::setSelectedIndex(unsigned selectedIndex)
74{
75    if (!m_popupMenuListener)
76        return false;
77
78    if (selectedIndex >= eina_list_count(m_popupMenuItems))
79        return false;
80
81    if (m_selectedIndex == selectedIndex)
82        return true;
83
84    m_selectedIndex = selectedIndex;
85    WKPopupMenuListenerSetSelection(m_popupMenuListener.get(), selectedIndex);
86
87    return true;
88}
89
90Eina_Bool ewk_popup_menu_close(Ewk_Popup_Menu* popupMenu)
91{
92    EINA_SAFETY_ON_NULL_RETURN_VAL(popupMenu, false);
93
94    popupMenu->close();
95
96    return true;
97}
98
99Eina_Bool ewk_popup_menu_selected_index_set(Ewk_Popup_Menu* popupMenu, unsigned selectedIndex)
100{
101    EINA_SAFETY_ON_NULL_RETURN_VAL(popupMenu, false);
102
103    return popupMenu->setSelectedIndex(selectedIndex);
104}
105
106unsigned ewk_popup_menu_selected_index_get(const Ewk_Popup_Menu* popupMenu)
107{
108    EINA_SAFETY_ON_NULL_RETURN_VAL(popupMenu, 0);
109
110    return popupMenu->selectedIndex();
111}
112
113const Eina_List* ewk_popup_menu_items_get(const Ewk_Popup_Menu* popupMenu)
114{
115    EINA_SAFETY_ON_NULL_RETURN_VAL(popupMenu, 0);
116
117    return popupMenu->items();
118}
119