[gedit/zbrown/deteplification-src: 377/633] FileChooserOpen: start to create class




commit ba3b5e5d922cf60278948ce0c5c8a0f0675e2cf7
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jun 5 13:17:52 2020 +0200

    FileChooserOpen: start to create class
    
    - The goal is to use a GtkFileChooserNative, and no longer use
    GeditFileChooserDialogOSX.
    - Have a FileChooser specifically for opening files. For the 'Save As'
    another class will be created. It doesn't prevent from having common
    code between the two. By splitting Open and Save in separate classes,
    the code will hopefully be easier to understand.

 gedit/gedit-file-chooser-open.c | 162 ++++++++++++++++++++++++++++++++++++++++
 gedit/gedit-file-chooser-open.h |  75 +++++++++++++++++++
 gedit/meson.build               |   2 +
 po/POTFILES.in                  |   1 +
 4 files changed, 240 insertions(+)
---
diff --git a/gedit/gedit-file-chooser-open.c b/gedit/gedit-file-chooser-open.c
new file mode 100644
index 000000000..18bd1c453
--- /dev/null
+++ b/gedit/gedit-file-chooser-open.c
@@ -0,0 +1,162 @@
+/*
+ * This file is part of gedit
+ *
+ * Copyright (C) 2020 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gedit-file-chooser-open.h"
+#include <glib/gi18n.h>
+
+struct _GeditFileChooserOpenPrivate
+{
+       GtkFileChooserNative *chooser_native;
+};
+
+enum
+{
+       SIGNAL_DONE,
+       N_SIGNALS
+};
+
+static guint signals[N_SIGNALS];
+
+G_DEFINE_TYPE_WITH_PRIVATE (GeditFileChooserOpen, _gedit_file_chooser_open, G_TYPE_OBJECT)
+
+static void
+_gedit_file_chooser_open_dispose (GObject *object)
+{
+       GeditFileChooserOpen *chooser = GEDIT_FILE_CHOOSER_OPEN (object);
+
+       g_clear_object (&chooser->priv->chooser_native);
+
+       G_OBJECT_CLASS (_gedit_file_chooser_open_parent_class)->dispose (object);
+}
+
+static void
+_gedit_file_chooser_open_class_init (GeditFileChooserOpenClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       object_class->dispose = _gedit_file_chooser_open_dispose;
+
+       /*
+        * GeditFileChooserOpen::done:
+        * @chooser: the #GeditFileChooserOpen emitting the signal.
+        * @accept: whether the response code is %GTK_RESPONSE_ACCEPT.
+        */
+       signals[SIGNAL_DONE] =
+               g_signal_new ("done",
+                             G_TYPE_FROM_CLASS (klass),
+                             G_SIGNAL_RUN_FIRST,
+                             0, NULL, NULL, NULL,
+                             G_TYPE_NONE,
+                             1, G_TYPE_BOOLEAN);
+}
+
+static void
+chooser_native_response_cb (GtkFileChooserNative *chooser_native,
+                           gint                  response_id,
+                           GeditFileChooserOpen *chooser)
+{
+       gboolean accept;
+
+       accept = response_id == GTK_RESPONSE_ACCEPT;
+       g_signal_emit (chooser, signals[SIGNAL_DONE], 0, accept);
+}
+
+static void
+_gedit_file_chooser_open_init (GeditFileChooserOpen *chooser)
+{
+       chooser->priv = _gedit_file_chooser_open_get_instance_private (chooser);
+
+       /* Translators: "Open Files" is the title of the file chooser window. */
+       chooser->priv->chooser_native = 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 (chooser->priv->chooser_native), TRUE);
+
+       g_signal_connect (chooser->priv->chooser_native,
+                         "response",
+                         G_CALLBACK (chooser_native_response_cb),
+                         chooser);
+}
+
+GeditFileChooserOpen *
+_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 (GTK_NATIVE_DIALOG (chooser->priv->chooser_native), parent);
+}
+
+void
+_gedit_file_chooser_open_show (GeditFileChooserOpen *chooser)
+{
+       g_return_if_fail (GEDIT_IS_FILE_CHOOSER_OPEN (chooser));
+
+       gtk_native_dialog_show (GTK_NATIVE_DIALOG (chooser->priv->chooser_native));
+}
+
+GSList *
+_gedit_file_chooser_open_get_files (GeditFileChooserOpen *chooser)
+{
+       g_return_val_if_fail (GEDIT_IS_FILE_CHOOSER_OPEN (chooser), NULL);
+
+       return gtk_file_chooser_get_files (GTK_FILE_CHOOSER (chooser->priv->chooser_native));
+}
+
+gchar *
+_gedit_file_chooser_open_get_current_folder_uri (GeditFileChooserOpen *chooser)
+{
+       g_return_val_if_fail (GEDIT_IS_FILE_CHOOSER_OPEN (chooser), NULL);
+
+       return gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (chooser->priv->chooser_native));
+}
+
+void
+_gedit_file_chooser_open_set_current_folder_uri (GeditFileChooserOpen *chooser,
+                                                const gchar          *uri)
+{
+       g_return_if_fail (GEDIT_IS_FILE_CHOOSER_OPEN (chooser));
+
+       gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (chooser->priv->chooser_native), uri);
+}
diff --git a/gedit/gedit-file-chooser-open.h b/gedit/gedit-file-chooser-open.h
new file mode 100644
index 000000000..4305621ee
--- /dev/null
+++ b/gedit/gedit-file-chooser-open.h
@@ -0,0 +1,75 @@
+/*
+ * This file is part of gedit
+ *
+ * Copyright (C) 2020 Sébastien Wilmet <swilmet gnome org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GEDIT_FILE_CHOOSER_OPEN_H
+#define GEDIT_FILE_CHOOSER_OPEN_H
+
+#include <gtksourceview/gtksource.h>
+
+G_BEGIN_DECLS
+
+#define GEDIT_TYPE_FILE_CHOOSER_OPEN             (_gedit_file_chooser_open_get_type ())
+#define GEDIT_FILE_CHOOSER_OPEN(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GEDIT_TYPE_FILE_CHOOSER_OPEN, GeditFileChooserOpen))
+#define GEDIT_FILE_CHOOSER_OPEN_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), 
GEDIT_TYPE_FILE_CHOOSER_OPEN, GeditFileChooserOpenClass))
+#define GEDIT_IS_FILE_CHOOSER_OPEN(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GEDIT_TYPE_FILE_CHOOSER_OPEN))
+#define GEDIT_IS_FILE_CHOOSER_OPEN_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), 
GEDIT_TYPE_FILE_CHOOSER_OPEN))
+#define GEDIT_FILE_CHOOSER_OPEN_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), 
GEDIT_TYPE_FILE_CHOOSER_OPEN, GeditFileChooserOpenClass))
+
+typedef struct _GeditFileChooserOpen         GeditFileChooserOpen;
+typedef struct _GeditFileChooserOpenClass    GeditFileChooserOpenClass;
+typedef struct _GeditFileChooserOpenPrivate  GeditFileChooserOpenPrivate;
+
+struct _GeditFileChooserOpen
+{
+       GObject parent;
+
+       GeditFileChooserOpenPrivate *priv;
+};
+
+struct _GeditFileChooserOpenClass
+{
+       GObjectClass parent_class;
+};
+
+G_GNUC_INTERNAL
+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);
+
+G_GNUC_INTERNAL
+gchar *                        _gedit_file_chooser_open_get_current_folder_uri (GeditFileChooserOpen 
*chooser);
+
+G_GNUC_INTERNAL
+void                   _gedit_file_chooser_open_set_current_folder_uri (GeditFileChooserOpen *chooser,
+                                                                        const gchar          *uri);
+
+G_END_DECLS
+
+#endif /* GEDIT_FILE_CHOOSER_OPEN_H */
diff --git a/gedit/meson.build b/gedit/meson.build
index 1365b5cae..d012cc1c5 100644
--- a/gedit/meson.build
+++ b/gedit/meson.build
@@ -51,6 +51,7 @@ libgedit_private_headers = [
   'gedit-file-chooser-dialog-gtk.h',
   'gedit-file-chooser-dialog.h',
   'gedit-file-chooser-dialog-osx.h',
+  'gedit-file-chooser-open.h',
   'gedit-highlight-mode-dialog.h',
   'gedit-highlight-mode-selector.h',
   'gedit-history-entry.h',
@@ -94,6 +95,7 @@ libgedit_private_sources = [
   'gedit-encodings-dialog.c',
   'gedit-file-chooser-dialog.c',
   'gedit-file-chooser-dialog-gtk.c',
+  'gedit-file-chooser-open.c',
   'gedit-highlight-mode-dialog.c',
   'gedit-highlight-mode-selector.c',
   'gedit-history-entry.c',
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ff2f41710..99a684508 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -18,6 +18,7 @@ gedit/gedit-encoding-items.c
 gedit/gedit-encodings-dialog.c
 gedit/gedit-file-chooser-dialog-gtk.c
 gedit/gedit-file-chooser-dialog-osx.m
+gedit/gedit-file-chooser-open.c
 gedit/gedit-highlight-mode-dialog.c
 gedit/gedit-highlight-mode-selector.c
 gedit/gedit-io-error-info-bar.c


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