Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / DateTimeChooserImpl.cpp
blob670f97a698355787208dfe17b9f0e154907c691f
1 /*
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
6 * met:
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.
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.
31 #include "config.h"
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/html/forms/DateTimeChooserClient.h"
38 #include "core/layout/LayoutTheme.h"
39 #include "core/page/PagePopup.h"
40 #include "platform/DateComponents.h"
41 #include "platform/Language.h"
42 #include "platform/text/PlatformLocale.h"
43 #include "public/platform/Platform.h"
44 #include "web/ChromeClientImpl.h"
45 #include "web/WebViewImpl.h"
47 namespace blink {
49 DateTimeChooserImpl::DateTimeChooserImpl(ChromeClientImpl* chromeClient, DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
50 : m_chromeClient(chromeClient)
51 , m_client(client)
52 , m_popup(0)
53 , m_parameters(parameters)
54 , m_locale(Locale::create(parameters.locale))
56 ASSERT(m_chromeClient);
57 ASSERT(m_client);
58 m_popup = m_chromeClient->openPagePopup(this);
61 PassRefPtr<DateTimeChooserImpl> DateTimeChooserImpl::create(ChromeClientImpl* chromeClient, DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
63 return adoptRef(new DateTimeChooserImpl(chromeClient, client, parameters));
66 DateTimeChooserImpl::~DateTimeChooserImpl()
70 void DateTimeChooserImpl::endChooser()
72 if (!m_popup)
73 return;
74 m_chromeClient->closePagePopup(m_popup);
77 AXObject* DateTimeChooserImpl::rootAXObject()
79 return m_popup ? m_popup->rootAXObject() : 0;
82 IntSize DateTimeChooserImpl::contentSize()
84 return IntSize(0, 0);
87 static String valueToDateTimeString(double value, AtomicString type)
89 DateComponents components;
90 if (type == InputTypeNames::date)
91 components.setMillisecondsSinceEpochForDate(value);
92 else if (type == InputTypeNames::datetime_local)
93 components.setMillisecondsSinceEpochForDateTimeLocal(value);
94 else if (type == InputTypeNames::month)
95 components.setMonthsSinceEpoch(value);
96 else if (type == InputTypeNames::time)
97 components.setMillisecondsSinceMidnight(value);
98 else if (type == InputTypeNames::week)
99 components.setMillisecondsSinceEpochForWeek(value);
100 else
101 ASSERT_NOT_REACHED();
102 return components.type() == DateComponents::Invalid ? String() : components.toString();
105 void DateTimeChooserImpl::writeDocument(SharedBuffer* data)
107 String stepString = String::number(m_parameters.step);
108 String stepBaseString = String::number(m_parameters.stepBase, 11, WTF::TruncateTrailingZeros);
109 String todayLabelString;
110 String otherDateLabelString;
111 if (m_parameters.type == InputTypeNames::month) {
112 todayLabelString = locale().queryString(WebLocalizedString::ThisMonthButtonLabel);
113 otherDateLabelString = locale().queryString(WebLocalizedString::OtherMonthLabel);
114 } else if (m_parameters.type == InputTypeNames::week) {
115 todayLabelString = locale().queryString(WebLocalizedString::ThisWeekButtonLabel);
116 otherDateLabelString = locale().queryString(WebLocalizedString::OtherWeekLabel);
117 } else {
118 todayLabelString = locale().queryString(WebLocalizedString::CalendarToday);
119 otherDateLabelString = locale().queryString(WebLocalizedString::OtherDateLabel);
122 addString("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", data);
123 data->append(Platform::current()->loadResource("pickerCommon.css"));
124 data->append(Platform::current()->loadResource("pickerButton.css"));
125 data->append(Platform::current()->loadResource("suggestionPicker.css"));
126 data->append(Platform::current()->loadResource("calendarPicker.css"));
127 addString("</style></head><body><div id=main>Loading...</div><script>\n"
128 "window.dialogArguments = {\n", data);
129 addProperty("anchorRectInScreen", m_parameters.anchorRectInScreen, data);
130 addProperty("min", valueToDateTimeString(m_parameters.minimum, m_parameters.type), data);
131 addProperty("max", valueToDateTimeString(m_parameters.maximum, m_parameters.type), data);
132 addProperty("step", stepString, data);
133 addProperty("stepBase", stepBaseString, data);
134 addProperty("required", m_parameters.required, data);
135 addProperty("currentValue", valueToDateTimeString(m_parameters.doubleValue, m_parameters.type), data);
136 addProperty("locale", m_parameters.locale.string(), data);
137 addProperty("todayLabel", todayLabelString, data);
138 addProperty("clearLabel", locale().queryString(WebLocalizedString::CalendarClear), data);
139 addProperty("weekLabel", locale().queryString(WebLocalizedString::WeekNumberLabel), data);
140 addProperty("axShowMonthSelector", locale().queryString(WebLocalizedString::AXCalendarShowMonthSelector), data);
141 addProperty("axShowNextMonth", locale().queryString(WebLocalizedString::AXCalendarShowNextMonth), data);
142 addProperty("axShowPreviousMonth", locale().queryString(WebLocalizedString::AXCalendarShowPreviousMonth), data);
143 addProperty("weekStartDay", m_locale->firstDayOfWeek(), data);
144 addProperty("shortMonthLabels", m_locale->shortMonthLabels(), data);
145 addProperty("dayLabels", m_locale->weekDayShortLabels(), data);
146 addProperty("isLocaleRTL", m_locale->isRTL(), data);
147 addProperty("isRTL", m_parameters.isAnchorElementRTL, data);
148 addProperty("mode", m_parameters.type.string(), data);
149 if (m_parameters.suggestions.size()) {
150 Vector<String> suggestionValues;
151 Vector<String> localizedSuggestionValues;
152 Vector<String> suggestionLabels;
153 for (unsigned i = 0; i < m_parameters.suggestions.size(); i++) {
154 suggestionValues.append(valueToDateTimeString(m_parameters.suggestions[i].value, m_parameters.type));
155 localizedSuggestionValues.append(m_parameters.suggestions[i].localizedValue);
156 suggestionLabels.append(m_parameters.suggestions[i].label);
158 addProperty("suggestionValues", suggestionValues, data);
159 addProperty("localizedSuggestionValues", localizedSuggestionValues, data);
160 addProperty("suggestionLabels", suggestionLabels, data);
161 addProperty("inputWidth", static_cast<unsigned>(m_parameters.anchorRectInRootFrame.width()), data);
162 addProperty("showOtherDateEntry", LayoutTheme::theme().supportsCalendarPicker(m_parameters.type), data);
163 addProperty("otherDateLabel", otherDateLabelString, data);
164 addProperty("suggestionHighlightColor", LayoutTheme::theme().activeListBoxSelectionBackgroundColor().serialized(), data);
165 addProperty("suggestionHighlightTextColor", LayoutTheme::theme().activeListBoxSelectionForegroundColor().serialized(), data);
167 addString("}\n", data);
169 data->append(Platform::current()->loadResource("pickerCommon.js"));
170 data->append(Platform::current()->loadResource("suggestionPicker.js"));
171 data->append(Platform::current()->loadResource("calendarPicker.js"));
172 addString("</script></body>\n", data);
175 Element& DateTimeChooserImpl::ownerElement()
177 return m_client->ownerElement();
180 Locale& DateTimeChooserImpl::locale()
182 return *m_locale;
185 void DateTimeChooserImpl::setValueAndClosePopup(int numValue, const String& stringValue)
187 RefPtr<DateTimeChooserImpl> protector(this);
188 if (numValue >= 0)
189 setValue(stringValue);
190 endChooser();
193 void DateTimeChooserImpl::setValue(const String& value)
195 m_client->didChooseValue(value);
198 void DateTimeChooserImpl::closePopup()
200 endChooser();
203 void DateTimeChooserImpl::didClosePopup()
205 ASSERT(m_client);
206 m_popup = 0;
207 m_client->didEndChooser();
210 } // namespace blink
212 #endif // ENABLE(INPUT_MULTIPLE_FIELDS_UI)