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
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.
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.
27 #include "web/ColorChooserPopupUIController.h"
29 #include "core/frame/FrameView.h"
30 #include "core/html/forms/ColorChooserClient.h"
31 #include "core/page/PagePopup.h"
32 #include "platform/geometry/IntRect.h"
33 #include "public/platform/Platform.h"
34 #include "public/web/WebColorChooser.h"
35 #include "web/ChromeClientImpl.h"
36 #include "web/WebViewImpl.h"
40 // Keep in sync with Actions in colorSuggestionPicker.js.
41 enum ColorPickerPopupAction
{
42 ColorPickerPopupActionChooseOtherColor
= -2,
43 ColorPickerPopupActionCancel
= -1,
44 ColorPickerPopupActionSetValue
= 0
47 ColorChooserPopupUIController::ColorChooserPopupUIController(LocalFrame
* frame
, ChromeClientImpl
* chromeClient
, ColorChooserClient
* client
)
48 : ColorChooserUIController(frame
, client
)
49 , m_chromeClient(chromeClient
)
51 , m_locale(Locale::defaultLocale())
55 ColorChooserPopupUIController::~ColorChooserPopupUIController()
58 // ~ColorChooserUIController ends the ColorChooser.
61 void ColorChooserPopupUIController::openUI()
63 if (m_client
->shouldShowSuggestions())
69 void ColorChooserPopupUIController::endChooser()
72 m_chooser
->endChooser();
77 AXObject
* ColorChooserPopupUIController::rootAXObject()
79 return m_popup
? m_popup
->rootAXObject() : 0;
82 IntSize
ColorChooserPopupUIController::contentSize()
87 void ColorChooserPopupUIController::writeDocument(SharedBuffer
* data
)
89 Vector
<ColorSuggestion
> suggestions
= m_client
->suggestions();
90 Vector
<String
> suggestionValues
;
91 for (unsigned i
= 0; i
< suggestions
.size(); i
++)
92 suggestionValues
.append(suggestions
[i
].color
.serialized());
93 IntRect anchorRectInScreen
= m_chromeClient
->viewportToScreen(m_client
->elementRectRelativeToViewport());
95 PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", data
);
96 data
->append(Platform::current()->loadResource("pickerCommon.css"));
97 data
->append(Platform::current()->loadResource("colorSuggestionPicker.css"));
98 PagePopupClient::addString("</style></head><body><div id=main>Loading...</div><script>\n"
99 "window.dialogArguments = {\n", data
);
100 PagePopupClient::addProperty("values", suggestionValues
, data
);
101 PagePopupClient::addProperty("otherColorLabel", locale().queryString(WebLocalizedString::OtherColorLabel
), data
);
102 addProperty("anchorRectInScreen", anchorRectInScreen
, data
);
103 PagePopupClient::addString("};\n", data
);
104 data
->append(Platform::current()->loadResource("pickerCommon.js"));
105 data
->append(Platform::current()->loadResource("colorSuggestionPicker.js"));
106 PagePopupClient::addString("</script></body>\n", data
);
109 Locale
& ColorChooserPopupUIController::locale()
114 void ColorChooserPopupUIController::setValueAndClosePopup(int numValue
, const String
& stringValue
)
118 if (numValue
== ColorPickerPopupActionSetValue
)
119 setValue(stringValue
);
120 if (numValue
== ColorPickerPopupActionChooseOtherColor
)
125 void ColorChooserPopupUIController::setValue(const String
& value
)
129 bool success
= color
.setFromString(value
);
130 ASSERT_UNUSED(success
, success
);
131 m_client
->didChooseColor(color
);
134 void ColorChooserPopupUIController::didClosePopup()
142 Element
& ColorChooserPopupUIController::ownerElement()
144 return m_client
->ownerElement();
147 void ColorChooserPopupUIController::openPopup()
150 m_popup
= m_chromeClient
->openPagePopup(this);
153 void ColorChooserPopupUIController::closePopup()
157 m_chromeClient
->closePagePopup(m_popup
);