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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
|
/*
* Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "ScrollView.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "FloatRect.h"
#include "FocusController.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "IntRect.h"
#include "NotImplemented.h"
#include "Page.h"
#include "PlatformScrollBar.h"
#include "PlatformMouseEvent.h"
#include "PlatformWheelEvent.h"
#include "RenderTheme.h"
#include "ScrollBar.h"
#include <algorithm>
#include <winsock2.h>
#include <windows.h>
#include <wtf/Assertions.h>
#include <wtf/HashSet.h>
using namespace std;
namespace WebCore {
class ScrollView::ScrollViewPrivate : public ScrollbarClient {
public:
ScrollViewPrivate(ScrollView* view)
: m_view(view)
, m_hasStaticBackground(false)
, m_scrollbarsSuppressed(false)
, m_inUpdateScrollbars(false)
, m_scrollbarsAvoidingResizer(0)
, m_vScrollbarMode(ScrollbarAuto)
, m_hScrollbarMode(ScrollbarAuto)
, m_visible(false)
, m_attachedToWindow(false)
{
}
~ScrollViewPrivate()
{
setHasHorizontalScrollbar(false);
setHasVerticalScrollbar(false);
}
void setHasHorizontalScrollbar(bool hasBar);
void setHasVerticalScrollbar(bool hasBar);
virtual void valueChanged(Scrollbar*);
virtual IntRect windowClipRect() const;
virtual bool isActive() const;
void scrollBackingStore(const IntSize& scrollDelta);
void setAllowsScrolling(bool);
bool allowsScrolling() const;
ScrollView* m_view;
IntSize m_scrollOffset;
IntSize m_contentsSize;
bool m_hasStaticBackground;
bool m_scrollbarsSuppressed;
bool m_inUpdateScrollbars;
int m_scrollbarsAvoidingResizer;
ScrollbarMode m_vScrollbarMode;
ScrollbarMode m_hScrollbarMode;
RefPtr<PlatformScrollbar> m_vBar;
RefPtr<PlatformScrollbar> m_hBar;
HRGN m_dirtyRegion;
HashSet<Widget*> m_children;
bool m_visible;
bool m_attachedToWindow;
};
void ScrollView::ScrollViewPrivate::setHasHorizontalScrollbar(bool hasBar)
{
if (Scrollbar::hasPlatformScrollbars()) {
if (hasBar && !m_hBar) {
m_hBar = PlatformScrollbar::create(this, HorizontalScrollbar, RegularScrollbar);
m_view->addChild(m_hBar.get());
} else if (!hasBar && m_hBar) {
m_view->removeChild(m_hBar.get());
m_hBar = 0;
}
}
}
void ScrollView::ScrollViewPrivate::setHasVerticalScrollbar(bool hasBar)
{
if (Scrollbar::hasPlatformScrollbars()) {
if (hasBar && !m_vBar) {
m_vBar = PlatformScrollbar::create(this, VerticalScrollbar, RegularScrollbar);
m_view->addChild(m_vBar.get());
} else if (!hasBar && m_vBar) {
m_view->removeChild(m_vBar.get());
m_vBar = 0;
}
}
}
void ScrollView::ScrollViewPrivate::valueChanged(Scrollbar* bar)
{
// Figure out if we really moved.
IntSize newOffset = m_scrollOffset;
if (bar) {
if (bar == m_hBar)
newOffset.setWidth(bar->value());
else if (bar == m_vBar)
newOffset.setHeight(bar->value());
}
IntSize scrollDelta = newOffset - m_scrollOffset;
if (scrollDelta == IntSize())
return;
m_scrollOffset = newOffset;
if (m_scrollbarsSuppressed)
return;
static_cast<FrameView*>(m_view)->frame()->sendScrollEvent();
scrollBackingStore(scrollDelta);
}
void ScrollView::ScrollViewPrivate::scrollBackingStore(const IntSize& scrollDelta)
{
// Since scrolling is double buffered, we will be blitting the scroll view's intersection
// with the clip rect every time to keep it smooth.
HWND containingWindowHandle = m_view->containingWindow();
IntRect clipRect = m_view->windowClipRect();
IntRect scrollViewRect = m_view->convertToContainingWindow(IntRect(0, 0, m_view->visibleWidth(), m_view->visibleHeight()));
IntRect updateRect = clipRect;
updateRect.intersect(scrollViewRect);
RECT r = updateRect;
::InvalidateRect(containingWindowHandle, &r, false);
if (!m_hasStaticBackground) // The main frame can just blit the WebView window
// FIXME: Find a way to blit subframes without blitting overlapping content
m_view->scrollBackingStore(-scrollDelta.width(), -scrollDelta.height(), scrollViewRect, clipRect);
else {
// We need to go ahead and repaint the entire backing store. Do it now before moving the
// plugins.
m_view->addToDirtyRegion(updateRect);
m_view->updateBackingStore();
}
// This call will move child HWNDs (plugins) and invalidate them as well.
m_view->geometryChanged();
// Now update the window (which should do nothing but a blit of the backing store's updateRect and so should
// be very fast).
::UpdateWindow(containingWindowHandle);
}
void ScrollView::ScrollViewPrivate::setAllowsScrolling(bool flag)
{
if (flag && m_vScrollbarMode == ScrollbarAlwaysOff)
m_vScrollbarMode = ScrollbarAuto;
else if (!flag)
m_vScrollbarMode = ScrollbarAlwaysOff;
if (flag && m_hScrollbarMode == ScrollbarAlwaysOff)
m_hScrollbarMode = ScrollbarAuto;
else if (!flag)
m_hScrollbarMode = ScrollbarAlwaysOff;
m_view->updateScrollbars(m_scrollOffset);
}
bool ScrollView::ScrollViewPrivate::allowsScrolling() const
{
// Return YES if either horizontal or vertical scrolling is allowed.
return m_hScrollbarMode != ScrollbarAlwaysOff || m_vScrollbarMode != ScrollbarAlwaysOff;
}
IntRect ScrollView::ScrollViewPrivate::windowClipRect() const
{
return static_cast<const FrameView*>(m_view)->windowClipRect(false);
}
bool ScrollView::ScrollViewPrivate::isActive() const
{
Page* page = static_cast<const FrameView*>(m_view)->frame()->page();
return page && page->focusController()->isActive();
}
ScrollView::ScrollView()
: m_data(new ScrollViewPrivate(this))
{
}
ScrollView::~ScrollView()
{
delete m_data;
}
void ScrollView::updateContents(const IntRect& rect, bool now)
{
if (rect.isEmpty())
return;
IntPoint windowPoint = contentsToWindow(rect.location());
IntRect containingWindowRect = rect;
containingWindowRect.setLocation(windowPoint);
RECT containingWindowRectWin = containingWindowRect;
HWND containingWindowHandle = containingWindow();
::InvalidateRect(containingWindowHandle, &containingWindowRectWin, false);
// Cache the dirty spot.
addToDirtyRegion(containingWindowRect);
if (now)
::UpdateWindow(containingWindowHandle);
}
void ScrollView::update()
{
::UpdateWindow(containingWindow());
}
int ScrollView::visibleWidth() const
{
return max(0, width() - (m_data->m_vBar ? m_data->m_vBar->width() : 0));
}
int ScrollView::visibleHeight() const
{
return max(0, height() - (m_data->m_hBar ? m_data->m_hBar->height() : 0));
}
FloatRect ScrollView::visibleContentRect() const
{
return FloatRect(contentsX(), contentsY(), visibleWidth(), visibleHeight());
}
FloatRect ScrollView::visibleContentRectConsideringExternalScrollers() const
{
// external scrollers not supported for now
return visibleContentRect();
}
void ScrollView::setContentsPos(int newX, int newY)
{
int dx = newX - contentsX();
int dy = newY - contentsY();
scrollBy(dx, dy);
}
void ScrollView::resizeContents(int w, int h)
{
IntSize newContentsSize(w, h);
if (m_data->m_contentsSize != newContentsSize) {
m_data->m_contentsSize = newContentsSize;
updateScrollbars(m_data->m_scrollOffset);
}
}
void ScrollView::setFrameGeometry(const IntRect& newGeometry)
{
IntRect oldGeometry = frameGeometry();
Widget::setFrameGeometry(newGeometry);
if (newGeometry == oldGeometry)
return;
if (newGeometry.width() != oldGeometry.width() || newGeometry.height() != oldGeometry.height()) {
updateScrollbars(m_data->m_scrollOffset);
static_cast<FrameView*>(this)->setNeedsLayout();
}
geometryChanged();
}
int ScrollView::contentsX() const
{
return scrollOffset().width();
}
int ScrollView::contentsY() const
{
return scrollOffset().height();
}
int ScrollView::contentsWidth() const
{
return m_data->m_contentsSize.width();
}
int ScrollView::contentsHeight() const
{
return m_data->m_contentsSize.height();
}
IntPoint ScrollView::windowToContents(const IntPoint& windowPoint) const
{
IntPoint viewPoint = convertFromContainingWindow(windowPoint);
return viewPoint + scrollOffset();
}
IntPoint ScrollView::contentsToWindow(const IntPoint& contentsPoint) const
{
IntPoint viewPoint = contentsPoint - scrollOffset();
return convertToContainingWindow(viewPoint);
}
IntPoint ScrollView::convertChildToSelf(const Widget* child, const IntPoint& point) const
{
IntPoint newPoint = point;
if (child != m_data->m_hBar && child != m_data->m_vBar)
newPoint = point - scrollOffset();
return Widget::convertChildToSelf(child, newPoint);
}
IntPoint ScrollView::convertSelfToChild(const Widget* child, const IntPoint& point) const
{
IntPoint newPoint = point;
if (child != m_data->m_hBar && child != m_data->m_vBar)
newPoint = point + scrollOffset();
return Widget::convertSelfToChild(child, newPoint);
}
IntSize ScrollView::scrollOffset() const
{
return m_data->m_scrollOffset;
}
IntSize ScrollView::maximumScroll() const
{
IntSize delta = (m_data->m_contentsSize - IntSize(visibleWidth(), visibleHeight())) - scrollOffset();
delta.clampNegativeToZero();
return delta;
}
void ScrollView::scrollBy(int dx, int dy)
{
IntSize scrollOffset = m_data->m_scrollOffset;
IntSize newScrollOffset = scrollOffset + IntSize(dx, dy).shrunkTo(maximumScroll());
newScrollOffset.clampNegativeToZero();
if (newScrollOffset == scrollOffset)
return;
updateScrollbars(newScrollOffset);
}
void ScrollView::scrollRectIntoViewRecursively(const IntRect& r)
{
IntPoint p(max(0, r.x()), max(0, r.y()));
ScrollView* view = this;
while (view) {
view->setContentsPos(p.x(), p.y());
p.move(view->x() - view->scrollOffset().width(), view->y() - view->scrollOffset().height());
view = static_cast<ScrollView*>(view->parent());
}
}
WebCore::ScrollbarMode ScrollView::hScrollbarMode() const
{
return m_data->m_hScrollbarMode;
}
WebCore::ScrollbarMode ScrollView::vScrollbarMode() const
{
return m_data->m_vScrollbarMode;
}
void ScrollView::suppressScrollbars(bool suppressed, bool repaintOnSuppress)
{
m_data->m_scrollbarsSuppressed = suppressed;
if (repaintOnSuppress && !suppressed) {
if (m_data->m_hBar)
m_data->m_hBar->invalidate();
if (m_data->m_vBar)
m_data->m_vBar->invalidate();
// Invalidate the scroll corner too on unsuppress.
IntRect hCorner;
if (m_data->m_hBar && width() - m_data->m_hBar->width() > 0) {
hCorner = IntRect(m_data->m_hBar->width(),
height() - m_data->m_hBar->height(),
width() - m_data->m_hBar->width(),
m_data->m_hBar->height());
invalidateRect(hCorner);
}
if (m_data->m_vBar && height() - m_data->m_vBar->height() > 0) {
IntRect vCorner(width() - m_data->m_vBar->width(),
m_data->m_vBar->height(),
m_data->m_vBar->width(),
height() - m_data->m_vBar->height());
if (vCorner != hCorner)
invalidateRect(vCorner);
}
}
}
void ScrollView::setHScrollbarMode(ScrollbarMode newMode)
{
if (m_data->m_hScrollbarMode != newMode) {
m_data->m_hScrollbarMode = newMode;
updateScrollbars(m_data->m_scrollOffset);
}
}
void ScrollView::setVScrollbarMode(ScrollbarMode newMode)
{
if (m_data->m_vScrollbarMode != newMode) {
m_data->m_vScrollbarMode = newMode;
updateScrollbars(m_data->m_scrollOffset);
}
}
void ScrollView::setScrollbarsMode(ScrollbarMode newMode)
{
if (m_data->m_hScrollbarMode != newMode ||
m_data->m_vScrollbarMode != newMode) {
m_data->m_hScrollbarMode = m_data->m_vScrollbarMode = newMode;
updateScrollbars(m_data->m_scrollOffset);
}
}
void ScrollView::setStaticBackground(bool flag)
{
m_data->m_hasStaticBackground = flag;
}
void ScrollView::updateScrollbars(const IntSize& desiredOffset)
{
// Don't allow re-entrancy into this function.
if (m_data->m_inUpdateScrollbars)
return;
// FIXME: This code is here so we don't have to fork FrameView.h/.cpp.
// In the end, FrameView should just merge with ScrollView.
if (static_cast<const FrameView*>(this)->frame()->prohibitsScrolling())
return;
m_data->m_inUpdateScrollbars = true;
bool hasVerticalScrollbar = m_data->m_vBar;
bool hasHorizontalScrollbar = m_data->m_hBar;
bool oldHasVertical = hasVerticalScrollbar;
bool oldHasHorizontal = hasHorizontalScrollbar;
ScrollbarMode hScroll = m_data->m_hScrollbarMode;
ScrollbarMode vScroll = m_data->m_vScrollbarMode;
const int cVerticalWidth = PlatformScrollbar::verticalScrollbarWidth();
const int cHorizontalHeight = PlatformScrollbar::horizontalScrollbarHeight();
for (int pass = 0; pass < 2; pass++) {
bool scrollsVertically;
bool scrollsHorizontally;
if (!m_data->m_scrollbarsSuppressed && (hScroll == ScrollbarAuto || vScroll == ScrollbarAuto)) {
// Do a layout if pending before checking if scrollbars are needed.
if (hasVerticalScrollbar != oldHasVertical || hasHorizontalScrollbar != oldHasHorizontal)
static_cast<FrameView*>(this)->layout();
scrollsVertically = (vScroll == ScrollbarAlwaysOn) || (vScroll == ScrollbarAuto && contentsHeight() > height());
if (scrollsVertically)
scrollsHorizontally = (hScroll == ScrollbarAlwaysOn) || (hScroll == ScrollbarAuto && contentsWidth() + cVerticalWidth > width());
else {
scrollsHorizontally = (hScroll == ScrollbarAlwaysOn) || (hScroll == ScrollbarAuto && contentsWidth() > width());
if (scrollsHorizontally)
scrollsVertically = (vScroll == ScrollbarAlwaysOn) || (vScroll == ScrollbarAuto && contentsHeight() + cHorizontalHeight > height());
}
}
else {
scrollsHorizontally = (hScroll == ScrollbarAuto) ? hasHorizontalScrollbar : (hScroll == ScrollbarAlwaysOn);
scrollsVertically = (vScroll == ScrollbarAuto) ? hasVerticalScrollbar : (vScroll == ScrollbarAlwaysOn);
}
if (hasVerticalScrollbar != scrollsVertically) {
m_data->setHasVerticalScrollbar(scrollsVertically);
hasVerticalScrollbar = scrollsVertically;
}
if (hasHorizontalScrollbar != scrollsHorizontally) {
m_data->setHasHorizontalScrollbar(scrollsHorizontally);
hasHorizontalScrollbar = scrollsHorizontally;
}
}
// Set up the range (and page step/line step).
IntSize maxScrollPosition(contentsWidth() - visibleWidth(), contentsHeight() - visibleHeight());
IntSize scroll = desiredOffset.shrunkTo(maxScrollPosition);
scroll.clampNegativeToZero();
if (m_data->m_hBar) {
int clientWidth = visibleWidth();
m_data->m_hBar->setEnabled(contentsWidth() > clientWidth);
int pageStep = (clientWidth - PAGE_KEEP);
if (pageStep < 0) pageStep = clientWidth;
IntRect oldRect(m_data->m_hBar->frameGeometry());
IntRect hBarRect = IntRect(0,
height() - m_data->m_hBar->height(),
width() - (m_data->m_vBar ? m_data->m_vBar->width() : 0),
m_data->m_hBar->height());
m_data->m_hBar->setRect(hBarRect);
if (!m_data->m_scrollbarsSuppressed && oldRect != m_data->m_hBar->frameGeometry())
m_data->m_hBar->invalidate();
if (m_data->m_scrollbarsSuppressed)
m_data->m_hBar->setSuppressInvalidation(true);
m_data->m_hBar->setSteps(LINE_STEP, pageStep);
m_data->m_hBar->setProportion(clientWidth, contentsWidth());
m_data->m_hBar->setValue(scroll.width());
if (m_data->m_scrollbarsSuppressed)
m_data->m_hBar->setSuppressInvalidation(false);
}
if (m_data->m_vBar) {
int clientHeight = visibleHeight();
m_data->m_vBar->setEnabled(contentsHeight() > clientHeight);
int pageStep = (clientHeight - PAGE_KEEP);
if (pageStep < 0) pageStep = clientHeight;
IntRect oldRect(m_data->m_vBar->frameGeometry());
IntRect vBarRect = IntRect(width() - m_data->m_vBar->width(),
0,
m_data->m_vBar->width(),
height() - (m_data->m_hBar ? m_data->m_hBar->height() : 0));
m_data->m_vBar->setRect(vBarRect);
if (!m_data->m_scrollbarsSuppressed && oldRect != m_data->m_vBar->frameGeometry())
m_data->m_vBar->invalidate();
if (m_data->m_scrollbarsSuppressed)
m_data->m_vBar->setSuppressInvalidation(true);
m_data->m_vBar->setSteps(LINE_STEP, pageStep);
m_data->m_vBar->setProportion(clientHeight, contentsHeight());
m_data->m_vBar->setValue(scroll.height());
if (m_data->m_scrollbarsSuppressed)
m_data->m_vBar->setSuppressInvalidation(false);
}
if (oldHasVertical != (m_data->m_vBar != 0) || oldHasHorizontal != (m_data->m_hBar != 0))
geometryChanged();
// See if our offset has changed in a situation where we might not have scrollbars.
// This can happen when editing a body with overflow:hidden and scrolling to reveal selection.
// It can also happen when maximizing a window that has scrollbars (but the new maximized result
// does not).
IntSize scrollDelta = scroll - m_data->m_scrollOffset;
if (scrollDelta != IntSize()) {
m_data->m_scrollOffset = scroll;
m_data->scrollBackingStore(scrollDelta);
}
m_data->m_inUpdateScrollbars = false;
}
PlatformScrollbar* ScrollView::scrollbarUnderMouse(const PlatformMouseEvent& mouseEvent)
{
IntPoint viewPoint = convertFromContainingWindow(mouseEvent.pos());
if (m_data->m_hBar && m_data->m_hBar->frameGeometry().contains(viewPoint))
return m_data->m_hBar.get();
if (m_data->m_vBar && m_data->m_vBar->frameGeometry().contains(viewPoint))
return m_data->m_vBar.get();
return 0;
}
void ScrollView::addChild(Widget* child)
{
child->setParent(this);
child->setContainingWindow(containingWindow());
m_data->m_children.add(child);
}
void ScrollView::removeChild(Widget* child)
{
child->setParent(0);
m_data->m_children.remove(child);
}
void ScrollView::paint(GraphicsContext* context, const IntRect& rect)
{
// FIXME: This code is here so we don't have to fork FrameView.h/.cpp.
// In the end, FrameView should just merge with ScrollView.
ASSERT(isFrameView());
if (context->paintingDisabled() && !context->updatingControlTints())
return;
IntRect documentDirtyRect = rect;
documentDirtyRect.intersect(frameGeometry());
context->save();
context->translate(x(), y());
documentDirtyRect.move(-x(), -y());
context->translate(-contentsX(), -contentsY());
documentDirtyRect.move(contentsX(), contentsY());
context->clip(enclosingIntRect(visibleContentRect()));
static_cast<const FrameView*>(this)->frame()->paint(context, documentDirtyRect);
context->restore();
// Now paint the scrollbars.
if (!m_data->m_scrollbarsSuppressed && (m_data->m_hBar || m_data->m_vBar)) {
context->save();
IntRect scrollViewDirtyRect = rect;
scrollViewDirtyRect.intersect(frameGeometry());
context->translate(x(), y());
scrollViewDirtyRect.move(-x(), -y());
if (m_data->m_hBar)
m_data->m_hBar->paint(context, scrollViewDirtyRect);
if (m_data->m_vBar)
m_data->m_vBar->paint(context, scrollViewDirtyRect);
// Fill the scroll corner with white.
IntRect hCorner;
if (m_data->m_hBar && width() - m_data->m_hBar->width() > 0) {
hCorner = IntRect(m_data->m_hBar->width(),
height() - m_data->m_hBar->height(),
width() - m_data->m_hBar->width(),
m_data->m_hBar->height());
if (hCorner.intersects(scrollViewDirtyRect))
context->fillRect(hCorner, Color::white);
}
if (m_data->m_vBar && height() - m_data->m_vBar->height() > 0) {
IntRect vCorner(width() - m_data->m_vBar->width(),
m_data->m_vBar->height(),
m_data->m_vBar->width(),
height() - m_data->m_vBar->height());
if (vCorner != hCorner && vCorner.intersects(scrollViewDirtyRect))
context->fillRect(vCorner, Color::white);
}
context->restore();
}
}
void ScrollView::themeChanged()
{
PlatformScrollbar::themeChanged();
theme()->themeChanged();
invalidate();
}
void ScrollView::wheelEvent(PlatformWheelEvent& e)
{
if (!m_data->allowsScrolling())
return;
// Determine how much we want to scroll. If we can move at all, we will accept the event.
IntSize maxScrollDelta = maximumScroll();
if ((e.deltaX() < 0 && maxScrollDelta.width() > 0) ||
(e.deltaX() > 0 && scrollOffset().width() > 0) ||
(e.deltaY() < 0 && maxScrollDelta.height() > 0) ||
(e.deltaY() > 0 && scrollOffset().height() > 0)) {
e.accept();
scrollBy(-e.deltaX() * LINE_STEP, -e.deltaY() * LINE_STEP);
}
}
HashSet<Widget*>* ScrollView::children()
{
return &(m_data->m_children);
}
void ScrollView::geometryChanged() const
{
HashSet<Widget*>::const_iterator end = m_data->m_children.end();
for (HashSet<Widget*>::const_iterator current = m_data->m_children.begin(); current != end; ++current)
(*current)->geometryChanged();
}
bool ScrollView::scroll(ScrollDirection direction, ScrollGranularity granularity)
{
if (direction == ScrollUp || direction == ScrollDown) {
if (m_data->m_vBar)
return m_data->m_vBar->scroll(direction, granularity);
} else {
if (m_data->m_hBar)
return m_data->m_hBar->scroll(direction, granularity);
}
return false;
}
IntRect ScrollView::windowResizerRect()
{
ASSERT(isFrameView());
const FrameView* frameView = static_cast<const FrameView*>(this);
Page* page = frameView->frame() ? frameView->frame()->page() : 0;
if (!page)
return IntRect();
return page->chrome()->windowResizerRect();
}
bool ScrollView::resizerOverlapsContent() const
{
return !m_data->m_scrollbarsAvoidingResizer;
}
void ScrollView::adjustOverlappingScrollbarCount(int overlapDelta)
{
int oldCount = m_data->m_scrollbarsAvoidingResizer;
m_data->m_scrollbarsAvoidingResizer += overlapDelta;
if (parent() && parent()->isFrameView())
static_cast<FrameView*>(parent())->adjustOverlappingScrollbarCount(overlapDelta);
else if (!m_data->m_scrollbarsSuppressed) {
// If we went from n to 0 or from 0 to n and we're the outermost view,
// we need to invalidate the windowResizerRect(), since it will now need to paint
// differently.
if (oldCount > 0 && m_data->m_scrollbarsAvoidingResizer == 0 ||
oldCount == 0 && m_data->m_scrollbarsAvoidingResizer > 0)
invalidateRect(windowResizerRect());
}
}
void ScrollView::setParent(ScrollView* parentView)
{
if (!parentView && m_data->m_scrollbarsAvoidingResizer && parent() && parent()->isFrameView())
static_cast<FrameView*>(parent())->adjustOverlappingScrollbarCount(false);
Widget::setParent(parentView);
}
void ScrollView::attachToWindow()
{
if (m_data->m_attachedToWindow)
return;
m_data->m_attachedToWindow = true;
if (m_data->m_visible) {
HashSet<Widget*>::iterator end = m_data->m_children.end();
for (HashSet<Widget*>::iterator it = m_data->m_children.begin(); it != end; ++it)
(*it)->attachToWindow();
}
}
void ScrollView::detachFromWindow()
{
if (!m_data->m_attachedToWindow)
return;
if (m_data->m_visible) {
HashSet<Widget*>::iterator end = m_data->m_children.end();
for (HashSet<Widget*>::iterator it = m_data->m_children.begin(); it != end; ++it)
(*it)->detachFromWindow();
}
m_data->m_attachedToWindow = false;
}
void ScrollView::show()
{
if (!m_data->m_visible) {
m_data->m_visible = true;
if (isAttachedToWindow()) {
HashSet<Widget*>::iterator end = m_data->m_children.end();
for (HashSet<Widget*>::iterator it = m_data->m_children.begin(); it != end; ++it)
(*it)->attachToWindow();
}
}
Widget::show();
}
void ScrollView::hide()
{
if (m_data->m_visible) {
if (isAttachedToWindow()) {
HashSet<Widget*>::iterator end = m_data->m_children.end();
for (HashSet<Widget*>::iterator it = m_data->m_children.begin(); it != end; ++it)
(*it)->detachFromWindow();
}
m_data->m_visible = false;
}
Widget::hide();
}
bool ScrollView::isAttachedToWindow() const
{
return m_data->m_attachedToWindow;
}
void ScrollView::addToDirtyRegion(const IntRect& containingWindowRect)
{
ASSERT(isFrameView());
const FrameView* frameView = static_cast<const FrameView*>(this);
Page* page = frameView->frame() ? frameView->frame()->page() : 0;
if (!page)
return;
page->chrome()->addToDirtyRegion(containingWindowRect);
}
void ScrollView::scrollBackingStore(int dx, int dy, const IntRect& scrollViewRect, const IntRect& clipRect)
{
ASSERT(isFrameView());
const FrameView* frameView = static_cast<const FrameView*>(this);
Page* page = frameView->frame() ? frameView->frame()->page() : 0;
if (!page)
return;
page->chrome()->scrollBackingStore(dx, dy, scrollViewRect, clipRect);
}
void ScrollView::updateBackingStore()
{
ASSERT(isFrameView());
const FrameView* frameView = static_cast<const FrameView*>(this);
Page* page = frameView->frame() ? frameView->frame()->page() : 0;
if (!page)
return;
page->chrome()->updateBackingStore();
}
void ScrollView::setAllowsScrolling(bool flag)
{
m_data->setAllowsScrolling(flag);
}
bool ScrollView::allowsScrolling() const
{
return m_data->allowsScrolling();
}
bool ScrollView::inWindow() const
{
// Needed for back/forward cache.
notImplemented();
return true;
}
IntRect ScrollView::contentsToScreen(const IntRect& rect) const
{
IntRect result(contentsToWindow(rect));
POINT topLeft = {0, 0};
// Find the top left corner of the Widget's containing window in screen coords,
// and adjust the result rect's position by this amount.
::ClientToScreen(containingWindow(), &topLeft);
result.move(topLeft.x, topLeft.y);
return result;
}
IntPoint ScrollView::screenToContents(const IntPoint& point) const
{
POINT result = point;
::ScreenToClient(containingWindow(), &result);
return windowToContents(result);
}
} // namespace WebCore
|