# HG changeset patch # User IBBoard # Date 1273176343 0 # Node ID 540c8aa6e565d782ec9ae69d2cd1bc7200da8c4a # Parent 0e38371706379917bb10807ea2ac4454cf18e04f Re #269: Handle multiple stat lines * Start to rebuild UI using DataGridView, as we can get the header height from that and it replaces the original DataGrid diff -r 0e3837170637 -r 540c8aa6e565 FrmUnit.cs --- a/FrmUnit.cs Sun May 02 15:46:24 2010 +0000 +++ b/FrmUnit.cs Thu May 06 20:05:43 2010 +0000 @@ -27,7 +27,7 @@ { private Unit unit; private Dictionary equipmentChoices = new Dictionary(); - private Dictionary dataGrids = new Dictionary(); + private Dictionary DataGridViews = new Dictionary(); private CommandStack commandStack; private System.Windows.Forms.TextBox tbUnitName; private System.Windows.Forms.NumericUpDown unitSize; @@ -103,7 +103,7 @@ for (int i = 0; i < statsCount; i++) { - DataGrid statsGrid = GetDataGrid(statsIDs[i]); + DataGridView statsGrid = GetDataGridView(statsIDs[i]); DataTable dt = (DataTable)statsGrid.DataSource; DataRow dr = dt.NewRow(); dr.ItemArray = stats[i]; @@ -111,84 +111,68 @@ } } - private DataGrid GetDataGrid(string statsID) + private DataGridView GetDataGridView(string statsID) { - DataGrid grid; + DataGridView grid; - if (dataGrids.ContainsKey(statsID)) + if (DataGridViews.ContainsKey(statsID)) { - grid = DictionaryUtils.GetValue(dataGrids, statsID); + grid = DictionaryUtils.GetValue(DataGridViews, statsID); } else { - grid = CreateDataGrid(statsID); - dataGrids[statsID] = grid; + grid = CreateDataGridView(statsID); + DataGridViews[statsID] = grid; } return grid; } - private DataGrid CreateDataGrid(string statsID) + private DataGridView CreateDataGridView(string statsID) { SystemStats sysStats = unit.Race.GameSystem.GetSystemStatsForID(statsID); StatSlot[] sysStatSlots = sysStats.StatSlots; - StatSlot[] stats = new StatSlot[sysStatSlots.Length + 1]; - stats[0] = new StatSlot("Name"); - sysStatSlots.CopyTo(stats, 1); - DataColumn[] dc = new DataColumn[stats.Length]; - DataGridTableStyle dgStyle = new DataGridTableStyle(); - dgStyle.RowHeadersVisible = false; + 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 = stats.Length; + int statsCount = statsWithName.Length; for (int i = 0; i < statsCount; i++) { - StatSlot stat = stats[i]; + StatSlot stat = statsWithName[i]; string slotName = stat.Name; dc[i] = CreateDataColumn(slotName); - dgStyle.GridColumnStyles.Add(CreateColumnStyle(slotName)); } - DataGridColumnStyle nameColStyle = dgStyle.GridColumnStyles[0]; - nameColStyle.HeaderText = Translation.GetTranslation("UnitName", "Name"); - nameColStyle.Alignment = HorizontalAlignment.Left; - DataGrid statsGrid = CreateDataGrid(); - nameColStyle.Width = statsGrid.ClientSize.Width - ((stats.Length) * 40) - 4; + DataGridView statsGrid = CreateDataGridView(); dt.Columns.AddRange(dc); statsGrid.DataSource = dt; - statsGrid.TableStyles.Add(dgStyle); + statsGrid.Columns[0].Name = Translation.GetTranslation("UnitName", "Name"); + statsGrid.Columns[0].Width = statsGrid.ClientSize.Width - ((sysStatSlots.Length) * 40) - 4; + + for (int i = 0; i < statsCount; i++) + { + statsGrid.Columns[i].CellTemplate = new StatsDataGridViewCell(); + } + return statsGrid; } private static DataColumn CreateDataColumn(string slotName) { - DataColumn tempCol = new DataColumn(slotName); - tempCol.DataType = typeof(Stat); + DataColumn tempCol = new DataColumn(slotName, typeof(Stat)); return tempCol; } - private static DataGridColumnStyle CreateColumnStyle(string slotName) + public DataGridView CreateDataGridView() { - DataGridColumnStyle colStyle = new StatColumnStyle(); - colStyle.Width = 40; - colStyle.MappingName = slotName; - colStyle.HeaderText = slotName; - colStyle.Alignment = HorizontalAlignment.Center; - colStyle.ReadOnly = true; - return colStyle; - } - - public DataGrid CreateDataGrid() - { - DataGrid statsGrid = new DataGrid(); - statsGrid.AllowNavigation = false; - statsGrid.AllowSorting = false; + DataGridView statsGrid = new DataGridView(); statsGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - statsGrid.CaptionVisible = false; statsGrid.CausesValidation = false; - statsGrid.PreferredColumnWidth = 40; statsGrid.ReadOnly = true; statsGrid.RowHeadersVisible = false; statsGrid.Size = new System.Drawing.Size(600, 88); diff -r 0e3837170637 -r 540c8aa6e565 IBBoard.WarFoundry.GUI.WinForms.csproj --- a/IBBoard.WarFoundry.GUI.WinForms.csproj Sun May 02 15:46:24 2010 +0000 +++ b/IBBoard.WarFoundry.GUI.WinForms.csproj Thu May 06 20:05:43 2010 +0000 @@ -166,6 +166,7 @@ Component + Designer diff -r 0e3837170637 -r 540c8aa6e565 UI/StatsDataGridViewCell.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/UI/StatsDataGridViewCell.cs Thu May 06 20:05:43 2010 +0000 @@ -0,0 +1,19 @@ +// This file (StatsDataGridViewCell.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2010 IBBoard. +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; +using System.ComponentModel; +using System.Windows.Forms; +using IBBoard.WarFoundry.API.Objects; + +namespace IBBoard.WarFoundry.GUI.WinForms.UI +{ + public class StatsDataGridViewCell : DataGridViewTextBoxCell + { + protected override Object GetFormattedValue(Object value, int rowIndex, ref DataGridViewCellStyle cellStyle, TypeConverter valueTypeConverter, TypeConverter formattedValueTypeConverter, DataGridViewDataErrorContexts context) + { + return value == null ? "" : ((Stat)value).SlotValueString; + } + } +}