1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsISupportsUtils.h"
6 #include "nsIMenuBoxObject.h"
7 #include "nsBoxObject.h"
8 #include "nsIDOMKeyEvent.h"
10 #include "nsMenuBarFrame.h"
11 #include "nsMenuBarListener.h"
12 #include "nsMenuFrame.h"
13 #include "nsMenuPopupFrame.h"
15 class nsMenuBoxObject
: public nsIMenuBoxObject
,
19 NS_DECL_ISUPPORTS_INHERITED
20 NS_DECL_NSIMENUBOXOBJECT
24 virtual ~nsMenuBoxObject();
27 nsMenuBoxObject::nsMenuBoxObject()
31 nsMenuBoxObject::~nsMenuBoxObject()
35 NS_IMPL_ISUPPORTS_INHERITED(nsMenuBoxObject
, nsBoxObject
, nsIMenuBoxObject
)
37 /* void openMenu (in boolean openFlag); */
38 NS_IMETHODIMP
nsMenuBoxObject::OpenMenu(bool aOpenFlag
)
40 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
42 nsIFrame
* frame
= GetFrame(false);
45 nsCOMPtr
<nsIContent
> content
= mContent
;
46 pm
->ShowMenu(content
, false, false);
49 nsMenuFrame
* menu
= do_QueryFrame(frame
);
51 nsMenuPopupFrame
* popupFrame
= menu
->GetPopup();
53 pm
->HidePopup(popupFrame
->GetContent(), false, true, false, false);
62 NS_IMETHODIMP
nsMenuBoxObject::GetActiveChild(nsIDOMElement
** aResult
)
65 nsMenuFrame
* menu
= do_QueryFrame(GetFrame(false));
67 return menu
->GetActiveChild(aResult
);
71 NS_IMETHODIMP
nsMenuBoxObject::SetActiveChild(nsIDOMElement
* aResult
)
73 nsMenuFrame
* menu
= do_QueryFrame(GetFrame(false));
75 return menu
->SetActiveChild(aResult
);
79 /* boolean handleKeyPress (in nsIDOMKeyEvent keyEvent); */
80 NS_IMETHODIMP
nsMenuBoxObject::HandleKeyPress(nsIDOMKeyEvent
* aKeyEvent
, bool* aHandledFlag
)
82 *aHandledFlag
= false;
83 NS_ENSURE_ARG(aKeyEvent
);
85 nsXULPopupManager
* pm
= nsXULPopupManager::GetInstance();
89 // if event has already been handled, bail
90 bool eventHandled
= false;
91 aKeyEvent
->GetDefaultPrevented(&eventHandled
);
95 if (nsMenuBarListener::IsAccessKeyPressed(aKeyEvent
))
98 nsMenuFrame
* menu
= do_QueryFrame(GetFrame(false));
102 nsMenuPopupFrame
* popupFrame
= menu
->GetPopup();
107 aKeyEvent
->GetKeyCode(&keyCode
);
109 case nsIDOMKeyEvent::DOM_VK_UP
:
110 case nsIDOMKeyEvent::DOM_VK_DOWN
:
111 case nsIDOMKeyEvent::DOM_VK_HOME
:
112 case nsIDOMKeyEvent::DOM_VK_END
:
114 nsNavigationDirection theDirection
;
115 theDirection
= NS_DIRECTION_FROM_KEY_CODE(popupFrame
, keyCode
);
117 pm
->HandleKeyboardNavigationInPopup(popupFrame
, theDirection
);
121 *aHandledFlag
= pm
->HandleShortcutNavigation(aKeyEvent
, popupFrame
);
127 nsMenuBoxObject::GetOpenedWithKey(bool* aOpenedWithKey
)
129 *aOpenedWithKey
= false;
131 nsMenuFrame
* menuframe
= do_QueryFrame(GetFrame(false));
135 nsIFrame
* frame
= menuframe
->GetParent();
137 nsMenuBarFrame
* menubar
= do_QueryFrame(frame
);
139 *aOpenedWithKey
= menubar
->IsActiveByKeyboard();
142 frame
= frame
->GetParent();
149 // Creation Routine ///////////////////////////////////////////////////////////////////////
152 NS_NewMenuBoxObject(nsIBoxObject
** aResult
)
154 *aResult
= new nsMenuBoxObject
;
156 return NS_ERROR_OUT_OF_MEMORY
;