/src/mozilla-central/dom/xul/XULMenuElement.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "mozilla/dom/KeyboardEvent.h" |
8 | | #include "mozilla/dom/KeyboardEventBinding.h" |
9 | | #include "mozilla/dom/Element.h" |
10 | | #include "nsIFrame.h" |
11 | | #include "nsMenuBarFrame.h" |
12 | | #include "nsMenuBarListener.h" |
13 | | #include "nsMenuFrame.h" |
14 | | #include "nsMenuPopupFrame.h" |
15 | | #include "mozilla/dom/XULMenuElement.h" |
16 | | #include "mozilla/dom/XULMenuElementBinding.h" |
17 | | #include "nsXULPopupManager.h" |
18 | | |
19 | | namespace mozilla { |
20 | | namespace dom { |
21 | | |
22 | | JSObject* |
23 | | XULMenuElement::WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
24 | 0 | { |
25 | 0 | return XULMenuElement_Binding::Wrap(aCx, this, aGivenProto); |
26 | 0 | } |
27 | | |
28 | | nsIFrame* |
29 | | XULMenuElement::GetFrame() |
30 | 0 | { |
31 | 0 | nsCOMPtr<nsIContent> kungFuDeathGrip = this; // keep a reference |
32 | 0 |
|
33 | 0 | nsCOMPtr<nsIDocument> doc = GetUncomposedDoc(); |
34 | 0 | if (doc) { |
35 | 0 | doc->FlushPendingNotifications(FlushType::Frames); |
36 | 0 | } |
37 | 0 |
|
38 | 0 | return GetPrimaryFrame(); |
39 | 0 | } |
40 | | |
41 | | already_AddRefed<Element> |
42 | | XULMenuElement::GetActiveChild() |
43 | 0 | { |
44 | 0 | nsMenuFrame* menu = do_QueryFrame(GetFrame()); |
45 | 0 | if (menu) { |
46 | 0 | RefPtr<Element> el; |
47 | 0 | menu->GetActiveChild(getter_AddRefs(el)); |
48 | 0 | return el.forget(); |
49 | 0 | } |
50 | 0 | return nullptr; |
51 | 0 | } |
52 | | |
53 | | void |
54 | | XULMenuElement::SetActiveChild(Element* arg) |
55 | 0 | { |
56 | 0 | nsMenuFrame* menu = do_QueryFrame(GetFrame()); |
57 | 0 | if (menu) { |
58 | 0 | menu->SetActiveChild(arg); |
59 | 0 | } |
60 | 0 | } |
61 | | |
62 | | bool |
63 | | XULMenuElement::HandleKeyPress(KeyboardEvent& keyEvent) |
64 | 0 | { |
65 | 0 | nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); |
66 | 0 | if (!pm) { |
67 | 0 | return false; |
68 | 0 | } |
69 | 0 | |
70 | 0 | // if event has already been handled, bail |
71 | 0 | if (keyEvent.DefaultPrevented()) { |
72 | 0 | return false; |
73 | 0 | } |
74 | 0 | |
75 | 0 | if (nsMenuBarListener::IsAccessKeyPressed(&keyEvent)) |
76 | 0 | return false; |
77 | 0 | |
78 | 0 | nsMenuFrame* menu = do_QueryFrame(GetFrame()); |
79 | 0 | if (!menu) { |
80 | 0 | return false; |
81 | 0 | } |
82 | 0 | |
83 | 0 | nsMenuPopupFrame* popupFrame = menu->GetPopup(); |
84 | 0 | if (!popupFrame) { |
85 | 0 | return false; |
86 | 0 | } |
87 | 0 | |
88 | 0 | uint32_t keyCode = keyEvent.KeyCode(); |
89 | 0 | switch (keyCode) { |
90 | 0 | case KeyboardEvent_Binding::DOM_VK_UP: |
91 | 0 | case KeyboardEvent_Binding::DOM_VK_DOWN: |
92 | 0 | case KeyboardEvent_Binding::DOM_VK_HOME: |
93 | 0 | case KeyboardEvent_Binding::DOM_VK_END: |
94 | 0 | { |
95 | 0 | nsNavigationDirection theDirection; |
96 | 0 | theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode); |
97 | 0 | return pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection); |
98 | 0 | } |
99 | 0 | default: |
100 | 0 | return pm->HandleShortcutNavigation(&keyEvent, popupFrame); |
101 | 0 | } |
102 | 0 | } |
103 | | |
104 | | bool |
105 | | XULMenuElement::OpenedWithKey() |
106 | 0 | { |
107 | 0 | nsMenuFrame* menuframe = do_QueryFrame(GetFrame()); |
108 | 0 | if (!menuframe) { |
109 | 0 | return false; |
110 | 0 | } |
111 | 0 | |
112 | 0 | nsIFrame* frame = menuframe->GetParent(); |
113 | 0 | while (frame) { |
114 | 0 | nsMenuBarFrame* menubar = do_QueryFrame(frame); |
115 | 0 | if (menubar) { |
116 | 0 | return menubar->IsActiveByKeyboard(); |
117 | 0 | } |
118 | 0 | frame = frame->GetParent(); |
119 | 0 | } |
120 | 0 | return false; |
121 | 0 | } |
122 | | |
123 | | } // namespace dom |
124 | | } // namespace mozilla |