From a4f5faec2dccec35ca4dec205057241505176a66 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 24 Mar 2015 16:00:58 +0000 Subject: [PATCH] Fix for a crash when deleting FileBrowserComponent under certain circumstances. --- .../juce_gui_basics/layout/juce_ScrollBar.cpp | 10 +++--- .../juce_gui_basics/layout/juce_ScrollBar.h | 1 + .../juce_gui_basics/layout/juce_Viewport.cpp | 34 ++++++++++++++++--- .../juce_gui_basics/layout/juce_Viewport.h | 3 ++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/modules/juce_gui_basics/layout/juce_ScrollBar.cpp b/modules/juce_gui_basics/layout/juce_ScrollBar.cpp index 6184306ae6..83524c90b7 100644 --- a/modules/juce_gui_basics/layout/juce_ScrollBar.cpp +++ b/modules/juce_gui_basics/layout/juce_ScrollBar.cpp @@ -60,6 +60,7 @@ ScrollBar::ScrollBar (const bool vertical_) thumbAreaSize (0), thumbStart (0), thumbSize (0), + minimumScrollBarThumbSize (0), initialDelayInMillisecs (100), repeatDelayInMillisecs (50), minimumDelayInMillisecs (10), @@ -67,6 +68,8 @@ ScrollBar::ScrollBar (const bool vertical_) isDraggingThumb (false), autohides (true) { + minimumScrollBarThumbSize = getLookAndFeel().getMinimumScrollbarThumbSize (*this); + setRepaintsOnMouseActivity (true); setFocusContainer (true); } @@ -190,10 +193,8 @@ void ScrollBar::updateThumbPosition() int newThumbSize = roundToInt (totalRange.getLength() > 0 ? (visibleRange.getLength() * thumbAreaSize) / totalRange.getLength() : thumbAreaSize); - LookAndFeel& lf = getLookAndFeel(); - - if (newThumbSize < lf.getMinimumScrollbarThumbSize (*this)) - newThumbSize = jmin (lf.getMinimumScrollbarThumbSize (*this), thumbAreaSize - 1); + if (newThumbSize < minimumScrollBarThumbSize) + newThumbSize = jmin (minimumScrollBarThumbSize, thumbAreaSize - 1); if (newThumbSize > thumbAreaSize) newThumbSize = thumbAreaSize; @@ -280,6 +281,7 @@ void ScrollBar::resized() const int length = vertical ? getHeight() : getWidth(); LookAndFeel& lf = getLookAndFeel(); + minimumScrollBarThumbSize = lf.getMinimumScrollbarThumbSize (*this); const bool buttonsVisible = lf.areScrollbarButtonsVisible(); int buttonSize = 0; diff --git a/modules/juce_gui_basics/layout/juce_ScrollBar.h b/modules/juce_gui_basics/layout/juce_ScrollBar.h index 2731af030d..0bb1ddf790 100644 --- a/modules/juce_gui_basics/layout/juce_ScrollBar.h +++ b/modules/juce_gui_basics/layout/juce_ScrollBar.h @@ -382,6 +382,7 @@ private: Range totalRange, visibleRange; double singleStepSize, dragStartRange; int thumbAreaStart, thumbAreaSize, thumbStart, thumbSize; + int minimumScrollBarThumbSize; int dragStartMousePos, lastMousePos; int initialDelayInMillisecs, repeatDelayInMillisecs, minimumDelayInMillisecs; bool vertical, isDraggingThumb, autohides; diff --git a/modules/juce_gui_basics/layout/juce_Viewport.cpp b/modules/juce_gui_basics/layout/juce_Viewport.cpp index 80266cea6f..78d94a76e9 100644 --- a/modules/juce_gui_basics/layout/juce_Viewport.cpp +++ b/modules/juce_gui_basics/layout/juce_Viewport.cpp @@ -24,6 +24,7 @@ Viewport::Viewport (const String& name) : Component (name), + customScrollBarThickness(false), scrollBarThickness (0), singleStepX (16), singleStepY (16), @@ -39,6 +40,8 @@ Viewport::Viewport (const String& name) addAndMakeVisible (contentHolder); contentHolder.setInterceptsMouseClicks (false, true); + scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth(); + addChildComponent (verticalScrollBar); addChildComponent (horizontalScrollBar); @@ -174,6 +177,14 @@ void Viewport::componentMovedOrResized (Component&, bool, bool) updateVisibleArea(); } +void Viewport::lookAndFeelChanged() +{ + if (customScrollBarThickness == false) + { + scrollBarThickness = getLookAndFeel().getDefaultScrollbarWidth(); + } +} + void Viewport::resized() { updateVisibleArea(); @@ -314,17 +325,32 @@ void Viewport::setScrollBarsShown (const bool showVerticalScrollbarIfNeeded, void Viewport::setScrollBarThickness (const int thickness) { - if (scrollBarThickness != thickness) + int newThickness; + + // To stay compatible with the previous code: use the + // default thickness if thickness parameter is zero + // or negative + if (thickness <= 0) { - scrollBarThickness = thickness; + customScrollBarThickness = false; + newThickness = getLookAndFeel().getDefaultScrollbarWidth(); + } + else + { + customScrollBarThickness = true; + newThickness = thickness; + } + + if (scrollBarThickness != newThickness) + { + scrollBarThickness = newThickness; updateVisibleArea(); } } int Viewport::getScrollBarThickness() const { - return scrollBarThickness > 0 ? scrollBarThickness - : getLookAndFeel().getDefaultScrollbarWidth(); + return scrollBarThickness; } void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRangeStart) diff --git a/modules/juce_gui_basics/layout/juce_Viewport.h b/modules/juce_gui_basics/layout/juce_Viewport.h index c427515298..78aa4fa01f 100644 --- a/modules/juce_gui_basics/layout/juce_Viewport.h +++ b/modules/juce_gui_basics/layout/juce_Viewport.h @@ -253,6 +253,8 @@ public: /** @internal */ void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override; /** @internal */ + void lookAndFeelChanged() override; + /** @internal */ bool useMouseWheelMoveIfNeeded (const MouseEvent&, const MouseWheelDetails&); /** @internal */ static bool respondsToKey (const KeyPress&); @@ -261,6 +263,7 @@ private: //============================================================================== WeakReference contentComp; Rectangle lastVisibleArea; + bool customScrollBarThickness; int scrollBarThickness; int singleStepX, singleStepY; bool showHScrollbar, showVScrollbar, deleteContent;