diff options
| author | Greg Ercolano <erco@seriss.com> | 2013-11-06 20:44:47 +0000 |
|---|---|---|
| committer | Greg Ercolano <erco@seriss.com> | 2013-11-06 20:44:47 +0000 |
| commit | 33ab9cfed1693a3cb9577c680e1d504354cd98c0 (patch) | |
| tree | d06ccdcd4953a08c74b8a01a527dfab66b3c8b95 | |
| parent | 52d395ad818d28053d34e7a634d4c9c3eb8ab755 (diff) | |
Fl_Tree:
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
| -rw-r--r-- | FL/Fl_Tree.H | 3 | ||||
| -rw-r--r-- | FL/Fl_Tree_Item_Array.H | 17 | ||||
| -rw-r--r-- | src/Fl_Tree.cxx | 25 | ||||
| -rw-r--r-- | src/Fl_Tree_Item.cxx | 3 | ||||
| -rw-r--r-- | src/Fl_Tree_Item_Array.cxx | 68 |
5 files changed, 102 insertions, 14 deletions
diff --git a/FL/Fl_Tree.H b/FL/Fl_Tree.H index 849222aaf..f29161c01 100644 --- a/FL/Fl_Tree.H +++ b/FL/Fl_Tree.H @@ -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 diff --git a/FL/Fl_Tree_Item_Array.H b/FL/Fl_Tree_Item_Array.H index a8de00dcc..802381f7f 100644 --- a/FL/Fl_Tree_Item_Array.H +++ b/FL/Fl_Tree_Item_Array.H @@ -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*/ diff --git a/src/Fl_Tree.cxx b/src/Fl_Tree.cxx index 0ff40a8c7..ba37dab3f 100644 --- a/src/Fl_Tree.cxx +++ b/src/Fl_Tree.cxx @@ -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. diff --git a/src/Fl_Tree_Item.cxx b/src/Fl_Tree_Item.cxx index 307f1e9c3..daccbdbde 100644 --- a/src/Fl_Tree_Item.cxx +++ b/src/Fl_Tree_Item.cxx @@ -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; diff --git a/src/Fl_Tree_Item_Array.cxx b/src/Fl_Tree_Item_Array.cxx index b6eddd09d..2519cf226 100644 --- a/src/Fl_Tree_Item_Array.cxx +++ b/src/Fl_Tree_Item_Array.cxx @@ -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 + _items[t]->update_prev_next(t); // update uses _total's current value +#endif } } @@ -65,8 +82,13 @@ 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++ ) { - delete _items[t]; - _items[t] = 0; +#if FLTK_ABI_VERSION >= 10303 + if ( _flags & MANAGE_ITEM ) +#endif + { + delete _items[t]; + _items[t] = 0; + } } free((void*)_items); _items = 0; } @@ -110,7 +132,12 @@ void Fl_Tree_Item_Array::insert(int pos, Fl_Tree_Item *new_item) { } _items[pos] = new_item; _total++; - _items[pos]->update_prev_next(pos); // adjust item's prev/next and its neighbors +#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,18 +156,26 @@ 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 - delete _items[index]; +#if FLTK_ABI_VERSION >= 10303 + if ( _flags & MANAGE_ITEM ) +#endif + delete _items[index]; } _items[index] = 0; _total--; for ( int i=index; i<_total; i++ ) { // reshuffle the array _items[i] = _items[i+1]; } - 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 +#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 + } } } @@ -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; - // Adjust prev/next ptrs - _items[ax]->update_prev_next(ax); - _items[bx]->update_prev_next(bx); +#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 */ |
