nsImageControlFrame.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "nsImageFrame.h"
  6. #include "nsIFormControlFrame.h"
  7. #include "nsPresContext.h"
  8. #include "nsGkAtoms.h"
  9. #include "nsStyleConsts.h"
  10. #include "nsFormControlFrame.h"
  11. #include "nsLayoutUtils.h"
  12. #include "mozilla/MouseEvents.h"
  13. #include "nsIContent.h"
  14. using namespace mozilla;
  15. class nsImageControlFrame : public nsImageFrame,
  16. public nsIFormControlFrame
  17. {
  18. public:
  19. explicit nsImageControlFrame(nsStyleContext* aContext);
  20. ~nsImageControlFrame();
  21. virtual void DestroyFrom(nsIFrame* aDestructRoot) override;
  22. virtual void Init(nsIContent* aContent,
  23. nsContainerFrame* aParent,
  24. nsIFrame* aPrevInFlow) override;
  25. NS_DECL_QUERYFRAME
  26. NS_DECL_FRAMEARENA_HELPERS
  27. virtual void Reflow(nsPresContext* aPresContext,
  28. ReflowOutput& aDesiredSize,
  29. const ReflowInput& aReflowInput,
  30. nsReflowStatus& aStatus) override;
  31. virtual nsresult HandleEvent(nsPresContext* aPresContext,
  32. WidgetGUIEvent* aEvent,
  33. nsEventStatus* aEventStatus) override;
  34. virtual nsIAtom* GetType() const override;
  35. #ifdef ACCESSIBILITY
  36. virtual mozilla::a11y::AccType AccessibleType() override;
  37. #endif
  38. #ifdef DEBUG_FRAME_DUMP
  39. virtual nsresult GetFrameName(nsAString& aResult) const override {
  40. return MakeFrameName(NS_LITERAL_STRING("ImageControl"), aResult);
  41. }
  42. #endif
  43. virtual nsresult GetCursor(const nsPoint& aPoint,
  44. nsIFrame::Cursor& aCursor) override;
  45. // nsIFormContromFrame
  46. virtual void SetFocus(bool aOn, bool aRepaint) override;
  47. virtual nsresult SetFormProperty(nsIAtom* aName,
  48. const nsAString& aValue) override;
  49. };
  50. nsImageControlFrame::nsImageControlFrame(nsStyleContext* aContext)
  51. : nsImageFrame(aContext)
  52. {
  53. }
  54. nsImageControlFrame::~nsImageControlFrame()
  55. {
  56. }
  57. void
  58. nsImageControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
  59. {
  60. if (!GetPrevInFlow()) {
  61. nsFormControlFrame::RegUnRegAccessKey(this, false);
  62. }
  63. nsImageFrame::DestroyFrom(aDestructRoot);
  64. }
  65. nsIFrame*
  66. NS_NewImageControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
  67. {
  68. return new (aPresShell) nsImageControlFrame(aContext);
  69. }
  70. NS_IMPL_FRAMEARENA_HELPERS(nsImageControlFrame)
  71. void
  72. nsImageControlFrame::Init(nsIContent* aContent,
  73. nsContainerFrame* aParent,
  74. nsIFrame* aPrevInFlow)
  75. {
  76. nsImageFrame::Init(aContent, aParent, aPrevInFlow);
  77. if (aPrevInFlow) {
  78. return;
  79. }
  80. mContent->SetProperty(nsGkAtoms::imageClickedPoint,
  81. new nsIntPoint(0, 0),
  82. nsINode::DeleteProperty<nsIntPoint>);
  83. }
  84. NS_QUERYFRAME_HEAD(nsImageControlFrame)
  85. NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
  86. NS_QUERYFRAME_TAIL_INHERITING(nsImageFrame)
  87. #ifdef ACCESSIBILITY
  88. a11y::AccType
  89. nsImageControlFrame::AccessibleType()
  90. {
  91. if (mContent->IsAnyOfHTMLElements(nsGkAtoms::button, nsGkAtoms::input)) {
  92. return a11y::eHTMLButtonType;
  93. }
  94. return a11y::eNoType;
  95. }
  96. #endif
  97. nsIAtom*
  98. nsImageControlFrame::GetType() const
  99. {
  100. return nsGkAtoms::imageControlFrame;
  101. }
  102. void
  103. nsImageControlFrame::Reflow(nsPresContext* aPresContext,
  104. ReflowOutput& aDesiredSize,
  105. const ReflowInput& aReflowInput,
  106. nsReflowStatus& aStatus)
  107. {
  108. DO_GLOBAL_REFLOW_COUNT("nsImageControlFrame");
  109. DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
  110. if (!GetPrevInFlow() && (mState & NS_FRAME_FIRST_REFLOW)) {
  111. nsFormControlFrame::RegUnRegAccessKey(this, true);
  112. }
  113. return nsImageFrame::Reflow(aPresContext, aDesiredSize, aReflowInput, aStatus);
  114. }
  115. nsresult
  116. nsImageControlFrame::HandleEvent(nsPresContext* aPresContext,
  117. WidgetGUIEvent* aEvent,
  118. nsEventStatus* aEventStatus)
  119. {
  120. NS_ENSURE_ARG_POINTER(aEventStatus);
  121. // Don't do anything if the event has already been handled by someone
  122. if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
  123. return NS_OK;
  124. }
  125. // do we have user-input style?
  126. const nsStyleUserInterface* uiStyle = StyleUserInterface();
  127. if (uiStyle->mUserInput == StyleUserInput::None ||
  128. uiStyle->mUserInput == StyleUserInput::Disabled) {
  129. return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  130. }
  131. if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) { // XXX cache disabled
  132. return NS_OK;
  133. }
  134. *aEventStatus = nsEventStatus_eIgnore;
  135. if (aEvent->mMessage == eMouseUp &&
  136. aEvent->AsMouseEvent()->button == WidgetMouseEvent::eLeftButton) {
  137. // Store click point for HTMLInputElement::SubmitNamesValues
  138. // Do this on MouseUp because the specs don't say and that's what IE does
  139. nsIntPoint* lastClickPoint =
  140. static_cast<nsIntPoint*>
  141. (mContent->GetProperty(nsGkAtoms::imageClickedPoint));
  142. if (lastClickPoint) {
  143. // normally lastClickedPoint is not null, as it's allocated in Init()
  144. nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
  145. TranslateEventCoords(pt, *lastClickPoint);
  146. }
  147. }
  148. return nsImageFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
  149. }
  150. void
  151. nsImageControlFrame::SetFocus(bool aOn, bool aRepaint)
  152. {
  153. }
  154. nsresult
  155. nsImageControlFrame::GetCursor(const nsPoint& aPoint,
  156. nsIFrame::Cursor& aCursor)
  157. {
  158. // Use style defined cursor if one is provided, otherwise when
  159. // the cursor style is "auto" we use the pointer cursor.
  160. FillCursorInformationFromStyle(StyleUserInterface(), aCursor);
  161. if (NS_STYLE_CURSOR_AUTO == aCursor.mCursor) {
  162. aCursor.mCursor = NS_STYLE_CURSOR_POINTER;
  163. }
  164. return NS_OK;
  165. }
  166. nsresult
  167. nsImageControlFrame::SetFormProperty(nsIAtom* aName,
  168. const nsAString& aValue)
  169. {
  170. return NS_OK;
  171. }