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