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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsGfxButtonControlFrame.h"
39 : #include "nsWidgetsCID.h"
40 : #include "nsFormControlFrame.h"
41 : #include "nsIFormControl.h"
42 : #include "nsINameSpaceManager.h"
43 : #ifdef ACCESSIBILITY
44 : #include "nsAccessibilityService.h"
45 : #endif
46 : #include "nsIServiceManager.h"
47 : #include "nsIDOMNode.h"
48 : #include "nsGkAtoms.h"
49 : #include "nsAutoPtr.h"
50 : #include "nsStyleSet.h"
51 : #include "nsContentUtils.h"
52 : // MouseEvent suppression in PP
53 : #include "nsGUIEvent.h"
54 : #include "nsContentCreatorFunctions.h"
55 :
56 : #include "nsNodeInfoManager.h"
57 : #include "nsIDOMHTMLInputElement.h"
58 :
59 : const nscoord kSuggestedNotSet = -1;
60 :
61 0 : nsGfxButtonControlFrame::nsGfxButtonControlFrame(nsStyleContext* aContext):
62 0 : nsHTMLButtonControlFrame(aContext)
63 : {
64 0 : }
65 :
66 : nsIFrame*
67 0 : NS_NewGfxButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
68 : {
69 0 : return new (aPresShell) nsGfxButtonControlFrame(aContext);
70 : }
71 :
72 0 : NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)
73 :
74 0 : void nsGfxButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
75 : {
76 0 : nsContentUtils::DestroyAnonymousContent(&mTextContent);
77 0 : nsHTMLButtonControlFrame::DestroyFrom(aDestructRoot);
78 0 : }
79 :
80 : nsIAtom*
81 0 : nsGfxButtonControlFrame::GetType() const
82 : {
83 0 : return nsGkAtoms::gfxButtonControlFrame;
84 : }
85 :
86 : // Special check for the browse button of a file input.
87 : //
88 : // We'll return true if type is NS_FORM_INPUT_BUTTON and our parent
89 : // is a file input.
90 : bool
91 0 : nsGfxButtonControlFrame::IsFileBrowseButton(PRInt32 type)
92 : {
93 0 : bool rv = false;
94 0 : if (NS_FORM_INPUT_BUTTON == type) {
95 : // Check to see if parent is a file input
96 : nsCOMPtr<nsIFormControl> formCtrl =
97 0 : do_QueryInterface(mContent->GetParent());
98 :
99 0 : rv = formCtrl && formCtrl->GetType() == NS_FORM_INPUT_FILE;
100 : }
101 0 : return rv;
102 : }
103 :
104 : #ifdef DEBUG
105 : NS_IMETHODIMP
106 0 : nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const
107 : {
108 0 : return MakeFrameName(NS_LITERAL_STRING("ButtonControl"), aResult);
109 : }
110 : #endif
111 :
112 : // Create the text content used as label for the button.
113 : // The frame will be generated by the frame constructor.
114 : nsresult
115 0 : nsGfxButtonControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
116 : {
117 0 : nsXPIDLString label;
118 0 : GetLabel(label);
119 :
120 : // Add a child text content node for the label
121 0 : NS_NewTextNode(getter_AddRefs(mTextContent),
122 0 : mContent->NodeInfo()->NodeInfoManager());
123 0 : if (!mTextContent)
124 0 : return NS_ERROR_OUT_OF_MEMORY;
125 :
126 : // set the value of the text node and add it to the child list
127 0 : mTextContent->SetText(label, false);
128 0 : if (!aElements.AppendElement(mTextContent))
129 0 : return NS_ERROR_OUT_OF_MEMORY;
130 0 : return NS_OK;
131 : }
132 :
133 : void
134 0 : nsGfxButtonControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
135 : PRUint32 aFilter)
136 : {
137 0 : aElements.MaybeAppendElement(mTextContent);
138 0 : }
139 :
140 : // Create the text content used as label for the button.
141 : // The frame will be generated by the frame constructor.
142 : nsIFrame*
143 0 : nsGfxButtonControlFrame::CreateFrameFor(nsIContent* aContent)
144 : {
145 0 : nsIFrame * newFrame = nsnull;
146 :
147 0 : if (aContent == mTextContent) {
148 0 : nsIFrame * parentFrame = mFrames.FirstChild();
149 :
150 0 : nsPresContext* presContext = PresContext();
151 0 : nsRefPtr<nsStyleContext> textStyleContext;
152 : textStyleContext = presContext->StyleSet()->
153 0 : ResolveStyleForNonElement(mStyleContext);
154 :
155 0 : if (textStyleContext) {
156 0 : newFrame = NS_NewTextFrame(presContext->PresShell(), textStyleContext);
157 0 : if (newFrame) {
158 : // initialize the text frame
159 0 : newFrame->Init(mTextContent, parentFrame, nsnull);
160 0 : mTextContent->SetPrimaryFrame(newFrame);
161 : }
162 : }
163 : }
164 :
165 0 : return newFrame;
166 : }
167 :
168 : nsresult
169 0 : nsGfxButtonControlFrame::GetFormProperty(nsIAtom* aName, nsAString& aValue) const
170 : {
171 0 : nsresult rv = NS_OK;
172 0 : if (nsGkAtoms::defaultLabel == aName) {
173 : // This property is used by accessibility to get
174 : // the default label of the button.
175 0 : nsXPIDLString temp;
176 0 : rv = const_cast<nsGfxButtonControlFrame*>(this)->GetDefaultLabel(temp);
177 0 : aValue = temp;
178 : } else {
179 0 : aValue.Truncate();
180 : }
181 0 : return rv;
182 : }
183 :
184 0 : NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
185 0 : NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
186 0 : NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
187 :
188 : // Initially we hardcoded the default strings here.
189 : // Next, we used html.css to store the default label for various types
190 : // of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
191 : // However, since html.css is not internationalized, we now grab the default
192 : // label from a string bundle as is done for all other UI strings.
193 : // See bug 16999 for further details.
194 : nsresult
195 0 : nsGfxButtonControlFrame::GetDefaultLabel(nsXPIDLString& aString)
196 : {
197 0 : nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
198 0 : NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
199 :
200 0 : PRInt32 type = form->GetType();
201 : const char *prop;
202 0 : if (type == NS_FORM_INPUT_RESET) {
203 0 : prop = "Reset";
204 : }
205 0 : else if (type == NS_FORM_INPUT_SUBMIT) {
206 0 : prop = "Submit";
207 : }
208 0 : else if (IsFileBrowseButton(type)) {
209 0 : prop = "Browse";
210 : }
211 : else {
212 0 : aString.Truncate();
213 0 : return NS_OK;
214 : }
215 :
216 : return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
217 0 : prop, aString);
218 : }
219 :
220 : nsresult
221 0 : nsGfxButtonControlFrame::GetLabel(nsXPIDLString& aLabel)
222 : {
223 : // Get the text from the "value" property on our content if there is
224 : // one; otherwise set it to a default value (localized).
225 : nsresult rv;
226 0 : nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
227 0 : if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value) && elt) {
228 0 : rv = elt->GetValue(aLabel);
229 : } else {
230 : // Generate localized label.
231 : // We can't make any assumption as to what the default would be
232 : // because the value is localized for non-english platforms, thus
233 : // it might not be the string "Reset", "Submit Query", or "Browse..."
234 0 : rv = GetDefaultLabel(aLabel);
235 : }
236 :
237 0 : NS_ENSURE_SUCCESS(rv, rv);
238 :
239 : // Compress whitespace out of label if needed.
240 0 : if (!GetStyleText()->WhiteSpaceIsSignificant()) {
241 0 : aLabel.CompressWhitespace();
242 0 : } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
243 0 : aLabel.CharAt(aLabel.Length() - 1) == ' ') {
244 : // This is a bit of a hack. The reason this is here is as follows: we now
245 : // have default padding on our buttons to make them non-ugly.
246 : // Unfortunately, IE-windows does not have such padding, so people will
247 : // stick values like " ok " (with the spaces) in the buttons in an attempt
248 : // to make them look decent. Unfortunately, if they do this the button
249 : // looks way too big in Mozilla. Worse yet, if they do this _and_ set a
250 : // fixed width for the button we run into trouble because our focus-rect
251 : // border/padding and outer border take up 10px of the horizontal button
252 : // space or so; the result is that the text is misaligned, even with the
253 : // recentering we do in nsHTMLButtonFrame::Reflow. So to solve this, even
254 : // if the whitespace is significant, single leading and trailing _spaces_
255 : // (and not other whitespace) are removed. The proper solution, of
256 : // course, is to not have the focus rect painting taking up 6px of
257 : // horizontal space. We should do that instead (via XBL form controls or
258 : // changing the renderer) and remove this.
259 0 : aLabel.Cut(0, 1);
260 0 : aLabel.Truncate(aLabel.Length() - 1);
261 : }
262 :
263 0 : return NS_OK;
264 : }
265 :
266 : NS_IMETHODIMP
267 0 : nsGfxButtonControlFrame::AttributeChanged(PRInt32 aNameSpaceID,
268 : nsIAtom* aAttribute,
269 : PRInt32 aModType)
270 : {
271 0 : nsresult rv = NS_OK;
272 :
273 : // If the value attribute is set, update the text of the label
274 0 : if (nsGkAtoms::value == aAttribute) {
275 0 : if (mTextContent && mContent) {
276 0 : nsXPIDLString label;
277 0 : rv = GetLabel(label);
278 0 : NS_ENSURE_SUCCESS(rv, rv);
279 :
280 0 : mTextContent->SetText(label, true);
281 : } else {
282 0 : rv = NS_ERROR_UNEXPECTED;
283 : }
284 :
285 : // defer to HTMLButtonControlFrame
286 : } else {
287 0 : rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
288 : }
289 0 : return rv;
290 : }
291 :
292 : bool
293 0 : nsGfxButtonControlFrame::IsLeaf() const
294 : {
295 0 : return true;
296 : }
297 :
298 : nsIFrame*
299 0 : nsGfxButtonControlFrame::GetContentInsertionFrame()
300 : {
301 0 : return this;
302 : }
303 :
304 : NS_IMETHODIMP
305 0 : nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
306 : nsGUIEvent* aEvent,
307 : nsEventStatus* aEventStatus)
308 : {
309 : // Override the HandleEvent to prevent the nsFrame::HandleEvent
310 : // from being called. The nsFrame::HandleEvent causes the button label
311 : // to be selected (Drawn with an XOR rectangle over the label)
312 :
313 : // do we have user-input style?
314 0 : const nsStyleUserInterface* uiStyle = GetStyleUserInterface();
315 0 : if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
316 0 : return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
317 :
318 0 : return NS_OK;
319 : }
|