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/. */
6
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"
14
15 using namespace mozilla;
16
nsGfxButtonControlFrame(ComputedStyle * aStyle,nsPresContext * aPresContext)17 nsGfxButtonControlFrame::nsGfxButtonControlFrame(ComputedStyle* aStyle,
18 nsPresContext* aPresContext)
19 : nsHTMLButtonControlFrame(aStyle, aPresContext, kClassID) {}
20
NS_NewGfxButtonControlFrame(PresShell * aPresShell,ComputedStyle * aStyle)21 nsContainerFrame* NS_NewGfxButtonControlFrame(PresShell* aPresShell,
22 ComputedStyle* aStyle) {
23 return new (aPresShell)
24 nsGfxButtonControlFrame(aStyle, aPresShell->GetPresContext());
25 }
26
NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)27 NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)
28
29 void nsGfxButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot,
30 PostDestroyData& aPostDestroyData) {
31 aPostDestroyData.AddAnonymousContent(mTextContent.forget());
32 nsHTMLButtonControlFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
33 }
34
35 #ifdef DEBUG_FRAME_DUMP
GetFrameName(nsAString & aResult) const36 nsresult nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const {
37 return MakeFrameName(u"ButtonControl"_ns, aResult);
38 }
39 #endif
40
41 // Create the text content used as label for the button.
42 // The frame will be generated by the frame constructor.
CreateAnonymousContent(nsTArray<ContentInfo> & aElements)43 nsresult nsGfxButtonControlFrame::CreateAnonymousContent(
44 nsTArray<ContentInfo>& aElements) {
45 nsAutoString label;
46 nsresult rv = GetLabel(label);
47 NS_ENSURE_SUCCESS(rv, rv);
48
49 // Add a child text content node for the label
50 mTextContent = new (mContent->NodeInfo()->NodeInfoManager())
51 nsTextNode(mContent->NodeInfo()->NodeInfoManager());
52
53 // set the value of the text node and add it to the child list
54 mTextContent->SetText(label, false);
55 aElements.AppendElement(mTextContent);
56
57 return NS_OK;
58 }
59
AppendAnonymousContentTo(nsTArray<nsIContent * > & aElements,uint32_t aFilter)60 void nsGfxButtonControlFrame::AppendAnonymousContentTo(
61 nsTArray<nsIContent*>& aElements, uint32_t aFilter) {
62 if (mTextContent) {
63 aElements.AppendElement(mTextContent);
64 }
65 }
66
67 NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)68 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
69 NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
70
71 // Initially we hardcoded the default strings here.
72 // Next, we used html.css to store the default label for various types
73 // of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
74 // However, since html.css is not internationalized, we now grab the default
75 // label from a string bundle as is done for all other UI strings.
76 // See bug 16999 for further details.
77 nsresult nsGfxButtonControlFrame::GetDefaultLabel(nsAString& aString) const {
78 nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
79 NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
80
81 auto type = form->ControlType();
82 const char* prop;
83 if (type == FormControlType::InputReset) {
84 prop = "Reset";
85 } else if (type == FormControlType::InputSubmit) {
86 prop = "Submit";
87 } else {
88 aString.Truncate();
89 return NS_OK;
90 }
91
92 return nsContentUtils::GetMaybeLocalizedString(
93 nsContentUtils::eFORMS_PROPERTIES, prop, mContent->OwnerDoc(), aString);
94 }
95
GetLabel(nsString & aLabel)96 nsresult nsGfxButtonControlFrame::GetLabel(nsString& aLabel) {
97 // Get the text from the "value" property on our content if there is
98 // one; otherwise set it to a default value (localized).
99 auto* elt = dom::HTMLInputElement::FromNode(mContent);
100 if (elt && elt->HasAttr(nsGkAtoms::value)) {
101 elt->GetValue(aLabel, dom::CallerType::System);
102 } else {
103 // Generate localized label.
104 // We can't make any assumption as to what the default would be
105 // because the value is localized for non-english platforms, thus
106 // it might not be the string "Reset", "Submit Query", or "Browse..."
107 nsresult rv;
108 rv = GetDefaultLabel(aLabel);
109 NS_ENSURE_SUCCESS(rv, rv);
110 }
111
112 // Compress whitespace out of label if needed.
113 if (!StyleText()->WhiteSpaceIsSignificant()) {
114 aLabel.CompressWhitespace();
115 } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
116 aLabel.CharAt(aLabel.Length() - 1) == ' ') {
117 // This is a bit of a hack. The reason this is here is as follows: we now
118 // have default padding on our buttons to make them non-ugly.
119 // Unfortunately, IE-windows does not have such padding, so people will
120 // stick values like " ok " (with the spaces) in the buttons in an attempt
121 // to make them look decent. Unfortunately, if they do this the button
122 // looks way too big in Mozilla. Worse yet, if they do this _and_ set a
123 // fixed width for the button we run into trouble because our focus-rect
124 // border/padding and outer border take up 10px of the horizontal button
125 // space or so; the result is that the text is misaligned, even with the
126 // recentering we do in nsHTMLButtonControlFrame::Reflow. So to solve
127 // this, even if the whitespace is significant, single leading and trailing
128 // _spaces_ (and not other whitespace) are removed. The proper solution,
129 // of course, is to not have the focus rect painting taking up 6px of
130 // horizontal space. We should do that instead (changing the renderer) and
131 // remove this.
132 aLabel.Cut(0, 1);
133 aLabel.Truncate(aLabel.Length() - 1);
134 }
135
136 return NS_OK;
137 }
138
AttributeChanged(int32_t aNameSpaceID,nsAtom * aAttribute,int32_t aModType)139 nsresult nsGfxButtonControlFrame::AttributeChanged(int32_t aNameSpaceID,
140 nsAtom* aAttribute,
141 int32_t aModType) {
142 nsresult rv = NS_OK;
143
144 // If the value attribute is set, update the text of the label
145 if (nsGkAtoms::value == aAttribute) {
146 if (mTextContent && mContent) {
147 nsAutoString label;
148 rv = GetLabel(label);
149 NS_ENSURE_SUCCESS(rv, rv);
150
151 mTextContent->SetText(label, true);
152 } else {
153 rv = NS_ERROR_UNEXPECTED;
154 }
155
156 // defer to HTMLButtonControlFrame
157 } else {
158 rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute,
159 aModType);
160 }
161 return rv;
162 }
163
GetContentInsertionFrame()164 nsContainerFrame* nsGfxButtonControlFrame::GetContentInsertionFrame() {
165 return this;
166 }
167
HandleEvent(nsPresContext * aPresContext,WidgetGUIEvent * aEvent,nsEventStatus * aEventStatus)168 nsresult nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
169 WidgetGUIEvent* aEvent,
170 nsEventStatus* aEventStatus) {
171 // Override the HandleEvent to prevent the nsIFrame::HandleEvent
172 // from being called. The nsIFrame::HandleEvent causes the button label
173 // to be selected (Drawn with an XOR rectangle over the label)
174
175 if (IsContentDisabled()) {
176 return nsIFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
177 }
178 return NS_OK;
179 }
180