OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "web/PopupMenuImpl.h" |
| 7 |
| 8 #include "core/HTMLNames.h" |
| 9 #include "core/css/CSSFontSelector.h" |
| 10 #include "core/dom/StyleEngine.h" |
| 11 #include "core/frame/FrameView.h" |
| 12 #include "core/html/HTMLHRElement.h" |
| 13 #include "core/html/HTMLOptGroupElement.h" |
| 14 #include "core/html/HTMLOptionElement.h" |
| 15 #include "core/page/PagePopup.h" |
| 16 #include "platform/geometry/IntRect.h" |
| 17 #include "platform/text/PlatformLocale.h" |
| 18 #include "public/platform/Platform.h" |
| 19 #include "public/web/WebColorChooser.h" |
| 20 #include "web/ChromeClientImpl.h" |
| 21 #include "web/WebViewImpl.h" |
| 22 |
| 23 namespace blink { |
| 24 |
| 25 class PopupMenuCSSFontSelector : public CSSFontSelector { |
| 26 public: |
| 27 static PassRefPtrWillBeRawPtr<PopupMenuCSSFontSelector> create(Document* doc
ument, CSSFontSelector* ownerFontSelector) |
| 28 { |
| 29 return adoptRefWillBeNoop(new PopupMenuCSSFontSelector(document, ownerFo
ntSelector)); |
| 30 } |
| 31 |
| 32 // We don't override willUseFontData() for now because the old PopupListBox |
| 33 // only worked with fonts loaded when opening the popup. |
| 34 virtual PassRefPtr<FontData> getFontData(const FontDescription&, const Atomi
cString&) override; |
| 35 |
| 36 virtual void trace(Visitor*) override; |
| 37 |
| 38 private: |
| 39 PopupMenuCSSFontSelector(Document* document, CSSFontSelector* ownerFontSelec
tor) |
| 40 : CSSFontSelector(document) |
| 41 , m_ownerFontSelector(ownerFontSelector) |
| 42 { |
| 43 } |
| 44 RefPtrWillBeMember<CSSFontSelector> m_ownerFontSelector; |
| 45 }; |
| 46 |
| 47 PassRefPtr<FontData> PopupMenuCSSFontSelector::getFontData(const FontDescription
& description, const AtomicString& name) |
| 48 { |
| 49 return m_ownerFontSelector->getFontData(description, name); |
| 50 } |
| 51 |
| 52 void PopupMenuCSSFontSelector::trace(Visitor* visitor) |
| 53 { |
| 54 visitor->trace(m_ownerFontSelector); |
| 55 CSSFontSelector::trace(visitor); |
| 56 } |
| 57 |
| 58 PassRefPtrWillBeRawPtr<PopupMenuImpl> PopupMenuImpl::create(ChromeClientImpl* ch
romeClient, PopupMenuClient* client) |
| 59 { |
| 60 return adoptRefWillBeNoop(new PopupMenuImpl(chromeClient, client)); |
| 61 } |
| 62 |
| 63 PopupMenuImpl::PopupMenuImpl(ChromeClientImpl* chromeClient, PopupMenuClient* cl
ient) |
| 64 : m_chromeClient(chromeClient) |
| 65 , m_client(client) |
| 66 , m_popup(nullptr) |
| 67 , m_indexToSetOnClose(-1) |
| 68 { |
| 69 } |
| 70 |
| 71 PopupMenuImpl::~PopupMenuImpl() |
| 72 { |
| 73 closePopup(); |
| 74 } |
| 75 |
| 76 IntSize PopupMenuImpl::contentSize() |
| 77 { |
| 78 return IntSize(); |
| 79 } |
| 80 |
| 81 void PopupMenuImpl::writeDocument(SharedBuffer* data) |
| 82 { |
| 83 IntRect anchorRectInScreen = m_chromeClient->rootViewToScreen(m_client->elem
entRectRelativeToRootView()); |
| 84 |
| 85 PagePopupClient::addString("<!DOCTYPE html><head><meta charset='UTF-8'><styl
e>\n", data); |
| 86 data->append(Platform::current()->loadResource("pickerCommon.css")); |
| 87 data->append(Platform::current()->loadResource("listPicker.css")); |
| 88 PagePopupClient::addString("</style></head><body><div id=main>Loading...</di
v><script>\n" |
| 89 "window.dialogArguments = {\n", data); |
| 90 addProperty("selectedIndex", m_client->selectedIndex(), data); |
| 91 PagePopupClient::addString("children: [\n", data); |
| 92 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(ownerElement())
) { |
| 93 if (isHTMLOptionElement(child)) |
| 94 addOption(toHTMLOptionElement(child), data); |
| 95 if (isHTMLOptGroupElement(child)) |
| 96 addOptGroup(toHTMLOptGroupElement(child), data); |
| 97 if (isHTMLHRElement(child)) |
| 98 addSeparator(toHTMLHRElement(child), data); |
| 99 } |
| 100 PagePopupClient::addString("],\n", data); |
| 101 addProperty("anchorRectInScreen", anchorRectInScreen, data); |
| 102 PagePopupClient::addString("};\n", data); |
| 103 data->append(Platform::current()->loadResource("pickerCommon.js")); |
| 104 data->append(Platform::current()->loadResource("listPicker.js")); |
| 105 PagePopupClient::addString("</script></body>\n", data); |
| 106 } |
| 107 |
| 108 const char* fontWeightToString(FontWeight weight) |
| 109 { |
| 110 switch (weight) { |
| 111 case FontWeight100: |
| 112 return "100"; |
| 113 case FontWeight200: |
| 114 return "200"; |
| 115 case FontWeight300: |
| 116 return "300"; |
| 117 case FontWeight400: |
| 118 return "400"; |
| 119 case FontWeight500: |
| 120 return "500"; |
| 121 case FontWeight600: |
| 122 return "600"; |
| 123 case FontWeight700: |
| 124 return "700"; |
| 125 case FontWeight800: |
| 126 return "800"; |
| 127 case FontWeight900: |
| 128 return "900"; |
| 129 default: |
| 130 ASSERT_NOT_REACHED(); |
| 131 break; |
| 132 } |
| 133 return 0; |
| 134 } |
| 135 |
| 136 void PopupMenuImpl::addElementStyle(HTMLElement& element, SharedBuffer* data) |
| 137 { |
| 138 RenderStyle* style = m_client->renderStyleForItem(element); |
| 139 ASSERT(style); |
| 140 PagePopupClient::addString("style: {\n", data); |
| 141 addProperty("color", style->visitedDependentColor(CSSPropertyColor).serializ
ed(), data); |
| 142 addProperty("backgroundColor", style->visitedDependentColor(CSSPropertyBackg
roundColor).serialized(), data); |
| 143 const FontDescription& fontDescription = style->font().fontDescription(); |
| 144 addProperty("fontSize", fontDescription.computedPixelSize(), data); |
| 145 addProperty("fontWeight", String(fontWeightToString(fontDescription.weight()
)), data); |
| 146 PagePopupClient::addString("fontFamily: [\n", data); |
| 147 for (const FontFamily* f = &fontDescription.family(); f; f = f->next()) { |
| 148 addJavaScriptString(f->family().string(), data); |
| 149 if (f->next()) |
| 150 PagePopupClient::addString(",\n", data); |
| 151 } |
| 152 PagePopupClient::addString("],\n", data); |
| 153 addProperty("visibility", String(style->visibility() == HIDDEN ? "hidden" :
"visible"), data); |
| 154 addProperty("display", String(style->display() == NONE ? "none" : "block"),
data); |
| 155 addProperty("direction", String(style->direction() == RTL ? "rtl" : "ltr"),
data); |
| 156 addProperty("unicodeBidi", String(isOverride(style->unicodeBidi()) ? "bidi-o
verride" : "normal"), data); |
| 157 PagePopupClient::addString("},\n", data); |
| 158 } |
| 159 |
| 160 void PopupMenuImpl::addOption(HTMLOptionElement& element, SharedBuffer* data) |
| 161 { |
| 162 PagePopupClient::addString("{\n", data); |
| 163 PagePopupClient::addString("type: \"option\",\n", data); |
| 164 addProperty("label", element.text(), data); |
| 165 addProperty("title", element.title(), data); |
| 166 addProperty("value", element.listIndex(), data); |
| 167 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr)
, data); |
| 168 addProperty("disabled", element.isDisabledFormControl(), data); |
| 169 addElementStyle(element, data); |
| 170 PagePopupClient::addString("},\n", data); |
| 171 } |
| 172 |
| 173 void PopupMenuImpl::addOptGroup(HTMLOptGroupElement& element, SharedBuffer* data
) |
| 174 { |
| 175 PagePopupClient::addString("{\n", data); |
| 176 PagePopupClient::addString("type: \"optgroup\",\n", data); |
| 177 addProperty("label", element.groupLabelText(), data); |
| 178 addProperty("title", element.title(), data); |
| 179 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr)
, data); |
| 180 addProperty("disabled", element.isDisabledFormControl(), data); |
| 181 addElementStyle(element, data); |
| 182 PagePopupClient::addString("children: [", data); |
| 183 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(element)) { |
| 184 if (isHTMLOptionElement(child)) |
| 185 addOption(toHTMLOptionElement(child), data); |
| 186 if (isHTMLOptGroupElement(child)) |
| 187 addOptGroup(toHTMLOptGroupElement(child), data); |
| 188 if (isHTMLHRElement(child)) |
| 189 addSeparator(toHTMLHRElement(child), data); |
| 190 } |
| 191 PagePopupClient::addString("],\n", data); |
| 192 PagePopupClient::addString("},\n", data); |
| 193 } |
| 194 |
| 195 void PopupMenuImpl::addSeparator(HTMLHRElement& element, SharedBuffer* data) |
| 196 { |
| 197 PagePopupClient::addString("{\n", data); |
| 198 PagePopupClient::addString("type: \"separator\",\n", data); |
| 199 addProperty("title", element.title(), data); |
| 200 addProperty("ariaLabel", element.fastGetAttribute(HTMLNames::aria_labelAttr)
, data); |
| 201 addProperty("disabled", element.isDisabledFormControl(), data); |
| 202 addElementStyle(element, data); |
| 203 PagePopupClient::addString("},\n", data); |
| 204 } |
| 205 |
| 206 void PopupMenuImpl::didWriteDocument(Document& document) |
| 207 { |
| 208 Document& ownerDocument = ownerElement().document(); |
| 209 document.styleEngine()->setFontSelector(PopupMenuCSSFontSelector::create(&do
cument, ownerDocument.styleEngine()->fontSelector())); |
| 210 } |
| 211 |
| 212 void PopupMenuImpl::setValueAndClosePopup(int numValue, const String& stringValu
e) |
| 213 { |
| 214 ASSERT(m_popup); |
| 215 ASSERT(m_client); |
| 216 bool success; |
| 217 int listIndex = stringValue.toInt(&success); |
| 218 ASSERT(success); |
| 219 m_client->selectionChanged(listIndex); |
| 220 m_client->valueChanged(listIndex); |
| 221 m_indexToSetOnClose = -1; |
| 222 closePopup(); |
| 223 } |
| 224 |
| 225 void PopupMenuImpl::setValue(const String& value) |
| 226 { |
| 227 ASSERT(m_client); |
| 228 bool success; |
| 229 int listIndex = value.toInt(&success); |
| 230 ASSERT(success); |
| 231 m_client->setTextFromItem(listIndex); |
| 232 m_indexToSetOnClose = listIndex; |
| 233 } |
| 234 |
| 235 void PopupMenuImpl::didClosePopup() |
| 236 { |
| 237 if (m_indexToSetOnClose >= 0) |
| 238 m_client->valueChanged(m_indexToSetOnClose); |
| 239 m_indexToSetOnClose = -1; |
| 240 m_popup = nullptr; |
| 241 m_client->popupDidHide(); |
| 242 } |
| 243 |
| 244 Element& PopupMenuImpl::ownerElement() |
| 245 { |
| 246 return m_client->ownerElement(); |
| 247 } |
| 248 |
| 249 Locale& PopupMenuImpl::locale() |
| 250 { |
| 251 return Locale::defaultLocale(); |
| 252 } |
| 253 |
| 254 void PopupMenuImpl::closePopup() |
| 255 { |
| 256 if (m_popup) |
| 257 m_chromeClient->closePagePopup(m_popup); |
| 258 } |
| 259 |
| 260 void PopupMenuImpl::dispose() |
| 261 { |
| 262 closePopup(); |
| 263 } |
| 264 |
| 265 void PopupMenuImpl::show(const FloatQuad& /*controlPosition*/, const IntSize& /*
controlSize*/, int /*index*/) |
| 266 { |
| 267 ASSERT(!m_popup); |
| 268 m_popup = m_chromeClient->openPagePopup(this, m_client->elementRectRelativeT
oRootView()); |
| 269 } |
| 270 |
| 271 void PopupMenuImpl::hide() |
| 272 { |
| 273 if (m_popup) |
| 274 m_chromeClient->closePagePopup(m_popup); |
| 275 } |
| 276 |
| 277 void PopupMenuImpl::updateFromElement() |
| 278 { |
| 279 RefPtr<SharedBuffer> data = SharedBuffer::create(); |
| 280 PagePopupClient::addString("window.updateData = {\n", data.get()); |
| 281 PagePopupClient::addString("type: \"update\",\n", data.get()); |
| 282 PagePopupClient::addString("children: [", data.get()); |
| 283 for (HTMLElement& child : Traversal<HTMLElement>::childrenOf(ownerElement())
) { |
| 284 if (isHTMLOptionElement(child)) |
| 285 addOption(toHTMLOptionElement(child), data.get()); |
| 286 if (isHTMLOptGroupElement(child)) |
| 287 addOptGroup(toHTMLOptGroupElement(child), data.get()); |
| 288 if (isHTMLHRElement(child)) |
| 289 addSeparator(toHTMLHRElement(child), data.get()); |
| 290 } |
| 291 PagePopupClient::addString("],\n", data.get()); |
| 292 PagePopupClient::addString("}\n", data.get()); |
| 293 m_popup->postMessage(String(data->data(), data->size())); |
| 294 } |
| 295 |
| 296 |
| 297 void PopupMenuImpl::disconnectClient() |
| 298 { |
| 299 m_client = nullptr; |
| 300 #if ENABLE(OILPAN) |
| 301 // Cannot be done during finalization, so instead done when the |
| 302 // render object is destroyed and disconnected. |
| 303 // |
| 304 // FIXME: do this always, regardless of ENABLE(OILPAN). |
| 305 dispose(); |
| 306 #endif |
| 307 } |
| 308 |
| 309 } // namespace blink |
OLD | NEW |