2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
33 #include "web/DateTimeChooserImpl.h"
35 #include "core/InputTypeNames.h"
36 #include "core/frame/FrameView.h"
37 #include "core/rendering/RenderTheme.h"
38 #include "platform/DateComponents.h"
39 #include "platform/DateTimeChooserClient.h"
40 #include "platform/Language.h"
41 #include "platform/text/PlatformLocale.h"
42 #include "public/platform/Platform.h"
43 #include "web/ChromeClientImpl.h"
44 #include "web/WebViewImpl.h"
48 DateTimeChooserImpl::DateTimeChooserImpl(ChromeClientImpl* chromeClient, DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
49 : m_chromeClient(chromeClient)
52 , m_parameters(parameters)
53 , m_locale(Locale::create(parameters.locale))
55 ASSERT(m_chromeClient);
57 m_popup = m_chromeClient->openPagePopup(this, m_parameters.anchorRectInRootView);
60 PassRefPtr<DateTimeChooserImpl> DateTimeChooserImpl::create(ChromeClientImpl* chromeClient, DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
62 return adoptRef(new DateTimeChooserImpl(chromeClient, client, parameters));
65 DateTimeChooserImpl::~DateTimeChooserImpl()
69 void DateTimeChooserImpl::endChooser()
73 m_chromeClient->closePagePopup(m_popup);
76 IntSize DateTimeChooserImpl::contentSize()
81 static String valueToDateTimeString(double value, AtomicString type)
83 DateComponents components;
84 if (type == InputTypeNames::date)
85 components.setMillisecondsSinceEpochForDate(value);
86 else if (type == InputTypeNames::datetime_local)
87 components.setMillisecondsSinceEpochForDateTimeLocal(value);
88 else if (type == InputTypeNames::month)
89 components.setMonthsSinceEpoch(value);
90 else if (type == InputTypeNames::time)
91 components.setMillisecondsSinceMidnight(value);
92 else if (type == InputTypeNames::week)
93 components.setMillisecondsSinceEpochForWeek(value);
96 return components.type() == DateComponents::Invalid ? String() : components.toString();
99 void DateTimeChooserImpl::writeDocument(SharedBuffer* data)
101 String stepString = String::number(m_parameters.step);
102 String stepBaseString = String::number(m_parameters.stepBase, 11, WTF::TruncateTrailingZeros);
103 IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_parameters.anchorRectInRootView);
104 String todayLabelString;
105 String otherDateLabelString;
106 if (m_parameters.type == InputTypeNames::month) {
107 todayLabelString = locale().queryString(WebLocalizedString::ThisMonthButtonLabel);
108 otherDateLabelString = locale().queryString(WebLocalizedString::OtherMonthLabel);
109 } else if (m_parameters.type == InputTypeNames::week) {
110 todayLabelString = locale().queryString(WebLocalizedString::ThisWeekButtonLabel);
111 otherDateLabelString = locale().queryString(WebLocalizedString::OtherWeekLabel);
113 todayLabelString = locale().queryString(WebLocalizedString::CalendarToday);
114 otherDateLabelString = locale().queryString(WebLocalizedString::OtherDateLabel);
117 addString("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", data);
118 data->append(Platform::current()->loadResource("pickerCommon.css"));
119 data->append(Platform::current()->loadResource("pickerButton.css"));
120 data->append(Platform::current()->loadResource("suggestionPicker.css"));
121 data->append(Platform::current()->loadResource("calendarPicker.css"));
122 addString("</style></head><body><div id=main>Loading...</div><script>\n"
123 "window.dialogArguments = {\n", data);
124 addProperty("anchorRectInScreen", anchorRectInScreen, data);
125 addProperty("min", valueToDateTimeString(m_parameters.minimum, m_parameters.type), data);
126 addProperty("max", valueToDateTimeString(m_parameters.maximum, m_parameters.type), data);
127 addProperty("step", stepString, data);
128 addProperty("stepBase", stepBaseString, data);
129 addProperty("required", m_parameters.required, data);
130 addProperty("currentValue", valueToDateTimeString(m_parameters.doubleValue, m_parameters.type), data);
131 addProperty("locale", m_parameters.locale.string(), data);
132 addProperty("todayLabel", todayLabelString, data);
133 addProperty("clearLabel", locale().queryString(WebLocalizedString::CalendarClear), data);
134 addProperty("weekLabel", locale().queryString(WebLocalizedString::WeekNumberLabel), data);
135 addProperty("weekStartDay", m_locale->firstDayOfWeek(), data);
136 addProperty("shortMonthLabels", m_locale->shortMonthLabels(), data);
137 addProperty("dayLabels", m_locale->weekDayShortLabels(), data);
138 addProperty("isLocaleRTL", m_locale->isRTL(), data);
139 addProperty("isRTL", m_parameters.isAnchorElementRTL, data);
140 addProperty("mode", m_parameters.type.string(), data);
141 if (m_parameters.suggestions.size()) {
142 Vector<String> suggestionValues;
143 Vector<String> localizedSuggestionValues;
144 Vector<String> suggestionLabels;
145 for (unsigned i = 0; i < m_parameters.suggestions.size(); i++) {
146 suggestionValues.append(valueToDateTimeString(m_parameters.suggestions[i].value, m_parameters.type));
147 localizedSuggestionValues.append(m_parameters.suggestions[i].localizedValue);
148 suggestionLabels.append(m_parameters.suggestions[i].label);
150 addProperty("suggestionValues", suggestionValues, data);
151 addProperty("localizedSuggestionValues", localizedSuggestionValues, data);
152 addProperty("suggestionLabels", suggestionLabels, data);
153 addProperty("inputWidth", static_cast<unsigned>(m_parameters.anchorRectInRootView.width()), data);
154 addProperty("showOtherDateEntry", RenderTheme::theme().supportsCalendarPicker(m_parameters.type), data);
155 addProperty("otherDateLabel", otherDateLabelString, data);
156 addProperty("suggestionHighlightColor", RenderTheme::theme().activeListBoxSelectionBackgroundColor().serialized(), data);
157 addProperty("suggestionHighlightTextColor", RenderTheme::theme().activeListBoxSelectionForegroundColor().serialized(), data);
159 addString("}\n", data);
161 data->append(Platform::current()->loadResource("pickerCommon.js"));
162 data->append(Platform::current()->loadResource("suggestionPicker.js"));
163 data->append(Platform::current()->loadResource("calendarPicker.js"));
164 addString("</script></body>\n", data);
167 Locale& DateTimeChooserImpl::locale()
172 void DateTimeChooserImpl::setValueAndClosePopup(int numValue, const String& stringValue)
174 RefPtr<DateTimeChooserImpl> protector(this);
176 setValue(stringValue);
180 void DateTimeChooserImpl::setValue(const String& value)
182 m_client->didChooseValue(value);
185 void DateTimeChooserImpl::closePopup()
190 void DateTimeChooserImpl::didClosePopup()
194 m_client->didEndChooser();
199 #endif // ENABLE(INPUT_MULTIPLE_FIELDS_UI)