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
|
/*
==============================================================================
This file is part of the JUCE framework.
Copyright (c) Raw Material Software Limited
JUCE is an open source framework subject to commercial or open source
licensing.
By downloading, installing, or using the JUCE framework, or combining the
JUCE framework with any other source code, object code, content or any other
copyrightable work, you agree to the terms of the JUCE End User Licence
Agreement, and all incorporated terms including the JUCE Privacy Policy and
the JUCE Website Terms of Service, as applicable, which will bind you. If you
do not agree to the terms of these agreements, we will not license the JUCE
framework to you, and you must discontinue the installation or download
process and cease use of the JUCE framework.
JUCE End User Licence Agreement: https://juce.com/legal/juce-8-licence/
JUCE Privacy Policy: https://juce.com/juce-privacy-policy
JUCE Website Terms of Service: https://juce.com/juce-website-terms-of-service/
Or:
You may also use this code under the terms of the AGPLv3:
https://www.gnu.org/licenses/agpl-3.0.en.html
THE JUCE FRAMEWORK IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL
WARRANTIES, WHETHER EXPRESSED OR IMPLIED, INCLUDING WARRANTY OF
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
==============================================================================
*/
namespace juce
{
using ViewportDragPosition = AnimatedPosition<AnimatedPositionBehaviours::ContinuousWithMomentum>;
struct Viewport::DragToScrollListener final : private MouseListener,
private ViewportDragPosition::Listener
{
DragToScrollListener (Viewport& v) : viewport (v)
{
viewport.contentHolder.addMouseListener (this, true);
offsetX.addListener (this);
offsetY.addListener (this);
offsetX.behaviour.setMinimumVelocity (60);
offsetY.behaviour.setMinimumVelocity (60);
}
~DragToScrollListener() override
{
viewport.contentHolder.removeMouseListener (this);
Desktop::getInstance().removeGlobalMouseListener (this);
}
void stopOngoingAnimation()
{
offsetX.setPosition (offsetX.getPosition());
offsetY.setPosition (offsetY.getPosition());
}
void positionChanged (ViewportDragPosition&, double) override
{
viewport.setViewPosition (originalViewPos - Point<int> ((int) offsetX.getPosition(),
(int) offsetY.getPosition()));
}
void mouseDown (const MouseEvent& e) override
{
if (! isGlobalMouseListener && detail::ViewportHelpers::wouldScrollOnEvent (&viewport, e.source))
{
offsetX.setPosition (offsetX.getPosition());
offsetY.setPosition (offsetY.getPosition());
// switch to a global mouse listener so we still receive mouseUp events
// if the original event component is deleted
viewport.contentHolder.removeMouseListener (this);
Desktop::getInstance().addGlobalMouseListener (this);
isGlobalMouseListener = true;
scrollSource = e.source;
}
}
void mouseDrag (const MouseEvent& e) override
{
if (e.source == scrollSource
&& ! doesMouseEventComponentBlockViewportDrag (e.eventComponent))
{
auto totalOffset = e.getEventRelativeTo (&viewport).getOffsetFromDragStart().toFloat();
if (! isDragging && totalOffset.getDistanceFromOrigin() > 8.0f && detail::ViewportHelpers::wouldScrollOnEvent (&viewport, e.source))
{
isDragging = true;
originalViewPos = viewport.getViewPosition();
offsetX.setPosition (0.0);
offsetX.beginDrag();
offsetY.setPosition (0.0);
offsetY.beginDrag();
}
if (isDragging)
{
offsetX.drag (totalOffset.x);
offsetY.drag (totalOffset.y);
}
}
}
void mouseUp (const MouseEvent& e) override
{
if (isGlobalMouseListener && e.source == scrollSource)
endDragAndClearGlobalMouseListener();
}
void endDragAndClearGlobalMouseListener()
{
if (std::exchange (isDragging, false) == true)
{
offsetX.endDrag();
offsetY.endDrag();
}
viewport.contentHolder.addMouseListener (this, true);
Desktop::getInstance().removeGlobalMouseListener (this);
isGlobalMouseListener = false;
}
bool doesMouseEventComponentBlockViewportDrag (const Component* eventComp)
{
for (auto c = eventComp; c != nullptr && c != &viewport; c = c->getParentComponent())
if (c->getViewportIgnoreDragFlag())
return true;
return false;
}
Viewport& viewport;
ViewportDragPosition offsetX, offsetY;
Point<int> originalViewPos;
MouseInputSource scrollSource = Desktop::getInstance().getMainMouseSource();
bool isDragging = false;
bool isGlobalMouseListener = false;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DragToScrollListener)
};
//==============================================================================
Viewport::Viewport (const String& name)
: Component (name),
dragToScrollListener (std::make_unique<DragToScrollListener> (*this))
{
// content holder is used to clip the contents so they don't overlap the scrollbars
addAndMakeVisible (contentHolder);
contentHolder.setInterceptsMouseClicks (false, true);
scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
setInterceptsMouseClicks (false, true);
setWantsKeyboardFocus (true);
recreateScrollbars();
}
Viewport::~Viewport()
{
deleteOrRemoveContentComp();
}
//==============================================================================
void Viewport::visibleAreaChanged (const Rectangle<int>&) {}
void Viewport::viewedComponentChanged (Component*) {}
//==============================================================================
void Viewport::deleteOrRemoveContentComp()
{
if (contentComp != nullptr)
{
contentComp->removeComponentListener (this);
if (deleteContent)
{
// This sets the content comp to a null pointer before deleting the old one, in case
// anything tries to use the old one while it's in mid-deletion..
std::unique_ptr<Component> oldCompDeleter (contentComp.get());
contentComp = nullptr;
}
else
{
contentHolder.removeChildComponent (contentComp);
contentComp = nullptr;
}
}
}
void Viewport::setViewedComponent (Component* const newViewedComponent, const bool deleteComponentWhenNoLongerNeeded)
{
if (contentComp.get() != newViewedComponent)
{
deleteOrRemoveContentComp();
contentComp = newViewedComponent;
deleteContent = deleteComponentWhenNoLongerNeeded;
if (contentComp != nullptr)
{
contentHolder.addAndMakeVisible (contentComp);
setViewPosition (Point<int>());
contentComp->addComponentListener (this);
}
viewedComponentChanged (contentComp);
updateVisibleArea();
}
}
void Viewport::recreateScrollbars()
{
verticalScrollBar.reset();
horizontalScrollBar.reset();
verticalScrollBar .reset (createScrollBarComponent (true));
horizontalScrollBar.reset (createScrollBarComponent (false));
addChildComponent (verticalScrollBar.get());
addChildComponent (horizontalScrollBar.get());
getVerticalScrollBar().addListener (this);
getHorizontalScrollBar().addListener (this);
getVerticalScrollBar().addMouseListener (this, true);
getHorizontalScrollBar().addMouseListener (this, true);
resized();
}
int Viewport::getMaximumVisibleWidth() const { return contentHolder.getWidth(); }
int Viewport::getMaximumVisibleHeight() const { return contentHolder.getHeight(); }
bool Viewport::canScrollVertically() const noexcept { return contentComp->getY() < 0 || contentComp->getBottom() > getHeight(); }
bool Viewport::canScrollHorizontally() const noexcept { return contentComp->getX() < 0 || contentComp->getRight() > getWidth(); }
Point<int> Viewport::viewportPosToCompPos (Point<int> pos) const
{
jassert (contentComp != nullptr);
const auto contentBounds = getContentBounds();
const Point p (jmax (jmin (0, contentHolder.getWidth() - contentBounds.getWidth()), jmin (0, -(pos.x))),
jmax (jmin (0, contentHolder.getHeight() - contentBounds.getHeight()), jmin (0, -(pos.y))));
return p.transformedBy (contentComp->getTransform().inverted());
}
Rectangle<int> Viewport::getContentBounds() const
{
if (auto* cc = contentComp.get())
return contentHolder.getLocalArea (cc, cc->getLocalBounds());
return {};
}
void Viewport::setViewPosition (const int xPixelsOffset, const int yPixelsOffset)
{
setViewPosition ({ xPixelsOffset, yPixelsOffset });
}
void Viewport::setViewPosition (Point<int> newPosition)
{
if (contentComp != nullptr)
contentComp->setTopLeftPosition (viewportPosToCompPos (newPosition));
}
void Viewport::setViewPositionProportionately (const double x, const double y)
{
if (contentComp != nullptr)
setViewPosition (jmax (0, roundToInt (x * (contentComp->getWidth() - getWidth()))),
jmax (0, roundToInt (y * (contentComp->getHeight() - getHeight()))));
}
bool Viewport::autoScroll (const int mouseX, const int mouseY, const int activeBorderThickness, const int maximumSpeed)
{
if (contentComp != nullptr)
{
int dx = 0, dy = 0;
if (getHorizontalScrollBar().isVisible() || canScrollHorizontally())
{
if (mouseX < activeBorderThickness)
dx = activeBorderThickness - mouseX;
else if (mouseX >= contentHolder.getWidth() - activeBorderThickness)
dx = (contentHolder.getWidth() - activeBorderThickness) - mouseX;
if (dx < 0)
dx = jmax (dx, -maximumSpeed, contentHolder.getWidth() - contentComp->getRight());
else
dx = jmin (dx, maximumSpeed, -contentComp->getX());
}
if (getVerticalScrollBar().isVisible() || canScrollVertically())
{
if (mouseY < activeBorderThickness)
dy = activeBorderThickness - mouseY;
else if (mouseY >= contentHolder.getHeight() - activeBorderThickness)
dy = (contentHolder.getHeight() - activeBorderThickness) - mouseY;
if (dy < 0)
dy = jmax (dy, -maximumSpeed, contentHolder.getHeight() - contentComp->getBottom());
else
dy = jmin (dy, maximumSpeed, -contentComp->getY());
}
if (dx != 0 || dy != 0)
{
contentComp->setTopLeftPosition (contentComp->getX() + dx,
contentComp->getY() + dy);
return true;
}
}
return false;
}
void Viewport::componentMovedOrResized (Component&, bool, bool)
{
updateVisibleArea();
}
//==============================================================================
void Viewport::setScrollOnDragMode (const ScrollOnDragMode mode)
{
scrollOnDragMode = mode;
}
bool Viewport::isCurrentlyScrollingOnDrag() const noexcept
{
return dragToScrollListener->isDragging;
}
//==============================================================================
void Viewport::lookAndFeelChanged()
{
if (! customScrollBarThickness)
{
scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth();
resized();
}
}
void Viewport::resized()
{
updateVisibleArea();
}
//==============================================================================
void Viewport::updateVisibleArea()
{
auto scrollbarWidth = getScrollBarThickness();
const bool canShowAnyBars = getWidth() > scrollbarWidth && getHeight() > scrollbarWidth;
const bool canShowHBar = showHScrollbar && canShowAnyBars;
const bool canShowVBar = showVScrollbar && canShowAnyBars;
bool hBarVisible = false, vBarVisible = false;
Rectangle<int> contentArea;
for (int i = 3; --i >= 0;)
{
hBarVisible = canShowHBar && ! getHorizontalScrollBar().autoHides();
vBarVisible = canShowVBar && ! getVerticalScrollBar().autoHides();
contentArea = getLocalBounds();
if (contentComp != nullptr && ! contentArea.contains (contentComp->getBounds()))
{
hBarVisible = canShowHBar && (hBarVisible || contentComp->getX() < 0 || contentComp->getRight() > contentArea.getWidth());
vBarVisible = canShowVBar && (vBarVisible || contentComp->getY() < 0 || contentComp->getBottom() > contentArea.getHeight());
if (vBarVisible)
contentArea.setWidth (getWidth() - scrollbarWidth);
if (hBarVisible)
contentArea.setHeight (getHeight() - scrollbarWidth);
if (! contentArea.contains (contentComp->getBounds()))
{
hBarVisible = canShowHBar && (hBarVisible || contentComp->getRight() > contentArea.getWidth());
vBarVisible = canShowVBar && (vBarVisible || contentComp->getBottom() > contentArea.getHeight());
}
}
if (vBarVisible) contentArea.setWidth (getWidth() - scrollbarWidth);
if (hBarVisible) contentArea.setHeight (getHeight() - scrollbarWidth);
if (! vScrollbarRight && vBarVisible)
contentArea.setX (scrollbarWidth);
if (! hScrollbarBottom && hBarVisible)
contentArea.setY (scrollbarWidth);
if (contentComp == nullptr)
{
contentHolder.setBounds (contentArea);
break;
}
auto oldContentBounds = contentComp->getBounds();
contentHolder.setBounds (contentArea);
// If the content has changed its size, that might affect our scrollbars, so go round again and re-calculate..
if (oldContentBounds == contentComp->getBounds())
break;
}
const auto contentBounds = getContentBounds();
auto visibleOrigin = -contentBounds.getPosition();
auto& hbar = getHorizontalScrollBar();
auto& vbar = getVerticalScrollBar();
hbar.setBounds (contentArea.getX(), hScrollbarBottom ? contentArea.getHeight() : 0, contentArea.getWidth(), scrollbarWidth);
hbar.setRangeLimits (0.0, contentBounds.getWidth());
hbar.setCurrentRange (visibleOrigin.x, contentArea.getWidth());
hbar.setSingleStepSize (singleStepX);
if (canShowHBar && ! hBarVisible)
visibleOrigin.setX (0);
vbar.setBounds (vScrollbarRight ? contentArea.getWidth() : 0, contentArea.getY(), scrollbarWidth, contentArea.getHeight());
vbar.setRangeLimits (0.0, contentBounds.getHeight());
vbar.setCurrentRange (visibleOrigin.y, contentArea.getHeight());
vbar.setSingleStepSize (singleStepY);
if (canShowVBar && ! vBarVisible)
visibleOrigin.setY (0);
// Force the visibility *after* setting the ranges to avoid flicker caused by edge conditions in the numbers.
hbar.setVisible (hBarVisible);
vbar.setVisible (vBarVisible);
if (contentComp != nullptr)
{
auto newContentCompPos = viewportPosToCompPos (visibleOrigin);
if (contentComp->getBounds().getPosition() != newContentCompPos)
{
contentComp->setTopLeftPosition (newContentCompPos); // (this will re-entrantly call updateVisibleArea again)
return;
}
}
const Rectangle<int> visibleArea (visibleOrigin.x, visibleOrigin.y,
jmin (contentBounds.getWidth() - visibleOrigin.x, contentArea.getWidth()),
jmin (contentBounds.getHeight() - visibleOrigin.y, contentArea.getHeight()));
if (lastVisibleArea != visibleArea)
{
lastVisibleArea = visibleArea;
visibleAreaChanged (visibleArea);
}
hbar.handleUpdateNowIfNeeded();
vbar.handleUpdateNowIfNeeded();
}
//==============================================================================
void Viewport::setSingleStepSizes (const int stepX, const int stepY)
{
if (singleStepX != stepX || singleStepY != stepY)
{
singleStepX = stepX;
singleStepY = stepY;
updateVisibleArea();
}
}
void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded,
const bool showHorizontalScrollbarIfNeeded,
const bool allowVerticalScrollingWithoutScrollbar,
const bool allowHorizontalScrollingWithoutScrollbar)
{
allowScrollingWithoutScrollbarV = allowVerticalScrollingWithoutScrollbar;
allowScrollingWithoutScrollbarH = allowHorizontalScrollingWithoutScrollbar;
if (showVScrollbar != showVerticalScrollbarIfNeeded
|| showHScrollbar != showHorizontalScrollbarIfNeeded)
{
showVScrollbar = showVerticalScrollbarIfNeeded;
showHScrollbar = showHorizontalScrollbarIfNeeded;
updateVisibleArea();
}
}
void Viewport::setScrollBarThickness (const int thickness)
{
int newThickness;
// To stay compatible with the previous code: use the
// default thickness if thickness parameter is zero
// or negative
if (thickness <= 0)
{
customScrollBarThickness = false;
newThickness = getLookAndFeel().getDefaultScrollbarWidth();
}
else
{
customScrollBarThickness = true;
newThickness = thickness;
}
if (scrollBarThickness != newThickness)
{
scrollBarThickness = newThickness;
updateVisibleArea();
}
}
int Viewport::getScrollBarThickness() const
{
return scrollBarThickness;
}
void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart)
{
const auto contentOrigin = -getContentBounds().getPosition();
const auto newRangeStartInt = roundToInt (newRangeStart);
for (const auto& [member, bar] : { std::tuple (&Point<int>::x, horizontalScrollBar.get()),
std::tuple (&Point<int>::y, verticalScrollBar.get()) })
{
if (scrollBarThatHasMoved != bar)
continue;
if (contentOrigin.*member == newRangeStartInt)
return;
auto pt = getViewPosition();
pt.*member = newRangeStartInt;
setViewPosition (pt);
return;
}
}
void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
{
if (e.eventComponent == this)
if (! useMouseWheelMoveIfNeeded (e, wheel))
Component::mouseWheelMove (e, wheel);
}
void Viewport::mouseDown (const MouseEvent& e)
{
if (e.eventComponent == horizontalScrollBar.get() || e.eventComponent == verticalScrollBar.get())
dragToScrollListener->stopOngoingAnimation();
}
static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept
{
if (approximatelyEqual (distance, 0.0f))
return 0;
distance *= 14.0f * (float) singleStepSize;
return roundToInt (distance < 0 ? jmin (distance, -1.0f)
: jmax (distance, 1.0f));
}
bool Viewport::useMouseWheelMoveIfNeeded (const MouseEvent& e, const MouseWheelDetails& wheel)
{
if (! (e.mods.isAltDown() || e.mods.isCtrlDown() || e.mods.isCommandDown()))
{
const bool canScrollVert = (allowScrollingWithoutScrollbarV || getVerticalScrollBar().isVisible());
const bool canScrollHorz = (allowScrollingWithoutScrollbarH || getHorizontalScrollBar().isVisible());
if (canScrollHorz || canScrollVert)
{
auto deltaX = rescaleMouseWheelDistance (wheel.deltaX, singleStepX);
auto deltaY = rescaleMouseWheelDistance (wheel.deltaY, singleStepY);
auto pos = getViewPosition();
if (deltaX != 0 && deltaY != 0 && canScrollHorz && canScrollVert)
{
pos.x -= deltaX;
pos.y -= deltaY;
}
else if (canScrollHorz && (deltaX != 0 || e.mods.isShiftDown() || ! canScrollVert))
{
pos.x -= deltaX != 0 ? deltaX : deltaY;
}
else if (canScrollVert && deltaY != 0)
{
pos.y -= deltaY;
}
if (pos != getViewPosition())
{
setViewPosition (pos);
return true;
}
}
}
return false;
}
static bool isUpDownKeyPress (const KeyPress& key)
{
return key == KeyPress::upKey
|| key == KeyPress::downKey
|| key == KeyPress::pageUpKey
|| key == KeyPress::pageDownKey
|| key == KeyPress::homeKey
|| key == KeyPress::endKey;
}
static bool isLeftRightKeyPress (const KeyPress& key)
{
return key == KeyPress::leftKey
|| key == KeyPress::rightKey;
}
bool Viewport::keyPressed (const KeyPress& key)
{
const bool isUpDownKey = isUpDownKeyPress (key);
if (getVerticalScrollBar().isVisible() && isUpDownKey)
return getVerticalScrollBar().keyPressed (key);
const bool isLeftRightKey = isLeftRightKeyPress (key);
if (getHorizontalScrollBar().isVisible() && (isUpDownKey || isLeftRightKey))
return getHorizontalScrollBar().keyPressed (key);
return false;
}
bool Viewport::respondsToKey (const KeyPress& key)
{
return isUpDownKeyPress (key) || isLeftRightKeyPress (key);
}
ScrollBar* Viewport::createScrollBarComponent (bool isVertical)
{
return new ScrollBar (isVertical);
}
void Viewport::setScrollBarPosition (bool verticalScrollbarOnRight,
bool horizontalScrollbarAtBottom)
{
vScrollbarRight = verticalScrollbarOnRight;
hScrollbarBottom = horizontalScrollbarAtBottom;
resized();
}
} // namespace juce
|