OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/page/CustomContextMenuProvider.h" |
| 7 |
| 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/ElementTraversal.h" |
| 10 #include "core/events/EventDispatcher.h" |
| 11 #include "core/events/MouseEvent.h" |
| 12 #include "core/html/HTMLMenuElement.h" |
| 13 #include "core/html/HTMLMenuItemElement.h" |
| 14 #include "core/page/ContextMenuController.h" |
| 15 #include "core/page/Page.h" |
| 16 #include "platform/ContextMenu.h" |
| 17 |
| 18 namespace blink { |
| 19 |
| 20 using namespace HTMLNames; |
| 21 |
| 22 CustomContextMenuProvider::CustomContextMenuProvider(HTMLMenuElement& menu, HTML
Element& subject) |
| 23 : m_menu(menu) |
| 24 , m_subjectElement(subject) |
| 25 { |
| 26 } |
| 27 |
| 28 CustomContextMenuProvider::~CustomContextMenuProvider() |
| 29 { |
| 30 } |
| 31 |
| 32 void CustomContextMenuProvider::populateContextMenu(ContextMenu* menu) |
| 33 { |
| 34 populateContextMenuItems(*m_menu, *menu); |
| 35 } |
| 36 |
| 37 void CustomContextMenuProvider::contextMenuItemSelected(const ContextMenuItem* i
tem) |
| 38 { |
| 39 if (HTMLElement* element = menuItemAt(item->action())) { |
| 40 RefPtrWillBeRawPtr<SimulatedMouseEvent> click = SimulatedMouseEvent::cre
ate(EventTypeNames::click, m_menu->document().domWindow(), Event::create()); |
| 41 click->setRelatedTarget(m_subjectElement.get()); |
| 42 element->dispatchEvent(click.release()); |
| 43 } |
| 44 } |
| 45 |
| 46 void CustomContextMenuProvider::contextMenuCleared() |
| 47 { |
| 48 m_menuItems.clear(); |
| 49 m_subjectElement = nullptr; |
| 50 } |
| 51 |
| 52 void CustomContextMenuProvider::populateContextMenuItems(const HTMLMenuElement&
menu, ContextMenu& contextMenu) |
| 53 { |
| 54 HTMLElement* nextElement = Traversal<HTMLElement>::firstWithin(menu); |
| 55 while (nextElement) { |
| 56 if (isHTMLMenuElement(*nextElement)) { |
| 57 ContextMenu subMenu; |
| 58 populateContextMenuItems(*toHTMLMenuElement(nextElement), subMenu); |
| 59 contextMenu.appendItem(ContextMenuItem(SubmenuType, ContextMenuItemC
ustomTagNoAction, nextElement->fastGetAttribute(labelAttr), &subMenu)); |
| 60 nextElement = Traversal<HTMLElement>::nextSibling(*nextElement); |
| 61 } else if (isHTMLMenuItemElement(*nextElement)) { |
| 62 m_menuItems.append(nextElement); |
| 63 contextMenu.appendItem(ContextMenuItem(ActionType, static_cast<Conte
xtMenuAction>(ContextMenuItemBaseCustomTag + m_menuItems.size() - 1), nextElemen
t->fastGetAttribute(labelAttr))); |
| 64 if (ContextMenuItemBaseCustomTag + m_menuItems.size() >= ContextMenu
ItemLastCustomTag) |
| 65 break; |
| 66 nextElement = Traversal<HTMLElement>::next(*nextElement, &menu); |
| 67 } else { |
| 68 nextElement = Traversal<HTMLElement>::next(*nextElement, &menu); |
| 69 } |
| 70 } |
| 71 } |
| 72 |
| 73 HTMLElement* CustomContextMenuProvider::menuItemAt(unsigned menuId) |
| 74 { |
| 75 int itemIndex = menuId - ContextMenuItemBaseCustomTag; |
| 76 if (itemIndex < 0 || static_cast<unsigned long>(itemIndex) >= m_menuItems.si
ze()) |
| 77 return 0; |
| 78 return m_menuItems[itemIndex].get(); |
| 79 } |
| 80 |
| 81 } // namespace blink |
OLD | NEW |