2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-08-06 09:13:07 +04:00
|
|
|
|
|
|
|
#include "nsGfxButtonControlFrame.h"
|
2004-10-14 23:05:31 +04:00
|
|
|
#include "nsIFormControl.h"
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2016-02-24 10:01:11 +03:00
|
|
|
#include "mozilla/StyleSetHandle.h"
|
|
|
|
#include "mozilla/StyleSetHandleInlines.h"
|
2016-11-15 20:46:32 +03:00
|
|
|
#include "mozilla/dom/HTMLInputElement.h"
|
2004-12-17 23:40:48 +03:00
|
|
|
#include "nsContentUtils.h"
|
2006-02-13 22:42:08 +03:00
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
2013-04-04 16:01:08 +04:00
|
|
|
#include "nsTextNode.h"
|
2001-02-06 04:31:18 +03:00
|
|
|
|
2013-10-02 07:46:03 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2017-04-30 18:30:08 +03:00
|
|
|
nsGfxButtonControlFrame::nsGfxButtonControlFrame(nsStyleContext* aContext)
|
2017-05-26 13:11:11 +03:00
|
|
|
: nsHTMLButtonControlFrame(aContext, kClassID)
|
1999-08-10 23:13:57 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-25 02:20:40 +04:00
|
|
|
nsContainerFrame*
|
2006-03-27 01:30:36 +04:00
|
|
|
NS_NewGfxButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2006-03-27 01:30:36 +04:00
|
|
|
return new (aPresShell) nsGfxButtonControlFrame(aContext);
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|
2007-02-18 20:34:09 +03:00
|
|
|
|
2009-09-12 20:49:24 +04:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)
|
|
|
|
|
2017-11-07 03:20:33 +03:00
|
|
|
void nsGfxButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot, PostDestroyData& aPostDestroyData)
|
2007-02-18 20:34:09 +03:00
|
|
|
{
|
2017-11-07 03:20:33 +03:00
|
|
|
aPostDestroyData.AddAnonymousContent(mTextContent.forget());
|
|
|
|
nsHTMLButtonControlFrame::DestroyFrom(aDestructRoot, aPostDestroyData);
|
2007-02-18 20:34:09 +03:00
|
|
|
}
|
|
|
|
|
2014-01-06 03:31:14 +04:00
|
|
|
#ifdef DEBUG_FRAME_DUMP
|
2014-02-18 11:47:48 +04:00
|
|
|
nsresult
|
2001-11-14 04:33:42 +03:00
|
|
|
nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
2001-11-14 04:33:42 +03:00
|
|
|
return MakeFrameName(NS_LITERAL_STRING("ButtonControl"), aResult);
|
1999-08-10 23:13:57 +04:00
|
|
|
}
|
1999-11-02 01:12:45 +03:00
|
|
|
#endif
|
1999-08-06 09:13:07 +04:00
|
|
|
|
2000-02-21 08:55:25 +03:00
|
|
|
// Create the text content used as label for the button.
|
|
|
|
// The frame will be generated by the frame constructor.
|
2007-02-18 20:34:09 +03:00
|
|
|
nsresult
|
2011-05-07 00:04:44 +04:00
|
|
|
nsGfxButtonControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
|
2000-02-21 08:55:25 +03:00
|
|
|
{
|
2017-08-11 11:31:22 +03:00
|
|
|
nsAutoString label;
|
|
|
|
nsresult rv = GetLabel(label);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-02-18 20:34:09 +03:00
|
|
|
|
2000-02-21 08:55:25 +03:00
|
|
|
// Add a child text content node for the label
|
2013-04-04 16:01:08 +04:00
|
|
|
mTextContent = new nsTextNode(mContent->NodeInfo()->NodeInfoManager());
|
2007-02-18 20:34:09 +03:00
|
|
|
|
|
|
|
// set the value of the text node and add it to the child list
|
2011-10-17 18:59:28 +04:00
|
|
|
mTextContent->SetText(label, false);
|
2013-04-04 16:01:08 +04:00
|
|
|
aElements.AppendElement(mTextContent);
|
|
|
|
|
2005-10-28 15:25:24 +04:00
|
|
|
return NS_OK;
|
2000-02-21 08:55:25 +03:00
|
|
|
}
|
|
|
|
|
2010-02-11 20:34:01 +03:00
|
|
|
void
|
2014-07-16 22:41:57 +04:00
|
|
|
nsGfxButtonControlFrame::AppendAnonymousContentTo(nsTArray<nsIContent*>& aElements,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aFilter)
|
2010-02-11 20:34:01 +03:00
|
|
|
{
|
2014-07-16 22:41:57 +04:00
|
|
|
if (mTextContent) {
|
|
|
|
aElements.AppendElement(mTextContent);
|
|
|
|
}
|
2010-02-11 20:34:01 +03:00
|
|
|
}
|
|
|
|
|
2009-01-12 22:20:59 +03:00
|
|
|
NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
|
|
|
|
NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
|
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
|
2000-02-21 08:55:25 +03:00
|
|
|
|
|
|
|
// Initially we hardcoded the default strings here.
|
|
|
|
// Next, we used html.css to store the default label for various types
|
|
|
|
// of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
|
|
|
|
// However, since html.css is not internationalized, we now grab the default
|
|
|
|
// label from a string bundle as is done for all other UI strings.
|
|
|
|
// See bug 16999 for further details.
|
2002-09-16 10:23:52 +04:00
|
|
|
nsresult
|
2017-08-11 11:31:22 +03:00
|
|
|
nsGfxButtonControlFrame::GetDefaultLabel(nsAString& aString) const
|
2000-02-21 08:55:25 +03:00
|
|
|
{
|
2005-12-13 02:53:06 +03:00
|
|
|
nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
|
|
|
|
NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
|
|
|
|
|
2017-04-01 05:49:00 +03:00
|
|
|
int32_t type = form->ControlType();
|
2004-12-17 23:40:48 +03:00
|
|
|
const char *prop;
|
2002-09-16 10:23:52 +04:00
|
|
|
if (type == NS_FORM_INPUT_RESET) {
|
2004-12-17 23:40:48 +03:00
|
|
|
prop = "Reset";
|
2012-09-11 14:26:29 +04:00
|
|
|
}
|
2002-09-16 10:23:52 +04:00
|
|
|
else if (type == NS_FORM_INPUT_SUBMIT) {
|
2004-12-17 23:40:48 +03:00
|
|
|
prop = "Submit";
|
2012-09-11 14:26:29 +04:00
|
|
|
}
|
2000-02-21 08:55:25 +03:00
|
|
|
else {
|
2004-01-30 01:04:45 +03:00
|
|
|
aString.Truncate();
|
2004-12-17 23:40:48 +03:00
|
|
|
return NS_OK;
|
2000-02-21 08:55:25 +03:00
|
|
|
}
|
2004-12-17 23:40:48 +03:00
|
|
|
|
|
|
|
return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
|
|
|
|
prop, aString);
|
2000-02-21 08:55:25 +03:00
|
|
|
}
|
|
|
|
|
2006-02-13 22:42:08 +03:00
|
|
|
nsresult
|
2017-08-11 11:31:22 +03:00
|
|
|
nsGfxButtonControlFrame::GetLabel(nsString& aLabel)
|
2006-02-13 22:42:08 +03:00
|
|
|
{
|
2006-04-05 02:34:10 +04:00
|
|
|
// Get the text from the "value" property on our content if there is
|
|
|
|
// one; otherwise set it to a default value (localized).
|
2016-11-15 20:46:32 +03:00
|
|
|
dom::HTMLInputElement* elt = dom::HTMLInputElement::FromContent(mContent);
|
2017-12-07 21:13:50 +03:00
|
|
|
if (elt && elt->HasAttr(kNameSpaceID_None, nsGkAtoms::value)) {
|
2016-11-15 20:46:32 +03:00
|
|
|
elt->GetValue(aLabel, dom::CallerType::System);
|
2006-04-05 02:34:10 +04:00
|
|
|
} else {
|
2006-02-13 22:42:08 +03:00
|
|
|
// Generate localized label.
|
|
|
|
// We can't make any assumption as to what the default would be
|
|
|
|
// because the value is localized for non-english platforms, thus
|
|
|
|
// it might not be the string "Reset", "Submit Query", or "Browse..."
|
2016-11-15 20:46:32 +03:00
|
|
|
nsresult rv;
|
2006-02-13 22:42:08 +03:00
|
|
|
rv = GetDefaultLabel(aLabel);
|
2016-11-15 20:46:32 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-02-13 22:42:08 +03:00
|
|
|
}
|
|
|
|
|
2006-04-05 02:34:10 +04:00
|
|
|
// Compress whitespace out of label if needed.
|
2013-02-17 01:51:02 +04:00
|
|
|
if (!StyleText()->WhiteSpaceIsSignificant()) {
|
2006-02-13 22:42:08 +03:00
|
|
|
aLabel.CompressWhitespace();
|
|
|
|
} else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
|
|
|
|
aLabel.CharAt(aLabel.Length() - 1) == ' ') {
|
|
|
|
// This is a bit of a hack. The reason this is here is as follows: we now
|
|
|
|
// have default padding on our buttons to make them non-ugly.
|
|
|
|
// Unfortunately, IE-windows does not have such padding, so people will
|
|
|
|
// stick values like " ok " (with the spaces) in the buttons in an attempt
|
|
|
|
// to make them look decent. Unfortunately, if they do this the button
|
|
|
|
// looks way too big in Mozilla. Worse yet, if they do this _and_ set a
|
|
|
|
// fixed width for the button we run into trouble because our focus-rect
|
|
|
|
// border/padding and outer border take up 10px of the horizontal button
|
|
|
|
// space or so; the result is that the text is misaligned, even with the
|
2013-10-02 20:04:09 +04:00
|
|
|
// recentering we do in nsHTMLButtonControlFrame::Reflow. So to solve
|
|
|
|
// this, even if the whitespace is significant, single leading and trailing
|
|
|
|
// _spaces_ (and not other whitespace) are removed. The proper solution,
|
|
|
|
// of course, is to not have the focus rect painting taking up 6px of
|
2006-02-13 22:42:08 +03:00
|
|
|
// horizontal space. We should do that instead (via XBL form controls or
|
|
|
|
// changing the renderer) and remove this.
|
|
|
|
aLabel.Cut(0, 1);
|
|
|
|
aLabel.Truncate(aLabel.Length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-02-18 11:47:48 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsGfxButtonControlFrame::AttributeChanged(int32_t aNameSpaceID,
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* aAttribute,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aModType)
|
2000-02-21 08:55:25 +03:00
|
|
|
{
|
2000-02-23 23:56:39 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-02-21 08:55:25 +03:00
|
|
|
// If the value attribute is set, update the text of the label
|
2006-12-26 20:47:52 +03:00
|
|
|
if (nsGkAtoms::value == aAttribute) {
|
2000-02-23 23:56:39 +03:00
|
|
|
if (mTextContent && mContent) {
|
2017-08-11 11:31:22 +03:00
|
|
|
nsAutoString label;
|
2006-02-13 22:42:08 +03:00
|
|
|
rv = GetLabel(label);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mTextContent->SetText(label, true);
|
2000-02-23 23:56:39 +03:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2000-02-21 08:55:25 +03:00
|
|
|
}
|
|
|
|
|
2000-02-23 23:56:39 +03:00
|
|
|
// defer to HTMLButtonControlFrame
|
|
|
|
} else {
|
2005-09-07 20:49:21 +04:00
|
|
|
rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
|
2000-02-23 23:56:39 +03:00
|
|
|
}
|
|
|
|
return rv;
|
2000-02-21 08:55:25 +03:00
|
|
|
}
|
|
|
|
|
2014-05-25 02:20:39 +04:00
|
|
|
nsContainerFrame*
|
2007-02-17 17:14:08 +03:00
|
|
|
nsGfxButtonControlFrame::GetContentInsertionFrame()
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2014-02-18 11:47:48 +04:00
|
|
|
nsresult
|
2017-07-06 15:00:35 +03:00
|
|
|
nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext,
|
2013-10-02 07:46:03 +04:00
|
|
|
WidgetGUIEvent* aEvent,
|
|
|
|
nsEventStatus* aEventStatus)
|
1999-08-06 09:13:07 +04:00
|
|
|
{
|
1999-08-10 23:13:57 +04:00
|
|
|
// Override the HandleEvent to prevent the nsFrame::HandleEvent
|
|
|
|
// from being called. The nsFrame::HandleEvent causes the button label
|
|
|
|
// to be selected (Drawn with an XOR rectangle over the label)
|
|
|
|
|
2000-08-09 03:38:00 +04:00
|
|
|
// do we have user-input style?
|
2013-02-17 01:51:02 +04:00
|
|
|
const nsStyleUserInterface* uiStyle = StyleUserInterface();
|
2016-10-23 01:08:18 +03:00
|
|
|
if (uiStyle->mUserInput == StyleUserInput::None ||
|
|
|
|
uiStyle->mUserInput == StyleUserInput::Disabled) {
|
2000-08-09 03:38:00 +04:00
|
|
|
return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
|
2016-10-23 01:08:18 +03:00
|
|
|
}
|
1999-08-10 23:13:57 +04:00
|
|
|
return NS_OK;
|
1999-08-06 09:13:07 +04:00
|
|
|
}
|