RenderMenuList.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * This file is part of the select element renderer in WebCore.
  3. *
  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/)
  7. *
  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.
  12. *
  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.
  17. *
  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.
  22. *
  23. */
  24. #include "config.h"
  25. #include "RenderMenuList.h"
  26. #include "AXObjectCache.h"
  27. #include "AccessibilityMenuList.h"
  28. #include "CSSFontSelector.h"
  29. #include "Chrome.h"
  30. #include "FontCache.h"
  31. #include "Frame.h"
  32. #include "FrameView.h"
  33. #include "HTMLNames.h"
  34. #include "HTMLOptionElement.h"
  35. #include "HTMLOptGroupElement.h"
  36. #include "HTMLSelectElement.h"
  37. #include "NodeRenderStyle.h"
  38. #include "Page.h"
  39. #include "PopupMenu.h"
  40. #include "RenderBR.h"
  41. #include "RenderScrollbar.h"
  42. #include "RenderTheme.h"
  43. #include "Settings.h"
  44. #include "StyleResolver.h"
  45. #include "TextRun.h"
  46. #include <math.h>
  47. using namespace std;
  48. namespace WebCore {
  49. using namespace HTMLNames;
  50. RenderMenuList::RenderMenuList(Element* element)
  51. : RenderFlexibleBox(element)
  52. , m_buttonText(0)
  53. , m_innerBlock(0)
  54. , m_optionsChanged(true)
  55. , m_optionsWidth(0)
  56. , m_lastActiveIndex(-1)
  57. , m_popupIsVisible(false)
  58. {
  59. ASSERT(element);
  60. ASSERT(element->isHTMLElement());
  61. ASSERT(element->hasTagName(HTMLNames::selectTag));
  62. }
  63. RenderMenuList::~RenderMenuList()
  64. {
  65. if (m_popup)
  66. m_popup->disconnectClient();
  67. m_popup = 0;
  68. }
  69. bool RenderMenuList::canBeReplacedWithInlineRunIn() const
  70. {
  71. return false;
  72. }
  73. void RenderMenuList::createInnerBlock()
  74. {
  75. if (m_innerBlock) {
  76. ASSERT(firstChild() == m_innerBlock);
  77. ASSERT(!m_innerBlock->nextSibling());
  78. return;
  79. }
  80. // Create an anonymous block.
  81. ASSERT(!firstChild());
  82. m_innerBlock = createAnonymousBlock();
  83. adjustInnerStyle();
  84. RenderFlexibleBox::addChild(m_innerBlock);
  85. }
  86. void RenderMenuList::adjustInnerStyle()
  87. {
  88. RenderStyle* innerStyle = m_innerBlock->style();
  89. innerStyle->setFlexGrow(1);
  90. innerStyle->setFlexShrink(1);
  91. // min-width: 0; is needed for correct shrinking.
  92. // FIXME: Remove this line when https://bugs.webkit.org/show_bug.cgi?id=111790 is fixed.
  93. innerStyle->setMinWidth(Length(0, Fixed));
  94. // Use margin:auto instead of align-items:center to get safe centering, i.e.
  95. // when the content overflows, treat it the same as align-items: flex-start.
  96. // But we only do that for the cases where html.css would otherwise use center.
  97. if (style()->alignItems() == AlignCenter) {
  98. innerStyle->setMarginTop(Length());
  99. innerStyle->setMarginBottom(Length());
  100. innerStyle->setAlignSelf(AlignFlexStart);
  101. }
  102. innerStyle->setPaddingLeft(Length(theme()->popupInternalPaddingLeft(style()), Fixed));
  103. innerStyle->setPaddingRight(Length(theme()->popupInternalPaddingRight(style()), Fixed));
  104. innerStyle->setPaddingTop(Length(theme()->popupInternalPaddingTop(style()), Fixed));
  105. innerStyle->setPaddingBottom(Length(theme()->popupInternalPaddingBottom(style()), Fixed));
  106. if (document()->page()->chrome().selectItemWritingDirectionIsNatural()) {
  107. // Items in the popup will not respect the CSS text-align and direction properties,
  108. // so we must adjust our own style to match.
  109. innerStyle->setTextAlign(LEFT);
  110. TextDirection direction = (m_buttonText && m_buttonText->text()->defaultWritingDirection() == WTF::Unicode::RightToLeft) ? RTL : LTR;
  111. innerStyle->setDirection(direction);
  112. } else if (m_optionStyle && document()->page()->chrome().selectItemAlignmentFollowsMenuWritingDirection()) {
  113. if ((m_optionStyle->direction() != innerStyle->direction() || m_optionStyle->unicodeBidi() != innerStyle->unicodeBidi()))
  114. m_innerBlock->setNeedsLayoutAndPrefWidthsRecalc();
  115. innerStyle->setTextAlign(style()->isLeftToRightDirection() ? LEFT : RIGHT);
  116. innerStyle->setDirection(m_optionStyle->direction());
  117. innerStyle->setUnicodeBidi(m_optionStyle->unicodeBidi());
  118. }
  119. }
  120. inline HTMLSelectElement* RenderMenuList::selectElement() const
  121. {
  122. return toHTMLSelectElement(node());
  123. }
  124. void RenderMenuList::addChild(RenderObject* newChild, RenderObject* beforeChild)
  125. {
  126. createInnerBlock();
  127. m_innerBlock->addChild(newChild, beforeChild);
  128. ASSERT(m_innerBlock == firstChild());
  129. if (AXObjectCache* cache = document()->existingAXObjectCache())
  130. cache->childrenChanged(this);
  131. }
  132. void RenderMenuList::removeChild(RenderObject* oldChild)
  133. {
  134. if (oldChild == m_innerBlock || !m_innerBlock) {
  135. RenderFlexibleBox::removeChild(oldChild);
  136. m_innerBlock = 0;
  137. } else
  138. m_innerBlock->removeChild(oldChild);
  139. }
  140. void RenderMenuList::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
  141. {
  142. RenderBlock::styleDidChange(diff, oldStyle);
  143. if (m_buttonText)
  144. m_buttonText->setStyle(style());
  145. if (m_innerBlock) // RenderBlock handled updating the anonymous block's style.
  146. adjustInnerStyle();
  147. bool fontChanged = !oldStyle || oldStyle->font() != style()->font();
  148. if (fontChanged)
  149. updateOptionsWidth();
  150. }
  151. void RenderMenuList::updateOptionsWidth()
  152. {
  153. float maxOptionWidth = 0;
  154. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  155. int size = listItems.size();
  156. FontCachePurgePreventer fontCachePurgePreventer;
  157. for (int i = 0; i < size; ++i) {
  158. HTMLElement* element = listItems[i];
  159. if (!element->hasTagName(optionTag))
  160. continue;
  161. String text = toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
  162. applyTextTransform(style(), text, ' ');
  163. if (theme()->popupOptionSupportsTextIndent()) {
  164. // Add in the option's text indent. We can't calculate percentage values for now.
  165. float optionWidth = 0;
  166. if (RenderStyle* optionStyle = element->renderStyle())
  167. optionWidth += minimumValueForLength(optionStyle->textIndent(), 0, view());
  168. if (!text.isEmpty())
  169. optionWidth += style()->font().width(text);
  170. maxOptionWidth = max(maxOptionWidth, optionWidth);
  171. } else if (!text.isEmpty())
  172. maxOptionWidth = max(maxOptionWidth, style()->font().width(text));
  173. }
  174. int width = static_cast<int>(ceilf(maxOptionWidth));
  175. if (m_optionsWidth == width)
  176. return;
  177. m_optionsWidth = width;
  178. if (parent())
  179. setNeedsLayoutAndPrefWidthsRecalc();
  180. }
  181. void RenderMenuList::updateFromElement()
  182. {
  183. if (m_optionsChanged) {
  184. updateOptionsWidth();
  185. m_optionsChanged = false;
  186. }
  187. if (m_popupIsVisible)
  188. m_popup->updateFromElement();
  189. else
  190. setTextFromOption(selectElement()->selectedIndex());
  191. }
  192. void RenderMenuList::setTextFromOption(int optionIndex)
  193. {
  194. HTMLSelectElement* select = selectElement();
  195. const Vector<HTMLElement*>& listItems = select->listItems();
  196. int size = listItems.size();
  197. int i = select->optionToListIndex(optionIndex);
  198. String text = emptyString();
  199. if (i >= 0 && i < size) {
  200. Element* element = listItems[i];
  201. if (element->hasTagName(optionTag)) {
  202. text = toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
  203. m_optionStyle = element->renderStyle();
  204. }
  205. }
  206. setText(text.stripWhiteSpace());
  207. didUpdateActiveOption(optionIndex);
  208. }
  209. void RenderMenuList::setText(const String& s)
  210. {
  211. if (s.isEmpty()) {
  212. if (!m_buttonText || !m_buttonText->isBR()) {
  213. if (m_buttonText)
  214. m_buttonText->destroy();
  215. m_buttonText = new (renderArena()) RenderBR(document());
  216. m_buttonText->setStyle(style());
  217. addChild(m_buttonText);
  218. }
  219. } else {
  220. if (m_buttonText && !m_buttonText->isBR())
  221. m_buttonText->setText(s.impl(), true);
  222. else {
  223. if (m_buttonText)
  224. m_buttonText->destroy();
  225. m_buttonText = new (renderArena()) RenderText(document(), s.impl());
  226. m_buttonText->setStyle(style());
  227. addChild(m_buttonText);
  228. }
  229. adjustInnerStyle();
  230. }
  231. }
  232. String RenderMenuList::text() const
  233. {
  234. return m_buttonText ? m_buttonText->text() : 0;
  235. }
  236. LayoutRect RenderMenuList::controlClipRect(const LayoutPoint& additionalOffset) const
  237. {
  238. // Clip to the intersection of the content box and the content box for the inner box
  239. // This will leave room for the arrows which sit in the inner box padding,
  240. // and if the inner box ever spills out of the outer box, that will get clipped too.
  241. LayoutRect outerBox(additionalOffset.x() + borderLeft() + paddingLeft(),
  242. additionalOffset.y() + borderTop() + paddingTop(),
  243. contentWidth(),
  244. contentHeight());
  245. LayoutRect innerBox(additionalOffset.x() + m_innerBlock->x() + m_innerBlock->paddingLeft(),
  246. additionalOffset.y() + m_innerBlock->y() + m_innerBlock->paddingTop(),
  247. m_innerBlock->contentWidth(),
  248. m_innerBlock->contentHeight());
  249. return intersection(outerBox, innerBox);
  250. }
  251. void RenderMenuList::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
  252. {
  253. maxLogicalWidth = max(m_optionsWidth, theme()->minimumMenuListSize(style())) + m_innerBlock->paddingLeft() + m_innerBlock->paddingRight();
  254. if (!style()->width().isPercent())
  255. minLogicalWidth = maxLogicalWidth;
  256. }
  257. void RenderMenuList::computePreferredLogicalWidths()
  258. {
  259. m_minPreferredLogicalWidth = 0;
  260. m_maxPreferredLogicalWidth = 0;
  261. if (style()->width().isFixed() && style()->width().value() > 0)
  262. m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(style()->width().value());
  263. else
  264. computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
  265. if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
  266. m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->minWidth().value()));
  267. m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->minWidth().value()));
  268. }
  269. if (style()->maxWidth().isFixed()) {
  270. m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->maxWidth().value()));
  271. m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(style()->maxWidth().value()));
  272. }
  273. LayoutUnit toAdd = borderAndPaddingWidth();
  274. m_minPreferredLogicalWidth += toAdd;
  275. m_maxPreferredLogicalWidth += toAdd;
  276. setPreferredLogicalWidthsDirty(false);
  277. }
  278. void RenderMenuList::showPopup()
  279. {
  280. if (m_popupIsVisible)
  281. return;
  282. if (document()->page()->chrome().hasOpenedPopup())
  283. return;
  284. // Create m_innerBlock here so it ends up as the first child.
  285. // This is important because otherwise we might try to create m_innerBlock
  286. // inside the showPopup call and it would fail.
  287. createInnerBlock();
  288. if (!m_popup)
  289. m_popup = document()->page()->chrome().createPopupMenu(this);
  290. m_popupIsVisible = true;
  291. // Compute the top left taking transforms into account, but use
  292. // the actual width of the element to size the popup.
  293. FloatPoint absTopLeft = localToAbsolute(FloatPoint(), UseTransforms);
  294. IntRect absBounds = absoluteBoundingBoxRectIgnoringTransforms();
  295. absBounds.setLocation(roundedIntPoint(absTopLeft));
  296. HTMLSelectElement* select = selectElement();
  297. m_popup->show(absBounds, document()->view(), select->optionToListIndex(select->selectedIndex()));
  298. }
  299. void RenderMenuList::hidePopup()
  300. {
  301. if (m_popup)
  302. m_popup->hide();
  303. }
  304. void RenderMenuList::valueChanged(unsigned listIndex, bool fireOnChange)
  305. {
  306. // Check to ensure a page navigation has not occurred while
  307. // the popup was up.
  308. Document* doc = toElement(node())->document();
  309. if (!doc || doc != doc->frame()->document())
  310. return;
  311. HTMLSelectElement* select = selectElement();
  312. select->optionSelectedByUser(select->listToOptionIndex(listIndex), fireOnChange);
  313. }
  314. void RenderMenuList::listBoxSelectItem(int listIndex, bool allowMultiplySelections, bool shift, bool fireOnChangeNow)
  315. {
  316. selectElement()->listBoxSelectItem(listIndex, allowMultiplySelections, shift, fireOnChangeNow);
  317. }
  318. bool RenderMenuList::multiple() const
  319. {
  320. return selectElement()->multiple();
  321. }
  322. void RenderMenuList::didSetSelectedIndex(int listIndex)
  323. {
  324. didUpdateActiveOption(selectElement()->listToOptionIndex(listIndex));
  325. }
  326. void RenderMenuList::didUpdateActiveOption(int optionIndex)
  327. {
  328. if (!AXObjectCache::accessibilityEnabled() || !document()->existingAXObjectCache())
  329. return;
  330. if (m_lastActiveIndex == optionIndex)
  331. return;
  332. m_lastActiveIndex = optionIndex;
  333. HTMLSelectElement* select = selectElement();
  334. int listIndex = select->optionToListIndex(optionIndex);
  335. if (listIndex < 0 || listIndex >= static_cast<int>(select->listItems().size()))
  336. return;
  337. ASSERT(select->listItems()[listIndex]);
  338. if (AccessibilityMenuList* menuList = static_cast<AccessibilityMenuList*>(document()->axObjectCache()->get(this)))
  339. menuList->didUpdateActiveOption(optionIndex);
  340. }
  341. String RenderMenuList::itemText(unsigned listIndex) const
  342. {
  343. HTMLSelectElement* select = selectElement();
  344. const Vector<HTMLElement*>& listItems = select->listItems();
  345. if (listIndex >= listItems.size())
  346. return String();
  347. String itemString;
  348. Element* element = listItems[listIndex];
  349. if (element->hasTagName(optgroupTag))
  350. itemString = static_cast<const HTMLOptGroupElement*>(element)->groupLabelText();
  351. else if (element->hasTagName(optionTag))
  352. itemString = toHTMLOptionElement(element)->textIndentedToRespectGroupLabel();
  353. applyTextTransform(style(), itemString, ' ');
  354. return itemString;
  355. }
  356. String RenderMenuList::itemLabel(unsigned) const
  357. {
  358. return String();
  359. }
  360. String RenderMenuList::itemIcon(unsigned) const
  361. {
  362. return String();
  363. }
  364. String RenderMenuList::itemAccessibilityText(unsigned listIndex) const
  365. {
  366. // Allow the accessible name be changed if necessary.
  367. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  368. if (listIndex >= listItems.size())
  369. return String();
  370. return listItems[listIndex]->fastGetAttribute(aria_labelAttr);
  371. }
  372. String RenderMenuList::itemToolTip(unsigned listIndex) const
  373. {
  374. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  375. if (listIndex >= listItems.size())
  376. return String();
  377. return listItems[listIndex]->title();
  378. }
  379. bool RenderMenuList::itemIsEnabled(unsigned listIndex) const
  380. {
  381. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  382. if (listIndex >= listItems.size())
  383. return false;
  384. HTMLElement* element = listItems[listIndex];
  385. if (!element->hasTagName(optionTag))
  386. return false;
  387. bool groupEnabled = true;
  388. if (Element* parentElement = element->parentElement()) {
  389. if (parentElement->hasTagName(optgroupTag))
  390. groupEnabled = !parentElement->isDisabledFormControl();
  391. }
  392. if (!groupEnabled)
  393. return false;
  394. return !element->isDisabledFormControl();
  395. }
  396. PopupMenuStyle RenderMenuList::itemStyle(unsigned listIndex) const
  397. {
  398. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  399. if (listIndex >= listItems.size()) {
  400. // If we are making an out of bounds access, then we want to use the style
  401. // of a different option element (index 0). However, if there isn't an option element
  402. // before at index 0, we fall back to the menu's style.
  403. if (!listIndex)
  404. return menuStyle();
  405. // Try to retrieve the style of an option element we know exists (index 0).
  406. listIndex = 0;
  407. }
  408. HTMLElement* element = listItems[listIndex];
  409. Color itemBackgroundColor;
  410. bool itemHasCustomBackgroundColor;
  411. getItemBackgroundColor(listIndex, itemBackgroundColor, itemHasCustomBackgroundColor);
  412. RenderStyle* style = element->renderStyle() ? element->renderStyle() : element->computedStyle();
  413. return style ? PopupMenuStyle(style->visitedDependentColor(CSSPropertyColor), itemBackgroundColor, style->font(), style->visibility() == VISIBLE,
  414. style->display() == NONE, style->textIndent(), style->direction(), isOverride(style->unicodeBidi()),
  415. itemHasCustomBackgroundColor ? PopupMenuStyle::CustomBackgroundColor : PopupMenuStyle::DefaultBackgroundColor) : menuStyle();
  416. }
  417. void RenderMenuList::getItemBackgroundColor(unsigned listIndex, Color& itemBackgroundColor, bool& itemHasCustomBackgroundColor) const
  418. {
  419. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  420. if (listIndex >= listItems.size()) {
  421. itemBackgroundColor = style()->visitedDependentColor(CSSPropertyBackgroundColor);
  422. itemHasCustomBackgroundColor = false;
  423. return;
  424. }
  425. HTMLElement* element = listItems[listIndex];
  426. Color backgroundColor;
  427. if (element->renderStyle())
  428. backgroundColor = element->renderStyle()->visitedDependentColor(CSSPropertyBackgroundColor);
  429. itemHasCustomBackgroundColor = backgroundColor.isValid() && backgroundColor.alpha();
  430. // If the item has an opaque background color, return that.
  431. if (!backgroundColor.hasAlpha()) {
  432. itemBackgroundColor = backgroundColor;
  433. return;
  434. }
  435. // Otherwise, the item's background is overlayed on top of the menu background.
  436. backgroundColor = style()->visitedDependentColor(CSSPropertyBackgroundColor).blend(backgroundColor);
  437. if (!backgroundColor.hasAlpha()) {
  438. itemBackgroundColor = backgroundColor;
  439. return;
  440. }
  441. // If the menu background is not opaque, then add an opaque white background behind.
  442. itemBackgroundColor = Color(Color::white).blend(backgroundColor);
  443. }
  444. PopupMenuStyle RenderMenuList::menuStyle() const
  445. {
  446. RenderStyle* s = m_innerBlock ? m_innerBlock->style() : style();
  447. return PopupMenuStyle(s->visitedDependentColor(CSSPropertyColor), s->visitedDependentColor(CSSPropertyBackgroundColor), s->font(), s->visibility() == VISIBLE,
  448. s->display() == NONE, s->textIndent(), style()->direction(), isOverride(style()->unicodeBidi()));
  449. }
  450. HostWindow* RenderMenuList::hostWindow() const
  451. {
  452. return document()->view()->hostWindow();
  453. }
  454. PassRefPtr<Scrollbar> RenderMenuList::createScrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize)
  455. {
  456. RefPtr<Scrollbar> widget;
  457. bool hasCustomScrollbarStyle = style()->hasPseudoStyle(SCROLLBAR);
  458. if (hasCustomScrollbarStyle)
  459. widget = RenderScrollbar::createCustomScrollbar(scrollableArea, orientation, this->node());
  460. else
  461. widget = Scrollbar::createNativeScrollbar(scrollableArea, orientation, controlSize);
  462. return widget.release();
  463. }
  464. int RenderMenuList::clientInsetLeft() const
  465. {
  466. return 0;
  467. }
  468. int RenderMenuList::clientInsetRight() const
  469. {
  470. return 0;
  471. }
  472. LayoutUnit RenderMenuList::clientPaddingLeft() const
  473. {
  474. return paddingLeft() + m_innerBlock->paddingLeft();
  475. }
  476. const int endOfLinePadding = 2;
  477. LayoutUnit RenderMenuList::clientPaddingRight() const
  478. {
  479. if (style()->appearance() == MenulistPart || style()->appearance() == MenulistButtonPart) {
  480. // For these appearance values, the theme applies padding to leave room for the
  481. // drop-down button. But leaving room for the button inside the popup menu itself
  482. // looks strange, so we return a small default padding to avoid having a large empty
  483. // space appear on the side of the popup menu.
  484. return endOfLinePadding;
  485. }
  486. // If the appearance isn't MenulistPart, then the select is styled (non-native), so
  487. // we want to return the user specified padding.
  488. return paddingRight() + m_innerBlock->paddingRight();
  489. }
  490. int RenderMenuList::listSize() const
  491. {
  492. return selectElement()->listItems().size();
  493. }
  494. int RenderMenuList::selectedIndex() const
  495. {
  496. HTMLSelectElement* select = selectElement();
  497. return select->optionToListIndex(select->selectedIndex());
  498. }
  499. void RenderMenuList::popupDidHide()
  500. {
  501. m_popupIsVisible = false;
  502. }
  503. bool RenderMenuList::itemIsSeparator(unsigned listIndex) const
  504. {
  505. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  506. return listIndex < listItems.size() && listItems[listIndex]->hasTagName(hrTag);
  507. }
  508. bool RenderMenuList::itemIsLabel(unsigned listIndex) const
  509. {
  510. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  511. return listIndex < listItems.size() && listItems[listIndex]->hasTagName(optgroupTag);
  512. }
  513. bool RenderMenuList::itemIsSelected(unsigned listIndex) const
  514. {
  515. const Vector<HTMLElement*>& listItems = selectElement()->listItems();
  516. if (listIndex >= listItems.size())
  517. return false;
  518. HTMLElement* element = listItems[listIndex];
  519. return element->hasTagName(optionTag) && toHTMLOptionElement(element)->selected();
  520. }
  521. void RenderMenuList::setTextFromItem(unsigned listIndex)
  522. {
  523. setTextFromOption(selectElement()->listToOptionIndex(listIndex));
  524. }
  525. FontSelector* RenderMenuList::fontSelector() const
  526. {
  527. return document()->ensureStyleResolver()->fontSelector();
  528. }
  529. }