themes: Workaround for bug where a background color of RGB 0,0,0 in Black color schem...
[ntk.git] / src / Fl_Tree_Item_Array.cxx
blob13600bd882dbfedd4c48292e93ea59ed40d11e6c
1 //
2 // "$Id: Fl_Tree_Item_Array.cxx 7903 2010-11-28 21:06:39Z matt $"
3 //
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 #include <FL/Fl_Tree_Item_Array.H>
10 #include <FL/Fl_Tree_Item.H>
12 //////////////////////
13 // Fl_Tree_Item_Array.cxx
14 //////////////////////
16 // Fl_Tree -- This file is part of the Fl_Tree widget for FLTK
17 // Copyright (C) 2009-2010 by Greg Ercolano.
19 // This library is free software; you can redistribute it and/or
20 // modify it under the terms of the GNU Library General Public
21 // License as published by the Free Software Foundation; either
22 // version 2 of the License, or (at your option) any later version.
24 // This library is distributed in the hope that it will be useful,
25 // but WITHOUT ANY WARRANTY; without even the implied warranty of
26 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 // Library General Public License for more details.
29 // You should have received a copy of the GNU Library General Public
30 // License along with this library; if not, write to the Free Software
31 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 // USA.
35 /// Constructor; creates an empty array.
36 ///
37 /// The optional 'chunksize' can be specified to optimize
38 /// memory allocation for potentially large arrays. Default chunksize is 10.
39 ///
40 Fl_Tree_Item_Array::Fl_Tree_Item_Array(int new_chunksize) {
41 _items = 0;
42 _total = 0;
43 _size = 0;
44 _chunksize = new_chunksize;
47 /// Destructor. Calls each item's destructor, destroys internal _items array.
48 Fl_Tree_Item_Array::~Fl_Tree_Item_Array() {
49 clear();
52 /// Copy constructor. Makes new copy of array, with new instances of each item.
53 Fl_Tree_Item_Array::Fl_Tree_Item_Array(const Fl_Tree_Item_Array* o) {
54 _items = (Fl_Tree_Item**)malloc(o->_size * sizeof(Fl_Tree_Item*));
55 _total = o->_total;
56 _size = o->_size;
57 _chunksize = o->_chunksize;
58 for ( int t=0; t<o->_total; t++ ) {
59 _items[t] = new Fl_Tree_Item(o->_items[t]);
63 /// Clear the entire array.
64 ///
65 /// Each item will be deleted (destructors will be called),
66 /// and the array will be cleared. total() will return 0.
67 ///
68 void Fl_Tree_Item_Array::clear() {
69 if ( _items ) {
70 for ( int t=0; t<_total; t++ ) {
71 delete _items[t];
72 _items[t] = 0;
74 free((void*)_items); _items = 0;
76 _total = _size = 0;
79 // Internal: Enlarge the items array.
81 // Adjusts size/items memory allocation as needed.
82 // Does NOT change total.
84 void Fl_Tree_Item_Array::enlarge(int count) {
85 int newtotal = _total + count; // new total
86 if ( newtotal >= _size ) { // more than we have allocated?
87 // Increase size of array
88 int newsize = _size + _chunksize;
89 Fl_Tree_Item **newitems = (Fl_Tree_Item**)malloc(newsize * sizeof(Fl_Tree_Item*));
90 if ( _items ) {
91 // Copy old array -> new, delete old
92 memmove(newitems, _items, _size * sizeof(Fl_Tree_Item*));
93 free((void*)_items); _items = 0;
95 // Adjust items/sizeitems
96 _items = newitems;
97 _size = newsize;
101 /// Insert an item at index position \p pos.
103 /// Handles enlarging array if needed, total increased by 1.
104 /// If \p pos == total(), an empty item is appended to the array.
106 void Fl_Tree_Item_Array::insert(int pos, Fl_Tree_Item *new_item) {
107 enlarge(1);
108 // printf("*** POS=%d TOTAL-1=%d NITEMS=%d\n", pos, _total-1, (_total-pos));
109 if ( pos <= (_total - 1) ) { // need to move memory around?
110 int nitems = _total - pos;
111 memmove(&_items[pos+1], &_items[pos], sizeof(Fl_Tree_Item*) * nitems);
113 _items[pos] = new_item;
114 _total++;
117 /// Add an item* to the end of the array.
119 /// Assumes the item was created with 'new', and will remain
120 /// allocated.. Fl_Tree_Item_Array will handle calling the
121 /// item's destructor when the array is cleared or the item remove()'ed.
123 void Fl_Tree_Item_Array::add(Fl_Tree_Item *val) {
124 insert(_total, val);
127 /// Remove the item at \param[in] index from the array.
129 /// The item will be delete'd (if non-NULL), so its destructor will be called.
131 void Fl_Tree_Item_Array::remove(int index) {
132 if ( _items[index] ) { // delete if non-zero
133 delete _items[index];
135 _items[index] = 0;
136 for ( _total--; index<_total; index++ ) {
137 _items[index] = _items[index+1];
141 /// Remove the item from the array.
143 /// \returns 0 if removed, or -1 if the item was not in the array.
145 int Fl_Tree_Item_Array::remove(Fl_Tree_Item *item) {
146 for ( int t=0; t<_total; t++ ) {
147 if ( item == _items[t] ) {
148 remove(t);
149 return(0);
152 return(-1);
156 // End of "$Id: Fl_Tree_Item_Array.cxx 7903 2010-11-28 21:06:39Z matt $".