summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid King <amigadave@amigadave.com>2015-05-07 10:19:06 +0700
committerDavid King <amigadave@amigadave.com>2015-05-07 10:19:06 +0700
commit298308c90defe4e16805b4a38784fcfd22c7e3de (patch)
treea04538901b9b8bdf85e9cde95629b3d23c7abfd2
parent348a01d86cee6fd90eea413465ec4449e23b3dd8 (diff)
downloadc-gnome-app-298308c90defe4e16805b4a38784fcfd22c7e3de.tar.gz
c-gnome-app-298308c90defe4e16805b4a38784fcfd22c7e3de.tar.xz
c-gnome-app-298308c90defe4e16805b4a38784fcfd22c7e3de.zip
Add actions and handlers for hello world and quit
Create two actions, "hello-world" and "quit", and add them to the application (which is an implementation of GActionMap). Connect the "activate" signal of the actions to handlers. Add an accelerator for each action, so that they can be triggered with a keyboard shortcut.
-rw-r--r--c-gnome-app.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/c-gnome-app.c b/c-gnome-app.c
index fabfece..6d20243 100644
--- a/c-gnome-app.c
+++ b/c-gnome-app.c
@@ -1,14 +1,53 @@
/* Build with "gcc `pkg-config --cflags --libs gtk+-3.0` c-gnome-app.c */
#include <gtk/gtk.h>
+static GtkWidget *window;
+
+static void
+on_hello_world (GSimpleAction *action,
+ GVariant *parameter,
+ gpointer user_data)
+{
+ g_print ("%s\n", "Hello world!");
+}
+
+static void
+on_quit (GSimpleAction *action,
+ GVariant *parameter,
+ gpointer user_data)
+{
+ g_application_quit (G_APPLICATION (user_data));
+}
+
+static const GActionEntry actions[] =
+{
+ { "hello-world", on_hello_world },
+ { "quit", on_quit }
+};
+
static void
on_activate (GApplication *app,
gpointer user_data)
{
- GtkWidget *window;
+ gtk_widget_show_all (window);
+}
+
+static void
+on_startup (GApplication *app,
+ gpointer user_data)
+{
+ const gchar * const hello_world_accel[] = { "<Primary>h", NULL };
+ const gchar * const quit_accel[] = { "<Primary>q", NULL };
+ g_action_map_add_action_entries (G_ACTION_MAP (app), actions,
+ G_N_ELEMENTS (actions), app);
window = gtk_application_window_new (GTK_APPLICATION (app));
- gtk_widget_show_all (window);
+ gtk_application_set_accels_for_action (GTK_APPLICATION (app),
+ "app.hello-world",
+ hello_world_accel);
+ gtk_application_set_accels_for_action (GTK_APPLICATION (app),
+ "app.quit",
+ quit_accel);
}
int
@@ -20,6 +59,7 @@ main (int argc,
app = gtk_application_new ("org.example.CGnome", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
+ g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);