[sysprof/wip/gtk4-port] display: port to GTK 4



commit f8008179334d2ce6270841dcf42c3189f9d5ff67
Author: Christian Hergert <chergert redhat com>
Date:   Thu Sep 30 18:57:33 2021 -0700

    display: port to GTK 4

 src/libsysprof-ui/sysprof-display.c | 114 ++++++++++++++++++++----------------
 1 file changed, 65 insertions(+), 49 deletions(-)
---
diff --git a/src/libsysprof-ui/sysprof-display.c b/src/libsysprof-ui/sysprof-display.c
index 0c86e1e7..5143fe07 100644
--- a/src/libsysprof-ui/sysprof-display.c
+++ b/src/libsysprof-ui/sysprof-display.c
@@ -95,10 +95,7 @@ update_title_child_property (SysprofDisplay *self)
   if ((parent = gtk_widget_get_parent (GTK_WIDGET (self))) && GTK_IS_NOTEBOOK (parent))
     {
       g_autofree gchar *title = sysprof_display_dup_title (self);
-
-      gtk_container_child_set (GTK_CONTAINER (parent), GTK_WIDGET (self),
-                               "menu-label", title,
-                               NULL);
+      gtk_notebook_set_menu_label_text (GTK_NOTEBOOK (parent), GTK_WIDGET (self), title);
     }
 
   g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
@@ -311,19 +308,17 @@ sysprof_display_notify_selection_cb (SysprofDisplay          *self,
       /* Opportunistically load pages */
       if (priv->reader != NULL)
         {
-          GList *pages = gtk_container_get_children (GTK_CONTAINER (priv->pages));
-
-          for (const GList *iter = pages; iter; iter = iter->next)
+          for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (priv->pages));
+               child;
+               child = gtk_widget_get_next_sibling (child))
             {
-              if (SYSPROF_IS_PAGE (iter->data))
-                sysprof_page_load_async (iter->data,
+              if (SYSPROF_IS_PAGE (child))
+                sysprof_page_load_async (SYSPROF_PAGE (child),
                                          priv->reader,
                                          selection,
                                          priv->filter,
                                          NULL, NULL, NULL);
             }
-
-          g_list_free (pages);
         }
     }
 }
@@ -544,7 +539,7 @@ sysprof_display_add_group (SysprofDisplay         *self,
   if (priv->reader != NULL)
     _sysprof_visualizer_group_set_reader (group, priv->reader);
 
-  gtk_container_add (GTK_CONTAINER (priv->visualizers), GTK_WIDGET (group));
+  sysprof_visualizers_frame_add_group (priv->visualizers, group);
 }
 
 void
