layout/xul/nsMenuBoxObject.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/xul/nsMenuBoxObject.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +#include "nsISupportsUtils.h"
     1.9 +#include "nsIMenuBoxObject.h"
    1.10 +#include "nsBoxObject.h"
    1.11 +#include "nsIDOMKeyEvent.h"
    1.12 +#include "nsIFrame.h"
    1.13 +#include "nsMenuBarFrame.h"
    1.14 +#include "nsMenuBarListener.h"
    1.15 +#include "nsMenuFrame.h"
    1.16 +#include "nsMenuPopupFrame.h"
    1.17 +
    1.18 +class nsMenuBoxObject : public nsIMenuBoxObject,
    1.19 +                        public nsBoxObject
    1.20 +{
    1.21 +public:
    1.22 +  NS_DECL_ISUPPORTS_INHERITED
    1.23 +  NS_DECL_NSIMENUBOXOBJECT
    1.24 +
    1.25 +  nsMenuBoxObject();
    1.26 +  virtual ~nsMenuBoxObject();
    1.27 +};
    1.28 +
    1.29 +nsMenuBoxObject::nsMenuBoxObject()
    1.30 +{
    1.31 +}
    1.32 +
    1.33 +nsMenuBoxObject::~nsMenuBoxObject()
    1.34 +{
    1.35 +}
    1.36 +
    1.37 +NS_IMPL_ISUPPORTS_INHERITED(nsMenuBoxObject, nsBoxObject, nsIMenuBoxObject)
    1.38 +
    1.39 +/* void openMenu (in boolean openFlag); */
    1.40 +NS_IMETHODIMP nsMenuBoxObject::OpenMenu(bool aOpenFlag)
    1.41 +{
    1.42 +  nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
    1.43 +  if (pm) {
    1.44 +    nsIFrame* frame = GetFrame(false);
    1.45 +    if (frame) {
    1.46 +      if (aOpenFlag) {
    1.47 +        nsCOMPtr<nsIContent> content = mContent;
    1.48 +        pm->ShowMenu(content, false, false);
    1.49 +      }
    1.50 +      else {
    1.51 +        nsMenuFrame* menu = do_QueryFrame(frame);
    1.52 +        if (menu) {
    1.53 +          nsMenuPopupFrame* popupFrame = menu->GetPopup();
    1.54 +          if (popupFrame)
    1.55 +            pm->HidePopup(popupFrame->GetContent(), false, true, false, false);
    1.56 +        }
    1.57 +      }
    1.58 +    }
    1.59 +  }
    1.60 +
    1.61 +  return NS_OK;
    1.62 +}
    1.63 +
    1.64 +NS_IMETHODIMP nsMenuBoxObject::GetActiveChild(nsIDOMElement** aResult)
    1.65 +{
    1.66 +  *aResult = nullptr;
    1.67 +  nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
    1.68 +  if (menu)
    1.69 +    return menu->GetActiveChild(aResult);
    1.70 +  return NS_OK;
    1.71 +}
    1.72 +
    1.73 +NS_IMETHODIMP nsMenuBoxObject::SetActiveChild(nsIDOMElement* aResult)
    1.74 +{
    1.75 +  nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
    1.76 +  if (menu)
    1.77 +    return menu->SetActiveChild(aResult);
    1.78 +  return NS_OK;
    1.79 +}
    1.80 +
    1.81 +/* boolean handleKeyPress (in nsIDOMKeyEvent keyEvent); */
    1.82 +NS_IMETHODIMP nsMenuBoxObject::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent, bool* aHandledFlag)
    1.83 +{
    1.84 +  *aHandledFlag = false;
    1.85 +  NS_ENSURE_ARG(aKeyEvent);
    1.86 +
    1.87 +  nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
    1.88 +  if (!pm)
    1.89 +    return NS_OK;
    1.90 +
    1.91 +  // if event has already been handled, bail
    1.92 +  bool eventHandled = false;
    1.93 +  aKeyEvent->GetDefaultPrevented(&eventHandled);
    1.94 +  if (eventHandled)
    1.95 +    return NS_OK;
    1.96 +
    1.97 +  if (nsMenuBarListener::IsAccessKeyPressed(aKeyEvent))
    1.98 +    return NS_OK;
    1.99 +
   1.100 +  nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
   1.101 +  if (!menu)
   1.102 +    return NS_OK;
   1.103 +
   1.104 +  nsMenuPopupFrame* popupFrame = menu->GetPopup();
   1.105 +  if (!popupFrame)
   1.106 +    return NS_OK;
   1.107 +
   1.108 +  uint32_t keyCode;
   1.109 +  aKeyEvent->GetKeyCode(&keyCode);
   1.110 +  switch (keyCode) {
   1.111 +    case nsIDOMKeyEvent::DOM_VK_UP:
   1.112 +    case nsIDOMKeyEvent::DOM_VK_DOWN:
   1.113 +    case nsIDOMKeyEvent::DOM_VK_HOME:
   1.114 +    case nsIDOMKeyEvent::DOM_VK_END:
   1.115 +    {
   1.116 +      nsNavigationDirection theDirection;
   1.117 +      theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode);
   1.118 +      *aHandledFlag =
   1.119 +        pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection);
   1.120 +      return NS_OK;
   1.121 +    }
   1.122 +    default:
   1.123 +      *aHandledFlag = pm->HandleShortcutNavigation(aKeyEvent, popupFrame);
   1.124 +      return NS_OK;
   1.125 +  }
   1.126 +}
   1.127 +
   1.128 +NS_IMETHODIMP
   1.129 +nsMenuBoxObject::GetOpenedWithKey(bool* aOpenedWithKey)
   1.130 +{
   1.131 +  *aOpenedWithKey = false;
   1.132 +
   1.133 +  nsMenuFrame* menuframe = do_QueryFrame(GetFrame(false));
   1.134 +  if (!menuframe)
   1.135 +    return NS_OK;
   1.136 +
   1.137 +  nsIFrame* frame = menuframe->GetParent();
   1.138 +  while (frame) {
   1.139 +    nsMenuBarFrame* menubar = do_QueryFrame(frame);
   1.140 +    if (menubar) {
   1.141 +      *aOpenedWithKey = menubar->IsActiveByKeyboard();
   1.142 +      return NS_OK;
   1.143 +    }
   1.144 +    frame = frame->GetParent();
   1.145 +  }
   1.146 +
   1.147 +  return NS_OK;
   1.148 +}
   1.149 +
   1.150 +
   1.151 +// Creation Routine ///////////////////////////////////////////////////////////////////////
   1.152 +
   1.153 +nsresult
   1.154 +NS_NewMenuBoxObject(nsIBoxObject** aResult)
   1.155 +{
   1.156 +  *aResult = new nsMenuBoxObject;
   1.157 +  if (!*aResult)
   1.158 +    return NS_ERROR_OUT_OF_MEMORY;
   1.159 +  NS_ADDREF(*aResult);
   1.160 +  return NS_OK;
   1.161 +}
   1.162 +

mercurial