summaryrefslogtreecommitdiff
path: root/src/Fl_Group.cxx
diff options
context:
space:
mode:
authorAlbrecht Schlosser <albrechts.fltk@online.de>2010-08-31 10:01:59 +0000
committerAlbrecht Schlosser <albrechts.fltk@online.de>2010-08-31 10:01:59 +0000
commit5a3aced3ab97ff110e44d2ed8ff6e5c47c7064d7 (patch)
treee918f2d9ac2492dbb4be0573f06ec08028ac6a92 /src/Fl_Group.cxx
parent95aa6513258805a7f75377e87317824c65a22d32 (diff)
New method: Fl_Group::remove(int index) to speed up the removal
of widgets if the widget's index is known. Thanks to Greg for the patch. See also STR #2409. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.3@7693 ea41ed52-d2ee-0310-a9c1-e6b18d33e121
Diffstat (limited to 'src/Fl_Group.cxx')
-rw-r--r--src/Fl_Group.cxx58
1 files changed, 34 insertions, 24 deletions
diff --git a/src/Fl_Group.cxx b/src/Fl_Group.cxx
index 46de470e4..edc2036e7 100644
--- a/src/Fl_Group.cxx
+++ b/src/Fl_Group.cxx
@@ -389,16 +389,12 @@ void Fl_Group::clear() {
init_sizes();
// okay, now it is safe to destroy the children:
while (children_) {
- Fl_Widget* o = child(0); // *first* child widget
- if (o->parent() == this) { // should always be true
- remove(o); // remove child widget first
- delete o; // then delete it
+ Fl_Widget* w = child(0); // *first* child widget
+ if (w->parent() == this) { // should always be true
+ remove(0); // remove child widget first
+ delete w; // then delete it
} else { // this should never happen !
-#ifdef DEBUG_CLEAR
- printf ("Fl_Group::clear() widget:%p, parent: %p != this (%p)\n",
- o, o->parent(), this); fflush(stdout);
-#endif // DEBUG_CLEAR
- remove(o); // remove it
+ remove(0); // remove it only
}
}
}
@@ -435,7 +431,7 @@ void Fl_Group::insert(Fl_Widget &o, int index) {
if (index > n) index--;
if (index == n) return;
}
- g->remove(o);
+ g->remove(n);
}
o.parent_ = this;
if (children_ == 0) { // use array pointer to point at single child
@@ -463,41 +459,55 @@ void Fl_Group::insert(Fl_Widget &o, int index) {
void Fl_Group::add(Fl_Widget &o) {insert(o, children_);}
/**
- Removes a widget from the group but does not delete it.
+ Removes the widget at \p index from the group but does not delete it.
- This method does nothing if the widget is not a child of the group.
+ This method does nothing if \p index is out of bounds.
This method differs from the clear() method in that it only affects
a single widget and does not delete it from memory.
+
+ \since FLTK 1.3.0
*/
-void Fl_Group::remove(Fl_Widget &o) {
- if (!children_) return;
- int i = find(o);
- if (i >= children_) return;
+void Fl_Group::remove(int index) {
+ if (index < 0 || index >= children_) return;
+ Fl_Widget &o = *child(index);
if (&o == savedfocus_) savedfocus_ = 0;
if (o.parent_ == this) { // this should always be true
o.parent_ = 0;
}
-#ifdef DEBUG_REMOVE
- else { // this should never happen !
- printf ("Fl_Group::remove(): widget %p, parent_ (%p) != this (%p)\n",
- &o, o.parent_, this);
- }
-#endif // DEBUG_REMOVE
// remove the widget from the group
children_--;
if (children_ == 1) { // go from 2 to 1 child
- Fl_Widget *t = array_[!i];
+ Fl_Widget *t = array_[!index];
free((void*)array_);
array_ = (Fl_Widget**)t;
} else if (children_ > 1) { // delete from array
- for (; i < children_; i++) array_[i] = array_[i+1];
+ for (; index < children_; index++) array_[index] = array_[index+1];
}
init_sizes();
}
+/**
+ Removes a widget from the group but does not delete it.
+
+ This method does nothing if the widget is not a child of the group.
+
+ This method differs from the clear() method in that it only affects
+ a single widget and does not delete it from memory.
+
+ \note If you have the child's index anyway, use remove(int index)
+ instead, because this doesn't need a child lookup in the group's
+ table of children. This can be much faster, if there are lots of
+ children.
+*/
+void Fl_Group::remove(Fl_Widget &o) {
+ if (!children_) return;
+ int i = find(o);
+ if (i < children_) remove(i);
+}
+
////////////////////////////////////////////////////////////////
// Rather lame kludge here, I need to detect windows and ignore the