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