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 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)
59 , m_popupIsVisible(false)
63 RenderMenuList::~RenderMenuList()
66 m_popup->disconnectClient();
70 void RenderMenuList::createInnerBlock()
73 ASSERT(firstChild() == m_innerBlock);
74 ASSERT(!m_innerBlock->nextSibling());
78 // Create an anonymous block.
79 ASSERT(!firstChild());
80 m_innerBlock = createAnonymousBlock();
82 RenderFlexibleBox::addChild(m_innerBlock);
85 void RenderMenuList::adjustInnerStyle()
87 m_innerBlock->style()->setBoxFlex(1.0f);
89 m_innerBlock->style()->setPaddingLeft(Length(theme()->popupInternalPaddingLeft(style()), Fixed));
90 m_innerBlock->style()->setPaddingRight(Length(theme()->popupInternalPaddingRight(style()), Fixed));
91 m_innerBlock->style()->setPaddingTop(Length(theme()->popupInternalPaddingTop(style()), Fixed));
92 m_innerBlock->style()->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 m_innerBlock->style()->setTextAlign(LEFT);
98 TextDirection direction = (m_buttonText && m_buttonText->text()->defaultWritingDirection() == WTF::Unicode::RightToLeft) ? RTL : LTR;
99 m_innerBlock->style()->setDirection(direction);
103 void RenderMenuList::addChild(RenderObject* newChild, RenderObject* beforeChild)
106 m_innerBlock->addChild(newChild, beforeChild);
109 void RenderMenuList::removeChild(RenderObject* oldChild)
111 if (oldChild == m_innerBlock || !m_innerBlock) {
112 RenderFlexibleBox::removeChild(oldChild);
115 m_innerBlock->removeChild(oldChild);
118 void RenderMenuList::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
120 RenderBlock::styleDidChange(diff, oldStyle);
123 m_buttonText->setStyle(style());
124 if (m_innerBlock) // RenderBlock handled updating the anonymous block's style.
127 bool fontChanged = !oldStyle || oldStyle->font() != style()->font();
129 updateOptionsWidth();
132 void RenderMenuList::updateOptionsWidth()
134 float maxOptionWidth = 0;
135 const Vector<Element*>& listItems = toSelectElement(static_cast<Element*>(node()))->listItems();
136 int size = listItems.size();
137 for (int i = 0; i < size; ++i) {
138 Element* element = listItems[i];
139 OptionElement* optionElement = toOptionElement(element);
143 String text = optionElement->textIndentedToRespectGroupLabel();
144 if (theme()->popupOptionSupportsTextIndent()) {
145 // Add in the option's text indent. We can't calculate percentage values for now.
146 float optionWidth = 0;
147 if (RenderStyle* optionStyle = element->renderStyle())
148 optionWidth += optionStyle->textIndent().calcMinValue(0);
150 optionWidth += style()->font().floatWidth(text);
151 maxOptionWidth = max(maxOptionWidth, optionWidth);
152 } else if (!text.isEmpty())
153 maxOptionWidth = max(maxOptionWidth, style()->font().floatWidth(text));
156 int width = static_cast<int>(ceilf(maxOptionWidth));
157 if (m_optionsWidth == width)
160 m_optionsWidth = width;
162 setNeedsLayoutAndPrefWidthsRecalc();
165 void RenderMenuList::updateFromElement()
167 if (m_optionsChanged) {
168 updateOptionsWidth();
169 m_optionsChanged = false;
172 if (m_popupIsVisible)
173 m_popup->updateFromElement();
175 setTextFromOption(toSelectElement(static_cast<Element*>(node()))->selectedIndex());
178 void RenderMenuList::setTextFromOption(int optionIndex)
180 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
181 const Vector<Element*>& listItems = select->listItems();
182 int size = listItems.size();
184 int i = select->optionToListIndex(optionIndex);
186 if (i >= 0 && i < size) {
187 if (OptionElement* optionElement = toOptionElement(listItems[i]))
188 text = optionElement->textIndentedToRespectGroupLabel();
191 setText(text.stripWhiteSpace());
194 void RenderMenuList::setText(const String& s)
197 if (!m_buttonText || !m_buttonText->isBR()) {
199 m_buttonText->destroy();
200 m_buttonText = new (renderArena()) RenderBR(document());
201 m_buttonText->setStyle(style());
202 addChild(m_buttonText);
205 if (m_buttonText && !m_buttonText->isBR())
206 m_buttonText->setText(s.impl());
209 m_buttonText->destroy();
210 m_buttonText = new (renderArena()) RenderText(document(), s.impl());
211 m_buttonText->setStyle(style());
212 addChild(m_buttonText);
218 String RenderMenuList::text() const
220 return m_buttonText ? m_buttonText->text() : 0;
223 IntRect RenderMenuList::controlClipRect(int tx, int ty) const
225 // Clip to the intersection of the content box and the content box for the inner box
226 // This will leave room for the arrows which sit in the inner box padding,
227 // and if the inner box ever spills out of the outer box, that will get clipped too.
228 IntRect outerBox(tx + borderLeft() + paddingLeft(),
229 ty + borderTop() + paddingTop(),
233 IntRect innerBox(tx + m_innerBlock->x() + m_innerBlock->paddingLeft(),
234 ty + m_innerBlock->y() + m_innerBlock->paddingTop(),
235 m_innerBlock->contentWidth(),
236 m_innerBlock->contentHeight());
238 return intersection(outerBox, innerBox);
241 void RenderMenuList::computePreferredLogicalWidths()
243 m_minPreferredLogicalWidth = 0;
244 m_maxPreferredLogicalWidth = 0;
246 if (style()->width().isFixed() && style()->width().value() > 0)
247 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = computeContentBoxLogicalWidth(style()->width().value());
249 m_maxPreferredLogicalWidth = max(m_optionsWidth, theme()->minimumMenuListSize(style())) + m_innerBlock->paddingLeft() + m_innerBlock->paddingRight();
251 if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
252 m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
253 m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->minWidth().value()));
254 } else if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent()))
255 m_minPreferredLogicalWidth = 0;
257 m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth;
259 if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
260 m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
261 m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, computeContentBoxLogicalWidth(style()->maxWidth().value()));
264 int toAdd = borderAndPaddingWidth();
265 m_minPreferredLogicalWidth += toAdd;
266 m_maxPreferredLogicalWidth += toAdd;
268 setPreferredLogicalWidthsDirty(false);
271 void RenderMenuList::showPopup()
273 if (m_popupIsVisible)
276 // Create m_innerBlock here so it ends up as the first child.
277 // This is important because otherwise we might try to create m_innerBlock
278 // inside the showPopup call and it would fail.
281 m_popup = document()->page()->chrome()->createPopupMenu(this);
282 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
283 m_popupIsVisible = true;
285 // Compute the top left taking transforms into account, but use
286 // the actual width of the element to size the popup.
287 FloatPoint absTopLeft = localToAbsolute(FloatPoint(), false, true);
288 IntRect absBounds = absoluteBoundingBoxRect();
289 absBounds.setLocation(roundedIntPoint(absTopLeft));
290 m_popup->show(absBounds, document()->view(),
291 select->optionToListIndex(select->selectedIndex()));
294 void RenderMenuList::hidePopup()
300 void RenderMenuList::valueChanged(unsigned listIndex, bool fireOnChange)
302 // Check to ensure a page navigation has not occurred while
304 Document* doc = static_cast<Element*>(node())->document();
305 if (!doc || doc != doc->frame()->document())
308 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
309 select->setSelectedIndexByUser(select->listToOptionIndex(listIndex), true, fireOnChange);
312 #if ENABLE(NO_LISTBOX_RENDERING)
313 void RenderMenuList::listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow)
315 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
316 select->listBoxSelectItem(listIndex, allowMultiplySelections, shift, fireOnChangeNow);
319 bool RenderMenuList::multiple()
321 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
322 return select->multiple();
326 void RenderMenuList::didSetSelectedIndex()
328 int index = selectedIndex();
329 if (m_lastSelectedIndex == index)
332 m_lastSelectedIndex = index;
334 if (AXObjectCache::accessibilityEnabled())
335 document()->axObjectCache()->postNotification(this, AXObjectCache::AXMenuListValueChanged, true, PostSynchronously);
338 String RenderMenuList::itemText(unsigned listIndex) const
340 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
341 const Vector<Element*>& listItems = select->listItems();
342 if (listIndex >= listItems.size())
344 Element* element = listItems[listIndex];
345 if (OptionGroupElement* optionGroupElement = toOptionGroupElement(element))
346 return optionGroupElement->groupLabelText();
347 else if (OptionElement* optionElement = toOptionElement(element))
348 return optionElement->textIndentedToRespectGroupLabel();
352 String RenderMenuList::itemLabel(unsigned) const
357 String RenderMenuList::itemIcon(unsigned) const
362 String RenderMenuList::itemAccessibilityText(unsigned listIndex) const
364 // Allow the accessible name be changed if necessary.
365 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
366 const Vector<Element*>& listItems = select->listItems();
367 if (listIndex >= listItems.size())
370 return listItems[listIndex]->getAttribute(aria_labelAttr);
373 String RenderMenuList::itemToolTip(unsigned listIndex) const
375 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
376 const Vector<Element*>& listItems = select->listItems();
377 if (listIndex >= listItems.size())
379 Element* element = listItems[listIndex];
380 return element->title();
383 bool RenderMenuList::itemIsEnabled(unsigned listIndex) const
385 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
386 const Vector<Element*>& listItems = select->listItems();
387 if (listIndex >= listItems.size())
389 Element* element = listItems[listIndex];
390 if (!isOptionElement(element))
393 bool groupEnabled = true;
394 if (Element* parentElement = element->parentElement()) {
395 if (isOptionGroupElement(parentElement))
396 groupEnabled = parentElement->isEnabledFormControl();
401 return element->isEnabledFormControl();
404 PopupMenuStyle RenderMenuList::itemStyle(unsigned listIndex) const
406 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
407 const Vector<Element*>& listItems = select->listItems();
408 if (listIndex >= listItems.size()) {
409 // If we are making an out of bounds access, then we want to use the style
410 // of a different option element (index 0). However, if there isn't an option element
411 // before at index 0, we fall back to the menu's style.
415 // Try to retrieve the style of an option element we know exists (index 0).
418 Element* element = listItems[listIndex];
420 RenderStyle* style = element->renderStyle() ? element->renderStyle() : element->computedStyle();
421 return style ? PopupMenuStyle(style->visitedDependentColor(CSSPropertyColor), itemBackgroundColor(listIndex), style->font(), style->visibility() == VISIBLE, style->display() == NONE, style->textIndent(), style->direction()) : menuStyle();
424 Color RenderMenuList::itemBackgroundColor(unsigned listIndex) const
426 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
427 const Vector<Element*>& listItems = select->listItems();
428 if (listIndex >= listItems.size())
429 return style()->visitedDependentColor(CSSPropertyBackgroundColor);
430 Element* element = listItems[listIndex];
432 Color backgroundColor;
433 if (element->renderStyle())
434 backgroundColor = element->renderStyle()->visitedDependentColor(CSSPropertyBackgroundColor);
435 // If the item has an opaque background color, return that.
436 if (!backgroundColor.hasAlpha())
437 return backgroundColor;
439 // Otherwise, the item's background is overlayed on top of the menu background.
440 backgroundColor = style()->visitedDependentColor(CSSPropertyBackgroundColor).blend(backgroundColor);
441 if (!backgroundColor.hasAlpha())
442 return backgroundColor;
444 // If the menu background is not opaque, then add an opaque white background behind.
445 return Color(Color::white).blend(backgroundColor);
448 PopupMenuStyle RenderMenuList::menuStyle() const
450 RenderStyle* s = m_innerBlock ? m_innerBlock->style() : style();
451 return PopupMenuStyle(s->visitedDependentColor(CSSPropertyColor), s->visitedDependentColor(CSSPropertyBackgroundColor), s->font(), s->visibility() == VISIBLE, s->display() == NONE, s->textIndent(), s->direction());
454 HostWindow* RenderMenuList::hostWindow() const
456 return document()->view()->hostWindow();
459 PassRefPtr<Scrollbar> RenderMenuList::createScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
461 RefPtr<Scrollbar> widget;
462 bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR);
463 if (hasCustomScrollbarStyle)
464 widget = RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, this);
466 widget = Scrollbar::createNativeScrollbar(scrollableArea, orientation, controlSize);
467 return widget.release();
470 int RenderMenuList::clientInsetLeft() const
475 int RenderMenuList::clientInsetRight() const
480 int RenderMenuList::clientPaddingLeft() const
482 return paddingLeft();
485 const int endOfLinePadding = 2;
486 int RenderMenuList::clientPaddingRight() const
488 if (style()->appearance() == MenulistPart || style()->appearance() == MenulistButtonPart) {
489 // For these appearance values, the theme applies padding to leave room for the
490 // drop-down button. But leaving room for the button inside the popup menu itself
491 // looks strange, so we return a small default padding to avoid having a large empty
492 // space appear on the side of the popup menu.
493 return endOfLinePadding;
496 // If the appearance isn't MenulistPart, then the select is styled (non-native), so
497 // we want to return the user specified padding.
498 return paddingRight();
501 int RenderMenuList::listSize() const
503 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
504 return select->listItems().size();
507 int RenderMenuList::selectedIndex() const
509 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
510 return select->optionToListIndex(select->selectedIndex());
513 void RenderMenuList::popupDidHide()
515 m_popupIsVisible = false;
518 bool RenderMenuList::itemIsSeparator(unsigned listIndex) const
520 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
521 const Vector<Element*>& listItems = select->listItems();
522 if (listIndex >= listItems.size())
524 Element* element = listItems[listIndex];
525 return element->hasTagName(hrTag);
528 bool RenderMenuList::itemIsLabel(unsigned listIndex) const
530 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
531 const Vector<Element*>& listItems = select->listItems();
532 if (listIndex >= listItems.size())
534 Element* element = listItems[listIndex];
535 return isOptionGroupElement(element);
538 bool RenderMenuList::itemIsSelected(unsigned listIndex) const
540 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
541 const Vector<Element*>& listItems = select->listItems();
542 if (listIndex >= listItems.size())
544 Element* element = listItems[listIndex];
545 if (OptionElement* optionElement = toOptionElement(element))
546 return optionElement->selected();
550 void RenderMenuList::setTextFromItem(unsigned listIndex)
552 SelectElement* select = toSelectElement(static_cast<Element*>(node()));
553 setTextFromOption(select->listToOptionIndex(listIndex));
556 FontSelector* RenderMenuList::fontSelector() const
558 return document()->styleSelector()->fontSelector();