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