layout/forms/nsGfxButtonControlFrame.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/forms/nsGfxButtonControlFrame.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,234 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsGfxButtonControlFrame.h"
    1.10 +#include "nsIFormControl.h"
    1.11 +#include "nsGkAtoms.h"
    1.12 +#include "nsAutoPtr.h"
    1.13 +#include "nsStyleSet.h"
    1.14 +#include "nsContentUtils.h"
    1.15 +// MouseEvent suppression in PP
    1.16 +#include "nsContentList.h"
    1.17 +
    1.18 +#include "nsIDOMHTMLInputElement.h"
    1.19 +#include "nsTextNode.h"
    1.20 +
    1.21 +using namespace mozilla;
    1.22 +
    1.23 +nsGfxButtonControlFrame::nsGfxButtonControlFrame(nsStyleContext* aContext):
    1.24 +  nsHTMLButtonControlFrame(aContext)
    1.25 +{
    1.26 +}
    1.27 +
    1.28 +nsIFrame*
    1.29 +NS_NewGfxButtonControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
    1.30 +{
    1.31 +  return new (aPresShell) nsGfxButtonControlFrame(aContext);
    1.32 +}
    1.33 +
    1.34 +NS_IMPL_FRAMEARENA_HELPERS(nsGfxButtonControlFrame)
    1.35 +
    1.36 +void nsGfxButtonControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
    1.37 +{
    1.38 +  nsContentUtils::DestroyAnonymousContent(&mTextContent);
    1.39 +  nsHTMLButtonControlFrame::DestroyFrom(aDestructRoot);
    1.40 +}
    1.41 +
    1.42 +nsIAtom*
    1.43 +nsGfxButtonControlFrame::GetType() const
    1.44 +{
    1.45 +  return nsGkAtoms::gfxButtonControlFrame;
    1.46 +}
    1.47 +
    1.48 +#ifdef DEBUG_FRAME_DUMP
    1.49 +nsresult
    1.50 +nsGfxButtonControlFrame::GetFrameName(nsAString& aResult) const
    1.51 +{
    1.52 +  return MakeFrameName(NS_LITERAL_STRING("ButtonControl"), aResult);
    1.53 +}
    1.54 +#endif
    1.55 +
    1.56 +// Create the text content used as label for the button.
    1.57 +// The frame will be generated by the frame constructor.
    1.58 +nsresult
    1.59 +nsGfxButtonControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
    1.60 +{
    1.61 +  nsXPIDLString label;
    1.62 +  GetLabel(label);
    1.63 +
    1.64 +  // Add a child text content node for the label
    1.65 +  mTextContent = new nsTextNode(mContent->NodeInfo()->NodeInfoManager());
    1.66 +
    1.67 +  // set the value of the text node and add it to the child list
    1.68 +  mTextContent->SetText(label, false);
    1.69 +  aElements.AppendElement(mTextContent);
    1.70 +
    1.71 +  return NS_OK;
    1.72 +}
    1.73 +
    1.74 +void
    1.75 +nsGfxButtonControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
    1.76 +                                                  uint32_t aFilter)
    1.77 +{
    1.78 +  aElements.MaybeAppendElement(mTextContent);
    1.79 +}
    1.80 +
    1.81 +// Create the text content used as label for the button.
    1.82 +// The frame will be generated by the frame constructor.
    1.83 +nsIFrame*
    1.84 +nsGfxButtonControlFrame::CreateFrameFor(nsIContent*      aContent)
    1.85 +{
    1.86 +  nsIFrame * newFrame = nullptr;
    1.87 +
    1.88 +  if (aContent == mTextContent) {
    1.89 +    nsIFrame * parentFrame = mFrames.FirstChild();
    1.90 +
    1.91 +    nsPresContext* presContext = PresContext();
    1.92 +    nsRefPtr<nsStyleContext> textStyleContext;
    1.93 +    textStyleContext = presContext->StyleSet()->
    1.94 +      ResolveStyleForNonElement(mStyleContext);
    1.95 +
    1.96 +    newFrame = NS_NewTextFrame(presContext->PresShell(), textStyleContext);
    1.97 +    // initialize the text frame
    1.98 +    newFrame->Init(mTextContent, parentFrame, nullptr);
    1.99 +    mTextContent->SetPrimaryFrame(newFrame);
   1.100 +  }
   1.101 +
   1.102 +  return newFrame;
   1.103 +}
   1.104 +
   1.105 +NS_QUERYFRAME_HEAD(nsGfxButtonControlFrame)
   1.106 +  NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
   1.107 +NS_QUERYFRAME_TAIL_INHERITING(nsHTMLButtonControlFrame)
   1.108 +
   1.109 +// Initially we hardcoded the default strings here.
   1.110 +// Next, we used html.css to store the default label for various types
   1.111 +// of buttons. (nsGfxButtonControlFrame::DoNavQuirksReflow rev 1.20)
   1.112 +// However, since html.css is not internationalized, we now grab the default
   1.113 +// label from a string bundle as is done for all other UI strings.
   1.114 +// See bug 16999 for further details.
   1.115 +nsresult
   1.116 +nsGfxButtonControlFrame::GetDefaultLabel(nsXPIDLString& aString) const
   1.117 +{
   1.118 +  nsCOMPtr<nsIFormControl> form = do_QueryInterface(mContent);
   1.119 +  NS_ENSURE_TRUE(form, NS_ERROR_UNEXPECTED);
   1.120 +
   1.121 +  int32_t type = form->GetType();
   1.122 +  const char *prop;
   1.123 +  if (type == NS_FORM_INPUT_RESET) {
   1.124 +    prop = "Reset";
   1.125 +  }
   1.126 +  else if (type == NS_FORM_INPUT_SUBMIT) {
   1.127 +    prop = "Submit";
   1.128 +  }
   1.129 +  else {
   1.130 +    aString.Truncate();
   1.131 +    return NS_OK;
   1.132 +  }
   1.133 +
   1.134 +  return nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
   1.135 +                                            prop, aString);
   1.136 +}
   1.137 +
   1.138 +nsresult
   1.139 +nsGfxButtonControlFrame::GetLabel(nsXPIDLString& aLabel)
   1.140 +{
   1.141 +  // Get the text from the "value" property on our content if there is
   1.142 +  // one; otherwise set it to a default value (localized).
   1.143 +  nsresult rv;
   1.144 +  nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
   1.145 +  if (mContent->HasAttr(kNameSpaceID_None, nsGkAtoms::value) && elt) {
   1.146 +    rv = elt->GetValue(aLabel);
   1.147 +  } else {
   1.148 +    // Generate localized label.
   1.149 +    // We can't make any assumption as to what the default would be
   1.150 +    // because the value is localized for non-english platforms, thus
   1.151 +    // it might not be the string "Reset", "Submit Query", or "Browse..."
   1.152 +    rv = GetDefaultLabel(aLabel);
   1.153 +  }
   1.154 +
   1.155 +  NS_ENSURE_SUCCESS(rv, rv);
   1.156 +
   1.157 +  // Compress whitespace out of label if needed.
   1.158 +  if (!StyleText()->WhiteSpaceIsSignificant()) {
   1.159 +    aLabel.CompressWhitespace();
   1.160 +  } else if (aLabel.Length() > 2 && aLabel.First() == ' ' &&
   1.161 +             aLabel.CharAt(aLabel.Length() - 1) == ' ') {
   1.162 +    // This is a bit of a hack.  The reason this is here is as follows: we now
   1.163 +    // have default padding on our buttons to make them non-ugly.
   1.164 +    // Unfortunately, IE-windows does not have such padding, so people will
   1.165 +    // stick values like " ok " (with the spaces) in the buttons in an attempt
   1.166 +    // to make them look decent.  Unfortunately, if they do this the button
   1.167 +    // looks way too big in Mozilla.  Worse yet, if they do this _and_ set a
   1.168 +    // fixed width for the button we run into trouble because our focus-rect
   1.169 +    // border/padding and outer border take up 10px of the horizontal button
   1.170 +    // space or so; the result is that the text is misaligned, even with the
   1.171 +    // recentering we do in nsHTMLButtonControlFrame::Reflow.  So to solve
   1.172 +    // this, even if the whitespace is significant, single leading and trailing
   1.173 +    // _spaces_ (and not other whitespace) are removed.  The proper solution,
   1.174 +    // of course, is to not have the focus rect painting taking up 6px of
   1.175 +    // horizontal space. We should do that instead (via XBL form controls or
   1.176 +    // changing the renderer) and remove this.
   1.177 +    aLabel.Cut(0, 1);
   1.178 +    aLabel.Truncate(aLabel.Length() - 1);
   1.179 +  }
   1.180 +
   1.181 +  return NS_OK;
   1.182 +}
   1.183 +
   1.184 +nsresult
   1.185 +nsGfxButtonControlFrame::AttributeChanged(int32_t         aNameSpaceID,
   1.186 +                                          nsIAtom*        aAttribute,
   1.187 +                                          int32_t         aModType)
   1.188 +{
   1.189 +  nsresult rv = NS_OK;
   1.190 +
   1.191 +  // If the value attribute is set, update the text of the label
   1.192 +  if (nsGkAtoms::value == aAttribute) {
   1.193 +    if (mTextContent && mContent) {
   1.194 +      nsXPIDLString label;
   1.195 +      rv = GetLabel(label);
   1.196 +      NS_ENSURE_SUCCESS(rv, rv);
   1.197 +    
   1.198 +      mTextContent->SetText(label, true);
   1.199 +    } else {
   1.200 +      rv = NS_ERROR_UNEXPECTED;
   1.201 +    }
   1.202 +
   1.203 +  // defer to HTMLButtonControlFrame
   1.204 +  } else {
   1.205 +    rv = nsHTMLButtonControlFrame::AttributeChanged(aNameSpaceID, aAttribute, aModType);
   1.206 +  }
   1.207 +  return rv;
   1.208 +}
   1.209 +
   1.210 +bool
   1.211 +nsGfxButtonControlFrame::IsLeaf() const
   1.212 +{
   1.213 +  return true;
   1.214 +}
   1.215 +
   1.216 +nsIFrame*
   1.217 +nsGfxButtonControlFrame::GetContentInsertionFrame()
   1.218 +{
   1.219 +  return this;
   1.220 +}
   1.221 +
   1.222 +nsresult
   1.223 +nsGfxButtonControlFrame::HandleEvent(nsPresContext* aPresContext, 
   1.224 +                                     WidgetGUIEvent* aEvent,
   1.225 +                                     nsEventStatus* aEventStatus)
   1.226 +{
   1.227 +  // Override the HandleEvent to prevent the nsFrame::HandleEvent
   1.228 +  // from being called. The nsFrame::HandleEvent causes the button label
   1.229 +  // to be selected (Drawn with an XOR rectangle over the label)
   1.230 +
   1.231 +  // do we have user-input style?
   1.232 +  const nsStyleUserInterface* uiStyle = StyleUserInterface();
   1.233 +  if (uiStyle->mUserInput == NS_STYLE_USER_INPUT_NONE || uiStyle->mUserInput == NS_STYLE_USER_INPUT_DISABLED)
   1.234 +    return nsFrame::HandleEvent(aPresContext, aEvent, aEventStatus);
   1.235 +  
   1.236 +  return NS_OK;
   1.237 +}

mercurial