[gbrainy] Create separate class to handle the gbrainy's gtk classical toolbar



commit f3b40255a3588f7e3237fdb56ac33c4b958966f6
Author: Jordi Mas <jmas softcatala org>
Date:   Sun Mar 6 16:35:57 2011 +0100

    Create separate class to handle the gbrainy's gtk classical toolbar

 gbrainy.csproj                           |    1 +
 po/POTFILES.in                           |    1 +
 src/Clients/Classical/Makefile.am        |    3 +-
 src/Clients/Classical/Widgets/Toolbar.cs |  169 ++++++++++++++++++++++++++
 src/Clients/Classical/gbrainy.cs         |  195 ++++++------------------------
 5 files changed, 208 insertions(+), 161 deletions(-)
---
diff --git a/gbrainy.csproj b/gbrainy.csproj
index fd727ac..c76b27e 100644
--- a/gbrainy.csproj
+++ b/gbrainy.csproj
@@ -70,6 +70,7 @@
     <Compile Include="src\Clients\Classical\Widgets\BrowseFile.cs" />
     <Compile Include="src\Clients\Classical\Dialogs\PdfExportDialog.cs" />
     <Compile Include="src\Clients\Classical\Widgets\GameDrawingArea.cs" />
+    <Compile Include="src\Clients\Classical\Widgets\Toolbar.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 11218bd..4fa2a8a 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -41,6 +41,7 @@ src/Clients/Classical/Dialogs/PreferencesDialog.cs
 [type: gettext/glade] src/Clients/Classical/Dialogs/ui/PreferencesDialog.ui
 src/Clients/Classical/Widgets/BrowseFile.cs
 src/Clients/Classical/Widgets/GameDrawingArea.cs
+src/Clients/Classical/Widgets/Toolbar.cs
 src/Games/Logic/Puzzle3DCube.cs
 src/Games/Logic/PuzzleBalance.cs
 src/Games/Logic/PuzzleBuildTriangle.cs
diff --git a/src/Clients/Classical/Makefile.am b/src/Clients/Classical/Makefile.am
index 96d3047..e6f35d9 100644
--- a/src/Clients/Classical/Makefile.am
+++ b/src/Clients/Classical/Makefile.am
@@ -19,7 +19,8 @@ GBRAINY_CSDISTFILES =					\
 	$(srcdir)/Dialogs/PreferencesDialog.cs		\
 	$(srcdir)/Dialogs/PdfExportDialog.cs		\
 	$(srcdir)/Widgets/GameDrawingArea.cs		\
-	$(srcdir)/Widgets/BrowseFile.cs
+	$(srcdir)/Widgets/BrowseFile.cs			\
+	$(srcdir)/Widgets/Toolbar.cs
 
 
 ASSEMBLIES = \
