Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / scroll / Scrollbar.cpp
1 /*
2  * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "platform/scroll/Scrollbar.h"
28
29 #include <algorithm>
30 #include "platform/graphics/GraphicsContext.h"
31 #include "platform/PlatformGestureEvent.h"
32 #include "platform/PlatformMouseEvent.h"
33 #include "platform/scroll/ScrollAnimator.h"
34 #include "platform/scroll/ScrollableArea.h"
35 #include "platform/scroll/ScrollbarTheme.h"
36
37 #if ((OS(POSIX) && !OS(MACOSX)) || OS(WIN))
38 // The position of the scrollbar thumb affects the appearance of the steppers, so
39 // when the thumb moves, we have to invalidate them for painting.
40 #define THUMB_POSITION_AFFECTS_BUTTONS
41 #endif
42
43 namespace blink {
44
45 PassRefPtrWillBeRawPtr<Scrollbar> Scrollbar::create(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
46 {
47     return adoptRefWillBeNoop(new Scrollbar(scrollableArea, orientation, size));
48 }
49
50 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, ScrollbarTheme* theme)
51     : m_scrollableArea(scrollableArea)
52     , m_orientation(orientation)
53     , m_controlSize(controlSize)
54     , m_theme(theme)
55     , m_visibleSize(0)
56     , m_totalSize(0)
57     , m_currentPos(0)
58     , m_dragOrigin(0)
59     , m_hoveredPart(NoPart)
60     , m_pressedPart(NoPart)
61     , m_pressedPos(0)
62     , m_scrollPos(0)
63     , m_draggingDocument(false)
64     , m_documentDragPos(0)
65     , m_enabled(true)
66     , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
67     , m_overlapsResizer(false)
68     , m_suppressInvalidation(false)
69     , m_isAlphaLocked(false)
70 {
71     if (!m_theme)
72         m_theme = ScrollbarTheme::theme();
73
74     m_theme->registerScrollbar(this);
75
76     // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
77     // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
78     // alone when sizing).
79     int thickness = m_theme->scrollbarThickness(controlSize);
80     Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
81
82     m_currentPos = scrollableAreaCurrentPos();
83
84 #if ENABLE(OILPAN)
85     if (m_scrollableArea)
86         m_animator = m_scrollableArea->scrollAnimator();
87 #endif
88 }
89
90 Scrollbar::~Scrollbar()
91 {
92     stopTimerIfNeeded();
93
94     m_theme->unregisterScrollbar(this);
95
96 #if ENABLE(OILPAN)
97     if (!m_animator)
98         return;
99
100     ASSERT(m_scrollableArea);
101     if (m_orientation == VerticalScrollbar)
102         m_animator->willRemoveVerticalScrollbar(this);
103     else
104         m_animator->willRemoveHorizontalScrollbar(this);
105 #endif
106 }
107
108 ScrollbarOverlayStyle Scrollbar::scrollbarOverlayStyle() const
109 {
110     return m_scrollableArea ? m_scrollableArea->scrollbarOverlayStyle() : ScrollbarOverlayStyleDefault;
111 }
112
113 void Scrollbar::getTickmarks(Vector<IntRect>& tickmarks) const
114 {
115     if (m_scrollableArea)
116         m_scrollableArea->getTickmarks(tickmarks);
117 }
118
119 bool Scrollbar::isScrollableAreaActive() const
120 {
121     return m_scrollableArea && m_scrollableArea->isActive();
122 }
123
124 bool Scrollbar::isLeftSideVerticalScrollbar() const
125 {
126     if (m_orientation == VerticalScrollbar && m_scrollableArea)
127         return m_scrollableArea->shouldPlaceVerticalScrollbarOnLeft();
128     return false;
129 }
130
131 void Scrollbar::offsetDidChange()
132 {
133     ASSERT(m_scrollableArea);
134
135     float position = scrollableAreaCurrentPos();
136     if (position == m_currentPos)
137         return;
138
139     int oldThumbPosition = theme()->thumbPosition(this);
140     m_currentPos = position;
141     updateThumbPosition();
142     if (m_pressedPart == ThumbPart)
143         setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
144 }
145
146 void Scrollbar::disconnectFromScrollableArea()
147 {
148     m_scrollableArea = nullptr;
149 #if ENABLE(OILPAN)
150     m_animator = nullptr;
151 #endif
152 }
153
154 void Scrollbar::setProportion(int visibleSize, int totalSize)
155 {
156     if (visibleSize == m_visibleSize && totalSize == m_totalSize)
157         return;
158
159     m_visibleSize = visibleSize;
160     m_totalSize = totalSize;
161
162     updateThumbProportion();
163 }
164
165 void Scrollbar::updateThumb()
166 {
167 #ifdef THUMB_POSITION_AFFECTS_BUTTONS
168     invalidate();
169 #else
170     theme()->invalidateParts(this, ForwardTrackPart | BackTrackPart | ThumbPart);
171 #endif
172 }
173
174 void Scrollbar::updateThumbPosition()
175 {
176     updateThumb();
177 }
178
179 void Scrollbar::updateThumbProportion()
180 {
181     updateThumb();
182 }
183
184 void Scrollbar::paint(GraphicsContext* context, const IntRect& damageRect)
185 {
186     if (!frameRect().intersects(damageRect))
187         return;
188
189     if (!theme()->paint(this, context, damageRect))
190         Widget::paint(context, damageRect);
191 }
192
193 void Scrollbar::autoscrollTimerFired(Timer<Scrollbar>*)
194 {
195     autoscrollPressedPart(theme()->autoscrollTimerDelay());
196 }
197
198 static bool thumbUnderMouse(Scrollbar* scrollbar)
199 {
200     int thumbPos = scrollbar->theme()->trackPosition(scrollbar) + scrollbar->theme()->thumbPosition(scrollbar);
201     int thumbLength = scrollbar->theme()->thumbLength(scrollbar);
202     return scrollbar->pressedPos() >= thumbPos && scrollbar->pressedPos() < thumbPos + thumbLength;
203 }
204
205 void Scrollbar::autoscrollPressedPart(double delay)
206 {
207     // Don't do anything for the thumb or if nothing was pressed.
208     if (m_pressedPart == ThumbPart || m_pressedPart == NoPart)
209         return;
210
211     // Handle the track.
212     if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
213         theme()->invalidatePart(this, m_pressedPart);
214         setHoveredPart(ThumbPart);
215         return;
216     }
217
218     // Handle the arrows and track.
219     if (m_scrollableArea && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity()))
220         startTimerIfNeeded(delay);
221 }
222
223 void Scrollbar::startTimerIfNeeded(double delay)
224 {
225     // Don't do anything for the thumb.
226     if (m_pressedPart == ThumbPart)
227         return;
228
229     // Handle the track.  We halt track scrolling once the thumb is level
230     // with us.
231     if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && thumbUnderMouse(this)) {
232         theme()->invalidatePart(this, m_pressedPart);
233         setHoveredPart(ThumbPart);
234         return;
235     }
236
237     // We can't scroll if we've hit the beginning or end.
238     ScrollDirection dir = pressedPartScrollDirection();
239     if (dir == ScrollUp || dir == ScrollLeft) {
240         if (m_currentPos == 0)
241             return;
242     } else {
243         if (m_currentPos == maximum())
244             return;
245     }
246
247     m_scrollTimer.startOneShot(delay, FROM_HERE);
248 }
249
250 void Scrollbar::stopTimerIfNeeded()
251 {
252     if (m_scrollTimer.isActive())
253         m_scrollTimer.stop();
254 }
255
256 ScrollDirection Scrollbar::pressedPartScrollDirection()
257 {
258     if (m_orientation == HorizontalScrollbar) {
259         if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
260             return ScrollLeft;
261         return ScrollRight;
262     } else {
263         if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart || m_pressedPart == BackTrackPart)
264             return ScrollUp;
265         return ScrollDown;
266     }
267 }
268
269 ScrollGranularity Scrollbar::pressedPartScrollGranularity()
270 {
271     if (m_pressedPart == BackButtonStartPart || m_pressedPart == BackButtonEndPart ||  m_pressedPart == ForwardButtonStartPart || m_pressedPart == ForwardButtonEndPart)
272         return ScrollByLine;
273     return ScrollByPage;
274 }
275
276 void Scrollbar::moveThumb(int pos, bool draggingDocument)
277 {
278     if (!m_scrollableArea)
279         return;
280
281     int delta = pos - m_pressedPos;
282
283     if (draggingDocument) {
284         if (m_draggingDocument)
285             delta = pos - m_documentDragPos;
286         m_draggingDocument = true;
287         FloatPoint currentPosition = m_scrollableArea->scrollAnimator()->currentPosition();
288         float destinationPosition = (m_orientation == HorizontalScrollbar ? currentPosition.x() : currentPosition.y()) + delta;
289         destinationPosition = m_scrollableArea->clampScrollPosition(m_orientation, destinationPosition);
290         m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, destinationPosition);
291         m_documentDragPos = pos;
292         return;
293     }
294
295     if (m_draggingDocument) {
296         delta += m_pressedPos - m_documentDragPos;
297         m_draggingDocument = false;
298     }
299
300     // Drag the thumb.
301     int thumbPos = theme()->thumbPosition(this);
302     int thumbLen = theme()->thumbLength(this);
303     int trackLen = theme()->trackLength(this);
304     if (delta > 0)
305         delta = std::min(trackLen - thumbLen - thumbPos, delta);
306     else if (delta < 0)
307         delta = std::max(-thumbPos, delta);
308
309     float minPos = m_scrollableArea->minimumScrollPosition(m_orientation);
310     float maxPos = m_scrollableArea->maximumScrollPosition(m_orientation);
311     if (delta) {
312         float newPosition = static_cast<float>(thumbPos + delta) * (maxPos - minPos) / (trackLen - thumbLen) + minPos;
313         m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, newPosition);
314     }
315 }
316
317 void Scrollbar::setHoveredPart(ScrollbarPart part)
318 {
319     if (part == m_hoveredPart)
320         return;
321
322     if ((m_hoveredPart == NoPart || part == NoPart) && theme()->invalidateOnMouseEnterExit())
323         invalidate();  // Just invalidate the whole scrollbar, since the buttons at either end change anyway.
324     else if (m_pressedPart == NoPart) {  // When there's a pressed part, we don't draw a hovered state, so there's no reason to invalidate.
325         theme()->invalidatePart(this, part);
326         theme()->invalidatePart(this, m_hoveredPart);
327     }
328     m_hoveredPart = part;
329 }
330
331 void Scrollbar::setPressedPart(ScrollbarPart part)
332 {
333     if (m_pressedPart != NoPart)
334         theme()->invalidatePart(this, m_pressedPart);
335     m_pressedPart = part;
336     if (m_pressedPart != NoPart)
337         theme()->invalidatePart(this, m_pressedPart);
338     else if (m_hoveredPart != NoPart)  // When we no longer have a pressed part, we can start drawing a hovered state on the hovered part.
339         theme()->invalidatePart(this, m_hoveredPart);
340 }
341
342 bool Scrollbar::gestureEvent(const PlatformGestureEvent& evt)
343 {
344     switch (evt.type()) {
345     case PlatformEvent::GestureTapDown:
346         setPressedPart(theme()->hitTest(this, evt.position()));
347         m_pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
348         return true;
349     case PlatformEvent::GestureTapDownCancel:
350     case PlatformEvent::GestureScrollBegin:
351         if (m_pressedPart != ThumbPart)
352             return false;
353         m_scrollPos = m_pressedPos;
354         return true;
355     case PlatformEvent::GestureScrollUpdate:
356     case PlatformEvent::GestureScrollUpdateWithoutPropagation:
357         if (m_pressedPart != ThumbPart)
358             return false;
359         m_scrollPos += orientation() == HorizontalScrollbar ? evt.deltaX() : evt.deltaY();
360         moveThumb(m_scrollPos, false);
361         return true;
362     case PlatformEvent::GestureScrollEnd:
363     case PlatformEvent::GestureLongPress:
364     case PlatformEvent::GestureFlingStart:
365         m_scrollPos = 0;
366         m_pressedPos = 0;
367         setPressedPart(NoPart);
368         return false;
369     case PlatformEvent::GestureTap: {
370         if (m_pressedPart != ThumbPart && m_pressedPart != NoPart && m_scrollableArea
371             && m_scrollableArea->scroll(pressedPartScrollDirection(), pressedPartScrollGranularity())) {
372             return true;
373         }
374         m_scrollPos = 0;
375         m_pressedPos = 0;
376         setPressedPart(NoPart);
377         return false;
378     }
379     default:
380         // By default, we assume that gestures don't deselect the scrollbar.
381         return true;
382     }
383 }
384
385 void Scrollbar::mouseMoved(const PlatformMouseEvent& evt)
386 {
387     if (m_pressedPart == ThumbPart) {
388         if (theme()->shouldSnapBackToDragOrigin(this, evt)) {
389             if (m_scrollableArea)
390                 m_scrollableArea->scrollToOffsetWithoutAnimation(m_orientation, m_dragOrigin + m_scrollableArea->minimumScrollPosition(m_orientation));
391         } else {
392             moveThumb(m_orientation == HorizontalScrollbar ?
393                       convertFromContainingWindow(evt.position()).x() :
394                       convertFromContainingWindow(evt.position()).y(), theme()->shouldDragDocumentInsteadOfThumb(this, evt));
395         }
396         return;
397     }
398
399     if (m_pressedPart != NoPart)
400         m_pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
401
402     ScrollbarPart part = theme()->hitTest(this, evt.position());
403     if (part != m_hoveredPart) {
404         if (m_pressedPart != NoPart) {
405             if (part == m_pressedPart) {
406                 // The mouse is moving back over the pressed part.  We
407                 // need to start up the timer action again.
408                 startTimerIfNeeded(theme()->autoscrollTimerDelay());
409                 theme()->invalidatePart(this, m_pressedPart);
410             } else if (m_hoveredPart == m_pressedPart) {
411                 // The mouse is leaving the pressed part.  Kill our timer
412                 // if needed.
413                 stopTimerIfNeeded();
414                 theme()->invalidatePart(this, m_pressedPart);
415             }
416         }
417
418         setHoveredPart(part);
419     }
420
421     return;
422 }
423
424 void Scrollbar::mouseEntered()
425 {
426     if (m_scrollableArea)
427         m_scrollableArea->mouseEnteredScrollbar(this);
428 }
429
430 void Scrollbar::mouseExited()
431 {
432     if (m_scrollableArea)
433         m_scrollableArea->mouseExitedScrollbar(this);
434     setHoveredPart(NoPart);
435 }
436
437 void Scrollbar::mouseUp(const PlatformMouseEvent& mouseEvent)
438 {
439     setPressedPart(NoPart);
440     m_pressedPos = 0;
441     m_draggingDocument = false;
442     stopTimerIfNeeded();
443
444     if (m_scrollableArea) {
445         // m_hoveredPart won't be updated until the next mouseMoved or mouseDown, so we have to hit test
446         // to really know if the mouse has exited the scrollbar on a mouseUp.
447         ScrollbarPart part = theme()->hitTest(this, mouseEvent.position());
448         if (part == NoPart)
449             m_scrollableArea->mouseExitedScrollbar(this);
450     }
451 }
452
453 void Scrollbar::mouseDown(const PlatformMouseEvent& evt)
454 {
455     // Early exit for right click
456     if (evt.button() == RightButton)
457         return;
458
459     setPressedPart(theme()->hitTest(this, evt.position()));
460     int pressedPos = orientation() == HorizontalScrollbar ? convertFromContainingWindow(evt.position()).x() : convertFromContainingWindow(evt.position()).y();
461
462     if ((m_pressedPart == BackTrackPart || m_pressedPart == ForwardTrackPart) && theme()->shouldCenterOnThumb(this, evt)) {
463         setHoveredPart(ThumbPart);
464         setPressedPart(ThumbPart);
465         m_dragOrigin = m_currentPos;
466         int thumbLen = theme()->thumbLength(this);
467         int desiredPos = pressedPos;
468         // Set the pressed position to the middle of the thumb so that when we do the move, the delta
469         // will be from the current pixel position of the thumb to the new desired position for the thumb.
470         m_pressedPos = theme()->trackPosition(this) + theme()->thumbPosition(this) + thumbLen / 2;
471         moveThumb(desiredPos);
472         return;
473     } else if (m_pressedPart == ThumbPart)
474         m_dragOrigin = m_currentPos;
475
476     m_pressedPos = pressedPos;
477
478     autoscrollPressedPart(theme()->initialAutoscrollTimerDelay());
479 }
480
481 void Scrollbar::setEnabled(bool e)
482 {
483     if (m_enabled == e)
484         return;
485     m_enabled = e;
486     theme()->updateEnabledState(this);
487     invalidate();
488 }
489
490 bool Scrollbar::isOverlayScrollbar() const
491 {
492     return m_theme->usesOverlayScrollbars();
493 }
494
495 bool Scrollbar::shouldParticipateInHitTesting()
496 {
497     // Non-overlay scrollbars should always participate in hit testing.
498     if (!isOverlayScrollbar())
499         return true;
500     return m_scrollableArea->scrollAnimator()->shouldScrollbarParticipateInHitTesting(this);
501 }
502
503 bool Scrollbar::isWindowActive() const
504 {
505     return m_scrollableArea && m_scrollableArea->isActive();
506 }
507
508 void Scrollbar::invalidateRect(const IntRect& rect)
509 {
510     if (suppressInvalidation())
511         return;
512
513     if (m_scrollableArea)
514         m_scrollableArea->invalidateScrollbar(this, rect);
515 }
516
517 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
518 {
519     if (m_scrollableArea)
520         return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
521
522     return Widget::convertToContainingView(localRect);
523 }
524
525 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
526 {
527     if (m_scrollableArea)
528         return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
529
530     return Widget::convertFromContainingView(parentRect);
531 }
532
533 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
534 {
535     if (m_scrollableArea)
536         return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
537
538     return Widget::convertToContainingView(localPoint);
539 }
540
541 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
542 {
543     if (m_scrollableArea)
544         return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
545
546     return Widget::convertFromContainingView(parentPoint);
547 }
548
549 float Scrollbar::scrollableAreaCurrentPos() const
550 {
551     if (!m_scrollableArea)
552         return 0;
553
554     if (m_orientation == HorizontalScrollbar)
555         return m_scrollableArea->scrollPosition().x() - m_scrollableArea->minimumScrollPosition().x();
556
557     return m_scrollableArea->scrollPosition().y() - m_scrollableArea->minimumScrollPosition().y();
558 }
559
560 } // namespace blink