Blame layout/forms/nsGfxButtonControlFrame.cpp

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