summaryrefslogtreecommitdiff
path: root/src/Fl_Tree_Item_Array.cxx
diff options
context:
space:
mode:
authorGreg Ercolano <erco@seriss.com>2014-09-04 18:56:52 +0000
committerGreg Ercolano <erco@seriss.com>2014-09-04 18:56:52 +0000
commit107fc50e48c96d3eca7980cdb227d8d22aad30a8 (patch)
treed9588a5246a55f627d5e68b912e5a9312011080a /src/Fl_Tree_Item_Array.cxx
parent0ed1e8e0f0f29492a53f92bd222fb99e8d6030ae (diff)
Solves STR #3127.
Added to assist cand for his patch to solve RFE STR #2828 option (I). o Add move() methods to Fl_Tree_Item. o Add deparent()/reparent() methods o Supporting methods added to Fl_Tree_Item_Array, and enhancement to update_prev_next() to allow -1 option to create an orphan item Added to Fl_Tree_Item: * Fl_Tree_Item* deparent(int pos) * int reparent(Fl_Tree_Item *newchild, int pos) * int move(int to, int from) * int move(Fl_Tree_Item *item, int op, int pos) * int move_above(Fl_Tree_Item *item) * int move_below(Fl_Tree_Item *item) * int move_into(Fl_Tree_Item *item, int pos) Added to Fl_Tree_Item_Array: * int move(int to, int from) * int deparent(int pos) * int reparent(Fl_Tree_Item *item, Fl_Tree_Item* newparent, int pos) git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10271 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/Fl_Tree_Item_Array.cxx')
-rw-r--r--src/Fl_Tree_Item_Array.cxx72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Fl_Tree_Item_Array.cxx b/src/Fl_Tree_Item_Array.cxx
index d83b0be12..2610bb2a6 100644
--- a/src/Fl_Tree_Item_Array.cxx
+++ b/src/Fl_Tree_Item_Array.cxx
@@ -233,6 +233,78 @@ void Fl_Tree_Item_Array::swap(int ax, int bx) {
}
#endif /* FLTK_ABI_VERSION */
+/// Move item at 'from' to new position 'to' in the array.
+///
+/// \returns 0 on success, -1 on range error (e.g. if \p 'to' or \p 'from' out of range)
+///
+int Fl_Tree_Item_Array::move(int to, int from) {
+ if ( from == to ) return 0; // nop
+ if ( to<0 || to>=_total || from<0 || from>=_total ) return -1;
+ Fl_Tree_Item *item = _items[from];
+ Fl_Tree_Item *prev = item->prev_sibling();
+ Fl_Tree_Item *next = item->next_sibling();
+ // Remove item..
+ if ( from < to )
+ for ( int t=from; t<to && t<_total; t++ )
+ _items[t] = _items[t+1];
+ else
+ for ( int t=from; t>to; t-- )
+ _items[t] = _items[t-1];
+ // Move to new position
+ _items[to] = item;
+ // Adjust for new siblings
+ _items[to]->update_prev_next(to);
+ _items[from]->update_prev_next(from);
+ // Adjust old siblings
+ if ( prev ) prev->update_prev_next(from-1);
+ if ( next ) next->update_prev_next(from);
+ return 0;
+}
+
+/// Deparent item at \p 'pos' from our list of children.
+/// Similar to a remove() without the destruction of the item.
+/// This creates an orphaned item (still allocated, has no parent)
+/// which soon after is typically reparented elsewhere.
+///
+/// \returns 0 on success, -1 on error (e.g. if \p 'pos' out of range)
+///
+int Fl_Tree_Item_Array::deparent(int pos) {
+ if ( pos>=_total || pos<0 ) return -1;
+ // Save item being deparented, and its two nearest siblings
+ Fl_Tree_Item *item = _items[pos];
+ Fl_Tree_Item *prev = item->prev_sibling();
+ Fl_Tree_Item *next = item->next_sibling();
+ // Remove from parent's list of children
+ _total -= 1;
+ for ( int t=pos; t<_total; t++ )
+ _items[t] = _items[t+1]; // delete, no destroy
+ // Now an orphan: remove association with old parent and siblings
+ item->update_prev_next(-1); // become an orphan
+ // Adjust bereaved siblings
+ if ( prev ) prev->update_prev_next(pos-1);
+ if ( next ) next->update_prev_next(pos);
+ return 0;
+}
+
+/// Reparent specified item as a child of ourself.
+/// Typically 'newchild' was recently orphaned with deparent().
+///
+/// \returns 0 on success, -1 on error (e.g. if \p 'pos' out of range)
+///
+int Fl_Tree_Item_Array::reparent(Fl_Tree_Item *item, Fl_Tree_Item* newparent, int pos) {
+ if ( pos<0 || pos>_total ) return -1;
+ // Add item to new parent
+ enlarge(1);
+ _total += 1;
+ for ( int t=_total-1; t>pos; --t ) // shuffle array to make room for new entry
+ _items[t] = _items[t-1];
+ _items[pos] = item; // insert new entry
+ // Attach to new parent and siblings
+ _items[pos]->parent(newparent); // reparent (update_prev_next() needs this)
+ _items[pos]->update_prev_next(pos); // find new siblings
+ return 0;
+}
+
//
// End of "$Id$".
//