1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-18 00:54:19 +00:00

Added a sort method to the TreeView class.

This commit is contained in:
Julian Storer 2010-02-03 23:03:38 +00:00
parent 22ecd45505
commit fe4a37ef49
6 changed files with 84 additions and 8 deletions

View file

@ -6457,6 +6457,17 @@ public:
void removeListener (Listener* listener);
template <typename ElementComparator>
void sort (ElementComparator& comparator, const bool retainOrderOfEquivalentItems = false)
{
if (object != 0)
{
ComparatorAdapter <ElementComparator> adapter (comparator);
object->children.sort (adapter, retainOrderOfEquivalentItems);
object->sendChildChangeMessage();
}
}
juce_UseDebuggingNewOperator
private:
@ -6500,6 +6511,21 @@ private:
const SharedObject& operator= (const SharedObject&);
};
template <typename ElementComparator>
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 <SharedObject> SharedObjectPtr;

View file

@ -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 <typename ElementComparator>
void sort (ElementComparator& comparator, const bool retainOrderOfEquivalentItems = false)
{
if (object != 0)
{
ComparatorAdapter <ElementComparator> adapter (comparator);
object->children.sort (adapter, retainOrderOfEquivalentItems);
object->sendChildChangeMessage();
}
}
//==============================================================================
juce_UseDebuggingNewOperator
@ -385,6 +420,21 @@ private:
const SharedObject& operator= (const SharedObject&);
};
template <typename ElementComparator>
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 <SharedObject> SharedObjectPtr;

View file

@ -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,

View file

@ -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;

View file

@ -248,7 +248,7 @@ private:
~Desktop() throw();
Array <Rectangle> monitorCoordsClipped, monitorCoordsUnclipped;
int lastFakeMouseMoveX, lastFakeMouseMoveY, mouseClickCounter;
bool mouseMovedSignificantlySincePressed;
@ -258,7 +258,7 @@ private:
int64 time;
Component* component;
};
RecentMouseDown mouseDowns[4];
void incrementMouseClickCounter() throw();

View file

@ -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<int>::min(), 255,
clipped.getX() << 8, 0,
const int rectLine[] = { 4, std::numeric_limits<int>::min(), 255,
clipped.getX() << 8, 0,
clipped.getRight() << 8, 255,
std::numeric_limits<int>::max(), 0 };