[gbrainy] Replace TextViews by custom control



commit 892e2f7bae53d67cbf0cc954674eb295d2cb6379
Author: Jordi Mas <jmas softcatala org>
Date:   Thu Aug 20 22:07:51 2009 +0200

    Replace TextViews by custom control

 src/GameDrawingArea.cs |    2 +-
 src/Makefile.am        |    1 +
 src/SimpleLabel.cs     |   88 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/gbrainy.cs         |   56 ++++++++++--------------------
 src/gbrainy.glade      |   52 ++--------------------------
 5 files changed, 112 insertions(+), 87 deletions(-)
---
diff --git a/src/GameDrawingArea.cs b/src/GameDrawingArea.cs
index bfea149..127c619 100644
--- a/src/GameDrawingArea.cs
+++ b/src/GameDrawingArea.cs
@@ -276,7 +276,7 @@ public class GameDrawingArea : DrawingArea
 		if (margins)
 			application.SetMargin ((int) x);
 		else
-			application.SetMargin (0);
+			application.SetMargin (2);
 
 		cr.Translate (x, y);
 		switch (mode) {
diff --git a/src/Makefile.am b/src/Makefile.am
index f5f3efe..cd14dd4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -74,6 +74,7 @@ GBRAINY_CSDISTFILES =				\
 	$(srcdir)/CalculationGames/CalculationPrimes.cs	\
 	$(srcdir)/MemoryGames/MemoryFacts.cs		\
 	$(srcdir)/PuzzleGames/PuzzleCounting.cs	\
+	$(srcdir)/SimpleLabel.cs	\
 	$(srcdir)/gbrainy.cs			
 
 ASSEMBLIES = \
diff --git a/src/SimpleLabel.cs b/src/SimpleLabel.cs
new file mode 100644
index 0000000..ca38b86
--- /dev/null
+++ b/src/SimpleLabel.cs
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2009 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;
+
+//
+// Simple Label control that draws a piece of text
+//
+public class SimpleLabel : DrawingArea
+{
+	string text;
+	int width_margin, height_margin;
+
+	public SimpleLabel ()
+	{
+
+	}
+
+	public string Text {
+		get { return text;}
+		set {
+			if (text == value)
+				return;
+
+			text = value;
+			QueueDraw ();
+		}
+	}
+
+	public int WidthMargin {
+		set {
+			if (width_margin == value)
+				return;
+
+			width_margin = value;
+			QueueDraw ();
+		}
+	}
+
+	public int HeightMargin {
+		set {
+			if (height_margin == value)
+				return;
+
+			height_margin = value;
+			QueueDraw ();
+		}
+	}
+
+	protected override bool OnExposeEvent (Gdk.EventExpose args)
+	{
+		if (String.IsNullOrEmpty (text))
+			return base.OnExposeEvent (args);
+
+		using (Cairo.Context cr = Gdk.CairoHelper.Create (args.Window))
+		{
+			int winWidth, winHeight;
+			Gdk.GC light = Style.ForegroundGC (StateType.Normal);
+			args.Window.GetSize (out winWidth, out winHeight);
+
+			using (Pango.Layout layout = new Pango.Layout (this.PangoContext))
+			{
+				layout.Width = (winWidth - width_margin * 2) * (int) Pango.Scale.PangoScale;
+				layout.SetMarkup (text);
+				args.Window.DrawLayout (light, width_margin, height_margin, layout);
+			}
+		}
+
+		return base.OnExposeEvent (args);
+	}
+}
diff --git a/src/gbrainy.cs b/src/gbrainy.cs
index 5002f13..33462bd 100644
--- a/src/gbrainy.cs
+++ b/src/gbrainy.cs
@@ -40,15 +40,14 @@ public class gbrainy: Program
 	[Glade.Widget("gbrainy")] Gtk.Window app_window;
 	[Glade.Widget ("toolbar_item")] Gtk.CheckMenuItem toolbar_menuitem;
 	[Glade.Widget] Box drawing_vbox;
-	[Glade.Widget] Gtk.TextView question_textview;
-	[Glade.Widget] Gtk.TextView solution_textview;
+	[Glade.Widget] Gtk.VBox question_vbox;
+	[Glade.Widget] Gtk.VBox solution_vbox;
 	[Glade.Widget] Gtk.Entry answer_entry;
 	[Glade.Widget] Gtk.Button answer_button;
 	[Glade.Widget] Gtk.Button tip_button;
 	[Glade.Widget] Gtk.Button next_button;
 	[Glade.Widget] Gtk.Statusbar statusbar;
 	[Glade.Widget] Gtk.Toolbar toolbar;
-	[Glade.Widget] Gtk.Label label_answer;
 	[Glade.Widget] Gtk.Menu settings_menu;
 	[Glade.Widget] Gtk.Menu help_menu;
 	[Glade.Widget] Gtk.MenuItem pause_menuitem;
@@ -57,11 +56,11 @@ public class gbrainy: Program
 	GameDrawingArea drawing_area;
 	GameSession session;
 	ToolButton all_tbbutton, logic_tbbutton, calculation_tbbutton, memory_tbbutton, pause_tbbutton, finish_tbbutton;
-	Gtk.TextBuffer question_buffer;
-	Gtk.TextBuffer solution_buffer;
 	TextTag tag_green;
 	bool low_res;
 	bool full_screen;
+	SimpleLabel question_label;
+	SimpleLabel solution_label;
 	
 	public static PlayerHistory history = null;
 	public static Preferences preferences = null;
@@ -90,16 +89,6 @@ public class gbrainy: Program
 		toolbar.IconSize = Gtk.IconSize.Dnd;
 	
 		Tooltips tooltips = new Tooltips ();
-		question_buffer = new Gtk.TextBuffer (new Gtk.TextTagTable ());
-		solution_buffer = new Gtk.TextBuffer (new Gtk.TextTagTable ());
-		question_textview.Buffer = question_buffer;
-		solution_textview.Buffer = solution_buffer;
-		solution_textview.Visible = false;
-		question_textview.Visible = false;
-		tag_green = new TextTag ("Congratulations");
-		tag_green.Foreground = "#00A000";
-		solution_buffer.TagTable.Add (tag_green);
-
 		all_tbbutton = new ToolButton ("allgames");
 		all_tbbutton.SetTooltip (tooltips, Catalog.GetString ("Play all the games"), null);
 		all_tbbutton.Label = Catalog.GetString ("All");
@@ -156,6 +145,14 @@ public class gbrainy: Program
 			}
 		}
 
