using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Linq; using System.Windows.Forms; namespace SAGA.DotNetUtils.WinForms { public class MultiEditor : TUserControl { private IContainer components; private bool hasValueChanged; private MultiEditorItemCollection items; private List listChangedItems; public event EventHandler EditorItemValueChanged; public event EventHandler EditorValueChanged; public MultiEditor() { this.listChangedItems = new List(); this.InitializeComponent(); base.StartEnterLeaveT(); } public MultiEditor(bool startEnterLeave) { this.listChangedItems = new List(); this.InitializeComponent(); if (startEnterLeave) { base.StartEnterLeaveT(); } } protected override void Dispose(bool disposing) { if (disposing && (this.components != null)) { this.components.Dispose(); } base.Dispose(disposing); } private void EditorItem_ValueChanged(object sender, EventArgs e) { if (!this.hasValueChanged) { this.hasValueChanged = true; } MultiEditorItem item = (MultiEditorItem) sender; if (!this.listChangedItems.Contains(item)) { this.listChangedItems.Add(item); } if (this.EditorItemValueChanged != null) { this.OnEditorItemValueChanged(new EditorItemValueChangedEventArgs((MultiEditorItem) sender)); } } public bool GetChecked(string strLable) { MultiEditorItem editorItem = this.GetEditorItem(strLable); if (editorItem == null) { throw new NotFindLableException(strLable); } object obj2 = editorItem.Value; if (obj2 == null) { return false; } return Convert.ToBoolean(obj2); } public double GetDouble(string strLable) { MultiEditorItem editorItem = this.GetEditorItem(strLable); if (editorItem == null) { throw new NotFindLableException(strLable); } object obj2 = editorItem.Value; if (obj2 == null) { return 0.0; } return Convert.ToDouble(obj2); } public T GetEditorCtrl(string strLable) where T: Control { foreach (MultiEditorItem item in this.EditorItems) { if (item.Label == strLable) { return item.GetCtrl(); } } return default(T); } public MultiEditorItem GetEditorItem(string strLable) { foreach (MultiEditorItem item in this.EditorItems) { if (item.Label == strLable) { return item; } } return null; } public T GetEditorItemT(string strLable) where T: MultiEditorItem { foreach (MultiEditorItem item in this.EditorItems) { if (item.Label == strLable) { return (item as T); } } return default(T); } public bool? GetEnabled(string strLable) { foreach (MultiEditorItem item in this.EditorItems) { if (item.Label == strLable) { return new bool?(item.Enabled); } } return null; } public int GetInteger(string strLable) { MultiEditorItem editorItem = this.GetEditorItem(strLable); if (editorItem == null) { throw new NotFindLableException(strLable); } object obj2 = editorItem.Value; if (obj2 == null) { return 0; } return Convert.ToInt32(obj2); } public Dictionary GetNamedParams() { Dictionary dictionary = new Dictionary(); foreach (MultiEditorItem item in this.Items) { if (item.Enabled) { foreach (KeyValuePair pair in item.GetNamedParams()) { dictionary.Add(pair.Key, pair.Value); } } } return dictionary; } public Dictionary GetNamedParamsByLalels(string[] keys) { Dictionary dictionary = new Dictionary(); if ((keys == null) || (keys.Length != this.Items.Count)) { throw new InvalidOperationException(); } MultiEditorItem[] editorItems = this.EditorItems; for (int i = 0; i < editorItems.Length; i++) { MultiEditorItem item = editorItems[i]; dictionary.Add(keys[i], item.Value); } return dictionary; } private void InitializeComponent() { this.components = new Container(); base.AutoScaleMode = AutoScaleMode.Font; } public void LoadEditorItems(params MultiEditorItem[] items) { this.LoadEditorItems(items, null); } public void LoadEditorItems(MultiEditorItem[] items, int? intEditorWidth) { base.Controls.Clear(); this.Items.Clear(); this.Items.AddRang(items); this.LoadItemCtrols(intEditorWidth); } public void LoadEditorItems(MultiEditorItem[] items, int intStartIndex, int? intEditorWidth) { if ((intStartIndex <= 0) || (intStartIndex >= this.Items.Count)) { if (intStartIndex <= 0) { base.Controls.Clear(); this.Items.Clear(); } } else { for (int i = base.Controls.Count - 2; i >= 0; i -= 2) { int index = i * 2; int num3 = (i * 2) + 1; base.Controls.RemoveAt(num3); base.Controls.RemoveAt(index); } this.Items.RomoveSIndexToEIndex(intStartIndex); } this.Items.AddRang(items); this.LoadItemCtrols(intEditorWidth); } public void LoadGroupEditorItems(List listItems, string[] groupNames, int intColumns, int? intEditorWidth) { base.Controls.Clear(); this.Items.Clear(); foreach (MultiEditorItem[] itemArray in listItems) { this.Items.AddRang(itemArray); } this.LoadGroupItemCtrols(listItems, groupNames, intColumns, intEditorWidth); } protected virtual void LoadGroupItemCtrols(List listItems, string[] groupNames, int intColumns, int? intEditorWidth) { int[] source = new int[intColumns]; List list = new List(); foreach (MultiEditorItem[] itemArray in listItems) { int num = (itemArray.Length / intColumns) + (((itemArray.Length % intColumns) == 0) ? 0 : 1); MultiEditorItem[,] itemArray2 = new MultiEditorItem[num, intColumns]; for (int j = 0; j < itemArray.Length; j++) { itemArray2[j / intColumns, j % intColumns] = itemArray[j]; } list.Add(itemArray2); } using (Graphics graphics = base.CreateGraphics()) { for (int k = 0; k < intColumns; k++) { MultiEditorItemCollection items = new MultiEditorItemCollection(); foreach (MultiEditorItem[,] itemArray3 in list) { for (int m = 0; m < itemArray3.GetLength(0); m++) { MultiEditorItem item = itemArray3[m, k]; if (item != null) { items.Add(item); } } } int itemMinLableWidth = items.ItemMinLableWidth; source[k] = Math.Max(items.GetMaxLableWidth(graphics, this.Font), itemMinLableWidth); } } if (!this.AutoScroll) { this.AutoScroll = true; } int num8 = 5; int num9 = (((base.Width - (3 * num8)) - source.Sum()) - (intColumns * num8)) / intColumns; num9 = Math.Max(60, num9); if (intEditorWidth.HasValue) { num9 = intEditorWidth.Value; } int num10 = SystemInformation.VerticalScrollBarWidth + 5; num9 -= num10 / 2; int x = 5; int y = 0; new List(); for (int i = 0; i < list.Count; i++) { GroupBox box = new GroupBox { Name = "groupBox" + (i + 1), Width = ((base.Width - num8) - num10) - 2, Text = groupNames[i] }; MultiEditorItem[,] itemArray4 = list[i]; int num14 = 0x10; for (int n = 0; n < itemArray4.GetLength(0); n++) { int num16 = 2; int height = 0x15; for (int num18 = 0; num18 < itemArray4.GetLength(1); num18++) { MultiEditorItem item2 = itemArray4[n, num18]; if (item2 == null) { break; } Label labelControl = item2.LabelControl; labelControl.AutoSize = false; labelControl.Width = source[num18]; labelControl.TextAlign = ContentAlignment.MiddleRight; labelControl.Location = new Point(num16, num14); if (item2.WrapLabels.Length > 1) { labelControl.Height = item2.WrapLabels.Length * 15; } else { labelControl.Height = 0x15; } height = labelControl.Height; box.Controls.Add(labelControl); Control control = item2.Control; control.Name = base.Name + box.Name + "_editor_" + item2.Label; control.Width = num9; num16 = (num16 + labelControl.Width) + num8; control.Location = new Point(num16, num14); box.Controls.Add(control); num16 += control.Width; item2.ValueChanged += new EventHandler(this.EditorItem_ValueChanged); } num14 = (num14 + height) + num8; } box.Height = num14; box.Location = new Point(x, y); y = (box.Location.Y + box.Height) + num8; base.Controls.Add(box); } } protected virtual void LoadItemCtrols(int? intEditorWidth) { int itemMinLableWidth; int maxLableWidth; MultiEditorItem[] editorItems = this.EditorItems; using (Graphics graphics = base.CreateGraphics()) { itemMinLableWidth = this.Items.ItemMinLableWidth; maxLableWidth = this.Items.GetMaxLableWidth(graphics, this.Font); } int num3 = Math.Max(maxLableWidth, itemMinLableWidth); if (!this.AutoScroll) { this.AutoScroll = true; } int x = 2; int y = 2; int num6 = 5; if (base.Controls.Count >= 2) { Control control = base.Controls[base.Controls.Count - 2]; y = (control.Location.Y + control.Height) + num6; } int num7 = y; for (int i = 0; i < editorItems.Length; i++) { MultiEditorItem item = editorItems[i]; Control control2 = item.Control; num7 = (num7 + control2.Height) + num6; if (item.IsWrapLabel) { num7 += 8; } } bool flag = false; int num9 = SystemInformation.VerticalScrollBarWidth + 5; if (num7 < base.Height) { num9 = 0; } for (int j = 0; j < editorItems.Length; j++) { MultiEditorItem item2 = editorItems[j]; Label labelControl = item2.LabelControl; labelControl.AutoSize = false; labelControl.Width = num3; labelControl.TextAlign = ContentAlignment.MiddleRight; labelControl.Location = new Point(x, y); if (item2.WrapLabels.Length > 1) { labelControl.Height = item2.WrapLabels.Length * 15; } else { labelControl.Height = 0x15; } base.Controls.Add(labelControl); Control control3 = item2.Control; if (string.IsNullOrEmpty(control3.Name)) { control3.Name = base.Name + "_editor_" + item2.Label; } if (intEditorWidth.HasValue) { control3.Width = intEditorWidth.Value; } else { if (!flag) { flag = true; } control3.Width = Math.Max(((base.Width - num3) - num9) - (num6 * 2), 60); } control3.Location = new Point((x + labelControl.Width) + num6, y); base.Controls.Add(control3); y = (control3.Location.Y + labelControl.Height) + num6; item2.ValueChanged += new EventHandler(this.EditorItem_ValueChanged); } this.Refresh(); } protected virtual void OnEditorItemValueChanged(EditorItemValueChangedEventArgs e) { if (this.EditorItemValueChanged != null) { this.EditorItemValueChanged(this, e); } } protected virtual void OnEditorValueChanged(EditorValueChangedEventArgs e) { if (this.EditorValueChanged != null) { this.EditorValueChanged(this, e); } } protected override void OnMouseLeaveT() { base.OnMouseLeaveT(); if (this.hasValueChanged) { this.hasValueChanged = false; MultiEditorItem[] editorItems = this.listChangedItems.ToArray(); this.listChangedItems.Clear(); this.OnEditorValueChanged(new EditorValueChangedEventArgs(editorItems)); } } public bool SetEnabled(bool enabled) { base.Enabled = enabled; foreach (MultiEditorItem item in this.EditorItems) { item.Enabled = enabled; } return true; } public bool SetEnabled(string strLable, bool enabled) { foreach (MultiEditorItem item in this.EditorItems) { if (item.Label == strLable) { item.Enabled = enabled; return true; } } return false; } public MultiEditorItem[] EditorItems { get { return this.Items.ToArray(); } } protected MultiEditorItemCollection Items { get { if (this.items == null) { this.items = new MultiEditorItemCollection(); } return this.items; } } } }