nsXULLabelFrame.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. /* derived class of nsBlockFrame used for xul:label elements */
  6. #include "mozilla/EventStateManager.h"
  7. #include "nsXULLabelFrame.h"
  8. #include "nsHTMLParts.h"
  9. #include "nsNameSpaceManager.h"
  10. using namespace mozilla;
  11. nsIFrame*
  12. NS_NewXULLabelFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  13. {
  14. nsXULLabelFrame* it = new (aPresShell) nsXULLabelFrame(aContext);
  15. it->AddStateBits(NS_BLOCK_FORMATTING_CONTEXT_STATE_BITS);
  16. return it;
  17. }
  18. NS_IMPL_FRAMEARENA_HELPERS(nsXULLabelFrame)
  19. // If you make changes to this function, check its counterparts
  20. // in nsBoxFrame and nsTextBoxFrame
  21. nsresult
  22. nsXULLabelFrame::RegUnregAccessKey(bool aDoReg)
  23. {
  24. // if we have no content, we can't do anything
  25. if (!mContent)
  26. return NS_ERROR_FAILURE;
  27. // To filter out <label>s without a control attribute.
  28. // XXXjag a side-effect is that we filter out anonymous <label>s
  29. // in e.g. <menu>, <menuitem>, <button>. These <label>s inherit
  30. // |accesskey| and would otherwise register themselves, overwriting
  31. // the content we really meant to be registered.
  32. if (!mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::control))
  33. return NS_OK;
  34. nsAutoString accessKey;
  35. mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::accesskey, accessKey);
  36. if (accessKey.IsEmpty())
  37. return NS_OK;
  38. // With a valid PresContext we can get the ESM
  39. // and register the access key
  40. EventStateManager* esm = PresContext()->EventStateManager();
  41. uint32_t key = accessKey.First();
  42. if (aDoReg)
  43. esm->RegisterAccessKey(mContent, key);
  44. else
  45. esm->UnregisterAccessKey(mContent, key);
  46. return NS_OK;
  47. }
  48. /////////////////////////////////////////////////////////////////////////////
  49. // nsIFrame
  50. void
  51. nsXULLabelFrame::Init(nsIContent* aContent,
  52. nsContainerFrame* aParent,
  53. nsIFrame* aPrevInFlow)
  54. {
  55. nsBlockFrame::Init(aContent, aParent, aPrevInFlow);
  56. // register access key
  57. RegUnregAccessKey(true);
  58. }
  59. void
  60. nsXULLabelFrame::DestroyFrom(nsIFrame* aDestructRoot)
  61. {
  62. // unregister access key
  63. RegUnregAccessKey(false);
  64. nsBlockFrame::DestroyFrom(aDestructRoot);
  65. }
  66. nsresult
  67. nsXULLabelFrame::AttributeChanged(int32_t aNameSpaceID,
  68. nsIAtom* aAttribute,
  69. int32_t aModType)
  70. {
  71. nsresult rv = nsBlockFrame::AttributeChanged(aNameSpaceID,
  72. aAttribute, aModType);
  73. // If the accesskey changed, register for the new value
  74. // The old value has been unregistered in nsXULElement::SetAttr
  75. if (aAttribute == nsGkAtoms::accesskey || aAttribute == nsGkAtoms::control)
  76. RegUnregAccessKey(true);
  77. return rv;
  78. }
  79. nsIAtom*
  80. nsXULLabelFrame::GetType() const
  81. {
  82. return nsGkAtoms::XULLabelFrame;
  83. }
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Diagnostics
  86. #ifdef DEBUG_FRAME_DUMP
  87. nsresult
  88. nsXULLabelFrame::GetFrameName(nsAString& aResult) const
  89. {
  90. return MakeFrameName(NS_LITERAL_STRING("XULLabel"), aResult);
  91. }
  92. #endif