[gedit/zbrown/deteplification-src: 425/633] File choosers: implement GeditFileChooserOpenNative




commit 07a0ee2c42a27e4b75851649506339103af1a405
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Jun 11 20:40:54 2020 +0200

    File choosers: implement GeditFileChooserOpenNative

 gedit/gedit-commands-file.c            |  5 ++-
 gedit/gedit-file-chooser-open-native.c | 27 +++++++++++++
 gedit/gedit-file-chooser-open.c        | 63 +++----------------------------
 gedit/gedit-file-chooser-open.h        |  7 ----
 gedit/gedit-file-chooser.c             | 69 +++++++++++++++++++++++++++++++++-
 gedit/gedit-file-chooser.h             |  9 +++++
 6 files changed, 112 insertions(+), 68 deletions(-)
---
diff --git a/gedit/gedit-commands-file.c b/gedit/gedit-commands-file.c
index 21a128efc..7be091f73 100644
--- a/gedit/gedit-commands-file.c
+++ b/gedit/gedit-commands-file.c
@@ -421,7 +421,8 @@ _gedit_cmd_file_open (GSimpleAction *action,
        {
                const gchar *folder_uri;
 
-               _gedit_file_chooser_open_set_transient_for (file_chooser, GTK_WINDOW (window));
+               _gedit_file_chooser_set_transient_for (GEDIT_FILE_CHOOSER (file_chooser),
+                                                      GTK_WINDOW (window));
 
                folder_uri = _gedit_window_get_file_chooser_folder_uri (window, GTK_FILE_CHOOSER_ACTION_OPEN);
                if (folder_uri != NULL)
@@ -435,7 +436,7 @@ _gedit_cmd_file_open (GSimpleAction *action,
                          G_CALLBACK (file_chooser_open_done_cb),
                          window);
 
-       _gedit_file_chooser_open_show (file_chooser);
+       _gedit_file_chooser_show (GEDIT_FILE_CHOOSER (file_chooser));
 }
 
 void
diff --git a/gedit/gedit-file-chooser-open-native.c b/gedit/gedit-file-chooser-open-native.c
index da2c3eb63..b98b6656f 100644
--- a/gedit/gedit-file-chooser-open-native.c
+++ b/gedit/gedit-file-chooser-open-native.c
@@ -18,6 +18,7 @@
  */
 
 #include "gedit-file-chooser-open-native.h"
+#include <glib/gi18n.h>
 
 struct _GeditFileChooserOpenNativePrivate
 {
@@ -26,6 +27,14 @@ struct _GeditFileChooserOpenNativePrivate
 
 G_DEFINE_TYPE_WITH_PRIVATE (GeditFileChooserOpenNative, _gedit_file_chooser_open_native, 
GEDIT_TYPE_FILE_CHOOSER_OPEN)
 
+#if 0
+static GtkNativeDialog *
+get_native_dialog (GeditFileChooserOpenNative *chooser)
+{
+       return GTK_NATIVE_DIALOG (_gedit_file_chooser_get_gtk_file_chooser (GEDIT_FILE_CHOOSER (chooser)));
+}
+#endif
+
 static void
 _gedit_file_chooser_open_native_dispose (GObject *object)
 {
@@ -33,12 +42,30 @@ _gedit_file_chooser_open_native_dispose (GObject *object)
        G_OBJECT_CLASS (_gedit_file_chooser_open_native_parent_class)->dispose (object);
 }
 
+static GtkFileChooser *
+chooser_create_gtk_file_chooser (GeditFileChooser *chooser)
+{
+       GtkFileChooserNative *native_chooser;
+
+       /* Translators: "Open Files" is the title of the file chooser window. */
+       native_chooser = gtk_file_chooser_native_new (C_("window title", "Open Files"),
+                                                     NULL,
+                                                     GTK_FILE_CHOOSER_ACTION_OPEN,
+                                                     NULL,
+                                                     NULL);
+
+       return GTK_FILE_CHOOSER (native_chooser);
+}
+
 static void
 _gedit_file_chooser_open_native_class_init (GeditFileChooserOpenNativeClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GeditFileChooserClass *chooser_class = GEDIT_FILE_CHOOSER_CLASS (klass);
 
        object_class->dispose = _gedit_file_chooser_open_native_dispose;
+
+       chooser_class->create_gtk_file_chooser = chooser_create_gtk_file_chooser;
 }
 
 static void
diff --git a/gedit/gedit-file-chooser-open.c b/gedit/gedit-file-chooser-open.c
index 1b8c032bd..7e39cb867 100644
--- a/gedit/gedit-file-chooser-open.c
+++ b/gedit/gedit-file-chooser-open.c
@@ -18,8 +18,6 @@
  */
 
 #include "gedit-file-chooser-open.h"
-#include <glib/gi18n.h>
-#include "gedit-file-chooser.h"
 
 struct _GeditFileChooserOpenPrivate
 {
@@ -42,12 +40,6 @@ get_gtk_file_chooser (GeditFileChooserOpen *chooser)
        return _gedit_file_chooser_get_gtk_file_chooser (GEDIT_FILE_CHOOSER (chooser));
 }
 
-static GtkNativeDialog *
-get_native_dialog (GeditFileChooserOpen *chooser)
-{
-       return GTK_NATIVE_DIALOG (get_gtk_file_chooser (chooser));
-}
-
 static void
 _gedit_file_chooser_open_class_init (GeditFileChooserOpenClass *klass)
 {
@@ -66,7 +58,7 @@ _gedit_file_chooser_open_class_init (GeditFileChooserOpenClass *klass)
 }
 
 static void
-response_cb (GtkFileChooserNative *chooser_native,
+response_cb (GtkFileChooser       *gtk_chooser,
             gint                  response_id,
             GeditFileChooserOpen *chooser)
 {
@@ -79,41 +71,14 @@ response_cb (GtkFileChooserNative *chooser_native,
 static void
 _gedit_file_chooser_open_init (GeditFileChooserOpen *chooser)
 {
-       GtkFileChooserNative *native_chooser;
+       GtkFileChooser *gtk_chooser;
 
        chooser->priv = _gedit_file_chooser_open_get_instance_private (chooser);
 
-       /* Translators: "Open Files" is the title of the file chooser window. */
-       native_chooser = gtk_file_chooser_native_new (C_("window title", "Open Files"),
-                                                     NULL,
-                                                     GTK_FILE_CHOOSER_ACTION_OPEN,
-                                                     NULL,
-                                                     NULL);
-
-       /* Set the dialog as modal. It's a workaround for this bug:
-        * https://gitlab.gnome.org/GNOME/gtk/issues/2824
-        * "GtkNativeDialog: non-modal and gtk_native_dialog_show(), doesn't
-        * present the window"
-        *
-        * - Drag-and-drop files from the file chooser to the GeditWindow: OK,
-        *   it still works.
-        * - Other main windows not being "blocked"/insensitive
-        *   (GtkWindowGroup): OK, calling gtk_native_dialog_set_transient_for()
-        *   does the right thing.
-        *
-        * Even if the above GTK bug is fixed, the file chooser can be kept
-        * modal, except if there was a good reason for not being modal (what
-        * reason(s)?).
-        */
-       gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (native_chooser), TRUE);
-
-       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (native_chooser), FALSE);
-       gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (native_chooser), TRUE);
+       gtk_chooser = get_gtk_file_chooser (chooser);
+       gtk_file_chooser_set_select_multiple (gtk_chooser, TRUE);
 
-       _gedit_file_chooser_set_gtk_file_chooser (GEDIT_FILE_CHOOSER (chooser),
-                                                 GTK_FILE_CHOOSER (native_chooser));
-
-       g_signal_connect (native_chooser,
+       g_signal_connect (gtk_chooser,
                          "response",
                          G_CALLBACK (response_cb),
                          chooser);
@@ -125,24 +90,6 @@ _gedit_file_chooser_open_new (void)
        return g_object_new (GEDIT_TYPE_FILE_CHOOSER_OPEN, NULL);
 }
 
-void
-_gedit_file_chooser_open_set_transient_for (GeditFileChooserOpen *chooser,
-                                           GtkWindow            *parent)
-{
-       g_return_if_fail (GEDIT_IS_FILE_CHOOSER_OPEN (chooser));
-       g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
-
-       gtk_native_dialog_set_transient_for (get_native_dialog (chooser), parent);
-}
-
-void
-_gedit_file_chooser_open_show (GeditFileChooserOpen *chooser)
-{
-       g_return_if_fail (GEDIT_IS_FILE_CHOOSER_OPEN (chooser));
-
-       gtk_native_dialog_show (get_native_dialog (chooser));
-}
-
 GSList *
 _gedit_file_chooser_open_get_files (GeditFileChooserOpen *chooser)
 {
diff --git a/gedit/gedit-file-chooser-open.h b/gedit/gedit-file-chooser-open.h
index 1c27106da..6c233c455 100644
--- a/gedit/gedit-file-chooser-open.h
+++ b/gedit/gedit-file-chooser-open.h
@@ -54,13 +54,6 @@ GType                        _gedit_file_chooser_open_get_type               (void);
 G_GNUC_INTERNAL
 GeditFileChooserOpen * _gedit_file_chooser_open_new                    (void);
 
-G_GNUC_INTERNAL
-void                   _gedit_file_chooser_open_set_transient_for      (GeditFileChooserOpen *chooser,
-                                                                        GtkWindow            *parent);
-
-G_GNUC_INTERNAL
-void                   _gedit_file_chooser_open_show                   (GeditFileChooserOpen *chooser);
-
 G_GNUC_INTERNAL
 GSList *               _gedit_file_chooser_open_get_files              (GeditFileChooserOpen *chooser);
 
diff --git a/gedit/gedit-file-chooser.c b/gedit/gedit-file-chooser.c
index 18d559aa8..8f6f36987 100644
--- a/gedit/gedit-file-chooser.c
+++ b/gedit/gedit-file-chooser.c
@@ -572,10 +572,46 @@ _gedit_file_chooser_class_init (GeditFileChooserClass *klass)
        object_class->dispose = _gedit_file_chooser_dispose;
 }
 
+/* Set the dialog as modal. It's a workaround for this bug:
+ * https://gitlab.gnome.org/GNOME/gtk/issues/2824
+ * "GtkNativeDialog: non-modal and gtk_native_dialog_show(), doesn't present the
+ * window"
+ *
+ * - For opening files: drag-and-drop files from the file chooser to the
+ *   GeditWindow: OK, it still works.
+ * - Other main windows not being "blocked"/insensitive (GtkWindowGroup): OK,
+ *   calling gtk_native_dialog_set_transient_for() does the right thing.
+ *
+ * Even if the above GTK bug is fixed, the file chooser can be kept modal,
+ * except if there was a good reason for not being modal (what reason(s)?).
+ */
+static void
+set_modal (GeditFileChooser *chooser)
+{
+       if (GTK_IS_NATIVE_DIALOG (chooser->priv->gtk_chooser))
+       {
+               gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (chooser->priv->gtk_chooser), TRUE);
+       }
+       else
+       {
+               g_warn_if_reached ();
+       }
+}
+
 static void
 _gedit_file_chooser_init (GeditFileChooser *chooser)
 {
+       GeditFileChooserClass *klass;
+
        chooser->priv = _gedit_file_chooser_get_instance_private (chooser);
+
+       klass = GEDIT_FILE_CHOOSER_GET_CLASS (chooser);
+       if (klass->create_gtk_file_chooser != NULL)
+       {
+               _gedit_file_chooser_set_gtk_file_chooser (chooser, klass->create_gtk_file_chooser (chooser));
+               set_modal (chooser);
+               gtk_file_chooser_set_local_only (chooser->priv->gtk_chooser, FALSE);
+       }
 }
 
 GeditFileChooser *
@@ -593,7 +629,6 @@ _gedit_file_chooser_set_gtk_file_chooser (GeditFileChooser *chooser,
        g_return_if_fail (chooser->priv->gtk_chooser == NULL);
 
        chooser->priv->gtk_chooser = g_object_ref (gtk_chooser);
-
        setup_filters (chooser);
 }
 
@@ -603,3 +638,35 @@ _gedit_file_chooser_get_gtk_file_chooser (GeditFileChooser *chooser)
        g_return_val_if_fail (GEDIT_IS_FILE_CHOOSER (chooser), NULL);
        return chooser->priv->gtk_chooser;
 }
+
+void
+_gedit_file_chooser_set_transient_for (GeditFileChooser *chooser,
+                                      GtkWindow        *parent)
+{
+       g_return_if_fail (GEDIT_IS_FILE_CHOOSER (chooser));
+       g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
+
+       if (GTK_IS_NATIVE_DIALOG (chooser->priv->gtk_chooser))
+       {
+               gtk_native_dialog_set_transient_for (GTK_NATIVE_DIALOG (chooser->priv->gtk_chooser), parent);
+       }
+       else
+       {
+               g_warn_if_reached ();
+       }
+}
+
+void
+_gedit_file_chooser_show (GeditFileChooser *chooser)
+{
+       g_return_if_fail (GEDIT_IS_FILE_CHOOSER (chooser));
+
+       if (GTK_IS_NATIVE_DIALOG (chooser->priv->gtk_chooser))
+       {
+               gtk_native_dialog_show (GTK_NATIVE_DIALOG (chooser->priv->gtk_chooser));
+       }
+       else
+       {
+               g_warn_if_reached ();
+       }
+}
diff --git a/gedit/gedit-file-chooser.h b/gedit/gedit-file-chooser.h
index 1ae68736a..a1b6f30d5 100644
--- a/gedit/gedit-file-chooser.h
+++ b/gedit/gedit-file-chooser.h
@@ -45,6 +45,8 @@ struct _GeditFileChooser
 struct _GeditFileChooserClass
 {
        GObjectClass parent_class;
+
+       GtkFileChooser *        (* create_gtk_file_chooser)     (GeditFileChooser *chooser);
 };
 
 G_GNUC_INTERNAL
@@ -60,6 +62,13 @@ void                 _gedit_file_chooser_set_gtk_file_chooser        (GeditFileChooser 
*chooser,
 G_GNUC_INTERNAL
 GtkFileChooser *       _gedit_file_chooser_get_gtk_file_chooser        (GeditFileChooser *chooser);
 
+G_GNUC_INTERNAL
+void                   _gedit_file_chooser_set_transient_for           (GeditFileChooser *chooser,
+                                                                        GtkWindow        *parent);
+
+G_GNUC_INTERNAL
+void                   _gedit_file_chooser_show                        (GeditFileChooser *chooser);
+
 G_END_DECLS
 
 #endif /* GEDIT_FILE_CHOOSER_H */


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