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"
12 #include "nsAccessibilityService.h"
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
)
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
);
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
60 nsGfxButtonControlFrame::IsFileBrowseButton(int32_t type
)
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
;
75 nsGfxButtonControlFrame::GetFrameName(nsAString
& aResult
) const
77 return MakeFrameName(NS_LITERAL_STRING("ButtonControl"), aResult
);
81 // Create the text content used as label for the button.
82 // The frame will be generated by the frame constructor.
84 nsGfxButtonControlFrame::CreateAnonymousContent(nsTArray
<ContentInfo
>& aElements
)
89 // Add a child text content node for the label
90 NS_NewTextNode(getter_AddRefs(mTextContent
),
91 mContent
->NodeInfo()->NodeInfoManager());
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
;
103 nsGfxButtonControlFrame::AppendAnonymousContentTo(nsBaseContentList
& aElements
,
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.
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
);
127 // initialize the text frame
128 newFrame
->Init(mTextContent
, parentFrame
, nullptr);
129 mTextContent
->SetPrimaryFrame(newFrame
);
138 nsGfxButtonControlFrame::GetFormProperty(nsIAtom
* aName
, nsAString
& aValue
) const
141 if (nsGkAtoms::defaultLabel
== aName
) {
142 // This property is used by accessibility to get
143 // the default label of the button.
145 rv
= const_cast<nsGfxButtonControlFrame
*>(this)->GetDefaultLabel(temp
);
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.
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();
171 if (type
== NS_FORM_INPUT_RESET
) {
174 else if (type
== NS_FORM_INPUT_SUBMIT
) {
177 else if (IsFileBrowseButton(type
)) {
185 return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES
,
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).
195 nsCOMPtr
<nsIDOMHTMLInputElement
> elt
= do_QueryInterface(mContent
);
196 if (mContent
->HasAttr(kNameSpaceID_None
, nsGkAtoms::value
) && elt
) {
197 rv
= elt
->GetValue(aLabel
);
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.
229 aLabel
.Truncate(aLabel
.Length() - 1);
236 nsGfxButtonControlFrame::AttributeChanged(int32_t aNameSpaceID
,
242 // If the value attribute is set, update the text of the label
243 if (nsGkAtoms::value
== aAttribute
) {
244 if (mTextContent
&& mContent
) {
246 rv
= GetLabel(label
);
247 NS_ENSURE_SUCCESS(rv
, rv
);
249 mTextContent
->SetText(label
, true);
251 rv
= NS_ERROR_UNEXPECTED
;
254 // defer to HTMLButtonControlFrame
256 rv
= nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID
, aAttribute
, aModType
);
262 nsGfxButtonControlFrame::IsLeaf() const
268 nsGfxButtonControlFrame::GetContentInsertionFrame()
274 nsGfxButtonControlFrame::HandleEvent(nsPresContext
* aPresContext
,
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
);