Reputation: 3165
I'm into a problem with a GTK+ C application. I have a container that, when starting the application, contains a button. During the running an user interation must cause this widget to contain more of them.
I need to write a function that removes all the "old" inner buttons, then adds all the ones from a list and finally refresh the view. This is what I'm writing but some parts are missing (TODOs)
void refresh_sequence_panel()
{
GSList* iterator = NULL;
GtkWidget* button;
// TODO: Here the container must be empty
// Now add all the buttons
for (iterator = steps; iterator; iterator = iterator->next) {
button = gtk_button_new_from_stock(GTK_STOCK_ADD);
gtk_widget_set_size_request(button, SEQ_BUTTON_W, SEQ_BUTTON_H);
gtk_box_pack_start(GTK_BOX(sequence_panel), button, FALSE, FALSE, 5);
handler_id = g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(seq_popup), GTK_BOX(sequence_panel));
}
// TODO: Now refresh the view, so I can see the changes...
}
Hope that someone can help, thanks!
Upvotes: 10
Views: 16056
Reputation: 5606
One-liner:
gtk_container_foreach (GTK_CONTAINER (container), (void*) gtk_widget_destroy, NULL);
Upvotes: 1
Reputation: 113
This one worked for me (it's a variation of unwind's answer):
Glib::ListHandle<Widget*> childList = this->get_children();
Glib::ListHandle<Widget*>::iterator it = childList.begin();
while (it != childList.end()) {
remove(*(*it));
it++;
}
(GTKMM 2.4)
Upvotes: 0
Reputation: 3802
Here is the method that I followed. Because I am using gtkmm on c++
Gtk::Box_Helpers::BoxList *childList = &vboxImgLst->children();
Box_Helpers::BoxList::iterator start = childList->begin();
Box_Helpers::BoxList::iterator end = childList->end();
childList->erase(start, end);
where vboxImgLst is,
VBox *vboxImgLst;
Hope this will help to someone who are using gtkmm and c++.
Thanks
Upvotes: 0
Reputation: 399833
Removing all children:
GList *children, *iter;
children = gtk_container_get_children(GTK_CONTAINER(container));
for(iter = children; iter != NULL; iter = g_list_next(iter))
gtk_widget_destroy(GTK_WIDGET(iter->data));
g_list_free(children);
Note that the above just deletes each child widget directly, rather than asking the container to remove it (with gtk_container_remove()
), this is recommended by the documentation and matches what you intend, so it's fine in my opinion.
There's no point in "refreshing the view", as long as you actually add and show the newly built widgets to the container. GTK+ is event-based, and adding children to a container makes the container realize it needs to refresh its visual appearance automatically.
Upvotes: 16