no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / layout / forms / nsGfxButtonControlFrame.cpp
blobb250030e134101a8e8f6fc13da7886f7cc9412ea
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsGfxButtonControlFrame.h"
8 #include "nsIFormControl.h"
9 #include "nsGkAtoms.h"
10 #include "mozilla/PresShell.h"
11 #include "mozilla/dom/HTMLInputElement.h"
12 #include "nsContentUtils.h"
13 #include "nsTextNode.h"
15 using namespace mozilla;
17 nsGfxButtonControlFrame::nsGfxButtonControlFrame(ComputedStyle* aStyle,
18 nsPresContext* aPresContext)
19 : nsHTMLButtonControlFrame(aStyle, aPresContext, kClassID) {}
21 nsContainerFrame* NS_NewGfxButtonControlFrame(PresShell* aPresShell,
22 ComputedStyle* aStyle) {
23 return new (aPresShell)
24 nsGfxButtonControlFrame(aStyle, aPresShell->GetPresContext());
27 NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)
29 void nsGfxButtonControlFrame::Destroy(DestroyContext& aContext) {
30 aContext.AddAnonymousContent(mTextContent.forget());
31 nsHTMLButtonControlFrame::Destroy(aContext);
34 #ifdef DEBUG_FRAME_DUMP
35 nsresult nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const {
36 return MakeFrameName(u"ButtonControl"_ns, aResult);
38 #endif
40 // Create the text content used as label for the button.
41 // The frame will be generated by the frame constructor.
42 nsresult nsGfxButtonControlFrame::CreateAnonymousContent(
43 nsTArray<ContentInfo>& aElements) {
44 nsAutoString label;
45 nsresult rv = GetLabel(label);
46 NS_ENSURE_SUCCESS(rv, rv);
48 // Add a child text content node for the label
49 mTextContent = new (mContent->NodeInfo()->NodeInfoManager())
50 nsTextNode(mContent->NodeInfo()->NodeInfoManager());
52 // set the value of the text node and add it to the child list
53 mTextContent->SetText(label, false);
54 aElements.AppendElement(mTextContent);
56 return NS_OK;
59 void nsGfxButtonControlFrame::AppendAnonymousContentTo(
60 nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
61 if (mTextContent) {
62 aElements.AppendElement(mTextContent);
66 NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
67 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
68 NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
70 // Initially we hardcoded the default strings here.
71 // Next, we used html.css to store the default label for various types
72 // of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
73 // However, since html.css is not internationalized, we now grab the default
74 // label from a string bundle as is done for all other UI strings.
75 // See bug 16999 for further details.
76 nsresult nsGfxButtonControlFrame::GetDefaultLabel(nsAString& aString) const {
77 nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
78 NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
80 auto type = form->ControlType();
81 const char* prop;
82 if (type == FormControlType::InputReset) {
83 prop = "Reset";
84 } else if (type == FormControlType::InputSubmit) {
85 prop = "Submit";
86 } else {
87 aString.Truncate();
88 return NS_OK;
91 return nsContentUtils::GetMaybeLocalizedString(
92 nsContentUtils::eFORMS_PROPERTIES, prop, mContent->OwnerDoc(), aString);
95 nsresult nsGfxButtonControlFrame::GetLabel(nsString& aLabel) {
96 // Get the text from the "value" property on our content if there is
97 // one; otherwise set it to a default value (localized).
98 auto* elt = dom::HTMLInputElement::FromNode(mContent);
99 if (elt && elt->HasAttr(nsGkAtoms::value)) {
100 elt->GetValue(aLabel, dom::CallerType::System);
101 } else {
102 // Generate localized label.
103 // We can't make any assumption as to what the default would be
104 // because the value is localized for non-english platforms, thus
105 // it might not be the string "Reset", "Submit Query", or "Browse..."
106 nsresult rv;
107 rv = GetDefaultLabel(aLabel);
108 NS_ENSURE_SUCCESS(rv, rv);
111 // Compress whitespace out of label if needed.
112 if (!StyleText()->WhiteSpaceIsSignificant()) {
113 aLabel.CompressWhitespace();
114 } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
115 aLabel.CharAt(aLabel.Length() - 1) == ' ') {
116 // This is a bit of a hack. The reason this is here is as follows: we now
117 // have default padding on our buttons to make them non-ugly.
118 // Unfortunately, IE-windows does not have such padding, so people will
119 // stick values like " ok " (with the spaces) in the buttons in an attempt
120 // to make them look decent. Unfortunately, if they do this the button
121 // looks way too big in Mozilla. Worse yet, if they do this _and_ set a
122 // fixed width for the button we run into trouble because our focus-rect
123 // border/padding and outer border take up 10px of the horizontal button
124 // space or so; the result is that the text is misaligned, even with the
125 // recentering we do in nsHTMLButtonControlFrame::Reflow. So to solve
126 // this, even if the whitespace is significant, single leading and trailing
127 // _spaces_ (and not other whitespace) are removed. The proper solution,
128 // of course, is to not have the focus rect painting taking up 6px of
129 // horizontal space. We should do that instead (changing the renderer) and
130 // remove this.
131 aLabel.Cut(0, 1);
132 aLabel.Truncate(aLabel.Length() - 1);
135 return NS_OK;
138 nsresult nsGfxButtonControlFrame::AttributeChanged(int32_t aNameSpaceID,
139 nsAtom* aAttribute,
140 int32_t aModType) {
141 nsresult rv = NS_OK;
143 // If the value attribute is set, update the text of the label
144 if (nsGkAtoms::value == aAttribute) {
145 if (mTextContent && mContent) {
146 nsAutoString label;
147 rv = GetLabel(label);
148 NS_ENSURE_SUCCESS(rv, rv);
150 mTextContent->SetText(label, true);
151 } else {
152 rv = NS_ERROR_UNEXPECTED;
155 // defer to HTMLButtonControlFrame
156 } else {
157 rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute,
158 aModType);
160 return rv;
163 nsresult nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
164 WidgetGUIEvent* aEvent,
165 nsEventStatus* aEventStatus) {
166 // Override the HandleEvent to prevent the nsIFrame::HandleEvent
167 // from being called. The nsIFrame::HandleEvent causes the button label
168 // to be selected (Drawn with an XOR rectangle over the label)
170 if (IsContentDisabled()) {
171 return nsIFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
173 return NS_OK;