Fix bogus assertion left in bug 463334.
[mozilla-central.git] / layout / forms / nsGfxButtonControlFrame.cpp
blob40e5eeb6d5eeec501c4e4afc2b2bc2c109b1866f
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 ***** */
38 #include "nsGfxButtonControlFrame.h"
39 #include "nsWidgetsCID.h"
40 #include "nsIFontMetrics.h"
41 #include "nsFormControlFrame.h"
42 #include "nsIFormControl.h"
43 #include "nsINameSpaceManager.h"
44 #ifdef ACCESSIBILITY
45 #include "nsIAccessibilityService.h"
46 #endif
47 #include "nsIServiceManager.h"
48 #include "nsIDOMNode.h"
49 #include "nsGkAtoms.h"
50 #include "nsAutoPtr.h"
51 #include "nsStyleSet.h"
52 #include "nsContentUtils.h"
53 // MouseEvent suppression in PP
54 #include "nsGUIEvent.h"
55 #include "nsContentCreatorFunctions.h"
57 #include "nsNodeInfoManager.h"
58 #include "nsIDOMHTMLInputElement.h"
60 const nscoord kSuggestedNotSet = -1;
62 nsGfxButtonControlFrame::nsGfxButtonControlFrame(nsStyleContext* aContext):
63 nsHTMLButtonControlFrame(aContext)
67 nsIFrame*
68 NS_NewGfxButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
70 return new (aPresShell) nsGfxButtonControlFrame(aContext);
73 void nsGfxButtonControlFrame::Destroy()
75 nsContentUtils::DestroyAnonymousContent(&mTextContent);
76 nsHTMLButtonControlFrame::Destroy();
79 nsIAtom*
80 nsGfxButtonControlFrame::GetType() const
82 return nsGkAtoms::gfxButtonControlFrame;
85 // Special check for the browse button of a file input.
87 // We'll return PR_TRUE if type is NS_FORM_INPUT_BUTTON and our parent
88 // is a file input.
89 PRBool
90 nsGfxButtonControlFrame::IsFileBrowseButton(PRInt32 type)
92 PRBool rv = PR_FALSE;
93 if (NS_FORM_INPUT_BUTTON == type) {
94 // Check to see if parent is a file input
95 nsCOMPtr<nsIFormControl> formCtrl =
96 do_QueryInterface(mContent->GetParent());
98 rv = formCtrl && formCtrl->GetType() == NS_FORM_INPUT_FILE;
100 return rv;
103 #ifdef DEBUG
104 NS_IMETHODIMP
105 nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const
107 return MakeFrameName(NS_LITERAL_STRING("ButtonControl"), aResult);
109 #endif
111 // Create the text content used as label for the button.
112 // The frame will be generated by the frame constructor.
113 nsresult
114 nsGfxButtonControlFrame::CreateAnonymousContent(nsTArray<nsIContent*>& aElements)
116 nsXPIDLString label;
117 GetLabel(label);
119 // Add a child text content node for the label
120 NS_NewTextNode(getter_AddRefs(mTextContent),
121 mContent->NodeInfo()->NodeInfoManager());
122 if (!mTextContent)
123 return NS_ERROR_OUT_OF_MEMORY;
125 // set the value of the text node and add it to the child list
126 mTextContent->SetText(label, PR_FALSE);
127 if (!aElements.AppendElement(mTextContent))
128 return NS_ERROR_OUT_OF_MEMORY;
129 return NS_OK;
132 // Create the text content used as label for the button.
133 // The frame will be generated by the frame constructor.
134 nsIFrame*
135 nsGfxButtonControlFrame::CreateFrameFor(nsIContent* aContent)
137 nsIFrame * newFrame = nsnull;
139 if (aContent == mTextContent) {
140 nsIFrame * parentFrame = mFrames.FirstChild();
142 nsPresContext* presContext = PresContext();
143 nsRefPtr<nsStyleContext> textStyleContext;
144 textStyleContext = presContext->StyleSet()->
145 ResolveStyleForNonElement(mStyleContext);
147 if (textStyleContext) {
148 newFrame = NS_NewTextFrame(presContext->PresShell(), textStyleContext);
149 if (newFrame) {
150 // initialize the text frame
151 newFrame->Init(mTextContent, parentFrame, nsnull);
152 newFrame->SetInitialChildList(nsnull, nsnull);
157 return newFrame;
160 nsresult
161 nsGfxButtonControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
163 nsresult rv = NS_OK;
164 if (nsGkAtoms::defaultLabel == aName) {
165 // This property is used by accessibility to get
166 // the default label of the button.
167 nsXPIDLString temp;
168 rv = const_cast<nsGfxButtonControlFrame*>(this)->GetDefaultLabel(temp);
169 aValue = temp;
170 } else {
171 aValue.Truncate();
173 return rv;
176 #ifdef ACCESSIBILITY
177 NS_IMETHODIMP nsGfxButtonControlFrame::GetAccessible(nsIAccessible** aAccessible)
179 nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
181 if (accService) {
182 return accService->CreateHTMLButtonAccessible(static_cast<nsIFrame*>(this), aAccessible);
185 return NS_ERROR_FAILURE;
187 #endif
189 // Frames are not refcounted, no need to AddRef
190 NS_IMETHODIMP
191 nsGfxButtonControlFrame::QueryInterface(const nsIID& aIID, void** aInstancePtr)
193 NS_PRECONDITION(aInstancePtr, "null out param");
195 if (aIID.Equals(NS_GET_IID(nsIAnonymousContentCreator))) {
196 *aInstancePtr = static_cast<nsIAnonymousContentCreator*>(this);
197 return NS_OK;
200 return nsHTMLButtonControlFrame::QueryInterface(aIID, aInstancePtr);
203 // Initially we hardcoded the default strings here.
204 // Next, we used html.css to store the default label for various types
205 // of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
206 // However, since html.css is not internationalized, we now grab the default
207 // label from a string bundle as is done for all other UI strings.
208 // See bug 16999 for further details.
209 nsresult
210 nsGfxButtonControlFrame::GetDefaultLabel(nsXPIDLString& aString)
212 nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
213 NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
215 PRInt32 type = form->GetType();
216 const char *prop;
217 if (type == NS_FORM_INPUT_RESET) {
218 prop = "Reset";
220 else if (type == NS_FORM_INPUT_SUBMIT) {
221 prop = "Submit";
223 else if (IsFileBrowseButton(type)) {
224 prop = "Browse";
226 else {
227 aString.Truncate();
228 return NS_OK;
231 return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
232 prop, aString);
235 nsresult
236 nsGfxButtonControlFrame::GetLabel(nsXPIDLString& aLabel)
238 // Get the text from the "value" property on our content if there is
239 // one; otherwise set it to a default value (localized).
240 nsresult rv;
241 nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
242 if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value) && elt) {
243 rv = elt->GetValue(aLabel);
244 } else {
245 // Generate localized label.
246 // We can't make any assumption as to what the default would be
247 // because the value is localized for non-english platforms, thus
248 // it might not be the string "Reset", "Submit Query", or "Browse..."
249 rv = GetDefaultLabel(aLabel);
252 NS_ENSURE_SUCCESS(rv, rv);
254 // Compress whitespace out of label if needed.
255 if (!GetStyleText()->WhiteSpaceIsSignificant()) {
256 aLabel.CompressWhitespace();
257 } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
258 aLabel.CharAt(aLabel.Length() - 1) == ' ') {
259 // This is a bit of a hack. The reason this is here is as follows: we now
260 // have default padding on our buttons to make them non-ugly.
261 // Unfortunately, IE-windows does not have such padding, so people will
262 // stick values like " ok " (with the spaces) in the buttons in an attempt
263 // to make them look decent. Unfortunately, if they do this the button
264 // looks way too big in Mozilla. Worse yet, if they do this _and_ set a
265 // fixed width for the button we run into trouble because our focus-rect
266 // border/padding and outer border take up 10px of the horizontal button
267 // space or so; the result is that the text is misaligned, even with the
268 // recentering we do in nsHTMLButtonFrame::Reflow. So to solve this, even
269 // if the whitespace is significant, single leading and trailing _spaces_
270 // (and not other whitespace) are removed. The proper solution, of
271 // course, is to not have the focus rect painting taking up 6px of
272 // horizontal space. We should do that instead (via XBL form controls or
273 // changing the renderer) and remove this.
274 aLabel.Cut(0, 1);
275 aLabel.Truncate(aLabel.Length() - 1);
278 return NS_OK;
281 NS_IMETHODIMP
282 nsGfxButtonControlFrame::AttributeChanged(PRInt32 aNameSpaceID,
283 nsIAtom* aAttribute,
284 PRInt32 aModType)
286 nsresult rv = NS_OK;
288 // If the value attribute is set, update the text of the label
289 if (nsGkAtoms::value == aAttribute) {
290 if (mTextContent && mContent) {
291 nsXPIDLString label;
292 rv = GetLabel(label);
293 NS_ENSURE_SUCCESS(rv, rv);
295 mTextContent->SetText(label, PR_TRUE);
296 } else {
297 rv = NS_ERROR_UNEXPECTED;
300 // defer to HTMLButtonControlFrame
301 } else {
302 rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
304 return rv;
307 PRBool
308 nsGfxButtonControlFrame::IsLeaf() const
310 return PR_TRUE;
313 nsIFrame*
314 nsGfxButtonControlFrame::GetContentInsertionFrame()
316 return this;
319 NS_IMETHODIMP
320 nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
321 nsGUIEvent* aEvent,
322 nsEventStatus* aEventStatus)
324 // Override the HandleEvent to prevent the nsFrame::HandleEvent
325 // from being called. The nsFrame::HandleEvent causes the button label
326 // to be selected (Drawn with an XOR rectangle over the label)
328 // do we have user-input style?
329 const nsStyleUserInterface* uiStyle = GetStyleUserInterface();
330 if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
331 return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
333 return NS_OK;