Bug 596094 - fix a race condition w/ipc disabled in focus related event handling...
[mozilla-central.git] / layout / forms / nsImageControlFrame.cpp
blob9dd5520ba37293e76f83e4cc8c0fcfaefc505124
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 "nsIAccessibilityService.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 nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
172 if (accService) {
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());
181 return nsnull;
183 #endif
185 nsIAtom*
186 nsImageControlFrame::GetType() const
188 return nsGkAtoms::imageControlFrame;
191 NS_METHOD
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);
205 NS_METHOD
206 nsImageControlFrame::HandleEvent(nsPresContext* aPresContext,
207 nsGUIEvent* aEvent,
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) {
214 return NS_OK;
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
223 return NS_OK;
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,
243 aEventStatus);
246 void
247 nsImageControlFrame::SetFocus(PRBool aOn, PRBool aRepaint)
251 NS_IMETHODIMP
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;
263 return NS_OK;
266 nsresult
267 nsImageControlFrame::SetFormProperty(nsIAtom* aName,
268 const nsAString& aValue)
270 return NS_OK;
273 nsresult
274 nsImageControlFrame::GetFormProperty(nsIAtom* aName,
275 nsAString& aValue) const
277 aValue.Truncate();
278 return NS_OK;