diff --git a/src/Clients/Classical/Widgets/Toolbar.cs b/src/Clients/Classical/Widgets/Toolbar.cs
new file mode 100644
index 0000000..c5ebb91
--- /dev/null
+++ b/src/Clients/Classical/Widgets/Toolbar.cs
@@ -0,0 +1,169 @@
+/*
+ * Copyright (C) 2007-2011 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using Gtk;
+using Mono.Unix;
+
+namespace gbrainy.Clients.Classical.Widgets
+{
+	// Build and manages gbrainy's client Toolbar
+	public class Toolbar : Gtk.Toolbar
+	{
+		Gtk.HBox main_hbox;
+		Gtk.VBox framework_vbox;
+
+		public ToolButton AllButton {get; private set; }
+		public ToolButton CalculationButton {get; private set; }
+		public ToolButton FinishButton {get; private set; }
+		public ToolButton LogicButton {get; private set; }
+		public ToolButton MemoryButton {get; private set; }
+		public ToolButton PauseButton {get; private set; }
+		public ToolButton VerbalButton {get; private set; }
+		public bool InitCompleted {get; private set; }
+
+		public Toolbar (Gtk.HBox main_hbox, Gtk.VBox framework_vbox)
+		{
+			this.main_hbox = main_hbox;
+			this.framework_vbox = framework_vbox;
+			ToolbarStyle = ToolbarStyle.Both;
+			BuildToolBar ();
+			AttachToolBar (Orientation);
+		}
+
+		public void AttachToolBar (Gtk.Orientation orientation_new)
+		{
+			Gtk.Box.BoxChild child = null;
+			Box box;
+
+			switch (Orientation) {
+			case Gtk.Orientation.Vertical:
+				box = main_hbox;
+				break;
+			case Gtk.Orientation.Horizontal:
+			{
+				box = framework_vbox;
+				break;
+			}
+			default:
+				throw new InvalidOperationException ();
+			}
+
+			bool contained = false;
+			foreach (var ch in box.AllChildren)
+			{
+				if (ch == this)
+				{
+					contained = true;
+					break;
+				}
+			}
+			if (contained == true)
+				box.Remove (this);
+
+			Orientation = (Gtk.Orientation) orientation_new;
+
+			switch (Orientation) {
+			case Gtk.Orientation.Vertical:
+				main_hbox.Add (this);
+				main_hbox.ReorderChild (this, 0);
+				child = ((Gtk.Box.BoxChild)(main_hbox[this]));
+				break;
+			case Gtk.Orientation.Horizontal:
+				framework_vbox.Add (this);
+				framework_vbox.ReorderChild (this, 1);
+				child = ((Gtk.Box.BoxChild)(framework_vbox[this]));
+				break;
+			default:
+				throw new InvalidOperationException ();
+			}
+
+			child.Expand = false;
+			child.Fill = false;
+			ShowAll ();
+			InitCompleted = true;
+		}
+
+		void BuildToolBar ()
+		{
+			IconFactory icon_factory = new IconFactory ();
+			AddIcon (icon_factory, "logic-games", "logic-games-32.png");
+			AddIcon (icon_factory, "math-games", "math-games-32.png");
+			AddIcon (icon_factory, "memory-games", "memory-games-32.png");
+			AddIcon (icon_factory, "verbal-games", "verbal-games-32.png");
+			AddIcon (icon_factory, "pause", "pause-32.png");
+			AddIcon (icon_factory, "resume", "resume-32.png");
+			AddIcon (icon_factory, "endgame", "endgame-32.png");
+			AddIcon (icon_factory, "allgames", "allgames-32.png");
+			icon_factory.AddDefault ();
+
+			IconSize = Gtk.IconSize.Dnd;
+
+			AllButton = new ToolButton ("allgames");
+			AllButton.TooltipText = Catalog.GetString ("Play all the games");
+			AllButton.Label = Catalog.GetString ("All");
+			Insert (AllButton, -1);
+
+			LogicButton = new ToolButton ("logic-games");
+			LogicButton.TooltipText = Catalog.GetString ("Play games that challenge your reasoning and thinking");
+			LogicButton.Label = Catalog.GetString ("Logic");
+			Insert (LogicButton, -1);
+
+			CalculationButton = new ToolButton ("math-games");
+			CalculationButton.Label = Catalog.GetString ("Calculation");
+			CalculationButton.TooltipText = Catalog.GetString ("Play games that challenge your mental calculation skills");
+			Insert (CalculationButton, -1);
+
+			MemoryButton = new ToolButton ("memory-games");
+			MemoryButton.Label = Catalog.GetString ("Memory");
+			MemoryButton.TooltipText = Catalog.GetString ("Play games that challenge your short term memory");
+			Insert (MemoryButton, -1);
+
+			VerbalButton = new ToolButton ("verbal-games");
+			VerbalButton.Label = Catalog.GetString ("Verbal");
+			VerbalButton.TooltipText = Catalog.GetString ("Play games that challenge your verbal aptitude");
+			Insert (VerbalButton, -1);
+
+			PauseButton = new ToolButton ("pause");
+			PauseButton.Label = Catalog.GetString ("Pause");
+			PauseButton.TooltipText = Catalog.GetString ("Pause or resume the game");
+			Insert (PauseButton, -1);
+
+			FinishButton = new ToolButton ("endgame");
+			FinishButton.TooltipText = Catalog.GetString ("End the game and show score");
+			FinishButton.Label = Catalog.GetString ("Finish");
+			Insert (FinishButton, -1);
+		}
+
+		void AddIcon (IconFactory stock, string stockid, string resource)
+		{
+			Gtk.IconSet iconset = stock.Lookup (stockid);
+
+			if (iconset != null)
+				return;
+
+			iconset = new Gtk.IconSet ();
+			Gdk.Pixbuf img = Gdk.Pixbuf.LoadFromResource (resource);
+			IconSource source = new IconSource ();
+			source.Pixbuf = img;
+			iconset.AddSource (source);
+			stock.Add (stockid, iconset);
+		}
+	}
+}
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index 781de8f..d6c3aad 100755
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2010 Jordi Mas i Hernàndez <jmas softcatala org>
+ * Copyright (C) 2007-2011 Jordi Mas i Hernàndez <jmas softcatala org>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -71,15 +71,13 @@ namespace gbrainy.Clients.Classical
 		[GtkBeans.Builder.Object] Gtk.RadioMenuItem horizontal_radiomenuitem;
 		[GtkBeans.Builder.Object] Gtk.MenuItem toolbar_orientation_menuitem;
 
-		Gtk.Toolbar toolbar;
+		Widgets.Toolbar toolbar;
 
 		GameDrawingArea drawing_area;
 		GameSession session;
-		ToolButton all_tbbutton, logic_tbbutton, calculation_tbbutton, memory_tbbutton, verbal_tbbutton, pause_tbbutton, finish_tbbutton;
 		bool low_res;
 		bool full_screen;
 		GameSession.Types initial_session;
-		bool init_completed = false;
 
 		public GtkClient ()
 #if GNOME
@@ -122,61 +120,6 @@ namespace gbrainy.Clients.Classical
 			gm.LoadPlugins ();
 		}
 
-		void AttachToolBar ()
-		{
-			Gtk.Box.BoxChild child;
-
-			if (toolbar != null)
-			{
-				Box box;
-
-				switch (toolbar.Orientation) {
-				case Gtk.Orientation.Vertical:
-					box = main_hbox;
-					break;
-				case Gtk.Orientation.Horizontal:
-				{
-					box = framework_vbox;
-					break;
-				}
-				default:
-					throw new InvalidOperationException ();
-				}
-
-				bool contained = false;
-				foreach (var ch in box.AllChildren)
-				{
-					if (ch == toolbar)
-					{
-						contained = true;
-						break;
-					}
-				}
-				if (contained == true)
-					box.Remove (toolbar);
-			}
-			toolbar.Orientation = (Gtk.Orientation) Preferences.GetIntValue (Preferences.ToolbarOrientationKey);
-
-			switch (toolbar.Orientation) {
-			case Gtk.Orientation.Vertical:
-				main_hbox.Add (toolbar);
-				main_hbox.ReorderChild (toolbar, 0);
-				child = ((Gtk.Box.BoxChild)(main_hbox[toolbar]));
-				break;
-			case Gtk.Orientation.Horizontal:
-				framework_vbox.Add (toolbar);
-				framework_vbox.ReorderChild (toolbar, 1);
-				child = ((Gtk.Box.BoxChild)(framework_vbox[toolbar]));
-				break;
-			default:
-				throw new InvalidOperationException ();
-			}
-
-			child.Expand = false;
-			child.Fill = false;
-			toolbar.ShowAll ();
-			init_completed = true;
-		}
 
 		void BuildUI ()
 		{
@@ -188,10 +131,14 @@ namespace gbrainy.Clients.Classical
 			show_toolbar = Preferences.GetBoolValue (Preferences.ToolbarShowKey) == true && low_res == false;
 
 			// Toolbar creation
-			toolbar = new Gtk.Toolbar ();
-			toolbar.ToolbarStyle = ToolbarStyle.Both;
-			BuildToolBar ();
-			AttachToolBar ();
+			toolbar = new Widgets.Toolbar (main_hbox, framework_vbox);
+			toolbar.AllButton.Clicked += OnAllGames;
+			toolbar.LogicButton.Clicked += OnLogicOnly;
+			toolbar.CalculationButton.Clicked += OnMathOnly;
+			toolbar.MemoryButton.Clicked += OnMemoryOnly;
+			toolbar.VerbalButton.Clicked += OnVerbalOnly;
+			toolbar.PauseButton.Clicked += OnPauseGame;
+			toolbar.FinishButton.Clicked += OnEndGame;
 
 			drawing_area = new GameDrawingArea ();
 			drawing_area.Drawable = session;
@@ -355,7 +302,7 @@ namespace gbrainy.Clients.Classical
 			answer_entry.Sensitive = entry;
 			next_button.Sensitive = next;
 			tip_button.Sensitive = tip;
-			pause_menuitem.Sensitive = pause_tbbutton.Sensitive = can_pause;
+			pause_menuitem.Sensitive = toolbar.PauseButton.Sensitive = can_pause;
 
 			if (entry == true)
 				answer_entry.GrabFocus ();
@@ -378,64 +325,6 @@ namespace gbrainy.Clients.Classical
 			QueueDraw ();
 		}
 
-		void BuildToolBar ()
-		{
-			IconFactory icon_factory = new IconFactory ();
-		        AddIcon (icon_factory, "logic-games", "logic-games-32.png");
-			AddIcon (icon_factory, "math-games", "math-games-32.png");
-			AddIcon (icon_factory, "memory-games", "memory-games-32.png");
-			AddIcon (icon_factory, "verbal-games", "verbal-games-32.png");
-			AddIcon (icon_factory, "pause", "pause-32.png");
-			AddIcon (icon_factory, "resume", "resume-32.png");
-			AddIcon (icon_factory, "endgame", "endgame-32.png");
-			AddIcon (icon_factory, "allgames", "allgames-32.png");
-			icon_factory.AddDefault ();
-
-			toolbar.IconSize = Gtk.IconSize.Dnd;
-
-			all_tbbutton = new ToolButton ("allgames");
-			all_tbbutton.TooltipText = Catalog.GetString ("Play all the games");
-			all_tbbutton.Label = Catalog.GetString ("All");
-			all_tbbutton.Clicked += OnAllGames;
-			toolbar.Insert (all_tbbutton, -1);
-
-			logic_tbbutton = new ToolButton ("logic-games");
-			logic_tbbutton.TooltipText = Catalog.GetString ("Play games that challenge your reasoning and thinking");
-			logic_tbbutton.Label = Catalog.GetString ("Logic");
-			logic_tbbutton.Clicked += OnLogicOnly;
-			toolbar.Insert (logic_tbbutton, -1);
-
-			calculation_tbbutton = new ToolButton ("math-games");
-			calculation_tbbutton.Label = Catalog.GetString ("Calculation");
-			calculation_tbbutton.TooltipText = Catalog.GetString ("Play games that challenge your mental calculation skills");
-			calculation_tbbutton.Clicked += OnMathOnly;
-			toolbar.Insert (calculation_tbbutton, -1);
-
-			memory_tbbutton = new ToolButton ("memory-games");
-			memory_tbbutton.Label = Catalog.GetString ("Memory");
-			memory_tbbutton.TooltipText = Catalog.GetString ("Play games that challenge your short term memory");
-			memory_tbbutton.Clicked += OnMemoryOnly;
-			toolbar.Insert (memory_tbbutton, -1);
-
-			verbal_tbbutton = new ToolButton ("verbal-games");
-			verbal_tbbutton.Label = Catalog.GetString ("Verbal");
-			verbal_tbbutton.TooltipText = Catalog.GetString ("Play games that challenge your verbal aptitude");
-			verbal_tbbutton.Clicked += OnVerbalOnly;
-			toolbar.Insert (verbal_tbbutton, -1);
-
-			pause_tbbutton = new ToolButton ("pause");
-			pause_tbbutton.Label = Catalog.GetString ("Pause");
-			pause_tbbutton.TooltipText = Catalog.GetString ("Pause or resume the game");
-			pause_tbbutton.Clicked += OnPauseGame;
-			toolbar.Insert (pause_tbbutton, -1);
-
-			finish_tbbutton = new ToolButton ("endgame");
-			finish_tbbutton.TooltipText = Catalog.GetString ("End the game and show score");
-			finish_tbbutton.Label = Catalog.GetString ("Finish");
-			finish_tbbutton.Clicked += OnEndGame;
-			toolbar.Insert (finish_tbbutton, -1);
-		}
-
 		// These are UI elements independent of the game status, set only when the game starts / ends
 		void GameSensitiveUI ()
 		{
@@ -444,34 +333,34 @@ namespace gbrainy.Clients.Classical
 			GameTypes available;
 
 			playing = (session.Status == GameSession.SessionStatus.Playing);
-			finish_tbbutton.Sensitive = playing;
+			toolbar.FinishButton.Sensitive = playing;
 
 			available = session.AvailableGames;
 
 			if (playing == false && ((available & GameTypes.LogicPuzzle) == GameTypes.LogicPuzzle))
-				logic_menuitem.Sensitive = logic_tbbutton.Sensitive = true;
+				logic_menuitem.Sensitive = toolbar.LogicButton.Sensitive = true;
 			else
-				logic_menuitem.Sensitive = logic_tbbutton.Sensitive = false;
+				logic_menuitem.Sensitive = toolbar.LogicButton.Sensitive = false;
 
 			if (playing == false && ((available & GameTypes.Calculation) == GameTypes.Calculation))
-				memory_menuitem.Sensitive = memory_tbbutton.Sensitive = true;
+				memory_menuitem.Sensitive = toolbar.MemoryButton.Sensitive = true;
 			else
-				memory_menuitem.Sensitive = memory_tbbutton.Sensitive = false;
+				memory_menuitem.Sensitive = toolbar.MemoryButton.Sensitive = false;
 
 			if (playing == false && ((available & GameTypes.Calculation) == GameTypes.Calculation))
-				calculation_menuitem.Sensitive = calculation_tbbutton.Sensitive = true;
+				calculation_menuitem.Sensitive = toolbar.CalculationButton.Sensitive = true;
 			else
-				calculation_menuitem.Sensitive = calculation_tbbutton.Sensitive = false;
+				calculation_menuitem.Sensitive = toolbar.CalculationButton.Sensitive = false;
 
 			if (playing == false && ((available & GameTypes.VerbalAnalogy) == GameTypes.VerbalAnalogy))
-				verbal_menuitem.Sensitive = verbal_tbbutton.Sensitive = true;
+				verbal_menuitem.Sensitive = toolbar.VerbalButton.Sensitive = true;
 			else
-				verbal_menuitem.Sensitive = verbal_tbbutton.Sensitive = false;
+				verbal_menuitem.Sensitive = toolbar.VerbalButton.Sensitive = false;
 
 			if (playing == false && (available != GameTypes.None))
-				allgames_menuitem.Sensitive = all_tbbutton.Sensitive = true;
+				allgames_menuitem.Sensitive = toolbar.AllButton.Sensitive = true;
 			else
-				allgames_menuitem.Sensitive = all_tbbutton.Sensitive = false;
+				allgames_menuitem.Sensitive = toolbar.AllButton.Sensitive = false;
 
 			finish_menuitem.Sensitive = playing;
 			newgame_menuitem.Sensitive = !playing;
@@ -672,19 +561,18 @@ namespace gbrainy.Clients.Classical
 		{
 			if (pause) {
 				drawing_area.Paused = false;
-				pause_tbbutton.StockId = "pause";
-				pause_tbbutton.Label = Catalog.GetString ("Pause");
+				toolbar.PauseButton.StockId = "pause";
+				toolbar.PauseButton.Label = Catalog.GetString ("Pause");
 				ActiveInputControls (true);
 			} else {
 				drawing_area.Paused = true;
-				pause_tbbutton.StockId = "resume";
-				pause_tbbutton.Label = Catalog.GetString ("Resume");
+				toolbar.PauseButton.StockId = "resume";
+				toolbar.PauseButton.Label = Catalog.GetString ("Resume");
 				ActiveInputControls (false);
 			}
 			UpdateStatusBar ();
 		}
 
-
 		void SetPauseResumeButton (bool pause)
 		{
 			if (pause)
@@ -721,22 +609,24 @@ namespace gbrainy.Clients.Classical
 
 		void OnVerticalToolbar (object sender, System.EventArgs args)
 		{
-			if (init_completed  == false)
+			if (toolbar.InitCompleted  == false)
 				return;
 
-			Preferences.SetIntValue (Preferences.ToolbarOrientationKey, (int) Gtk.Orientation.Vertical);
+			const Gtk.Orientation orientation = Gtk.Orientation.Vertical;
+			Preferences.SetIntValue (Preferences.ToolbarOrientationKey, (int) orientation);
 			Preferences.Save ();
-			AttachToolBar ();
+			toolbar.AttachToolBar (orientation);
 		}
 
 		void OnHorizontalToolbar (object sender, System.EventArgs args)
 		{
-			if (init_completed  == false)
+			if (toolbar.InitCompleted  == false)
 				return;
 
+			const Gtk.Orientation orientation = Gtk.Orientation.Horizontal;
 			Preferences.SetIntValue (Preferences.ToolbarOrientationKey, (int) Gtk.Orientation.Horizontal);
 			Preferences.Save ();
-			AttachToolBar ();
+			toolbar.AttachToolBar (orientation);
 		}
 
 		void OnHistory (object sender, EventArgs args)
@@ -748,21 +638,6 @@ namespace gbrainy.Clients.Classical
 			dialog.Destroy ();
 		}
 
-		private void AddIcon (IconFactory stock, string stockid, string resource)
-		{
-			Gtk.IconSet iconset = stock.Lookup (stockid);
-
-			if (iconset != null)
-				return;
-
-			iconset = new Gtk.IconSet ();
-			Gdk.Pixbuf img = Gdk.Pixbuf.LoadFromResource (resource);
-			IconSource source = new IconSource ();
-			source.Pixbuf = img;
-			iconset.AddSource (source);
-			stock.Add (stockid, iconset);
-		}
-
 		void OnFullscreen (object sender, EventArgs args)
 		{
 			if (full_screen == false) {
@@ -790,9 +665,9 @@ namespace gbrainy.Clients.Classical
 			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.GamesDefinitions, Defines.DATA_DIR);
 			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.GamesGraphics, Defines.DATA_DIR);
 			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.ThemesDir, Defines.DATA_DIR);
-			
+
 			string assemblies_dir;
-			assemblies_dir =  System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);				
+			assemblies_dir =  System.IO.Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location);
 			ServiceLocator.Instance.GetService <IConfiguration> ().Set (ConfigurationKeys.AssembliesDir, assemblies_dir);
 		}
 



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