OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 * Copyright (C) 2010 Apple Inc. All rights reserved. | 4 * Copyright (C) 2010 Apple Inc. All rights reserved. |
5 * | 5 * |
6 * This library is free software; you can redistribute it and/or | 6 * This library is free software; you can redistribute it and/or |
7 * modify it under the terms of the GNU Library General Public | 7 * modify it under the terms of the GNU Library General Public |
8 * License as published by the Free Software Foundation; either | 8 * License as published by the Free Software Foundation; either |
9 * version 2 of the License, or (at your option) any later version. | 9 * version 2 of the License, or (at your option) any later version. |
10 * | 10 * |
11 * This library is distributed in the hope that it will be useful, | 11 * This library is distributed in the hope that it will be useful, |
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 * Library General Public License for more details. | 14 * Library General Public License for more details. |
15 * | 15 * |
16 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 * Boston, MA 02110-1301, USA. | 19 * Boston, MA 02110-1301, USA. |
20 * | 20 * |
21 */ | 21 */ |
22 | 22 |
23 #include "config.h" | 23 #include "config.h" |
| 24 |
24 #include "core/html/HTMLMenuElement.h" | 25 #include "core/html/HTMLMenuElement.h" |
25 | 26 |
26 #include "HTMLNames.h" | 27 #include "core/dom/ElementTraversal.h" |
| 28 #include "core/events/RelatedEvent.h" |
| 29 #include "core/html/HTMLMenuItemElement.h" |
| 30 #include "core/page/ContextMenuController.h" |
| 31 #include "core/page/Page.h" |
27 | 32 |
28 namespace WebCore { | 33 namespace WebCore { |
29 | 34 |
30 using namespace HTMLNames; | 35 using namespace HTMLNames; |
31 | 36 |
32 inline HTMLMenuElement::HTMLMenuElement(Document& document) | 37 inline HTMLMenuElement::HTMLMenuElement(Document& document) |
33 : HTMLElement(menuTag, document) | 38 : HTMLElement(menuTag, document) |
34 { | 39 { |
35 ScriptWrappable::init(this); | 40 ScriptWrappable::init(this); |
36 } | 41 } |
37 | 42 |
38 PassRefPtrWillBeRawPtr<HTMLMenuElement> HTMLMenuElement::create(Document& docume
nt) | 43 PassRefPtrWillBeRawPtr<HTMLMenuElement> HTMLMenuElement::create(Document& docume
nt) |
39 { | 44 { |
40 return adoptRefWillBeRefCountedGarbageCollected(new HTMLMenuElement(document
)); | 45 return adoptRefWillBeRefCountedGarbageCollected(new HTMLMenuElement(document
)); |
41 } | 46 } |
42 | 47 |
| 48 CustomContextMenuProvider& HTMLMenuElement::ensureMenuProvider() |
| 49 { |
| 50 if (!m_menuProvider) { |
| 51 ContextMenu menu; |
| 52 populateContextMenuItems(this, menu); |
| 53 m_menuProvider = CustomContextMenuProvider::create(this, menu.items()); |
| 54 } |
| 55 return *m_menuProvider; |
43 } | 56 } |
| 57 |
| 58 void HTMLMenuElement::populateContextMenuItems(HTMLMenuElement* menu, ContextMen
u& contextMenu) |
| 59 { |
| 60 Element* nextElement = ElementTraversal::firstWithin(*menu); |
| 61 while (nextElement) { |
| 62 if (nextElement->hasTagName(HTMLNames::menuTag)) { |
| 63 ContextMenu subMenu; |
| 64 populateContextMenuItems(toHTMLMenuElement(nextElement), subMenu); |
| 65 contextMenu.appendItem(ContextMenuItem(SubmenuType, ContextMenuItemC
ustomTagNoAction, nextElement->fastGetAttribute(HTMLNames::labelAttr), &subMenu)
); |
| 66 nextElement = ElementTraversal::nextSibling(*nextElement); |
| 67 } else if (nextElement->hasTagName(HTMLNames::menuitemTag)) { |
| 68 m_menuItems.append(toHTMLElement(nextElement)); |
| 69 contextMenu.appendItem(ContextMenuItem(ActionType, static_cast<Conte
xtMenuAction>(ContextMenuItemBaseCustomTag + m_menuItems.size() - 1), nextElemen
t->fastGetAttribute(HTMLNames::labelAttr))); |
| 70 if (ContextMenuItemBaseCustomTag + m_menuItems.size() >= ContextMenu
ItemLastCustomTag) |
| 71 break; |
| 72 nextElement = ElementTraversal::next(*nextElement, menu); |
| 73 } |
| 74 } |
| 75 } |
| 76 |
| 77 void HTMLMenuElement::defaultEventHandler(Event* event) |
| 78 { |
| 79 if (event->type() == EventTypeNames::show && event->isRelatedEvent()) { |
| 80 m_subject = toHTMLElement(toRelatedEvent(event)->relatedTarget()->toNode
()); |
| 81 ensureMenuProvider(); |
| 82 event->setDefaultHandled(); |
| 83 } |
| 84 } |
| 85 |
| 86 HTMLElement* HTMLMenuElement::menuItemAt(unsigned menuId) |
| 87 { |
| 88 if (menuId < ContextMenuItemBaseCustomTag || menuId > ContextMenuItemLastCus
tomTag) |
| 89 return 0; |
| 90 int itemNumber = menuId - ContextMenuItemBaseCustomTag; |
| 91 return m_menuItems[itemNumber]; |
| 92 } |
| 93 |
| 94 } |
OLD | NEW |