MultiEditor.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. namespace SAGA.DotNetUtils.WinForms
  8. {
  9. public class MultiEditor : TUserControl
  10. {
  11. private IContainer components;
  12. private bool hasValueChanged;
  13. private MultiEditorItemCollection items;
  14. private List<MultiEditorItem> listChangedItems;
  15. public event EventHandler<EditorItemValueChangedEventArgs> EditorItemValueChanged;
  16. public event EventHandler<EditorValueChangedEventArgs> EditorValueChanged;
  17. public MultiEditor()
  18. {
  19. this.listChangedItems = new List<MultiEditorItem>();
  20. this.InitializeComponent();
  21. base.StartEnterLeaveT();
  22. }
  23. public MultiEditor(bool startEnterLeave)
  24. {
  25. this.listChangedItems = new List<MultiEditorItem>();
  26. this.InitializeComponent();
  27. if (startEnterLeave)
  28. {
  29. base.StartEnterLeaveT();
  30. }
  31. }
  32. protected override void Dispose(bool disposing)
  33. {
  34. if (disposing && (this.components != null))
  35. {
  36. this.components.Dispose();
  37. }
  38. base.Dispose(disposing);
  39. }
  40. private void EditorItem_ValueChanged(object sender, EventArgs e)
  41. {
  42. if (!this.hasValueChanged)
  43. {
  44. this.hasValueChanged = true;
  45. }
  46. MultiEditorItem item = (MultiEditorItem) sender;
  47. if (!this.listChangedItems.Contains(item))
  48. {
  49. this.listChangedItems.Add(item);
  50. }
  51. if (this.EditorItemValueChanged != null)
  52. {
  53. this.OnEditorItemValueChanged(new EditorItemValueChangedEventArgs((MultiEditorItem) sender));
  54. }
  55. }
  56. public bool GetChecked(string strLable)
  57. {
  58. MultiEditorItem editorItem = this.GetEditorItem(strLable);
  59. if (editorItem == null)
  60. {
  61. throw new NotFindLableException(strLable);
  62. }
  63. object obj2 = editorItem.Value;
  64. if (obj2 == null)
  65. {
  66. return false;
  67. }
  68. return Convert.ToBoolean(obj2);
  69. }
  70. public double GetDouble(string strLable)
  71. {
  72. MultiEditorItem editorItem = this.GetEditorItem(strLable);
  73. if (editorItem == null)
  74. {
  75. throw new NotFindLableException(strLable);
  76. }
  77. object obj2 = editorItem.Value;
  78. if (obj2 == null)
  79. {
  80. return 0.0;
  81. }
  82. return Convert.ToDouble(obj2);
  83. }
  84. public T GetEditorCtrl<T>(string strLable) where T: Control
  85. {
  86. foreach (MultiEditorItem item in this.EditorItems)
  87. {
  88. if (item.Label == strLable)
  89. {
  90. return item.GetCtrl<T>();
  91. }
  92. }
  93. return default(T);
  94. }
  95. public MultiEditorItem GetEditorItem(string strLable)
  96. {
  97. foreach (MultiEditorItem item in this.EditorItems)
  98. {
  99. if (item.Label == strLable)
  100. {
  101. return item;
  102. }
  103. }
  104. return null;
  105. }
  106. public T GetEditorItemT<T>(string strLable) where T: MultiEditorItem
  107. {
  108. foreach (MultiEditorItem item in this.EditorItems)
  109. {
  110. if (item.Label == strLable)
  111. {
  112. return (item as T);
  113. }
  114. }
  115. return default(T);
  116. }
  117. public bool? GetEnabled(string strLable)
  118. {
  119. foreach (MultiEditorItem item in this.EditorItems)
  120. {
  121. if (item.Label == strLable)
  122. {
  123. return new bool?(item.Enabled);
  124. }
  125. }
  126. return null;
  127. }
  128. public int GetInteger(string strLable)
  129. {
  130. MultiEditorItem editorItem = this.GetEditorItem(strLable);
  131. if (editorItem == null)
  132. {
  133. throw new NotFindLableException(strLable);
  134. }
  135. object obj2 = editorItem.Value;
  136. if (obj2 == null)
  137. {
  138. return 0;
  139. }
  140. return Convert.ToInt32(obj2);
  141. }
  142. public Dictionary<ParameterName, object> GetNamedParams()
  143. {
  144. Dictionary<ParameterName, object> dictionary = new Dictionary<ParameterName, object>();
  145. foreach (MultiEditorItem item in this.Items)
  146. {
  147. if (item.Enabled)
  148. {
  149. foreach (KeyValuePair<ParameterName, object> pair in item.GetNamedParams())
  150. {
  151. dictionary.Add(pair.Key, pair.Value);
  152. }
  153. }
  154. }
  155. return dictionary;
  156. }
  157. public Dictionary<string, object> GetNamedParamsByLalels(string[] keys)
  158. {
  159. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  160. if ((keys == null) || (keys.Length != this.Items.Count))
  161. {
  162. throw new InvalidOperationException();
  163. }
  164. MultiEditorItem[] editorItems = this.EditorItems;
  165. for (int i = 0; i < editorItems.Length; i++)
  166. {
  167. MultiEditorItem item = editorItems[i];
  168. dictionary.Add(keys[i], item.Value);
  169. }
  170. return dictionary;
  171. }
  172. private void InitializeComponent()
  173. {
  174. this.components = new Container();
  175. base.AutoScaleMode = AutoScaleMode.Font;
  176. }
  177. public void LoadEditorItems(params MultiEditorItem[] items)
  178. {
  179. this.LoadEditorItems(items, null);
  180. }
  181. public void LoadEditorItems(MultiEditorItem[] items, int? intEditorWidth)
  182. {
  183. base.Controls.Clear();
  184. this.Items.Clear();
  185. this.Items.AddRang(items);
  186. this.LoadItemCtrols(intEditorWidth);
  187. }
  188. public void LoadEditorItems(MultiEditorItem[] items, int intStartIndex, int? intEditorWidth)
  189. {
  190. if ((intStartIndex <= 0) || (intStartIndex >= this.Items.Count))
  191. {
  192. if (intStartIndex <= 0)
  193. {
  194. base.Controls.Clear();
  195. this.Items.Clear();
  196. }
  197. }
  198. else
  199. {
  200. for (int i = base.Controls.Count - 2; i >= 0; i -= 2)
  201. {
  202. int index = i * 2;
  203. int num3 = (i * 2) + 1;
  204. base.Controls.RemoveAt(num3);
  205. base.Controls.RemoveAt(index);
  206. }
  207. this.Items.RomoveSIndexToEIndex(intStartIndex);
  208. }
  209. this.Items.AddRang(items);
  210. this.LoadItemCtrols(intEditorWidth);
  211. }
  212. public void LoadGroupEditorItems(List<MultiEditorItem[]> listItems, string[] groupNames, int intColumns, int? intEditorWidth)
  213. {
  214. base.Controls.Clear();
  215. this.Items.Clear();
  216. foreach (MultiEditorItem[] itemArray in listItems)
  217. {
  218. this.Items.AddRang(itemArray);
  219. }
  220. this.LoadGroupItemCtrols(listItems, groupNames, intColumns, intEditorWidth);
  221. }
  222. protected virtual void LoadGroupItemCtrols(List<MultiEditorItem[]> listItems, string[] groupNames, int intColumns, int? intEditorWidth)
  223. {
  224. int[] source = new int[intColumns];
  225. List<MultiEditorItem[,]> list = new List<MultiEditorItem[,]>();
  226. foreach (MultiEditorItem[] itemArray in listItems)
  227. {
  228. int num = (itemArray.Length / intColumns) + (((itemArray.Length % intColumns) == 0) ? 0 : 1);
  229. MultiEditorItem[,] itemArray2 = new MultiEditorItem[num, intColumns];
  230. for (int j = 0; j < itemArray.Length; j++)
  231. {
  232. itemArray2[j / intColumns, j % intColumns] = itemArray[j];
  233. }
  234. list.Add(itemArray2);
  235. }
  236. using (Graphics graphics = base.CreateGraphics())
  237. {
  238. for (int k = 0; k < intColumns; k++)
  239. {
  240. MultiEditorItemCollection items = new MultiEditorItemCollection();
  241. foreach (MultiEditorItem[,] itemArray3 in list)
  242. {
  243. for (int m = 0; m < itemArray3.GetLength(0); m++)
  244. {
  245. MultiEditorItem item = itemArray3[m, k];
  246. if (item != null)
  247. {
  248. items.Add(item);
  249. }
  250. }
  251. }
  252. int itemMinLableWidth = items.ItemMinLableWidth;
  253. source[k] = Math.Max(items.GetMaxLableWidth(graphics, this.Font), itemMinLableWidth);
  254. }
  255. }
  256. if (!this.AutoScroll)
  257. {
  258. this.AutoScroll = true;
  259. }
  260. int num8 = 5;
  261. int num9 = (((base.Width - (3 * num8)) - source.Sum()) - (intColumns * num8)) / intColumns;
  262. num9 = Math.Max(60, num9);
  263. if (intEditorWidth.HasValue)
  264. {
  265. num9 = intEditorWidth.Value;
  266. }
  267. int num10 = SystemInformation.VerticalScrollBarWidth + 5;
  268. num9 -= num10 / 2;
  269. int x = 5;
  270. int y = 0;
  271. new List<GroupBox>();
  272. for (int i = 0; i < list.Count; i++)
  273. {
  274. GroupBox box = new GroupBox {
  275. Name = "groupBox" + (i + 1),
  276. Width = ((base.Width - num8) - num10) - 2,
  277. Text = groupNames[i]
  278. };
  279. MultiEditorItem[,] itemArray4 = list[i];
  280. int num14 = 0x10;
  281. for (int n = 0; n < itemArray4.GetLength(0); n++)
  282. {
  283. int num16 = 2;
  284. int height = 0x15;
  285. for (int num18 = 0; num18 < itemArray4.GetLength(1); num18++)
  286. {
  287. MultiEditorItem item2 = itemArray4[n, num18];
  288. if (item2 == null)
  289. {
  290. break;
  291. }
  292. Label labelControl = item2.LabelControl;
  293. labelControl.AutoSize = false;
  294. labelControl.Width = source[num18];
  295. labelControl.TextAlign = ContentAlignment.MiddleRight;
  296. labelControl.Location = new Point(num16, num14);
  297. if (item2.WrapLabels.Length > 1)
  298. {
  299. labelControl.Height = item2.WrapLabels.Length * 15;
  300. }
  301. else
  302. {
  303. labelControl.Height = 0x15;
  304. }
  305. height = labelControl.Height;
  306. box.Controls.Add(labelControl);
  307. Control control = item2.Control;
  308. control.Name = base.Name + box.Name + "_editor_" + item2.Label;
  309. control.Width = num9;
  310. num16 = (num16 + labelControl.Width) + num8;
  311. control.Location = new Point(num16, num14);
  312. box.Controls.Add(control);
  313. num16 += control.Width;
  314. item2.ValueChanged += new EventHandler(this.EditorItem_ValueChanged);
  315. }
  316. num14 = (num14 + height) + num8;
  317. }
  318. box.Height = num14;
  319. box.Location = new Point(x, y);
  320. y = (box.Location.Y + box.Height) + num8;
  321. base.Controls.Add(box);
  322. }
  323. }
  324. protected virtual void LoadItemCtrols(int? intEditorWidth)
  325. {
  326. int itemMinLableWidth;
  327. int maxLableWidth;
  328. MultiEditorItem[] editorItems = this.EditorItems;
  329. using (Graphics graphics = base.CreateGraphics())
  330. {
  331. itemMinLableWidth = this.Items.ItemMinLableWidth;
  332. maxLableWidth = this.Items.GetMaxLableWidth(graphics, this.Font);
  333. }
  334. int num3 = Math.Max(maxLableWidth, itemMinLableWidth);
  335. if (!this.AutoScroll)
  336. {
  337. this.AutoScroll = true;
  338. }
  339. int x = 2;
  340. int y = 2;
  341. int num6 = 5;
  342. if (base.Controls.Count >= 2)
  343. {
  344. Control control = base.Controls[base.Controls.Count - 2];
  345. y = (control.Location.Y + control.Height) + num6;
  346. }
  347. int num7 = y;
  348. for (int i = 0; i < editorItems.Length; i++)
  349. {
  350. MultiEditorItem item = editorItems[i];
  351. Control control2 = item.Control;
  352. num7 = (num7 + control2.Height) + num6;
  353. if (item.IsWrapLabel)
  354. {
  355. num7 += 8;
  356. }
  357. }
  358. bool flag = false;
  359. int num9 = SystemInformation.VerticalScrollBarWidth + 5;
  360. if (num7 < base.Height)
  361. {
  362. num9 = 0;
  363. }
  364. for (int j = 0; j < editorItems.Length; j++)
  365. {
  366. MultiEditorItem item2 = editorItems[j];
  367. Label labelControl = item2.LabelControl;
  368. labelControl.AutoSize = false;
  369. labelControl.Width = num3;
  370. labelControl.TextAlign = ContentAlignment.MiddleRight;
  371. labelControl.Location = new Point(x, y);
  372. if (item2.WrapLabels.Length > 1)
  373. {
  374. labelControl.Height = item2.WrapLabels.Length * 15;
  375. }
  376. else
  377. {
  378. labelControl.Height = 0x15;
  379. }
  380. base.Controls.Add(labelControl);
  381. Control control3 = item2.Control;
  382. if (string.IsNullOrEmpty(control3.Name))
  383. {
  384. control3.Name = base.Name + "_editor_" + item2.Label;
  385. }
  386. if (intEditorWidth.HasValue)
  387. {
  388. control3.Width = intEditorWidth.Value;
  389. }
  390. else
  391. {
  392. if (!flag)
  393. {
  394. flag = true;
  395. }
  396. control3.Width = Math.Max(((base.Width - num3) - num9) - (num6 * 2), 60);
  397. }
  398. control3.Location = new Point((x + labelControl.Width) + num6, y);
  399. base.Controls.Add(control3);
  400. y = (control3.Location.Y + labelControl.Height) + num6;
  401. item2.ValueChanged += new EventHandler(this.EditorItem_ValueChanged);
  402. }
  403. this.Refresh();
  404. }
  405. protected virtual void OnEditorItemValueChanged(EditorItemValueChangedEventArgs e)
  406. {
  407. if (this.EditorItemValueChanged != null)
  408. {
  409. this.EditorItemValueChanged(this, e);
  410. }
  411. }
  412. protected virtual void OnEditorValueChanged(EditorValueChangedEventArgs e)
  413. {
  414. if (this.EditorValueChanged != null)
  415. {
  416. this.EditorValueChanged(this, e);
  417. }
  418. }
  419. protected override void OnMouseLeaveT()
  420. {
  421. base.OnMouseLeaveT();
  422. if (this.hasValueChanged)
  423. {
  424. this.hasValueChanged = false;
  425. MultiEditorItem[] editorItems = this.listChangedItems.ToArray();
  426. this.listChangedItems.Clear();
  427. this.OnEditorValueChanged(new EditorValueChangedEventArgs(editorItems));
  428. }
  429. }
  430. public bool SetEnabled(bool enabled)
  431. {
  432. base.Enabled = enabled;
  433. foreach (MultiEditorItem item in this.EditorItems)
  434. {
  435. item.Enabled = enabled;
  436. }
  437. return true;
  438. }
  439. public bool SetEnabled(string strLable, bool enabled)
  440. {
  441. foreach (MultiEditorItem item in this.EditorItems)
  442. {
  443. if (item.Label == strLable)
  444. {
  445. item.Enabled = enabled;
  446. return true;
  447. }
  448. }
  449. return false;
  450. }
  451. public MultiEditorItem[] EditorItems
  452. {
  453. get
  454. {
  455. return this.Items.ToArray<MultiEditorItem>();
  456. }
  457. }
  458. protected MultiEditorItemCollection Items
  459. {
  460. get
  461. {
  462. if (this.items == null)
  463. {
  464. this.items = new MultiEditorItemCollection();
  465. }
  466. return this.items;
  467. }
  468. }
  469. }
  470. }