diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.cpp b/modules/juce_gui_basics/widgets/juce_ListBox.cpp index ed12f980ae..c12d3b2a10 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ListBox.cpp @@ -367,6 +367,7 @@ ListBox::ListBox (const String& name, ListBoxModel* const m) outlineThickness (0), lastRowSelected (-1), multipleSelection (false), + alwaysFlipSelection (false), hasDoneInitialUpdate (false) { addAndMakeVisible (viewport = new ListViewport (*this)); @@ -391,11 +392,16 @@ void ListBox::setModel (ListBoxModel* const newModel) } } -void ListBox::setMultipleSelectionEnabled (bool b) +void ListBox::setMultipleSelectionEnabled (bool b) noexcept { multipleSelection = b; } +void ListBox::setClickingTogglesRowSelection (bool b) noexcept +{ + alwaysFlipSelection = b; +} + void ListBox::setMouseMoveSelectsRows (bool b) { if (b) @@ -470,9 +476,7 @@ void ListBox::updateContent() } //============================================================================== -void ListBox::selectRow (const int row, - bool dontScroll, - bool deselectOthersFirst) +void ListBox::selectRow (int row, bool dontScroll, bool deselectOthersFirst) { selectRowInternal (row, dontScroll, deselectOthersFirst, false); } @@ -589,7 +593,7 @@ void ListBox::selectRowsBasedOnModifierKeys (const int row, ModifierKeys mods, const bool isMouseUpEvent) { - if (multipleSelection && mods.isCommandDown()) + if (multipleSelection && (mods.isCommandDown() || alwaysFlipSelection)) { flipRowSelection (row); } diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.h b/modules/juce_gui_basics/widgets/juce_ListBox.h index 71679e9654..fadf24b71c 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.h +++ b/modules/juce_gui_basics/widgets/juce_ListBox.h @@ -219,7 +219,16 @@ public: clicked and to get it to do the appropriate selection based on whether the ctrl/shift keys are held down. */ - void setMultipleSelectionEnabled (bool shouldBeEnabled); + void setMultipleSelectionEnabled (bool shouldBeEnabled) noexcept; + + /** If enabled, this makes the listbox flip the selection status of + each row that the user clicks, without affecting other selected rows. + + (This only has an effect if multiple selection is also enabled). + If not enabled, you can still get the same row-flipping behaviour by holding + down CMD or CTRL when clicking. + */ + void setClickingTogglesRowSelection (bool flipRowSelection) noexcept; /** Makes the list react to mouse moves by selecting the row that the mouse if over. @@ -560,7 +569,7 @@ private: int totalItems, rowHeight, minimumRowWidth; int outlineThickness; int lastRowSelected; - bool multipleSelection, hasDoneInitialUpdate; + bool multipleSelection, alwaysFlipSelection, hasDoneInitialUpdate; SparseSet selected; void selectRowInternal (int rowNumber, bool dontScrollToShowThisRow,