Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / layout / forms / nsImageControlFrame.cpp
blob1bc00dc58c0f193e0c8f6ec5082199e2e04977eb
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
37 #include "nsCOMPtr.h"
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"
58 #ifdef ACCESSIBILITY
59 #include "nsAccessibilityService.h"
60 #endif
62 void
63 IntPointDtorFunc(void *aObject, nsIAtom *aPropertyName,
64 void *aPropertyValue, void *aData)
66 nsIntPoint *propertyValue = static_cast<nsIntPoint*>(aPropertyValue);
67 delete propertyValue;
71 #define nsImageControlFrameSuper nsImageFrame
72 class nsImageControlFrame : public nsImageControlFrameSuper,
73 public nsIFormControlFrame
75 public:
76 nsImageControlFrame(nsStyleContext* aContext);
77 ~nsImageControlFrame();
79 virtual void DestroyFrom(nsIFrame* aDestructRoot);
80 NS_IMETHOD Init(nsIContent* aContent,
81 nsIFrame* aParent,
82 nsIFrame* aPrevInFlow);
84 NS_DECL_QUERYFRAME
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,
93 nsGUIEvent* aEvent,
94 nsEventStatus* aEventStatus);
96 virtual nsIAtom* GetType() const;
98 #ifdef ACCESSIBILITY
99 virtual already_AddRefed<nsAccessible> CreateAccessible();
100 #endif
102 #ifdef DEBUG
103 NS_IMETHOD GetFrameName(nsAString& aResult) const {
104 return MakeFrameName(NS_LITERAL_STRING("ImageControl"), aResult);
106 #endif
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()
126 void
127 nsImageControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
129 if (!GetPrevInFlow()) {
130 nsFormControlFrame::RegUnRegAccessKey(this, PR_FALSE);
132 nsImageControlFrameSuper::DestroyFrom(aDestructRoot);
135 nsIFrame*
136 NS_NewImageControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
138 return new (aPresShell) nsImageControlFrame(aContext);
141 NS_IMPL_FRAMEARENA_HELPERS(nsImageControlFrame)
143 NS_IMETHODIMP
144 nsImageControlFrame::Init(nsIContent* aContent,
145 nsIFrame* aParent,
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
153 if (aPrevInFlow) {
154 return NS_OK;
157 return mContent->SetProperty(nsGkAtoms::imageClickedPoint,
158 new nsIntPoint(0, 0),
159 IntPointDtorFunc);
162 NS_QUERYFRAME_HEAD(nsImageControlFrame)
163 NS_QUERYFRAME_ENTRY(nsIFormControlFrame)
164 NS_QUERYFRAME_TAIL_INHERITING(nsImageControlFrameSuper)
166 #ifdef ACCESSIBILITY
167 already_AddRefed<nsAccessible>
168 nsImageControlFrame::CreateAccessible()
170 nsAccessibilityService* accService = nsIPresShell::AccService();
171 if (accService) {
172 if (mContent->Tag() == nsGkAtoms::button) {
173 return accService->CreateHTML4ButtonAccessible(mContent, PresContext()->PresShell());
175 else if (mContent->Tag() == nsGkAtoms::input) {
176 return accService->CreateHTMLButtonAccessible(mContent, PresContext()->PresShell());
180 return nsnull;
182 #endif
184 nsIAtom*
185 nsImageControlFrame::GetType() const
187 return nsGkAtoms::imageControlFrame;
190 NS_METHOD
191 nsImageControlFrame::Reflow(nsPresContext* aPresContext,
192 nsHTMLReflowMetrics& aDesiredSize,
193 const nsHTMLReflowState& aReflowState,
194 nsReflowStatus& aStatus)
196 DO_GLOBAL_REFLOW_COUNT("nsImageControlFrame");
197 DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
198 if (!GetPrevInFlow() && (mState & NS_FRAME_FIRST_REFLOW)) {
199 nsFormControlFrame::RegUnRegAccessKey(this, PR_TRUE);
201 return nsImageControlFrameSuper::Reflow(aPresContext, aDesiredSize, aReflowState, aStatus);
204 NS_METHOD
205 nsImageControlFrame::HandleEvent(nsPresContext* aPresContext,
206 nsGUIEvent* aEvent,
207 nsEventStatus* aEventStatus)
209 NS_ENSURE_ARG_POINTER(aEventStatus);
211 // Don't do anything if the event has already been handled by someone
212 if (nsEventStatus_eConsumeNoDefault == *aEventStatus) {
213 return NS_OK;
216 // do we have user-input style?
217 const nsStyleUserInterface* uiStyle = GetStyleUserInterface();
218 if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
219 return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
221 if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::disabled)) { // XXX cache disabled
222 return NS_OK;
225 *aEventStatus = nsEventStatus_eIgnore;
227 if (aEvent->eventStructType == NS_MOUSE_EVENT &&
228 aEvent->message == NS_MOUSE_BUTTON_UP &&
229 static_cast<nsMouseEvent*>(aEvent)->button == nsMouseEvent::eLeftButton) {
230 // Store click point for nsHTMLInputElement::SubmitNamesValues
231 // Do this on MouseUp because the specs don't say and that's what IE does
232 nsIntPoint* lastClickPoint =
233 static_cast<nsIntPoint*>
234 (mContent->GetProperty(nsGkAtoms::imageClickedPoint));
235 if (lastClickPoint) {
236 // normally lastClickedPoint is not null, as it's allocated in Init()
237 nsPoint pt = nsLayoutUtils::GetEventCoordinatesRelativeTo(aEvent, this);
238 TranslateEventCoords(pt, *lastClickPoint);
241 return nsImageControlFrameSuper::HandleEvent(aPresContext, aEvent,
242 aEventStatus);
245 void
246 nsImageControlFrame::SetFocus(PRBool aOn, PRBool aRepaint)
250 NS_IMETHODIMP
251 nsImageControlFrame::GetCursor(const nsPoint& aPoint,
252 nsIFrame::Cursor& aCursor)
254 // Use style defined cursor if one is provided, otherwise when
255 // the cursor style is "auto" we use the pointer cursor.
256 FillCursorInformationFromStyle(GetStyleUserInterface(), aCursor);
258 if (NS_STYLE_CURSOR_AUTO == aCursor.mCursor) {
259 aCursor.mCursor = NS_STYLE_CURSOR_POINTER;
262 return NS_OK;
265 nsresult
266 nsImageControlFrame::SetFormProperty(nsIAtom* aName,
267 const nsAString& aValue)
269 return NS_OK;
272 nsresult
273 nsImageControlFrame::GetFormProperty(nsIAtom* aName,
274 nsAString& aValue) const
276 aValue.Truncate();
277 return NS_OK;