+		question_label = new SimpleLabel ();
+		question_label.HeightMargin = 2;
+		question_vbox.Add (question_label);
+
+		solution_label = new SimpleLabel ();
+		solution_label.HeightMargin = 2;
+		solution_vbox.Add (solution_label);
+
 	#if MONO_ADDINS
 		Gtk.MenuItem item = new Gtk.MenuItem (Catalog.GetString ("Extensions"));
 		settings_menu.Append (item);
@@ -174,10 +171,6 @@ public class gbrainy: Program
 		if (preferences.GetBoolValue (Preferences.Toolbar) == false || low_res == true)
 			toolbar_menuitem.Active = false;
 
-		color = label_answer.Style.Background (StateType.Normal);
-		question_textview.ModifyBase (Gtk.StateType.Normal, color);
-		solution_textview.ModifyBase (Gtk.StateType.Normal, color);
-
 		ActiveInputControls (false);
 	}
 
@@ -288,12 +281,12 @@ public class gbrainy: Program
 
 	public void UpdateQuestion (string question)
 	{
-		question_buffer.Text = question;
+		question_label.Text = question;
 	}
 
-	private void UpdateSolution (string question)
+	private void UpdateSolution (string solution)
 	{		
-		solution_buffer.Text = question;
+		solution_label.Text = solution;
 	}
 
 	public void QueueDraw ()
@@ -303,10 +296,8 @@ public class gbrainy: Program
 
 	public void SetMargin (int margin)
 	{
-		question_textview.RightMargin = margin;
-		solution_textview.RightMargin = margin;
-		question_textview.LeftMargin = margin;
-		solution_textview.LeftMargin = margin;
+		question_label.WidthMargin = margin;
+		solution_label.WidthMargin = margin;
 	}
 
 	void GameSensitiveUI () 
