Example #1
0
void RenderListBox::updateFromElement()
{
    if (m_optionsChanged) {
        const Vector<HTMLElement*>& listItems = static_cast<HTMLSelectElement*>(node())->listItems();
        int size = numItems();
        
        float width = 0;
        TextStyle textStyle(0, 0, 0, false, false, false, false);
        for (int i = 0; i < size; ++i) {
            HTMLElement* element = listItems[i];
            String text;
            Font itemFont = style()->font();
            if (element->hasTagName(optionTag))
                text = static_cast<HTMLOptionElement*>(element)->optionText();
            else if (element->hasTagName(optgroupTag)) {
                text = static_cast<HTMLOptGroupElement*>(element)->groupLabelText();
                FontDescription d = itemFont.fontDescription();
                d.setBold(true);
                itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing());
                itemFont.update();
            }
                
            if (!text.isEmpty()) {
                float textWidth = itemFont.floatWidth(TextRun(text.impl()), textStyle);
                width = max(width, textWidth);
            }
        }
        m_optionsWidth = static_cast<int>(ceilf(width));
        m_optionsChanged = false;
        setNeedsLayoutAndMinMaxRecalc();
    }
}
Example #2
0
void RenderListBox::paintItemForeground(PaintInfo& paintInfo, int tx, int ty, int listIndex)
{
    HTMLSelectElement* select = static_cast<HTMLSelectElement*>(node());
    const Vector<HTMLElement*>& listItems = select->listItems();
    HTMLElement* element = listItems[listIndex];

    String itemText;
    if (element->hasTagName(optionTag))
        itemText = static_cast<HTMLOptionElement*>(element)->optionText();
    else if (element->hasTagName(optgroupTag))
        itemText = static_cast<HTMLOptGroupElement*>(element)->groupLabelText();
       
    // Determine where the item text should be placed
    IntRect r = itemBoundingBoxRect(tx, ty, listIndex);
    r.move(optionsSpacingHorizontal, style()->font().ascent());

    RenderStyle* itemStyle = element->renderStyle();
    if (!itemStyle)
        itemStyle = style();
    
    Color textColor = element->renderStyle() ? element->renderStyle()->color() : style()->color();
    if (element->hasTagName(optionTag) && static_cast<HTMLOptionElement*>(element)->selected()) {
        if (document()->frame()->isActive() && document()->focusedNode() == node())
            textColor = theme()->activeListBoxSelectionForegroundColor();
        // Honor the foreground color for disabled items
        else if (!element->disabled())
            textColor = theme()->inactiveListBoxSelectionForegroundColor();
    }

    paintInfo.context->setFillColor(textColor);

    Font itemFont = style()->font();
    if (element->hasTagName(optgroupTag)) {
        FontDescription d = itemFont.fontDescription();
        d.setBold(true);
        itemFont = Font(d, itemFont.letterSpacing(), itemFont.wordSpacing());
        itemFont.update();
    }
    paintInfo.context->setFont(itemFont);
    
    unsigned length = itemText.length();
    const UChar* string = itemText.characters();
    TextStyle textStyle(0, 0, 0, false, true);
    RenderBlock::CharacterBuffer characterBuffer;

    if (itemStyle->direction() == RTL && itemStyle->unicodeBidi() == Override)
        textStyle.setRTL(true);
    else if ((itemStyle->direction() == RTL || itemStyle->unicodeBidi() != Override) && !itemStyle->visuallyOrdered()) {
        // If necessary, reorder characters by running the string through the bidi algorithm
        characterBuffer.append(string, length);
        RenderBlock::bidiReorderCharacters(document(), itemStyle, characterBuffer);
        string = characterBuffer.data();
    }
    TextRun textRun(string, length);

    // Draw the item text
    if (itemStyle->visibility() != HIDDEN)
        paintInfo.context->drawText(textRun, r.location(), textStyle);
}