[gnome-devel-docs] Upated Vala Toolbar sample to use simple actions.



commit dca57b707bade595b8ac6883bef0ccd0441071c3
Author: Tiffany Antopolski <tiffany antopolski gmail com>
Date:   Sun May 27 19:46:31 2012 -0400

    Upated Vala Toolbar sample to use simple actions.
    
    Also, made the Toolbar and all its ToolButtons instance
    variables of the window.

 platform-demos/C/samples/toolbar.vala |  140 +++++++++++++++++++++------------
 platform-demos/C/toolbar.vala.page    |    1 +
 2 files changed, 91 insertions(+), 50 deletions(-)
---
diff --git a/platform-demos/C/samples/toolbar.vala b/platform-demos/C/samples/toolbar.vala
index 759e426..6918048 100644
--- a/platform-demos/C/samples/toolbar.vala
+++ b/platform-demos/C/samples/toolbar.vala
@@ -1,101 +1,141 @@
-public class MyToolbar : Gtk.Toolbar {
-
-	Gtk.ApplicationWindow window;
+/* This is the Window */
+class MyWindow : Gtk.ApplicationWindow {
+	
+	/* Instance variables belonging to the window */
+	Gtk.Toolbar toolbar;
 	Gtk.ToolButton new_button;
 	Gtk.ToolButton open_button;
 	Gtk.ToolButton undo_button;
 	Gtk.ToolButton fullscreen_button;
 	Gtk.ToolButton leave_fullscreen_button;
 	bool window_is_fullscreen = false;
- 
-	public MyToolbar (MyWindow window) {
 
-		this.window = window;
-	
-		this.get_style_context ().add_class (Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
+	/* Contstructor */	
+	internal MyWindow (MyApplication app) {
+		Object (application: app, title: "Toolbar Example");
+
+		this.set_default_size (400, 200);
+		var grid = new Gtk.Grid ();
+		this.add (grid);
+		grid.show ();
+
+		create_toolbar ();
+		toolbar.set_hexpand (true);
+		grid.attach (toolbar, 1, 1, 1, 1);
+		toolbar.show ();
+
+		/* create the "undo" window action action */
+		var undo_action = new SimpleAction ("undo", null);
+		undo_action.activate.connect (undo_callback);
+		this.add_action (undo_action);
+
+		/* create the "fullscreen" window action */
+		var fullscreen_action = new SimpleAction ("fullscreen", null);
+		fullscreen_action.activate.connect (fullscreen_callback);
+		this.add_action (fullscreen_action);
+	}
+
+	/* This function creates the toolbar, its  ToolButtons,
+	 * and assigns the actions names to the ToolButtons.*/
+	void create_toolbar () {
+
+		toolbar = new Gtk.Toolbar ();	
+		toolbar.get_style_context ().add_class (Gtk.STYLE_CLASS_PRIMARY_TOOLBAR);
 		
 		new_button = new Gtk.ToolButton.from_stock (Gtk.Stock.NEW);
 		new_button.is_important = true; //decides whether to show the label
-		this.add (new_button);
+		toolbar.add (new_button);
 		new_button.show ();
-		new_button.clicked.connect (on_new_clicked);
+		new_button.action_name = "app.new";
 
 	  	open_button = new Gtk.ToolButton.from_stock (Gtk.Stock.OPEN);
         	open_button.is_important = true;
-        	this.add (open_button);
-		open_button.clicked.connect (on_open_clicked);
+        	toolbar.add (open_button);
+		open_button.show ();
+		open_button.action_name = "app.open";
 
       		undo_button = new Gtk.ToolButton.from_stock (Gtk.Stock.UNDO);
 		undo_button.is_important = true;
-		this.add (undo_button);
-		undo_button.clicked.connect (on_undo_clicked);
+		toolbar.add (undo_button);
+		undo_button.show ();
+		undo_button.action_name = "win.undo";
 
 		fullscreen_button = new Gtk.ToolButton.from_stock (Gtk.Stock.FULLSCREEN);
 		fullscreen_button.is_important = true;
-		this.add (fullscreen_button);
-		fullscreen_button.clicked.connect (on_toggle_fullscreen);
+		toolbar.add (fullscreen_button);
+		fullscreen_button.show ();
+		fullscreen_button.action_name = "win.fullscreen";
 	
-		leave_fullscreen_button = new Gtk.ToolButton.from_stock (Gtk.Stock.LEAVE_FULLSCREEN);
+		leave_fullscreen_button = new Gtk.ToolButton.from_stock (Gtk.Stock.LEAVE_FULLSCREEN)
+;
 		leave_fullscreen_button.is_important = true;
-		leave_fullscreen_button.clicked.connect (on_toggle_fullscreen);
+		toolbar.add (leave_fullscreen_button);
+		
+		leave_fullscreen_button.action_name = "win.fullscreen";
 	}
 
-	void on_new_clicked () {
-		print ("You clicked the \"New\" ToolButton.\n");
-	}
-	
-	void on_open_clicked () {
-			print ("You clicked the \"Open\" ToolButton.\n");
-	}
-	
-	void on_undo_clicked () {
-			print ("You clicked the \"Undo\" ToolButton.\n");
+	void undo_callback () {
+			print ("You clicked \"Undo\".\n");
 	}
 
-	void on_toggle_fullscreen (Gtk.ToolButton toolbutton) {
+	void fullscreen_callback () {
 		if (window_is_fullscreen) {
-			this.window.unfullscreen ();
+			this.unfullscreen ();
 			window_is_fullscreen = false;
-			this.remove (toolbutton);
-			this.add (fullscreen_button);
+			leave_fullscreen_button.hide ();
 			fullscreen_button.show ();
 		}
 		else {
-			this.window.fullscreen ();
+			this.fullscreen ();
 			window_is_fullscreen = true;
-			this.remove (toolbutton);
-			this.add (leave_fullscreen_button);
+			fullscreen_button.hide ();
 			leave_fullscreen_button.show ();
 		}
 	}
 }
 
-class MyWindow : Gtk.ApplicationWindow {
-	
-	internal MyWindow (MyApplication app) {
-		Object (application: app, title: "Toolbar Example");
+/* This is the application */
+class MyApplication : Gtk.Application {
+	protected override void activate () {
+		new MyWindow (this).show ();
+	}
 
-		this.set_default_size (400, 200);
-		var grid = new Gtk.Grid ();
-		this.add (grid);
+	protected override void startup () {
+		base.startup ();
+		
+		/* Create the "new" action and add it to the app*/
+		var new_action = new SimpleAction ("new", null);
+		new_action.activate.connect (new_callback);
+		this.add_action (new_action);
 
-		MyToolbar toolbar = new MyToolbar (this);
-		toolbar.set_hexpand (true);
-		grid.attach (toolbar, 1, 1, 1, 1);
-		toolbar.show ();
+		/* Create the "open" action, and add it to the app */
+		var open_action = new SimpleAction ("open", null);
+		open_action.activate.connect (open_callback);
+		this.add_action (open_action);
+
+		/* You could also add the action to the app menu
+		 * if you wanted to.
+		 */
+		//var menu = new Menu ();
+		//menu.append ("New", "app.new");
+		//this.app_menu = menu;
 	}
-}
 
-class MyApplication : Gtk.Application {
-	protected override void activate () {
-		new MyWindow (this).show_all ();
+	void new_callback () {
+		print ("You clicked \"New\".\n");
+	}
+
+	void open_callback () {
+			print ("You clicked \"Open\".\n");
 	}
 
+	/* Constructor */	
 	internal MyApplication () {
 		Object (application_id: "org.example.toolbar");
 	}
 }
 
+/* The main function creates the application and runs it. */
 int main (string[] args) {
 	return new MyApplication ().run (args);
 }
diff --git a/platform-demos/C/toolbar.vala.page b/platform-demos/C/toolbar.vala.page
index 0f1317f..a84517d 100644
--- a/platform-demos/C/toolbar.vala.page
+++ b/platform-demos/C/toolbar.vala.page
@@ -20,6 +20,7 @@
 
   <media type="image" mime="image/png" src="media/toolbar2.png"/>
   <p>Toolbar can contain either text or stock icons. In this sample we use stock icons. This example has fullscreen functionality.</p>
+  <p>This example uses SimpleActions (window and app).  App actions can easily be added the the app menu.</p>
 
 <code mime="text/x-vala" style="numbered"><xi:include href="samples/toolbar.vala" parse="text"><xi:fallback/></xi:include></code>
 



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