[gnome-devel-docs/wip/develguide-gtkapp] Add menu example to GtkApplication guide



commit 700d5012d64266c36a644f4b2c3544e05d0a96c1
Author: David King <amigadave amigadave com>
Date:   Thu May 1 19:21:06 2014 +0200

    Add menu example to GtkApplication guide

 devel-guide/C/examples/gtkapp-menu.c   |   70 ++++++++++++++++++++++++++++++++
 devel-guide/C/gtkapplication-menu.page |   46 +++++++++++++++++++++
 2 files changed, 116 insertions(+), 0 deletions(-)
---
diff --git a/devel-guide/C/examples/gtkapp-menu.c b/devel-guide/C/examples/gtkapp-menu.c
new file mode 100644
index 0000000..5da2f30
--- /dev/null
+++ b/devel-guide/C/examples/gtkapp-menu.c
@@ -0,0 +1,70 @@
+/* gtkapp-menu.c: Build with:
+ * gcc `pkg-config --cflags gtk+-3.0` -o gtkapp-menu gtkapp-menu.c `pkg-config --libs gtk+-3.0`
+ */
+#include <gtk/gtk.h>
+
+static void
+on_activate (GApplication *app,
+             gpointer user_data)
+{
+    GtkWidget *window;
+
+    /* Application windows are associated with a GtkApplication. */
+    window = gtk_application_window_new (GTK_APPLICATION (app));
+    gtk_widget_show (window);
+}
+
+static void
+on_startup (GApplication *app,
+            gpointer user_data)
+{
+    GMenu *menu;
+    const gchar * const accels[] = { "<Primary>H", NULL };
+
+    /* Accelerators must be added after the application is registered, so
+     * the startup handler is a good place. */
+    gtk_application_set_accels_for_action (GTK_APPLICATION (app),
+                                           "app.hello-world",
+                                           accels);
+
+    /* Add an item to the application menu. */
+    menu = g_menu_new ();
+    g_menu_append (menu, "Hello world!", "app.hello-world");
+    gtk_application_set_app_menu (GTK_APPLICATION (app), G_MENU_MODEL (menu));
+}
+
+static void
+on_hello_world (GSimpleAction *action,
+                GVariant *parameter,
+                gpointer user_data)
+{
+    g_print ("%s\n", "Hello world!");
+}
+
+static GActionEntry actions[] = {
+    { "hello-world", on_hello_world }
+};
+
+int
+main (int argc, char **argv)
+{
+    GtkApplication *app;
+    GtkWidget *window;
+    int result;
+
+    /* com.example.MyApp is a unique application identifier. */
+    app = gtk_application_new ("com.example.MyApp", G_APPLICATION_FLAGS_NONE);
+
+    /* Add the actions to the application. */
+    g_action_map_add_action_entries (G_ACTION_MAP (app), actions,
+                                     G_N_ELEMENTS (actions), app);
+
+    g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
+    g_signal_connect (app, "startup", G_CALLBACK (on_startup), NULL);
+
+    result = g_application_run (G_APPLICATION (app), argc, argv);
+
+    g_object_unref (app);
+
+    return result;
+}
diff --git a/devel-guide/C/gtkapplication-menu.page b/devel-guide/C/gtkapplication-menu.page
new file mode 100644
index 0000000..7039238
--- /dev/null
+++ b/devel-guide/C/gtkapplication-menu.page
@@ -0,0 +1,46 @@
+<page xmlns="http://projectmallard.org/1.0/";
+      xmlns:its="http://www.w3.org/2005/11/its";
+      type="topic" style="task"
+      id="gtkapplication-menu">
+
+  <info>
+    <link type="next" xref="gtkapplication-"/>
+    <revision pkgversion="3.14" date="2014-05-01" status="draft"/>
+
+    <credit type="author">
+      <name>David King</name>
+      <email its:translate="no">davidk gnome org</email>
+      <years>2014</years>
+    </credit>
+
+    <include href="legal.xml" xmlns="http://www.w3.org/2001/XInclude"/>
+
+  </info>
+
+  <title>Application menus</title>
+
+  <links type="series" style="floatend">
+    <title>GtkApplication</title>
+  </links>
+
+  <p>Add an application-wide menu.</p>
+  
+  <!-- Link to HowDoI/ApplicationMenu? -->
+  <p>GNOME Shell shows menus from applications in the top bar. Using this
+  technique, you can improve consistency with other GNOME applications.</p>
+
+  <p>A simple GtkApplication with an app menu is as follows:</p>
+  <example>
+    <listing> 
+      <title><file>gtkapp-menu.c</file></title>
+<code mime="text/x-csrc">
+<include href="examples/gtkapp-menu.c" parse="text" xmlns="http://www.w3.org/2001/XInclude"/>
+</code>
+    </listing>
+  </example>
+
+  <p>The example creates an application with a window. When the user presses
+  <keyseq><key>Ctrl</key><key>H</key></keyseq>, or selects "Hello world!" from
+  the app menu, "Hello world!" is printed to the terminal.</p>
+ 
+</page>


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