MenuBoxObject.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "mozilla/dom/MenuBoxObject.h"
  6. #include "mozilla/dom/MenuBoxObjectBinding.h"
  7. #include "mozilla/dom/KeyboardEvent.h"
  8. #include "mozilla/dom/Element.h"
  9. #include "nsIDOMKeyEvent.h"
  10. #include "nsIFrame.h"
  11. #include "nsMenuBarFrame.h"
  12. #include "nsMenuBarListener.h"
  13. #include "nsMenuFrame.h"
  14. #include "nsMenuPopupFrame.h"
  15. namespace mozilla {
  16. namespace dom {
  17. MenuBoxObject::MenuBoxObject()
  18. {
  19. }
  20. MenuBoxObject::~MenuBoxObject()
  21. {
  22. }
  23. JSObject* MenuBoxObject::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  24. {
  25. return MenuBoxObjectBinding::Wrap(aCx, this, aGivenProto);
  26. }
  27. void MenuBoxObject::OpenMenu(bool aOpenFlag)
  28. {
  29. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  30. if (pm) {
  31. nsIFrame* frame = GetFrame(false);
  32. if (frame) {
  33. if (aOpenFlag) {
  34. nsCOMPtr<nsIContent> content = mContent;
  35. pm->ShowMenu(content, false, false);
  36. }
  37. else {
  38. nsMenuFrame* menu = do_QueryFrame(frame);
  39. if (menu) {
  40. nsMenuPopupFrame* popupFrame = menu->GetPopup();
  41. if (popupFrame)
  42. pm->HidePopup(popupFrame->GetContent(), false, true, false, false);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. already_AddRefed<Element>
  49. MenuBoxObject::GetActiveChild()
  50. {
  51. nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
  52. if (menu) {
  53. nsCOMPtr<nsIDOMElement> el;
  54. menu->GetActiveChild(getter_AddRefs(el));
  55. nsCOMPtr<Element> ret(do_QueryInterface(el));
  56. return ret.forget();
  57. }
  58. return nullptr;
  59. }
  60. void MenuBoxObject::SetActiveChild(Element* arg)
  61. {
  62. nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
  63. if (menu) {
  64. nsCOMPtr<nsIDOMElement> el(do_QueryInterface(arg));
  65. menu->SetActiveChild(el);
  66. }
  67. }
  68. bool MenuBoxObject::HandleKeyPress(KeyboardEvent& keyEvent)
  69. {
  70. nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
  71. if (!pm) {
  72. return false;
  73. }
  74. // if event has already been handled, bail
  75. bool eventHandled = false;
  76. keyEvent.GetDefaultPrevented(&eventHandled);
  77. if (eventHandled) {
  78. return false;
  79. }
  80. if (nsMenuBarListener::IsAccessKeyPressed(&keyEvent))
  81. return false;
  82. nsMenuFrame* menu = do_QueryFrame(GetFrame(false));
  83. if (!menu) {
  84. return false;
  85. }
  86. nsMenuPopupFrame* popupFrame = menu->GetPopup();
  87. if (!popupFrame) {
  88. return false;
  89. }
  90. uint32_t keyCode = keyEvent.KeyCode();
  91. switch (keyCode) {
  92. case nsIDOMKeyEvent::DOM_VK_UP:
  93. case nsIDOMKeyEvent::DOM_VK_DOWN:
  94. case nsIDOMKeyEvent::DOM_VK_HOME:
  95. case nsIDOMKeyEvent::DOM_VK_END:
  96. {
  97. nsNavigationDirection theDirection;
  98. theDirection = NS_DIRECTION_FROM_KEY_CODE(popupFrame, keyCode);
  99. return pm->HandleKeyboardNavigationInPopup(popupFrame, theDirection);
  100. }
  101. default:
  102. return pm->HandleShortcutNavigation(&keyEvent, popupFrame);
  103. }
  104. }
  105. bool MenuBoxObject::OpenedWithKey()
  106. {
  107. nsMenuFrame* menuframe = do_QueryFrame(GetFrame(false));
  108. if (!menuframe) {
  109. return false;
  110. }
  111. nsIFrame* frame = menuframe->GetParent();
  112. while (frame) {
  113. nsMenuBarFrame* menubar = do_QueryFrame(frame);
  114. if (menubar) {
  115. return menubar->IsActiveByKeyboard();
  116. }
  117. frame = frame->GetParent();
  118. }
  119. return false;
  120. }
  121. } // namespace dom
  122. } // namespace mozilla
  123. // Creation Routine ///////////////////////////////////////////////////////////////////////
  124. using namespace mozilla::dom;
  125. nsresult
  126. NS_NewMenuBoxObject(nsIBoxObject** aResult)
  127. {
  128. NS_ADDREF(*aResult = new MenuBoxObject());
  129. return NS_OK;
  130. }