initial import
[vuplus_webkit] / Source / WebCore / html / ColorInputType.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "ColorInputType.h"
33
34 #include "Chrome.h"
35 #include "Color.h"
36 #include "HTMLDivElement.h"
37 #include "HTMLInputElement.h"
38 #include "MouseEvent.h"
39 #include "ScriptController.h"
40 #include "ShadowRoot.h"
41 #include <wtf/PassOwnPtr.h>
42 #include <wtf/text/WTFString.h>
43
44 #if ENABLE(INPUT_COLOR)
45
46 namespace WebCore {
47
48 static bool isValidColorString(const String& value)
49 {
50     if (value.isEmpty())
51         return false;
52     if (value[0] != '#')
53         return false;
54
55     // We don't accept #rgb and #aarrggbb formats.
56     if (value.length() != 7)
57         return false;
58     Color color(value);
59     return color.isValid() && !color.hasAlpha();
60 }
61
62 PassOwnPtr<InputType> ColorInputType::create(HTMLInputElement* element)
63 {
64     return adoptPtr(new ColorInputType(element));
65 }
66
67 ColorInputType::~ColorInputType()
68 {
69     cleanupColorChooserIfCurrentClient();
70 }
71
72 bool ColorInputType::isColorControl() const
73 {
74     return true;
75 }
76
77 const AtomicString& ColorInputType::formControlType() const
78 {
79     return InputTypeNames::color();
80 }
81
82 bool ColorInputType::supportsRequired() const
83 {
84     return false;
85 }
86
87 String ColorInputType::fallbackValue()
88 {
89     return String("#000000");
90 }
91
92 String ColorInputType::sanitizeValue(const String& proposedValue)
93 {
94     if (proposedValue.isNull())
95         return proposedValue;
96
97     if (!isValidColorString(proposedValue))
98         return fallbackValue();
99
100     return proposedValue.lower();
101 }
102
103 Color ColorInputType::valueAsColor() const
104 {
105     return Color(element()->value());
106 }
107
108 void ColorInputType::createShadowSubtree()
109 {
110     Document* document = element()->document();
111     RefPtr<HTMLDivElement> wrapperElement = HTMLDivElement::create(document);
112     wrapperElement->setShadowPseudoId("-webkit-color-swatch-wrapper");
113     RefPtr<HTMLDivElement> colorSwatch = HTMLDivElement::create(document);
114     colorSwatch->setShadowPseudoId("-webkit-color-swatch");
115     ExceptionCode ec = 0;
116     wrapperElement->appendChild(colorSwatch.release(), ec);
117     ASSERT(!ec);
118     element()->ensureShadowRoot()->appendChild(wrapperElement.release(), ec);
119     ASSERT(!ec);
120     
121     updateColorSwatch();
122 }
123
124 void ColorInputType::setValue(const String& value, bool valueChanged, bool sendChangeEvent);
125 {
126     InputType::setValue(value, valueChanged, sendChangeEvent);
127
128     if (!valueChanged)
129         return;
130
131     updateColorSwatch();
132     if (ColorChooser::chooser()->client() == this) {
133         if (Chrome* chrome = this->chrome())
134             chrome->setSelectedColorInColorChooser(valueAsColor());
135     }
136 }
137
138 void ColorInputType::handleClickEvent(MouseEvent* event)
139 {
140     if (event->isSimulated())
141         return;
142
143     if (element()->disabled() || element()->readOnly())
144         return;
145
146     if (Chrome* chrome = this->chrome()) {
147         ColorChooser::chooser()->connectClient(this);
148         chrome->openColorChooser(ColorChooser::chooser(), valueAsColor());
149     }
150     event->setDefaultHandled();
151 }
152
153 void ColorInputType::handleDOMActivateEvent(Event* event)
154 {
155     if (element()->disabled() || element()->readOnly() || !element()->renderer())
156         return;
157
158     if (!ScriptController::processingUserGesture())
159         return;
160
161     if (Chrome* chrome = this->chrome()) {
162         ColorChooser::chooser()->connectClient(this);
163         chrome->openColorChooser(ColorChooser::chooser(), valueAsColor());
164     }
165     event->setDefaultHandled();
166 }
167
168 void ColorInputType::detach()
169 {
170     cleanupColorChooserIfCurrentClient();
171 }
172
173 void ColorInputType::didChooseColor(const Color& color)
174 {
175     if (element()->disabled() || element()->readOnly() || color == valueAsColor())
176         return;
177     element()->setValueFromRenderer(color.serialized());
178     updateColorSwatch();
179     element()->dispatchFormControlChangeEvent();
180 }
181
182 bool ColorInputType::isColorInputType() const
183 {
184     return true;
185 }
186
187 void ColorInputType::cleanupColorChooserIfCurrentClient() const
188 {
189     if (ColorChooser::chooser()->client() != this)
190         return;
191     if (Chrome* chrome = this->chrome())
192         chrome->cleanupColorChooser();
193 }
194
195 void ColorInputType::updateColorSwatch()
196 {
197     HTMLElement* colorSwatch = shadowColorSwatch();
198     if (!colorSwatch)
199         return;
200
201     ExceptionCode ec;
202     colorSwatch->style()->setProperty("background-color", element()->value(), ec);
203 }
204
205 HTMLElement* ColorInputType::shadowColorSwatch() const
206 {
207     ShadowRoot* shadow = element()->shadowRoot();
208     return shadow ? toHTMLElement(shadow->firstChild()->firstChild()) : 0;
209 }
210
211 } // namespace WebCore
212
213 #endif // ENABLE(INPUT_COLOR)