| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- 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<MultiEditorItem> listChangedItems;
- public event EventHandler<EditorItemValueChangedEventArgs> EditorItemValueChanged;
- public event EventHandler<EditorValueChangedEventArgs> EditorValueChanged;
- public MultiEditor()
- {
- this.listChangedItems = new List<MultiEditorItem>();
- this.InitializeComponent();
- base.StartEnterLeaveT();
- }
- public MultiEditor(bool startEnterLeave)
- {
- this.listChangedItems = new List<MultiEditorItem>();
- 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<T>(string strLable) where T: Control
- {
- foreach (MultiEditorItem item in this.EditorItems)
- {
- if (item.Label == strLable)
- {
- return item.GetCtrl<T>();
- }
- }
- 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<T>(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<ParameterName, object> GetNamedParams()
- {
- Dictionary<ParameterName, object> dictionary = new Dictionary<ParameterName, object>();
- foreach (MultiEditorItem item in this.Items)
- {
- if (item.Enabled)
- {
- foreach (KeyValuePair<ParameterName, object> pair in item.GetNamedParams())
- {
- dictionary.Add(pair.Key, pair.Value);
- }
- }
- }
- return dictionary;
- }
- public Dictionary<string, object> GetNamedParamsByLalels(string[] keys)
- {
- Dictionary<string, object> dictionary = new Dictionary<string, object>();
- 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<MultiEditorItem[]> 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<MultiEditorItem[]> listItems, string[] groupNames, int intColumns, int? intEditorWidth)
- {
- int[] source = new int[intColumns];
- List<MultiEditorItem[,]> list = new List<MultiEditorItem[,]>();
- 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<GroupBox>();
- 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<MultiEditorItem>();
- }
- }
- protected MultiEditorItemCollection Items
- {
- get
- {
- if (this.items == null)
- {
- this.items = new MultiEditorItemCollection();
- }
- return this.items;
- }
- }
- }
- }
|