• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of the popup menu implementation for <select> elements in WebCore.
3  *
4  * Copyright (C) 2008 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "config.h"
24 #include "PopupMenu.h"
25 
26 #include "Frame.h"
27 #include "FrameView.h"
28 #include "NotImplemented.h"
29 #include "PopupMenuClient.h"
30 #include "PlatformString.h"
31 
32 #include <wx/defs.h>
33 #if __WXMSW__
34 #include <wx/msw/winundef.h>
35 #endif
36 #include <wx/event.h>
37 #include <wx/menu.h>
38 
39 static int s_menuStartId = wxNewId();
40 
41 
42 
43 namespace WebCore {
44 
PopupMenu(PopupMenuClient * client)45 PopupMenu::PopupMenu(PopupMenuClient* client)
46     : m_popupClient(client)
47     , m_menu(NULL)
48 {
49 }
50 
~PopupMenu()51 PopupMenu::~PopupMenu()
52 {
53     delete m_menu;
54 }
55 
show(const IntRect & r,FrameView * v,int index)56 void PopupMenu::show(const IntRect& r, FrameView* v, int index)
57 {
58     // just delete and recreate
59     delete m_menu;
60     ASSERT(client());
61 
62     wxWindow* nativeWin = v->platformWidget();
63 
64     if (nativeWin) {
65         // construct the menu
66         m_menu = new wxMenu();
67         int size = client()->listSize();
68         for (int i = 0; i < size; i++) {
69             int id = s_menuStartId + i;
70 
71             if (client()->itemIsSeparator(i)) {
72                 m_menu->AppendSeparator();
73             }
74             else {
75                 // FIXME: appending a menu item with an empty label asserts in
76                 // wx. This needs to be fixed at wx level so that we can have
77                 // the concept of "no selection" in choice boxes, etc.
78                 if (!client()->itemText(i).isEmpty())
79                     m_menu->Append(s_menuStartId + i, client()->itemText(i));
80             }
81         }
82         nativeWin->Connect(s_menuStartId, s_menuStartId + (size-1), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(PopupMenu::OnMenuItemSelected), NULL, this);
83         nativeWin->PopupMenu(m_menu, r.x() - v->scrollX(), r.y() - v->scrollY());
84         nativeWin->Disconnect(s_menuStartId, s_menuStartId + (size-1), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(PopupMenu::OnMenuItemSelected), NULL, this);
85     }
86 }
87 
OnMenuItemSelected(wxCommandEvent & event)88 void PopupMenu::OnMenuItemSelected(wxCommandEvent& event)
89 {
90     if (client()) {
91         client()->valueChanged(event.GetId() - s_menuStartId);
92         client()->hidePopup();
93     }
94     // TODO: Do we need to call Disconnect here? Do we have a ref to the native window still?
95 }
96 
hide()97 void PopupMenu::hide()
98 {
99     // we don't need to do anything here, the native control only exists during the time
100     // show is called
101 }
102 
updateFromElement()103 void PopupMenu::updateFromElement()
104 {
105     client()->setTextFromItem(m_popupClient->selectedIndex());
106 }
107 
itemWritingDirectionIsNatural()108 bool PopupMenu::itemWritingDirectionIsNatural()
109 {
110     return false;
111 }
112 
113 }
114