1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
/**
*
* Copyright (C) 2006, 2007, 2008 Apple Computer, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderSlider.h"
#include "CSSPropertyNames.h"
#include "Document.h"
#include "Event.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "Frame.h"
#include "HTMLInputElement.h"
#include "HTMLDivElement.h"
#include "HTMLNames.h"
#include "MouseEvent.h"
#include "RenderTheme.h"
#include <wtf/MathExtras.h>
using std::min;
namespace WebCore {
using namespace EventNames;
using namespace HTMLNames;
const int defaultTrackLength = 129;
class HTMLSliderThumbElement : public HTMLDivElement {
public:
HTMLSliderThumbElement(Document*, Node* shadowParent = 0);
virtual void defaultEventHandler(Event*);
virtual bool isShadowNode() const { return true; }
virtual Node* shadowParentNode() { return m_shadowParent; }
bool inDragMode() const { return m_inDragMode; }
private:
Node* m_shadowParent;
IntPoint m_initialClickPoint;
int m_initialPosition;
bool m_inDragMode;
};
HTMLSliderThumbElement::HTMLSliderThumbElement(Document* doc, Node* shadowParent)
: HTMLDivElement(doc)
, m_shadowParent(shadowParent)
, m_initialClickPoint(IntPoint())
, m_initialPosition(0)
, m_inDragMode(false)
{
}
void HTMLSliderThumbElement::defaultEventHandler(Event* event)
{
const AtomicString& eventType = event->type();
if (eventType == mousedownEvent && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() == LeftButton) {
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
if (document()->frame() && renderer() && renderer()->parent()
&& static_cast<RenderSlider*>(renderer()->parent())->mouseEventIsInThumb(mouseEvent)) {
// Cache the initial point where the mouse down occurred.
m_initialClickPoint = IntPoint(mouseEvent->pageX(), mouseEvent->pageY());
// Cache the initial position of the thumb.
m_initialPosition = static_cast<RenderSlider*>(renderer()->parent())->currentPosition();
m_inDragMode = true;
document()->frame()->eventHandler()->setCapturingMouseEventsNode(m_shadowParent);
event->setDefaultHandled();
return;
}
} else if (eventType == mouseupEvent && event->isMouseEvent() && static_cast<MouseEvent*>(event)->button() == LeftButton) {
if (m_inDragMode) {
if (Frame* frame = document()->frame())
frame->eventHandler()->setCapturingMouseEventsNode(0);
m_inDragMode = false;
event->setDefaultHandled();
return;
}
} else if (eventType == mousemoveEvent && event->isMouseEvent()) {
if (m_inDragMode && renderer() && renderer()->parent()) {
// Move the slider
MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
RenderSlider* slider = static_cast<RenderSlider*>(renderer()->parent());
int newPosition = slider->positionForOffset(
IntPoint(m_initialPosition + mouseEvent->pageX() - m_initialClickPoint.x()
+ (renderer()->width() / 2),
m_initialPosition + mouseEvent->pageY() - m_initialClickPoint.y()
+ (renderer()->height() / 2)));
if (slider->currentPosition() != newPosition) {
slider->setCurrentPosition(newPosition);
slider->valueChanged();
}
event->setDefaultHandled();
return;
}
}
HTMLDivElement::defaultEventHandler(event);
}
RenderSlider::RenderSlider(HTMLInputElement* element)
: RenderBlock(element)
, m_thumb(0)
{
}
RenderSlider::~RenderSlider()
{
if (m_thumb)
m_thumb->detach();
}
short RenderSlider::baselinePosition(bool b, bool isRootLineBox) const
{
return height() + marginTop();
}
void RenderSlider::calcPrefWidths()
{
m_minPrefWidth = 0;
m_maxPrefWidth = 0;
if (style()->width().isFixed() && style()->width().value() > 0)
m_minPrefWidth = m_maxPrefWidth = calcContentBoxWidth(style()->width().value());
else
m_maxPrefWidth = defaultTrackLength;
if (style()->minWidth().isFixed() && style()->minWidth().value() > 0) {
m_maxPrefWidth = max(m_maxPrefWidth, calcContentBoxWidth(style()->minWidth().value()));
m_minPrefWidth = max(m_minPrefWidth, calcContentBoxWidth(style()->minWidth().value()));
} else if (style()->width().isPercent() || (style()->width().isAuto() && style()->height().isPercent()))
m_minPrefWidth = 0;
else
m_minPrefWidth = m_maxPrefWidth;
if (style()->maxWidth().isFixed() && style()->maxWidth().value() != undefinedLength) {
m_maxPrefWidth = min(m_maxPrefWidth, calcContentBoxWidth(style()->maxWidth().value()));
m_minPrefWidth = min(m_minPrefWidth, calcContentBoxWidth(style()->maxWidth().value()));
}
int toAdd = paddingLeft() + paddingRight() + borderLeft() + borderRight();
m_minPrefWidth += toAdd;
m_maxPrefWidth += toAdd;
setPrefWidthsDirty(false);
}
void RenderSlider::setStyle(RenderStyle* newStyle)
{
RenderBlock::setStyle(newStyle);
if (m_thumb) {
RenderStyle* thumbStyle = createThumbStyle(newStyle, m_thumb->renderer()->style());
m_thumb->renderer()->setStyle(thumbStyle);
}
setReplaced(isInline());
}
RenderStyle* RenderSlider::createThumbStyle(RenderStyle* parentStyle, RenderStyle* oldStyle)
{
RenderStyle* style;
RenderStyle* pseudoStyle = getPseudoStyle(RenderStyle::SLIDER_THUMB);
if (pseudoStyle)
// We may be sharing style with another slider, but we must not share the thumb style.
style = new (renderArena()) RenderStyle(*pseudoStyle);
else
style = new (renderArena()) RenderStyle();
if (parentStyle)
style->inheritFrom(parentStyle);
style->setDisplay(BLOCK);
style->setPosition(RelativePosition);
if (oldStyle) {
style->setLeft(oldStyle->left());
style->setTop(oldStyle->top());
}
if (parentStyle->appearance() == SliderVerticalAppearance)
style->setAppearance(SliderThumbVerticalAppearance);
else if (parentStyle->appearance() == SliderHorizontalAppearance)
style->setAppearance(SliderThumbHorizontalAppearance);
else if (parentStyle->appearance() == MediaSliderAppearance)
style->setAppearance(MediaSliderThumbAppearance);
return style;
}
void RenderSlider::layout()
{
bool relayoutChildren = false;
if (m_thumb && m_thumb->renderer()) {
int oldWidth = m_width;
calcWidth();
int oldHeight = m_height;
calcHeight();
if (oldWidth != m_width || oldHeight != m_height)
relayoutChildren = true;
// Allow the theme to set the size of the thumb
if (m_thumb->renderer()->style()->hasAppearance())
theme()->adjustSliderThumbSize(m_thumb->renderer());
if (style()->appearance() == SliderVerticalAppearance) {
// FIXME: Handle percentage widths correctly. See http://bugs.webkit.org/show_bug.cgi?id=12104
m_thumb->renderer()->style()->setLeft(Length(contentWidth() / 2 - m_thumb->renderer()->style()->width().value() / 2, Fixed));
} else {
// FIXME: Handle percentage heights correctly. See http://bugs.webkit.org/show_bug.cgi?id=12104
m_thumb->renderer()->style()->setTop(Length(contentHeight() / 2 - m_thumb->renderer()->style()->height().value() / 2, Fixed));
}
if (relayoutChildren)
setPositionFromValue(true);
}
RenderBlock::layoutBlock(relayoutChildren);
}
void RenderSlider::updateFromElement()
{
if (!m_thumb) {
m_thumb = new HTMLSliderThumbElement(document(), node());
RenderStyle* thumbStyle = createThumbStyle(style());
m_thumb->setRenderer(m_thumb->createRenderer(renderArena(), thumbStyle));
m_thumb->renderer()->setStyle(thumbStyle);
m_thumb->setAttached();
m_thumb->setInDocument(true);
addChild(m_thumb->renderer());
}
setPositionFromValue();
setNeedsLayout(true);
}
bool RenderSlider::mouseEventIsInThumb(MouseEvent* evt)
{
if (!m_thumb || !m_thumb->renderer())
return false;
IntRect thumbBounds = m_thumb->renderer()->absoluteBoundingBoxRect();
return thumbBounds.contains(evt->pageX(), evt->pageY());
}
void RenderSlider::setValueForPosition(int position)
{
if (!m_thumb || !m_thumb->renderer())
return;
const AtomicString& minStr = static_cast<HTMLInputElement*>(node())->getAttribute(minAttr);
const AtomicString& maxStr = static_cast<HTMLInputElement*>(node())->getAttribute(maxAttr);
const AtomicString& precision = static_cast<HTMLInputElement*>(node())->getAttribute(precisionAttr);
double minVal = minStr.isNull() ? 0.0 : minStr.toDouble();
double maxVal = maxStr.isNull() ? 100.0 : maxStr.toDouble();
minVal = min(minVal, maxVal); // Make sure the range is sane.
// Calculate the new value based on the position
double factor = (double)position / (double)trackSize();
if (style()->appearance() == SliderVerticalAppearance)
factor = 1.0 - factor;
double val = minVal + factor * (maxVal - minVal);
val = max(minVal, min(val, maxVal)); // Make sure val is within min/max.
// Force integer value if not float.
if (!equalIgnoringCase(precision, "float"))
val = lround(val);
static_cast<HTMLInputElement*>(node())->setValueFromRenderer(String::number(val));
if (position != currentPosition()) {
setCurrentPosition(position);
static_cast<HTMLInputElement*>(node())->onChange();
}
}
double RenderSlider::setPositionFromValue(bool inLayout)
{
if (!m_thumb || !m_thumb->renderer())
return 0;
if (!inLayout)
document()->updateLayout();
String value = static_cast<HTMLInputElement*>(node())->value();
const AtomicString& minStr = static_cast<HTMLInputElement*>(node())->getAttribute(minAttr);
const AtomicString& maxStr = static_cast<HTMLInputElement*>(node())->getAttribute(maxAttr);
const AtomicString& precision = static_cast<HTMLInputElement*>(node())->getAttribute(precisionAttr);
double minVal = minStr.isNull() ? 0.0 : minStr.toDouble();
double maxVal = maxStr.isNull() ? 100.0 : maxStr.toDouble();
minVal = min(minVal, maxVal); // Make sure the range is sane.
double oldVal = value.isNull() ? (maxVal + minVal)/2.0 : value.toDouble();
double val = max(minVal, min(oldVal, maxVal)); // Make sure val is within min/max.
// Force integer value if not float.
if (!equalIgnoringCase(precision, "float"))
val = lround(val);
// Calculate the new position based on the value
double factor = (val - minVal) / (maxVal - minVal);
if (style()->appearance() == SliderVerticalAppearance)
factor = 1.0 - factor;
setCurrentPosition((int)(factor * trackSize()));
if (val != oldVal)
static_cast<HTMLInputElement*>(node())->setValueFromRenderer(String::number(val));
return val;
}
int RenderSlider::positionForOffset(const IntPoint& p)
{
if (!m_thumb || !m_thumb->renderer())
return 0;
int position;
if (style()->appearance() == SliderVerticalAppearance)
position = p.y() - m_thumb->renderer()->height() / 2;
else
position = p.x() - m_thumb->renderer()->width() / 2;
return max(0, min(position, trackSize()));
}
void RenderSlider::valueChanged()
{
setValueForPosition(currentPosition());
static_cast<HTMLInputElement*>(node())->onChange();
}
int RenderSlider::currentPosition()
{
if (!m_thumb || !m_thumb->renderer())
return 0;
if (style()->appearance() == SliderVerticalAppearance)
return m_thumb->renderer()->style()->top().value();
return m_thumb->renderer()->style()->left().value();
}
void RenderSlider::setCurrentPosition(int pos)
{
if (!m_thumb || !m_thumb->renderer())
return;
if (style()->appearance() == SliderVerticalAppearance)
m_thumb->renderer()->style()->setTop(Length(pos, Fixed));
else
m_thumb->renderer()->style()->setLeft(Length(pos, Fixed));
m_thumb->renderer()->layer()->updateLayerPosition();
repaint();
m_thumb->renderer()->repaint();
}
int RenderSlider::trackSize()
{
if (!m_thumb || !m_thumb->renderer())
return 0;
if (style()->appearance() == SliderVerticalAppearance)
return contentHeight() - m_thumb->renderer()->height();
return contentWidth() - m_thumb->renderer()->width();
}
void RenderSlider::forwardEvent(Event* evt)
{
m_thumb->defaultEventHandler(evt);
}
bool RenderSlider::inDragMode() const
{
return m_thumb->inDragMode();
}
} // namespace WebCore
|