Bug 1909121 - [wpt-sync] Update web-platform-tests to 5af3e9c2a2aba76ade00f0dbc3486e5...
[gecko.git] / layout / forms / nsImageControlFrame.cpp
blob8207a427f154e2bcdaa0e500cb915e65089a37eb
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsImageFrame.h"
9 #include "mozilla/MouseEvents.h"
10 #include "mozilla/PresShell.h"
11 #include "nsIFormControlFrame.h"
12 #include "nsPresContext.h"
13 #include "nsGkAtoms.h"
14 #include "nsStyleConsts.h"
15 #include "nsLayoutUtils.h"
16 #include "nsIContent.h"
18 using namespace mozilla;
20 class nsImageControlFrame final : public nsImageFrame,
21 public nsIFormControlFrame {
22 public:
23 explicit nsImageControlFrame(ComputedStyle* aStyle,
24 nsPresContext* aPresContext);
25 ~nsImageControlFrame() final;
27 void Init(nsIContent* aContent, nsContainerFrame* aParent,
28 nsIFrame* aPrevInFlow) final;
30 NS_DECL_QUERYFRAME
31 NS_DECL_FRAMEARENA_HELPERS(nsImageControlFrame)
33 void Reflow(nsPresContext*, ReflowOutput&, const ReflowInput&,
34 nsReflowStatus&) final;
36 nsresult HandleEvent(nsPresContext*, WidgetGUIEvent*, nsEventStatus*) final;
38 #ifdef ACCESSIBILITY
39 mozilla::a11y::AccType AccessibleType() final;
40 #endif
42 #ifdef DEBUG_FRAME_DUMP
43 nsresult GetFrameName(nsAString& aResult) const final {
44 return MakeFrameName(u"ImageControl"_ns, aResult);
46 #endif
48 Cursor GetCursor(const nsPoint&) final;
50 // nsIFormContromFrame
51 void SetFocus(bool aOn, bool aRepaint) final;
52 nsresult SetFormProperty(nsAtom* aName, const nsAString& aValue) final;
55 nsImageControlFrame::nsImageControlFrame(ComputedStyle* aStyle,
56 nsPresContext* aPresContext)
57 : nsImageFrame(aStyle, aPresContext, kClassID) {}
59 nsImageControlFrame::~nsImageControlFrame() = default;
61 nsIFrame* NS_NewImageControlFrame(PresShell* aPresShell,
62 ComputedStyle* aStyle) {
63 return new (aPresShell)
64 nsImageControlFrame(aStyle, aPresShell->GetPresContext());
67 NS_IMPL_FRAMEARENA_HELPERS(nsImageControlFrame)
69 void nsImageControlFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
70 nsIFrame* aPrevInFlow) {
71 nsImageFrame::Init(aContent, aParent, aPrevInFlow);
73 if (aPrevInFlow) {
74 return;
77 mContent->SetProperty(nsGkAtoms::imageClickedPoint, new CSSIntPoint(0, 0),
78 nsINode::DeleteProperty<CSSIntPoint>);
81 NS_QUERYFRAME_HEAD(nsImageControlFrame)
82 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
83 NS_QUERYFRAME_TAIL_INHERITING(nsImageFrame)
85 #ifdef ACCESSIBILITY
86 a11y::AccType nsImageControlFrame::AccessibleType() {
87 if (mContent->IsAnyOfHTMLElements(nsGkAtoms::button, nsGkAtoms::input)) {
88 return a11y::eHTMLButtonType;
91 return a11y::eNoType;
93 #endif
95 void nsImageControlFrame::Reflow(nsPresContext* aPresContext,
96 ReflowOutput& aDesiredSize,
97 const ReflowInput& aReflowInput,
98 nsReflowStatus& aStatus) {
99 DO_GLOBAL_REFLOW_COUNT("nsImageControlFrame");
100 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
101 return nsImageFrame::Reflow(aPresContext, aDesiredSize, aReflowInput,
102 aStatus);
105 nsresult nsImageControlFrame::HandleEvent(nsPresContext* aPresContext,
106 WidgetGUIEvent* aEvent,
107 nsEventStatus* aEventStatus) {
108 NS_ENSURE_ARG_POINTER(aEventStatus);
110 // Don't do anything if the event has already been handled by someone
111 if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
112 return NS_OK;
115 if (IsContentDisabled()) {
116 return nsIFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
119 *aEventStatus = nsEventStatus_eIgnore;
121 if (aEvent->mMessage == eMouseUp &&
122 aEvent->AsMouseEvent()->mButton == MouseButton::ePrimary) {
123 // Store click point for HTMLInputElement::SubmitNamesValues
124 // Do this on MouseUp because the specs don't say and that's what IE does
125 auto* lastClickedPoint = static_cast<CSSIntPoint*>(
126 mContent->GetProperty(nsGkAtoms::imageClickedPoint));
127 if (lastClickedPoint) {
128 // normally lastClickedPoint is not null, as it's allocated in Init()
129 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
130 aEvent, RelativeTo{this});
131 *lastClickedPoint = TranslateEventCoords(pt);
134 return nsImageFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
137 void nsImageControlFrame::SetFocus(bool aOn, bool aRepaint) {}
139 nsIFrame::Cursor nsImageControlFrame::GetCursor(const nsPoint&) {
140 StyleCursorKind kind = StyleUI()->Cursor().keyword;
141 if (kind == StyleCursorKind::Auto) {
142 kind = StyleCursorKind::Pointer;
144 return Cursor{kind, AllowCustomCursorImage::Yes};
147 nsresult nsImageControlFrame::SetFormProperty(nsAtom* aName,
148 const nsAString& aValue) {
149 return NS_OK;