[gnome-devel-docs] tutorials Vala: Complete MenuBar example



commit ceb9c5d12e3f8b857255eb1a90dd551db9ce5655
Author: Tiffany Antopolski <tiffany antopolski gmail com>
Date:   Fri Jun 29 14:15:16 2012 -0400

    tutorials Vala: Complete MenuBar example
    
    Includes UI file, vala code, page file and exercises.

 platform-demos/C/exercises/menubar.exercises |    4 +
 platform-demos/C/media/menubar.png           |  Bin 0 -> 6009 bytes
 platform-demos/C/media/menubar_choices.png   |  Bin 0 -> 16509 bytes
 platform-demos/C/menubar.vala.page           |  199 ++++++++++++++++++++++++++
 platform-demos/C/samples/menubar.ui          |  103 +++++++++++++
 platform-demos/C/samples/menubar.vala        |   99 +++++++++++++
 platform-demos/C/samples/menubar_basis.ui    |   17 +++
 platform-demos/C/samples/menubar_basis.vala  |   33 +++++
 8 files changed, 455 insertions(+), 0 deletions(-)
---
diff --git a/platform-demos/C/exercises/menubar.exercises b/platform-demos/C/exercises/menubar.exercises
new file mode 100644
index 0000000..b8691db
--- /dev/null
+++ b/platform-demos/C/exercises/menubar.exercises
@@ -0,0 +1,4 @@
+<list type="numbered" xmlns="http://projectmallard.org/1.0/";>
+  <item><p>Add an <gui>About</gui> dialog to the <gui>Help</gui> menu.</p></item>
+  <item><p>Redo the application menu (GMenu) example using an XML UI file and Gtk.Builder.</p></item>
+</list>
diff --git a/platform-demos/C/media/menubar.png b/platform-demos/C/media/menubar.png
new file mode 100644
index 0000000..4b94bad
Binary files /dev/null and b/platform-demos/C/media/menubar.png differ
diff --git a/platform-demos/C/media/menubar_choices.png b/platform-demos/C/media/menubar_choices.png
new file mode 100644
index 0000000..9606136
Binary files /dev/null and b/platform-demos/C/media/menubar_choices.png differ
diff --git a/platform-demos/C/menubar.vala.page b/platform-demos/C/menubar.vala.page
new file mode 100644
index 0000000..924bde9
--- /dev/null
+++ b/platform-demos/C/menubar.vala.page
@@ -0,0 +1,199 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<page xmlns="http://projectmallard.org/1.0/";
+      xmlns:xi="http://www.w3.org/2001/XInclude";
+      type="guide" style="task"
+      id="menubar.vala">
+  <info>
+  <link type="guide" xref="beginner.vala#menu-combo-toolbar"/>
+  <link type="seealso" xref="aboutdialog.vala"/>
+  <link type="seealso" xref="gmenu.vala"/>
+    <revision version="0.1" date="2012-05-25" status="draft"/>
+
+    <credit type="author copyright">
+      <name>Tiffany Antopolski</name>
+      <email>tiffany antopolski gmail com</email>
+      <years>2012</years>
+    </credit>
+
+    <desc>A widget which holds GtkMenuItem widgets</desc>
+  </info>
+
+  <title>MenuBar</title>
+  <media type="image" mime="image/png" src="media/menubar.png"/>
+  <p>A MenuBar created using XML and GtkBuilder.</p>
+
+  <links type="section"/>
+
+  <section id="xml"> <title>Create a MenuBar using XML</title>
+   <p>To create the menubar using XML:</p>
+   <steps>
+     <item><p>Create <file>menubar.ui</file> using your favorite text editor.</p></item>
+     <item><p>Enter the following line at the top of the file:</p>
+           <code mime="text"><![CDATA[
+<?xml version="1.0"? encoding="UTF-8"?>]]></code>
+     </item>
+    <item><p>We want to create the interface which will contain our menubar and it's submenus.  Our menubar will contain <gui>File</gui>, <gui>Edit</gui>, <gui>Choices</gui> and <gui>Help</gui> submenus. We add the following XML code to the file:</p>
+    <code mime="text"><xi:include href="samples/menubar_basis.ui" parse="text"><xi:fallback/></xi:include></code>
+     </item>
+     <item><p>Now we will create the .vala file and use GtkBuilder to import the <file>menubar.ui</file> we just created.</p></item>
+   </steps>
+   </section>
+   <section id="basis"> <title>Add the MenuBar to the window using GtkBuilder</title>
+<code mime="text/x-vala"><xi:include href="samples/menubar_basis.vala" parse="text"><xi:fallback/></xi:include></code>
+<p>
+Now, compile the vala file, and run it. The application should look like the picture at the top of this page.</p>
+</section>
+
+
+
+<section id="xml2"> <title>Add items to the menus</title>
+<p>We start off by adding 2 menuitems to the <gui>File</gui> menu: <gui>New</gui> and <gui>Quit</gui>.  We do this by adding a <code>section</code> to the the <code>File</code> submenu with these items. The <file>menubar.ui</file> should look like this  (lines 6 to 13 inclusive comprise the newly added section):</p>
+      <code mime="text" style="numbered"><![CDATA[
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <menu id="menubar">
+    <submenu>
+      <attribute name="label">File</attribute>
+      <section>
+        <item>
+          <attribute name="label">New</attribute>
+        </item>
+        <item>
+          <attribute name ="label">Quit</attribute>
+        </item>
+      </section>
+    </submenu>
+    <submenu>
+      <attribute name="label">Edit</attribute>
+    </submenu>
+    <submenu>
+      <attribute name="label">Choices</attribute>
+    </submenu>
+    <submenu>
+      <attribute name="label">Help</attribute>
+    </submenu>
+  </menu>
+</interface>]]></code>
+
+<p>Following this pattern, you can now add a <code>Copy</code> and a <code>Paste</code> item to the <code>Edit</code> submenu, and an <code>About</code> item to the <code>Help</code> submenu.  We will hold off on adding items to the <link xref="menubar.vala#choices">Choices submenu</link> until further in the tutorial.</p>
+
+<note style="tip"><p>
+  You do not need to recompile the vala program if you only made changes to the UI file.  Just run your previously compiled application, and the UI changes will be reflected.
+</p></note>
+</section>
+
+<section id="actions"><title>Setup actions</title>
+<p>This is a three step process.</p>
+<steps>
+  <item><p>First we create the ActionEntry array in the MyApplication class.
+           An ActionEntry consists of: </p>
+  <list>
+    <item><p>the "action name" (mandatory)</p></item>
+    <item><p>the callback function to connect to the "activate" signal of the action (if applicable)</p></item>
+    <item><p>the type of the parameter that must be passed to the activate function for the action (if applicable)</p></item>
+    <item><p>the initial state for this action (if applicable)</p></item>
+    <item><p>the callback to connect to "change-state" signal (if applicable)</p></item>
+  </list>
+   <code mime="text/x-vala">
+const ActionEntry[] actions = {
+    { "new", new_cb }, // {"action name", callback_function}
+    { "quit", quit_cb }
+};</code>
+
+  </item>
+  <item><p>Second, we create the callback functions the actions are connected to.</p>
+  <code mime="text/x-vala">
+void new_cb (SimpleAction action, Variant? parameter) {
+    print ("You clicked \"New\"\n");
+    //new MyWindow (this).show ();
+}
+
+void quit_cb (SimpleAction action, Variant? parameter) {
+    print ("You clicked \"Quit\"\n");
+    //this.quit ();  **Bug #674090**
+}</code>
+ </item>
+  <item><p>And lastly, we connect the menu items to the actions in the XML file by adding the "action" attribute:</p>
+    <code mime="text"><![CDATA[
+<item>
+  <attribute name="label">New</attribute>
+  <attribute name="action">app.new</attribute>
+</item>
+<item>
+  <attribute name="label">Quit</attribute>
+  <attribute name="action">app.quit</attribute>
+</item>]]></code>
+ </item>
+</steps>
+</section>
+
+
+<section id="choices"><title>Choices submenu and items with state</title>
+  <media type="image" mime="image/png" src="media/menubar_choices.png"/>
+  <p>Lines 30 to 80 inclusive of the <link xref="menubar.vala#xml-code" /> demonstrate
+     the XML code used to create the UI for <gui>Choices</gui> menu.</p>
+</section>
+
+<section id="win-app"><title>Actions: Application or Window?</title>
+  <p>Above, we created the "new" and "open" actions as part of the MyApplication class.
+  Actions which control the application itself should be created similarly.</p>
+
+  <p>Some actions, such as "copy" and "paste" deal with the window, not the application.
+  Window actions should be created as part of the window class.</p>
+
+  <p>
+   The complete example files contain both application actions and
+   window applications.  The window actions are the ones usually included in the <link xref="gmenu.vala">application menu</link> also.  It is not good practice to include window actions in the applcation menu. For demonstration purposes, the complete example files which follow include XML in the UI file which creates the application menu which includes a "New" and "Open" item, and these are hooked up to the same actions as the menubar items of the same name.
+  </p>
+
+
+</section>
+
+  <section id="xml-code"><title>Complete XML UI file for this example</title>
+<code mime="text" style="numbered"><xi:include href="samples/menubar.ui" parse="text"><xi:fallback/></xi:include></code>
+  </section>
+  <section id="vala-code"><title>Complete Vala file for this example</title>
+<code mime="text/x-vala" style="numbered"><xi:include href="samples/menubar.vala" parse="text"><xi:fallback/></xi:include></code>
+  </section>
+
+  <section id="mnemonics"><title>Mnemonics</title>
+    <p>Labels may contain mnemonics. Mnemonics are underlined characters in the label, used for keyboard navigation. Mnemonics are created by placing an underscore before the mnemonic character.  For example "_File" instead of just "File" in the menubar.ui label attribute.</p>
+   <p>The mnemonics are visible when you press the <key>Alt</key> key.  Pressing <keyseq><key>Alt</key><key>F</key></keyseq> will open the <gui>File</gui> menu.
+   </p>
+  </section>
+
+  <section id="accelerators"><title>Accelerators</title>
+    <p>Accelerators  can be explicitly added in the UI definitions.  For example, it is common to be able to quit an application by pressing <keyseq><key>Ctrl</key><key>Q</key></keyseq> or to save a file by pressing <keyseq><key>Ctrl</key><key>S</key></keyseq>.  To add an accelerator to the UI definition, you simply need add an "accel" attribute to the item.</p>
+<p><code mime="text"><![CDATA[<attribute name="accel">&lt;Primary&gt;q</attribute>]]></code> will create the <keyseq><key>Ctrl</key><key>Q</key></keyseq> sequence when added to the <code>Quit</code> label item.  Here, "Primary" refers to the <key>Ctrl</key> key on a PC or the <key>â</key> key on a Mac.</p>
+
+  <code mime="text"><![CDATA[
+<item>
+  <attribute name="label">_Quit</attribute>
+  <attribute name="action">app.quit</attribute>
+  <attribute name="accel">&lt;Primary&gt;q</attribute>
+</item>]]></code>
+  </section>
+
+  <section id="translatable"><title>Translatable strings</title>
+   <p>
+   Since GNOME applications are being translated into <link href="http://l10n.gnome.org/languages/";>many languages</link>, it is important that the strings in your application are translable.  To make a label translatable, simple set <code>translatable="yes"</code>:
+   </p>
+   <p>
+     <code mime="text"><![CDATA[<attribute name="label" translatable="yes">Quit</attribute>]]></code>
+  </p>
+  </section>
+  <section id="documentation"><title>Relevant API documentation</title>
+<p>
+  In this sample we used the following:
+</p>
+<list>
+  <item><p><link href="http://valadoc.org/gio-2.0/GLib.ActionEntry.html";>Glib.ActionEntry</link></p></item>
+  <item><p><link href="http://valadoc.org/gio-2.0/Gtk.Builder.html";>Gtk.Builder</link></p></item>
+</list>
+</section>
+
+
+<section id="exercises"><title>Exercises</title>
+  <xi:include href="exercises/menubar.exercises"><xi:fallback/></xi:include>
+</section>
+</page>
diff --git a/platform-demos/C/samples/menubar.ui b/platform-demos/C/samples/menubar.ui
new file mode 100644
index 0000000..f515006
--- /dev/null
+++ b/platform-demos/C/samples/menubar.ui
@@ -0,0 +1,103 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <menu id="menubar">
+    <submenu>
+      <attribute name="label">File</attribute>
+      <section>
+        <item>
+          <attribute name="label">New</attribute>
+          <attribute name="action">app.new</attribute>
+        </item>
+        <item>
+          <attribute name="label">Quit</attribute>
+          <attribute name="action">app.quit</attribute>
+        </item>
+      </section>
+    </submenu>
+    <submenu>
+      <attribute name="label">Edit</attribute>
+      <section>
+        <item>
+          <attribute name="label">Copy</attribute>
+          <attribute name="action">win.copy</attribute>
+        </item>
+        <item>
+          <attribute name="label">Paste</attribute>
+          <attribute name="action">win.paste</attribute>
+        </item>
+      </section>
+    </submenu>
+    <submenu>
+      <attribute name="label">Choices</attribute>
+      <submenu>
+        <attribute name="label">Shapes</attribute>
+          <section>
+            <item>
+              <attribute name="label">Line</attribute>
+              <attribute name="action">win.shape</attribute>
+              <attribute name="target">line</attribute>
+            </item>
+            <item>
+              <attribute name="label">Triangle</attribute>
+              <attribute name="action">win.shape</attribute>
+              <attribute name="target">triangle</attribute>
+            </item>
+            <item>
+              <attribute name="label">Square</attribute>
+              <attribute name="action">win.shape</attribute>
+              <attribute name="target">square</attribute>
+            </item>
+            <item>
+              <attribute name="label">Polygon</attribute>
+              <attribute name="action">win.shape</attribute>
+              <attribute name="target">polygon</attribute>
+            </item>
+            <item>
+              <attribute name="label">Circle</attribute>
+              <attribute name="action">win.shape</attribute>
+              <attribute name="target">circle</attribute>
+            </item>
+          </section>
+      </submenu>
+      <section>
+        <item>
+          <attribute name="label">On</attribute>
+          <attribute name="action">app.state</attribute>
+          <attribute name="target">on</attribute>
+        </item>
+        <item>
+          <attribute name="label">Off</attribute>
+          <attribute name="action">app.state</attribute>
+          <attribute name="target">off</attribute>
+        </item>
+      </section>
+      <section>
+        <item>
+          <attribute name="label">Awesome</attribute>
+          <attribute name="action">app.awesome</attribute>
+        </item>
+      </section>
+    </submenu>
+    <submenu>
+      <attribute name="label">Help</attribute>
+      <section>
+        <item>
+          <attribute name="label">About</attribute>
+          <attribute name="action">win.about</attribute>
+        </item>
+      </section>
+    </submenu>
+  </menu>
+  <menu id="appmenu">
+    <section>
+      <item>
+        <attribute name="label">New</attribute>
+        <attribute name="action">app.new</attribute>
+      </item>
+      <item>
+        <attribute name="label">Quit</attribute>
+        <attribute name="action">app.quit</attribute>
+      </item>
+    </section>
+  </menu>
+</interface>
diff --git a/platform-demos/C/samples/menubar.vala b/platform-demos/C/samples/menubar.vala
new file mode 100644
index 0000000..78c2808
--- /dev/null
+++ b/platform-demos/C/samples/menubar.vala
@@ -0,0 +1,99 @@
+public class MyWindow : Gtk.ApplicationWindow {
+
+	/* Callback functions for the window actions. */
+	void copy_cb (SimpleAction action, Variant? parameter) {
+		print ("\"Copy\" activated\n");
+	}
+
+	void paste_cb (SimpleAction action, Variant? parameter) {
+		print ("\"Paste\" activated\n");
+	}
+
+	void shape_cb (SimpleAction action, Variant? parameter) {
+		print ("shape is set to %s\n", parameter.get_string(null));
+		action.set_state (parameter);
+	}
+
+	/* Create the window actions. */
+	const ActionEntry[] actions = {
+		/*{ "action name", cb to connect to "activate" signal, parameter type,
+		     initial state, cb to connect to "change-state" signal } */
+		{ "copy", copy_cb },
+		{ "paste", paste_cb },
+		{ "shape", shape_cb, "s", "'line'"}
+	};
+
+	internal MyWindow (MyApplication app) {
+		Object (application: app, title: "MenuBar Example");
+		this.set_default_size (200, 200);
+
+		/* Setup window actions. */
+		this.add_action_entries (actions, this);
+	}
+}
+
+class MyApplication: Gtk.Application {
+	protected override void activate () {
+		new MyWindow (this).show ();
+	}
+
+	/* Callback functions for the application actions. */
+	void new_cb (SimpleAction action, Variant? parameter) {
+		//new MyWindow (this).show ();
+		print ("You clicked \"New\"\n");
+	}
+
+	void quit_cb (SimpleAction action, Variant? parameter) {
+		print ("You clicked \"Quit\"\n");
+		//this.quit ();  **Bug #674090**
+	}
+
+	void awesome_cb (SimpleAction action, Variant? parameter) {
+		var active = action.get_state ().get_boolean ();
+		action.set_state (new Variant.boolean (!active));
+		if (active)
+			print ("You unchecked \"Awesome\"\n");
+		else
+			print ("You checked \"Awesome\"\n");
+	}
+
+	void state_cb (SimpleAction action, Variant? parameter) {
+		print ("state is set to %s\n", parameter.get_string(null));
+		action.set_state (parameter);
+	}
+
+	/* Create the application actions. */
+	const ActionEntry[] actions = {
+		{ "new", new_cb },
+		{ "quit", quit_cb },
+		{ "awesome", awesome_cb, null, "false" },
+		{ "state", state_cb, "s", "'off'" }
+	};
+
+	protected override void startup () {
+		base.startup ();
+
+		/* Setup application actions. */
+		this.add_action_entries (actions, this);
+
+		/* Setup menubar and app_menu. */
+		/* Get the UI file. */
+		var builder = new Gtk.Builder ();
+		try {
+			builder.add_from_file ("menubar.ui");
+		} catch (Error e) {
+			error ("Unable to load file: %s", e.message);
+		}
+
+		/* Get the menubar from the builder. */
+		this.menubar = builder.get_object ("menubar") as MenuModel;
+
+		/* Get the app_menu from the builder. */
+		this.app_menu = builder.get_object ("appmenu") as MenuModel;
+	}
+}
+
+/* main creates and runs the application. */
+public int main (string[] args) {
+	return new MyApplication ().run (args);
+}
diff --git a/platform-demos/C/samples/menubar_basis.ui b/platform-demos/C/samples/menubar_basis.ui
new file mode 100644
index 0000000..131fe04
--- /dev/null
+++ b/platform-demos/C/samples/menubar_basis.ui
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <menu id="menubar">
+    <submenu>
+      <attribute name="label">File</attribute>
+    </submenu>
+    <submenu>
+      <attribute name="label">Edit</attribute>
+    </submenu>
+    <submenu>
+      <attribute name="label">Choices</attribute>
+    </submenu>
+    <submenu>
+      <attribute name="label">Help</attribute>
+    </submenu>
+  </menu>
+</interface>
diff --git a/platform-demos/C/samples/menubar_basis.vala b/platform-demos/C/samples/menubar_basis.vala
new file mode 100644
index 0000000..f00c9f0
--- /dev/null
+++ b/platform-demos/C/samples/menubar_basis.vala
@@ -0,0 +1,33 @@
+public class MyWindow : Gtk.ApplicationWindow {
+	internal MyWindow (MyApplication app) {
+		Object (application: app, title: "MenuBar Example");
+		this.set_default_size (200, 200);
+	}
+}
+
+class MyApplication: Gtk.Application {
+	protected override void activate () {
+		new MyWindow (this).show ();
+	}
+	protected override void startup () {
+		base.startup ();
+
+		/* Setup menubar. */
+		/* Get the UI file. */
+		var builder = new Gtk.Builder ();
+		try {
+			builder.add_from_file ("menubar_basis.ui");
+		/* Handle the exception. */
+		} catch (Error e) {
+			error ("Unable to load file: %s", e.message);
+		}
+
+		/* Get the menubar from the builder. */
+		this.menubar = builder.get_object ("menubar") as MenuModel;
+	}
+}
+
+/* main creates and runs the application. */
+public int main (string[] args) {
+	return new MyApplication ().run (args);
+}



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