gecko-dev/layout/xul/nsMenuBoxObject.cpp

160 lines
3.9 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2012-05-21 11:12:37 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISupportsUtils.h"
2000-05-30 07:59:13 +00:00
#include "nsIMenuBoxObject.h"
#include "nsBoxObject.h"
#include "nsIDOMKeyEvent.h"
2000-05-30 07:59:13 +00:00
#include "nsIFrame.h"
#include "nsMenuBarFrame.h"
#include "nsMenuBarListener.h"
#include "nsMenuFrame.h"
#include "nsMenuPopupFrame.h"
2000-05-30 07:59:13 +00:00
class nsMenuBoxObject : public nsIMenuBoxObject,
public nsBoxObject
2000-05-30 07:59:13 +00:00
{
public:
NS_DECL_ISUPPORTS_INHERITED
2000-05-30 07:59:13 +00:00
NS_DECL_NSIMENUBOXOBJECT
nsMenuBoxObject();
virtual ~nsMenuBoxObject();
};
nsMenuBoxObject::nsMenuBoxObject()
{
}
nsMenuBoxObject::~nsMenuBoxObject()
{
}
NS_IMPL_ISUPPORTS_INHERITED(nsMenuBoxObject, nsBoxObject, nsIMenuBoxObject)
2000-05-30 07:59:13 +00:00
/* void openMenu (in boolean openFlag); */
NS_IMETHODIMP nsMenuBoxObject::OpenMenu(bool aOpenFlag)
2000-05-30 07:59:13 +00:00
{
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (pm) {
nsIFrame* frame = GetFrame(false);
if (frame) {
if (aOpenFlag) {
nsCOMPtr<nsIContent> content = mContent;
pm->ShowMenu(content, false, false);
}
else {
nsMenuFrame* menu = do_QueryFrame(frame);
if (menu) {
nsMenuPopupFrame* popupFrame = menu->GetPopup();
if (popupFrame)
pm->HidePopup(popupFrame->GetContent(), false, true, false, false);
}
}
}
}
return NS_OK;
2000-05-30 07:59:13 +00:00
}
2000-08-17 09:15:51 +00:00
NS_IMETHODIMP nsMenuBoxObject::GetActiveChild(nsIDOMElement** aResult)
{
*aResult = nullptr;
nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
if (menu)
return menu->GetActiveChild(aResult);
return NS_OK;
2000-08-17 09:15:51 +00:00
}
NS_IMETHODIMP nsMenuBoxObject::SetActiveChild(nsIDOMElement* aResult)
{
nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
if (menu)
return menu->SetActiveChild(aResult);
return NS_OK;
2000-08-17 09:15:51 +00:00
}
2000-05-30 07:59:13 +00:00
/* boolean handleKeyPress (in nsIDOMKeyEvent keyEvent); */
NS_IMETHODIMP nsMenuBoxObject::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent, bool* aHandledFlag)
{
*aHandledFlag = false;
NS_ENSURE_ARG(aKeyEvent);
nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
if (!pm)
return NS_OK;
// if event has already been handled, bail
bool eventHandled = false;
aKeyEvent->GetDefaultPrevented(&eventHandled);
if (eventHandled)
return NS_OK;
if (nsMenuBarListener::IsAccessKeyPressed(aKeyEvent))
return NS_OK;
nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
if (!menu)
return NS_OK;
nsMenuPopupFrame* popupFrame = menu->GetPopup();
if (!popupFrame)
return NS_OK;
uint32_t keyCode;
aKeyEvent->GetKeyCode(&keyCode);
switch (keyCode) {
case nsIDOMKeyEvent::DOM_VK_UP:
case nsIDOMKeyEvent::DOM_VK_DOWN:
case nsIDOMKeyEvent::DOM_VK_HOME:
case nsIDOMKeyEvent::DOM_VK_END:
{
nsNavigationDirection theDirection;
theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode);
*aHandledFlag =
pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection);
return NS_OK;
}
default:
*aHandledFlag = pm->HandleShortcutNavigation(aKeyEvent, popupFrame);
return NS_OK;
}
}
NS_IMETHODIMP
nsMenuBoxObject::GetOpenedWithKey(bool* aOpenedWithKey)
{
*aOpenedWithKey = false;
nsMenuFrame* menuframe = do_QueryFrame(GetFrame(false));
if (!menuframe)
return NS_OK;
nsIFrame* frame = menuframe->GetParent();
while (frame) {
nsMenuBarFrame* menubar = do_QueryFrame(frame);
if (menubar) {
*aOpenedWithKey = menubar->IsActiveByKeyboard();
return NS_OK;
}
frame = frame->GetParent();
}
return NS_OK;
}
2000-05-30 07:59:13 +00:00
// Creation Routine ///////////////////////////////////////////////////////////////////////
nsresult
NS_NewMenuBoxObject(nsIBoxObject** aResult)
{
*aResult = new nsMenuBoxObject;
if (!*aResult)
return NS_ERROR_OUT_OF_MEMORY;
NS_ADDREF(*aResult);
return NS_OK;
}