1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsImageFrame.h"
39 #include "nsIFormControlFrame.h"
40 #include "nsIFormControl.h"
41 #include "nsHTMLParts.h"
42 #include "nsIRenderingContext.h"
43 #include "nsPresContext.h"
44 #include "nsIPresShell.h"
45 #include "nsStyleContext.h"
46 #include "nsLeafFrame.h"
47 #include "nsCSSRendering.h"
48 #include "nsISupports.h"
49 #include "nsGkAtoms.h"
50 #include "nsIDeviceContext.h"
51 #include "nsIFontMetrics.h"
52 #include "nsStyleConsts.h"
53 #include "nsFormControlFrame.h"
54 #include "nsGUIEvent.h"
55 #include "nsIServiceManager.h"
56 #include "nsContainerFrame.h"
57 #include "nsLayoutUtils.h"
59 #include "nsIAccessibilityService.h"
63 IntPointDtorFunc(void *aObject
, nsIAtom
*aPropertyName
,
64 void *aPropertyValue
, void *aData
)
66 nsIntPoint
*propertyValue
= static_cast<nsIntPoint
*>(aPropertyValue
);
71 #define nsImageControlFrameSuper nsImageFrame
72 class nsImageControlFrame
: public nsImageControlFrameSuper
,
73 public nsIFormControlFrame
76 nsImageControlFrame(nsStyleContext
* aContext
);
77 ~nsImageControlFrame();
79 virtual void DestroyFrom(nsIFrame
* aDestructRoot
);
80 NS_IMETHOD
Init(nsIContent
* aContent
,
82 nsIFrame
* aPrevInFlow
);
85 NS_DECL_FRAMEARENA_HELPERS
87 NS_IMETHOD
Reflow(nsPresContext
* aPresContext
,
88 nsHTMLReflowMetrics
& aDesiredSize
,
89 const nsHTMLReflowState
& aReflowState
,
90 nsReflowStatus
& aStatus
);
92 NS_IMETHOD
HandleEvent(nsPresContext
* aPresContext
,
94 nsEventStatus
* aEventStatus
);
96 virtual nsIAtom
* GetType() const;
99 virtual already_AddRefed
<nsAccessible
> CreateAccessible();
103 NS_IMETHOD
GetFrameName(nsAString
& aResult
) const {
104 return MakeFrameName(NS_LITERAL_STRING("ImageControl"), aResult
);
108 NS_IMETHOD
GetCursor(const nsPoint
& aPoint
,
109 nsIFrame::Cursor
& aCursor
);
110 // nsIFormContromFrame
111 virtual void SetFocus(PRBool aOn
, PRBool aRepaint
);
112 virtual nsresult
SetFormProperty(nsIAtom
* aName
, const nsAString
& aValue
);
113 virtual nsresult
GetFormProperty(nsIAtom
* aName
, nsAString
& aValue
) const;
117 nsImageControlFrame::nsImageControlFrame(nsStyleContext
* aContext
):
118 nsImageControlFrameSuper(aContext
)
122 nsImageControlFrame::~nsImageControlFrame()
127 nsImageControlFrame::DestroyFrom(nsIFrame
* aDestructRoot
)
129 if (!GetPrevInFlow()) {
130 nsFormControlFrame::RegUnRegAccessKey(this, PR_FALSE
);
132 nsImageControlFrameSuper::DestroyFrom(aDestructRoot
);
136 NS_NewImageControlFrame(nsIPresShell
* aPresShell
, nsStyleContext
* aContext
)
138 return new (aPresShell
) nsImageControlFrame(aContext
);
141 NS_IMPL_FRAMEARENA_HELPERS(nsImageControlFrame
)
144 nsImageControlFrame::Init(nsIContent
* aContent
,
146 nsIFrame
* aPrevInFlow
)
148 nsresult rv
= nsImageControlFrameSuper::Init(aContent
, aParent
, aPrevInFlow
);
149 NS_ENSURE_SUCCESS(rv
, rv
);
151 // nsIntPoint allocation can fail, in which case we just set the property
152 // to null, which is safe
157 return mContent
->SetProperty(nsGkAtoms::imageClickedPoint
,
158 new nsIntPoint(0, 0),
162 NS_QUERYFRAME_HEAD(nsImageControlFrame
)
163 NS_QUERYFRAME_ENTRY(nsIFormControlFrame
)
164 NS_QUERYFRAME_TAIL_INHERITING(nsImageControlFrameSuper
)
167 already_AddRefed
<nsAccessible
>
168 nsImageControlFrame::CreateAccessible()
170 nsCOMPtr
<nsIAccessibilityService
> accService
= do_GetService("@mozilla.org/accessibilityService;1");
173 if (mContent
->Tag() == nsGkAtoms::button
) {
174 return accService
->CreateHTML4ButtonAccessible(mContent
, PresContext()->PresShell());
176 else if (mContent
->Tag() == nsGkAtoms::input
) {
177 return accService
->CreateHTMLButtonAccessible(mContent
, PresContext()->PresShell());
186 nsImageControlFrame::GetType() const
188 return nsGkAtoms::imageControlFrame
;
192 nsImageControlFrame::Reflow(nsPresContext
* aPresContext
,
193 nsHTMLReflowMetrics
& aDesiredSize
,
194 const nsHTMLReflowState
& aReflowState
,
195 nsReflowStatus
& aStatus
)
197 DO_GLOBAL_REFLOW_COUNT("nsImageControlFrame");
198 DISPLAY_REFLOW(aPresContext
, this, aReflowState
, aDesiredSize
, aStatus
);
199 if (!GetPrevInFlow() && (mState
& NS_FRAME_FIRST_REFLOW
)) {
200 nsFormControlFrame::RegUnRegAccessKey(this, PR_TRUE
);
202 return nsImageControlFrameSuper::Reflow(aPresContext
, aDesiredSize
, aReflowState
, aStatus
);
206 nsImageControlFrame::HandleEvent(nsPresContext
* aPresContext
,
208 nsEventStatus
* aEventStatus
)
210 NS_ENSURE_ARG_POINTER(aEventStatus
);
212 // Don't do anything if the event has already been handled by someone
213 if (nsEventStatus_eConsumeNoDefault
== *aEventStatus
) {
217 // do we have user-input style?
218 const nsStyleUserInterface
* uiStyle
= GetStyleUserInterface();
219 if (uiStyle
->mUserInput
== NS_STYLE_USER_INPUT_NONE
|| uiStyle
->mUserInput
== NS_STYLE_USER_INPUT_DISABLED
)
220 return nsFrame::HandleEvent(aPresContext
, aEvent
, aEventStatus
);
222 if (mContent
->HasAttr(kNameSpaceID_None
, nsGkAtoms::disabled
)) { // XXX cache disabled
226 *aEventStatus
= nsEventStatus_eIgnore
;
228 if (aEvent
->eventStructType
== NS_MOUSE_EVENT
&&
229 aEvent
->message
== NS_MOUSE_BUTTON_UP
&&
230 static_cast<nsMouseEvent
*>(aEvent
)->button
== nsMouseEvent::eLeftButton
) {
231 // Store click point for nsHTMLInputElement::SubmitNamesValues
232 // Do this on MouseUp because the specs don't say and that's what IE does
233 nsIntPoint
* lastClickPoint
=
234 static_cast<nsIntPoint
*>
235 (mContent
->GetProperty(nsGkAtoms::imageClickedPoint
));
236 if (lastClickPoint
) {
237 // normally lastClickedPoint is not null, as it's allocated in Init()
238 nsPoint pt
= nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent
, this);
239 TranslateEventCoords(pt
, *lastClickPoint
);
242 return nsImageControlFrameSuper::HandleEvent(aPresContext
, aEvent
,
247 nsImageControlFrame::SetFocus(PRBool aOn
, PRBool aRepaint
)
252 nsImageControlFrame::GetCursor(const nsPoint
& aPoint
,
253 nsIFrame::Cursor
& aCursor
)
255 // Use style defined cursor if one is provided, otherwise when
256 // the cursor style is "auto" we use the pointer cursor.
257 FillCursorInformationFromStyle(GetStyleUserInterface(), aCursor
);
259 if (NS_STYLE_CURSOR_AUTO
== aCursor
.mCursor
) {
260 aCursor
.mCursor
= NS_STYLE_CURSOR_POINTER
;
267 nsImageControlFrame::SetFormProperty(nsIAtom
* aName
,
268 const nsAString
& aValue
)
274 nsImageControlFrame::GetFormProperty(nsIAtom
* aName
,
275 nsAString
& aValue
) const