MainForm.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. using EsPy.Dialogs;
  2. using EsPy.Forms;
  3. using EsPy.Units;
  4. using EsPy.Utility;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Runtime.InteropServices;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. using WeifenLuo.WinFormsUI.Docking;
  17. namespace EsPy
  18. {
  19. public partial class MainForm : Form, IDeviceChange
  20. {
  21. [DllImport("User32.dll")]
  22. protected static extern int SetClipboardViewer(int hWndNewViewer);
  23. [DllImport("User32.dll", CharSet = CharSet.Auto)]
  24. public static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
  25. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  26. public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
  27. IntPtr ClipboardViewer;
  28. FileSystemWatcher FileSystemWatcher = null;
  29. private DeserializeDockContent deserializeDockContent;
  30. public TerminalForm TerminalForm = null;
  31. //public ErrorListForm ErrorListForm = null;
  32. public MainForm()
  33. {
  34. InitializeComponent();
  35. Globals.MainForm = this;
  36. //Globals.PyClientStart();
  37. AutoScaleMode = AutoScaleMode.Dpi;
  38. this.Text = Application.ProductName + " " + Application.ProductVersion;
  39. this.SetSchema(this, null);
  40. ClipboardViewer = (IntPtr)SetClipboardViewer((int)this.Handle);
  41. this.FileSystemWatcher = new FileSystemWatcher();
  42. this.FileSystemWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
  43. | NotifyFilters.FileName | NotifyFilters.DirectoryName;
  44. //this.FileSystemWatcher.Filter = "*.py";
  45. //// Add event handlers.
  46. //this.FileSystemWatcher.Changed += FileSystemWatcher_Changed;
  47. //this.FileSystemWatcher.Created += FileSystemWatcher_Changed;
  48. //this.FileSystemWatcher.Deleted += FileSystemWatcher_Changed;
  49. //this.FileSystemWatcher.Renamed += FileSystemWatcher_Changed;
  50. //// Begin watching.
  51. //this.FileSystemWatcher.EnableRaisingEvents = true;
  52. this.AllowDrop = true;
  53. this.DragEnter += MainForm_DragEnter;
  54. this.DragDrop += MainForm_DragDrop;
  55. this.DragOver += MainForm_DragOver;
  56. }
  57. private void MainForm_DragOver(object sender, DragEventArgs e)
  58. {
  59. }
  60. private void MainForm_DragDrop(object sender, DragEventArgs e)
  61. {
  62. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  63. {
  64. object data = e.Data.GetData(DataFormats.FileDrop);
  65. if (data != null)
  66. {
  67. string[] files = (string[])data;
  68. foreach (string file in files)
  69. {
  70. IDocument doc = this.OpenFromFile(file, EditorForm.EditorFileFormats);
  71. if (doc != null)
  72. {
  73. (doc as DockContent).Show(this.dockPanel1);
  74. }
  75. }
  76. }
  77. }
  78. else if (e.Data.GetDataPresent(DataFormats.Text))
  79. {
  80. if (e.Effect == DragDropEffects.Copy)
  81. {
  82. }
  83. else if (e.Effect == DragDropEffects.Move)
  84. {
  85. }
  86. }
  87. }
  88. private void MainForm_DragEnter(object sender, DragEventArgs e)
  89. {
  90. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  91. {
  92. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  93. foreach (string file in files)
  94. {
  95. if (EditorForm.EditorFileFormats.Find(Path.GetExtension(file)) != null)
  96. e.Effect = DragDropEffects.All;
  97. else e.Effect = DragDropEffects.None;
  98. }
  99. return;
  100. }
  101. //else if (e.Data.GetDataPresent(DataFormats.Text))
  102. //{
  103. // e.Effect = DragDropEffects.All;
  104. //}
  105. else
  106. {
  107. e.Effect = DragDropEffects.None;
  108. }
  109. }
  110. private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
  111. {
  112. foreach (IDocument doc in Application.OpenForms)
  113. {
  114. if (doc is IDocument && (doc as IDocument).FileName == e.FullPath)
  115. {
  116. if (e.ChangeType == WatcherChangeTypes.Changed)
  117. {
  118. if (MessageBox.Show($"{e.FullPath}\r\n\r\nhas been modified by another program!\r\nWould you like reload it?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
  119. {
  120. (doc as IDocument).LoadFromFile(e.FullPath);
  121. }
  122. }
  123. }
  124. }
  125. }
  126. protected override void WndProc(ref System.Windows.Forms.Message m)
  127. {
  128. // defined in winuser.h
  129. const int WM_DRAWCLIPBOARD = 0x308;
  130. const int WM_CHANGECBCHAIN = 0x030D;
  131. const int WM_DEVICECHANGE = 0x219;
  132. switch (m.Msg)
  133. {
  134. case WM_DRAWCLIPBOARD:
  135. DisplayClipboardData();
  136. SendMessage(ClipboardViewer, m.Msg, m.WParam, m.LParam);
  137. break;
  138. case WM_CHANGECBCHAIN:
  139. if (m.WParam == ClipboardViewer)
  140. ClipboardViewer = m.LParam;
  141. else
  142. SendMessage(ClipboardViewer, m.Msg, m.WParam, m.LParam);
  143. break;
  144. case WM_DEVICECHANGE:
  145. foreach (Form form in Application.OpenForms)
  146. {
  147. if (form is IDeviceChange)
  148. {
  149. (form as IDeviceChange).UpdateDevices();
  150. }
  151. }
  152. this.UpdateDevices();
  153. break;
  154. default:
  155. base.WndProc(ref m);
  156. break;
  157. }
  158. }
  159. public void UpdateDevices()
  160. {
  161. this.mnDevice.HideDropDown();
  162. if (!this.ComportIsExists)
  163. {
  164. this.Port = null;
  165. this.btnConnect.Enabled =
  166. this.mnFileManager.Enabled =
  167. this.btnDisconnect.Enabled =
  168. this.btnReset.Enabled =
  169. this.btnFileManager.Enabled = false;
  170. }
  171. else
  172. {
  173. this.mnPorts.Enabled = true;
  174. this.btnConnect.Enabled = true;
  175. }
  176. this.mnPorts.Enabled = true;
  177. }
  178. private void DisplayClipboardData()
  179. {
  180. try
  181. {
  182. IDataObject iData = new DataObject();
  183. iData = System.Windows.Forms.Clipboard.GetDataObject();
  184. if (this.dockPanel1.ActiveDocument != null && this.dockPanel1.ActiveDocument is IDocument)
  185. {
  186. IDocument doc = this.dockPanel1.ActiveDocument as IDocument;
  187. doc.CanPaste = iData.GetDataPresent(DataFormats.Text); //&& doc.CanPaste;
  188. }
  189. }
  190. catch (Exception e)
  191. {
  192. MessageBox.Show(e.ToString());
  193. }
  194. }
  195. private string DockPanelConfigFile
  196. { get { return Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config"); } }
  197. private string CurretPortName
  198. {
  199. get { return Properties.Settings.Default.PortName; }
  200. set
  201. {
  202. Properties.Settings.Default.PortName = value ?? "";
  203. this.toolStripStatusLabel2.Text = value ?? "Unknown";
  204. }
  205. }
  206. private void MainForm_Load(object sender, EventArgs e)
  207. {
  208. //this.TerminalForm.PortOpen += TerminalForm_PortOpen;
  209. //this.TerminalForm.PortClose += TerminalForm_PortClose;
  210. string configFile = this.DockPanelConfigFile;
  211. if (!File.Exists(configFile))
  212. {
  213. }
  214. else
  215. {
  216. try
  217. {
  218. deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
  219. dockPanel1.LoadFromXml(configFile, this.deserializeDockContent);
  220. }
  221. catch (Exception ex)
  222. {
  223. Helpers.ErrorBox(ex.Message + "\r\n\r\n"+ "Plase restart the program!");
  224. this.TerminalForm.Close();
  225. this.Close();
  226. return;
  227. }
  228. }
  229. this.CurretPortName = Properties.Settings.Default.PortName;
  230. if (this.TerminalForm == null)
  231. {
  232. this.TerminalForm = new TerminalForm();
  233. this.TerminalForm.Show(this.dockPanel1);
  234. }
  235. //
  236. //if (!this.ComportIsExists)
  237. //{
  238. // this.btnConnect.Enabled = false;
  239. // MessageBox.Show($"Serialport \"{this.CurretPortName}\" does not exists!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  240. //}
  241. //else
  242. //{
  243. this.btnConnect.Enabled = true;
  244. //}
  245. this.UpdateUI();
  246. }
  247. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  248. {
  249. List<IDocument> docs = new List<IDocument>();
  250. List<string> files = new List<string>();
  251. foreach (DockContent dc in this.dockPanel1.Documents)
  252. {
  253. if (dc is IDocument)
  254. {
  255. IDocument d = dc as IDocument;
  256. if (d.Modified)
  257. {
  258. if(d.FileName == null || d.Modified)
  259. {
  260. docs.Add(d);
  261. files.Add(d.FileName ?? "New");
  262. }
  263. }
  264. }
  265. }
  266. if (docs.Count > 0)
  267. {
  268. FileSavesDialog d = new FileSavesDialog();
  269. d.listBox1.Items.AddRange(files.ToArray());
  270. DialogResult res = d.ShowDialog();
  271. if (res == DialogResult.Yes)
  272. {
  273. foreach (IDocument dd in docs)
  274. {
  275. dd.Save();
  276. }
  277. }
  278. else if (res == DialogResult.Cancel)
  279. {
  280. e.Cancel = true;
  281. return;
  282. }
  283. }
  284. string configFile = this.DockPanelConfigFile;
  285. dockPanel1.SaveAsXml(configFile);
  286. Properties.Settings.Default.Save();
  287. if (this.Port != null)
  288. {
  289. try
  290. {
  291. this.Port.Close();
  292. this.Port = null;
  293. }
  294. catch(Exception ex)
  295. {
  296. Helpers.ErrorBox(ex);
  297. }
  298. }
  299. //if (Globals.PyClient != null)
  300. // Globals.PyClient.Stop();
  301. }
  302. private bool CanConnect
  303. {
  304. get
  305. {
  306. return !String.IsNullOrEmpty(this.CurretPortName);
  307. }
  308. }
  309. private void UpdateUI()
  310. {
  311. this.mnTerminal.Enabled = this.TerminalForm == null;
  312. //this.JediState.Text = Globals.PyClient != null ? "Connected" : "Not connected";
  313. }
  314. private void SetSchema(object sender, System.EventArgs e)
  315. {
  316. if (true)
  317. {
  318. this.dockPanel1.Theme = this.vS2015BlueTheme1;
  319. this.EnableVSRenderer(VisualStudioToolStripExtender.VsVersion.Vs2008, vS2015BlueTheme1);
  320. }
  321. if (dockPanel1.Theme.ColorPalette != null)
  322. {
  323. statusStrip1.BackColor = dockPanel1.Theme.ColorPalette.MainWindowStatusBarDefault.Background;
  324. statusStrip1.ForeColor = dockPanel1.Theme.ColorPalette.MainWindowStatusBarDefault.HighlightText;
  325. }
  326. }
  327. private void EnableVSRenderer(VisualStudioToolStripExtender.VsVersion version, ThemeBase theme)
  328. {
  329. visualStudioToolStripExtender1.SetStyle(menuStrip1, version, theme);
  330. visualStudioToolStripExtender1.SetStyle(toolStrip1, version, theme);
  331. visualStudioToolStripExtender1.SetStyle(statusStrip1, version, theme);
  332. }
  333. private void mnTerminal_Click(object sender, EventArgs e)
  334. {
  335. if (this.TerminalForm == null)
  336. this.TerminalForm = new TerminalForm();
  337. this.TerminalForm.Show(this.dockPanel1);
  338. this.UpdateUI();
  339. }
  340. private IDockContent GetContentFromPersistString(string persistString)
  341. {
  342. string[] items = persistString.Split(',');
  343. persistString = items[0];
  344. if (persistString == typeof(TerminalForm).ToString())
  345. {
  346. if (this.TerminalForm != null)
  347. return this.TerminalForm;
  348. this.TerminalForm = new TerminalForm();
  349. this.TerminalForm.IsHidden = false;
  350. if(this.TerminalForm.DockState == DockState.Document)
  351. this.TerminalForm.DockState = DockState.DockRight;
  352. return this.TerminalForm;
  353. }
  354. //else if (persistString == typeof(ErrorListForm).ToString())
  355. //{
  356. // if (this.ErrorListForm != null)
  357. // return this.ErrorListForm;
  358. // this.ErrorListForm = new ErrorListForm();
  359. // this.ErrorListForm.IsHidden = false;
  360. // return this.ErrorListForm;
  361. //}
  362. else if (persistString == typeof(EditorForm).ToString())
  363. {
  364. if (items.Length == 3 && File.Exists(items[1]))
  365. {
  366. if (this.FindDocument(items[1]) == null)
  367. {
  368. EditorForm editor = new EditorForm(this);
  369. editor.LoadFromFile(items[1]);
  370. editor.scintilla.GotoPosition(int.Parse(items[2]));
  371. editor.scintilla.SetSavePoint();
  372. editor.IsHidden = false;
  373. editor.Focus();
  374. return editor;
  375. }
  376. return null;
  377. }
  378. else return null;
  379. }
  380. return null;
  381. }
  382. private void dockPanel1_ActiveContentChanged(object sender, EventArgs e)
  383. {
  384. ToolStripManager.RevertMerge(this.menuStrip1);
  385. ToolStripManager.RevertMerge(this.toolStrip1);
  386. if (this.dockPanel1.ActiveContent != null)
  387. {
  388. if (this.dockPanel1.ActiveContent is IForm)
  389. {
  390. IForm form = this.dockPanel1.ActiveContent as IForm;
  391. if (form.ToolStrip != null)
  392. ToolStripManager.Merge(form.ToolStrip, this.toolStrip1);
  393. if (form.MenuStrip != null)
  394. ToolStripManager.Merge(form.MenuStrip, this.menuStrip1);
  395. }
  396. }
  397. this.UpdateUI();
  398. }
  399. private void dockPanel1_ActiveDocumentChanged(object sender, EventArgs e)
  400. {
  401. if (this.dockPanel1.ActiveContent is IDocument)
  402. {
  403. IDocument doc = this.dockPanel1.ActiveDocument as IDocument;
  404. doc.CanPaste = Clipboard.ContainsText();
  405. this.FilePath.Text = doc.FileName;
  406. //doc.UpdateUI();
  407. }
  408. else
  409. {
  410. this.FilePath.Text = "";
  411. }
  412. this.UpdateUI();
  413. }
  414. public IDocument FindDocument(string fname)
  415. {
  416. foreach (DockContent dc in this.dockPanel1.Documents)
  417. {
  418. if (dc is IDocument)
  419. {
  420. if ((dc as IDocument).FileName == fname)
  421. return dc as IDocument;
  422. }
  423. }
  424. return null;
  425. }
  426. private void mnOpen_Click(object sender, EventArgs e)
  427. {
  428. FileFormats ff = EditorForm.EditorFileFormats;
  429. IDocument[] items = this.OpenFromFile(ff);
  430. if (items != null)
  431. {
  432. foreach (DockContent item in items)
  433. {
  434. item.Show(this.dockPanel1);
  435. }
  436. }
  437. }
  438. public IDocument OpenFromFile(string path, FileFormats file_formats)
  439. {
  440. string ext = Path.GetExtension(path).ToLower();
  441. foreach (FileFormat format in file_formats)
  442. {
  443. string[] items = format.Filter.Split('|')[1].Split(';');
  444. foreach (string item in items)
  445. {
  446. if (item.Remove(0, 1) == ext)
  447. {
  448. foreach (DockContent dc in this.dockPanel1.Documents)
  449. {
  450. if (dc is IDocument)
  451. {
  452. IDocument d = dc as IDocument;
  453. if (d.FileName == path)
  454. {
  455. if (MessageBox.Show($"{path} already exists!\r\n\r\nWould you like reload it?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
  456. {
  457. d.LoadFromFile(path);
  458. return d;
  459. }
  460. }
  461. }
  462. }
  463. IDocument doc = (IDocument)Activator.CreateInstance(format.EditorType, new object[] { this });
  464. doc.LoadFromFile(path);
  465. return doc;
  466. }
  467. }
  468. }
  469. return null;
  470. }
  471. public IDocument[] OpenFromFile(FileFormats file_formats)
  472. {
  473. List<IDocument> list = new List<IDocument>();
  474. OpenFileDialog d = new OpenFileDialog();
  475. d.DefaultExt = file_formats.DefaultExt;
  476. d.Filter = file_formats.Filters;
  477. //d.FilterIndex = file_formats.Count;
  478. d.Multiselect = true;
  479. if (d.ShowDialog() == DialogResult.OK)
  480. {
  481. foreach (string path in d.FileNames)
  482. {
  483. IDocument item = null;
  484. try
  485. {
  486. item = OpenFromFile(path, file_formats);
  487. if (item != null)
  488. {
  489. list.Add(item);
  490. }
  491. }
  492. catch(Exception ex)
  493. {
  494. if (item != null && item is IDisposable)
  495. {
  496. (item as IDisposable).Dispose();
  497. }
  498. item = null;
  499. }
  500. }
  501. return list.ToArray();
  502. }
  503. return null;
  504. }
  505. private void NewFile(FileFormat ff)
  506. {
  507. SaveFileDialog d = new SaveFileDialog();
  508. d.DefaultExt = ff.DefaultExt;
  509. d.Filter = ff.Filter;
  510. if (d.ShowDialog() == DialogResult.OK)
  511. {
  512. IDocument doc = this.FindDocument(d.FileName);
  513. if (doc != null)
  514. {
  515. }
  516. else
  517. {
  518. doc = (IDocument)Activator.CreateInstance(ff.EditorType, new object[] { this });
  519. }
  520. File.WriteAllText(d.FileName, "");
  521. doc.LoadFromFile(d.FileName);
  522. (doc as DockContent).Show(this.dockPanel1);
  523. }
  524. }
  525. private void mnNewPython_Click(object sender, EventArgs e)
  526. {
  527. NewFile(FileFormat.Python);
  528. }
  529. private void mnNewHtml_Click(object sender, EventArgs e)
  530. {
  531. NewFile(FileFormat.Html);
  532. }
  533. private void mnNewCss_Click(object sender, EventArgs e)
  534. {
  535. NewFile(FileFormat.Css);
  536. }
  537. private void mnNewJs_Click(object sender, EventArgs e)
  538. {
  539. NewFile(FileFormat.Js);
  540. }
  541. private void mnNewJson_Click(object sender, EventArgs e)
  542. {
  543. NewFile(FileFormat.Json);
  544. }
  545. private void mnNewTxt_Click(object sender, EventArgs e)
  546. {
  547. NewFile(FileFormat.Txt);
  548. }
  549. private void mnNewOther_Click(object sender, EventArgs e)
  550. {
  551. NewFile(FileFormat.All);
  552. }
  553. private PySerial FPort = null;
  554. public PySerial Port
  555. {
  556. get { return this.FPort; }
  557. private set
  558. {
  559. if (this.FPort != null)
  560. {
  561. // if(this.Port.IsOpen)
  562. this.Port.Close();
  563. this.FPort.PortOpen -= Port_PortOpen;
  564. this.FPort.PortClose -= Port_PortClose;
  565. this.FPort.DataReceived -= Port_DataReceived;
  566. this.FPort.ErrorReceived -= Port_ErrorReceived;
  567. this.FPort.PortBusy -= Port_PortBusy;
  568. this.FPort.PortFree -= Port_PortFree;
  569. foreach (DockContent dc in this.dockPanel1.Contents)
  570. {
  571. if (dc is IPort)
  572. {
  573. (dc as IPort).Port = null;
  574. }
  575. }
  576. this.FPort = null;
  577. }
  578. this.FPort = value;
  579. if (this.FPort != null)
  580. {
  581. if (this.FPort is SerialPort)
  582. {
  583. this.FPort.PortName = this.CurretPortName;
  584. this.FPort.PortOpen += Port_PortOpen;
  585. this.FPort.PortClose += Port_PortClose;
  586. this.FPort.DataReceived += Port_DataReceived;
  587. this.FPort.ErrorReceived += Port_ErrorReceived;
  588. this.FPort.PortBusy += Port_PortBusy;
  589. this.FPort.PortFree += Port_PortFree;
  590. }
  591. else
  592. {
  593. //Websocket
  594. }
  595. foreach (DockContent dc in this.dockPanel1.Contents)
  596. {
  597. if (dc is IPort)
  598. {
  599. (dc as IPort).Port = this.Port;
  600. }
  601. }
  602. }
  603. }
  604. }
  605. private Color ColorFree = Color.FromArgb(255, 0, 122, 204);
  606. private Color ColorBusy = Color.DarkOrange;
  607. private delegate void UpdateStatusEvent(bool busy);
  608. public void UpdateBusy(bool busy)
  609. {
  610. if (this.InvokeRequired)
  611. {
  612. this.Invoke(new UpdateStatusEvent(UpdateBusy), new object[] { busy });
  613. }
  614. else
  615. {
  616. if(busy)
  617. this.statusStrip1.BackColor = this.ColorBusy;
  618. else this.statusStrip1.BackColor = this.ColorFree;
  619. this.mnFileManager.Enabled =
  620. this.btnFileManager.Enabled = !busy;
  621. }
  622. }
  623. private void Port_PortFree(object sender, EventArgs e)
  624. {
  625. this.UpdateBusy(false);
  626. }
  627. private void Port_PortBusy(object sender, EventArgs e)
  628. {
  629. this.UpdateBusy(true);
  630. // this.UpdateBusy(false);
  631. }
  632. private void btnConnect_Click(object sender, EventArgs e)
  633. {
  634. if (!CanConnect)
  635. {
  636. Helpers.ErrorBox("Internal error at connecting!");
  637. return;
  638. }
  639. if (!this.ComportIsExists)
  640. {
  641. //this.btnConnect.Enabled = false;
  642. MessageBox.Show($"Serialport \"{this.CurretPortName}\" does not exists!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  643. return;
  644. }
  645. try
  646. {
  647. this.Port = new PySerial();
  648. this.Port.BaudRate = Properties.Settings.Default.PyBaudRate;
  649. this.Port.LineSeparator = "\r\n"; // Properties.Settings.Default.PyPortLineSeparator;
  650. this.Port.WriteTimeout = Properties.Settings.Default.PyPortWriteTimeout;
  651. this.Port.ReadTimeout = Properties.Settings.Default.PyPortReadTmeout;
  652. this.Port.Open();
  653. }
  654. catch (Exception ex)
  655. {
  656. this.Port = null;
  657. Helpers.ErrorBox(ex.Message);
  658. }
  659. }
  660. private void btnDisconnect_Click(object sender, EventArgs e)
  661. {
  662. if (this.Port != null)
  663. {
  664. try
  665. {
  666. this.Port.Close();
  667. }
  668. catch (Exception ex)
  669. {
  670. Helpers.ErrorBox(ex);
  671. }
  672. finally
  673. {
  674. this.Port = null;
  675. }
  676. }
  677. }
  678. private void Port_ErrorReceived(object sender, string data)
  679. {
  680. }
  681. private void Port_DataReceived(object sender, string data)
  682. {
  683. }
  684. private void Port_PortOpen(object sender, EventArgs e)
  685. {
  686. this.mnPorts.Enabled =
  687. this.btnConnect.Enabled = false;
  688. this.mnFileManager.Enabled =
  689. this.btnFileManager.Enabled =
  690. this.btnReset.Enabled =
  691. this.btnDisconnect.Enabled = true;
  692. }
  693. private void Port_PortClose(object sender, EventArgs e)
  694. {
  695. this.mnPorts.Enabled =
  696. this.btnConnect.Enabled = true;
  697. this.mnFileManager.Enabled =
  698. this.btnFileManager.Enabled =
  699. this.btnReset.Enabled =
  700. this.btnDisconnect.Enabled = false;
  701. this.statusStrip1.BackColor = this.ColorFree;
  702. }
  703. private void btnReset_Click(object sender, EventArgs e)
  704. {
  705. this.Port.SoftReset();
  706. }
  707. private void mnSave_Click(object sender, EventArgs e)
  708. {
  709. if (this.dockPanel1.ActiveContent is IDocument)
  710. {
  711. (this.dockPanel1.ActiveContent as IDocument).Save();
  712. }
  713. }
  714. private void mnSaveAs_Click(object sender, EventArgs e)
  715. {
  716. if (this.dockPanel1.ActiveContent is IDocument)
  717. {
  718. (this.dockPanel1.ActiveContent as IDocument).SaveAs();
  719. }
  720. }
  721. private void mnSaveAll_Click(object sender, EventArgs e)
  722. {
  723. foreach (DockContent dc in this.dockPanel1.Documents)
  724. {
  725. if (dc is IDocument)
  726. {
  727. IDocument d = dc as IDocument;
  728. if (d.Modified)
  729. {
  730. d.Save();
  731. }
  732. }
  733. }
  734. }
  735. private void mnDevice_DropDownOpening(object sender, EventArgs e)
  736. {
  737. foreach (ToolStripItem mi in this.mnPorts.DropDownItems)
  738. {
  739. if(mi is ToolStripMenuItem)
  740. mi.Click -= Port_Click;
  741. }
  742. mnPorts.DropDownItems.Clear();
  743. mnPorts.DropDownItems.Clear();
  744. string[] ports = System.IO.Ports.SerialPort.GetPortNames();
  745. if (ports != null && ports.Length > 0)
  746. {
  747. foreach (string port in ports)
  748. {
  749. ToolStripMenuItem mi = new ToolStripMenuItem();
  750. mi.Text = port;
  751. mi.Tag = port;
  752. mi.Click += Port_Click;
  753. if (this.CurretPortName != null && this.CurretPortName == port)
  754. {
  755. mi.Checked = true;
  756. }
  757. this.mnPorts.DropDownItems.Add(mi);
  758. }
  759. }
  760. else
  761. {
  762. ToolStripMenuItem mi = new ToolStripMenuItem();
  763. mi.Text = "empty";
  764. mi.Enabled = false;
  765. this.mnPorts.DropDownItems.Add(mi);
  766. }
  767. //this.mnEspTool.Enabled = this.Port == null && this.ComportIsExists;
  768. }
  769. private bool ComportIsExists
  770. {
  771. get
  772. {
  773. foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
  774. {
  775. if (this.CurretPortName == port)
  776. return true;
  777. }
  778. return false;
  779. }
  780. }
  781. private void Port_Click(object sender, EventArgs e)
  782. {
  783. this.CurretPortName = (sender as ToolStripMenuItem).Tag.ToString();
  784. this.btnConnect.Enabled = this.ComportIsExists;
  785. this.UpdateUI();
  786. }
  787. public void btnSaveAll_Click(object sender, EventArgs e)
  788. {
  789. foreach (DockContent dc in this.dockPanel1.Documents)
  790. {
  791. if (dc is IDocument)
  792. {
  793. IDocument doc = dc as IDocument;
  794. if (doc.CanSave && doc.Modified)
  795. {
  796. doc.Save();
  797. }
  798. }
  799. }
  800. }
  801. private void btnView_DropDownOpening(object sender, EventArgs e)
  802. {
  803. this.mnTerminal.Enabled = this.TerminalForm.DockState == DockState.Hidden;
  804. }
  805. private void mnViewHelp_Click(object sender, EventArgs e)
  806. {
  807. string p = Path.Combine(Application.StartupPath, "Helps", "help.html");
  808. if (File.Exists(p))
  809. System.Diagnostics.Process.Start(p);
  810. else MessageBox.Show("Help file does not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  811. }
  812. private void mnWebREPL_Click(object sender, EventArgs e)
  813. {
  814. string p = Path.Combine(Application.StartupPath, "Tools", "webrepl", "webrepl.html");
  815. if (File.Exists(p))
  816. System.Diagnostics.Process.Start(p);
  817. else MessageBox.Show("Help file does not exits!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  818. }
  819. private void btnFlashing_Click(object sender, EventArgs e)
  820. {
  821. if (this.ComportIsExists)
  822. {
  823. EspToolDialog d = new EspToolDialog();
  824. d.PortName = this.CurretPortName;
  825. d.ShowDialog();
  826. d.Dispose();
  827. }
  828. else
  829. {
  830. MessageBox.Show("Select a Port first!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  831. }
  832. }
  833. private void mnFileManager_Click(object sender, EventArgs e)
  834. {
  835. if (this.Port != null)
  836. {
  837. Globals.Terminal.Locked = true;
  838. this.Port.Sync(true);
  839. this.Port.Clean();
  840. PyFileManager d = new PyFileManager();
  841. d.Port = this.Port;
  842. d.ShowDialog();
  843. d.Dispose();
  844. this.Port.Clean();
  845. this.Port.Sync(false);
  846. Globals.Terminal.Locked = false;
  847. }
  848. }
  849. private void mnAbout_Click(object sender, EventArgs e)
  850. {
  851. About s = new About();
  852. s.ShowDialog();
  853. s.Dispose();
  854. //AboutDialog d = new AboutDialog();
  855. //d.ShowDialog();
  856. //d.Dispose();
  857. }
  858. //private void mnErrorList_Click(object sender, EventArgs e)
  859. //{
  860. // if (this.ErrorListForm != null)
  861. // this.ErrorListForm.Show(this.dockPanel1);
  862. //}
  863. private void MainForm_Deactivate(object sender, EventArgs e)
  864. {
  865. //UIChangedEventArgs ee = new UIChangedEventArgs();
  866. //ee.ChangedReson = UIChangedEventArgs.UIChangedReasons.FocusLost;
  867. //lock (this)
  868. //{
  869. // foreach (DockContent dc in this.dockPanel1.Contents)
  870. // {
  871. // if (dc is IForm)
  872. // {
  873. // (dc as IForm).UIStateChanged(this, ee);
  874. // }
  875. // }
  876. //}
  877. }
  878. private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
  879. {
  880. SettingsDialog d = new SettingsDialog();
  881. d.ShowDialog();
  882. d.Dispose();
  883. }
  884. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  885. {
  886. }
  887. private void ePS8266ToolStripMenuItem_Click(object sender, EventArgs e)
  888. {
  889. System.Diagnostics.Process.Start("http://docs.micropython.org/en/latest/esp8266/");
  890. }
  891. private void mnOpenFileFolder_Click(object sender, EventArgs e)
  892. {
  893. if (this.dockPanel1.ActiveDocument != null && this.dockPanel1.ActiveDocument is IDocument)
  894. {
  895. IDocument d = this.dockPanel1.ActiveDocument as IDocument;
  896. System.Diagnostics.Process.Start("explorer.exe", $"/select, \"{d.FileName}\"");
  897. }
  898. }
  899. private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
  900. {
  901. mnOpenFileFolder.Enabled = this.dockPanel1.ActiveDocument != null && this.dockPanel1.ActiveDocument is IDocument;
  902. }
  903. private void btnTerminalClear_Click(object sender, EventArgs e)
  904. {
  905. if (this.TerminalForm != null)
  906. {
  907. this.TerminalForm.scintilla.Clean();
  908. }
  909. }
  910. }
  911. }