summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/android/PopupMenuAndroid.cpp
blob: b35ea0c0493c4be8e07e5f5c96e629c13775bd91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * This file is part of the popup menu implementation for <select> elements in WebCore.
 *
 * Copyright 2009, The Android Open Source Project
 * Copyright (C) 2006 Apple Computer, Inc.
 * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"
#include "PopupMenuAndroid.h"

#include "PopupMenuClient.h"
#include "SkTDArray.h"
#include "WebViewCore.h"

class PopupReply : public android::WebCoreReply {
public:
    PopupReply(const IntRect& rect, android::WebViewCore* view, PopupMenuClient* client)
        : m_rect(rect)
        , m_viewImpl(view)
        , m_popupClient(client)
    {}

    virtual ~PopupReply() {}

    virtual void replyInt(int value) {
        if (m_popupClient) {
            m_popupClient->popupDidHide();
            // -2 is a special value signaling that the popup was canceled.
            if (-2 == value)
                return;
            m_popupClient->valueChanged(value, true);
        }
        if (m_viewImpl)
            m_viewImpl->contentInvalidate(m_rect);
    }

    virtual void replyIntArray(const int* array, int count) {
        // Should never be called.
    }
private:
    IntRect m_rect;
    // Not needed if we handle ChromeClientAndroid::formStateDidChange
    android::WebViewCore* m_viewImpl;
    PopupMenuClient* m_popupClient;
};

namespace WebCore {

PopupMenuAndroid::PopupMenuAndroid(PopupMenuClient* menuList)
    : m_popupClient(menuList)
{
}

// Copied from WebViewCore.cpp.  Once we move ListBox handling to this class,
// we can remove the one in WebViewCore.cpp.
// Convert a WTF::String into an array of characters where the first
// character represents the length, for easy conversion to java.
static uint16_t* stringConverter(const WTF::String& text)
{
    size_t length = text.length();
    uint16_t* itemName = new uint16_t[length+1];
    itemName[0] = (uint16_t)length;
    uint16_t* firstChar = &(itemName[1]);
    memcpy((void*)firstChar, text.characters(), sizeof(UChar)*length);
    return itemName;
}

void PopupMenuAndroid::show(const IntRect& rect, FrameView* frameView, int)
{
    android::WebViewCore* viewImpl = android::WebViewCore::getWebViewCore(frameView);
    android::WebCoreReply* reply = new PopupReply(rect, viewImpl, m_popupClient);

    SkTDArray<const uint16_t*> names;
    // Possible values for enabledArray.  Keep in Sync with values in
    // InvokeListBox.Container in WebView.java
    enum OptionStatus {
        OPTGROUP = -1,
        OPTION_DISABLED = 0,
        OPTION_ENABLED = 1,
    };
    SkTDArray<int> enabledArray;
    SkTDArray<int> selectedArray;
    int size = m_popupClient->listSize();
    // If we use this for ListBoxes in addition to MenuLists, we will need to
    // account for 'multiple'
    bool multiple = false;
    for (int i = 0; i < size; i++) {
        *names.append() = stringConverter(m_popupClient->itemText(i));
        if (m_popupClient->itemIsSeparator(i)) {
            *enabledArray.append() = OPTION_DISABLED;
        } else if (m_popupClient->itemIsLabel(i)) {
            *enabledArray.append() = OPTGROUP;
        } else {
            // Must be an Option
            *enabledArray.append() = m_popupClient->itemIsEnabled(i)
                    ? OPTION_ENABLED : OPTION_DISABLED;
            if (multiple && m_popupClient->itemIsSelected(i))
                *selectedArray.append() = i;
        }
    }

    viewImpl->listBoxRequest(reply,
                             names.begin(),
                             size,
                             enabledArray.begin(),
                             enabledArray.count(),
                             multiple,
                             selectedArray.begin(),
                             multiple ? selectedArray.count() : m_popupClient->selectedIndex());
}

} // namespace WebCore