TerminalForm.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using EsPy.Units;
  2. using EsPy.Utility;
  3. using ScintillaNET;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Drawing;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Xml;
  15. using WeifenLuo.WinFormsUI.Docking;
  16. namespace EsPy.Forms
  17. {
  18. public partial class TerminalForm : DockContent, IForm, IPort, IDisposable
  19. {
  20. public TerminalForm()
  21. {
  22. InitializeComponent();
  23. this.HideOnClose = true;
  24. this.DockAreas =
  25. DockAreas.Document |
  26. DockAreas.DockBottom |
  27. DockAreas.DockLeft |
  28. DockAreas.DockRight |
  29. DockAreas.DockTop;
  30. this.scintilla.ReadOnly = true;
  31. //this.scintilla.Preprocess += new Components.Terminal.PreprocessEvent(TerminalPreprocess);
  32. this.ViewEOL = Properties.Settings.Default.TerminalShowEol;
  33. this.ViewWhitespace = Properties.Settings.Default.TerminalShowWhitespace;
  34. this.scintilla.SetLang();
  35. }
  36. /// <summary>
  37. /// Clean up any resources being used.
  38. /// </summary>
  39. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  40. protected override void Dispose(bool disposing)
  41. {
  42. if (disposing && (components != null))
  43. {
  44. try
  45. {
  46. if (this.scintilla.HistoryModified)
  47. this.scintilla.SaveHistory(this.HistoryFile);
  48. this.Port = null;
  49. }
  50. catch
  51. { }
  52. components.Dispose();
  53. }
  54. base.Dispose(disposing);
  55. }
  56. public PySerial Port
  57. {
  58. get { return this.scintilla.Port; }
  59. set
  60. {
  61. if (this.scintilla.Port != null)
  62. {
  63. this.scintilla.Port.PortOpen -= Port_PortOpen;
  64. this.scintilla.Port.PortClose -= Port_PortClose;
  65. this.scintilla.Port.DataReceived -= Port_DataReceived;
  66. this.scintilla.Port.ErrorReceived -= Port_ErrorReceived;
  67. this.scintilla.Port.PortBusy -= Port_PortBusy;
  68. this.scintilla.Port.PortFree -= Port_PortFree;
  69. }
  70. if (value != null)
  71. {
  72. value.PortOpen += Port_PortOpen;
  73. value.PortClose += Port_PortClose;
  74. value.DataReceived += Port_DataReceived;
  75. value.ErrorReceived += Port_ErrorReceived;
  76. value.PortBusy += Port_PortBusy;
  77. value.PortFree += Port_PortFree;
  78. }
  79. this.scintilla.Port = value;
  80. }
  81. }
  82. private delegate void UpdateStatusEvent(bool busy);
  83. public void UpdateBusy(bool busy)
  84. {
  85. if (this.InvokeRequired)
  86. {
  87. this.Invoke(new UpdateStatusEvent(UpdateBusy), new object[] { busy });
  88. }
  89. else
  90. {
  91. }
  92. }
  93. private void Port_PortFree(object sender, EventArgs e)
  94. {
  95. //throw new NotImplementedException();
  96. }
  97. private void Port_PortBusy(object sender, EventArgs e)
  98. {
  99. //throw new NotImplementedException();
  100. }
  101. private void Port_ErrorReceived(object sender, string data)
  102. {
  103. }
  104. private void Port_DataReceived(object sender, string data)
  105. {
  106. }
  107. private void Port_PortClose(object sender, EventArgs e)
  108. {
  109. this.scintilla.Append("\nDisconnected.\r\n");
  110. this.scintilla.ReadOnly = true;
  111. }
  112. private void Port_PortOpen(object sender, EventArgs e)
  113. {
  114. this.scintilla.ReadOnly = false;
  115. this.scintilla.Append($"{this.Port.PortName} {this.Port.BaudRate } Connected...\r\n");
  116. this.scintilla.Append("Press CTRL + D or Soft Reset Button on the Toolbar\r\n");
  117. this.scintilla.Append("Press CTRL + C to interrupt current program.\r\n");
  118. }
  119. public ToolStrip ToolStrip
  120. {
  121. get
  122. {
  123. //return this.toolStrip1;
  124. return null;
  125. }
  126. }
  127. public ToolStrip MenuStrip
  128. { get { return this.menuStrip1; } }
  129. private void bntClean_Click(object sender, EventArgs e)
  130. {
  131. this.scintilla.Clean();
  132. }
  133. private string HistoryFile
  134. { get { return Path.Combine(Application.StartupPath, "history.txt"); } }
  135. private void TerminalForm_Load(object sender, EventArgs e)
  136. {
  137. this.scintilla.LoadHistory(this.HistoryFile);
  138. }
  139. private void UpdateUI()
  140. {
  141. this.scintilla.Margins[0].Width = this.scintilla.TextWidth(Style.LineNumber, " " + (this.scintilla.Lines.Count + 1).ToString());
  142. this.cmSoftReset.Enabled = this.Port != null && this.Port.IsOpen;
  143. this.mnClean.Enabled =
  144. this.cmClean.Enabled =
  145. this.mnSelectAll.Enabled =
  146. this.cmSelectAll.Enabled = this.scintilla.TextLength > 0;
  147. this.mnUndo.Enabled =
  148. this.cmUndo.Enabled = this.scintilla.CanUndo;
  149. this.mnRedo.Enabled =
  150. this.cmRedo.Enabled = this.scintilla.CanRedo;
  151. this.mnCut.Enabled =
  152. this.mnCopy.Enabled =
  153. this.mnDelete.Enabled =
  154. this.cmCut.Enabled =
  155. this.cmCopy.Enabled =
  156. this.cmDelete.Enabled = this.scintilla.SelectionEnd > this.scintilla.SelectionStart;
  157. }
  158. private void terminal_UpdateUI(object sender, UpdateUIEventArgs e)
  159. {
  160. this.UpdateUI();
  161. }
  162. private void cmSoftReset_Click(object sender, EventArgs e)
  163. {
  164. this.Port.SoftReset();
  165. }
  166. private void mnUndo_Click(object sender, EventArgs e)
  167. {
  168. this.scintilla.Undo();
  169. }
  170. private void mnRedo_Click(object sender, EventArgs e)
  171. {
  172. this.scintilla.Redo();
  173. }
  174. private void mnCut_Click(object sender, EventArgs e)
  175. {
  176. this.scintilla.Cut();
  177. }
  178. private void mnCopy_Click(object sender, EventArgs e)
  179. {
  180. this.scintilla.Copy();
  181. }
  182. private void mnPaste_Click(object sender, EventArgs e)
  183. {
  184. this.scintilla.Paste();
  185. }
  186. private void mnDelete_Click(object sender, EventArgs e)
  187. {
  188. this.scintilla.DeleteRange(this.scintilla.SelectionStart, this.scintilla.SelectionEnd);
  189. }
  190. private void mnSelectAll_Click(object sender, EventArgs e)
  191. {
  192. this.scintilla.SelectAll();
  193. }
  194. private void mnClean_Click(object sender, EventArgs e)
  195. {
  196. this.scintilla.Clean();
  197. }
  198. private void SendCommand(object sender, EventArgs e)
  199. {
  200. ToolStripMenuItem c = sender as ToolStripMenuItem;
  201. if (c != null && c.Tag != null)
  202. {
  203. string[] lines = c.Tag.ToString().Split(';');
  204. foreach (string line in lines)
  205. {
  206. this.Port.WriteLine(line);
  207. }
  208. }
  209. }
  210. public bool ViewEOL
  211. {
  212. get { return this.scintilla.ViewEol; }
  213. set
  214. {
  215. this.scintilla.ViewEol = value;
  216. Properties.Settings.Default.TerminalShowEol = value;
  217. this.mnShowEol.Checked = value;
  218. this.cmShowEOL.Checked = value;
  219. }
  220. }
  221. public bool ViewWhitespace
  222. {
  223. get { return this.scintilla.ViewWhitespace == WhitespaceMode.VisibleAlways; }
  224. set
  225. {
  226. if (value)
  227. this.scintilla.ViewWhitespace = WhitespaceMode.VisibleAlways;
  228. else this.scintilla.ViewWhitespace = WhitespaceMode.Invisible;
  229. Properties.Settings.Default.TerminalShowWhitespace = value;
  230. this.mnShowWhitespace.Checked = value;
  231. this.cmShowWhitespace.Checked = value;
  232. }
  233. }
  234. private void mnShowEol_Click(object sender, EventArgs e)
  235. {
  236. this.ViewEOL = !this.ViewEOL;
  237. }
  238. private void mnShowWhitespace_Click(object sender, EventArgs e)
  239. {
  240. this.ViewWhitespace = !this.ViewWhitespace;
  241. }
  242. private void TerminalForm_Activated(object sender, EventArgs e)
  243. {
  244. this.scintilla.Focus();
  245. }
  246. //public void UIStateChanged(object sender, UIChangedEventArgs e)
  247. //{
  248. // //throw new NotImplementedException();
  249. //}
  250. }
  251. }