[nautilus/wip/antoniof/gtk4-preparation-step-container-api: 7/7] general: Stop using GtkContainer::get_children() and ::foreach()




commit 0d7814b409f26a702669472d23c6f7d085f1e5fd
Author: António Fernandes <antoniof gnome org>
Date:   Thu Nov 25 15:19:48 2021 +0000

    general: Stop using GtkContainer::get_children() and ::foreach()
    
    Add transition wrappers for needed GTK4 methods unavailable in GTK3.

 src/nautilus-gtk4-helpers.c | 25 +++++++++++++++++++++++++
 src/nautilus-gtk4-helpers.h |  4 ++++
 src/nautilus-toolbar.c      | 23 +++++++----------------
 src/nautilus-window-slot.c  | 22 ++++------------------
 4 files changed, 40 insertions(+), 34 deletions(-)
---
diff --git a/src/nautilus-gtk4-helpers.c b/src/nautilus-gtk4-helpers.c
index 50ee263e5..60da7672a 100644
--- a/src/nautilus-gtk4-helpers.c
+++ b/src/nautilus-gtk4-helpers.c
@@ -18,6 +18,15 @@ gtk_box_append (GtkBox    *box,
     gtk_container_add (GTK_CONTAINER (box), child);
 }
 
+void
+gtk_box_remove (GtkBox    *box,
+                GtkWidget *child)
+{
+    g_assert (GTK_IS_BOX (box));
+
+    gtk_container_remove (GTK_CONTAINER (box), child);
+}
+
 void
 gtk_overlay_set_child (GtkOverlay *overlay,
                        GtkWidget  *child)
@@ -63,3 +72,19 @@ gtk_revealer_set_child (GtkRevealer *revealer,
 
     gtk_container_add (GTK_CONTAINER (revealer), child);
 }
+
+GtkWidget *
+gtk_widget_get_first_child (GtkWidget *widget)
+{
+    g_autoptr (GList) children = NULL;
+
+    g_assert (GTK_IS_CONTAINER (widget));
+
+    children = gtk_container_get_children (GTK_CONTAINER (widget));
+    if (children != NULL)
+    {
+        return GTK_WIDGET (children->data);
+    }
+
+    return NULL;
+}
diff --git a/src/nautilus-gtk4-helpers.h b/src/nautilus-gtk4-helpers.h
index b245f1dd6..201183c15 100644
--- a/src/nautilus-gtk4-helpers.h
+++ b/src/nautilus-gtk4-helpers.h
@@ -11,6 +11,8 @@ void gtk_button_set_child          (GtkButton         *button,
                                     GtkWidget         *child);
 void gtk_box_append                (GtkBox            *box,
                                     GtkWidget         *child);
+void gtk_box_remove                (GtkBox            *box,
+                                    GtkWidget         *child);
 void gtk_overlay_set_child         (GtkOverlay        *overlay,
                                     GtkWidget         *child);
 void gtk_scrolled_window_set_child (GtkScrolledWindow *scrolled_window,
@@ -22,5 +24,7 @@ void gtk_info_bar_add_child        (GtkInfoBar        *info_bar,
 void gtk_revealer_set_child        (GtkRevealer       *revealer,
                                     GtkWidget         *child);
 
+GtkWidget *gtk_widget_get_first_child (GtkWidget *widget);
+
 #endif
 G_END_DECLS
diff --git a/src/nautilus-toolbar.c b/src/nautilus-toolbar.c
index 846e4c0dc..b9fae45d7 100644
--- a/src/nautilus-toolbar.c
+++ b/src/nautilus-toolbar.c
@@ -1285,17 +1285,13 @@ nautilus_toolbar_set_show_location_entry (NautilusToolbar *self,
 }
 
 static void
-container_remove_all_children (GtkContainer *container)
+box_remove_all_children (GtkBox *box)
 {
-    GList *children;
-    GList *child;
-
-    children = gtk_container_get_children (container);
-    for (child = children; child != NULL; child = g_list_next (child))
+    GtkWidget *child;
+    while ((child = gtk_widget_get_first_child (GTK_WIDGET (box))) != NULL)
     {
-        gtk_container_remove (container, GTK_WIDGET (child->data));
+        gtk_box_remove (GTK_BOX (box), child);
     }
-    g_list_free (children);
 }
 
 static void
@@ -1329,8 +1325,8 @@ on_slot_toolbar_menu_sections_changed (NautilusToolbar    *self,
 {
     NautilusToolbarMenuSections *new_sections;
 
-    container_remove_all_children (GTK_CONTAINER (self->view_menu_zoom_section));
-    container_remove_all_children (GTK_CONTAINER (self->view_menu_extended_section));
+    box_remove_all_children (GTK_BOX (self->view_menu_zoom_section));
+    box_remove_all_children (GTK_BOX (self->view_menu_extended_section));
 
     new_sections = nautilus_window_slot_get_toolbar_menu_sections (slot);
     if (new_sections == NULL)
@@ -1460,12 +1456,7 @@ nautilus_toolbar_set_window_slot_real (NautilusToolbar    *self,
                                   G_CALLBACK (toolbar_update_appearance), self);
     }
 
-    children = gtk_container_get_children (GTK_CONTAINER (self->search_container));
-    if (children != NULL)
-    {
-        gtk_container_remove (GTK_CONTAINER (self->search_container),
-                              children->data);
-    }
+    box_remove_all_children (GTK_BOX (self->search_container));
 
     if (self->window_slot != NULL)
     {
diff --git a/src/nautilus-window-slot.c b/src/nautilus-window-slot.c
index ef8cbb2a3..40f3cabcf 100644
--- a/src/nautilus-window-slot.c
+++ b/src/nautilus-window-slot.c
@@ -656,27 +656,13 @@ nautilus_window_slot_handle_event (NautilusWindowSlot *self,
 }
 
 static void
-remove_all_extra_location_widgets (GtkWidget *widget,
-                                   gpointer   data)
+nautilus_window_slot_remove_extra_location_widgets (NautilusWindowSlot *self)
 {
-    NautilusWindowSlot *self = data;
-    NautilusDirectory *directory;
-
-    directory = nautilus_directory_get (self->location);
-    if (widget != GTK_WIDGET (self->query_editor))
+    GtkWidget *widget;
+    while ((widget = gtk_widget_get_first_child (self->extra_location_widgets)) != NULL)
     {
-        gtk_container_remove (GTK_CONTAINER (self->extra_location_widgets), widget);
+        gtk_box_remove (GTK_BOX (self->extra_location_widgets), widget);
     }
-
-    nautilus_directory_unref (directory);
-}
-
-static void
-nautilus_window_slot_remove_extra_location_widgets (NautilusWindowSlot *self)
-{
-    gtk_container_foreach (GTK_CONTAINER (self->extra_location_widgets),
-                           remove_all_extra_location_widgets,
-                           self);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]