Backed out changeset 8366e5cc9f57 (bug 125282) because of four windows unit test...
[mozilla-central.git] / layout / forms / nsGfxButtonControlFrame.cpp
bloba41bc316e84a606e69304e4483a3b435ab88b40c
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);
156 return newFrame;
159 nsresult
160 nsGfxButtonControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
162 nsresult rv = NS_OK;
163 if (nsGkAtoms::defaultLabel == aName) {
164 // This property is used by accessibility to get
165 // the default label of the button.
166 nsXPIDLString temp;
167 rv = const_cast<nsGfxButtonControlFrame*>(this)->GetDefaultLabel(temp);
168 aValue = temp;
169 } else {
170 aValue.Truncate();
172 return rv;
175 #ifdef ACCESSIBILITY
176 NS_IMETHODIMP nsGfxButtonControlFrame::GetAccessible(nsIAccessible** aAccessible)
178 nsCOMPtr<nsIAccessibilityService> accService = do_GetService("@mozilla.org/accessibilityService;1");
180 if (accService) {
181 return accService->CreateHTMLButtonAccessible(static_cast<nsIFrame*>(this), aAccessible);
184 return NS_ERROR_FAILURE;
186 #endif
188 NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
189 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
190 NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
192 // Initially we hardcoded the default strings here.
193 // Next, we used html.css to store the default label for various types
194 // of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
195 // However, since html.css is not internationalized, we now grab the default
196 // label from a string bundle as is done for all other UI strings.
197 // See bug 16999 for further details.
198 nsresult
199 nsGfxButtonControlFrame::GetDefaultLabel(nsXPIDLString& aString)
201 nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
202 NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
204 PRInt32 type = form->GetType();
205 const char *prop;
206 if (type == NS_FORM_INPUT_RESET) {
207 prop = "Reset";
209 else if (type == NS_FORM_INPUT_SUBMIT) {
210 prop = "Submit";
212 else if (IsFileBrowseButton(type)) {
213 prop = "Browse";
215 else {
216 aString.Truncate();
217 return NS_OK;
220 return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
221 prop, aString);
224 nsresult
225 nsGfxButtonControlFrame::GetLabel(nsXPIDLString& aLabel)
227 // Get the text from the "value" property on our content if there is
228 // one; otherwise set it to a default value (localized).
229 nsresult rv;
230 nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
231 if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value) && elt) {
232 rv = elt->GetValue(aLabel);
233 } else {
234 // Generate localized label.
235 // We can't make any assumption as to what the default would be
236 // because the value is localized for non-english platforms, thus
237 // it might not be the string "Reset", "Submit Query", or "Browse..."
238 rv = GetDefaultLabel(aLabel);
241 NS_ENSURE_SUCCESS(rv, rv);
243 // Compress whitespace out of label if needed.
244 if (!GetStyleText()->WhiteSpaceIsSignificant()) {
245 aLabel.CompressWhitespace();
246 } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
247 aLabel.CharAt(aLabel.Length() - 1) == ' ') {
248 // This is a bit of a hack. The reason this is here is as follows: we now
249 // have default padding on our buttons to make them non-ugly.
250 // Unfortunately, IE-windows does not have such padding, so people will
251 // stick values like " ok " (with the spaces) in the buttons in an attempt
252 // to make them look decent. Unfortunately, if they do this the button
253 // looks way too big in Mozilla. Worse yet, if they do this _and_ set a
254 // fixed width for the button we run into trouble because our focus-rect
255 // border/padding and outer border take up 10px of the horizontal button
256 // space or so; the result is that the text is misaligned, even with the
257 // recentering we do in nsHTMLButtonFrame::Reflow. So to solve this, even
258 // if the whitespace is significant, single leading and trailing _spaces_
259 // (and not other whitespace) are removed. The proper solution, of
260 // course, is to not have the focus rect painting taking up 6px of
261 // horizontal space. We should do that instead (via XBL form controls or
262 // changing the renderer) and remove this.
263 aLabel.Cut(0, 1);
264 aLabel.Truncate(aLabel.Length() - 1);
267 return NS_OK;
270 NS_IMETHODIMP
271 nsGfxButtonControlFrame::AttributeChanged(PRInt32 aNameSpaceID,
272 nsIAtom* aAttribute,
273 PRInt32 aModType)
275 nsresult rv = NS_OK;
277 // If the value attribute is set, update the text of the label
278 if (nsGkAtoms::value == aAttribute) {
279 if (mTextContent && mContent) {
280 nsXPIDLString label;
281 rv = GetLabel(label);
282 NS_ENSURE_SUCCESS(rv, rv);
284 mTextContent->SetText(label, PR_TRUE);
285 } else {
286 rv = NS_ERROR_UNEXPECTED;
289 // defer to HTMLButtonControlFrame
290 } else {
291 rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
293 return rv;
296 PRBool
297 nsGfxButtonControlFrame::IsLeaf() const
299 return PR_TRUE;
302 nsIFrame*
303 nsGfxButtonControlFrame::GetContentInsertionFrame()
305 return this;
308 NS_IMETHODIMP
309 nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
310 nsGUIEvent* aEvent,
311 nsEventStatus* aEventStatus)
313 // Override the HandleEvent to prevent the nsFrame::HandleEvent
314 // from being called. The nsFrame::HandleEvent causes the button label
315 // to be selected (Drawn with an XOR rectangle over the label)
317 // do we have user-input style?
318 const nsStyleUserInterface* uiStyle = GetStyleUserInterface();
319 if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
320 return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
322 return NS_OK;