From db7115d841fc148bc9c146a7a196e31fdafec195 Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Tue, 9 Jul 2013 23:51:40 -0400 Subject: [PATCH] bloatpad: test dynamic accels ...this stuff will be in the action description soon. --- examples/bloatpad.c | 120 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 5 deletions(-) diff --git a/examples/bloatpad.c b/examples/bloatpad.c index b7664afa82..0789ea920e 100644 --- a/examples/bloatpad.c +++ b/examples/bloatpad.c @@ -268,6 +268,80 @@ quit_activated (GSimpleAction *action, g_application_quit (app); } +static void +combo_changed (GtkComboBox *combo, + gpointer user_data) +{ + GtkEntry *entry = g_object_get_data (user_data, "entry"); + const gchar *action; + gchar **accels; + gchar *str; + + action = gtk_combo_box_get_active_id (combo); + + if (!action) + return; + + accels = gtk_application_get_accels_for_action (gtk_window_get_application (user_data), action); + str = g_strjoinv (",", accels); + g_strfreev (accels); + + gtk_entry_set_text (entry, str); +} + +static void +response (GtkDialog *dialog, + guint response_id, + gpointer user_data) +{ + GtkEntry *entry = g_object_get_data (user_data, "entry"); + GtkComboBox *combo = g_object_get_data (user_data, "combo"); + const gchar *action; + const gchar *str; + gchar **accels; + + action = gtk_combo_box_get_active_id (combo); + + if (!action) + return; + + str = gtk_entry_get_text (entry); + accels = g_strsplit (str, ",", 0); + + gtk_application_set_accels_for_action (gtk_window_get_application (user_data), action, (const gchar **) accels); + g_strfreev (accels); +} + +static void +edit_accels (GSimpleAction *action, + GVariant *parameter, + gpointer user_data) +{ + GtkApplication *app = user_data; + GtkWidget *combo; + GtkWidget *entry; + gchar **actions; + GtkWidget *dialog; + gint i; + + dialog = gtk_dialog_new (); + gtk_window_set_application (GTK_WINDOW (dialog), app); + actions = gtk_application_list_action_descriptions (app); + combo = gtk_combo_box_text_new (); + gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), combo); + for (i = 0; actions[i]; i++) + gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (combo), actions[i], actions[i]); + g_signal_connect (combo, "changed", G_CALLBACK (combo_changed), dialog); + entry = gtk_entry_new (); + gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), entry); + gtk_dialog_add_button (GTK_DIALOG (dialog), "Set", GTK_RESPONSE_APPLY); + g_signal_connect (dialog, "response", G_CALLBACK (response), dialog); + g_object_set_data (G_OBJECT (dialog), "combo", combo); + g_object_set_data (G_OBJECT (dialog), "entry", entry); + + gtk_widget_show_all (dialog); +} + static gboolean update_time (gpointer user_data) { @@ -320,9 +394,32 @@ static GActionEntry app_entries[] = { { "new", new_activated, NULL, NULL, NULL }, { "about", about_activated, NULL, NULL, NULL }, { "quit", quit_activated, NULL, NULL, NULL }, + { "edit-accels", edit_accels }, { "time-active", NULL, NULL, "false", time_active_changed } }; +static void +dump_accels (GtkApplication *app) +{ + gchar **actions; + gint i; + + actions = gtk_application_list_action_descriptions (app); + for (i = 0; actions[i]; i++) + { + gchar **accels; + gchar *str; + + accels = gtk_application_get_accels_for_action (app, actions[i]); + + str = g_strjoinv (",", accels); + g_print ("%s -> %s\n", actions[i], str); + g_strfreev (accels); + g_free (str); + } + g_strfreev (actions); +} + static void bloat_pad_startup (GApplication *application) { @@ -342,7 +439,6 @@ bloat_pad_startup (GApplication *application) " " " _New Window" " app.new" - " <Primary>n" " " " " "
" @@ -355,7 +451,6 @@ bloat_pad_startup (GApplication *application) " " " _Quit" " app.quit" - " <Primary>q" " " "
" " " @@ -366,12 +461,16 @@ bloat_pad_startup (GApplication *application) " " " _Copy" " win.copy" - " <Primary>c" " " " " " _Paste" " win.paste" - " <Primary>v" + " " + " " + "
" + " " + " Accelerators..." + " app.edit-accels" " " "
" " " @@ -381,7 +480,6 @@ bloat_pad_startup (GApplication *application) " " " _Fullscreen" " win.fullscreen" - " F11" " " " " " _Look Busy" @@ -397,6 +495,18 @@ bloat_pad_startup (GApplication *application) "", -1, NULL); gtk_application_set_app_menu (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"))); gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "menubar"))); + gtk_application_add_accelerator (GTK_APPLICATION (application), "n", "app.new", NULL); + gtk_application_add_accelerator (GTK_APPLICATION (application), "q", "app.quit", NULL); + gtk_application_add_accelerator (GTK_APPLICATION (application), "c", "win.copy", NULL); + gtk_application_add_accelerator (GTK_APPLICATION (application), "p", "win.paste", NULL); + gtk_application_add_accelerator (GTK_APPLICATION (application), "l", "win.justify", g_variant_new_string ("left")); + gtk_application_add_accelerator (GTK_APPLICATION (application), "m", "win.justify", g_variant_new_string ("center")); + gtk_application_add_accelerator (GTK_APPLICATION (application), "r", "win.justify", g_variant_new_string ("right")); + + const gchar *new_accels[] = { "n", "t", NULL }; + gtk_application_set_accels_for_action (GTK_APPLICATION (application), "app.new", new_accels); + + dump_accels (GTK_APPLICATION (application)); //gtk_application_set_menubar (GTK_APPLICATION (application), G_MENU_MODEL (gtk_builder_get_object (builder, "app-menu"))); bloatpad->time = G_MENU (gtk_builder_get_object (builder, "time-menu")); g_object_unref (builder);