Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | #include "nsISupportsUtils.h" |
michael@0 | 6 | #include "nsIMenuBoxObject.h" |
michael@0 | 7 | #include "nsBoxObject.h" |
michael@0 | 8 | #include "nsIDOMKeyEvent.h" |
michael@0 | 9 | #include "nsIFrame.h" |
michael@0 | 10 | #include "nsMenuBarFrame.h" |
michael@0 | 11 | #include "nsMenuBarListener.h" |
michael@0 | 12 | #include "nsMenuFrame.h" |
michael@0 | 13 | #include "nsMenuPopupFrame.h" |
michael@0 | 14 | |
michael@0 | 15 | class nsMenuBoxObject : public nsIMenuBoxObject, |
michael@0 | 16 | public nsBoxObject |
michael@0 | 17 | { |
michael@0 | 18 | public: |
michael@0 | 19 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 20 | NS_DECL_NSIMENUBOXOBJECT |
michael@0 | 21 | |
michael@0 | 22 | nsMenuBoxObject(); |
michael@0 | 23 | virtual ~nsMenuBoxObject(); |
michael@0 | 24 | }; |
michael@0 | 25 | |
michael@0 | 26 | nsMenuBoxObject::nsMenuBoxObject() |
michael@0 | 27 | { |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | nsMenuBoxObject::~nsMenuBoxObject() |
michael@0 | 31 | { |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | NS_IMPL_ISUPPORTS_INHERITED(nsMenuBoxObject, nsBoxObject, nsIMenuBoxObject) |
michael@0 | 35 | |
michael@0 | 36 | /* void openMenu (in boolean openFlag); */ |
michael@0 | 37 | NS_IMETHODIMP nsMenuBoxObject::OpenMenu(bool aOpenFlag) |
michael@0 | 38 | { |
michael@0 | 39 | nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); |
michael@0 | 40 | if (pm) { |
michael@0 | 41 | nsIFrame* frame = GetFrame(false); |
michael@0 | 42 | if (frame) { |
michael@0 | 43 | if (aOpenFlag) { |
michael@0 | 44 | nsCOMPtr<nsIContent> content = mContent; |
michael@0 | 45 | pm->ShowMenu(content, false, false); |
michael@0 | 46 | } |
michael@0 | 47 | else { |
michael@0 | 48 | nsMenuFrame* menu = do_QueryFrame(frame); |
michael@0 | 49 | if (menu) { |
michael@0 | 50 | nsMenuPopupFrame* popupFrame = menu->GetPopup(); |
michael@0 | 51 | if (popupFrame) |
michael@0 | 52 | pm->HidePopup(popupFrame->GetContent(), false, true, false, false); |
michael@0 | 53 | } |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | return NS_OK; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | NS_IMETHODIMP nsMenuBoxObject::GetActiveChild(nsIDOMElement** aResult) |
michael@0 | 62 | { |
michael@0 | 63 | *aResult = nullptr; |
michael@0 | 64 | nsMenuFrame* menu = do_QueryFrame(GetFrame(false)); |
michael@0 | 65 | if (menu) |
michael@0 | 66 | return menu->GetActiveChild(aResult); |
michael@0 | 67 | return NS_OK; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | NS_IMETHODIMP nsMenuBoxObject::SetActiveChild(nsIDOMElement* aResult) |
michael@0 | 71 | { |
michael@0 | 72 | nsMenuFrame* menu = do_QueryFrame(GetFrame(false)); |
michael@0 | 73 | if (menu) |
michael@0 | 74 | return menu->SetActiveChild(aResult); |
michael@0 | 75 | return NS_OK; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | /* boolean handleKeyPress (in nsIDOMKeyEvent keyEvent); */ |
michael@0 | 79 | NS_IMETHODIMP nsMenuBoxObject::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent, bool* aHandledFlag) |
michael@0 | 80 | { |
michael@0 | 81 | *aHandledFlag = false; |
michael@0 | 82 | NS_ENSURE_ARG(aKeyEvent); |
michael@0 | 83 | |
michael@0 | 84 | nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); |
michael@0 | 85 | if (!pm) |
michael@0 | 86 | return NS_OK; |
michael@0 | 87 | |
michael@0 | 88 | // if event has already been handled, bail |
michael@0 | 89 | bool eventHandled = false; |
michael@0 | 90 | aKeyEvent->GetDefaultPrevented(&eventHandled); |
michael@0 | 91 | if (eventHandled) |
michael@0 | 92 | return NS_OK; |
michael@0 | 93 | |
michael@0 | 94 | if (nsMenuBarListener::IsAccessKeyPressed(aKeyEvent)) |
michael@0 | 95 | return NS_OK; |
michael@0 | 96 | |
michael@0 | 97 | nsMenuFrame* menu = do_QueryFrame(GetFrame(false)); |
michael@0 | 98 | if (!menu) |
michael@0 | 99 | return NS_OK; |
michael@0 | 100 | |
michael@0 | 101 | nsMenuPopupFrame* popupFrame = menu->GetPopup(); |
michael@0 | 102 | if (!popupFrame) |
michael@0 | 103 | return NS_OK; |
michael@0 | 104 | |
michael@0 | 105 | uint32_t keyCode; |
michael@0 | 106 | aKeyEvent->GetKeyCode(&keyCode); |
michael@0 | 107 | switch (keyCode) { |
michael@0 | 108 | case nsIDOMKeyEvent::DOM_VK_UP: |
michael@0 | 109 | case nsIDOMKeyEvent::DOM_VK_DOWN: |
michael@0 | 110 | case nsIDOMKeyEvent::DOM_VK_HOME: |
michael@0 | 111 | case nsIDOMKeyEvent::DOM_VK_END: |
michael@0 | 112 | { |
michael@0 | 113 | nsNavigationDirection theDirection; |
michael@0 | 114 | theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode); |
michael@0 | 115 | *aHandledFlag = |
michael@0 | 116 | pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection); |
michael@0 | 117 | return NS_OK; |
michael@0 | 118 | } |
michael@0 | 119 | default: |
michael@0 | 120 | *aHandledFlag = pm->HandleShortcutNavigation(aKeyEvent, popupFrame); |
michael@0 | 121 | return NS_OK; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | NS_IMETHODIMP |
michael@0 | 126 | nsMenuBoxObject::GetOpenedWithKey(bool* aOpenedWithKey) |
michael@0 | 127 | { |
michael@0 | 128 | *aOpenedWithKey = false; |
michael@0 | 129 | |
michael@0 | 130 | nsMenuFrame* menuframe = do_QueryFrame(GetFrame(false)); |
michael@0 | 131 | if (!menuframe) |
michael@0 | 132 | return NS_OK; |
michael@0 | 133 | |
michael@0 | 134 | nsIFrame* frame = menuframe->GetParent(); |
michael@0 | 135 | while (frame) { |
michael@0 | 136 | nsMenuBarFrame* menubar = do_QueryFrame(frame); |
michael@0 | 137 | if (menubar) { |
michael@0 | 138 | *aOpenedWithKey = menubar->IsActiveByKeyboard(); |
michael@0 | 139 | return NS_OK; |
michael@0 | 140 | } |
michael@0 | 141 | frame = frame->GetParent(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | return NS_OK; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | |
michael@0 | 148 | // Creation Routine /////////////////////////////////////////////////////////////////////// |
michael@0 | 149 | |
michael@0 | 150 | nsresult |
michael@0 | 151 | NS_NewMenuBoxObject(nsIBoxObject** aResult) |
michael@0 | 152 | { |
michael@0 | 153 | *aResult = new nsMenuBoxObject; |
michael@0 | 154 | if (!*aResult) |
michael@0 | 155 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 156 | NS_ADDREF(*aResult); |
michael@0 | 157 | return NS_OK; |
michael@0 | 158 | } |
michael@0 | 159 |