Upstream version 9.38.198.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/ScrollView.h"
35 #include "platform/scroll/ScrollableArea.h"
36 #include "platform/scroll/ScrollbarTheme.h"
37
38 using namespace std;
39
40 #if ((OS(POSIX) && !OS(MACOSX)) || OS(WIN))
41 // The position of the scrollbar thumb affects the appearance of the steppers, so
42 // when the thumb moves, we have to invalidate them for painting.
43 #define THUMB_POSITION_AFFECTS_BUTTONS
44 #endif
45
46 namespace blink {
47
48 PassRefPtr<Scrollbar> Scrollbar::create(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize size)
49 {
50     return adoptRef(new Scrollbar(scrollableArea, orientation, size));
51 }
52
53 Scrollbar::Scrollbar(ScrollableArea* scrollableArea, ScrollbarOrientation orientation, ScrollbarControlSize controlSize, ScrollbarTheme* theme)
54     : m_scrollableArea(scrollableArea)
55     , m_orientation(orientation)
56     , m_controlSize(controlSize)
57     , m_theme(theme)
58     , m_visibleSize(0)
59     , m_totalSize(0)
60     , m_currentPos(0)
61     , m_dragOrigin(0)
62     , m_hoveredPart(NoPart)
63     , m_pressedPart(NoPart)
64     , m_pressedPos(0)
65     , m_scrollPos(0)
66     , m_draggingDocument(false)
67     , m_documentDragPos(0)
68     , m_enabled(true)
69     , m_scrollTimer(this, &Scrollbar::autoscrollTimerFired)
70     , m_overlapsResizer(false)
71     , m_suppressInvalidation(false)
72     , m_isAlphaLocked(false)
73 {
74     if (!m_theme)
75         m_theme = ScrollbarTheme::theme();
76
77     m_theme->registerScrollbar(this);
78
79     // FIXME: This is ugly and would not be necessary if we fix cross-platform code to actually query for
80     // scrollbar thickness and use it when sizing scrollbars (rather than leaving one dimension of the scrollbar
81     // alone when sizing).
82     int thickness = m_theme->scrollbarThickness(controlSize);
83     Widget::setFrameRect(IntRect(0, 0, thickness, thickness));
84
85     m_currentPos = scrollableAreaCurrentPos();
86 }
87
88 Scrollbar::~Scrollbar()
89 {
90     stopTimerIfNeeded();
91
92     m_theme->unregisterScrollbar(this);
93 }
94
95 void Scrollbar::removeFromParent()
96 {
97     if (parent())
98         toScrollView(parent())->removeChild(this);
99 }
100
101 ScrollView* Scrollbar::parentScrollView() const
102 {
103     return toScrollView(parent());
104 }
105
106 ScrollView* Scrollbar::rootScrollView() const
107 {
108     return toScrollView(root());
109 }
110
111 ScrollbarOverlayStyle Scrollbar::scrollbarOverlayStyle() const
112 {
113     return m_scrollableArea ? m_scrollableArea->scrollbarOverlayStyle() : ScrollbarOverlayStyleDefault;
114 }
115
116 void Scrollbar::getTickmarks(Vector<IntRect>& tickmarks) const
117 {
118     if (m_scrollableArea)
119         m_scrollableArea->getTickmarks(tickmarks);
120 }
121
122 bool Scrollbar::isScrollableAreaActive() const
123 {
124     return m_scrollableArea && m_scrollableArea->isActive();
125 }
126
127 bool Scrollbar::isScrollViewScrollbar() const
128 {
129     return parent() && parent()->isFrameView() && toScrollView(parent())->isScrollViewScrollbar(this);
130 }
131
132 bool Scrollbar::isLeftSideVerticalScrollbar() const
133 {
134     if (m_orientation == VerticalScrollbar && m_scrollableArea)
135         return m_scrollableArea->shouldPlaceVerticalScrollbarOnLeft();
136     return false;
137 }
138
139 void Scrollbar::offsetDidChange()
140 {
141     ASSERT(m_scrollableArea);
142
143     float position = scrollableAreaCurrentPos();
144     if (position == m_currentPos)
145         return;
146
147     int oldThumbPosition = theme()->thumbPosition(this);
148     m_currentPos = position;
149     updateThumbPosition();
150     if (m_pressedPart == ThumbPart)
151         setPressedPos(m_pressedPos + theme()->thumbPosition(this) - oldThumbPosition);
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 = min(trackLen - thumbLen - thumbPos, delta);
306     else if (delta < 0)
307         delta = 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::setFrameRect(const IntRect& rect)
482 {
483     // Get our window resizer rect and see if we overlap. Adjust to avoid the overlap
484     // if necessary.
485     IntRect adjustedRect(rect);
486     bool overlapsResizer = false;
487     ScrollView* view = parentScrollView();
488     if (view && !rect.isEmpty() && !view->windowResizerRect().isEmpty()) {
489         IntRect resizerRect = view->convertFromContainingWindow(view->windowResizerRect());
490         if (rect.intersects(resizerRect)) {
491             if (orientation() == HorizontalScrollbar) {
492                 int overlap = rect.maxX() - resizerRect.x();
493                 if (overlap > 0 && resizerRect.maxX() >= rect.maxX()) {
494                     adjustedRect.setWidth(rect.width() - overlap);
495                     overlapsResizer = true;
496                 }
497             } else {
498                 int overlap = rect.maxY() - resizerRect.y();
499                 if (overlap > 0 && resizerRect.maxY() >= rect.maxY()) {
500                     adjustedRect.setHeight(rect.height() - overlap);
501                     overlapsResizer = true;
502                 }
503             }
504         }
505     }
506     if (overlapsResizer != m_overlapsResizer) {
507         m_overlapsResizer = overlapsResizer;
508         if (view)
509             view->adjustScrollbarsAvoidingResizerCount(m_overlapsResizer ? 1 : -1);
510     }
511
512     Widget::setFrameRect(adjustedRect);
513 }
514
515 void Scrollbar::setParent(Widget* parentView)
516 {
517     if (!parentView && m_overlapsResizer && parentScrollView())
518         parentScrollView()->adjustScrollbarsAvoidingResizerCount(-1);
519     Widget::setParent(parentView);
520 }
521
522 void Scrollbar::setEnabled(bool e)
523 {
524     if (m_enabled == e)
525         return;
526     m_enabled = e;
527     theme()->updateEnabledState(this);
528     invalidate();
529 }
530
531 bool Scrollbar::isOverlayScrollbar() const
532 {
533     return m_theme->usesOverlayScrollbars();
534 }
535
536 bool Scrollbar::shouldParticipateInHitTesting()
537 {
538     // Non-overlay scrollbars should always participate in hit testing.
539     if (!isOverlayScrollbar())
540         return true;
541     return m_scrollableArea->scrollAnimator()->shouldScrollbarParticipateInHitTesting(this);
542 }
543
544 bool Scrollbar::isWindowActive() const
545 {
546     return m_scrollableArea && m_scrollableArea->isActive();
547 }
548
549 void Scrollbar::invalidateRect(const IntRect& rect)
550 {
551     if (suppressInvalidation())
552         return;
553
554     if (m_scrollableArea)
555         m_scrollableArea->invalidateScrollbar(this, rect);
556 }
557
558 IntRect Scrollbar::convertToContainingView(const IntRect& localRect) const
559 {
560     if (m_scrollableArea)
561         return m_scrollableArea->convertFromScrollbarToContainingView(this, localRect);
562
563     return Widget::convertToContainingView(localRect);
564 }
565
566 IntRect Scrollbar::convertFromContainingView(const IntRect& parentRect) const
567 {
568     if (m_scrollableArea)
569         return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentRect);
570
571     return Widget::convertFromContainingView(parentRect);
572 }
573
574 IntPoint Scrollbar::convertToContainingView(const IntPoint& localPoint) const
575 {
576     if (m_scrollableArea)
577         return m_scrollableArea->convertFromScrollbarToContainingView(this, localPoint);
578
579     return Widget::convertToContainingView(localPoint);
580 }
581
582 IntPoint Scrollbar::convertFromContainingView(const IntPoint& parentPoint) const
583 {
584     if (m_scrollableArea)
585         return m_scrollableArea->convertFromContainingViewToScrollbar(this, parentPoint);
586
587     return Widget::convertFromContainingView(parentPoint);
588 }
589
590 float Scrollbar::scrollableAreaCurrentPos() const
591 {
592     if (!m_scrollableArea)
593         return 0;
594
595     if (m_orientation == HorizontalScrollbar)
596         return m_scrollableArea->scrollPosition().x() - m_scrollableArea->minimumScrollPosition().x();
597
598     return m_scrollableArea->scrollPosition().y() - m_scrollableArea->minimumScrollPosition().y();
599 }
600
601 } // namespace blink