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/. */
7 #include "mozilla/dom/XULMenuElement.h"
8 #include "mozilla/StaticAnalysisFunctions.h"
9 #include "mozilla/dom/XULButtonElement.h"
10 #include "mozilla/dom/XULMenuElementBinding.h"
11 #include "mozilla/dom/XULPopupElement.h"
12 #include "mozilla/dom/KeyboardEvent.h"
13 #include "mozilla/dom/KeyboardEventBinding.h"
14 #include "nsXULPopupManager.h"
15 #include "nsMenuPopupFrame.h"
17 namespace mozilla::dom
{
19 JSObject
* XULMenuElement::WrapNode(JSContext
* aCx
,
20 JS::Handle
<JSObject
*> aGivenProto
) {
21 return XULMenuElement_Binding::Wrap(aCx
, this, aGivenProto
);
24 Element
* XULMenuElement::GetActiveMenuChild() {
25 RefPtr popup
= GetMenuPopupContent();
26 return popup
? popup
->GetActiveMenuChild() : nullptr;
29 void XULMenuElement::SetActiveMenuChild(Element
* aChild
) {
30 RefPtr popup
= GetMenuPopupContent();
31 if (NS_WARN_IF(!popup
)) {
36 popup
->SetActiveMenuChild(nullptr);
39 auto* button
= XULButtonElement::FromNode(aChild
);
40 if (NS_WARN_IF(!button
) || NS_WARN_IF(!button
->IsMenu())) {
43 // KnownLive because it's aChild.
44 popup
->SetActiveMenuChild(MOZ_KnownLive(button
));
47 bool XULButtonElement::HandleKeyPress(KeyboardEvent
& keyEvent
) {
48 RefPtr
<nsXULPopupManager
> pm
= nsXULPopupManager::GetInstance();
53 // if event has already been handled, bail
54 if (keyEvent
.DefaultPrevented()) {
58 if (keyEvent
.IsMenuAccessKeyPressed()) {
62 nsMenuPopupFrame
* popupFrame
= GetMenuPopup(FlushType::Frames
);
63 if (NS_WARN_IF(!popupFrame
)) {
67 uint32_t keyCode
= keyEvent
.KeyCode();
69 case KeyboardEvent_Binding::DOM_VK_UP
:
70 case KeyboardEvent_Binding::DOM_VK_DOWN
:
71 case KeyboardEvent_Binding::DOM_VK_HOME
:
72 case KeyboardEvent_Binding::DOM_VK_END
: {
73 nsNavigationDirection theDirection
;
74 theDirection
= NS_DIRECTION_FROM_KEY_CODE(popupFrame
, keyCode
);
75 return pm
->HandleKeyboardNavigationInPopup(popupFrame
, theDirection
);
78 return pm
->HandleShortcutNavigation(keyEvent
, popupFrame
);
82 } // namespace mozilla::dom