o Added new method Fl_Tree::get_selected_items()
	o Modified Fl_Tree_Item_Array to usable in a general way (i.e. beyond Fl_Tree's internal use)



git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@10016 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
This commit is contained in:
Greg Ercolano 2013-11-06 20:44:47 +00:00
parent 52d395ad81
commit 33ab9cfed1
5 changed files with 102 additions and 14 deletions

View File

@ -373,6 +373,9 @@ public:
Fl_Tree_Item *last_visible();
Fl_Tree_Item *first_selected_item();
Fl_Tree_Item *next_selected_item(Fl_Tree_Item *item=0);
#if FLTK_ABI_VERSION >= 10303
int get_selected_items(Fl_Tree_Item_Array &items);
#endif
//////////////////////////
// Item open/close methods

View File

@ -49,6 +49,12 @@ class FL_EXPORT Fl_Tree_Item_Array {
int _total; // #items in array
int _size; // #items *allocated* for array
int _chunksize; // #items to enlarge mem allocation
#if FLTK_ABI_VERSION >= 10303
enum {
MANAGE_ITEM = 1, ///> manage the Fl_Tree_Item's internals (internal use only)
};
char _flags; // flags to control behavior
#endif
void enlarge(int count);
public:
Fl_Tree_Item_Array(int new_chunksize = 10); // CTOR
@ -83,6 +89,17 @@ public:
void insert(int pos, Fl_Tree_Item *new_item);
void remove(int index);
int remove(Fl_Tree_Item *item);
#if FLTK_ABI_VERSION >= 10303
/// Option to control if Fl_Tree_Item_Array's destructor will also destroy the Fl_Tree_Item's.
/// If set: items and item array is destroyed.
/// If clear: only the item array is destroyed, not items themselves.
void manage_item_destroy(int val) {
if ( val ) _flags |= MANAGE_ITEM; else _flags &= ~MANAGE_ITEM;
}
int manage_item_destroy() const {
return _flags & MANAGE_ITEM ? 1 : 0;
}
#endif
};
#endif /*_FL_TREE_ITEM_ARRAY_H*/

View File

@ -924,6 +924,31 @@ Fl_Tree_Item *Fl_Tree::next_selected_item(Fl_Tree_Item *item) {
return(0);
}
#if FLTK_ABI_VERSION >= 10303 /* reason for this: Fl_Tree_Item_Array::manage_item_destroy() */
/// Returns the currently selected items as an array. Example:
/// \code
/// // Get selected items as an array
/// Fl_Tree_Item_Array items;
/// tree->get_selected_items(items);
/// // Manipulate the returned array
/// for ( int t=0; t<items.total(); t++ ) {
/// Fl_Tree_Item &item = items[t];
/// ..do stuff with each selected item..
/// }
/// \endcode
/// \param[in] items The returned array of selected items.
/// \returns The number of items in the returned array.
/// \see first_selected_item(), next_selected_item()
///
int Fl_Tree::get_selected_items(Fl_Tree_Item_Array &ret_items) {
ret_items.clear();
for ( Fl_Tree_Item *i=first_selected_item(); i; i=next_selected_item(i) ) {
ret_items.add(i);
}
return ret_items.total();
}
#endif
/// Open the specified 'item'.
/// This causes the item's children (if any) to be shown.
/// Handles redrawing if anything was actually changed.

View File

@ -67,6 +67,9 @@ Fl_Tree_Item::Fl_Tree_Item(const Fl_Tree_Prefs &prefs) {
_usericon = 0;
_userdata = 0;
_parent = 0;
#if FLTK_ABI_VERSION >= 10303
_children.manage_item_destroy(1); // let array's dtor manage destroying Fl_Tree_Items
#endif
#if FLTK_ABI_VERSION >= 10301
_prev_sibling = 0;
_next_sibling = 0;

View File

@ -36,6 +36,9 @@ Fl_Tree_Item_Array::Fl_Tree_Item_Array(int new_chunksize) {
_items = 0;
_total = 0;
_size = 0;
#if FLTK_ABI_VERSION >= 10303
_flags = 0;
#endif
_chunksize = new_chunksize;
}
@ -50,10 +53,24 @@ Fl_Tree_Item_Array::Fl_Tree_Item_Array(const Fl_Tree_Item_Array* o) {
_total = 0;
_size = o->_size;
_chunksize = o->_chunksize;
#if FLTK_ABI_VERSION >= 10303
_flags = o->_flags;
#endif
for ( int t=0; t<o->_total; t++ ) {
_items[t] = new Fl_Tree_Item(o->_items[t]);
#if FLTK_ABI_VERSION >= 10303
if ( _flags & MANAGE_ITEM ) {
_items[t] = new Fl_Tree_Item(o->_items[t]); // make new copy of item
++_total;
_items[t]->update_prev_next(t); // update uses _total's current value
} else {
_items[t] = o->_items[t]; // copy ptr only
++_total;
}
#else
_items[t] = new Fl_Tree_Item(o->_items[t]); // make new copy of item
++_total;
_items[t]->update_prev_next(t); // update uses _total's current value
#endif
}
}
@ -65,9 +82,14 @@ Fl_Tree_Item_Array::Fl_Tree_Item_Array(const Fl_Tree_Item_Array* o) {
void Fl_Tree_Item_Array::clear() {
if ( _items ) {
for ( int t=0; t<_total; t++ ) {
#if FLTK_ABI_VERSION >= 10303
if ( _flags & MANAGE_ITEM )
#endif
{
delete _items[t];
_items[t] = 0;
}
}
free((void*)_items); _items = 0;
}
_total = _size = 0;
@ -110,7 +132,12 @@ void Fl_Tree_Item_Array::insert(int pos, Fl_Tree_Item *new_item) {
}
_items[pos] = new_item;
_total++;
#if FLTK_ABI_VERSION >= 10303
if ( _flags & MANAGE_ITEM )
#endif
{
_items[pos]->update_prev_next(pos); // adjust item's prev/next and its neighbors
}
}
/// Add an item* to the end of the array.
@ -129,6 +156,9 @@ void Fl_Tree_Item_Array::add(Fl_Tree_Item *val) {
///
void Fl_Tree_Item_Array::remove(int index) {
if ( _items[index] ) { // delete if non-zero
#if FLTK_ABI_VERSION >= 10303
if ( _flags & MANAGE_ITEM )
#endif
delete _items[index];
}
_items[index] = 0;
@ -136,11 +166,16 @@ void Fl_Tree_Item_Array::remove(int index) {
for ( int i=index; i<_total; i++ ) { // reshuffle the array
_items[i] = _items[i+1];
}
#if FLTK_ABI_VERSION >= 10303
if ( _flags & MANAGE_ITEM )
#endif
{
if ( index < _total ) { // removed item not last?
_items[index]->update_prev_next(index); // update next item's prev/next and neighbors
} else if ( ((index-1) >= 0) && // removed item IS last?
((index-1) < _total)) {
_items[index-1]->update_prev_next(index-1); // update prev item's prev/next and neighbors
_items[index-1]->update_prev_next(index-1);// update prev item's prev/next and neighbors
}
}
}
@ -164,9 +199,14 @@ void Fl_Tree_Item_Array::swap(int ax, int bx) {
Fl_Tree_Item *asave = _items[ax];
_items[ax] = _items[bx];
_items[bx] = asave;
#if FLTK_ABI_VERSION >= 10303
if ( _flags & MANAGE_ITEM )
#endif
{
// Adjust prev/next ptrs
_items[ax]->update_prev_next(ax);
_items[bx]->update_prev_next(bx);
}
}
#endif /* FLTK_ABI_VERSION */