Home | History | Annotate | Download | only in rendering
      1 /*
      2  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #include "config.h"
     22 #include "core/rendering/RenderSlider.h"
     23 
     24 #include "core/dom/shadow/ShadowRoot.h"
     25 #include "core/html/HTMLInputElement.h"
     26 #include "core/html/shadow/ShadowElementNames.h"
     27 #include "core/html/shadow/SliderThumbElement.h"
     28 #include "wtf/MathExtras.h"
     29 
     30 using std::min;
     31 
     32 namespace WebCore {
     33 
     34 const int RenderSlider::defaultTrackLength = 129;
     35 
     36 RenderSlider::RenderSlider(HTMLInputElement* element)
     37     : RenderFlexibleBox(element)
     38 {
     39     // We assume RenderSlider works only with <input type=range>.
     40     ASSERT(element->isRangeControl());
     41 }
     42 
     43 RenderSlider::~RenderSlider()
     44 {
     45 }
     46 
     47 int RenderSlider::baselinePosition(FontBaseline, bool /*firstLine*/, LineDirectionMode, LinePositionMode linePositionMode) const
     48 {
     49     ASSERT(linePositionMode == PositionOnContainingLine);
     50     // FIXME: Patch this function for writing-mode.
     51     return height() + marginTop();
     52 }
     53 
     54 void RenderSlider::computeIntrinsicLogicalWidths(LayoutUnit& minLogicalWidth, LayoutUnit& maxLogicalWidth) const
     55 {
     56     maxLogicalWidth = defaultTrackLength * style()->effectiveZoom();
     57     if (!style()->width().isPercent())
     58         minLogicalWidth = maxLogicalWidth;
     59 }
     60 
     61 void RenderSlider::computePreferredLogicalWidths()
     62 {
     63     m_minPreferredLogicalWidth = 0;
     64     m_maxPreferredLogicalWidth = 0;
     65     RenderStyle* styleToUse = style();
     66 
     67     if (styleToUse->width().isFixed() && styleToUse->width().value() > 0)
     68         m_minPreferredLogicalWidth = m_maxPreferredLogicalWidth = adjustContentBoxLogicalWidthForBoxSizing(styleToUse->width().value());
     69     else
     70         computeIntrinsicLogicalWidths(m_minPreferredLogicalWidth, m_maxPreferredLogicalWidth);
     71 
     72     if (styleToUse->minWidth().isFixed() && styleToUse->minWidth().value() > 0) {
     73         m_maxPreferredLogicalWidth = max(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->minWidth().value()));
     74         m_minPreferredLogicalWidth = max(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->minWidth().value()));
     75     }
     76 
     77     if (styleToUse->maxWidth().isFixed()) {
     78         m_maxPreferredLogicalWidth = min(m_maxPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->maxWidth().value()));
     79         m_minPreferredLogicalWidth = min(m_minPreferredLogicalWidth, adjustContentBoxLogicalWidthForBoxSizing(styleToUse->maxWidth().value()));
     80     }
     81 
     82     LayoutUnit toAdd = borderAndPaddingWidth();
     83     m_minPreferredLogicalWidth += toAdd;
     84     m_maxPreferredLogicalWidth += toAdd;
     85 
     86     clearPreferredLogicalWidthsDirty();
     87 }
     88 
     89 inline SliderThumbElement* RenderSlider::sliderThumbElement() const
     90 {
     91     return toSliderThumbElement(toElement(node())->userAgentShadowRoot()->getElementById(ShadowElementNames::sliderThumb()));
     92 }
     93 
     94 void RenderSlider::layout()
     95 {
     96     // FIXME: Find a way to cascade appearance.
     97     // http://webkit.org/b/62535
     98     RenderBox* thumbBox = sliderThumbElement()->renderBox();
     99     if (thumbBox && thumbBox->isSliderThumb())
    100         static_cast<RenderSliderThumb*>(thumbBox)->updateAppearance(style());
    101 
    102     RenderFlexibleBox::layout();
    103 }
    104 
    105 bool RenderSlider::inDragMode() const
    106 {
    107     return sliderThumbElement()->active();
    108 }
    109 
    110 } // namespace WebCore
    111