PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Rusty/Core/Gui/Gui.cs

http://github.com/polyethene/IronAHK
C# | 2007 lines | 1634 code | 327 blank | 46 comment | 442 complexity | ec3dc8a92590f112873d2ae330b6e0d8 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Windows.Forms;
  6. namespace IronAHK.Rusty
  7. {
  8. partial class Core
  9. {
  10. // TODO: organise Gui.cs
  11. /// <summary>
  12. /// Creates and manages windows and controls.
  13. /// </summary>
  14. /// <param name="Command">
  15. /// <list type="bullet">
  16. /// <item><term>Add</term>: <description>creates controls.</description></item>
  17. /// <item><term>Show</term>: <description>display or move the window.</description></item>
  18. /// <item><term>Submit</term>: <description>saves user input.</description></item>
  19. /// <item><term>Hide</term>: <description>hides the window.</description></item>
  20. /// <item><term>Destroy</term>: <description>deletes the window.</description></item>
  21. /// <item><term>Font</term>: <description>sets the default font style for subsequently created controls.</description></item>
  22. /// <item><term>Color</term>: <description>sets the color for the window or controls.</description></item>
  23. /// <item><term>Margin</term>: <description>sets the spacing used between the edges of the window and controls when an absolute position is unspecified.</description></item>
  24. /// <item><term>Options</term>: <description>sets various options for the appearance and behaviour of the window.</description></item>
  25. /// <item><term>Menu</term>: <description>associates a menu bar with the window.</description></item>
  26. /// <item><term>Minimize/Maximize/Restore</term>: <description>performs the indicated operation on the window.</description></item>
  27. /// <item><term>Flash</term>: <description>blinks the window in the task bar.</description></item>
  28. /// <item><term>Default</term>: <description>changes the default window on the current thread.</description></item>
  29. /// </list>
  30. /// </param>
  31. /// <param name="Param2"></param>
  32. /// <param name="Param3"></param>
  33. /// <param name="Param4"></param>
  34. public static void Gui(string Command, string Param2, string Param3, string Param4)
  35. {
  36. if (guis == null)
  37. guis = new Dictionary<string, Form>();
  38. string id = GuiId(ref Command);
  39. if (!guis.ContainsKey(id))
  40. guis.Add(id, GuiCreateWindow(id));
  41. switch (Command.ToLowerInvariant())
  42. {
  43. #region Add
  44. case Keyword_Add:
  45. {
  46. Control control = null;
  47. GuiControlEdit(ref control, guis[id], Param2, Param3, Param4);
  48. GuiAssociatedInfo(control).LastControl = control;
  49. }
  50. break;
  51. #endregion
  52. #region Show
  53. case Keyword_Show:
  54. {
  55. bool center = false, cX = false, cY = false, auto = false, min = false, max = false, restore = false, hide = false;
  56. int?[] pos = { null, null, null, null };
  57. foreach (var option in ParseOptions(Param2))
  58. {
  59. string mode = option.ToLowerInvariant();
  60. int select = -1;
  61. switch (mode[0])
  62. {
  63. case 'w': select = 0; break;
  64. case 'h': select = 1; break;
  65. case 'x': select = 2; break;
  66. case 'y': select = 3; break;
  67. }
  68. if (select == -1)
  69. {
  70. switch (mode)
  71. {
  72. case Keyword_Center: center = true; break;
  73. case Keyword_AutoSize: auto = true; break;
  74. case Keyword_Maximize: max = true; break;
  75. case Keyword_Minimize: min = true; break;
  76. case Keyword_Restore: restore = true; break;
  77. case Keyword_NoActivate: break;
  78. case Keyword_NA: break;
  79. case Keyword_Hide: hide = true; break;
  80. }
  81. }
  82. else
  83. {
  84. mode = mode.Substring(1);
  85. int n;
  86. if (mode.Equals(Keyword_Center, StringComparison.OrdinalIgnoreCase))
  87. {
  88. if (select == 2)
  89. cX = true;
  90. else
  91. cY = true;
  92. }
  93. else if (mode.Length != 0 && int.TryParse(mode, out n))
  94. pos[select] = n;
  95. }
  96. }
  97. if (auto || pos[0] == null && pos[1] == null)
  98. {
  99. guis[id].Size = guis[id].PreferredSize;
  100. var status = GuiAssociatedInfo(guis[id]).StatusBar;
  101. int d = status == null ? 0 : status.Height;
  102. if (d > 0)
  103. guis[id].ClientSize = new Size(guis[id].ClientSize.Width, guis[id].ClientSize.Height + d);
  104. }
  105. else
  106. {
  107. var size = guis[id].PreferredSize;
  108. if (pos[0] != null)
  109. size.Width = (int)pos[0];
  110. if (pos[1] != null)
  111. size.Height = (int)pos[1];
  112. guis[id].ClientSize = size;
  113. }
  114. var location = new Point();
  115. if (pos[2] != null)
  116. location.X = (int)pos[2];
  117. if (pos[3] != null)
  118. location.Y = (int)pos[3];
  119. var screen = Screen.PrimaryScreen.Bounds;
  120. if (location == null)
  121. center = true;
  122. if (center)
  123. cX = cY = true;
  124. if (cX)
  125. location.X = (screen.Width - guis[id].Size.Width) / 2 + screen.X;
  126. if (cY)
  127. location.Y = (screen.Height - guis[id].Size.Height) / 2 + screen.Y;
  128. guis[id].StartPosition = FormStartPosition.Manual;
  129. guis[id].Location = location;
  130. guis[id].Text = Param3;
  131. if (hide)
  132. guis[id].Hide();
  133. else
  134. guis[id].Show();
  135. guis[id].ResumeLayout(true);
  136. if (min)
  137. guis[id].WindowState = FormWindowState.Minimized;
  138. else if (max)
  139. guis[id].WindowState = FormWindowState.Maximized;
  140. else if (restore)
  141. guis[id].WindowState = FormWindowState.Normal;
  142. }
  143. break;
  144. #endregion
  145. #region Misc.
  146. case Keyword_Submit:
  147. {
  148. if (!Keyword_NoHide.Equals(Param2, StringComparison.OrdinalIgnoreCase))
  149. guis[id].Hide();
  150. // TODO: way to return multipart variable (A_LastResult?) for Gui,Submit and GuiControlGet,,Pos
  151. }
  152. break;
  153. case Keyword_Cancel:
  154. case Keyword_Hide:
  155. guis[id].Hide();
  156. break;
  157. case Keyword_Destroy:
  158. guis[id].Hide();
  159. guis[id].Dispose();
  160. guis.Remove(id);
  161. break;
  162. case Keyword_Font:
  163. GuiAssociatedInfo(guis[id]).Font = string.IsNullOrEmpty(Param2) && string.IsNullOrEmpty(Param3) ?
  164. guis[id].Font : ParseFont(GuiAssociatedInfo(guis[id]).Font, Param2, Param3);
  165. break;
  166. case Keyword_Color:
  167. Color c;
  168. if(Keyword_Default.Equals(Param2, StringComparison.OrdinalIgnoreCase))
  169. c = Color.LightGray; //TODO: Use correctly Control color, BUT NOT Transparent.
  170. else
  171. c = ParseColor(Param2);
  172. if(c.A == 0xFF)
  173. guis[id].BackColor = c;
  174. break;
  175. case Keyword_Margin:
  176. {
  177. int d, x = guis[id].Margin.Left, y = guis[id].Margin.Top;
  178. if (int.TryParse(Param2, out d))
  179. x = d;
  180. if (int.TryParse(Param3, out d))
  181. y = d;
  182. guis[id].Margin = new Padding(x, y, x, y);
  183. }
  184. break;
  185. case Keyword_Menu:
  186. break;
  187. case Keyword_Minimize:
  188. guis[id].WindowState = FormWindowState.Minimized;
  189. break;
  190. case Keyword_Maximize:
  191. guis[id].WindowState = FormWindowState.Maximized;
  192. break;
  193. case Keyword_Restore:
  194. guis[id].WindowState = FormWindowState.Normal;
  195. break;
  196. case Keyword_Flash:
  197. if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  198. WindowsAPI.FlashWindow(guis[id].Handle, OnOff(Param2) ?? true);
  199. break;
  200. case Keyword_Default:
  201. DefaultGuiId = id;
  202. break;
  203. case Keyword_TreeView:
  204. {
  205. var tree = GuiFindControl(Param2, guis[id]);
  206. if (tree == null || !typeof(TreeView).IsAssignableFrom(tree.GetType()))
  207. DefaultTreeView = null;
  208. else
  209. DefaultTreeView = (TreeView)tree;
  210. }
  211. break;
  212. case Keyword_ListView:
  213. {
  214. var list = GuiFindControl(Param2, guis[id]);
  215. if (list == null || !typeof(ListView).IsAssignableFrom(list.GetType()))
  216. DefaultListView = null;
  217. else
  218. DefaultListView = (ListView)list;
  219. }
  220. break;
  221. #endregion
  222. #region Options
  223. default:
  224. {
  225. foreach (var option in ParseOptions(Command))
  226. {
  227. bool on = option[0] != '-';
  228. string mode = option;
  229. if (mode[0] == '+' || mode[0] == '-')
  230. mode = mode.Substring(1);
  231. if (mode.Length == 0)
  232. continue;
  233. mode = mode.ToLowerInvariant();
  234. switch (mode)
  235. {
  236. case Keyword_AlwaysOnTop: guis[id].TopMost = on; break;
  237. case Keyword_Border: break;
  238. case Keyword_Caption: break;
  239. case Keyword_Disabled: guis[id].Enabled = !on; break;
  240. case Keyword_LastFound: lastFoundForm = guis[id].Handle.ToInt64(); break;
  241. case Keyword_LastFoundExist: lastFoundForm = guis[id].Handle.ToInt64(); break;
  242. case Keyword_MaximizeBox: guis[id].MaximizeBox = on; break;
  243. case Keyword_MinimizeBox: guis[id].MinimizeBox = on; break;
  244. case Keyword_OwnDialogs: dialogOwner = guis[id]; break;
  245. case Keyword_Owner: break;
  246. case Keyword_Resize: break;
  247. case Keyword_SysMenu: guis[id].ControlBox = on; break;
  248. case Keyword_Theme: Application.EnableVisualStyles(); break;
  249. case Keyword_ToolWindow: break;
  250. case Keyword_Redraw: guis[id].Refresh(); break;
  251. case Keyword_Cursor:
  252. {
  253. switch (Param2.ToLowerInvariant())
  254. {
  255. case "cross": guis[id].Cursor = Cursors.Cross; break;
  256. case "hand": guis[id].Cursor = Cursors.Hand; break;
  257. case "help": guis[id].Cursor = Cursors.Help; break;
  258. case "beam": guis[id].Cursor = Cursors.IBeam; break;
  259. case "no": guis[id].Cursor = Cursors.No; break;
  260. case "wait": guis[id].Cursor = Cursors.WaitCursor; break;
  261. case "nomove": guis[id].Cursor = Cursors.NoMove2D; break;
  262. case "size": guis[id].Cursor = Cursors.SizeAll; break;
  263. case "split":
  264. {
  265. if (Param3.ToLowerInvariant() == Keyword_Vertical)
  266. guis[id].Cursor = Cursors.VSplit;
  267. else
  268. guis[id].Cursor = Cursors.HSplit;
  269. break;
  270. }
  271. case "pan":
  272. {
  273. switch (Param3.ToLowerInvariant())
  274. {
  275. case "east":
  276. case "e": guis[id].Cursor = Cursors.PanEast; break;
  277. case "south":
  278. case "s": guis[id].Cursor = Cursors.PanSouth; break;
  279. case "west":
  280. case "w": guis[id].Cursor = Cursors.PanWest; break;
  281. default:
  282. case "north":
  283. case "n": guis[id].Cursor = Cursors.PanNorth; break;
  284. }
  285. break;
  286. }
  287. case "arrow":
  288. default: guis[id].Cursor = Cursors.Arrow; break;
  289. }
  290. break;
  291. }
  292. case Keyword_Icon:
  293. {
  294. if (Param2 != string.Empty)
  295. {
  296. if (File.Exists(Param2))
  297. if (Path.GetExtension(Param2.ToLowerInvariant())==".ico")
  298. guis[id].Icon = new Icon(Param2);
  299. }
  300. guis[id].ShowIcon = on;
  301. break;
  302. }
  303. case Keyword_TaskBar:
  304. {
  305. guis[id].ShowInTaskbar = on;
  306. break;
  307. }
  308. case Keyword_BackGroundImage:
  309. {
  310. if (on)
  311. {
  312. if (File.Exists(Param2))
  313. {
  314. guis[id].BackgroundImage = new Bitmap(Param2);
  315. switch (Param3.ToLowerInvariant())
  316. {
  317. case Keyword_None: guis[id].BackgroundImageLayout = ImageLayout.None; break;
  318. case Keyword_Tile: guis[id].BackgroundImageLayout = ImageLayout.Tile; break;
  319. case Keyword_Center: guis[id].BackgroundImageLayout = ImageLayout.Center; break;
  320. case Keyword_Zoom: guis[id].BackgroundImageLayout = ImageLayout.Zoom; break;
  321. case Keyword_Stretch:
  322. default: guis[id].BackgroundImageLayout = ImageLayout.Stretch; break;
  323. }
  324. }
  325. }
  326. else
  327. {
  328. guis[id].BackgroundImage = null;
  329. }
  330. break;
  331. }
  332. default:
  333. string arg;
  334. string[] parts;
  335. int n;
  336. Size size;
  337. if (mode.StartsWith(Keyword_Delimiter))
  338. {
  339. arg = mode.Substring(Keyword_Delimiter.Length);
  340. if (arg.Length > 0)
  341. GuiAssociatedInfo(guis[id]).Delimiter = arg[0];
  342. }
  343. else if (mode.StartsWith(Keyword_Label))
  344. {
  345. arg = mode.Substring(Keyword_Label.Length);
  346. if (arg.Length > 0)
  347. guis[id].Name = arg;
  348. }
  349. else if (mode.StartsWith(Keyword_MinSize))
  350. {
  351. arg = mode.Substring(Keyword_MinSize.Length);
  352. parts = arg.Split(new[] { "x", "X", "*" }, 2, StringSplitOptions.None);
  353. size = guis[id].MinimumSize;
  354. if (parts.Length > 0 && int.TryParse(parts[0], out n))
  355. size.Width = n;
  356. if (parts.Length > 1 && int.TryParse(parts[1], out n))
  357. size.Height = n;
  358. guis[id].MinimumSize = size;
  359. }
  360. else if (mode.StartsWith(Keyword_MaxSize))
  361. {
  362. arg = mode.Substring(Keyword_MaxSize.Length);
  363. parts = arg.Split(new[] { "x", "X", "*" }, 2, StringSplitOptions.None);
  364. size = guis[id].MaximumSize;
  365. if (parts.Length > 0 && int.TryParse(parts[0], out n))
  366. size.Width = n;
  367. if (parts.Length > 1 && int.TryParse(parts[1], out n))
  368. size.Height = n;
  369. guis[id].MaximumSize = size;
  370. }
  371. break;
  372. }
  373. }
  374. }
  375. break;
  376. #endregion
  377. }
  378. }
  379. #region Helpers
  380. static void GuiControlEdit(ref Control control, Form parent, string type, string options, string content)
  381. {
  382. string opts = null;
  383. switch (type.ToLowerInvariant())
  384. {
  385. #region Text
  386. case Keyword_Text:
  387. {
  388. var text = (Label)(control ?? new Label());
  389. parent.Controls.Add(text);
  390. control = text;
  391. text.Text = content;
  392. }
  393. break;
  394. #endregion
  395. #region Edit
  396. case Keyword_Edit:
  397. {
  398. var edit = (TextBox)(control ?? new TextBox());
  399. parent.Controls.Add(edit);
  400. control = edit;
  401. edit.Text = NormaliseEol(content);
  402. edit.Tag = options;
  403. opts = GuiApplyStyles(edit, options);
  404. foreach (var opt in ParseOptions(opts))
  405. {
  406. bool on = opt[0] != '-';
  407. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  408. switch (mode)
  409. {
  410. case Keyword_Limit:
  411. if (!on)
  412. edit.MaxLength = int.MaxValue;
  413. break;
  414. case Keyword_Lowercase: edit.CharacterCasing = on ? CharacterCasing.Lower : CharacterCasing.Normal; break;
  415. case Keyword_Multi: edit.Multiline = on; break;
  416. case Keyword_Number:
  417. {
  418. if (on)
  419. edit.KeyPress += OnEditKeyPress;
  420. else
  421. edit.KeyPress -= OnEditKeyPress;
  422. break;
  423. }
  424. case Keyword_Password: edit.PasswordChar = '●'; break;
  425. case Keyword_Readonly: edit.ReadOnly = on; break;
  426. case Keyword_Uppercase: edit.CharacterCasing = on ? CharacterCasing.Upper : CharacterCasing.Normal; break;
  427. case Keyword_WantCtrlA: break; //I dont see that the normal ctrl+A works! perhaps new implementation?
  428. case Keyword_WantReturn: edit.AcceptsReturn = on; break;
  429. case Keyword_WantTab: edit.AcceptsTab = on; break;
  430. case Keyword_Wrap: edit.WordWrap = on; break;
  431. default:
  432. int n;
  433. if (mode.StartsWith(Keyword_Limit) && int.TryParse(mode.Substring(Keyword_Limit.Length), out n))
  434. edit.MaxLength = n;
  435. break;
  436. }
  437. }
  438. }
  439. break;
  440. #endregion
  441. #region UpDown
  442. case Keyword_UpDown:
  443. {
  444. var updown = (NumericUpDown)(control ?? new NumericUpDown());
  445. var last = GuiAssociatedInfo(parent).LastControl;
  446. if (last != null && last is TextBox)
  447. {
  448. updown.Location = last.Location;
  449. updown.Size = last.Size;
  450. updown.Font = last.Font;
  451. updown.ForeColor = last.ForeColor;
  452. last.Parent.Controls.Remove(last);
  453. GuiAssociatedInfo(parent).Controls.Pop();
  454. options = string.Concat(last.Tag as string ?? string.Empty, " ", options);
  455. }
  456. parent.Controls.Add(updown);
  457. control = updown;
  458. updown.Value = decimal.Parse(content);
  459. opts = GuiApplyStyles(updown, options);
  460. foreach (var opt in ParseOptions(opts))
  461. {
  462. bool on = opt[0] != '-';
  463. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  464. switch (mode)
  465. {
  466. case Keyword_Horz: break;
  467. case Keyword_Left: break;
  468. case Keyword_Wrap: break;
  469. case "16": break;
  470. case "0x80": break;
  471. default:
  472. if (mode.StartsWith(Keyword_Range))
  473. {
  474. string[] range = mode.Substring(Keyword_Range.Length).Split(new[] { "-" }, 2, StringSplitOptions.None);
  475. decimal n;
  476. if (decimal.TryParse(range[0], out n))
  477. updown.Minimum = n;
  478. if (range.Length > 1 && decimal.TryParse(range[1], out n))
  479. updown.Maximum = n;
  480. }
  481. break;
  482. }
  483. }
  484. }
  485. break;
  486. #endregion
  487. #region Picture
  488. case Keyword_Picture:
  489. case Keyword_Pic:
  490. {
  491. var pic = (PictureBox)(control ?? new PictureBox());
  492. parent.Controls.Add(pic);
  493. control = pic;
  494. bool exists = File.Exists(content);
  495. if (exists)
  496. {
  497. pic.ImageLocation = content;
  498. try
  499. {
  500. var image = Image.FromFile(pic.ImageLocation);
  501. pic.Size = image.Size;
  502. }
  503. catch (Exception) { }
  504. }
  505. GuiApplyStyles(pic, options);
  506. }
  507. break;
  508. #endregion
  509. #region Button
  510. case Keyword_Button:
  511. {
  512. var button = (Button)(control ?? new Button());
  513. parent.Controls.Add(button);
  514. control = button;
  515. button.Text = content;
  516. }
  517. break;
  518. #endregion
  519. #region CheckBox
  520. case Keyword_CheckBox:
  521. {
  522. var check = (CheckBox)(control ?? new CheckBox());
  523. parent.Controls.Add(check);
  524. control = check;
  525. check.Text = content;
  526. opts = GuiApplyStyles(check, options);
  527. foreach (var opt in ParseOptions(opts))
  528. {
  529. switch (opt.ToLowerInvariant())
  530. {
  531. case Keyword_Check3:
  532. case Keyword_CheckedGray:
  533. check.CheckState = CheckState.Indeterminate;
  534. break;
  535. case Keyword_Checked:
  536. check.CheckState = CheckState.Checked;
  537. break;
  538. default:
  539. if (opt.StartsWith(Keyword_Checked, StringComparison.OrdinalIgnoreCase))
  540. {
  541. string arg = opt.Substring(Keyword_Checked.Length).Trim();
  542. int n;
  543. if (int.TryParse(arg, out n))
  544. check.CheckState = n == -1 ? CheckState.Indeterminate : n == 1 ? CheckState.Checked : CheckState.Unchecked;
  545. }
  546. break;
  547. }
  548. }
  549. }
  550. break;
  551. #endregion
  552. #region Radio
  553. case Keyword_Radio:
  554. {
  555. var radio = (RadioButton)(control ?? new RadioButton());
  556. parent.Controls.Add(radio);
  557. control = radio;
  558. radio.Text = content;
  559. radio.Checked = false;
  560. opts = GuiApplyStyles(radio, options);
  561. foreach (var opt in ParseOptions(opts))
  562. {
  563. switch (opt.ToLowerInvariant())
  564. {
  565. case Keyword_Checked:
  566. radio.Checked = true;
  567. break;
  568. default:
  569. if (opt.StartsWith(Keyword_Checked, StringComparison.OrdinalIgnoreCase))
  570. {
  571. string arg = opt.Substring(Keyword_Checked.Length).Trim();
  572. int n;
  573. if (int.TryParse(arg, out n))
  574. radio.Checked = n == 1;
  575. }
  576. break;
  577. }
  578. }
  579. }
  580. break;
  581. #endregion
  582. #region DropDownList
  583. case Keyword_DropDownList:
  584. case Keyword_DDL:
  585. {
  586. var ddl = (ComboBox)(control ?? new ComboBox());
  587. parent.Controls.Add(ddl);
  588. control = ddl;
  589. ddl.Text = content;
  590. ddl.DropDownStyle = ComboBoxStyle.DropDownList;
  591. opts = GuiApplyStyles(ddl, options);
  592. int select;
  593. bool clear;
  594. ddl.Items.AddRange(GuiParseList(ddl, out select, out clear));
  595. if (select > -1 && select < ddl.Items.Count)
  596. ddl.SelectedIndex = select;
  597. foreach (var opt in ParseOptions(opts))
  598. {
  599. bool on = opt[0] != '-';
  600. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  601. switch (mode)
  602. {
  603. case Keyword_Sort: ddl.Sorted = on; break;
  604. case Keyword_Uppercase: ddl.Text = ddl.Text.ToUpperInvariant(); break;
  605. case Keyword_Lowercase: ddl.Text = ddl.Text.ToLowerInvariant(); break;
  606. default:
  607. if (mode.StartsWith(Keyword_Choose, StringComparison.OrdinalIgnoreCase))
  608. {
  609. mode = mode.Substring(Keyword_Choose.Length);
  610. int n;
  611. if (int.TryParse(mode, out n) && n > -1 && n < ddl.Items.Count)
  612. ddl.SelectedIndex = n;
  613. }
  614. break;
  615. }
  616. }
  617. }
  618. break;
  619. #endregion
  620. #region ComboBox
  621. case Keyword_ComboBox:
  622. {
  623. var combo = (ComboBox)(control ?? new ComboBox());
  624. parent.Controls.Add(combo);
  625. control = combo;
  626. combo.Text = content;
  627. opts = GuiApplyStyles(combo, options);
  628. int select;
  629. bool clear;
  630. combo.Items.AddRange(GuiParseList(combo, out select, out clear));
  631. if (select > -1 && select < combo.Items.Count)
  632. combo.SelectedIndex = select;
  633. foreach (var opt in ParseOptions(opts))
  634. {
  635. bool on = opt[0] != '-';
  636. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  637. switch (mode)
  638. {
  639. case Keyword_Limit: break;
  640. case Keyword_Simple: break;
  641. }
  642. }
  643. }
  644. break;
  645. #endregion
  646. #region ListBox
  647. case Keyword_ListBox:
  648. {
  649. var listbox = new ListBox();
  650. parent.Controls.Add(listbox);
  651. control = listbox;
  652. listbox.Text = content;
  653. opts = GuiApplyStyles(listbox, options);
  654. int select;
  655. bool clear;
  656. listbox.Items.AddRange(GuiParseList(listbox, out select, out clear));
  657. if (select > -1 && select < listbox.Items.Count)
  658. listbox.SelectedIndex = select;
  659. bool multi = false, read = false;
  660. foreach (var opt in ParseOptions(opts))
  661. {
  662. bool on = opt[0] != '-';
  663. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  664. switch (mode)
  665. {
  666. case Keyword_Multi:
  667. case "8":
  668. multi = on;
  669. break;
  670. case Keyword_Readonly: read = on; break;
  671. case Keyword_Sort: listbox.Sorted = on; break;
  672. default:
  673. if (mode.StartsWith(Keyword_Choose, StringComparison.OrdinalIgnoreCase))
  674. {
  675. mode = mode.Substring(Keyword_Choose.Length);
  676. int n;
  677. if (int.TryParse(mode, out n) && n > -1 && n < listbox.Items.Count)
  678. listbox.SelectedIndex = n;
  679. }
  680. break;
  681. }
  682. }
  683. listbox.SelectionMode = multi ? SelectionMode.MultiExtended : read ? SelectionMode.None : SelectionMode.One;
  684. }
  685. break;
  686. #endregion
  687. #region ListView
  688. case Keyword_ListView:
  689. {
  690. var lv = (ListView)(control ?? new ListView());
  691. parent.Controls.Add(lv);
  692. control = lv;
  693. lv.Text = content;
  694. opts = GuiApplyStyles(lv, options);
  695. lv.View = View.Details;
  696. int select;
  697. bool clear;
  698. foreach (var item in GuiParseList(lv, out select, out clear))
  699. lv.Columns.Add(new ColumnHeader { Text = item });
  700. foreach (var opt in ParseOptions(opts))
  701. {
  702. bool on = opt[0] != '-';
  703. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  704. switch (mode)
  705. {
  706. case Keyword_Checked: lv.CheckBoxes = on; break;
  707. case Keyword_Grid: lv.GridLines = on; break;
  708. case Keyword_Hdr: break;
  709. case "lv0x10": break;
  710. case "lv0x20": break;
  711. case Keyword_Multi: lv.MultiSelect = on; break;
  712. case Keyword_NoSortHdr: break;
  713. case Keyword_Readonly: break;
  714. case Keyword_Sort: lv.Sorting = on ? SortOrder.Ascending : SortOrder.None; break;
  715. case Keyword_SortDesc: lv.Sorting = on ? SortOrder.Descending : SortOrder.None; break;
  716. case Keyword_WantF2: break;
  717. }
  718. }
  719. }
  720. break;
  721. #endregion
  722. #region TreeView
  723. case Keyword_TreeView:
  724. {
  725. var tree = (TreeView)(control ?? new TreeView());
  726. parent.Controls.Add(tree);
  727. control = tree;
  728. opts = GuiApplyStyles(tree, options);
  729. foreach (var opt in ParseOptions(opts))
  730. {
  731. bool on = opt[0] != '-';
  732. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  733. switch (mode)
  734. {
  735. case Keyword_Buttons: break;
  736. case Keyword_HScroll: break;
  737. case Keyword_Lines: break;
  738. case Keyword_Readonly: break;
  739. case Keyword_WantF2: break;
  740. default:
  741. if (mode.StartsWith(Keyword_ImageList))
  742. {
  743. mode = mode.Substring(Keyword_ImageList.Length);
  744. // UNDONE: TreeView control ImageList
  745. }
  746. break;
  747. }
  748. }
  749. }
  750. break;
  751. #endregion
  752. #region Hotkey
  753. case Keyword_Hotkey:
  754. {
  755. var hotkey = (HotkeyBox)(control ?? new HotkeyBox());
  756. parent.Controls.Add(hotkey);
  757. control = hotkey;
  758. opts = GuiApplyStyles(hotkey, options);
  759. foreach (var opt in ParseOptions(opts))
  760. {
  761. bool on = opt[0] != '-';
  762. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  763. switch (mode)
  764. {
  765. case Keyword_Limit:
  766. if (!on)
  767. hotkey.Limit = HotkeyBox.Limits.None;
  768. else
  769. {
  770. int n;
  771. if (int.TryParse(mode, out n))
  772. hotkey.Limit = (HotkeyBox.Limits)n;
  773. }
  774. break;
  775. }
  776. }
  777. }
  778. break;
  779. #endregion
  780. #region DateTime
  781. case Keyword_DateTime:
  782. {
  783. var date = (DateTimePicker)(control ?? new DateTimePicker());
  784. parent.Controls.Add(date);
  785. control = date;
  786. opts = GuiApplyStyles(date, options);
  787. if (content == string.Empty)
  788. date.Value = DateTime.Now;
  789. else
  790. date.Value = ToDateTime(content);
  791. date.Format = DateTimePickerFormat.Short;
  792. foreach (var opt in ParseOptions(opts))
  793. {
  794. bool on = opt[0] != '-';
  795. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  796. switch (mode)
  797. {
  798. case "1": date.ShowUpDown = on; break;
  799. case "2": date.ShowCheckBox = on; break;
  800. case Keyword_Right: date.DropDownAlign = LeftRightAlignment.Right; break; //***Bug*** - case dont match!
  801. case Keyword_LongDate: date.Format = DateTimePickerFormat.Long; date.Value = DateTime.Now; break;
  802. case Keyword_Time: date.Format = DateTimePickerFormat.Time; date.Value = DateTime.Now; break;
  803. default:
  804. if (mode.StartsWith(Keyword_Range))
  805. {
  806. string[] range = mode.Substring(Keyword_Range.Length).Split(new[] { "-" }, 2, StringSplitOptions.None);
  807. if (range[0].Length != 0)
  808. date.MinDate = ToDateTime(range[0]);
  809. if (range.Length > 0 && range[1].Length != 0)
  810. date.MaxDate = ToDateTime(range[1]);
  811. }
  812. else if (mode.StartsWith(Keyword_Choose))
  813. {
  814. mode = mode.Substring(Keyword_Choose.Length);
  815. if (mode.Length != 0)
  816. date.Value = ToDateTime(mode);
  817. }
  818. break;
  819. }
  820. }
  821. }
  822. break;
  823. #endregion
  824. #region MonthCal
  825. case Keyword_MonthCal:
  826. {
  827. var cal = (MonthCalendar)(control ?? new MonthCalendar());
  828. parent.Controls.Add(cal);
  829. control = cal;
  830. opts = GuiApplyStyles(cal, options);
  831. if (!string.IsNullOrEmpty(content))
  832. cal.SetDate(ToDateTime(content));
  833. foreach (var opt in ParseOptions(opts))
  834. {
  835. bool on = opt[0] != '-';
  836. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  837. switch (mode)
  838. {
  839. case "4": cal.ShowWeekNumbers = on; break;
  840. case "8": cal.ShowTodayCircle = on; break;
  841. case "16": cal.ShowToday = on; break;
  842. case Keyword_Multi: cal.MaxSelectionCount = int.MaxValue; break;
  843. default:
  844. if (mode.StartsWith(Keyword_Range, StringComparison.OrdinalIgnoreCase))
  845. {
  846. string[] range = mode.Substring(Keyword_Range.Length).Split(new[] { "-" }, 2, StringSplitOptions.None);
  847. if (!string.IsNullOrEmpty(range[0]))
  848. cal.MinDate = ToDateTime(range[0]);
  849. if (range.Length > 1 && !string.IsNullOrEmpty(range[1]))
  850. cal.MaxDate = ToDateTime(range[1]);
  851. }
  852. break;
  853. }
  854. }
  855. }
  856. break;
  857. #endregion
  858. #region Slider
  859. case Keyword_Slider:
  860. {
  861. var slider = (TrackBar)(control ?? new TrackBar());
  862. parent.Controls.Add(slider);
  863. control = slider;
  864. opts = GuiApplyStyles(slider, options);
  865. int n;
  866. foreach (var opt in ParseOptions(opts))
  867. {
  868. bool on = opt[0] != '-';
  869. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  870. switch (mode)
  871. {
  872. // UNDONE: misc slider properties
  873. case Keyword_Center: break;
  874. case Keyword_Invert: break;
  875. case Keyword_Left: break;
  876. case Keyword_NoTicks: slider.TickStyle = TickStyle.None; break;
  877. case Keyword_Thick: break;
  878. case Keyword_Vertical: slider.Orientation = Orientation.Vertical; break;
  879. default:
  880. if (mode.StartsWith(Keyword_Line))
  881. {
  882. mode = mode.Substring(Keyword_Line.Length);
  883. // UNDONE: slider line property
  884. }
  885. else if (mode.StartsWith(Keyword_Page))
  886. {
  887. mode = mode.Substring(Keyword_Page.Length);
  888. // UNDONE: slider page property
  889. }
  890. else if (mode.StartsWith(Keyword_Range))
  891. {
  892. mode = mode.Substring(Keyword_Range.Length);
  893. string[] parts = mode.Split(new[] { "-" }, 2, StringSplitOptions.None);
  894. if (parts[0].Length != 0 && int.TryParse(parts[0], out n))
  895. slider.Minimum = n;
  896. if (parts.Length > 0 && parts[1].Length != 0 && int.TryParse(parts[1], out n))
  897. slider.Maximum = n;
  898. }
  899. else if (mode.StartsWith(Keyword_TickInterval))
  900. {
  901. mode = mode.Substring(Keyword_TickInterval.Length);
  902. if (mode.Length != 0 && int.TryParse(mode, out n))
  903. slider.TickFrequency = n;
  904. }
  905. else if (mode.StartsWith(Keyword_ToolTip))
  906. {
  907. mode = mode.Substring(Keyword_ToolTip.Length);
  908. switch (mode) // UNDONE: slider tooltip alignment
  909. {
  910. case Keyword_Left: break;
  911. case Keyword_Right: break;
  912. case Keyword_Top: break;
  913. case Keyword_Bottom: break;
  914. }
  915. }
  916. break;
  917. }
  918. }
  919. if (!string.IsNullOrEmpty(content) && int.TryParse(content, out n))
  920. slider.Value = Math.Max(slider.Minimum, Math.Min(slider.Maximum, n));
  921. }
  922. break;
  923. #endregion
  924. #region Progress
  925. case Keyword_Progress:
  926. {
  927. var progress = (ProgressBar)(control ?? new ProgressBar());
  928. parent.Controls.Add(progress);
  929. control = progress;
  930. opts = GuiApplyStyles(progress, options);
  931. foreach (var opt in ParseOptions(opts))
  932. {
  933. bool on = opt[0] != '-';
  934. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  935. switch (mode)
  936. {
  937. case Keyword_Smooth: break;
  938. case Keyword_Vertical: break; // TODO: vertical progress bar Gui control
  939. default:
  940. if (mode.StartsWith(Keyword_Range))
  941. {
  942. mode = mode.Substring(Keyword_Range.Length);
  943. int z = mode.IndexOf('-');
  944. string a = mode, b;
  945. if (z == -1)
  946. b = string.Empty;
  947. else
  948. {
  949. a = mode.Substring(0, z);
  950. z++;
  951. b = z == mode.Length ? string.Empty : mode.Substring(z);
  952. }
  953. int x, y;
  954. if (int.TryParse(a, out x))
  955. progress.Minimum = x;
  956. if (int.TryParse(b, out y) && y > x)
  957. progress.Maximum = y;
  958. }
  959. else if (mode.StartsWith(Keyword_Background))
  960. {
  961. mode = mode.Substring(Keyword_Background.Length);
  962. progress.ForeColor = ParseColor(mode);
  963. }
  964. break;
  965. }
  966. }
  967. int n;
  968. if (!string.IsNullOrEmpty(content) && int.TryParse(content, out n))
  969. progress.Value = Math.Max(progress.Minimum, Math.Min(progress.Maximum, n));
  970. }
  971. break;
  972. #endregion
  973. #region GroupBox
  974. case Keyword_GroupBox:
  975. {
  976. var group = (GroupBox)(control ?? new GroupBox());
  977. parent.Controls.Add(group);
  978. control = group;
  979. group.Text = content;
  980. }
  981. break;
  982. #endregion
  983. #region Tab
  984. case Keyword_Tab:
  985. case Keyword_Tab2:
  986. {
  987. var tab = (TabControl)(control ?? new TabControl());
  988. parent.Controls.Add(tab);
  989. control = tab;
  990. opts = GuiApplyStyles(tab, options);
  991. foreach (var opt in ParseOptions(opts))
  992. {
  993. bool on = opt[0] != '-';
  994. string mode = opt.Substring(!on || opt[0] == '+' ? 1 : 0).ToLowerInvariant();
  995. switch (mode)
  996. {
  997. case Keyword_Background: break;
  998. case Keyword_Buttons: break;
  999. case Keyword_Top: tab.Alignment = TabAlignment.Top; break;
  1000. case Keyword_Left: tab.Alignment = TabAlignment.Left; break;
  1001. case Keyword_Right: tab.Alignment = TabAlignment.Right; break;
  1002. case Keyword_Bottom: tab.Alignment = TabAlignment.Bottom; break;
  1003. case Keyword_Wrap: break;
  1004. default:
  1005. if (mode.StartsWith(Keyword_Choose, StringComparison.OrdinalIgnoreCase))
  1006. {
  1007. mode = mode.Substring(Keyword_Choose.Length);
  1008. int n;
  1009. if (int.TryParse(mode, out n) && n > -1 && n < tab.TabPages.Count)
  1010. tab.SelectedIndex = n;
  1011. }
  1012. break;
  1013. }
  1014. }
  1015. }
  1016. break;
  1017. #endregion
  1018. #region StatusBar
  1019. case Keyword_StatusBar:
  1020. {
  1021. var info = GuiAssociatedInfo(parent);
  1022. if (info.StatusBar != null)
  1023. {
  1024. opts = string.Empty;
  1025. break;
  1026. }
  1027. var status = (StatusBar)(control ?? new StatusBar());
  1028. parent.Controls.Add(status);
  1029. control = status;
  1030. info.StatusBar = status;
  1031. status.Text = content;
  1032. }
  1033. break;
  1034. #endregion
  1035. #region WebBrowser
  1036. case Keyword_WebBrowser:
  1037. {
  1038. var web = (WebBrowser)(control ?? new WebBrowser());
  1039. parent.Controls.Add(web);
  1040. control = web;
  1041. web.Navigate(content);
  1042. }
  1043. break;
  1044. #endregion
  1045. }
  1046. if (opts == null)
  1047. GuiApplyStyles(control, options);
  1048. if (control is Button)
  1049. {
  1050. var button = (Button)control;
  1051. control.BackColor = Color.Transparent;
  1052. if ((button.Tag as bool?) != true)
  1053. button.Click += delegate { SafeInvoke(Keyword_GuiButton + content); };
  1054. else
  1055. button.Tag = null;
  1056. }
  1057. }
  1058. static string[] GuiParseList(Control control, out int select, out bool clear)
  1059. {
  1060. select = 0;
  1061. clear = false;
  1062. if (string.IsNullOrEmpty(control.Text))
  1063. return new string[] { };
  1064. var split = GuiAssociatedInfo(control).Delimiter;
  1065. clear = control.Text.IndexOf(split) == 0;
  1066. string text = control.Text.Substring(clear ? 1 : 0);
  1067. var items = text.Split(split);
  1068. var list = new List<string>();
  1069. for (int i = 0; i < items.Length; i++)
  1070. {
  1071. if (items[i].Length == 0)
  1072. select = i - 1;
  1073. else
  1074. list.Add(items[i]);
  1075. }
  1076. if (select == list.Count)
  1077. select--;
  1078. if (select < 0)
  1079. select = 0;
  1080. control.Text = string.Empty;
  1081. return list.ToArray();
  1082. }
  1083. static Form GuiCreateWindow(string name)
  1084. {
  1085. int n;
  1086. if (name == "1")
  1087. name = Keyword_GuiPrefix;
  1088. else if (name.Length < 3 && name.Length > 0 && int.TryParse(name, out n) && n > 0 && n < 99)
  1089. name += Keyword_GuiPrefix;
  1090. var win = new Form { Name = name, Tag = new GuiInfo { Delimiter = '|' }, KeyPreview = true };
  1091. GuiAssociatedInfo(win).Font = win.Font;
  1092. win.SuspendLayout();
  1093. int x = (int)Math.Round(win.Font.Size * 1.25), y = (int)Math.Round(win.Font.Size * .75);
  1094. win.Margin = new Padding(x, y, x, y);
  1095. win.FormClosed += delegate
  1096. {
  1097. SafeInvoke(win.Name + Keyword_GuiClose);
  1098. };
  1099. win.KeyDown += delegate(object sender, KeyEventArgs e)
  1100. {
  1101. if (e.KeyCode == Keys.Escape)
  1102. {
  1103. e.Handled = true;
  1104. SafeInvoke(win.Name + Keyword_GuiEscape);
  1105. }
  1106. };
  1107. win.Resize += delegate
  1108. {
  1109. SafeInvoke(win.Name + Keyword_GuiSize);
  1110. };
  1111. return win;
  1112. }
  1113. static string GuiApplyStyles(Control control, string styles)
  1114. {
  1115. bool first = control.Parent.Controls.Count == 1, dx = false, dy = false, sec = false;
  1116. if (first)
  1117. control.Location = new Point(control.Parent.Margin.Left, control.Parent.Margin.Top);
  1118. control.Font = GuiAssociatedInfo(control).Font;
  1119. #region Default sizing
  1120. float dw = control.Font.SizeInPoints * 15;
  1121. float w = 0;
  1122. int r = 0;
  1123. if (control is ComboBox || control is ListBox || control is HotkeyBox || control is TextBox)
  1124. w = dw;
  1125. else if (control is ListView || control is TreeView || control is DateTimePicker)
  1126. w = dw * 2;
  1127. else if (control is NumericUpDown)
  1128. w = dw;
  1129. else if (control is TrackBar)
  1130. w = dw;
  1131. else if (control is ProgressBar)
  1132. w = dw;
  1133. else if (control is GroupBox)
  1134. w = dw + 2 * control.Parent.Margin.Left;
  1135. else if (control is TabPage)
  1136. w = 2 * dw + 3 * control.Parent.Margin.Left;
  1137. if (control is ComboBox)
  1138. r = 2;
  1139. else if (control is ListBox)
  1140. r = 3;
  1141. else if (control is ListView || control is TreeView)
  1142. r = 5;
  1143. else if (control is GroupBox)
  1144. r = 2;
  1145. else if (control is TextBox)
  1146. r = ((TextBox)control).Multiline ? 3 : 1;
  1147. else if (control is DateTimePicker || control is HotkeyBox)
  1148. r = 1;
  1149. else if (control is TabPage)
  1150. r = 10;
  1151. control.Size = new Size(Math.Max((int)w, control.PreferredSize.Width), ++r > 2 ? (int)(r * control.Parent.Font.Height) : control.PreferredSize.Height);
  1152. #endregion
  1153. #region Options
  1154. string[] opts = ParseOptions(styles), excess = new string[opts.Length];
  1155. for (int i = 0; i < opts.Length; i++)
  1156. {
  1157. string mode = opts[i].ToLowerInvariant();
  1158. bool append = false;
  1159. bool on = mode[0] != '-';
  1160. if (!on || mode[0] == '+')
  1161. mode = mode.Substring(1);
  1162. if (mode.Length == 0)
  1163. continue;
  1164. string arg = mode.Substring(1);
  1165. int n;
  1166. switch (mode)
  1167. {
  1168. case Keyword_Left:
  1169. SafeSetProperty(control, "TextAlign", ContentAlignment.MiddleLeft);
  1170. break;
  1171. case Keyword_Center:
  1172. SafeSetProperty(control, "TextAlign", ContentAlignment.MiddleCenter);
  1173. break;
  1174. case Keyword_Right:
  1175. SafeSetProperty(control, "TextAlign", ContentAlignment.MiddleRight);
  1176. break;
  1177. case Keyword_AltSubmit:
  1178. break;
  1179. case Keyword_Background:
  1180. break;
  1181. case Keyword_Border:
  1182. SafeSetProperty(control, "BorderStyle", on ? BorderStyle.FixedSingle : BorderStyle.None);
  1183. break;
  1184. case Keyword_Enabled:
  1185. control.Enabled = on;
  1186. break;
  1187. case Keyword_Disabled:
  1188. control.Enabled = !on;
  1189. break;
  1190. case Keyword_HScroll:
  1191. break;
  1192. case Keyword_VScroll:
  1193. break;
  1194. case Keyword_TabStop:
  1195. control.TabStop = on;
  1196. break;
  1197. case Keyword_Theme:
  1198. break;
  1199. case Keyword_Transparent:
  1200. control.BackColor = Color.Transparent;
  1201. break;
  1202. case Keyword_Visible:
  1203. case Keyword_Vis:
  1204. control.Visible = on;
  1205. break;
  1206. case Keyword_Wrap:
  1207. break;
  1208. case Keyword_Section:
  1209. sec = true;
  1210. break;
  1211. default:
  1212. switch (mode[0])
  1213. {
  1214. case 'x':
  1215. dx = true;
  1216. dy = dy || (mode[1] == 'm' || mode[1] == 'M');
  1217. goto case 'h';
  1218. case 'y':
  1219. dy = true;
  1220. dx = dx || (mode[1] == 'm' || mode[1] == 'M');
  1221. goto case 'h';
  1222. case 'w':
  1223. case 'h':
  1224. GuiControlMove(mode, control);
  1225. break;
  1226. case 'r':
  1227. if (int.TryParse(arg, out n))
  1228. {
  1229. if (control.Parent != null && control.Parent.Font != null)
  1230. {
  1231. var h = (int)(n * control.Parent.Font.GetHeight());
  1232. if (control is GroupBox)
  1233. h += control.ClientSize.Height;
  1234. control.Size = new Size(control.Size.Width, h);
  1235. }
  1236. if (control is TextBox)
  1237. ((TextBox)control).Multiline = true;
  1238. }
  1239. else
  1240. append = true;
  1241. break;
  1242. case 'c':
  1243. if (arg.Length != 0 &&
  1244. !mode.StartsWith(Keyword_Check, StringComparison.OrdinalIgnoreCase) &&
  1245. !mode.StartsWith(Keyword_Choose, StringComparison.OrdinalIgnoreCase))
  1246. control.ForeColor = ParseColor(arg);
  1247. else
  1248. append = true;
  1249. break;
  1250. case 'v':
  1251. control.Name = arg;
  1252. break;
  1253. case 'g':
  1254. if (control is Button)
  1255. control.Tag = true;
  1256. control.Click += delegate { SafeInvoke(arg); };
  1257. break;
  1258. default:
  1259. append = true;
  1260. break;
  1261. }
  1262. break;
  1263. }
  1264. if (append)
  1265. excess[i] = opts[i];
  1266. }
  1267. #endregion
  1268. #region Secondary controls
  1269. if (!first)
  1270. {
  1271. var last = GuiAssociatedInfo(control).LastControl;
  1272. if (last is MonthCalendar && !last.Parent.Visible) // strange bug
  1273. {
  1274. last.Parent.Show();
  1275. last.Parent.Hide();
  1276. }
  1277. var loc = new Point(last.Location.X + last.Size.Width + last.Margin.Right + control.Margin.Left,
  1278. last.Location.Y + last.Size.Height + last.Margin.Bottom + control.Margin.Top);
  1279. if (!dx && !dy)
  1280. control.Location = new Point(last.Location.X, loc.Y);
  1281. else if (!dy)
  1282. control.Location = new Point(control.Location.X, loc.Y);
  1283. else if (!dx)
  1284. control.Location = new Point(loc.X, control.Location.Y);
  1285. }
  1286. #endregion
  1287. if (sec)
  1288. GuiAssociatedInfo(control).Section = control.Location;
  1289. control.BringToFront();
  1290. return string.Join(Keyword_Spaces[1].ToString(), excess).Trim();
  1291. }
  1292. static GuiInfo GuiAssociatedInfo(Control control)
  1293. {
  1294. return control.FindForm().Tag as GuiInfo;
  1295. }
  1296. static void GuiControlMove(string mode, Control control)
  1297. {
  1298. if (mode.Length < 2)
  1299. return;
  1300. bool alt = false, offset = false;
  1301. string arg;
  1302. int d;
  1303. switch (mode[0])
  1304. {
  1305. case 'x':
  1306. case 'X':
  1307. {
  1308. offset = true;
  1309. int p = 0;
  1310. switch (mode[1])
  1311. {
  1312. case 's':
  1313. case 'S':
  1314. {
  1315. var sec = GuiAssociatedInfo(control).Section;
  1316. var last = sec.IsEmpty ? new Point(control.Parent.Margin.Left, control.Parent.Margin.Top) : sec;
  1317. p = alt ? last.Y : last.X;
  1318. }
  1319. break;
  1320. case 'm':
  1321. case 'M':
  1322. {
  1323. p = alt ? control.Parent.Margin.Top : control.Parent.Margin.Left;
  1324. var all = control.FindForm().Controls;
  1325. if (all.Count == 0)
  1326. break;
  1327. int x = 0, y = 0;
  1328. int px = 0, py = 0;
  1329. foreach (Control ctrl in all)
  1330. {
  1331. if (ctrl == control)
  1332. continue;
  1333. x = Math.Max(x, ctrl.Location.X + ctrl.Width);
  1334. y = Math.Max(y, ctrl.Location.Y + ctrl.Height);
  1335. px = ctrl.Margin.Right;
  1336. py = ctrl.Margin.Bottom;
  1337. }
  1338. px += control.Margin.Left;
  1339. py += control.Margin.Top;
  1340. // don't know why this is necessary:
  1341. px *= 2;
  1342. py *= 2;
  1343. control.Location = alt ? new Point(px + x, control.Location.Y) : new Point(control.Location.X, py + y);
  1344. }
  1345. break;
  1346. case 'p':
  1347. case 'P':
  1348. {
  1349. Control last = null;
  1350. try {
  1351. last = GuiAssociatedInfo(control).LastControl;
  1352. } catch(InvalidOperationException) { }
  1353. if (last == null)
  1354. return;
  1355. var s = last.Location;
  1356. p = alt ? s.Y : s.X;
  1357. }
  1358. break;
  1359. case '+':
  1360. {
  1361. Control last = null;
  1362. try {
  1363. last = GuiAssociatedInfo(control).LastControl;
  1364. } catch(InvalidOperationException) { }
  1365. if(last == null)
  1366. return;
  1367. p = alt ? last.Location.Y + last.Size.Height : last.Location.X + last.Size.Width;
  1368. }
  1369. break;
  1370. default:
  1371. offset = false;
  1372. break;
  1373. }
  1374. arg = mode.Substring(offset ? 2 : 1);
  1375. if (!int.TryParse(arg, out d))
  1376. d = 0;
  1377. d += p;
  1378. if (alt)
  1379. control.Location = new Point(control.Location.X, d);
  1380. else
  1381. control.Location = new Point(d, control.Location.Y);
  1382. }
  1383. break;
  1384. case 'y':
  1385. case 'Y':
  1386. alt = true;
  1387. goto case 'x';
  1388. case 'w':
  1389. case 'W':
  1390. {
  1391. offset = mode[1] == 'p' || mode[1] == 'P';
  1392. arg = mode.Substring(offset ? 2 : 1);
  1393. if (arg.Length == 0)
  1394. d = 0;
  1395. else if (!int.TryParse(arg, out d))
  1396. return;
  1397. if (offset)
  1398. {
  1399. Control last = null;
  1400. try {
  1401. last = GuiAssociatedInfo(control).LastControl;
  1402. } catch(InvalidOperationException) { }
  1403. if (last == null)
  1404. return;
  1405. var s = last.Size;
  1406. d += alt ? s.Height : s.Width;
  1407. }
  1408. if (alt)
  1409. control.Size = new Size(control.Size.Width, d);
  1410. else
  1411. control.Size = new Size(d, control.Size.Height);
  1412. }
  1413. break;
  1414. case 'h':
  1415. case 'H':
  1416. if (control is TextBox)
  1417. ((TextBox)control).Multiline = true;
  1418. alt = true;
  1419. goto case 'w';
  1420. }
  1421. }
  1422. static Control GuiFindControl(string name)
  1423. {
  1424. return GuiFindControl(name, DefaultGui);
  1425. }
  1426. static Control GuiFindControl(string name, Form gui)
  1427. {
  1428. if (gui == null)
  1429. return null;
  1430. foreach (Control control in gui.Controls)
  1431. if (control.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  1432. return control;
  1433. return null;
  1434. }
  1435. static void GuiControlAsync(Control ctrl, string cmd, string arg)
  1436. {
  1437. cmd = cmd.ToLowerInvariant();
  1438. switch (cmd)
  1439. {
  1440. case Keyword_Text:
  1441. if (ctrl is TextBox)
  1442. arg = NormaliseEol(arg);
  1443. ctrl.Text = arg;
  1444. break;
  1445. case "":
  1446. {
  1447. if (ctrl is ProgressBar || ctrl is TrackBar || ctrl is NumericUpDown)
  1448. {
  1449. int argAsInt;
  1450. if (int.TryParse(arg, out argAsInt))
  1451. SafeSetProperty(ctrl, "Value", argAsInt);
  1452. }
  1453. else
  1454. {
  1455. if (ctrl is DateTimePicker)
  1456. {
  1457. DateTime argAsDateTime = ToDateTime(arg);
  1458. if (!(string.IsNullOrEmpty(arg)))
  1459. SafeSetProperty(ctrl, "Value", argAsDateTime);
  1460. else
  1461. SafeSetProperty(ctrl, "Value", DateTime.Now);
  1462. }
  1463. else
  1464. {
  1465. if (ctrl is ComboBox)
  1466. {
  1467. if (SubStr(arg, 1, 1) == "|")
  1468. ((ComboBox)ctrl).Items.Clear(); arg = SubStr(arg, 2, StrLen(arg) - 1);
  1469. string[] argAsStringArray = arg.Split('|');
  1470. foreach (string s in argAsStringArray)
  1471. ((ComboBox)ctrl).Items.Add(s);
  1472. }
  1473. else
  1474. {
  1475. if (ctrl is PictureBox)
  1476. {
  1477. if (File.Exists(arg))
  1478. {
  1479. try
  1480. {
  1481. SafeSetProperty(ctrl, "ImageLocation", arg);
  1482. }
  1483. catch(Exception){ }
  1484. }
  1485. }
  1486. else
  1487. {
  1488. if (ctrl is WebBrowser)
  1489. {
  1490. ((WebBrowser)ctrl).Navigate(arg);
  1491. }
  1492. else
  1493. {
  1494. if (ctrl is ListBox)
  1495. {
  1496. if (SubStr(arg, 1, 1) == "|")
  1497. ((ListBox)ctrl).Items.Clear(); arg = SubStr(arg, 2, StrLen(arg) - 1);
  1498. string[] argAsStringArray = arg.Split('|');
  1499. foreach (string s in argAsStringArray)
  1500. ((ListBox)ctrl).Items.Add(s);
  1501. }
  1502. else
  1503. {
  1504. arg = NormaliseEol(arg);
  1505. SafeSetProperty(ctrl, "Text", arg);
  1506. }
  1507. }
  1508. }
  1509. }
  1510. }
  1511. }
  1512. }
  1513. break;
  1514. case Keyword_Move:
  1515. case Keyword_MoveDraw:
  1516. GuiControlMove(arg, ctrl);
  1517. break;
  1518. case Keyword_Focus:
  1519. ctrl.Focus();
  1520. break;
  1521. case Keyword_Enable:
  1522. ctrl.Enabled = true;
  1523. break;
  1524. case Keyword_Disable:
  1525. ctrl.Enabled = false;
  1526. break;
  1527. case Keyword_Hide:
  1528. ctrl.Visible = false;
  1529. break;
  1530. case Keyword_Show:
  1531. ctrl.Visible = true;
  1532. break;
  1533. case Keyword_Delete:
  1534. ctrl.Parent.Controls.Remove(ctrl);
  1535. ctrl.Dispose();
  1536. break;
  1537. case Keyword_Choose:
  1538. // UNDONE: choose item for gui control
  1539. break;
  1540. case Keyword_Font:
  1541. // TODO: change control font
  1542. break;
  1543. default:
  1544. int n;
  1545. if (cmd.StartsWith(Keyword_Enable) && int.TryParse(cmd.Substring(Keyword_Enable.Length), out n) && (n == 1 || n == 0))
  1546. ctrl.Enabled = n == 1;
  1547. if (cmd.StartsWith(Keyword_Disable) && int.TryParse(cmd.Substring(Keyword_Disable.Length), out n) && (n == 1 || n == 0))
  1548. ctrl.Enabled = n == 0;
  1549. GuiApplyExtendedStyles(ctrl, arg);
  1550. break;
  1551. }
  1552. }
  1553. static Control GuiControlGetFocused(Control parent)
  1554. {
  1555. foreach (Control child in parent.Controls)
  1556. {
  1557. if (child.Focused)
  1558. return child;
  1559. else if (child.Controls.Count != 0)
  1560. {
  1561. var item = GuiControlGetFocused(child);
  1562. if (item != null)
  1563. return item;
  1564. }
  1565. }
  1566. return null;
  1567. }
  1568. #endregion
  1569. /// <summary>
  1570. /// Makes a variety of changes to a control in a GUI window.
  1571. /// </summary>
  1572. /// <param name="Command"></param>
  1573. /// <param name="ControlID"></param>
  1574. /// <param name="Param3"></param>
  1575. public static void GuiControl(string Command, string ControlID, string Param3)
  1576. {
  1577. var ctrl = GuiFindControl(ControlID);
  1578. if (ctrl == null)
  1579. return;
  1580. if (!ctrl.Created)
  1581. {
  1582. var vis = ctrl.Parent.Visible;
  1583. ctrl.Parent.Show();
  1584. if (!vis)
  1585. ctrl.Parent.Hide();
  1586. }
  1587. ctrl.Invoke((SimpleDelegate)delegate { GuiControlAsync(ctrl, Command, Param3); });
  1588. }
  1589. static void GuiApplyExtendedStyles(Control control, string options)
  1590. {
  1591. }
  1592. /// <summary>
  1593. /// Retrieves various types of information about a control in a GUI window.
  1594. /// </summary>
  1595. /// <param name="result"></param>
  1596. /// <param name="command"></param>
  1597. /// <param name="control"></param>
  1598. /// <param name="option"></param>
  1599. public static void GuiControlGet(out object result, string command, string control, string option)
  1600. {
  1601. result = string.Empty;
  1602. var id = GuiId(ref command);
  1603. var gui = guis.ContainsKey(id) ? guis[id] : DefaultGui;
  1604. var ctrl = GuiFindControl(control, gui);
  1605. command = command.ToLowerInvariant();
  1606. switch (command)
  1607. {
  1608. case Keyword_Focus:
  1609. case Keyword_Focus + "v":
  1610. var focued = GuiControlGetFocused(gui);
  1611. if (Environment.OSVersion.Platform == PlatformID.Win32NT && command == Keyword_Focus)
  1612. result = WindowsAPI.GetClassName(focued.Handle);
  1613. else
  1614. result = focued.Name;
  1615. return;
  1616. }
  1617. if (ctrl == null)
  1618. return;
  1619. switch (command)
  1620. {
  1621. case Keyword_Text:
  1622. result = ctrl.Text;
  1623. break;
  1624. case "":
  1625. {
  1626. if(ctrl is ProgressBar) {
  1627. result = ((ProgressBar)ctrl).Value.ToString();
  1628. } else if(ctrl is ComboBox) {
  1629. result = ((ComboBox)ctrl).SelectedText;
  1630. } else if(ctrl is TrackBar) {
  1631. result = ((TrackBar)ctrl).Value.ToString();
  1632. } else if(ctrl is ListBox) {
  1633. result = ((ListBox)ctrl).SelectedItems.ToString();
  1634. } else if(ctrl is DateTimePicker) {
  1635. result = ((DateTimePicker)ctrl).Value.ToString();
  1636. } else if(ctrl is MonthCalendar) {
  1637. //TODO: MonthCalender Get!
  1638. } else if(ctrl is WebBrowser) {
  1639. result = ((WebBrowser)ctrl).Url.ToString();
  1640. } else if(ctrl is PictureBox) {
  1641. result = ((PictureBox)ctrl).ImageLocation;
  1642. } else if(ctrl is NumericUpDown) {
  1643. result = ((NumericUpDown)ctrl).Value.ToString();
  1644. } else {
  1645. result = ctrl.Text;
  1646. }
  1647. break;
  1648. }
  1649. case Keyword_Pos:
  1650. {
  1651. var loc = new Dictionary<string, object>();
  1652. loc.Add("x", ctrl.Location.X);
  1653. loc.Add("y", ctrl.Location.Y);
  1654. loc.Add("w", ctrl.Size.Width);
  1655. loc.Add("h", ctrl.Size.Height);
  1656. result = loc;
  1657. }
  1658. break;
  1659. case Keyword_Enabled:
  1660. result = ctrl.Enabled ? 1 : 0;
  1661. break;
  1662. case Keyword_Visible:
  1663. result = ctrl.Visible ? 1 : 0;
  1664. break;
  1665. case Keyword_Hwnd:
  1666. result = ctrl.Handle.ToInt64().ToString();
  1667. break;
  1668. }
  1669. }
  1670. #region Eventhandler
  1671. private static void OnEditKeyPress(object sender, KeyPressEventArgs e)
  1672. {
  1673. if (!(char.IsDigit(e.KeyChar) || char.IsNumber(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == ',' || (int)e.KeyChar == 8 || (int)e.KeyChar == 58 || (int)e.KeyChar == 59))
  1674. {
  1675. e.Handled = true;
  1676. }
  1677. }
  1678. #endregion
  1679. }
  1680. }