[tepl] AppWindow: implement win.tepl-open GAction



commit d99e4ef3a6a1e741a844c5433a34ed3539335503
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Oct 5 13:50:53 2017 +0200

    AppWindow: implement win.tepl-open GAction

 tepl/tepl-application-window.c |   62 ++++++++++++++++++++++++++++++++++++++++
 tepl/tepl-application.c        |    3 ++
 2 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/tepl/tepl-application-window.c b/tepl/tepl-application-window.c
index 6c0d477..c6c2deb 100644
--- a/tepl/tepl-application-window.c
+++ b/tepl/tepl-application-window.c
@@ -22,6 +22,7 @@
 #include <amtk/amtk.h>
 #include <glib/gi18n-lib.h>
 #include "tepl-abstract-factory.h"
+#include "tepl-application.h"
 #include "tepl-buffer.h"
 #include "tepl-tab-group.h"
 #include "tepl-view.h"
@@ -52,6 +53,7 @@
  *
  * - `"win.tepl-new-file"`: creates a new #TeplTab, appends it with
  *   tepl_tab_group_append_tab() and set it as the active tab.
+ * - `"win.tepl-open"`: shows a #GtkFileChooser to open a new file.
  *
  * ## For the Edit menu
  *
@@ -123,6 +125,65 @@ new_file_cb (GSimpleAction *action,
 }
 
 static void
+open_file_chooser_response_cb (GtkFileChooserDialog  *file_chooser_dialog,
+                              gint                   response_id,
+                              TeplApplicationWindow *tepl_window)
+{
+       if (response_id == GTK_RESPONSE_ACCEPT)
+       {
+               GFile *file;
+               GtkApplication *gtk_app;
+               TeplApplication *tepl_app;
+
+               file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_chooser_dialog));
+
+               gtk_app = gtk_window_get_application (GTK_WINDOW (tepl_window->priv->gtk_window));
+               tepl_app = tepl_application_get_from_gtk_application (gtk_app);
+               tepl_application_open_simple (tepl_app, file);
+
+               g_object_unref (file);
+       }
+
+       gtk_widget_destroy (GTK_WIDGET (file_chooser_dialog));
+}
+
+static void
+open_cb (GSimpleAction *open_action,
+        GVariant      *parameter,
+        gpointer       user_data)
+{
+       TeplApplicationWindow *tepl_window = TEPL_APPLICATION_WINDOW (user_data);
+       GtkWidget *file_chooser_dialog;
+
+       /* Create a GtkFileChooserDialog, not a GtkFileChooserNative, because
+        * with GtkFileChooserNative the GFile that we obtain (in flatpak)
+        * doesn't have the real path to the file, so it would screw up some
+        * features for text editors:
+        * - showing the directory in parentheses in the window title, or in the
+        *   tab tooltip;
+        * - opening a recent file.
+        * Basically everywhere where the directory is shown.
+        */
+       file_chooser_dialog = gtk_file_chooser_dialog_new (_("Open File"),
+                                                          GTK_WINDOW (tepl_window->priv->gtk_window),
+                                                          GTK_FILE_CHOOSER_ACTION_OPEN,
+                                                          _("_Cancel"), GTK_RESPONSE_CANCEL,
+                                                          _("_Open"), GTK_RESPONSE_ACCEPT,
+                                                          NULL);
+
+       gtk_dialog_set_default_response (GTK_DIALOG (file_chooser_dialog), GTK_RESPONSE_ACCEPT);
+       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (file_chooser_dialog), FALSE);
+
+       g_signal_connect_object (file_chooser_dialog,
+                                "response",
+                                G_CALLBACK (open_file_chooser_response_cb),
+                                tepl_window,
+                                0);
+
+       gtk_widget_show (file_chooser_dialog);
+}
+
+static void
 undo_cb (GSimpleAction *action,
         GVariant      *parameter,
         gpointer       user_data)
@@ -501,6 +562,7 @@ add_actions (TeplApplicationWindow *tepl_window)
        const GActionEntry entries[] = {
                /* File menu */
                { "tepl-new-file", new_file_cb },
+               { "tepl-open", open_cb },
 
                /* Edit menu */
                { "tepl-undo", undo_cb },
diff --git a/tepl/tepl-application.c b/tepl/tepl-application.c
index 507e4e0..d90bb7b 100644
--- a/tepl/tepl-application.c
+++ b/tepl/tepl-application.c
@@ -71,6 +71,9 @@ init_tepl_action_info_store (TeplApplication *tepl_app)
                { "win.tepl-new-file", "document-new", N_("_New"), "<Control>n",
                  N_("New file") },
 
+               { "win.tepl-open", "document-open", N_("_Open"), "<Control>o",
+                 N_("Open a file") },
+
                /* Edit menu */
 
                { "win.tepl-undo", "edit-undo", N_("_Undo"), "<Control>z",


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