layout/forms/nsColorControlFrame.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/forms/nsColorControlFrame.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,142 @@
     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 "nsColorControlFrame.h"
    1.10 +
    1.11 +#include "nsContentCreatorFunctions.h"
    1.12 +#include "nsContentList.h"
    1.13 +#include "nsContentUtils.h"
    1.14 +#include "nsFormControlFrame.h"
    1.15 +#include "nsGkAtoms.h"
    1.16 +#include "nsIDOMHTMLInputElement.h"
    1.17 +#include "nsIDOMNode.h"
    1.18 +#include "nsIFormControl.h"
    1.19 +#include "nsStyleSet.h"
    1.20 +
    1.21 +using mozilla::dom::Element;
    1.22 +
    1.23 +nsColorControlFrame::nsColorControlFrame(nsStyleContext* aContext):
    1.24 +  nsColorControlFrameSuper(aContext)
    1.25 +{
    1.26 +}
    1.27 +
    1.28 +nsIFrame*
    1.29 +NS_NewColorControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
    1.30 +{
    1.31 +  return new (aPresShell) nsColorControlFrame(aContext);
    1.32 +}
    1.33 +
    1.34 +NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame)
    1.35 +
    1.36 +NS_QUERYFRAME_HEAD(nsColorControlFrame)
    1.37 +  NS_QUERYFRAME_ENTRY(nsColorControlFrame)
    1.38 +  NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
    1.39 +NS_QUERYFRAME_TAIL_INHERITING(nsColorControlFrameSuper)
    1.40 +
    1.41 +
    1.42 +void nsColorControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
    1.43 +{
    1.44 +  nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
    1.45 +  nsContentUtils::DestroyAnonymousContent(&mColorContent);
    1.46 +  nsColorControlFrameSuper::DestroyFrom(aDestructRoot);
    1.47 +}
    1.48 +
    1.49 +nsIAtom*
    1.50 +nsColorControlFrame::GetType() const
    1.51 +{
    1.52 +  return nsGkAtoms::colorControlFrame;
    1.53 +}
    1.54 +
    1.55 +#ifdef DEBUG_FRAME_DUMP
    1.56 +nsresult
    1.57 +nsColorControlFrame::GetFrameName(nsAString& aResult) const
    1.58 +{
    1.59 +  return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult);
    1.60 +}
    1.61 +#endif
    1.62 +
    1.63 +// Create the color area for the button.
    1.64 +// The frame will be generated by the frame constructor.
    1.65 +nsresult
    1.66 +nsColorControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
    1.67 +{
    1.68 +  nsCOMPtr<nsIDocument> doc = mContent->GetCurrentDoc();
    1.69 +  mColorContent = doc->CreateHTMLElement(nsGkAtoms::div);
    1.70 +
    1.71 +  // Mark the element to be native anonymous before setting any attributes.
    1.72 +  mColorContent->SetIsNativeAnonymousRoot();
    1.73 +
    1.74 +  nsresult rv = UpdateColor();
    1.75 +  NS_ENSURE_SUCCESS(rv, rv);
    1.76 +
    1.77 +  nsCSSPseudoElements::Type pseudoType = nsCSSPseudoElements::ePseudo_mozColorSwatch;
    1.78 +  nsRefPtr<nsStyleContext> newStyleContext = PresContext()->StyleSet()->
    1.79 +    ResolvePseudoElementStyle(mContent->AsElement(), pseudoType,
    1.80 +                              StyleContext(), mColorContent->AsElement());
    1.81 +  if (!aElements.AppendElement(ContentInfo(mColorContent, newStyleContext))) {
    1.82 +    return NS_ERROR_OUT_OF_MEMORY;
    1.83 +  }
    1.84 +
    1.85 +  return NS_OK;
    1.86 +}
    1.87 +
    1.88 +void
    1.89 +nsColorControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
    1.90 +                                              uint32_t aFilter)
    1.91 +{
    1.92 +  aElements.MaybeAppendElement(mColorContent);
    1.93 +}
    1.94 +
    1.95 +nsresult
    1.96 +nsColorControlFrame::UpdateColor()
    1.97 +{
    1.98 +  // Get the color from the "value" property of our content; it will return the
    1.99 +  // default color (through the sanitization algorithm) if there is none.
   1.100 +  nsAutoString color;
   1.101 +  nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
   1.102 +  elt->GetValue(color);
   1.103 +  MOZ_ASSERT(!color.IsEmpty(),
   1.104 +             "Content node's GetValue() should return a valid color string "
   1.105 +             "(the default color, in case no valid color is set)");
   1.106 +
   1.107 +  // Set the background-color style property of the swatch element to this color
   1.108 +  return mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
   1.109 +      NS_LITERAL_STRING("background-color:") + color, true);
   1.110 +}
   1.111 +
   1.112 +nsresult
   1.113 +nsColorControlFrame::AttributeChanged(int32_t  aNameSpaceID,
   1.114 +                                      nsIAtom* aAttribute,
   1.115 +                                      int32_t  aModType)
   1.116 +{
   1.117 +  NS_ASSERTION(mColorContent, "The color div must exist");
   1.118 +
   1.119 +  // If the value attribute is set, update the color box, but only if we're
   1.120 +  // still a color control, which might not be the case if the type attribute
   1.121 +  // was removed/changed.
   1.122 +  nsCOMPtr<nsIFormControl> fctrl = do_QueryInterface(GetContent());
   1.123 +  if (fctrl->GetType() == NS_FORM_INPUT_COLOR &&
   1.124 +      aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
   1.125 +    UpdateColor();
   1.126 +  }
   1.127 +  return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID, aAttribute,
   1.128 +                                                    aModType);
   1.129 +}
   1.130 +
   1.131 +nsIFrame*
   1.132 +nsColorControlFrame::GetContentInsertionFrame()
   1.133 +{
   1.134 +  return this;
   1.135 +}
   1.136 +
   1.137 +Element*
   1.138 +nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType)
   1.139 +{
   1.140 +  if (aType == nsCSSPseudoElements::ePseudo_mozColorSwatch) {
   1.141 +    return mColorContent;
   1.142 +  }
   1.143 +
   1.144 +  return nsContainerFrame::GetPseudoElement(aType);
   1.145 +}

mercurial