Bug 785860 - fix sts preload list tests to skip private mode tests if private browsin...
[gecko.git] / layout / forms / nsGfxButtonControlFrame.cpp
blobc2f16053c1431f7c8a899144f1d0cc0ce3017b25
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsGfxButtonControlFrame.h"
7 #include "nsWidgetsCID.h"
8 #include "nsFormControlFrame.h"
9 #include "nsIFormControl.h"
10 #include "nsINameSpaceManager.h"
11 #ifdef ACCESSIBILITY
12 #include "nsAccessibilityService.h"
13 #endif
14 #include "nsIServiceManager.h"
15 #include "nsIDOMNode.h"
16 #include "nsGkAtoms.h"
17 #include "nsAutoPtr.h"
18 #include "nsStyleSet.h"
19 #include "nsContentUtils.h"
20 // MouseEvent suppression in PP
21 #include "nsGUIEvent.h"
22 #include "nsContentList.h"
23 #include "nsContentCreatorFunctions.h"
25 #include "nsNodeInfoManager.h"
26 #include "nsIDOMHTMLInputElement.h"
28 const nscoord kSuggestedNotSet = -1;
30 nsGfxButtonControlFrame::nsGfxButtonControlFrame(nsStyleContext* aContext):
31 nsHTMLButtonControlFrame(aContext)
35 nsIFrame*
36 NS_NewGfxButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
38 return new (aPresShell) nsGfxButtonControlFrame(aContext);
41 NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)
43 void nsGfxButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
45 nsContentUtils::DestroyAnonymousContent(&mTextContent);
46 nsHTMLButtonControlFrame::DestroyFrom(aDestructRoot);
49 nsIAtom*
50 nsGfxButtonControlFrame::GetType() const
52 return nsGkAtoms::gfxButtonControlFrame;
55 // Special check for the browse button of a file input.
57 // We'll return true if type is NS_FORM_INPUT_BUTTON and our parent
58 // is a file input.
59 bool
60 nsGfxButtonControlFrame::IsFileBrowseButton(int32_t type)
62 bool rv = false;
63 if (NS_FORM_INPUT_BUTTON == type) {
64 // Check to see if parent is a file input
65 nsCOMPtr<nsIFormControl> formCtrl =
66 do_QueryInterface(mContent->GetParent());
68 rv = formCtrl && formCtrl->GetType() == NS_FORM_INPUT_FILE;
70 return rv;
73 #ifdef DEBUG
74 NS_IMETHODIMP
75 nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const
77 return MakeFrameName(NS_LITERAL_STRING("ButtonControl"), aResult);
79 #endif
81 // Create the text content used as label for the button.
82 // The frame will be generated by the frame constructor.
83 nsresult
84 nsGfxButtonControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
86 nsXPIDLString label;
87 GetLabel(label);
89 // Add a child text content node for the label
90 NS_NewTextNode(getter_AddRefs(mTextContent),
91 mContent->NodeInfo()->NodeInfoManager());
92 if (!mTextContent)
93 return NS_ERROR_OUT_OF_MEMORY;
95 // set the value of the text node and add it to the child list
96 mTextContent->SetText(label, false);
97 if (!aElements.AppendElement(mTextContent))
98 return NS_ERROR_OUT_OF_MEMORY;
99 return NS_OK;
102 void
103 nsGfxButtonControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
104 uint32_t aFilter)
106 aElements.MaybeAppendElement(mTextContent);
109 // Create the text content used as label for the button.
110 // The frame will be generated by the frame constructor.
111 nsIFrame*
112 nsGfxButtonControlFrame::CreateFrameFor(nsIContent* aContent)
114 nsIFrame * newFrame = nullptr;
116 if (aContent == mTextContent) {
117 nsIFrame * parentFrame = mFrames.FirstChild();
119 nsPresContext* presContext = PresContext();
120 nsRefPtr<nsStyleContext> textStyleContext;
121 textStyleContext = presContext->StyleSet()->
122 ResolveStyleForNonElement(mStyleContext);
124 if (textStyleContext) {
125 newFrame = NS_NewTextFrame(presContext->PresShell(), textStyleContext);
126 if (newFrame) {
127 // initialize the text frame
128 newFrame->Init(mTextContent, parentFrame, nullptr);
129 mTextContent->SetPrimaryFrame(newFrame);
134 return newFrame;
137 nsresult
138 nsGfxButtonControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
140 nsresult rv = NS_OK;
141 if (nsGkAtoms::defaultLabel == aName) {
142 // This property is used by accessibility to get
143 // the default label of the button.
144 nsXPIDLString temp;
145 rv = const_cast<nsGfxButtonControlFrame*>(this)->GetDefaultLabel(temp);
146 aValue = temp;
147 } else {
148 aValue.Truncate();
150 return rv;
153 NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
154 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
155 NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
157 // Initially we hardcoded the default strings here.
158 // Next, we used html.css to store the default label for various types
159 // of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
160 // However, since html.css is not internationalized, we now grab the default
161 // label from a string bundle as is done for all other UI strings.
162 // See bug 16999 for further details.
163 nsresult
164 nsGfxButtonControlFrame::GetDefaultLabel(nsXPIDLString& aString)
166 nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
167 NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
169 int32_t type = form->GetType();
170 const char *prop;
171 if (type == NS_FORM_INPUT_RESET) {
172 prop = "Reset";
174 else if (type == NS_FORM_INPUT_SUBMIT) {
175 prop = "Submit";
177 else if (IsFileBrowseButton(type)) {
178 prop = "Browse";
180 else {
181 aString.Truncate();
182 return NS_OK;
185 return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
186 prop, aString);
189 nsresult
190 nsGfxButtonControlFrame::GetLabel(nsXPIDLString& aLabel)
192 // Get the text from the "value" property on our content if there is
193 // one; otherwise set it to a default value (localized).
194 nsresult rv;
195 nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
196 if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value) && elt) {
197 rv = elt->GetValue(aLabel);
198 } else {
199 // Generate localized label.
200 // We can't make any assumption as to what the default would be
201 // because the value is localized for non-english platforms, thus
202 // it might not be the string "Reset", "Submit Query", or "Browse..."
203 rv = GetDefaultLabel(aLabel);
206 NS_ENSURE_SUCCESS(rv, rv);
208 // Compress whitespace out of label if needed.
209 if (!GetStyleText()->WhiteSpaceIsSignificant()) {
210 aLabel.CompressWhitespace();
211 } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
212 aLabel.CharAt(aLabel.Length() - 1) == ' ') {
213 // This is a bit of a hack. The reason this is here is as follows: we now
214 // have default padding on our buttons to make them non-ugly.
215 // Unfortunately, IE-windows does not have such padding, so people will
216 // stick values like " ok " (with the spaces) in the buttons in an attempt
217 // to make them look decent. Unfortunately, if they do this the button
218 // looks way too big in Mozilla. Worse yet, if they do this _and_ set a
219 // fixed width for the button we run into trouble because our focus-rect
220 // border/padding and outer border take up 10px of the horizontal button
221 // space or so; the result is that the text is misaligned, even with the
222 // recentering we do in nsHTMLButtonFrame::Reflow. So to solve this, even
223 // if the whitespace is significant, single leading and trailing _spaces_
224 // (and not other whitespace) are removed. The proper solution, of
225 // course, is to not have the focus rect painting taking up 6px of
226 // horizontal space. We should do that instead (via XBL form controls or
227 // changing the renderer) and remove this.
228 aLabel.Cut(0, 1);
229 aLabel.Truncate(aLabel.Length() - 1);
232 return NS_OK;
235 NS_IMETHODIMP
236 nsGfxButtonControlFrame::AttributeChanged(int32_t aNameSpaceID,
237 nsIAtom* aAttribute,
238 int32_t aModType)
240 nsresult rv = NS_OK;
242 // If the value attribute is set, update the text of the label
243 if (nsGkAtoms::value == aAttribute) {
244 if (mTextContent && mContent) {
245 nsXPIDLString label;
246 rv = GetLabel(label);
247 NS_ENSURE_SUCCESS(rv, rv);
249 mTextContent->SetText(label, true);
250 } else {
251 rv = NS_ERROR_UNEXPECTED;
254 // defer to HTMLButtonControlFrame
255 } else {
256 rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
258 return rv;
261 bool
262 nsGfxButtonControlFrame::IsLeaf() const
264 return true;
267 nsIFrame*
268 nsGfxButtonControlFrame::GetContentInsertionFrame()
270 return this;
273 NS_IMETHODIMP
274 nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
275 nsGUIEvent* aEvent,
276 nsEventStatus* aEventStatus)
278 // Override the HandleEvent to prevent the nsFrame::HandleEvent
279 // from being called. The nsFrame::HandleEvent causes the button label
280 // to be selected (Drawn with an XOR rectangle over the label)
282 // do we have user-input style?
283 const nsStyleUserInterface* uiStyle = GetStyleUserInterface();
284 if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
285 return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
287 return NS_OK;