@@ -363,17 +354,14 @@ public class gbrainy: Program
 	void OnAnswerButtonClicked (object sender, EventArgs args)
 	{
 		string answer;
-		bool congratulations = false;
-		TextIter begin_iter, end_iter;
 
 		if (session.CurrentGame == null)
 			return;
 	
 		if (answer_button.Sensitive == true && session.CurrentGame.CheckAnswer (answer_entry.Text) == true) {
-			congratulations = true;
 			session.GamesWon++;
 			session.CurrentGame.Won = true;
-			answer = Catalog.GetString ("Congratulations.");
+			answer = "<span color='#00A000'>" + Catalog.GetString ("Congratulations.") + "</span>";
 		} else
 			answer = Catalog.GetString ("Incorrect answer.");
 
@@ -382,14 +370,6 @@ public class gbrainy: Program
 		answer_entry.Text = String.Empty;
 		UpdateStatusBar ();
 		UpdateSolution (answer + " " + session.CurrentGame.Answer);
-		begin_iter = solution_buffer.GetIterAtOffset (0);
-		end_iter = solution_buffer.GetIterAtOffset (answer.Length);
-
-		if (congratulations) {
-			solution_buffer.ApplyTag (tag_green, begin_iter, end_iter);
-		} else {
-			solution_buffer.RemoveTag (tag_green, begin_iter, end_iter);
-		}
 
 		session.CurrentGame.DrawAnswer = true;
 		session.Status = GameSession.SessionStatus.Answered;
diff --git a/src/gbrainy.glade b/src/gbrainy.glade
index 5acbdcf..9551207 100644
--- a/src/gbrainy.glade
+++ b/src/gbrainy.glade
@@ -257,36 +257,14 @@
       </child>
 
       <child>
-	<widget class="GtkVBox" id="vbox2">
+	<widget class="GtkVBox" id="question_vbox">
 	  <property name="height_request">55</property>
 	  <property name="visible">True</property>
 	  <property name="homogeneous">False</property>
 	  <property name="spacing">1</property>
 
 	  <child>
-	    <widget class="GtkTextView" id="question_textview">
-	      <property name="width_request">532</property>
-	      <property name="height_request">55</property>
-	      <property name="visible">True</property>
-	      <property name="editable">False</property>
-	      <property name="overwrite">False</property>
-	      <property name="accepts_tab">True</property>
-	      <property name="justification">GTK_JUSTIFY_LEFT</property>
-	      <property name="wrap_mode">GTK_WRAP_WORD</property>
-	      <property name="cursor_visible">False</property>
-	      <property name="pixels_above_lines">3</property>
-	      <property name="pixels_below_lines">3</property>
-	      <property name="pixels_inside_wrap">0</property>
-	      <property name="left_margin">3</property>
-	      <property name="right_margin">3</property>
-	      <property name="indent">0</property>
-	      <property name="text" translatable="yes"></property>
-	    </widget>
-	    <packing>
-	      <property name="padding">0</property>
-	      <property name="expand">False</property>
-	      <property name="fill">False</property>
-	    </packing>
+	    <placeholder/>
 	  </child>
 	</widget>
 	<packing>
@@ -315,36 +293,14 @@
       </child>
 
       <child>
-	<widget class="GtkVBox" id="vbox4">
+	<widget class="GtkVBox" id="solution_vbox">
 	  <property name="height_request">55</property>
 	  <property name="visible">True</property>
 	  <property name="homogeneous">False</property>
 	  <property name="spacing">1</property>
 
 	  <child>
-	    <widget class="GtkTextView" id="solution_textview">
-	      <property name="width_request">532</property>
-	      <property name="height_request">55</property>
-	      <property name="visible">True</property>
-	      <property name="editable">False</property>
-	      <property name="overwrite">False</property>
-	      <property name="accepts_tab">True</property>
-	      <property name="justification">GTK_JUSTIFY_LEFT</property>
-	      <property name="wrap_mode">GTK_WRAP_WORD</property>
-	      <property name="cursor_visible">False</property>
-	      <property name="pixels_above_lines">3</property>
-	      <property name="pixels_below_lines">3</property>
-	      <property name="pixels_inside_wrap">0</property>
-	      <property name="left_margin">3</property>
-	      <property name="right_margin">3</property>
-	      <property name="indent">0</property>
-	      <property name="text" translatable="yes"></property>
-	    </widget>
-	    <packing>
-	      <property name="padding">0</property>
-	      <property name="expand">False</property>
-	      <property name="fill">False</property>
-	    </packing>
+	    <placeholder/>
 	  </child>
 	</widget>
 	<packing>



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