From 9b7d2cb8d6909601efc8519ee9cde1baa3b6e1b2 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Tue, 21 Apr 2015 16:14:56 +0100 Subject: [PATCH 1/3] fixed drag and drop behaviour of ListBox for the new case selectOnMouseDown == false --- .../juce_gui_basics/widgets/juce_ListBox.cpp | 25 +++++++++++-------- .../juce_gui_basics/widgets/juce_ListBox.h | 11 ++++---- .../widgets/juce_TableListBox.cpp | 13 +++++++--- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.cpp b/modules/juce_gui_basics/widgets/juce_ListBox.cpp index dcdced3c2b..356a6be085 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ListBox.cpp @@ -106,16 +106,21 @@ public: { if (isEnabled() && ! (e.mouseWasClicked() || isDragging)) { - const SparseSet selectedRows (owner.getSelectedRows()); + SparseSet rowsToDrag; + + if (owner.selectOnMouseDown || owner.isRowSelected (row)) + rowsToDrag = owner.getSelectedRows(); + else + rowsToDrag.addRange (Range::withStartAndLength (row, 1)); - if (selectedRows.size() > 0) + if (rowsToDrag.size() > 0) { - const var dragDescription (m->getDragSourceDescription (selectedRows)); + const var dragDescription (m->getDragSourceDescription (rowsToDrag)); if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty()))) { isDragging = true; - owner.startDragAndDrop (e, dragDescription, true); + owner.startDragAndDrop (e, rowsToDrag, dragDescription, true); } } } @@ -881,7 +886,7 @@ void ListBox::repaintRow (const int rowNumber) noexcept repaint (getRowPosition (rowNumber, true)); } -Image ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY) +Image ListBox::createSnapshotOfRows (SparseSet rows, int& imageX, int& imageY) { Rectangle imageArea; const int firstRow = getRowContainingPosition (0, viewport->getY()); @@ -890,7 +895,7 @@ Image ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY) { Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i); - if (rowComp != nullptr && isRowSelected (firstRow + i)) + if (rowComp != nullptr && rows.contains (firstRow + i)) { const Point pos (getLocalPoint (rowComp, Point())); const Rectangle rowRect (pos.getX(), pos.getY(), rowComp->getWidth(), rowComp->getHeight()); @@ -907,7 +912,7 @@ Image ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY) { Component* rowComp = viewport->getComponentForRowIfOnscreen (firstRow + i); - if (rowComp != nullptr && isRowSelected (firstRow + i)) + if (rowComp != nullptr && rows.contains (firstRow + i)) { Graphics g (snapshot); g.setOrigin (getLocalPoint (rowComp, Point()) - imageArea.getPosition()); @@ -924,13 +929,13 @@ Image ListBox::createSnapshotOfSelectedRows (int& imageX, int& imageY) return snapshot; } -void ListBox::startDragAndDrop (const MouseEvent& e, const var& dragDescription, bool allowDraggingToOtherWindows) +void ListBox::startDragAndDrop (const MouseEvent& e, SparseSet rowsToDrag, const var& dragDescription, bool allowDraggingToOtherWindows) { if (DragAndDropContainer* const dragContainer = DragAndDropContainer::findParentDragContainerFor (this)) { int x, y; - Image dragImage (createSnapshotOfSelectedRows (x, y)); - + Image dragImage = createSnapshotOfRows (rowsToDrag, x, y); + MouseEvent e2 (e.getEventRelativeTo (this)); const Point p (x - e2.x, y - e2.y); dragContainer->startDragging (dragDescription, this, dragImage, allowDraggingToOtherWindows, &p); diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.h b/modules/juce_gui_basics/widgets/juce_ListBox.h index 0d4509081b..eaf729fad1 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.h +++ b/modules/juce_gui_basics/widgets/juce_ListBox.h @@ -146,7 +146,7 @@ public: @see DragAndDropContainer::startDragging */ - virtual var getDragSourceDescription (const SparseSet& currentlySelectedRows); + virtual var getDragSourceDescription (const SparseSet& rowsToDescribe); /** You can override this to provide tool tips for specific rows. @see TooltipClient @@ -518,8 +518,8 @@ public: */ void repaintRow (int rowNumber) noexcept; - /** This fairly obscure method creates an image that just shows the currently - selected row components. + /** This fairly obscure method creates an image that shows the row components specified + in rows (for example, these could be the currently selected row components). It's a handy method for doing drag-and-drop, as it can be passed to the DragAndDropContainer for use as the drag image. @@ -530,7 +530,7 @@ public: @see Component::createComponentSnapshot */ - virtual Image createSnapshotOfSelectedRows (int& x, int& y); + virtual Image createSnapshotOfRows (SparseSet rows, int& x, int& y); /** Returns the viewport that this ListBox uses. @@ -561,7 +561,8 @@ public: /** @internal */ void parentHierarchyChanged() override; /** @internal */ - void startDragAndDrop (const MouseEvent&, const var& dragDescription, bool allowDraggingToOtherWindows); + void startDragAndDrop (const MouseEvent&, SparseSet rowsToDrag, + const var& dragDescription, bool allowDraggingToOtherWindows); private: //============================================================================== diff --git a/modules/juce_gui_basics/widgets/juce_TableListBox.cpp b/modules/juce_gui_basics/widgets/juce_TableListBox.cpp index cedec8db29..819b8cd815 100644 --- a/modules/juce_gui_basics/widgets/juce_TableListBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_TableListBox.cpp @@ -146,16 +146,21 @@ public: { if (isEnabled() && owner.getModel() != nullptr && ! (e.mouseWasClicked() || isDragging)) { - const SparseSet selectedRows (owner.getSelectedRows()); + SparseSet rowsToDrag; + + if (owner.selectOnMouseDown || owner.isRowSelected (row)) + rowsToDrag = owner.getSelectedRows(); + else + rowsToDrag.addRange (Range::withStartAndLength (row, 1)); - if (selectedRows.size() > 0) + if (rowsToDrag.size() > 0) { - const var dragDescription (owner.getModel()->getDragSourceDescription (selectedRows)); + const var dragDescription (owner.getModel()->getDragSourceDescription (rowsToDrag)); if (! (dragDescription.isVoid() || (dragDescription.isString() && dragDescription.toString().isEmpty()))) { isDragging = true; - owner.startDragAndDrop (e, dragDescription, true); + owner.startDragAndDrop (e, rowsToDrag, dragDescription, true); } } } From d3d7670f83cccd2509e4159a65d4ff3c988531e0 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Tue, 21 Apr 2015 16:27:10 +0100 Subject: [PATCH 2/3] moved deprecated method ListBox::createSnapshotOfSelectedRows into JUCE_CATCH_DEPRECATED_CODE_MISUSE block. --- modules/juce_gui_basics/widgets/juce_ListBox.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.h b/modules/juce_gui_basics/widgets/juce_ListBox.h index eaf729fad1..6db2c68035 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.h +++ b/modules/juce_gui_basics/widgets/juce_ListBox.h @@ -586,6 +586,9 @@ private: #if JUCE_CATCH_DEPRECATED_CODE_MISUSE // This method's bool parameter has changed: see the new method signature. JUCE_DEPRECATED (void setSelectedRows (const SparseSet&, bool)); + // This method has been replaced by the more flexible method createSnapshotOfRows. + // Please call createSnapshotOfRows (getSelectedRows(), x, y) to get the same behaviour. + JUCE_DEPRECATED (virtual void createSnapshotOfSelectedRows (int&, int&)) {} #endif JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ListBox) From ca6aa75bc8725e8aa6688892e8a2e66cb97d1700 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Wed, 22 Apr 2015 16:01:01 +0100 Subject: [PATCH 3/3] now passing SparseSet as const& instead of by value in ListBox::createSnapshotOfRows/startDragAndDrop. --- modules/juce_gui_basics/widgets/juce_ListBox.cpp | 4 ++-- modules/juce_gui_basics/widgets/juce_ListBox.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.cpp b/modules/juce_gui_basics/widgets/juce_ListBox.cpp index 356a6be085..c90db1876e 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.cpp +++ b/modules/juce_gui_basics/widgets/juce_ListBox.cpp @@ -886,7 +886,7 @@ void ListBox::repaintRow (const int rowNumber) noexcept repaint (getRowPosition (rowNumber, true)); } -Image ListBox::createSnapshotOfRows (SparseSet rows, int& imageX, int& imageY) +Image ListBox::createSnapshotOfRows (const SparseSet& rows, int& imageX, int& imageY) { Rectangle imageArea; const int firstRow = getRowContainingPosition (0, viewport->getY()); @@ -929,7 +929,7 @@ Image ListBox::createSnapshotOfRows (SparseSet rows, int& imageX, int& imag return snapshot; } -void ListBox::startDragAndDrop (const MouseEvent& e, SparseSet rowsToDrag, const var& dragDescription, bool allowDraggingToOtherWindows) +void ListBox::startDragAndDrop (const MouseEvent& e, const SparseSet& rowsToDrag, const var& dragDescription, bool allowDraggingToOtherWindows) { if (DragAndDropContainer* const dragContainer = DragAndDropContainer::findParentDragContainerFor (this)) { diff --git a/modules/juce_gui_basics/widgets/juce_ListBox.h b/modules/juce_gui_basics/widgets/juce_ListBox.h index 6db2c68035..b48061f433 100644 --- a/modules/juce_gui_basics/widgets/juce_ListBox.h +++ b/modules/juce_gui_basics/widgets/juce_ListBox.h @@ -530,7 +530,7 @@ public: @see Component::createComponentSnapshot */ - virtual Image createSnapshotOfRows (SparseSet rows, int& x, int& y); + virtual Image createSnapshotOfRows (const SparseSet& rows, int& x, int& y); /** Returns the viewport that this ListBox uses. @@ -561,7 +561,7 @@ public: /** @internal */ void parentHierarchyChanged() override; /** @internal */ - void startDragAndDrop (const MouseEvent&, SparseSet rowsToDrag, + void startDragAndDrop (const MouseEvent&, const SparseSet& rowsToDrag, const var& dragDescription, bool allowDraggingToOtherWindows); private: