[sysprof/wip/gtk4-port: 60/132] notebook: port to GTK 4




commit 47caea35b0eff00927bdc219331bf2ba595a6c2f
Author: Christian Hergert <chergert redhat com>
Date:   Thu Sep 30 19:09:43 2021 -0700

    notebook: port to GTK 4

 src/libsysprof-ui/sysprof-notebook.c | 138 +++++++++++++++++++++++------------
 src/libsysprof-ui/sysprof-notebook.h |   4 +-
 2 files changed, 94 insertions(+), 48 deletions(-)
---
diff --git a/src/libsysprof-ui/sysprof-notebook.c b/src/libsysprof-ui/sysprof-notebook.c
index b919df70..ae7145f5 100644
--- a/src/libsysprof-ui/sysprof-notebook.c
+++ b/src/libsysprof-ui/sysprof-notebook.c
@@ -29,10 +29,11 @@
 
 typedef struct
 {
+  GtkNotebook *notebook;
   guint always_show_tabs : 1;
 } SysprofNotebookPrivate;
 
-G_DEFINE_TYPE_WITH_PRIVATE (SysprofNotebook, sysprof_notebook, GTK_TYPE_NOTEBOOK)
+G_DEFINE_TYPE_WITH_PRIVATE (SysprofNotebook, sysprof_notebook, GTK_TYPE_WIDGET)
 
 enum {
   PROP_0,
@@ -83,15 +84,16 @@ sysprof_notebook_notify_can_replay_cb (SysprofNotebook *self,
 }
 
 static void
-sysprof_notebook_page_added (GtkNotebook *notebook,
-                             GtkWidget   *child,
-                             guint        page_num)
+sysprof_notebook_page_added (SysprofNotebook *self,
+                             GtkWidget       *child,
+                             guint            page_num,
+                             GtkNotebook     *notebook)
 {
-  SysprofNotebook *self = (SysprofNotebook *)notebook;
   SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
 
   g_assert (SYSPROF_IS_NOTEBOOK (self));
   g_assert (GTK_IS_WIDGET (child));
+  g_assert (GTK_IS_NOTEBOOK (notebook));
 
   gtk_notebook_set_show_tabs (notebook,
                               (priv->always_show_tabs ||
@@ -125,15 +127,16 @@ sysprof_notebook_page_added (GtkNotebook *notebook,
 }
 
 static void
-sysprof_notebook_page_removed (GtkNotebook *notebook,
-                               GtkWidget   *child,
-                               guint        page_num)
+sysprof_notebook_page_removed (SysprofNotebook *self,
+                               GtkWidget       *child,
+                               guint            page_num,
+                               GtkNotebook     *notebook)
 {
-  SysprofNotebook *self = (SysprofNotebook *)notebook;
   SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
 
   g_assert (SYSPROF_IS_NOTEBOOK (self));
   g_assert (GTK_IS_WIDGET (child));
+  g_assert (GTK_IS_NOTEBOOK (notebook));
 
   if (gtk_widget_in_destruction (GTK_WIDGET (notebook)))
     return;
@@ -141,16 +144,16 @@ sysprof_notebook_page_removed (GtkNotebook *notebook,
   if (gtk_notebook_get_n_pages (notebook) == 0)
     {
       child = sysprof_display_new ();
-      gtk_container_add (GTK_CONTAINER (notebook), child);
+      gtk_notebook_append_page (notebook, child, NULL);
       gtk_widget_show (child);
 
       g_signal_handlers_disconnect_by_func (child,
                                             G_CALLBACK (sysprof_notebook_notify_can_save_cb),
                                             notebook);
 
-      g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_REPLAY]);
-      g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_SAVE]);
-      g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CURRENT]);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_REPLAY]);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_SAVE]);
+      g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CURRENT]);
     }
 
   gtk_notebook_set_show_tabs (notebook,
@@ -159,18 +162,33 @@ sysprof_notebook_page_removed (GtkNotebook *notebook,
 }
 
 static void
-sysprof_notebook_switch_page (GtkNotebook *notebook,
-                              GtkWidget   *widget,
-                              guint        page)
+sysprof_notebook_switch_page (SysprofNotebook *self,
+                              GtkWidget       *widget,
+                              guint            page,
+                              GtkNotebook     *notebook)
 {
+  g_assert (SYSPROF_IS_NOTEBOOK (self));
   g_assert (GTK_IS_NOTEBOOK (notebook));
   g_assert (GTK_IS_WIDGET (widget));
 
-  GTK_NOTEBOOK_CLASS (sysprof_notebook_parent_class)->switch_page (notebook, widget, page);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_REPLAY]);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CAN_SAVE]);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CURRENT]);
+}
+
+static void
+sysprof_notebook_dispose (GObject *object)
+{
+  SysprofNotebook *self = (SysprofNotebook *)object;
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
+
+  if (priv->notebook)
+    {
+      gtk_widget_unparent (GTK_WIDGET (priv->notebook));
+      priv->notebook = NULL;
+    }
 
-  g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_REPLAY]);
-  g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CAN_SAVE]);
-  g_object_notify_by_pspec (G_OBJECT (notebook), properties [PROP_CURRENT]);
+  G_OBJECT_CLASS (sysprof_notebook_parent_class)->dispose (object);
 }
 
 static void
