[recipes] Implement copy and paste actions for OS X



commit 8dc18a917b40ec96251b71a3975e032e02449f07
Author: Matthias Clasen <mclasen Matthiass-MBP home>
Date:   Mon Mar 6 21:32:15 2017 -0500

    Implement copy and paste actions for OS X
    
    Make Copy and Paste actions available
    from the Edit menu on OS X.

 src/gr-app.c    |   19 +++++++++++++++----
 src/gr-window.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 66 insertions(+), 6 deletions(-)
---
diff --git a/src/gr-app.c b/src/gr-app.c
index e85769d..b45040b 100644
--- a/src/gr-app.c
+++ b/src/gr-app.c
@@ -335,8 +335,16 @@ load_application_css (GrApp *app)
 static void
 gr_app_startup (GApplication *app)
 {
-        const gchar *quit_accels[2] = { "<Primary>Q", NULL };
-        const gchar *search_accels[2] = { "<Primary>F", NULL };
+       struct {
+               const char *detailed_action;
+               const char *accelerators[2];
+       } accels[] = {
+               { "app.quit", { "<Primary>q", NULL } },
+               { "app.search('')", { "<Primary>f", NULL } },
+               { "win.copy", { "<Primary>c", NULL } },
+               { "win.paste", { "<Primary>v", NULL } },
+       };
+       int i;
 
         G_APPLICATION_CLASS (gr_app_parent_class)->startup (app);
 
@@ -353,8 +361,11 @@ gr_app_startup (GApplication *app)
         }
 #endif
 
-        gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.quit", quit_accels);
-        gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.search('')", search_accels);
+       for (i = 0; i < G_N_ELEMENTS (accels); i++) {
+               gtk_application_set_accels_for_action (GTK_APPLICATION (app),
+                                                      accels[i].detailed_action,
+                                                      accels[i].accelerators);
+       }
 
 #ifdef GDK_WINDOWING_QUARTZ
         if (GDK_IS_QUARTZ_DISPLAY (gdk_display_get_default ())) {
diff --git a/src/gr-window.c b/src/gr-window.c
index 1fc4579..4005370 100644
--- a/src/gr-window.c
+++ b/src/gr-window.c
@@ -692,11 +692,60 @@ gr_window_class_init (GrWindowClass *klass)
         gtk_widget_class_bind_template_callback (widget_class, shopping_title_changed);
 }
 
+static GtkClipboard *
+get_clipboard (GtkWidget *widget)
+{
+        return gtk_widget_get_clipboard (widget, gdk_atom_intern_static_string ("CLIPBOARD"));
+}
+
+static void
+copy_activated (GSimpleAction *action,
+                GVariant      *parameter,
+                gpointer       user_data)
+{
+        GtkWindow *window = GTK_WINDOW (user_data);
+        GtkWidget *focus;
+
+        focus = gtk_window_get_focus (window);
+        if (GTK_IS_TEXT_VIEW (focus))
+                gtk_text_buffer_copy_clipboard (gtk_text_view_get_buffer (GTK_TEXT_VIEW (focus)),
+                                                get_clipboard (focus));
+        else if (GTK_IS_ENTRY (focus))
+                gtk_editable_copy_clipboard (GTK_EDITABLE (focus));
+}
+
+static void
+paste_activated (GSimpleAction *action,
+                 GVariant      *parameter,
+                 gpointer       user_data)
+{
+        GtkWindow *window = GTK_WINDOW (user_data);
+        GtkWidget *focus;
+
+        focus = gtk_window_get_focus (window);
+        if (GTK_IS_TEXT_VIEW (focus))
+                gtk_text_buffer_paste_clipboard (gtk_text_view_get_buffer (GTK_TEXT_VIEW (focus)),
+                                                 get_clipboard (focus),
+                                                 NULL,
+                                                 TRUE);
+        else if (GTK_IS_ENTRY (focus))
+                gtk_editable_paste_clipboard (GTK_EDITABLE (focus));
+}
+
+static GActionEntry entries[] = {
+        { "copy", copy_activated, NULL, NULL, NULL },
+        { "paste", paste_activated, NULL, NULL, NULL }
+};
+
 static void
 gr_window_init (GrWindow *self)
 {
         gtk_widget_init_template (GTK_WIDGET (self));
         self->back_entry_stack = g_queue_new ();
+
+        g_action_map_add_action_entries (G_ACTION_MAP (self),
+                                         entries, G_N_ELEMENTS (entries),
+                                         self);
 }
 
 void
@@ -1204,8 +1253,8 @@ gr_window_show_news (GrWindow *window)
         dialog = GTK_WINDOW (gtk_builder_get_object (builder, "dialog"));
         gtk_window_set_transient_for (dialog, GTK_WINDOW (window));
 
-       gtk_widget_realize (GTK_WIDGET (dialog));
-       gdk_window_set_functions (gtk_widget_get_window (GTK_WIDGET (dialog)),
+        gtk_widget_realize (GTK_WIDGET (dialog));
+        gdk_window_set_functions (gtk_widget_get_window (GTK_WIDGET (dialog)),
                                   GDK_FUNC_ALL | GDK_FUNC_MINIMIZE | GDK_FUNC_MAXIMIZE);
 
         box = GTK_WIDGET (gtk_builder_get_object (builder, "box"));


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