Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / DateTimeChooserImpl.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 #if ENABLE(INPUT_MULTIPLE_FIELDS_UI)
33 #include "web/DateTimeChooserImpl.h"
34
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"
45
46 namespace blink {
47
48 DateTimeChooserImpl::DateTimeChooserImpl(ChromeClientImpl* chromeClient, DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
49     : m_chromeClient(chromeClient)
50     , m_client(client)
51     , m_popup(0)
52     , m_parameters(parameters)
53     , m_locale(Locale::create(parameters.locale))
54 {
55     ASSERT(m_chromeClient);
56     ASSERT(m_client);
57     m_popup = m_chromeClient->openPagePopup(this, m_parameters.anchorRectInRootView);
58 }
59
60 PassRefPtr<DateTimeChooserImpl> DateTimeChooserImpl::create(ChromeClientImpl* chromeClient, DateTimeChooserClient* client, const DateTimeChooserParameters& parameters)
61 {
62     return adoptRef(new DateTimeChooserImpl(chromeClient, client, parameters));
63 }
64
65 DateTimeChooserImpl::~DateTimeChooserImpl()
66 {
67 }
68
69 void DateTimeChooserImpl::endChooser()
70 {
71     if (!m_popup)
72         return;
73     m_chromeClient->closePagePopup(m_popup);
74 }
75
76 IntSize DateTimeChooserImpl::contentSize()
77 {
78     return IntSize(0, 0);
79 }
80
81 static String valueToDateTimeString(double value, AtomicString type)
82 {
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);
94     else
95         ASSERT_NOT_REACHED();
96     return components.type() == DateComponents::Invalid ? String() : components.toString();
97 }
98
99 void DateTimeChooserImpl::writeDocument(SharedBuffer* data)
100 {
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);
112     } else {
113         todayLabelString = locale().queryString(WebLocalizedString::CalendarToday);
114         otherDateLabelString = locale().queryString(WebLocalizedString::OtherDateLabel);
115     }
116
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);
149         }
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);
158     }
159     addString("}\n", data);
160
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);
165 }
166
167 Locale& DateTimeChooserImpl::locale()
168 {
169     return *m_locale;
170 }
171
172 void DateTimeChooserImpl::setValueAndClosePopup(int numValue, const String& stringValue)
173 {
174     RefPtr<DateTimeChooserImpl> protector(this);
175     if (numValue >= 0)
176         setValue(stringValue);
177     endChooser();
178 }
179
180 void DateTimeChooserImpl::setValue(const String& value)
181 {
182     m_client->didChooseValue(value);
183 }
184
185 void DateTimeChooserImpl::closePopup()
186 {
187     endChooser();
188 }
189
190 void DateTimeChooserImpl::didClosePopup()
191 {
192     ASSERT(m_client);
193     m_popup = 0;
194     m_client->didEndChooser();
195 }
196
197 } // namespace blink
198
199 #endif // ENABLE(INPUT_MULTIPLE_FIELDS_UI)