1 /*
2  * Copyright (C) 2008-2009 Jordi Mas i Hernàndez <jmas@softcatala.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 using System;
19 using Gtk;
20 using Mono.Unix;
21 
22 using gbrainy.Core.Main;
23 using gbrainy.Core.Views;
24 using gbrainy.Core.Services;
25 
26 namespace gbrainy.Clients.Classical.Dialogs
27 {
28 	public class PlayerHistoryDialog : BuilderDialog
29 	{
30 		[Builder.Object] Box history_preview;
31 		[Builder.Object] Label label_playerhistory;
32 		[Builder.Object] Gtk.CheckButton checkbutton_total;
33 		[Builder.Object] Gtk.CheckButton checkbutton_memory;
34 		[Builder.Object] Gtk.CheckButton checkbutton_logic;
35 		[Builder.Object] Gtk.CheckButton checkbutton_calculation;
36 		[Builder.Object] Gtk.CheckButton checkbutton_verbal;
37 
38 		CairoPreview drawing_area;
39 
PlayerHistoryDialog(ITranslations translations, PlayerHistory history)40 		public PlayerHistoryDialog (ITranslations translations, PlayerHistory history) : base (translations, "PlayerHistoryDialog.ui", "playerhistory")
41 		{
42 			string intro, built;
43 
44 			intro = Catalog.GetString ("The graph below shows the player's game score evolution.");
45 
46 			if (history.Games.Count < 2)
47 			{
48 				built = Catalog.GetString ("You need more than one game session recorded to see the score evolution.");
49 			}
50 			else
51 			{
52 				built =  String.Format (Catalog.GetPluralString ("It is built using the results of {0} recorded game session.",
53 					"It is built using the results of the last {0} recorded game sessions.",
54 					history.Games.Count),
55 					history.Games.Count);
56 			}
57 
58 			// Translators: "The graph below" +  "It is built using" sentences
59 			label_playerhistory.Text = String.Format (Catalog.GetString ("{0} {1}"), intro, built);
60 
61 			drawing_area = new CairoPreview (translations, history);
62             drawing_area.SetSizeRequest (history_preview.WidthRequest, history_preview.HeightRequest);
63 			history_preview.Add (drawing_area);
64 			drawing_area.Visible = true;
65 
66 	 		checkbutton_total.Label = Catalog.GetString ("Total");
67 	 		checkbutton_logic.Label = GameTypesDescription.GetLocalized (translations, GameTypes.LogicPuzzle);
68 	 		checkbutton_calculation.Label = GameTypesDescription.GetLocalized (translations, GameTypes.Calculation);
69 	 		checkbutton_memory.Label = GameTypesDescription.GetLocalized (translations, GameTypes.Memory);
70 	 		checkbutton_verbal.Label = GameTypesDescription.GetLocalized (translations, GameTypes.VerbalAnalogy);
71 
72 	 		checkbutton_total.Active = checkbutton_memory.Active = checkbutton_logic.Active = checkbutton_calculation.Active = checkbutton_verbal.Active = true;
73 		}
74 
OnTotalToggled(object sender, EventArgs args)75 		void OnTotalToggled (object sender, EventArgs args)
76 		{
77 			drawing_area.View.ShowTotal = checkbutton_total.Active;
78 			drawing_area.QueueDraw ();
79 		}
80 
OnLogicToggled(object sender, EventArgs args)81 		void OnLogicToggled (object sender, EventArgs args)
82 		{
83 			drawing_area.View.ShowLogic = checkbutton_logic.Active;
84 			drawing_area.QueueDraw ();
85 		}
86 
OnMemoryToggled(object sender, EventArgs args)87 		void OnMemoryToggled (object sender, EventArgs args)
88 		{
89 			drawing_area.View.ShowMemory = checkbutton_memory.Active;
90 			drawing_area.QueueDraw ();
91 		}
92 
OnCalculationToggled(object sender, EventArgs args)93 		void OnCalculationToggled (object sender, EventArgs args)
94 		{
95 			drawing_area.View.ShowCalculation = checkbutton_calculation.Active;
96 			drawing_area.QueueDraw ();
97 		}
98 
OnVerbalToggled(object sender, EventArgs args)99 		void OnVerbalToggled (object sender, EventArgs args)
100 		{
101 			drawing_area.View.ShowVerbal = checkbutton_verbal.Active;
102 			drawing_area.QueueDraw ();
103 		}
104 
105 		class CairoPreview : DrawingArea
106 		{
107 			PlayerHistoryView view;
108 
CairoPreview(ITranslations translations, PlayerHistory history)109 			public CairoPreview (ITranslations translations, PlayerHistory history)
110 			{
111 				view = new PlayerHistoryView (translations, history);
112 			}
113 
114 			public PlayerHistoryView View {
115 				get { return view; }
116 			}
117 
OnDrawn(Cairo.Context cc)118 			protected override bool OnDrawn (Cairo.Context cc)
119 			{
120 				if(!IsRealized)
121 					return false;
122 
123 				int w, h, nw, nh;
124 				double x = 0, y = 0;
125 
126 				CairoContextEx cr = new CairoContextEx (cc.Handle);
127 				cr.PangoFontDescription = PangoContext.FontDescription;
128                 w = Window.Width;
129                 h = Window.Height;
130 
131 				nh = nw = Math.Min (w, h);
132 
133 				if (nw < w) {
134 					x = (w - nw) / 2d;
135 				}
136 
137 				if (nh < h) {
138 					y = (h - nh) / 2d;
139 				}
140 
141 				cr.Translate (x, y);
142 				cr.Scale (nw, nh);
143 
144 				view.Draw (cr, nw, nh, Direction == Gtk.TextDirection.Rtl);
145 
146 				((IDisposable)cr).Dispose();
147 	   			return true;
148 			}
149 		}
150 	}
151 }
152