Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / ColorChooserPopupUIController.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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "web/ColorChooserPopupUIController.h"
28
29 #include "core/frame/FrameView.h"
30 #include "platform/ColorChooserClient.h"
31 #include "platform/geometry/IntRect.h"
32 #include "public/platform/Platform.h"
33 #include "public/web/WebColorChooser.h"
34 #include "web/ChromeClientImpl.h"
35 #include "web/WebViewImpl.h"
36
37 namespace blink {
38
39 // Keep in sync with Actions in colorSuggestionPicker.js.
40 enum ColorPickerPopupAction {
41     ColorPickerPopupActionChooseOtherColor = -2,
42     ColorPickerPopupActionCancel = -1,
43     ColorPickerPopupActionSetValue = 0
44 };
45
46 ColorChooserPopupUIController::ColorChooserPopupUIController(LocalFrame* frame, ChromeClientImpl* chromeClient, ColorChooserClient* client)
47     : ColorChooserUIController(frame, client)
48     , m_chromeClient(chromeClient)
49     , m_client(client)
50     , m_popup(0)
51     , m_locale(Locale::defaultLocale())
52 {
53 }
54
55 ColorChooserPopupUIController::~ColorChooserPopupUIController()
56 {
57 }
58
59 void ColorChooserPopupUIController::openUI()
60 {
61     if (m_client->shouldShowSuggestions())
62         openPopup();
63     else
64         openColorChooser();
65 }
66
67 void ColorChooserPopupUIController::endChooser()
68 {
69     if (m_chooser)
70         m_chooser->endChooser();
71     if (m_popup)
72         closePopup();
73 }
74
75 IntSize ColorChooserPopupUIController::contentSize()
76 {
77     return IntSize(0, 0);
78 }
79
80 void ColorChooserPopupUIController::writeDocument(SharedBuffer* data)
81 {
82     Vector<ColorSuggestion> suggestions = m_client->suggestions();
83     Vector<String> suggestionValues;
84     for (unsigned i = 0; i < suggestions.size(); i++)
85         suggestionValues.append(suggestions[i].color.serialized());
86     IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_client->elementRectRelativeToRootView());
87
88     PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", data);
89     data->append(Platform::current()->loadResource("pickerCommon.css"));
90     data->append(Platform::current()->loadResource("colorSuggestionPicker.css"));
91     PagePopupClient::addString("</style></head><body><div id=main>Loading...</div><script>\n"
92         "window.dialogArguments = {\n", data);
93     PagePopupClient::addProperty("values", suggestionValues, data);
94     PagePopupClient::addProperty("otherColorLabel", locale().queryString(WebLocalizedString::OtherColorLabel), data);
95     addProperty("anchorRectInScreen", anchorRectInScreen, data);
96     PagePopupClient::addString("};\n", data);
97     data->append(Platform::current()->loadResource("pickerCommon.js"));
98     data->append(Platform::current()->loadResource("colorSuggestionPicker.js"));
99     PagePopupClient::addString("</script></body>\n", data);
100 }
101
102 Locale& ColorChooserPopupUIController::locale()
103 {
104     return m_locale;
105 }
106
107 void ColorChooserPopupUIController::setValueAndClosePopup(int numValue, const String& stringValue)
108 {
109     ASSERT(m_popup);
110     ASSERT(m_client);
111     if (numValue == ColorPickerPopupActionSetValue)
112         setValue(stringValue);
113     if (numValue == ColorPickerPopupActionChooseOtherColor)
114         openColorChooser();
115     closePopup();
116 }
117
118 void ColorChooserPopupUIController::setValue(const String& value)
119 {
120     ASSERT(m_client);
121     Color color;
122     bool success = color.setFromString(value);
123     ASSERT_UNUSED(success, success);
124     m_client->didChooseColor(color);
125 }
126
127 void ColorChooserPopupUIController::didClosePopup()
128 {
129     m_popup = 0;
130
131     if (!m_chooser)
132         didEndChooser();
133 }
134
135
136 void ColorChooserPopupUIController::openPopup()
137 {
138     ASSERT(!m_popup);
139     m_popup = m_chromeClient->openPagePopup(this, m_client->elementRectRelativeToRootView());
140 }
141
142 void ColorChooserPopupUIController::closePopup()
143 {
144     if (!m_popup)
145         return;
146     m_chromeClient->closePagePopup(m_popup);
147 }
148
149 } // namespace blink