fltk 1.3.0rc3
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X. Release candidate.
  SfR Fresh Dox: fltk-1.3.0rc3-source.tar.gz ("inofficial" and yet experimental doxygen-generated source code documentation)  

Fl_Tree_Item_Array.cxx

Go to the documentation of this file.
00001 //
00002 // "$Id: Fl_Tree_Item_Array.cxx 7903 2010-11-28 21:06:39Z matt $"
00003 //
00004 
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <string.h>
00008 
00009 #include <FL/Fl_Tree_Item_Array.H>
00010 #include <FL/Fl_Tree_Item.H>
00011 
00013 // Fl_Tree_Item_Array.cxx
00015 //
00016 // Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
00017 // Copyright (C) 2009-2010 by Greg Ercolano.
00018 //
00019 // This library is free software; you can redistribute it and/or
00020 // modify it under the terms of the GNU Library General Public
00021 // License as published by the Free Software Foundation; either
00022 // version 2 of the License, or (at your option) any later version.
00023 //
00024 // This library is distributed in the hope that it will be useful,
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00027 // Library General Public License for more details.
00028 //
00029 // You should have received a copy of the GNU Library General Public
00030 // License along with this library; if not, write to the Free Software
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00032 // USA.
00033 //
00034 
00040 Fl_Tree_Item_Array::Fl_Tree_Item_Array(int new_chunksize) {
00041   _items     = 0;
00042   _total     = 0;
00043   _size      = 0;
00044   _chunksize = new_chunksize;
00045 }
00046 
00048 Fl_Tree_Item_Array::~Fl_Tree_Item_Array() {
00049   clear();
00050 }
00051 
00053 Fl_Tree_Item_Array::Fl_Tree_Item_Array(const Fl_Tree_Item_Array* o) {
00054   _items = (Fl_Tree_Item**)malloc(o->_size * sizeof(Fl_Tree_Item*));
00055   _total     = o->_total;
00056   _size      = o->_size;
00057   _chunksize = o->_chunksize;
00058   for ( int t=0; t<o->_total; t++ ) {
00059     _items[t] = new Fl_Tree_Item(o->_items[t]);
00060   }
00061 }
00062 
00068 void Fl_Tree_Item_Array::clear() {
00069   if ( _items ) {
00070     for ( int t=0; t<_total; t++ ) {
00071       delete _items[t];
00072       _items[t] = 0;
00073     }
00074     free((void*)_items); _items = 0;
00075   }
00076   _total = _size = 0;
00077 }
00078 
00079 // Internal: Enlarge the items array.
00080 //
00081 //    Adjusts size/items memory allocation as needed.
00082 //    Does NOT change total.
00083 //
00084 void Fl_Tree_Item_Array::enlarge(int count) {
00085   int newtotal = _total + count;        // new total
00086   if ( newtotal >= _size ) {            // more than we have allocated?
00087     // Increase size of array
00088     int newsize = _size + _chunksize;
00089     Fl_Tree_Item **newitems = (Fl_Tree_Item**)malloc(newsize * sizeof(Fl_Tree_Item*));
00090     if ( _items ) { 
00091       // Copy old array -> new, delete old
00092       memmove(newitems, _items, _size * sizeof(Fl_Tree_Item*));
00093       free((void*)_items); _items = 0;
00094     }
00095     // Adjust items/sizeitems
00096     _items = newitems;
00097     _size = newsize;
00098   }
00099 }
00100 
00106 void Fl_Tree_Item_Array::insert(int pos, Fl_Tree_Item *new_item) {
00107   enlarge(1);
00108   // printf("*** POS=%d TOTAL-1=%d NITEMS=%d\n", pos, _total-1, (_total-pos));
00109   if ( pos <= (_total - 1) ) {  // need to move memory around?
00110     int nitems = _total - pos;
00111     memmove(&_items[pos+1], &_items[pos], sizeof(Fl_Tree_Item*) * nitems);
00112   } 
00113   _items[pos] = new_item;
00114   _total++;
00115 }
00116 
00123 void Fl_Tree_Item_Array::add(Fl_Tree_Item *val) {
00124   insert(_total, val);
00125 }
00126 
00131 void Fl_Tree_Item_Array::remove(int index) {
00132   if ( _items[index] ) {                // delete if non-zero
00133     delete _items[index];
00134   }
00135   _items[index] = 0;
00136   for ( _total--; index<_total; index++ ) {
00137     _items[index] = _items[index+1];
00138   }
00139 }
00140 
00145 int Fl_Tree_Item_Array::remove(Fl_Tree_Item *item) {
00146   for ( int t=0; t<_total; t++ ) {
00147     if ( item == _items[t] ) {
00148       remove(t);
00149       return(0);
00150     }
00151   }
00152   return(-1);
00153 }
00154 
00155 //
00156 // End of "$Id: Fl_Tree_Item_Array.cxx 7903 2010-11-28 21:06:39Z matt $".
00157 //