From fe4a37ef495d67c86224b363756f3dbd299cd28b Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Wed, 3 Feb 2010 23:03:38 +0000 Subject: [PATCH] Added a sort method to the TreeView class. --- juce_amalgamated.h | 26 ++++++++++ src/containers/juce_ValueTree.h | 50 ++++++++++++++++++++ src/gui/components/juce_Component.cpp | 6 +-- src/gui/components/juce_Desktop.cpp | 2 +- src/gui/components/juce_Desktop.h | 4 +- src/gui/graphics/contexts/juce_EdgeTable.cpp | 4 +- 6 files changed, 84 insertions(+), 8 deletions(-) diff --git a/juce_amalgamated.h b/juce_amalgamated.h index 7a03a2d5df..fe3ba7c4e1 100644 --- a/juce_amalgamated.h +++ b/juce_amalgamated.h @@ -6457,6 +6457,17 @@ public: void removeListener (Listener* listener); + template + void sort (ElementComparator& comparator, const bool retainOrderOfEquivalentItems = false) + { + if (object != 0) + { + ComparatorAdapter adapter (comparator); + object->children.sort (adapter, retainOrderOfEquivalentItems); + object->sendChildChangeMessage(); + } + } + juce_UseDebuggingNewOperator private: @@ -6500,6 +6511,21 @@ private: const SharedObject& operator= (const SharedObject&); }; + template + class ComparatorAdapter + { + public: + ComparatorAdapter (ElementComparator& comparator_) throw() : comparator (comparator_) {} + + int compareElements (SharedObject* const first, SharedObject* const second) + { + return comparator.compareElements (ValueTree (first), ValueTree (second)); + } + + private: + ElementComparator& comparator; + }; + friend class SharedObject; typedef ReferenceCountedObjectPtr SharedObjectPtr; diff --git a/src/containers/juce_ValueTree.h b/src/containers/juce_ValueTree.h index 1744b2e1e5..4ad181b1e9 100644 --- a/src/containers/juce_ValueTree.h +++ b/src/containers/juce_ValueTree.h @@ -341,6 +341,41 @@ public: /** Removes a listener that was previously added with addListener(). */ void removeListener (Listener* listener); + //============================================================================== + /** This method uses a comparator object to sort the tree's children into order. + + The object provided must have a method of the form: + @code + int compareElements (const ValueTree& first, const ValueTree& second); + @endcode + + ..and this method must return: + - a value of < 0 if the first comes before the second + - a value of 0 if the two objects are equivalent + - a value of > 0 if the second comes before the first + + To improve performance, the compareElements() method can be declared as static or const. + + @param comparator the comparator to use for comparing elements. + @param retainOrderOfEquivalentItems if this is true, then items + which the comparator says are equivalent will be + kept in the order in which they currently appear + in the array. This is slower to perform, but may + be important in some cases. If it's false, a faster + algorithm is used, but equivalent elements may be + rearranged. + */ + template + void sort (ElementComparator& comparator, const bool retainOrderOfEquivalentItems = false) + { + if (object != 0) + { + ComparatorAdapter adapter (comparator); + object->children.sort (adapter, retainOrderOfEquivalentItems); + object->sendChildChangeMessage(); + } + } + //============================================================================== juce_UseDebuggingNewOperator @@ -385,6 +420,21 @@ private: const SharedObject& operator= (const SharedObject&); }; + template + class ComparatorAdapter + { + public: + ComparatorAdapter (ElementComparator& comparator_) throw() : comparator (comparator_) {} + + int compareElements (SharedObject* const first, SharedObject* const second) + { + return comparator.compareElements (ValueTree (first), ValueTree (second)); + } + + private: + ElementComparator& comparator; + }; + friend class SharedObject; typedef ReferenceCountedObjectPtr SharedObjectPtr; diff --git a/src/gui/components/juce_Component.cpp b/src/gui/components/juce_Component.cpp index 2d7afefab0..f66682c126 100644 --- a/src/gui/components/juce_Component.cpp +++ b/src/gui/components/juce_Component.cpp @@ -2735,11 +2735,11 @@ void Component::internalMouseDrag (int x, int y, const int64 time) x += unboundedMouseOffsetX; y += unboundedMouseOffsetY; - + int gx = x, gy = y; relativePositionToGlobal (gx, gy); desktop.registerMouseDrag (gx, gy); - + const ComponentDeletionWatcher deletionChecker (this); int mdx, mdy; @@ -2747,7 +2747,7 @@ void Component::internalMouseDrag (int x, int y, const int64 time) globalPositionToRelative (mdx, mdy); const Time lastMouseDownTime (desktop.getLastMouseDownTime()); - + const MouseEvent me (x, y, ModifierKeys::getCurrentModifiers(), this, diff --git a/src/gui/components/juce_Desktop.cpp b/src/gui/components/juce_Desktop.cpp index fbb74253c9..761b95e712 100644 --- a/src/gui/components/juce_Desktop.cpp +++ b/src/gui/components/juce_Desktop.cpp @@ -244,7 +244,7 @@ void Desktop::registerMouseDown (int x, int y, int64 time, Component* component) void Desktop::registerMouseDrag (int x, int y) throw() { - mouseMovedSignificantlySincePressed + mouseMovedSignificantlySincePressed = mouseMovedSignificantlySincePressed || abs (mouseDowns[0].x - x) >= 4 || abs (mouseDowns[0].y - y) >= 4; diff --git a/src/gui/components/juce_Desktop.h b/src/gui/components/juce_Desktop.h index a31d46f699..bb1570972e 100644 --- a/src/gui/components/juce_Desktop.h +++ b/src/gui/components/juce_Desktop.h @@ -248,7 +248,7 @@ private: ~Desktop() throw(); Array monitorCoordsClipped, monitorCoordsUnclipped; - + int lastFakeMouseMoveX, lastFakeMouseMoveY, mouseClickCounter; bool mouseMovedSignificantlySincePressed; @@ -258,7 +258,7 @@ private: int64 time; Component* component; }; - + RecentMouseDown mouseDowns[4]; void incrementMouseClickCounter() throw(); diff --git a/src/gui/graphics/contexts/juce_EdgeTable.cpp b/src/gui/graphics/contexts/juce_EdgeTable.cpp index f6ddbe06fe..04c89076f7 100644 --- a/src/gui/graphics/contexts/juce_EdgeTable.cpp +++ b/src/gui/graphics/contexts/juce_EdgeTable.cpp @@ -640,8 +640,8 @@ void EdgeTable::excludeRectangle (const Rectangle& r) throw() //XXX optimise here by shortening the table if it fills top or bottom - const int rectLine[] = { 4, std::numeric_limits::min(), 255, - clipped.getX() << 8, 0, + const int rectLine[] = { 4, std::numeric_limits::min(), 255, + clipped.getX() << 8, 0, clipped.getRight() << 8, 255, std::numeric_limits::max(), 0 };