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/. */
6 
7 #include "nsImageFrame.h"
8 
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"
17 
18 using namespace mozilla;
19 
20 class nsImageControlFrame final : public nsImageFrame,
21                                   public nsIFormControlFrame {
22  public:
23   explicit nsImageControlFrame(ComputedStyle* aStyle,
24                                nsPresContext* aPresContext);
25   ~nsImageControlFrame() final;
26 
27   void Init(nsIContent* aContent, nsContainerFrame* aParent,
28             nsIFrame* aPrevInFlow) final;
29 
30   NS_DECL_QUERYFRAME
31   NS_DECL_FRAMEARENA_HELPERS(nsImageControlFrame)
32 
33   void Reflow(nsPresContext*, ReflowOutput&, const ReflowInput&,
34               nsReflowStatus&) final;
35 
36   nsresult HandleEvent(nsPresContext*, WidgetGUIEvent*, nsEventStatus*) final;
37 
38 #ifdef ACCESSIBILITY
39   mozilla::a11y::AccType AccessibleType() final;
40 #endif
41 
42 #ifdef DEBUG_FRAME_DUMP
GetFrameName(nsAString & aResult) const43   nsresult GetFrameName(nsAString& aResult) const final {
44     return MakeFrameName(u"ImageControl"_ns, aResult);
45   }
46 #endif
47 
48   Maybe<Cursor> GetCursor(const nsPoint&) final;
49 
50   // nsIFormContromFrame
51   void SetFocus(bool aOn, bool aRepaint) final;
52   nsresult SetFormProperty(nsAtom* aName, const nsAString& aValue) final;
53 };
54 
nsImageControlFrame(ComputedStyle * aStyle,nsPresContext * aPresContext)55 nsImageControlFrame::nsImageControlFrame(ComputedStyle* aStyle,
56                                          nsPresContext* aPresContext)
57     : nsImageFrame(aStyle, aPresContext, kClassID) {}
58 
59 nsImageControlFrame::~nsImageControlFrame() = default;
60 
NS_NewImageControlFrame(PresShell * aPresShell,ComputedStyle * aStyle)61 nsIFrame* NS_NewImageControlFrame(PresShell* aPresShell,
62                                   ComputedStyle* aStyle) {
63   return new (aPresShell)
64       nsImageControlFrame(aStyle, aPresShell->GetPresContext());
65 }
66 
NS_IMPL_FRAMEARENA_HELPERS(nsImageControlFrame)67 NS_IMPL_FRAMEARENA_HELPERS(nsImageControlFrame)
68 
69 void nsImageControlFrame::Init(nsIContent* aContent, nsContainerFrame* aParent,
70                                nsIFrame* aPrevInFlow) {
71   nsImageFrame::Init(aContent, aParent, aPrevInFlow);
72 
73   if (aPrevInFlow) {
74     return;
75   }
76 
77   mContent->SetProperty(nsGkAtoms::imageClickedPoint, new nsIntPoint(0, 0),
78                         nsINode::DeleteProperty<nsIntPoint>);
79 }
80 
81 NS_QUERYFRAME_HEAD(nsImageControlFrame)
NS_QUERYFRAME_ENTRY(nsIFormControlFrame)82   NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
83 NS_QUERYFRAME_TAIL_INHERITING(nsImageFrame)
84 
85 #ifdef ACCESSIBILITY
86 a11y::AccType nsImageControlFrame::AccessibleType() {
87   if (mContent->IsAnyOfHTMLElements(nsGkAtoms::button, nsGkAtoms::input)) {
88     return a11y::eHTMLButtonType;
89   }
90 
91   return a11y::eNoType;
92 }
93 #endif
94 
Reflow(nsPresContext * aPresContext,ReflowOutput & aDesiredSize,const ReflowInput & aReflowInput,nsReflowStatus & aStatus)95 void nsImageControlFrame::Reflow(nsPresContext* aPresContext,
96                                  ReflowOutput& aDesiredSize,
97                                  const ReflowInput& aReflowInput,
98                                  nsReflowStatus& aStatus) {
99   DO_GLOBAL_REFLOW_COUNT("nsImageControlFrame");
100   DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
101   MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
102   return nsImageFrame::Reflow(aPresContext, aDesiredSize, aReflowInput,
103                               aStatus);
104 }
105 
HandleEvent(nsPresContext * aPresContext,WidgetGUIEvent * aEvent,nsEventStatus * aEventStatus)106 nsresult nsImageControlFrame::HandleEvent(nsPresContext* aPresContext,
107                                           WidgetGUIEvent* aEvent,
108                                           nsEventStatus* aEventStatus) {
109   NS_ENSURE_ARG_POINTER(aEventStatus);
110 
111   // Don't do anything if the event has already been handled by someone
112   if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
113     return NS_OK;
114   }
115 
116   if (IsContentDisabled()) {
117     return nsIFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
118   }
119 
120   *aEventStatus = nsEventStatus_eIgnore;
121 
122   if (aEvent->mMessage == eMouseUp &&
123       aEvent->AsMouseEvent()->mButton == MouseButton::ePrimary) {
124     // Store click point for HTMLInputElement::SubmitNamesValues
125     // Do this on MouseUp because the specs don't say and that's what IE does
126     nsIntPoint* lastClickPoint = static_cast<nsIntPoint*>(
127         mContent->GetProperty(nsGkAtoms::imageClickedPoint));
128     if (lastClickPoint) {
129       // normally lastClickedPoint is not null, as it's allocated in Init()
130       nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(
131           aEvent, RelativeTo{this});
132       TranslateEventCoords(pt, *lastClickPoint);
133     }
134   }
135   return nsImageFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
136 }
137 
SetFocus(bool aOn,bool aRepaint)138 void nsImageControlFrame::SetFocus(bool aOn, bool aRepaint) {}
139 
GetCursor(const nsPoint &)140 Maybe<nsIFrame::Cursor> nsImageControlFrame::GetCursor(const nsPoint&) {
141   StyleCursorKind kind = StyleUI()->mCursor.keyword;
142   if (kind == StyleCursorKind::Auto) {
143     kind = StyleCursorKind::Pointer;
144   }
145   return Some(Cursor{kind, AllowCustomCursorImage::Yes});
146 }
147 
SetFormProperty(nsAtom * aName,const nsAString & aValue)148 nsresult nsImageControlFrame::SetFormProperty(nsAtom* aName,
149                                               const nsAString& aValue) {
150   return NS_OK;
151 }
152