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 "ColorChooserPopupUIController.h" 28 29 #include "ChromeClientImpl.h" 30 #include "ColorSuggestionPicker.h" 31 #include "PickerCommon.h" 32 #include "WebColorChooser.h" 33 #include "WebViewImpl.h" 34 #include "core/page/FrameView.h" 35 #include "core/platform/ColorChooserClient.h" 36 #include "core/platform/LocalizedStrings.h" 37 #include "core/platform/graphics/IntRect.h" 38 #include "public/platform/Platform.h" 39 #include "public/platform/WebLocalizedString.h" 40 41 using namespace WebCore; 42 43 namespace WebKit { 44 45 // Keep in sync with Actions in colorSuggestionPicker.js. 46 enum ColorPickerPopupAction { 47 ColorPickerPopupActionChooseOtherColor = -2, 48 ColorPickerPopupActionCancel = -1, 49 ColorPickerPopupActionSetValue = 0 50 }; 51 52 ColorChooserPopupUIController::ColorChooserPopupUIController(ChromeClientImpl* chromeClient, ColorChooserClient* client) 53 : ColorChooserUIController(chromeClient, client) 54 , m_chromeClient(chromeClient) 55 , m_client(client) 56 , m_popup(0) 57 , m_locale(Locale::createDefault()) 58 { 59 } 60 61 ColorChooserPopupUIController::~ColorChooserPopupUIController() 62 { 63 } 64 65 void ColorChooserPopupUIController::openUI() 66 { 67 if (m_client->shouldShowSuggestions()) 68 openPopup(); 69 else 70 openColorChooser(); 71 } 72 73 void ColorChooserPopupUIController::endChooser() 74 { 75 if (m_chooser) 76 m_chooser->endChooser(); 77 if (m_popup) 78 closePopup(); 79 } 80 81 IntSize ColorChooserPopupUIController::contentSize() 82 { 83 return IntSize(0, 0); 84 } 85 86 void ColorChooserPopupUIController::writeDocument(DocumentWriter& writer) 87 { 88 Vector<Color> suggestions = m_client->suggestions(); 89 Vector<String> suggestionValues; 90 for (unsigned i = 0; i < suggestions.size(); i++) 91 suggestionValues.append(suggestions[i].serialized()); 92 IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_client->elementRectRelativeToRootView()); 93 94 PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><style>\n", writer); 95 writer.addData(pickerCommonCss, sizeof(pickerCommonCss)); 96 writer.addData(colorSuggestionPickerCss, sizeof(colorSuggestionPickerCss)); 97 PagePopupClient::addString("</style></head><body><div id=main>Loading...</div><script>\n" 98 "window.dialogArguments = {\n", writer); 99 PagePopupClient::addProperty("values", suggestionValues, writer); 100 PagePopupClient::addProperty("otherColorLabel", Platform::current()->queryLocalizedString(WebLocalizedString::OtherColorLabel), writer); 101 addProperty("anchorRectInScreen", anchorRectInScreen, writer); 102 PagePopupClient::addString("};\n", writer); 103 writer.addData(pickerCommonJs, sizeof(pickerCommonJs)); 104 writer.addData(colorSuggestionPickerJs, sizeof(colorSuggestionPickerJs)); 105 PagePopupClient::addString("</script></body>\n", writer); 106 } 107 108 Locale& ColorChooserPopupUIController::locale() 109 { 110 return *m_locale; 111 } 112 113 void ColorChooserPopupUIController::setValueAndClosePopup(int numValue, const String& stringValue) 114 { 115 ASSERT(m_popup); 116 ASSERT(m_client); 117 if (numValue == ColorPickerPopupActionSetValue) 118 m_client->didChooseColor(StyleColor(stringValue).color()); 119 if (numValue == ColorPickerPopupActionChooseOtherColor) 120 openColorChooser(); 121 closePopup(); 122 } 123 124 void ColorChooserPopupUIController::setValue(const String& value) 125 { 126 ASSERT(m_client); 127 m_client->didChooseColor(StyleColor(value).color()); 128 } 129 130 void ColorChooserPopupUIController::didClosePopup() 131 { 132 m_popup = 0; 133 134 if (!m_chooser) 135 didEndChooser(); 136 } 137 138 139 void ColorChooserPopupUIController::openPopup() 140 { 141 ASSERT(!m_popup); 142 m_popup = m_chromeClient->openPagePopup(this, m_client->elementRectRelativeToRootView()); 143 } 144 145 void ColorChooserPopupUIController::closePopup() 146 { 147 if (!m_popup) 148 return; 149 m_chromeClient->closePagePopup(m_popup); 150 } 151 152 } 153