OLD | NEW |
| (Empty) |
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 reserv
ed. | |
6 * 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmo
bile.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 | |
25 #include "config.h" | |
26 #include "core/rendering/RenderMenuList.h" | |
27 | |
28 #include "core/HTMLNames.h" | |
29 #include "core/css/CSSFontSelector.h" | |
30 #include "core/css/resolver/StyleResolver.h" | |
31 #include "core/dom/AXObjectCache.h" | |
32 #include "core/dom/NodeLayoutStyle.h" | |
33 #include "core/frame/FrameHost.h" | |
34 #include "core/frame/FrameView.h" | |
35 #include "core/frame/LocalFrame.h" | |
36 #include "core/frame/Settings.h" | |
37 #include "core/html/HTMLOptGroupElement.h" | |
38 #include "core/html/HTMLOptionElement.h" | |
39 #include "core/html/HTMLSelectElement.h" | |
40 #include "core/layout/LayoutBR.h" | |
41 #include "core/layout/LayoutScrollbar.h" | |
42 #include "core/layout/LayoutTheme.h" | |
43 #include "core/layout/LayoutView.h" | |
44 #include "core/page/Chrome.h" | |
45 #include "platform/fonts/FontCache.h" | |
46 #include "platform/geometry/IntSize.h" | |
47 #include "platform/text/PlatformLocale.h" | |
48 #include <math.h> | |
49 | |
50 namespace blink { | |
51 | |
52 using namespace HTMLNames; | |
53 | |
54 RenderMenuList::RenderMenuList(Element* element) | |
55 : LayoutFlexibleBox(element) | |
56 , m_buttonText(nullptr) | |
57 , m_innerBlock(nullptr) | |
58 , m_optionsChanged(true) | |
59 , m_optionsWidth(0) | |
60 , m_lastActiveIndex(-1) | |
61 , m_popupIsVisible(false) | |
62 { | |
63 ASSERT(isHTMLSelectElement(element)); | |
64 } | |
65 | |
66 RenderMenuList::~RenderMenuList() | |
67 { | |
68 ASSERT(!m_popup); | |
69 } | |
70 | |
71 void RenderMenuList::destroy() | |
72 { | |
73 if (m_popup) | |
74 m_popup->disconnectClient(); | |
75 m_popup = nullptr; | |
76 LayoutFlexibleBox::destroy(); | |
77 } | |
78 | |
79 // FIXME: Instead of this hack we should add a ShadowRoot to <select> with no in
sertion point | |
80 // to prevent children from rendering. | |
81 bool RenderMenuList::isChildAllowed(LayoutObject* object, const LayoutStyle&) co
nst | |
82 { | |
83 return object->isAnonymous() && !object->isLayoutFullScreen(); | |
84 } | |
85 | |
86 void RenderMenuList::createInnerBlock() | |
87 { | |
88 if (m_innerBlock) { | |
89 ASSERT(firstChild() == m_innerBlock); | |
90 ASSERT(!m_innerBlock->nextSibling()); | |
91 return; | |
92 } | |
93 | |
94 // Create an anonymous block. | |
95 ASSERT(!firstChild()); | |
96 m_innerBlock = createAnonymousBlock(); | |
97 adjustInnerStyle(); | |
98 LayoutFlexibleBox::addChild(m_innerBlock); | |
99 } | |
100 | |
101 void RenderMenuList::adjustInnerStyle() | |
102 { | |
103 LayoutStyle& innerStyle = m_innerBlock->mutableStyleRef(); | |
104 innerStyle.setFlexGrow(1); | |
105 innerStyle.setFlexShrink(1); | |
106 // Use margin:auto instead of align-items:center to get safe centering, i.e. | |
107 // when the content overflows, treat it the same as align-items: flex-start. | |
108 // But we only do that for the cases where html.css would otherwise use cent
er. | |
109 if (style()->alignItems() == ItemPositionCenter) { | |
110 innerStyle.setMarginTop(Length()); | |
111 innerStyle.setMarginBottom(Length()); | |
112 innerStyle.setAlignSelf(ItemPositionFlexStart); | |
113 } | |
114 | |
115 innerStyle.setPaddingLeft(Length(LayoutTheme::theme().popupInternalPaddingLe
ft(styleRef()), Fixed)); | |
116 innerStyle.setPaddingRight(Length(LayoutTheme::theme().popupInternalPaddingR
ight(styleRef()), Fixed)); | |
117 innerStyle.setPaddingTop(Length(LayoutTheme::theme().popupInternalPaddingTop
(styleRef()), Fixed)); | |
118 innerStyle.setPaddingBottom(Length(LayoutTheme::theme().popupInternalPadding
Bottom(styleRef()), Fixed)); | |
119 | |
120 if (m_optionStyle) { | |
121 if ((m_optionStyle->direction() != innerStyle.direction() || m_optionSty
le->unicodeBidi() != innerStyle.unicodeBidi())) | |
122 m_innerBlock->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidat
ion(); | |
123 innerStyle.setTextAlign(style()->isLeftToRightDirection() ? LEFT : RIGHT
); | |
124 innerStyle.setDirection(m_optionStyle->direction()); | |
125 innerStyle.setUnicodeBidi(m_optionStyle->unicodeBidi()); | |
126 } | |
127 } | |
128 | |
129 inline HTMLSelectElement* RenderMenuList::selectElement() const | |
130 { | |
131 return toHTMLSelectElement(node()); | |
132 } | |
133 | |
134 void RenderMenuList::addChild(LayoutObject* newChild, LayoutObject* beforeChild) | |
135 { | |
136 createInnerBlock(); | |
137 m_innerBlock->addChild(newChild, beforeChild); | |
138 ASSERT(m_innerBlock == firstChild()); | |
139 | |
140 if (AXObjectCache* cache = document().existingAXObjectCache()) | |
141 cache->childrenChanged(this); | |
142 } | |
143 | |
144 void RenderMenuList::removeChild(LayoutObject* oldChild) | |
145 { | |
146 if (oldChild == m_innerBlock || !m_innerBlock) { | |
147 LayoutFlexibleBox::removeChild(oldChild); | |
148 m_innerBlock = nullptr; | |
149 } else | |
150 m_innerBlock->removeChild(oldChild); | |
151 } | |
152 | |
153 void RenderMenuList::styleDidChange(StyleDifference diff, const LayoutStyle* old
Style) | |
154 { | |
155 LayoutBlock::styleDidChange(diff, oldStyle); | |
156 | |
157 if (m_buttonText) | |
158 m_buttonText->setStyle(style()); | |
159 if (m_innerBlock) // LayoutBlock handled updating the anonymous block's styl
e. | |
160 adjustInnerStyle(); | |
161 | |
162 bool fontChanged = !oldStyle || oldStyle->font() != style()->font(); | |
163 if (fontChanged) | |
164 updateOptionsWidth(); | |
165 } | |
166 | |
167 void RenderMenuList::updateOptionsWidth() | |
168 { | |
169 float maxOptionWidth = 0; | |
170 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
171 int size = listItems.size(); | |
172 | |
173 for (int i = 0; i < size; ++i) { | |
174 HTMLElement* element = listItems[i]; | |
175 if (!isHTMLOptionElement(*element)) | |
176 continue; | |
177 | |
178 String text = toHTMLOptionElement(element)->textIndentedToRespectGroupLa
bel(); | |
179 applyTextTransform(style(), text, ' '); | |
180 if (LayoutTheme::theme().popupOptionSupportsTextIndent()) { | |
181 // Add in the option's text indent. We can't calculate percentage v
alues for now. | |
182 float optionWidth = 0; | |
183 if (const LayoutStyle* optionStyle = element->layoutStyle()) | |
184 optionWidth += minimumValueForLength(optionStyle->textIndent(),
0); | |
185 if (!text.isEmpty()) | |
186 optionWidth += style()->font().width(text); | |
187 maxOptionWidth = std::max(maxOptionWidth, optionWidth); | |
188 } else if (!text.isEmpty()) { | |
189 maxOptionWidth = std::max(maxOptionWidth, style()->font().width(text
)); | |
190 } | |
191 } | |
192 | |
193 int width = static_cast<int>(ceilf(maxOptionWidth)); | |
194 if (m_optionsWidth == width) | |
195 return; | |
196 | |
197 m_optionsWidth = width; | |
198 if (parent()) | |
199 setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation(); | |
200 } | |
201 | |
202 void RenderMenuList::updateFromElement() | |
203 { | |
204 if (m_optionsChanged) { | |
205 updateOptionsWidth(); | |
206 m_optionsChanged = false; | |
207 } | |
208 | |
209 if (m_popupIsVisible) | |
210 m_popup->updateFromElement(); | |
211 | |
212 if (selectElement()->suggestedIndex() >= 0) | |
213 setTextFromOption(selectElement()->suggestedIndex()); | |
214 else | |
215 setTextFromOption(selectElement()->selectedIndex()); | |
216 } | |
217 | |
218 void RenderMenuList::setTextFromOption(int optionIndex) | |
219 { | |
220 HTMLSelectElement* select = selectElement(); | |
221 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
->listItems(); | |
222 const int size = listItems.size(); | |
223 | |
224 String text = emptyString(); | |
225 m_optionStyle.clear(); | |
226 | |
227 if (multiple()) { | |
228 unsigned selectedCount = 0; | |
229 int firstSelectedIndex = -1; | |
230 for (int i = 0; i < size; ++i) { | |
231 Element* element = listItems[i]; | |
232 if (!isHTMLOptionElement(*element)) | |
233 continue; | |
234 | |
235 if (toHTMLOptionElement(element)->selected()) { | |
236 if (++selectedCount == 1) | |
237 firstSelectedIndex = i; | |
238 } | |
239 } | |
240 | |
241 if (selectedCount == 1) { | |
242 ASSERT(0 <= firstSelectedIndex); | |
243 ASSERT(firstSelectedIndex < size); | |
244 HTMLOptionElement* selectedOptionElement = toHTMLOptionElement(listI
tems[firstSelectedIndex]); | |
245 ASSERT(selectedOptionElement->selected()); | |
246 text = selectedOptionElement->textIndentedToRespectGroupLabel(); | |
247 m_optionStyle = selectedOptionElement->layoutStyle(); | |
248 } else { | |
249 Locale& locale = select->locale(); | |
250 String localizedNumberString = locale.convertToLocalizedNumber(Strin
g::number(selectedCount)); | |
251 text = locale.queryString(WebLocalizedString::SelectMenuListText, lo
calizedNumberString); | |
252 ASSERT(!m_optionStyle); | |
253 } | |
254 } else { | |
255 const int i = select->optionToListIndex(optionIndex); | |
256 if (i >= 0 && i < size) { | |
257 Element* element = listItems[i]; | |
258 if (isHTMLOptionElement(*element)) { | |
259 text = toHTMLOptionElement(element)->textIndentedToRespectGroupL
abel(); | |
260 m_optionStyle = element->layoutStyle(); | |
261 } | |
262 } | |
263 } | |
264 | |
265 setText(text.stripWhiteSpace()); | |
266 | |
267 didUpdateActiveOption(optionIndex); | |
268 } | |
269 | |
270 void RenderMenuList::setText(const String& s) | |
271 { | |
272 if (s.isEmpty()) { | |
273 if (!m_buttonText || !m_buttonText->isBR()) { | |
274 // FIXME: We should not modify the structure of the render tree | |
275 // during layout. crbug.com/370462 | |
276 DeprecatedDisableModifyRenderTreeStructureAsserts disabler; | |
277 if (m_buttonText) | |
278 m_buttonText->destroy(); | |
279 m_buttonText = new LayoutBR(&document()); | |
280 m_buttonText->setStyle(style()); | |
281 addChild(m_buttonText); | |
282 } | |
283 } else { | |
284 if (m_buttonText && !m_buttonText->isBR()) | |
285 m_buttonText->setText(s.impl(), true); | |
286 else { | |
287 // FIXME: We should not modify the structure of the render tree | |
288 // during layout. crbug.com/370462 | |
289 DeprecatedDisableModifyRenderTreeStructureAsserts disabler; | |
290 if (m_buttonText) | |
291 m_buttonText->destroy(); | |
292 m_buttonText = new LayoutText(&document(), s.impl()); | |
293 m_buttonText->setStyle(style()); | |
294 // We need to set the text explicitly though it was specified in the | |
295 // constructor because LayoutText doesn't refer to the text | |
296 // specified in the constructor in a case of re-transforming. | |
297 m_buttonText->setText(s.impl(), true); | |
298 addChild(m_buttonText); | |
299 } | |
300 adjustInnerStyle(); | |
301 } | |
302 } | |
303 | |
304 String RenderMenuList::text() const | |
305 { | |
306 return m_buttonText ? m_buttonText->text() : String(); | |
307 } | |
308 | |
309 LayoutRect RenderMenuList::controlClipRect(const LayoutPoint& additionalOffset)
const | |
310 { | |
311 // Clip to the intersection of the content box and the content box for the i
nner box | |
312 // This will leave room for the arrows which sit in the inner box padding, | |
313 // and if the inner box ever spills out of the outer box, that will get clip
ped too. | |
314 LayoutRect outerBox = contentBoxRect(); | |
315 outerBox.moveBy(additionalOffset); | |
316 | |
317 LayoutRect innerBox(additionalOffset + m_innerBlock->location() | |
318 + LayoutSize(m_innerBlock->paddingLeft(), m_innerBlock->paddingTop()) | |
319 , m_innerBlock->contentSize()); | |
320 | |
321 return intersection(outerBox, innerBox); | |
322 } | |
323 | |
324 void RenderMenuList::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth,
LayoutUnit& maxLogicalWidth) const | |
325 { | |
326 maxLogicalWidth = std::max(m_optionsWidth, LayoutTheme::theme().minimumMenuL
istSize(styleRef())) + m_innerBlock->paddingLeft() + m_innerBlock->paddingRight(
); | |
327 if (!style()->width().isPercent()) | |
328 minLogicalWidth = maxLogicalWidth; | |
329 } | |
330 | |
331 void RenderMenuList::showPopup() | |
332 { | |
333 if (m_popupIsVisible) | |
334 return; | |
335 | |
336 if (document().frameHost()->chrome().hasOpenedPopup()) | |
337 return; | |
338 | |
339 // Create m_innerBlock here so it ends up as the first child. | |
340 // This is important because otherwise we might try to create m_innerBlock | |
341 // inside the showPopup call and it would fail. | |
342 createInnerBlock(); | |
343 if (!m_popup) | |
344 m_popup = document().frameHost()->chrome().createPopupMenu(*document().f
rame(), this); | |
345 m_popupIsVisible = true; | |
346 | |
347 FloatQuad quad(localToAbsoluteQuad(FloatQuad(borderBoundingBox()))); | |
348 IntSize size = pixelSnappedIntRect(frameRect()).size(); | |
349 HTMLSelectElement* select = selectElement(); | |
350 m_popup->show(quad, size, select->optionToListIndex(select->selectedIndex())
); | |
351 } | |
352 | |
353 void RenderMenuList::hidePopup() | |
354 { | |
355 if (m_popup) | |
356 m_popup->hide(); | |
357 } | |
358 | |
359 void RenderMenuList::valueChanged(unsigned listIndex, bool fireOnChange) | |
360 { | |
361 // Check to ensure a page navigation has not occurred while | |
362 // the popup was up. | |
363 Document& doc = toElement(node())->document(); | |
364 if (&doc != doc.frame()->document()) | |
365 return; | |
366 | |
367 HTMLSelectElement* select = selectElement(); | |
368 select->optionSelectedByUser(select->listToOptionIndex(listIndex), fireOnCha
nge); | |
369 } | |
370 | |
371 void RenderMenuList::listBoxSelectItem(int listIndex, bool allowMultiplySelectio
ns, bool shift, bool fireOnChangeNow) | |
372 { | |
373 selectElement()->listBoxSelectItem(listIndex, allowMultiplySelections, shift
, fireOnChangeNow); | |
374 } | |
375 | |
376 bool RenderMenuList::multiple() const | |
377 { | |
378 return selectElement()->multiple(); | |
379 } | |
380 | |
381 IntRect RenderMenuList::elementRectRelativeToViewport() const | |
382 { | |
383 return selectElement()->document().view()->contentsToWindow(absoluteBounding
BoxRect()); | |
384 } | |
385 | |
386 Element& RenderMenuList::ownerElement() const | |
387 { | |
388 return *selectElement(); | |
389 } | |
390 | |
391 const LayoutStyle* RenderMenuList::layoutStyleForItem(Element& element) const | |
392 { | |
393 document().updateRenderTreeIfNeeded(); | |
394 return element.layoutStyle() ? element.layoutStyle() : element.computedStyle
(); | |
395 } | |
396 | |
397 void RenderMenuList::didSetSelectedIndex(int listIndex) | |
398 { | |
399 didUpdateActiveOption(selectElement()->listToOptionIndex(listIndex)); | |
400 } | |
401 | |
402 void RenderMenuList::didUpdateActiveOption(int optionIndex) | |
403 { | |
404 if (!document().existingAXObjectCache()) | |
405 return; | |
406 | |
407 if (m_lastActiveIndex == optionIndex) | |
408 return; | |
409 m_lastActiveIndex = optionIndex; | |
410 | |
411 HTMLSelectElement* select = selectElement(); | |
412 int listIndex = select->optionToListIndex(optionIndex); | |
413 if (listIndex < 0 || listIndex >= static_cast<int>(select->listItems().size(
))) | |
414 return; | |
415 document().existingAXObjectCache()->handleUpdateActiveMenuOption(this, optio
nIndex); | |
416 } | |
417 | |
418 String RenderMenuList::itemText(unsigned listIndex) const | |
419 { | |
420 HTMLSelectElement* select = selectElement(); | |
421 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
->listItems(); | |
422 if (listIndex >= listItems.size()) | |
423 return String(); | |
424 | |
425 String itemString; | |
426 Element* element = listItems[listIndex]; | |
427 if (isHTMLOptGroupElement(*element)) | |
428 itemString = toHTMLOptGroupElement(*element).groupLabelText(); | |
429 else if (isHTMLOptionElement(*element)) | |
430 itemString = toHTMLOptionElement(*element).textIndentedToRespectGroupLab
el(); | |
431 | |
432 applyTextTransform(style(), itemString, ' '); | |
433 return itemString; | |
434 } | |
435 | |
436 String RenderMenuList::itemAccessibilityText(unsigned listIndex) const | |
437 { | |
438 // Allow the accessible name be changed if necessary. | |
439 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
440 if (listIndex >= listItems.size()) | |
441 return String(); | |
442 return listItems[listIndex]->fastGetAttribute(aria_labelAttr); | |
443 } | |
444 | |
445 String RenderMenuList::itemToolTip(unsigned listIndex) const | |
446 { | |
447 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
448 if (listIndex >= listItems.size()) | |
449 return String(); | |
450 return listItems[listIndex]->title(); | |
451 } | |
452 | |
453 bool RenderMenuList::itemIsEnabled(unsigned listIndex) const | |
454 { | |
455 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
456 if (listIndex >= listItems.size()) | |
457 return false; | |
458 HTMLElement* element = listItems[listIndex]; | |
459 if (!isHTMLOptionElement(*element)) | |
460 return false; | |
461 | |
462 bool groupEnabled = true; | |
463 if (Element* parentElement = element->parentElement()) { | |
464 if (isHTMLOptGroupElement(*parentElement)) | |
465 groupEnabled = !parentElement->isDisabledFormControl(); | |
466 } | |
467 if (!groupEnabled) | |
468 return false; | |
469 | |
470 return !element->isDisabledFormControl(); | |
471 } | |
472 | |
473 PopupMenuStyle RenderMenuList::itemStyle(unsigned listIndex) const | |
474 { | |
475 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
476 if (listIndex >= listItems.size()) { | |
477 // If we are making an out of bounds access, then we want to use the sty
le | |
478 // of a different option element (index 0). However, if there isn't an o
ption element | |
479 // before at index 0, we fall back to the menu's style. | |
480 if (!listIndex) | |
481 return menuStyle(); | |
482 | |
483 // Try to retrieve the style of an option element we know exists (index
0). | |
484 listIndex = 0; | |
485 } | |
486 HTMLElement* element = listItems[listIndex]; | |
487 | |
488 Color itemBackgroundColor; | |
489 bool itemHasCustomBackgroundColor; | |
490 getItemBackgroundColor(listIndex, itemBackgroundColor, itemHasCustomBackgrou
ndColor); | |
491 | |
492 const LayoutStyle* style = element->layoutStyle() ? element->layoutStyle() :
element->computedStyle(); | |
493 return style ? PopupMenuStyle(resolveColor(*style, CSSPropertyColor), itemBa
ckgroundColor, style->font(), style->visibility() == VISIBLE, | |
494 isHTMLOptionElement(*element) ? toHTMLOptionElement(*element).isDisplayN
one() : style->display() == NONE, | |
495 style->textIndent(), style->direction(), isOverride(style->unicodeBidi()
), | |
496 itemHasCustomBackgroundColor ? PopupMenuStyle::CustomBackgroundColor : P
opupMenuStyle::DefaultBackgroundColor) : menuStyle(); | |
497 } | |
498 | |
499 void RenderMenuList::getItemBackgroundColor(unsigned listIndex, Color& itemBackg
roundColor, bool& itemHasCustomBackgroundColor) const | |
500 { | |
501 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
502 if (listIndex >= listItems.size()) { | |
503 itemBackgroundColor = resolveColor(CSSPropertyBackgroundColor); | |
504 itemHasCustomBackgroundColor = false; | |
505 return; | |
506 } | |
507 HTMLElement* element = listItems[listIndex]; | |
508 | |
509 Color backgroundColor; | |
510 if (const LayoutStyle* style = element->layoutStyle()) | |
511 backgroundColor = resolveColor(*style, CSSPropertyBackgroundColor); | |
512 itemHasCustomBackgroundColor = backgroundColor.alpha(); | |
513 // If the item has an opaque background color, return that. | |
514 if (!backgroundColor.hasAlpha()) { | |
515 itemBackgroundColor = backgroundColor; | |
516 return; | |
517 } | |
518 | |
519 // Otherwise, the item's background is overlayed on top of the menu backgrou
nd. | |
520 backgroundColor = resolveColor(CSSPropertyBackgroundColor).blend(backgroundC
olor); | |
521 if (!backgroundColor.hasAlpha()) { | |
522 itemBackgroundColor = backgroundColor; | |
523 return; | |
524 } | |
525 | |
526 // If the menu background is not opaque, then add an opaque white background
behind. | |
527 itemBackgroundColor = Color(Color::white).blend(backgroundColor); | |
528 } | |
529 | |
530 PopupMenuStyle RenderMenuList::menuStyle() const | |
531 { | |
532 const LayoutObject* o = m_innerBlock ? m_innerBlock : this; | |
533 const LayoutStyle& style = o->styleRef(); | |
534 return PopupMenuStyle(o->resolveColor(CSSPropertyColor), o->resolveColor(CSS
PropertyBackgroundColor), style.font(), style.visibility() == VISIBLE, | |
535 style.display() == NONE, style.textIndent(), style.direction(), isOverri
de(style.unicodeBidi())); | |
536 } | |
537 | |
538 LayoutUnit RenderMenuList::clientPaddingLeft() const | |
539 { | |
540 return paddingLeft() + m_innerBlock->paddingLeft(); | |
541 } | |
542 | |
543 const int endOfLinePadding = 2; | |
544 LayoutUnit RenderMenuList::clientPaddingRight() const | |
545 { | |
546 if (style()->appearance() == MenulistPart || style()->appearance() == Menuli
stButtonPart) { | |
547 // For these appearance values, the theme applies padding to leave room
for the | |
548 // drop-down button. But leaving room for the button inside the popup me
nu itself | |
549 // looks strange, so we return a small default padding to avoid having a
large empty | |
550 // space appear on the side of the popup menu. | |
551 return endOfLinePadding; | |
552 } | |
553 | |
554 // If the appearance isn't MenulistPart, then the select is styled (non-nati
ve), so | |
555 // we want to return the user specified padding. | |
556 return paddingRight() + m_innerBlock->paddingRight(); | |
557 } | |
558 | |
559 int RenderMenuList::listSize() const | |
560 { | |
561 return selectElement()->listItems().size(); | |
562 } | |
563 | |
564 int RenderMenuList::selectedIndex() const | |
565 { | |
566 HTMLSelectElement* select = selectElement(); | |
567 return select->optionToListIndex(select->selectedIndex()); | |
568 } | |
569 | |
570 void RenderMenuList::popupDidHide() | |
571 { | |
572 m_popupIsVisible = false; | |
573 } | |
574 | |
575 bool RenderMenuList::itemIsSeparator(unsigned listIndex) const | |
576 { | |
577 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
578 return listIndex < listItems.size() && isHTMLHRElement(*listItems[listIndex]
); | |
579 } | |
580 | |
581 bool RenderMenuList::itemIsLabel(unsigned listIndex) const | |
582 { | |
583 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
584 return listIndex < listItems.size() && isHTMLOptGroupElement(*listItems[list
Index]); | |
585 } | |
586 | |
587 bool RenderMenuList::itemIsSelected(unsigned listIndex) const | |
588 { | |
589 const WillBeHeapVector<RawPtrWillBeMember<HTMLElement> >& listItems = select
Element()->listItems(); | |
590 if (listIndex >= listItems.size()) | |
591 return false; | |
592 HTMLElement* element = listItems[listIndex]; | |
593 return isHTMLOptionElement(*element) && toHTMLOptionElement(*element).select
ed(); | |
594 } | |
595 | |
596 void RenderMenuList::setTextFromItem(unsigned listIndex) | |
597 { | |
598 setTextFromOption(selectElement()->listToOptionIndex(listIndex)); | |
599 } | |
600 | |
601 } // namespace blink | |
OLD | NEW |