diff FrmUnit.cs @ 153:fcd90498d9a2

Re #269: Handle multiple stat lines * Improve rendering of stats * Fix column widths * Set width and height based on content Also: * Make FrmUnit title contain unit type as we're using it more now Still needs more testing and fixing
author IBBoard <dev@ibboard.co.uk>
date Wed, 26 May 2010 20:05:20 +0000
parents 540c8aa6e565
children 24d0cfaa95da
line wrap: on
line diff
--- a/FrmUnit.cs	Fri May 14 19:46:29 2010 +0000
+++ b/FrmUnit.cs	Wed May 26 20:05:20 2010 +0000
@@ -25,6 +25,8 @@
 	/// </summary>
 	public class FrmUnit : IBBoard.Windows.Forms.IBBForm
 	{
+		private static readonly int COLUMN_WIDTH = 40;
+		private static readonly int BORDER_WIDTH = 1;
 		private Unit unit;
 		private Dictionary<UnitEquipmentItem, UnitEquipmentChoice> equipmentChoices = new Dictionary<UnitEquipmentItem, UnitEquipmentChoice>();
 		private Dictionary<string, DataGridView> DataGridViews = new Dictionary<string, DataGridView>();
@@ -80,9 +82,14 @@
 		private void TranslateForm()
 		{
 			ControlTranslator.TranslateControl(this);
+			SetUnitName();
+			RefreshUnitEquipment();
+		}
+
+		private void SetUnitName()
+		{
 			tbUnitName.Text = unit.Name;
-			Text = Translation.GetTranslation("FrmUnit", "{0}", unit.Name);
-			RefreshUnitEquipment();
+			Text = Translation.GetTranslation("FrmUnit", "{0} ({1})", unit.Name, unit.UnitType.Name);
 		}
 
 		void unit_PointsValueChanged(WarFoundryObject obj, double oldValue, double newValue)
@@ -108,6 +115,7 @@
 				DataRow dr = dt.NewRow();
 				dr.ItemArray = stats[i];
 				dt.Rows.Add(dr);
+				statsGrid.SelectedCells[0].Selected = false;
 			}
 		}
 
@@ -135,23 +143,22 @@
 			StatSlot[] statsWithName = new StatSlot[sysStatSlots.Length + 1];
 			statsWithName[0] = new StatSlot("Name");
 			sysStatSlots.CopyTo(statsWithName, 1);
-			DataColumn[] dc = new DataColumn[statsWithName.Length];
 			DataTable dt = new DataTable();
 			int statsCount = statsWithName.Length;
+			DataGridView statsGrid = CreateDataGridView();
+			statsGrid.DataSource = dt;
 
 			for (int i = 0; i < statsCount; i++)
 			{
 				StatSlot stat = statsWithName[i];
 				string slotName = stat.Name;
-				dc[i] = CreateDataColumn(slotName);
+				dt.Columns.Add(CreateDataColumn(slotName));
+				statsGrid.Columns[i].Width = COLUMN_WIDTH;
 			}
 
-			DataGridView statsGrid = CreateDataGridView();
-
-			dt.Columns.AddRange(dc);
-			statsGrid.DataSource = dt;
-			statsGrid.Columns[0].Name = Translation.GetTranslation("UnitName", "Name");
-			statsGrid.Columns[0].Width = statsGrid.ClientSize.Width - ((sysStatSlots.Length) * 40) - 4;
+			DataGridViewColumn nameColumn = statsGrid.Columns[0];
+			nameColumn.Name = Translation.GetTranslation("UnitName", "Name");
+			nameColumn.Width = statsGrid.ClientSize.Width - (sysStatSlots.Length * (COLUMN_WIDTH + BORDER_WIDTH));
 
 			for (int i = 0; i < statsCount; i++)
 			{
@@ -177,11 +184,31 @@
 			statsGrid.RowHeadersVisible = false;
 			statsGrid.Size = new System.Drawing.Size(600, 88);
 			statsGrid.TabStop = false;
+			statsGrid.AllowUserToAddRows = false;
+			statsGrid.ScrollBars = ScrollBars.None;
+			statsGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
+			statsGrid.BorderStyle = BorderStyle.None;
+			statsGrid.BackgroundColor = SystemColors.Control;
+			statsGrid.RowsAdded += new DataGridViewRowsAddedEventHandler(statsGrid_RowsAdded);
 			statsPanel.Controls.Add(statsGrid);
 			statsGrid.Width = statsPanel.Width;
 			return statsGrid;
 		}
 
+		private void statsGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
+		{
+			if (sender is DataGridView)
+			{
+				SetGridHeight((DataGridView)sender);
+			}
+		}
+
+		private static void SetGridHeight(DataGridView statsGrid)
+		{
+			DataGridViewRowCollection rows = statsGrid.Rows;
+			statsGrid.Height = statsGrid.Columns[0].HeaderCell.Size.Height + (rows.Count * rows[0].Height);
+		}
+
 		private void SetWeapons()
 		{
 			foreach (UnitEquipmentItem item in unit.GetEquipment())
@@ -483,9 +510,7 @@
 		{
 			if (obj is Unit && obj.Equals(unit))
 			{
-				Unit u = (Unit) obj;
-				tbUnitName.Text = obj.Name;
-				Text = obj.Name;
+				SetUnitName();
 			}
 		}
 
@@ -494,7 +519,6 @@
 			if (obj is Unit && obj.Equals(unit))
 			{
 				unitSize.Value = newValue;
-
 				RefreshUnitEquipment();
 			}
 		}