From 9abbff2078e2a092ecc252f6b147c24fe567f616 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Tue, 25 May 2021 17:39:20 +0200 Subject: Add virtual int Fl_Group::delete_child(int n) (STR 3218) This is a convenience method that does range checking (index n), removes the child given by index n from the group and deletes it. --- FL/Fl_Group.H | 5 ++++- src/Fl_Group.cxx | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/FL/Fl_Group.H b/FL/Fl_Group.H index 5d28f8cdd..2271962b3 100644 --- a/FL/Fl_Group.H +++ b/FL/Fl_Group.H @@ -1,7 +1,7 @@ // // Group header file for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2020 by Bill Spitzak and others. +// Copyright 1998-2021 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -117,6 +117,9 @@ public: void remove(Fl_Widget* o) {remove(*o);} void clear(); + /* delete child n (by index) */ + virtual int delete_child(int n); + /** Sets the group's resizable widget. See void Fl_Group::resizable(Fl_Widget *o) diff --git a/src/Fl_Group.cxx b/src/Fl_Group.cxx index 769d28600..541c8418b 100644 --- a/src/Fl_Group.cxx +++ b/src/Fl_Group.cxx @@ -546,6 +546,52 @@ void Fl_Group::remove(Fl_Widget &o) { if (i < children_) remove(i); } +/** + Removes the widget at \p index from the group and deletes it. + + This method does nothing if \p index is out of bounds. + + This method differs from the remove() method in that it deletes + the widget from memory. Since this method is virtual it can be + reimplemented in subclasses with additional requirements and + consequences. See the documentation of subclasses. + + Many subclasses don't need to reimplement this method. + + \note This method \b may refuse to remove and delete the widget + if it is an essential part of the Fl_Group, for instance + a scrollbar in an Fl_Scroll group. In this case the widget is + neither removed nor deleted. + + This method does not call init_sizes() or redraw(). This is left + to user code if necessary. + + Returns 0 if the widget was removed and deleted. + Return values \> 0 are reserved for use by FLTK core widgets. + Return values \< 0 are free to be used by user defined widgets. + + \todo Reimplementation of Fl_Group::delete_widget(int) in more FLTK + subclasses. This is not yet complete. + + \param[in] index index of child to be removed + + \returns success (0) or error code + \retval 0 success + \retval 1 index out of range + \retval 2 widget not allowed to be removed (see note) + \retval >2 reserved for FLTK use + + \since FLTK 1.4.0 +*/ +int Fl_Group::delete_child(int index) { + if (index < 0 || index >= children_) + return 1; + Fl_Widget *w = child(index); + remove(index); + delete w; + return 0; +} + /** Resets the internal array of widget sizes and positions. -- cgit v1.2.3