1/* 2 * Copyright (C) 2010 Apple 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 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27#include "WebPopupMenu.h" 28 29#include "PlatformPopupMenuData.h" 30#include <WebCore/Font.h> 31#include <WebCore/GraphicsContext.h> 32#include <WebCore/TextRun.h> 33#include <WebCore/PopupMenuClient.h> 34#include <WebCore/PopupMenuStyle.h> 35#include <WebCore/RenderTheme.h> 36 37using namespace WebCore; 38 39namespace WebKit { 40 41static const int separatorPadding = 4; 42static const int separatorHeight = 1; 43static const int popupWindowBorderWidth = 1; 44 45void WebPopupMenu::setUpPlatformData(const WebCore::IntRect& pageCoordinates, PlatformPopupMenuData& data) 46{ 47 int itemCount = m_popupClient->listSize(); 48 49 data.m_clientPaddingLeft = m_popupClient->clientPaddingLeft(); 50 data.m_clientPaddingRight = m_popupClient->clientPaddingRight(); 51 data.m_clientInsetLeft = m_popupClient->clientInsetLeft(); 52 data.m_clientInsetRight = m_popupClient->clientInsetRight(); 53 data.m_itemHeight = m_popupClient->menuStyle().font().fontMetrics().height() + 1; 54 55 int popupWidth = 0; 56 for (size_t i = 0; i < itemCount; ++i) { 57 String text = m_popupClient->itemText(i); 58 if (text.isEmpty()) 59 continue; 60 61 Font itemFont = m_popupClient->menuStyle().font(); 62 if (m_popupClient->itemIsLabel(i)) { 63 FontDescription d = itemFont.fontDescription(); 64 d.setWeight(d.bolderWeight()); 65 itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing()); 66 itemFont.update(m_popupClient->fontSelector()); 67 } 68 69 popupWidth = std::max<float>(popupWidth, ceilf(itemFont.width(TextRun(text.characters(), text.length())))); 70 } 71 72 // FIXME: popupWidth should probably take into account monitor constraints as is done with WebPopupMenuProxyWin::calculatePositionAndSize. 73 74 popupWidth += max(0, data.m_clientPaddingRight - data.m_clientInsetRight) + max(0, data.m_clientPaddingLeft - data.m_clientInsetLeft); 75 popupWidth += 2 * popupWindowBorderWidth; 76 data.m_popupWidth = popupWidth; 77 78 // The backing stores should be drawn at least as wide as the control on the page to match the width of the popup window we'll create. 79 int backingStoreWidth = max(pageCoordinates.width() - m_popupClient->clientInsetLeft() - m_popupClient->clientInsetRight(), popupWidth); 80 81 IntSize backingStoreSize(backingStoreWidth, (itemCount * data.m_itemHeight)); 82 data.m_notSelectedBackingStore = ShareableBitmap::createShareable(backingStoreSize, ShareableBitmap::SupportsAlpha); 83 data.m_selectedBackingStore = ShareableBitmap::createShareable(backingStoreSize, ShareableBitmap::SupportsAlpha); 84 85 OwnPtr<GraphicsContext> notSelectedBackingStoreContext = data.m_notSelectedBackingStore->createGraphicsContext(); 86 OwnPtr<GraphicsContext> selectedBackingStoreContext = data.m_selectedBackingStore->createGraphicsContext(); 87 88 Color activeOptionBackgroundColor = RenderTheme::defaultTheme()->activeListBoxSelectionBackgroundColor(); 89 Color activeOptionTextColor = RenderTheme::defaultTheme()->activeListBoxSelectionForegroundColor(); 90 91 for (int y = 0; y < backingStoreSize.height(); y += data.m_itemHeight) { 92 int index = y / data.m_itemHeight; 93 94 PopupMenuStyle itemStyle = m_popupClient->itemStyle(index); 95 96 Color optionBackgroundColor = itemStyle.backgroundColor(); 97 Color optionTextColor = itemStyle.foregroundColor(); 98 99 IntRect itemRect(0, y, backingStoreWidth, data.m_itemHeight); 100 101 // Draw the background for this menu item 102 if (itemStyle.isVisible()) { 103 notSelectedBackingStoreContext->fillRect(itemRect, optionBackgroundColor, ColorSpaceDeviceRGB); 104 selectedBackingStoreContext->fillRect(itemRect, activeOptionBackgroundColor, ColorSpaceDeviceRGB); 105 } 106 107 if (m_popupClient->itemIsSeparator(index)) { 108 IntRect separatorRect(itemRect.x() + separatorPadding, itemRect.y() + (itemRect.height() - separatorHeight) / 2, itemRect.width() - 2 * separatorPadding, separatorHeight); 109 110 notSelectedBackingStoreContext->fillRect(separatorRect, optionTextColor, ColorSpaceDeviceRGB); 111 selectedBackingStoreContext->fillRect(separatorRect, activeOptionTextColor, ColorSpaceDeviceRGB); 112 continue; 113 } 114 115 String itemText = m_popupClient->itemText(index); 116 117 unsigned length = itemText.length(); 118 const UChar* string = itemText.characters(); 119 TextRun textRun(string, length, false, 0, 0, TextRun::AllowTrailingExpansion, itemText.defaultWritingDirection() == WTF::Unicode::RightToLeft); 120 121 notSelectedBackingStoreContext->setFillColor(optionTextColor, ColorSpaceDeviceRGB); 122 selectedBackingStoreContext->setFillColor(activeOptionTextColor, ColorSpaceDeviceRGB); 123 124 Font itemFont = m_popupClient->menuStyle().font(); 125 if (m_popupClient->itemIsLabel(index)) { 126 FontDescription d = itemFont.fontDescription(); 127 d.setWeight(d.bolderWeight()); 128 itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing()); 129 itemFont.update(m_popupClient->fontSelector()); 130 } 131 132 // Draw the item text 133 if (itemStyle.isVisible()) { 134 int textX = std::max(0, data.m_clientPaddingLeft - data.m_clientInsetLeft); 135 if (RenderTheme::defaultTheme()->popupOptionSupportsTextIndent() && itemStyle.textDirection() == LTR) 136 textX += itemStyle.textIndent().calcMinValue(itemRect.width()); 137 int textY = itemRect.y() + itemFont.fontMetrics().ascent() + (itemRect.height() - itemFont.fontMetrics().height()) / 2; 138 139 notSelectedBackingStoreContext->drawBidiText(itemFont, textRun, IntPoint(textX, textY)); 140 selectedBackingStoreContext->drawBidiText(itemFont, textRun, IntPoint(textX, textY)); 141 } 142 } 143} 144 145} // namespace WebKit 146