@@ -227,15 +245,12 @@ static void
 sysprof_notebook_class_init (SysprofNotebookClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
-  GtkNotebookClass *notebook_class = GTK_NOTEBOOK_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
 
+  object_class->dispose = sysprof_notebook_dispose;
   object_class->get_property = sysprof_notebook_get_property;
   object_class->set_property = sysprof_notebook_set_property;
 
-  notebook_class->page_added = sysprof_notebook_page_added;
-  notebook_class->page_removed = sysprof_notebook_page_removed;
-  notebook_class->switch_page = sysprof_notebook_switch_page;
-
   properties [PROP_ALWAYS_SHOW_TABS] =
     g_param_spec_boolean ("always-show-tabs",
                           "Always Show Tabs",
@@ -265,25 +280,51 @@ sysprof_notebook_class_init (SysprofNotebookClass *klass)
                          (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
 
   g_object_class_install_properties (object_class, N_PROPS, properties);
+
+  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
 }
 
 static void
 sysprof_notebook_init (SysprofNotebook *self)
 {
-  gtk_notebook_set_show_border (GTK_NOTEBOOK (self), FALSE);
-  gtk_notebook_set_scrollable (GTK_NOTEBOOK (self), TRUE);
-  gtk_notebook_popup_enable (GTK_NOTEBOOK (self));
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
+
+  priv->notebook = GTK_NOTEBOOK (gtk_notebook_new ());
+  gtk_widget_set_parent (GTK_WIDGET (priv->notebook), GTK_WIDGET (self));
+
+  gtk_notebook_set_show_border (priv->notebook, FALSE);
+  gtk_notebook_set_scrollable (priv->notebook, TRUE);
+  gtk_notebook_popup_enable (priv->notebook);
+
+  g_signal_connect_object (priv->notebook,
+                           "page-added",
+                           G_CALLBACK (sysprof_notebook_page_added),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->notebook,
+                           "page-removed",
+                           G_CALLBACK (sysprof_notebook_page_removed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->notebook,
+                           "switch-page",
+                           G_CALLBACK (sysprof_notebook_switch_page),
+                           self,
+                           G_CONNECT_SWAPPED | G_CONNECT_AFTER);
 }
 
 void
 sysprof_notebook_close_current (SysprofNotebook *self)
 {
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
   gint page;
 
   g_return_if_fail (SYSPROF_IS_NOTEBOOK (self));
 
-  if ((page = gtk_notebook_get_current_page (GTK_NOTEBOOK (self))) >= 0)
-    gtk_widget_destroy (gtk_notebook_get_nth_page (GTK_NOTEBOOK (self), page));
+  if ((page = gtk_notebook_get_current_page (priv->notebook)) >= 0)
+    gtk_notebook_remove_page (priv->notebook, page);
 }
 
 static void
@@ -307,29 +348,31 @@ void
 sysprof_notebook_open (SysprofNotebook *self,
                        GFile           *file)
 {
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
   GtkWidget *display = NULL;
   gint page;
 
   g_return_if_fail (SYSPROF_IS_NOTEBOOK (self));
   g_return_if_fail (g_file_is_native (file));
 
-  gtk_container_foreach (GTK_CONTAINER (self),
-                         find_empty_display_cb,
-                         &display);
+  for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (priv->notebook));
+       child && !display;
+       child = gtk_widget_get_next_sibling (child))
+    find_empty_display_cb (child, &display);
 
   if (display == NULL)
     {
 
       display = sysprof_display_new ();
-      page = gtk_notebook_insert_page (GTK_NOTEBOOK (self), display, NULL, -1);
+      page = gtk_notebook_insert_page (priv->notebook, display, NULL, -1);
       gtk_widget_show (display);
     }
   else
     {
-      page = gtk_notebook_page_num (GTK_NOTEBOOK (self), display);
+      page = gtk_notebook_page_num (priv->notebook, display);
     }
 
-  gtk_notebook_set_current_page (GTK_NOTEBOOK (self), page);
+  gtk_notebook_set_current_page (priv->notebook, page);
 
   sysprof_display_open (SYSPROF_DISPLAY (display), file);
 }
@@ -337,12 +380,13 @@ sysprof_notebook_open (SysprofNotebook *self,
 SysprofDisplay *
 sysprof_notebook_get_current (SysprofNotebook *self)
 {
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
   gint page;
 
   g_assert (SYSPROF_IS_NOTEBOOK (self));
 
-  if ((page = gtk_notebook_get_current_page (GTK_NOTEBOOK (self))) >= 0)
-    return SYSPROF_DISPLAY (gtk_notebook_get_nth_page (GTK_NOTEBOOK (self), page));
+  if ((page = gtk_notebook_get_current_page (priv->notebook)) >= 0)
+    return SYSPROF_DISPLAY (gtk_notebook_get_nth_page (priv->notebook, page));
 
   return NULL;
 }
@@ -387,6 +431,7 @@ sysprof_notebook_get_can_replay (SysprofNotebook *self)
 void
 sysprof_notebook_replay (SysprofNotebook *self)
 {
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
   SysprofDisplay *display;
   SysprofDisplay *replay;
   gint page;
@@ -401,15 +446,16 @@ sysprof_notebook_replay (SysprofNotebook *self)
   g_return_if_fail (SYSPROF_IS_DISPLAY (replay));
 
   gtk_widget_show (GTK_WIDGET (replay));
-  gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (replay));
-  page = gtk_notebook_page_num (GTK_NOTEBOOK (self), GTK_WIDGET (replay));
-  gtk_notebook_set_current_page (GTK_NOTEBOOK (self), page);
+  gtk_notebook_append_page (priv->notebook, GTK_WIDGET (replay), NULL);
+  page = gtk_notebook_page_num (priv->notebook, GTK_WIDGET (replay));
+  gtk_notebook_set_current_page (priv->notebook, page);
 }
 
 void
 sysprof_notebook_add_profiler (SysprofNotebook *self,
                                SysprofProfiler *profiler)
 {
+  SysprofNotebookPrivate *priv = sysprof_notebook_get_instance_private (self);
   GtkWidget *display;
   gint page;
 
@@ -419,9 +465,9 @@ sysprof_notebook_add_profiler (SysprofNotebook *self,
   display = sysprof_display_new_for_profiler (profiler);
 
   gtk_widget_show (display);
-  gtk_container_add (GTK_CONTAINER (self), display);
-  page = gtk_notebook_page_num (GTK_NOTEBOOK (self), display);
-  gtk_notebook_set_current_page (GTK_NOTEBOOK (self), page);
+  gtk_notebook_append_page (priv->notebook, GTK_WIDGET (display), NULL);
+  page = gtk_notebook_page_num (priv->notebook, display);
+  gtk_notebook_set_current_page (priv->notebook, page);
 }
 
 gboolean
@@ -447,9 +493,9 @@ sysprof_notebook_set_always_show_tabs (SysprofNotebook *self,
   if (always_show_tabs != priv->always_show_tabs)
     {
       priv->always_show_tabs = always_show_tabs;
-      gtk_notebook_set_show_tabs (GTK_NOTEBOOK (self),
+      gtk_notebook_set_show_tabs (priv->notebook,
                                   (priv->always_show_tabs ||
-                                   gtk_notebook_get_n_pages (GTK_NOTEBOOK (self)) > 1));
+                                   gtk_notebook_get_n_pages (priv->notebook) > 1));
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ALWAYS_SHOW_TABS]);
     }
 }
diff --git a/src/libsysprof-ui/sysprof-notebook.h b/src/libsysprof-ui/sysprof-notebook.h
index 676f4f5e..e256db73 100644
--- a/src/libsysprof-ui/sysprof-notebook.h
+++ b/src/libsysprof-ui/sysprof-notebook.h
@@ -35,11 +35,11 @@ G_BEGIN_DECLS
 #define SYSPROF_TYPE_NOTEBOOK (sysprof_notebook_get_type())
 
 SYSPROF_AVAILABLE_IN_ALL
-G_DECLARE_DERIVABLE_TYPE (SysprofNotebook, sysprof_notebook, SYSPROF, NOTEBOOK, GtkNotebook)
+G_DECLARE_DERIVABLE_TYPE (SysprofNotebook, sysprof_notebook, SYSPROF, NOTEBOOK, GtkWidget)
 
 struct _SysprofNotebookClass
 {
-  GtkNotebookClass parent_class;
+  GtkWidgetClass parent_class;
 
   /*< private >*/
   gpointer _reserved[16];


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