layout/forms/nsColorControlFrame.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsColorControlFrame.h"
michael@0 7
michael@0 8 #include "nsContentCreatorFunctions.h"
michael@0 9 #include "nsContentList.h"
michael@0 10 #include "nsContentUtils.h"
michael@0 11 #include "nsFormControlFrame.h"
michael@0 12 #include "nsGkAtoms.h"
michael@0 13 #include "nsIDOMHTMLInputElement.h"
michael@0 14 #include "nsIDOMNode.h"
michael@0 15 #include "nsIFormControl.h"
michael@0 16 #include "nsStyleSet.h"
michael@0 17
michael@0 18 using mozilla::dom::Element;
michael@0 19
michael@0 20 nsColorControlFrame::nsColorControlFrame(nsStyleContext* aContext):
michael@0 21 nsColorControlFrameSuper(aContext)
michael@0 22 {
michael@0 23 }
michael@0 24
michael@0 25 nsIFrame*
michael@0 26 NS_NewColorControlFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
michael@0 27 {
michael@0 28 return new (aPresShell) nsColorControlFrame(aContext);
michael@0 29 }
michael@0 30
michael@0 31 NS_IMPL_FRAMEARENA_HELPERS(nsColorControlFrame)
michael@0 32
michael@0 33 NS_QUERYFRAME_HEAD(nsColorControlFrame)
michael@0 34 NS_QUERYFRAME_ENTRY(nsColorControlFrame)
michael@0 35 NS_QUERYFRAME_ENTRY(nsIAnonymousContentCreator)
michael@0 36 NS_QUERYFRAME_TAIL_INHERITING(nsColorControlFrameSuper)
michael@0 37
michael@0 38
michael@0 39 void nsColorControlFrame::DestroyFrom(nsIFrame* aDestructRoot)
michael@0 40 {
michael@0 41 nsFormControlFrame::RegUnRegAccessKey(static_cast<nsIFrame*>(this), false);
michael@0 42 nsContentUtils::DestroyAnonymousContent(&mColorContent);
michael@0 43 nsColorControlFrameSuper::DestroyFrom(aDestructRoot);
michael@0 44 }
michael@0 45
michael@0 46 nsIAtom*
michael@0 47 nsColorControlFrame::GetType() const
michael@0 48 {
michael@0 49 return nsGkAtoms::colorControlFrame;
michael@0 50 }
michael@0 51
michael@0 52 #ifdef DEBUG_FRAME_DUMP
michael@0 53 nsresult
michael@0 54 nsColorControlFrame::GetFrameName(nsAString& aResult) const
michael@0 55 {
michael@0 56 return MakeFrameName(NS_LITERAL_STRING("ColorControl"), aResult);
michael@0 57 }
michael@0 58 #endif
michael@0 59
michael@0 60 // Create the color area for the button.
michael@0 61 // The frame will be generated by the frame constructor.
michael@0 62 nsresult
michael@0 63 nsColorControlFrame::CreateAnonymousContent(nsTArray<ContentInfo>& aElements)
michael@0 64 {
michael@0 65 nsCOMPtr<nsIDocument> doc = mContent->GetCurrentDoc();
michael@0 66 mColorContent = doc->CreateHTMLElement(nsGkAtoms::div);
michael@0 67
michael@0 68 // Mark the element to be native anonymous before setting any attributes.
michael@0 69 mColorContent->SetIsNativeAnonymousRoot();
michael@0 70
michael@0 71 nsresult rv = UpdateColor();
michael@0 72 NS_ENSURE_SUCCESS(rv, rv);
michael@0 73
michael@0 74 nsCSSPseudoElements::Type pseudoType = nsCSSPseudoElements::ePseudo_mozColorSwatch;
michael@0 75 nsRefPtr<nsStyleContext> newStyleContext = PresContext()->StyleSet()->
michael@0 76 ResolvePseudoElementStyle(mContent->AsElement(), pseudoType,
michael@0 77 StyleContext(), mColorContent->AsElement());
michael@0 78 if (!aElements.AppendElement(ContentInfo(mColorContent, newStyleContext))) {
michael@0 79 return NS_ERROR_OUT_OF_MEMORY;
michael@0 80 }
michael@0 81
michael@0 82 return NS_OK;
michael@0 83 }
michael@0 84
michael@0 85 void
michael@0 86 nsColorControlFrame::AppendAnonymousContentTo(nsBaseContentList& aElements,
michael@0 87 uint32_t aFilter)
michael@0 88 {
michael@0 89 aElements.MaybeAppendElement(mColorContent);
michael@0 90 }
michael@0 91
michael@0 92 nsresult
michael@0 93 nsColorControlFrame::UpdateColor()
michael@0 94 {
michael@0 95 // Get the color from the "value" property of our content; it will return the
michael@0 96 // default color (through the sanitization algorithm) if there is none.
michael@0 97 nsAutoString color;
michael@0 98 nsCOMPtr<nsIDOMHTMLInputElement> elt = do_QueryInterface(mContent);
michael@0 99 elt->GetValue(color);
michael@0 100 MOZ_ASSERT(!color.IsEmpty(),
michael@0 101 "Content node's GetValue() should return a valid color string "
michael@0 102 "(the default color, in case no valid color is set)");
michael@0 103
michael@0 104 // Set the background-color style property of the swatch element to this color
michael@0 105 return mColorContent->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
michael@0 106 NS_LITERAL_STRING("background-color:") + color, true);
michael@0 107 }
michael@0 108
michael@0 109 nsresult
michael@0 110 nsColorControlFrame::AttributeChanged(int32_t aNameSpaceID,
michael@0 111 nsIAtom* aAttribute,
michael@0 112 int32_t aModType)
michael@0 113 {
michael@0 114 NS_ASSERTION(mColorContent, "The color div must exist");
michael@0 115
michael@0 116 // If the value attribute is set, update the color box, but only if we're
michael@0 117 // still a color control, which might not be the case if the type attribute
michael@0 118 // was removed/changed.
michael@0 119 nsCOMPtr<nsIFormControl> fctrl = do_QueryInterface(GetContent());
michael@0 120 if (fctrl->GetType() == NS_FORM_INPUT_COLOR &&
michael@0 121 aNameSpaceID == kNameSpaceID_None && nsGkAtoms::value == aAttribute) {
michael@0 122 UpdateColor();
michael@0 123 }
michael@0 124 return nsColorControlFrameSuper::AttributeChanged(aNameSpaceID, aAttribute,
michael@0 125 aModType);
michael@0 126 }
michael@0 127
michael@0 128 nsIFrame*
michael@0 129 nsColorControlFrame::GetContentInsertionFrame()
michael@0 130 {
michael@0 131 return this;
michael@0 132 }
michael@0 133
michael@0 134 Element*
michael@0 135 nsColorControlFrame::GetPseudoElement(nsCSSPseudoElements::Type aType)
michael@0 136 {
michael@0 137 if (aType == nsCSSPseudoElements::ePseudo_mozColorSwatch) {
michael@0 138 return mColorContent;
michael@0 139 }
michael@0 140
michael@0 141 return nsContainerFrame::GetPseudoElement(aType);
michael@0 142 }

mercurial