Calendar Picker: Add capability to add platform-specific style sheet
[WebKit-https.git] / Source / WebCore / html / shadow / CalendarPickerElement.cpp
1 /*
2  * Copyright (C) 2012 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 "CalendarPickerElement.h"
33
34 #if ENABLE(CALENDAR_PICKER)
35
36 #include "CalendarPicker.h"
37 #include "Chrome.h"
38 #include "ChromeClient.h"
39 #include "DateComponents.h"
40 #include "DocumentWriter.h"
41 #include "Event.h"
42 #include "FrameView.h"
43 #include "HTMLInputElement.h"
44 #include "HTMLNames.h"
45 #include "Language.h"
46 #include "LocalizedDate.h"
47 #include "LocalizedStrings.h"
48 #include "Page.h"
49 #include "RenderDetailsMarker.h"
50 #include "RenderTheme.h"
51 #include <wtf/text/StringBuilder.h>
52
53 using namespace WTF::Unicode;
54
55 namespace WebCore {
56
57 using namespace HTMLNames;
58
59 inline CalendarPickerElement::CalendarPickerElement(Document* document)
60     : HTMLDivElement(divTag, document)
61     , m_popup(0)
62 {
63     setShadowPseudoId("-webkit-calendar-picker-indicator");
64 }
65
66 PassRefPtr<CalendarPickerElement> CalendarPickerElement::create(Document* document)
67 {
68     return adoptRef(new CalendarPickerElement(document));
69 }
70
71 RenderObject* CalendarPickerElement::createRenderer(RenderArena* arena, RenderStyle*)
72 {
73     return new (arena) RenderDetailsMarker(this);
74 }
75
76 inline HTMLInputElement* CalendarPickerElement::hostInput()
77 {
78     ASSERT(shadowAncestorNode());
79     ASSERT(shadowAncestorNode()->hasTagName(inputTag));
80     return static_cast<HTMLInputElement*>(shadowAncestorNode());
81 }
82
83 void CalendarPickerElement::defaultEventHandler(Event* event)
84 {
85     HTMLInputElement* input = hostInput();
86     if (input->readOnly() || input->disabled())
87         return;
88
89     if (event->type() == eventNames().clickEvent) {
90         openPopup();
91         event->setDefaultHandled();
92     }
93
94     if (!event->defaultHandled())
95         HTMLDivElement::defaultEventHandler(event);
96 }
97
98 void CalendarPickerElement::openPopup()
99 {
100     if (m_popup)
101         return;
102     if (!document()->page())
103         return;
104     Chrome* chrome = document()->page()->chrome();
105     if (!chrome)
106         return;
107     if (!document()->view())
108         return;
109     IntRect elementRectInRootView = document()->view()->contentsToRootView(hostInput()->getPixelSnappedRect());
110     m_popup = chrome->client()->openPagePopup(this, elementRectInRootView);
111 }
112
113 void CalendarPickerElement::closePopup()
114 {
115     if (!m_popup)
116         return;
117     if (!document()->page())
118         return;
119     Chrome* chrome = document()->page()->chrome();
120     if (!chrome)
121         return;
122     chrome->client()->closePagePopup(m_popup);
123 }
124
125 void CalendarPickerElement::detach()
126 {
127     closePopup();
128     HTMLDivElement::detach();
129 }
130
131 IntSize CalendarPickerElement::contentSize()
132 {
133     return IntSize(100, 100);
134 }
135
136 #define addLiteral(literal, writer)    writer.addData(literal, sizeof(literal) - 1)
137
138 static inline void addString(const String& str, DocumentWriter& writer)
139 {
140     CString str8 = str.utf8();
141     writer.addData(str8.data(), str8.length());
142 }
143
144 static void addJavaScriptString(const String& str, DocumentWriter& writer)
145 {
146     addLiteral("\"", writer);
147     StringBuilder builder;
148     builder.reserveCapacity(str.length());
149     for (unsigned i = 0; i < str.length(); ++i) {
150         if (str[i] == '\\' || str[i] == '"')
151             builder.append('\\');
152         builder.append(str[i]);
153     }
154     addString(builder.toString(), writer);
155     addLiteral("\"", writer);
156 }
157
158 static void addProperty(const char* name, const String& value, DocumentWriter& writer)
159 {
160     writer.addData(name, strlen(name));
161     addLiteral(": ", writer);
162     addJavaScriptString(value, writer);
163     addLiteral(",\n", writer);
164 }
165
166 static void addProperty(const char* name, unsigned value, DocumentWriter& writer)
167 {
168     writer.addData(name, strlen(name));
169     addLiteral(": ", writer);
170     addString(String::number(value), writer);
171     addLiteral(",\n", writer);
172 }
173
174 static void addProperty(const char* name, bool value, DocumentWriter& writer)
175 {
176     writer.addData(name, strlen(name));
177     addLiteral(": ", writer);
178     if (value)
179         addLiteral("true", writer);
180     else
181         addLiteral("false", writer);
182     addLiteral(",\n", writer);
183 }
184
185 static void addProperty(const char* name, const Vector<String>& values, DocumentWriter& writer)
186 {
187     writer.addData(name, strlen(name));
188     addLiteral(": [", writer);
189     for (unsigned i = 0; i < values.size(); ++i) {
190         if (i)
191             addLiteral(",", writer);
192         addJavaScriptString(values[i], writer);
193     }
194     addLiteral("],\n", writer);
195 }
196
197 void CalendarPickerElement::writeDocument(DocumentWriter& writer)
198 {
199     HTMLInputElement* input = hostInput();
200     DateComponents date;
201     date.setMillisecondsSinceEpochForDate(input->minimum());
202     String minString = date.toString();
203     date.setMillisecondsSinceEpochForDate(input->maximum());
204     String maxString = date.toString();
205     double step;
206     String stepString = input->fastGetAttribute(stepAttr);
207     if (stepString.isEmpty() || !input->getAllowedValueStep(&step))
208         stepString = "1";
209
210     addLiteral("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", writer);
211     writer.addData(calendarPickerCss, sizeof(calendarPickerCss));
212     if (document()->page()) {
213         CString extraStyle = document()->page()->theme()->extraCalendarPickerStyleSheet();
214         if (extraStyle.length())
215             writer.addData(extraStyle.data(), extraStyle.length());
216     }
217     addLiteral("</style></head><body><div id=main>Loading...</div><script>\n"
218                "window.dialogArguments = {\n", writer);
219     addProperty("min", minString, writer);
220     addProperty("max", maxString, writer);
221     addProperty("step", stepString, writer);
222     addProperty("required", input->required(), writer);
223     addProperty("currentValue", input->value(), writer);
224     addProperty("locale", defaultLanguage(), writer);
225     addProperty("todayLabel", calendarTodayText(), writer);
226     addProperty("clearLabel", calendarClearText(), writer);
227     addProperty("weekStartDay", firstDayOfWeek(), writer);
228     addProperty("monthLabels", monthLabels(), writer);
229     addProperty("dayLabels", weekDayShortLabels(), writer);
230     Direction dir = direction(monthLabels()[0][0]);
231     addProperty("isRTL", dir == RightToLeft || dir == RightToLeftArabic, writer);
232     addLiteral("}\n", writer);
233
234     writer.addData(calendarPickerJs, sizeof(calendarPickerJs));
235     addLiteral("</script></body>\n", writer);
236 }
237
238 void CalendarPickerElement::setValueAndClosePopup(int numValue, const String& stringValue)
239 {
240     ASSERT(m_popup);
241     closePopup();
242     if (numValue >= 0)
243         hostInput()->setValue(stringValue, DispatchChangeEvent);
244 }
245
246 void CalendarPickerElement::didClosePopup()
247 {
248     m_popup = 0;
249 }
250
251 }
252
253 #endif