@@ -559,10 +554,7 @@ sysprof_display_add_page (SysprofDisplay *self,
   g_return_if_fail (SYSPROF_IS_PAGE (page));
 
   title = sysprof_page_get_title (page);
-
-  gtk_container_add_with_properties (GTK_CONTAINER (priv->pages), GTK_WIDGET (page),
-                                     "title", title,
-                                     NULL);
+  gtk_stack_add_titled (priv->pages, GTK_WIDGET (page), NULL, title);
 
   selection = sysprof_visualizers_frame_get_selection (priv->visualizers);
 
@@ -925,7 +917,6 @@ sysprof_display_load_scan_cb (GObject      *object,
   SysprofCaptureReader *reader;
   SysprofSelection *selection;
   GCancellable *cancellable;
-  GList *pages;
 
   g_assert (SYSPROF_IS_DISPLAY (self));
   g_assert (G_IS_ASYNC_RESULT (result));
@@ -948,17 +939,17 @@ sysprof_display_load_scan_cb (GObject      *object,
   sysprof_details_page_set_reader (priv->details, reader);
 
   /* Opportunistically load pages */
-  pages = gtk_container_get_children (GTK_CONTAINER (priv->pages));
-  for (const GList *iter = pages; iter; iter = iter->next)
+  for (GtkWidget *child = gtk_widget_get_first_child (GTK_WIDGET (priv->pages));
+       child;
+       child = gtk_widget_get_next_sibling (child))
     {
-      if (SYSPROF_IS_PAGE (iter->data))
-        sysprof_page_load_async (iter->data,
+      if (SYSPROF_IS_PAGE (child))
+        sysprof_page_load_async (SYSPROF_PAGE (child),
                                  reader,
                                  selection,
                                  priv->filter,
                                  NULL, NULL, NULL);
     }
-  g_list_free (pages);
 
   gtk_stack_set_visible_child_name (priv->stack, "view");
 }
@@ -1072,6 +1063,7 @@ sysprof_display_open (SysprofDisplay *self,
 
   if (!(reader = sysprof_capture_reader_new_with_error (path, &error)))
     {
+      GtkWidget *parent;
       GtkWidget *dialog;
       GtkWidget *window;
 
@@ -1089,12 +1081,16 @@ sysprof_display_open (SysprofDisplay *self,
                                                 error->message);
       g_signal_connect (dialog,
                         "response",
-                        G_CALLBACK (gtk_widget_destroy),
+                        G_CALLBACK (gtk_window_destroy),
                         NULL);
       gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
       gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (window));
       gtk_window_present (GTK_WINDOW (dialog));
-      gtk_widget_destroy (GTK_WIDGET (self));
+
+      if ((parent = gtk_widget_get_parent (GTK_WIDGET (self))) &&
+          GTK_IS_NOTEBOOK (parent))
+        gtk_notebook_remove_page (GTK_NOTEBOOK (parent),
+                                  gtk_notebook_page_num (GTK_NOTEBOOK (parent), GTK_WIDGET (self)));
 
       return;
     }
@@ -1192,36 +1188,21 @@ sysprof_display_new_for_profiler (SysprofProfiler *profiler)
   return GTK_WIDGET (g_steal_pointer (&self));
 }
 
-void
-sysprof_display_save (SysprofDisplay *self)
+static void
+on_save_response_cb (SysprofDisplay       *self,
+                     int                   res,
+                     GtkFileChooserNative *chooser)
 {
   SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self);
   g_autoptr(GFile) file = NULL;
-  GtkFileChooserNative *native;
-  GtkWindow *parent;
-  gint res;
-
-  g_return_if_fail (SYSPROF_IS_DISPLAY (self));
-  g_return_if_fail (priv->reader != NULL);
 
-  parent = GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (self)));
-
-  native = gtk_file_chooser_native_new (_("Save Recording"),
-                                        parent,
-                                        GTK_FILE_CHOOSER_ACTION_SAVE,
-                                        _("Save"),
-                                        _("Cancel"));
-  gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (native), TRUE);
-  gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (native), TRUE);
-  gtk_file_chooser_set_create_folders (GTK_FILE_CHOOSER (native), TRUE);
-  gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (native), "capture.syscap");
-
-  res = gtk_native_dialog_run (GTK_NATIVE_DIALOG (native));
+  g_assert (SYSPROF_IS_DISPLAY (self));
+  g_assert (GTK_IS_FILE_CHOOSER_NATIVE (chooser));
 
   switch (res)
     {
     case GTK_RESPONSE_ACCEPT:
-      file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (native));
+      file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (chooser));
 
       if (g_file_is_native (file))
         {
@@ -1231,17 +1212,24 @@ sysprof_display_save (SysprofDisplay *self)
           if (!sysprof_capture_reader_save_as_with_error (priv->reader, path, &error))
             {
               GtkWidget *msg;
+              GtkNative *root;
 
-              msg = gtk_message_dialog_new (parent,
+              root = gtk_widget_get_native (GTK_WIDGET (self));
+              msg = gtk_message_dialog_new (GTK_WINDOW (root),
                                             GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | 
GTK_DIALOG_USE_HEADER_BAR,
                                             GTK_MESSAGE_ERROR,
                                             GTK_BUTTONS_CLOSE,
                                             _("Failed to save recording: %s"),
                                             error->message);
               gtk_window_present (GTK_WINDOW (msg));
-              g_signal_connect (msg, "response", G_CALLBACK (gtk_widget_destroy), NULL);
+              g_signal_connect (msg, "response", G_CALLBACK (gtk_window_destroy), NULL);
             }
         }
+      else
+        {
+          g_autofree char *uri = g_file_get_uri (file);
+          g_warning ("%s is not native, cannot open", uri);
+        }
 
       break;
 
@@ -1250,7 +1238,35 @@ sysprof_display_save (SysprofDisplay *self)
     }
 
   g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
-  gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (native));
+  gtk_native_dialog_destroy (GTK_NATIVE_DIALOG (chooser));
+}
+
+void
+sysprof_display_save (SysprofDisplay *self)
+{
+  SysprofDisplayPrivate *priv = sysprof_display_get_instance_private (self);
+  GtkFileChooserNative *native;
+  GtkNative *root;
+
+  g_return_if_fail (SYSPROF_IS_DISPLAY (self));
+  g_return_if_fail (priv->reader != NULL);
+
+  root = gtk_widget_get_native (GTK_WIDGET (self));
+  native = gtk_file_chooser_native_new (_("Save Recording"),
+                                        GTK_WINDOW (root),
+                                        GTK_FILE_CHOOSER_ACTION_SAVE,
+                                        _("Save"),
+                                        _("Cancel"));
+  gtk_file_chooser_set_create_folders (GTK_FILE_CHOOSER (native), TRUE);
+  gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (native), "capture.syscap");
+
+  g_signal_connect_object (native,
+                           "response",
+                           G_CALLBACK (on_save_response_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
 }
 
 void


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