From 9b7d2cb8d6909601efc8519ee9cde1baa3b6e1b2 Mon Sep 17 00:00:00 2001 From: Timur Doumler Date: Tue, 21 Apr 2015 16:14:56 +0100 Subject: [PATCH] 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); } } }