2 * This file is part of the select element renderer in WebCore.
4 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 * 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
26 #include "RenderMenuList.h"
28 #include "AXObjectCache.h"
30 #include "CSSStyleSelector.h"
32 #include "FrameView.h"
33 #include "HTMLNames.h"
34 #include "NodeRenderStyle.h"
35 #include "OptionElement.h"
36 #include "OptionGroupElement.h"
37 #include "PopupMenu.h"
39 #include "RenderScrollbar.h"
40 #include "RenderTheme.h"
41 #include "SelectElement.h"
49 using namespace HTMLNames;
51 RenderMenuList::RenderMenuList(Element* element)
52 : RenderFlexibleBox(element)
55 , m_optionsChanged(true)
57 , m_lastSelectedIndex(-1)
58 , m_popupIsVisible(false)
62 RenderMenuList::~RenderMenuList()
65 m_popup->disconnectClient();
69 void RenderMenuList::createInnerBlock()
72 ASSERT(firstChild() == m_innerBlock);
73 ASSERT(!m_innerBlock->nextSibling());
77 // Create an anonymous block.
78 ASSERT(!firstChild());
79 m_innerBlock = createAnonymousBlock();
81 RenderFlexibleBox::addChild(m_innerBlock);
84 void RenderMenuList::adjustInnerStyle()
86 RenderStyle* innerStyle = m_innerBlock->style();
87 innerStyle->setBoxFlex(1);
89 innerStyle->setPaddingLeft(Length(theme()->popupInternalPaddingLeft(style()), Fixed));
90 innerStyle->setPaddingRight(Length(theme()->popupInternalPaddingRight(style()), Fixed));
91 innerStyle->setPaddingTop(Length(theme()->popupInternalPaddingTop(style()), Fixed));
92 innerStyle->setPaddingBottom(Length(theme()->popupInternalPaddingBottom(style()), Fixed));
94 if (document()->page()->chrome()->selectItemWritingDirectionIsNatural()) {
95 // Items in the popup will not respect the CSS text-align and direction properties,
96 // so we must adjust our own style to match.
97 innerStyle->setTextAlign(LEFT);
98 TextDirection direction = (m_buttonText && m_buttonText->text()->defaultWritingDirection() == WTF::Unicode::RightToLeft) ? RTL : LTR;
99 innerStyle->setDirection(direction);
100 } else if (m_optionStyle && document()->page()->chrome()->selectItemAlignmentFollowsMenuWritingDirection()) {
101 if ((m_optionStyle->direction() != innerStyle->direction() || m_optionStyle->unicodeBidi() != innerStyle->unicodeBidi()))
102 m_innerBlock->setNeedsLayoutAndPrefWidthsRecalc();
103 innerStyle->setTextAlign(style()->isLeftToRightDirection() ? LEFT : RIGHT);
104 innerStyle->setDirection(m_optionStyle->direction());
105 innerStyle->setUnicodeBidi(m_optionStyle->unicodeBidi());
109 void RenderMenuList::addChild(RenderObject* newChild, RenderObject* beforeChild)
112 m_innerBlock->addChild(newChild, beforeChild);
115 void RenderMenuList::removeChild(RenderObject* oldChild)
117 if (oldChild == m_innerBlock || !m_innerBlock) {
118 RenderFlexibleBox::removeChild(oldChild);
121 m_innerBlock->removeChild(oldChild);
124 void RenderMenuList::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
126 RenderBlock::styleDidChange(diff, oldStyle);
129 m_buttonText->setStyle(style());
130 if (m_innerBlock) // RenderBlock handled updating the anonymous block's style.
133 bool fontChanged = !oldStyle || oldStyle->font() != style()->font();
135 updateOptionsWidth();
138 void RenderMenuList::updateOptionsWidth()
140 float maxOptionWidth = 0;
141 const Vector<Element*>& listItems = toSelectElement(static_cast<Element*>(node()))->listItems();
142 int size = listItems.size();
143 for (int i = 0; i < size; ++i) {
144 Element* element = listItems[i];
145 OptionElement* optionElement = toOptionElement(element);
149 String text = optionElement->textIndentedToRespectGroupLabel();
150 if (theme()->popupOptionSupportsTextIndent()) {
151 // Add in the option's text indent. We can't calculate percentage values for now.
152 float optionWidth = 0;
153 if (RenderStyle* optionStyle = element->renderStyle())
154 optionWidth += optionStyle->textIndent().calcMinValue(0);
156 optionWidth += style()->font().floatWidth(text);
157 maxOptionWidth = max(maxOptionWidth, optionWidth);
158 } else if (!text.isEmpty())
159 maxOptionWidth = max(maxOptionWidth, style()->font().floatWidth(text));
162 int width = static_cast<int>(ceilf(maxOptionWidth));
163 if (m_optionsWidth == width)
166 m_optionsWidth = width;
168 setNeedsLayoutAndPrefWidthsRecalc();
171 void RenderMenuList::updateFromElement()
173 if (m_optionsChanged) {
174 updateOptionsWidth();
175 m_optionsChanged = false;
178 if (m_popupIsVisible)
179 m_popup->updateFromElement();
181 setTextFromOption(toSelectElement(static_cast<Element*>(node()))->selectedIndex());
184 void RenderMenuList::setTextFromOption(int optionIndex)
186 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
187 const Vector<Element*>& listItems = select->listItems();
188 int size = listItems.size();
190 int i = select->optionToListIndex(optionIndex);
192 if (i >= 0 && i < size) {
193 Element* element = listItems[i];
194 if (OptionElement* optionElement = toOptionElement(element)) {
195 text = optionElement->textIndentedToRespectGroupLabel();
196 m_optionStyle = element->renderStyle();
200 setText(text.stripWhiteSpace());
203 void RenderMenuList::setText(const String& s)
206 if (!m_buttonText || !m_buttonText->isBR()) {
208 m_buttonText->destroy();
209 m_buttonText = new (renderArena()) RenderBR(document());
210 m_buttonText->setStyle(style());
211 addChild(m_buttonText);
214 if (m_buttonText && !m_buttonText->isBR())
215 m_buttonText->setText(s.impl());
218 m_buttonText->destroy();
219 m_buttonText = new (renderArena()) RenderText(document(), s.impl());
220 m_buttonText->setStyle(style());
221 addChild(m_buttonText);
227 String RenderMenuList::text() const
229 return m_buttonText ? m_buttonText->text() : 0;
232 IntRect RenderMenuList::controlClipRect(int tx, int ty) const
234 // Clip to the intersection of the content box and the content box for the inner box
235 // This will leave room for the arrows which sit in the inner box padding,
236 // and if the inner box ever spills out of the outer box, that will get clipped too.
237 IntRect outerBox(tx + borderLeft() + paddingLeft(),
238 ty + borderTop() + paddingTop(),
242 IntRect innerBox(tx + m_innerBlock->x() + m_innerBlock->paddingLeft(),
243 ty + m_innerBlock->y() + m_innerBlock->paddingTop(),
244 m_innerBlock->contentWidth(),
245 m_innerBlock->contentHeight());
247 return intersection(outerBox, innerBox);
250 void RenderMenuList::computePreferredLogicalWidths()
252 m_minPreferredLogicalWidth = 0;
253 m_maxPreferredLogicalWidth = 0;
255 if (style()->width().isFixed() && style()->width().value() > 0)
256 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(style()->width().value());
258 m_maxPreferredLogicalWidth = max(m_optionsWidth, theme()->minimumMenuListSize(style())) + m_innerBlock->paddingLeft() + m_innerBlock->paddingRight();
260 if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
261 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
262 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
263 } else if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent()))
264 m_minPreferredLogicalWidth = 0;
266 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth;
268 if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
269 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
270 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
273 int toAdd = borderAndPaddingWidth();
274 m_minPreferredLogicalWidth += toAdd;
275 m_maxPreferredLogicalWidth += toAdd;
277 setPreferredLogicalWidthsDirty(false);
280 void RenderMenuList::showPopup()
282 if (m_popupIsVisible)
285 // Create m_innerBlock here so it ends up as the first child.
286 // This is important because otherwise we might try to create m_innerBlock
287 // inside the showPopup call and it would fail.
290 m_popup = document()->page()->chrome()->createPopupMenu(this);
291 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
292 m_popupIsVisible = true;
294 // Compute the top left taking transforms into account, but use
295 // the actual width of the element to size the popup.
296 FloatPoint absTopLeft = localToAbsolute(FloatPoint(), false, true);
297 IntRect absBounds = absoluteBoundingBoxRect();
298 absBounds.setLocation(roundedIntPoint(absTopLeft));
299 m_popup->show(absBounds, document()->view(),
300 select->optionToListIndex(select->selectedIndex()));
303 void RenderMenuList::hidePopup()
309 void RenderMenuList::valueChanged(unsigned listIndex, bool fireOnChange)
311 // Check to ensure a page navigation has not occurred while
313 Document* doc = static_cast<Element*>(node())->document();
314 if (!doc || doc != doc->frame()->document())
317 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
318 select->setSelectedIndexByUser(select->listToOptionIndex(listIndex), true, fireOnChange);
321 #if ENABLE(NO_LISTBOX_RENDERING)
322 void RenderMenuList::listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow)
324 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
325 select->listBoxSelectItem(listIndex, allowMultiplySelections, shift, fireOnChangeNow);
328 bool RenderMenuList::multiple()
330 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
331 return select->multiple();
335 void RenderMenuList::didSetSelectedIndex()
337 int index = selectedIndex();
338 if (m_lastSelectedIndex == index)
341 m_lastSelectedIndex = index;
343 if (AXObjectCache::accessibilityEnabled())
344 document()->axObjectCache()->postNotification(this, AXObjectCache::AXMenuListValueChanged, true, PostSynchronously);
347 String RenderMenuList::itemText(unsigned listIndex) const
349 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
350 const Vector<Element*>& listItems = select->listItems();
351 if (listIndex >= listItems.size())
353 Element* element = listItems[listIndex];
354 if (OptionGroupElement* optionGroupElement = toOptionGroupElement(element))
355 return optionGroupElement->groupLabelText();
356 else if (OptionElement* optionElement = toOptionElement(element))
357 return optionElement->textIndentedToRespectGroupLabel();
361 String RenderMenuList::itemLabel(unsigned) const
366 String RenderMenuList::itemIcon(unsigned) const
371 String RenderMenuList::itemAccessibilityText(unsigned listIndex) const
373 // Allow the accessible name be changed if necessary.
374 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
375 const Vector<Element*>& listItems = select->listItems();
376 if (listIndex >= listItems.size())
379 return listItems[listIndex]->getAttribute(aria_labelAttr);
382 String RenderMenuList::itemToolTip(unsigned listIndex) const
384 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
385 const Vector<Element*>& listItems = select->listItems();
386 if (listIndex >= listItems.size())
388 Element* element = listItems[listIndex];
389 return element->title();
392 bool RenderMenuList::itemIsEnabled(unsigned listIndex) const
394 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
395 const Vector<Element*>& listItems = select->listItems();
396 if (listIndex >= listItems.size())
398 Element* element = listItems[listIndex];
399 if (!isOptionElement(element))
402 bool groupEnabled = true;
403 if (Element* parentElement = element->parentElement()) {
404 if (isOptionGroupElement(parentElement))
405 groupEnabled = parentElement->isEnabledFormControl();
410 return element->isEnabledFormControl();
413 PopupMenuStyle RenderMenuList::itemStyle(unsigned listIndex) const
415 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
416 const Vector<Element*>& listItems = select->listItems();
417 if (listIndex >= listItems.size()) {
418 // If we are making an out of bounds access, then we want to use the style
419 // of a different option element (index 0). However, if there isn't an option element
420 // before at index 0, we fall back to the menu's style.
424 // Try to retrieve the style of an option element we know exists (index 0).
427 Element* element = listItems[listIndex];
429 RenderStyle* style = element->renderStyle() ? element->renderStyle() : element->computedStyle();
430 return style ? PopupMenuStyle(style->visitedDependentColor(CSSPropertyColor), itemBackgroundColor(listIndex), style->font(), style->visibility() == VISIBLE, style->display() == NONE, style->textIndent(), style->direction(), style->unicodeBidi() == Override) : menuStyle();
433 Color RenderMenuList::itemBackgroundColor(unsigned listIndex) const
435 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
436 const Vector<Element*>& listItems = select->listItems();
437 if (listIndex >= listItems.size())
438 return style()->visitedDependentColor(CSSPropertyBackgroundColor);
439 Element* element = listItems[listIndex];
441 Color backgroundColor;
442 if (element->renderStyle())
443 backgroundColor = element->renderStyle()->visitedDependentColor(CSSPropertyBackgroundColor);
444 // If the item has an opaque background color, return that.
445 if (!backgroundColor.hasAlpha())
446 return backgroundColor;
448 // Otherwise, the item's background is overlayed on top of the menu background.
449 backgroundColor = style()->visitedDependentColor(CSSPropertyBackgroundColor).blend(backgroundColor);
450 if (!backgroundColor.hasAlpha())
451 return backgroundColor;
453 // If the menu background is not opaque, then add an opaque white background behind.
454 return Color(Color::white).blend(backgroundColor);
457 PopupMenuStyle RenderMenuList::menuStyle() const
459 RenderStyle* s = m_innerBlock ? m_innerBlock->style() : style();
460 return PopupMenuStyle(s->visitedDependentColor(CSSPropertyColor), s->visitedDependentColor(CSSPropertyBackgroundColor), s->font(), s->visibility() == VISIBLE, s->display() == NONE, s->textIndent(), style()->direction(), style()->unicodeBidi() == Override);
463 HostWindow* RenderMenuList::hostWindow() const
465 return document()->view()->hostWindow();
468 PassRefPtr<Scrollbar> RenderMenuList::createScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
470 RefPtr<Scrollbar> widget;
471 bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR);
472 if (hasCustomScrollbarStyle)
473 widget = RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, this);
475 widget = Scrollbar::createNativeScrollbar(scrollableArea, orientation, controlSize);
476 return widget.release();
479 int RenderMenuList::clientInsetLeft() const
484 int RenderMenuList::clientInsetRight() const
489 int RenderMenuList::clientPaddingLeft() const
491 return paddingLeft() + m_innerBlock->paddingLeft();
494 const int endOfLinePadding = 2;
495 int RenderMenuList::clientPaddingRight() const
497 if (style()->appearance() == MenulistPart || style()->appearance() == MenulistButtonPart) {
498 // For these appearance values, the theme applies padding to leave room for the
499 // drop-down button. But leaving room for the button inside the popup menu itself
500 // looks strange, so we return a small default padding to avoid having a large empty
501 // space appear on the side of the popup menu.
502 return endOfLinePadding;
505 // If the appearance isn't MenulistPart, then the select is styled (non-native), so
506 // we want to return the user specified padding.
507 return paddingRight() + m_innerBlock->paddingRight();
510 int RenderMenuList::listSize() const
512 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
513 return select->listItems().size();
516 int RenderMenuList::selectedIndex() const
518 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
519 return select->optionToListIndex(select->selectedIndex());
522 void RenderMenuList::popupDidHide()
524 m_popupIsVisible = false;
527 bool RenderMenuList::itemIsSeparator(unsigned listIndex) const
529 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
530 const Vector<Element*>& listItems = select->listItems();
531 if (listIndex >= listItems.size())
533 Element* element = listItems[listIndex];
534 return element->hasTagName(hrTag);
537 bool RenderMenuList::itemIsLabel(unsigned listIndex) const
539 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
540 const Vector<Element*>& listItems = select->listItems();
541 if (listIndex >= listItems.size())
543 Element* element = listItems[listIndex];
544 return isOptionGroupElement(element);
547 bool RenderMenuList::itemIsSelected(unsigned listIndex) const
549 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
550 const Vector<Element*>& listItems = select->listItems();
551 if (listIndex >= listItems.size())
553 Element* element = listItems[listIndex];
554 if (OptionElement* optionElement = toOptionElement(element))
555 return optionElement->selected();
559 void RenderMenuList::setTextFromItem(unsigned listIndex)
561 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
562 setTextFromOption(select->listToOptionIndex(listIndex));
565 FontSelector* RenderMenuList::fontSelector() const
567 return document()->styleSelector()->fontSelector();