nsNumberControlFrame.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #ifndef nsNumberControlFrame_h__
  6. #define nsNumberControlFrame_h__
  7. #include "mozilla/Attributes.h"
  8. #include "nsContainerFrame.h"
  9. #include "nsIFormControlFrame.h"
  10. #include "nsIAnonymousContentCreator.h"
  11. #include "nsCOMPtr.h"
  12. class nsITextControlFrame;
  13. class nsPresContext;
  14. namespace mozilla {
  15. enum class CSSPseudoElementType : uint8_t;
  16. class WidgetEvent;
  17. class WidgetGUIEvent;
  18. namespace dom {
  19. class HTMLInputElement;
  20. } // namespace dom
  21. } // namespace mozilla
  22. /**
  23. * This frame type is used for <input type=number>.
  24. */
  25. class nsNumberControlFrame final : public nsContainerFrame
  26. , public nsIAnonymousContentCreator
  27. , public nsIFormControlFrame
  28. {
  29. friend nsIFrame*
  30. NS_NewNumberControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext);
  31. typedef mozilla::CSSPseudoElementType CSSPseudoElementType;
  32. typedef mozilla::dom::Element Element;
  33. typedef mozilla::dom::HTMLInputElement HTMLInputElement;
  34. typedef mozilla::WidgetEvent WidgetEvent;
  35. typedef mozilla::WidgetGUIEvent WidgetGUIEvent;
  36. explicit nsNumberControlFrame(nsStyleContext* aContext);
  37. public:
  38. NS_DECL_QUERYFRAME_TARGET(nsNumberControlFrame)
  39. NS_DECL_QUERYFRAME
  40. NS_DECL_FRAMEARENA_HELPERS
  41. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  42. virtual void ContentStatesChanged(mozilla::EventStates aStates) override;
  43. virtual bool IsLeaf() const override { return true; }
  44. #ifdef ACCESSIBILITY
  45. virtual mozilla::a11y::AccType AccessibleType() override;
  46. #endif
  47. virtual nscoord GetMinISize(nsRenderingContext* aRenderingContext) override;
  48. virtual nscoord GetPrefISize(nsRenderingContext* aRenderingContext) override;
  49. virtual void Reflow(nsPresContext* aPresContext,
  50. ReflowOutput& aDesiredSize,
  51. const ReflowInput& aReflowInput,
  52. nsReflowStatus& aStatus) override;
  53. virtual nsresult AttributeChanged(int32_t aNameSpaceID,
  54. nsIAtom* aAttribute,
  55. int32_t aModType) override;
  56. // nsIAnonymousContentCreator
  57. virtual nsresult CreateAnonymousContent(nsTArray<ContentInfo>& aElements) override;
  58. virtual void AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
  59. uint32_t aFilter) override;
  60. #ifdef DEBUG_FRAME_DUMP
  61. virtual nsresult GetFrameName(nsAString& aResult) const override {
  62. return MakeFrameName(NS_LITERAL_STRING("NumberControl"), aResult);
  63. }
  64. #endif
  65. virtual nsIAtom* GetType() const override;
  66. virtual bool IsFrameOfType(uint32_t aFlags) const override
  67. {
  68. return nsContainerFrame::IsFrameOfType(aFlags &
  69. ~(nsIFrame::eReplaced | nsIFrame::eReplacedContainsBlock));
  70. }
  71. // nsIFormControlFrame
  72. virtual void SetFocus(bool aOn, bool aRepaint) override;
  73. virtual nsresult SetFormProperty(nsIAtom* aName, const nsAString& aValue) override;
  74. /**
  75. * This method attempts to localizes aValue and then sets the result as the
  76. * value of our anonymous text control. It's called when our
  77. * HTMLInputElement's value changes, when we need to sync up the value
  78. * displayed in our anonymous text control.
  79. */
  80. void SetValueOfAnonTextControl(const nsAString& aValue);
  81. /**
  82. * This method gets the string value of our anonymous text control,
  83. * attempts to normalizes (de-localizes) it, then sets the outparam aValue to
  84. * the result. It's called when user input changes the text value of our
  85. * anonymous text control so that we can sync up the internal value of our
  86. * HTMLInputElement.
  87. */
  88. void GetValueOfAnonTextControl(nsAString& aValue);
  89. bool AnonTextControlIsEmpty();
  90. /**
  91. * Called to notify this frame that its HTMLInputElement is currently
  92. * processing a DOM 'input' event.
  93. */
  94. void HandlingInputEvent(bool aHandlingEvent)
  95. {
  96. mHandlingInputEvent = aHandlingEvent;
  97. }
  98. HTMLInputElement* GetAnonTextControl();
  99. /**
  100. * If the frame is the frame for an nsNumberControlFrame's anonymous text
  101. * field, returns the nsNumberControlFrame. Else returns nullptr.
  102. */
  103. static nsNumberControlFrame* GetNumberControlFrameForTextField(nsIFrame* aFrame);
  104. /**
  105. * If the frame is the frame for an nsNumberControlFrame's up or down spin
  106. * button, returns the nsNumberControlFrame. Else returns nullptr.
  107. */
  108. static nsNumberControlFrame* GetNumberControlFrameForSpinButton(nsIFrame* aFrame);
  109. enum SpinButtonEnum {
  110. eSpinButtonNone,
  111. eSpinButtonUp,
  112. eSpinButtonDown
  113. };
  114. /**
  115. * Returns one of the SpinButtonEnum values to depending on whether the
  116. * pointer event is over the spin-up button, the spin-down button, or
  117. * neither.
  118. */
  119. int32_t GetSpinButtonForPointerEvent(WidgetGUIEvent* aEvent) const;
  120. void SpinnerStateChanged() const;
  121. bool SpinnerUpButtonIsDepressed() const;
  122. bool SpinnerDownButtonIsDepressed() const;
  123. bool IsFocused() const;
  124. void HandleFocusEvent(WidgetEvent* aEvent);
  125. /**
  126. * Our element had HTMLInputElement::Select() called on it.
  127. */
  128. nsresult HandleSelectCall();
  129. virtual Element* GetPseudoElement(CSSPseudoElementType aType) override;
  130. bool ShouldUseNativeStyleForSpinner() const;
  131. private:
  132. nsITextControlFrame* GetTextFieldFrame();
  133. nsresult MakeAnonymousElement(Element** aResult,
  134. nsTArray<ContentInfo>& aElements,
  135. nsIAtom* aTagName,
  136. CSSPseudoElementType aPseudoType);
  137. class SyncDisabledStateEvent;
  138. friend class SyncDisabledStateEvent;
  139. class SyncDisabledStateEvent : public mozilla::Runnable
  140. {
  141. public:
  142. explicit SyncDisabledStateEvent(nsNumberControlFrame* aFrame)
  143. : mFrame(aFrame)
  144. {}
  145. NS_IMETHOD Run() override
  146. {
  147. nsNumberControlFrame* frame =
  148. static_cast<nsNumberControlFrame*>(mFrame.GetFrame());
  149. NS_ENSURE_STATE(frame);
  150. frame->SyncDisabledState();
  151. return NS_OK;
  152. }
  153. private:
  154. nsWeakFrame mFrame;
  155. };
  156. /**
  157. * Sync the disabled state of the anonymous children up with our content's.
  158. */
  159. void SyncDisabledState();
  160. /**
  161. * The text field used to edit and show the number.
  162. * @see nsNumberControlFrame::CreateAnonymousContent.
  163. */
  164. nsCOMPtr<Element> mOuterWrapper;
  165. nsCOMPtr<Element> mTextField;
  166. nsCOMPtr<Element> mSpinBox;
  167. nsCOMPtr<Element> mSpinUp;
  168. nsCOMPtr<Element> mSpinDown;
  169. bool mHandlingInputEvent;
  170. };
  171. #endif // nsNumberControlFrame_h__